@mastra/client-js 0.1.0-alpha.2 → 0.1.0-alpha.4
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/.eslintrc.js +10 -0
- package/.prettierignore +3 -0
- package/.prettierrc.json +26 -0
- package/LICENSE +44 -201
- package/README.md +90 -271
- package/dist/index.d.mts +359 -0
- package/dist/index.mjs +399 -0
- package/package.json +30 -95
- package/src/client.ts +201 -0
- package/src/index.test.ts +528 -0
- package/src/index.ts +2 -200
- package/src/resources/agent.ts +97 -0
- package/src/resources/index.ts +5 -14
- package/src/resources/memory-thread.ts +51 -0
- package/src/resources/tool.ts +28 -0
- package/src/resources/vector.ts +79 -0
- package/src/resources/workflow.ts +28 -0
- package/src/types.ts +135 -0
- package/tsconfig.json +29 -0
- package/vitest.config.js +8 -0
- package/CHANGELOG.md +0 -29
- package/_shims/MultipartBody.d.ts +0 -9
- package/_shims/MultipartBody.d.ts.map +0 -1
- package/_shims/MultipartBody.js +0 -16
- package/_shims/MultipartBody.js.map +0 -1
- package/_shims/MultipartBody.mjs +0 -12
- package/_shims/MultipartBody.mjs.map +0 -1
- package/_shims/README.md +0 -46
- package/_shims/auto/runtime-bun.d.ts +0 -5
- package/_shims/auto/runtime-bun.d.ts.map +0 -1
- package/_shims/auto/runtime-bun.js +0 -21
- package/_shims/auto/runtime-bun.js.map +0 -1
- package/_shims/auto/runtime-bun.mjs +0 -2
- package/_shims/auto/runtime-bun.mjs.map +0 -1
- package/_shims/auto/runtime-node.d.ts +0 -5
- package/_shims/auto/runtime-node.d.ts.map +0 -1
- package/_shims/auto/runtime-node.js +0 -21
- package/_shims/auto/runtime-node.js.map +0 -1
- package/_shims/auto/runtime-node.mjs +0 -2
- package/_shims/auto/runtime-node.mjs.map +0 -1
- package/_shims/auto/runtime.d.ts +0 -5
- package/_shims/auto/runtime.d.ts.map +0 -1
- package/_shims/auto/runtime.js +0 -21
- package/_shims/auto/runtime.js.map +0 -1
- package/_shims/auto/runtime.mjs +0 -2
- package/_shims/auto/runtime.mjs.map +0 -1
- package/_shims/auto/types-node.d.ts +0 -5
- package/_shims/auto/types-node.d.ts.map +0 -1
- package/_shims/auto/types-node.js +0 -21
- package/_shims/auto/types-node.js.map +0 -1
- package/_shims/auto/types-node.mjs +0 -2
- package/_shims/auto/types-node.mjs.map +0 -1
- package/_shims/auto/types.d.ts +0 -101
- package/_shims/auto/types.js +0 -3
- package/_shims/auto/types.mjs +0 -3
- package/_shims/bun-runtime.d.ts +0 -6
- package/_shims/bun-runtime.d.ts.map +0 -1
- package/_shims/bun-runtime.js +0 -14
- package/_shims/bun-runtime.js.map +0 -1
- package/_shims/bun-runtime.mjs +0 -10
- package/_shims/bun-runtime.mjs.map +0 -1
- package/_shims/index.d.ts +0 -81
- package/_shims/index.js +0 -13
- package/_shims/index.mjs +0 -7
- package/_shims/manual-types.d.ts +0 -12
- package/_shims/manual-types.js +0 -3
- package/_shims/manual-types.mjs +0 -3
- package/_shims/node-runtime.d.ts +0 -3
- package/_shims/node-runtime.d.ts.map +0 -1
- package/_shims/node-runtime.js +0 -89
- package/_shims/node-runtime.js.map +0 -1
- package/_shims/node-runtime.mjs +0 -56
- package/_shims/node-runtime.mjs.map +0 -1
- package/_shims/node-types.d.ts +0 -42
- package/_shims/node-types.js +0 -3
- package/_shims/node-types.mjs +0 -3
- package/_shims/registry.d.ts +0 -37
- package/_shims/registry.d.ts.map +0 -1
- package/_shims/registry.js +0 -41
- package/_shims/registry.js.map +0 -1
- package/_shims/registry.mjs +0 -37
- package/_shims/registry.mjs.map +0 -1
- package/_shims/web-runtime.d.ts +0 -5
- package/_shims/web-runtime.d.ts.map +0 -1
- package/_shims/web-runtime.js +0 -78
- package/_shims/web-runtime.js.map +0 -1
- package/_shims/web-runtime.mjs +0 -71
- package/_shims/web-runtime.mjs.map +0 -1
- package/_shims/web-types.d.ts +0 -83
- package/_shims/web-types.js +0 -3
- package/_shims/web-types.mjs +0 -3
- package/core.d.ts +0 -241
- package/core.d.ts.map +0 -1
- package/core.js +0 -908
- package/core.js.map +0 -1
- package/core.mjs +0 -876
- package/core.mjs.map +0 -1
- package/error.d.ts +0 -47
- package/error.d.ts.map +0 -1
- package/error.js +0 -113
- package/error.js.map +0 -1
- package/error.mjs +0 -97
- package/error.mjs.map +0 -1
- package/index.d.mts +0 -121
- package/index.d.ts +0 -121
- package/index.d.ts.map +0 -1
- package/index.js +0 -131
- package/index.js.map +0 -1
- package/index.mjs +0 -88
- package/index.mjs.map +0 -1
- package/resource.d.ts +0 -6
- package/resource.d.ts.map +0 -1
- package/resource.js +0 -11
- package/resource.js.map +0 -1
- package/resource.mjs +0 -7
- package/resource.mjs.map +0 -1
- package/resources/agents/agents.d.ts +0 -41
- package/resources/agents/agents.d.ts.map +0 -1
- package/resources/agents/agents.js +0 -74
- package/resources/agents/agents.js.map +0 -1
- package/resources/agents/agents.mjs +0 -47
- package/resources/agents/agents.mjs.map +0 -1
- package/resources/agents/index.d.ts +0 -3
- package/resources/agents/index.d.ts.map +0 -1
- package/resources/agents/index.js +0 -9
- package/resources/agents/index.js.map +0 -1
- package/resources/agents/index.mjs +0 -4
- package/resources/agents/index.mjs.map +0 -1
- package/resources/agents/tools.d.ts +0 -17
- package/resources/agents/tools.d.ts.map +0 -1
- package/resources/agents/tools.js +0 -19
- package/resources/agents/tools.js.map +0 -1
- package/resources/agents/tools.mjs +0 -15
- package/resources/agents/tools.mjs.map +0 -1
- package/resources/index.d.ts +0 -8
- package/resources/index.d.ts.map +0 -1
- package/resources/index.js +0 -19
- package/resources/index.js.map +0 -1
- package/resources/index.mjs +0 -9
- package/resources/index.mjs.map +0 -1
- package/resources/logs.d.ts +0 -13
- package/resources/logs.d.ts.map +0 -1
- package/resources/logs.js +0 -24
- package/resources/logs.js.map +0 -1
- package/resources/logs.mjs +0 -20
- package/resources/logs.mjs.map +0 -1
- package/resources/memory/index.d.ts +0 -4
- package/resources/memory/index.d.ts.map +0 -1
- package/resources/memory/index.js +0 -11
- package/resources/memory/index.js.map +0 -1
- package/resources/memory/index.mjs +0 -5
- package/resources/memory/index.mjs.map +0 -1
- package/resources/memory/memory.d.ts +0 -23
- package/resources/memory/memory.d.ts.map +0 -1
- package/resources/memory/memory.js +0 -53
- package/resources/memory/memory.js.map +0 -1
- package/resources/memory/memory.mjs +0 -26
- package/resources/memory/memory.mjs.map +0 -1
- package/resources/memory/status.d.ts +0 -9
- package/resources/memory/status.d.ts.map +0 -1
- package/resources/memory/status.js +0 -18
- package/resources/memory/status.js.map +0 -1
- package/resources/memory/status.mjs +0 -14
- package/resources/memory/status.mjs.map +0 -1
- package/resources/memory/threads/index.d.ts +0 -3
- package/resources/memory/threads/index.d.ts.map +0 -1
- package/resources/memory/threads/index.js +0 -9
- package/resources/memory/threads/index.js.map +0 -1
- package/resources/memory/threads/index.mjs +0 -4
- package/resources/memory/threads/index.mjs.map +0 -1
- package/resources/memory/threads/messages.d.ts +0 -9
- package/resources/memory/threads/messages.d.ts.map +0 -1
- package/resources/memory/threads/messages.js +0 -18
- package/resources/memory/threads/messages.js.map +0 -1
- package/resources/memory/threads/messages.mjs +0 -14
- package/resources/memory/threads/messages.mjs.map +0 -1
- package/resources/memory/threads/threads.d.ts +0 -45
- package/resources/memory/threads/threads.d.ts.map +0 -1
- package/resources/memory/threads/threads.js +0 -104
- package/resources/memory/threads/threads.js.map +0 -1
- package/resources/memory/threads/threads.mjs +0 -77
- package/resources/memory/threads/threads.mjs.map +0 -1
- package/resources/syncs.d.ts +0 -15
- package/resources/syncs.d.ts.map +0 -1
- package/resources/syncs.js +0 -19
- package/resources/syncs.js.map +0 -1
- package/resources/syncs.mjs +0 -15
- package/resources/syncs.mjs.map +0 -1
- package/resources/system.d.ts +0 -9
- package/resources/system.d.ts.map +0 -1
- package/resources/system.js +0 -15
- package/resources/system.js.map +0 -1
- package/resources/system.mjs +0 -11
- package/resources/system.mjs.map +0 -1
- package/resources/tools/index.d.ts +0 -3
- package/resources/tools/index.d.ts.map +0 -1
- package/resources/tools/index.js +0 -9
- package/resources/tools/index.js.map +0 -1
- package/resources/tools/index.mjs +0 -4
- package/resources/tools/index.mjs.map +0 -1
- package/resources/tools/result.d.ts +0 -9
- package/resources/tools/result.d.ts.map +0 -1
- package/resources/tools/result.js +0 -18
- package/resources/tools/result.js.map +0 -1
- package/resources/tools/result.mjs +0 -14
- package/resources/tools/result.mjs.map +0 -1
- package/resources/tools/tools.d.ts +0 -29
- package/resources/tools/tools.d.ts.map +0 -1
- package/resources/tools/tools.js +0 -64
- package/resources/tools/tools.js.map +0 -1
- package/resources/tools/tools.mjs +0 -37
- package/resources/tools/tools.mjs.map +0 -1
- package/resources/workflows.d.ts +0 -23
- package/resources/workflows.d.ts.map +0 -1
- package/resources/workflows.js +0 -37
- package/resources/workflows.js.map +0 -1
- package/resources/workflows.mjs +0 -33
- package/resources/workflows.mjs.map +0 -1
- package/shims/node.d.ts +0 -30
- package/shims/node.d.ts.map +0 -1
- package/shims/node.js +0 -31
- package/shims/node.js.map +0 -1
- package/shims/node.mjs +0 -5
- package/shims/node.mjs.map +0 -1
- package/shims/web.d.ts +0 -26
- package/shims/web.d.ts.map +0 -1
- package/shims/web.js +0 -31
- package/shims/web.js.map +0 -1
- package/shims/web.mjs +0 -5
- package/shims/web.mjs.map +0 -1
- package/src/_shims/MultipartBody.ts +0 -9
- package/src/_shims/README.md +0 -46
- package/src/_shims/auto/runtime-bun.ts +0 -4
- package/src/_shims/auto/runtime-node.ts +0 -4
- package/src/_shims/auto/runtime.ts +0 -4
- package/src/_shims/auto/types-node.ts +0 -4
- package/src/_shims/auto/types.d.ts +0 -101
- package/src/_shims/auto/types.js +0 -3
- package/src/_shims/auto/types.mjs +0 -3
- package/src/_shims/bun-runtime.ts +0 -14
- package/src/_shims/index.d.ts +0 -81
- package/src/_shims/index.js +0 -13
- package/src/_shims/index.mjs +0 -7
- package/src/_shims/manual-types.d.ts +0 -12
- package/src/_shims/manual-types.js +0 -3
- package/src/_shims/manual-types.mjs +0 -3
- package/src/_shims/node-runtime.ts +0 -81
- package/src/_shims/node-types.d.ts +0 -42
- package/src/_shims/node-types.js +0 -3
- package/src/_shims/node-types.mjs +0 -3
- package/src/_shims/registry.ts +0 -67
- package/src/_shims/web-runtime.ts +0 -103
- package/src/_shims/web-types.d.ts +0 -83
- package/src/_shims/web-types.js +0 -3
- package/src/_shims/web-types.mjs +0 -3
- package/src/core.ts +0 -1201
- package/src/error.ts +0 -130
- package/src/lib/.keep +0 -4
- package/src/resource.ts +0 -11
- package/src/resources/agents/agents.ts +0 -83
- package/src/resources/agents/index.ts +0 -4
- package/src/resources/agents/tools.ts +0 -34
- package/src/resources/logs.ts +0 -23
- package/src/resources/memory/index.ts +0 -5
- package/src/resources/memory/memory.ts +0 -43
- package/src/resources/memory/status.ts +0 -16
- package/src/resources/memory/threads/index.ts +0 -4
- package/src/resources/memory/threads/messages.ts +0 -16
- package/src/resources/memory/threads/threads.ts +0 -105
- package/src/resources/syncs.ts +0 -25
- package/src/resources/system.ts +0 -13
- package/src/resources/tools/index.ts +0 -4
- package/src/resources/tools/result.ts +0 -16
- package/src/resources/tools/tools.ts +0 -54
- package/src/resources/workflows.ts +0 -49
- package/src/shims/node.ts +0 -50
- package/src/shims/web.ts +0 -50
- package/src/tsconfig.json +0 -11
- package/src/uploads.ts +0 -255
- package/src/version.ts +0 -1
- package/uploads.d.ts +0 -75
- package/uploads.d.ts.map +0 -1
- package/uploads.js +0 -171
- package/uploads.js.map +0 -1
- package/uploads.mjs +0 -158
- package/uploads.mjs.map +0 -1
- package/version.d.ts +0 -2
- package/version.d.ts.map +0 -1
- package/version.js +0 -5
- package/version.js.map +0 -1
- package/version.mjs +0 -2
- package/version.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1,291 +1,110 @@
|
|
|
1
|
-
# Mastra Client
|
|
1
|
+
# Mastra Client
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
-
|
|
5
|
-
This library provides convenient access to the Mastra Client REST API from server-side TypeScript or JavaScript.
|
|
6
|
-
|
|
7
|
-
The REST API documentation can be found on [mastra.ai](https://mastra.ai/docs). The full API of this library can be found in [api.md](api.md).
|
|
8
|
-
|
|
9
|
-
It is generated with [Stainless](https://www.stainlessapi.com/).
|
|
3
|
+
JavaScript/TypeScript client library for the [Mastra AI](https://mastra.ai) framework. This client provides a simple interface to interact with Mastra AI's APIs for agents, vectors, memory, tools, and workflows.
|
|
10
4
|
|
|
11
5
|
## Installation
|
|
12
6
|
|
|
13
|
-
```
|
|
7
|
+
```bash
|
|
14
8
|
npm install @mastra/client-js
|
|
15
9
|
```
|
|
16
10
|
|
|
17
|
-
##
|
|
18
|
-
|
|
19
|
-
The full API of this library can be found in [api.md](api.md).
|
|
20
|
-
|
|
21
|
-
<!-- prettier-ignore -->
|
|
22
|
-
```js
|
|
23
|
-
import MastraClient from '@mastra/client-js';
|
|
24
|
-
|
|
25
|
-
const client = new MastraClient();
|
|
26
|
-
|
|
27
|
-
async function main() {
|
|
28
|
-
await client.agents.list();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
main();
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### Request & Response types
|
|
35
|
-
|
|
36
|
-
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
|
|
37
|
-
|
|
38
|
-
<!-- prettier-ignore -->
|
|
39
|
-
```ts
|
|
40
|
-
import MastraClient from '@mastra/client-js';
|
|
41
|
-
|
|
42
|
-
const client = new MastraClient();
|
|
43
|
-
|
|
44
|
-
async function main() {
|
|
45
|
-
await client.agents.list();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
main();
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
|
|
52
|
-
|
|
53
|
-
## Handling errors
|
|
54
|
-
|
|
55
|
-
When the library is unable to connect to the API,
|
|
56
|
-
or if the API returns a non-success status code (i.e., 4xx or 5xx response),
|
|
57
|
-
a subclass of `APIError` will be thrown:
|
|
58
|
-
|
|
59
|
-
<!-- prettier-ignore -->
|
|
60
|
-
```ts
|
|
61
|
-
async function main() {
|
|
62
|
-
const response = await client.agents.list().catch(async (err) => {
|
|
63
|
-
if (err instanceof MastraClient.APIError) {
|
|
64
|
-
console.log(err.status); // 400
|
|
65
|
-
console.log(err.name); // BadRequestError
|
|
66
|
-
console.log(err.headers); // {server: 'nginx', ...}
|
|
67
|
-
} else {
|
|
68
|
-
throw err;
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
main();
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
Error codes are as followed:
|
|
77
|
-
|
|
78
|
-
| Status Code | Error Type |
|
|
79
|
-
| ----------- | -------------------------- |
|
|
80
|
-
| 400 | `BadRequestError` |
|
|
81
|
-
| 401 | `AuthenticationError` |
|
|
82
|
-
| 403 | `PermissionDeniedError` |
|
|
83
|
-
| 404 | `NotFoundError` |
|
|
84
|
-
| 422 | `UnprocessableEntityError` |
|
|
85
|
-
| 429 | `RateLimitError` |
|
|
86
|
-
| >=500 | `InternalServerError` |
|
|
87
|
-
| N/A | `APIConnectionError` |
|
|
88
|
-
|
|
89
|
-
### Retries
|
|
90
|
-
|
|
91
|
-
Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
|
|
92
|
-
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
|
|
93
|
-
429 Rate Limit, and >=500 Internal errors will all be retried by default.
|
|
11
|
+
## Quick Start
|
|
94
12
|
|
|
95
|
-
|
|
13
|
+
```typescript
|
|
14
|
+
import { MastraClient } from '@mastra/client';
|
|
96
15
|
|
|
97
|
-
|
|
98
|
-
```js
|
|
99
|
-
// Configure the default for all requests:
|
|
16
|
+
// Initialize the client
|
|
100
17
|
const client = new MastraClient({
|
|
101
|
-
|
|
18
|
+
baseUrl: 'http://localhost:4111', // Your Mastra API endpoint
|
|
102
19
|
});
|
|
103
20
|
|
|
104
|
-
//
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// Configure the default for all requests:
|
|
117
|
-
const client = new MastraClient({
|
|
118
|
-
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
// Override per-request:
|
|
122
|
-
await client.agents.list({
|
|
123
|
-
timeout: 5 * 1000,
|
|
124
|
-
});
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
On timeout, an `APIConnectionTimeoutError` is thrown.
|
|
128
|
-
|
|
129
|
-
Note that requests which time out will be [retried twice by default](#retries).
|
|
130
|
-
|
|
131
|
-
## Advanced Usage
|
|
132
|
-
|
|
133
|
-
### Accessing raw Response data (e.g., headers)
|
|
134
|
-
|
|
135
|
-
The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.
|
|
136
|
-
|
|
137
|
-
You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.
|
|
138
|
-
|
|
139
|
-
<!-- prettier-ignore -->
|
|
140
|
-
```ts
|
|
141
|
-
const client = new MastraClient();
|
|
142
|
-
|
|
143
|
-
const response = await client.agents.list().asResponse();
|
|
144
|
-
console.log(response.headers.get('X-My-Header'));
|
|
145
|
-
console.log(response.statusText); // access the underlying Response object
|
|
146
|
-
|
|
147
|
-
const { data: result, response: raw } = await client.agents.list().withResponse();
|
|
148
|
-
console.log(raw.headers.get('X-My-Header'));
|
|
149
|
-
console.log(result);
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
### Making custom/undocumented requests
|
|
153
|
-
|
|
154
|
-
This library is typed for convenient access to the documented API. If you need to access undocumented
|
|
155
|
-
endpoints, params, or response properties, the library can still be used.
|
|
156
|
-
|
|
157
|
-
#### Undocumented endpoints
|
|
158
|
-
|
|
159
|
-
To make requests to undocumented endpoints, you can use `client.get`, `client.post`, and other HTTP verbs.
|
|
160
|
-
Options on the client, such as retries, will be respected when making these requests.
|
|
161
|
-
|
|
162
|
-
```ts
|
|
163
|
-
await client.post('/some/path', {
|
|
164
|
-
body: { some_prop: 'foo' },
|
|
165
|
-
query: { some_query_arg: 'bar' },
|
|
166
|
-
});
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
#### Undocumented request params
|
|
170
|
-
|
|
171
|
-
To make requests using undocumented parameters, you may use `// @ts-expect-error` on the undocumented
|
|
172
|
-
parameter. This library doesn't validate at runtime that the request matches the type, so any extra values you
|
|
173
|
-
send will be sent as-is.
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
client.foo.create({
|
|
177
|
-
foo: 'my_param',
|
|
178
|
-
bar: 12,
|
|
179
|
-
// @ts-expect-error baz is not yet public
|
|
180
|
-
baz: 'undocumented option',
|
|
181
|
-
});
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
For requests with the `GET` verb, any extra params will be in the query, all other requests will send the
|
|
185
|
-
extra param in the body.
|
|
186
|
-
|
|
187
|
-
If you want to explicitly send an extra argument, you can do so with the `query`, `body`, and `headers` request
|
|
188
|
-
options.
|
|
189
|
-
|
|
190
|
-
#### Undocumented response properties
|
|
191
|
-
|
|
192
|
-
To access undocumented response properties, you may access the response object with `// @ts-expect-error` on
|
|
193
|
-
the response object, or cast the response object to the requisite type. Like the request params, we do not
|
|
194
|
-
validate or strip extra properties from the response from the API.
|
|
195
|
-
|
|
196
|
-
### Customizing the fetch client
|
|
197
|
-
|
|
198
|
-
By default, this library uses `node-fetch` in Node, and expects a global `fetch` function in other environments.
|
|
199
|
-
|
|
200
|
-
If you would prefer to use a global, web-standards-compliant `fetch` function even in a Node environment,
|
|
201
|
-
(for example, if you are running Node with `--experimental-fetch` or using NextJS which polyfills with `undici`),
|
|
202
|
-
add the following import before your first import `from "MastraClient"`:
|
|
203
|
-
|
|
204
|
-
```ts
|
|
205
|
-
// Tell TypeScript and the package to use the global web fetch instead of node-fetch.
|
|
206
|
-
// Note, despite the name, this does not add any polyfills, but expects them to be provided if needed.
|
|
207
|
-
import '@mastra/client-js/shims/web';
|
|
208
|
-
import MastraClient from '@mastra/client-js';
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
To do the inverse, add `import "@mastra/client-js/shims/node"` (which does import polyfills).
|
|
212
|
-
This can also be useful if you are getting the wrong TypeScript types for `Response` ([more details](https://github.com/mastra-ai/client-js/tree/main/src/_shims#readme)).
|
|
213
|
-
|
|
214
|
-
### Logging and middleware
|
|
215
|
-
|
|
216
|
-
You may also provide a custom `fetch` function when instantiating the client,
|
|
217
|
-
which can be used to inspect or alter the `Request` or `Response` before/after each request:
|
|
218
|
-
|
|
219
|
-
```ts
|
|
220
|
-
import { fetch } from 'undici'; // as one example
|
|
221
|
-
import MastraClient from '@mastra/client-js';
|
|
222
|
-
|
|
223
|
-
const client = new MastraClient({
|
|
224
|
-
fetch: async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
|
|
225
|
-
console.log('About to make a request', url, init);
|
|
226
|
-
const response = await fetch(url, init);
|
|
227
|
-
console.log('Got response', response);
|
|
228
|
-
return response;
|
|
229
|
-
},
|
|
230
|
-
});
|
|
21
|
+
// Example: Working with an Agent
|
|
22
|
+
async function main() {
|
|
23
|
+
// Get an agent instance
|
|
24
|
+
const agent = client.getAgent('your-agent-id');
|
|
25
|
+
|
|
26
|
+
// Generate a response
|
|
27
|
+
const response = await agent.generate({
|
|
28
|
+
messages: [{ role: 'user', content: "What's the weather like today?" }],
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log(response);
|
|
32
|
+
}
|
|
231
33
|
```
|
|
232
34
|
|
|
233
|
-
|
|
234
|
-
This is intended for debugging purposes only and may change in the future without notice.
|
|
235
|
-
|
|
236
|
-
### Configuring an HTTP(S) Agent (e.g., for proxies)
|
|
237
|
-
|
|
238
|
-
By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.
|
|
35
|
+
## Client Configuration
|
|
239
36
|
|
|
240
|
-
|
|
37
|
+
The client can be configured with several options:
|
|
241
38
|
|
|
242
|
-
|
|
243
|
-
```ts
|
|
244
|
-
import http from 'http';
|
|
245
|
-
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
246
|
-
|
|
247
|
-
// Configure the default for all requests:
|
|
39
|
+
```typescript
|
|
248
40
|
const client = new MastraClient({
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
//
|
|
253
|
-
|
|
254
|
-
httpAgent: new http.Agent({ keepAlive: false }),
|
|
41
|
+
baseUrl: string; // Base URL for the Mastra API
|
|
42
|
+
retries?: number; // Number of retry attempts (default: 3)
|
|
43
|
+
backoffMs?: number; // Initial backoff time in ms (default: 300)
|
|
44
|
+
maxBackoffMs?: number; // Maximum backoff time in ms (default: 5000)
|
|
45
|
+
headers?: Record<string, string>; // Custom headers
|
|
255
46
|
});
|
|
256
47
|
```
|
|
257
48
|
|
|
258
|
-
##
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
-
|
|
279
|
-
-
|
|
280
|
-
-
|
|
281
|
-
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
49
|
+
## Available Methods
|
|
50
|
+
|
|
51
|
+
### Agents
|
|
52
|
+
- `getAgents()`: Get all available agents
|
|
53
|
+
- `getAgent(agentId)`: Get a specific agent instance
|
|
54
|
+
- `agent.details()`: Get agent details
|
|
55
|
+
- `agent.generate(params)`: Generate a response
|
|
56
|
+
- `agent.stream(params)`: Stream a response
|
|
57
|
+
- `agent.getTool(toolId)`: Get agent tool details
|
|
58
|
+
- `agent.evals()`: Get agent evaluations
|
|
59
|
+
- `agent.liveEvals()`: Get live evaluations
|
|
60
|
+
|
|
61
|
+
### Memory
|
|
62
|
+
- `getMemoryThreads(params)`: Get memory threads
|
|
63
|
+
- `createMemoryThread(params)`: Create a new memory thread
|
|
64
|
+
- `getMemoryThread(threadId)`: Get a memory thread instance
|
|
65
|
+
- `saveMessageToMemory(params)`: Save messages to memory
|
|
66
|
+
- `getMemoryStatus()`: Get memory system status
|
|
67
|
+
|
|
68
|
+
### Tools
|
|
69
|
+
- `getTools()`: Get all available tools
|
|
70
|
+
- `getTool(toolId)`: Get a tool instance
|
|
71
|
+
- `tool.details()`: Get tool details
|
|
72
|
+
- `tool.execute(params)`: Execute the tool
|
|
73
|
+
|
|
74
|
+
### Workflows
|
|
75
|
+
- `getWorkflows()`: Get all workflows
|
|
76
|
+
- `getWorkflow(workflowId)`: Get a workflow instance
|
|
77
|
+
- `workflow.details()`: Get workflow details
|
|
78
|
+
- `workflow.execute(params)`: Execute the workflow
|
|
79
|
+
|
|
80
|
+
### Vectors
|
|
81
|
+
- `getVector(vectorName)`: Get a vector instance
|
|
82
|
+
- `vector.details(indexName)`: Get vector index details
|
|
83
|
+
- `vector.delete(indexName)`: Delete a vector index
|
|
84
|
+
- `vector.getIndexes()`: Get all indexes
|
|
85
|
+
- `vector.createIndex(params)`: Create a new index
|
|
86
|
+
- `vector.upsert(params)`: Upsert vectors
|
|
87
|
+
- `vector.query(params)`: Query vectors
|
|
88
|
+
|
|
89
|
+
### Logs
|
|
90
|
+
- `getLogs(params)`: Get system logs
|
|
91
|
+
- `getLog(params)`: Get specific log entry
|
|
92
|
+
|
|
93
|
+
## Error Handling
|
|
94
|
+
|
|
95
|
+
The client includes built-in retry logic for failed requests:
|
|
96
|
+
- Automatically retries failed requests with exponential backoff
|
|
97
|
+
- Configurable retry count and backoff timing
|
|
98
|
+
- Throws error after max retries reached
|
|
99
|
+
|
|
100
|
+
## Internal Implementation
|
|
101
|
+
|
|
102
|
+
The client uses the native `fetch` API internally for making HTTP requests. All requests are automatically handled with:
|
|
103
|
+
- JSON serialization/deserialization
|
|
104
|
+
- Retry logic with exponential backoff
|
|
105
|
+
- Custom header management
|
|
106
|
+
- Error handling
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
MIT
|