@mastra/client-js 0.1.13 → 0.1.14-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,5 +1,5 @@
1
1
 
2
- > @mastra/client-js@0.1.13-alpha.3 build /home/runner/work/mastra/mastra/client-sdks/client-js
2
+ > @mastra/client-js@0.1.14-alpha.2 build /home/runner/work/mastra/mastra/client-sdks/client-js
3
3
  > tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting
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 21.58 KB
13
- ESM ⚡️ Build success in 957ms
14
- CJS dist/index.cjs 21.76 KB
15
- CJS ⚡️ Build success in 957ms
12
+ ESM dist/index.js 21.12 KB
13
+ ESM ⚡️ Build success in 893ms
14
+ CJS dist/index.cjs 21.30 KB
15
+ CJS ⚡️ Build success in 900ms
16
16
  DTS Build start
17
- DTS ⚡️ Build success in 11249ms
18
- DTS dist/index.d.ts 19.18 KB
19
- DTS dist/index.d.cts 19.18 KB
17
+ DTS ⚡️ Build success in 12468ms
18
+ DTS dist/index.d.ts 18.89 KB
19
+ DTS dist/index.d.cts 18.89 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @mastra/client-js
2
2
 
3
+ ## 0.1.14-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 84fe241: Improve streaming of workflows
8
+ - Updated dependencies [56c31b7]
9
+ - Updated dependencies [dbbbf80]
10
+ - Updated dependencies [99d43b9]
11
+ - @mastra/core@0.8.0-alpha.2
12
+
13
+ ## 0.1.14-alpha.1
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [619c39d]
18
+ - Updated dependencies [fe56be0]
19
+ - Updated dependencies [a0967a0]
20
+ - Updated dependencies [fca3b21]
21
+ - Updated dependencies [0118361]
22
+ - Updated dependencies [619c39d]
23
+ - @mastra/core@0.8.0-alpha.1
24
+
25
+ ## 0.1.14-alpha.0
26
+
27
+ ### Patch Changes
28
+
29
+ - 8076ecf: Unify workflow watch/start response
30
+ - Updated dependencies [107bcfe]
31
+ - Updated dependencies [5b4e19f]
32
+ - Updated dependencies [7599d77]
33
+ - Updated dependencies [cafae83]
34
+ - Updated dependencies [8076ecf]
35
+ - Updated dependencies [304397c]
36
+ - @mastra/core@0.7.1-alpha.0
37
+
3
38
  ## 0.1.13
4
39
 
5
40
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -359,6 +359,7 @@ var Vector = class extends BaseResource {
359
359
  };
360
360
 
361
361
  // src/resources/workflow.ts
