@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/index.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export * from './client';
2
- export * from './types';
2
+ export * from './types';
@@ -1,115 +1,116 @@
1
- import type {
2
- GenerateParams,
3
- GetAgentResponse,
4
- GetEvalsByAgentIdResponse,
5
- GetToolResponse,
6
- ClientOptions,
7
- StreamParams,
8
- } from '../types';
9
- import type {
10
- GenerateReturn,
11
- StreamReturn,
12
- } from '@mastra/core';
1
+ import type { GenerateReturn, StreamReturn } from '@mastra/core';
13
2
  import type { JSONSchema7 } from 'json-schema';
14
- import { ZodSchema } from "zod";
3
+ import { ZodSchema } from 'zod';
15
4
  import { zodToJsonSchema } from 'zod-to-json-schema';
5
+
6
+ import type {
7
+ GenerateParams,
8
+ GetAgentResponse,
9
+ GetEvalsByAgentIdResponse,
10
+ GetToolResponse,
11
+ ClientOptions,
12
+ StreamParams,
13
+ } from '../types';
14
+
16
15
  import { BaseResource } from './base';
17
16
 
18
17
  export class AgentTool extends BaseResource {
19
- constructor(
20
- options: ClientOptions,
21
- private agentId: string,
22
- private toolId: string
23
- ) {
24
- super(options);
25
- }
18
+ constructor(
19
+ options: ClientOptions,
20
+ private agentId: string,
21
+ private toolId: string,
22
+ ) {
23
+ super(options);
24
+ }
26
25
 
27
- /**
28
- * Executes a specific tool for an agent
29
- * @param params - Parameters required for tool execution
30
- * @returns Promise containing tool execution results
31
- */
32
- execute(params: Record<string, any>): Promise<Record<string, any>> {
33
- return this.request(`/api/agents/${this.agentId}/tools/${this.toolId}/execute`, {
34
- method: 'POST',
35
- body: params,
36
- });
37
- }
26
+ /**
27
+ * Executes a specific tool for an agent
28
+ * @param params - Parameters required for tool execution
29
+ * @returns Promise containing tool execution results
30
+ */
31
+ execute(params: { data: any }): Promise<any> {
32
+ return this.request(`/api/agents/${this.agentId}/tools/${this.toolId}/execute`, {
33
+ method: 'POST',
34
+ body: params,
35
+ });
36
+ }
38
37
  }
39
38
 
40
39
  export class Agent extends BaseResource {
41
- constructor(
42
- options: ClientOptions,
43
- private agentId: string
44
- ) {
45
- super(options);
46
- }
40
+ constructor(
41
+ options: ClientOptions,
42
+ private agentId: string,
43
+ ) {
44
+ super(options);
45
+ }
47
46
 
48
- /**
49
- * Retrieves details about the agent
50
- * @returns Promise containing agent details including model and instructions
51
- */
52
- details(): Promise<GetAgentResponse> {
53
- return this.request(`/api/agents/${this.agentId}`);
54
- }
47
+ /**
48
+ * Retrieves details about the agent
49
+ * @returns Promise containing agent details including model and instructions
50
+ */
51
+ details(): Promise<GetAgentResponse> {
52
+ return this.request(`/api/agents/${this.agentId}`);
53
+ }
55
54
 
56
- /**
57
- * Generates a response from the agent
58
- * @param params - Generation parameters including prompt
59
- * @returns Promise containing the generated response
60
- */
61
- generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: GenerateParams<T>): Promise<GenerateReturn<T>> {
62
- const processedParams = {
63
- ...params,
64
- output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output
65
- };
55
+ /**
56
+ * Generates a response from the agent
57
+ * @param params - Generation parameters including prompt
58
+ * @returns Promise containing the generated response
59
+ */
60
+ generate<T extends JSONSchema7 | ZodSchema | undefined = undefined>(
61
+ params: GenerateParams<T>,
62
+ ): Promise<GenerateReturn<T>> {
63
+ const processedParams = {
64
+ ...params,
65
+ output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
66
+ };
66
67
 
67
- return this.request(`/api/agents/${this.agentId}/generate`, {
68
- method: 'POST',
69
- body: processedParams,
70
- });
71
- }
68
+ return this.request(`/api/agents/${this.agentId}/generate`, {
69
+ method: 'POST',
70
+ body: processedParams,
71
+ });
72
+ }
72
73
 
