会话
会话为 Agents SDK 提供持久化记忆层。向 Runner.run 提供任何实现 Session 接口的对象,SDK 就会处理其余工作。当存在会话时,runner 会自动:
- 获取先前存储的对话项,并将它们前置到下一轮。
- 在每次运行完成后,持久化新的用户输入和助手输出。
- 让该会话可用于后续轮次,无论你是用新的用户文本调用 runner,还是从被中断的
RunState恢复。
这让你无需手动调用 toInputList(),也无需在轮次之间拼接历史记录。TypeScript SDK 随附两个实现:面向 Conversations API 的 OpenAIConversationsSession,以及用于本地开发的 MemorySession。由于它们共享 Session 接口,你可以接入自己的存储后端。若想获得 Conversations API 之外的灵感,可以查看 examples/memory/ 下的示例会话后端(Prisma、基于文件的后端等)。使用 OpenAI Responses 模型时,请用 OpenAIResponsesCompactionSession 包装任意会话,以通过 responses.compact 自动压缩已存储的对话历史。
提示:要运行本页中的
OpenAIConversationsSession示例,请设置OPENAI_API_KEY环境变量(或在构造会话时提供apiKey),以便 SDK 可以调用 Conversations API。
当你希望 SDK 为你管理客户端侧记忆时,请使用会话。如果你已经在通过 conversationId 或 previousResponseId 使用 OpenAI 服务器管理的状态,通常不需要再为同一段对话历史使用会话。
使用 OpenAIConversationsSession 将记忆与 Conversations API 同步,或替换为任何其他 Session 实现。
import { Agent, OpenAIConversationsSession, run } from '@openai/agents';
const agent = new Agent({ name: 'TourGuide', instructions: 'Answer with compact travel facts.',});
// Any object that implements the Session interface works here. This example uses// the built-in OpenAIConversationsSession, but you can swap in a custom Session.const session = new OpenAIConversationsSession();
const firstTurn = await run(agent, 'What city is the Golden Gate Bridge in?', { session,});console.log(firstTurn.finalOutput); // "San Francisco"
const secondTurn = await run(agent, 'What state is it in?', { session });console.log(secondTurn.finalOutput); // "California"复用同一个会话实例可确保智能体在每一轮之前收到完整的对话历史,并自动持久化新项。切换到不同的 Session 实现无需进行其他代码更改。
对于本地演示、测试或进程本地聊天状态,MemorySession 提供相同的接口且无需与 OpenAI 通信:
import { Agent, MemorySession, run } from '@openai/agents';
const agent = new Agent({ name: 'TourGuide', instructions: 'Answer with compact travel facts.',});
const session = new MemorySession();const result = await run(agent, 'What city is the Golden Gate Bridge in?', { session,});
console.log(result.finalOutput);OpenAIConversationsSession 构造函数选项:
| 选项 | 类型 | 说明 |
|---|---|---|
conversationId | string | 复用现有对话,而不是延迟创建一个对话。 |
client | OpenAI | 传入预配置的 OpenAI 客户端。 |
apiKey | string | 创建内部 OpenAI 客户端时使用的 API 密钥。 |
baseURL | string | OpenAI 兼容端点的基础 URL。 |
organization | string | 请求使用的 OpenAI 组织 ID。 |
project | string | 请求使用的 OpenAI 项目 ID。 |
MemorySession 构造函数选项:
| 选项 | 类型 | 说明 |
|---|---|---|
sessionId | string | 用于日志或测试的稳定标识符。默认自动生成。 |
initialItems | AgentInputItem[] | 用现有历史初始化会话。 |
logger | Logger | 覆盖用于调试输出的日志记录器。 |
MemorySession 会将所有内容存储在本地进程内存中,因此当进程退出时会被重置。
如果你需要在构造会话前预先创建对话 ID,请使用 startOpenAIConversationsSession(client?),并将返回的 ID 作为 conversationId 传入。
核心会话行为
Section titled “核心会话行为”runner 对会话的使用方式
Section titled “runner 对会话的使用方式”- 每次运行前它会检索会话历史,将其与新轮次的输入合并,并将合并后的列表传给你的智能体。
- 非流式运行后一次
session.addItems()调用会同时持久化原始用户输入和最新轮次的模型输出。 - 对于流式运行它会先写入用户输入,并在该轮完成后追加流式输出。
- 从
RunResult.state恢复时(用于审批或其他中断)请继续传入同一个session。恢复后的轮次会被添加到记忆中,而不会重新准备输入。
历史记录的查看与编辑
Section titled “历史记录的查看与编辑”会话提供简单的 CRUD 辅助方法,方便你构建”撤销”、“清空聊天”或审计功能。
import { OpenAIConversationsSession } from '@openai/agents';import type { AgentInputItem } from '@openai/agents-core';
// Replace OpenAIConversationsSession with any other Session implementation that// supports get/add/pop/clear if you store history elsewhere.const session = new OpenAIConversationsSession({ conversationId: 'conv_123', // Resume an existing conversation if you have one.});
const history = await session.getItems();console.log(`Loaded ${history.length} prior items.`);
const followUp: AgentInputItem[] = [ { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'Let’s continue later.' }], },];await session.addItems(followUp);
const undone = await session.popItem();
if (undone?.type === 'message') { console.log(undone.role); // "user"}
await session.clearSession();session.getItems() 返回存储的 AgentInputItem[]。调用 popItem() 删除最后一项——这对于在重新运行智能体前处理用户更正很有用。
自定义存储与合并行为
Section titled “自定义存储与合并行为”实现 Session 接口,即可用 Redis、DynamoDB、SQLite 或其他数据存储来支撑记忆。只需五个异步方法。
import { Agent, run } from '@openai/agents';import { randomUUID } from '@openai/agents-core/_shims';import { getLogger } from '@openai/agents-core';import type { AgentInputItem, Session } from '@openai/agents-core';
/** * Minimal example of a Session implementation; swap this class for any storage-backed version. */export class CustomMemorySession implements Session { private readonly sessionId: string; private readonly logger: ReturnType<typeof getLogger>;
private items: AgentInputItem[];
constructor( options: { sessionId?: string; initialItems?: AgentInputItem[]; logger?: ReturnType<typeof getLogger>; } = {}, ) { this.sessionId = options.sessionId ?? randomUUID(); this.items = options.initialItems ? options.initialItems.map(cloneAgentItem) : []; this.logger = options.logger ?? getLogger('openai-agents:memory-session'); }
async getSessionId(): Promise<string> { return this.sessionId; }
async getItems(limit?: number): Promise<AgentInputItem[]> { if (limit === undefined) { const cloned = this.items.map(cloneAgentItem); this.logger.debug( `Getting items from memory session (${this.sessionId}): ${JSON.stringify(cloned)}`, ); return cloned; } if (limit <= 0) { return []; } const start = Math.max(this.items.length - limit, 0); const items = this.items.slice(start).map(cloneAgentItem); this.logger.debug( `Getting items from memory session (${this.sessionId}): ${JSON.stringify(items)}`, ); return items; }
async addItems(items: AgentInputItem[]): Promise<void> { if (items.length === 0) { return; } const cloned = items.map(cloneAgentItem); this.logger.debug( `Adding items to memory session (${this.sessionId}): ${JSON.stringify(cloned)}`, ); this.items = [...this.items, ...cloned]; }
async popItem(): Promise<AgentInputItem | undefined> { if (this.items.length === 0) { return undefined; } const item = this.items[this.items.length - 1]; const cloned = cloneAgentItem(item); this.logger.debug( `Popping item from memory session (${this.sessionId}): ${JSON.stringify(cloned)}`, ); this.items = this.items.slice(0, -1); return cloned; }
async clearSession(): Promise<void> { this.logger.debug(`Clearing memory session (${this.sessionId})`); this.items = []; }}
function cloneAgentItem<T extends AgentInputItem>(item: T): T { return structuredClone(item);}
const agent = new Agent({ name: 'MemoryDemo', instructions: 'Remember the running total.',});
// Using the above custom memory session implementation hereconst session = new CustomMemorySession({ sessionId: 'session-123-4567',});
const first = await run(agent, 'Add 3 to the total.', { session });console.log(first.finalOutput);
const second = await run(agent, 'Add 4 more.', { session });console.log(second.finalOutput);自定义会话让你能够强制执行保留策略、添加加密,或在持久化前为每个对话轮次附加元数据。
历史与新项合并方式的控制
Section titled “历史与新项合并方式的控制”当你将 AgentInputItem 数组作为运行输入传入时,请提供 sessionInputCallback,以确定性的方式将它们与已存储历史合并。runner 会加载现有历史,在调用模型之前调用你的回调,并将返回的数组作为本轮完整输入交给模型。这个钩子非常适合裁剪旧项、去重工具结果,或只突出你希望模型看到的上下文。
import { Agent, OpenAIConversationsSession, run } from '@openai/agents';import type { AgentInputItem } from '@openai/agents-core';
const agent = new Agent({ name: 'Planner', instructions: 'Track outstanding tasks before responding.',});
// Any Session implementation can be passed here; customize storage as needed.const session = new OpenAIConversationsSession();
const todoUpdate: AgentInputItem[] = [ { type: 'message', role: 'user', content: [ { type: 'input_text', text: 'Add booking a hotel to my todo list.' }, ], },];
await run(agent, todoUpdate, { session, // function that combines session history with new input items before the model call sessionInputCallback: (history, newItems) => { const recentHistory = history.slice(-8); return [...recentHistory, ...newItems]; },});对于字符串输入,runner 会自动合并历史,因此回调是可选的。只有当你的本轮输入已经是项数组时,该回调才会运行。
如果你还在使用 conversationId 或 previousResponseId,请在回调结果中保留当前轮次的至少一个新项。这些由服务器管理的 API 依赖当前轮次的增量。如果回调丢弃了所有新项,SDK 会恢复原始的新输入并记录警告,而不是发送空增量。
审批与可恢复运行
Section titled “审批与可恢复运行”人工干预流程通常会暂停运行以等待审批:
import { Agent, MemorySession, Runner } from '@openai/agents';
const agent = new Agent({ name: 'Trip Planner', instructions: 'Plan trips and ask for approval before booking anything.',});
const runner = new Runner();const session = new MemorySession();
const result = await runner.run(agent, 'Search the itinerary', { session,});
if (result.interruptions?.length) { // ... collect user feedback, then resume the agent in a later turn. for (const interruption of result.interruptions) { result.state.approve(interruption); }
const continuation = await runner.run(agent, result.state, { session }); console.log(continuation.finalOutput);}当你从先前的 RunState 恢复时,新轮次会追加到同一条记忆记录中,以保留单一的对话历史。人工干预(HITL)流程保持完全兼容——审批检查点仍通过 RunState 往返传递,而会话则保持对话历史完整。
高级:历史压缩
Section titled “高级:历史压缩”OpenAI Responses 历史的自动压缩
Section titled “OpenAI Responses 历史的自动压缩”OpenAIResponsesCompactionSession 会装饰任意 Session,并使用 OpenAI Responses API 将较长的已存储历史替换为更短且等价的对话项列表。每个轮次持久化后,runner 会将最新的 responseId 传入 runCompaction;当你的决策钩子返回 true 时,它会调用 responses.compact。根据 compactionMode 的不同,请求会基于最新的 Responses API 链,或基于会话当前项来构建。默认触发器会在累计至少 10 个非用户项后进行压缩;你可以覆盖 shouldTriggerCompaction,以基于 token 数量或自定义启发式规则做出判断。压缩返回后,装饰器会清空底层会话,并用缩减后的项列表重写它,因此请避免将其与 OpenAIConversationsSession 搭配使用,后者使用不同的服务器管理历史流程。
import { Agent, MemorySession, OpenAIResponsesCompactionSession, run,} from '@openai/agents';
const agent = new Agent({ name: 'Support', instructions: 'Answer briefly and keep track of prior context.', model: 'gpt-5.4',});
// Wrap any Session to trigger responses.compact once history grows beyond your threshold.const session = new OpenAIResponsesCompactionSession({ // You can pass any Session implementation except OpenAIConversationsSession underlyingSession: new MemorySession(), // (optional) The model used for calling responses.compact API model: 'gpt-5.4', // (optional) your custom logic here shouldTriggerCompaction: ({ compactionCandidateItems }) => { return compactionCandidateItems.length >= 12; },});
await run(agent, 'Summarize order #8472 in one sentence.', { session });await run(agent, 'Remind me of the shipping address.', { session });
// Compaction runs automatically after each persisted turn. You can also force it manually.await session.runCompaction({ force: true });OpenAIResponsesCompactionSession 构造函数选项:
| 选项 | 类型 | 说明 |
|---|---|---|
client | OpenAI | 用于 responses.compact 的 OpenAI 客户端。 |
underlyingSession | Session | 要用压缩后项清空并重写的后端会话存储。默认是用于演示的内存会话,且不得是 OpenAIConversationsSession。 |
model | OpenAI.ResponsesModel | 用于压缩请求的模型。默认使用 SDK 当前默认的 OpenAI 模型。 |
compactionMode | 'auto' | 'previous_response_id' | 'input' | 控制压缩使用服务器响应链还是本地输入项。 |
shouldTriggerCompaction | (context) => boolean | Promise<boolean> | 基于 responseId、compactionMode、候选项和当前会话项的自定义触发钩子。 |
当你已经使用 Responses API 响应 ID 串联轮次时,compactionMode: 'previous_response_id' 很有用。compactionMode: 'input' 会改为从当前会话项重建压缩请求,这在响应链不可用,或你希望底层会话内容成为事实来源时很有帮助。
runCompaction(args) 选项:
| 选项 | 类型 | 说明 |
|---|---|---|
responseId | string | previous_response_id 模式下最新的 Responses API 响应 ID。 |
compactionMode | 'auto' | 'previous_response_id' | 'input' | 按调用可选覆盖已配置的模式。 |
store | boolean | 指示上一次运行是否存储了服务器状态。 |
force | boolean | 绕过 shouldTriggerCompaction 并立即压缩。 |
低延迟流式传输的手动压缩
Section titled “低延迟流式传输的手动压缩”压缩会清空并重写底层会话,因此 SDK 会等它完成后才结束流式运行。如果压缩开销较大,result.completed 可能会在最后一个输出 token 之后继续挂起数秒。对于低延迟流式传输或更快的轮次切换,请禁用自动压缩,并在轮次之间(或空闲时)自行调用 runCompaction。
import { Agent, MemorySession, OpenAIResponsesCompactionSession, run,} from '@openai/agents';
const agent = new Agent({ name: 'Support', instructions: 'Answer briefly and keep track of prior context.', model: 'gpt-5.4',});
// Disable auto-compaction to avoid delaying stream completion.const session = new OpenAIResponsesCompactionSession({ underlyingSession: new MemorySession(), shouldTriggerCompaction: () => false,});
const result = await run(agent, 'Share the latest ticket update.', { session, stream: true,});
// Wait for the streaming run to finish before compacting.await result.completed;
// Choose force based on your own thresholds or heuristics, between turns or during idle time.await session.runCompaction({ force: true });你可以随时调用 runCompaction({ force: true }),在归档或交接前缩减历史。使用 DEBUG=openai-agents:openai:compaction 启用调试日志,以追踪压缩决策。