JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(chat): resolve chat CLI team mode premature exit and route through /tui
haowenzhong1f313cae99fe8
diff --git a/docs/en/CLI.md b/docs/en/CLI.mdindex 6db330e169..5bd0ba6b08 100644--- a/docs/en/CLI.md+++ b/docs/en/CLI.md@@ -247,7 +247,7 @@ jiuwenswarm-start appjiuwenswarm chat "Hello, introduce yourself"```-`jiuwenswarm chat` calls JiuwenSwarm's runtime through the Gateway's `/cli` WebSocket route, using the same MessageHandler and AgentServer path as the TUI.+`jiuwenswarm chat` calls JiuwenSwarm's runtime through the Gateway's `/tui` WebSocket route (`channel_id="tui"`), sharing the same MessageHandler and AgentServer path as the TUI.### Basic Usage@@ -267,7 +267,7 @@ jiuwenswarm chat "Hello, introduce yourself"| `--cwd <path>` | Current dir | Working directory for file mentions and agent context || `--project-dir <path>` | `--cwd` | Project identity for session and agent cache || `--trusted-dir <path>` | `--project-dir` | Trusted directory (repeatable) |-| `--gateway-url <url>` | `ws://127.0.0.1:19001/cli` | Explicit Gateway WebSocket URL |+| `--gateway-url <url>` | `ws://127.0.0.1:19001/tui` | Explicit Gateway WebSocket URL || `--name <instance>` | — | Named instance for env isolation || `--dotenv <path>` | — | Path to .env file || `--json` | — | Print one final JSON object |@@ -375,4 +375,4 @@ jiuwenswarm chat --jsonl "analyze README" | jq### Relationship with TUI-`jiuwenswarm chat` uses an independent `/cli` route (`channel_id="cli"`), isolated from the TUI's `/tui` route. Both can be used simultaneously without interfering with each other. The first version of the terminal CLI does not aim to replicate all TUI slash commands.+`jiuwenswarm chat` reuses the TUI's `/tui` route (`channel_id="tui"`), sharing the same MessageHandler and AgentServer pipeline. Only one TUI/CLI connection per `channel_id="tui"` is active at a time — opening the TUI while `jiuwenswarm chat` is running (or vice versa) will replace the previous WebSocket client on that channel. The first version of the terminal CLI does not aim to replicate all TUI slash commands.diff --git "a/docs/zh/\345\221\275\344\273\244\350\241\214\346\214\207\344\273\244.md" "b/docs/zh/\345\221\275\344\273\244\350\241\214\346\214\207\344\273\244.md"index 6219328284..ee4aaa60e0 100644--- "a/docs/zh/\345\221\275\344\273\244\350\241\214\346\214\207\344\273\244.md"+++ "b/docs/zh/\345\221\275\344\273\244\350\241\214\346\214\207\344\273\244.md"@@ -209,7 +209,7 @@ jiuwenswarm-start appjiuwenswarm chat "你好,介绍一下你自己"```-`jiuwenswarm chat` 通过 Gateway 的 `/cli` WebSocket 路由调用 JiuwenSwarm 的运行时,与 TUI 使用相同的 MessageHandler 和 AgentServer 路径。+`jiuwenswarm chat` 通过 Gateway 的 `/tui` WebSocket 路由(`channel_id="tui"`)调用 JiuwenSwarm 的运行时,与 TUI 共用相同的 MessageHandler 和 AgentServer 路径。### 基本用法@@ -229,7 +229,7 @@ jiuwenswarm chat "你好,介绍一下你自己"| `--cwd <path>` | 当前目录 | 文件引用和 Agent 上下文的工作目录 || `--project-dir <path>` | `--cwd` | 项目和 agent 缓存标识 || `--trusted-dir <path>` | `--project-dir` | 信任目录(可重复) |-| `--gateway-url <url>` | `ws://127.0.0.1:19001/cli` | 自定义 Gateway 地址 |+| `--gateway-url <url>` | `ws://127.0.0.1:19001/tui` | 自定义 Gateway 地址 || `--name <instance>` | — | 多实例隔离 || `--dotenv <path>` | — | 指定 .env 文件 || `--json` | — | 输出一个最终 JSON 对象 |@@ -337,6 +337,6 @@ jiuwenswarm chat --jsonl "分析 README" | jq### 与 TUI 的关系-`jiuwenswarm chat` 使用独立的 `/cli` 路由(`channel_id="cli"`),与 TUI 的 `/tui` 路由隔离,两者可以同时使用而不会互相干扰。终端 CLI 的第一版不追求完整复刻 TUI 的所有 slash command。+`jiuwenswarm chat` 复用 TUI 的 `/tui` 路由(`channel_id="tui"`),与 TUI 共用同一套 MessageHandler 和 AgentServer 管线。同一 `channel_id="tui"` 同一时刻只能有一个 WebSocket 客户端在线——在 `jiuwenswarm chat` 运行时打开 TUI(或反过来)会替换该通道上的上一个客户端。终端 CLI 的第一版不追求完整复刻 TUI 的所有 slash command。diff --git a/jiuwenswarm/cli/chat.py b/jiuwenswarm/cli/chat.pyindex 33a489af8d..88587f9760 100644--- a/jiuwenswarm/cli/chat.py+++ b/jiuwenswarm/cli/chat.py@@ -183,6 +183,11 @@ async def _run_interactive_loop() -> int:interrupted = Falseper_recv_timeout = timeout if timeout and timeout > 0 else 300.0+ # In team mode the session is persistent: leader replies arrive as+ # chat.final but the team keeps working (creating workflows, delegating+ # to members, etc.). Only chat.processing_status(is_processing=False)+ # or team.error should terminate the CLI stream.+ team_mode = request.get("params", {}).get("mode", "") in ("team", "team.plan", "code.team")loop = asyncio.get_running_loop()@@ -248,6 +253,13 @@ async def _run_interactive_loop(elif kind == "tool_result":renderer.handle_tool_result(payload)elif kind == "final":+ # team.error is broadcast through the chat.final envelope+ # (gateway default for unknown EventType). Route it to the+ # error handler so the message is shown instead of silently+ # exiting on an empty content field.+ if payload.get("event_type") == "team.error":+ renderer.handle_error(payload)+ return 1renderer.handle_final(payload)elif kind == "error":renderer.handle_error(payload)@@ -282,6 +294,16 @@ async def _run_interactive_loop(return 4if is_terminal_event(event_type, payload):+ # In team mode, chat.final (leader reply) is not terminal —+ # the team keeps working after the leader responds. Only+ # processing_status(is_processing=False) or team.error ends+ # the stream.+ if team_mode and event_type == "chat.final":+ if payload.get("event_type") == "team.error":+ renderer.clear_loading()+ return 1+ # leader reply or team control event — keep listening+ continuerenderer.clear_loading()if renderer.streamed_text and not renderer.streamed_text.endswith("\n"):write_stdout("\n")@@ -304,6 +326,7 @@ async def _run_jsonl_loop(renderer: JsonlRenderer,request: dict,) -> int:+ team_mode = request.get("params", {}).get("mode", "") in ("team", "team.plan", "code.team")await client.send_request(request)while True:data = await client.recv()@@ -315,6 +338,15 @@ async def _run_jsonl_loop(if event_type == "chat.error":return 1if is_terminal_event(event_type, payload):+ # team.error is wrapped in chat.final by the gateway; treat it+ # as an error exit so callers can detect the failure.+ if payload.get("event_type") == "team.error":+ return 1+ # In team mode, chat.final (leader reply) is not terminal —+ # the team keeps working. Keep listening for+ # processing_status(is_processing=False).+ if team_mode and event_type == "chat.final":+ continuereturn 0@@ -323,6 +355,7 @@ async def _run_json_loop(renderer: JsonRenderer,request: dict,) -> int:+ team_mode = request.get("params", {}).get("mode", "") in ("team", "team.plan", "code.team")await client.send_request(request)has_error = Falsewhile True:@@ -332,7 +365,14 @@ async def _run_json_loop(event_type = data.get("event", "")payload = data.get("payload", {})if event_type == "chat.final":- renderer.handle_event(event_type, payload)+ # team.error is wrapped in chat.final by the gateway; route it+ # through the error path so has_error is set and the JSON output+ # reports ok=false.+ if payload.get("event_type") == "team.error":+ renderer.handle_error(payload)+ has_error = True+ else:+ renderer.handle_event(event_type, payload)elif event_type == "chat.error":renderer.handle_error(payload)has_error = True@@ -341,6 +381,12 @@ async def _run_json_loop(else:renderer.handle_event(event_type, payload)if is_terminal_event(event_type, payload):+ if team_mode and event_type == "chat.final":+ if payload.get("event_type") == "team.error":+ renderer.output()+ return 1+ # leader reply — team keeps working, keep listening+ continuerenderer.output()return 1 if has_error else 0diff --git a/jiuwenswarm/cli/events.py b/jiuwenswarm/cli/events.pyindex 2d007ba8ce..19625b33da 100644--- a/jiuwenswarm/cli/events.py+++ b/jiuwenswarm/cli/events.py@@ -14,6 +14,14 @@ def is_terminal_event(event_type: str, payload: dict[str, Any]) -> bool:inner = payload.get("event_type", "")if inner == "keepalive":return False+ # Team control events (team.runtime_ready, team.completed) are+ # broadcast through the chat.final envelope because they lack an+ # EventType enum mapping on the gateway side. They are NOT terminal —+ # the real round-complete signal is+ # chat.processing_status(is_processing=False). team.error is the+ # exception: it indicates a failed team stream and should terminate.+ if isinstance(inner, str) and inner.startswith("team."):+ return inner == "team.error"return Trueif event_type == "chat.processing_status":if not payload.get("is_processing", True):diff --git a/tests/unit_tests/cli/test_events.py b/tests/unit_tests/cli/test_events.pyindex e339aaeeec..42ff36089a 100644--- a/tests/unit_tests/cli/test_events.py+++ b/tests/unit_tests/cli/test_events.py@@ -58,6 +58,29 @@ class TestEvents:def test_is_terminal_chat_final_keepalive():assert is_terminal_event("chat.final", {"event_type": "keepalive"}) is False+ @staticmethod+ def test_is_terminal_chat_final_team_runtime_ready():+ # team.runtime_ready is a control event wrapped in chat.final envelope;+ # it must NOT terminate the CLI stream (the team hasn't answered yet).+ assert is_terminal_event(+ "chat.final", {"event_type": "team.runtime_ready"}+ ) is False++ @staticmethod+ def test_is_terminal_chat_final_team_completed():+ # team.completed is a control event; the real terminal signal is+ # chat.processing_status(is_processing=False).+ assert is_terminal_event(+ "chat.final", {"event_type": "team.completed"}+ ) is False++ @staticmethod+ def test_is_terminal_chat_final_team_error():+ # team.error indicates a failed team stream and should terminate.+ assert is_terminal_event(+ "chat.final", {"event_type": "team.error", "error": "boom"}+ ) is True+@staticmethoddef test_is_terminal_chat_error():assert is_terminal_event("chat.error", {}) is True