@openai/agents-core 0.11.8 → 0.12.1
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/handoff.d.ts +1 -1
- package/dist/handoff.js +2 -2
- package/dist/handoff.js.map +1 -1
- package/dist/handoff.mjs +2 -2
- package/dist/handoff.mjs.map +1 -1
- package/dist/metadata.js +3 -3
- package/dist/metadata.mjs +3 -3
- package/dist/run.d.ts +2 -13
- package/dist/run.js +15 -40
- package/dist/run.js.map +1 -1
- package/dist/run.mjs +14 -39
- package/dist/run.mjs.map +1 -1
- package/dist/runState.d.ts +1 -1
- package/dist/runState.js +7 -184
- package/dist/runState.js.map +1 -1
- package/dist/runState.mjs +2 -179
- package/dist/runState.mjs.map +1 -1
- package/dist/runStateIdentity.d.ts +6 -0
- package/dist/runStateIdentity.js +171 -0
- package/dist/runStateIdentity.js.map +1 -0
- package/dist/runStateIdentity.mjs +168 -0
- package/dist/runStateIdentity.mjs.map +1 -0
- package/dist/runner/conversation.js +46 -4
- package/dist/runner/conversation.js.map +1 -1
- package/dist/runner/conversation.mjs +46 -4
- package/dist/runner/conversation.mjs.map +1 -1
- package/dist/runner/errorHandlers.d.ts +7 -4
- package/dist/runner/errorHandlers.js +31 -10
- package/dist/runner/errorHandlers.js.map +1 -1
- package/dist/runner/errorHandlers.mjs +31 -10
- package/dist/runner/errorHandlers.mjs.map +1 -1
- package/dist/runner/guardrails.d.ts +4 -2
- package/dist/runner/guardrails.js +23 -20
- package/dist/runner/guardrails.js.map +1 -1
- package/dist/runner/guardrails.mjs +23 -20
- package/dist/runner/guardrails.mjs.map +1 -1
- package/dist/runner/items.js +3 -8
- package/dist/runner/items.js.map +1 -1
- package/dist/runner/items.mjs +3 -8
- package/dist/runner/items.mjs.map +1 -1
- package/dist/runner/modelSettings.d.ts +1 -1
- package/dist/runner/modelSettings.js +2 -2
- package/dist/runner/modelSettings.js.map +1 -1
- package/dist/runner/modelSettings.mjs +2 -2
- package/dist/runner/modelSettings.mjs.map +1 -1
- package/dist/runner/runConfig.d.ts +16 -0
- package/dist/runner/runConfig.js +31 -0
- package/dist/runner/runConfig.js.map +1 -0
- package/dist/runner/runConfig.mjs +27 -0
- package/dist/runner/runConfig.mjs.map +1 -0
- package/dist/runner/runLoop.js +1 -1
- package/dist/runner/runLoop.js.map +1 -1
- package/dist/runner/runLoop.mjs +1 -1
- package/dist/runner/runLoop.mjs.map +1 -1
- package/dist/runner/steps.d.ts +3 -1
- package/dist/runner/steps.js +3 -1
- package/dist/runner/steps.js.map +1 -1
- package/dist/runner/steps.mjs +3 -1
- package/dist/runner/steps.mjs.map +1 -1
- package/dist/runner/toolExecution.js +50 -373
- package/dist/runner/toolExecution.js.map +1 -1
- package/dist/runner/toolExecution.mjs +48 -371
- package/dist/runner/toolExecution.mjs.map +1 -1
- package/dist/runner/toolOutputNormalization.d.ts +16 -0
- package/dist/runner/toolOutputNormalization.js +328 -0
- package/dist/runner/toolOutputNormalization.js.map +1 -0
- package/dist/runner/toolOutputNormalization.mjs +324 -0
- package/dist/runner/toolOutputNormalization.mjs.map +1 -0
- package/dist/runner/toolResultCorrelation.d.ts +22 -0
- package/dist/runner/toolResultCorrelation.js +138 -0
- package/dist/runner/toolResultCorrelation.js.map +1 -0
- package/dist/runner/toolResultCorrelation.mjs +130 -0
- package/dist/runner/toolResultCorrelation.mjs.map +1 -0
- package/dist/runner/turnPreparation.js +1 -5
- package/dist/runner/turnPreparation.js.map +1 -1
- package/dist/runner/turnPreparation.mjs +1 -5
- package/dist/runner/turnPreparation.mjs.map +1 -1
- package/dist/runner/turnResolution.js +66 -15
- package/dist/runner/turnResolution.js.map +1 -1
- package/dist/runner/turnResolution.mjs +67 -16
- package/dist/runner/turnResolution.mjs.map +1 -1
- package/dist/runner/types.d.ts +1 -1
- package/dist/sandbox/internal/process.d.ts +1 -0
- package/dist/sandbox/internal/process.js +7 -0
- package/dist/sandbox/internal/process.js.map +1 -0
- package/dist/sandbox/internal/process.mjs +2 -0
- package/dist/sandbox/internal/process.mjs.map +1 -0
- package/dist/sandbox/internal.d.ts +2 -0
- package/dist/sandbox/internal.js +15 -1
- package/dist/sandbox/internal.js.map +1 -1
- package/dist/sandbox/internal.mjs +2 -0
- package/dist/sandbox/internal.mjs.map +1 -1
- package/dist/sandbox/permissions.js +7 -6
- package/dist/sandbox/permissions.js.map +1 -1
- package/dist/sandbox/permissions.mjs +7 -6
- package/dist/sandbox/permissions.mjs.map +1 -1
- package/dist/sandbox/runtime/agentPreparation.js +11 -1
- package/dist/sandbox/runtime/agentPreparation.js.map +1 -1
- package/dist/sandbox/runtime/agentPreparation.mjs +12 -2
- package/dist/sandbox/runtime/agentPreparation.mjs.map +1 -1
- package/dist/sandbox/runtime/prompts.d.ts +1 -1
- package/dist/sandbox/runtime/prompts.js +36 -4
- package/dist/sandbox/runtime/prompts.js.map +1 -1
- package/dist/sandbox/runtime/prompts.mjs +36 -4
- package/dist/sandbox/runtime/prompts.mjs.map +1 -1
- package/dist/sandbox/sandboxes/docker.js +17 -3
- package/dist/sandbox/sandboxes/docker.js.map +1 -1
- package/dist/sandbox/sandboxes/docker.mjs +17 -3
- package/dist/sandbox/sandboxes/docker.mjs.map +1 -1
- package/dist/sandbox/sandboxes/unixLocal.js +9 -0
- package/dist/sandbox/sandboxes/unixLocal.js.map +1 -1
- package/dist/sandbox/sandboxes/unixLocal.mjs +9 -0
- package/dist/sandbox/sandboxes/unixLocal.mjs.map +1 -1
- package/dist/sandbox/shared/credentials.d.ts +9 -0
- package/dist/sandbox/shared/credentials.js +10 -0
- package/dist/sandbox/shared/credentials.js.map +1 -0
- package/dist/sandbox/shared/credentials.mjs +7 -0
- package/dist/sandbox/shared/credentials.mjs.map +1 -0
- package/dist/sandbox/shared/typeGuards.d.ts +6 -1
- package/dist/sandbox/shared/typeGuards.js +23 -1
- package/dist/sandbox/shared/typeGuards.js.map +1 -1
- package/dist/sandbox/shared/typeGuards.mjs +18 -1
- package/dist/sandbox/shared/typeGuards.mjs.map +1 -1
- package/dist/utils/inlineData.d.ts +2 -0
- package/dist/utils/inlineData.js +22 -0
- package/dist/utils/inlineData.js.map +1 -0
- package/dist/utils/inlineData.mjs +18 -0
- package/dist/utils/inlineData.mjs.map +1 -0
- package/dist/utils/internal.d.ts +1 -0
- package/dist/utils/internal.js +7 -0
- package/dist/utils/internal.js.map +1 -0
- package/dist/utils/internal.mjs +2 -0
- package/dist/utils/internal.mjs.map +1 -0
- package/package.json +22 -3
|
@@ -19,12 +19,12 @@ const items_1 = require("../items.js");
|
|
|
19
19
|
const message_1 = require("../helpers/message.js");
|
|
20
20
|
const logger_1 = __importDefault(require("../logger.js"));
|
|
21
21
|
const tool_1 = require("../tool.js");
|
|
22
|
-
const base64_1 = require("../utils/base64.js");
|
|
23
22
|
const smartString_1 = require("../utils/smartString.js");
|
|
24
23
|
const utils_1 = require("../utils/index.js");
|
|
25
24
|
const createSpans_1 = require("../tracing/createSpans.js");
|
|
26
25
|
const context_1 = require("../tracing/context.js");
|
|
27
26
|
const toolIdentity_1 = require("../toolIdentity.js");
|
|
27
|
+
const toolOutputNormalization_1 = require("./toolOutputNormalization.js");
|
|
28
28
|
const toolGuardrails_1 = require("../utils/toolGuardrails.js");
|
|
29
29
|
const customData_1 = require("../utils/customData.js");
|
|
30
30
|
const approvalRejection_1 = require("./approvalRejection.js");
|
|
@@ -66,9 +66,9 @@ function getComputerTraceInputPayload(toolCall) {
|
|
|
66
66
|
* Doing this here keeps API surface stable even when providers add new shapes.
|
|
67
67
|
*/
|
|
68
68
|
function getToolCallOutputItem(toolCall, output) {
|
|
69
|
-
const maybeStructuredOutputs = normalizeStructuredToolOutputs(output);
|
|
69
|
+
const maybeStructuredOutputs = (0, toolOutputNormalization_1.normalizeStructuredToolOutputs)(output);
|
|
70
70
|
if (maybeStructuredOutputs) {
|
|
71
|
-
const structuredItems = maybeStructuredOutputs.map(convertStructuredToolOutputToInputItem);
|
|
71
|
+
const structuredItems = maybeStructuredOutputs.map(toolOutputNormalization_1.convertStructuredToolOutputToInputItem);
|
|
72
72
|
return {
|
|
73
73
|
type: 'function_call_result',
|
|
74
74
|
name: toolCall.name,
|
|
@@ -238,10 +238,6 @@ async function buildApprovalRejectionResult(deps, toolRun) {
|
|
|
238
238
|
async function handleFunctionApproval(deps, toolRun, parsedArgs) {
|
|
239
239
|
const { agent, state } = deps;
|
|
240
240
|
const toolName = getFunctionToolIdentity(toolRun);
|
|
241
|
-
const needsApproval = await toolRun.tool.needsApproval(state._context, parsedArgs, toolRun.toolCall.callId);
|
|
242
|
-
if (!needsApproval) {
|
|
243
|
-
return 'approved';
|
|
244
|
-
}
|
|
245
241
|
const approval = state._context.isToolApproved({
|
|
246
242
|
toolName,
|
|
247
243
|
callId: toolRun.toolCall.callId,
|
|
@@ -250,24 +246,28 @@ async function handleFunctionApproval(deps, toolRun, parsedArgs) {
|
|
|
250
246
|
state.clearPendingAgentToolRun(toolName, toolRun.toolCall.callId);
|
|
251
247
|
return await buildApprovalRejectionResult(deps, toolRun);
|
|
252
248
|
}
|
|
253
|
-
if (approval
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
249
|
+
if (approval === true) {
|
|
250
|
+
return 'approved';
|
|
251
|
+
}
|
|
252
|
+
const needsApproval = await toolRun.tool.needsApproval(state._context, parsedArgs, toolRun.toolCall.callId);
|
|
253
|
+
if (!needsApproval) {
|
|
254
|
+
return 'approved';
|
|
255
|
+
}
|
|
256
|
+
if (shouldRunPreApprovalInputGuardrails(deps)) {
|
|
257
|
+
const inputGuardrailResult = await runFunctionToolInputGuardrails({
|
|
258
|
+
guardrails: toolRun.tool.inputGuardrails,
|
|
259
|
+
context: state._context,
|
|
260
|
+
agent,
|
|
261
|
+
toolCall: toolRun.toolCall,
|
|
262
|
+
onResult: (result) => {
|
|
263
|
+
state._toolInputGuardrailResults.push(result);
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
if (inputGuardrailResult.type === 'reject') {
|
|
267
|
+
return buildInputGuardrailRejectionResult(deps, toolRun, inputGuardrailResult.message);
|
|
267
268
|
}
|
|
268
|
-
return buildApprovalRequestResult(deps, toolRun);
|
|
269
269
|
}
|
|
270
|
-
return
|
|
270
|
+
return buildApprovalRequestResult(deps, toolRun);
|
|
271
271
|
}
|
|
272
272
|
function buildInputGuardrailRejectionResult(deps, toolRun, message) {
|
|
273
273
|
return {
|
|
@@ -459,7 +459,17 @@ async function withToolFunctionSpan(runner, toolName, fn) {
|
|
|
459
459
|
}
|
|
460
460
|
async function resolveToolApproval(options) {
|
|
461
461
|
const { runContext, toolName, callId, approvalItem, needsApproval, onApproval, } = options;
|
|
462
|
-
|
|
462
|
+
const existingApproval = runContext.isToolApproved({
|
|
463
|
+
toolName,
|
|
464
|
+
callId,
|
|
465
|
+
});
|
|
466
|
+
if (existingApproval === true) {
|
|
467
|
+
return 'approved';
|
|
468
|
+
}
|
|
469
|
+
if (existingApproval === false) {
|
|
470
|
+
return 'rejected';
|
|
471
|
+
}
|
|
472
|
+
if (!(await needsApproval())) {
|
|
463
473
|
return 'approved';
|
|
464
474
|
}
|
|
465
475
|
if (onApproval) {
|
|
@@ -542,7 +552,7 @@ async function executeShellActions(agent, actions, runner, runContext, customLog
|
|
|
542
552
|
toolName: shellTool.name,
|
|
543
553
|
callId: toolCallKey,
|
|
544
554
|
approvalItem,
|
|
545
|
-
needsApproval:
|
|
555
|
+
needsApproval: () => shellTool.needsApproval(runContext, toolCall.action, toolCallKey),
|
|
546
556
|
onApproval: shellTool.onApproval,
|
|
547
557
|
buildRejectionItem: async () => {
|
|
548
558
|
const response = await (0, approvalRejection_1.resolveApprovalRejectionMessage)({
|
|
@@ -642,7 +652,7 @@ async function executeApplyPatchOperations(agent, actions, runner, runContext, c
|
|
|
642
652
|
toolName: applyPatchTool.name,
|
|
643
653
|
callId: toolCallKey,
|
|
644
654
|
approvalItem,
|
|
645
|
-
needsApproval:
|
|
655
|
+
needsApproval: () => applyPatchTool.needsApproval(runContext, toolCall.operation, toolCallKey),
|
|
646
656
|
onApproval: applyPatchTool.onApproval,
|
|
647
657
|
buildRejectionItem: async () => {
|
|
648
658
|
const response = await (0, approvalRejection_1.resolveApprovalRejectionMessage)({
|
|
@@ -762,17 +772,16 @@ async function executeComputerActions(agent, actions, runner, runContext, custom
|
|
|
762
772
|
const approvalItem = new items_1.RunToolApprovalItem(toolCall, agent, computerTool.name);
|
|
763
773
|
const needsApprovalCandidate = computerTool
|
|
764
774
|
.needsApproval;
|
|
765
|
-
const needsApproval = typeof needsApprovalCandidate === 'function'
|
|
766
|
-
? (await Promise.all(computerActions.map((computerAction) => needsApprovalCandidate(runContext, computerAction, toolCall.callId)))).some(Boolean)
|
|
767
|
-
: typeof needsApprovalCandidate === 'boolean'
|
|
768
|
-
? needsApprovalCandidate
|
|
769
|
-
: false;
|
|
770
775
|
const approvalDecision = await handleToolApprovalDecision({
|
|
771
776
|
runContext,
|
|
772
777
|
toolName: computerTool.name,
|
|
773
778
|
callId: toolCall.callId,
|
|
774
779
|
approvalItem,
|
|
775
|
-
needsApproval
|
|
780
|
+
needsApproval: async () => typeof needsApprovalCandidate === 'function'
|
|
781
|
+
? (await Promise.all(computerActions.map((computerAction) => needsApprovalCandidate(runContext, computerAction, toolCall.callId)))).some(Boolean)
|
|
782
|
+
: typeof needsApprovalCandidate === 'boolean'
|
|
783
|
+
? needsApprovalCandidate
|
|
784
|
+
: false,
|
|
776
785
|
buildRejectionItem: async () => {
|
|
777
786
|
const rejectionMessage = await getRejectionMessage();
|
|
778
787
|
const rejectionOutput = {
|
|
@@ -889,6 +898,14 @@ async function executeHandoffCalls(agent, originalInput, preStepItems, newStepIt
|
|
|
889
898
|
const actualHandoff = runHandoffs[0];
|
|
890
899
|
return (0, createSpans_1.withHandoffSpan)(async (handoffSpan) => {
|
|
891
900
|
const handoff = actualHandoff.handoff;
|
|
901
|
+
const inputFilter = handoff.inputFilter ?? runner.config.handoffInputFilter;
|
|
902
|
+
if (inputFilter != null && typeof inputFilter !== 'function') {
|
|
903
|
+
throw Object.assign(new errors_1.UserError('Invalid handoff input filter: not callable'), {
|
|
904
|
+
data: {
|
|
905
|
+
details: 'not callable',
|
|
906
|
+
},
|
|
907
|
+
});
|
|
908
|
+
}
|
|
892
909
|
const newAgent = await handoff.onInvokeHandoff(runContext, actualHandoff.toolCall.arguments);
|
|
893
910
|
handoffSpan.spanData.to_agent = newAgent.name;
|
|
894
911
|
if (runHandoffs.length > 1) {
|
|
@@ -903,17 +920,8 @@ async function executeHandoffCalls(agent, originalInput, preStepItems, newStepIt
|
|
|
903
920
|
newStepItems.push(new items_1.RunHandoffOutputItem(getToolCallOutputItem(actualHandoff.toolCall, (0, handoff_1.getTransferMessage)(newAgent)), agent, newAgent));
|
|
904
921
|
runner.emit('agent_handoff', runContext, agent, newAgent);
|
|
905
922
|
agent.emit('agent_handoff', runContext, newAgent);
|
|
906
|
-
|
|
907
|
-
if (inputFilter) {
|
|
923
|
+
if (inputFilter != null) {
|
|
908
924
|
logger_1.default.debug('Filtering inputs for handoff');
|
|
909
|
-
if (typeof inputFilter !== 'function') {
|
|
910
|
-
handoffSpan.setError({
|
|
911
|
-
message: 'Invalid input filter',
|
|
912
|
-
data: {
|
|
913
|
-
details: 'not callable',
|
|
914
|
-
},
|
|
915
|
-
});
|
|
916
|
-
}
|
|
917
925
|
const handoffInputData = {
|
|
918
926
|
inputHistory: Array.isArray(originalInput)
|
|
919
927
|
? [...originalInput]
|
|
@@ -1019,315 +1027,6 @@ async function checkForFinalOutputFromTools(agent, toolResults, state, additiona
|
|
|
1019
1027
|
}
|
|
1020
1028
|
throw new errors_1.UserError(`Invalid toolUseBehavior: ${toolUseBehavior}`, state);
|
|
1021
1029
|
}
|
|
1022
|
-
/**
|
|
1023
|
-
* Accepts whatever the tool returned and attempts to coerce it into the structured protocol
|
|
1024
|
-
* shapes we expose to downstream model adapters (input_text/input_image/input_file). Tools are
|
|
1025
|
-
* allowed to return either a single structured object or an array of them; anything else falls
|
|
1026
|
-
* back to the legacy string pipeline.
|
|
1027
|
-
*/
|
|
1028
|
-
function normalizeStructuredToolOutputs(output) {
|
|
1029
|
-
if (Array.isArray(output)) {
|
|
1030
|
-
const structured = [];
|
|
1031
|
-
for (const item of output) {
|
|
1032
|
-
const normalized = normalizeStructuredToolOutput(item);
|
|
1033
|
-
if (!normalized) {
|
|
1034
|
-
return null;
|
|
1035
|
-
}
|
|
1036
|
-
structured.push(normalized);
|
|
1037
|
-
}
|
|
1038
|
-
return structured;
|
|
1039
|
-
}
|
|
1040
|
-
const normalized = normalizeStructuredToolOutput(output);
|
|
1041
|
-
return normalized ? [normalized] : null;
|
|
1042
|
-
}
|
|
1043
|
-
/**
|
|
1044
|
-
* Best-effort normalization of a single tool output item. If the object already matches the
|
|
1045
|
-
* protocol shape we simply cast it; otherwise we copy the recognised fields into the canonical
|
|
1046
|
-
* structure. Returning null lets the caller know we should revert to plain-string handling.
|
|
1047
|
-
*/
|
|
1048
|
-
function normalizeStructuredToolOutput(value) {
|
|
1049
|
-
if (!isRecord(value)) {
|
|
1050
|
-
return null;
|
|
1051
|
-
}
|
|
1052
|
-
const type = value.type;
|
|
1053
|
-
if (type === 'text' && typeof value.text === 'string') {
|
|
1054
|
-
const output = { type: 'text', text: value.text };
|
|
1055
|
-
if (isRecord(value.providerData)) {
|
|
1056
|
-
output.providerData = value.providerData;
|
|
1057
|
-
}
|
|
1058
|
-
return output;
|
|
1059
|
-
}
|
|
1060
|
-
if (type === 'image') {
|
|
1061
|
-
const output = { type: 'image' };
|
|
1062
|
-
let imageString;
|
|
1063
|
-
let imageFileId;
|
|
1064
|
-
const fallbackImageMediaType = getImageInlineMediaType(value);
|
|
1065
|
-
const imageField = value.image;
|
|
1066
|
-
if (typeof imageField === 'string' && imageField.length > 0) {
|
|
1067
|
-
imageString = imageField;
|
|
1068
|
-
}
|
|
1069
|
-
else if (isRecord(imageField)) {
|
|
1070
|
-
const imageObj = imageField;
|
|
1071
|
-
const inlineMediaType = getImageInlineMediaType(imageObj) ?? fallbackImageMediaType;
|
|
1072
|
-
if (isNonEmptyString(imageObj.url)) {
|
|
1073
|
-
imageString = imageObj.url;
|
|
1074
|
-
}
|
|
1075
|
-
else if (isNonEmptyString(imageObj.data)) {
|
|
1076
|
-
imageString = toInlineImageString(imageObj.data, inlineMediaType);
|
|
1077
|
-
}
|
|
1078
|
-
else if (imageObj.data instanceof Uint8Array &&
|
|
1079
|
-
imageObj.data.length > 0) {
|
|
1080
|
-
imageString = toInlineImageString(imageObj.data, inlineMediaType);
|
|
1081
|
-
}
|
|
1082
|
-
if (!imageString) {
|
|
1083
|
-
const candidateId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
|
|
1084
|
-
(isNonEmptyString(imageObj.id) && imageObj.id) ||
|
|
1085
|
-
undefined;
|
|
1086
|
-
if (candidateId) {
|
|
1087
|
-
imageFileId = candidateId;
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
if (!imageString &&
|
|
1092
|
-
typeof value.imageUrl === 'string' &&
|
|
1093
|
-
value.imageUrl.length > 0) {
|
|
1094
|
-
imageString = value.imageUrl;
|
|
1095
|
-
}
|
|
1096
|
-
if (!imageFileId &&
|
|
1097
|
-
typeof value.fileId === 'string' &&
|
|
1098
|
-
value.fileId.length > 0) {
|
|
1099
|
-
imageFileId = value.fileId;
|
|
1100
|
-
}
|
|
1101
|
-
if (!imageString &&
|
|
1102
|
-
typeof value.data === 'string' &&
|
|
1103
|
-
value.data.length > 0) {
|
|
1104
|
-
imageString = fallbackImageMediaType
|
|
1105
|
-
? toInlineImageString(value.data, fallbackImageMediaType)
|
|
1106
|
-
: value.data;
|
|
1107
|
-
}
|
|
1108
|
-
else if (!imageString &&
|
|
1109
|
-
value.data instanceof Uint8Array &&
|
|
1110
|
-
value.data.length > 0) {
|
|
1111
|
-
imageString = toInlineImageString(value.data, fallbackImageMediaType);
|
|
1112
|
-
}
|
|
1113
|
-
if (typeof value.detail === 'string' && value.detail.length > 0) {
|
|
1114
|
-
output.detail = value.detail;
|
|
1115
|
-
}
|
|
1116
|
-
if (imageString) {
|
|
1117
|
-
output.image = imageString;
|
|
1118
|
-
}
|
|
1119
|
-
else if (imageFileId) {
|
|
1120
|
-
output.image = { fileId: imageFileId };
|
|
1121
|
-
}
|
|
1122
|
-
else {
|
|
1123
|
-
return null;
|
|
1124
|
-
}
|
|
1125
|
-
if (isRecord(value.providerData)) {
|
|
1126
|
-
output.providerData = value.providerData;
|
|
1127
|
-
}
|
|
1128
|
-
return output;
|
|
1129
|
-
}
|
|
1130
|
-
if (type === 'file') {
|
|
1131
|
-
const fileValue = normalizeFileValue(value);
|
|
1132
|
-
if (!fileValue) {
|
|
1133
|
-
return null;
|
|
1134
|
-
}
|
|
1135
|
-
const output = { type: 'file', file: fileValue };
|
|
1136
|
-
if (isRecord(value.providerData)) {
|
|
1137
|
-
output.providerData = value.providerData;
|
|
1138
|
-
}
|
|
1139
|
-
return output;
|
|
1140
|
-
}
|
|
1141
|
-
return null;
|
|
1142
|
-
}
|
|
1143
|
-
/**
|
|
1144
|
-
* Translates the normalized tool output into the protocol `input_*` items. This is the last hop
|
|
1145
|
-
* before we hand the data to model-specific adapters, so we generate the exact schema expected by
|
|
1146
|
-
* the protocol definitions.
|
|
1147
|
-
*/
|
|
1148
|
-
function convertStructuredToolOutputToInputItem(output) {
|
|
1149
|
-
if (output.type === 'text') {
|
|
1150
|
-
const result = {
|
|
1151
|
-
type: 'input_text',
|
|
1152
|
-
text: output.text,
|
|
1153
|
-
};
|
|
1154
|
-
if (output.providerData) {
|
|
1155
|
-
result.providerData = output.providerData;
|
|
1156
|
-
}
|
|
1157
|
-
return result;
|
|
1158
|
-
}
|
|
1159
|
-
if (output.type === 'image') {
|
|
1160
|
-
const result = { type: 'input_image' };
|
|
1161
|
-
if (typeof output.detail === 'string' && output.detail.length > 0) {
|
|
1162
|
-
result.detail = output.detail;
|
|
1163
|
-
}
|
|
1164
|
-
if (typeof output.image === 'string' && output.image.length > 0) {
|
|
1165
|
-
result.image = output.image;
|
|
1166
|
-
}
|
|
1167
|
-
else if (isRecord(output.image)) {
|
|
1168
|
-
const imageObj = output.image;
|
|
1169
|
-
const inlineMediaType = getImageInlineMediaType(imageObj);
|
|
1170
|
-
if (isNonEmptyString(imageObj.url)) {
|
|
1171
|
-
result.image = imageObj.url;
|
|
1172
|
-
}
|
|
1173
|
-
else if (isNonEmptyString(imageObj.data)) {
|
|
1174
|
-
result.image =
|
|
1175
|
-
inlineMediaType && !imageObj.data.startsWith('data:')
|
|
1176
|
-
? asDataUrl(imageObj.data, inlineMediaType)
|
|
1177
|
-
: imageObj.data;
|
|
1178
|
-
}
|
|
1179
|
-
else if (imageObj.data instanceof Uint8Array &&
|
|
1180
|
-
imageObj.data.length > 0) {
|
|
1181
|
-
const base64 = (0, base64_1.encodeUint8ArrayToBase64)(imageObj.data);
|
|
1182
|
-
result.image = asDataUrl(base64, inlineMediaType);
|
|
1183
|
-
}
|
|
1184
|
-
else {
|
|
1185
|
-
const referencedId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
|
|
1186
|
-
(isNonEmptyString(imageObj.id) && imageObj.id) ||
|
|
1187
|
-
undefined;
|
|
1188
|
-
if (referencedId) {
|
|
1189
|
-
result.image = { id: referencedId };
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
}
|
|
1193
|
-
if (output.providerData) {
|
|
1194
|
-
result.providerData = output.providerData;
|
|
1195
|
-
}
|
|
1196
|
-
return result;
|
|
1197
|
-
}
|
|
1198
|
-
if (output.type === 'file') {
|
|
1199
|
-
const result = { type: 'input_file' };
|
|
1200
|
-
const fileValue = output.file;
|
|
1201
|
-
if (typeof fileValue === 'string') {
|
|
1202
|
-
result.file = fileValue;
|
|
1203
|
-
}
|
|
1204
|
-
else if (fileValue && typeof fileValue === 'object') {
|
|
1205
|
-
const record = fileValue;
|
|
1206
|
-
if ('data' in record && record.data) {
|
|
1207
|
-
const mediaType = record.mediaType ?? 'text/plain';
|
|
1208
|
-
if (typeof record.data === 'string') {
|
|
1209
|
-
result.file = asDataUrl(record.data, mediaType);
|
|
1210
|
-
}
|
|
1211
|
-
else {
|
|
1212
|
-
const base64 = (0, base64_1.encodeUint8ArrayToBase64)(record.data);
|
|
1213
|
-
result.file = asDataUrl(base64, mediaType);
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
else if (typeof record.url === 'string' && record.url.length > 0) {
|
|
1217
|
-
result.file = { url: record.url };
|
|
1218
|
-
}
|
|
1219
|
-
else {
|
|
1220
|
-
const referencedId = (typeof record.id === 'string' &&
|
|
1221
|
-
record.id.length > 0 &&
|
|
1222
|
-
record.id) ||
|
|
1223
|
-
(typeof record.fileId === 'string' && record.fileId.length > 0
|
|
1224
|
-
? record.fileId
|
|
1225
|
-
: undefined);
|
|
1226
|
-
if (referencedId) {
|
|
1227
|
-
result.file = { id: referencedId };
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
if (typeof record.filename === 'string' && record.filename.length > 0) {
|
|
1231
|
-
result.filename = record.filename;
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
if (output.providerData) {
|
|
1235
|
-
result.providerData = output.providerData;
|
|
1236
|
-
}
|
|
1237
|
-
return result;
|
|
1238
|
-
}
|
|
1239
|
-
const exhaustiveCheck = output;
|
|
1240
|
-
return exhaustiveCheck;
|
|
1241
|
-
}
|
|
1242
|
-
function normalizeFileValue(value) {
|
|
1243
|
-
const directFile = value.file;
|
|
1244
|
-
if (typeof directFile === 'string' && directFile.length > 0) {
|
|
1245
|
-
return directFile;
|
|
1246
|
-
}
|
|
1247
|
-
const normalizedObject = normalizeFileObjectCandidate(directFile);
|
|
1248
|
-
if (normalizedObject) {
|
|
1249
|
-
return normalizedObject;
|
|
1250
|
-
}
|
|
1251
|
-
const legacyValue = normalizeLegacyFileValue(value);
|
|
1252
|
-
if (legacyValue) {
|
|
1253
|
-
return legacyValue;
|
|
1254
|
-
}
|
|
1255
|
-
return null;
|
|
1256
|
-
}
|
|
1257
|
-
function normalizeFileObjectCandidate(value) {
|
|
1258
|
-
if (!isRecord(value)) {
|
|
1259
|
-
return null;
|
|
1260
|
-
}
|
|
1261
|
-
if ('data' in value && value.data !== undefined) {
|
|
1262
|
-
const dataValue = value.data;
|
|
1263
|
-
const hasStringData = typeof dataValue === 'string' && dataValue.length > 0;
|
|
1264
|
-
const hasBinaryData = dataValue instanceof Uint8Array && dataValue.length > 0;
|
|
1265
|
-
if (!hasStringData && !hasBinaryData) {
|
|
1266
|
-
return null;
|
|
1267
|
-
}
|
|
1268
|
-
if (!isNonEmptyString(value.mediaType) ||
|
|
1269
|
-
!isNonEmptyString(value.filename)) {
|
|
1270
|
-
return null;
|
|
1271
|
-
}
|
|
1272
|
-
return {
|
|
1273
|
-
data: typeof dataValue === 'string' ? dataValue : new Uint8Array(dataValue),
|
|
1274
|
-
mediaType: value.mediaType,
|
|
1275
|
-
filename: value.filename,
|
|
1276
|
-
};
|
|
1277
|
-
}
|
|
1278
|
-
if (isNonEmptyString(value.url)) {
|
|
1279
|
-
const result = { url: value.url };
|
|
1280
|
-
if (isNonEmptyString(value.filename)) {
|
|
1281
|
-
result.filename = value.filename;
|
|
1282
|
-
}
|
|
1283
|
-
return result;
|
|
1284
|
-
}
|
|
1285
|
-
const referencedId = (isNonEmptyString(value.id) && value.id) ||
|
|
1286
|
-
(isNonEmptyString(value.fileId) && value.fileId);
|
|
1287
|
-
if (referencedId) {
|
|
1288
|
-
const result = { id: referencedId };
|
|
1289
|
-
if (isNonEmptyString(value.filename)) {
|
|
1290
|
-
result.filename = value.filename;
|
|
1291
|
-
}
|
|
1292
|
-
return result;
|
|
1293
|
-
}
|
|
1294
|
-
return null;
|
|
1295
|
-
}
|
|
1296
|
-
function normalizeLegacyFileValue(value) {
|
|
1297
|
-
const filename = typeof value.filename === 'string' && value.filename.length > 0
|
|
1298
|
-
? value.filename
|
|
1299
|
-
: undefined;
|
|
1300
|
-
const mediaType = typeof value.mediaType === 'string' && value.mediaType.length > 0
|
|
1301
|
-
? value.mediaType
|
|
1302
|
-
: undefined;
|
|
1303
|
-
if (typeof value.fileData === 'string' && value.fileData.length > 0) {
|
|
1304
|
-
if (!mediaType || !filename) {
|
|
1305
|
-
return null;
|
|
1306
|
-
}
|
|
1307
|
-
return { data: value.fileData, mediaType, filename };
|
|
1308
|
-
}
|
|
1309
|
-
if (value.fileData instanceof Uint8Array && value.fileData.length > 0) {
|
|
1310
|
-
if (!mediaType || !filename) {
|
|
1311
|
-
return null;
|
|
1312
|
-
}
|
|
1313
|
-
return { data: new Uint8Array(value.fileData), mediaType, filename };
|
|
1314
|
-
}
|
|
1315
|
-
if (typeof value.fileUrl === 'string' && value.fileUrl.length > 0) {
|
|
1316
|
-
const result = { url: value.fileUrl };
|
|
1317
|
-
if (filename) {
|
|
1318
|
-
result.filename = filename;
|
|
1319
|
-
}
|
|
1320
|
-
return result;
|
|
1321
|
-
}
|
|
1322
|
-
if (typeof value.fileId === 'string' && value.fileId.length > 0) {
|
|
1323
|
-
const result = { id: value.fileId };
|
|
1324
|
-
if (filename) {
|
|
1325
|
-
result.filename = filename;
|
|
1326
|
-
}
|
|
1327
|
-
return result;
|
|
1328
|
-
}
|
|
1329
|
-
return null;
|
|
1330
|
-
}
|
|
1331
1030
|
function normalizeSafetyChecks(checks) {
|
|
1332
1031
|
if (!Array.isArray(checks)) {
|
|
1333
1032
|
return undefined;
|
|
@@ -1405,26 +1104,4 @@ function isRecord(value) {
|
|
|
1405
1104
|
function isNonEmptyString(value) {
|
|
1406
1105
|
return typeof value === 'string' && value.length > 0;
|
|
1407
1106
|
}
|
|
1408
|
-
function getImageInlineMediaType(value) {
|
|
1409
|
-
if (isNonEmptyString(value.mediaType)) {
|
|
1410
|
-
return value.mediaType;
|
|
1411
|
-
}
|
|
1412
|
-
if (isNonEmptyString(value.mimeType)) {
|
|
1413
|
-
return value.mimeType;
|
|
1414
|
-
}
|
|
1415
|
-
return undefined;
|
|
1416
|
-
}
|
|
1417
|
-
function toInlineImageString(data, mediaType) {
|
|
1418
|
-
if (typeof data === 'string') {
|
|
1419
|
-
if (mediaType && !data.startsWith('data:')) {
|
|
1420
|
-
return asDataUrl(data, mediaType);
|
|
1421
|
-
}
|
|
1422
|
-
return data;
|
|
1423
|
-
}
|
|
1424
|
-
const base64 = (0, base64_1.encodeUint8ArrayToBase64)(data);
|
|
1425
|
-
return asDataUrl(base64, mediaType);
|
|
1426
|
-
}
|
|
1427
|
-
function asDataUrl(base64, mediaType) {
|
|
1428
|
-
return mediaType ? `data:${mediaType};base64,${base64}` : base64;
|
|
1429
|
-
}
|
|
1430
1107
|
//# sourceMappingURL=toolExecution.js.map
|