@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.
Files changed (134) hide show
  1. package/dist/handoff.d.ts +1 -1
  2. package/dist/handoff.js +2 -2
  3. package/dist/handoff.js.map +1 -1
  4. package/dist/handoff.mjs +2 -2
  5. package/dist/handoff.mjs.map +1 -1
  6. package/dist/metadata.js +3 -3
  7. package/dist/metadata.mjs +3 -3
  8. package/dist/run.d.ts +2 -13
  9. package/dist/run.js +15 -40
  10. package/dist/run.js.map +1 -1
  11. package/dist/run.mjs +14 -39
  12. package/dist/run.mjs.map +1 -1
  13. package/dist/runState.d.ts +1 -1
  14. package/dist/runState.js +7 -184
  15. package/dist/runState.js.map +1 -1
  16. package/dist/runState.mjs +2 -179
  17. package/dist/runState.mjs.map +1 -1
  18. package/dist/runStateIdentity.d.ts +6 -0
  19. package/dist/runStateIdentity.js +171 -0
  20. package/dist/runStateIdentity.js.map +1 -0
  21. package/dist/runStateIdentity.mjs +168 -0
  22. package/dist/runStateIdentity.mjs.map +1 -0
  23. package/dist/runner/conversation.js +46 -4
  24. package/dist/runner/conversation.js.map +1 -1
  25. package/dist/runner/conversation.mjs +46 -4
  26. package/dist/runner/conversation.mjs.map +1 -1
  27. package/dist/runner/errorHandlers.d.ts +7 -4
  28. package/dist/runner/errorHandlers.js +31 -10
  29. package/dist/runner/errorHandlers.js.map +1 -1
  30. package/dist/runner/errorHandlers.mjs +31 -10
  31. package/dist/runner/errorHandlers.mjs.map +1 -1
  32. package/dist/runner/guardrails.d.ts +4 -2
  33. package/dist/runner/guardrails.js +23 -20
  34. package/dist/runner/guardrails.js.map +1 -1
  35. package/dist/runner/guardrails.mjs +23 -20
  36. package/dist/runner/guardrails.mjs.map +1 -1
  37. package/dist/runner/items.js +3 -8
  38. package/dist/runner/items.js.map +1 -1
  39. package/dist/runner/items.mjs +3 -8
  40. package/dist/runner/items.mjs.map +1 -1
  41. package/dist/runner/modelSettings.d.ts +1 -1
  42. package/dist/runner/modelSettings.js +2 -2
  43. package/dist/runner/modelSettings.js.map +1 -1
  44. package/dist/runner/modelSettings.mjs +2 -2
  45. package/dist/runner/modelSettings.mjs.map +1 -1
  46. package/dist/runner/runConfig.d.ts +16 -0
  47. package/dist/runner/runConfig.js +31 -0
  48. package/dist/runner/runConfig.js.map +1 -0
  49. package/dist/runner/runConfig.mjs +27 -0
  50. package/dist/runner/runConfig.mjs.map +1 -0
  51. package/dist/runner/runLoop.js +1 -1
  52. package/dist/runner/runLoop.js.map +1 -1
  53. package/dist/runner/runLoop.mjs +1 -1
  54. package/dist/runner/runLoop.mjs.map +1 -1
  55. package/dist/runner/steps.d.ts +3 -1
  56. package/dist/runner/steps.js +3 -1
  57. package/dist/runner/steps.js.map +1 -1
  58. package/dist/runner/steps.mjs +3 -1
  59. package/dist/runner/steps.mjs.map +1 -1
  60. package/dist/runner/toolExecution.js +50 -373
  61. package/dist/runner/toolExecution.js.map +1 -1
  62. package/dist/runner/toolExecution.mjs +48 -371
  63. package/dist/runner/toolExecution.mjs.map +1 -1
  64. package/dist/runner/toolOutputNormalization.d.ts +16 -0
  65. package/dist/runner/toolOutputNormalization.js +328 -0
  66. package/dist/runner/toolOutputNormalization.js.map +1 -0
  67. package/dist/runner/toolOutputNormalization.mjs +324 -0
  68. package/dist/runner/toolOutputNormalization.mjs.map +1 -0
  69. package/dist/runner/toolResultCorrelation.d.ts +22 -0
  70. package/dist/runner/toolResultCorrelation.js +138 -0
  71. package/dist/runner/toolResultCorrelation.js.map +1 -0
  72. package/dist/runner/toolResultCorrelation.mjs +130 -0
  73. package/dist/runner/toolResultCorrelation.mjs.map +1 -0
  74. package/dist/runner/turnPreparation.js +1 -5
  75. package/dist/runner/turnPreparation.js.map +1 -1
  76. package/dist/runner/turnPreparation.mjs +1 -5
  77. package/dist/runner/turnPreparation.mjs.map +1 -1
  78. package/dist/runner/turnResolution.js +66 -15
  79. package/dist/runner/turnResolution.js.map +1 -1
  80. package/dist/runner/turnResolution.mjs +67 -16
  81. package/dist/runner/turnResolution.mjs.map +1 -1
  82. package/dist/runner/types.d.ts +1 -1
  83. package/dist/sandbox/internal/process.d.ts +1 -0
  84. package/dist/sandbox/internal/process.js +7 -0
  85. package/dist/sandbox/internal/process.js.map +1 -0
  86. package/dist/sandbox/internal/process.mjs +2 -0
  87. package/dist/sandbox/internal/process.mjs.map +1 -0
  88. package/dist/sandbox/internal.d.ts +2 -0
  89. package/dist/sandbox/internal.js +15 -1
  90. package/dist/sandbox/internal.js.map +1 -1
  91. package/dist/sandbox/internal.mjs +2 -0
  92. package/dist/sandbox/internal.mjs.map +1 -1
  93. package/dist/sandbox/permissions.js +7 -6
  94. package/dist/sandbox/permissions.js.map +1 -1
  95. package/dist/sandbox/permissions.mjs +7 -6
  96. package/dist/sandbox/permissions.mjs.map +1 -1
  97. package/dist/sandbox/runtime/agentPreparation.js +11 -1
  98. package/dist/sandbox/runtime/agentPreparation.js.map +1 -1
  99. package/dist/sandbox/runtime/agentPreparation.mjs +12 -2
  100. package/dist/sandbox/runtime/agentPreparation.mjs.map +1 -1
  101. package/dist/sandbox/runtime/prompts.d.ts +1 -1
  102. package/dist/sandbox/runtime/prompts.js +36 -4
  103. package/dist/sandbox/runtime/prompts.js.map +1 -1
  104. package/dist/sandbox/runtime/prompts.mjs +36 -4
  105. package/dist/sandbox/runtime/prompts.mjs.map +1 -1
  106. package/dist/sandbox/sandboxes/docker.js +17 -3
  107. package/dist/sandbox/sandboxes/docker.js.map +1 -1
  108. package/dist/sandbox/sandboxes/docker.mjs +17 -3
  109. package/dist/sandbox/sandboxes/docker.mjs.map +1 -1
  110. package/dist/sandbox/sandboxes/unixLocal.js +9 -0
  111. package/dist/sandbox/sandboxes/unixLocal.js.map +1 -1
  112. package/dist/sandbox/sandboxes/unixLocal.mjs +9 -0
  113. package/dist/sandbox/sandboxes/unixLocal.mjs.map +1 -1
  114. package/dist/sandbox/shared/credentials.d.ts +9 -0
  115. package/dist/sandbox/shared/credentials.js +10 -0
  116. package/dist/sandbox/shared/credentials.js.map +1 -0
  117. package/dist/sandbox/shared/credentials.mjs +7 -0
  118. package/dist/sandbox/shared/credentials.mjs.map +1 -0
  119. package/dist/sandbox/shared/typeGuards.d.ts +6 -1
  120. package/dist/sandbox/shared/typeGuards.js +23 -1
  121. package/dist/sandbox/shared/typeGuards.js.map +1 -1
  122. package/dist/sandbox/shared/typeGuards.mjs +18 -1
  123. package/dist/sandbox/shared/typeGuards.mjs.map +1 -1
  124. package/dist/utils/inlineData.d.ts +2 -0
  125. package/dist/utils/inlineData.js +22 -0
  126. package/dist/utils/inlineData.js.map +1 -0
  127. package/dist/utils/inlineData.mjs +18 -0
  128. package/dist/utils/inlineData.mjs.map +1 -0
  129. package/dist/utils/internal.d.ts +1 -0
  130. package/dist/utils/internal.js +7 -0
  131. package/dist/utils/internal.js.map +1 -0
  132. package/dist/utils/internal.mjs +2 -0
  133. package/dist/utils/internal.mjs.map +1 -0
  134. package/package.json +22 -3
