@mastra/client-js 0.1.6 → 0.1.7-alpha.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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @mastra/client-js@0.1.6-alpha.0 build /home/runner/work/mastra/mastra/client-sdks/client-js
2
+ > @mastra/client-js@0.1.7-alpha.0 build /home/runner/work/mastra/mastra/client-sdks/client-js
3
3
  > tsup src/index.ts --format esm,cjs --dts --clean --treeshake
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -9,11 +9,11 @@
9
9
  CLI Cleaning output folder
10
10
  ESM Build start
11
11
  CJS Build start
12
- ESM dist/index.js 13.86 KB
13
- ESM ⚡️ Build success in 408ms
14
- CJS dist/index.cjs 13.96 KB
15
- CJS ⚡️ Build success in 594ms
12
+ CJS dist/index.cjs 15.48 KB
13
+ CJS ⚡️ Build success in 942ms
14
+ ESM dist/index.js 15.38 KB
15
+ ESM ⚡️ Build success in 944ms
16
16
  DTS Build start
17
- DTS ⚡️ Build success in 9964ms
18
- DTS dist/index.d.ts 13.64 KB
19
- DTS dist/index.d.cts 13.64 KB
17
+ DTS ⚡️ Build success in 11611ms
18
+ DTS dist/index.d.ts 14.44 KB
19
+ DTS dist/index.d.cts 14.44 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @mastra/client-js
2
2
 
3
+ ## 0.1.7-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 960690d: Improve client-js workflow watch dx
8
+ - Updated dependencies [59df7b6]
9
+ - Updated dependencies [29f3a82]
10
+ - Updated dependencies [59df7b6]
11
+ - Updated dependencies [c139344]
12
+ - @mastra/core@0.5.0-alpha.0
13
+
3
14
  ## 0.1.6
4
15
 
5
16
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -287,14 +287,63 @@ var Workflow = class extends BaseResource {
287
287
  }
288
288
  });
289
289
  }
290
+ /**
291
+ * Creates an async generator that processes a readable stream and yields records
292
+ * separated by the Record Separator character (\x1E)
293
+ *
294
+ * @param stream - The readable stream to process
295
+ * @returns An async generator that yields parsed records
296
+ */
297
+ async *streamProcessor(stream) {
298
+ const reader = stream.getReader();
299
+ let buffer = "";
300
+ try {
301
+ while (true) {
302
+ const { done, value } = await reader.read();
303
+ if (done) {
304
+ if (buffer.trim().length > 0) {
305
+ try {
306
+ const record = JSON.parse(buffer);
307
+ yield record;
308
+ } catch (e) {
309
+ console.warn("Could not parse final buffer content:", buffer);
310
+ }
311
+ }
312
+ break;
313
+ }
314
+ buffer += new TextDecoder().decode(value);
315
+ const records = buffer.split("");
316
+ buffer = records.pop() || "";
317
+ for (const record of records) {
318
+ if (record.trim().length > 0) {
319
+ try {
320
+ const parsedRecord = JSON.parse(record);
321
+ yield parsedRecord;
322
+ } catch (e) {
323
+ throw new Error(`Could not parse record: ${record}`);
324
+ }
325
+ }
326
+ }
327
+ }
328
+ } finally {
329
+ reader.releaseLock();
330
+ }
331
+ }
290
332
  /**
291
333
  * Watches workflow transitions in real-time
292
- * @returns Promise containing the workflow watch stream
334
+ * @returns AsyncGenerator that yields parsed records from the workflow watch stream
293
335
  */
