@openai/agents-core 0.2.1 → 0.3.0
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/index.d.ts +3 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/memory/memorySession.d.ts +22 -0
- package/dist/memory/memorySession.js +64 -0
- package/dist/memory/memorySession.js.map +1 -0
- package/dist/memory/memorySession.mjs +60 -0
- package/dist/memory/memorySession.mjs.map +1 -0
- package/dist/memory/session.d.ts +36 -0
- package/dist/memory/session.js +3 -0
- package/dist/memory/session.js.map +1 -0
- package/dist/memory/session.mjs +2 -0
- package/dist/memory/session.mjs.map +1 -0
- package/dist/metadata.js +2 -2
- package/dist/metadata.mjs +2 -2
- package/dist/run.d.ts +88 -8
- package/dist/run.js +859 -347
- package/dist/run.js.map +1 -1
- package/dist/run.mjs +859 -347
- package/dist/run.mjs.map +1 -1
- package/dist/runImplementation.d.ts +21 -2
- package/dist/runImplementation.js +885 -346
- package/dist/runImplementation.js.map +1 -1
- package/dist/runImplementation.mjs +877 -344
- package/dist/runImplementation.mjs.map +1 -1
- package/dist/runState.d.ts +1360 -226
- package/dist/runState.js +16 -0
- package/dist/runState.js.map +1 -1
- package/dist/runState.mjs +16 -0
- package/dist/runState.mjs.map +1 -1
- package/dist/types/protocol.d.ts +1869 -193
- package/dist/types/protocol.js +1 -0
- package/dist/types/protocol.js.map +1 -1
- package/dist/types/protocol.mjs +1 -0
- package/dist/types/protocol.mjs.map +1 -1
- package/dist/utils/smartString.d.ts +9 -0
- package/dist/utils/smartString.js +15 -0
- package/dist/utils/smartString.js.map +1 -1
- package/dist/utils/smartString.mjs +14 -3
- package/dist/utils/smartString.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -7,14 +7,59 @@ import { getLastTextFromOutputMessage } from "./utils/messages.mjs";
|
|
|
7
7
|
import { withFunctionSpan, withHandoffSpan } from "./tracing/createSpans.mjs";
|
|
8
8
|
import { getSchemaAndParserFromInputType } from "./utils/tools.mjs";
|
|
9
9
|
import { encodeUint8ArrayToBase64 } from "./utils/base64.mjs";
|
|
10
|
+
import { isArrayBufferView, isNodeBuffer, isSerializedBufferSnapshot, toSmartString, } from "./utils/smartString.mjs";
|
|
10
11
|
import { safeExecute } from "./utils/safeExecute.mjs";
|
|
11
12
|
import { addErrorToCurrentSpan } from "./tracing/context.mjs";
|
|
12
13
|
import { RunItemStreamEvent } from "./events.mjs";
|
|
13
14
|
import { z } from 'zod';
|
|
14
|
-
import { toSmartString } from "./utils/smartString.mjs";
|
|
15
15
|
import { isZodObject } from "./utils/index.mjs";
|
|
16
|
+
function isApprovalItemLike(value) {
|
|
17
|
+
if (!value || typeof value !== 'object') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (!('rawItem' in value)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const rawItem = value.rawItem;
|
|
24
|
+
if (!rawItem || typeof rawItem !== 'object') {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const itemType = rawItem.type;
|
|
28
|
+
return itemType === 'function_call' || itemType === 'hosted_tool_call';
|
|
29
|
+
}
|
|
30
|
+
function getApprovalIdentity(approval) {
|
|
31
|
+
const rawItem = approval.rawItem;
|
|
32
|
+
if (!rawItem) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
if (rawItem.type === 'function_call' && rawItem.callId) {
|
|
36
|
+
return `function_call:${rawItem.callId}`;
|
|
37
|
+
}
|
|
38
|
+
if ('callId' in rawItem && rawItem.callId) {
|
|
39
|
+
return `${rawItem.type}:${rawItem.callId}`;
|
|
40
|
+
}
|
|
41
|
+
const id = 'id' in rawItem ? rawItem.id : undefined;
|
|
42
|
+
if (id) {
|
|
43
|
+
return `${rawItem.type}:${id}`;
|
|
44
|
+
}
|
|
45
|
+
const providerData = typeof rawItem.providerData === 'object' && rawItem.providerData
|
|
46
|
+
? rawItem.providerData
|
|
47
|
+
: undefined;
|
|
48
|
+
if (providerData?.id) {
|
|
49
|
+
return `${rawItem.type}:provider:${providerData.id}`;
|
|
50
|
+
}
|
|
51
|
+
const agentName = 'agent' in approval && approval.agent ? approval.agent.name : '';
|
|
52
|
+
try {
|
|
53
|
+
return `${agentName}:${rawItem.type}:${JSON.stringify(rawItem)}`;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return `${agentName}:${rawItem.type}`;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
16
59
|
/**
|
|
17
60
|
* @internal
|
|
61
|
+
* Walks a raw model response and classifies each item so the runner can schedule follow-up work.
|
|
62
|
+
* Returns both the serializable RunItems (for history/streaming) and the actionable tool metadata.
|
|
18
63
|
*/
|
|
19
64
|
export function processModelResponse(modelResponse, agent, tools, handoffs) {
|
|
20
65
|
const items = [];
|
|
@@ -24,6 +69,7 @@ export function processModelResponse(modelResponse, agent, tools, handoffs) {
|
|
|
24
69
|
const runMCPApprovalRequests = [];
|
|
25
70
|
const toolsUsed = [];
|
|
26
71
|
const handoffMap = new Map(handoffs.map((h) => [h.toolName, h]));
|
|
72
|
+
// Resolve tools upfront so we can look up the concrete handler in O(1) while iterating outputs.
|
|
27
73
|
const functionMap = new Map(tools.filter((t) => t.type === 'function').map((t) => [t.name, t]));
|
|
28
74
|
const computerTool = tools.find((t) => t.type === 'computer');
|
|
29
75
|
const mcpToolMap = new Map(tools
|
|
@@ -158,6 +204,10 @@ export const nextStepSchema = z.discriminatedUnion('type', [
|
|
|
158
204
|
data: z.record(z.string(), z.any()),
|
|
159
205
|
}),
|
|
160
206
|
]);
|
|
207
|
+
/**
|
|
208
|
+
* Internal convenience wrapper that groups the outcome of a single agent turn. It lets the caller
|
|
209
|
+
* update the RunState in one shot and decide which step to execute next.
|
|
210
|
+
*/
|
|
161
211
|
class SingleStepResult {
|
|
162
212
|
originalInput;
|
|
163
213
|
modelResponse;
|
|
@@ -166,7 +216,7 @@ class SingleStepResult {
|
|
|
166
216
|
nextStep;
|
|
167
217
|
constructor(
|
|
168
218
|
/**
|
|
169
|
-
* The input items i.e
|
|
219
|
+
* The input items (i.e., the items before run() was called). May be mutated by handoff input filters.
|
|
170
220
|
*/
|
|
171
221
|
originalInput,
|
|
172
222
|
/**
|
|
@@ -200,6 +250,8 @@ class SingleStepResult {
|
|
|
200
250
|
}
|
|
201
251
|
/**
|
|
202
252
|
* @internal
|
|
253
|
+
* Resets the tool choice when the agent is configured to prefer a fresh tool selection after
|
|
254
|
+
* any tool usage. This prevents the provider from reusing stale tool hints across turns.
|
|
203
255
|
*/
|
|
204
256
|
export function maybeResetToolChoice(agent, toolUseTracker, modelSettings) {
|
|
205
257
|
if (agent.resetToolChoice && toolUseTracker.hasUsedTools(agent)) {
|
|
@@ -209,27 +261,94 @@ export function maybeResetToolChoice(agent, toolUseTracker, modelSettings) {
|
|
|
209
261
|
}
|
|
210
262
|
/**
|
|
211
263
|
* @internal
|
|
264
|
+
* Continues a turn that was previously interrupted waiting for tool approval. Executes the now
|
|
265
|
+
* approved tools and returns the resulting step transition.
|
|
212
266
|
*/
|
|
213
|
-
export async function
|
|
267
|
+
export async function resolveInterruptedTurn(agent, originalInput, originalPreStepItems, newResponse, processedResponse, runner, state) {
|
|
214
268
|
// call_ids for function tools
|
|
215
269
|
const functionCallIds = originalPreStepItems
|
|
216
270
|
.filter((item) => item instanceof RunToolApprovalItem &&
|
|
217
271
|
'callId' in item.rawItem &&
|
|
218
272
|
item.rawItem.type === 'function_call')
|
|
219
273
|
.map((item) => item.rawItem.callId);
|
|
274
|
+
// We already persisted the turn once when the approval interrupt was raised, so the
|
|
275
|
+
// counter reflects the approval items as "flushed". When we resume the same turn we need
|
|
276
|
+
// to rewind it so the eventual tool output for this call is still written to the session.
|
|
277
|
+
const pendingApprovalItems = state
|
|
278
|
+
.getInterruptions()
|
|
279
|
+
.filter(isApprovalItemLike);
|
|
280
|
+
if (pendingApprovalItems.length > 0) {
|
|
281
|
+
const pendingApprovalIdentities = new Set();
|
|
282
|
+
for (const approval of pendingApprovalItems) {
|
|
283
|
+
const identity = getApprovalIdentity(approval);
|
|
284
|
+
if (identity) {
|
|
285
|
+
pendingApprovalIdentities.add(identity);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (pendingApprovalIdentities.size > 0) {
|
|
289
|
+
let rewindCount = 0;
|
|
290
|
+
for (let index = originalPreStepItems.length - 1; index >= 0; index--) {
|
|
291
|
+
const item = originalPreStepItems[index];
|
|
292
|
+
if (!(item instanceof RunToolApprovalItem)) {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const identity = getApprovalIdentity(item);
|
|
296
|
+
if (!identity) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (!pendingApprovalIdentities.has(identity)) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
rewindCount++;
|
|
303
|
+
pendingApprovalIdentities.delete(identity);
|
|
304
|
+
if (pendingApprovalIdentities.size === 0) {
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// Persisting the approval request already advanced the counter once, so undo the increment
|
|
309
|
+
// to make sure we write the final tool output back to the session when the turn resumes.
|
|
310
|
+
if (rewindCount > 0) {
|
|
311
|
+
state._currentTurnPersistedItemCount = Math.max(0, state._currentTurnPersistedItemCount - rewindCount);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
220
315
|
// Run function tools that require approval after they get their approval results
|
|
221
316
|
const functionToolRuns = processedResponse.functions.filter((run) => {
|
|
222
317
|
return functionCallIds.includes(run.toolCall.callId);
|
|
223
318
|
});
|
|
224
319
|
const functionResults = await executeFunctionToolCalls(agent, functionToolRuns, runner, state);
|
|
225
|
-
//
|
|
226
|
-
|
|
320
|
+
// There is no built-in HITL approval surface for computer tools today, so every pending action
|
|
321
|
+
// is executed immediately when the turn resumes.
|
|
322
|
+
const computerResults = processedResponse.computerActions.length > 0
|
|
323
|
+
? await executeComputerActions(agent, processedResponse.computerActions, runner, state._context)
|
|
324
|
+
: [];
|
|
325
|
+
// When resuming we receive the original RunItem references; suppress duplicates so history and streaming do not double-emit the same items.
|
|
326
|
+
const originalPreStepItemSet = new Set(originalPreStepItems);
|
|
327
|
+
const newItems = [];
|
|
328
|
+
const newItemsSet = new Set();
|
|
329
|
+
const appendIfNew = (item) => {
|
|
330
|
+
if (originalPreStepItemSet.has(item) || newItemsSet.has(item)) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
newItems.push(item);
|
|
334
|
+
newItemsSet.add(item);
|
|
335
|
+
};
|
|
336
|
+
for (const result of functionResults) {
|
|
337
|
+
appendIfNew(result.runItem);
|
|
338
|
+
}
|
|
339
|
+
for (const result of computerResults) {
|
|
340
|
+
appendIfNew(result);
|
|
341
|
+
}
|
|
227
342
|
// Run MCP tools that require approval after they get their approval results
|
|
228
343
|
const mcpApprovalRuns = processedResponse.mcpApprovalRequests.filter((run) => {
|
|
229
344
|
return (run.requestItem.type === 'tool_approval_item' &&
|
|
230
345
|
run.requestItem.rawItem.type === 'hosted_tool_call' &&
|
|
231
346
|
run.requestItem.rawItem.providerData?.type === 'mcp_approval_request');
|
|
232
347
|
});
|
|
348
|
+
// Hosted MCP approvals may still be waiting on a human decision when the turn resumes.
|
|
349
|
+
const pendingHostedMCPApprovals = new Set();
|
|
350
|
+
const pendingHostedMCPApprovalIds = new Set();
|
|
351
|
+
// Keep track of approvals we still need to surface next turn so HITL flows can resume cleanly.
|
|
233
352
|
for (const run of mcpApprovalRuns) {
|
|
234
353
|
// the approval_request_id "mcpr_123..."
|
|
235
354
|
const approvalRequestId = run.requestItem.rawItem.id;
|
|
@@ -245,50 +364,93 @@ export async function executeInterruptedToolsAndSideEffects(agent, originalInput
|
|
|
245
364
|
reason: undefined,
|
|
246
365
|
};
|
|
247
366
|
// Tell Responses API server the approval result in the next turn
|
|
248
|
-
|
|
367
|
+
const responseItem = new RunToolCallItem({
|
|
249
368
|
type: 'hosted_tool_call',
|
|
250
369
|
name: 'mcp_approval_response',
|
|
251
370
|
providerData,
|
|
252
|
-
}, agent)
|
|
371
|
+
}, agent);
|
|
372
|
+
appendIfNew(responseItem);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
pendingHostedMCPApprovals.add(run.requestItem);
|
|
376
|
+
pendingHostedMCPApprovalIds.add(approvalRequestId);
|
|
377
|
+
functionResults.push({
|
|
378
|
+
type: 'hosted_mcp_tool_approval',
|
|
379
|
+
tool: run.mcpTool,
|
|
380
|
+
runItem: run.requestItem,
|
|
381
|
+
});
|
|
382
|
+
appendIfNew(run.requestItem);
|
|
253
383
|
}
|
|
254
384
|
}
|
|
255
|
-
|
|
256
|
-
//
|
|
257
|
-
//
|
|
385
|
+
// Server-managed conversations rely on preStepItems to re-surface pending approvals.
|
|
386
|
+
// Keep unresolved hosted MCP approvals in place so HITL flows still have something to approve next turn.
|
|
387
|
+
// Drop resolved approval placeholders so they are not replayed on the next turn, but keep
|
|
388
|
+
// pending approvals in place to signal the outstanding work to the UI and session store.
|
|
258
389
|
const preStepItems = originalPreStepItems.filter((item) => {
|
|
259
|
-
|
|
390
|
+
if (!(item instanceof RunToolApprovalItem)) {
|
|
391
|
+
return true;
|
|
392
|
+
}
|
|
393
|
+
if (item.rawItem.type === 'hosted_tool_call' &&
|
|
394
|
+
item.rawItem.providerData?.type === 'mcp_approval_request') {
|
|
395
|
+
if (pendingHostedMCPApprovals.has(item)) {
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
const approvalRequestId = item.rawItem.id;
|
|
399
|
+
if (approvalRequestId) {
|
|
400
|
+
return pendingHostedMCPApprovalIds.has(approvalRequestId);
|
|
401
|
+
}
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
return false;
|
|
260
405
|
});
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
interruptions: checkToolOutput.interruptions,
|
|
274
|
-
},
|
|
275
|
-
});
|
|
406
|
+
const completedStep = await maybeCompleteTurnFromToolResults({
|
|
407
|
+
agent,
|
|
408
|
+
runner,
|
|
409
|
+
state,
|
|
410
|
+
functionResults,
|
|
411
|
+
originalInput,
|
|
412
|
+
newResponse,
|
|
413
|
+
preStepItems,
|
|
414
|
+
newItems,
|
|
415
|
+
});
|
|
416
|
+
if (completedStep) {
|
|
417
|
+
return completedStep;
|
|
276
418
|
}
|
|
277
419
|
// we only ran new tools and side effects. We need to run the rest of the agent
|
|
278
420
|
return new SingleStepResult(originalInput, newResponse, preStepItems, newItems, { type: 'next_step_run_again' });
|
|
279
421
|
}
|
|
280
422
|
/**
|
|
281
423
|
* @internal
|
|
424
|
+
* Executes every follow-up action the model requested (function tools, computer actions, MCP flows),
|
|
425
|
+
* appends their outputs to the run history, and determines the next step for the agent loop.
|
|
282
426
|
*/
|
|
283
|
-
export async function
|
|
427
|
+
export async function resolveTurnAfterModelResponse(agent, originalInput, originalPreStepItems, newResponse, processedResponse, runner, state) {
|
|
428
|
+
// Reuse the same array reference so we can compare object identity when deciding whether to
|
|
429
|
+
// append new items, ensuring we never double-stream existing RunItems.
|
|
284
430
|
const preStepItems = originalPreStepItems;
|
|
285
|
-
|
|
431
|
+
const seenItems = new Set(originalPreStepItems);
|
|
432
|
+
const newItems = [];
|
|
433
|
+
const appendIfNew = (item) => {
|
|
434
|
+
if (seenItems.has(item)) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
newItems.push(item);
|
|
438
|
+
seenItems.add(item);
|
|
439
|
+
};
|
|
440
|
+
for (const item of processedResponse.newItems) {
|
|
441
|
+
appendIfNew(item);
|
|
442
|
+
}
|
|
443
|
+
// Run function tools and computer actions in parallel; neither depends on the other's side effects.
|
|
286
444
|
const [functionResults, computerResults] = await Promise.all([
|
|
287
445
|
executeFunctionToolCalls(agent, processedResponse.functions, runner, state),
|
|
288
446
|
executeComputerActions(agent, processedResponse.computerActions, runner, state._context),
|
|
289
447
|
]);
|
|
290
|
-
|
|
291
|
-
|
|
448
|
+
for (const result of functionResults) {
|
|
449
|
+
appendIfNew(result.runItem);
|
|
450
|
+
}
|
|
451
|
+
for (const item of computerResults) {
|
|
452
|
+
appendIfNew(item);
|
|
453
|
+
}
|
|
292
454
|
// run hosted MCP approval requests
|
|
293
455
|
if (processedResponse.mcpApprovalRequests.length > 0) {
|
|
294
456
|
for (const approvalRequest of processedResponse.mcpApprovalRequests) {
|
|
@@ -334,22 +496,18 @@ export async function executeToolsAndSideEffects(agent, originalInput, originalP
|
|
|
334
496
|
if (processedResponse.handoffs.length > 0) {
|
|
335
497
|
return await executeHandoffCalls(agent, originalInput, preStepItems, newItems, newResponse, processedResponse.handoffs, runner, state._context);
|
|
336
498
|
}
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
runner
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
data: {
|
|
350
|
-
interruptions: checkToolOutput.interruptions,
|
|
351
|
-
},
|
|
352
|
-
});
|
|
499
|
+
const completedStep = await maybeCompleteTurnFromToolResults({
|
|
500
|
+
agent,
|
|
501
|
+
runner,
|
|
502
|
+
state,
|
|
503
|
+
functionResults,
|
|
504
|
+
originalInput,
|
|
505
|
+
newResponse,
|
|
506
|
+
preStepItems,
|
|
507
|
+
newItems,
|
|
508
|
+
});
|
|
509
|
+
if (completedStep) {
|
|
510
|
+
return completedStep;
|
|
353
511
|
}
|
|
354
512
|
// If the model issued any tool calls or handoffs in this turn,
|
|
355
513
|
// we must NOT treat any assistant message in the same turn as the final output.
|
|
@@ -371,6 +529,7 @@ export async function executeToolsAndSideEffects(agent, originalInput, originalP
|
|
|
371
529
|
if (typeof potentialFinalOutput === 'undefined') {
|
|
372
530
|
return new SingleStepResult(originalInput, newResponse, preStepItems, newItems, { type: 'next_step_run_again' });
|
|
373
531
|
}
|
|
532
|
+
// Keep looping if any tool output placeholders still require an approval follow-up.
|
|
374
533
|
const hasPendingToolsOrApprovals = functionResults.some((result) => result.runItem instanceof RunToolApprovalItem);
|
|
375
534
|
if (!hasPendingToolsOrApprovals) {
|
|
376
535
|
if (agent.outputType === 'text') {
|
|
@@ -397,8 +556,32 @@ export async function executeToolsAndSideEffects(agent, originalInput, originalP
|
|
|
397
556
|
}
|
|
398
557
|
return new SingleStepResult(originalInput, newResponse, preStepItems, newItems, { type: 'next_step_run_again' });
|
|
399
558
|
}
|
|
559
|
+
// Consolidates the logic that determines whether tool results yielded a final answer,
|
|
560
|
+
// triggered an interruption, or require the agent loop to continue running.
|
|
561
|
+
async function maybeCompleteTurnFromToolResults({ agent, runner, state, functionResults, originalInput, newResponse, preStepItems, newItems, }) {
|
|
562
|
+
const toolOutcome = await checkForFinalOutputFromTools(agent, functionResults, state);
|
|
563
|
+
if (toolOutcome.isFinalOutput) {
|
|
564
|
+
runner.emit('agent_end', state._context, agent, toolOutcome.finalOutput);
|
|
565
|
+
agent.emit('agent_end', state._context, toolOutcome.finalOutput);
|
|
566
|
+
return new SingleStepResult(originalInput, newResponse, preStepItems, newItems, {
|
|
567
|
+
type: 'next_step_final_output',
|
|
568
|
+
output: toolOutcome.finalOutput,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
if (toolOutcome.isInterrupted) {
|
|
572
|
+
return new SingleStepResult(originalInput, newResponse, preStepItems, newItems, {
|
|
573
|
+
type: 'next_step_interruption',
|
|
574
|
+
data: {
|
|
575
|
+
interruptions: toolOutcome.interruptions,
|
|
576
|
+
},
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
400
581
|
/**
|
|
401
582
|
* @internal
|
|
583
|
+
* Normalizes tool outputs once so downstream code works with fully structured protocol items.
|
|
584
|
+
* Doing this here keeps API surface stable even when providers add new shapes.
|
|
402
585
|
*/
|
|
403
586
|
export function getToolCallOutputItem(toolCall, output) {
|
|
404
587
|
const maybeStructuredOutputs = normalizeStructuredToolOutputs(output);
|
|
@@ -423,318 +606,92 @@ export function getToolCallOutputItem(toolCall, output) {
|
|
|
423
606
|
},
|
|
424
607
|
};
|
|
425
608
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
* back to the legacy string pipeline.
|
|
431
|
-
*/
|
|
432
|
-
function normalizeStructuredToolOutputs(output) {
|
|
433
|
-
if (Array.isArray(output)) {
|
|
434
|
-
const structured = [];
|
|
435
|
-
for (const item of output) {
|
|
436
|
-
const normalized = normalizeStructuredToolOutput(item);
|
|
437
|
-
if (!normalized) {
|
|
438
|
-
return null;
|
|
439
|
-
}
|
|
440
|
-
structured.push(normalized);
|
|
441
|
-
}
|
|
442
|
-
return structured;
|
|
609
|
+
function normalizeFileValue(value) {
|
|
610
|
+
const directFile = value.file;
|
|
611
|
+
if (typeof directFile === 'string' && directFile.length > 0) {
|
|
612
|
+
return directFile;
|
|
443
613
|
}
|
|
444
|
-
const
|
|
445
|
-
|
|
614
|
+
const normalizedObject = normalizeFileObjectCandidate(directFile);
|
|
615
|
+
if (normalizedObject) {
|
|
616
|
+
return normalizedObject;
|
|
617
|
+
}
|
|
618
|
+
const legacyValue = normalizeLegacyFileValue(value);
|
|
619
|
+
if (legacyValue) {
|
|
620
|
+
return legacyValue;
|
|
621
|
+
}
|
|
622
|
+
return null;
|
|
446
623
|
}
|
|
447
|
-
|
|
448
|
-
* Best-effort normalization of a single tool output item. If the object already matches the
|
|
449
|
-
* protocol shape we simply cast it; otherwise we copy the recognised fields into the canonical
|
|
450
|
-
* structure. Returning null lets the caller know we should revert to plain-string handling.
|
|
451
|
-
*/
|
|
452
|
-
function normalizeStructuredToolOutput(value) {
|
|
624
|
+
function normalizeFileObjectCandidate(value) {
|
|
453
625
|
if (!isRecord(value)) {
|
|
454
626
|
return null;
|
|
455
627
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return output;
|
|
463
|
-
}
|
|
464
|
-
if (type === 'image') {
|
|
465
|
-
const output = { type: 'image' };
|
|
466
|
-
let imageString;
|
|
467
|
-
let imageFileId;
|
|
468
|
-
const fallbackImageMediaType = isNonEmptyString(value.mediaType)
|
|
469
|
-
? value.mediaType
|
|
470
|
-
: undefined;
|
|
471
|
-
const imageField = value.image;
|
|
472
|
-
if (typeof imageField === 'string' && imageField.length > 0) {
|
|
473
|
-
imageString = imageField;
|
|
474
|
-
}
|
|
475
|
-
else if (isRecord(imageField)) {
|
|
476
|
-
const imageObj = imageField;
|
|
477
|
-
const inlineMediaType = isNonEmptyString(imageObj.mediaType)
|
|
478
|
-
? imageObj.mediaType
|
|
479
|
-
: fallbackImageMediaType;
|
|
480
|
-
if (isNonEmptyString(imageObj.url)) {
|
|
481
|
-
imageString = imageObj.url;
|
|
482
|
-
}
|
|
483
|
-
else if (isNonEmptyString(imageObj.data)) {
|
|
484
|
-
imageString = toInlineImageString(imageObj.data, inlineMediaType);
|
|
485
|
-
}
|
|
486
|
-
else if (imageObj.data instanceof Uint8Array &&
|
|
487
|
-
imageObj.data.length > 0) {
|
|
488
|
-
imageString = toInlineImageString(imageObj.data, inlineMediaType);
|
|
489
|
-
}
|
|
490
|
-
if (!imageString) {
|
|
491
|
-
const candidateId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
|
|
492
|
-
(isNonEmptyString(imageObj.id) && imageObj.id) ||
|
|
493
|
-
undefined;
|
|
494
|
-
if (candidateId) {
|
|
495
|
-
imageFileId = candidateId;
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
if (!imageString &&
|
|
500
|
-
typeof value.imageUrl === 'string' &&
|
|
501
|
-
value.imageUrl.length > 0) {
|
|
502
|
-
imageString = value.imageUrl;
|
|
503
|
-
}
|
|
504
|
-
if (!imageFileId &&
|
|
505
|
-
typeof value.fileId === 'string' &&
|
|
506
|
-
value.fileId.length > 0) {
|
|
507
|
-
imageFileId = value.fileId;
|
|
508
|
-
}
|
|
509
|
-
if (!imageString &&
|
|
510
|
-
typeof value.data === 'string' &&
|
|
511
|
-
value.data.length > 0) {
|
|
512
|
-
imageString = fallbackImageMediaType
|
|
513
|
-
? toInlineImageString(value.data, fallbackImageMediaType)
|
|
514
|
-
: value.data;
|
|
515
|
-
}
|
|
516
|
-
else if (!imageString &&
|
|
517
|
-
value.data instanceof Uint8Array &&
|
|
518
|
-
value.data.length > 0) {
|
|
519
|
-
imageString = toInlineImageString(value.data, fallbackImageMediaType);
|
|
628
|
+
if ('data' in value && value.data !== undefined) {
|
|
629
|
+
const dataValue = value.data;
|
|
630
|
+
const hasStringData = typeof dataValue === 'string' && dataValue.length > 0;
|
|
631
|
+
const hasBinaryData = dataValue instanceof Uint8Array && dataValue.length > 0;
|
|
632
|
+
if (!hasStringData && !hasBinaryData) {
|
|
633
|
+
return null;
|
|
520
634
|
}
|
|
521
|
-
if (
|
|
522
|
-
|
|
635
|
+
if (!isNonEmptyString(value.mediaType) ||
|
|
636
|
+
!isNonEmptyString(value.filename)) {
|
|
637
|
+
return null;
|
|
523
638
|
}
|
|
524
|
-
|
|
525
|
-
|
|
639
|
+
return {
|
|
640
|
+
data: typeof dataValue === 'string' ? dataValue : new Uint8Array(dataValue),
|
|
641
|
+
mediaType: value.mediaType,
|
|
642
|
+
filename: value.filename,
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
if (isNonEmptyString(value.url)) {
|
|
646
|
+
const result = { url: value.url };
|
|
647
|
+
if (isNonEmptyString(value.filename)) {
|
|
648
|
+
result.filename = value.filename;
|
|
526
649
|
}
|
|
527
|
-
|
|
528
|
-
|
|
650
|
+
return result;
|
|
651
|
+
}
|
|
652
|
+
const referencedId = (isNonEmptyString(value.id) && value.id) ||
|
|
653
|
+
(isNonEmptyString(value.fileId) && value.fileId);
|
|
654
|
+
if (referencedId) {
|
|
655
|
+
const result = { id: referencedId };
|
|
656
|
+
if (isNonEmptyString(value.filename)) {
|
|
657
|
+
result.filename = value.filename;
|
|
529
658
|
}
|
|
530
|
-
|
|
659
|
+
return result;
|
|
660
|
+
}
|
|
661
|
+
return null;
|
|
662
|
+
}
|
|
663
|
+
function normalizeLegacyFileValue(value) {
|
|
664
|
+
const filename = typeof value.filename === 'string' && value.filename.length > 0
|
|
665
|
+
? value.filename
|
|
666
|
+
: undefined;
|
|
667
|
+
const mediaType = typeof value.mediaType === 'string' && value.mediaType.length > 0
|
|
668
|
+
? value.mediaType
|
|
669
|
+
: undefined;
|
|
670
|
+
if (typeof value.fileData === 'string' && value.fileData.length > 0) {
|
|
671
|
+
if (!mediaType || !filename) {
|
|
531
672
|
return null;
|
|
532
673
|
}
|
|
533
|
-
|
|
534
|
-
output.providerData = value.providerData;
|
|
535
|
-
}
|
|
536
|
-
return output;
|
|
674
|
+
return { data: value.fileData, mediaType, filename };
|
|
537
675
|
}
|
|
538
|
-
if (
|
|
539
|
-
|
|
540
|
-
if (!fileValue) {
|
|
676
|
+
if (value.fileData instanceof Uint8Array && value.fileData.length > 0) {
|
|
677
|
+
if (!mediaType || !filename) {
|
|
541
678
|
return null;
|
|
542
679
|
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
680
|
+
return { data: new Uint8Array(value.fileData), mediaType, filename };
|
|
681
|
+
}
|
|
682
|
+
if (typeof value.fileUrl === 'string' && value.fileUrl.length > 0) {
|
|
683
|
+
const result = { url: value.fileUrl };
|
|
684
|
+
if (filename) {
|
|
685
|
+
result.filename = filename;
|
|
546
686
|
}
|
|
547
|
-
return
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
*/
|
|
556
|
-
function convertStructuredToolOutputToInputItem(output) {
|
|
557
|
-
if (output.type === 'text') {
|
|
558
|
-
const result = {
|
|
559
|
-
type: 'input_text',
|
|
560
|
-
text: output.text,
|
|
561
|
-
};
|
|
562
|
-
if (output.providerData) {
|
|
563
|
-
result.providerData = output.providerData;
|
|
564
|
-
}
|
|
565
|
-
return result;
|
|
566
|
-
}
|
|
567
|
-
if (output.type === 'image') {
|
|
568
|
-
const result = { type: 'input_image' };
|
|
569
|
-
if (typeof output.detail === 'string' && output.detail.length > 0) {
|
|
570
|
-
result.detail = output.detail;
|
|
571
|
-
}
|
|
572
|
-
if (typeof output.image === 'string' && output.image.length > 0) {
|
|
573
|
-
result.image = output.image;
|
|
574
|
-
}
|
|
575
|
-
else if (isRecord(output.image)) {
|
|
576
|
-
const imageObj = output.image;
|
|
577
|
-
const inlineMediaType = isNonEmptyString(imageObj.mediaType)
|
|
578
|
-
? imageObj.mediaType
|
|
579
|
-
: undefined;
|
|
580
|
-
if (isNonEmptyString(imageObj.url)) {
|
|
581
|
-
result.image = imageObj.url;
|
|
582
|
-
}
|
|
583
|
-
else if (isNonEmptyString(imageObj.data)) {
|
|
584
|
-
result.image =
|
|
585
|
-
inlineMediaType && !imageObj.data.startsWith('data:')
|
|
586
|
-
? asDataUrl(imageObj.data, inlineMediaType)
|
|
587
|
-
: imageObj.data;
|
|
588
|
-
}
|
|
589
|
-
else if (imageObj.data instanceof Uint8Array &&
|
|
590
|
-
imageObj.data.length > 0) {
|
|
591
|
-
const base64 = encodeUint8ArrayToBase64(imageObj.data);
|
|
592
|
-
result.image = asDataUrl(base64, inlineMediaType);
|
|
593
|
-
}
|
|
594
|
-
else {
|
|
595
|
-
const referencedId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
|
|
596
|
-
(isNonEmptyString(imageObj.id) && imageObj.id) ||
|
|
597
|
-
undefined;
|
|
598
|
-
if (referencedId) {
|
|
599
|
-
result.image = { id: referencedId };
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
if (output.providerData) {
|
|
604
|
-
result.providerData = output.providerData;
|
|
605
|
-
}
|
|
606
|
-
return result;
|
|
607
|
-
}
|
|
608
|
-
if (output.type === 'file') {
|
|
609
|
-
const result = { type: 'input_file' };
|
|
610
|
-
const fileValue = output.file;
|
|
611
|
-
if (typeof fileValue === 'string') {
|
|
612
|
-
result.file = fileValue;
|
|
613
|
-
}
|
|
614
|
-
else if (fileValue && typeof fileValue === 'object') {
|
|
615
|
-
const record = fileValue;
|
|
616
|
-
if ('data' in record && record.data) {
|
|
617
|
-
const mediaType = record.mediaType ?? 'text/plain';
|
|
618
|
-
if (typeof record.data === 'string') {
|
|
619
|
-
result.file = asDataUrl(record.data, mediaType);
|
|
620
|
-
}
|
|
621
|
-
else {
|
|
622
|
-
const base64 = encodeUint8ArrayToBase64(record.data);
|
|
623
|
-
result.file = asDataUrl(base64, mediaType);
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
else if (typeof record.url === 'string' && record.url.length > 0) {
|
|
627
|
-
result.file = { url: record.url };
|
|
628
|
-
}
|
|
629
|
-
else {
|
|
630
|
-
const referencedId = (typeof record.id === 'string' &&
|
|
631
|
-
record.id.length > 0 &&
|
|
632
|
-
record.id) ||
|
|
633
|
-
(typeof record.fileId === 'string' && record.fileId.length > 0
|
|
634
|
-
? record.fileId
|
|
635
|
-
: undefined);
|
|
636
|
-
if (referencedId) {
|
|
637
|
-
result.file = { id: referencedId };
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
if (typeof record.filename === 'string' && record.filename.length > 0) {
|
|
641
|
-
result.filename = record.filename;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
if (output.providerData) {
|
|
645
|
-
result.providerData = output.providerData;
|
|
646
|
-
}
|
|
647
|
-
return result;
|
|
648
|
-
}
|
|
649
|
-
const exhaustiveCheck = output;
|
|
650
|
-
return exhaustiveCheck;
|
|
651
|
-
}
|
|
652
|
-
function normalizeFileValue(value) {
|
|
653
|
-
const directFile = value.file;
|
|
654
|
-
if (typeof directFile === 'string' && directFile.length > 0) {
|
|
655
|
-
return directFile;
|
|
656
|
-
}
|
|
657
|
-
const normalizedObject = normalizeFileObjectCandidate(directFile);
|
|
658
|
-
if (normalizedObject) {
|
|
659
|
-
return normalizedObject;
|
|
660
|
-
}
|
|
661
|
-
const legacyValue = normalizeLegacyFileValue(value);
|
|
662
|
-
if (legacyValue) {
|
|
663
|
-
return legacyValue;
|
|
664
|
-
}
|
|
665
|
-
return null;
|
|
666
|
-
}
|
|
667
|
-
function normalizeFileObjectCandidate(value) {
|
|
668
|
-
if (!isRecord(value)) {
|
|
669
|
-
return null;
|
|
670
|
-
}
|
|
671
|
-
if ('data' in value && value.data !== undefined) {
|
|
672
|
-
const dataValue = value.data;
|
|
673
|
-
const hasStringData = typeof dataValue === 'string' && dataValue.length > 0;
|
|
674
|
-
const hasBinaryData = dataValue instanceof Uint8Array && dataValue.length > 0;
|
|
675
|
-
if (!hasStringData && !hasBinaryData) {
|
|
676
|
-
return null;
|
|
677
|
-
}
|
|
678
|
-
if (!isNonEmptyString(value.mediaType) ||
|
|
679
|
-
!isNonEmptyString(value.filename)) {
|
|
680
|
-
return null;
|
|
681
|
-
}
|
|
682
|
-
return {
|
|
683
|
-
data: typeof dataValue === 'string' ? dataValue : new Uint8Array(dataValue),
|
|
684
|
-
mediaType: value.mediaType,
|
|
685
|
-
filename: value.filename,
|
|
686
|
-
};
|
|
687
|
-
}
|
|
688
|
-
if (isNonEmptyString(value.url)) {
|
|
689
|
-
const result = { url: value.url };
|
|
690
|
-
if (isNonEmptyString(value.filename)) {
|
|
691
|
-
result.filename = value.filename;
|
|
692
|
-
}
|
|
693
|
-
return result;
|
|
694
|
-
}
|
|
695
|
-
const referencedId = (isNonEmptyString(value.id) && value.id) ||
|
|
696
|
-
(isNonEmptyString(value.fileId) && value.fileId);
|
|
697
|
-
if (referencedId) {
|
|
698
|
-
const result = { id: referencedId };
|
|
699
|
-
if (isNonEmptyString(value.filename)) {
|
|
700
|
-
result.filename = value.filename;
|
|
701
|
-
}
|
|
702
|
-
return result;
|
|
703
|
-
}
|
|
704
|
-
return null;
|
|
705
|
-
}
|
|
706
|
-
function normalizeLegacyFileValue(value) {
|
|
707
|
-
const filename = typeof value.filename === 'string' && value.filename.length > 0
|
|
708
|
-
? value.filename
|
|
709
|
-
: undefined;
|
|
710
|
-
const mediaType = typeof value.mediaType === 'string' && value.mediaType.length > 0
|
|
711
|
-
? value.mediaType
|
|
712
|
-
: undefined;
|
|
713
|
-
if (typeof value.fileData === 'string' && value.fileData.length > 0) {
|
|
714
|
-
if (!mediaType || !filename) {
|
|
715
|
-
return null;
|
|
716
|
-
}
|
|
717
|
-
return { data: value.fileData, mediaType, filename };
|
|
718
|
-
}
|
|
719
|
-
if (value.fileData instanceof Uint8Array && value.fileData.length > 0) {
|
|
720
|
-
if (!mediaType || !filename) {
|
|
721
|
-
return null;
|
|
722
|
-
}
|
|
723
|
-
return { data: new Uint8Array(value.fileData), mediaType, filename };
|
|
724
|
-
}
|
|
725
|
-
if (typeof value.fileUrl === 'string' && value.fileUrl.length > 0) {
|
|
726
|
-
const result = { url: value.fileUrl };
|
|
727
|
-
if (filename) {
|
|
728
|
-
result.filename = filename;
|
|
729
|
-
}
|
|
730
|
-
return result;
|
|
731
|
-
}
|
|
732
|
-
if (typeof value.fileId === 'string' && value.fileId.length > 0) {
|
|
733
|
-
const result = { id: value.fileId };
|
|
734
|
-
if (filename) {
|
|
735
|
-
result.filename = filename;
|
|
736
|
-
}
|
|
737
|
-
return result;
|
|
687
|
+
return result;
|
|
688
|
+
}
|
|
689
|
+
if (typeof value.fileId === 'string' && value.fileId.length > 0) {
|
|
690
|
+
const result = { id: value.fileId };
|
|
691
|
+
if (filename) {
|
|
692
|
+
result.filename = filename;
|
|
693
|
+
}
|
|
694
|
+
return result;
|
|
738
695
|
}
|
|
739
696
|
return null;
|
|
740
697
|
}
|
|
@@ -759,6 +716,8 @@ function asDataUrl(base64, mediaType) {
|
|
|
759
716
|
}
|
|
760
717
|
/**
|
|
761
718
|
* @internal
|
|
719
|
+
* Runs every function tool call requested by the model and returns their outputs alongside
|
|
720
|
+
* the `RunItem` instances that should be appended to history.
|
|
762
721
|
*/
|
|
763
722
|
export async function executeFunctionToolCalls(agent, toolRuns, runner, state) {
|
|
764
723
|
async function runSingleTool(toolRun) {
|
|
@@ -771,6 +730,7 @@ export async function executeFunctionToolCalls(agent, toolRuns, runner, state) {
|
|
|
771
730
|
parsedArgs = JSON.parse(parsedArgs);
|
|
772
731
|
}
|
|
773
732
|
}
|
|
733
|
+
// Some tools require a human or policy check before execution; defer until approval is recorded.
|
|
774
734
|
const needsApproval = await toolRun.tool.needsApproval(state._context, parsedArgs, toolRun.toolCall.callId);
|
|
775
735
|
if (needsApproval) {
|
|
776
736
|
const approval = state._context.isToolApproved({
|
|
@@ -924,6 +884,8 @@ async function _runComputerActionAndScreenshot(computer, toolCall) {
|
|
|
924
884
|
}
|
|
925
885
|
/**
|
|
926
886
|
* @internal
|
|
887
|
+
* Executes any computer-use actions emitted by the model and returns the resulting items so the
|
|
888
|
+
* run history reflects the computer session.
|
|
927
889
|
*/
|
|
928
890
|
export async function executeComputerActions(agent, actions, runner, runContext, customLogger = undefined) {
|
|
929
891
|
const _logger = customLogger ?? logger;
|
|
@@ -956,7 +918,7 @@ export async function executeComputerActions(agent, actions, runner, runContext,
|
|
|
956
918
|
toolCall,
|
|
957
919
|
});
|
|
958
920
|
}
|
|
959
|
-
//
|
|
921
|
+
// Return the screenshot as a data URL when available; fall back to an empty string on failures.
|
|
960
922
|
const imageUrl = output ? `data:image/png;base64,${output}` : '';
|
|
961
923
|
const rawItem = {
|
|
962
924
|
type: 'computer_call_result',
|
|
@@ -969,6 +931,8 @@ export async function executeComputerActions(agent, actions, runner, runContext,
|
|
|
969
931
|
}
|
|
970
932
|
/**
|
|
971
933
|
* @internal
|
|
934
|
+
* Drives handoff calls by invoking the downstream agent and capturing any generated items so
|
|
935
|
+
* the current agent can continue with the new context.
|
|
972
936
|
*/
|
|
973
937
|
export async function executeHandoffCalls(agent, originalInput, preStepItems, newStepItems, newResponse, runHandoffs, runner, runContext) {
|
|
974
938
|
newStepItems = [...newStepItems];
|
|
@@ -1037,6 +1001,8 @@ const NOT_FINAL_OUTPUT = {
|
|
|
1037
1001
|
};
|
|
1038
1002
|
/**
|
|
1039
1003
|
* @internal
|
|
1004
|
+
* Determines whether tool executions produced a final agent output, triggered an interruption,
|
|
1005
|
+
* or whether the agent loop should continue collecting more responses.
|
|
1040
1006
|
*/
|
|
1041
1007
|
export async function checkForFinalOutputFromTools(agent, toolResults, state) {
|
|
1042
1008
|
if (toolResults.length === 0) {
|
|
@@ -1163,4 +1129,571 @@ export class AgentToolUseTracker {
|
|
|
1163
1129
|
}));
|
|
1164
1130
|
}
|
|
1165
1131
|
}
|
|
1132
|
+
/**
|
|
1133
|
+
* @internal
|
|
1134
|
+
* Convert a user-provided input into a list of input items.
|
|
1135
|
+
*/
|
|
1136
|
+
export function toInputItemList(input) {
|
|
1137
|
+
if (typeof input === 'string') {
|
|
1138
|
+
return [
|
|
1139
|
+
{
|
|
1140
|
+
type: 'message',
|
|
1141
|
+
role: 'user',
|
|
1142
|
+
content: input,
|
|
1143
|
+
},
|
|
1144
|
+
];
|
|
1145
|
+
}
|
|
1146
|
+
return [...input];
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* @internal
|
|
1150
|
+
* Extract model output items from run items, excluding tool approval items.
|
|
1151
|
+
*/
|
|
1152
|
+
export function extractOutputItemsFromRunItems(items) {
|
|
1153
|
+
return items
|
|
1154
|
+
.filter((item) => item.type !== 'tool_approval_item')
|
|
1155
|
+
.map((item) => item.rawItem);
|
|
1156
|
+
}
|
|
1157
|
+
function normalizeItemsForSessionPersistence(items) {
|
|
1158
|
+
// Persisted sessions must avoid raw binary so we convert every item into a JSON-safe shape before writing to storage.
|
|
1159
|
+
return items.map((item) => sanitizeValueForSession(stripTransientCallIds(item)));
|
|
1160
|
+
}
|
|
1161
|
+
function sanitizeValueForSession(value, context = {}) {
|
|
1162
|
+
if (value === null || value === undefined) {
|
|
1163
|
+
return value;
|
|
1164
|
+
}
|
|
1165
|
+
// Convert supported binary payloads into ArrayBuffer views before serialization.
|
|
1166
|
+
const binary = toUint8ArrayIfBinary(value);
|
|
1167
|
+
if (binary) {
|
|
1168
|
+
return toDataUrlFromBytes(binary, context.mediaType);
|
|
1169
|
+
}
|
|
1170
|
+
if (Array.isArray(value)) {
|
|
1171
|
+
return value.map((entry) => sanitizeValueForSession(entry, context));
|
|
1172
|
+
}
|
|
1173
|
+
if (!isPlainObject(value)) {
|
|
1174
|
+
return value;
|
|
1175
|
+
}
|
|
1176
|
+
const record = value;
|
|
1177
|
+
const result = {};
|
|
1178
|
+
const mediaType = typeof record.mediaType === 'string' && record.mediaType.length > 0
|
|
1179
|
+
? record.mediaType
|
|
1180
|
+
: context.mediaType;
|
|
1181
|
+
for (const [key, entry] of Object.entries(record)) {
|
|
1182
|
+
// Propagate explicit media type only when walking into binary payload containers.
|
|
1183
|
+
const nextContext = key === 'data' || key === 'fileData' ? { mediaType } : context;
|
|
1184
|
+
result[key] = sanitizeValueForSession(entry, nextContext);
|
|
1185
|
+
}
|
|
1186
|
+
return result;
|
|
1187
|
+
}
|
|
1188
|
+
function toUint8ArrayIfBinary(value) {
|
|
1189
|
+
// Normalize the diverse binary containers we may receive into a shared Uint8Array view.
|
|
1190
|
+
if (value instanceof ArrayBuffer) {
|
|
1191
|
+
return new Uint8Array(value);
|
|
1192
|
+
}
|
|
1193
|
+
if (isArrayBufferView(value)) {
|
|
1194
|
+
const view = value;
|
|
1195
|
+
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1196
|
+
}
|
|
1197
|
+
if (isNodeBuffer(value)) {
|
|
1198
|
+
const view = value;
|
|
1199
|
+
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1200
|
+
}
|
|
1201
|
+
if (isSerializedBufferSnapshot(value)) {
|
|
1202
|
+
const snapshot = value;
|
|
1203
|
+
return Uint8Array.from(snapshot.data);
|
|
1204
|
+
}
|
|
1205
|
+
return undefined;
|
|
1206
|
+
}
|
|
1207
|
+
function toDataUrlFromBytes(bytes, mediaType) {
|
|
1208
|
+
// Convert binary payloads into a durable data URL so session files remain self-contained.
|
|
1209
|
+
const base64 = encodeUint8ArrayToBase64(bytes);
|
|
1210
|
+
// Note that OpenAI Responses API never accepts application/octet-stream as a media type,
|
|
1211
|
+
// so we fall back to text/plain; that said, tools are supposed to return a valid media type when this utility is used.
|
|
1212
|
+
const type = mediaType && !mediaType.startsWith('data:') ? mediaType : 'text/plain';
|
|
1213
|
+
return `data:${type};base64,${base64}`;
|
|
1214
|
+
}
|
|
1215
|
+
function isPlainObject(value) {
|
|
1216
|
+
if (typeof value !== 'object' || value === null) {
|
|
1217
|
+
return false;
|
|
1218
|
+
}
|
|
1219
|
+
const proto = Object.getPrototypeOf(value);
|
|
1220
|
+
return proto === Object.prototype || proto === null;
|
|
1221
|
+
}
|
|
1222
|
+
function stripTransientCallIds(value) {
|
|
1223
|
+
if (value === null || value === undefined) {
|
|
1224
|
+
return value;
|
|
1225
|
+
}
|
|
1226
|
+
if (Array.isArray(value)) {
|
|
1227
|
+
return value.map((entry) => stripTransientCallIds(entry));
|
|
1228
|
+
}
|
|
1229
|
+
if (!isPlainObject(value)) {
|
|
1230
|
+
return value;
|
|
1231
|
+
}
|
|
1232
|
+
const record = value;
|
|
1233
|
+
const result = {};
|
|
1234
|
+
const isProtocolItem = typeof record.type === 'string' && record.type.length > 0;
|
|
1235
|
+
const shouldStripId = isProtocolItem && shouldStripIdForType(record.type);
|
|
1236
|
+
for (const [key, entry] of Object.entries(record)) {
|
|
1237
|
+
if (shouldStripId && key === 'id') {
|
|
1238
|
+
continue;
|
|
1239
|
+
}
|
|
1240
|
+
result[key] = stripTransientCallIds(entry);
|
|
1241
|
+
}
|
|
1242
|
+
return result;
|
|
1243
|
+
}
|
|
1244
|
+
function shouldStripIdForType(type) {
|
|
1245
|
+
switch (type) {
|
|
1246
|
+
case 'function_call':
|
|
1247
|
+
case 'function_call_result':
|
|
1248
|
+
return true;
|
|
1249
|
+
default:
|
|
1250
|
+
return false;
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* @internal
|
|
1255
|
+
* Persist full turn (input + outputs) for non-streaming runs.
|
|
1256
|
+
*/
|
|
1257
|
+
// Persists the combination of user inputs (possibly filtered) and model outputs for a completed turn.
|
|
1258
|
+
export async function saveToSession(session, sessionInputItems, result) {
|
|
1259
|
+
if (!session) {
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
const inputItems = sessionInputItems ?? [];
|
|
1263
|
+
const state = result.state;
|
|
1264
|
+
const alreadyPersisted = state._currentTurnPersistedItemCount ?? 0;
|
|
1265
|
+
// Persist only the portion of _generatedItems that has not yet been stored for this turn.
|
|
1266
|
+
const newRunItems = result.newItems.slice(alreadyPersisted);
|
|
1267
|
+
if (process.env.OPENAI_AGENTS__DEBUG_SAVE_SESSION) {
|
|
1268
|
+
console.debug('saveToSession:newRunItems', newRunItems.map((item) => item.type));
|
|
1269
|
+
}
|
|
1270
|
+
const outputItems = extractOutputItemsFromRunItems(newRunItems);
|
|
1271
|
+
const itemsToSave = [...inputItems, ...outputItems];
|
|
1272
|
+
if (itemsToSave.length === 0) {
|
|
1273
|
+
state._currentTurnPersistedItemCount =
|
|
1274
|
+
alreadyPersisted + newRunItems.length;
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
const sanitizedItems = normalizeItemsForSessionPersistence(itemsToSave);
|
|
1278
|
+
await session.addItems(sanitizedItems);
|
|
1279
|
+
state._currentTurnPersistedItemCount = alreadyPersisted + newRunItems.length;
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* @internal
|
|
1283
|
+
* Persist only the user input for streaming runs at start.
|
|
1284
|
+
*/
|
|
1285
|
+
// For streaming runs we persist user input as soon as it is sent so reconnections can resume.
|
|
1286
|
+
export async function saveStreamInputToSession(session, sessionInputItems) {
|
|
1287
|
+
if (!session) {
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
if (!sessionInputItems || sessionInputItems.length === 0) {
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
const sanitizedInput = normalizeItemsForSessionPersistence(sessionInputItems);
|
|
1294
|
+
await session.addItems(sanitizedInput);
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* @internal
|
|
1298
|
+
* Persist only the model outputs for streaming runs at the end of a turn.
|
|
1299
|
+
*/
|
|
1300
|
+
// Complements saveStreamInputToSession by recording the streaming outputs at the end of the turn.
|
|
1301
|
+
export async function saveStreamResultToSession(session, result) {
|
|
1302
|
+
if (!session) {
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
const state = result.state;
|
|
1306
|
+
const alreadyPersisted = state._currentTurnPersistedItemCount ?? 0;
|
|
1307
|
+
const newRunItems = result.newItems.slice(alreadyPersisted);
|
|
1308
|
+
const itemsToSave = extractOutputItemsFromRunItems(newRunItems);
|
|
1309
|
+
if (itemsToSave.length === 0) {
|
|
1310
|
+
state._currentTurnPersistedItemCount =
|
|
1311
|
+
alreadyPersisted + newRunItems.length;
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
const sanitizedItems = normalizeItemsForSessionPersistence(itemsToSave);
|
|
1315
|
+
await session.addItems(sanitizedItems);
|
|
1316
|
+
state._currentTurnPersistedItemCount = alreadyPersisted + newRunItems.length;
|
|
1317
|
+
}
|
|
1318
|
+
export async function prepareInputItemsWithSession(input, session, sessionInputCallback, options) {
|
|
1319
|
+
if (!session) {
|
|
1320
|
+
return {
|
|
1321
|
+
preparedInput: input,
|
|
1322
|
+
sessionItems: undefined,
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
const includeHistoryInPreparedInput = options?.includeHistoryInPreparedInput ?? true;
|
|
1326
|
+
const preserveDroppedNewItems = options?.preserveDroppedNewItems ?? false;
|
|
1327
|
+
const history = await session.getItems();
|
|
1328
|
+
const newInputItems = Array.isArray(input)
|
|
1329
|
+
? [...input]
|
|
1330
|
+
: toInputItemList(input);
|
|
1331
|
+
if (!sessionInputCallback) {
|
|
1332
|
+
return {
|
|
1333
|
+
preparedInput: includeHistoryInPreparedInput
|
|
1334
|
+
? [...history, ...newInputItems]
|
|
1335
|
+
: newInputItems,
|
|
1336
|
+
sessionItems: newInputItems,
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
// Capture snapshots before invoking the callback so we can reason about the original state even
|
|
1340
|
+
// if the callback mutates the history array in-place.
|
|
1341
|
+
const historySnapshot = history.slice();
|
|
1342
|
+
const newInputSnapshot = newInputItems.slice();
|
|
1343
|
+
// Delegate history reconciliation to the user-supplied callback. It must return a concrete list
|
|
1344
|
+
// to keep downstream model requests well-typed.
|
|
1345
|
+
const combined = await sessionInputCallback(history, newInputItems);
|
|
1346
|
+
if (!Array.isArray(combined)) {
|
|
1347
|
+
throw new UserError('Session input callback must return an array of AgentInputItem objects.');
|
|
1348
|
+
}
|
|
1349
|
+
const historyCounts = buildItemFrequencyMap(historySnapshot);
|
|
1350
|
+
const newInputCounts = buildItemFrequencyMap(newInputSnapshot);
|
|
1351
|
+
const historyRefs = buildItemReferenceMap(historySnapshot);
|
|
1352
|
+
const newInputRefs = buildItemReferenceMap(newInputSnapshot);
|
|
1353
|
+
const appended = [];
|
|
1354
|
+
for (const item of combined) {
|
|
1355
|
+
const key = sessionItemKey(item);
|
|
1356
|
+
if (consumeReference(newInputRefs, key, item)) {
|
|
1357
|
+
decrementCount(newInputCounts, key);
|
|
1358
|
+
appended.push(item);
|
|
1359
|
+
continue;
|
|
1360
|
+
}
|
|
1361
|
+
// Prioritize exact history matches before payload-based counts so callbacks that surface
|
|
1362
|
+
// history ahead of identical new inputs keep previously persisted items out of the new queue.
|
|
1363
|
+
if (consumeReference(historyRefs, key, item)) {
|
|
1364
|
+
decrementCount(historyCounts, key);
|
|
1365
|
+
continue;
|
|
1366
|
+
}
|
|
1367
|
+
const historyRemaining = historyCounts.get(key) ?? 0;
|
|
1368
|
+
if (historyRemaining > 0) {
|
|
1369
|
+
historyCounts.set(key, historyRemaining - 1);
|
|
1370
|
+
continue;
|
|
1371
|
+
}
|
|
1372
|
+
const newRemaining = newInputCounts.get(key) ?? 0;
|
|
1373
|
+
if (newRemaining > 0) {
|
|
1374
|
+
newInputCounts.set(key, newRemaining - 1);
|
|
1375
|
+
appended.push(item);
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
appended.push(item);
|
|
1379
|
+
}
|
|
1380
|
+
// Preserve redacted inputs for model delivery when requested (e.g. server-managed histories).
|
|
1381
|
+
const preparedItems = includeHistoryInPreparedInput
|
|
1382
|
+
? combined
|
|
1383
|
+
: appended.length > 0
|
|
1384
|
+
? appended
|
|
1385
|
+
: preserveDroppedNewItems
|
|
1386
|
+
? newInputSnapshot
|
|
1387
|
+
: [];
|
|
1388
|
+
return {
|
|
1389
|
+
preparedInput: preparedItems,
|
|
1390
|
+
// Respect callbacks that intentionally drop the latest inputs (e.g. to redact sensitive
|
|
1391
|
+
// values) by persisting only the items they kept in the combined array.
|
|
1392
|
+
sessionItems: appended,
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1395
|
+
/**
|
|
1396
|
+
* Accepts whatever the tool returned and attempts to coerce it into the structured protocol
|
|
1397
|
+
* shapes we expose to downstream model adapters (input_text/input_image/input_file). Tools are
|
|
1398
|
+
* allowed to return either a single structured object or an array of them; anything else falls
|
|
1399
|
+
* back to the legacy string pipeline.
|
|
1400
|
+
*/
|
|
1401
|
+
function normalizeStructuredToolOutputs(output) {
|
|
1402
|
+
if (Array.isArray(output)) {
|
|
1403
|
+
const structured = [];
|
|
1404
|
+
for (const item of output) {
|
|
1405
|
+
const normalized = normalizeStructuredToolOutput(item);
|
|
1406
|
+
if (!normalized) {
|
|
1407
|
+
return null;
|
|
1408
|
+
}
|
|
1409
|
+
structured.push(normalized);
|
|
1410
|
+
}
|
|
1411
|
+
return structured;
|
|
1412
|
+
}
|
|
1413
|
+
const normalized = normalizeStructuredToolOutput(output);
|
|
1414
|
+
return normalized ? [normalized] : null;
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* Best-effort normalization of a single tool output item. If the object already matches the
|
|
1418
|
+
* protocol shape we simply cast it; otherwise we copy the recognised fields into the canonical
|
|
1419
|
+
* structure. Returning null lets the caller know we should revert to plain-string handling.
|
|
1420
|
+
*/
|
|
1421
|
+
function normalizeStructuredToolOutput(value) {
|
|
1422
|
+
if (!isRecord(value)) {
|
|
1423
|
+
return null;
|
|
1424
|
+
}
|
|
1425
|
+
const type = value.type;
|
|
1426
|
+
if (type === 'text' && typeof value.text === 'string') {
|
|
1427
|
+
const output = { type: 'text', text: value.text };
|
|
1428
|
+
if (isRecord(value.providerData)) {
|
|
1429
|
+
output.providerData = value.providerData;
|
|
1430
|
+
}
|
|
1431
|
+
return output;
|
|
1432
|
+
}
|
|
1433
|
+
if (type === 'image') {
|
|
1434
|
+
const output = { type: 'image' };
|
|
1435
|
+
let imageString;
|
|
1436
|
+
let imageFileId;
|
|
1437
|
+
const fallbackImageMediaType = isNonEmptyString(value.mediaType)
|
|
1438
|
+
? value.mediaType
|
|
1439
|
+
: undefined;
|
|
1440
|
+
const imageField = value.image;
|
|
1441
|
+
if (typeof imageField === 'string' && imageField.length > 0) {
|
|
1442
|
+
imageString = imageField;
|
|
1443
|
+
}
|
|
1444
|
+
else if (isRecord(imageField)) {
|
|
1445
|
+
const imageObj = imageField;
|
|
1446
|
+
const inlineMediaType = isNonEmptyString(imageObj.mediaType)
|
|
1447
|
+
? imageObj.mediaType
|
|
1448
|
+
: fallbackImageMediaType;
|
|
1449
|
+
if (isNonEmptyString(imageObj.url)) {
|
|
1450
|
+
imageString = imageObj.url;
|
|
1451
|
+
}
|
|
1452
|
+
else if (isNonEmptyString(imageObj.data)) {
|
|
1453
|
+
imageString = toInlineImageString(imageObj.data, inlineMediaType);
|
|
1454
|
+
}
|
|
1455
|
+
else if (imageObj.data instanceof Uint8Array &&
|
|
1456
|
+
imageObj.data.length > 0) {
|
|
1457
|
+
imageString = toInlineImageString(imageObj.data, inlineMediaType);
|
|
1458
|
+
}
|
|
1459
|
+
if (!imageString) {
|
|
1460
|
+
const candidateId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
|
|
1461
|
+
(isNonEmptyString(imageObj.id) && imageObj.id) ||
|
|
1462
|
+
undefined;
|
|
1463
|
+
if (candidateId) {
|
|
1464
|
+
imageFileId = candidateId;
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
if (!imageString &&
|
|
1469
|
+
typeof value.imageUrl === 'string' &&
|
|
1470
|
+
value.imageUrl.length > 0) {
|
|
1471
|
+
imageString = value.imageUrl;
|
|
1472
|
+
}
|
|
1473
|
+
if (!imageFileId &&
|
|
1474
|
+
typeof value.fileId === 'string' &&
|
|
1475
|
+
value.fileId.length > 0) {
|
|
1476
|
+
imageFileId = value.fileId;
|
|
1477
|
+
}
|
|
1478
|
+
if (!imageString &&
|
|
1479
|
+
typeof value.data === 'string' &&
|
|
1480
|
+
value.data.length > 0) {
|
|
1481
|
+
imageString = fallbackImageMediaType
|
|
1482
|
+
? toInlineImageString(value.data, fallbackImageMediaType)
|
|
1483
|
+
: value.data;
|
|
1484
|
+
}
|
|
1485
|
+
else if (!imageString &&
|
|
1486
|
+
value.data instanceof Uint8Array &&
|
|
1487
|
+
value.data.length > 0) {
|
|
1488
|
+
imageString = toInlineImageString(value.data, fallbackImageMediaType);
|
|
1489
|
+
}
|
|
1490
|
+
if (typeof value.detail === 'string' && value.detail.length > 0) {
|
|
1491
|
+
output.detail = value.detail;
|
|
1492
|
+
}
|
|
1493
|
+
if (imageString) {
|
|
1494
|
+
output.image = imageString;
|
|
1495
|
+
}
|
|
1496
|
+
else if (imageFileId) {
|
|
1497
|
+
output.image = { fileId: imageFileId };
|
|
1498
|
+
}
|
|
1499
|
+
else {
|
|
1500
|
+
return null;
|
|
1501
|
+
}
|
|
1502
|
+
if (isRecord(value.providerData)) {
|
|
1503
|
+
output.providerData = value.providerData;
|
|
1504
|
+
}
|
|
1505
|
+
return output;
|
|
1506
|
+
}
|
|
1507
|
+
if (type === 'file') {
|
|
1508
|
+
const fileValue = normalizeFileValue(value);
|
|
1509
|
+
if (!fileValue) {
|
|
1510
|
+
return null;
|
|
1511
|
+
}
|
|
1512
|
+
const output = { type: 'file', file: fileValue };
|
|
1513
|
+
if (isRecord(value.providerData)) {
|
|
1514
|
+
output.providerData = value.providerData;
|
|
1515
|
+
}
|
|
1516
|
+
return output;
|
|
1517
|
+
}
|
|
1518
|
+
return null;
|
|
1519
|
+
}
|
|
1520
|
+
/**
|
|
1521
|
+
* Translates the normalized tool output into the protocol `input_*` items. This is the last hop
|
|
1522
|
+
* before we hand the data to model-specific adapters, so we generate the exact schema expected by
|
|
1523
|
+
* the protocol definitions.
|
|
1524
|
+
*/
|
|
1525
|
+
function convertStructuredToolOutputToInputItem(output) {
|
|
1526
|
+
if (output.type === 'text') {
|
|
1527
|
+
const result = {
|
|
1528
|
+
type: 'input_text',
|
|
1529
|
+
text: output.text,
|
|
1530
|
+
};
|
|
1531
|
+
if (output.providerData) {
|
|
1532
|
+
result.providerData = output.providerData;
|
|
1533
|
+
}
|
|
1534
|
+
return result;
|
|
1535
|
+
}
|
|
1536
|
+
if (output.type === 'image') {
|
|
1537
|
+
const result = { type: 'input_image' };
|
|
1538
|
+
if (typeof output.detail === 'string' && output.detail.length > 0) {
|
|
1539
|
+
result.detail = output.detail;
|
|
1540
|
+
}
|
|
1541
|
+
if (typeof output.image === 'string' && output.image.length > 0) {
|
|
1542
|
+
result.image = output.image;
|
|
1543
|
+
}
|
|
1544
|
+
else if (isRecord(output.image)) {
|
|
1545
|
+
const imageObj = output.image;
|
|
1546
|
+
const inlineMediaType = isNonEmptyString(imageObj.mediaType)
|
|
1547
|
+
? imageObj.mediaType
|
|
1548
|
+
: undefined;
|
|
1549
|
+
if (isNonEmptyString(imageObj.url)) {
|
|
1550
|
+
result.image = imageObj.url;
|
|
1551
|
+
}
|
|
1552
|
+
else if (isNonEmptyString(imageObj.data)) {
|
|
1553
|
+
result.image =
|
|
1554
|
+
inlineMediaType && !imageObj.data.startsWith('data:')
|
|
1555
|
+
? asDataUrl(imageObj.data, inlineMediaType)
|
|
1556
|
+
: imageObj.data;
|
|
1557
|
+
}
|
|
1558
|
+
else if (imageObj.data instanceof Uint8Array &&
|
|
1559
|
+
imageObj.data.length > 0) {
|
|
1560
|
+
const base64 = encodeUint8ArrayToBase64(imageObj.data);
|
|
1561
|
+
result.image = asDataUrl(base64, inlineMediaType);
|
|
1562
|
+
}
|
|
1563
|
+
else {
|
|
1564
|
+
const referencedId = (isNonEmptyString(imageObj.fileId) && imageObj.fileId) ||
|
|
1565
|
+
(isNonEmptyString(imageObj.id) && imageObj.id) ||
|
|
1566
|
+
undefined;
|
|
1567
|
+
if (referencedId) {
|
|
1568
|
+
result.image = { id: referencedId };
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
if (output.providerData) {
|
|
1573
|
+
result.providerData = output.providerData;
|
|
1574
|
+
}
|
|
1575
|
+
return result;
|
|
1576
|
+
}
|
|
1577
|
+
if (output.type === 'file') {
|
|
1578
|
+
const result = { type: 'input_file' };
|
|
1579
|
+
const fileValue = output.file;
|
|
1580
|
+
if (typeof fileValue === 'string') {
|
|
1581
|
+
result.file = fileValue;
|
|
1582
|
+
}
|
|
1583
|
+
else if (fileValue && typeof fileValue === 'object') {
|
|
1584
|
+
const record = fileValue;
|
|
1585
|
+
if ('data' in record && record.data) {
|
|
1586
|
+
const mediaType = record.mediaType ?? 'text/plain';
|
|
1587
|
+
if (typeof record.data === 'string') {
|
|
1588
|
+
result.file = asDataUrl(record.data, mediaType);
|
|
1589
|
+
}
|
|
1590
|
+
else {
|
|
1591
|
+
const base64 = encodeUint8ArrayToBase64(record.data);
|
|
1592
|
+
result.file = asDataUrl(base64, mediaType);
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
else if (typeof record.url === 'string' && record.url.length > 0) {
|
|
1596
|
+
result.file = { url: record.url };
|
|
1597
|
+
}
|
|
1598
|
+
else {
|
|
1599
|
+
const referencedId = (typeof record.id === 'string' &&
|
|
1600
|
+
record.id.length > 0 &&
|
|
1601
|
+
record.id) ||
|
|
1602
|
+
(typeof record.fileId === 'string' && record.fileId.length > 0
|
|
1603
|
+
? record.fileId
|
|
1604
|
+
: undefined);
|
|
1605
|
+
if (referencedId) {
|
|
1606
|
+
result.file = { id: referencedId };
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
if (typeof record.filename === 'string' && record.filename.length > 0) {
|
|
1610
|
+
result.filename = record.filename;
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
if (output.providerData) {
|
|
1614
|
+
result.providerData = output.providerData;
|
|
1615
|
+
}
|
|
1616
|
+
return result;
|
|
1617
|
+
}
|
|
1618
|
+
const exhaustiveCheck = output;
|
|
1619
|
+
return exhaustiveCheck;
|
|
1620
|
+
}
|
|
1621
|
+
function buildItemFrequencyMap(items) {
|
|
1622
|
+
const counts = new Map();
|
|
1623
|
+
for (const item of items) {
|
|
1624
|
+
const key = sessionItemKey(item);
|
|
1625
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
1626
|
+
}
|
|
1627
|
+
return counts;
|
|
1628
|
+
}
|
|
1629
|
+
function buildItemReferenceMap(items) {
|
|
1630
|
+
const refs = new Map();
|
|
1631
|
+
for (const item of items) {
|
|
1632
|
+
const key = sessionItemKey(item);
|
|
1633
|
+
const list = refs.get(key);
|
|
1634
|
+
if (list) {
|
|
1635
|
+
list.push(item);
|
|
1636
|
+
}
|
|
1637
|
+
else {
|
|
1638
|
+
refs.set(key, [item]);
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
return refs;
|
|
1642
|
+
}
|
|
1643
|
+
function consumeReference(refs, key, target) {
|
|
1644
|
+
const candidates = refs.get(key);
|
|
1645
|
+
if (!candidates || candidates.length === 0) {
|
|
1646
|
+
return false;
|
|
1647
|
+
}
|
|
1648
|
+
const index = candidates.findIndex((candidate) => candidate === target);
|
|
1649
|
+
if (index === -1) {
|
|
1650
|
+
return false;
|
|
1651
|
+
}
|
|
1652
|
+
candidates.splice(index, 1);
|
|
1653
|
+
if (candidates.length === 0) {
|
|
1654
|
+
refs.delete(key);
|
|
1655
|
+
}
|
|
1656
|
+
return true;
|
|
1657
|
+
}
|
|
1658
|
+
function decrementCount(map, key) {
|
|
1659
|
+
const remaining = (map.get(key) ?? 0) - 1;
|
|
1660
|
+
if (remaining <= 0) {
|
|
1661
|
+
map.delete(key);
|
|
1662
|
+
}
|
|
1663
|
+
else {
|
|
1664
|
+
map.set(key, remaining);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
function sessionItemKey(item) {
|
|
1668
|
+
return JSON.stringify(item, sessionSerializationReplacer);
|
|
1669
|
+
}
|
|
1670
|
+
function sessionSerializationReplacer(_key, value) {
|
|
1671
|
+
if (value instanceof ArrayBuffer) {
|
|
1672
|
+
return {
|
|
1673
|
+
__type: 'ArrayBuffer',
|
|
1674
|
+
data: encodeUint8ArrayToBase64(new Uint8Array(value)),
|
|
1675
|
+
};
|
|
1676
|
+
}
|
|
1677
|
+
if (isArrayBufferView(value)) {
|
|
1678
|
+
const view = value;
|
|
1679
|
+
return {
|
|
1680
|
+
__type: view.constructor.name,
|
|
1681
|
+
data: encodeUint8ArrayToBase64(new Uint8Array(view.buffer, view.byteOffset, view.byteLength)),
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
if (isNodeBuffer(value)) {
|
|
1685
|
+
const view = value;
|
|
1686
|
+
return {
|
|
1687
|
+
__type: 'Buffer',
|
|
1688
|
+
data: encodeUint8ArrayToBase64(new Uint8Array(view.buffer, view.byteOffset, view.byteLength)),
|
|
1689
|
+
};
|
|
1690
|
+
}
|
|
1691
|
+
if (isSerializedBufferSnapshot(value)) {
|
|
1692
|
+
return {
|
|
1693
|
+
__type: 'Buffer',
|
|
1694
|
+
data: encodeUint8ArrayToBase64(Uint8Array.from(value.data)),
|
|
1695
|
+
};
|
|
1696
|
+
}
|
|
1697
|
+
return value;
|
|
1698
|
+
}
|
|
1166
1699
|
//# sourceMappingURL=runImplementation.mjs.map
|