P430分钟

记忆系统

记忆系统让 Agent 能够保持上下文、从经验中学习,并在多个会话之间保持一致性。
SDK FocusresumeforkSession()continuesessionStorepersistSessionPreCompact

记忆类型

Agent 记忆借用了人类记忆的术语,但这只是工程类比,不是生物学模型:

记忆架构:工作记忆、情景记忆、语义记忆及长期存储工作记忆(短期/缓冲区)情景记忆语义记忆长期存储(向量数据库 / 文件系统)

工作记忆

当前对话上下文,直接保存在 LLM 的上下文窗口中:

  • 用户的当前请求
  • 最近的对话历史
  • 当前任务状态
  • 最近读取的文件内容
sessions.ts
import { forkSession, query } from "@anthropic-ai/claude-agent-sdk";

// 1. 启动会话——从 result 消息捕获 session_id
let sessionId: string | undefined;
for await (const msg of query({ prompt: "Analyze src/ directory structure" })) {
if (msg.type === "result") {
  sessionId = msg.session_id;
}
}
if (!sessionId) throw new Error("No session id returned");

// 2. 按 ID 恢复——精确,适合按用户/任务管理会话
for await (const msg of query({
prompt: "Now refactor the largest file you found",
options: { resume: sessionId }
})) {
if (msg.type === "result") console.log(msg.subtype);
}

// 3. continue——不传 ID;恢复当前 cwd 下最近的会话
for await (const msg of query({
prompt: "Also add error handling",
options: { continue: true }
})) {
if (msg.type === "result") console.log(msg.subtype);
}

// 4. 离线分叉,再恢复分支
const { sessionId: branchId } = await forkSession(sessionId, {
title: "GraphQL alternative"
});
for await (const msg of query({
prompt: "Try a different approach using GraphQL",
options: { resume: branchId }
})) {
if (msg.type === "result") console.log(msg.subtype);
}
piPi 等效写法有状态 session 对象,加上 SessionManager 文件与树
typescript
// sessions.ts
import { createAgentSession, SessionManager } from "@earendil-works/pi-coding-agent";

const cwd = process.cwd();

// 1. 启动一个持久化会话——默认位置是
//    ~/.pi/agent/sessions/<encoded-cwd>/。
const sessionManager = SessionManager.create(cwd);
const { session } = await createAgentSession({
  sessionManager,
  tools: ["read", "glob"],
});
// 2. 多轮 = 复用同一个有状态对象再 prompt 一次。
await session.prompt("Analyze src/ directory structure");
await session.prompt("Now refactor the largest file you found");

// 3. 续接当前 cwd 最近的持久化会话。
const continuedManager = SessionManager.continueRecent(cwd);
const { session: continued } = await createAgentSession({
  sessionManager: continuedManager,
  tools: ["read", "glob"],
});
await continued.prompt("Also add error handling");

// 4. 在同一个 JSONL 树内切分支,然后追加一条不同路径。
const targetEntryId = sessionManager.getLeafId();
if (targetEntryId) {
  await session.navigateTree(targetEntryId, {
    summarize: true,
    label: "graphql-alternative",
  });
  await session.prompt("Try a different approach using GraphQL");
}

// 5. fork/clone 风格的工作流使用 SessionManager 文件。
const sessions = await SessionManager.list(cwd);
const forkManager = SessionManager.forkFrom(sessions[0].path, cwd);
const { session: forked } = await createAgentSession({ sessionManager: forkManager });
await forked.prompt("Try a different approach using GraphQL");

// 6. 临时运行可以完全不落文件。
const scratch = SessionManager.inMemory(cwd);
await createAgentSession({ sessionManager: scratch, tools: ["read"] });

核心差异

续接模型:

SessionManager.open(path) / continueRecent(cwd) 加活的 AgentSession vs 在 query() 上传 resume: id

分叉:

navigateTree() 在 JSONL 树内切分支,或用 SessionManager.forkFrom() / CLI /fork/clone 创建独立 session 文件 vs forkSession(sessionId)

continue:

新进程用 continueRecent(cwd),活对象上直接再 .prompt(),vs Claude Agent SDK 的 continue: true

情景记忆

过去的经验和事件,用于:

高级课程预览

继续阅读完整内容

这节课会保留在当前 URL 下供预览。完整访问权限会解锁正文、闪卡和相关练习

登录后解锁

一次购买,解锁进阶课程、实战练习以及后续更新