@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
198 lines • 8.12 kB
JavaScript
import { UserError } from "../../errors.mjs";
import { FUNCTION_TOOL_NAMESPACE, FUNCTION_TOOL_NAMESPACE_DESCRIPTION, getFunctionToolLookupKeyForCall, getFunctionToolLookupKeyForTool, } from "../../toolIdentity.mjs";
import { isSandboxAgent } from "./agentKeys.mjs";
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);
}
export 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 = getFunctionToolLookupKeyForCall(toolCall);
return Boolean(callKey &&
tools.some((tool) => tool.type === 'function' &&
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 &&
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 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.`);
}
export 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, {
[FUNCTION_TOOL_NAMESPACE]: toolCall.namespace,
[FUNCTION_TOOL_NAMESPACE_DESCRIPTION]: toolCall.namespace,
});
}
return markSerializedExecutionToolPlaceholder(placeholder);
}
export 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,
});
}
export 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,
});
}
export 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.mjs.map