@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
139 lines • 5.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSimpleToolResultTypeForCall = getSimpleToolResultTypeForCall;
exports.isSimpleToolResultType = isSimpleToolResultType;
exports.getToolResultCorrelationForCall = getToolResultCorrelationForCall;
exports.getToolResultCorrelationForResult = getToolResultCorrelationForResult;
exports.getToolResultCorrelationKey = getToolResultCorrelationKey;
exports.getUnresolvedToolResultCorrelationsForResponse = getUnresolvedToolResultCorrelationsForResponse;
const tooling_1 = require("../tooling.js");
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;
}
function getSimpleToolResultTypeForCall(type) {
return typeof type === 'string'
? SIMPLE_TOOL_RESULT_TYPE_BY_CALL_TYPE[type]
: undefined;
}
function isSimpleToolResultType(type) {
return typeof type === 'string' && SIMPLE_TOOL_RESULT_TYPES.has(type);
}
function getToolResultCorrelationForCall(item) {
if (!item || typeof item !== 'object') {
return undefined;
}
const type = item.type;
if (type === 'tool_search_call') {
if ((0, tooling_1.getToolSearchExecution)(item) === 'server') {
return undefined;
}
return createCorrelation(TOOL_SEARCH_OUTPUT, (0, tooling_1.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);
}
function getToolResultCorrelationForResult(item) {
if (!item || typeof item !== 'object') {
return undefined;
}
const type = item.type;
if (type === TOOL_SEARCH_OUTPUT) {
if ((0, tooling_1.getToolSearchExecution)(item) === 'server') {
return undefined;
}
return createCorrelation(TOOL_SEARCH_OUTPUT, (0, tooling_1.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);
}
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);
}
}
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 ((0, tooling_1.getToolSearchExecution)(item) === 'server') {
continue;
}
const providerCallId = (0, tooling_1.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.js.map