UNPKG

@openai/agents-core

Version:

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

128 lines • 5.61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareSandboxInterruptedTurnResume = prepareSandboxInterruptedTurnResume; exports.finalizeSandboxRuntime = finalizeSandboxRuntime; exports.rehydrateInterruptedTurnExecutionTools = rehydrateInterruptedTurnExecutionTools; exports.isSandboxRuntimeAgent = isSandboxRuntimeAgent; const errors_1 = require("../errors.js"); const logger_1 = __importStar(require("../logger.js")); const runState_1 = require("../runState.js"); const agentKeys_1 = require("../sandbox/runtime/agentKeys.js"); const toolRehydration_1 = require("../sandbox/runtime/toolRehydration.js"); const tool_1 = require("../tool.js"); const context_1 = require("../tracing/context.js"); const modelPreparation_1 = require("./modelPreparation.js"); async function prepareSandboxInterruptedTurnResume(args) { const { startingAgent, state, sandboxRuntime, runConfigModel, toolNameCollisionPolicy, tracingParent, } = args; logger_1.default.debug('Continuing from interruption'); if (!state._lastTurnResponse || !state._lastProcessedResponse) { throw new errors_1.UserError('No model response found in previous state', state); } const requiresExecutionToolRehydration = (0, toolRehydration_1.processedResponseRequiresExecutionToolRehydration)(state._lastProcessedResponse); if (!(0, agentKeys_1.isSandboxAgent)(state._currentAgent) && !requiresExecutionToolRehydration) { return; } const resumedPreservedSessions = await sandboxRuntime.adoptPreservedOwnedSessions(tracingParent); if (resumedPreservedSessions || requiresExecutionToolRehydration) { await rehydrateInterruptedTurnExecutionTools({ startingAgent, state, sandboxRuntime, runConfigModel, toolNameCollisionPolicy, force: resumedPreservedSessions, tracingParent, }); } } async function finalizeSandboxRuntime(args) { const { state, sandboxRuntime, preserveSessionsForInterruption, finishAgentSpanForInterruption = false, runError, groupId, memoryContext, runAgent, tracingParent, } = args; if (!preserveSessionsForInterruption) { try { await (0, tool_1.disposeResolvedComputers)({ runContext: state._context }); } catch (error) { (0, logger_1.logToolActionWarning)(logger_1.default, 'Failed to dispose computers after run:', error); } } await sandboxRuntime.enqueueMemoryGeneration(state, { exception: runError, groupId, inputOverride: memoryContext?.inputOverride?.(), sdkSessionId: memoryContext?.sdkSessionId, runAgent: async (agent, input, runOptions) => await runAgent(agent, input, runOptions), tracingParent, }); try { await sandboxRuntime.cleanup(state, { preserveOwnedSessions: preserveSessionsForInterruption, tracingParent, }); } finally { if (state._currentAgentSpan) { try { if (!preserveSessionsForInterruption || finishAgentSpanForInterruption) { state._currentAgentSpan.end(); } } finally { (0, context_1.resetCurrentSpan)(); } } } } async function rehydrateInterruptedTurnExecutionTools(args) { const { startingAgent, state, sandboxRuntime, runConfigModel, toolNameCollisionPolicy, force, tracingParent, } = args; if (!force && !(0, toolRehydration_1.processedResponseRequiresExecutionToolRehydration)(state._lastProcessedResponse)) { return; } const preparedSandboxAgent = await sandboxRuntime.prepareAgent({ currentAgent: state._currentAgent, turnInput: [], runConfigModel, tracingParent, }); const artifacts = await (0, modelPreparation_1.prepareAgentArtifacts)(state, preparedSandboxAgent.executionAgent, toolNameCollisionPolicy); await (0, runState_1.rehydrateProcessedResponseTools)(startingAgent, state, artifacts.tools); } function isSandboxRuntimeAgent(agent) { return (0, agentKeys_1.isSandboxAgent)(agent); } //# sourceMappingURL=sandbox.js.map