L1 词汇表:全框架只说一套话¶
core/types.py 定义所有跨模块流动的数据。它被复用最多,所以字段一旦落库就是数据迁移——
schema_version 因此从第一天就埋好。
一条消息长什么样¶
Message(
role="assistant", # system / user / assistant / tool
content="我先查一下天气。", # str 或 list[TextPart | ImagePart]
thinking=None, # 思考内容,默认不回传后续轮
tool_calls=[ToolCall(id="c1", name="get_weather", arguments='{"city":"上海"}')],
)
| 字段 | 作用 |
|---|---|
schema_version |
落库词汇表版本;补字段 = 数据迁移,先有版本号 |
id |
消息级唯一标识,SessionStore 幂等去重的依据 |
content |
文本或 Part 列表;text_content() 取纯文本部分 |
thinking / thinking_signature |
thinking 内容与 Anthropic 回传签名(非 Anthropic 恒 None) |
tool_calls / tool_call_id |
assistant 发起的调用 / role="tool" 对应的调用 id |
tool_is_error / tool_untrusted / tool_handle |
工具结果元数据,随消息落库、跨 run 不丢 |
Part 是多模态的扩展点:
TextPart(type="text", text="...")
ImagePart(type="image", url="https://...") # 或 data=base64 + media_type
Part = Annotated[TextPart | ImagePart, Field(discriminator="type")]
v1 只支持图像输入;新增类型沿 discriminator 向前兼容。
Usage:五个字段,一个口径¶
Usage(
input_tokens=1200, # 只统计「未命中缓存」的输入
output_tokens=300,
cache_creation_tokens=0, # Anthropic cache_creation
cache_read_tokens=800,
reasoning_tokens=0, # 思考 token(若厂商单列)
)
input_tokens 只记未命中缓存的输入是全框架最容易被抄错的口径:OpenAI 系的
prompt_tokens、Gemini 的 promptTokenCount 都把命中部分算在里面,方言层用
Usage.from_prompt_total 折减后才进预算。否则同一批 token 会被计两遍,预算提前触发。
工具三件套¶
| 类型 | 谁产生 | 内容 |
|---|---|---|
ToolSpec |
@tool |
给模型看的 JSON Schema:name / description / parameters / timeout / strict / idempotent |
ToolCall |
模型 | id / name / arguments(原文 JSON) |
ToolResult |
工具管道 | call_id / content(统一标记包裹)/ is_error / untrusted / handle |
不变量:assistant 消息里的每个 tool_call,最终都有对应的 tool result 消息——
来源只有三种:执行结果、Hook deny 回填、终态未执行回填。
运行级类型¶
RunConfig(max_turns=None, token_budget=None, wall_clock=None,
approval_ttl=None, parent_run_id=None, no_progress_steps=5)
RunResult(run_id, status="finished", message, usage, finish_reason, pending, output)
RunEvent(run_id, parent_run_id, step_id, seq, type, payload, ts)
RunResult.status只区分finished/pending;细分终态看finish_reason(stop/max_turns/no_progress/budget/wall_clock/guardrail/cancelled)。RunEvent的全局序是(step_id, seq),ts只作墙钟参考。seq由 Runner 在事件循环内 分配,外挂 sink 只收不改号。RunSnapshot是挂起 / 崩溃恢复共用的一条快照路径:全量消息 + 步号 + 待审批集合 + 原 run 配置。
顶层重导出预算¶
from mainspring import ... 只给 20 个最常用符号(Agent / Runner / Message / ToolCall /
ToolResult / Usage / tool 等);其余符号从子模块取(from mainspring.model import TestModel)。
这条预算由 tests/test_public_api.py 的双层快照守住——新增公开符号必须显式改快照。