UNPKG

@openai/agents-core

Version:

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

89 lines • 3.82 kB
import { UserError } from "../errors.mjs"; import logger, { logToolActionWarning } from "../logger.mjs"; import { rehydrateProcessedResponseTools } from "../runState.mjs"; import { isSandboxAgent } from "../sandbox/runtime/agentKeys.mjs"; import { processedResponseRequiresExecutionToolRehydration } from "../sandbox/runtime/toolRehydration.mjs"; import { disposeResolvedComputers } from "../tool.mjs"; import { resetCurrentSpan } from "../tracing/context.mjs"; import { prepareAgentArtifacts } from "./modelPreparation.mjs"; export async function prepareSandboxInterruptedTurnResume(args) { const { startingAgent, state, sandboxRuntime, runConfigModel, toolNameCollisionPolicy, tracingParent, } = args; logger.debug('Continuing from interruption'); if (!state._lastTurnResponse || !state._lastProcessedResponse) { throw new UserError('No model response found in previous state', state); } const requiresExecutionToolRehydration = processedResponseRequiresExecutionToolRehydration(state._lastProcessedResponse); if (!isSandboxAgent(state._currentAgent) && !requiresExecutionToolRehydration) { return; } const resumedPreservedSessions = await sandboxRuntime.adoptPreservedOwnedSessions(tracingParent); if (resumedPreservedSessions || requiresExecutionToolRehydration) { await rehydrateInterruptedTurnExecutionTools({ startingAgent, state, sandboxRuntime, runConfigModel, toolNameCollisionPolicy, force: resumedPreservedSessions, tracingParent, }); } } export async function finalizeSandboxRuntime(args) { const { state, sandboxRuntime, preserveSessionsForInterruption, finishAgentSpanForInterruption = false, runError, groupId, memoryContext, runAgent, tracingParent, } = args; if (!preserveSessionsForInterruption) { try { await disposeResolvedComputers({ runContext: state._context }); } catch (error) { logToolActionWarning(logger, '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 { resetCurrentSpan(); } } } } export async function rehydrateInterruptedTurnExecutionTools(args) { const { startingAgent, state, sandboxRuntime, runConfigModel, toolNameCollisionPolicy, force, tracingParent, } = args; if (!force && !processedResponseRequiresExecutionToolRehydration(state._lastProcessedResponse)) { return; } const preparedSandboxAgent = await sandboxRuntime.prepareAgent({ currentAgent: state._currentAgent, turnInput: [], runConfigModel, tracingParent, }); const artifacts = await prepareAgentArtifacts(state, preparedSandboxAgent.executionAgent, toolNameCollisionPolicy); await rehydrateProcessedResponseTools(startingAgent, state, artifacts.tools); } export function isSandboxRuntimeAgent(agent) { return isSandboxAgent(agent); } //# sourceMappingURL=sandbox.mjs.map