294
- watch() {
295
- return this.request(`/api/workflows/${this.workflowId}/watch`, {
336
+ async *watch() {
337
+ const response = await this.request(`/api/workflows/${this.workflowId}/watch`, {
296
338
  stream: true
297
339
  });
340
+ if (!response.ok) {
341
+ throw new Error(`Failed to watch workflow: ${response.statusText}`);
342
+ }
343
+ if (!response.body) {
344
+ throw new Error("Response body is null");
345
+ }
346
+ yield* this.streamProcessor(response.body);
298
347
  }
299
348
  };
300
349
 
package/dist/index.d.cts CHANGED
@@ -50,6 +50,22 @@ interface GetWorkflowResponse {
50
50
  stepGraph: StepGraph;
51
51
  stepSubscriberGraph: Record<string, StepGraph>;
52
52
  }
53
+ type GetWorkflowWatchResponse = {
54
+ activePaths: Array<{
55
+ stepId: string;
56
+ stepPath: string[];
57
+ status: 'completed' | 'suspended' | 'pending';
58
+ }>;
59
+ context: {
60
+ steps: Record<string, {
61
+ status: 'completed' | 'suspended' | 'running';
62
+ [key: string]: any;
63
+ }>;
64
+ };
65
+ timestamp: number;
66
+ suspendedSteps: Record<string, any>;
67
+ runId: string;
68
+ };
53
69
  interface UpsertVectorParams {
54
70
  indexName: string;
55
71
  vectors: number[][];
@@ -274,11 +290,19 @@ declare class Workflow extends BaseResource {
274
290
  runId: string;
275
291
  context: Record<string, any>;
276
292
  }): Promise<Record<string, any>>;
293
+ /**
294
+ * Creates an async generator that processes a readable stream and yields records
295
+ * separated by the Record Separator character (\x1E)
296
+ *
297
+ * @param stream - The readable stream to process
298
+ * @returns An async generator that yields parsed records
299
+ */
300
+ private streamProcessor;
277
301
  /**
278
302
  * Watches workflow transitions in real-time
279
- * @returns Promise containing the workflow watch stream
303
+ * @returns AsyncGenerator that yields parsed records from the workflow watch stream
280
304
  */
281
- watch(): Promise<Response>;
305
+ watch(): AsyncGenerator<GetWorkflowWatchResponse, void, unknown>;
282
306
  }
283
307
 
284
308
  declare class Tool extends BaseResource {
@@ -398,4 +422,4 @@ declare class MastraClient extends BaseResource {
398
422
  getTelemetry(params?: GetTelemetryParams): Promise<GetTelemetryResponse>;
399
423
  }
400
424
 
401
- export { type ClientOptions, type CreateIndexParams, type CreateMemoryThreadParams, type CreateMemoryThreadResponse, type GenerateParams, type GetAgentResponse, type GetEvalsByAgentIdResponse, type GetLogParams, type GetLogsParams, type GetLogsResponse, type GetMemoryThreadMessagesResponse, type GetMemoryThreadParams, type GetMemoryThreadResponse, type GetTelemetryParams, type GetTelemetryResponse, type GetToolResponse, type GetVectorIndexResponse, type GetWorkflowResponse, MastraClient, type QueryVectorParams, type QueryVectorResponse, type RequestFunction, type RequestOptions, type SaveMessageToMemoryParams, type SaveMessageToMemoryResponse, type StreamParams, type UpdateMemoryThreadParams, type UpsertVectorParams };
425
+ export { type ClientOptions, type CreateIndexParams, type CreateMemoryThreadParams, type CreateMemoryThreadResponse, type GenerateParams, type GetAgentResponse, type GetEvalsByAgentIdResponse, type GetLogParams, type GetLogsParams, type GetLogsResponse, type GetMemoryThreadMessagesResponse, type GetMemoryThreadParams, type GetMemoryThreadResponse, type GetTelemetryParams, type GetTelemetryResponse, type GetToolResponse, type GetVectorIndexResponse, type GetWorkflowResponse, type GetWorkflowWatchResponse, MastraClient, type QueryVectorParams, type QueryVectorResponse, type RequestFunction, type RequestOptions, type SaveMessageToMemoryParams, type SaveMessageToMemoryResponse, type StreamParams, type UpdateMemoryThreadParams, type UpsertVectorParams };
package/dist/index.d.ts CHANGED
@@ -50,6 +50,22 @@ interface GetWorkflowResponse {
50
50
  stepGraph: StepGraph;
51
51
  stepSubscriberGraph: Record<string, StepGraph>;
52
52
  }
53
+ type GetWorkflowWatchResponse = {
54
+ activePaths: Array<{
55
+ stepId: string;
56
+ stepPath: string[];
57
+ status: 'completed' | 'suspended' | 'pending';
58
+ }>;
59
+ context: {
60
+ steps: Record<string, {
61
+ status: 'completed' | 'suspended' | 'running';
62
+ [key: string]: any;
63
+ }>;
64
+ };
65
+ timestamp: number;
66
+ suspendedSteps: Record<string, any>;
67
+ runId: string;
68
+ };
53
69
  interface UpsertVectorParams {
54
70
  indexName: string;
55
71
  vectors: number[][];
@@ -274,11 +290,19 @@ declare class Workflow extends BaseResource {
274
290
  runId: string;
275
291
  context: Record<string, any>;
276
292
  }): Promise<Record<string, any>>;
293
+ /**
294
+ * Creates an async generator that processes a readable stream and yields records
295
+ * separated by the Record Separator character (\x1E)
296
+ *
297
+ * @param stream - The readable stream to process
298
+ * @returns An async generator that yields parsed records
299
+ */
300
+ private streamProcessor;
277
301
  /**
278
302
  * Watches workflow transitions in real-time
279
- * @returns Promise containing the workflow watch stream
303
+ * @returns AsyncGenerator that yields parsed records from the workflow watch stream
280
304
  */
281
- watch(): Promise<Response>;
305
+ watch(): AsyncGenerator<GetWorkflowWatchResponse, void, unknown>;
282
306
  }
283
307
 
284
308
  declare class Tool extends BaseResource {
@@ -398,4 +422,4 @@ declare class MastraClient extends BaseResource {
398
422
  getTelemetry(params?: GetTelemetryParams): Promise<GetTelemetryResponse>;
399
423
  }
400
424
 
401
- export { type ClientOptions, type CreateIndexParams, type CreateMemoryThreadParams, type CreateMemoryThreadResponse, type GenerateParams, type GetAgentResponse, type GetEvalsByAgentIdResponse, type GetLogParams, type GetLogsParams, type GetLogsResponse, type GetMemoryThreadMessagesResponse, type GetMemoryThreadParams, type GetMemoryThreadResponse, type GetTelemetryParams, type GetTelemetryResponse, type GetToolResponse, type GetVectorIndexResponse, type GetWorkflowResponse, MastraClient, type QueryVectorParams, type QueryVectorResponse, type RequestFunction, type RequestOptions, type SaveMessageToMemoryParams, type SaveMessageToMemoryResponse, type StreamParams, type UpdateMemoryThreadParams, type UpsertVectorParams };
425
+ export { type ClientOptions, type CreateIndexParams, type CreateMemoryThreadParams, type CreateMemoryThreadResponse, type GenerateParams, type GetAgentResponse, type GetEvalsByAgentIdResponse, type GetLogParams, type GetLogsParams, type GetLogsResponse, type GetMemoryThreadMessagesResponse, type GetMemoryThreadParams, type GetMemoryThreadResponse, type GetTelemetryParams, type GetTelemetryResponse, type GetToolResponse, type GetVectorIndexResponse, type GetWorkflowResponse, type GetWorkflowWatchResponse, MastraClient, type QueryVectorParams, type QueryVectorResponse, type RequestFunction, type RequestOptions, type SaveMessageToMemoryParams, type SaveMessageToMemoryResponse, type StreamParams, type UpdateMemoryThreadParams, type UpsertVectorParams };
package/dist/index.js CHANGED
@@ -285,14 +285,63 @@ var Workflow = class extends BaseResource {
285
285
  }
286
286
  });
