@mastra/client-js 0.10.15-alpha.1 → 0.10.15-alpha.3
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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +37 -0
- package/README.md +1 -0
- package/dist/index.cjs +327 -248
- package/dist/index.d.cts +124 -19
- package/dist/index.d.ts +124 -19
- package/dist/index.js +327 -248
- package/package.json +2 -2
- package/src/client.ts +97 -0
- package/src/index.test.ts +294 -6
- package/src/resources/agent.ts +272 -297
- package/src/resources/base.ts +3 -1
- package/src/resources/memory-thread.ts +18 -0
- package/src/resources/network.ts +4 -3
- package/src/resources/workflow.ts +9 -0
- package/src/types.ts +75 -1
- package/src/utils/process-client-tools.ts +1 -1
package/src/resources/agent.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
parsePartialJson,
|
|
3
3
|
processDataStream,
|
|
4
|
-
processTextStream,
|
|
5
4
|
type JSONValue,
|
|
6
5
|
type ReasoningUIPart,
|
|
7
6
|
type TextUIPart,
|
|
@@ -10,7 +9,8 @@ import {
|
|
|
10
9
|
type UIMessage,
|
|
11
10
|
type UseChatOptions,
|
|
12
11
|
} from '@ai-sdk/ui-utils';
|
|
13
|
-
import { Tool, type CoreMessage
|
|
12
|
+
import { Tool, type CoreMessage } from '@mastra/core';
|
|
13
|
+
import { type GenerateReturn } from '@mastra/core/llm';
|
|
14
14
|
import type { JSONSchema7 } from 'json-schema';
|
|
15
15
|
import { ZodSchema } from 'zod';
|
|
16
16
|
import { zodToJsonSchema } from '../utils/zod-to-json-schema';
|
|
@@ -118,18 +118,19 @@ export class Agent extends BaseResource {
|
|
|
118
118
|
* @param params - Generation parameters including prompt
|
|
119
119
|
* @returns Promise containing the generated response
|
|
120
120
|
*/
|
|
121
|
-
async generate
|
|
122
|
-
params: GenerateParams<
|
|
123
|
-
): Promise<GenerateReturn<
|
|
124
|
-
async generate<
|
|
125
|
-
params: GenerateParams<
|
|
126
|
-
): Promise<GenerateReturn<
|
|
127
|
-
async generate<
|
|
128
|
-
params: GenerateParams<
|
|
129
|
-
): Promise<GenerateReturn<
|
|
130
|
-
async generate<
|
|
131
|
-
|
|
132
|
-
|
|
121
|
+
async generate(
|
|
122
|
+
params: GenerateParams<undefined> & { output?: never; experimental_output?: never },
|
|
123
|
+
): Promise<GenerateReturn<any, undefined, undefined>>;
|
|
124
|
+
async generate<Output extends JSONSchema7 | ZodSchema>(
|
|
125
|
+
params: GenerateParams<Output> & { output: Output; experimental_output?: never },
|
|
126
|
+
): Promise<GenerateReturn<any, Output, undefined>>;
|
|
127
|
+
async generate<StructuredOutput extends JSONSchema7 | ZodSchema>(
|
|
128
|
+
params: GenerateParams<StructuredOutput> & { output?: never; experimental_output: StructuredOutput },
|
|
129
|
+
): Promise<GenerateReturn<any, undefined, StructuredOutput>>;
|
|
130
|
+
async generate<
|
|
131
|
+
Output extends JSONSchema7 | ZodSchema | undefined = undefined,
|
|
132
|
+
StructuredOutput extends JSONSchema7 | ZodSchema | undefined = undefined,
|
|
133
|
+
>(params: GenerateParams<Output>): Promise<GenerateReturn<any, Output, StructuredOutput>> {
|
|
133
134
|
const processedParams = {
|
|
134
135
|
...params,
|
|
135
136
|
output: params.output ? zodToJsonSchema(params.output) : undefined,
|
|
@@ -140,10 +141,13 @@ export class Agent extends BaseResource {
|
|
|
140
141
|
|
|
141
142
|
const { runId, resourceId, threadId, runtimeContext } = processedParams as GenerateParams;
|
|
142
143
|
|
|
143
|
-
const response: GenerateReturn<
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
144
|
+
const response: GenerateReturn<any, Output, StructuredOutput> = await this.request(
|
|
145
|
+
`/api/agents/${this.agentId}/generate`,
|
|
146
|
+
{
|
|
147
|
+
method: 'POST',
|
|
148
|
+
body: processedParams,
|
|
149
|
+
},
|
|
150
|
+
);
|
|
147
151
|
|
|
148
152
|
if (response.finishReason === 'tool-calls') {
|
|
149
153
|
const toolCalls = (
|
|
@@ -206,7 +210,6 @@ export class Agent extends BaseResource {
|
|
|
206
210
|
onFinish,
|
|
207
211
|
getCurrentDate = () => new Date(),
|
|
208
212
|
lastMessage,
|
|
209
|
-
streamProtocol,
|
|
210
213
|
}: {
|
|
211
214
|
stream: ReadableStream<Uint8Array>;
|
|
212
215
|
update: (options: { message: UIMessage; data: JSONValue[] | undefined; replaceLastMessage: boolean }) => void;
|
|
@@ -215,7 +218,6 @@ export class Agent extends BaseResource {
|
|
|
215
218
|
generateId?: () => string;
|
|
216
219
|
getCurrentDate?: () => Date;
|
|
217
220
|
lastMessage: UIMessage | undefined;
|
|
218
|
-
streamProtocol: 'text' | 'data';
|
|
219
221
|
}) {
|
|
220
222
|
const replaceLastMessage = lastMessage?.role === 'assistant';
|
|
221
223
|
let step = replaceLastMessage
|
|
@@ -299,265 +301,297 @@ export class Agent extends BaseResource {
|
|
|
299
301
|
});
|
|
300
302
|
}
|
|
301
303
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
304
|
+
await processDataStream({
|
|
305
|
+
stream,
|
|
306
|
+
onTextPart(value) {
|
|
307
|
+
if (currentTextPart == null) {
|
|
308
|
+
currentTextPart = {
|
|
309
|
+
type: 'text',
|
|
310
|
+
text: value,
|
|
311
|
+
};
|
|
312
|
+
message.parts.push(currentTextPart);
|
|
313
|
+
} else {
|
|
314
|
+
currentTextPart.text += value;
|
|
315
|
+
}
|
|
310
316
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
text: value,
|
|
320
|
-
};
|
|
321
|
-
message.parts.push(currentTextPart);
|
|
322
|
-
} else {
|
|
323
|
-
currentTextPart.text += value;
|
|
317
|
+
message.content += value;
|
|
318
|
+
execUpdate();
|
|
319
|
+
},
|
|
320
|
+
onReasoningPart(value) {
|
|
321
|
+
if (currentReasoningTextDetail == null) {
|
|
322
|
+
currentReasoningTextDetail = { type: 'text', text: value };
|
|
323
|
+
if (currentReasoningPart != null) {
|
|
324
|
+
currentReasoningPart.details.push(currentReasoningTextDetail);
|
|
324
325
|
}
|
|
326
|
+
} else {
|
|
327
|
+
currentReasoningTextDetail.text += value;
|
|
328
|
+
}
|
|
325
329
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
currentReasoningTextDetail.text += value;
|
|
337
|
-
}
|
|
330
|
+
if (currentReasoningPart == null) {
|
|
331
|
+
currentReasoningPart = {
|
|
332
|
+
type: 'reasoning',
|
|
333
|
+
reasoning: value,
|
|
334
|
+
details: [currentReasoningTextDetail],
|
|
335
|
+
};
|
|
336
|
+
message.parts.push(currentReasoningPart);
|
|
337
|
+
} else {
|
|
338
|
+
currentReasoningPart.reasoning += value;
|
|
339
|
+
}
|
|
338
340
|
|
|
339
|
-
|
|
340
|
-
currentReasoningPart = {
|
|
341
|
-
type: 'reasoning',
|
|
342
|
-
reasoning: value,
|
|
343
|
-
details: [currentReasoningTextDetail],
|
|
344
|
-
};
|
|
345
|
-
message.parts.push(currentReasoningPart);
|
|
346
|
-
} else {
|
|
347
|
-
currentReasoningPart.reasoning += value;
|
|
348
|
-
}
|
|
341
|
+
message.reasoning = (message.reasoning ?? '') + value;
|
|
349
342
|
|
|
350
|
-
|
|
343
|
+
execUpdate();
|
|
344
|
+
},
|
|
345
|
+
onReasoningSignaturePart(value) {
|
|
346
|
+
if (currentReasoningTextDetail != null) {
|
|
347
|
+
currentReasoningTextDetail.signature = value.signature;
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
onRedactedReasoningPart(value) {
|
|
351
|
+
if (currentReasoningPart == null) {
|
|
352
|
+
currentReasoningPart = {
|
|
353
|
+
type: 'reasoning',
|
|
354
|
+
reasoning: '',
|
|
355
|
+
details: [],
|
|
356
|
+
};
|
|
357
|
+
message.parts.push(currentReasoningPart);
|
|
358
|
+
}
|
|
351
359
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
currentReasoningTextDetail.signature = value.signature;
|
|
357
|
-
}
|
|
358
|
-
},
|
|
359
|
-
onRedactedReasoningPart(value) {
|
|
360
|
-
if (currentReasoningPart == null) {
|
|
361
|
-
currentReasoningPart = {
|
|
362
|
-
type: 'reasoning',
|
|
363
|
-
reasoning: '',
|
|
364
|
-
details: [],
|
|
365
|
-
};
|
|
366
|
-
message.parts.push(currentReasoningPart);
|
|
367
|
-
}
|
|
360
|
+
currentReasoningPart.details.push({
|
|
361
|
+
type: 'redacted',
|
|
362
|
+
data: value.data,
|
|
363
|
+
});
|
|
368
364
|
|
|
369
|
-
|
|
370
|
-
type: 'redacted',
|
|
371
|
-
data: value.data,
|
|
372
|
-
});
|
|
365
|
+
currentReasoningTextDetail = undefined;
|
|
373
366
|
|
|
374
|
-
|
|
367
|
+
execUpdate();
|
|
368
|
+
},
|
|
369
|
+
onFilePart(value) {
|
|
370
|
+
message.parts.push({
|
|
371
|
+
type: 'file',
|
|
372
|
+
mimeType: value.mimeType,
|
|
373
|
+
data: value.data,
|
|
374
|
+
});
|
|
375
375
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
});
|
|
376
|
+
execUpdate();
|
|
377
|
+
},
|
|
378
|
+
onSourcePart(value) {
|
|
379
|
+
message.parts.push({
|
|
380
|
+
type: 'source',
|
|
381
|
+
source: value,
|
|
382
|
+
});
|
|
384
383
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
});
|
|
384
|
+
execUpdate();
|
|
385
|
+
},
|
|
386
|
+
onToolCallStreamingStartPart(value) {
|
|
387
|
+
if (message.toolInvocations == null) {
|
|
388
|
+
message.toolInvocations = [];
|
|
389
|
+
}
|
|
392
390
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
391
|
+
// add the partial tool call to the map
|
|
392
|
+
partialToolCalls[value.toolCallId] = {
|
|
393
|
+
text: '',
|
|
394
|
+
step,
|
|
395
|
+
toolName: value.toolName,
|
|
396
|
+
index: message.toolInvocations.length,
|
|
397
|
+
};
|
|
399
398
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
399
|
+
const invocation = {
|
|
400
|
+
state: 'partial-call',
|
|
401
|
+
step,
|
|
402
|
+
toolCallId: value.toolCallId,
|
|
403
|
+
toolName: value.toolName,
|
|
404
|
+
args: undefined,
|
|
405
|
+
} as const;
|
|
407
406
|
|
|
408
|
-
|
|
409
|
-
state: 'partial-call',
|
|
410
|
-
step,
|
|
411
|
-
toolCallId: value.toolCallId,
|
|
412
|
-
toolName: value.toolName,
|
|
413
|
-
args: undefined,
|
|
414
|
-
} as const;
|
|
407
|
+
message.toolInvocations.push(invocation);
|
|
415
408
|
|
|
416
|
-
|
|
409
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
417
410
|
|
|
418
|
-
|
|
411
|
+
execUpdate();
|
|
412
|
+
},
|
|
413
|
+
onToolCallDeltaPart(value) {
|
|
414
|
+
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
419
415
|
|
|
420
|
-
|
|
421
|
-
},
|
|
422
|
-
onToolCallDeltaPart(value) {
|
|
423
|
-
const partialToolCall = partialToolCalls[value.toolCallId];
|
|
416
|
+
partialToolCall!.text += value.argsTextDelta;
|
|
424
417
|
|
|
425
|
-
|
|
418
|
+
const { value: partialArgs } = parsePartialJson(partialToolCall!.text);
|
|
426
419
|
|
|
427
|
-
|
|
420
|
+
const invocation = {
|
|
421
|
+
state: 'partial-call',
|
|
422
|
+
step: partialToolCall!.step,
|
|
423
|
+
toolCallId: value.toolCallId,
|
|
424
|
+
toolName: partialToolCall!.toolName,
|
|
425
|
+
args: partialArgs,
|
|
426
|
+
} as const;
|
|
428
427
|
|
|
429
|
-
|
|
430
|
-
state: 'partial-call',
|
|
431
|
-
step: partialToolCall!.step,
|
|
432
|
-
toolCallId: value.toolCallId,
|
|
433
|
-
toolName: partialToolCall!.toolName,
|
|
434
|
-
args: partialArgs,
|
|
435
|
-
} as const;
|
|
428
|
+
message.toolInvocations![partialToolCall!.index] = invocation;
|
|
436
429
|
|
|
437
|
-
|
|
430
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
438
431
|
|
|
439
|
-
|
|
432
|
+
execUpdate();
|
|
433
|
+
},
|
|
434
|
+
async onToolCallPart(value) {
|
|
435
|
+
const invocation = {
|
|
436
|
+
state: 'call',
|
|
437
|
+
step,
|
|
438
|
+
...value,
|
|
439
|
+
} as const;
|
|
440
|
+
|
|
441
|
+
if (partialToolCalls[value.toolCallId] != null) {
|
|
442
|
+
// change the partial tool call to a full tool call
|
|
443
|
+
message.toolInvocations![partialToolCalls[value.toolCallId]!.index] = invocation;
|
|
444
|
+
} else {
|
|
445
|
+
if (message.toolInvocations == null) {
|
|
446
|
+
message.toolInvocations = [];
|
|
447
|
+
}
|
|
440
448
|
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
449
|
+
message.toolInvocations.push(invocation);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
453
|
+
|
|
454
|
+
execUpdate();
|
|
455
|
+
|
|
456
|
+
// invoke the onToolCall callback if it exists. This is blocking.
|
|
457
|
+
// In the future we should make this non-blocking, which
|
|
458
|
+
// requires additional state management for error handling etc.
|
|
459
|
+
if (onToolCall) {
|
|
460
|
+
const result = await onToolCall({ toolCall: value });
|
|
461
|
+
if (result != null) {
|
|
462
|
+
const invocation = {
|
|
463
|
+
state: 'result',
|
|
464
|
+
step,
|
|
465
|
+
...value,
|
|
466
|
+
result,
|
|
467
|
+
} as const;
|
|
468
|
+
|
|
469
|
+
// store the result in the tool invocation
|
|
470
|
+
message.toolInvocations![message.toolInvocations!.length - 1] = invocation;
|
|
457
471
|
|
|
458
|
-
|
|
472
|
+
updateToolInvocationPart(value.toolCallId, invocation);
|
|
473
|
+
|
|
474
|
+
execUpdate();
|
|
459
475
|
}
|
|
476
|
+
}
|
|
477
|
+
},
|
|
478
|
+
onToolResultPart(value) {
|
|
479
|
+
const toolInvocations = message.toolInvocations;
|
|
460
480
|
|
|
461
|
-
|
|
481
|
+
if (toolInvocations == null) {
|
|
482
|
+
throw new Error('tool_result must be preceded by a tool_call');
|
|
483
|
+
}
|
|
462
484
|
|
|
463
|
-
|
|
485
|
+
// find if there is any tool invocation with the same toolCallId
|
|
486
|
+
// and replace it with the result
|
|
487
|
+
const toolInvocationIndex = toolInvocations.findIndex(invocation => invocation.toolCallId === value.toolCallId);
|
|
464
488
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
if (onToolCall) {
|
|
469
|
-
const result = await onToolCall({ toolCall: value });
|
|
470
|
-
if (result != null) {
|
|
471
|
-
const invocation = {
|
|
472
|
-
state: 'result',
|
|
473
|
-
step,
|
|
474
|
-
...value,
|
|
475
|
-
result,
|
|
476
|
-
} as const;
|
|
489
|
+
if (toolInvocationIndex === -1) {
|
|
490
|
+
throw new Error('tool_result must be preceded by a tool_call with the same toolCallId');
|
|
491
|
+
}
|
|
477
492
|
|
|
478
|
-
|
|
479
|
-
|
|
493
|
+
const invocation = {
|
|
494
|
+
...toolInvocations[toolInvocationIndex],
|
|
495
|
+
state: 'result' as const,
|
|
496
|
+
...value,
|
|
497
|
+
} as const;
|
|
480
498
|
|
|
481
|
-
|
|
499
|
+
toolInvocations[toolInvocationIndex] = invocation as ToolInvocation;
|
|
482
500
|
|
|
483
|
-
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
},
|
|
487
|
-
onToolResultPart(value) {
|
|
488
|
-
const toolInvocations = message.toolInvocations;
|
|
501
|
+
updateToolInvocationPart(value.toolCallId, invocation as ToolInvocation);
|
|
489
502
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
503
|
+
execUpdate();
|
|
504
|
+
},
|
|
505
|
+
onDataPart(value) {
|
|
506
|
+
data.push(...value);
|
|
507
|
+
execUpdate();
|
|
508
|
+
},
|
|
509
|
+
onMessageAnnotationsPart(value) {
|
|
510
|
+
if (messageAnnotations == null) {
|
|
511
|
+
messageAnnotations = [...value];
|
|
512
|
+
} else {
|
|
513
|
+
messageAnnotations.push(...value);
|
|
514
|
+
}
|
|
493
515
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
);
|
|
516
|
+
execUpdate();
|
|
517
|
+
},
|
|
518
|
+
onFinishStepPart(value) {
|
|
519
|
+
step += 1;
|
|
499
520
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
521
|
+
// reset the current text and reasoning parts
|
|
522
|
+
currentTextPart = value.isContinued ? currentTextPart : undefined;
|
|
523
|
+
currentReasoningPart = undefined;
|
|
524
|
+
currentReasoningTextDetail = undefined;
|
|
525
|
+
},
|
|
526
|
+
onStartStepPart(value) {
|
|
527
|
+
// keep message id stable when we are updating an existing message:
|
|
528
|
+
if (!replaceLastMessage) {
|
|
529
|
+
message.id = value.messageId;
|
|
530
|
+
}
|
|
503
531
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
532
|
+
// add a step boundary part to the message
|
|
533
|
+
message.parts.push({ type: 'step-start' });
|
|
534
|
+
execUpdate();
|
|
535
|
+
},
|
|
536
|
+
onFinishMessagePart(value) {
|
|
537
|
+
finishReason = value.finishReason;
|
|
538
|
+
if (value.usage != null) {
|
|
539
|
+
// usage = calculateLanguageModelUsage(value.usage);
|
|
540
|
+
usage = value.usage;
|
|
541
|
+
}
|
|
542
|
+
},
|
|
543
|
+
onErrorPart(error) {
|
|
544
|
+
throw new Error(error);
|
|
545
|
+
},
|
|
546
|
+
});
|
|
509
547
|
|
|
510
|
-
|
|
548
|
+
onFinish?.({ message, finishReason, usage });
|
|
549
|
+
}
|
|
511
550
|
|
|
512
|
-
|
|
551
|
+
/**
|
|
552
|
+
* Streams a response from the agent
|
|
553
|
+
* @param params - Stream parameters including prompt
|
|
554
|
+
* @returns Promise containing the enhanced Response object with processDataStream method
|
|
555
|
+
*/
|
|
556
|
+
async stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
557
|
+
params: StreamParams<T>,
|
|
558
|
+
): Promise<
|
|
559
|
+
Response & {
|
|
560
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
561
|
+
}
|
|
562
|
+
> {
|
|
563
|
+
const processedParams = {
|
|
564
|
+
...params,
|
|
565
|
+
output: params.output ? zodToJsonSchema(params.output) : undefined,
|
|
566
|
+
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : undefined,
|
|
567
|
+
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
568
|
+
clientTools: processClientTools(params.clientTools),
|
|
569
|
+
};
|
|
513
570
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
onDataPart(value) {
|
|
517
|
-
data.push(...value);
|
|
518
|
-
execUpdate();
|
|
519
|
-
},
|
|
520
|
-
onMessageAnnotationsPart(value) {
|
|
521
|
-
if (messageAnnotations == null) {
|
|
522
|
-
messageAnnotations = [...value];
|
|
523
|
-
} else {
|
|
524
|
-
messageAnnotations.push(...value);
|
|
525
|
-
}
|
|
571
|
+
// Create a readable stream that will handle the response processing
|
|
572
|
+
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
|
|
526
573
|
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
onFinishStepPart(value) {
|
|
530
|
-
step += 1;
|
|
574
|
+
// Start processing the response in the background
|
|
575
|
+
const response = await this.processStreamResponse(processedParams, writable);
|
|
531
576
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
message.id = value.messageId;
|
|
541
|
-
}
|
|
577
|
+
// Create a new response with the readable stream
|
|
578
|
+
const streamResponse = new Response(readable, {
|
|
579
|
+
status: response.status,
|
|
580
|
+
statusText: response.statusText,
|
|
581
|
+
headers: response.headers,
|
|
582
|
+
}) as Response & {
|
|
583
|
+
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
584
|
+
};
|
|
542
585
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
finishReason = value.finishReason;
|
|
549
|
-
if (value.usage != null) {
|
|
550
|
-
// usage = calculateLanguageModelUsage(value.usage);
|
|
551
|
-
usage = value.usage;
|
|
552
|
-
}
|
|
553
|
-
},
|
|
554
|
-
onErrorPart(error) {
|
|
555
|
-
throw new Error(error);
|
|
556
|
-
},
|
|
586
|
+
// Add the processDataStream method to the response
|
|
587
|
+
streamResponse.processDataStream = async (options = {}) => {
|
|
588
|
+
await processDataStream({
|
|
589
|
+
stream: streamResponse.body as ReadableStream<Uint8Array>,
|
|
590
|
+
...options,
|
|
557
591
|
});
|
|
592
|
+
};
|
|
558
593
|
|
|
559
|
-
|
|
560
|
-
}
|
|
594
|
+
return streamResponse;
|
|
561
595
|
}
|
|
562
596
|
|
|
563
597
|
/**
|
|
@@ -577,7 +611,6 @@ export class Agent extends BaseResource {
|
|
|
577
611
|
}
|
|
578
612
|
|
|
579
613
|
try {
|
|
580
|
-
const streamProtocol = processedParams.output ? 'text' : 'data';
|
|
581
614
|
let toolCalls: ToolInvocation[] = [];
|
|
582
615
|
let finishReasonToolCalls = false;
|
|
583
616
|
let messages: UIMessage[] = [];
|
|
@@ -686,14 +719,11 @@ export class Agent extends BaseResource {
|
|
|
686
719
|
}
|
|
687
720
|
} else {
|
|
688
721
|
setTimeout(() => {
|
|
689
|
-
|
|
690
|
-
writable.close();
|
|
691
|
-
}
|
|
722
|
+
writable.close();
|
|
692
723
|
}, 0);
|
|
693
724
|
}
|
|
694
725
|
},
|
|
695
726
|
lastMessage: undefined,
|
|
696
|
-
streamProtocol,
|
|
697
727
|
});
|
|
698
728
|
} catch (error) {
|
|
699
729
|
console.error('Error processing stream response:', error);
|
|
@@ -701,61 +731,6 @@ export class Agent extends BaseResource {
|
|
|
701
731
|
return response;
|
|
702
732
|
}
|
|
703
733
|
|
|
704
|
-
/**
|
|
705
|
-
* Streams a response from the agent
|
|
706
|
-
* @param params - Stream parameters including prompt
|
|
707
|
-
* @returns Promise containing the enhanced Response object with processDataStream and processTextStream methods
|
|
708
|
-
*/
|
|
709
|
-
async stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
|
|
710
|
-
params: StreamParams<T>,
|
|
711
|
-
): Promise<
|
|
712
|
-
Response & {
|
|
713
|
-
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
714
|
-
processTextStream: (options?: Omit<Parameters<typeof processTextStream>[0], 'stream'>) => Promise<void>;
|
|
715
|
-
}
|
|
716
|
-
> {
|
|
717
|
-
const processedParams = {
|
|
718
|
-
...params,
|
|
719
|
-
output: params.output ? zodToJsonSchema(params.output) : undefined,
|
|
720
|
-
experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : undefined,
|
|
721
|
-
runtimeContext: parseClientRuntimeContext(params.runtimeContext),
|
|
722
|
-
clientTools: processClientTools(params.clientTools),
|
|
723
|
-
};
|
|
724
|
-
|
|
725
|
-
// Create a readable stream that will handle the response processing
|
|
726
|
-
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
|
|
727
|
-
// Start processing the response in the background
|
|
728
|
-
const response = await this.processStreamResponse(processedParams, writable);
|
|
729
|
-
|
|
730
|
-
// Create a new response with the readable stream
|
|
731
|
-
const streamResponse = new Response(readable, {
|
|
732
|
-
status: response.status,
|
|
733
|
-
statusText: response.statusText,
|
|
734
|
-
headers: response.headers,
|
|
735
|
-
}) as Response & {
|
|
736
|
-
processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
|
|
737
|
-
processTextStream: (options?: Omit<Parameters<typeof processTextStream>[0], 'stream'>) => Promise<void>;
|
|
738
|
-
};
|
|
739
|
-
|
|
740
|
-
// Add the processDataStream method to the response
|
|
741
|
-
streamResponse.processDataStream = async (options = {}) => {
|
|
742
|
-
await processDataStream({
|
|
743
|
-
stream: streamResponse.body as ReadableStream<Uint8Array>,
|
|
744
|
-
...options,
|
|
745
|
-
});
|
|
746
|
-
};
|
|
747
|
-
|
|
748
|
-
//Add the processTextStream method to the response
|
|
749
|
-
streamResponse.processTextStream = async options => {
|
|
750
|
-
await processTextStream({
|
|
751
|
-
stream: streamResponse.body as ReadableStream<Uint8Array>,
|
|
752
|
-
onTextPart: options?.onTextPart ?? (() => {}),
|
|
753
|
-
});
|
|
754
|
-
};
|
|
755
|
-
|
|
756
|
-
return streamResponse;
|
|
757
|
-
}
|
|
758
|
-
|
|
759
734
|
/**
|
|
760
735
|
* Gets details about a specific tool available to the agent
|
|
761
736
|
* @param toolId - ID of the tool to retrieve
|