模式 5:独立记忆(批处理)
对话结束之后再调一次。把完整记录发给 /process(或带 messages 的 sessions.end),让 Sonzai 在后台抽取事实、情绪、人格与习惯。
你拥有完整对话。Sonzai 永远看不到实时过程。当对话结束 — 通话挂断、客服 工单关闭、日志会话写完 — 你一次性 POST 整段记录,Sonzai 的提取器会把它 转成事实、情绪更新、人格漂移、习惯检测和主动外联信号。最适合辅导、健身、 CRM、语音通话、写日记,以及任何让 Sonzai 处于热路径既不可取也不可行的 流程。
何时使用
- 延迟预算无法承受每个回合一次
/turn往返。 - 记录已经存在(录音、Gong/Zoom 导出、日记内容)。
- 你想事后批量摄入 — 回放日志、迁移用户、给智能体质量做基准测试。
何时切换
- 你想要每回合的新鲜上下文 — 模式 4:独立实时。
- 可以接受连 LLM 调用也交给 Sonzai — 模式 1:托管运行时。
架构
┌─────────────┐ ┌──────────────────┐ ┌──────────────┐
│ Your App │ │ Sonzai API │ │ Your LLM │
└──────┬──────┘ └────────┬─────────┘ └──────┬───────┘
│ │ │
│ GET /context │ │
│────────────────────>│ (optional pre-session │
│ <── user profile ──│ personalization) │
│ │ │
│ ══ Your conversation (Sonzai not involved) ═════════│
│ │ │ │
│ Chat ──────────────┼──────────────────────>│ │
│ <── reply ─────────┼───────────────────────│ │
│ [N turns, your loop, your tools] │ │
│ │ │ │
│ ════════════════════════════════════════════════════════│
│ │ │
│ /process or sessions.end({ messages }) │
│────────────────────>│── extract facts, │
│ (full transcript) │ personality, mood, │
│ │ habits, interests │
│ <── extractions ───│ (Sonzai LLM) │
│ │ │
│ Use insights │ │
│ (push notif, │ │
│ dashboard, │ │
│ exercises, …) │ │
└─────────────────────┴───────────────────────┘
端到端示例
最简路径是 /process:一次调用、自动建会话、返回生成的 session_id
便于关联。当你需要会话级工具、时长、或异步轮询时,再使用显式
sessions.start → end({ messages }) 生命周期。
import { Sonzai } from "@sonzai-labs/agents";
const sonzai = new Sonzai({ apiKey: process.env.SONZAI_API_KEY! });
async function ingestTranscript(
agentId: string,
userId: string,
transcript: { role: "user" | "assistant" | "tool"; content: string; tool_calls?: any[] }[],
) {
// One call. Auto-creates a session. Tool messages allowed.
const result = await sonzai.agents.process(agentId, {
userId,
messages: transcript,
provider: "gemini", // optional override
model: "gemini-3.1-flash-lite-preview", // optional override
});
// result.session_id is the auto-created session id.
// Pull extractions from the read endpoints when ready:
const memory = await sonzai.agents.memory.list(agentId, { userId });
const mood = await sonzai.agents.getMood(agentId, { userId });
return { sessionId: result.session_id, memory, mood };
}二选一,别两个都用
/process 与 sessions.end({ messages }) 在批量摄入语义上等价 — 两者
都会基于完整记录同步抽取事实和副作用。对同一份记录不要两个都调,
否则提取会跑两次。要"一次调用"的简单形态选 /process;需要显式生命
周期、异步轮询或会话级工具时选 sessions.start + sessions.end({ messages })。
什么时候跑什么
/process 与 sessions.end 故意做得轻量:每次调用只抽取事实和会话摘要
(每个分块一次 LLM 调用)。昂贵的跨会话工作(去重、聚类、日记、衰减)
由平台按调度自动跑 — 你不会因为每次调用而被收这部分费用。