287
287
  }
288
+ /**
289
+ * Creates an async generator that processes a readable stream and yields records
290
+ * separated by the Record Separator character (\x1E)
291
+ *
292
+ * @param stream - The readable stream to process
293
+ * @returns An async generator that yields parsed records
294
+ */
295
+ async *streamProcessor(stream) {
296
+ const reader = stream.getReader();
297
+ let buffer = "";
298
+ try {
299
+ while (true) {
300
+ const { done, value } = await reader.read();
301
+ if (done) {
302
+ if (buffer.trim().length > 0) {
303
+ try {
304
+ const record = JSON.parse(buffer);
305
+ yield record;
306
+ } catch (e) {
307
+ console.warn("Could not parse final buffer content:", buffer);
308
+ }
309
+ }
310
+ break;
311
+ }
312
+ buffer += new TextDecoder().decode(value);
313
+ const records = buffer.split("");
314
+ buffer = records.pop() || "";
315
+ for (const record of records) {
316
+ if (record.trim().length > 0) {
317
+ try {
318
+ const parsedRecord = JSON.parse(record);
319
+ yield parsedRecord;
320
+ } catch (e) {
321
+ throw new Error(`Could not parse record: ${record}`);
322
+ }
323
+ }
324
+ }
325
+ }
326
+ } finally {
327
+ reader.releaseLock();
328
+ }
329
+ }
288
330
  /**
289
331
  * Watches workflow transitions in real-time
290
- * @returns Promise containing the workflow watch stream
332
+ * @returns AsyncGenerator that yields parsed records from the workflow watch stream
291
333
  */
