UNPKG

@openai/agents-core

Version:

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

131 lines • 4.85 kB
import { getToolSearchExecution, getToolSearchMatchKey, getToolSearchProviderCallId, } from "../tooling.mjs"; const SIMPLE_TOOL_RESULT_TYPE_BY_CALL_TYPE = { program: 'program_output', function_call: 'function_call_result', computer_call: 'computer_call_result', shell_call: 'shell_call_output', apply_patch_call: 'apply_patch_call_output', }; const SIMPLE_TOOL_RESULT_TYPES = new Set(Object.values(SIMPLE_TOOL_RESULT_TYPE_BY_CALL_TYPE)); const TOOL_SEARCH_OUTPUT = 'tool_search_output'; const HOSTED_MCP_APPROVAL_REQUEST = 'mcp_approval_request'; const HOSTED_MCP_APPROVAL_RESPONSE = 'mcp_approval_response'; function createCorrelation(resultType, id) { return typeof id === 'string' ? { resultType, id } : undefined; } export function getSimpleToolResultTypeForCall(type) { return typeof type === 'string' ? SIMPLE_TOOL_RESULT_TYPE_BY_CALL_TYPE[type] : undefined; } export function isSimpleToolResultType(type) { return typeof type === 'string' && SIMPLE_TOOL_RESULT_TYPES.has(type); } export function getToolResultCorrelationForCall(item) { if (!item || typeof item !== 'object') { return undefined; } const type = item.type; if (type === 'tool_search_call') { if (getToolSearchExecution(item) === 'server') { return undefined; } return createCorrelation(TOOL_SEARCH_OUTPUT, getToolSearchMatchKey(item)); } const callId = item.callId; const resultType = getSimpleToolResultTypeForCall(type); if (resultType) { return createCorrelation(resultType, callId); } if (type !== 'hosted_tool_call') { return undefined; } const hostedItem = item; const providerData = hostedItem.providerData; if (!providerData || typeof providerData !== 'object') { return undefined; } const approvalRequest = providerData; if (hostedItem.name !== HOSTED_MCP_APPROVAL_REQUEST && approvalRequest.type !== HOSTED_MCP_APPROVAL_REQUEST) { return undefined; } return createCorrelation(HOSTED_MCP_APPROVAL_RESPONSE, approvalRequest.id ?? hostedItem.id); } export function getToolResultCorrelationForResult(item) { if (!item || typeof item !== 'object') { return undefined; } const type = item.type; if (type === TOOL_SEARCH_OUTPUT) { if (getToolSearchExecution(item) === 'server') { return undefined; } return createCorrelation(TOOL_SEARCH_OUTPUT, getToolSearchProviderCallId(item)); } const callId = item.callId; if (isSimpleToolResultType(type)) { return createCorrelation(type, callId); } if (type !== 'hosted_tool_call') { return undefined; } const hostedItem = item; if (hostedItem.name !== HOSTED_MCP_APPROVAL_RESPONSE) { return undefined; } const providerData = hostedItem.providerData; if (!providerData || typeof providerData !== 'object') { return undefined; } return createCorrelation(HOSTED_MCP_APPROVAL_RESPONSE, providerData.approval_request_id); } export function getToolResultCorrelationKey(correlation) { return JSON.stringify([correlation.resultType, correlation.id]); } function removePendingCorrelation(pending, correlation) { const key = getToolResultCorrelationKey(correlation); const index = pending.findIndex((candidate) => getToolResultCorrelationKey(candidate) === key); if (index >= 0) { pending.splice(index, 1); } } export function getUnresolvedToolResultCorrelationsForResponse(items) { const calls = []; const results = []; const pendingToolSearchCalls = []; for (const item of items) { const call = getToolResultCorrelationForCall(item); if (call) { calls.push(call); if (item.type === 'tool_search_call') { pendingToolSearchCalls.push(call); } } if (item.type === TOOL_SEARCH_OUTPUT) { if (getToolSearchExecution(item) === 'server') { continue; } const providerCallId = getToolSearchProviderCallId(item); const result = providerCallId ? createCorrelation(TOOL_SEARCH_OUTPUT, providerCallId) : pendingToolSearchCalls.shift(); if (result) { results.push(result); if (providerCallId) { removePendingCorrelation(pendingToolSearchCalls, result); } } continue; } const result = getToolResultCorrelationForResult(item); if (result) { results.push(result); } } for (const result of results) { removePendingCorrelation(calls, result); } return calls; } //# sourceMappingURL=toolResultCorrelation.mjs.map