73
- /**
74
- * Streams a response from the agent
75
- * @param params - Stream parameters including prompt
76
- * @returns Promise containing the streamed response
77
- */
78
- stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response> {
79
- const processedParams = {
80
- ...params,
81
- output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
82
- };
74
+ /**
75
+ * Streams a response from the agent
76
+ * @param params - Stream parameters including prompt
77
+ * @returns Promise containing the streamed response
78
+ */
79
+ stream<T extends JSONSchema7 | ZodSchema | undefined = undefined>(params: StreamParams<T>): Promise<Response> {
80
+ const processedParams = {
81
+ ...params,
82
+ output: params.output instanceof ZodSchema ? zodToJsonSchema(params.output) : params.output,
83
+ };
83
84
 
84
- return this.request(`/api/agents/${this.agentId}/stream`, {
85
- method: 'POST',
86
- body: processedParams,
87
- stream: true
88
- })
89
- }
85
+ return this.request(`/api/agents/${this.agentId}/stream`, {
86
+ method: 'POST',
87
+ body: processedParams,
88
+ stream: true,
89
+ });
90
+ }
90
91
 
91
- /**
92
- * Gets details about a specific tool available to the agent
93
- * @param toolId - ID of the tool to retrieve
94
- * @returns Promise containing tool details
95
- */
96
- getTool(toolId: string): Promise<GetToolResponse> {
97
- return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
98
- }
92
+ /**
93
+ * Gets details about a specific tool available to the agent
94
+ * @param toolId - ID of the tool to retrieve
95
+ * @returns Promise containing tool details
96
+ */
97
+ getTool(toolId: string): Promise<GetToolResponse> {
98
+ return this.request(`/api/agents/${this.agentId}/tools/${toolId}`);
99
+ }
99
100
 
100
- /**
101
- * Retrieves evaluation results for the agent
102
- * @returns Promise containing agent evaluations
103
- */
104
- evals(): Promise<GetEvalsByAgentIdResponse> {
105
- return this.request(`/api/agents/${this.agentId}/evals`);
106
- }
101
+ /**
102
+ * Retrieves evaluation results for the agent
103
+ * @returns Promise containing agent evaluations
104
+ */
105
+ evals(): Promise<GetEvalsByAgentIdResponse> {
106
+ return this.request(`/api/agents/${this.agentId}/evals/ci`);
107
+ }
107
108
 
108
- /**
109
- * Retrieves live evaluation results for the agent
110
- * @returns Promise containing live agent evaluations
111
- */
112
- liveEvals(): Promise<GetEvalsByAgentIdResponse> {
113
- return this.request(`/api/agents/${this.agentId}/evals/live`);
114
- }
115
- }
109
+ /**
110
+ * Retrieves live evaluation results for the agent
111
+ * @returns Promise containing live agent evaluations
112
+ */
113
+ liveEvals(): Promise<GetEvalsByAgentIdResponse> {
114
+ return this.request(`/api/agents/${this.agentId}/evals/live`);
115
+ }
116
+ }
@@ -1,68 +1,68 @@
1
1
  import type { RequestFunction, RequestOptions, ClientOptions } from '../types';
2
2
 
