CMDC.Message (cmdc v0.4.0)

Copy Markdown View Source

Agent 会话中的消息结构体。

消息在 Agent Loop 中流转,承载用户、助手和工具执行之间的内容。 每种消息由 role 字段区分语义:

  • :system — 系统级指令(系统提示词、压缩指令等)
  • :user — 用户发出的文本消息
  • :assistant — 助手回复,包含文本和可选的工具调用列表
  • :tool_result — 工具执行结果,通过 call_id 与调用关联

每条消息在构建时自动分配唯一 ID。

Summary

Functions

创建助手消息,可附带工具调用列表。opts 支持 :thinking 键。

创建系统消息。

将消息序列化为普通 map,适合 JSON 编码。可选字段仅在存在时包含。

创建工具结果消息,is_error 标识是否为错误输出。

创建用户消息。

Types

role()

@type role() :: :system | :user | :assistant | :tool_result

t()

@type t() :: %CMDC.Message{
  call_id: String.t() | nil,
  content: String.t() | nil,
  id: String.t(),
  is_error: boolean(),
  metadata: map() | nil,
  name: String.t() | nil,
  parent_id: String.t() | nil,
  role: role(),
  thinking: String.t() | nil,
  tool_calls: [tool_call()] | nil
}

tool_call()

@type tool_call() :: %{call_id: String.t(), name: String.t(), arguments: map()}

Functions

assistant(content, tool_calls \\ [], opts \\ [])

@spec assistant(String.t() | nil, [tool_call()], keyword()) :: t()

创建助手消息,可附带工具调用列表。opts 支持 :thinking 键。

system(content)

@spec system(String.t()) :: t()

创建系统消息。

to_map(msg)

@spec to_map(t()) :: map()

将消息序列化为普通 map,适合 JSON 编码。可选字段仅在存在时包含。

tool_result(call_id, output, is_error \\ false)

@spec tool_result(String.t(), String.t(), boolean()) :: t()

创建工具结果消息,is_error 标识是否为错误输出。

user(content)

@spec user(String.t()) :: t()

创建用户消息。