UNPKG

@openai/agents-core

Version:

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

205 lines • 8.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processedResponseRequiresExecutionToolRehydration = processedResponseRequiresExecutionToolRehydration; exports.getSerializedFunctionToolPlaceholder = getSerializedFunctionToolPlaceholder; exports.getSerializedComputerToolPlaceholder = getSerializedComputerToolPlaceholder; exports.getSerializedShellToolPlaceholder = getSerializedShellToolPlaceholder; exports.getSerializedApplyPatchToolPlaceholder = getSerializedApplyPatchToolPlaceholder; const errors_1 = require("../../errors.js"); const toolIdentity_1 = require("../../toolIdentity.js"); const agentKeys_1 = require("./agentKeys.js"); const SERIALIZED_EXECUTION_TOOL_PLACEHOLDER = Symbol('serializedExecutionToolPlaceholder'); function markSerializedExecutionToolPlaceholder(tool) { Object.defineProperty(tool, SERIALIZED_EXECUTION_TOOL_PLACEHOLDER, { value: true, enumerable: false, }); return tool; } function isSerializedExecutionToolPlaceholder(tool) { return Boolean(tool && typeof tool === 'object' && tool[SERIALIZED_EXECUTION_TOOL_PLACEHOLDER] === true); } function processedResponseRequiresExecutionToolRehydration(processedResponse) { if (!processedResponse) { return false; } return ((processedResponse.functions ?? []).some((item) => isSerializedExecutionToolPlaceholder(item.tool)) || (processedResponse.computerActions ?? []).some((item) => isSerializedExecutionToolPlaceholder(item.computer)) || (processedResponse.shellActions ?? []).some((item) => isSerializedExecutionToolPlaceholder(item.shell)) || (processedResponse.applyPatchActions ?? []).some((item) => isSerializedExecutionToolPlaceholder(item.applyPatch))); } function hasConfiguredFunctionTool(tools, toolCall) { const callKey = (0, toolIdentity_1.getFunctionToolLookupKeyForCall)(toolCall); return Boolean(callKey && tools.some((tool) => tool.type === 'function' && (0, toolIdentity_1.getFunctionToolLookupKeyForTool)(tool) === callKey)); } function hasConfiguredTool(tools, type, name) { return tools.some((tool) => tool.type === type && 'name' in tool && typeof tool.name === 'string' && tool.name === name); } function canUseSerializedExecutionToolPlaceholder(args) { const { agent, allowSerializedExecutionToolPlaceholder, configuredToolExists, serializedTool, type, } = args; return (allowSerializedExecutionToolPlaceholder && (0, agentKeys_1.isSandboxAgent)(agent) && !configuredToolExists && Boolean(serializedTool) && typeof serializedTool === 'object' && serializedTool.type === type); } function getSerializedToolName(serializedTool, fallbackName) { const name = serializedTool && typeof serializedTool === 'object' && typeof serializedTool.name === 'string' ? serializedTool.name : fallbackName; return name; } function createSerializedExecutionToolError(type, name) { return new errors_1.UserError(`${type} tool ${name} was restored from serialized execution-time metadata without an executable handler. Resume the RunState through Runner.run() with the matching execution-time tool configuration before executing it.`); } function getSerializedFunctionToolPlaceholder(args) { const { agent, baseAgentTools, serializedTool, toolCall, toolIdentity, allowSerializedExecutionToolPlaceholder, } = args; if (!canUseSerializedExecutionToolPlaceholder({ agent, allowSerializedExecutionToolPlaceholder, configuredToolExists: hasConfiguredFunctionTool(baseAgentTools, toolCall), serializedTool, type: 'function', })) { return undefined; } const serialized = serializedTool; const name = getSerializedToolName(serializedTool, toolCall.name); const placeholder = { ...serialized, type: 'function', name, description: typeof serialized.description === 'string' ? serialized.description : `Serialized execution-time tool ${name}.`, parameters: serialized.parameters && typeof serialized.parameters === 'object' && !Array.isArray(serialized.parameters) ? serialized.parameters : { type: 'object', properties: {}, required: [], additionalProperties: false, }, strict: typeof serialized.strict === 'boolean' ? serialized.strict : true, deferLoading: typeof serialized.deferLoading === 'boolean' ? serialized.deferLoading : false, invoke: async () => { throw createSerializedExecutionToolError('Function', toolIdentity); }, needsApproval: async () => false, isEnabled: async () => true, }; if (typeof toolCall.namespace === 'string' && toolCall.namespace.length > 0 && toolCall.namespace !== name) { Object.assign(placeholder, { [toolIdentity_1.FUNCTION_TOOL_NAMESPACE]: toolCall.namespace, [toolIdentity_1.FUNCTION_TOOL_NAMESPACE_DESCRIPTION]: toolCall.namespace, }); } return markSerializedExecutionToolPlaceholder(placeholder); } function getSerializedComputerToolPlaceholder(args) { const { agent, baseAgentTools, serializedTool, toolName, allowSerializedExecutionToolPlaceholder, } = args; if (!canUseSerializedExecutionToolPlaceholder({ agent, allowSerializedExecutionToolPlaceholder, configuredToolExists: hasConfiguredTool(baseAgentTools, 'computer', toolName), serializedTool, type: 'computer', })) { return undefined; } const serialized = serializedTool; const name = getSerializedToolName(serializedTool, toolName); const throwComputerError = async () => { throw createSerializedExecutionToolError('Computer', name); }; return markSerializedExecutionToolPlaceholder({ ...serialized, type: 'computer', name, computer: { environment: 'browser', dimensions: [1, 1], screenshot: throwComputerError, click: throwComputerError, doubleClick: throwComputerError, scroll: throwComputerError, type: throwComputerError, wait: throwComputerError, move: throwComputerError, keypress: throwComputerError, drag: throwComputerError, }, needsApproval: async () => false, }); } function getSerializedShellToolPlaceholder(args) { const { agent, baseAgentTools, serializedTool, toolName, allowSerializedExecutionToolPlaceholder, } = args; if (!canUseSerializedExecutionToolPlaceholder({ agent, allowSerializedExecutionToolPlaceholder, configuredToolExists: hasConfiguredTool(baseAgentTools, 'shell', toolName), serializedTool, type: 'shell', })) { return undefined; } const serialized = serializedTool; const name = getSerializedToolName(serializedTool, toolName); return markSerializedExecutionToolPlaceholder({ ...serialized, type: 'shell', name, environment: { type: 'local' }, shell: { run: async () => { throw createSerializedExecutionToolError('Shell', name); }, }, needsApproval: async () => false, }); } function getSerializedApplyPatchToolPlaceholder(args) { const { agent, baseAgentTools, serializedTool, toolName, allowSerializedExecutionToolPlaceholder, } = args; if (!canUseSerializedExecutionToolPlaceholder({ agent, allowSerializedExecutionToolPlaceholder, configuredToolExists: hasConfiguredTool(baseAgentTools, 'apply_patch', toolName), serializedTool, type: 'apply_patch', })) { return undefined; } const serialized = serializedTool; const name = getSerializedToolName(serializedTool, toolName); const throwEditorError = async () => { throw createSerializedExecutionToolError('Apply patch', name); }; return markSerializedExecutionToolPlaceholder({ ...serialized, type: 'apply_patch', name, editor: { createFile: throwEditorError, updateFile: throwEditorError, deleteFile: throwEditorError, }, needsApproval: async () => false, }); } //# sourceMappingURL=toolRehydration.js.map