292
- watch() {
293
- return this.request(`/api/workflows/${this.workflowId}/watch`, {
334
+ async *watch() {
335
+ const response = await this.request(`/api/workflows/${this.workflowId}/watch`, {
294
336
  stream: true
295
337
  });
338
+ if (!response.ok) {
339
+ throw new Error(`Failed to watch workflow: ${response.statusText}`);
340
+ }
341
+ if (!response.body) {
342
+ throw new Error("Response body is null");
343
+ }
344
+ yield* this.streamProcessor(response.body);
296
345
  }
297
346
  };
298
347
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/client-js",
3
- "version": "0.1.6",
3
+ "version": "0.1.7-alpha.0",
4
4
  "description": "The official TypeScript library for the Mastra Client API",
5
5
  "author": "",
6
6
  "type": "module",
@@ -25,7 +25,7 @@
25
25
  "json-schema": "^0.4.0",
26
26
  "zod": "^3.24.1",
27
27
  "zod-to-json-schema": "^3.24.1",
28
- "@mastra/core": "^0.4.4"
28
+ "@mastra/core": "^0.5.0-alpha.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@babel/preset-env": "^7.26.0",
package/src/example.ts CHANGED
@@ -1,43 +1,66 @@
1
1
  import { MastraClient } from './client';
2
2
 
3
- (async () => {
4
- const client = new MastraClient({
5
- baseUrl: 'http://localhost:4111',
6
- });
7
-
8
- try {
9
- const agent = client.getAgent('weatherAgent');
10
- const response = await agent.stream({
11
- messages: [
12
- {
13
- role: 'user',
14
- content: 'Hello, world!',
15
- },
16
- ],
17
- });
18
-
19
- const reader = response?.body?.getReader();
20
- const decoder = new TextDecoder();
21
- let buffer = '';
22
-
23
- while (true) {
24
- if (!reader) break;
25
- const { value, done } = await reader.read();
26
- if (done) break;
27
-
28
- const chunk = decoder.decode(value);
29
- buffer += chunk;
30
-
31
- console.log(buffer);
32
-
33
- const matches = buffer.matchAll(/0:"([^"]*)"/g);
34
-
35
- for (const match of matches) {
36
- const content = match[1];
37
- process.stdout.write(`${content}\n`);
38
- }
39
- }
40
- } catch (error) {
41
- console.error(error);
42
- }
43
- })();
3
+ // Agent
4
+
5
+ // (async () => {
6
+ // const client = new MastraClient({
7
+ // baseUrl: 'http://localhost:4111',
8
+ // });
9
+
10
+ // try {
11
+ // const agent = client.getAgent('weatherAgent');
12
+ // const response = await agent.stream({
13
+ // messages: [
14
+ // {
15
+ // role: 'user',
16
+ // content: 'Hello, world!',
17
+ // },
18
+ // ],
19
+ // });
20
+
21
+ // const reader = response?.body?.getReader();
22
+ // const decoder = new TextDecoder();
23
+ // let buffer = '';
24
+
25
+ // while (true) {
26
+ // if (!reader) break;
27
+ // const { value, done } = await reader.read();
28
+ // if (done) break;
29
+
30
+ // const chunk = decoder.decode(value);
31
+ // buffer += chunk;
32
+
33
+ // console.log(buffer);
34
+
35
+ // const matches = buffer.matchAll(/0:"([^"]*)"/g);
36
+
37
+ // for (const match of matches) {
38
+ // const content = match[1];
39
+ // process.stdout.write(`${content}\n`);
40
+ // }
41
+ // }
42
+ // } catch (error) {
43
+ // console.error(error);
44
+ // }
45
+ // })();
46
+
47
+ // Workflow
48
+
49
+ // (async () => {
50
+ // const client = new MastraClient({
51
+ // baseUrl: 'http://localhost:4111',
52
+ // });
53
+
54
+ // const workflowId = 'weatherWorkflow';
55
+
56
+ // const workflow = client.getWorkflow(workflowId);
57
+ // const response = workflow.watch();
58
+
59
+ // workflow.execute({
60
+ // city: 'New York',
61
+ // });
62
+
63
+ // for await (const record of response) {
64
+ // console.log(new Date().toTimeString(), record);
65
+ // }
66
+ // })();
@@ -1,4 +1,4 @@
1
- import type { GetWorkflowResponse, ClientOptions } from '../types';
1
+ import type { GetWorkflowResponse, ClientOptions, GetWorkflowWatchResponse } from '../types';
2
2
 
3
3
  import { BaseResource } from './base';
4
4
 
@@ -56,13 +56,78 @@ export class Workflow extends BaseResource {
56
56
  });
