@mastra/client-js 0.1.0-alpha.8 → 0.1.0
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/.turbo/turbo-build.log +16 -0
- package/CHANGELOG.md +13 -0
- package/README.md +24 -10
- package/dist/index.d.mts +48 -9
- package/dist/index.mjs +63 -13
- package/eslint.config.js +6 -0
- package/package.json +5 -4
- package/src/client.ts +170 -132
- package/src/example.ts +38 -42
- package/src/index.test.ts +542 -530
- package/src/index.ts +1 -1
- package/src/resources/agent.ts +99 -98
- package/src/resources/base.ts +54 -54
- package/src/resources/index.ts +1 -1
- package/src/resources/memory-thread.ts +50 -47
- package/src/resources/tool.ts +25 -24
- package/src/resources/vector.ts +72 -71
- package/src/resources/workflow.ts +62 -25
- package/src/types.ts +98 -77
- package/tsconfig.json +4 -28
- package/.eslintrc.js +0 -10
- package/.prettierignore +0 -3
- package/.prettierrc.json +0 -26
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './client';
|
|
2
|
-
export * from './types';
|
|
2
|
+
export * from './types';
|
package/src/resources/agent.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
constructor(
|
|
19
|
+
options: ClientOptions,
|
|
20
|
+
private agentId: string,
|
|
21
|
+
private toolId: string,
|
|
22
|
+
) {
|
|
23
|
+
super(options);
|
|
24
|
+
}
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
constructor(
|
|
41
|
+
options: ClientOptions,
|
|
42
|
+
private agentId: string,
|
|
43
|
+
) {
|
|
44
|
+
super(options);
|
|
45
|
+
}
|
|
47
46
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
return this.request(`/api/agents/${this.agentId}/generate`, {
|
|
69
|
+
method: 'POST',
|
|
70
|
+
body: processedParams,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
72
73
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
+
}
|
package/src/resources/base.ts
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
1
|
import type { RequestFunction, RequestOptions, ClientOptions } from '../types';
|
|
2
2
|
|
|
3
3
|
export class BaseResource {
|
|
4
|
-
|
|
4
|
+
readonly options: ClientOptions;
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
49
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
lastError = error as Error;
|
|
48
|
+
if (options.stream) {
|
|
49
|
+
return response as unknown as T;
|
|
50
|
+
}
|
|
56
51
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
const data = await response.json();
|
|
53
|
+
return data as T;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
lastError = error as Error;
|
|
60
56
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
57
|
+
if (attempt === retries) {
|
|
58
|
+
break;
|
|
64
59
|
}
|
|
65
60
|
|
|
66
|
-
|
|
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
|
+
}
|
package/src/resources/index.ts
CHANGED
|
@@ -1,57 +1,60 @@
|
|
|
1
1
|
import type { StorageThreadType } from '@mastra/core';
|
|
2
|
+
|
|
2
3
|
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
constructor(
|
|
16
|
+
options: ClientOptions,
|
|
17
|
+
private threadId: string,
|
|
18
|
+
private agentId: string,
|
|
19
|
+
) {
|
|
20
|
+
super(options);
|
|
21
|
+
}
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
+
}
|
package/src/resources/tool.ts
CHANGED
|
@@ -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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
constructor(
|
|
7
|
+
options: ClientOptions,
|
|
8
|
+
private toolId: string,
|
|
9
|
+
) {
|
|
10
|
+
super(options);
|
|
11
|
+
}
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
}
|