@mastra/client-js 0.1.0-alpha.7 → 0.1.0-alpha.9

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/dist/index.d.mts CHANGED
@@ -18,6 +18,7 @@ interface RequestOptions {
18
18
  method?: string;
19
19
  headers?: Record<string, string>;
20
20
  body?: any;
21
+ stream?: boolean;
21
22
  }
22
23
  interface GetAgentResponse {
23
24
  name: string;
@@ -144,7 +145,7 @@ declare class Agent extends BaseResource {
144
145
  * @param params - Stream parameters including prompt
145
146
  * @returns Promise containing the streamed response
146
147
  */
147
- stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): T extends undefined ? Promise<ReadableStream<Uint8Array>> : Promise<Response>;
148
+ stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response>;
148
149
  /**
149
150
  * Gets details about a specific tool available to the agent
150
151
  * @param toolId - ID of the tool to retrieve
package/dist/index.mjs CHANGED
@@ -43,10 +43,7 @@ var BaseResource = class {
43
43
  }
44
44
  throw new Error(errorMessage);
45
45
  }
46
- if (response.headers.get("Content-Type")?.includes("text/event-stream")) {
47
- return response.body;
48
- }
49
- if (response.headers.get("Content-Type")?.includes("text/x-unknown")) {
46
+ if (options.stream) {
50
47
  return response;
51
48
  }
52
49
  const data = await response.json();
@@ -100,12 +97,12 @@ var Agent = class extends BaseResource {
100
97
  stream(params) {
101
98
  const processedParams = {
102
99
  ...params,
103
- output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
104
- stream: true
100
+ output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output
105
101
  };
106
102
  return this.request(`/api/agents/${this.agentId}/stream`, {
107
103
  method: "POST",
108
- body: processedParams
104
+ body: processedParams,
105
+ stream: true
109
106
  });
110
107
  }
111
108
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/client-js",
3
- "version": "0.1.0-alpha.7",
3
+ "version": "0.1.0-alpha.9",
4
4
  "description": "The official TypeScript library for the Mastra Client API",
5
5
  "author": "",
6
6
  "types": "dist/index.d.mts",
package/src/example.ts CHANGED
@@ -16,17 +16,30 @@ import { MastraClient } from "./client";
16
16
  createdAt: new Date(),
17
17
  threadId: '1',
18
18
  type: 'text',
19
- }],
19
+ }]
20
20
  })
21
21
 
22
22
 
23
- const reader = response?.getReader()
23
+ const reader = response?.body?.getReader()
24
+ const decoder = new TextDecoder()
25
+ let buffer = ''
26
+
24
27
  while (true) {
25
- const { done, value } = await reader.read();
28
+ if (!reader) break;
29
+ const { value, done } = await reader.read()
26
30
  if (done) break;
27
31
 
28
- const text = new TextDecoder().decode(value);
29
- console.log(text);
32
+ const chunk = decoder.decode(value);
33
+ buffer += chunk;
34
+
35
+ console.log(buffer);
36
+
37
+ const matches = buffer.matchAll(/0:"([^"]*)"/g);
38
+
39
+ for (const match of matches) {
40
+ const content = match[1];
41
+ process.stdout.write(`${content}\n`);
42
+ }
30
43
  }
31
44
  } catch (error) {
32
45
  console.error(error);
package/src/index.test.ts CHANGED
@@ -266,8 +266,8 @@ describe('MastraClient Resources', () => {
266
266
  }]
267
267
  });
268
268
 
269
- expect(response).toBeInstanceOf(ReadableStream);
270
- const reader = response?.getReader();
269
+ expect(response.body).toBeInstanceOf(ReadableStream);
270
+ const reader = response?.body?.getReader();
271
271
  expect(reader).toBeDefined();
272
272
 
273
273
  if (reader) {
@@ -75,17 +75,17 @@ export class Agent extends BaseResource {
75
75
  * @param params - Stream parameters including prompt
76
76
  * @returns Promise containing the streamed response
77
77
  */
78
- stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>) {
78
+ stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response> {
79
79
  const processedParams = {
80
80
  ...params,
81
81
  output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
82
- stream: true
83
82
  };
84
83
 
85
84
  return this.request(`/api/agents/${this.agentId}/stream`, {
86
85
  method: 'POST',
87
86
  body: processedParams,
88
- }) as T extends undefined ? Promise<ReadableStream<Uint8Array>> : Promise<Response>;
87
+ stream: true
88
+ })
89
89
  }
90
90
 
91
91
  /**
@@ -17,10 +17,8 @@ export class BaseResource {
17
17
  let lastError: Error | null = null;
18
18
  const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1000, headers = {} } = this.options;
19
19
 
20
-
21
20
  let delay = backoffMs;
22
21
 
23
-
24
22
  for (let attempt = 0; attempt <= retries; attempt++) {
25
23
  try {
26
24
  const response = await fetch(`${baseUrl}${path}`, {
@@ -37,11 +35,9 @@ export class BaseResource {
37
35
  const errorBody = await response.text();
38
36
  let errorMessage = `HTTP error! status: ${response.status}`;
39
37
  try {
40
- // Try to parse as JSON for structured error messages
41
38
  const errorJson = JSON.parse(errorBody);
42
39
  errorMessage += ` - ${JSON.stringify(errorJson)}`;
43
40
  } catch {
44
- // If not JSON, include the raw error body if present
45
41
  if (errorBody) {
46
42
  errorMessage += ` - ${errorBody}`;
47
43
  }
@@ -49,14 +45,8 @@ export class BaseResource {
49
45
  throw new Error(errorMessage);
50
46
  }
51
47
 
52
- // For streaming responses, return the stream directly
53
- if (response.headers.get('Content-Type')?.includes('text/event-stream')) {
54
- return response.body as T;
55
- }
56
-
57
- // For unknown responses, return the response as is
58
- if (response.headers.get('Content-Type')?.includes('text/x-unknown')) {
59
- return response as T;
48
+ if (options.stream) {
49
+ return response as unknown as T;
60
50
  }
61
51
 
62
52
  const data = await response.json();
@@ -68,7 +58,6 @@ export class BaseResource {
68
58
  break;
69
59
  }
70
60
 
71
- // Wait with exponential backoff
72
61
  await new Promise(resolve => setTimeout(resolve, delay));
73
62
  delay = Math.min(delay * 2, maxBackoffMs);
74
63
  }
package/src/types.ts CHANGED
@@ -19,6 +19,7 @@ export interface RequestOptions {
19
19
  method?: string;
20
20
  headers?: Record<string, string>;
21
21
  body?: any;
22
+ stream?: boolean;
22
23
  }
23
24
 
24
25
  export interface GetAgentResponse {