mainspring.config¶
yaml 配置装载与 Agent / Runner 装配
本页由
scripts/gen_api_docs.py从模块__all__自动生成,请勿手改。 新增或删除公开符号:先改模块__all__与tests/test_public_api.py快照, 再重新生成本页。
配置装载与装配(T1-11;ADR-004 / §2.11 配置装配层)。
三段式:yaml → Config 纯数据模型 → 装配器 → 运行时对象(Agent / ChatModel / Runner)。
Config 数据模型 零运行时实例,是 yaml 的唯一反序列化目标;extra="forbid" 拒绝未知字段
${VAR} 插值 解析后按字符串值替换;缺失变量立即报错并指明变量名与行号
错误定位 yaml 解析 / 校验失败均给出来源、行号与字段路径(ConfigError)
timedelta wall_clock / approval_ttl 支持 ISO 8601 与 "90s" / "5m" / "1h30m" 简写
装配器 build_model / build_agent / build_run_config / build_store /
build_sink / build_runner
yaml 解析依赖 PyYAML:core 基础依赖仍只 pydantic + httpx(T0-1 红线),PyYAML 作为
可选依赖 mainspring[config] 懒加载;缺失时报 MainspringError 并给出安装提示。
AgentConfig ¶
Bases: BaseModel
Agent 中可配置的声明部分;工具 / system_sources 是运行时对象,由代码注入。
LangfuseConfig ¶
Bases: BaseModel
Langfuse 轨迹导出声明段:纯数据,导出实现在 server 装配层(trace_export)。
enabled 三态:None(缺省)= 自动(keys 齐全即启用);true = 显式开启,
keys 在 yaml 与环境变量里都拿不到则装配即红(点名要的能力不静默降级);
false = 显式关闭(env 有 key 也不连)。key 来源优先级 yaml > env
(LANGFUSE_PUBLIC_KEY / LANGFUSE_SECRET_KEY);mainspring.yaml 入 git,
生产密钥建议写 ${LANGFUSE_PUBLIC_KEY} 插值或留 env,yaml 只放本地一次性值。
MainspringConfig ¶
Bases: BaseModel
yaml 根对象:纯数据,不持有任何运行时实例。
McpConfig ¶
Bases: BaseModel
MCP 接入段:servers 为空 = 不接 MCP,装配层零开销(T15-⑧)。
McpServerConfig ¶
Bases: BaseModel
单个 MCP server 的接入声明(T15-⑧):纯数据,协议实现在 mainspring-mcp 外挂。
command / url 二选一:command = stdio 传输(本地子进程 JSONL),url = Streamable
HTTP 传输;allow 是工具白名单(匹配 MCP 原始工具名或加前缀后的注册名,任一命中
即保留,None = 全部);prefix 是注册名前缀(None = 用 name,{prefix}__{工具名}
消歧)。core 对 MCP 零感知——本段只是 yaml 的声明面,连接与桥接全在装配层。
ModelConfig ¶
ModelSettingsConfig ¶
RetryPolicyConfig ¶
RunSection ¶
Bases: BaseModel
run 预算段:装配为 types.RunConfig,字段语义与 None 缺省含义见彼处,不在此复述。
SessionConfig ¶
Bases: BaseModel
会话后端段:内置 memory;sqlite / redis / postgres 由 extras 注册工厂。
TracingConfig ¶
Bases: BaseModel
追踪出口段:M0 内置 console;otel 随 extras 落地;langfuse 为可选导出声明面。
WebSection ¶
Bases: BaseModel
web 工具段(T23):搜索后端选型。
大陆网络 DDG 被墙(connect 挂起,原默认端点 100% 失败),默认 bing 直连 可达;海外部署可切回 duckduckgo。工具参数 schema 不随之变化——后端在 装配层闭包绑定,core 的 tools_digest 回放指纹不受影响。
build_agent ¶
build_agent(
config: MainspringConfig, *, tools: Sequence[Tool] = ()
) -> Agent
MainspringConfig → 运行时 Agent:模型由装配器构建,工具由代码注入。
build_model ¶
build_model(config: ModelConfig) -> ChatModel
ModelConfig → ChatModel:provider 名解析方言,密钥经 Provider(SecretStr / 环境变量)。
build_run_config ¶
build_run_config(run: RunSection) -> RunConfig
RunSection → types.RunConfig(预算四件套的纯数据载体)。
build_runner ¶
build_runner(
config: MainspringConfig,
*,
backends: Mapping[str, SessionBackend] | None = None,
checkpoint: RunCheckpoint | None = None,
) -> Runner
按 yaml 装配 Runner(store / sink / checkpoint),循环与预算走 build_run_config。
checkpoint 由调用方注入(内存默认 / SQLite 跨进程),与 store 同源:两者通常来自同一个 外挂实例(§2.9 store 同源规则)。
build_sink ¶
build_sink(config: TracingConfig) -> TracingSink
TracingConfig → TracingSink;M0 仅内置 console,otel 随 extras 装配。
build_store ¶
build_store(
config: SessionConfig,
*,
backends: Mapping[str, SessionBackend] | None = None,
) -> SessionStore
SessionConfig → SessionStore:memory 内置,其余后端由调用方注入工厂。
注入式装配是为了守住「core 不 import 外挂」的红线:sqlite / redis / postgres 实现在 extras/,由其自行注册,core 只认名字——换后端仍只改 yaml,依赖方向不反转。
load_config ¶
load_config(
text: str,
*,
env: Mapping[str, str] | None = None,
source: str = "<yaml>",
) -> MainspringConfig
yaml 文本 → MainspringConfig:${VAR} 插值 + 校验(错误带行号与字段路径)。
load_config_file ¶
load_config_file(
path: str | PathLike[str],
*,
env: Mapping[str, str] | None = None,
) -> MainspringConfig
从文件装载配置;来源默认为文件路径,报错可定位到行。
parse_duration ¶
解析时长:timedelta 原样返回;数字按秒;ISO 8601 或 "90s"/"5m"/"1h30m" 简写。