API Request Execution
The execution of an ApiPlugin’s request, from its resolved ApiToolCall, and of a SqlPlugin’s query, from its resolved SqlToolCall.
ApiConnection.execute_query
resolves each request in the app, into an ApiToolCall: the full url, and the headers with the
connection’s Authorization already added from its Smarter Secret. The tool runner
(smarter.apps.llmtool.runner) executes it with execute_api_call(), which reads no
database, so the same call can run in the app’s process today, or in a worker without credentials
(ADR-038).
The request is made with smarter.apps.llmtool.plugin.safe_http.fetch(): plain http and any port
are permitted, but every url, the first and each redirect’s, must resolve to public addresses only,
except loopback hosts in the local environment. A redirect to another origin drops the
Authorization header.
SqlConnection.execute_query
resolves each query into a SqlToolCall: the database’s settings, with its password decrypted,
the SSH tunnel’s, if any, with its password and known host keys, and the final SQL. The tool runner
executes it with execute_sql_call(), which opens the tunnel, connects, runs the query and closes
both, without the app’s database.
- smarter.apps.connection.execution.API_MAX_RESPONSE_BYTES = 10000000
The largest api response that
execute_api_call()reads, in bytes.
- smarter.apps.connection.execution.API_TOOL_CALL_KIND: Final = 'api'
The
kindof an ApiPlugin’s call, which selectsexecute_api_call().
- class smarter.apps.connection.execution.ApiToolCall(**data)[source]
Bases:
ToolCallAn ApiPlugin’s request, resolved for execution.
Warning
headersholds the connection’s credential. It is excluded fromrepr(). Never log the call’smodel_dump().- Variables:
url – The full url: the connection’s base url joined with the plugin’s endpoint.
method – The HTTP method, upper case.
headers – The request headers, with the connection’s
Authorization.params – The query string parameters.
json_body – The JSON body, for POST, PUT and PATCH, or None.
timeout – Seconds to wait for each request.
limit – The number of items to keep from the response, or None for all of them.
- json_body: JsonValue
- model_config = {'extra': 'forbid', 'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- smarter.apps.connection.execution.SQL_TOOL_CALL_KIND: Final = 'sql'
The
kindof a SqlPlugin’s query, which selectsexecute_sql_call().
- smarter.apps.connection.execution.SUCCESS_STATUS_CODES = (200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299)
The status codes that
fetch()returns rather than raises.Only 200 is a successful query. The others are answered, but report the query as failed.
( 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, )
- class smarter.apps.connection.execution.SqlToolCall(**data)[source]
Bases:
ToolCallA SqlPlugin’s query, resolved for execution.
Warning
password, and the SSH tunnel’s, hold credentials. They are excluded fromrepr(). Never log the call’smodel_dump().- Variables:
engine – The Django database backend, e.g.
django.db.backends.mysql.database – The database’s name.
username – The database login.
password – Its password, decrypted.
host – The database’s host. With an SSH tunnel, as the SSH server reaches it.
port – The database’s port.
options – The driver’s options: timeouts, TLS, LDAP.
ssh – The SSH tunnel, or None.
sql – The final SQL, with its limit.
timeout – Seconds to wait for the SSH server.
- model_config = {'extra': 'forbid', 'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- ssh: SshTunnelSettings | None
- class smarter.apps.connection.execution.SshTunnelSettings(**data)[source]
Bases:
BaseModelThe SSH tunnel of a SqlConnection whose
authenticationMethodistcpip_ssh, resolved.See
smarter.apps.connection.ssh.- Variables:
host – The SSH server.
port – Its port.
username – The SSH login.
password – Its password, decrypted. Excluded from
repr().known_hosts – The SSH server’s host keys, in
known_hostsformat.
- model_config = {'extra': 'forbid', 'frozen': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- smarter.apps.connection.execution.database_settings(call, host, port)[source]
Return Django’s settings for a call’s database, at a host and port.
- smarter.apps.connection.execution.execute_api_call(call)[source]
Execute a resolved ApiPlugin request.
- Parameters:
call (
ToolCall) – The call, anApiToolCall.- Return type:
- Returns:
The api’s JSON, limited, with status 200; or a result with a
failure: when the url is refused or can’t be reached (nostatus_code), when the api answers with an error status, or with a 2xx other than 200, or when its answer isn’t JSON (with itsstatus_code).- Raises:
TypeError – If the call isn’t an
ApiToolCall.
- smarter.apps.connection.execution.execute_sql_call(call)[source]
Execute a resolved SqlPlugin query, through its SSH tunnel if it has one.
- Parameters:
call (
ToolCall) – The call, aSqlToolCall.- Return type:
- Returns:
The rows as JSON, a database error with
is_error, or afailurewhen the SSH tunnel can’t be opened or the database can’t be reached.- Raises:
TypeError – If the call isn’t a
SqlToolCall.
- smarter.apps.connection.execution.limit_response(response_data, limit)[source]
Limit an api’s JSON response to
limititems.A list keeps its first
limititems. A dict keeps every key, and each of its values that is a list keeps its firstlimititems. Anything else, or no limit, is returned as it is.
- smarter.apps.connection.execution.query_database(call, host, port)[source]
Connect to a call’s database at a host and port, run its query, and disconnect.
- Parameters:
call (
SqlToolCall) – The call.host (
str) – The host to connect to.port (
int) – Its port.
- Return type:
- Returns:
The rows as JSON; a result with
is_errorand the database’s message when the query fails; or a result with afailurewhen the database can’t be reached or refuses the login.