@context-chef/core
核心上下文编译器 —— 历史压缩、工具裁剪、记忆、VFS 卸载、快照/恢复和多 provider 适配,无框架依赖。当你直接驱动 LLM SDK(OpenAI / Anthropic / Gemini)并希望完全控制编译管道时使用它。
安装
bash
npm install @context-chef/core zod快速开始
typescript
import { ContextChef } from "@context-chef/core";
import { z } from "zod";
const TaskSchema = z.object({
activeFile: z.string(),
todo: z.array(z.string()),
});
const chef = new ContextChef({
janitor: {
contextWindow: 200000,
compressionModel: async (msgs) => callGpt4oMini(msgs),
},
});
const payload = await chef
.setSystemPrompt([
{
role: "system",
content: "You are an expert coder.",
_cache_breakpoint: true,
},
])
.setHistory(conversationHistory)
.setDynamicState(TaskSchema, {
activeFile: "auth.ts",
todo: ["Fix login bug"],
})
.withGuardrails({
enforceXML: { outputTag: "response" },
prefill: "<thinking>\n1.",
})
.compile({ target: "anthropic" });
const response = await anthropic.messages.create(payload);内含模块
完整 API 按模块分布在指南中,一页一个关注点:
| 模块 | 指南页面 |
|---|---|
Janitor —— 历史压缩、compact()、ensureValidHistory | 历史压缩 |
planCompaction / compactHistory / summarizeHistory | 持久化压缩 |
contextManagement: { strategy: 'server' } | 服务端上下文管理 |
Offloader / VFS —— offload、cleanupAsync、reconcileAsync | 卸载与 VFS |
Pruner —— 扁平模式、blocklist、namespace、deferLoading | 工具管理 |
Memory —— store、记忆工具、memoryPlacement | 记忆 |
Skill —— SKILL.md 加载、renderSkill、交付模型 | Skill |
withGuardrails —— XML 契约 + prefill | 护栏 |
snapshot() / restore() | 快照与恢复 |
事件、compile({ signal })、并发、onBeforeCompile | 事件与钩子 |
输入/目标适配器、adapterRegistry、openai-responses | 适配器 |
工具函数
estimate(text)/estimateObject(obj)—— 粗略 token 估算(替代已移除的TokenUtils)。objectToXml(obj)—— 动态状态背后的 XML 序列化器(替代已移除的XmlGenerator)。getAdapter(name)/adapterRegistry—— 适配器查找与注册(替代已移除的AdapterFactory)。ensureValidHistory(messages)—— 修复消息数组以满足 LLM API 约束。getRecallToolDefinition()/chef.resolveRecall(uri)—— 归档/卸载内容的召回工具。