|
4 | 4 | - wait_for_message_tool with streaming: push messages in and see the agent react |
5 | 5 | - Using handle.stream() to observe WAITING → processing → WAITING cycles |
6 | 6 | - runtime.send_message() to push payloads into the Workflow Message Queue |
| 7 | + - handle.stop() ending the loop deterministically |
7 | 8 |
|
8 | | -The agent starts, immediately waits for a message, processes whatever it |
9 | | -receives (by calling wait_for_message again), then waits again. The caller |
10 | | -drives the conversation by sending messages and reading streamed events. |
| 9 | +The agent starts, immediately waits for a message, answers it with respond(), |
| 10 | +then loops back to wait_for_message. The caller drives the conversation from a |
| 11 | +background thread — sending a task every 8 seconds — while the main thread reads |
| 12 | +streamed events. |
| 13 | +
|
| 14 | +The agent's instructions tell it to never stop, so the loop only ends when the |
| 15 | +sender calls handle.stop() — after giving the last task time to be answered. |
| 16 | +That sets the ``_stop_requested`` workflow variable |
| 17 | +checked by the DoWhile condition and pushes a ``{"_signal": "stop"}`` message to |
| 18 | +unblock the pending PULL_WORKFLOW_MESSAGES. stream() then yields DONE. |
11 | 19 |
|
12 | 20 | Requirements: |
13 | 21 | - Conductor server running at http://localhost:8080 |
@@ -66,15 +74,16 @@ def main() -> None: |
66 | 74 | print(f"Agent started: {handle.execution_id}\n") |
67 | 75 |
|
68 | 76 | # Push messages from a background thread while we stream events on the main thread. |
69 | | - # Wait long enough between sends for the agent to finish processing each message. |
70 | | - # No sleep after the last send — handle.stream() on the main thread is already the |
71 | | - # barrier: it blocks until DONE, which only fires once the workflow reaches a |
72 | | - # terminal state (after stop() sets the flag and the current iteration completes). |
| 77 | + # Wait long enough between sends for the agent to finish processing each message — |
| 78 | + # including after the last one. Calling stop() immediately after the final send |
| 79 | + # would set _stop_requested while the agent is still mid-turn on that task, and the |
| 80 | + # DoWhile would exit before it ever answers. |
73 | 81 | def sender(): |
74 | 82 | for task in TASKS: |
75 | 83 | time.sleep(8) |
76 | 84 | print(f"\n [caller] sending -> {task!r}") |
77 | 85 | runtime.send_message(handle.execution_id, {"task": task}) |
| 86 | + time.sleep(8) |
78 | 87 | handle.stop() |
79 | 88 |
|
80 | 89 | threading.Thread(target=sender, daemon=True).start() |
|
0 commit comments