CMDC.SubAgent (cmdc v0.4.1)

Copy Markdown View Source

子代理声明式规格 struct。

在创建父 Agent 时通过 CMDC.Options:subagents 字段声明。 字段值为 nil 表示继承父 Agent 的对应配置。

快速开始

subagent = CMDC.SubAgent.new!(
  name: "coder",
  description: "专门负责写代码的子代理",
  model: "anthropic:claude-opus-4-5",
  tools: [CMDC.Tool.ReadFile, CMDC.Tool.WriteFile, CMDC.Tool.Shell]
)

CMDC.Options.new!(
  model: "anthropic:claude-sonnet-4-5",
  subagents: [subagent]
)

字段说明

  • :name — 必填,子代理唯一标识名,用于 tool_task 调用时指定目标
  • :description — 对父 Agent 可见的描述,用于 Task Tool 的工具描述生成
  • :system_prompt — 子代理的系统提示词;nil 则继承父 Agent 的系统提示词
  • :model — 子代理使用的模型;nil 则继承父 Agent 的 model
  • :tools — 子代理可用工具列表;nil 则继承父 Agent 的 tools
  • :plugins — 子代理的 Plugin 列表;nil 则继承父 Agent 的 plugins
  • :skills_dirs — 子代理的 Skills 目录;nil 则继承父 Agent 的 skills_dirs
  • :user_data — 子代理的业务上下文 map;nil 则继承父 Agent 的 user_data
  • :prompt_mode — 系统提示词模式;默认 :task(SubAgent 默认省 token), 可显式传入 :full | :task | :minimal | :none 覆盖,或传 nil 继承父 Agent

Summary

Functions

从 keyword list 构建 SubAgent struct,返回 {:ok, t()}{:error, ...}

从 keyword list 构建 SubAgent struct,校验失败时抛出异常。

将子代理规格与父 Agent Options 合并,nil 字段自动继承父值。

Types

plugin_spec()

@type plugin_spec() :: module() | {module(), keyword()}

t()

@type t() :: %CMDC.SubAgent{
  description: String.t() | nil,
  model: String.t() | nil,
  name: String.t(),
  plugins: [plugin_spec()] | nil,
  prompt_mode: CMDC.Options.prompt_mode() | nil,
  skills_dirs: [String.t()] | nil,
  system_prompt: String.t() | nil,
  tools: [module()] | nil,
  user_data: map() | nil
}

Functions

new(opts)

@spec new(keyword()) :: {:ok, t()} | {:error, NimbleOptions.ValidationError.t()}

从 keyword list 构建 SubAgent struct,返回 {:ok, t()}{:error, ...}

new!(opts)

@spec new!(keyword()) :: t()

从 keyword list 构建 SubAgent struct,校验失败时抛出异常。

示例

iex> CMDC.SubAgent.new!(name: "researcher", description: "负责信息收集")
%CMDC.SubAgent{name: "researcher", description: "负责信息收集", model: nil, ...}

to_options(sub, parent)

@spec to_options(t(), CMDC.Options.t()) :: CMDC.Options.t()

将子代理规格与父 Agent Options 合并,nil 字段自动继承父值。

用于 Task Tool 启动子代理进程时构建完整 Options。