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: 企业claw场景gateway支持两种session生成模式、修改session_id间隔符为双冒号

yukuanj9ecb51fb5cba
diff --git a/jiuwenclaw/gateway/message_handler.py b/jiuwenclaw/gateway/message_handler.py
index 02e9dd8e46..93f4346eda 100644
--- a/jiuwenclaw/gateway/message_handler.py
+++ b/jiuwenclaw/gateway/message_handler.py
@@ -83,7 +83,7 @@ class MessageHandler(ABC):
ChannelType.WHATSAPP.value,
ChannelType.WECOM.value,
}
- # 使用 SessionMap(identity 四元组)的 channel 族:channel_id 等于 base 或为 base:<suffix>
+ # 使用 SessionMap 的 channel 族(由 config 中 gateway.session_map_scope 决定是否在 key 中含 user)
self._session_map_channel_types = frozenset({
"feishu_enterprise",
})
diff --git a/jiuwenclaw/gateway/session_map.py b/jiuwenclaw/gateway/session_map.py
index edd381c174..62a25a3934 100644
--- a/jiuwenclaw/gateway/session_map.py
+++ b/jiuwenclaw/gateway/session_map.py
@@ -3,25 +3,67 @@ from __future__ import annotations
import json
import secrets
import time
+from enum import Enum
from pathlib import Path
from jiuwenclaw.utils import get_checkpoint_dir, logger
-def _make_key(provider: str, chat_id: str, bot_id: str, user_id: str) -> str:
+class SessionMapScope(str, Enum):
+ """How SessionMap keys and agent session_id strings are derived from inbound identity."""
+
+ # (default) One agent session per (provider, chat, bot); users in the same chat share context.
+ PER_CHAT_BOT = "per_chat_bot"
+ # One agent session per (provider, chat, bot, user)
+ PER_CHAT_BOT_USER = "per_chat_bot_user"
+
+
+def load_session_map_scope() -> SessionMapScope:
+ default = SessionMapScope.PER_CHAT_BOT
+ try:
+ from jiuwenclaw.config import get_config
+
+ raw = str((get_config().get("gateway") or {}).get("session_map_scope") or default.value).strip().lower()
+ return SessionMapScope(raw)
+ except ValueError:
+ logger.warning("Unknown gateway.session_map_scope %r, using %s", raw, default.value)
+ return default
+ except Exception as exc: # noqa: BLE001
+ logger.warning("SessionMap scope load failed, using %s: %s", default.value, exc)
+ return default
+
+
+def _make_key(
+ scope: SessionMapScope,
+ provider: str,
+ chat_id: str,
+ bot_id: str,
+ user_id: str,
+) -> str:
+ if scope == SessionMapScope.PER_CHAT_BOT:
+ return f"{provider}::{chat_id}::{bot_id}"
return f"{provider}::{chat_id}::{bot_id}::{user_id}"
-def _make_session_id(provider: str, chat_id: str, bot_id: str, user_id: str) -> str:
+def _make_session_id(
+ scope: SessionMapScope,
+ provider: str,
+ chat_id: str,
+ bot_id: str,
+ user_id: str,
+) -> str:
ts = format(int(time.time() * 1000), "x")
suffix = secrets.token_hex(3)
- return f"{provider}_{chat_id}_{bot_id}_{user_id}_{ts}_{suffix}"
+ if scope == SessionMapScope.PER_CHAT_BOT:
+ return f"{provider}::{chat_id}::{bot_id}::{ts}::{suffix}"
+ return f"{provider}::{chat_id}::{bot_id}::{user_id}::{ts}::{suffix}"
class SessionMap:
- """Simple mapping: (provider,chat_id,bot_id,user_id) -> session_id."""
+ """Map stable identity (per config scope) -> rotating agent ``session_id``."""
- def __init__(self) -> None:
+ def __init__(self, *, scope: SessionMapScope | None = None) -> None:
+ self._scope = scope if scope is not None else load_session_map_scope()
self._store_path: Path = get_checkpoint_dir() / "session_map.json"
self._mapping: dict[str, str] = {}
self._load()
@@ -54,12 +96,12 @@ class SessionMap:
*,
rotate: bool = False,
) -> str:
- key = _make_key(provider, chat_id, bot_id, user_id)
+ key = _make_key(self._scope, provider, chat_id, bot_id, user_id)
existing = self._mapping.get(key)
if existing and not rotate:
return existing
- sid = _make_session_id(provider, chat_id, bot_id, user_id)
+ sid = _make_session_id(self._scope, provider, chat_id, bot_id, user_id)
if existing == sid:
return sid
self._mapping[key] = sid
diff --git a/jiuwenclaw/resources/config.yaml b/jiuwenclaw/resources/config.yaml
index 0cb69237c5..adf95e651b 100644
--- a/jiuwenclaw/resources/config.yaml
+++ b/jiuwenclaw/resources/config.yaml
@@ -131,6 +131,11 @@ task_memory:
retrieval_algo: ACE
summary_algo: ACE
+gateway:
+ # SessionMap 仅用于企业claw场景,如 feishu_enterprise 等通道:将平台身份映射为 Agent 的 session_id。
+ # 取值:per_chat_bot(默认,同 chat+bot 共享会话), per_chat_bot_user(按 user 分会话)
+ session_map_scope: per_chat_bot
+
heartbeat:
# 心跳间隔(秒),默认 60
every: 3600