362
+ var RECORD_SEPARATOR = "";
362
363
  var Workflow = class extends BaseResource {
363
364
  constructor(options, workflowId) {
364
365
  super(options);
@@ -460,43 +461,34 @@ var Workflow = class extends BaseResource {
460
461
  */
461
462
  async *streamProcessor(stream) {
462
463
  const reader = stream.getReader();
464
+ let doneReading = false;
463
465
  let buffer = "";
464
466
  try {
465
- while (true) {
467
+ while (!doneReading) {
466
468
  const { done, value } = await reader.read();
467
- if (done) {
468
- if (buffer.trim().length > 0) {
469
- try {
470
- const record = JSON.parse(buffer);
471
- yield record;
472
- } catch (e) {
473
- console.warn("Could not parse final buffer content:", buffer);
469
+ doneReading = done;
470
+ if (done && !value) continue;
471
+ try {
472
+ const decoded = value ? new TextDecoder().decode(value) : "";
473
+ const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
474
+ buffer = chunks.pop() || "";
475
+ for (const chunk of chunks) {
476
+ if (chunk) {
477
+ yield JSON.parse(chunk);
474
478
  }
475
479
  }
476
- break;
480
+ } catch (error) {
477
481
  }
478
- buffer += new TextDecoder().decode(value);
479
- const records = buffer.split("");
480
- buffer = records.pop() || "";
481
- for (const record of records) {
482
- if (record.trim().length > 0) {
483
- try {
484
- const parsedRecord = JSON.parse(record);
485
- const isWorkflowCompleted = parsedRecord?.activePaths?.every(
486
- (path) => path.status === "completed" || path.status === "suspended" || path.status === "failed" || path.status === "skipped"
487
- );
488
- if (isWorkflowCompleted) {
489
- reader.cancel();
490
- }
491
- yield parsedRecord;
492
- } catch (e) {
493
- throw new Error(`Could not parse record: ${record}`);
494
- }
495
- }
482
+ }
483
+ if (buffer) {
484
+ try {
485
+ yield JSON.parse(buffer);
486
+ } catch {
496
487
  }
497
488
  }
498
489
  } finally {
499
- reader.cancel();
490
+ reader.cancel().catch(() => {
491
+ });
500
492
  }
501
493
  }
502
494
  /**
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
1
+ import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, WorkflowRunResult as WorkflowRunResult$1, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
2
2
  import { JSONSchema7 } from 'json-schema';
3
3
  import { ZodSchema } from 'zod';
4
4
  import { processDataStream } from '@ai-sdk/ui-utils';
@@ -53,27 +53,13 @@ interface GetWorkflowResponse {
53
53
  stepSubscriberGraph: Record<string, StepGraph>;
54
54
  }
55
55
  type WorkflowRunResult = {
56
- activePaths: Array<{
57
- stepId: string;
56
+ activePaths: Record<string, {
57
+ status: string;
58
+ suspendPayload?: any;
58
59
  stepPath: string[];
59
- status: 'completed' | 'suspended' | 'pending';
60
60
  }>;
61
- context: {
62
- steps: Record<string, {
63
- status: 'success';
64
- output: any;
65
- [key: string]: any;
66
- } | {
67
- status: 'pending';
68
- [key: string]: any;
69
- } | {
70
- status: 'suspended';
71
- suspendPayload: any;
72
- [key: string]: any;
73
- }>;
74
- };
61
+ results: WorkflowRunResult$1<any, any, any>['results'];
75
62
  timestamp: number;
76
- suspendedSteps: Record<string, any>;
77
63
  runId: string;
78
64
  };
79
65
  interface UpsertVectorParams {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
1
+ import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, WorkflowRunResult as WorkflowRunResult$1, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
2
2
  import { JSONSchema7 } from 'json-schema';
3
3
  import { ZodSchema } from 'zod';
4
4
  import { processDataStream } from '@ai-sdk/ui-utils';
@@ -53,27 +53,13 @@ interface GetWorkflowResponse {
53
53
  stepSubscriberGraph: Record<string, StepGraph>;
54
54
  }
55
55
  type WorkflowRunResult = {
56
- activePaths: Array<{
57
- stepId: string;
56
+ activePaths: Record<string, {
57
+ status: string;
58
+ suspendPayload?: any;
58
59
  stepPath: string[];
59
- status: 'completed' | 'suspended' | 'pending';
60
60
  }>;
61
- context: {
62
- steps: Record<string, {
63
- status: 'success';
64
- output: any;
65
- [key: string]: any;
66
- } | {
67
- status: 'pending';
68
- [key: string]: any;
69
- } | {
70
- status: 'suspended';
71
- suspendPayload: any;
72
- [key: string]: any;
73
- }>;
74
- };
61
+ results: WorkflowRunResult$1<any, any, any>['results'];
75
62
  timestamp: number;
76
- suspendedSteps: Record<string, any>;
77
63
  runId: string;
78
64
  };
79
65
  interface UpsertVectorParams {
package/dist/index.js CHANGED
@@ -357,6 +357,7 @@ var Vector = class extends BaseResource {
357
357
  };
358
358
 
359
359
  // src/resources/workflow.ts
360
+ var RECORD_SEPARATOR = "";
360
361
  var Workflow = class extends BaseResource {
361
362
  constructor(options, workflowId) {
362
363
  super(options);
@@ -458,43 +459,34 @@ var Workflow = class extends BaseResource {
458
459
  */
459
460
  async *streamProcessor(stream) {
460
461
  const reader = stream.getReader();
462
+ let doneReading = false;
461
463
  let buffer = "";
462
464
  try {
463
- while (true) {
465
+ while (!doneReading) {
464
466
  const { done, value } = await reader.read();
465
- if (done) {
466
- if (buffer.trim().length > 0) {
467
- try {
468
- const record = JSON.parse(buffer);
469
- yield record;
470
- } catch (e) {
471
- console.warn("Could not parse final buffer content:", buffer);
467
+ doneReading = done;
468
+ if (done && !value) continue;
469
+ try {
470
+ const decoded = value ? new TextDecoder().decode(value) : "";
471
+ const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
472
+ buffer = chunks.pop() || "";
473
+ for (const chunk of chunks) {
474
+ if (chunk) {
475
+ yield JSON.parse(chunk);
472
476
  }
473
477
  }
474
- break;
478
+ } catch (error) {
475
479
  }
476
- buffer += new TextDecoder().decode(value);
477
- const records = buffer.split("");
478
- buffer = records.pop() || "";
479
- for (const record of records) {
480
- if (record.trim().length > 0) {
481
- try {
482
- const parsedRecord = JSON.parse(record);
483
- const isWorkflowCompleted = parsedRecord?.activePaths?.every(
484
- (path) => path.status === "completed" || path.status === "suspended" || path.status === "failed" || path.status === "skipped"
485
- );
486
- if (isWorkflowCompleted) {
487
- reader.cancel();
488
- }
489
- yield parsedRecord;
490
- } catch (e) {
491
- throw new Error(`Could not parse record: ${record}`);
492
- }
493
- }
480
+ }
481
+ if (buffer) {
482
+ try {
483
+ yield JSON.parse(buffer);
484
+ } catch {
494
485
  }
495
486
  }
496
487
  } finally {
497
- reader.cancel();
488
+ reader.cancel().catch(() => {
489
+ });
498
490
  }
499
491
  }
500
492
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/client-js",
3
- "version": "0.1.13",
3
+ "version": "0.1.14-alpha.2",
4
4
  "description": "The official TypeScript library for the Mastra Client API",
5
5
  "author": "",
6
6
  "type": "module",
@@ -26,11 +26,11 @@
26
26
  "json-schema": "^0.4.0",
27
27
  "zod": "^3.24.2",
28
28
  "zod-to-json-schema": "^3.24.3",
29
- "@mastra/core": "^0.7.0"
29
+ "@mastra/core": "^0.8.0-alpha.2"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@babel/preset-env": "^7.26.9",
33
- "@babel/preset-typescript": "^7.26.0",
33
+ "@babel/preset-typescript": "^7.27.0",
34
34
  "@tsconfig/recommended": "^1.0.8",
35
35
  "@types/json-schema": "^7.0.15",
36
36
  "@types/node": "^20.17.27",
@@ -2,6 +2,8 @@ import type { GetWorkflowResponse, ClientOptions, WorkflowRunResult } from '../t
2
2
 
3
3
  import { BaseResource } from './base';
4
4
 
5
+ const RECORD_SEPARATOR = '\x1E';
6
+
5
7
  export class Workflow extends BaseResource {
6
8
  constructor(
7
9
  options: ClientOptions,
@@ -119,61 +121,57 @@ export class Workflow extends BaseResource {
119
121
  */
120
122
  private async *streamProcessor(stream: ReadableStream): AsyncGenerator<WorkflowRunResult, void, unknown> {
121
123
  const reader = stream.getReader();
124
+
125
+ // Track if we've finished reading from the stream
126
+ let doneReading = false;
127
+ // Buffer to accumulate partial chunks
122
128
  let buffer = '';
123
129
 
124
130
  try {
125
- while (true) {
131
+ while (!doneReading) {
132
+ // Read the next chunk from the stream
126
133
  const { done, value } = await reader.read();
134
+ doneReading = done;
135
+
136
+ // Skip processing if we're done and there's no value
137
+ if (done && !value) continue;
138
+
139
+ try {
140
+ // Decode binary data to text
141
+ const decoded = value ? new TextDecoder().decode(value) : '';
142
+
143
+ // Split the combined buffer and new data by record separator
144
+ const chunks = (buffer + decoded).split(RECORD_SEPARATOR);
127
145
 
128
- if (done) {
129
- // Process any remaining data in buffer before finishing
130
- if (buffer.trim().length > 0) {
131
- try {
132
- const record = JSON.parse(buffer);
133
- yield record;
134
- } catch (e) {
135
- console.warn('Could not parse final buffer content:', buffer);
146
+ // The last chunk might be incomplete, so save it for the next iteration
147
+ buffer = chunks.pop() || '';
148
+
149
+ // Process complete chunks
150
+ for (const chunk of chunks) {
151
+ if (chunk) {
152
+ // Only process non-empty chunks
153
+ yield JSON.parse(chunk);
136
154
  }
137
155
  }
138
- break;
156
+ } catch (error) {
157
+ // Silently ignore parsing errors to maintain stream processing
158
+ // This allows the stream to continue even if one record is malformed
139
159
  }
160
+ }
140
161
 
141
- // Decode and add to buffer
142
- buffer += new TextDecoder().decode(value);
143
-
144
- // Split the buffer into records
145
- const records = buffer.split('\x1E');
146
-
147
- // Keep the last (potentially incomplete) chunk in the buffer
148
- buffer = records.pop() || '';
149
-
150
- // Process each complete record
151
- for (const record of records) {
152
- if (record.trim().length > 0) {
153
- try {
154
- // Assuming the records are JSON strings
155
- const parsedRecord = JSON.parse(record);
156
-
157
- //Check to see if all steps are completed and cancel reader
158
- const isWorkflowCompleted = parsedRecord?.activePaths?.every(
159
- (path: any) =>
160
- path.status === 'completed' ||
161
- path.status === 'suspended' ||
162
- path.status === 'failed' ||
163
- path.status === 'skipped',
164
- );
165
- if (isWorkflowCompleted) {
166
- reader.cancel();
167
- }
168
- yield parsedRecord;
169
- } catch (e) {
170
- throw new Error(`Could not parse record: ${record}`);
171
- }
172
- }
162
+ // Process any remaining data in the buffer after stream is done
163
+ if (buffer) {
164
+ try {
165
+ yield JSON.parse(buffer);
166
+ } catch {
167
+ // Ignore parsing error for final chunk
173
168
  }
174
169
  }
175
170
  } finally {
176
- reader.cancel();
171
+ // Always ensure we clean up the reader
172
+ reader.cancel().catch(() => {
173
+ // Ignore cancel errors
174
+ });
177
175
  }
178
176
  }
179
177
 
package/src/types.ts CHANGED
@@ -7,6 +7,7 @@ import type {
7
7
  StepGraph,
8
8
  StorageThreadType,
9
9
  BaseLogMessage,
10
+ WorkflowRunResult as CoreWorkflowRunResult,
10
11
  } from '@mastra/core';
11
12
 
12
13
  import type { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
@@ -71,35 +72,11 @@ export interface GetWorkflowResponse {
71
72
  }
72
73
 
73
74
  export type WorkflowRunResult = {
74
- activePaths: Array<{
75
- stepId: string;
76
- stepPath: string[];
77
- status: 'completed' | 'suspended' | 'pending';
78
- }>;
79
- context: {
80
- steps: Record<
81
- string,
82
- | {
83
- status: 'success';
84
- output: any;
85
- [key: string]: any;
86
- }
87
- | {
88
- status: 'pending';
89
- [key: string]: any;
90
- }
91
- | {
92
- status: 'suspended';
93
- suspendPayload: any;
94
- [key: string]: any;
95
- }
96
- >;
97
- };
75
+ activePaths: Record<string, { status: string; suspendPayload?: any; stepPath: string[] }>;
76
+ results: CoreWorkflowRunResult<any, any, any>['results'];
98
77
  timestamp: number;
99
- suspendedSteps: Record<string, any>;
100
78
  runId: string;
101
79
  };
102
-
103
80
  export interface UpsertVectorParams {
104
81
  indexName: string;
105
82
  vectors: number[][];