trading_agent.client.message_client
¶
Sends one message to a running gateway and prints the reply as it streams in.
It is what trading-agent send and trading-agent -p use, so a desk can be talked to from a script, a pipe, or with ! from Claude Code.
When the desk is busy, the gateway hands a plain message to the running turn instead of queueing it. The client then follows that turn until it ends, as the Telegram channel does. Questions, plans and permission requests the desk raises meanwhile are printed with their ids, to be answered in the terminal program or on the web page.
Three output formats are offered, as in claude -p:
text the reply as it streams, with one line per tool call json one JSON object at the end, with the result, tool calls and backend stream-json every event of the turn as one JSON line, then a final result line
Typical usage example:
client = MessageClient('http://127.0.0.1:8120', token) exit_code = asyncio.run(client.send('general', 'What time is it?'))
MessageClient
¶
A small client for the gateway's message route and event stream.
Creates the client.
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 |
output
|
TextIO
|
Where the reply is printed. |
stdout
|
output_format
|
str
|
"text", "json" or "stream-json". |
'text'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
The output format is not one of the three. |
Source code in src/trading_agent/client/message_client.py
send
async
¶
Sends the message and prints everything the desk does until its turn ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk to talk to. |
required |
text
|
str
|
The message, or a /command. |
required |
Returns:
| Type | Description |
|---|---|
int
|
0 when the turn finished normally, 1 when it ended in an error or the gateway refused the message. |
Source code in src/trading_agent/client/message_client.py
TurnFollower
¶
Reads the event stream for one message and prints what its turn does.
Creates the follower.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk the message went to. |
required |
trigger_id
|
str
|
The message's trigger id. |
required |
output
|
TextIO
|
Where to print. |
required |
output_format
|
str
|
"text", "json" or "stream-json". |
required |
Source code in src/trading_agent/client/message_client.py
follow
async
¶
Prints the turn's events until it finishes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
socket
|
Any
|
The open event stream, with an async recv method that returns JSON text. |
required |
Returns:
| Type | Description |
|---|---|
int
|
0 for a normal finish, 1 when the turn ended in an error. |
Source code in src/trading_agent/client/message_client.py
handle
¶
Takes one event from the stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
dict[str, Any]
|
The event. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the followed turn has finished. |
Source code in src/trading_agent/client/message_client.py
finish
¶
Prints the closing output for the json formats and picks the exit code.
Returns:
| Type | Description |
|---|---|
int
|
0 for a normal finish, 1 when the turn ended in an error. |