3
3
  export class BaseResource {
4
- readonly options: ClientOptions
4
+ readonly options: ClientOptions;
5
5
 
6
- constructor(options: ClientOptions) {
7
- this.options = options;
8
- }
9
-
10
- /**
11
- * Makes an HTTP request to the API with retries and exponential backoff
12
- * @param path - The API endpoint path
13
- * @param options - Optional request configuration
14
- * @returns Promise containing the response data
15
- */
16
- public async request<T>(path: string, options: RequestOptions = {}): Promise<T> {
17
- let lastError: Error | null = null;
18
- const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1000, headers = {} } = this.options;
6
+ constructor(options: ClientOptions) {
7
+ this.options = options;
8
+ }
19
9
 
20
- let delay = backoffMs;
10
+ /**
11
+ * Makes an HTTP request to the API with retries and exponential backoff
12
+ * @param path - The API endpoint path
13
+ * @param options - Optional request configuration
14
+ * @returns Promise containing the response data
15
+ */
16
+ public async request<T>(path: string, options: RequestOptions = {}): Promise<T> {
17
+ let lastError: Error | null = null;
18
+ const { baseUrl, retries = 3, backoffMs = 100, maxBackoffMs = 1000, headers = {} } = this.options;
21
19
 
22
- for (let attempt = 0; attempt <= retries; attempt++) {
23
- try {
24
- const response = await fetch(`${baseUrl}${path}`, {
25
- ...options,
26
- headers: {
27
- 'Content-Type': 'application/json',
28
- ...headers,
29
- ...options.headers,
30
- },
31
- body: options.body ? JSON.stringify(options.body) : undefined,
32
- });
20
+ let delay = backoffMs;
33
21
 
34
- if (!response.ok) {
35
- const errorBody = await response.text();
36
- let errorMessage = `HTTP error! status: ${response.status}`;
37
- try {
38
- const errorJson = JSON.parse(errorBody);
39
- errorMessage += ` - ${JSON.stringify(errorJson)}`;
40
- } catch {
41
- if (errorBody) {
42
- errorMessage += ` - ${errorBody}`;
43
- }
44
- }
45
- throw new Error(errorMessage);
46
- }
22
+ for (let attempt = 0; attempt <= retries; attempt++) {
23
+ try {
24
+ const response = await fetch(`${baseUrl}${path}`, {
25
+ ...options,
26
+ headers: {
27
+ 'Content-Type': 'application/json',
28
+ ...headers,
29
+ ...options.headers,
30
+ },
31
+ body: options.body ? JSON.stringify(options.body) : undefined,
32
+ });
47
33
 
48
- if (options.stream) {
49
- return response as unknown as T;
50
- }
34
+ if (!response.ok) {
35
+ const errorBody = await response.text();
36
+ let errorMessage = `HTTP error! status: ${response.status}`;
37
+ try {
38
+ const errorJson = JSON.parse(errorBody);
39
+ errorMessage += ` - ${JSON.stringify(errorJson)}`;
40
+ } catch {
41
+ if (errorBody) {
42
+ errorMessage += ` - ${errorBody}`;
43
+ }
44
+ }
45
+ throw new Error(errorMessage);
46
+ }
51
47
 
52
- const data = await response.json();
53
- return data as T;
54
- } catch (error) {
55
- lastError = error as Error;
48
+ if (options.stream) {
49
+ return response as unknown as T;
50
+ }
56
51
 
57
- if (attempt === retries) {
58
- break;
59
- }
52
+ const data = await response.json();
53
+ return data as T;
54
+ } catch (error) {
55
+ lastError = error as Error;
60
56
 
61
- await new Promise(resolve => setTimeout(resolve, delay));
62
- delay = Math.min(delay * 2, maxBackoffMs);
63
- }
57
+ if (attempt === retries) {
58
+ break;
64
59
  }
65
60
 
66
- throw lastError || new Error('Request failed');
61
+ await new Promise(resolve => setTimeout(resolve, delay));
62
+ delay = Math.min(delay * 2, maxBackoffMs);
63
+ }
67
64
  }
68
- }
65
+
66
+ throw lastError || new Error('Request failed');
67
+ }
68
+ }
@@ -3,4 +3,4 @@ export * from './memory-thread';
3
3
  export * from './vector';
4
4
  export * from './workflow';
5
5
  export * from './tool';
6
- export * from './base';
6
+ export * from './base';
@@ -1,57 +1,60 @@
1
1
  import type { StorageThreadType } from '@mastra/core';
2
+
2
3
  import type {
3
- CreateMemoryThreadParams,
4
- GetMemoryThreadMessagesResponse,
5
- GetMemoryThreadResponse,
6
- ClientOptions,
7
- SaveMessageToMemoryParams,
8
- UpdateMemoryThreadParams,
4
+ CreateMemoryThreadParams,
5
+ GetMemoryThreadMessagesResponse,
6
+ GetMemoryThreadResponse,
7
+ ClientOptions,
8
+ SaveMessageToMemoryParams,
9
+ UpdateMemoryThreadParams,
9
10
  } from '../types';
11
+
10
12
  import { BaseResource } from './base';
11
13
 
