@mastra/client-js 0.0.0-commonjs-20250227130920

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.cjs ADDED
@@ -0,0 +1,489 @@
1
+ 'use strict';
2
+
3
+ var zod = require('zod');
4
+ var zodToJsonSchema = require('zod-to-json-schema');
5
+
6
+ // src/resources/agent.ts
7
+
8
+ // src/resources/base.ts
9
+ var BaseResource = class {
10
+ options;
11
+ constructor(options) {
12
+ this.options = options;
13
+ }
14
+ /**
15
+ * Makes an HTTP request to the API with retries and exponential backoff
16
+ * @param path - The API endpoint path
17
+ * @param options - Optional request configuration
18
+ * @returns Promise containing the response data
19
+ */
20
+ async request(path, options = {}) {
21
+ let lastError = null;
22
+ const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1e3, headers = {} } = this.options;
23
+ let delay = backoffMs;
24
+ for (let attempt = 0; attempt <= retries; attempt++) {
25
+ try {
26
+ const response = await fetch(`${baseUrl}${path}`, {
27
+ ...options,
28
+ headers: {
29
+ "Content-Type": "application/json",
30
+ ...headers,
31
+ ...options.headers
32
+ },
33
+ body: options.body ? JSON.stringify(options.body) : void 0
34
+ });
35
+ if (!response.ok) {
36
+ const errorBody = await response.text();
37
+ let errorMessage = `HTTP error! status: ${response.status}`;
38
+ try {
39
+ const errorJson = JSON.parse(errorBody);
40
+ errorMessage += ` - ${JSON.stringify(errorJson)}`;
41
+ } catch {
42
+ if (errorBody) {
43
+ errorMessage += ` - ${errorBody}`;
44
+ }
45
+ }
46
+ throw new Error(errorMessage);
47
+ }
48
+ if (options.stream) {
49
+ return response;
50
+ }
51
+ const data = await response.json();
52
+ return data;
53
+ } catch (error) {
54
+ lastError = error;
55
+ if (attempt === retries) {
56
+ break;
57
+ }
58
+ await new Promise((resolve) => setTimeout(resolve, delay));
59
+ delay = Math.min(delay * 2, maxBackoffMs);
60
+ }
61
+ }
62
+ throw lastError || new Error("Request failed");
63
+ }
64
+ };
65
+
66
+ // src/resources/agent.ts
67
+ var Agent = class extends BaseResource {
68
+ constructor(options, agentId) {
69
+ super(options);
70
+ this.agentId = agentId;
71
+ }
72
+ /**
73
+ * Retrieves details about the agent
74
+ * @returns Promise containing agent details including model and instructions
75
+ */
76
+ details() {
77
+ return this.request(`/api/agents/${this.agentId}`);
78
+ }
79
+ /**
80
+ * Generates a response from the agent
81
+ * @param params - Generation parameters including prompt
82
+ * @returns Promise containing the generated response
83
+ */
84
+ generate(params) {
85
+ const processedParams = {
86
+ ...params,
87
+ output: params.output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.output) : params.output
88
+ };
89
+ return this.request(`/api/agents/${this.agentId}/generate`, {
90
+ method: "POST",
91
+ body: processedParams
92
+ });
93
+ }
94
+ /**
95
+ * Streams a response from the agent
96
+ * @param params - Stream parameters including prompt
97
+ * @returns Promise containing the streamed response
98
+ */
99
+ stream(params) {
100
+ const processedParams = {
101
+ ...params,
102
+ output: params.output instanceof zod.ZodSchema ? zodToJsonSchema.zodToJsonSchema(params.output) : params.output
103
+ };
104
+ return this.request(`/api/agents/${this.agentId}/stream`, {
105
+ method: "POST",
106
+ body: processedParams,
107
+ stream: true
108
+ });
109
+ }
110
+ /**
111
+ * Gets details about a specific tool available to the agent
112
+ * @param toolId - ID of the tool to retrieve
113
+ * @returns Promise containing tool details
114
+ */
115
+ getTool(toolId) {
116
+ return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
117
+ }
118
+ /**
119
+ * Retrieves evaluation results for the agent
120
+ * @returns Promise containing agent evaluations
121
+ */
122
+ evals() {
123
+ return this.request(`/api/agents/${this.agentId}/evals/ci`);
124
+ }
125
+ /**
126
+ * Retrieves live evaluation results for the agent
127
+ * @returns Promise containing live agent evaluations
128
+ */
129
+ liveEvals() {
130
+ return this.request(`/api/agents/${this.agentId}/evals/live`);
131
+ }
132
+ };
133
+
134
+ // src/resources/memory-thread.ts
135
+ var MemoryThread = class extends BaseResource {
136
+ constructor(options, threadId, agentId) {
137
+ super(options);
138
+ this.threadId = threadId;
139
+ this.agentId = agentId;
140
+ }
141
+ /**
142
+ * Retrieves the memory thread details
143
+ * @returns Promise containing thread details including title and metadata
144
+ */
145
+ get() {
146
+ return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`);
147
+ }
148
+ /**
149
+ * Updates the memory thread properties
150
+ * @param params - Update parameters including title and metadata
151
+ * @returns Promise containing updated thread details
152
+ */
153
+ update(params) {
154
+ return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`, {
155
+ method: "PATCH",
156
+ body: params
157
+ });
158
+ }
159
+ /**
160
+ * Deletes the memory thread
161
+ * @returns Promise containing deletion result
162
+ */
163
+ delete() {
164
+ return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`, {
165
+ method: "DELETE"
166
+ });
167
+ }
168
+ /**
169
+ * Retrieves messages associated with the thread
170
+ * @returns Promise containing thread messages and UI messages
171
+ */
172
+ getMessages() {
173
+ return this.request(`/api/memory/threads/${this.threadId}/messages?agentId=${this.agentId}`);
174
+ }
175
+ };
176
+
177
+ // src/resources/vector.ts
178
+ var Vector = class extends BaseResource {
179
+ constructor(options, vectorName) {
180
+ super(options);
181
+ this.vectorName = vectorName;
182
+ }
183
+ /**
184
+ * Retrieves details about a specific vector index
185
+ * @param indexName - Name of the index to get details for
186
+ * @returns Promise containing vector index details
187
+ */
188
+ details(indexName) {
189
+ return this.request(`/api/vector/${this.vectorName}/indexes/${indexName}`);
190
+ }
191
+ /**
192
+ * Deletes a vector index
193
+ * @param indexName - Name of the index to delete
194
+ * @returns Promise indicating deletion success
195
+ */
196
+ delete(indexName) {
197
+ return this.request(`/api/vector/${this.vectorName}/indexes/${indexName}`, {
198
+ method: "DELETE"
199
+ });
200
+ }
201
+ /**
202
+ * Retrieves a list of all available indexes
203
+ * @returns Promise containing array of index names
204
+ */
205
+ getIndexes() {
206
+ return this.request(`/api/vector/${this.vectorName}/indexes`);
207
+ }
208
+ /**
209
+ * Creates a new vector index
210
+ * @param params - Parameters for index creation including dimension and metric
211
+ * @returns Promise indicating creation success
212
+ */
213
+ createIndex(params) {
214
+ return this.request(`/api/vector/${this.vectorName}/create-index`, {
215
+ method: "POST",
216
+ body: params
217
+ });
218
+ }
219
+ /**
220
+ * Upserts vectors into an index
221
+ * @param params - Parameters containing vectors, metadata, and optional IDs
222
+ * @returns Promise containing array of vector IDs
223
+ */
224
+ upsert(params) {
225
+ return this.request(`/api/vector/${this.vectorName}/upsert`, {
226
+ method: "POST",
227
+ body: params
228
+ });
229
+ }
230
+ /**
231
+ * Queries vectors in an index
232
+ * @param params - Query parameters including query vector and search options
233
+ * @returns Promise containing query results
234
+ */
235
+ query(params) {
236
+ return this.request(`/api/vector/${this.vectorName}/query`, {
237
+ method: "POST",
238
+ body: params
239
+ });
240
+ }
241
+ };
242
+
243
+ // src/resources/workflow.ts
244
+ var Workflow = class extends BaseResource {
245
+ constructor(options, workflowId) {
246
+ super(options);
247
+ this.workflowId = workflowId;
248
+ }
249
+ /**
250
+ * Retrieves details about the workflow
251
+ * @returns Promise containing workflow details including steps and graphs
252
+ */
253
+ details() {
254
+ return this.request(`/api/workflows/${this.workflowId}`);
255
+ }
256
+ /**
257
+ * Executes the workflow with the provided parameters
258
+ * @param params - Parameters required for workflow execution
259
+ * @returns Promise containing the workflow execution results
260
+ */
261
+ execute(params) {
262
+ return this.request(`/api/workflows/${this.workflowId}/execute`, {
263
+ method: "POST",
264
+ body: params
265
+ });
266
+ }
267
+ /**
268
+ * Resumes a suspended workflow step
269
+ * @param stepId - ID of the step to resume
270
+ * @param runId - ID of the workflow run
271
+ * @param context - Context to resume the workflow with
272
+ * @returns Promise containing the workflow resume results
273
+ */
274
+ resume({
275
+ stepId,
276
+ runId,
277
+ context
278
+ }) {
279
+ return this.request(`/api/workflows/${this.workflowId}/resume`, {
280
+ method: "POST",
281
+ body: {
282
+ stepId,
283
+ runId,
284
+ context
285
+ }
286
+ });
287
+ }
288
+ /**
289
+ * Watches workflow transitions in real-time
290
+ * @returns Promise containing the workflow watch stream
291
+ */
292
+ watch() {
293
+ return this.request(`/api/workflows/${this.workflowId}/watch`, {
294
+ stream: true
295
+ });
296
+ }
297
+ };
298
+
299
+ // src/resources/tool.ts
300
+ var Tool = class extends BaseResource {
301
+ constructor(options, toolId) {
302
+ super(options);
303
+ this.toolId = toolId;
304
+ }
305
+ /**
306
+ * Retrieves details about the tool
307
+ * @returns Promise containing tool details including description and schemas
308
+ */
309
+ details() {
310
+ return this.request(`/api/tools/${this.toolId}`);
311
+ }
312
+ /**
313
+ * Executes the tool with the provided parameters
314
+ * @param params - Parameters required for tool execution
315
+ * @returns Promise containing the tool execution results
316
+ */
317
+ execute(params) {
318
+ return this.request(`/api/tools/${this.toolId}/execute`, {
319
+ method: "POST",
320
+ body: params
321
+ });
322
+ }
323
+ };
324
+
325
+ // src/client.ts
326
+ var MastraClient = class extends BaseResource {
327
+ constructor(options) {
328
+ super(options);
329
+ }
330
+ /**
331
+ * Retrieves all available agents
332
+ * @returns Promise containing map of agent IDs to agent details
333
+ */
334
+ getAgents() {
335
+ return this.request("/api/agents");
336
+ }
337
+ /**
338
+ * Gets an agent instance by ID
339
+ * @param agentId - ID of the agent to retrieve
340
+ * @returns Agent instance
341
+ */
342
+ getAgent(agentId) {
343
+ return new Agent(this.options, agentId);
344
+ }
345
+ /**
346
+ * Retrieves memory threads for a resource
347
+ * @param params - Parameters containing the resource ID
348
+ * @returns Promise containing array of memory threads
349
+ */
350
+ getMemoryThreads(params) {
351
+ return this.request(`/api/memory/threads?resourceid=${params.resourceId}&agentId=${params.agentId}`);
352
+ }
353
+ /**
354
+ * Creates a new memory thread
355
+ * @param params - Parameters for creating the memory thread
356
+ * @returns Promise containing the created memory thread
357
+ */
358
+ createMemoryThread(params) {
359
+ return this.request(`/api/memory/threads?agentId=${params.agentId}`, { method: "POST", body: params });
360
+ }
361
+ /**
362
+ * Gets a memory thread instance by ID
363
+ * @param threadId - ID of the memory thread to retrieve
364
+ * @returns MemoryThread instance
365
+ */
366
+ getMemoryThread(threadId, agentId) {
367
+ return new MemoryThread(this.options, threadId, agentId);
368
+ }
369
+ /**
370
+ * Saves messages to memory
371
+ * @param params - Parameters containing messages to save
372
+ * @returns Promise containing the saved messages
373
+ */
374
+ saveMessageToMemory(params) {
375
+ return this.request(`/api/memory/save-messages?agentId=${params.agentId}`, {
376
+ method: "POST",
377
+ body: params
378
+ });
379
+ }
380
+ /**
381
+ * Gets the status of the memory system
382
+ * @returns Promise containing memory system status
383
+ */
384
+ getMemoryStatus(agentId) {
385
+ return this.request(`/api/memory/status?agentId=${agentId}`);
386
+ }
387
+ /**
388
+ * Retrieves all available tools
389
+ * @returns Promise containing map of tool IDs to tool details
390
+ */
391
+ getTools() {
392
+ return this.request("/api/tools");
393
+ }
394
+ /**
395
+ * Gets a tool instance by ID
396
+ * @param toolId - ID of the tool to retrieve
397
+ * @returns Tool instance
398
+ */
399
+ getTool(toolId) {
400
+ return new Tool(this.options, toolId);
401
+ }
402
+ /**
403
+ * Retrieves all available workflows
404
+ * @returns Promise containing map of workflow IDs to workflow details
405
+ */
406
+ getWorkflows() {
407
+ return this.request("/api/workflows");
408
+ }
409
+ /**
410
+ * Gets a workflow instance by ID
411
+ * @param workflowId - ID of the workflow to retrieve
412
+ * @returns Workflow instance
413
+ */
414
+ getWorkflow(workflowId) {
415
+ return new Workflow(this.options, workflowId);
416
+ }
417
+ /**
418
+ * Gets a vector instance by name
419
+ * @param vectorName - Name of the vector to retrieve
420
+ * @returns Vector instance
421
+ */
422
+ getVector(vectorName) {
423
+ return new Vector(this.options, vectorName);
424
+ }
425
+ /**
426
+ * Retrieves logs
427
+ * @param params - Parameters for filtering logs
428
+ * @returns Promise containing array of log messages
429
+ */
430
+ getLogs(params) {
431
+ return this.request(`/api/logs?transportId=${params.transportId}`);
432
+ }
433
+ /**
434
+ * Gets logs for a specific run
435
+ * @param params - Parameters containing run ID to retrieve
436
+ * @returns Promise containing array of log messages
437
+ */
438
+ getLogForRun(params) {
439
+ return this.request(`/api/logs/${params.runId}?transportId=${params.transportId}`);
440
+ }
441
+ /**
442
+ * List of all log transports
443
+ * @returns Promise containing list of log transports
444
+ */
445
+ getLogTransports() {
446
+ return this.request("/api/logs/transports");
447
+ }
448
+ /**
449
+ * List of all traces (paged)
450
+ * @param params - Parameters for filtering traces
451
+ * @returns Promise containing telemetry data
452
+ */
453
+ getTelemetry(params) {
454
+ const { name, scope, page, perPage, attribute } = params || {};
455
+ const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
456
+ ({
457
+ ..._attribute?.length ? { attribute: _attribute } : {}
458
+ });
459
+ const searchParams = new URLSearchParams();
460
+ if (name) {
461
+ searchParams.set("name", name);
462
+ }
463
+ if (scope) {
464
+ searchParams.set("scope", scope);
465
+ }
466
+ if (page) {
467
+ searchParams.set("page", String(page));
468
+ }
469
+ if (perPage) {
470
+ searchParams.set("perPage", String(perPage));
471
+ }
472
+ if (_attribute) {
473
+ if (Array.isArray(_attribute)) {
474
+ for (const attr of _attribute) {
475
+ searchParams.append("attribute", attr);
476
+ }
477
+ } else {
478
+ searchParams.set("attribute", _attribute);
479
+ }
480
+ }
481
+ if (searchParams.size) {
482
+ return this.request(`/api/telemetry?${searchParams}`);
483
+ } else {
484
+ return this.request(`/api/telemetry`);
485
+ }
486
+ }
487
+ };
488
+
489
+ exports.MastraClient = MastraClient;