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 kind of an ApiPlugin’s call, which selects execute_api_call().

class smarter.apps.connection.execution.ApiToolCall(**data)[source]

Bases: ToolCall

An ApiPlugin’s request, resolved for execution.

Warning

headers holds the connection’s credential. It is excluded from repr(). Never log the call’s model_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.

headers: dict[str, str]
json_body: JsonValue
kind: Literal['api']
limit: int | None
method: str
model_config = {'extra': 'forbid', 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

params: dict[str, JsonValue]
timeout: float
url: str
smarter.apps.connection.execution.SQL_TOOL_CALL_KIND: Final = 'sql'

The kind of a SqlPlugin’s query, which selects execute_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: ToolCall

A SqlPlugin’s query, resolved for execution.

Warning

password, and the SSH tunnel’s, hold credentials. They are excluded from repr(). Never log the call’s model_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.

database: str
engine: str
host: str
kind: Literal['sql']
model_config = {'extra': 'forbid', 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

options: dict[str, JsonValue]
password: str | None
port: int
sql: str
ssh: SshTunnelSettings | None
timeout: float
username: str | None
class smarter.apps.connection.execution.SshTunnelSettings(**data)[source]

Bases: BaseModel

The SSH tunnel of a SqlConnection whose authenticationMethod is tcpip_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_hosts format.

host: str
known_hosts: str | None
model_config = {'extra': 'forbid', 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

password: str | None
port: int
username: str | None
smarter.apps.connection.execution.database_settings(call, host, port)[source]

Return Django’s settings for a call’s database, at a host and port.

Parameters:
  • call (SqlToolCall) – The call.

  • host (str) – The host to connect to: the database’s, or the SSH tunnel’s loopback.

  • port (int) – Its port.

Return type:

dict[str, Any]

Returns:

Settings for django.db.utils.ConnectionHandler.

smarter.apps.connection.execution.execute_api_call(call)[source]

Execute a resolved ApiPlugin request.

Parameters:

call (ToolCall) – The call, an ApiToolCall.

Return type:

ToolCallResult

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 (no status_code), when the api answers with an error status, or with a 2xx other than 200, or when its answer isn’t JSON (with its status_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, a SqlToolCall.

Return type:

ToolCallResult

Returns:

The rows as JSON, a database error with is_error, or a failure when 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 limit items.

A list keeps its first limit items. A dict keeps every key, and each of its values that is a list keeps its first limit items. Anything else, or no limit, is returned as it is.

Parameters:
  • response_data (TypeAliasType) – The parsed response.

  • limit (int | None) – The number of items to keep, or None or 0 for all of them.

Return type:

TypeAliasType

Returns:

The limited response.

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:

ToolCallResult

Returns:

The rows as JSON; a result with is_error and the database’s message when the query fails; or a result with a failure when the database can’t be reached or refuses the login.

smarter.apps.connection.execution.rows_as_json(cursor)[source]

Return a query’s rows as a JSON list of objects, one per row, keyed by column.

Parameters:

cursor – The cursor, after the query ran.

Return type:

str

Returns:

The JSON.