mainspring.types¶
统一词汇表:Message / Usage / RunEvent / 工具三件套 / 检查点与运行配置
本页由
scripts/gen_api_docs.py从模块__all__自动生成,请勿手改。 新增或删除公开符号:先改模块__all__与tests/test_public_api.py快照, 再重新生成本页。
统一词汇表:全框架唯一的类型定义层,被复用最多的资产。
按统一词汇表的定稿约定落地(T1-1 修订版 §2.1~§2.7 / §2.11):
TextPart / ImagePart / Part 多模态 Part discriminated union(v1 仅图像输入)
Message schema_version + id + role + content + thinking
+ thinking_signature + tool_calls + tool 元数据三字段
Usage 五字段:input/output/cache_creation/cache_read/reasoning
ModelResponse message + usage + finish_reason(模型级词表,映射见 §2.4)
RunEvent schema_version + run_id + parent_run_id + step_id + seq + ts
工具三件套 ToolSpec / ToolCall / ToolResult(§2.7,六轮终审整组归本模块:
ChatModel.chat 引 ToolSpec + 工具重试引 RetryPolicy,
拆置 tool.py 会构成 model.py ↔ tool.py 环)
HookVerdict allow / deny / require_approval 三态(§2.2)
PendingCall call + reason,待审批调用持久化载体(§2.6,reason 随快照落库)
RunSnapshot 挂起/崩溃恢复快照(§2.6;RunCheckpoint Protocol 归 session.py)
RunConfig 运行配置(纯数据;RunSnapshot.config 引用,runner.py 重导出)
RunResult run 出口:status 只区分挂起 vs 终态(§2.11)
RunContext run_id / deps / usage / session_id / step_id
(§2.11,六轮终审自 agent.py 迁入)
上下游:providers 方言层把各家响应折算成 ModelResponse / Usage;runner 消费 RunConfig / RunSnapshot / RunEvent 产出 RunResult;session 层只做本模块类型的序列化落库。全部为 pydantic BaseModel,序列化与校验交给 pydantic,不另设 DTO 层。
注意: - session 序列化落库后补字段即数据迁移,字段务必一次定对。 - 工具三件套整组归本模块(六轮终审);tool.py 只留 @tool / ToolRegistry / 执行管道。 - SystemFragmentSource(T7-1 正名,原 SkillSource)留 agent.py(与唯一消费方 Agent.system_sources 同住更内聚,见 §2.10)。 - Message.id 赋值纪律:Runner 在消息进入落库路径前必须赋 id;id=None 照写不去重(见 §2.1 / §2.9)。
RUN_FINISH_REASON_BY_MODEL
module-attribute
¶
RUN_FINISH_REASON_BY_MODEL: dict[
ModelFinishReason | None, RunFinishReason
] = {
"stop": "stop",
"tool_calls": "stop",
"length": "stop",
"content_filter": "guardrail",
None: "stop",
}
ModelFinishReason
module-attribute
¶
RunEventType
module-attribute
¶
RunEventType = Literal[
"run_started",
"llm_call",
"llm_delta",
"tool_call",
"context_compact",
"guardrail",
"hook",
"model_fallback",
"approval_pending",
"approval_resolved",
"run_finished",
"run_failed",
]
RunFinishReason
module-attribute
¶
RunFinishReason = Literal[
"stop",
"max_turns",
"token_budget",
"wall_clock",
"no_progress",
"guardrail",
"cancelled",
]
HookVerdict ¶
ImagePart ¶
Bases: BaseModel
图像分片(v1 仅图像输入;PDF / 音视频显式不做)。
url 与 base64 二选一;data 给定时 media_type 必填(Anthropic base64 必带 media_type、 OpenAI 需拼 data URL,缺失即 provider 序列化失败,见 §2.1 四轮复审补)。
Message ¶
Bases: BaseModel
统一消息(§2.1)。
thinking 与 content 拍平存储,Anthropic 交错块序不保留;provider 回传时一律按 thinking → text → tool_calls 规范序重排。
ModelResponse ¶
Bases: BaseModel
模型层单次调用结果(§2.1)。
provider 方言层把各家响应折算成本类型,重试 / 降级之上的代码只认它。finish_reason 是模型级词表;运行级终态一律经 model_to_run_finish_reason 映射,调用点不再各写判断。
PendingCall ¶
RunConfig ¶
Bases: BaseModel
运行配置(§2.11;归 types.py,runner.py 重导出,防 session↔runner 循环 import)。
纯预算参数,不持任何运行时对象引用,可整体随快照落库;yaml 侧由 config.RunSection 装配而来,字段一一对应。
RunContext ¶
RunEvent ¶
Bases: BaseModel
运行层追踪事件(§2.4)。
payload 最小契约见 RunEvent 的事件面定义;payload 保留 dict 以配合 schema_version 演进,
但字段名定死。全局定序 = (step_id, seq),ts 仅作墙钟参考。
RunResult ¶
Bases: BaseModel
run 出口(§2.11):status 只区分挂起 vs 终态,细分终态看 finish_reason。
pending 态 final message 为 None、pending 为待审批只读视图(T2-7)。 异常终态(run_failed)不返回 RunResult 而是向上抛。
RunSnapshot ¶
Bases: BaseModel
挂起 / 崩溃恢复快照(§2.6)。
快照为全量 messages 拷贝;挂起 = 最后一步快照 + pending 集合;审批通过从快照重建循环。 usage / step_id / no_progress 随快照承接原 run——恢复是续跑而非重开,预算不重置。
status
class-attribute
instance-attribute
¶
TextPart ¶
ToolCall ¶
ToolResult ¶
Bases: BaseModel
工具执行结果(§2.7)。
统一分隔标记包裹 content(防注入第一道防线);is_error / untrusted / handle 元数据 随 role="tool" Message 落库,跨 run 不丢(映射见 §2.7)。
ToolSpec ¶
Bases: BaseModel
对外暴露给模型的工具 JSON Schema 描述(§2.7)。
归属 types.py(六轮终审):ChatModel.chat(model.py)签名引用本类型、tool.py 重试退避 引用 RetryPolicy(model.py);若留 tool.py 则 model.py ↔ tool.py 互引成环。
Usage ¶
Bases: BaseModel
用量五字段(§2.1;T3-8 补口径契约)。
口径契约:input_tokens 只统计「未命中缓存」的输入。 各家原生口径不一致:
Anthropic 的 input_tokens 本来就排除 cache_read / cache_creation;而
OpenAI 系 prompt_tokens、Gemini promptTokenCount 都把命中缓存的部分算在里面
(DeepSeek 更直白,拆成 prompt_cache_hit_tokens + prompt_cache_miss_tokens)。
含缓存的厂商在方言层先用 from_prompt_total 折减,再进本表——不折减有两个后果:
budget_tokens 把同一批 token 计两遍(预算提前触发),cache_hit_rate 分母虚高
(命中率偏低)。命中率要跨厂商可比,这条契约就得先定死。
cache_hit_rate
property
¶
输入侧缓存命中率(0~1):命中 token / 输入总量(T3-8)。
分母是「未命中 + 命中 + 写入」三者之和,即厂商口径下的完整输入;没有输入时 返回 0.0 而不是抛错——空调用的命中率无意义,不该让统计路径炸掉。
from_prompt_total
classmethod
¶
from_prompt_total(
*,
prompt_tokens: int,
output_tokens: int = 0,
cache_read_tokens: int = 0,
cache_creation_tokens: int = 0,
reasoning_tokens: int = 0,
) -> Usage
按「prompt 总量含缓存命中」的厂商口径构造,内部折减出 input_tokens。
负数与「命中数大于总量」的异常回传一律夹到合法区间:宁可少记缓存,也不让 预算虚增——厂商回传脏数据时,错误的预算比错误的命中率更疼。
model_to_run_finish_reason ¶
model_to_run_finish_reason(
finish_reason: ModelFinishReason | None,
) -> RunFinishReason
把模型级 finish_reason 映射为运行级终态(对照用例见 tests/test_types.py)。