-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathdo-stream-step.ts
More file actions
749 lines (700 loc) · 23.2 KB
/
do-stream-step.ts
File metadata and controls
749 lines (700 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import type {
LanguageModelV3CallOptions,
LanguageModelV3Prompt,
LanguageModelV3StreamPart,
LanguageModelV3ToolCall,
LanguageModelV3ToolChoice,
SharedV3ProviderOptions,
} from '@ai-sdk/provider';
import {
type FinishReason,
gateway,
generateId,
type StepResult,
type StopCondition,
type ToolChoice,
type ToolSet,
type UIMessageChunk,
} from 'ai';
import type {
ProviderOptions,
StreamTextTransform,
TelemetrySettings,
} from './durable-agent.js';
import { recordSpan } from './telemetry.js';
import type { CompatibleLanguageModel } from './types.js';
export type FinishPart = Extract<LanguageModelV3StreamPart, { type: 'finish' }>;
export type ModelStopCondition = StopCondition<NoInfer<ToolSet>>;
/**
* Provider-executed tool result captured from the stream.
*/
export interface ProviderExecutedToolResult {
toolCallId: string;
toolName: string;
result: unknown;
isError?: boolean;
}
/**
* Convert a Uint8Array to a base64 string safely.
* Uses a loop instead of spread operator to avoid stack overflow on large arrays.
*/
function uint8ArrayToBase64(data: Uint8Array): string {
let binary = '';
for (let i = 0; i < data.length; i++) {
binary += String.fromCharCode(data[i]);
}
return btoa(binary);
}
/**
* Options for the doStreamStep function.
*/
export interface DoStreamStepOptions {
sendStart?: boolean;
maxOutputTokens?: number;
temperature?: number;
topP?: number;
topK?: number;
presencePenalty?: number;
frequencyPenalty?: number;
stopSequences?: string[];
seed?: number;
maxRetries?: number;
abortSignal?: AbortSignal;
headers?: Record<string, string | undefined>;
providerOptions?: ProviderOptions;
toolChoice?: ToolChoice<ToolSet>;
includeRawChunks?: boolean;
experimental_telemetry?: TelemetrySettings;
transforms?: Array<StreamTextTransform<ToolSet>>;
responseFormat?: LanguageModelV3CallOptions['responseFormat'];
/**
* If true, collects and returns all UIMessageChunks written to the stream.
* This is used by DurableAgent when collectUIMessages is enabled.
*/
collectUIChunks?: boolean;
}
/**
* Convert AI SDK ToolChoice to LanguageModelV3ToolChoice
*/
function toLanguageModelToolChoice(
toolChoice: ToolChoice<ToolSet> | undefined
): LanguageModelV3ToolChoice | undefined {
if (toolChoice === undefined) {
return undefined;
}
if (toolChoice === 'auto') {
return { type: 'auto' };
}
if (toolChoice === 'none') {
return { type: 'none' };
}
if (toolChoice === 'required') {
return { type: 'required' };
}
if (typeof toolChoice === 'object' && toolChoice.type === 'tool') {
return { type: 'tool', toolName: toolChoice.toolName };
}
return undefined;
}
export async function doStreamStep(
conversationPrompt: LanguageModelV3Prompt,
modelInit: string | (() => Promise<CompatibleLanguageModel>),
writable: WritableStream<UIMessageChunk>,
tools?: LanguageModelV3CallOptions['tools'],
options?: DoStreamStepOptions
) {
'use step';
let model: CompatibleLanguageModel | undefined;
if (typeof modelInit === 'string') {
model = gateway(modelInit) as CompatibleLanguageModel;
} else if (typeof modelInit === 'function') {
// User-provided model factory - returns V3
model = await modelInit();
} else {
throw new Error(
'Invalid "model initialization" argument. Must be a string or a function that returns a LanguageModel instance.'
);
}
// Build call options with all generation settings
const callOptions: LanguageModelV3CallOptions = {
prompt: conversationPrompt,
tools,
...(options?.maxOutputTokens !== undefined && {
maxOutputTokens: options.maxOutputTokens,
}),
...(options?.temperature !== undefined && {
temperature: options.temperature,
}),
...(options?.topP !== undefined && { topP: options.topP }),
...(options?.topK !== undefined && { topK: options.topK }),
...(options?.presencePenalty !== undefined && {
presencePenalty: options.presencePenalty,
}),
...(options?.frequencyPenalty !== undefined && {
frequencyPenalty: options.frequencyPenalty,
}),
...(options?.stopSequences !== undefined && {
stopSequences: options.stopSequences,
}),
...(options?.seed !== undefined && { seed: options.seed }),
...(options?.abortSignal !== undefined && {
abortSignal: options.abortSignal,
}),
...(options?.headers !== undefined && { headers: options.headers }),
...(options?.providerOptions !== undefined && {
providerOptions: options.providerOptions as SharedV3ProviderOptions,
}),
...(options?.toolChoice !== undefined && {
toolChoice: toLanguageModelToolChoice(options.toolChoice),
}),
...(options?.includeRawChunks !== undefined && {
includeRawChunks: options.includeRawChunks,
}),
...(options?.responseFormat !== undefined && {
responseFormat: options.responseFormat,
}),
};
const result = await recordSpan({
name: 'ai.streamText.doStream',
telemetry: options?.experimental_telemetry,
attributes: {
'ai.model.provider': model.provider,
'ai.model.id': model.modelId,
// gen_ai semantic convention attributes
'gen_ai.system': model.provider,
'gen_ai.request.model': model.modelId,
...(options?.maxOutputTokens !== undefined && {
'gen_ai.request.max_tokens': options.maxOutputTokens,
}),
...(options?.temperature !== undefined && {
'gen_ai.request.temperature': options.temperature,
}),
...(options?.topP !== undefined && {
'gen_ai.request.top_p': options.topP,
}),
...(options?.topK !== undefined && {
'gen_ai.request.top_k': options.topK,
}),
...(options?.frequencyPenalty !== undefined && {
'gen_ai.request.frequency_penalty': options.frequencyPenalty,
}),
...(options?.presencePenalty !== undefined && {
'gen_ai.request.presence_penalty': options.presencePenalty,
}),
...(options?.stopSequences !== undefined && {
'gen_ai.request.stop_sequences': options.stopSequences,
}),
},
fn: () => model!.doStream(callOptions),
});
let finish: FinishPart | undefined;
const toolCalls: LanguageModelV3ToolCall[] = [];
// Map of tool call ID to provider-executed tool result
const providerExecutedToolResults = new Map<
string,
ProviderExecutedToolResult
>();
const chunks: LanguageModelV3StreamPart[] = [];
const includeRawChunks = options?.includeRawChunks ?? false;
const collectUIChunks = options?.collectUIChunks ?? false;
const uiChunks: UIMessageChunk[] = [];
// Build the stream pipeline
let stream: ReadableStream<LanguageModelV3StreamPart> = result.stream;
// Apply custom transforms if provided
if (options?.transforms && options.transforms.length > 0) {
let terminated = false;
const stopStream = () => {
terminated = true;
};
for (const transform of options.transforms) {
if (!terminated) {
stream = stream.pipeThrough(
transform({
tools: {} as ToolSet, // Note: toolSet not available inside step boundary due to serialization
stopStream,
})
);
}
}
}
await stream
.pipeThrough(
new TransformStream({
async transform(chunk, controller) {
if (chunk.type === 'tool-call') {
toolCalls.push({
...chunk,
input: chunk.input || '{}',
});
} else if (chunk.type === 'tool-result') {
// In V3, all tool-result stream parts are provider-executed by definition
providerExecutedToolResults.set(chunk.toolCallId, {
toolCallId: chunk.toolCallId,
toolName: chunk.toolName,
result: chunk.result,
isError: chunk.isError,
});
} else if (chunk.type === 'finish') {
finish = chunk;
}
chunks.push(chunk);
controller.enqueue(chunk);
},
})
)
.pipeThrough(
new TransformStream<LanguageModelV3StreamPart, UIMessageChunk>({
start: (controller) => {
if (options?.sendStart) {
controller.enqueue({
type: 'start',
// Note that if useChat is used client-side, useChat will generate a different
// messageId. It's hard to work around this.
messageId: generateId(),
});
}
controller.enqueue({
type: 'start-step',
});
},
flush: (controller) => {
controller.enqueue({
type: 'finish-step',
});
},
transform: async (part, controller) => {
const partType = part.type;
switch (partType) {
case 'text-start': {
controller.enqueue({
type: 'text-start',
id: part.id,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
break;
}
case 'text-delta': {
controller.enqueue({
type: 'text-delta',
id: part.id,
delta: part.delta,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
break;
}
case 'text-end': {
controller.enqueue({
type: 'text-end',
id: part.id,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
break;
}
case 'reasoning-start': {
controller.enqueue({
type: 'reasoning-start',
id: part.id,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
break;
}
case 'reasoning-delta': {
controller.enqueue({
type: 'reasoning-delta',
id: part.id,
delta: part.delta,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
break;
}
case 'reasoning-end': {
controller.enqueue({
type: 'reasoning-end',
id: part.id,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
break;
}
case 'file': {
// Convert data to URL, handling Uint8Array, URL, and string cases
let url: string;
const fileData = part.data as Uint8Array | string | URL;
if (fileData instanceof Uint8Array) {
// Convert Uint8Array to base64 and create data URL
const base64 = uint8ArrayToBase64(fileData);
url = `data:${part.mediaType};base64,${base64}`;
} else if (fileData instanceof URL) {
// Use URL directly (could be a data URL or remote URL)
url = fileData.href;
} else if (
fileData.startsWith('data:') ||
fileData.startsWith('http:') ||
fileData.startsWith('https:')
) {
// Already a URL string
url = fileData;
} else {
// Assume it's base64-encoded data
url = `data:${part.mediaType};base64,${fileData}`;
}
controller.enqueue({
type: 'file',
mediaType: part.mediaType,
url,
});
break;
}
case 'source': {
if (part.sourceType === 'url') {
controller.enqueue({
type: 'source-url',
sourceId: part.id,
url: part.url,
title: part.title,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
}
if (part.sourceType === 'document') {
controller.enqueue({
type: 'source-document',
sourceId: part.id,
mediaType: part.mediaType,
title: part.title,
filename: part.filename,
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
}
break;
}
case 'tool-input-start': {
controller.enqueue({
type: 'tool-input-start',
toolCallId: part.id,
toolName: part.toolName,
...(part.providerExecuted != null
? { providerExecuted: part.providerExecuted }
: {}),
});
break;
}
case 'tool-input-delta': {
controller.enqueue({
type: 'tool-input-delta',
toolCallId: part.id,
inputTextDelta: part.delta,
});
break;
}
case 'tool-input-end': {
// End of tool input streaming - no UI chunk needed
break;
}
case 'tool-call': {
controller.enqueue({
type: 'tool-input-available',
toolCallId: part.toolCallId,
toolName: part.toolName,
input: JSON.parse(part.input || '{}'),
...(part.providerExecuted != null
? { providerExecuted: part.providerExecuted }
: {}),
...(part.providerMetadata != null
? { providerMetadata: part.providerMetadata }
: {}),
});
break;
}
case 'tool-result': {
controller.enqueue({
type: 'tool-output-available',
toolCallId: part.toolCallId,
output: part.result,
});
break;
}
case 'error': {
const error = part.error;
controller.enqueue({
type: 'error',
errorText:
error instanceof Error ? error.message : String(error),
});
break;
}
case 'stream-start': {
// Stream start is internal, no UI chunk needed
break;
}
case 'response-metadata': {
// Response metadata is internal, no UI chunk needed
break;
}
case 'finish': {
// Finish is handled separately
break;
}
case 'raw': {
// Raw chunks are only included if explicitly requested
if (includeRawChunks) {
// Raw chunks contain provider-specific data
// We don't have a direct mapping to UIMessageChunk
// but we can log or handle them if needed
}
break;
}
default: {
// Handle any other chunk types gracefully
// const exhaustiveCheck: never = partType;
// console.warn(`Unknown chunk type: ${partType}`);
}
}
},
})
)
.pipeThrough(
// Optionally collect UIMessageChunks for later conversion to UIMessage[]
new TransformStream<UIMessageChunk, UIMessageChunk>({
transform: (chunk, controller) => {
if (collectUIChunks) {
uiChunks.push(chunk);
}
controller.enqueue(chunk);
},
})
)
.pipeTo(writable, { preventClose: true });
const step = chunksToStep(chunks, toolCalls, conversationPrompt, finish);
return {
toolCalls,
finish,
step,
uiChunks: collectUIChunks ? uiChunks : undefined,
providerExecutedToolResults,
};
}
/**
* Normalize the finish reason to the AI SDK FinishReason type.
* AI SDK v6 may return an object with a 'type' property,
* while AI SDK v5 returns a plain string. This function handles both.
*
* @internal Exported for testing
*/
export function normalizeFinishReason(rawFinishReason: unknown): FinishReason {
const KNOWN_FINISH_REASONS = new Set<string>([
'stop',
'length',
'content-filter',
'tool-calls',
'error',
'other',
]);
// Handle object-style finish reason (V3 returns { unified, raw })
if (typeof rawFinishReason === 'object' && rawFinishReason !== null) {
const objReason = rawFinishReason as { unified?: string; type?: string };
const extracted = objReason.unified ?? objReason.type ?? 'other';
return (
KNOWN_FINISH_REASONS.has(extracted) ? extracted : 'other'
) as FinishReason;
}
// Handle string finish reason (standard format)
if (typeof rawFinishReason === 'string') {
return rawFinishReason as FinishReason;
}
return 'other';
}
// This is a stand-in for logic in the AI-SDK streamText code which aggregates
// chunks into a single step result.
function chunksToStep(
chunks: LanguageModelV3StreamPart[],
toolCalls: LanguageModelV3ToolCall[],
conversationPrompt: LanguageModelV3Prompt,
finish?: FinishPart
): StepResult<any> {
// Transform chunks to a single step result
const text = chunks
.filter(
(chunk): chunk is Extract<typeof chunk, { type: 'text-delta' }> =>
chunk.type === 'text-delta'
)
.map((chunk) => chunk.delta)
.join('');
const reasoning = chunks.filter(
(chunk): chunk is Extract<typeof chunk, { type: 'reasoning-delta' }> =>
chunk.type === 'reasoning-delta'
);
const reasoningText = reasoning.map((chunk) => chunk.delta).join('');
// Extract warnings from stream-start chunk
const streamStart = chunks.find(
(chunk): chunk is Extract<typeof chunk, { type: 'stream-start' }> =>
chunk.type === 'stream-start'
);
// Extract response metadata from response-metadata chunk
const responseMetadata = chunks.find(
(chunk): chunk is Extract<typeof chunk, { type: 'response-metadata' }> =>
chunk.type === 'response-metadata'
);
// Extract files from file chunks
// File chunks contain mediaType and data (base64 string or Uint8Array)
// GeneratedFile requires both base64 and uint8Array properties
const files = chunks
.filter(
(chunk): chunk is Extract<typeof chunk, { type: 'file' }> =>
chunk.type === 'file'
)
.map((chunk) => {
const data = chunk.data;
// If data is already a Uint8Array, convert to base64; otherwise use as-is
if (data instanceof Uint8Array) {
// Convert Uint8Array to base64 string
const base64 = uint8ArrayToBase64(data);
return {
mediaType: chunk.mediaType,
base64,
uint8Array: data,
};
} else {
// Data is base64 string, decode to Uint8Array
const binaryString = atob(data);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return {
mediaType: chunk.mediaType,
base64: data,
uint8Array: bytes,
};
}
});
// Extract sources from source chunks
const sources = chunks
.filter(
(chunk): chunk is Extract<typeof chunk, { type: 'source' }> =>
chunk.type === 'source'
)
.map((chunk) => chunk);
// Extract the raw finish reason from the V3 finish reason object
const v3FinishReason = finish?.finishReason;
const rawFinishReason =
typeof v3FinishReason === 'object' && v3FinishReason !== null
? (v3FinishReason as { raw?: string }).raw
: typeof v3FinishReason === 'string'
? v3FinishReason
: undefined;
const stepResult: StepResult<any> = {
stepNumber: 0, // Will be overridden by the caller
model: {
provider: responseMetadata?.modelId?.split(':')[0] ?? 'unknown',
modelId: responseMetadata?.modelId ?? 'unknown',
},
functionId: undefined,
metadata: undefined,
experimental_context: undefined,
content: [
...(text ? [{ type: 'text' as const, text }] : []),
...toolCalls.map((toolCall) => ({
type: 'tool-call' as const,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
input: JSON.parse(toolCall.input),
dynamic: true as const,
})),
],
text,
reasoning: reasoning.map((chunk) => ({
type: 'reasoning' as const,
text: chunk.delta,
})),
reasoningText: reasoningText || undefined,
files,
sources,
toolCalls: toolCalls.map((toolCall) => ({
type: 'tool-call' as const,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
input: JSON.parse(toolCall.input),
dynamic: true as const,
})),
staticToolCalls: [],
dynamicToolCalls: toolCalls.map((toolCall) => ({
type: 'tool-call' as const,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
input: JSON.parse(toolCall.input),
dynamic: true as const,
})),
toolResults: [],
staticToolResults: [],
dynamicToolResults: [],
finishReason: normalizeFinishReason(finish?.finishReason),
rawFinishReason,
usage: finish?.usage
? {
inputTokens: finish.usage.inputTokens?.total ?? 0,
inputTokenDetails: {
noCacheTokens: finish.usage.inputTokens?.noCache,
cacheReadTokens: finish.usage.inputTokens?.cacheRead,
cacheWriteTokens: finish.usage.inputTokens?.cacheWrite,
},
outputTokens: finish.usage.outputTokens?.total ?? 0,
outputTokenDetails: {
textTokens: finish.usage.outputTokens?.text,
reasoningTokens: finish.usage.outputTokens?.reasoning,
},
totalTokens:
(finish.usage.inputTokens?.total ?? 0) +
(finish.usage.outputTokens?.total ?? 0),
}
: {
inputTokens: 0,
inputTokenDetails: {
noCacheTokens: undefined,
cacheReadTokens: undefined,
cacheWriteTokens: undefined,
},
outputTokens: 0,
outputTokenDetails: {
textTokens: undefined,
reasoningTokens: undefined,
},
totalTokens: 0,
},
warnings: streamStart?.warnings,
request: {
body: JSON.stringify({
prompt: conversationPrompt,
tools: toolCalls.map((toolCall) => ({
type: 'tool-call' as const,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
input: JSON.parse(toolCall.input),
dynamic: true as const,
})),
}),
},
response: {
id: responseMetadata?.id ?? 'unknown',
timestamp: responseMetadata?.timestamp ?? new Date(),
modelId: responseMetadata?.modelId ?? 'unknown',
messages: [],
},
providerMetadata: finish?.providerMetadata || {},
};
return stepResult;
}