Skip to content
Jai Coding Agent
Esc
↑↓navigate↵open⌘Jpreview
On this page

示例:持久化会话

建立多轮连续对话、注入 SessionStore 适配器与配置文件权责。

在构建交互式桌面端或 CLI 应用时,用户希望与 Agent 进行多轮迭代,且历史上下文能够安全落盘。本示例展示如何通过 session: { kind: "new" } 创建持久化会话。

import {
  createCodingAgent,
  InMemorySessionStore,
  type CodingAgentFileCapabilities,
} from "jai-coding-agent";

// 1. 准备存储驱动(生产环境推荐对接 SQLite 等持久存储)
const store = new InMemorySessionStore();

// 2. 声明本会话的物理工作区与配置边界
const fileCapabilities: CodingAgentFileCapabilities = {
  homeDirectory: "/Users/username",
  workspaceDirectory: "/Users/username/projects/my-web-app",
  workspaceTrusted: true, // 信任此工作区以读取项目级自定义规则
};

// 3. 创建持久化会话实例
const created = await createCodingAgent({
  model: "anthropic/claude-3-7-sonnet",
  cwd: fileCapabilities.workspaceDirectory,
  session: {
    kind: "new",
    store,
    // id 为可选参数,缺省时自动生成如 "coding-uuid" 格式的标识
  },
  fileCapabilities,
  permissionMode: "ask",
  // 简化的审批回调
  requestApproval: async () => "allowOnce",
});

if (created.isErr()) {
  console.error("创建失败:", created.error.code, created.error.message);
  process.exit(1);
}

const agent = created.value;
console.log("新建会话 ID:", agent.sessionId);

// 第一轮任务:生成脚手架
await agent.prompt("在 src/math.ts 中实现加减乘除四个基础算术函数,并导出它们");

// 第二轮任务:基于上一轮的上下文继续编写测试
await agent.prompt("为刚刚创建的 math.ts 编写对应的单元测试文件 test/math.test.ts");

// 优雅关闭实例
await agent.close();

关键要点

  • 连续上下文:在同一个 Agent 实例上多次调用 agent.prompt(),后续轮次会自动继承先前的消息历史与工具变更记录。
  • 保存会话 ID:记录下 agent.sessionId,后续即便当前进程退出,只要底层数据已写入 store,就可以随时通过 kind: "resume" 重新接入。

Was this page helpful?