UNPKG

@openai/agents-realtime

Version:

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows. This package contains the logic for building realtime voice agents on the server or in the browser.

97 lines • 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BACKGROUND_RESULT_SYMBOL = void 0; exports.backgroundResult = backgroundResult; exports.isBackgroundResult = isBackgroundResult; exports.isValidRealtimeTool = isValidRealtimeTool; exports.validateRealtimeToolNames = validateRealtimeToolNames; exports.toRealtimeToolDefinition = toRealtimeToolDefinition; const agents_core_1 = require("@openai/agents-core"); const utils_1 = require("@openai/agents-core/utils"); exports.BACKGROUND_RESULT_SYMBOL = Symbol('backgroundResult'); function backgroundResult(content) { return { [exports.BACKGROUND_RESULT_SYMBOL]: true, content, }; } function isBackgroundResult(result) { return (typeof result === 'object' && result !== null && exports.BACKGROUND_RESULT_SYMBOL in result); } function isValidRealtimeTool(tool) { return (tool.type === 'function' || (tool.type === 'hosted_tool' && tool.name === 'hosted_mcp')); } function validateRealtimeToolNames(tools, handoffs) { const sourcesByName = new Map(); for (const tool of tools) { const sources = sourcesByName.get(tool.name) ?? []; sources.push('function tool'); sourcesByName.set(tool.name, sources); } for (const handoff of handoffs) { const sources = sourcesByName.get(handoff.toolName) ?? []; sources.push('handoff'); sourcesByName.set(handoff.toolName, sources); } const duplicateDescriptions = [...sourcesByName.entries()] .filter(([, sources]) => sources.length > 1) .sort(([left], [right]) => left.localeCompare(right)) .map(([name, sources]) => `'${name}' (${formatRealtimeToolSources(sources)})`); if (duplicateDescriptions.length === 0) { return; } const label = duplicateDescriptions.length === 1 ? 'name' : 'names'; throw new agents_core_1.UserError(`Duplicate Realtime tool ${label} found: ${duplicateDescriptions.join(', ')}. ` + 'Realtime function tool and handoff names must be unique. Rename one of them before starting the session.'); } function formatRealtimeToolSources(sources) { const counts = new Map(); for (const source of sources) { counts.set(source, (counts.get(source) ?? 0) + 1); } const descriptions = [...counts.entries()].map(([source, count]) => count === 1 ? source : `${count} ${source}s`); if (descriptions.length === 1) { return descriptions[0]; } if (descriptions.length === 2) { return `${descriptions[0]} and ${descriptions[1]}`; } return `${descriptions.slice(0, -1).join(', ')}, and ${descriptions.at(-1)}`; } function toRealtimeToolDefinition(tool) { if (tool.type === 'function') { if (tool.allowedCallers?.includes('programmatic')) { throw new agents_core_1.UserError(`Realtime does not support function tool '${tool.name}' with allowedCallers including 'programmatic'. Programmatic Tool Calling is only supported with the Responses API.`); } if (typeof tool.outputSchema !== 'undefined') { throw new agents_core_1.UserError(`Realtime does not support function tool '${tool.name}' with outputSchema. Function tool outputSchema is only supported with the Responses API.`); } return tool; } if (tool.type === 'hosted_tool' && tool.name === 'hosted_mcp') { if (tool.providerData.allowed_callers?.includes('programmatic')) { throw new agents_core_1.UserError(`Realtime does not support hosted MCP tool '${tool.providerData.server_label}' with allowedCallers including 'programmatic'. Programmatic Tool Calling is only supported with the Responses API.`); } const serverUrl = tool.providerData.server_url && tool.providerData.server_url.length > 0 ? tool.providerData.server_url : undefined; const requireApproval = typeof tool.providerData.require_approval === 'undefined' ? undefined : (0, utils_1.normalizeHostedMcpRequireApproval)(tool.providerData.require_approval); return { type: 'mcp', server_label: tool.providerData.server_label, server_url: serverUrl, headers: tool.providerData.headers, allowed_tools: tool.providerData.allowed_tools, ...(typeof requireApproval === 'undefined' ? {} : { require_approval: requireApproval }), }; } throw new agents_core_1.UserError(`Invalid tool type: ${tool}`); } //# sourceMappingURL=tool.js.map