diff --git a/README.md b/README.md index fa729d2..b0085d6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ + +> [!NOTE] +> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。 +> [English](./README.en.md) · [原始项目](https://github.com/chopratejas/headroom) · [上游 README](https://github.com/chopratejas/headroom/blob/HEAD/README.md) +> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。 +
   ██╗  ██╗███████╗ █████╗ ██████╗ ██████╗  ██████╗  ██████╗ ███╗   ███╗
   ██║  ██║██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔═══██╗██╔═══██╗████╗ ████║
@@ -5,10 +11,10 @@
   ██╔══██║██╔══╝  ██╔══██║██║  ██║██╔══██╗██║   ██║██║   ██║██║╚██╔╝██║
   ██║  ██║███████╗██║  ██║██████╔╝██║  ██║╚██████╔╝╚██████╔╝██║ ╚═╝ ██║
   ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝╚═════╝ ╚═╝  ╚═╝ ╚═════╝  ╚═════╝ ╚═╝     ╚═╝
-              The context compression layer for AI agents
+              面向 AI 智能体的上下文压缩层
 
-

60–95% fewer tokens (for JSON data), 15-20% fewer tokens (for coding agents) · library · proxy · MCP · content-aware compressors · local-first · reversible

+

JSON 数据场景 token 减少 60–95%,编码智能体场景减少 15-20% · 库 · 代理 · MCP · 内容感知压缩器 · 本地优先 · 可逆

CI @@ -22,39 +28,39 @@

Docs · - Install · - Proof · - Agents · + 安装 · + 验证 · + 智能体 · Discord · llms.txt

- AI agents / LLMs: read /llms.txt here, or fetch the live index / full docs blob. + AI 智能体 / LLM:请在此阅读 /llms.txt,或获取 实时索引 / 完整文档 blob

---

chopratejas%2Fheadroom | Trendshift

-Headroom compresses everything your AI agent reads — tool outputs, logs, RAG chunks, files, and conversation history — before it reaches the LLM. Same answers, fraction of the tokens. +Headroom 会在内容到达 LLM 之前,压缩 AI 智能体读取的一切——工具输出、日志、RAG 分块、文件和对话历史。答案相同,token 仅为原来的一小部分。

- Headroom in action -
Live: 10,144 → 1,260 tokens — same FATAL found. + Headroom 实际运行效果 +
实时演示:10,144 → 1,260 tokens — 同样找到 FATAL。

-## What it does +## 它能做什么 -- **Library** — `compress(messages)` in Python or TypeScript, inline in any app -- **Proxy** — `headroom proxy --port 8787`, zero code changes, any language -- **Agent wrap** — `headroom wrap claude|codex|copilot|cursor|aider|opencode|cline|continue|goose|openhands|openclaw|vibe` in one command; undo with `headroom unwrap ` -- **MCP server** — `headroom_compress`, `headroom_retrieve`, `headroom_stats` for any MCP client -- **Cross-agent memory** — shared store across Claude, Codex, Gemini, auto-dedup -- **`headroom learn`** — mines failed sessions, writes corrections to `CLAUDE.local.md` (default, gitignored) or `CLAUDE.md` / `AGENTS.md` / `GEMINI.md` -- **Output token reduction** — trims what the model *writes back* (not just what you send): drops ceremony/restated code and skips deep "thinking" on routine steps. See [Output token reduction](#output-token-reduction-cut-what-the-model-writes-back). -- **Reversible (CCR)** — originals are cached for retrieval on demand +- **库(Library)** — 在 Python 或 TypeScript 中使用 `compress(messages)`,可内联集成到任意应用 +- **代理(Proxy)** — `headroom proxy --port 8787`,零代码改动,支持任意语言 +- **智能体封装(Agent wrap)** — 一条命令运行 `headroom wrap claude|codex|copilot|cursor|aider|opencode|cline|continue|goose|openhands|openclaw|vibe`;用 `headroom unwrap ` 撤销 +- **MCP 服务器** — `headroom_compress`、`headroom_retrieve`、`headroom_stats`,适用于任意 MCP 客户端 +- **跨智能体记忆(Cross-agent memory)** — 在 Claude、Codex、Gemini 之间共享存储,自动去重 +- **`headroom learn`** — 挖掘失败会话,将修正写入 `CLAUDE.local.md`(默认,已 gitignore)或 `CLAUDE.md` / `AGENTS.md` / `GEMINI.md` +- **输出 token 缩减(Output token reduction)** — 裁剪模型*写回*的内容(不仅是发送给它的内容):去除冗余套话/重复代码,并在常规步骤中跳过深度“思考”。参见[输出 token 缩减](#output-token-reduction-cut-what-the-model-writes-back)。 +- **可逆(CCR,Reversible)** — 原文本地缓存,可按需检索 -## How it works (30 seconds) +## 工作原理(30 秒) ``` Your agent / app @@ -76,14 +82,14 @@ Headroom compresses everything your AI agent reads — tool outputs, logs, RAG c LLM provider (Anthropic · OpenAI · Bedrock · …) ``` -- **ContentRouter** — detects content type, selects the right compressor -- **SmartCrusher / CodeCompressor / Kompress-v2-base** — compress JSON, AST, or prose -- **CacheAligner** — stabilizes prefixes so provider KV caches actually hit -- **CCR** — stores originals locally; LLM calls `headroom_retrieve` if it needs them +- **ContentRouter** — 检测内容类型,选择对应压缩器 +- **SmartCrusher / CodeCompressor / Kompress-v2-base** — 压缩 JSON、AST 或散文文本 +- **CacheAligner** — 稳定前缀,使提供商 KV 缓存真正命中 +- **CCR** — 本地存储原文;若需要,LLM 可调用 `headroom_retrieve` → [Architecture](https://headroom-docs.vercel.app/docs/architecture) · [CCR reversible compression](https://headroom-docs.vercel.app/docs/ccr) · [Kompress-v2-base model card](https://huggingface.co/chopratejas/kompress-v2-base) -## Get started (60 seconds) +## 快速开始(60 秒) ```bash # 1 — Install @@ -102,22 +108,22 @@ headroom perf headroom dashboard # live savings dashboard (proxy must be running) ``` -To use headroom, it is recommended you launch a wrapped agent session each time so that all necessary setup is completed. When wrapping a coding agent, headroom starts a local proxy, sets up an MCP server that provides tools such as rtk and tokensave, and launches a coding agent session configured to proxy requests to headroom. +建议使用 headroom 时,每次启动一个封装后的智能体会话,以确保完成所有必要配置。封装编码智能体时,headroom 会启动本地代理,设置提供 rtk、tokensave 等工具的 MCP 服务器,并启动已将请求代理到 headroom 的编码智能体会话。 -The `headroom` CLI ships **only** via the PyPI package. The npm `headroom-ai` is the TypeScript SDK — a library you import (`import { compress } from 'headroom-ai'`), not a CLI, so it provides no `headroom` command. +`headroom` CLI **仅**通过 PyPI 包分发。npm 上的 `headroom-ai` 是 TypeScript SDK——供导入的库(`import { compress } from 'headroom-ai'`),不是 CLI,因此不提供 `headroom` 命令。 -Granular extras: `[proxy]`, `[mcp]`, `[ml]`, `[code]`, `[memory]`, `[vector]` (optional HNSW backend — needs a C++ toolchain, not in `[all]`), `[relevance]`, `[image]`, `[agno]`, `[langchain]`, `[evals]`, `[pytorch-mps]` (Apple-GPU memory-embedder offload — set `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps`). Requires **Python 3.10+**. +细粒度可选依赖:`[proxy]`、`[mcp]`、`[ml]`、`[code]`、`[memory]`、`[vector]`(可选 HNSW 后端——需要 C++ 工具链,不包含在 `[all]` 中)、`[relevance]`、`[image]`、`[agno]`、`[langchain]`、`[evals]`、`[pytorch-mps]`(Apple GPU 内存嵌入器卸载——设置 `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps`)。需要 **Python 3.10+**。 -### Codex / global install +### Codex / 全局安装 -If Codex or another MCP client cannot inherit a shell `PATH` reliably, install Headroom as a persistent uv tool and point the client at the absolute binary path: +若 Codex 或其他 MCP 客户端无法可靠继承 shell 中的 `PATH`,请将 Headroom 安装为持久化 uv 工具,并在客户端中指向该二进制文件的绝对路径: ```bash uv tool install "headroom-ai[all]" command -v headroom ``` -Then use the returned path in MCP config: +然后在 MCP 配置中使用返回的路径: ```toml [mcp_servers.headroom] @@ -125,20 +131,20 @@ command = "/absolute/path/from/command-v/headroom" args = ["mcp", "serve"] ``` -`command = "headroom"` only works when the client starts with a `PATH` that already includes the uv tool directory. +仅当客户端启动时,`PATH` 已包含 uv 工具目录时,`command = "headroom"` 才有效。 -## Proof +## 验证 -**Savings on real agent workloads:** +**真实智能体工作负载上的节省:** -| Workload | Before | After | Savings | +| 工作负载 | 压缩前 | 压缩后 | 节省 | |-------------------------------|-------:|-------:|--------:| -| Code search (100 results) | 17,765 | 1,408 | **92%** | -| SRE incident debugging | 65,694 | 5,118 | **92%** | -| GitHub issue triage | 54,174 | 14,761 | **73%** | -| Codebase exploration | 78,502 | 41,254 | **47%** | +| 代码搜索(100 条结果) | 17,765 | 1,408 | **92%** | +| SRE 事故排查 | 65,694 | 5,118 | **92%** | +| GitHub issue 分类 | 54,174 | 14,761 | **73%** | +| 代码库探索 | 78,502 | 41,254 | **47%** | -**Accuracy preserved on standard benchmarks:** +**在标准基准测试中保持准确性:** | Benchmark | Category | N | Baseline | Headroom | Delta | |------------|----------|----:|---------:|---------:|------------| @@ -147,64 +153,43 @@ args = ["mcp", "serve"] | SQuAD v2 | QA | 100 | — | **97%** | 19% compression | | BFCL | Tools | 100 | — | **97%** | 32% compression | -Reproduce: `python -m headroom.evals suite --tier 1` · [Full benchmarks & methodology](https://headroom-docs.vercel.app/docs/benchmarks) +复现:`python -m headroom.evals suite --tier 1` · [完整基准测试与方法学](https://headroom-docs.vercel.app/docs/benchmarks) -## Output token reduction (cut what the model writes back) +## 输出 token 缩减(削减模型写回的内容) -Everything above shrinks the prompt you **send**. But you also pay for every -token the model **writes back** — and on Opus-class models output costs 5× input. -A lot of that output is waste: "Great, let me…" preambles, re-printing code you -just showed it, and deep "thinking" on routine steps like reading a file. +上文所述内容会缩小你**发送**的 prompt。但你也要为模型**写回**的每一个 token 付费——而在 Opus 级别模型上,输出成本是输入的 5 倍。其中大量输出是浪费:比如「好的,让我来…」这类开场白、重复打印你刚展示过的代码,以及在读取文件等常规步骤上进行冗长的「思考」。 -Headroom can trim that too, from the proxy, without you changing any code: +Headroom 也能从代理侧削减这些输出,无需你改动任何代码: -- **Verbosity steering** — appends a short "be terse, don't restate context" - note to the end of the system prompt (so your prompt cache still hits). -- **Effort routing** — when a turn is just the model resuming after a tool result - (a file read, a passing test), it dials the model's thinking effort down. New - questions and errors keep full effort. +- **冗长度引导(verbosity steering)** — 在 system prompt 末尾追加一条简短的「保持简洁、不要复述上下文」说明(这样你的 prompt 缓存仍能命中)。 +- **努力度路由(effort routing)** — 当某轮对话只是模型在工具结果之后继续执行时(例如读取文件、测试通过),会降低模型的思考努力度。新问题与错误仍保持全力。 -Turn it on: +开启方式: ```bash export HEADROOM_OUTPUT_SHAPER=1 # off by default headroom proxy --port 8787 ``` -> **Already running a proxy?** These switches are read *live* on every request, -> so a proxy that `headroom wrap` **reused** (rather than started) would not see -> a value you export afterwards — its environment was snapshotted at launch. -> `headroom wrap` now hot-syncs your current settings to the running proxy via a -> loopback `POST /admin/runtime-env`, so they take effect immediately with **no -> restart** (no cold start, no dropped requests, no lost caches). Set them before -> you `wrap`. On a shared proxy these overrides are global — the last explicit -> setting wins. +> **已经在运行代理?** 这些开关在每次请求时都会*实时*读取,因此若代理是 `headroom wrap` **复用**(而非重新启动),则不会看到你之后 export 的值——其环境在启动时已被快照。`headroom wrap` 现在会通过 loopback `POST /admin/runtime-env` 将你当前的设置热同步到正在运行的代理,因此可**无需重启**立即生效(无冷启动、无请求中断、无缓存丢失)。请在你 `wrap` 之前设置它们。在共享代理上,这些覆盖是全局的——以最后一次显式设置为准。 -**Learn the right terseness for you.** People don't *say* how terse they want -answers — they *show* it (they interrupt long replies, or move on before they -could have read them). `headroom learn --verbosity` reads your past sessions and -picks the level automatically: +**找到适合你的简洁度。** 人们不会*说*自己想要多简洁的回答——他们会*表现出来*(打断冗长回复,或在读完之前就继续下一步)。`headroom learn --verbosity` 会读取你过去的会话并自动选择合适级别: ```bash headroom learn --verbosity # preview what it found (dry run) headroom learn --verbosity --apply # save it; the proxy uses it from now on ``` -**See how many output tokens you saved.** Output savings are *counterfactual* — -we never see what the model *would* have written — so Headroom reports an honest -**estimate with a confidence range**, never a made-up number: +**查看你节省了多少输出 token。** 输出节省是*反事实*的——我们永远看不到模型*本来会*写什么——因此 Headroom 报告的是诚实的**估算值及置信区间**,而不是编造数字: ```bash headroom output-savings # Reduction: 31.7% (95% CI 27.7% … 35.7%) [estimated] ``` -Want a *measured* number instead of an estimate? Leave 10% of conversations -unshaped as a control group: `export HEADROOM_OUTPUT_HOLDOUT=0.1`. The dashboard -shows an **Output Tokens Saved** card next to input compression, labelled -`measured` or `estimated` with the confidence band. +想要*实测*数字而非估算?将 10% 的对话留作未塑形对照组:`export HEADROOM_OUTPUT_HOLDOUT=0.1`。仪表板会在输入压缩旁显示 **Output Tokens Saved** 卡片,标注为 `measured` 或 `estimated`,并附带置信区间。 -→ Full write-up incl. the measurement methodology: [Output token reduction](https://headroom-docs.vercel.app/docs/savings) +→ 完整说明(含测量方法学):[输出 token 缩减](https://headroom-docs.vercel.app/docs/savings) @@ -212,70 +197,65 @@ shows an **Output Tokens Saved** card next to input compression, labelled -## Agent compatibility matrix +## Agent 兼容性矩阵 | Agent | `headroom wrap` | Notes | |--------------|:---------------:|----------------------------------| | Claude Code | ✅ | `--memory` · `--code-graph` · `--1m` · `--tool-search` | -| Codex | ✅ | shares memory with Claude | -| Cursor | Manual setup | starts proxy and prints base URLs for Cursor settings | -| Aider | ✅ | starts proxy + launches | -| Copilot CLI | ✅ | starts proxy + launches | -| OpenClaw | ✅ | installs as ContextEngine plugin | -| OpenCode | ✅ | injects config · starts proxy + launches | -| Cline | ✅ | starts proxy + injects config | -| Continue | ✅ | starts proxy + injects config | -| Goose | ✅ | starts proxy + launches | -| OpenHands | ✅ | starts proxy + launches | -| Mistral Vibe | ✅ | starts proxy + launches | -| Cortex Code | Library only | 60–65% savings (library mode; no `wrap`) | +| Codex | ✅ | 与 Claude 共享记忆 | +| Cursor | Manual setup | 启动代理并打印 Cursor 设置的 base URL | +| Aider | ✅ | 启动代理 + 启动应用 | +| Copilot CLI | ✅ | 启动代理 + 启动应用 | +| OpenClaw | ✅ | 作为 ContextEngine 插件安装 | +| OpenCode | ✅ | 注入配置 · 启动代理 + 启动应用 | +| Cline | ✅ | 启动代理 + 注入配置 | +| Continue | ✅ | 启动代理 + 注入配置 | +| Goose | ✅ | 启动代理 + 启动应用 | +| OpenHands | ✅ | 启动代理 + 启动应用 | +| Mistral Vibe | ✅ | 启动代理 + 启动应用 | +| Cortex Code | Library only | 节省 60–65%(库模式;无 `wrap`) | -Any OpenAI-compatible client works via `headroom proxy`. MCP-native: `headroom mcp install`. -Undo durable wrapping with `headroom unwrap ` (supports: `claude`, `copilot`, `codex`, `opencode`, `openclaw`). +任何 OpenAI 兼容客户端均可通过 `headroom proxy` 使用。MCP 原生:`headroom mcp install`。 +使用 `headroom unwrap ` 撤销持久化包装(支持:`claude`、`copilot`、`codex`、`opencode`、`openclaw`)。 -### GitHub Copilot CLI subscription mode +### GitHub Copilot CLI 订阅模式 -Headroom can route GitHub Copilot CLI subscription traffic through the local proxy: +Headroom 可将 GitHub Copilot CLI 订阅流量路由到本地代理: ```bash headroom copilot-auth login headroom wrap copilot --subscription -- --model gpt-4o ``` -This lets Headroom intercept OpenAI-compatible Copilot CLI requests and apply the same proxy compression pipeline before forwarding to GitHub Copilot's hosted API. The wrapper exchanges Headroom's reusable GitHub OAuth token for Copilot's short-lived API token and prints the upstream endpoint as `COPILOT_PROVIDER_API_URL=...` during launch. +这样 Headroom 可拦截 OpenAI 兼容的 Copilot CLI 请求,在转发至 GitHub Copilot 托管 API 之前应用相同的代理压缩流水线。该包装器会用 Headroom 可复用的 GitHub OAuth token 换取 Copilot 的短期 API token,并在启动时将上游端点打印为 `COPILOT_PROVIDER_API_URL=...`。 -`headroom copilot-auth login` stores a Headroom-specific Copilot OAuth token. -This avoids relying on generic GitHub or Copilot CLI tokens that can read -Copilot account metadata but may still be rejected by Copilot's token-exchange -endpoint. +`headroom copilot-auth login` 会存储 Headroom 专用的 Copilot OAuth token。 +这可避免依赖通用的 GitHub 或 Copilot CLI token——它们虽可读取 Copilot 账户元数据,但仍可能被 Copilot 的 token 交换端点拒绝。 -For GitHub Enterprise Server or custom-domain Copilot deployments, set the -deployment domain before launching: +对于 GitHub Enterprise Server 或自定义域名的 Copilot 部署,请在启动前设置部署域名: ```bash export GITHUB_COPILOT_ENTERPRISE_DOMAIN=ghe.example.com ``` -For GitHub.com Enterprise Cloud URLs such as -`github.com/enterprises/your-enterprise`, do not set an enterprise-domain -override. Headroom uses GitHub's normal token-exchange endpoint and the Copilot -API endpoint advertised for the signed-in account. +对于 `github.com/enterprises/your-enterprise` 这类 GitHub.com Enterprise Cloud URL, +请勿设置 enterprise-domain 覆盖。Headroom 会使用 GitHub 的正常 token 交换端点,以及为已登录账户公布的 Copilot API 端点。 -Platform support note: macOS auth reuse via Copilot CLI Keychain storage has been smoke-tested. Windows Credential Manager, Linux Secret Service / `secret-tool`, and Docker/CI token-injection paths are implemented or planned as auth-discovery paths, but still need real OS validation before they should be considered fully vetted. For Docker and CI, prefer passing an explicit `GITHUB_COPILOT_TOKEN` or `GITHUB_COPILOT_GITHUB_TOKEN` rather than relying on host keychain access. +平台支持说明:通过 Copilot CLI Keychain 存储在 macOS 上复用认证已通过冒烟测试。Windows Credential Manager、Linux Secret Service / `secret-tool`,以及 Docker/CI token 注入路径已实现或规划中作为认证发现路径,但在经过真实操作系统验证之前,仍不应视为已完全验证。对于 Docker 与 CI,建议显式传入 `GITHUB_COPILOT_TOKEN` 或 `GITHUB_COPILOT_GITHUB_TOKEN`,而非依赖主机钥匙串访问。 -## When to use · When to skip +## 何时使用 · 何时跳过 -**Great fit if you…** -- run AI coding agents daily and want savings without changing your code -- work across multiple agents and want shared memory -- need reversible compression — originals are retrievable via CCR within the configured TTL +**非常适合,如果你…** +- 每天运行 AI 编程 agent,并希望在不改代码的情况下节省成本 +- 跨多个 agent 工作,并希望共享记忆 +- 需要可逆压缩——在配置的 TTL 内可通过 CCR 取回原始内容 -**Skip it if you…** -- only use a single provider's native compaction and don't need cross-agent memory -- work in a sandboxed environment where local processes can't run +**可以跳过,如果你…** +- 只使用单一提供商的原生压缩,且不需要跨 agent 记忆 +- 在沙箱环境中工作,本地进程无法运行
-Integrations — drop Headroom into any stack +集成 — 将 Headroom 接入任意技术栈 | Your setup | Hook in with | |------------------------|------------------------------------------------------------------| @@ -286,62 +266,63 @@ Platform support note: macOS auth reuse via Copilot CLI Keychain storage has bee | LiteLLM | `litellm.callbacks = [HeadroomCallback()]` | | LangChain | `HeadroomChatModel(your_llm)` | | Agno | `HeadroomAgnoModel(your_model)` | -| Strands | [Strands guide](https://headroom-docs.vercel.app/docs/strands) | +| Strands | [Strands 指南](https://headroom-docs.vercel.app/docs/strands) | | ASGI apps | `app.add_middleware(CompressionMiddleware)` | | Multi-agent | `SharedContext().put / .get` | | MCP clients | `headroom mcp install` | +
-What's inside +包含内容 -- **SmartCrusher** — universal JSON: arrays of dicts, nested objects, mixed types. -- **CodeCompressor** — AST-aware for Python, JS/TS, Go, Rust, Java, C/C++, Perl. -- **Kompress-v2-base** — our HuggingFace model, trained on agentic traces. -- **Image compression** — 40–90% reduction via trained ML router. -- **CacheAligner** — stabilizes prefixes so Anthropic/OpenAI KV caches actually hit. -- **Live-zone compression** — compresses only new bytes (fresh tool output, latest turn); frozen prefix stays byte-identical so provider cache is not busted. History is never dropped. -- **CCR** — reversible compression; LLM retrieves originals on demand. -- **Cross-agent memory** — shared store, agent provenance, auto-dedup. -- **SharedContext** — compressed context passing across multi-agent workflows. -- **`headroom learn`** — plugin-based failure mining for Claude, Codex, Gemini. +- **SmartCrusher** — 通用 JSON:字典数组、嵌套对象、混合类型。 +- **CodeCompressor** — 面向 Python、JS/TS、Go、Rust、Java、C/C++、Perl 的 AST 感知压缩。 +- **Kompress-v2-base** — 我们的 HuggingFace 模型,在 agentic traces 上训练。 +- **Image compression** — 通过训练的 ML 路由器实现 40–90% 缩减。 +- **CacheAligner** — 稳定前缀,使 Anthropic/OpenAI KV 缓存真正命中。 +- **Live-zone compression** — 仅压缩新增字节(新鲜工具输出、最新轮次);冻结前缀保持字节级一致,避免破坏提供商缓存。历史记录永不丢弃。 +- **CCR** — 可逆压缩;LLM 按需检索原始内容。 +- **Cross-agent memory** — 共享存储、智能体溯源、自动去重。 +- **SharedContext** — 跨多智能体工作流的压缩上下文传递。 +- **`headroom learn`** — 面向 Claude、Codex、Gemini 的基于插件的失败挖掘。
-Pipeline internals +流水线内部机制 -Headroom exposes one stable request lifecycle across `compress()`, the SDK, and the proxy: +Headroom 在 `compress()`、SDK 与代理之间暴露统一的请求生命周期: `Setup` → `Pre-Start` → `Post-Start` → `Input Received` → `Input Cached` → `Input Routed` → `Input Compressed` → `Input Remembered` → `Pre-Send` → `Post-Send` → `Response Received` -- **Transforms** do the work: CacheAligner → ContentRouter → SmartCrusher / CodeCompressor / Kompress-base (live-zone only; IntelligentContext and RollingWindow were retired in PR-B1). -- **Pipeline extensions** observe or customize lifecycle stages via `on_pipeline_event(...)`. -- **Compression hooks** sit alongside the canonical lifecycle as an additional extension seam. -- **Proxy extensions** remain the server/app integration seam for ASGI middleware, routes, and startup policy. +- **Transforms** 负责实际工作:CacheAligner → ContentRouter → SmartCrusher / CodeCompressor / Kompress-base(仅 live-zone;IntelligentContext 与 RollingWindow 已在 PR-B1 中退役)。 +- **Pipeline extensions** 通过 `on_pipeline_event(...)` 观察或自定义生命周期阶段。 +- **Compression hooks** 与规范生命周期并行,作为额外的扩展接缝。 +- **Proxy extensions** 仍是 ASGI 中间件、路由与启动策略的服务器/应用集成接缝。 -Provider and tool-specific behavior lives under `headroom/providers/` so core orchestration stays focused on lifecycle, sequencing, and policy. +提供商与工具特定行为位于 `headroom/providers/` 下,使核心编排专注于生命周期、顺序与策略。 -- **CLI/tool slices**: `headroom/providers/claude`, `copilot`, `codex`, `openclaw` -- **Provider runtime slices**: `headroom/providers/claude`, `gemini`, plus shared backend/runtime dispatch in `headroom/providers/registry.py` -- **Core files stay orchestration-first**: `wrap.py`, `client.py`, `cli/proxy.py`, and `proxy/server.py` delegate provider-specific env shaping, API target normalization, backend selection, and transport dispatch. +- **CLI/tool slices**:`headroom/providers/claude`、`copilot`、`codex`、`openclaw` +- **Provider runtime slices**:`headroom/providers/claude`、`gemini`,以及 `headroom/providers/registry.py` 中的共享后端/运行时调度 +- **Core files 保持编排优先**:`wrap.py`、`client.py`、`cli/proxy.py` 与 `proxy/server.py` 委托提供商特定的环境塑造、API 目标规范化、后端选择与传输调度。
-## Headroom for teams +## 面向团队的 Headroom -Headroom OSS is built for **individual developers**: run `headroom proxy` or `headroom wrap` on your laptop and start cutting tokens in minutes — free, local-first, your data never leaves your machine. +Headroom OSS 面向**个人开发者**构建:在笔记本上运行 `headroom proxy` 或 `headroom wrap`,几分钟内即可开始削减 token — 免费、本地优先,数据永不离开你的机器。 -Running it across a **whole engineering org** is a different job: a shared, always-on deployment; centralized config and version rollout; org-wide savings dashboards; SSO and access controls; air-gapped / VPC installs; and someone to call when it matters. That's what we help companies with — self-hosted with support, or fully managed. +在**整个工程组织**中运行则是另一回事:共享、始终在线的部署;集中化配置与版本发布;组织级节省仪表盘;SSO 与访问控制;气隙/VPC 安装;以及关键时刻有人可联系。这正是我们帮助企业完成的事 — 自托管加支持,或全托管。 -**If your team is spending real money on LLM tokens** — Claude Code, Codex, Cursor, or agents running in CI — **and you want those savings across everyone, not just one laptop:** +**若你的团队在大额支出 LLM token** — Claude Code、Codex、Cursor,或 CI 中运行的智能体 — **且希望所有人都能享受这些节省,而非仅一台笔记本:** -→ Email **[hello@headroomlabs.ai](mailto:hello@headroomlabs.ai)** with your stack and rough monthly LLM spend, and we'll help you roll Headroom out across your organization. +→ 发送邮件至 **[hello@headroomlabs.ai](mailto:hello@headroomlabs.ai)**,附上你的技术栈与大致月度 LLM 支出,我们将帮助你在组织内推广 Headroom。 -Everything in this repo stays open source (Apache 2.0). The managed offering is simply for teams that would rather have it deployed, supported, and scaled for them. +本仓库中的一切保持开源(Apache 2.0)。托管服务仅面向希望由我们部署、支持并扩展的团队。 -## Install +## 安装 ```bash pip install "headroom-ai[all]" # Python, everything — includes the `headroom` CLI @@ -349,27 +330,23 @@ npm install headroom-ai # TypeScript SDK (library only — no `h docker pull ghcr.io/chopratejas/headroom:latest ``` -Granular extras: `[proxy]`, `[mcp]`, `[ml]` (Kompress-v2-base), `[code]`, `[memory]`, `[vector]` (optional HNSW backend — needs a C++ toolchain, not in `[all]`), `[relevance]`, `[image]`, `[agno]`, `[langchain]`, `[evals]`, `[pytorch-mps]` (Apple-GPU memory-embedder offload — set `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps`). Requires **Python 3.10+**. +细粒度 extras:`[proxy]`、`[mcp]`、`[ml]`(Kompress-v2-base)、`[code]`、`[memory]`、`[vector]`(可选 HNSW 后端 — 需要 C++ 工具链,不在 `[all]` 中)、`[relevance]`、`[image]`、`[agno]`、`[langchain]`、`[evals]`、`[pytorch-mps]`(Apple-GPU memory-embedder 卸载 — 设置 `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps`)。需要 **Python 3.10+**。 -> **Note**: `[all]` covers the core stack but excludes framework adapters. Install them separately: `pip install "headroom-ai[langchain]"` (also `[agno]`, `[strands]`, `[anyllm]`, `[bedrock]`). +> **注意**:`[all]` 覆盖核心栈,但不包含框架适配器。请单独安装:`pip install "headroom-ai[langchain]"`(另有 `[agno]`、`[strands]`、`[anyllm]`、`[bedrock]`)。 -Using `pipx`? Choose a supported interpreter explicitly: +使用 `pipx`?请显式选择受支持的解释器: ```bash pipx install --python python3.13 "headroom-ai[all]" ``` -> **Pick 3.13 if you want dollar savings.** The dashboard's *Proxy $ Saved* tile prices compression with [LiteLLM](https://github.com/BerriAI/litellm), and LiteLLM can't be installed on Python 3.14+. On 3.14 token savings still track, but the dollar figure stays `$0.00`. If you already installed on 3.14, switch with `pipx reinstall headroom-ai --python python3.13` and restart the proxy. +> **若想看到美元节省,请选择 3.13。** 仪表盘的 *Proxy $ Saved* 磁贴使用 [LiteLLM](https://github.com/BerriAI/litellm), 为压缩定价,而 LiteLLM 无法在 Python 3.14+ 上安装。在 3.14 上仍可追踪 token 节省,但美元数字保持 `$0.00`。若你已在 3.14 上安装,请用 `pipx reinstall headroom-ai --python python3.13` 切换并重启代理。 -→ [Installation guide](https://headroom-docs.vercel.app/docs/installation) — Docker tags, persistent service, PowerShell, devcontainers. +→ [安装指南](https://headroom-docs.vercel.app/docs/installation) — Docker 标签、持久化服务、PowerShell、devcontainers。 -> **CPU requirement (x86/x86_64):** the ONNX-backed features — Magika content -> detection and embedding relevance — use a precompiled ONNX Runtime that needs -> **AVX2**. On x86 hosts without AVX2 (some Docker/QEMU setups and older cloud -> VMs) Headroom automatically falls back to its non-ONNX paths (BM25 relevance, -> heuristic detection) rather than crashing. `arm64`/Apple Silicon needs no AVX2. +> **CPU 要求(x86/x86_64):** ONNX 支持的功能 — Magika 内容检测与 embedding 相关性 — 使用需要 **AVX2** 的预编译 ONNX Runtime。在无 AVX2 的 x86 主机上(部分 Docker/QEMU 环境与较旧的云 VM),Headroom 会自动回退到非 ONNX 路径(BM25 相关性、启发式检测),而非崩溃。`arm64`/Apple Silicon 无需 AVX2。 -### Updating +### 更新 ```bash headroom update # detects pip / pipx / uv tool and upgrades in place @@ -377,21 +354,13 @@ headroom update --check # report the latest release without upgrading headroom update --pre # include pre-releases ``` -`headroom update` figures out how Headroom was installed (pip/venv, `pip --user`, -pipx, uv tool) and runs the matching upgrade across macOS, Linux, and Windows. -For git checkouts, editable installs, Docker images, and externally-managed -system Pythons (PEP 668) it prints the correct manual step instead of guessing. +`headroom update` 会判断 Headroom 的安装方式(pip/venv、`pip --user`、pipx、uv tool),并在 macOS、Linux 与 Windows 上执行匹配的升级。对于 git checkout、可编辑安装、Docker 镜像以及外部管理的系统 Python(PEP 668),它会打印正确的手动步骤,而非猜测。 -The proxy also shows a one-line "update available" notice on startup. It checks -PyPI at most once a day, in the background, and never blocks. Opt out with -`HEADROOM_UPDATE_CHECK=off` (also skipped in `--stateless` mode and CI). +代理还会在启动时显示一行「有可用更新」提示。它最多每天后台检查一次 PyPI,且从不阻塞。使用 `HEADROOM_UPDATE_CHECK=off` 可退出(在 `--stateless` 模式与 CI 中也会跳过)。 -### Corporate / SSL-inspection environments +### 企业 / SSL 检查环境 -If `pip install "headroom-ai[all]"` fails with `CERTIFICATE_VERIFY_FAILED` -(`unable to get local issuer certificate`), your network uses **SSL inspection** — a MITM -proxy presenting a company-issued CA. The build backend (`maturin`) downloads `rustup` over a -connection your TLS stack doesn't trust. **Install Rust first** so the build doesn't fetch it: +若 `pip install "headroom-ai[all]"` 因 `CERTIFICATE_VERIFY_FAILED`(`unable to get local issuer certificate`)失败,说明你的网络使用 **SSL inspection** — 即 MITM 代理出示公司签发的 CA。构建后端(`maturin`)通过你的 TLS 栈不信任的连接下载 `rustup`。**请先安装 Rust**,以免构建时再拉取: ```bash # macOS / Linux @@ -400,99 +369,84 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh && rustup default winget install Rustlang.Rustup && rustup default stable ``` -Restart your shell, then `pip install "headroom-ai[all]"`. A prebuilt wheel avoids the Rust -build entirely where available: `pip install --only-binary headroom-ai headroom-ai`. Prebuilt -wheels are published for Windows (`win_amd64`), Linux (`x86_64` / `aarch64`), and macOS -(Apple Silicon and Intel), so installs on those platforms never need a local Rust toolchain — the -Rust-first dance above is only for the platform-independent sdist fallback when no wheel matches. +重启 shell 后,再执行 `pip install "headroom-ai[all]"`。在可用时,预构建 wheel 可完全避免 Rust 构建:`pip install --only-binary headroom-ai headroom-ai`。预构建 wheel 面向 Windows(`win_amd64`)、Linux(`x86_64` / `aarch64`)与 macOS(Apple Silicon 与 Intel)发布,因此这些平台的安装永远不需要本地 Rust 工具链 — 上述「先装 Rust」流程仅适用于无匹配 wheel 时的平台无关 sdist 回退。 -Two runtime assets are fetched over TLS; if they are blocked, trust your corporate CA via -`REQUESTS_CA_BUNDLE` / `SSL_CERT_FILE` / `CURL_CA_BUNDLE`: +有两个运行时资源通过 TLS 拉取;若被阻断,请通过 `REQUESTS_CA_BUNDLE` / `SSL_CERT_FILE` / `CURL_CA_BUNDLE` 信任你的企业 CA: -- **`cdn.pyke.io`** — the ONNX Runtime for the Rust core. Alternatively pre-provide it with - `ORT_STRATEGY=system` and `ORT_LIB_LOCATION=/path/to/onnxruntime`. -- **`huggingface.co`** — the `kompress-base` compression model. Pre-download it and run with - `HF_HUB_OFFLINE=1`, or set `HF_ENDPOINT` to a trusted mirror. +- **`cdn.pyke.io`** — Rust 核心的 ONNX Runtime。也可通过 `ORT_STRATEGY=system` 与 `ORT_LIB_LOCATION=/path/to/onnxruntime` 预先提供。 +- **`huggingface.co`** — `kompress-base` 压缩模型。预先下载并用 `HF_HUB_OFFLINE=1` 运行,或设置 `HF_ENDPOINT` 指向可信镜像。 -Running with compression disabled (pure gateway) requires neither asset. +禁用压缩(纯网关)运行时无需上述任一资源。 -#### "Basic Constraints of CA cert not marked critical" (Python 3.13+ strict mode) +#### 「Basic Constraints of CA cert not marked critical」(Python 3.13+ 严格模式) -A **different** failure from the one above. If TLS fails with: +这与上文是**另一种**失败情形。若 TLS 失败并出现: ``` [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Basic Constraints of CA cert not marked critical ``` -then the corporate CA *is* found and trusted — adding it to a CA bundle changes nothing. -Python 3.13 + OpenSSL 3.x enable `VERIFY_X509_STRICT` by default, which enforces RFC 5280 -§4.2.1.9: a CA cert's `basicConstraints` must be marked *critical*. Inspection roots like -Zscaler set `CA:TRUE` without the critical bit, so the chain is rejected. +则企业 CA *已被*找到并信任 — 将其加入 CA bundle 不会改变结果。Python 3.13 + OpenSSL 3.x 默认启用 `VERIFY_X509_STRICT`,它强制执行 RFC 5280 §4.2.1.9:CA 证书的 `basicConstraints` 必须标记为 *critical*。Zscaler 等检查根证书将 `CA:TRUE` 设为无 critical 位,因此证书链被拒绝。 -Set **`HEADROOM_TLS_STRICT=0`** to clear *only* the strict flag from every TLS context -Headroom controls — the proxy's httpx upstream client **and** the urllib3/`huggingface_hub` -path used for model downloads. Chain validation, signature, expiry, and hostname checks all -stay on; this is strictly narrower than disabling verification. +将 **`HEADROOM_TLS_STRICT=0`** 设置为仅从每个 TLS 上下文中清除*严格*标志 +Headroom 控制项 — 代理的 httpx 上游客户端**以及**用于模型下载的 urllib3/`huggingface_hub` +路径。链验证、签名、过期和主机名检查均保持开启;这严格窄于禁用验证。 ```bash HEADROOM_TLS_STRICT=0 headroom proxy --port 8787 ``` -The Rust core's ONNX download (`cdn.pyke.io`) uses a separate TLS stack (rustls / OS trust -store), unaffected by `HEADROOM_TLS_STRICT`. On Windows the corporate root must be in the -**machine** certificate store (browsers already trust it there); or pre-provision ONNX -Runtime with `ORT_STRATEGY=system` + `ORT_LIB_LOCATION=/path/to/onnxruntime` to skip the -download entirely. +Rust 核心的 ONNX 下载(`cdn.pyke.io`)使用独立的 TLS 栈(rustls / 操作系统信任存储),不受 `HEADROOM_TLS_STRICT` 影响。在 Windows 上,企业根证书必须位于**机器**证书存储中(浏览器已在该处信任它);或使用 `ORT_STRATEGY=system` + `ORT_LIB_LOCATION=/path/to/onnxruntime` 预先配置 ONNX Runtime,以完全跳过下载。 ## headroom learn

- headroom learn in action + headroom learn 实际运行

-`headroom learn` — mines failed sessions, writes corrections to `CLAUDE.local.md` (default, gitignored; use `--target CLAUDE.md` for the shared team file) / `AGENTS.md` / `GEMINI.md`. +`headroom learn` — 挖掘失败会话,将修正写入 `CLAUDE.local.md`(默认,已被 git 忽略;团队共享文件请使用 `--target CLAUDE.md`)/ `AGENTS.md` / `GEMINI.md`。 -## Documentation +## 文档 -| Start here | Go deeper | +| 从这里开始 | 深入了解 | |-------------------------------------------------------------------------------|------------------------------------------------------------------------------------| -| [Quickstart](https://headroom-docs.vercel.app/docs/quickstart) | [Architecture](https://headroom-docs.vercel.app/docs/architecture) | -| [Proxy](https://headroom-docs.vercel.app/docs/proxy) | [How compression works](https://headroom-docs.vercel.app/docs/how-compression-works) | -| [MCP tools](https://headroom-docs.vercel.app/docs/mcp) | [CCR — reversible compression](https://headroom-docs.vercel.app/docs/ccr) | -| [Memory](https://headroom-docs.vercel.app/docs/memory) | [Cache optimization](https://headroom-docs.vercel.app/docs/cache-optimization) | -| [Failure learning](https://headroom-docs.vercel.app/docs/failure-learning) | [Benchmarks](https://headroom-docs.vercel.app/docs/benchmarks) | -| [Configuration](https://headroom-docs.vercel.app/docs/configuration) | [Limitations](https://headroom-docs.vercel.app/docs/limitations) | -| [Persistent installs](https://headroom-docs.vercel.app/docs/persistent-installs) (`headroom init` / `headroom install apply`) | [Savings analytics](https://headroom-docs.vercel.app/docs/savings) (`headroom savings` / `headroom perf` / `headroom doctor`) | +| [快速入门](https://headroom-docs.vercel.app/docs/quickstart) | [架构](https://headroom-docs.vercel.app/docs/architecture) | +| [代理](https://headroom-docs.vercel.app/docs/proxy) | [压缩原理](https://headroom-docs.vercel.app/docs/how-compression-works) | +| [MCP 工具](https://headroom-docs.vercel.app/docs/mcp) | [CCR — 可逆压缩](https://headroom-docs.vercel.app/docs/ccr) | +| [记忆](https://headroom-docs.vercel.app/docs/memory) | [缓存优化](https://headroom-docs.vercel.app/docs/cache-optimization) | +| [失败学习](https://headroom-docs.vercel.app/docs/failure-learning) | [基准测试](https://headroom-docs.vercel.app/docs/benchmarks) | +| [配置](https://headroom-docs.vercel.app/docs/configuration) | [局限性](https://headroom-docs.vercel.app/docs/limitations) | +| [持久化安装](https://headroom-docs.vercel.app/docs/persistent-installs) (`headroom init` / `headroom install apply`) | [节省分析](https://headroom-docs.vercel.app/docs/savings) (`headroom savings` / `headroom perf` / `headroom doctor`) | -## Compared to +## 对比 -Headroom runs **locally**, covers **every** content type, works with every major framework, and is **reversible**. +Headroom **在本地运行**,覆盖**所有**内容类型,兼容各大主流框架,且**可逆**。 -| | Scope | Deploy | Local | Reversible | +| | 范围 | 部署 | 本地 | 可逆 | |------------------------------------------------------------------------------|------------------------------------------------|------------------------------------|:-----:|:----------:| -| **Headroom** | All context — tools, RAG, logs, files, history | Proxy · library · middleware · MCP | Yes | Yes | -| [RTK](https://github.com/rtk-ai/rtk) | CLI command outputs | CLI wrapper | Yes | No | -| [lean-ctx](https://github.com/yvgude/lean-ctx) | Tool output, files, shell, history | Proxy · library · middleware · MCP · CLI | Yes | Yes | -| [Compresr](https://compresr.ai), [Token Co.](https://thetokencompany.ai) | Text sent to their API | Hosted API call | No | No | -| OpenAI Compaction | Conversation history | Provider-native | No | No | +| **Headroom** | 全部上下文 — 工具、RAG、日志、文件、历史记录 | 代理 · 库 · 中间件 · MCP | 是 | 是 | +| [RTK](https://github.com/rtk-ai/rtk) | CLI 命令输出 | CLI 包装器 | 是 | 否 | +| [lean-ctx](https://github.com/yvgude/lean-ctx) | 工具输出、文件、Shell、历史记录 | 代理 · 库 · 中间件 · MCP · CLI | 是 | 是 | +| [Compresr](https://compresr.ai), [Token Co.](https://thetokencompany.ai) | 发送至其 API 的文本 | 托管 API 调用 | 否 | 否 | +| OpenAI Compaction | 对话历史 | 提供商原生 | 否 | 否 | -> **Attribution.** Headroom ships with the excellent [RTK](https://github.com/rtk-ai/rtk) binary for shell-output rewriting — `git show --short`, scoped `ls`, summarized installers. Huge thanks to the RTK team; their tool is a first-class part of our stack, and Headroom compresses everything downstream of it. Headroom can also use [lean-ctx](https://github.com/yvgude/lean-ctx) as the selected CLI context tool; set `HEADROOM_CONTEXT_TOOL=lean-ctx` before running `headroom wrap ...`. +> **致谢。** Headroom 内置了出色的 [RTK](https://github.com/rtk-ai/rtk) 二进制文件,用于 Shell 输出重写 — `git show --short`、作用域化的 `ls`、摘要化安装器。衷心感谢 RTK 团队;他们的工具是我们技术栈的一等公民,Headroom 会压缩其下游的一切内容。Headroom 也可将 [lean-ctx](https://github.com/yvgude/lean-ctx) 用作选定的 CLI 上下文工具;在运行 `headroom wrap ...` 之前设置 `HEADROOM_CONTEXT_TOOL=lean-ctx`。 -## Contributing +## 贡献 ```bash git clone https://github.com/chopratejas/headroom.git && cd headroom uv sync --extra dev && uv run pytest ``` -Devcontainers in `.devcontainer/` (default + `memory-stack` with Qdrant & Neo4j). See [CONTRIBUTING.md](CONTRIBUTING.md). +`.devcontainer/` 中的 Devcontainers(默认 + 带 Qdrant 与 Neo4j 的 `memory-stack`)。参见 [CONTRIBUTING.md](CONTRIBUTING.md)。 -## Community +## 社区 -- **[Discord](https://discord.gg/yRmaUNpsPJ)** — questions, feedback, war stories. -- **[Kompress-v2-base on HuggingFace](https://huggingface.co/chopratejas/kompress-v2-base)** — the model behind our text compression. +- **[Discord](https://discord.gg/yRmaUNpsPJ)** — 提问、反馈与实战分享。 +- **[Kompress-v2-base on HuggingFace](https://huggingface.co/chopratejas/kompress-v2-base)** — 我们文本压缩背后的模型。 -## License +## 许可证 -Apache 2.0 — see [LICENSE](LICENSE). +Apache 2.0 — 参见 [LICENSE](LICENSE)。