12
14
  export class MemoryThread extends BaseResource {
13
- constructor(
14
- options: ClientOptions,
15
- private threadId: string
16
- ) {
17
- super(options);
18
- }
15
+ constructor(
16
+ options: ClientOptions,
17
+ private threadId: string,
18
+ private agentId: string,
19
+ ) {
20
+ super(options);
21
+ }
19
22
 
20
- /**
21
- * Retrieves the memory thread details
22
- * @returns Promise containing thread details including title and metadata
23
- */
24
- get(): Promise<StorageThreadType> {
25
- return this.request(`/api/memory/threads/${this.threadId}`);
26
- }
23
+ /**
24
+ * Retrieves the memory thread details
25
+ * @returns Promise containing thread details including title and metadata
26
+ */
27
+ get(): Promise<StorageThreadType> {
28
+ return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`);
29
+ }
27
30
 
28
- /**
29
- * Updates the memory thread properties
30
- * @param params - Update parameters including title and metadata
31
- * @returns Promise containing updated thread details
32
- */
33
- update(params: UpdateMemoryThreadParams): Promise<StorageThreadType> {
34
- return this.request(`/api/memory/threads/${this.threadId}`, {
35
- method: 'PATCH',
36
- body: params,
37
- });
38
- }
31
+ /**
32
+ * Updates the memory thread properties
33
+ * @param params - Update parameters including title and metadata
34
+ * @returns Promise containing updated thread details
35
+ */
36
+ update(params: UpdateMemoryThreadParams): Promise<StorageThreadType> {
37
+ return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`, {
38
+ method: 'PATCH',
39
+ body: params,
40
+ });
41
+ }
39
42
 
40
- /**
41
- * Deletes the memory thread
42
- * @returns Promise containing deletion result
43
- */
44
- delete(): Promise<{ result: string }> {
45
- return this.request(`/api/memory/threads/${this.threadId}`, {
46
- method: 'DELETE',
47
- });
48
- }
43
+ /**
44
+ * Deletes the memory thread
45
+ * @returns Promise containing deletion result
46
+ */
47
+ delete(): Promise<{ result: string }> {
48
+ return this.request(`/api/memory/threads/${this.threadId}?agentId=${this.agentId}`, {
49
+ method: 'DELETE',
50
+ });
51
+ }
49
52
 
50
- /**
51
- * Retrieves messages associated with the thread
52
- * @returns Promise containing thread messages and UI messages
53
- */
54
- getMessages(): Promise<GetMemoryThreadMessagesResponse> {
55
- return this.request(`/api/memory/threads/${this.threadId}/messages`);
56
- }
57
- }
53
+ /**
54
+ * Retrieves messages associated with the thread
55
+ * @returns Promise containing thread messages and UI messages
56
+ */
57
+ getMessages(): Promise<GetMemoryThreadMessagesResponse> {
58
+ return this.request(`/api/memory/threads/${this.threadId}/messages?agentId=${this.agentId}`);
59
+ }
60
+ }
@@ -1,31 +1,32 @@
1
1
  import type { GetToolResponse, ClientOptions } from '../types';
2
+
2
3
  import { BaseResource } from './base';
3
4
 
4
5
  export class Tool extends BaseResource {
5
- constructor(
6
- options: ClientOptions,
7
- private toolId: string
8
- ) {
9
- super(options);
10
- }
6
+ constructor(
7
+ options: ClientOptions,
8
+ private toolId: string,
9
+ ) {
10
+ super(options);
11
+ }
11
12
 
12
- /**
13
- * Retrieves details about the tool
14
- * @returns Promise containing tool details including description and schemas
15
- */
16
- details(): Promise<GetToolResponse> {
17
- return this.request(`/api/tools/${this.toolId}`);
18
- }
13
+ /**
14
+ * Retrieves details about the tool
15
+ * @returns Promise containing tool details including description and schemas
16
+ */
17
+ details(): Promise<GetToolResponse> {
18
+ return this.request(`/api/tools/${this.toolId}`);
19
+ }
19
20
 
20
- /**
21
- * Executes the tool with the provided parameters
22
- * @param params - Parameters required for tool execution
23
- * @returns Promise containing the tool execution results
24
- */
25
- execute(params: Record<string, any>): Promise<Record<string, any>> {
26
- return this.request(`/api/tools/${this.toolId}/execute`, {
27
- method: 'POST',
28
- body: params,
29
- });
30
- }
21
+ /**
22
+ * Executes the tool with the provided parameters
23
+ * @param params - Parameters required for tool execution
24
+ * @returns Promise containing the tool execution results
25
+ */
26
+ execute(params: { data: any }): Promise<any> {
27
+ return this.request(`/api/tools/${this.toolId}/execute`, {
28
+ method: 'POST',
29
+ body: params,
30
+ });
31
+ }
31
32
  }