mainspring-agent 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mainspring_agent-0.1.0/LICENSE +21 -0
- mainspring_agent-0.1.0/PKG-INFO +219 -0
- mainspring_agent-0.1.0/README.md +186 -0
- mainspring_agent-0.1.0/core/__init__.py +76 -0
- mainspring_agent-0.1.0/core/agent.py +103 -0
- mainspring_agent-0.1.0/core/config.py +594 -0
- mainspring_agent-0.1.0/core/context.py +322 -0
- mainspring_agent-0.1.0/core/errors.py +123 -0
- mainspring_agent-0.1.0/core/guardrails.py +259 -0
- mainspring_agent-0.1.0/core/hooks.py +236 -0
- mainspring_agent-0.1.0/core/logconf.py +165 -0
- mainspring_agent-0.1.0/core/model.py +782 -0
- mainspring_agent-0.1.0/core/output.py +379 -0
- mainspring_agent-0.1.0/core/providers/__init__.py +37 -0
- mainspring_agent-0.1.0/core/providers/_openai_compat.py +503 -0
- mainspring_agent-0.1.0/core/providers/anthropic.py +659 -0
- mainspring_agent-0.1.0/core/providers/baichuan.py +37 -0
- mainspring_agent-0.1.0/core/providers/base.py +365 -0
- mainspring_agent-0.1.0/core/providers/deepseek.py +41 -0
- mainspring_agent-0.1.0/core/providers/gemini.py +792 -0
- mainspring_agent-0.1.0/core/providers/glm.py +38 -0
- mainspring_agent-0.1.0/core/providers/hunyuan.py +39 -0
- mainspring_agent-0.1.0/core/providers/minimax.py +38 -0
- mainspring_agent-0.1.0/core/providers/moonshot.py +39 -0
- mainspring_agent-0.1.0/core/providers/ollama.py +39 -0
- mainspring_agent-0.1.0/core/providers/openai.py +42 -0
- mainspring_agent-0.1.0/core/providers/qwen.py +39 -0
- mainspring_agent-0.1.0/core/providers/stepfun.py +37 -0
- mainspring_agent-0.1.0/core/providers/volcengine.py +41 -0
- mainspring_agent-0.1.0/core/py.typed +0 -0
- mainspring_agent-0.1.0/core/runner.py +1544 -0
- mainspring_agent-0.1.0/core/session.py +154 -0
- mainspring_agent-0.1.0/core/testing.py +366 -0
- mainspring_agent-0.1.0/core/tokens.py +182 -0
- mainspring_agent-0.1.0/core/tool.py +379 -0
- mainspring_agent-0.1.0/core/tracing.py +129 -0
- mainspring_agent-0.1.0/core/types.py +415 -0
- mainspring_agent-0.1.0/mainspring_agent.egg-info/PKG-INFO +219 -0
- mainspring_agent-0.1.0/mainspring_agent.egg-info/SOURCES.txt +124 -0
- mainspring_agent-0.1.0/mainspring_agent.egg-info/dependency_links.txt +1 -0
- mainspring_agent-0.1.0/mainspring_agent.egg-info/requires.txt +8 -0
- mainspring_agent-0.1.0/mainspring_agent.egg-info/top_level.txt +1 -0
- mainspring_agent-0.1.0/pyproject.toml +298 -0
- mainspring_agent-0.1.0/setup.cfg +4 -0
- mainspring_agent-0.1.0/tests/test_agent.py +48 -0
- mainspring_agent-0.1.0/tests/test_anthropic.py +776 -0
- mainspring_agent-0.1.0/tests/test_approval.py +329 -0
- mainspring_agent-0.1.0/tests/test_budget.py +449 -0
- mainspring_agent-0.1.0/tests/test_cache.py +451 -0
- mainspring_agent-0.1.0/tests/test_coding_agent.py +240 -0
- mainspring_agent-0.1.0/tests/test_coding_cli.py +487 -0
- mainspring_agent-0.1.0/tests/test_coding_e2e.py +81 -0
- mainspring_agent-0.1.0/tests/test_coding_job_tools.py +270 -0
- mainspring_agent-0.1.0/tests/test_coding_read_tools.py +258 -0
- mainspring_agent-0.1.0/tests/test_coding_safety.py +216 -0
- mainspring_agent-0.1.0/tests/test_coding_shell_tools.py +223 -0
- mainspring_agent-0.1.0/tests/test_coding_todo_tools.py +145 -0
- mainspring_agent-0.1.0/tests/test_coding_web_tools.py +598 -0
- mainspring_agent-0.1.0/tests/test_coding_write_tools.py +158 -0
- mainspring_agent-0.1.0/tests/test_config.py +456 -0
- mainspring_agent-0.1.0/tests/test_context.py +411 -0
- mainspring_agent-0.1.0/tests/test_desktop_assembly.py +106 -0
- mainspring_agent-0.1.0/tests/test_docs.py +89 -0
- mainspring_agent-0.1.0/tests/test_eval.py +1526 -0
- mainspring_agent-0.1.0/tests/test_eval_langfuse.py +372 -0
- mainspring_agent-0.1.0/tests/test_eval_online.py +240 -0
- mainspring_agent-0.1.0/tests/test_fallback.py +388 -0
- mainspring_agent-0.1.0/tests/test_gemini.py +802 -0
- mainspring_agent-0.1.0/tests/test_guardrails.py +493 -0
- mainspring_agent-0.1.0/tests/test_hitl.py +415 -0
- mainspring_agent-0.1.0/tests/test_hooks.py +523 -0
- mainspring_agent-0.1.0/tests/test_logging.py +432 -0
- mainspring_agent-0.1.0/tests/test_m2_acceptance.py +508 -0
- mainspring_agent-0.1.0/tests/test_mcp.py +426 -0
- mainspring_agent-0.1.0/tests/test_memory_backends.py +230 -0
- mainspring_agent-0.1.0/tests/test_memory_recall.py +174 -0
- mainspring_agent-0.1.0/tests/test_memory_semantic.py +208 -0
- mainspring_agent-0.1.0/tests/test_memory_tools.py +115 -0
- mainspring_agent-0.1.0/tests/test_model.py +326 -0
- mainspring_agent-0.1.0/tests/test_no_provider_leak.py +186 -0
- mainspring_agent-0.1.0/tests/test_openai_compat.py +692 -0
- mainspring_agent-0.1.0/tests/test_otel.py +352 -0
- mainspring_agent-0.1.0/tests/test_output.py +437 -0
- mainspring_agent-0.1.0/tests/test_parallel.py +368 -0
- mainspring_agent-0.1.0/tests/test_permissions.py +260 -0
- mainspring_agent-0.1.0/tests/test_provider_matrix.py +396 -0
- mainspring_agent-0.1.0/tests/test_providers_base.py +218 -0
- mainspring_agent-0.1.0/tests/test_public_api.py +305 -0
- mainspring_agent-0.1.0/tests/test_reasoning.py +247 -0
- mainspring_agent-0.1.0/tests/test_reliability.py +369 -0
- mainspring_agent-0.1.0/tests/test_replay.py +295 -0
- mainspring_agent-0.1.0/tests/test_resume.py +406 -0
- mainspring_agent-0.1.0/tests/test_run_sync.py +161 -0
- mainspring_agent-0.1.0/tests/test_runner.py +639 -0
- mainspring_agent-0.1.0/tests/test_runner_freeze.py +49 -0
- mainspring_agent-0.1.0/tests/test_sandbox.py +574 -0
- mainspring_agent-0.1.0/tests/test_server_agui.py +309 -0
- mainspring_agent-0.1.0/tests/test_server_approval_log.py +331 -0
- mainspring_agent-0.1.0/tests/test_server_approvals.py +240 -0
- mainspring_agent-0.1.0/tests/test_server_auth.py +116 -0
- mainspring_agent-0.1.0/tests/test_server_mcp.py +191 -0
- mainspring_agent-0.1.0/tests/test_server_memory.py +171 -0
- mainspring_agent-0.1.0/tests/test_server_parsing.py +362 -0
- mainspring_agent-0.1.0/tests/test_server_persistence.py +351 -0
- mainspring_agent-0.1.0/tests/test_server_registry_gc.py +111 -0
- mainspring_agent-0.1.0/tests/test_server_sessions.py +266 -0
- mainspring_agent-0.1.0/tests/test_server_skeleton.py +138 -0
- mainspring_agent-0.1.0/tests/test_server_static.py +51 -0
- mainspring_agent-0.1.0/tests/test_server_streaming.py +375 -0
- mainspring_agent-0.1.0/tests/test_server_uploads.py +110 -0
- mainspring_agent-0.1.0/tests/test_server_web_toggle.py +137 -0
- mainspring_agent-0.1.0/tests/test_server_write_e2e.py +186 -0
- mainspring_agent-0.1.0/tests/test_session.py +170 -0
- mainspring_agent-0.1.0/tests/test_session_backends.py +310 -0
- mainspring_agent-0.1.0/tests/test_session_list_meta.py +376 -0
- mainspring_agent-0.1.0/tests/test_session_sqlite.py +44 -0
- mainspring_agent-0.1.0/tests/test_session_titles.py +217 -0
- mainspring_agent-0.1.0/tests/test_smoke.py +16 -0
- mainspring_agent-0.1.0/tests/test_stream.py +388 -0
- mainspring_agent-0.1.0/tests/test_tokens.py +181 -0
- mainspring_agent-0.1.0/tests/test_tool.py +544 -0
- mainspring_agent-0.1.0/tests/test_tracing.py +147 -0
- mainspring_agent-0.1.0/tests/test_types.py +243 -0
- mainspring_agent-0.1.0/tests/test_windows_job_engine.py +109 -0
- mainspring_agent-0.1.0/tests/test_workflow.py +343 -0
- mainspring_agent-0.1.0/tests/test_workspace_layout.py +73 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mainspring contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mainspring-agent
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A dependency-minimal Python agent framework: predictable execution, hard budgets, and plugin isolation
|
|
5
|
+
Author: mainspring contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/chyong/mainspring
|
|
8
|
+
Project-URL: Documentation, https://github.com/chyong/mainspring/tree/main/docs
|
|
9
|
+
Project-URL: Repository, https://github.com/chyong/mainspring
|
|
10
|
+
Project-URL: Issues, https://github.com/chyong/mainspring/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/chyong/mainspring/blob/main/CHANGELOG.md
|
|
12
|
+
Keywords: agent,llm,ai,agent-framework,mcp,openai,anthropic,gemini,ollama
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: pydantic<3,>=2.10
|
|
27
|
+
Requires-Dist: httpx<1,>=0.27
|
|
28
|
+
Provides-Extra: config
|
|
29
|
+
Requires-Dist: pyyaml>=6.0; extra == "config"
|
|
30
|
+
Provides-Extra: eval-rag
|
|
31
|
+
Requires-Dist: ragas>=0.2.13; extra == "eval-rag"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# mainspring
|
|
35
|
+
|
|
36
|
+
**Autonomous motion, precise control.**
|
|
37
|
+
|
|
38
|
+
A Python agent framework whose core has exactly two runtime dependencies: `pydantic` and `httpx`.
|
|
39
|
+
Everything else — MCP, workflow graphs, session stores, OpenTelemetry, sandboxing, coding tools — is
|
|
40
|
+
an opt-in package you can remove without touching the core.
|
|
41
|
+
|
|
42
|
+
English | [简体中文](README.zh-CN.md)
|
|
43
|
+
|
|
44
|
+
[](https://github.com/chyong/mainspring/actions/workflows/ci.yml)
|
|
45
|
+
[](https://pypi.org/project/mainspring-agent/)
|
|
46
|
+
[](https://pypi.org/project/mainspring-agent/)
|
|
47
|
+
[](LICENSE)
|
|
48
|
+
[](https://chyong.github.io/mainspring/)
|
|
49
|
+
|
|
50
|
+
## Why another agent framework?
|
|
51
|
+
|
|
52
|
+
Most agent frameworks grow into platforms. mainspring is built around five constraints instead.
|
|
53
|
+
|
|
54
|
+
| Constraint | What it means |
|
|
55
|
+
|---|---|
|
|
56
|
+
| **Two dependencies** | `pip install mainspring-agent` pulls in `pydantic` and `httpx`. Provider SDKs are never imported; every vendor is spoken to over plain HTTP. |
|
|
57
|
+
| **One switch for models** | 13 dialects: an OpenAI-compatible base plus native Anthropic and Gemini. Change `provider` and `model`, keep the same `Agent`. Missing capabilities degrade explicitly or fail before the request — never a vendor-side 500. |
|
|
58
|
+
| **Control is declarative** | Turn/token/wall-clock/no-progress budgets, tool allowlists, input and output guardrails, and human-in-the-loop approve → deny → resume are all declaration slots on `Agent`, not `if` statements scattered through business code. |
|
|
59
|
+
| **Isolation is enforced** | `import-linter` contracts make it impossible for the core to import an extra, or for extras to import each other. CI uninstalls each extra in turn and the core suite stays green. |
|
|
60
|
+
| **Observable and replayable** | Every step emits a `RunEvent`. The console sink is built in; the OTel extra turns the same stream into a span tree. Recorded JSONL replays into an identical event sequence. |
|
|
61
|
+
|
|
62
|
+
## Install
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install mainspring-agent
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The import name stays `mainspring` — the PyPI name `mainspring` was already taken by another project.
|
|
69
|
+
|
|
70
|
+
Optional pieces are separate packages:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install "mainspring-agent[config]" # YAML config loader (pyyaml)
|
|
74
|
+
pip install mainspring-coding-tools # 14 coding tools + terminal REPL
|
|
75
|
+
pip install mainspring-mcp # MCP client (stdio + Streamable HTTP)
|
|
76
|
+
pip install mainspring-otel # OpenTelemetry export + replay CLI
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Quickstart — no API key
|
|
80
|
+
|
|
81
|
+
Requires Python ≥ 3.11 and [Ollama](https://ollama.com/):
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
ollama pull qwen3:14b
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from mainspring import Agent, Runner, tool
|
|
89
|
+
from mainspring.providers.ollama import create_model
|
|
90
|
+
|
|
91
|
+
@tool
|
|
92
|
+
def add(a: int, b: int) -> int:
|
|
93
|
+
"""Add two integers."""
|
|
94
|
+
return a + b
|
|
95
|
+
|
|
96
|
+
agent = Agent(
|
|
97
|
+
name="assistant",
|
|
98
|
+
instructions="Use tools when they help. Keep answers short.",
|
|
99
|
+
model=create_model("qwen3:14b"), # local, zero cost
|
|
100
|
+
tools=[add],
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
result = Runner().run_sync(agent, "What is 2 + 3?")
|
|
104
|
+
print(result.status, result.finish_reason, result.message.text_content())
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
To use a cloud model, swap the import and the model name — the agent code does not change:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from mainspring.providers.deepseek import create_model
|
|
111
|
+
|
|
112
|
+
model = create_model("deepseek-chat") # reads DEEPSEEK_API_KEY
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Keys only ever enter through environment variables. They are never written to YAML files, event
|
|
116
|
+
payloads, or traces.
|
|
117
|
+
|
|
118
|
+
## Terminal coding agent
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
pip install mainspring-coding-tools
|
|
122
|
+
mainspring-coder --root .
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
You get tool cards, diffs, and a `y/n` approval prompt for every write or shell command. Three
|
|
126
|
+
commands to start: `/new` (new session), `/resume` (continue an interrupted run), `/model <name>`
|
|
127
|
+
(switch model). Read-only tools run without asking; side effects do not.
|
|
128
|
+
|
|
129
|
+
## HTTP server and web UI
|
|
130
|
+
|
|
131
|
+
The application layer is delivered as artifacts rather than PyPI packages, so this part runs from a
|
|
132
|
+
clone:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
git clone https://github.com/chyong/mainspring
|
|
136
|
+
cd mainspring
|
|
137
|
+
uv sync
|
|
138
|
+
uv run uvicorn mainspring_server.app:app --port 8900
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The server exposes an AG-UI event stream (SSE) plus REST endpoints for sessions and approvals.
|
|
142
|
+
A React web client and a Tauri desktop shell live under `apps/`.
|
|
143
|
+
|
|
144
|
+
## Repository layout
|
|
145
|
+
|
|
146
|
+
```text
|
|
147
|
+
core/ # framework (directory is core/, import name is mainspring)
|
|
148
|
+
extras/ # 11 independent packages: mcp, workflow, session-{sqlite,redis,postgres},
|
|
149
|
+
# otel, memory, sandbox, coding-tools, eval-online, langfuse
|
|
150
|
+
apps/ # application layer: mainspring-server (HTTP), web (React), desktop (Tauri)
|
|
151
|
+
examples/ # runnable examples, all offline-capable by default
|
|
152
|
+
docs/ # mkdocs documentation site
|
|
153
|
+
mainspring.yaml # config layer: model, storage, budgets
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Performance
|
|
157
|
+
|
|
158
|
+
Framework overhead is measured separately from model latency (local `qwen3:14b`, 2026-10-31):
|
|
159
|
+
|
|
160
|
+
| Scenario | End-to-end p50 | Framework overhead p50 |
|
|
161
|
+
|---|---|---|
|
|
162
|
+
| Single-step run (synthetic model) | 0.044 ms | 0.039 ms |
|
|
163
|
+
| Tool step (one tool call) | 0.315 ms | 0.301 ms |
|
|
164
|
+
| Replayed support-ticket run (hooks + 2 tools + structured output) | 0.958 ms | — |
|
|
165
|
+
| Live run (`qwen3:14b`) | 18,334 ms | **1.41 ms** (< 0.01%) |
|
|
166
|
+
|
|
167
|
+
`tracemalloc` peak per run: 25.2 KB. Reproduce with `uv run python benchmarks/bench_run.py`
|
|
168
|
+
(no API, no network).
|
|
169
|
+
|
|
170
|
+
## Documentation
|
|
171
|
+
|
|
172
|
+
- [Quickstart](docs/quickstart.md) — from zero to a working agent
|
|
173
|
+
- [Core concepts](docs/concepts/index.md) — the six layers of the framework
|
|
174
|
+
- [Tool design](docs/guides/tool-design.md) — writing tools a model actually calls correctly
|
|
175
|
+
- [HITL and approvals](docs/guides/hitl.md) — human approval for dangerous operations
|
|
176
|
+
- [Model switching and fallback](docs/guides/model-switching.md)
|
|
177
|
+
- [API reference](docs/api/index.md)
|
|
178
|
+
|
|
179
|
+
The docs site is built with mkdocs: `uv sync --group docs && uv run mkdocs serve`.
|
|
180
|
+
|
|
181
|
+
## Development
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
uv sync # installs the workspace, extras, and dev tools
|
|
185
|
+
uv run pytest # 700+ tests
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Quality gates that must pass on every commit:
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
uv run ruff check . && uv run ruff format --check .
|
|
192
|
+
uv run mypy # strict, core + server
|
|
193
|
+
uv run lint-imports # dependency-direction contracts
|
|
194
|
+
uv run pytest # coverage gate: 85% (currently 100% on core)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
CI runs with no network access and no API keys: recorded fixtures replay the model calls. The same
|
|
198
|
+
property holds locally — every example runs offline by default.
|
|
199
|
+
|
|
200
|
+
More detail in [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
201
|
+
|
|
202
|
+
## Status
|
|
203
|
+
|
|
204
|
+
`v0.1.0`. The framework, extensions, CLI, server, and web client are usable today. Known gap: cloud
|
|
205
|
+
smoke tests for eight hosted providers still need to be re-run with real API keys; the locally
|
|
206
|
+
reachable Ollama path and all zero-key replay paths are verified.
|
|
207
|
+
|
|
208
|
+
See [CHANGELOG.md](CHANGELOG.md) for what changed, and the [roadmap](docs/release.md) for the
|
|
209
|
+
compatibility policy.
|
|
210
|
+
|
|
211
|
+
## Contributing
|
|
212
|
+
|
|
213
|
+
Issues and pull requests are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) and
|
|
214
|
+
[CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) first. Security reports go through
|
|
215
|
+
[SECURITY.md](SECURITY.md), not public issues.
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# mainspring
|
|
2
|
+
|
|
3
|
+
**Autonomous motion, precise control.**
|
|
4
|
+
|
|
5
|
+
A Python agent framework whose core has exactly two runtime dependencies: `pydantic` and `httpx`.
|
|
6
|
+
Everything else — MCP, workflow graphs, session stores, OpenTelemetry, sandboxing, coding tools — is
|
|
7
|
+
an opt-in package you can remove without touching the core.
|
|
8
|
+
|
|
9
|
+
English | [简体中文](README.zh-CN.md)
|
|
10
|
+
|
|
11
|
+
[](https://github.com/chyong/mainspring/actions/workflows/ci.yml)
|
|
12
|
+
[](https://pypi.org/project/mainspring-agent/)
|
|
13
|
+
[](https://pypi.org/project/mainspring-agent/)
|
|
14
|
+
[](LICENSE)
|
|
15
|
+
[](https://chyong.github.io/mainspring/)
|
|
16
|
+
|
|
17
|
+
## Why another agent framework?
|
|
18
|
+
|
|
19
|
+
Most agent frameworks grow into platforms. mainspring is built around five constraints instead.
|
|
20
|
+
|
|
21
|
+
| Constraint | What it means |
|
|
22
|
+
|---|---|
|
|
23
|
+
| **Two dependencies** | `pip install mainspring-agent` pulls in `pydantic` and `httpx`. Provider SDKs are never imported; every vendor is spoken to over plain HTTP. |
|
|
24
|
+
| **One switch for models** | 13 dialects: an OpenAI-compatible base plus native Anthropic and Gemini. Change `provider` and `model`, keep the same `Agent`. Missing capabilities degrade explicitly or fail before the request — never a vendor-side 500. |
|
|
25
|
+
| **Control is declarative** | Turn/token/wall-clock/no-progress budgets, tool allowlists, input and output guardrails, and human-in-the-loop approve → deny → resume are all declaration slots on `Agent`, not `if` statements scattered through business code. |
|
|
26
|
+
| **Isolation is enforced** | `import-linter` contracts make it impossible for the core to import an extra, or for extras to import each other. CI uninstalls each extra in turn and the core suite stays green. |
|
|
27
|
+
| **Observable and replayable** | Every step emits a `RunEvent`. The console sink is built in; the OTel extra turns the same stream into a span tree. Recorded JSONL replays into an identical event sequence. |
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install mainspring-agent
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The import name stays `mainspring` — the PyPI name `mainspring` was already taken by another project.
|
|
36
|
+
|
|
37
|
+
Optional pieces are separate packages:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install "mainspring-agent[config]" # YAML config loader (pyyaml)
|
|
41
|
+
pip install mainspring-coding-tools # 14 coding tools + terminal REPL
|
|
42
|
+
pip install mainspring-mcp # MCP client (stdio + Streamable HTTP)
|
|
43
|
+
pip install mainspring-otel # OpenTelemetry export + replay CLI
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Quickstart — no API key
|
|
47
|
+
|
|
48
|
+
Requires Python ≥ 3.11 and [Ollama](https://ollama.com/):
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
ollama pull qwen3:14b
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from mainspring import Agent, Runner, tool
|
|
56
|
+
from mainspring.providers.ollama import create_model
|
|
57
|
+
|
|
58
|
+
@tool
|
|
59
|
+
def add(a: int, b: int) -> int:
|
|
60
|
+
"""Add two integers."""
|
|
61
|
+
return a + b
|
|
62
|
+
|
|
63
|
+
agent = Agent(
|
|
64
|
+
name="assistant",
|
|
65
|
+
instructions="Use tools when they help. Keep answers short.",
|
|
66
|
+
model=create_model("qwen3:14b"), # local, zero cost
|
|
67
|
+
tools=[add],
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
result = Runner().run_sync(agent, "What is 2 + 3?")
|
|
71
|
+
print(result.status, result.finish_reason, result.message.text_content())
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
To use a cloud model, swap the import and the model name — the agent code does not change:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from mainspring.providers.deepseek import create_model
|
|
78
|
+
|
|
79
|
+
model = create_model("deepseek-chat") # reads DEEPSEEK_API_KEY
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Keys only ever enter through environment variables. They are never written to YAML files, event
|
|
83
|
+
payloads, or traces.
|
|
84
|
+
|
|
85
|
+
## Terminal coding agent
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install mainspring-coding-tools
|
|
89
|
+
mainspring-coder --root .
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
You get tool cards, diffs, and a `y/n` approval prompt for every write or shell command. Three
|
|
93
|
+
commands to start: `/new` (new session), `/resume` (continue an interrupted run), `/model <name>`
|
|
94
|
+
(switch model). Read-only tools run without asking; side effects do not.
|
|
95
|
+
|
|
96
|
+
## HTTP server and web UI
|
|
97
|
+
|
|
98
|
+
The application layer is delivered as artifacts rather than PyPI packages, so this part runs from a
|
|
99
|
+
clone:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
git clone https://github.com/chyong/mainspring
|
|
103
|
+
cd mainspring
|
|
104
|
+
uv sync
|
|
105
|
+
uv run uvicorn mainspring_server.app:app --port 8900
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The server exposes an AG-UI event stream (SSE) plus REST endpoints for sessions and approvals.
|
|
109
|
+
A React web client and a Tauri desktop shell live under `apps/`.
|
|
110
|
+
|
|
111
|
+
## Repository layout
|
|
112
|
+
|
|
113
|
+
```text
|
|
114
|
+
core/ # framework (directory is core/, import name is mainspring)
|
|
115
|
+
extras/ # 11 independent packages: mcp, workflow, session-{sqlite,redis,postgres},
|
|
116
|
+
# otel, memory, sandbox, coding-tools, eval-online, langfuse
|
|
117
|
+
apps/ # application layer: mainspring-server (HTTP), web (React), desktop (Tauri)
|
|
118
|
+
examples/ # runnable examples, all offline-capable by default
|
|
119
|
+
docs/ # mkdocs documentation site
|
|
120
|
+
mainspring.yaml # config layer: model, storage, budgets
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Performance
|
|
124
|
+
|
|
125
|
+
Framework overhead is measured separately from model latency (local `qwen3:14b`, 2026-10-31):
|
|
126
|
+
|
|
127
|
+
| Scenario | End-to-end p50 | Framework overhead p50 |
|
|
128
|
+
|---|---|---|
|
|
129
|
+
| Single-step run (synthetic model) | 0.044 ms | 0.039 ms |
|
|
130
|
+
| Tool step (one tool call) | 0.315 ms | 0.301 ms |
|
|
131
|
+
| Replayed support-ticket run (hooks + 2 tools + structured output) | 0.958 ms | — |
|
|
132
|
+
| Live run (`qwen3:14b`) | 18,334 ms | **1.41 ms** (< 0.01%) |
|
|
133
|
+
|
|
134
|
+
`tracemalloc` peak per run: 25.2 KB. Reproduce with `uv run python benchmarks/bench_run.py`
|
|
135
|
+
(no API, no network).
|
|
136
|
+
|
|
137
|
+
## Documentation
|
|
138
|
+
|
|
139
|
+
- [Quickstart](docs/quickstart.md) — from zero to a working agent
|
|
140
|
+
- [Core concepts](docs/concepts/index.md) — the six layers of the framework
|
|
141
|
+
- [Tool design](docs/guides/tool-design.md) — writing tools a model actually calls correctly
|
|
142
|
+
- [HITL and approvals](docs/guides/hitl.md) — human approval for dangerous operations
|
|
143
|
+
- [Model switching and fallback](docs/guides/model-switching.md)
|
|
144
|
+
- [API reference](docs/api/index.md)
|
|
145
|
+
|
|
146
|
+
The docs site is built with mkdocs: `uv sync --group docs && uv run mkdocs serve`.
|
|
147
|
+
|
|
148
|
+
## Development
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
uv sync # installs the workspace, extras, and dev tools
|
|
152
|
+
uv run pytest # 700+ tests
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Quality gates that must pass on every commit:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
uv run ruff check . && uv run ruff format --check .
|
|
159
|
+
uv run mypy # strict, core + server
|
|
160
|
+
uv run lint-imports # dependency-direction contracts
|
|
161
|
+
uv run pytest # coverage gate: 85% (currently 100% on core)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
CI runs with no network access and no API keys: recorded fixtures replay the model calls. The same
|
|
165
|
+
property holds locally — every example runs offline by default.
|
|
166
|
+
|
|
167
|
+
More detail in [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
168
|
+
|
|
169
|
+
## Status
|
|
170
|
+
|
|
171
|
+
`v0.1.0`. The framework, extensions, CLI, server, and web client are usable today. Known gap: cloud
|
|
172
|
+
smoke tests for eight hosted providers still need to be re-run with real API keys; the locally
|
|
173
|
+
reachable Ollama path and all zero-key replay paths are verified.
|
|
174
|
+
|
|
175
|
+
See [CHANGELOG.md](CHANGELOG.md) for what changed, and the [roadmap](docs/release.md) for the
|
|
176
|
+
compatibility policy.
|
|
177
|
+
|
|
178
|
+
## Contributing
|
|
179
|
+
|
|
180
|
+
Issues and pull requests are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) and
|
|
181
|
+
[CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) first. Security reports go through
|
|
182
|
+
[SECURITY.md](SECURITY.md), not public issues.
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""mainspring —— 自研 Agent 框架 core 包。
|
|
2
|
+
|
|
3
|
+
发条供力、擒纵控速:自主运转、精密受控。
|
|
4
|
+
core 第三方依赖仅 pydantic + httpx(T0-1 红线);provider SDK 一律懒加载;
|
|
5
|
+
编排 / MCP / 会话后端 / OTel 均为 extras/ 下的独立外挂,可整体移除。
|
|
6
|
+
|
|
7
|
+
M0 已落地(2026-10-30):核心模块按下述归属填充完毕,词汇表 / 模型层 / 工具 / 循环 /
|
|
8
|
+
会话与追踪 / 配置装配均可用;M1 起在这些接缝上加管控能力(循环体冻结)。
|
|
9
|
+
各模块归属(2026-10-30 六轮终审定稿):
|
|
10
|
+
|
|
11
|
+
types.py 统一词汇表:Message / Usage / RunEvent / 工具三件套 / HookVerdict /
|
|
12
|
+
PendingCall / RunSnapshot / RunConfig / RunContext(T1-1)
|
|
13
|
+
model.py ChatModel 抽象 + ModelSettings + RetryPolicy + Provider/ModelProfile +
|
|
14
|
+
Fallback/TestModel(T1-2 / T1-3 / T2-8 / T2-9 / T3-3)
|
|
15
|
+
tool.py @tool / ToolRegistry / 执行管道(三件套归 types.py)(T1-5)
|
|
16
|
+
agent.py Agent 声明 + SystemFragmentSource / StaticSkill 装配点(T1-6;T7-1 正名)
|
|
17
|
+
runner.py 运行循环 + 终止条件 + resume / approve / deny(T1-6 / T2-7)
|
|
18
|
+
session.py Session / SessionStore Protocol + MemoryStore + RunCheckpoint Protocol
|
|
19
|
+
(T1-7 / T2-6;RunSnapshot 本体归 types.py)
|
|
20
|
+
hooks.py Hook Protocol + PermissionHook(T2-1 / T2-2;Guardrail 归 guardrails.py,
|
|
21
|
+
T2-3 新增文件)
|
|
22
|
+
tracing.py TracingSink Protocol + ConsoleSink(T1-7)
|
|
23
|
+
logconf.py 运维日志:RunEvent → logging 单点桥接 + 命名空间配置(T21)
|
|
24
|
+
|
|
25
|
+
顶层即门面:起步通常只碰三个符号——@tool 声明工具、Agent 组合模型与指令、Runner 执行
|
|
26
|
+
循环;Message / Usage 等词汇表经顶层重导出。更细的构建块(模型包装器、会话存储实现、
|
|
27
|
+
Hook 实现等)按需从子模块 import,不占顶层预算。
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
__version__ = "0.1.0"
|
|
31
|
+
|
|
32
|
+
# T0-3 公开 API 预算守卫——双层口径(2026-10-30 接口复审定稿):
|
|
33
|
+
# 第一层:各模块显式 __all__,进 tests/test_public_api.py 的 EXPECTED_MODULE_ALL 快照(总量不限);
|
|
34
|
+
# 第二层:顶层重导出(本 __all__)≤ 20 个精选符号,定稿清单见测试 docstring,T1-x 逐卡填入。
|
|
35
|
+
# 限 20 的意图:顶层命名空间是框架给人的第一印象,超预算会诱导用户绕过门面直挖子模块;
|
|
36
|
+
# 新符号进顶层须先证明不可替代,否则留在子模块。
|
|
37
|
+
from .agent import Agent, StaticSkill, SystemFragmentSource
|
|
38
|
+
from .model import ChatModel, ModelSettings
|
|
39
|
+
from .runner import RunConfig, Runner, RunResult
|
|
40
|
+
from .session import SessionStore
|
|
41
|
+
from .tool import tool
|
|
42
|
+
from .tracing import TracingSink
|
|
43
|
+
from .types import (
|
|
44
|
+
HookVerdict,
|
|
45
|
+
ImagePart,
|
|
46
|
+
Message,
|
|
47
|
+
Part,
|
|
48
|
+
RunContext,
|
|
49
|
+
TextPart,
|
|
50
|
+
ToolCall,
|
|
51
|
+
ToolResult,
|
|
52
|
+
Usage,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
__all__ = [
|
|
56
|
+
"Agent",
|
|
57
|
+
"ChatModel",
|
|
58
|
+
"HookVerdict",
|
|
59
|
+
"ImagePart",
|
|
60
|
+
"Message",
|
|
61
|
+
"ModelSettings",
|
|
62
|
+
"Part",
|
|
63
|
+
"RunConfig",
|
|
64
|
+
"RunContext",
|
|
65
|
+
"RunResult",
|
|
66
|
+
"Runner",
|
|
67
|
+
"SessionStore",
|
|
68
|
+
"StaticSkill",
|
|
69
|
+
"SystemFragmentSource",
|
|
70
|
+
"TextPart",
|
|
71
|
+
"ToolCall",
|
|
72
|
+
"ToolResult",
|
|
73
|
+
"TracingSink",
|
|
74
|
+
"Usage",
|
|
75
|
+
"tool",
|
|
76
|
+
]
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Agent 声明面与 SystemFragmentSource 装配点:一次运行的全部意图在此声明,Runner 按声明装配执行。
|
|
2
|
+
|
|
3
|
+
T1-6 落地(§2.10 / §2.11);T7-1 正名(M6,ADR-005 修订决议):机制与领域分层——
|
|
4
|
+
|
|
5
|
+
SystemFragmentSource system 常量区片段源 Protocol(name + resolve):run 开始时按
|
|
6
|
+
上下文动态产出注入 system 的片段;机制名,不携带领域
|
|
7
|
+
StaticSkill skill 领域包装:静态文本 skill(core 参照实现,名实相符)
|
|
8
|
+
Agent 声明式对象:name / instructions(静态或按 context 动态生成)/
|
|
9
|
+
model / model_settings / tools / system_sources / max_turns
|
|
10
|
+
|
|
11
|
+
职责边界:Agent 是纯声明 + 可选回调,自己不跑循环、不持有 run 状态;hooks /
|
|
12
|
+
allowed_tools / guardrails / context_policy 是 T2 / T3 管控能力的唯一注册位——
|
|
13
|
+
架构红线「循环体冻结」(M1):新增管控只扩注册点与对应模块,不改 runner 循环体。
|
|
14
|
+
|
|
15
|
+
依赖方向(RunContext 归 types.py 的原因):Agent 与 hooks / guardrails 都要引用
|
|
16
|
+
RunContext 作回调参数;若由本模块持有,agent↔hooks 会互相 import 成环,六轮终审
|
|
17
|
+
后统一收口到 types.py——依赖单向:本模块可 import 下游,下游不许回头。
|
|
18
|
+
|
|
19
|
+
system 常量区 = instructions + 各 source.resolve 命中片段(按声明序,落常量区尾部),
|
|
20
|
+
每次 run 开始时求值一次;instructions 为 Callable 时在同一点以 ctx 调用。
|
|
21
|
+
领域包装(skill / memory 等)实现该协议:Protocol 只认契约,实现方不必同领域。
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
from collections.abc import Callable
|
|
27
|
+
from dataclasses import dataclass
|
|
28
|
+
from typing import Any, Protocol, runtime_checkable
|
|
29
|
+
|
|
30
|
+
from pydantic import BaseModel, ConfigDict
|
|
31
|
+
|
|
32
|
+
from .context import ContextPolicy
|
|
33
|
+
from .guardrails import Guardrail
|
|
34
|
+
from .hooks import Hook
|
|
35
|
+
from .model import ChatModel, ModelSettings
|
|
36
|
+
from .tool import Tool
|
|
37
|
+
from .types import RunContext
|
|
38
|
+
|
|
39
|
+
__all__ = ["Agent", "StaticSkill", "SystemFragmentSource"]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@runtime_checkable
|
|
43
|
+
class SystemFragmentSource(Protocol):
|
|
44
|
+
"""system 常量区片段源(ADR-005;T7-1 正名,原 SkillSource):run 开始时按上下文
|
|
45
|
+
动态产出注入 system 的片段。
|
|
46
|
+
|
|
47
|
+
机制与领域分层(ADR-005 修订决议):本协议只描述机制——「run 开始时求值一次、
|
|
48
|
+
注入 system 常量区、享受 keep_system 永不压缩」;skill / memory 等领域包装实现它,
|
|
49
|
+
协议名不携带领域。契约:resolve 返回的字符串按声明序拼进常量区尾部;返回空列表 =
|
|
50
|
+
本 run 不注入,不是错误。片段落 system 常量区,随之享受「永不压缩」待遇
|
|
51
|
+
(policy.keep_system),所以适合放每轮都必须可见的硬约束,而不是可以进历史摘要的
|
|
52
|
+
大块背景知识。
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
name: str
|
|
56
|
+
|
|
57
|
+
async def resolve(self, ctx: RunContext[Any]) -> list[str]: ...
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass(frozen=True)
|
|
61
|
+
class StaticSkill:
|
|
62
|
+
"""静态文本 skill,SystemFragmentSource 的 skill 领域包装(core 参照实现)。
|
|
63
|
+
|
|
64
|
+
名实相符:它本来就是 skill(固定文本,恒命中,不读 ctx),所以保留 Skill 名;
|
|
65
|
+
记忆等非 skill 领域的包装不得借「Skill」命名——领域包装的类名讲领域,机制名讲机制。
|
|
66
|
+
需要按上下文动态注入时,自己实现 SystemFragmentSource。
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
name: str
|
|
70
|
+
text: str
|
|
71
|
+
|
|
72
|
+
async def resolve(self, ctx: RunContext[Any]) -> list[str]:
|
|
73
|
+
return [self.text]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class Agent(BaseModel):
|
|
77
|
+
"""Agent 声明(§2.11):运行时对象,不参与 yaml 反序列化(T1-11 装配器负责构建)。
|
|
78
|
+
|
|
79
|
+
字段即契约:每个管控字段都是对应能力的唯一声明位,Runner 只从这里读配置装配
|
|
80
|
+
管道——手工构造与 yaml 装配两条路径共用同一组字段,行为天然一致。
|
|
81
|
+
生效节奏:hooks / allowed_tools / guardrails 随 T2-1~T2-3 生效,output_type 随
|
|
82
|
+
T3-4 生效,context_policy 随 T3-7 生效(None = 不压缩,策略整体关闭的唯一开关)。
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
model_config = ConfigDict(arbitrary_types_allowed=True, protected_namespaces=())
|
|
86
|
+
|
|
87
|
+
name: str
|
|
88
|
+
instructions: str | Callable[[RunContext[Any]], str] # Callable 每 run 开始时以 ctx 求值一次
|
|
89
|
+
model: ChatModel
|
|
90
|
+
model_settings: ModelSettings | None = None # temperature 等的唯一声明位
|
|
91
|
+
tools: list[Tool] = []
|
|
92
|
+
# T7-1 正名(原 skills)。机制字段:注解 = 契约(SystemFragmentSource),不为领域设
|
|
93
|
+
# 专属字段,领域靠实现该协议的包装类进场——StaticSkill(skill,core)/ MemoryRecall
|
|
94
|
+
# (memory,extras/mainspring-memory)/ 任意自定义类(有 name + resolve 即可,鸭子
|
|
95
|
+
# 类型零注册)。run 开始时各 source 求值一次,片段按声明序注入 system 常量区尾部,
|
|
96
|
+
# 享受 keep_system 永不压缩。
|
|
97
|
+
system_sources: list[SystemFragmentSource] = []
|
|
98
|
+
hooks: list[Hook] = [] # T2-1:T2 工具级策略的唯一声明位(新增 Hook 不改循环体)
|
|
99
|
+
allowed_tools: list[str] | None = None # T2-2 白名单:精确名 + 通配符,None = 不启用
|
|
100
|
+
guardrails: list[Guardrail] = [] # T2-3 输入 / 输出护栏(与主循环并行 fail-fast)
|
|
101
|
+
output_type: type[BaseModel] | None = None # T3-4 结构化输出:pydantic 模型即 schema
|
|
102
|
+
context_policy: ContextPolicy | None = None # T3-7 上下文压缩;None = 不压缩(整体关闭)
|
|
103
|
+
max_turns: int = 20 # RunConfig.max_turns=None(未设置)时本值生效
|