@@ -6,12 +6,12 @@ import { RunHandoffCallItem, RunHandoffOutputItem, RunMessageOutputItem, RunTool
6
6
  import { assistant } from "../helpers/message.mjs";
7
7
  import logger from "../logger.mjs";
8
8
  import { FUNCTION_TOOL_PARSED_INPUT_CALLBACK, invokeFunctionTool, resolveComputer, } from "../tool.mjs";
9
- import { encodeUint8ArrayToBase64 } from "../utils/base64.mjs";
10
9
  import { toSmartString } from "../utils/smartString.mjs";
11
10
  import { isZodObject } from "../utils/index.mjs";
12
11
  import { withFunctionSpan, withHandoffSpan } from "../tracing/createSpans.mjs";
13
12
  import { getCurrentTrace } from "../tracing/context.mjs";
14
13
  import { getFunctionToolQualifiedName, matchesFunctionToolName, } from "../toolIdentity.mjs";
14
+ import { convertStructuredToolOutputToInputItem, normalizeStructuredToolOutputs, } from "./toolOutputNormalization.mjs";
15
15
  import { runToolInputGuardrails, runToolOutputGuardrails, } from "../utils/toolGuardrails.mjs";
16
16
  import { maybeExtractToolOutputCustomData } from "../utils/customData.mjs";
17
17
  import { resolveApprovalRejectionMessage, TOOL_APPROVAL_REJECTION_MESSAGE, } from "./approvalRejection.mjs";
@@ -225,10 +225,6 @@ async function buildApprovalRejectionResult(deps, toolRun) {
225
225
  async function handleFunctionApproval(deps, toolRun, parsedArgs) {
226
226
  const { agent, state } = deps;
227
227
  const toolName = getFunctionToolIdentity(toolRun);
228
- const needsApproval = await toolRun.tool.needsApproval(state._context, parsedArgs, toolRun.toolCall.callId);
229
- if (!needsApproval) {
230
- return 'approved';
231
- }
232
228
  const approval = state._context.isToolApproved({
233
229
  toolName,
234
230
  callId: toolRun.toolCall.callId,
@@ -237,24 +233,28 @@ async function handleFunctionApproval(deps, toolRun, parsedArgs) {
237
233
  state.clearPendingAgentToolRun(toolName, toolRun.toolCall.callId);
238
234
  return await buildApprovalRejectionResult(deps, toolRun);
239
235
  }
240
- if (approval !== true) {
241
- if (shouldRunPreApprovalInputGuardrails(deps)) {
242
- const inputGuardrailResult = await runFunctionToolInputGuardrails({
243
- guardrails: toolRun.tool.inputGuardrails,
244
- context: state._context,
245
- agent,
246
- toolCall: toolRun.toolCall,
247
- onResult: (result) => {
248
- state._toolInputGuardrailResults.push(result);
249
- },
250
- });
251
- if (inputGuardrailResult.type === 'reject') {
252
- return buildInputGuardrailRejectionResult(deps, toolRun, inputGuardrailResult.message);
253
- }
236
+ if (approval === true) {
237
+ return 'approved';
238
+ }
239
+ const needsApproval = await toolRun.tool.needsApproval(state._context, parsedArgs, toolRun.toolCall.callId);
240
+ if (!needsApproval) {
241
+ return 'approved';
242
+ }
243
+ if (shouldRunPreApprovalInputGuardrails(deps)) {
244
+ const inputGuardrailResult = await runFunctionToolInputGuardrails({
245
+ guardrails: toolRun.tool.inputGuardrails,
246
+ context: state._context,
247
+ agent,
248
+ toolCall: toolRun.toolCall,
249
+ onResult: (result) => {
250
+ state._toolInputGuardrailResults.push(result);
251
+ },
252
+ });
253
+ if (inputGuardrailResult.type === 'reject') {
254
+ return buildInputGuardrailRejectionResult(deps, toolRun, inputGuardrailResult.message);
254
255
  }
255
- return buildApprovalRequestResult(deps, toolRun);
256
256
  }
257
- return 'approved';
257
+ return buildApprovalRequestResult(deps, toolRun);
258
258
  }
259
259
  function buildInputGuardrailRejectionResult(deps, toolRun, message) {
260
260
  return {
@@ -446,7 +446,17 @@ async function withToolFunctionSpan(runner, toolName, fn) {
446
446
  }
447
447
  async function resolveToolApproval(options) {
448
448
  const { runContext, toolName, callId, approvalItem, needsApproval, onApproval, } = options;
449
- if (!needsApproval) {
449
+ const existingApproval = runContext.isToolApproved({
450
+ toolName,
451
+ callId,
452
+ });
453
+ if (existingApproval === true) {
454
+ return 'approved';
455
+ }
456
+ if (existingApproval === false) {
457
+ return 'rejected';
458
+ }
459
+ if (!(await needsApproval())) {
450
460
  return 'approved';
451
461
  }
452
462
  if (onApproval) {
@@ -529,7 +539,7 @@ export async function executeShellActions(agent, actions, runner, runContext, cu
529
539
  toolName: shellTool.name,
530
540
  callId: toolCallKey,
531
541
  approvalItem,
532
- needsApproval: await shellTool.needsApproval(runContext, toolCall.action, toolCallKey),
542
+ needsApproval: () => shellTool.needsApproval(runContext, toolCall.action, toolCallKey),
533
543
  onApproval: shellTool.onApproval,
534
544
  buildRejectionItem: async () => {
535
545
  const response = await resolveApprovalRejectionMessage({
@@ -629,7 +639,7 @@ export async function executeApplyPatchOperations(agent, actions, runner, runCon
629
639
  toolName: applyPatchTool.name,
630
640
  callId: toolCallKey,
631
641
  approvalItem,
632
- needsApproval: await applyPatchTool.needsApproval(runContext, toolCall.operation, toolCallKey),
642
+ needsApproval: () => applyPatchTool.needsApproval(runContext, toolCall.operation, toolCallKey),
633
643
  onApproval: applyPatchTool.onApproval,
634
644
  buildRejectionItem: async () => {
635
645
  const response = await resolveApprovalRejectionMessage({
@@ -749,17 +759,16 @@ export async function executeComputerActions(agent, actions, runner, runContext,
749
759
  const approvalItem = new RunToolApprovalItem(toolCall, agent, computerTool.name);
750
760
  const needsApprovalCandidate = computerTool
751
761
  .needsApproval;
752
- const needsApproval = typeof needsApprovalCandidate === 'function'
753
- ? (await Promise.all(computerActions.map((computerAction) => needsApprovalCandidate(runContext, computerAction, toolCall.callId)))).some(Boolean)
754
- : typeof needsApprovalCandidate === 'boolean'
755
- ? needsApprovalCandidate
756
- : false;
757
762
  const approvalDecision = await handleToolApprovalDecision({
758
763
  runContext,
759
764
  toolName: computerTool.name,
760
765
  callId: toolCall.callId,
761
766
  approvalItem,
762
- needsApproval,
767
+ needsApproval: async () => typeof needsApprovalCandidate === 'function'
768
+ ? (await Promise.all(computerActions.map((computerAction) => needsApprovalCandidate(runContext, computerAction, toolCall.callId)))).some(Boolean)
769
+ : typeof needsApprovalCandidate === 'boolean'
770
+ ? needsApprovalCandidate
771
+ : false,
763
772
  buildRejectionItem: async () => {
764
773
  const rejectionMessage = await getRejectionMessage();
765
774
  const rejectionOutput = {
@@ -876,6 +885,14 @@ export async function executeHandoffCalls(agent, originalInput, preStepItems, ne
876
885
  const actualHandoff = runHandoffs[0];
877
886
  return withHandoffSpan(async (handoffSpan) => {
878
887
  const handoff = actualHandoff.handoff;
888
+ const inputFilter = handoff.inputFilter ?? runner.config.handoffInputFilter;
889
+ if (inputFilter != null && typeof inputFilter !== 'function') {
890
+ throw Object.assign(new UserError('Invalid handoff input filter: not callable'), {
891
+ data: {
892
+ details: 'not callable',
893
+ },
894
+ });
895
+ }
879
896
  const newAgent = await handoff.onInvokeHandoff(runContext, actualHandoff.toolCall.arguments);
880
897
  handoffSpan.spanData.to_agent = newAgent.name;
881
898
  if (runHandoffs.length > 1) {
@@ -890,17 +907,8 @@ export async function executeHandoffCalls(agent, originalInput, preStepItems, ne
890
907
  newStepItems.push(new RunHandoffOutputItem(getToolCallOutputItem(actualHandoff.toolCall, getTransferMessage(newAgent)), agent, newAgent));
891
908
  runner.emit('agent_handoff', runContext, agent, newAgent);
892
909
  agent.emit('agent_handoff', runContext, newAgent);
893
- const inputFilter = handoff.inputFilter ?? runner.config.handoffInputFilter;
894
- if (inputFilter) {
910
+ if (inputFilter != null) {
895
911
  logger.debug('Filtering inputs for handoff');
896
- if (typeof inputFilter !== 'function') {
897
- handoffSpan.setError({
898
- message: 'Invalid input filter',
899
- data: {
900
- details: 'not callable',
901
- },
902
- });
903
- }
904
912
  const handoffInputData = {
905
913
  inputHistory: Array.isArray(originalInput)
906
914
  ? [...originalInput]
@@ -1006,315 +1014,6 @@ export async function checkForFinalOutputFromTools(agent, toolResults, state, ad
1006
1014
  }
1007
1015
  throw new UserError(`Invalid toolUseBehavior: ${toolUseBehavior}`, state);
1008
1016
  }
1009
- /**
1010
- * Accepts whatever the tool returned and attempts to coerce it into the structured protocol
1011
- * shapes we expose to downstream model adapters (input_text/input_image/input_file). Tools are
1012
- * allowed to return either a single structured object or an array of them; anything else falls
1013
- * back to the legacy string pipeline.
1014
- */
1015
- function normalizeStructuredToolOutputs(output) {
1016
- if (Array.isArray(output)) {
1017
- const structured = [];
1018
- for (const item of output) {
1019
- const normalized = normalizeStructuredToolOutput(item);
1020
- if (!normalized) {
1021
- return null;
1022
- }
1023
- structured.push(normalized);
1024
- }
1025
- return structured;
1026
- }
1027
- const normalized = normalizeStructuredToolOutput(output);
1028
- return normalized ? [normalized] : null;
1029
- }
1030
- /**
1031
- * Best-effort normalization of a single tool output item. If the object already matches the
1032
- * protocol shape we simply cast it; otherwise we copy the recognised fields into the canonical
1033
- * structure. Returning null lets the caller know we should revert to plain-string handling.
1034
- */
1035
- function normalizeStructuredToolOutput(value) {
1036
- if (!isRecord(value)) {
1037
- return null;
1038
- }
1039
- const type = value.type;
1040
- if (type === 'text' && typeof value.text === 'string') {
1041
- const output = { type: 'text', text: value.text };
1042
- if (isRecord(value.providerData)) {
1043
- output.providerData = value.providerData;
1044
- }
1045
- return output;
1046
- }
1047
- if (type === 'image') {
1048
- const output = { type: 'image' };
1049
- let imageString;
1050
- let imageFileId;
1051
- const fallbackImageMediaType = getImageInlineMediaType(value);
1052
- const imageField = value.image;
1053
- if (typeof imageField === 'string' && imageField.length > 0) {
1054
- imageString = imageField;
1055
- }
1056
- else if (isRecord(imageField)) {
1057
- const imageObj = imageField;
1058
- const inlineMediaType = getImageInlineMediaType(imageObj) ?? fallbackImageMediaType;
1059
- if (isNonEmptyString(imageObj.url)) {
1060
- imageString = imageObj.url;
1061
- }
1062
- else if (isNonEmptyString(imageObj.data)) {
1063
- imageString = toInlineImageString(imageObj.data, inlineMediaType);
1064
- }
1065
- else if (imageObj.data instanceof Uint8Array &&
1066
- imageObj.data.length > 0) {
1067
- imageString = toInlineImageString(imageObj.data, inlineMediaType);
1068
- }
1069
- if (!imageString) {
1070
- const candidateId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
1071
- (isNonEmptyString(imageObj.id) && imageObj.id) ||
1072
- undefined;
1073
- if (candidateId) {
1074
- imageFileId = candidateId;
1075
- }
1076
- }
1077
- }
1078
- if (!imageString &&
1079
- typeof value.imageUrl === 'string' &&
1080
- value.imageUrl.length > 0) {
1081
- imageString = value.imageUrl;
1082
- }
1083
- if (!imageFileId &&
1084
- typeof value.fileId === 'string' &&
1085
- value.fileId.length > 0) {
1086
- imageFileId = value.fileId;
1087
- }
1088
- if (!imageString &&
1089
- typeof value.data === 'string' &&
1090
- value.data.length > 0) {
1091
- imageString = fallbackImageMediaType
1092
- ? toInlineImageString(value.data, fallbackImageMediaType)
1093
- : value.data;
1094
- }
1095
- else if (!imageString &&
1096
- value.data instanceof Uint8Array &&
1097
- value.data.length > 0) {
1098
- imageString = toInlineImageString(value.data, fallbackImageMediaType);
1099
- }
1100
- if (typeof value.detail === 'string' && value.detail.length > 0) {
1101
- output.detail = value.detail;
1102
- }
1103
- if (imageString) {
1104
- output.image = imageString;
1105
- }
1106
- else if (imageFileId) {
1107
- output.image = { fileId: imageFileId };
1108
- }
1109
- else {
1110
- return null;
1111
- }
1112
- if (isRecord(value.providerData)) {
1113
- output.providerData = value.providerData;
1114
- }
1115
- return output;
1116
- }
1117
- if (type === 'file') {
1118
- const fileValue = normalizeFileValue(value);
1119
- if (!fileValue) {
1120
- return null;
1121
- }
1122
- const output = { type: 'file', file: fileValue };
1123
- if (isRecord(value.providerData)) {
1124
- output.providerData = value.providerData;
1125
- }
1126
- return output;
1127
- }
1128
- return null;
1129
- }
1130
- /**
1131
- * Translates the normalized tool output into the protocol `input_*` items. This is the last hop
1132
- * before we hand the data to model-specific adapters, so we generate the exact schema expected by
1133
- * the protocol definitions.
1134
- */
1135
- function convertStructuredToolOutputToInputItem(output) {
1136
- if (output.type === 'text') {
1137
- const result = {
1138
- type: 'input_text',
1139
- text: output.text,
1140
- };
1141
- if (output.providerData) {
1142
- result.providerData = output.providerData;
1143
- }
1144
- return result;
1145
- }
1146
- if (output.type === 'image') {
1147
- const result = { type: 'input_image' };
1148
- if (typeof output.detail === 'string' && output.detail.length > 0) {
1149
- result.detail = output.detail;
1150
- }
1151
- if (typeof output.image === 'string' && output.image.length > 0) {
1152
- result.image = output.image;
1153
- }
1154
- else if (isRecord(output.image)) {
1155
- const imageObj = output.image;
1156
- const inlineMediaType = getImageInlineMediaType(imageObj);
1157
- if (isNonEmptyString(imageObj.url)) {
1158
- result.image = imageObj.url;
1159
- }
1160
- else if (isNonEmptyString(imageObj.data)) {
1161
- result.image =
1162
- inlineMediaType && !imageObj.data.startsWith('data:')
1163
- ? asDataUrl(imageObj.data, inlineMediaType)
1164
- : imageObj.data;
1165
- }
1166
- else if (imageObj.data instanceof Uint8Array &&
1167
- imageObj.data.length > 0) {
1168
- const base64 = encodeUint8ArrayToBase64(imageObj.data);
1169
- result.image = asDataUrl(base64, inlineMediaType);
1170
- }
1171
- else {
1172
- const referencedId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
1173
- (isNonEmptyString(imageObj.id) && imageObj.id) ||
1174
- undefined;
1175
- if (referencedId) {
1176
- result.image = { id: referencedId };
1177
- }
1178
- }
1179
- }
1180
- if (output.providerData) {
1181
- result.providerData = output.providerData;
1182
- }
1183
- return result;
1184
- }
1185
- if (output.type === 'file') {
1186
- const result = { type: 'input_file' };
1187
- const fileValue = output.file;
1188
- if (typeof fileValue === 'string') {
1189
- result.file = fileValue;
1190
- }
1191
- else if (fileValue && typeof fileValue === 'object') {
1192
- const record = fileValue;
1193
- if ('data' in record && record.data) {
1194
- const mediaType = record.mediaType ?? 'text/plain';
1195
- if (typeof record.data === 'string') {
1196
- result.file = asDataUrl(record.data, mediaType);
1197
- }
1198
- else {
1199
- const base64 = encodeUint8ArrayToBase64(record.data);
1200
- result.file = asDataUrl(base64, mediaType);
1201
- }
1202
- }
1203
- else if (typeof record.url === 'string' && record.url.length > 0) {
1204
- result.file = { url: record.url };
1205
- }
1206
- else {
1207
- const referencedId = (typeof record.id === 'string' &&
1208
- record.id.length > 0 &&
1209
- record.id) ||
1210
- (typeof record.fileId === 'string' && record.fileId.length > 0
1211
- ? record.fileId
1212
- : undefined);
1213
- if (referencedId) {
1214
- result.file = { id: referencedId };
1215
- }
1216
- }
1217
- if (typeof record.filename === 'string' && record.filename.length > 0) {
1218
- result.filename = record.filename;
1219
- }
1220
- }
1221
- if (output.providerData) {
1222
- result.providerData = output.providerData;
1223
- }
1224
- return result;
1225
- }
1226
- const exhaustiveCheck = output;
1227
- return exhaustiveCheck;
1228
- }
1229
- function normalizeFileValue(value) {
1230
- const directFile = value.file;
1231
- if (typeof directFile === 'string' && directFile.length > 0) {
1232
- return directFile;
1233
- }
1234
- const normalizedObject = normalizeFileObjectCandidate(directFile);
1235
- if (normalizedObject) {
1236
- return normalizedObject;
1237
- }
1238
- const legacyValue = normalizeLegacyFileValue(value);
1239
- if (legacyValue) {
1240
- return legacyValue;
1241
- }
1242
- return null;
1243
- }
1244
- function normalizeFileObjectCandidate(value) {
1245
- if (!isRecord(value)) {
1246
- return null;
1247
- }
1248
- if ('data' in value && value.data !== undefined) {
1249
- const dataValue = value.data;
1250
- const hasStringData = typeof dataValue === 'string' && dataValue.length > 0;
1251
- const hasBinaryData = dataValue instanceof Uint8Array && dataValue.length > 0;
1252
- if (!hasStringData && !hasBinaryData) {
1253
- return null;
1254
- }
1255
- if (!isNonEmptyString(value.mediaType) ||
1256
- !isNonEmptyString(value.filename)) {
1257
- return null;
1258
- }
1259
- return {
1260
- data: typeof dataValue === 'string' ? dataValue : new Uint8Array(dataValue),
1261
- mediaType: value.mediaType,
1262
- filename: value.filename,
1263
- };
1264
- }
1265
- if (isNonEmptyString(value.url)) {
1266
- const result = { url: value.url };
1267
- if (isNonEmptyString(value.filename)) {
1268
- result.filename = value.filename;
1269
- }
1270
- return result;
1271
- }
1272
- const referencedId = (isNonEmptyString(value.id) && value.id) ||
1273
- (isNonEmptyString(value.fileId) && value.fileId);
1274
- if (referencedId) {
1275
- const result = { id: referencedId };
1276
- if (isNonEmptyString(value.filename)) {
1277
- result.filename = value.filename;
1278
- }
1279
- return result;
1280
- }
1281
- return null;
1282
- }
1283
- function normalizeLegacyFileValue(value) {
1284
- const filename = typeof value.filename === 'string' && value.filename.length > 0
1285
- ? value.filename
1286
- : undefined;
1287
- const mediaType = typeof value.mediaType === 'string' && value.mediaType.length > 0
1288
- ? value.mediaType
1289
- : undefined;
1290
- if (typeof value.fileData === 'string' && value.fileData.length > 0) {
1291
- if (!mediaType || !filename) {
1292
- return null;
1293
- }
1294
- return { data: value.fileData, mediaType, filename };
1295
- }
1296
- if (value.fileData instanceof Uint8Array && value.fileData.length > 0) {
1297
- if (!mediaType || !filename) {
1298
- return null;
1299
- }
1300
- return { data: new Uint8Array(value.fileData), mediaType, filename };
1301
- }
1302
- if (typeof value.fileUrl === 'string' && value.fileUrl.length > 0) {
1303
- const result = { url: value.fileUrl };
1304
- if (filename) {
1305
- result.filename = filename;
1306
- }
1307
- return result;
1308
- }
1309
- if (typeof value.fileId === 'string' && value.fileId.length > 0) {
1310
- const result = { id: value.fileId };
1311
- if (filename) {
1312
- result.filename = filename;
1313
- }
1314
- return result;
1315
- }
1316
- return null;
1317
- }
1318
1017
  function normalizeSafetyChecks(checks) {
1319
1018
  if (!Array.isArray(checks)) {
1320
1019
  return undefined;
@@ -1392,26 +1091,4 @@ function isRecord(value) {
1392
1091
  function isNonEmptyString(value) {
1393
1092
  return typeof value === 'string' && value.length > 0;
1394
1093
  }
1395
- function getImageInlineMediaType(value) {
1396
- if (isNonEmptyString(value.mediaType)) {
1397
- return value.mediaType;
1398
- }
1399
- if (isNonEmptyString(value.mimeType)) {
1400
- return value.mimeType;
1401
- }
1402
- return undefined;
1403
- }
1404
- function toInlineImageString(data, mediaType) {
1405
- if (typeof data === 'string') {
1406
- if (mediaType && !data.startsWith('data:')) {
1407
- return asDataUrl(data, mediaType);
1408
- }
1409
- return data;
1410
- }
1411
- const base64 = encodeUint8ArrayToBase64(data);
1412
- return asDataUrl(base64, mediaType);
1413
- }
1414
- function asDataUrl(base64, mediaType) {
1415
- return mediaType ? `data:${mediaType};base64,${base64}` : base64;
1416
- }
1417
1094
  //# sourceMappingURL=toolExecution.mjs.map