@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
222 lines • 8.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.COMPUTER_FALLBACK_SCREENSHOT_DATA_URL = void 0;
exports.buildFunctionAbortResult = buildFunctionAbortResult;
exports.buildComputerAbortResult = buildComputerAbortResult;
exports.buildShellAbortResult = buildShellAbortResult;
exports.buildApplyPatchAbortResult = buildApplyPatchAbortResult;
exports.createStreamAbortReconciliationState = createStreamAbortReconciliationState;
exports.recordStreamEventForAbortReconciliation = recordStreamEventForAbortReconciliation;
exports.buildAbortReconciliationInput = buildAbortReconciliationInput;
exports.getAbortReconciliationPreviousResponseId = getAbortReconciliationPreviousResponseId;
exports.shouldReconcileStreamAbort = shouldReconcileStreamAbort;
exports.markAbortReconciliationComplete = markAbortReconciliationComplete;
const _shims_1 = require("@openai/agents-core/_shims");
// 1x1 transparent PNG data URL used when a computer result requires an image
// but cancellation prevents the SDK from capturing the current screen.
exports.COMPUTER_FALLBACK_SCREENSHOT_DATA_URL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg==';
function buildFunctionAbortResult(toolCall) {
return {
type: 'function_call_result',
name: toolCall.name,
...(typeof toolCall.namespace === 'string'
? { namespace: toolCall.namespace }
: {}),
callId: toolCall.callId,
status: 'incomplete',
output: { type: 'text', text: 'aborted' },
...(toolCall.caller ? { caller: toolCall.caller } : {}),
};
}
function buildComputerAbortResult(toolCall) {
return {
type: 'computer_call_result',
callId: toolCall.callId,
output: {
type: 'computer_screenshot',
data: exports.COMPUTER_FALLBACK_SCREENSHOT_DATA_URL,
},
providerData: { status: 'incomplete' },
};
}
function buildShellAbortResult(toolCall) {
return {
type: 'shell_call_output',
callId: toolCall.callId,
status: 'incomplete',
output: [
{
stdout: '',
stderr: 'aborted',
outcome: { type: 'timeout' },
},
],
...(toolCall.caller ? { caller: toolCall.caller } : {}),
};
}
function buildApplyPatchAbortResult(toolCall) {
return {
type: 'apply_patch_call_output',
callId: toolCall.callId,
status: 'failed',
output: 'aborted',
...(toolCall.caller ? { caller: toolCall.caller } : {}),
};
}
function createStreamAbortReconciliationState() {
return {
pendingFunctionCalls: new Map(),
pendingShellCalls: new Map(),
pendingApplyPatchCalls: new Map(),
pendingProgramCalls: new Map(),
};
}
function recordStreamEventForAbortReconciliation(state, event) {
if (event.type === 'response_done') {
state.pendingFunctionCalls.clear();
state.pendingShellCalls.clear();
state.pendingApplyPatchCalls.clear();
state.pendingProgramCalls.clear();
state.responseId = event.response.id;
return;
}
if (event.type !== 'model' || !isRecord(event.event)) {
return;
}
const rawEvent = event.event;
if (rawEvent.type === 'response.created' &&
isRecord(rawEvent.response) &&
typeof rawEvent.response.id === 'string') {
state.responseId = rawEvent.response.id;
return;
}
if (rawEvent.type === 'response.output_item.added' &&
isRecord(rawEvent.item) &&
rawEvent.item.type === 'program_output' &&
typeof rawEvent.item.call_id === 'string' &&
typeof rawEvent.item.id === 'string') {
state.pendingProgramCalls.set(rawEvent.item.call_id, rawEvent.item.id);
return;
}
if (rawEvent.type !== 'response.output_item.done' ||
!isRecord(rawEvent.item)) {
return;
}
const item = rawEvent.item;
const caller = getToolCaller(item);
if (item.type === 'program' && typeof item.call_id === 'string') {
if (!state.pendingProgramCalls.has(item.call_id)) {
state.pendingProgramCalls.set(item.call_id, generateProgramOutputId());
}
return;
}
if (item.type === 'program_output' && typeof item.call_id === 'string') {
state.pendingProgramCalls.delete(item.call_id);
return;
}
if (item.type === 'function_call' && typeof item.call_id === 'string') {
state.pendingFunctionCalls.set(item.call_id, {
callId: item.call_id,
name: typeof item.name === 'string' ? item.name : item.call_id,
...(typeof item.namespace === 'string'
? { namespace: item.namespace }
: {}),
...(caller ? { caller } : {}),
});
return;
}
if (item.type === 'shell_call' &&
typeof item.call_id === 'string' &&
isClientOwnedShellCall(item)) {
state.pendingShellCalls.set(item.call_id, {
callId: item.call_id,
...(caller ? { caller } : {}),
});
return;
}
if (item.type === 'apply_patch_call' && typeof item.call_id === 'string') {
state.pendingApplyPatchCalls.set(item.call_id, {
callId: item.call_id,
...(caller ? { caller } : {}),
});
return;
}
if (item.type === 'function_call_output' &&
typeof item.call_id === 'string') {
state.pendingFunctionCalls.delete(item.call_id);
return;
}
if (item.type === 'shell_call_output' && typeof item.call_id === 'string') {
state.pendingShellCalls.delete(item.call_id);
return;
}
if (item.type === 'apply_patch_call_output' &&
typeof item.call_id === 'string') {
state.pendingApplyPatchCalls.delete(item.call_id);
}
}
function buildAbortReconciliationInput(state) {
const functionOutputs = Array.from(state.pendingFunctionCalls.values(), buildFunctionAbortResult);
const shellOutputs = Array.from(state.pendingShellCalls.values(), buildShellAbortResult);
const applyPatchOutputs = Array.from(state.pendingApplyPatchCalls.values(), buildApplyPatchAbortResult);
const programOutputs = Array.from(state.pendingProgramCalls, ([callId, id]) => ({
type: 'program_output',
id,
callId,
status: 'incomplete',
output: 'aborted',
}));
return [
...functionOutputs,
...shellOutputs,
...applyPatchOutputs,
...programOutputs,
];
}
function getAbortReconciliationPreviousResponseId(state, request) {
if (request.conversationId) {
return request.previousResponseId;
}
return state.responseId ?? request.previousResponseId;
}
function shouldReconcileStreamAbort(state) {
return (state.pendingFunctionCalls.size > 0 ||
state.pendingProgramCalls.size > 0 ||
state.pendingShellCalls.size > 0 ||
state.pendingApplyPatchCalls.size > 0);
}
function markAbortReconciliationComplete(state, response) {
state.pendingFunctionCalls.clear();
state.pendingShellCalls.clear();
state.pendingApplyPatchCalls.clear();
state.pendingProgramCalls.clear();
if (response?.responseId) {
state.responseId = response.responseId;
}
}
function isRecord(value) {
return typeof value === 'object' && value !== null;
}
function getToolCaller(item) {
if (!isRecord(item.caller)) {
return undefined;
}
if (item.caller.type === 'direct') {
return { type: 'direct' };
}
if (item.caller.type === 'program' &&
typeof item.caller.caller_id === 'string') {
return {
type: 'program',
callerId: item.caller.caller_id,
};
}
return undefined;
}
function isClientOwnedShellCall(item) {
return !isRecord(item.environment) || item.environment.type === 'local';
}
function generateProgramOutputId() {
return `prog_out_${(0, _shims_1.randomUUID)().replace(/-/g, '')}`;
}
//# sourceMappingURL=streamReconciliation.js.map