CMDC.SessionServer (cmdc v0.5.0)

Copy Markdown View Source

每个会话的进程树监督者。

每个会话拥有独立的监督子树:

CMDC.SessionServer (Supervisor, :one_for_one)
 CMDC.SubAgent.Supervisor    子代理动态监督者
 CMDC.Agent                   Agent 状态机

终止 SessionServer 会清理所有资源:主 Agent 和所有子代理进程。

必要选项

  • :session_id — 唯一会话标识符(字符串)

可选选项

  • :blueprint — Blueprint 模块(实现 CMDC.Blueprint behaviour)或 CMDC.Blueprint.t() struct, 自动调用 build([]) 展开为启动选项。Blueprint 展开的选项优先级低于显式传入的同名选项。
  • :working_dir — 工具执行的基础目录,默认当前目录
  • :model — LLM 模型 ID,格式 "provider:model_id"
  • :system_prompt — 系统提示词
  • :tools — Tool 模块列表
  • :plugins — Plugin 列表
  • :configCMDC.Config.t() struct
  • :provider_opts — Provider 初始化选项

示例

直接传选项:

{:ok, server} = CMDC.SessionServer.start_link(
  session_id: "s-001",
  model: "anthropic:claude-sonnet-4-5",
  working_dir: "/project"
)
agent_pid = CMDC.SessionServer.agent(server)

通过 Blueprint 模块启动:

{:ok, server} = CMDC.SessionServer.start_link(
  session_id: "s-002",
  blueprint: MyApp.Agents.Translator
)
agent_pid = CMDC.SessionServer.agent(server)
CMDC.Agent.prompt(agent_pid, "Hello")

Summary

Functions

从 SessionServer 监督者获取主 Agent pid。

Returns a specification to start this module under a supervisor.

向主 Agent 发送 prompt,返回结果。

启动会话监督者。

从 SessionServer 获取 SubAgent.Supervisor pid。

通过 session_id 字符串反查 SessionServer pid。

Functions

agent(session_server)

@spec agent(pid()) :: pid() | nil

从 SessionServer 监督者获取主 Agent pid。

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

prompt(session_server, text)

@spec prompt(pid(), String.t()) :: map()

向主 Agent 发送 prompt,返回结果。

start_link(opts)

@spec start_link(keyword()) :: Supervisor.on_start()

启动会话监督者。

必须在 opts 中提供 :session_id(字符串)。SessionServer 进程会以 {:via, Registry, {CMDC.SessionRegistry, session_id}} 注册,用于通过 字符串 session_id 反查 pid(Registry.lookup/2)。

同一 session_id 重复启动会返回 {:error, {:already_started, pid}}

sub_agent_supervisor(session_server)

@spec sub_agent_supervisor(pid()) :: pid() | nil

从 SessionServer 获取 SubAgent.Supervisor pid。

whereis(session_id)

@spec whereis(String.t()) :: pid() | nil

通过 session_id 字符串反查 SessionServer pid。

返回 pid 或 nil(session 未运行 / 已被 GC)。