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

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 CHANGED
@@ -1,135 +1,173 @@
1
- import type { ClientOptions, CreateMemoryThreadParams, CreateMemoryThreadResponse, GetAgentResponse, GetLogParams, GetLogsParams, GetLogsResponse, GetMemoryThreadParams, GetMemoryThreadResponse, GetToolResponse, GetWorkflowResponse, RequestOptions, SaveMessageToMemoryParams, SaveMessageToMemoryResponse } from './types';
2
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';
3
20
 
4
21
  export class MastraClient extends BaseResource {
5
-
6
- constructor(options: ClientOptions) {
7
- super(options);
8
- }
9
-
10
-
11
- /**
12
- * Retrieves all available agents
13
- * @returns Promise containing map of agent IDs to agent details
14
- */
15
- public getAgents(): Promise<Record<string, GetAgentResponse>> {
16
- return this.request('/api/agents');
17
- }
18
-
19
- /**
20
- * Gets an agent instance by ID
21
- * @param agentId - ID of the agent to retrieve
22
- * @returns Agent instance
23
- */
24
- public getAgent(agentId: string) {
25
- return new Agent(this.options, agentId);
26
- }
27
-
28
- /**
29
- * Retrieves memory threads for a resource
30
- * @param params - Parameters containing the resource ID
31
- * @returns Promise containing array of memory threads
32
- */
33
- public getMemoryThreads(params: GetMemoryThreadParams): Promise<GetMemoryThreadResponse> {
34
- return this.request(`/api/memory/threads?resourceid=${params.resourceId}`);
35
- }
36
-
37
- /**
38
- * Creates a new memory thread
39
- * @param params - Parameters for creating the memory thread
40
- * @returns Promise containing the created memory thread
41
- */
42
- public createMemoryThread(params: CreateMemoryThreadParams): Promise<CreateMemoryThreadResponse> {
43
- return this.request('/api/memory/threads', { method: 'POST', body: params });
44
- }
45
-
46
- /**
47
- * Gets a memory thread instance by ID
48
- * @param threadId - ID of the memory thread to retrieve
49
- * @returns MemoryThread instance
50
- */
51
- public getMemoryThread(threadId: string) {
52
- return new MemoryThread(this.options, threadId);
53
- }
54
-
55
- /**
56
- * Saves messages to memory
57
- * @param params - Parameters containing messages to save
58
- * @returns Promise containing the saved messages
59
- */
60
- public saveMessageToMemory(params: SaveMessageToMemoryParams): Promise<SaveMessageToMemoryResponse> {
61
- return this.request('/api/memory/save-messages', {
62
- method: 'POST',
63
- body: params,
64
- });
65
- }
66
-
67
- /**
68
- * Gets the status of the memory system
69
- * @returns Promise containing memory system status
70
- */
71
- public getMemoryStatus(): Promise<{ result: boolean }> {
72
- return this.request('/api/memory/status');
73
- }
74
-
75
- /**
76
- * Retrieves all available tools
77
- * @returns Promise containing map of tool IDs to tool details
78
- */
79
- public getTools(): Promise<Record<string, GetToolResponse>> {
80
- return this.request('/api/tools');
81
- }
82
-
83
- /**
84
- * Gets a tool instance by ID
85
- * @param toolId - ID of the tool to retrieve
86
- * @returns Tool instance
87
- */
88
- public getTool(toolId: string) {
89
- return new Tool(this.options, toolId);
90
- }
91
-
92
- /**
93
- * Retrieves all available workflows
94
- * @returns Promise containing map of workflow IDs to workflow details
95
- */
96
- public getWorkflows(): Promise<Record<string, GetWorkflowResponse>> {
97
- return this.request('/api/workflows');
98
- }
99
-
100
- /**
101
- * Gets a workflow instance by ID
102
- * @param workflowId - ID of the workflow to retrieve
103
- * @returns Workflow instance
104
- */
105
- public getWorkflow(workflowId: string) {
106
- return new Workflow(this.options, workflowId);
107
- }
108
-
109
- /**
110
- * Gets a vector instance by name
111
- * @param vectorName - Name of the vector to retrieve
112
- * @returns Vector instance
113
- */
114
- public getVector(vectorName: string) {
115
- return new Vector(this.options, vectorName);
116
- }
117
-
118
- /**
119
- * Retrieves logs
120
- * @param params - Parameters for filtering logs
121
- * @returns Promise containing array of log messages
122
- */
123
- public getLogs(params: GetLogsParams): Promise<GetLogsResponse> {
124
- return this.request(`/api/logs?transportId=${params.transportId}`);
125
- }
126
-
127
- /**
128
- * Gets logs for a specific run
129
- * @param params - Parameters containing run ID to retrieve
130
- * @returns Promise containing array of log messages
131
- */
132
- public getLogForRun(params: GetLogParams): Promise<GetLogsResponse> {
133
- return this.request(`/api/logs/${params.runId}?transportId=${params.transportId}`);
134
- }
135
- }
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 traces (paged)
153
+ * @param params - Parameters for filtering traces
154
+ * @returns Promise containing telemetry data
155
+ */
156
+ public getTelemetry(params?: GetTelemetryParams): Promise<GetTelemetryResponse> {
157
+ const { name, scope, page, perPage, attribute } = params || {};
158
+ const _attribute = attribute ? Object.entries(attribute).map(([key, value]) => `${key}:${value}`) : [];
159
+
160
+ const queryObj = {
161
+ ...(name ? { name } : {}),
162
+ ...(scope ? { scope } : {}),
163
+ ...(page ? { page: String(page) } : {}),
164
+ ...(perPage ? { perPage: String(perPage) } : {}),
165
+ ...(_attribute?.length ? { attribute: _attribute } : {}),
166
+ };
167
+
168
+ const hasQueryParams = Object.keys(queryObj).length > 0;
169
+
170
+ const queryParams = hasQueryParams ? new URLSearchParams(queryObj) : '';
171
+ return this.request(`/api/telemetry?${queryParams}`);
172
+ }
173
+ }
package/src/example.ts CHANGED
@@ -1,47 +1,43 @@
1
- import { MastraClient } from "./client";
2
-
1
+ import { MastraClient } from './client';
3
2
 
4
3
  (async () => {
5
- const client = new MastraClient({
6
- baseUrl: 'http://localhost:4111',
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
+ ],
7
17
  });
8
18
 
9
- try {
10
- const agent = client.getAgent('weatherAgent');
11
- const response = await agent.stream({
12
- messages: [{
13
- role: 'user',
14
- content: 'Hello, world!',
15
- id: '1',
16
- createdAt: new Date(),
17
- threadId: '1',
18
- type: 'text',
19
- }]
20
- })
21
-
22
-
23
- const reader = response?.body?.getReader()
24
- const decoder = new TextDecoder()
25
- let buffer = ''
26
-
27
- while (true) {
28
- if (!reader) break;
29
- const { value, done } = await reader.read()
30
- if (done) break;
31
-
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
- }
43
- }
44
- } catch (error) {
45
- console.error(error);
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
+ }
46
39
  }
47
- })()
40
+ } catch (error) {
41
+ console.error(error);
42
+ }
43
+ })();