Skip to content

@context-chef/core

核心上下文编译器 —— 历史压缩、工具裁剪、记忆、VFS 卸载、快照/恢复和多 provider 适配,无框架依赖。当你直接驱动 LLM SDK(OpenAI / Anthropic / Gemini)并希望完全控制编译管道时使用它。

npm versionnpm downloads

安装

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 —— offloadcleanupAsyncreconcileAsync卸载与 VFS
Pruner —— 扁平模式、blocklist、namespace、deferLoading工具管理
Memory —— store、记忆工具、memoryPlacement记忆
Skill —— SKILL.md 加载、renderSkill、交付模型Skill
withGuardrails —— XML 契约 + prefill护栏
snapshot() / restore()快照与恢复
事件、compile({ signal })、并发、onBeforeCompile事件与钩子
输入/目标适配器、adapterRegistryopenai-responses适配器

工具函数

  • estimate(text) / estimateObject(obj) —— 粗略 token 估算(替代已移除的 TokenUtils)。
  • objectToXml(obj) —— 动态状态背后的 XML 序列化器(替代已移除的 XmlGenerator)。
  • getAdapter(name) / adapterRegistry —— 适配器查找与注册(替代已移除的 AdapterFactory)。
  • ensureValidHistory(messages) —— 修复消息数组以满足 LLM API 约束。
  • getRecallToolDefinition() / chef.resolveRecall(uri) —— 归档/卸载内容的召回工具。

链接