Agent 蓝图声明式定义,可复用的 Agent 配置模板。
Blueprint 是一种声明式的 Agent 配置规格,包含身份、工具集、Plugin 列表、 Skills 目录、Sandbox 等所有启动参数。采用 behaviour + struct + 管道构建器模式,无 DSL 宏。
三种用法
用法 1:实现 behaviour(推荐)
defmodule MyApp.CodingAgent do
@behaviour CMDC.Blueprint
@impl true
def build(_opts) do
CMDC.Options.new!(
model: "anthropic:claude-sonnet-4-5",
tools: [CMDC.Tool.ReadFile, CMDC.Tool.WriteFile, CMDC.Tool.Shell],
plugins: [CMDC.Plugin.Builtin.SecurityGuard],
system_prompt: "你是一个专业的 Elixir 开发者。",
skills_dirs: ["./.cmdc/skills"],
working_dir: "."
)
end
end
# 使用
opts = MyApp.CodingAgent.build([])
{:ok, agent} = CMDC.create_agent(opts)用法 2:直接构造 struct 并通过管道组合
blueprint =
%CMDC.Blueprint{
name: "my-agent",
model: "anthropic:claude-sonnet-4-5",
system_prompt: "你是一个专业助手。"
}
|> CMDC.Blueprint.add_tools([CMDC.Tool.ReadFile])
|> CMDC.Blueprint.add_plugins([CMDC.Plugin.Builtin.SecurityGuard])
|> CMDC.Blueprint.add_skills_dirs(["./.cmdc/skills"])
opts = CMDC.Blueprint.to_options(blueprint)
{:ok, agent} = CMDC.create_agent(opts)用法 3:运行时覆盖(多租户场景)
opts = MyApp.CodingAgent.build(
system_prompt_suffix: "当前用户:Alice",
provider_api_key: "sk-..."
)
Summary
Callbacks
构建 Agent 启动选项。
Functions
追加 Memory 文件路径列表(去重)。
追加 Plugin 列表(按模块去重)。
追加 Skills 目录列表(去重)。
追加 SubAgent 声明式规格列表(按 name 去重)。
追加工具模块列表(按模块去重)。
覆盖蓝图字段,返回新 struct。
将 Blueprint struct 编译为 CMDC.Options.t()。
Types
@type t() :: %CMDC.Blueprint{ max_tokens: pos_integer() | nil, max_turns: pos_integer(), memory: [String.t()], model: String.t(), name: String.t(), plugins: [CMDC.Options.plugin_spec()], provider_opts: keyword(), sandbox: module() | nil, skills_dirs: [String.t()], subagents: [CMDC.SubAgent.t()], system_prompt: String.t() | nil, tools: [module()], working_dir: String.t() }
Callbacks
@callback build(opts :: keyword()) :: CMDC.Options.t()
构建 Agent 启动选项。
接收运行时 opts(可选覆盖),返回 CMDC.Options.t()。
参数
:system_prompt_suffix— 追加到 system_prompt 末尾:provider_api_key— 覆盖 provider_opts 的 api_key:extra_plugins— 追加到 plugins 列表:extra_tools— 追加到 tools 列表
Functions
追加 Memory 文件路径列表(去重)。
@spec add_plugins(t(), [CMDC.Options.plugin_spec()]) :: t()
追加 Plugin 列表(按模块去重)。
追加 Skills 目录列表(去重)。
@spec add_subagents(t(), [CMDC.SubAgent.t()]) :: t()
追加 SubAgent 声明式规格列表(按 name 去重)。
追加工具模块列表(按模块去重)。
覆盖蓝图字段,返回新 struct。
@spec to_options( t(), keyword() ) :: CMDC.Options.t()
将 Blueprint struct 编译为 CMDC.Options.t()。
支持运行时覆盖参数(多租户场景):
| key | 类型 | 说明 |
|---|---|---|
:system_prompt_suffix | String.t() | 追加到 system_prompt 末尾(换行分隔) |
:provider_api_key | String.t() | 注入 provider_opts 的 api_key |
:extra_plugins | [plugin_spec()] | 追加到 plugins 列表(按模块去重) |
:extra_tools | [module()] | 追加到 tools 列表(去重) |
示例
opts = CMDC.Blueprint.to_options(blueprint,
system_prompt_suffix: "当前用户:Alice",
provider_api_key: "sk-tenant-xxx"
)