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

Enhance HttpSseAgentServerClient by introducing a new method `_stream_scope` to identify streams based on session details, improving session management and preventing cross-cancellation of requests. This refactor simplifies the existing logic in `_supersede_other_streams` for better readability and maintainability.

NickFuryXXX573f0b1f686e
diff --git a/jiuwenswarm/gateway/routing/http_agent_client.py b/jiuwenswarm/gateway/routing/http_agent_client.py
index 9f81bf4121..273bbb7deb 100644
--- a/jiuwenswarm/gateway/routing/http_agent_client.py
+++ b/jiuwenswarm/gateway/routing/http_agent_client.py
@@ -310,6 +310,19 @@ class HttpSseAgentServerClient(AgentServerClient):
def _is_stream_cancelled(self, rid: str) -> bool:
return bool(rid) and rid in self._cancelled_request_ids
+ def _stream_scope(self, envelope: E2AEnvelope) -> tuple[str, ...] | None:
+ """Identify a stream by session so shared clients do not cross-cancel."""
+ if not envelope.session_id or not str(envelope.user_id or "").strip():
+ return None
+ return (
+ str(envelope.channel or ""),
+ str(envelope.user_id or ""),
+ str(envelope.agent_id or ""),
+ str(envelope.service_id or ""),
+ str(envelope.workspace_key or ""),
+ str(envelope.session_id or ""),
+ )
+
def _supersede_other_streams(
self, rid: str, scope: tuple[str, ...] | None
) -> None:
@@ -366,13 +379,7 @@ class HttpSseAgentServerClient(AgentServerClient):
)
# This client is shared across users and Runtime-routed Pods. A new
# request must never cancel another session merely by sharing the client.
- scope = (
- tuple(str(value or "") for value in (
- envelope.channel, envelope.user_id, envelope.agent_id,
- envelope.service_id, envelope.workspace_key, envelope.session_id,
- ))
- if envelope.session_id and str(envelope.user_id or "").strip() else None
- )
+ scope = self._stream_scope(envelope)
async with self._stream_lock:
self._supersede_other_streams(rid, scope)
if rid: