IM session strategies
IM 会话策略
IM deployments differ on how incoming chat messages map to agent sessions. A per-chat conversation deployment gives each chat its own persistent session and delegates long work to background tasks, whereas a multi-chat triage deployment wants one shared dispatcher session to read addressed messages across chats and reply or delegate work from there. Both models need the same message admission, durability, outbound re
English
Problem
IM deployments differ on how incoming chat messages map to agent sessions. A per-chat conversation deployment gives each chat its own persistent session and delegates long work to background tasks, whereas a multi-chat triage deployment wants one shared dispatcher session to read addressed messages across chats and reply or delegate work from there. Both models need the same message admission, durability, outbound receipts, and authorized im_* tools. Hard-coding a single routing model inside the IM host would force a fork of the host for any other routing model.
Decision
The IM service owns platform intake, durability, session hosting (ctx.im.sessions), outbound publication (ctx.im.send/stream), and the im_* tools, while session selection is handled by listeners on the im/message waterfall (and the optional built-in defaultDispatch fallback on @deepseek-ai/dsh-experimental-im).
The host's own fallback lives in dispatcher.ts: with defaultDispatch configured, addressed messages no listener claimed reach a single shared session (im:dispatcher) that accumulates bindings for each chat it hears and receives a <im-chat key="…" source="…" type="…" /> header before each message. Read and publication tools accept an optional chat parameter so a multi-chat session can target any chat in its bindings.
Alternatives considered
Hard-coding per-chat sessions in @deepseek-ai/dsh-experimental-im. Baking conversation:<chatKey> routing into the host would prevent one-shot command plugins, custom routing plugins, or a shared dispatcher session from using the same intake and session store.
Listing dynamically accumulated chats inside a static system prompt. A dispatcher session's bound chats grow as new chats send messages; prefixing each delivered message with <im-chat key="…" /> tells the model which chat sent the message without rewriting earlier turns.
Consequences
Each session key in im_sessions is owned by the owner name that created it (dispatcher for the default fallback, or the name a plugin picks), and ctx.im.sessions.ensure rejects attempts by a different owner to claim an existing key. A shared dispatcher session serializes turns across all chats it serves.
中文
Problem
不同的 IM 部署对入站聊天消息如何映射到 agent 会话有不同需求。按聊天隔离的部署为每个聊天维护一个独立的持久化会话,并将耗时工作委派给后台任务;而集中处理多个聊天的部署则希望由一个共享的分派会话统一接收各聊天的消息并负责回复或委派。两种模式都需要相同的消息准入、持久化存储、发送回执与 im_* 授权工具。如果将单一路由模型写死在 IM 宿主中,其他路由模式就必须复制整套宿主逻辑。
Decision
IM 服务 负责平台接收、持久化存储、会话托管(ctx.im.sessions)、消息发送(ctx.im.send/stream)与 im_* 工具,而消息进入哪个会话则由 im/message 事件链上的监听器(以及 @deepseek-ai/dsh-experimental-im 上可选的 defaultDispatch 兜底)决定。
宿主自带的兜底处理器位于 dispatcher.ts:配置了 defaultDispatch 后,没有被任何监听器认领且提及了机器人的消息会进入全局共享的 im:dispatcher 会话;该会话自动将见过的每个聊天加入绑定列表,并在每条消息前附上 <im-chat key="…" source="…" type="…" /> 头部。所有读取与发送工具均支持可选的 chat 参数,以便绑定了多个聊天的会话指定目标聊天。
Alternatives considered
在 @deepseek-ai/dsh-experimental-im 中写死按聊天路由。 将 conversation:<chatKey> 路由硬编码进宿主会导致单次命令插件、自定义路由插件或全局共享分派会话无法复用同一套接收与会话存储。
仅靠静态系统提示词标识来源聊天。 分派会话绑定的聊天随新消息到达而增加;在每条投递的消息前加上 <im-chat key="…" /> 头部,可以直接告知模型当前消息所属的聊天,而无需重写之前的轮次记录。
Consequences
im_sessions 中的每个会话键都记录创建它的 owner 名称(默认兜底为 dispatcher,其他插件可自取名称),当不同 owner 尝试使用已有键调用 ctx.im.sessions.ensure 时会被拒绝。共享的 dispatcher 会话在单个会话内串行处理多个聊天的轮次。