@mastra/client-js 0.10.6-alpha.0 → 0.10.6-alpha.2

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.
@@ -1,10 +1,9 @@
1
1
  import { processDataStream } from '@ai-sdk/ui-utils';
2
2
  import type { GenerateReturn } from '@mastra/core';
3
3
  import type { JSONSchema7 } from 'json-schema';
4
- import { ZodSchema } from 'zod';
5
- import { zodToJsonSchema } from '../utils/zod-to-json-schema';
6
-
4
+ import type { ZodSchema } from 'zod';
7
5
  import type { GenerateParams, ClientOptions, StreamParams, GetNetworkResponse } from '../types';
6
+ import { zodToJsonSchema } from '../utils/zod-to-json-schema';
8
7
 
9
8
  import { BaseResource } from './base';
10
9
 
@@ -0,0 +1,147 @@
1
+ import type { WatchEvent } from '@mastra/core/workflows';
2
+
3
+ import type {
4
+ ClientOptions,
5
+ GetVNextNetworkResponse,
6
+ GenerateVNextNetworkResponse,
7
+ LoopVNextNetworkResponse,
8
+ GenerateOrStreamVNextNetworkParams,
9
+ } from '../types';
10
+
11
+ import { BaseResource } from './base';
12
+
13
+ const RECORD_SEPARATOR = '\x1E';
14
+
15
+ export class VNextNetwork extends BaseResource {
16
+ constructor(
17
+ options: ClientOptions,
18
+ private networkId: string,
19
+ ) {
20
+ super(options);
21
+ }
22
+
23
+ /**
24
+ * Retrieves details about the network
25
+ * @returns Promise containing vNext network details
26
+ */
27
+ details(): Promise<GetVNextNetworkResponse> {
28
+ return this.request(`/api/networks/v-next/${this.networkId}`);
29
+ }
30
+
31
+ /**
32
+ * Generates a response from the v-next network
33
+ * @param params - Generation parameters including message
34
+ * @returns Promise containing the generated response
35
+ */
36
+ generate(params: GenerateOrStreamVNextNetworkParams): Promise<GenerateVNextNetworkResponse> {
37
+ return this.request(`/api/networks/v-next/${this.networkId}/generate`, {
38
+ method: 'POST',
39
+ body: params,
40
+ });
41
+ }
42
+
43
+ /**
44
+ * Generates a response from the v-next network using multiple primitives
45
+ * @param params - Generation parameters including message
46
+ * @returns Promise containing the generated response
47
+ */
48
+ loop(params: { message: string }): Promise<LoopVNextNetworkResponse> {
49
+ return this.request(`/api/networks/v-next/${this.networkId}/loop`, {
50
+ method: 'POST',
51
+ body: params,
52
+ });
53
+ }
54
+
55
+ private async *streamProcessor(stream: ReadableStream): AsyncGenerator<WatchEvent, void, unknown> {
56
+ const reader = stream.getReader();
57
+
58
+ // Track if we've finished reading from the stream
59
+ let doneReading = false;
60
+ // Buffer to accumulate partial chunks
61
+ let buffer = '';
62
+
63
+ try {
64
+ while (!doneReading) {
65
+ // Read the next chunk from the stream
66
+ const { done, value } = await reader.read();
67
+ doneReading = done;
68
+
69
+ // Skip processing if we're done and there's no value
70
+ if (done && !value) continue;
71
+
72
+ try {
73
+ // Decode binary data to text
74
+ const decoded = value ? new TextDecoder().decode(value) : '';
75
+
76
+ // Split the combined buffer and new data by record separator
77
+ const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
78
+
79
+ // The last chunk might be incomplete, so save it for the next iteration
80
+ buffer = chunks.pop() || '';
81
+
82
+ // Process complete chunks
83
+ for (const chunk of chunks) {
84
+ if (chunk) {
85
+ // Only process non-empty chunks
86
+ if (typeof chunk === 'string') {
87
+ try {
88
+ const parsedChunk = JSON.parse(chunk);
89
+ yield parsedChunk;
90
+ } catch {
91
+ // Silently ignore parsing errors to maintain stream processing
92
+ // This allows the stream to continue even if one record is malformed
93
+ }
94
+ }
95
+ }
96
+ }
97
+ } catch {
98
+ // Silently ignore parsing errors to maintain stream processing
99
+ // This allows the stream to continue even if one record is malformed
100
+ }
101
+ }
102
+
103
+ // Process any remaining data in the buffer after stream is done
104
+ if (buffer) {
105
+ try {
106
+ yield JSON.parse(buffer);
107
+ } catch {
108
+ // Ignore parsing error for final chunk
109
+ }
110
+ }
111
+ } finally {
112
+ // Always ensure we clean up the reader
113
+ reader.cancel().catch(() => {
114
+ // Ignore cancel errors
115
+ });
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Streams a response from the v-next network
121
+ * @param params - Stream parameters including message
122
+ * @returns Promise containing the results
123
+ */
124
+ async stream(params: GenerateOrStreamVNextNetworkParams, onRecord: (record: WatchEvent) => void) {
125
+ const response: Response = await this.request(`/api/networks/v-next/${this.networkId}/stream`, {
126
+ method: 'POST',
127
+ body: params,
128
+ stream: true,
129
+ });
130
+
131
+ if (!response.ok) {
132
+ throw new Error(`Failed to stream vNext network: ${response.statusText}`);
133
+ }
134
+
135
+ if (!response.body) {
136
+ throw new Error('Response body is null');
137
+ }
138
+
139
+ for await (const record of this.streamProcessor(response.body)) {
140
+ if (typeof record === 'string') {
141
+ onRecord(JSON.parse(record));
142
+ } else {
143
+ onRecord(record);
144
+ }
145
+ }
146
+ }
147
+ }
@@ -237,9 +237,9 @@ export class Workflow extends BaseResource {
237
237
  }
238
238
 
239
239
  /**
240
- * Starts a vNext workflow run and returns a stream
240
+ * Starts a workflow run and returns a stream
241
241
  * @param params - Object containing the optional runId, inputData and runtimeContext
242
- * @returns Promise containing the vNext workflow execution results
242
+ * @returns Promise containing the workflow execution results
243
243
  */
244
244
  async stream(params: { runId?: string; inputData: Record<string, any>; runtimeContext?: RuntimeContext }) {
245
245
  const searchParams = new URLSearchParams();
package/src/types.ts CHANGED
@@ -183,6 +183,11 @@ export interface SaveMessageToMemoryParams {
183
183
  agentId: string;
184
184
  }
185
185
 
186
+ export interface SaveNetworkMessageToMemoryParams {
187
+ messages: MastraMessageV1[];
188
+ networkId: string;
189
+ }
190
+
186
191
  export type SaveMessageToMemoryResponse = MastraMessageV1[];
187
192
 
188
193
  export interface CreateMemoryThreadParams {
@@ -193,6 +198,14 @@ export interface CreateMemoryThreadParams {
193
198
  agentId: string;
194
199
  }
195
200
 
201
+ export interface CreateNetworkMemoryThreadParams {
202
+ title?: string;
203
+ metadata?: Record<string, any>;
204
+ resourceId: string;
205
+ threadId?: string;
206
+ networkId: string;
207
+ }
208
+
196
209
  export type CreateMemoryThreadResponse = StorageThreadType;
197
210
 
198
211
  export interface GetMemoryThreadParams {
@@ -200,6 +213,11 @@ export interface GetMemoryThreadParams {
200
213
  agentId: string;
201
214
  }
202
215
 
216
+ export interface GetNetworkMemoryThreadParams {
217
+ resourceId: string;
218
+ networkId: string;
219
+ }
220
+
203
221
  export type GetMemoryThreadResponse = StorageThreadType[];
204
222
 
205
223
  export interface UpdateMemoryThreadParams {
@@ -306,6 +324,7 @@ export interface GetTelemetryParams {
306
324
  }
307
325
 
308
326
  export interface GetNetworkResponse {
327
+ id: string;
309
328
  name: string;
310
329
  instructions: string;
311
330
  agents: Array<{
@@ -320,6 +339,48 @@ export interface GetNetworkResponse {
320
339
  state?: Record<string, any>;
321
340
  }
322
341
 
342
+ export interface GetVNextNetworkResponse {
343
+ id: string;
344
+ name: string;
345
+ instructions: string;
346
+ agents: Array<{
347
+ name: string;
348
+ provider: string;
349
+ modelId: string;
350
+ }>;
351
+ routingModel: {
352
+ provider: string;
353
+ modelId: string;
354
+ };
355
+ workflows: Array<{
356
+ name: string;
357
+ description: string;
358
+ inputSchema: string | undefined;
359
+ outputSchema: string | undefined;
360
+ }>;
361
+ }
362
+
363
+ export interface GenerateVNextNetworkResponse {
364
+ task: string;
365
+ result: string;
366
+ resourceId: string;
367
+ resourceType: 'none' | 'tool' | 'agent' | 'workflow';
368
+ }
369
+
370
+ export interface GenerateOrStreamVNextNetworkParams {
371
+ message: string;
372
+ threadId?: string;
373
+ resourceId?: string;
374
+ }
375
+
376
+ export interface LoopVNextNetworkResponse {
377
+ status: 'success';
378
+ result: {
379
+ text: string;
380
+ };
381
+ steps: WorkflowResult<any, any>['steps'];
382
+ }
383
+
323
384
  export interface McpServerListResponse {
324
385
  servers: ServerInfo[];
325
386
  next: string | null;