trading_agent.terminal.gateway_client
¶
Talks to a running gateway's HTTP routes, one method per route, as the web page's apiClient.ts does.
Every request carries the access token as a Bearer header, and names the terminal program as the client, so the desks' requests answered here are recorded as answered "via terminal". A route that answers with an error raises GatewayError with the gateway's own explanation.
Typical usage example:
async with GatewayClient('http://127.0.0.1:8120', token) as gateway: desks = await gateway.desks() reply = await gateway.send_message('general', '/status')
GatewayError
¶
Bases: Exception
The gateway answered with an error, or could not be reached.
Attributes:
| Name | Type | Description |
|---|---|---|
status_code |
The HTTP status, or 0 when the gateway could not be reached. |
|
message |
The gateway's explanation. |
Creates the error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status_code
|
int
|
The HTTP status, or 0 when the gateway could not be reached. |
required |
message
|
str
|
The gateway's explanation. |
required |
Source code in src/trading_agent/terminal/gateway_client.py
GatewayClient
¶
A connection to the gateway's routes.
Creates the client without connecting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
The gateway's address, such as http://127.0.0.1:8120. |
required |
access_token
|
str
|
The gateway's access token. |
required |
transport
|
AsyncBaseTransport | None
|
A transport to use instead of the network, such as one that calls the gateway in the same process in tests. |
None
|
Source code in src/trading_agent/terminal/gateway_client.py
__aenter__
async
¶
__aexit__
async
¶
Closes the client's connections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_type
|
type[BaseException] | None
|
The type of the exception that ended the block, if any. |
required |
error
|
BaseException | None
|
The exception, if any. |
required |
traceback
|
TracebackType | None
|
Its traceback, if any. |
required |
Source code in src/trading_agent/terminal/gateway_client.py
close
async
¶
events_address
¶
The WebSocket address of the event stream, with the token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str | None
|
A desk to keep only that desk's events and events of no desk, or None for every event. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Such as ws://127.0.0.1:8120/ws/events?token=.... |
Source code in src/trading_agent/terminal/gateway_client.py
health
async
¶
Whether the gateway is up; this route needs no token.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The status and each desk's state. |
desks
async
¶
Every desk's state, models and session.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The mode and the desks. |
desk
async
¶
One desk's state, with its persona.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The desk's summary and Persona.md text. |
Source code in src/trading_agent/terminal/gateway_client.py
desk_context
async
¶
How full a desk's context window is, and its tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The desk, its usage snapshot or None, and its tool names. |
Source code in src/trading_agent/terminal/gateway_client.py
desk_commands
async
¶
A desk's slash commands and usable skills.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The desk and its command entries. |
Source code in src/trading_agent/terminal/gateway_client.py
send_message
async
¶
Queues a message or /command for a desk; the reply arrives on the event stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
text
|
str
|
The message or command. |
required |
attachment_ids
|
Sequence[str]
|
Uploads to send with it. |
()
|
Returns:
| Type | Description |
|---|---|
str
|
The trigger id that the reply's events carry. |
Source code in src/trading_agent/terminal/gateway_client.py
choose_model
async
¶
Makes a desk try one backend first, or go back to the configured order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
backend_name
|
str | None
|
The backend, or None for the configured order. |
required |
model
|
str | None
|
Another model of that backend's provider, or None for its configured model. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The desk's new summary. |
Source code in src/trading_agent/terminal/gateway_client.py
upload_attachment
async
¶
Uploads a file for a desk, to be named in a later message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
file_path
|
Path
|
The file to upload. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The attachment, with its id, kind and any problem reading it. |
Source code in src/trading_agent/terminal/gateway_client.py
attachment_bytes
async
¶
Downloads an attachment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
attachment_id
|
str
|
The attachment's id. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The file's contents. |
Source code in src/trading_agent/terminal/gateway_client.py
picture_bytes
async
¶
Downloads a picture from inside a desk's folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
picture_path
|
str
|
The picture's path relative to the desk folder. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The picture's contents. |
Source code in src/trading_agent/terminal/gateway_client.py
pause
async
¶
Stops a desk asking models until it is resumed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The desk's new summary. |
Source code in src/trading_agent/terminal/gateway_client.py
resume
async
¶
Makes a paused desk answer again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The desk's new summary. |
Source code in src/trading_agent/terminal/gateway_client.py
flatten
async
¶
Cancels a desk's orders and closes its positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
reason
|
str
|
Why, for the journal. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The flatten tool's report text and whether it failed. |
Source code in src/trading_agent/terminal/gateway_client.py
requests
async
¶
The desks' questions, plans and permission requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
"pending" for the ones waiting for an answer, or "all". |
'pending'
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
The requests. |
Source code in src/trading_agent/terminal/gateway_client.py
answer_questions
async
¶
Answers a desk's questions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
str
|
The request. |
required |
answers
|
Sequence[dict[str, Any]]
|
One answer per question, each with "selected" labels and "other" text. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The request, now answered. |
Source code in src/trading_agent/terminal/gateway_client.py
decide_plan
async
¶
Approves a desk's plan or asks it to keep planning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
str
|
The request. |
required |
decision
|
str
|
"approve" or "revise". |
required |
feedback
|
str
|
What to change, or notes on the approval. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The request, now answered. |
Source code in src/trading_agent/terminal/gateway_client.py
decide_permission
async
¶
Allows or denies a tool call that needs permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
str
|
The request. |
required |
decision
|
str
|
"allow_once", "always_allow" or "deny". |
required |
reason
|
str
|
Why, sent to the desk with a denial. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The request, with the call's result when it ran. |
Source code in src/trading_agent/terminal/gateway_client.py
dismiss_request
async
¶
Dismisses a question or plan without answering it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
str
|
The request. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The request, now dismissed. |
Source code in src/trading_agent/terminal/gateway_client.py
permission_grants
async
¶
The tools each desk may call without asking.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
The grants, each with desk, tool_name and granted_at. |
Source code in src/trading_agent/terminal/gateway_client.py
revoke_permission
async
¶
Makes a desk ask again before calling a tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
tool_name
|
str
|
The tool. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Whether a grant was revoked. |
Source code in src/trading_agent/terminal/gateway_client.py
account
async
¶
The account's balance, profit and loss, positions, owners and halts.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The account. |
desk_orders
async
¶
Each desk's open orders.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The orders, keyed by desk. |
broker_orders
async
¶
UBI's own order book.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
"orders", "trades" or "open_parents". |
'orders'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The rows, keyed by kind. |
Source code in src/trading_agent/terminal/gateway_client.py
kill
async
¶
Fires the kill switch: every desk is halted and the account is flattened.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reason
|
str
|
Why, for the journal. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The kill switch's report. |
Source code in src/trading_agent/terminal/gateway_client.py
desk_journal
async
¶
A desk's events of one day, oldest first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
limit
|
int
|
The most events to return, newest kept. |
_JOURNAL_LIMIT
|
day
|
str | None
|
The day as YYYY-MM-DD, or None for today. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The day and its events. |
Source code in src/trading_agent/terminal/gateway_client.py
journal
async
¶
Today's events that belong to no desk, oldest first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int
|
The most events to return, newest kept. |
_JOURNAL_LIMIT
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The day and its events. |
Source code in src/trading_agent/terminal/gateway_client.py
sessions
async
¶
A desk's sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The current session id and every session with its exchange count. |
Source code in src/trading_agent/terminal/gateway_client.py
session
async
¶
One session's exchanges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
session_id
|
str
|
The session. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The session id and its exchanges. |
Source code in src/trading_agent/terminal/gateway_client.py
summaries
async
¶
A desk's daily summaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The summaries, each with date and text. |
Source code in src/trading_agent/terminal/gateway_client.py
notes
async
¶
A desk's notes files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The notes, each with file and text. |
Source code in src/trading_agent/terminal/gateway_client.py
search
async
¶
Searches a desk's memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
keywords
|
str
|
The words to look for. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Whether every word matched, and the hits. |
Source code in src/trading_agent/terminal/gateway_client.py
files
async
¶
The files the user may edit.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The files, each with name, desk, whether it exists and its size. |
file_text
async
¶
Reads an editable file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The file's name, such as "TradingRules.md". |
required |
desk_name
|
str | None
|
Its desk, or None for a shared file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The name, desk and text. |
Source code in src/trading_agent/terminal/gateway_client.py
save_file
async
¶
Writes an editable file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The file's name. |
required |
desk_name
|
str | None
|
Its desk, or None for a shared file. |
required |
text
|
str
|
The new text. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The number of characters written. |
Source code in src/trading_agent/terminal/gateway_client.py
proposals
async
¶
The changes desks propose to the user's files.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The proposals, each with its unified diff. |
Source code in src/trading_agent/terminal/gateway_client.py
decide_proposal
async
¶
Accepts or rejects a proposal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proposal_id
|
str
|
The proposal. |
required |
accept
|
bool
|
True to apply it, False to reject it. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The outcome. |
Source code in src/trading_agent/terminal/gateway_client.py
jobs
async
¶
A desk's scheduled jobs and their latest runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The jobs and recent runs. |
Source code in src/trading_agent/terminal/gateway_client.py
create_job
async
¶
Schedules a new job for a desk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
job
|
dict[str, Any]
|
The job: name, schedule, run_at, instructions or command, trading_days_only and market_hours_only. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The job as stored. |
Source code in src/trading_agent/terminal/gateway_client.py
delete_job
async
¶
Removes a scheduled job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
job_id
|
str
|
The job. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The deleted job's id. |
Source code in src/trading_agent/terminal/gateway_client.py
run_job
async
¶
Runs a scheduled job now.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
job_id
|
str
|
The job. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The started job's id. |
Source code in src/trading_agent/terminal/gateway_client.py
alerts
async
¶
A desk's price alerts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The alerts. |
Source code in src/trading_agent/terminal/gateway_client.py
delete_alert
async
¶
Removes a price alert.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
alert_id
|
str
|
The alert. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The deleted alert's id. |
Source code in src/trading_agent/terminal/gateway_client.py
skills
async
¶
A desk's usable and hidden skills.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The available and hidden skills. |
Source code in src/trading_agent/terminal/gateway_client.py
subagents
async
¶
The subagent definitions, and files that could not be read.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The subagents and the broken files. |
Source code in src/trading_agent/terminal/gateway_client.py
mcp_servers
async
¶
The MCP servers and their tools.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The servers. |
packages
async
¶
The installed packages.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The packages. |
checkpoints
async
¶
A desk's checkpoints, newest first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The checkpoints. |
Source code in src/trading_agent/terminal/gateway_client.py
restore_checkpoint
async
¶
Puts a desk's own files back as they were at a checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
checkpoint_id
|
str
|
The checkpoint. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The restored checkpoint and the checkpoint that undoes it. |
Source code in src/trading_agent/terminal/gateway_client.py
usage
async
¶
Today's model calls, tokens and costs, and Claude's limits.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The usage per desk and backend, and the rate limits. |
Source code in src/trading_agent/terminal/gateway_client.py
configuration
async
¶
The configuration file's settings, with secrets masked.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The settings, by section. |
Source code in src/trading_agent/terminal/gateway_client.py
apps
async
¶
The packages' web apps.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
The apps, each with its health and unit state. |
Source code in src/trading_agent/terminal/gateway_client.py
app_action
async
¶
Starts, stops or restarts a package's app.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The package. |
required |
action
|
str
|
"start", "stop" or "restart". |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The app's new state. |
Source code in src/trading_agent/terminal/gateway_client.py
telegram
async
¶
The Telegram channel's state, pairings and paired chats.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The Telegram status. |
Source code in src/trading_agent/terminal/gateway_client.py
approve_pairing
async
¶
Pairs the Telegram chat that the bot gave a code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
The code. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The paired chat. |
Source code in src/trading_agent/terminal/gateway_client.py
revoke_pairing
async
¶
Unpairs a Telegram chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
The chat. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The outcome. |