57
57
  }
58
58
 
59
+ /**
60
+ * Creates an async generator that processes a readable stream and yields records
61
+ * separated by the Record Separator character (\x1E)
62
+ *
63
+ * @param stream - The readable stream to process
64
+ * @returns An async generator that yields parsed records
65
+ */
66
+ private async *streamProcessor(stream: ReadableStream): AsyncGenerator<GetWorkflowWatchResponse, void, unknown> {
67
+ const reader = stream.getReader();
68
+ let buffer = '';
69
+
70
+ try {
71
+ while (true) {
72
+ const { done, value } = await reader.read();
73
+
74
+ if (done) {
75
+ // Process any remaining data in buffer before finishing
76
+ if (buffer.trim().length > 0) {
77
+ try {
78
+ const record = JSON.parse(buffer);
79
+ yield record;
80
+ } catch (e) {
81
+ console.warn('Could not parse final buffer content:', buffer);
82
+ }
83
+ }
84
+ break;
85
+ }
86
+
87
+ // Decode and add to buffer
88
+ buffer += new TextDecoder().decode(value);
89
+
90
+ // Split the buffer into records
91
+ const records = buffer.split('\x1E');
92
+
93
+ // Keep the last (potentially incomplete) chunk in the buffer
94
+ buffer = records.pop() || '';
95
+
96
+ // Process each complete record
97
+ for (const record of records) {
98
+ if (record.trim().length > 0) {
99
+ try {
100
+ // Assuming the records are JSON strings
101
+ const parsedRecord = JSON.parse(record);
102
+ yield parsedRecord;
103
+ } catch (e) {
104
+ throw new Error(`Could not parse record: ${record}`);
105
+ }
106
+ }
107
+ }
108
+ }
109
+ } finally {
110
+ reader.releaseLock();
111
+ }
112
+ }
113
+
59
114
  /**
60
115
  * Watches workflow transitions in real-time
61
- * @returns Promise containing the workflow watch stream
116
+ * @returns AsyncGenerator that yields parsed records from the workflow watch stream
62
117
  */
63
- watch(): Promise<Response> {
64
- return this.request(`/api/workflows/${this.workflowId}/watch`, {
118
+ async *watch() {
119
+ const response: Response = await this.request(`/api/workflows/${this.workflowId}/watch`, {
65
120
  stream: true,
66
121
  });
122
+
123
+ if (!response.ok) {
124
+ throw new Error(`Failed to watch workflow: ${response.statusText}`);
125
+ }
126
+
127
+ if (!response.body) {
128
+ throw new Error('Response body is null');
129
+ }
130
+
131
+ yield* this.streamProcessor(response.body);
67
132
  }
68
133
  }
package/src/types.ts CHANGED
@@ -69,6 +69,26 @@ export interface GetWorkflowResponse {
69
69
  stepSubscriberGraph: Record<string, StepGraph>;
70
70
  }
71
71
 
72
+ export type GetWorkflowWatchResponse = {
73
+ activePaths: Array<{
74
+ stepId: string;
75
+ stepPath: string[];
76
+ status: 'completed' | 'suspended' | 'pending';
77
+ }>;
78
+ context: {
79
+ steps: Record<
80
+ string,
81
+ {
82
+ status: 'completed' | 'suspended' | 'running';
83
+ [key: string]: any;
84
+ }
85
+ >;
86
+ };
87
+ timestamp: number;
88
+ suspendedSteps: Record<string, any>;
89
+ runId: string;
90
+ };
91
+
72
92
  export interface UpsertVectorParams {
73
93
  indexName: string;
74
94
  vectors: number[][];