import { protocol } from '@openai/agents'; import type { AgentInputItem, Session } from '@openai/agents'; import * as fs from 'node:fs/promises'; import * as path from 'node:path'; import { randomUUID } from 'node:crypto'; export type FileSessionOptions = { dir?: string; sessionId?: string; }; export class FileSession implements Session { #dir: string; #sessionId?: string; constructor(options: FileSessionOptions = {}) { this.#dir = options.dir ?? path.resolve(process.cwd(), '.agents-sessions'); this.#sessionId = options.sessionId; } async getSessionId(): Promise { if (!this.#sessionId) { this.#sessionId = randomUUID().replace(/-/g, '').slice(0, 24); } await this.#ensureDir(); const file = this.#filePath(this.#sessionId); try { await fs.access(file); } catch { await fs.writeFile(file, '[]', 'utf8'); } return this.#sessionId; } async getItems(limit?: number): Promise { const sessionId = await this.getSessionId(); const items = (await this.#readRawItems(sessionId)).flatMap((raw) => { const item = coerceAgentItem(raw); return item ? [item] : []; }); if (typeof limit === 'number' && limit >= 0) { return items.slice(-limit); } return items; } async addItems(items: AgentInputItem[]): Promise { if (!items.length) { return; } const sessionId = await this.getSessionId(); const current = await this.#readRawItems(sessionId); // Store a structured clone so we don't accidentally persist references that can mutate. const serialized = items.flatMap((item) => { const parsed = coerceAgentItem(item); return parsed ? [JSON.parse(JSON.stringify(parsed))] : []; }); const next = current.concat(serialized); await this.#writeItems(sessionId, next); } async popItem(): Promise { const sessionId = await this.getSessionId(); const items = await this.#readRawItems(sessionId); while (items.length > 0) { const raw = items.pop(); const item = coerceAgentItem(raw); if (item) { await this.#writeItems(sessionId, items); return item; } } await this.#writeItems(sessionId, items); return undefined; } async clearSession(): Promise { if (!this.#sessionId) { return; } const file = this.#filePath(this.#sessionId); try { await fs.unlink(file); } catch { // ignore missing file } this.#sessionId = undefined; } async #ensureDir(): Promise { await fs.mkdir(this.#dir, { recursive: true }); } #filePath(sessionId: string): string { return path.join(this.#dir, `${sessionId}.json`); } async #readRawItems(sessionId: string): Promise { const file = this.#filePath(sessionId); try { const data = await fs.readFile(file, 'utf8'); const parsed = JSON.parse(data); return Array.isArray(parsed) ? parsed : []; } catch (err: any) { if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) { return []; } throw err; } } async #writeItems(sessionId: string, items: unknown[]): Promise { await this.#ensureDir(); const file = this.#filePath(sessionId); await fs.writeFile(file, JSON.stringify(items, null, 2), 'utf8'); } } function coerceAgentItem(raw: unknown): AgentInputItem | undefined { const parsed = protocol.ModelItem.safeParse(raw); if (!parsed.success) { return undefined; } return parsed.data as AgentInputItem; } export type { AgentInputItem } from '@openai/agents';