UNPKG

@openai/agents-openai

Version:

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.

370 lines • 14.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenAIConversationsSession = void 0; exports.startOpenAIConversationsSession = startOpenAIConversationsSession; const openai_1 = __importDefault(require("openai")); const defaults_1 = require("../defaults.js"); const openaiResponsesModel_1 = require("../openaiResponsesModel.js"); const agents_core_1 = require("@openai/agents-core"); const openaiSessionApi_1 = require("./openaiSessionApi.js"); const MAX_ITEMS_PER_CREATE = 20; async function startOpenAIConversationsSession(client) { const resolvedClient = client ? client : resolveClient({}); const response = await resolvedClient.conversations.create({ items: [] }); return response.id; } class OpenAIConversationsSession { // Marks this session as backed by the Conversations API so Responses-only integrations can reject it. [openaiSessionApi_1.OPENAI_SESSION_API] = 'conversations'; #client; #conversationId; #sessionOperation = Promise.resolve(); constructor(options = {}) { this.#client = resolveClient(options); this.#conversationId = options.conversationId; } get sessionId() { return this.#conversationId; } async getSessionId() { return this.#runSessionOperation(() => this.#getSessionId()); } async getItems(limit) { return this.#runItemOperation((conversationId) => this.#getItems(conversationId, limit)); } async #getItems(conversationId, limit) { // Convert each API item into the Agent SDK's input shape. Some API payloads expand into multiple items. const toAgentItems = (item) => { if (item.type === 'message' && item.role === 'system') { const message = item; return [ { id: item.id, type: 'message', role: 'system', content: normalizeSystemMessageContent(message.content), }, ]; } if (item.type === 'message' && item.role === 'user') { const message = item; return [ { id: item.id, type: 'message', role: 'user', content: (message.content ?? []) .map((c) => { if (c.type === 'input_text') { return { type: 'input_text', text: c.text }; } else if (c.type === 'input_image') { if (c.image_url) { return { type: 'input_image', image: c.image_url, ...(c.detail !== undefined ? { detail: c.detail } : {}), }; } else if (c.file_id) { return { type: 'input_image', image: { id: c.file_id }, ...(c.detail !== undefined ? { detail: c.detail } : {}), }; } } else if (c.type === 'input_file') { if (c.file_data) { const fileItem = { type: 'input_file', file: c.file_data, }; if (c.filename) { fileItem.filename = c.filename; } return fileItem; } if (c.file_url) { const fileItem = { type: 'input_file', file: c.file_url, }; if (c.filename) { fileItem.filename = c.filename; } return fileItem; } else if (c.file_id) { const fileItem = { type: 'input_file', file: { id: c.file_id }, }; if (c.filename) { fileItem.filename = c.filename; } return fileItem; } } // Add more content types here when they're added return null; }) .filter((c) => c !== null), }, ]; } const outputItems = item .output; if (isResponseOutputItemArray(outputItems)) { return (0, openaiResponsesModel_1.convertToOutputItem)(outputItems); } return (0, openaiResponsesModel_1.convertToOutputItem)([item]); }; if (limit === undefined) { const items = []; const iterator = this.#client.conversations.items.list(conversationId, { order: 'asc', }); for await (const item of iterator) { items.push(...toAgentItems(item)); } return items; } if (limit <= 0) { return []; } const itemGroups = []; let total = 0; const iterator = this.#client.conversations.items.list(conversationId, { limit: Math.min(limit, 100), order: 'desc', }); for await (const item of iterator) { const group = toAgentItems(item); if (!group.length) { continue; } itemGroups.push(group); total += group.length; if (total >= limit) { break; } } // Iterate in reverse because the API returned items in descending order. const orderedItems = []; for (let index = itemGroups.length - 1; index >= 0; index -= 1) { orderedItems.push(...itemGroups[index]); } if (orderedItems.length > limit) { orderedItems.splice(0, orderedItems.length - limit); } return orderedItems; } prepareHistoryItemForModelInput(item) { return stripAssistantReplayMetadata(item); } prepareHistoryItemsForPersistenceComparison(items) { return normalizeItemsForConversationPersistence(items); } preserveReasoningItemIdsForPersistence() { return true; } async addItems(items) { await this.#writeItems(items, 'append'); } async replaceHistoryWithCompaction(items) { await this.#writeItems(items, 'replace'); } async #writeItems(items, operation) { if (!items.length) { return; } await this.#runItemOperation(async (conversationId) => { const sanitizedItems = normalizeItemsForConversationPersistence(items); if (!sanitizedItems.length) { return; } if (sanitizedItems.length <= MAX_ITEMS_PER_CREATE) { await this.#client.conversations.items.create(conversationId, { items: sanitizedItems, }); return; } // The compaction item and its retained suffix must become visible together. if (operation === 'replace') { throw new agents_core_1.UserError(`A conversation compaction replacement must fit within one request of ${MAX_ITEMS_PER_CREATE} items.`); } // A lost acknowledgment can leave an accepted batch in the conversation. for (let start = 0; start < sanitizedItems.length; start += MAX_ITEMS_PER_CREATE) { await this.#client.conversations.items.create(conversationId, { items: sanitizedItems.slice(start, start + MAX_ITEMS_PER_CREATE) }, { maxRetries: 0 }); } }); } async popItem() { return this.#runItemOperation(async (conversationId) => { const [latest] = await this.#getItems(conversationId, 1); if (!latest) { return undefined; } const itemId = latest.id; if (itemId) { await this.#client.conversations.items.delete(itemId, { conversation_id: conversationId, }); } return latest; }); } async clearSession() { await this.#runSessionOperation(async () => { if (!this.#conversationId) { return; } await this.#client.conversations.delete(this.#conversationId); this.#conversationId = undefined; }); } async #getSessionId() { if (!this.#conversationId) { this.#conversationId = await startOpenAIConversationsSession(this.#client); } return this.#conversationId; } async #runItemOperation(operation) { return this.#runSessionOperation(async () => operation(await this.#getSessionId())); } #runSessionOperation(operation) { const result = this.#sessionOperation.then(operation); this.#sessionOperation = result.then(() => undefined, () => undefined); return result; } } exports.OpenAIConversationsSession = OpenAIConversationsSession; // -------------------------------------------------------------- // Internals // -------------------------------------------------------------- function normalizeItemsForConversationPersistence(items) { const normalizedItems = stripProviderModelForConversationPersistence(items); return stripConversationPersistenceMetadata((0, openaiResponsesModel_1.getInputItems)(normalizedItems)); } function stripProviderModelForConversationPersistence(items) { return items.map((item) => { if (Array.isArray(item) || item === null || typeof item !== 'object') { return item; } // Conversations API rejects unknown top-level fields (e.g., model merged from providerData). const rest = { ...item }; const providerData = item.providerData; if (providerData && typeof providerData === 'object' && !Array.isArray(providerData)) { const pdObj = providerData; const { model: _model, ...pdRest } = pdObj; rest.providerData = Object.keys(pdRest).length > 0 ? pdRest : undefined; } return rest; }); } function normalizeSystemMessageContent(content) { if (typeof content === 'string') { return content; } if (!Array.isArray(content)) { return ''; } return content .map((item) => item.type === 'input_text' || item.type === 'text' ? item.text : null) .filter((item) => item !== null) .join('\n'); } const CONVERSATION_ITEM_TYPES_WITH_PRESERVED_IDS = new Set([ 'reasoning', 'program', 'program_output', ]); function stripConversationPersistenceMetadata(items) { return items.flatMap((item) => { if (Array.isArray(item) || item === null || typeof item !== 'object') { return [item]; } const record = item; if (isUnpersistableReasoningItem(record)) { return []; } const { providerData: _providerData, provider_data: _provider_data, ...rest } = record; if (typeof rest.type !== 'string' || !CONVERSATION_ITEM_TYPES_WITH_PRESERVED_IDS.has(rest.type)) { delete rest.id; } return [rest]; }); } function isUnpersistableReasoningItem(item) { return (item.type === 'reasoning' && typeof item.id !== 'string' && typeof item.encrypted_content !== 'string'); } function stripAssistantReplayMetadata(item) { if (Array.isArray(item) || item === null || typeof item !== 'object') { return item; } const record = item; if (record.type !== 'message' || record.role !== 'assistant') { return item; } const { id: _id, providerData, provider_data: _provider_data, ...rest } = record; if (typeof rest.phase === 'undefined' && providerData && typeof providerData === 'object' && !Array.isArray(providerData) && typeof providerData.phase !== 'undefined') { rest.phase = providerData.phase; } return rest; } const INPUT_CONTENT_TYPES = new Set([ 'input_text', 'input_image', 'input_file', 'input_audio', ]); function isObject(value) { return typeof value === 'object' && value !== null; } // Treats a value as ResponseOutputItem[] only when each entry resembles an output item rather than raw input content. function isResponseOutputItemArray(value) { if (!Array.isArray(value) || value.length === 0) { return false; } return value.every((entry) => { if (!isObject(entry)) { return false; } const type = entry.type; if (typeof type !== 'string') { return false; } if (INPUT_CONTENT_TYPES.has(type)) { return false; } // Fallback: pre-emptively exclude future input_* variants so they never masquerade as response outputs. return !type.startsWith('input_'); }); } function resolveClient(options) { if (options.client) { return options.client; } return ((0, defaults_1.getDefaultOpenAIClient)() ?? new openai_1.default({ apiKey: options.apiKey ?? (0, defaults_1.getDefaultOpenAIKey)(), baseURL: options.baseURL, organization: options.organization, project: options.project, })); } //# sourceMappingURL=openaiConversationsSession.js.map