@mastra/client-js 0.0.0-share-agent-metadata-with-cloud-20250718110128 → 0.0.0-sidebar-window-undefined-fix-20251029233656

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +1359 -2
  2. package/README.md +6 -7
  3. package/dist/client.d.ts +287 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/example.d.ts +2 -0
  6. package/dist/example.d.ts.map +1 -0
  7. package/dist/index.cjs +2292 -1219
  8. package/dist/index.cjs.map +1 -0
  9. package/dist/index.d.ts +5 -1194
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +2292 -1221
  12. package/dist/index.js.map +1 -0
  13. package/dist/resources/a2a.d.ts +41 -0
  14. package/dist/resources/a2a.d.ts.map +1 -0
  15. package/dist/resources/agent-builder.d.ts +160 -0
  16. package/dist/resources/agent-builder.d.ts.map +1 -0
  17. package/dist/resources/agent.d.ts +204 -0
  18. package/dist/resources/agent.d.ts.map +1 -0
  19. package/dist/resources/base.d.ts +13 -0
  20. package/dist/resources/base.d.ts.map +1 -0
  21. package/dist/resources/index.d.ts +11 -0
  22. package/dist/resources/index.d.ts.map +1 -0
  23. package/dist/resources/mcp-tool.d.ts +28 -0
  24. package/dist/resources/mcp-tool.d.ts.map +1 -0
  25. package/dist/resources/memory-thread.d.ts +61 -0
  26. package/dist/resources/memory-thread.d.ts.map +1 -0
  27. package/dist/resources/network-memory-thread.d.ts +47 -0
  28. package/dist/resources/network-memory-thread.d.ts.map +1 -0
  29. package/dist/resources/observability.d.ts +35 -0
  30. package/dist/resources/observability.d.ts.map +1 -0
  31. package/dist/resources/tool.d.ts +24 -0
  32. package/dist/resources/tool.d.ts.map +1 -0
  33. package/dist/resources/vector.d.ts +51 -0
  34. package/dist/resources/vector.d.ts.map +1 -0
  35. package/dist/resources/workflow.d.ts +269 -0
  36. package/dist/resources/workflow.d.ts.map +1 -0
  37. package/dist/tools.d.ts +22 -0
  38. package/dist/tools.d.ts.map +1 -0
  39. package/dist/types.d.ts +475 -0
  40. package/dist/types.d.ts.map +1 -0
  41. package/dist/utils/index.d.ts +11 -0
  42. package/dist/utils/index.d.ts.map +1 -0
  43. package/dist/utils/process-client-tools.d.ts +3 -0
  44. package/dist/utils/process-client-tools.d.ts.map +1 -0
  45. package/dist/utils/process-mastra-stream.d.ts +11 -0
  46. package/dist/utils/process-mastra-stream.d.ts.map +1 -0
  47. package/dist/utils/zod-to-json-schema.d.ts +3 -0
  48. package/dist/utils/zod-to-json-schema.d.ts.map +1 -0
  49. package/package.json +26 -14
  50. package/.turbo/turbo-build.log +0 -19
  51. package/dist/index.d.cts +0 -1194
  52. package/eslint.config.js +0 -6
  53. package/src/adapters/agui.test.ts +0 -180
  54. package/src/adapters/agui.ts +0 -239
  55. package/src/client.ts +0 -526
  56. package/src/example.ts +0 -95
  57. package/src/index.test.ts +0 -836
  58. package/src/index.ts +0 -2
  59. package/src/resources/a2a.ts +0 -88
  60. package/src/resources/agent.ts +0 -800
  61. package/src/resources/base.ts +0 -72
  62. package/src/resources/index.ts +0 -10
  63. package/src/resources/legacy-workflow.ts +0 -242
  64. package/src/resources/mcp-tool.ts +0 -48
  65. package/src/resources/memory-thread.ts +0 -63
  66. package/src/resources/network-memory-thread.ts +0 -63
  67. package/src/resources/network.ts +0 -85
  68. package/src/resources/tool.ts +0 -45
  69. package/src/resources/vNextNetwork.ts +0 -194
  70. package/src/resources/vector.ts +0 -83
  71. package/src/resources/workflow.ts +0 -401
  72. package/src/types.ts +0 -428
  73. package/src/utils/index.ts +0 -11
  74. package/src/utils/process-client-tools.ts +0 -32
  75. package/src/utils/zod-to-json-schema.ts +0 -10
  76. package/tsconfig.json +0 -5
  77. package/vitest.config.js +0 -8
