Skip to content

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(team): In the team mode, directly invoking the "chat" command yields no response.

weichenhaob073b1fe579a
diff --git a/jiuwenswarm/agents/swarm/providers/code_rails.py b/jiuwenswarm/agents/swarm/providers/code_rails.py
index ddccf37d34..3c1193697f 100644
--- a/jiuwenswarm/agents/swarm/providers/code_rails.py
+++ b/jiuwenswarm/agents/swarm/providers/code_rails.py
@@ -145,6 +145,10 @@ def build_code_runtime_prompt(params: dict[str, Any], ctx: SwarmBuildContext) ->
try:
inp = CodeRuntimePromptInput.resolve(params, ctx)
rail = RuntimePromptRail(language=inp.language, channel=inp.channel)
+ # Code-team members need the same per-request runtime binding as the
+ # regular team profile; otherwise mode falls back to "unknown".
+ rail.set_mode(ctx.mode)
+ rail.set_session_id(ctx.session_id)
rail.set_runtime_paths(cwd=inp.project_dir, project_dir=inp.project_dir)
return rail
except Exception as exc:
diff --git a/jiuwenswarm/cli/chat.py b/jiuwenswarm/cli/chat.py
index 877aa1612f..24c498195c 100644
--- a/jiuwenswarm/cli/chat.py
+++ b/jiuwenswarm/cli/chat.py
@@ -43,6 +43,7 @@ from jiuwenswarm.cli._terminal import write_stderr, write_stdout
from jiuwenswarm.cli.gateway_client import GatewayClient
from jiuwenswarm.cli.events import (
event_kind,
+ is_content_final,
is_terminal_event,
)
from jiuwenswarm.cli.render import HumanRenderer, JsonRenderer, JsonlRenderer
@@ -637,6 +638,11 @@ async def _run_interactive_loop(
if payload.get("event_type") == "team.error":
renderer.handle_error(payload)
return 1
+ # Unknown/control event types are transported in a chat.final
+ # envelope. They must not consume HumanRenderer's one final
+ # slot or arm the team idle timer.
+ if not is_content_final(payload):
+ continue
renderer.handle_final(payload)
# In team mode, the leader's chat.final is a turn boundary
# but not a terminal event. Start the idle-watch timer so
diff --git a/jiuwenswarm/cli/events.py b/jiuwenswarm/cli/events.py
index 19625b33da..be889a0cd5 100644
--- a/jiuwenswarm/cli/events.py
+++ b/jiuwenswarm/cli/events.py
@@ -7,22 +7,23 @@ from __future__ import annotations
from typing import Any
+def is_content_final(payload: dict[str, Any]) -> bool:
+ """Return whether a chat.final envelope carries the assistant answer."""
+ inner = payload.get("event_type", "")
+ return not inner or inner == "chat.final"
+
+
def is_terminal_event(event_type: str, payload: dict[str, Any]) -> bool:
if event_type == "chat.error":
return True
if event_type == "chat.final":
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 True
+ if inner == "team.error":
+ return True
+ # Gateway uses chat.final as a compatibility envelope for event types
+ # without an EventType mapping (team.runtime_ready, chat.llm_usage,
+ # keepalive, etc.). Those control events do not end the response.
+ return is_content_final(payload)
if event_type == "chat.processing_status":
if not payload.get("is_processing", True):
return True
diff --git a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
index 122b6f88d6..8a2e4dfa7f 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
@@ -1996,7 +1996,15 @@ async def _consume_stream_with_query(
# is preserved.
if parsed.get("event_type") == "chat.final":
tm_ = get_team_manager(channel_id)
- if (not tm_.has_seen_team_events(session_id)) or tm_.is_workflow_completed(session_id):
+ should_finish_round = (
+ (not tm_.has_seen_team_events(session_id))
+ or tm_.is_workflow_completed(session_id)
+ )
+ # Deliver the final content before announcing that the
+ # round is complete. Clients may stop consuming the stream
+ # as soon as processing_status(False) arrives.
+ _broadcast_event(channel_id, session_id, parsed)
+ if should_finish_round:
_broadcast_event(
channel_id,
session_id,
@@ -2008,6 +2016,7 @@ async def _consume_stream_with_query(
"is_complete": True,
},
)
+ continue
_broadcast_event(channel_id, session_id, parsed)
# If stream ended without any chunks, broadcast an error event
diff --git a/tests/agents/swarm/test_swarm_assembly.py b/tests/agents/swarm/test_swarm_assembly.py
index 347e067dab..cadcdbe05f 100644
--- a/tests/agents/swarm/test_swarm_assembly.py
+++ b/tests/agents/swarm/test_swarm_assembly.py
@@ -1490,6 +1490,7 @@ def test_code_runtime_prompt_provider_carries_project_dir(tmp_path: Path) -> Non
ctx = SwarmBuildContext(
mode="code.team",
role="leader",
+ session_id="sess-code-team-runtime",
channel="tui",
project_dir=str(project_dir),
)
@@ -1501,6 +1502,8 @@ def test_code_runtime_prompt_provider_carries_project_dir(tmp_path: Path) -> Non
assert getattr(rail, "_project_dir") == str(project_dir)
assert getattr(rail, "_cwd") == str(project_dir)
+ assert getattr(rail, "_mode") == "code.team"
+ assert getattr(rail, "_session_id") == "sess-code-team-runtime"
def test_team_plan_approval_provider_builds_only_for_leader() -> None:
diff --git a/tests/unit_tests/agentserver/test_team_helpers.py b/tests/unit_tests/agentserver/test_team_helpers.py
index 32660b7c96..3de4c31c3c 100644
--- a/tests/unit_tests/agentserver/test_team_helpers.py
+++ b/tests/unit_tests/agentserver/test_team_helpers.py
@@ -2807,21 +2807,26 @@ async def test_consume_stream_with_query_broadcasts_leader_and_teammate_outputs(
assert [event["event_type"] for event in broadcasted] == [
"chat.processing_status",
"team.runtime_ready",
- "chat.processing_status",
'chat.final',
'chat.processing_status',
'chat.final',
'chat.processing_status',
'chat.final',
'chat.processing_status',
+ "chat.processing_status",
'team.completed',
]
# Round-start processing_status
assert broadcasted[0]["is_processing"] is True
assert broadcasted[0]["is_complete"] is False
# Round-end processing_status (from team.completed)
- assert broadcasted[2]["is_processing"] is False
- assert broadcasted[2]["is_complete"] is True
+ assert broadcasted[-2]["is_processing"] is False
+ assert broadcasted[-2]["is_complete"] is True
+ for index, event in enumerate(broadcasted):
+ if event.get("event_type") == "chat.final":
+ next_event = broadcasted[index + 1]
+ assert next_event["event_type"] == "chat.processing_status"
+ assert next_event["is_processing"] is False
@pytest.mark.anyio
diff --git a/tests/unit_tests/cli/test_chat.py b/tests/unit_tests/cli/test_chat.py
index 98d9b6222d..6f88fa4c3c 100644
--- a/tests/unit_tests/cli/test_chat.py
+++ b/tests/unit_tests/cli/test_chat.py
@@ -712,6 +712,59 @@ class TestInteractiveLoop:
assert code == 0
assert renderer.streamed_text == "Hello world"
+ @pytest.mark.asyncio
+ async def test_team_control_final_envelopes_do_not_hide_answer(self):
+ messages = [
+ {
+ "type": "event",
+ "event": "chat.processing_status",
+ "payload": {"is_processing": True},
+ },
+ {
+ "type": "event",
+ "event": "chat.final",
+ "payload": {"event_type": "team.runtime_ready"},
+ },
+ {
+ "type": "event",
+ "event": "chat.final",
+ "payload": {"event_type": "chat.llm_usage"},
+ },
+ {
+ "type": "event",
+ "event": "chat.delta",
+ "payload": {"content": "当前是集群模式"},
+ },
+ {
+ "type": "event",
+ "event": "chat.final",
+ "payload": {
+ "event_type": "chat.final",
+ "content": "当前是集群模式。",
+ },
+ },
+ {
+ "type": "event",
+ "event": "chat.processing_status",
+ "payload": {"is_processing": False},
+ },
+ ]
+
+ from jiuwenswarm.cli.chat import _run_interactive_loop
+
+ client = await self._make_connected_client(messages)
+ renderer = HumanRenderer()
+ request = {
+ "type": "req", "id": "r1", "method": "chat.send",
+ "is_stream": True,
+ "params": {"session_id": "s1", "content": "hi", "query": "hi",
+ "mode": "team", "cwd": "/tmp", "project_dir": "/tmp",
+ "trusted_dirs": ["/tmp"]},
+ }
+ code = await _run_interactive_loop(client, renderer, request)
+ assert code == 0
+ assert renderer.streamed_text == "当前是集群模式。"
+
@pytest.mark.asyncio
async def test_chat_error_returns_1(self):
messages = [
@@ -754,6 +807,7 @@ class TestInteractiveLoop:
}
code = await _run_interactive_loop(client, renderer, request)
assert code == 0
+ assert renderer.streamed_text == "ok"
@pytest.mark.asyncio
@staticmethod
diff --git a/tests/unit_tests/cli/test_events.py b/tests/unit_tests/cli/test_events.py
index 42ff36089a..05e6a20284 100644
--- a/tests/unit_tests/cli/test_events.py
+++ b/tests/unit_tests/cli/test_events.py
@@ -58,6 +58,12 @@ 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_llm_usage():
+ assert is_terminal_event(
+ "chat.final", {"event_type": "chat.llm_usage"}
+ ) is False
+
@staticmethod
def test_is_terminal_chat_final_team_runtime_ready():
# team.runtime_ready is a control event wrapped in chat.final envelope;