CMDCGateway.SessionStore (cmdc_gateway v0.3.0)

Copy Markdown View Source

ETS-based Session 注册表。

维护 session_id → Agent pid + metadata 映射关系。 通过 monitor 机制在 Agent 进程退出时自动清理对应条目。

metadata 字段

  • :pid — Agent 进程 pid
  • :api_key — 创建此 session 的 API Key
  • :tenant_id — 租户标识(Phase 10 多租户隔离预留,当前默认 "default"
  • :monitor_ref — 进程监控引用
  • :created_at — 创建时间戳(毫秒)
  • :blueprint — 使用的 Blueprint 模块名(可选)
  • :model — LLM 模型标识

Summary

Functions

Returns a specification to start this module under a supervisor.

返回当前活跃 session 数量。

注册一个新 session。

删除一个 session,取消进程监控。

获取 session 元数据。

列出所有活跃 session(返回 [{session_id, metadata}])。

按 api_key 过滤 session。

按 tenant_id 过滤 session。

启动 SessionStore GenServer。

Types

session_metadata()

@type session_metadata() :: %{
  pid: pid(),
  api_key: String.t(),
  tenant_id: String.t(),
  monitor_ref: reference(),
  created_at: integer(),
  blueprint: module() | nil,
  model: String.t() | nil
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count()

@spec count() :: non_neg_integer()

返回当前活跃 session 数量。

create(session_id, pid, attrs \\ [])

@spec create(String.t(), pid(), keyword()) ::
  {:ok, String.t()} | {:error, :already_exists}

注册一个新 session。

参数

  • session_id — 唯一会话标识
  • pid — Agent 进程 pid
  • attrs — 额外属性 keyword,支持 :api_key:tenant_id:blueprint:model

返回

  • {:ok, session_id} — 注册成功
  • {:error, :already_exists} — session_id 已存在

delete(session_id)

@spec delete(String.t()) :: :ok

删除一个 session,取消进程监控。

返回 :ok(无论 session 是否存在)。

get(session_id)

@spec get(String.t()) :: {:ok, session_metadata()} | {:error, :not_found}

获取 session 元数据。

返回 {:ok, metadata}{:error, :not_found}

list()

@spec list() :: [{String.t(), session_metadata()}]

列出所有活跃 session(返回 [{session_id, metadata}])。

list_by_api_key(target_api_key)

@spec list_by_api_key(String.t()) :: [{String.t(), session_metadata()}]

按 api_key 过滤 session。

list_by_tenant(target_tenant_id)

@spec list_by_tenant(String.t()) :: [{String.t(), session_metadata()}]

按 tenant_id 过滤 session。

Phase 10 多租户隔离核心查询路径。

start_link(opts \\ [])

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

启动 SessionStore GenServer。