@@ -1,800 +0,0 @@
1
- import {
2
- parsePartialJson,
3
- processDataStream,
4
- processTextStream,
5
- type JSONValue,
6
- type ReasoningUIPart,
7
- type TextUIPart,
8
- type ToolInvocation,
9
- type ToolInvocationUIPart,
10
- type UIMessage,
11
- type UseChatOptions,
12
- } from '@ai-sdk/ui-utils';
13
- import { Tool, type CoreMessage, type GenerateReturn } from '@mastra/core';
14
- import type { JSONSchema7 } from 'json-schema';
15
- import { ZodSchema } from 'zod';
16
- import { zodToJsonSchema } from '../utils/zod-to-json-schema';
17
- import { processClientTools } from '../utils/process-client-tools';
18
- import { v4 as uuid } from '@lukeed/uuid';
19
-
20
- import type {
21
- GenerateParams,
22
- GetAgentResponse,
23
- GetEvalsByAgentIdResponse,
24
- GetToolResponse,
25
- ClientOptions,
26
- StreamParams,
27
- } from '../types';
28
-
29
- import { BaseResource } from './base';
30
- import type { RuntimeContext } from '@mastra/core/runtime-context';
31
- import { parseClientRuntimeContext } from '../utils';
32
- import { MessageList } from '@mastra/core/agent';
33
-
34
- export class AgentVoice extends BaseResource {
35
- constructor(
36
- options: ClientOptions,
37
- private agentId: string,
38
- ) {
39
- super(options);
40
- this.agentId = agentId;
41
- }
42
-
43
- /**
44
- * Convert text to speech using the agent's voice provider
45
- * @param text - Text to convert to speech
46
- * @param options - Optional provider-specific options for speech generation
47
- * @returns Promise containing the audio data
48
- */
49
- async speak(text: string, options?: { speaker?: string; [key: string]: any }): Promise<Response> {
50
- return this.request<Response>(`/api/agents/${this.agentId}/voice/speak`, {
51
- method: 'POST',
52
- headers: {
53
- 'Content-Type': 'application/json',
54
- },
55
- body: { input: text, options },
56
- stream: true,
57
- });
58
- }
59
-
60
- /**
61
- * Convert speech to text using the agent's voice provider
62
- * @param audio - Audio data to transcribe
63
- * @param options - Optional provider-specific options
64
- * @returns Promise containing the transcribed text
65
- */
66
- listen(audio: Blob, options?: Record<string, any>): Promise<{ text: string }> {
67
- const formData = new FormData();
68
- formData.append('audio', audio);
69
-
70
- if (options) {
71
- formData.append('options', JSON.stringify(options));
72
- }
73
-
74
- return this.request(`/api/agents/${this.agentId}/voice/listen`, {
75
- method: 'POST',
76
- body: formData,
77
- });
78
- }
79
-
80
- /**
81
- * Get available speakers for the agent's voice provider
82
- * @returns Promise containing list of available speakers
83
- */
84
- getSpeakers(): Promise<Array<{ voiceId: string; [key: string]: any }>> {
85
- return this.request(`/api/agents/${this.agentId}/voice/speakers`);
86
- }
87
-
88
- /**
89
- * Get the listener configuration for the agent's voice provider
90
- * @returns Promise containing a check if the agent has listening capabilities
91
- */
92
- getListener(): Promise<{ enabled: boolean }> {
93
- return this.request(`/api/agents/${this.agentId}/voice/listener`);
94
- }
95
- }
96
-
97
- export class Agent extends BaseResource {
98
- public readonly voice: AgentVoice;
99
-
100
- constructor(
101
- options: ClientOptions,
102
- private agentId: string,
103
- ) {
104
- super(options);
105
- this.voice = new AgentVoice(options, this.agentId);
106
- }
107
-
108
- /**
109
- * Retrieves details about the agent
110
- * @returns Promise containing agent details including model and instructions
111
- */
112
- details(): Promise<GetAgentResponse> {
113
- return this.request(`/api/agents/${this.agentId}`);
114
- }
115
-
116
- /**
117
- * Generates a response from the agent
118
- * @param params - Generation parameters including prompt
119
- * @returns Promise containing the generated response
120
- */
121
- async generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
122
- params: GenerateParams<T> & { output?: never; experimental_output?: never },
123
- ): Promise<GenerateReturn<T>>;
124
- async generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
125
- params: GenerateParams<T> & { output: T; experimental_output?: never },
126
- ): Promise<GenerateReturn<T>>;
127
- async generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
128
- params: GenerateParams<T> & { output?: never; experimental_output: T },
129
- ): Promise<GenerateReturn<T>>;
130
- async generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
131
- params: GenerateParams<T>,
132
- ): Promise<GenerateReturn<T>> {
133
- const processedParams = {
134
- ...params,
135
- output: params.output ? zodToJsonSchema(params.output) : undefined,
136
- experimental_output: params.experimental_output ? zodToJsonSchema(params.experimental_output) : undefined,
137
- runtimeContext: parseClientRuntimeContext(params.runtimeContext),
138
- clientTools: processClientTools(params.clientTools),
139
- };
140
-
141
- const { runId, resourceId, threadId, runtimeContext } = processedParams as GenerateParams;
142
-
143
- const response: GenerateReturn<T> = await this.request(`/api/agents/${this.agentId}/generate`, {
144
- method: 'POST',
145
- body: processedParams,
146
- });
147
-
148
- if (response.finishReason === 'tool-calls') {
149
- const toolCalls = (
150
- response as unknown as {
151
- toolCalls: { toolName: string; args: any; toolCallId: string }[];
152
- messages: CoreMessage[];
153
- }
154
- ).toolCalls;
155
-
156
- if (!toolCalls || !Array.isArray(toolCalls)) {
157
- return response;
158
- }
159
-
160
- for (const toolCall of toolCalls) {
161
- const clientTool = params.clientTools?.[toolCall.toolName] as Tool;
162
-
163
- if (clientTool && clientTool.execute) {
164
- const result = await clientTool.execute(
165
- { context: toolCall?.args, runId, resourceId, threadId, runtimeContext: runtimeContext as RuntimeContext },
166
- {
167
- messages: (response as unknown as { messages: CoreMessage[] }).messages,
168
- toolCallId: toolCall?.toolCallId,
169
- },
170
- );
171
-
172
- const updatedMessages = [
173
- {
174
- role: 'user',
175
- content: params.messages,
176
- },
177
- ...(response.response as unknown as { messages: CoreMessage[] }).messages,
178
- {
179
- role: 'tool',
180
- content: [
181
- {
182
- type: 'tool-result',
183
- toolCallId: toolCall.toolCallId,
184
- toolName: toolCall.toolName,
185
- result,
186
- },
187
- ],
188
- },
189
- ];
190
- // @ts-ignore
191
- return this.generate({
192
- ...params,
193
- messages: updatedMessages,
194
- });
195
- }
196
- }
197
- }
198
-
199
- return response;
200
- }
201
-
202
- private async processChatResponse({
203
- stream,
204
- update,
205
- onToolCall,
206
- onFinish,
207
- getCurrentDate = () => new Date(),
208
- lastMessage,
209
- streamProtocol,
210
- }: {
211
- stream: ReadableStream<Uint8Array>;
212
- update: (options: { message: UIMessage; data: JSONValue[] | undefined; replaceLastMessage: boolean }) => void;
213
- onToolCall?: UseChatOptions['onToolCall'];
214
- onFinish?: (options: { message: UIMessage | undefined; finishReason: string; usage: string }) => void;
215
- generateId?: () => string;
216
- getCurrentDate?: () => Date;
217
- lastMessage: UIMessage | undefined;
218
- streamProtocol: 'text' | 'data';
219
- }) {
220
- const replaceLastMessage = lastMessage?.role === 'assistant';
221
- let step = replaceLastMessage
222
- ? 1 +
223
- // find max step in existing tool invocations:
224
- (lastMessage.toolInvocations?.reduce((max, toolInvocation) => {
225
- return Math.max(max, toolInvocation.step ?? 0);
226
- }, 0) ?? 0)
227
- : 0;
228
-
229
- const message: UIMessage = replaceLastMessage
230
- ? structuredClone(lastMessage)
231
- : {
232
- id: uuid(),
233
- createdAt: getCurrentDate(),
234
- role: 'assistant',
235
- content: '',
236
- parts: [],
237
- };
238
-
239
- let currentTextPart: TextUIPart | undefined = undefined;
240
- let currentReasoningPart: ReasoningUIPart | undefined = undefined;
241
- let currentReasoningTextDetail: { type: 'text'; text: string; signature?: string } | undefined = undefined;
242
-
243
- function updateToolInvocationPart(toolCallId: string, invocation: ToolInvocation) {
244
- const part = message.parts.find(
245
- part => part.type === 'tool-invocation' && part.toolInvocation.toolCallId === toolCallId,
246
- ) as ToolInvocationUIPart | undefined;
247
-
248
- if (part != null) {
249
- part.toolInvocation = invocation;
250
- } else {
251
- message.parts.push({
252
- type: 'tool-invocation',
253
- toolInvocation: invocation,
254
- });
255
- }
256
- }
257
-
258
- const data: JSONValue[] = [];
259
-
260
- // keep list of current message annotations for message
261
- let messageAnnotations: JSONValue[] | undefined = replaceLastMessage ? lastMessage?.annotations : undefined;
262
-
263
- // keep track of partial tool calls
264
- const partialToolCalls: Record<string, { text: string; step: number; index: number; toolName: string }> = {};
265
-
266
- let usage: any = {
267
- completionTokens: NaN,
268
- promptTokens: NaN,
269
- totalTokens: NaN,
270
- };
271
- let finishReason: string = 'unknown';
272
-
273
- function execUpdate() {
274
- // make a copy of the data array to ensure UI is updated (SWR)
275
- const copiedData = [...data];
276
-
277
- // keeps the currentMessage up to date with the latest annotations,
278
- // even if annotations preceded the message creation
279
- if (messageAnnotations?.length) {
280
- message.annotations = messageAnnotations;
281
- }
282
-
283
- const copiedMessage = {
284
- // deep copy the message to ensure that deep changes (msg attachments) are updated
285
- // with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
286
- ...structuredClone(message),
287
- // add a revision id to ensure that the message is updated with SWR. SWR uses a
288
- // hashing approach by default to detect changes, but it only works for shallow
289
- // changes. This is why we need to add a revision id to ensure that the message
290
- // is updated with SWR (without it, the changes get stuck in SWR and are not
291
- // forwarded to rendering):
292
- revisionId: uuid(),
293
- } as UIMessage;
294
-
295
- update({
296
- message: copiedMessage,
297
- data: copiedData,
298
- replaceLastMessage,
299
- });
300
- }
301
-
302
- if (streamProtocol === 'text') {
303
- await processTextStream({
304
- stream,
305
- onTextPart(value) {
306
- message.content += value;
307
- execUpdate();
308
- },
309
- });
310
-
311
- onFinish?.({ message, finishReason, usage });
312
- } else {
313
- await processDataStream({
314
- stream,
315
- onTextPart(value) {
316
- if (currentTextPart == null) {
317
- currentTextPart = {
318
- type: 'text',
319
- text: value,
320
- };
321
- message.parts.push(currentTextPart);
322
- } else {
323
- currentTextPart.text += value;
324
- }
325
-
326
- message.content += value;
327
- execUpdate();
328
- },
329
- onReasoningPart(value) {
330
- if (currentReasoningTextDetail == null) {
331
- currentReasoningTextDetail = { type: 'text', text: value };
332
- if (currentReasoningPart != null) {
333
- currentReasoningPart.details.push(currentReasoningTextDetail);
334
- }
335
- } else {
336
- currentReasoningTextDetail.text += value;
337
- }
338
-
339
- if (currentReasoningPart == null) {
340
- currentReasoningPart = {
341
- type: 'reasoning',
342
- reasoning: value,
343
- details: [currentReasoningTextDetail],
344
- };
345
- message.parts.push(currentReasoningPart);
346
- } else {
347
- currentReasoningPart.reasoning += value;
348
- }
349
-
350
- message.reasoning = (message.reasoning ?? '') + value;
351
-
352
- execUpdate();
353
- },
354
- onReasoningSignaturePart(value) {
355
- if (currentReasoningTextDetail != null) {
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
- }
368
-
369
- currentReasoningPart.details.push({
370
- type: 'redacted',
371
- data: value.data,
372
- });
373
-
374
- currentReasoningTextDetail = undefined;
375
-
376
- execUpdate();
377
- },
378
- onFilePart(value) {
379
- message.parts.push({
380
- type: 'file',
381
- mimeType: value.mimeType,
382
- data: value.data,
383
- });
384
-
385
- execUpdate();
386
- },
387
- onSourcePart(value) {
388
- message.parts.push({
389
- type: 'source',
390
- source: value,
391
- });
392
-
393
- execUpdate();
394
- },
395
- onToolCallStreamingStartPart(value) {
396
- if (message.toolInvocations == null) {
397
- message.toolInvocations = [];
398
- }
399
-
400
- // add the partial tool call to the map
401
- partialToolCalls[value.toolCallId] = {
402
- text: '',
403
- step,
404
- toolName: value.toolName,
405
- index: message.toolInvocations.length,
406
- };
407
-
408
- const invocation = {
409
- state: 'partial-call',
410
- step,
411
- toolCallId: value.toolCallId,
412
- toolName: value.toolName,
413
- args: undefined,
414
- } as const;
415
-
416
- message.toolInvocations.push(invocation);
417
-
418
- updateToolInvocationPart(value.toolCallId, invocation);
419
-
420
- execUpdate();
421
- },
422
- onToolCallDeltaPart(value) {
423
- const partialToolCall = partialToolCalls[value.toolCallId];
424
-
425
- partialToolCall!.text += value.argsTextDelta;
426
-
427
- const { value: partialArgs } = parsePartialJson(partialToolCall!.text);
428
-
429
- const invocation = {
430
- state: 'partial-call',
431
- step: partialToolCall!.step,
432
- toolCallId: value.toolCallId,
433
- toolName: partialToolCall!.toolName,
434
- args: partialArgs,
435
- } as const;
436
-
437
- message.toolInvocations![partialToolCall!.index] = invocation;
438
-
439
- updateToolInvocationPart(value.toolCallId, invocation);
440
-
441
- execUpdate();
442
- },
443
- async onToolCallPart(value) {
444
- const invocation = {
445
- state: 'call',
446
- step,
447
- ...value,
448
- } as const;
449
-
450
- if (partialToolCalls[value.toolCallId] != null) {
451
- // change the partial tool call to a full tool call
452
- message.toolInvocations![partialToolCalls[value.toolCallId]!.index] = invocation;
453
- } else {
454
- if (message.toolInvocations == null) {
455
- message.toolInvocations = [];
456
- }
457
-
458
- message.toolInvocations.push(invocation);
459
- }
460
-
461
- updateToolInvocationPart(value.toolCallId, invocation);
462
-
463
- execUpdate();
464
-
465
- // invoke the onToolCall callback if it exists. This is blocking.
466
- // In the future we should make this non-blocking, which
467
- // requires additional state management for error handling etc.
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;
477
-
478
- // store the result in the tool invocation
479
- message.toolInvocations![message.toolInvocations!.length - 1] = invocation;
480
-
481
- updateToolInvocationPart(value.toolCallId, invocation);
482
-
483
- execUpdate();
484
- }
485
- }
486
- },
487
- onToolResultPart(value) {
488
- const toolInvocations = message.toolInvocations;
489
-
490
- if (toolInvocations == null) {
491
- throw new Error('tool_result must be preceded by a tool_call');
492
- }
493
-
494
- // find if there is any tool invocation with the same toolCallId
495
- // and replace it with the result
496
- const toolInvocationIndex = toolInvocations.findIndex(
497
- invocation => invocation.toolCallId === value.toolCallId,
498
- );
499
-
500
- if (toolInvocationIndex === -1) {
501
- throw new Error('tool_result must be preceded by a tool_call with the same toolCallId');
502
- }
503
-
504
- const invocation = {
505
- ...toolInvocations[toolInvocationIndex],
506
- state: 'result' as const,
507
- ...value,
508
- } as const;
509
-
510
- toolInvocations[toolInvocationIndex] = invocation as ToolInvocation;
511
-
512
- updateToolInvocationPart(value.toolCallId, invocation as ToolInvocation);
513
-
514
- execUpdate();
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
- }
526
-
527
- execUpdate();
528
- },
529
- onFinishStepPart(value) {
530
- step += 1;
531
-
532
- // reset the current text and reasoning parts
533
- currentTextPart = value.isContinued ? currentTextPart : undefined;
534
- currentReasoningPart = undefined;
535
- currentReasoningTextDetail = undefined;
536
- },
537
- onStartStepPart(value) {
538
- // keep message id stable when we are updating an existing message:
539
- if (!replaceLastMessage) {
540
- message.id = value.messageId;
541
- }
542
-
543
- // add a step boundary part to the message
544
- message.parts.push({ type: 'step-start' });
545
- execUpdate();
546
- },
547
- onFinishMessagePart(value) {
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
- },
557
- });
558
-
559
- onFinish?.({ message, finishReason, usage });
560
- }
561
- }
562
-
563
- /**
564
- * Processes the stream response and handles tool calls
565
- */
566
- private async processStreamResponse(processedParams: any, writable: WritableStream<Uint8Array>) {
567
- const response: Response & {
568
- processDataStream: (options?: Omit<Parameters<typeof processDataStream>[0], 'stream'>) => Promise<void>;
569
- } = await this.request(`/api/agents/${this.agentId}/stream`, {
570
- method: 'POST',
571
- body: processedParams,
572
- stream: true,
573
- });
574
-
575
- if (!response.body) {
576
- throw new Error('No response body');
577
- }
578
-
579
- try {
580
- const streamProtocol = processedParams.output ? 'text' : 'data';
581
- let toolCalls: ToolInvocation[] = [];
582
- let finishReasonToolCalls = false;
583
- let messages: UIMessage[] = [];
584
- let hasProcessedToolCalls = false;
585
-
586
- // Use tee() to split the stream into two branches
587
- const [streamForWritable, streamForProcessing] = response.body.tee();
588
-
589
- // Pipe one branch to the writable stream
590
- streamForWritable
591
- .pipeTo(writable, {
592
- preventClose: true,
593
- })
594
- .catch(error => {
595
- console.error('Error piping to writable stream:', error);
596
- });
597
-
598
- // Process the other branch for chat response handling
599
- this.processChatResponse({
600
- stream: streamForProcessing,
601
- update: ({ message }) => {
602
- messages.push(message);
603
- },
604
- onFinish: async ({ finishReason, message }) => {
605
- if (finishReason === 'tool-calls') {
606
- const toolCall = [...(message?.parts ?? [])]
607
- .reverse()
608
- .find(part => part.type === 'tool-invocation')?.toolInvocation;
609
- if (toolCall) {
610
- toolCalls.push(toolCall);
611
- }
612
-
613
- // Handle tool calls if needed
614
- for (const toolCall of toolCalls) {
615
- const clientTool = processedParams.clientTools?.[toolCall.toolName] as Tool;
616
- if (clientTool && clientTool.execute) {
617
- const result = await clientTool.execute(
618
- {
619
- context: toolCall?.args,
620
- runId: processedParams.runId,
621
- resourceId: processedParams.resourceId,
622
- threadId: processedParams.threadId,
623
- runtimeContext: processedParams.runtimeContext as RuntimeContext,
624
- },
625
- {
626
- messages: (response as unknown as { messages: CoreMessage[] }).messages,
627
- toolCallId: toolCall?.toolCallId,
628
- },
629
- );
630
-
631
- const lastMessage: UIMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
632
-
633
- const toolInvocationPart = lastMessage?.parts?.find(
634
- part => part.type === 'tool-invocation' && part.toolInvocation?.toolCallId === toolCall.toolCallId,
635
- ) as ToolInvocationUIPart | undefined;
636
-
637
- if (toolInvocationPart) {
638
- toolInvocationPart.toolInvocation = {
639
- ...toolInvocationPart.toolInvocation,
640
- state: 'result',
641
- result,
642
- };
643
- }
644
-
645
- const toolInvocation = lastMessage?.toolInvocations?.find(
646
- toolInvocation => toolInvocation.toolCallId === toolCall.toolCallId,
647
- ) as ToolInvocation | undefined;
648
-
649
- if (toolInvocation) {
650
- toolInvocation.state = 'result';
651
- // @ts-ignore
652
- toolInvocation.result = result;
653
- }
654
-
655
- // write the tool result part to the stream
656
- const writer = writable.getWriter();
657
-
658
- try {
659
- await writer.write(
660
- new TextEncoder().encode(
661
- 'a:' +
662
- JSON.stringify({
663
- toolCallId: toolCall.toolCallId,
664
- result,
665
- }) +
666
- '\n',
667
- ),
668
- );
669
- } finally {
670
- writer.releaseLock();
671
- }
672
-
673
- // Convert messages to the correct format for the recursive call
674
- const originalMessages = processedParams.messages;
675
- const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
676
-
677
- // Recursively call stream with updated messages
678
- this.processStreamResponse(
679
- {
680
- ...processedParams,
681
- messages: [...messageArray, ...messages, lastMessage],
682
- },
683
- writable,
684
- );
685
- }
686
- }
687
- } else {
688
- setTimeout(() => {
689
- if (!writable.locked) {
690
- writable.close();
691
- }
692
- }, 0);
693
- }
694
- },
695
- lastMessage: undefined,
696
- streamProtocol,
697
- });
698
- } catch (error) {
699
- console.error('Error processing stream response:', error);
700
- }
701
- return response;
702
- }
703
-
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
- /**
760
- * Gets details about a specific tool available to the agent
761
- * @param toolId - ID of the tool to retrieve
762
- * @returns Promise containing tool details
763
- */
764
- getTool(toolId: string): Promise<GetToolResponse> {
765
- return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
766
- }
767
-
768
- /**
769
- * Executes a tool for the agent
770
- * @param toolId - ID of the tool to execute
771
- * @param params - Parameters required for tool execution
772
- * @returns Promise containing the tool execution results
773
- */
774
- executeTool(toolId: string, params: { data: any; runtimeContext?: RuntimeContext }): Promise<any> {
775
- const body = {
776
- data: params.data,
777
- runtimeContext: params.runtimeContext ? Object.fromEntries(params.runtimeContext.entries()) : undefined,
778
- };
779
- return this.request(`/api/agents/${this.agentId}/tools/${toolId}/execute`, {
780
- method: 'POST',
781
- body,
782
- });
783
- }
784
-
785
- /**
786
- * Retrieves evaluation results for the agent
787
- * @returns Promise containing agent evaluations
788
- */
789
- evals(): Promise<GetEvalsByAgentIdResponse> {
790
- return this.request(`/api/agents/${this.agentId}/evals/ci`);
791
- }
792
-
793
- /**
794
- * Retrieves live evaluation results for the agent
795
- * @returns Promise containing live agent evaluations
796
- */
797
- liveEvals(): Promise<GetEvalsByAgentIdResponse> {
798
- return this.request(`/api/agents/${this.agentId}/evals/live`);
799
- }
800
- }