|
| 1 | +# `StreamAgent_0` |
| 2 | + |
| 3 | +This agent is a **client-side streaming example** built with the Summoner SDK. It is derived from [`EchoAgent_0`](../agent_EchoAgent_0) and reuses the same receive-then-buffer-then-send pattern, but replaces the static echo payload with an LLM stream. The goal is to show how to trigger a streaming LLM response on `@receive`, buffer streamed tokens in an `asyncio.Queue`, and emit those tokens back to the server via `@send`. |
| 4 | + |
| 5 | +## Behavior |
| 6 | + |
| 7 | +<details> |
| 8 | +<summary><b>(Click to expand)</b> The agent goes through these steps:</summary> |
| 9 | +<br> |
| 10 | + |
| 11 | +1. The agent connects to the server. |
| 12 | +2. When it receives a message of the form: |
| 13 | + |
| 14 | + ```json |
| 15 | + {"remote_addr": "...", "content": ...} |
| 16 | + ``` |
| 17 | + |
| 18 | + it interprets `content` as a prompt (string or dict) and starts an LLM stream. |
| 19 | +3. While the LLM is streaming, the agent pushes events into a queue: |
| 20 | + |
| 21 | + * `{"type": "stream_start", "stream_id": ...}` |
| 22 | + * `{"type": "token", "stream_id": ..., "token": ...}` |
| 23 | + * `{"type": "stream_end", "stream_id": ...}` |
| 24 | + * (optional) `stream_cancelled` if a new prompt arrives while the previous stream is active |
| 25 | + * `stream_error` if the stream fails |
| 26 | +4. The `@send` route waits for the next queued event and sends it immediately. If no event is available, it returns `None` after 0.5 seconds. |
| 27 | + |
| 28 | +> 📝 **Note:** |
| 29 | +> |
| 30 | +> * Only one stream is active at a time. A new received prompt cancels the previous stream task. |
| 31 | +> * The server is responsible for attaching `remote_addr` and packaging the outbound payload into `content`. |
| 32 | +
|
| 33 | +</details> |
| 34 | + |
| 35 | +## SDK Features Used |
| 36 | + |
| 37 | +| Feature | Description | |
| 38 | +| ---------------------------- | ------------------------------------------------------------------------ | |
| 39 | +| `SummonerClient(name=...)` | Creates and manages the agent instance | |
| 40 | +| `@client.receive(route=...)` | Registers an async handler triggered on incoming server messages | |
| 41 | +| `@client.send(route=...)` | Registers an async sender that periodically emits payloads to the server | |
| 42 | +| `client.run(...)` | Connects the client to the server and initiates the async lifecycle | |
| 43 | + |
| 44 | +## How to Run |
| 45 | + |
| 46 | +First, ensure the Summoner server is running: |
| 47 | + |
| 48 | +```bash |
| 49 | +python server.py |
| 50 | +``` |
| 51 | + |
| 52 | +> [!TIP] |
| 53 | +> You can use the option `--config configs/server_config_nojsonlogs.json` for cleaner terminal output and log files. |
| 54 | +
|
| 55 | +Set your OpenAI credentials (recommended via `.env` in the agent folder): |
| 56 | + |
| 57 | +```bash |
| 58 | +export OPENAI_API_KEY="..." |
| 59 | +``` |
| 60 | + |
| 61 | +Then run the agent: |
| 62 | + |
| 63 | +```bash |
| 64 | +python agents/agent_StreamAgent_0/agent.py |
| 65 | +``` |
| 66 | + |
| 67 | +If you want to point to a specific client config: |
| 68 | + |
| 69 | +```bash |
| 70 | +python agents/agent_StreamAgent_0/agent.py --config configs/client_config.json |
| 71 | +``` |
| 72 | + |
| 73 | +## Simulation Scenarios |
| 74 | + |
| 75 | +This scenario demonstrates an end-to-end streaming round-trip across three processes: |
| 76 | + |
| 77 | +* **Server**: routes messages between clients and provides the envelope `{remote_addr, content}`. |
| 78 | +* **`StreamAgent_0`**: receives a prompt, starts a streaming LLM call, and emits a sequence of streaming events (`stream_start`, `token`, `stream_end`). |
| 79 | +* **`InputAgent`**: provides an interactive CLI, sending prompts and printing responses as they arrive. |
| 80 | + |
| 81 | +### 1) Start the three terminals |
| 82 | + |
| 83 | +```sh |
| 84 | +# Terminal 1: start the server |
| 85 | +python server.py |
| 86 | + |
| 87 | +# Terminal 2: start the streaming agent |
| 88 | +python agents/agent_StreamAgent_0/agent.py |
| 89 | + |
| 90 | +# Terminal 3: start the interactive input agent |
| 91 | +python agents/agent_InputAgent/agent.py |
| 92 | +``` |
| 93 | + |
| 94 | +### 2) Enter a prompt in the InputAgent |
| 95 | + |
| 96 | +In Terminal 3 you should see the InputAgent connect, then a prompt: |
| 97 | + |
| 98 | +```log |
| 99 | +python agents/agent_InputAgent/agent.py |
| 100 | +[DEBUG] Loaded config from: configs/client_config.json |
| 101 | +2026-01-30 18:48:39.168 - InputAgent - INFO - Connected to server @(host=127.0.0.1, port=8888) |
| 102 | +> How are you? |
| 103 | +``` |
| 104 | + |
| 105 | +When you type `How are you?` and press Enter: |
| 106 | + |
| 107 | +1. **`InputAgent`** sends the prompt to the server. |
| 108 | +2. The **server forwards** it to **`StreamAgent_0`**, packaging it as: |
| 109 | + |
| 110 | + ```json |
| 111 | + {"remote_addr": "...", "content": "How are you?"} |
| 112 | + ``` |
| 113 | +3. **`StreamAgent_0`** begins streaming an LLM response. As tokens arrive, it pushes events into its internal queue. |
| 114 | +4. The agent's `@send` loop emits those queued events back to the server as they become available. |
| 115 | +5. The **server forwards** those events to **`InputAgent`**, which prints them immediately. |
| 116 | + |
| 117 | +### 3) Observe the streamed events in `InputAgent` |
| 118 | + |
| 119 | +In Terminal 3, you should see a streaming envelope followed by many token events: |
| 120 | + |
| 121 | +```log |
| 122 | +[Received] {'type': 'stream_start', 'stream_id': '6e475111-8633-42ea-a456-f146366f131f'} |
| 123 | +[Received] {'type': 'token', 'stream_id': '6e475111-8633-42ea-a456-f146366f131f', 'token': "I'm"} |
| 124 | +[Received] {'type': 'token', 'stream_id': '6e475111-8633-42ea-a456-f146366f131f', 'token': ' just'} |
| 125 | +[Received] {'type': 'token', 'stream_id': '6e475111-8633-42ea-a456-f146366f131f', 'token': ' a'} |
| 126 | +... |
| 127 | +[Received] {'type': 'token', 'stream_id': '6e475111-8633-42ea-a456-f146366f131f', 'token': '?'} |
| 128 | +[Received] {'type': 'stream_end', 'stream_id': '6e475111-8633-42ea-a456-f146366f131f'} |
| 129 | +> |
| 130 | +``` |
| 131 | + |
| 132 | +What to pay attention to: |
| 133 | + |
| 134 | +* **`stream_id`**: all events for a single streamed response share the same `stream_id`. This is what allows the receiver (InputAgent or another client) to group tokens into the right response, even if multiple streams exist in the system. |
| 135 | +* **Token granularity**: tokens arrive as small chunks (sometimes including leading spaces). This is normal for streamed generation. |
| 136 | +* **Ordering**: you should always see `stream_start` first and `stream_end` last for a given `stream_id`, with one or many `token` events in between. |
| 137 | + |
| 138 | +### 4) Observe StreamAgent_0's logs (trigger confirmation) |
| 139 | + |
| 140 | +In Terminal 2, `StreamAgent_0` logs when it receives a prompt and starts an LLM stream: |
| 141 | + |
| 142 | +```log |
| 143 | +python agents/agent_StreamAgent_0/agent.py |
| 144 | +[DEBUG] Loaded config from: configs/client_config.json |
| 145 | +2026-01-30 18:42:17.003 - StreamAgent_0 - INFO - Connected to server @(host=127.0.0.1, port=8888) |
| 146 | +2026-01-30 18:48:42.192 - StreamAgent_0 - INFO - Triggering LLM streaming for remote_addr=127.0.0.1:50490 prompt='How are you?' |
| 147 | +``` |
0 commit comments