@openai/agents-core 0.4.4 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.d.ts +71 -55
- package/dist/agent.js +51 -54
- package/dist/agent.js.map +1 -1
- package/dist/agent.mjs +48 -49
- package/dist/agent.mjs.map +1 -1
- package/dist/agentToolInput.d.ts +23 -0
- package/dist/agentToolInput.js +419 -0
- package/dist/agentToolInput.js.map +1 -0
- package/dist/agentToolInput.mjs +413 -0
- package/dist/agentToolInput.mjs.map +1 -0
- package/dist/agentToolRunResults.d.ts +7 -0
- package/dist/agentToolRunResults.js +20 -0
- package/dist/agentToolRunResults.js.map +1 -0
- package/dist/agentToolRunResults.mjs +16 -0
- package/dist/agentToolRunResults.mjs.map +1 -0
- package/dist/computer.d.ts +11 -9
- package/dist/index.d.ts +3 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/items.d.ts +2 -2
- package/dist/items.js.map +1 -1
- package/dist/items.mjs.map +1 -1
- package/dist/mcp.d.ts +56 -9
- package/dist/mcp.js +51 -8
- package/dist/mcp.js.map +1 -1
- package/dist/mcp.mjs +51 -8
- package/dist/mcp.mjs.map +1 -1
- package/dist/mcpUtil.d.ts +13 -0
- package/dist/mcpUtil.js.map +1 -1
- package/dist/mcpUtil.mjs.map +1 -1
- package/dist/metadata.js +2 -2
- package/dist/metadata.mjs +2 -2
- package/dist/run.d.ts +33 -0
- package/dist/run.js +10 -2
- package/dist/run.js.map +1 -1
- package/dist/run.mjs +10 -2
- package/dist/run.mjs.map +1 -1
- package/dist/runContext.d.ts +5 -0
- package/dist/runContext.js +30 -1
- package/dist/runContext.js.map +1 -1
- package/dist/runContext.mjs +30 -1
- package/dist/runContext.mjs.map +1 -1
- package/dist/runState.d.ts +112 -3
- package/dist/runState.js +13 -1
- package/dist/runState.js.map +1 -1
- package/dist/runState.mjs +13 -1
- package/dist/runState.mjs.map +1 -1
- package/dist/runner/modelPreparation.js +46 -1
- package/dist/runner/modelPreparation.js.map +1 -1
- package/dist/runner/modelPreparation.mjs +46 -1
- package/dist/runner/modelPreparation.mjs.map +1 -1
- package/dist/runner/runLoop.d.ts +2 -1
- package/dist/runner/runLoop.js +2 -2
- package/dist/runner/runLoop.js.map +1 -1
- package/dist/runner/runLoop.mjs +2 -2
- package/dist/runner/runLoop.mjs.map +1 -1
- package/dist/runner/toolExecution.d.ts +3 -3
- package/dist/runner/toolExecution.js +229 -31
- package/dist/runner/toolExecution.js.map +1 -1
- package/dist/runner/toolExecution.mjs +230 -32
- package/dist/runner/toolExecution.mjs.map +1 -1
- package/dist/runner/turnResolution.js +22 -16
- package/dist/runner/turnResolution.js.map +1 -1
- package/dist/runner/turnResolution.mjs +22 -16
- package/dist/runner/turnResolution.mjs.map +1 -1
- package/dist/shims/mcp-server/browser.d.ts +3 -3
- package/dist/shims/mcp-server/browser.js +3 -3
- package/dist/shims/mcp-server/browser.js.map +1 -1
- package/dist/shims/mcp-server/browser.mjs +3 -3
- package/dist/shims/mcp-server/browser.mjs.map +1 -1
- package/dist/shims/mcp-server/node.d.ts +3 -3
- package/dist/shims/mcp-server/node.js +15 -9
- package/dist/shims/mcp-server/node.js.map +1 -1
- package/dist/shims/mcp-server/node.mjs +15 -9
- package/dist/shims/mcp-server/node.mjs.map +1 -1
- package/dist/tool.d.ts +27 -0
- package/dist/tool.js +7 -0
- package/dist/tool.js.map +1 -1
- package/dist/tool.mjs +7 -0
- package/dist/tool.mjs.map +1 -1
- package/dist/types/helpers.d.ts +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +3 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +1 -0
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/tools.js +4 -0
- package/dist/utils/tools.js.map +1 -1
- package/dist/utils/tools.mjs +5 -1
- package/dist/utils/tools.mjs.map +1 -1
- package/dist/utils/zodJsonSchemaCompat.d.ts +2 -0
- package/dist/utils/zodJsonSchemaCompat.js +105 -2
- package/dist/utils/zodJsonSchemaCompat.js.map +1 -1
- package/dist/utils/zodJsonSchemaCompat.mjs +104 -2
- package/dist/utils/zodJsonSchemaCompat.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1,6 +1,37 @@
|
|
|
1
|
-
import { serializeHandoff, serializeTool } from "../utils/serialize.mjs";
|
|
2
1
|
import { resolveComputer } from "../tool.mjs";
|
|
2
|
+
import { serializeHandoff, serializeTool } from "../utils/serialize.mjs";
|
|
3
3
|
import { ensureAgentSpan } from "./tracing.mjs";
|
|
4
|
+
const computerInitPromisesByRunState = new WeakMap();
|
|
5
|
+
function getComputerInitMap(state) {
|
|
6
|
+
let initMap = computerInitPromisesByRunState.get(state);
|
|
7
|
+
if (!initMap) {
|
|
8
|
+
initMap = new WeakMap();
|
|
9
|
+
computerInitPromisesByRunState.set(state, initMap);
|
|
10
|
+
}
|
|
11
|
+
return initMap;
|
|
12
|
+
}
|
|
13
|
+
async function initComputerOnce(computer, state) {
|
|
14
|
+
if (typeof computer.initRun !== 'function') {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const initMap = getComputerInitMap(state);
|
|
18
|
+
const existing = initMap.get(computer);
|
|
19
|
+
if (existing) {
|
|
20
|
+
await existing;
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const initPromise = (async () => {
|
|
24
|
+
await computer.initRun?.(state._context);
|
|
25
|
+
})();
|
|
26
|
+
initMap.set(computer, initPromise);
|
|
27
|
+
try {
|
|
28
|
+
await initPromise;
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
initMap.delete(computer);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
4
35
|
/**
|
|
5
36
|
* Collects tools and handoffs for the current agent so model calls and tracing share the same
|
|
6
37
|
* snapshot of enabled capabilities.
|
|
@@ -8,6 +39,7 @@ import { ensureAgentSpan } from "./tracing.mjs";
|
|
|
8
39
|
export async function prepareAgentArtifacts(state) {
|
|
9
40
|
const capabilities = await collectAgentCapabilities(state);
|
|
10
41
|
await warmUpComputerTools(capabilities.tools, state._context);
|
|
42
|
+
await initializeComputerTools(capabilities.tools, state);
|
|
11
43
|
state.setCurrentAgentSpan(ensureAgentSpan({
|
|
12
44
|
agent: state._currentAgent,
|
|
13
45
|
handoffs: capabilities.handoffs,
|
|
@@ -35,4 +67,17 @@ async function warmUpComputerTools(tools, runContext) {
|
|
|
35
67
|
await resolveComputer({ tool, runContext });
|
|
36
68
|
}));
|
|
37
69
|
}
|
|
70
|
+
async function initializeComputerTools(tools, state) {
|
|
71
|
+
const computerTools = tools.filter((tool) => tool.type === 'computer');
|
|
72
|
+
if (computerTools.length === 0) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
await Promise.all(computerTools.map(async (tool) => {
|
|
76
|
+
const computer = await resolveComputer({
|
|
77
|
+
tool,
|
|
78
|
+
runContext: state._context,
|
|
79
|
+
});
|
|
80
|
+
await initComputerOnce(computer, state);
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
38
83
|
//# sourceMappingURL=modelPreparation.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"modelPreparation.mjs","sourceRoot":"","sources":["../../src/runner/modelPreparation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"modelPreparation.mjs","sourceRoot":"","sources":["../../src/runner/modelPreparation.ts"],"names":[],"mappings":"OAIO,EAAsB,eAAe,EAAE;OACvC,EAAE,gBAAgB,EAAE,aAAa,EAAE;OACnC,EAAE,eAAe,EAAE;AAG1B,MAAM,8BAA8B,GAAG,IAAI,OAAO,EAG/C,CAAC;AAEJ,SAAS,kBAAkB,CACzB,KAAyB;IAEzB,IAAI,OAAO,GAAG,8BAA8B,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACxD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,GAAG,IAAI,OAAO,EAAE,CA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AAU,uBAAuB,CACpC,KAAuB,EACvB,KAA2D;IAE3D,MAAM,aAAa,GAAG,KAAK,CAAC,MAAM,CAChC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,UAAU,CACF,CAAC;IAEnC,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,OAAO;IACT,CAAC;IAED,MAAM,OAAO,CAAC,GAAG,CACf,aAAa,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QAC/B,MAAM,QAAQ,GAAG,MAAM,eAAe,CAAC;YACrC,IAAI;YACJ,UAAU,EAAE,KAAK,CAAC,QAAQ;SAC3B,CAAC,CAAC;QACH,MAAM,gBAAgB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAC1C,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
|
package/dist/runner/runLoop.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Agent, AgentOutputType } from '../agent';
|
|
2
2
|
import type { RunState } from '../runState';
|
|
3
|
-
import type { Runner } from '../run';
|
|
3
|
+
import type { Runner, ToolErrorFormatter } from '../run';
|
|
4
4
|
import type { SingleStepResult } from './steps';
|
|
5
5
|
export type InterruptedTurnOutcome = {
|
|
6
6
|
nextStep: SingleStepResult['nextStep'];
|
|
@@ -22,6 +22,7 @@ export declare function applyTurnResult<TContext, TAgent extends Agent<TContext,
|
|
|
22
22
|
export declare function resumeInterruptedTurn<TContext, TAgent extends Agent<TContext, AgentOutputType>>(options: {
|
|
23
23
|
state: RunState<TContext, TAgent>;
|
|
24
24
|
runner: Runner;
|
|
25
|
+
toolErrorFormatter?: ToolErrorFormatter;
|
|
25
26
|
onStepItems?: (turnResult: SingleStepResult) => void;
|
|
26
27
|
}): Promise<InterruptedTurnOutcome>;
|
|
27
28
|
export declare function handleInterruptedOutcome<TContext, TAgent extends Agent<TContext, AgentOutputType>>(options: {
|
package/dist/runner/runLoop.js
CHANGED
|
@@ -21,8 +21,8 @@ function applyTurnResult(options) {
|
|
|
21
21
|
: undefined;
|
|
22
22
|
}
|
|
23
23
|
async function resumeInterruptedTurn(options) {
|
|
24
|
-
const { state, runner, onStepItems } = options;
|
|
25
|
-
const turnResult = await (0, turnResolution_1.resolveInterruptedTurn)(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, runner, state);
|
|
24
|
+
const { state, runner, toolErrorFormatter, onStepItems } = options;
|
|
25
|
+
const turnResult = await (0, turnResolution_1.resolveInterruptedTurn)(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, runner, state, toolErrorFormatter);
|
|
26
26
|
applyTurnResult({
|
|
27
27
|
state,
|
|
28
28
|
turnResult,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runLoop.js","sourceRoot":"","sources":["../../src/runner/runLoop.ts"],"names":[],"mappings":";;AA6BA,0CA2BC;AAED,
|
|
1
|
+
{"version":3,"file":"runLoop.js","sourceRoot":"","sources":["../../src/runner/runLoop.ts"],"names":[],"mappings":";;AA6BA,0CA2BC;AAED,sDAwCC;AAED,4DA4BC;AA3HD,wDAA0D;AAwB1D,SAAgB,eAAe,CAG7B,OAAiD;IACjD,MAAM,EACJ,KAAK,EACL,UAAU,EACV,KAAK,EACL,SAAS,EACT,oBAAoB,EACpB,WAAW,GACZ,GAAG,OAAO,CAAC;IACZ,WAAW,EAAE,CAAC,UAAU,CAAC,CAAC;IAC1B,KAAK,CAAC,eAAe,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IACnD,KAAK,CAAC,cAAc,GAAG,UAAU,CAAC,aAAa,CAAC;IAChD,KAAK,CAAC,eAAe,GAAG,UAAU,CAAC,cAAc,CAAC;IAClD,IACE,oBAAoB;QACpB,UAAU,CAAC,QAAQ,CAAC,IAAI,KAAK,qBAAqB,EAClD,CAAC;QACD,KAAK,CAAC,oBAAoB,EAAE,CAA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E,CAAC;AAED,SAAgB,wBAAwB,CAGtC,OAID;IACC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,4BAA4B,EAAE,GAAG,OAAO,CAAC;IAEjE,QAAQ,OAAO,CAAC,MAAM,EAAE,CAAC;QACvB,KAAK,qBAAqB;YACxB,KAAK,CAAC,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC;YACtC,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC;QACvD,KAAK,YAAY;YACf,iGAAiG;YACjG,4BAA4B,CAAC,IAAI,CAAC,CAAC;YACnC,KAAK,CAAC,YAAY,GAAG,SAAS,CAAC;YAC/B,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,cAAc,EAAE,IAAI,EAAE,CAAC;QACvD,KAAK,cAAc;YACjB,4BAA4B,CAAC,KAAK,CAAC,CAAC;YACpC,KAAK,CAAC,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC;YACtC,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC;QACxD,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,WAAW,GAAU,OAAO,CAAC,MAAM,CAAC;YAC1C,MAAM,IAAI,KAAK,CAAC,mCAAmC,WAAW,EAAE,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/runner/runLoop.mjs
CHANGED
|
@@ -16,8 +16,8 @@ export function applyTurnResult(options) {
|
|
|
16
16
|
: undefined;
|
|
17
17
|
}
|
|
18
18
|
export async function resumeInterruptedTurn(options) {
|
|
19
|
-
const { state, runner, onStepItems } = options;
|
|
20
|
-
const turnResult = await resolveInterruptedTurn(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, runner, state);
|
|
19
|
+
const { state, runner, toolErrorFormatter, onStepItems } = options;
|
|
20
|
+
const turnResult = await resolveInterruptedTurn(state._currentAgent, state._originalInput, state._generatedItems, state._lastTurnResponse, state._lastProcessedResponse, runner, state, toolErrorFormatter);
|
|
21
21
|
applyTurnResult({
|
|
22
22
|
state,
|
|
23
23
|
turnResult,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runLoop.mjs","sourceRoot":"","sources":["../../src/runner/runLoop.ts"],"names":[],"mappings":"OAKO,EAAE,sBAAsB,EAAE;AAwBjC,MAAM,UAAU,eAAe,CAG7B,OAAiD;IACjD,MAAM,EACJ,KAAK,EACL,UAAU,EACV,KAAK,EACL,SAAS,EACT,oBAAoB,EACpB,WAAW,GACZ,GAAG,OAAO,CAAC;IACZ,WAAW,EAAE,CAAC,UAAU,CAAC,CAAC;IAC1B,KAAK,CAAC,eAAe,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IACnD,KAAK,CAAC,cAAc,GAAG,UAAU,CAAC,aAAa,CAAC;IAChD,KAAK,CAAC,eAAe,GAAG,UAAU,CAAC,cAAc,CAAC;IAClD,IACE,oBAAoB;QACpB,UAAU,CAAC,QAAQ,CAAC,IAAI,KAAK,qBAAqB,EAClD,CAAC;QACD,KAAK,CAAC,oBAAoB,EAAE,CAAC;IAC/B,CAAC;IACD,KAAK,CAAC,YAAY,GAAG,UAAU,CAAC,QAAQ,CAAC;IACzC,KAAK,CAAC,kBAAkB;QACtB,UAAU,CAAC,QAAQ,CAAC,IAAI,KAAK,wBAAwB;YACnD,CAAC,CAAC,iBAAiB;YACnB,CAAC,CAAC,SAAS,CAAC;AAClB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAGzC,
|
|
1
|
+
{"version":3,"file":"runLoop.mjs","sourceRoot":"","sources":["../../src/runner/runLoop.ts"],"names":[],"mappings":"OAKO,EAAE,sBAAsB,EAAE;AAwBjC,MAAM,UAAU,eAAe,CAG7B,OAAiD;IACjD,MAAM,EACJ,KAAK,EACL,UAAU,EACV,KAAK,EACL,SAAS,EACT,oBAAoB,EACpB,WAAW,GACZ,GAAG,OAA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 @@ import { Logger } from '../logger';
|
|
|
4
4
|
import { FunctionToolResult } from '../tool';
|
|
5
5
|
import { RunContext } from '../runContext';
|
|
6
6
|
import type { UnknownContext } from '../types';
|
|
7
|
-
import type { Runner } from '../run';
|
|
7
|
+
import type { Runner, ToolErrorFormatter } from '../run';
|
|
8
8
|
import type { ToolRunApplyPatch, ToolRunShell } from './types';
|
|
9
|
-
export declare function executeShellActions(agent: Agent<any, any>, actions: ToolRunShell[], runner: Runner, runContext: RunContext, customLogger?: Logger | undefined): Promise<RunItem[]>;
|
|
10
|
-
export declare function executeApplyPatchOperations(agent: Agent<any, any>, actions: ToolRunApplyPatch[], runner: Runner, runContext: RunContext, customLogger?: Logger | undefined): Promise<RunItem[]>;
|
|
9
|
+
export declare function executeShellActions(agent: Agent<any, any>, actions: ToolRunShell[], runner: Runner, runContext: RunContext, customLogger?: Logger | undefined, toolErrorFormatter?: ToolErrorFormatter): Promise<RunItem[]>;
|
|
10
|
+
export declare function executeApplyPatchOperations(agent: Agent<any, any>, actions: ToolRunApplyPatch[], runner: Runner, runContext: RunContext, customLogger?: Logger | undefined, toolErrorFormatter?: ToolErrorFormatter): Promise<RunItem[]>;
|
|
11
11
|
/**
|
|
12
12
|
* Collects approval interruptions from tool execution results and any additional
|
|
13
13
|
* RunItems (e.g., shell/apply_patch approval placeholders).
|
|
@@ -11,10 +11,11 @@ exports.executeComputerActions = executeComputerActions;
|
|
|
11
11
|
exports.executeHandoffCalls = executeHandoffCalls;
|
|
12
12
|
exports.collectInterruptions = collectInterruptions;
|
|
13
13
|
exports.checkForFinalOutputFromTools = checkForFinalOutputFromTools;
|
|
14
|
-
const
|
|
14
|
+
const agentToolRunResults_1 = require("../agentToolRunResults.js");
|
|
15
15
|
const errors_1 = require("../errors.js");
|
|
16
16
|
const handoff_1 = require("../handoff.js");
|
|
17
17
|
const items_1 = require("../items.js");
|
|
18
|
+
const message_1 = require("../helpers/message.js");
|
|
18
19
|
const logger_1 = __importDefault(require("../logger.js"));
|
|
19
20
|
const tool_1 = require("../tool.js");
|
|
20
21
|
const base64_1 = require("../utils/base64.js");
|
|
@@ -24,6 +25,33 @@ const createSpans_1 = require("../tracing/createSpans.js");
|
|
|
24
25
|
const toolGuardrails_1 = require("../utils/toolGuardrails.js");
|
|
25
26
|
const steps_1 = require("./steps.js");
|
|
26
27
|
const TOOL_APPROVAL_REJECTION_MESSAGE = 'Tool execution was not approved.';
|
|
28
|
+
// 1x1 transparent PNG data URL used for rejected computer actions.
|
|
29
|
+
const TOOL_APPROVAL_REJECTION_SCREENSHOT_DATA_URL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg==';
|
|
30
|
+
async function resolveApprovalRejectionMessage({ runContext, toolType, toolName, callId, toolErrorFormatter, }) {
|
|
31
|
+
if (!toolErrorFormatter) {
|
|
32
|
+
return TOOL_APPROVAL_REJECTION_MESSAGE;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const formattedMessage = await toolErrorFormatter({
|
|
36
|
+
kind: 'approval_rejected',
|
|
37
|
+
toolType,
|
|
38
|
+
toolName,
|
|
39
|
+
callId,
|
|
40
|
+
defaultMessage: TOOL_APPROVAL_REJECTION_MESSAGE,
|
|
41
|
+
runContext,
|
|
42
|
+
});
|
|
43
|
+
if (typeof formattedMessage === 'string') {
|
|
44
|
+
return formattedMessage;
|
|
45
|
+
}
|
|
46
|
+
if (typeof formattedMessage !== 'undefined') {
|
|
47
|
+
logger_1.default.warn('toolErrorFormatter returned a non-string value. Falling back to the default tool approval rejection message.');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
logger_1.default.warn(`toolErrorFormatter threw while formatting approval rejection: ${toErrorMessage(error)}`);
|
|
52
|
+
}
|
|
53
|
+
return TOOL_APPROVAL_REJECTION_MESSAGE;
|
|
54
|
+
}
|
|
27
55
|
/**
|
|
28
56
|
* @internal
|
|
29
57
|
* Normalizes tool outputs once so downstream code works with fully structured protocol items.
|
|
@@ -57,8 +85,13 @@ function getToolCallOutputItem(toolCall, output) {
|
|
|
57
85
|
* Runs every function tool call requested by the model and returns their outputs alongside
|
|
58
86
|
* the `RunItem` instances that should be appended to history.
|
|
59
87
|
*/
|
|
60
|
-
async function executeFunctionToolCalls(agent, toolRuns, runner, state) {
|
|
61
|
-
const deps = {
|
|
88
|
+
async function executeFunctionToolCalls(agent, toolRuns, runner, state, toolErrorFormatter) {
|
|
89
|
+
const deps = {
|
|
90
|
+
agent,
|
|
91
|
+
runner,
|
|
92
|
+
state,
|
|
93
|
+
toolErrorFormatter,
|
|
94
|
+
};
|
|
62
95
|
try {
|
|
63
96
|
const results = await Promise.all(toolRuns.map(async (toolRun) => {
|
|
64
97
|
const parseResult = parseToolArguments(toolRun);
|
|
@@ -113,17 +146,28 @@ function buildParseErrorResult(deps, toolRun, error) {
|
|
|
113
146
|
};
|
|
114
147
|
}
|
|
115
148
|
async function buildApprovalRejectionResult(deps, toolRun) {
|
|
116
|
-
const { agent } = deps;
|
|
149
|
+
const { agent, runner, state, toolErrorFormatter } = deps;
|
|
117
150
|
return (0, createSpans_1.withFunctionSpan)(async (span) => {
|
|
118
|
-
const response =
|
|
151
|
+
const response = await resolveApprovalRejectionMessage({
|
|
152
|
+
runContext: state._context,
|
|
153
|
+
toolType: 'function',
|
|
154
|
+
toolName: toolRun.tool.name,
|
|
155
|
+
callId: toolRun.toolCall.callId,
|
|
156
|
+
toolErrorFormatter,
|
|
157
|
+
});
|
|
158
|
+
const traceErrorMessage = runner.config.traceIncludeSensitiveData
|
|
159
|
+
? response
|
|
160
|
+
: TOOL_APPROVAL_REJECTION_MESSAGE;
|
|
119
161
|
span.setError({
|
|
120
|
-
message:
|
|
162
|
+
message: traceErrorMessage,
|
|
121
163
|
data: {
|
|
122
164
|
tool_name: toolRun.tool.name,
|
|
123
165
|
error: `Tool execution for ${toolRun.toolCall.callId} was manually rejected by user.`,
|
|
124
166
|
},
|
|
125
167
|
});
|
|
126
|
-
|
|
168
|
+
if (runner.config.traceIncludeSensitiveData) {
|
|
169
|
+
span.spanData.output = response;
|
|
170
|
+
}
|
|
127
171
|
return {
|
|
128
172
|
type: 'function_output',
|
|
129
173
|
tool: toolRun.tool,
|
|
@@ -201,7 +245,7 @@ async function runApprovedFunctionTool(deps, toolRun) {
|
|
|
201
245
|
output: toolOutput,
|
|
202
246
|
runItem: new items_1.RunToolCallOutputItem(getToolCallOutputItem(toolRun.toolCall, toolOutput), agent, toolOutput),
|
|
203
247
|
};
|
|
204
|
-
const nestedRunResult = (0,
|
|
248
|
+
const nestedRunResult = (0, agentToolRunResults_1.consumeAgentToolRunResult)(toolRun.toolCall);
|
|
205
249
|
if (nestedRunResult) {
|
|
206
250
|
functionResult.agentRunResult = nestedRunResult;
|
|
207
251
|
const nestedInterruptions = nestedRunResult.interruptions;
|
|
@@ -237,37 +281,37 @@ async function runApprovedFunctionTool(deps, toolRun) {
|
|
|
237
281
|
* @internal
|
|
238
282
|
*/
|
|
239
283
|
// Internal helper: dispatch a computer action and return a screenshot (sync/async)
|
|
240
|
-
async function _runComputerActionAndScreenshot(computer, toolCall) {
|
|
284
|
+
async function _runComputerActionAndScreenshot(computer, toolCall, runContext) {
|
|
241
285
|
const action = toolCall.action;
|
|
242
286
|
let screenshot;
|
|
243
287
|
// Dispatch based on action type string (assume action.type exists)
|
|
244
288
|
switch (action.type) {
|
|
245
289
|
case 'click':
|
|
246
|
-
await computer.click(action.x, action.y, action.button);
|
|
290
|
+
await computer.click(action.x, action.y, action.button, runContext);
|
|
247
291
|
break;
|
|
248
292
|
case 'double_click':
|
|
249
|
-
await computer.doubleClick(action.x, action.y);
|
|
293
|
+
await computer.doubleClick(action.x, action.y, runContext);
|
|
250
294
|
break;
|
|
251
295
|
case 'drag':
|
|
252
|
-
await computer.drag(action.path.map((p) => [p.x, p.y]));
|
|
296
|
+
await computer.drag(action.path.map((p) => [p.x, p.y]), runContext);
|
|
253
297
|
break;
|
|
254
298
|
case 'keypress':
|
|
255
|
-
await computer.keypress(action.keys);
|
|
299
|
+
await computer.keypress(action.keys, runContext);
|
|
256
300
|
break;
|
|
257
301
|
case 'move':
|
|
258
|
-
await computer.move(action.x, action.y);
|
|
302
|
+
await computer.move(action.x, action.y, runContext);
|
|
259
303
|
break;
|
|
260
304
|
case 'screenshot':
|
|
261
|
-
screenshot = await computer.screenshot();
|
|
305
|
+
screenshot = await computer.screenshot(runContext);
|
|
262
306
|
break;
|
|
263
307
|
case 'scroll':
|
|
264
|
-
await computer.scroll(action.x, action.y, action.scroll_x, action.scroll_y);
|
|
308
|
+
await computer.scroll(action.x, action.y, action.scroll_x, action.scroll_y, runContext);
|
|
265
309
|
break;
|
|
266
310
|
case 'type':
|
|
267
|
-
await computer.type(action.text);
|
|
311
|
+
await computer.type(action.text, runContext);
|
|
268
312
|
break;
|
|
269
313
|
case 'wait':
|
|
270
|
-
await computer.wait();
|
|
314
|
+
await computer.wait(runContext);
|
|
271
315
|
break;
|
|
272
316
|
default:
|
|
273
317
|
action; // ensures that we handle every action we know of
|
|
@@ -279,7 +323,7 @@ async function _runComputerActionAndScreenshot(computer, toolCall) {
|
|
|
279
323
|
}
|
|
280
324
|
// Always return screenshot as base64 string
|
|
281
325
|
if (typeof computer.screenshot === 'function') {
|
|
282
|
-
screenshot = await computer.screenshot();
|
|
326
|
+
screenshot = await computer.screenshot(runContext);
|
|
283
327
|
if (typeof screenshot !== 'undefined') {
|
|
284
328
|
return screenshot;
|
|
285
329
|
}
|
|
@@ -334,7 +378,7 @@ async function handleToolApprovalDecision(options) {
|
|
|
334
378
|
onApproval,
|
|
335
379
|
});
|
|
336
380
|
if (approvalState === 'rejected') {
|
|
337
|
-
return { status: 'rejected', item: buildRejectionItem() };
|
|
381
|
+
return { status: 'rejected', item: await buildRejectionItem() };
|
|
338
382
|
}
|
|
339
383
|
if (approvalState === 'pending') {
|
|
340
384
|
return { status: 'pending', item: approvalItem };
|
|
@@ -353,7 +397,7 @@ function emitToolEnd(runner, runContext, agent, tool, output, toolCall) {
|
|
|
353
397
|
agent.emit('agent_tool_end', runContext, tool, output, { toolCall });
|
|
354
398
|
}
|
|
355
399
|
}
|
|
356
|
-
async function executeShellActions(agent, actions, runner, runContext, customLogger = undefined) {
|
|
400
|
+
async function executeShellActions(agent, actions, runner, runContext, customLogger = undefined, toolErrorFormatter) {
|
|
357
401
|
const _logger = customLogger ?? logger_1.default;
|
|
358
402
|
const results = [];
|
|
359
403
|
for (const action of actions) {
|
|
@@ -367,8 +411,14 @@ async function executeShellActions(agent, actions, runner, runContext, customLog
|
|
|
367
411
|
approvalItem,
|
|
368
412
|
needsApproval: await shellTool.needsApproval(runContext, toolCall.action, toolCall.callId),
|
|
369
413
|
onApproval: shellTool.onApproval,
|
|
370
|
-
buildRejectionItem: () => {
|
|
371
|
-
const response =
|
|
414
|
+
buildRejectionItem: async () => {
|
|
415
|
+
const response = await resolveApprovalRejectionMessage({
|
|
416
|
+
runContext,
|
|
417
|
+
toolType: 'shell',
|
|
418
|
+
toolName: shellTool.name,
|
|
419
|
+
callId: toolCall.callId,
|
|
420
|
+
toolErrorFormatter,
|
|
421
|
+
});
|
|
372
422
|
const rejectionOutput = {
|
|
373
423
|
stdout: '',
|
|
374
424
|
stderr: response,
|
|
@@ -427,7 +477,7 @@ async function executeShellActions(agent, actions, runner, runContext, customLog
|
|
|
427
477
|
}
|
|
428
478
|
return results;
|
|
429
479
|
}
|
|
430
|
-
async function executeApplyPatchOperations(agent, actions, runner, runContext, customLogger = undefined) {
|
|
480
|
+
async function executeApplyPatchOperations(agent, actions, runner, runContext, customLogger = undefined, toolErrorFormatter) {
|
|
431
481
|
const _logger = customLogger ?? logger_1.default;
|
|
432
482
|
const results = [];
|
|
433
483
|
for (const action of actions) {
|
|
@@ -441,8 +491,14 @@ async function executeApplyPatchOperations(agent, actions, runner, runContext, c
|
|
|
441
491
|
approvalItem,
|
|
442
492
|
needsApproval: await applyPatchTool.needsApproval(runContext, toolCall.operation, toolCall.callId),
|
|
443
493
|
onApproval: applyPatchTool.onApproval,
|
|
444
|
-
buildRejectionItem: () => {
|
|
445
|
-
const response =
|
|
494
|
+
buildRejectionItem: async () => {
|
|
495
|
+
const response = await resolveApprovalRejectionMessage({
|
|
496
|
+
runContext,
|
|
497
|
+
toolType: 'apply_patch',
|
|
498
|
+
toolName: applyPatchTool.name,
|
|
499
|
+
callId: toolCall.callId,
|
|
500
|
+
toolErrorFormatter,
|
|
501
|
+
});
|
|
446
502
|
return new items_1.RunToolCallOutputItem({
|
|
447
503
|
type: 'apply_patch_call_output',
|
|
448
504
|
callId: toolCall.callId,
|
|
@@ -503,28 +559,94 @@ async function executeApplyPatchOperations(agent, actions, runner, runContext, c
|
|
|
503
559
|
* Executes any computer-use actions emitted by the model and returns the resulting items so
|
|
504
560
|
* the run history reflects the computer session.
|
|
505
561
|
*/
|
|
506
|
-
async function executeComputerActions(agent, actions, runner, runContext, customLogger = undefined) {
|
|
562
|
+
async function executeComputerActions(agent, actions, runner, runContext, customLogger = undefined, toolErrorFormatter) {
|
|
507
563
|
const _logger = customLogger ?? logger_1.default;
|
|
508
564
|
const results = [];
|
|
509
565
|
for (const action of actions) {
|
|
510
566
|
const toolCall = action.toolCall;
|
|
567
|
+
const computerTool = action.computer;
|
|
568
|
+
let cachedRejectionMessage;
|
|
569
|
+
const getRejectionMessage = async () => {
|
|
570
|
+
if (typeof cachedRejectionMessage === 'string') {
|
|
571
|
+
return cachedRejectionMessage;
|
|
572
|
+
}
|
|
573
|
+
cachedRejectionMessage = await resolveApprovalRejectionMessage({
|
|
574
|
+
runContext,
|
|
575
|
+
toolType: 'computer',
|
|
576
|
+
toolName: computerTool.name,
|
|
577
|
+
callId: toolCall.callId,
|
|
578
|
+
toolErrorFormatter,
|
|
579
|
+
});
|
|
580
|
+
return cachedRejectionMessage;
|
|
581
|
+
};
|
|
582
|
+
const pendingSafetyChecks = getPendingSafetyChecks(toolCall);
|
|
583
|
+
const approvalItem = new items_1.RunToolApprovalItem(toolCall, agent, computerTool.name);
|
|
584
|
+
const needsApprovalCandidate = computerTool
|
|
585
|
+
.needsApproval;
|
|
586
|
+
const needsApproval = typeof needsApprovalCandidate === 'function'
|
|
587
|
+
? await needsApprovalCandidate(runContext, toolCall.action, toolCall.callId)
|
|
588
|
+
: typeof needsApprovalCandidate === 'boolean'
|
|
589
|
+
? needsApprovalCandidate
|
|
590
|
+
: false;
|
|
591
|
+
const approvalDecision = await handleToolApprovalDecision({
|
|
592
|
+
runContext,
|
|
593
|
+
toolName: computerTool.name,
|
|
594
|
+
callId: toolCall.callId,
|
|
595
|
+
approvalItem,
|
|
596
|
+
needsApproval,
|
|
597
|
+
buildRejectionItem: async () => {
|
|
598
|
+
const rejectionMessage = await getRejectionMessage();
|
|
599
|
+
const rejectionOutput = {
|
|
600
|
+
type: 'computer_screenshot',
|
|
601
|
+
data: TOOL_APPROVAL_REJECTION_SCREENSHOT_DATA_URL,
|
|
602
|
+
providerData: {
|
|
603
|
+
approvalStatus: 'rejected',
|
|
604
|
+
message: rejectionMessage,
|
|
605
|
+
},
|
|
606
|
+
};
|
|
607
|
+
const rawItem = {
|
|
608
|
+
type: 'computer_call_result',
|
|
609
|
+
callId: toolCall.callId,
|
|
610
|
+
output: rejectionOutput,
|
|
611
|
+
};
|
|
612
|
+
return new items_1.RunToolCallOutputItem(rawItem, agent, TOOL_APPROVAL_REJECTION_SCREENSHOT_DATA_URL);
|
|
613
|
+
},
|
|
614
|
+
});
|
|
615
|
+
if (approvalDecision.status === 'rejected') {
|
|
616
|
+
const rejectionMessage = await getRejectionMessage();
|
|
617
|
+
results.push(approvalDecision.item);
|
|
618
|
+
results.push(new items_1.RunMessageOutputItem((0, message_1.assistant)(rejectionMessage), agent));
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (approvalDecision.status === 'pending') {
|
|
622
|
+
results.push(approvalDecision.item);
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
511
625
|
// Hooks: on_tool_start (global + agent)
|
|
512
|
-
emitToolStart(runner, runContext, agent,
|
|
626
|
+
emitToolStart(runner, runContext, agent, computerTool, toolCall);
|
|
627
|
+
const acknowledgedSafetyChecks = pendingSafetyChecks && pendingSafetyChecks.length > 0
|
|
628
|
+
? await resolveSafetyCheckAcknowledgements({
|
|
629
|
+
runContext,
|
|
630
|
+
toolCall,
|
|
631
|
+
pendingSafetyChecks,
|
|
632
|
+
onSafetyCheck: computerTool.onSafetyCheck,
|
|
633
|
+
})
|
|
634
|
+
: undefined;
|
|
513
635
|
// Run the action and get screenshot
|
|
514
636
|
let output;
|
|
515
637
|
try {
|
|
516
638
|
const computer = await (0, tool_1.resolveComputer)({
|
|
517
|
-
tool:
|
|
639
|
+
tool: computerTool,
|
|
518
640
|
runContext,
|
|
519
641
|
});
|
|
520
|
-
output = await _runComputerActionAndScreenshot(computer, toolCall);
|
|
642
|
+
output = await _runComputerActionAndScreenshot(computer, toolCall, runContext);
|
|
521
643
|
}
|
|
522
644
|
catch (err) {
|
|
523
645
|
_logger.error('Failed to execute computer action:', err);
|
|
524
646
|
output = '';
|
|
525
647
|
}
|
|
526
648
|
// Hooks: on_tool_end (global + agent)
|
|
527
|
-
emitToolEnd(runner, runContext, agent,
|
|
649
|
+
emitToolEnd(runner, runContext, agent, computerTool, output, toolCall);
|
|
528
650
|
// Return the screenshot as a data URL when available; fall back to an empty string on failures.
|
|
529
651
|
const imageUrl = output ? `data:image/png;base64,${output}` : '';
|
|
530
652
|
const rawItem = {
|
|
@@ -532,6 +654,11 @@ async function executeComputerActions(agent, actions, runner, runContext, custom
|
|
|
532
654
|
callId: toolCall.callId,
|
|
533
655
|
output: { type: 'computer_screenshot', data: imageUrl },
|
|
534
656
|
};
|
|
657
|
+
if (acknowledgedSafetyChecks && acknowledgedSafetyChecks.length > 0) {
|
|
658
|
+
rawItem.providerData = {
|
|
659
|
+
acknowledgedSafetyChecks,
|
|
660
|
+
};
|
|
661
|
+
}
|
|
535
662
|
results.push(new items_1.RunToolCallOutputItem(rawItem, agent, imageUrl));
|
|
536
663
|
}
|
|
537
664
|
return results;
|
|
@@ -1000,6 +1127,77 @@ function normalizeLegacyFileValue(value) {
|
|
|
1000
1127
|
}
|
|
1001
1128
|
return null;
|
|
1002
1129
|
}
|
|
1130
|
+
function normalizeSafetyChecks(checks) {
|
|
1131
|
+
if (!Array.isArray(checks)) {
|
|
1132
|
+
return undefined;
|
|
1133
|
+
}
|
|
1134
|
+
const normalized = [];
|
|
1135
|
+
for (const entry of checks) {
|
|
1136
|
+
if (!isRecord(entry)) {
|
|
1137
|
+
continue;
|
|
1138
|
+
}
|
|
1139
|
+
const id = entry.id;
|
|
1140
|
+
const code = entry.code;
|
|
1141
|
+
if (!isNonEmptyString(id) || !isNonEmptyString(code)) {
|
|
1142
|
+
continue;
|
|
1143
|
+
}
|
|
1144
|
+
const message = 'message' in entry && isNonEmptyString(entry.message)
|
|
1145
|
+
? entry.message
|
|
1146
|
+
: undefined;
|
|
1147
|
+
const normalizedEntry = { ...entry, id, code };
|
|
1148
|
+
if (message) {
|
|
1149
|
+
normalizedEntry.message = message;
|
|
1150
|
+
}
|
|
1151
|
+
normalized.push(normalizedEntry);
|
|
1152
|
+
}
|
|
1153
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
1154
|
+
}
|
|
1155
|
+
function normalizeSafetyCheckResult(result) {
|
|
1156
|
+
if (!result) {
|
|
1157
|
+
return undefined;
|
|
1158
|
+
}
|
|
1159
|
+
if (!isRecord(result)) {
|
|
1160
|
+
return undefined;
|
|
1161
|
+
}
|
|
1162
|
+
if ('acknowledgedSafetyChecks' in result) {
|
|
1163
|
+
return normalizeSafetyChecks(result.acknowledgedSafetyChecks);
|
|
1164
|
+
}
|
|
1165
|
+
if ('acknowledged_safety_checks' in result) {
|
|
1166
|
+
return normalizeSafetyChecks(result.acknowledged_safety_checks);
|
|
1167
|
+
}
|
|
1168
|
+
return undefined;
|
|
1169
|
+
}
|
|
1170
|
+
async function resolveSafetyCheckAcknowledgements(options) {
|
|
1171
|
+
const { runContext, toolCall, pendingSafetyChecks, onSafetyCheck } = options;
|
|
1172
|
+
if (!onSafetyCheck) {
|
|
1173
|
+
return undefined;
|
|
1174
|
+
}
|
|
1175
|
+
const result = await onSafetyCheck({
|
|
1176
|
+
runContext,
|
|
1177
|
+
pendingSafetyChecks,
|
|
1178
|
+
toolCall,
|
|
1179
|
+
});
|
|
1180
|
+
if (result === true) {
|
|
1181
|
+
return pendingSafetyChecks;
|
|
1182
|
+
}
|
|
1183
|
+
if (result === false) {
|
|
1184
|
+
return undefined;
|
|
1185
|
+
}
|
|
1186
|
+
return normalizeSafetyCheckResult(result);
|
|
1187
|
+
}
|
|
1188
|
+
function getPendingSafetyChecks(toolCall) {
|
|
1189
|
+
const providerData = toolCall.providerData;
|
|
1190
|
+
if (!isRecord(providerData)) {
|
|
1191
|
+
return undefined;
|
|
1192
|
+
}
|
|
1193
|
+
if ('pending_safety_checks' in providerData) {
|
|
1194
|
+
return normalizeSafetyChecks(providerData.pending_safety_checks);
|
|
1195
|
+
}
|
|
1196
|
+
if ('pendingSafetyChecks' in providerData) {
|
|
1197
|
+
return normalizeSafetyChecks(providerData.pendingSafetyChecks);
|
|
1198
|
+
}
|
|
1199
|
+
return undefined;
|
|
1200
|
+
}
|
|
1003
1201
|
function isRecord(value) {
|
|
1004
1202
|
return typeof value === 'object' && value !== null;
|
|
1005
1203
|
}
|