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

feat: 增加屏蔽私聊对话

alan_cheng36c305f7df87
diff --git a/jiuwenclaw/server/runtime/agent_adapter/team_helpers.py b/jiuwenclaw/server/runtime/agent_adapter/team_helpers.py
index ad4173ca6f..7798bfc9ce 100644
--- a/jiuwenclaw/server/runtime/agent_adapter/team_helpers.py
+++ b/jiuwenclaw/server/runtime/agent_adapter/team_helpers.py
@@ -52,6 +52,21 @@ _TEAM_CREATE_KINDS = {
RunActionKind.CREATE.value,
RunActionKind.NEW_TEAM_IN_SESSION.value,
}
+_HIDE_DM_PREFIX = "/hide_dm"
+
+
+def _extract_hide_dm_directive(query: str) -> tuple[str, bool]:
+ """Strip a leading ``/hide_dm`` directive from the first team query.
+
+ Returns the cleaned query and whether the directive was present.
+ """
+ stripped = query.lstrip()
+ if not stripped.startswith(_HIDE_DM_PREFIX):
+ return query, False
+ remainder = stripped[len(_HIDE_DM_PREFIX):]
+ if remainder and not remainder[0].isspace():
+ return query, False
+ return remainder.lstrip(), True
def sync_team_identity_metadata(
@@ -96,6 +111,7 @@ async def ensure_monitor_for_active_runtime(
channel_id: str | None,
session_id: str,
team_name: str,
+ hide_dm: bool = False,
) -> None:
"""Attach TeamMonitorHandler using the public Runner team monitor accessor."""
tm = get_team_manager(channel_id)
@@ -103,7 +119,11 @@ async def ensure_monitor_for_active_runtime(
if existing is not None and existing.is_running:
return
- monitor = await Runner.get_agent_team_monitor(team_name=team_name, session_id=session_id)
+ monitor = await Runner.get_agent_team_monitor(
+ team_name=team_name,
+ session_id=session_id,
+ hide_dm=hide_dm,
+ )
if monitor is None:
logger.warning(
"[TeamHelpers] active team monitor unavailable: channel_id=%s session_id=%s team_name=%s",
@@ -537,6 +557,17 @@ async def process_team_message_stream(
is_first_request = not team_manager.has_stream_task(session_id)
request_queue: asyncio.Queue | None = None
+ hide_dm = False
+ if is_first_request:
+ query, hide_dm = _extract_hide_dm_directive(str(query or ""))
+ if hide_dm:
+ logger.info(
+ "[TeamHelpers] hide_dm directive captured for first team request: "
+ "channel_id=%s session_id=%s",
+ _resolve_channel_id(channel_id),
+ session_id,
+ )
+
slash_result = await _handle_team_slash_command(
channel_id,
session_id,
@@ -654,6 +685,7 @@ async def process_team_message_stream(
session_id,
team_spec,
query,
+ hide_dm=hide_dm,
)
)
team_manager.register_stream_task(session_id, stream_task)
@@ -767,6 +799,7 @@ async def _consume_stream_with_query(
session_id: str,
team_spec: Any,
initial_query: str,
+ hide_dm: bool = False,
) -> None:
"""Consume the team stream in the background and broadcast parsed events."""
received_chunks = 0
@@ -807,6 +840,7 @@ async def _consume_stream_with_query(
channel_id,
session_id,
ready_team_name,
+ hide_dm=hide_dm,
)
ensure_team_evolution_watcher(
channel_id,
diff --git a/tests/unit_tests/agentserver/test_team_helpers.py b/tests/unit_tests/agentserver/test_team_helpers.py
index 20742d0fe4..a1dcd09edb 100644
--- a/tests/unit_tests/agentserver/test_team_helpers.py
+++ b/tests/unit_tests/agentserver/test_team_helpers.py
@@ -10,6 +10,7 @@ from types import SimpleNamespace
import pytest
from openjiuwen.agent_teams.schema.team import TeamRole
+from jiuwenclaw.agents.harness.team import TeamMonitorHandler
from jiuwenclaw.server.runtime.agent_adapter import team_helpers
@@ -945,7 +946,7 @@ async def test_consume_stream_with_query_broadcasts_only_leader_team_outputs(mon
run_agent_team_streaming = staticmethod(_fake_stream)
@staticmethod
- async def get_agent_team_monitor(team_name: str, session_id: str):
+ async def get_agent_team_monitor(team_name: str, session_id: str, hide_dm: bool = False):
return None
class _FakeManager:
@@ -999,6 +1000,99 @@ async def test_consume_stream_with_query_broadcasts_only_leader_team_outputs(mon
assert broadcasted[1]["content"] == "leader answer"
+def test_extract_hide_dm_directive_strips_prefix_and_flags():
+ cleaned, hide_dm = team_helpers._extract_hide_dm_directive( # pylint: disable=protected-access
+ "/hide_dm please summarize"
+ )
+ assert hide_dm is True
+ assert cleaned == "please summarize"
+
+
+def test_extract_hide_dm_directive_ignores_non_prefix():
+ cleaned, hide_dm = team_helpers._extract_hide_dm_directive( # pylint: disable=protected-access
+ "/hide_dmsomething else"
+ )
+ assert hide_dm is False
+ assert cleaned == "/hide_dmsomething else"
+
+
+def test_extract_hide_dm_directive_handles_bare_directive():
+ cleaned, hide_dm = team_helpers._extract_hide_dm_directive("/hide_dm") # pylint: disable=protected-access
+ assert hide_dm is True
+ assert cleaned == ""
+
+
+async def test_consume_stream_with_query_propagates_hide_dm_to_monitor(monkeypatch):
+ captured: dict[str, object] = {}
+
+ async def _fake_stream(**kwargs):
+ yield SimpleNamespace(
+ type="team.runtime_ready",
+ payload={
+ "event_type": "team.runtime_ready",
+ "team_name": "demo-team",
+ "activation_kind": "create",
+ },
+ role=TeamRole.LEADER,
+ )
+
+ class _FakeRunner:
+ run_agent_team_streaming = staticmethod(_fake_stream)
+
+ @staticmethod
+ async def get_agent_team_monitor(team_name: str, session_id: str, hide_dm: bool = False):
+ captured["team_name"] = team_name
+ captured["session_id"] = session_id
+ captured["hide_dm"] = hide_dm
+ return None
+
+ class _FakeManager:
+ @staticmethod
+ def commit_runtime_ready(session_id: str, team_name: str) -> None:
+ pass
+
+ @staticmethod
+ def clear_pending_runtime(session_id: str) -> None:
+ pass
+
+ @staticmethod
+ def pop_stream_task(session_id: str) -> None:
+ pass
+
+ @staticmethod
+ def get_monitor(session_id: str):
+ return None
+
+ @staticmethod
+ async def attach_distributed_hooks_for_runner_runtime(
+ team_name: str,
+ session_id: str,
+ channel_id: str,
+ ) -> None:
+ pass
+
+ monkeypatch.setattr(team_helpers, "Runner", _FakeRunner)
+ monkeypatch.setattr(team_helpers, "get_team_manager", lambda channel_id: _FakeManager())
+ monkeypatch.setattr(team_helpers, "_broadcast_event", lambda *args, **kwargs: None)
+ monkeypatch.setattr(team_helpers, "get_session_metadata", lambda session_id: {})
+ monkeypatch.setattr(team_helpers, "update_session_metadata", lambda **kwargs: None)
+
+ await team_helpers._consume_stream_with_query( # pylint: disable=protected-access
+ "web",
+ "sess-hide-dm",
+ SimpleNamespace(team_name="demo-team"),
+ "hello",
+ hide_dm=True,
+ )
+
+ assert captured == {
+ "team_name": "demo-team",
+ "session_id": "sess-hide-dm",
+ "hide_dm": True,
+ }
+
+
@pytest.mark.anyio
async def test_handle_team_slash_command_requires_explicit_evolve_intent(monkeypatch):
class _FakeStore: