@mastra/client-js 0.0.0-storage-20250225005900

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/src/client.ts ADDED
@@ -0,0 +1,205 @@
1
+ import { Agent, MemoryThread, Tool, Workflow, Vector, BaseResource } from './resources';
2
+ import type {
3
+ ClientOptions,
4
+ CreateMemoryThreadParams,
5
+ CreateMemoryThreadResponse,
6
+ GetAgentResponse,
7
+ GetLogParams,
8
+ GetLogsParams,
9
+ GetLogsResponse,
10
+ GetMemoryThreadParams,
11
+ GetMemoryThreadResponse,
12
+ GetTelemetryParams,
13
+ GetTelemetryResponse,
14
+ GetToolResponse,
15
+ GetWorkflowResponse,
16
+ RequestOptions,
17
+ SaveMessageToMemoryParams,
18
+ SaveMessageToMemoryResponse,
19
+ } from './types';
20
+
21
+ export class MastraClient extends BaseResource {
22
+ constructor(options: ClientOptions) {
23
+ super(options);
24
+ }
25
+
26
+ /**
27
+ * Retrieves all available agents
28
+ * @returns Promise containing map of agent IDs to agent details
29
+ */
30
+ public getAgents(): Promise<Record<string, GetAgentResponse>> {
31
+ return this.request('/api/agents');
32
+ }
33
+
34
+ /**
35
+ * Gets an agent instance by ID
36
+ * @param agentId - ID of the agent to retrieve
37
+ * @returns Agent instance
38
+ */
39
+ public getAgent(agentId: string) {
40
+ return new Agent(this.options, agentId);
41
+ }
42
+
43
+ /**
44
+ * Retrieves memory threads for a resource
45
+ * @param params - Parameters containing the resource ID
46
+ * @returns Promise containing array of memory threads
47
+ */
48
+ public getMemoryThreads(params: GetMemoryThreadParams): Promise<GetMemoryThreadResponse> {
49
+ return this.request(`/api/memory/threads?resourceid=${params.resourceId}&agentId=${params.agentId}`);
50
+ }
51
+
52
+ /**
53
+ * Creates a new memory thread
54
+ * @param params - Parameters for creating the memory thread
55
+ * @returns Promise containing the created memory thread
56
+ */
57
+ public createMemoryThread(params: CreateMemoryThreadParams): Promise<CreateMemoryThreadResponse> {
58
+ return this.request(`/api/memory/threads?agentId=${params.agentId}`, { method: 'POST', body: params });
59
+ }
60
+
61
+ /**
62
+ * Gets a memory thread instance by ID
63
+ * @param threadId - ID of the memory thread to retrieve
64
+ * @returns MemoryThread instance
65
+ */
66
+ public getMemoryThread(threadId: string, agentId: string) {
67
+ return new MemoryThread(this.options, threadId, agentId);
68
+ }
69
+
70
+ /**
71
+ * Saves messages to memory
72
+ * @param params - Parameters containing messages to save
73
+ * @returns Promise containing the saved messages
74
+ */
75
+ public saveMessageToMemory(params: SaveMessageToMemoryParams): Promise<SaveMessageToMemoryResponse> {
76
+ return this.request(`/api/memory/save-messages?agentId=${params.agentId}`, {
77
+ method: 'POST',
78
+ body: params,
79
+ });
80
+ }
81
+
82
+ /**
83
+ * Gets the status of the memory system
84
+ * @returns Promise containing memory system status
85
+ */
86
+ public getMemoryStatus(agentId: string): Promise<{ result: boolean }> {
87
+ return this.request(`/api/memory/status?agentId=${agentId}`);
88
+ }
89
+
90
+ /**
91
+ * Retrieves all available tools
92
+ * @returns Promise containing map of tool IDs to tool details
93
+ */
94
+ public getTools(): Promise<Record<string, GetToolResponse>> {
95
+ return this.request('/api/tools');
96
+ }
97
+
98
+ /**
99
+ * Gets a tool instance by ID
100
+ * @param toolId - ID of the tool to retrieve
101
+ * @returns Tool instance
102
+ */
103
+ public getTool(toolId: string) {
104
+ return new Tool(this.options, toolId);
105
+ }
106
+
107
+ /**
108
+ * Retrieves all available workflows
109
+ * @returns Promise containing map of workflow IDs to workflow details
110
+ */
111
+ public getWorkflows(): Promise<Record<string, GetWorkflowResponse>> {
112
+ return this.request('/api/workflows');
113
+ }
114
+
115
+ /**
116
+ * Gets a workflow instance by ID
117
+ * @param workflowId - ID of the workflow to retrieve
118
+ * @returns Workflow instance
119
+ */
120
+ public getWorkflow(workflowId: string) {
121
+ return new Workflow(this.options, workflowId);
122
+ }
123
+
124
+ /**
125
+ * Gets a vector instance by name
126
+ * @param vectorName - Name of the vector to retrieve
127
+ * @returns Vector instance
128
+ */
129
+ public getVector(vectorName: string) {
130
+ return new Vector(this.options, vectorName);
131
+ }
132
+
133
+ /**
134
+ * Retrieves logs
135
+ * @param params - Parameters for filtering logs
136
+ * @returns Promise containing array of log messages
137
+ */
138
+ public getLogs(params: GetLogsParams): Promise<GetLogsResponse> {
139
+ return this.request(`/api/logs?transportId=${params.transportId}`);
140
+ }
141
+
142
+ /**
143
+ * Gets logs for a specific run
144
+ * @param params - Parameters containing run ID to retrieve
145
+ * @returns Promise containing array of log messages
146
+ */
147
+ public getLogForRun(params: GetLogParams): Promise<GetLogsResponse> {
148
+ return this.request(`/api/logs/${params.runId}?transportId=${params.transportId}`);
149
+ }
150
+
151
+ /**
152
+ * List of all log transports
153
+ * @returns Promise containing list of log transports
154
+ */
155
+ public getLogTransports(): Promise<{ transports: string[] }> {
156
+ return this.request('/api/logs/transports');
157
+ }
158
+
159
+ /**
160
+ * List of all traces (paged)
161
+ * @param params - Parameters for filtering traces
162
+ * @returns Promise containing telemetry data
163
+ */
164
+ public getTelemetry(params?: GetTelemetryParams): Promise<GetTelemetryResponse> {
165
+ const { name, scope, page, perPage, attribute } = params || {};
166
+ const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
167
+
168
+ const queryObj = {
169
+ ...(name ? { name } : {}),
170
+ ...(scope ? { scope } : {}),
171
+ ...(page ? { page: String(page) } : {}),
172
+ ...(perPage ? { perPage: String(perPage) } : {}),
173
+ ...(_attribute?.length ? { attribute: _attribute } : {}),
174
+ } as const;
175
+
176
+ const searchParams = new URLSearchParams();
177
+ if (name) {
178
+ searchParams.set('name', name);
179
+ }
180
+ if (scope) {
181
+ searchParams.set('scope', scope);
182
+ }
183
+ if (page) {
184
+ searchParams.set('page', String(page));
185
+ }
186
+ if (perPage) {
187
+ searchParams.set('perPage', String(perPage));
188
+ }
189
+ if (_attribute) {
190
+ if (Array.isArray(_attribute)) {
191
+ for (const attr of _attribute) {
192
+ searchParams.append('attribute', attr);
193
+ }
194
+ } else {
195
+ searchParams.set('attribute', _attribute);
196
+ }
197
+ }
198
+
199
+ if (searchParams.size) {
200
+ return this.request(`/api/telemetry?${searchParams}`);
201
+ } else {
202
+ return this.request(`/api/telemetry`);
203
+ }
204
+ }
205
+ }
package/src/example.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { MastraClient } from './client';
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
+ })();