@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/.turbo/turbo-build.log +16 -0
- package/CHANGELOG.md +70 -0
- package/LICENSE +44 -0
- package/README.md +125 -0
- package/dist/index.d.mts +405 -0
- package/dist/index.mjs +487 -0
- package/eslint.config.js +6 -0
- package/package.json +41 -0
- package/src/client.ts +205 -0
- package/src/example.ts +43 -0
- package/src/index.test.ts +597 -0
- package/src/index.ts +2 -0
- package/src/resources/agent.ts +116 -0
- package/src/resources/base.ts +68 -0
- package/src/resources/index.ts +6 -0
- package/src/resources/memory-thread.ts +60 -0
- package/src/resources/tool.ts +32 -0
- package/src/resources/vector.ts +83 -0
- package/src/resources/workflow.ts +68 -0
- package/src/types.ts +163 -0
- package/tsconfig.json +5 -0
- package/vitest.config.js +8 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { RequestFunction, RequestOptions, ClientOptions } from '../types';
|
|
2
|
+
|
|
3
|
+
export class BaseResource {
|
|
4
|
+
readonly options: ClientOptions;
|
|
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;
|
|
19
|
+
|
|
20
|
+
let delay = backoffMs;
|
|
21
|
+
|
|
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
|
+
});
|
|
33
|
+
|
|
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
|
+
}
|
|
47
|
+
|
|
48
|
+
if (options.stream) {
|
|
49
|
+
return response as unknown as T;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const data = await response.json();
|
|
53
|
+
return data as T;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
lastError = error as Error;
|
|
56
|
+
|
|
57
|
+
if (attempt === retries) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
62
|
+
delay = Math.min(delay * 2, maxBackoffMs);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw lastError || new Error('Request failed');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { StorageThreadType } from '@mastra/core';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
CreateMemoryThreadParams,
|
|
5
|
+
GetMemoryThreadMessagesResponse,
|
|
6
|
+
GetMemoryThreadResponse,
|
|
7
|
+
ClientOptions,
|
|
8
|
+
SaveMessageToMemoryParams,
|
|
9
|
+
UpdateMemoryThreadParams,
|
|
10
|
+
} from '../types';
|
|
11
|
+
|
|
12
|
+
import { BaseResource } from './base';
|
|
13
|
+
|
|
14
|
+
export class MemoryThread extends BaseResource {
|
|
15
|
+
constructor(
|
|
16
|
+
options: ClientOptions,
|
|
17
|
+
private threadId: string,
|
|
18
|
+
private agentId: string,
|
|
19
|
+
) {
|
|
20
|
+
super(options);
|
|
21
|
+
}
|
|
22
|
+
|
|
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
|
+
}
|
|
30
|
+
|
|
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
|
+
}
|
|
42
|
+
|
|
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
|
+
}
|
|
52
|
+
|
|
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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { GetToolResponse, ClientOptions } from '../types';
|
|
2
|
+
|
|
3
|
+
import { BaseResource } from './base';
|
|
4
|
+
|
|
5
|
+
export class Tool extends BaseResource {
|
|
6
|
+
constructor(
|
|
7
|
+
options: ClientOptions,
|
|
8
|
+
private toolId: string,
|
|
9
|
+
) {
|
|
10
|
+
super(options);
|
|
11
|
+
}
|
|
12
|
+
|
|
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
|
+
}
|
|
20
|
+
|
|
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
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreateIndexParams,
|
|
3
|
+
GetVectorIndexResponse,
|
|
4
|
+
QueryVectorParams,
|
|
5
|
+
QueryVectorResponse,
|
|
6
|
+
ClientOptions,
|
|
7
|
+
UpsertVectorParams,
|
|
8
|
+
} from '../types';
|
|
9
|
+
|
|
10
|
+
import { BaseResource } from './base';
|
|
11
|
+
|
|
12
|
+
export class Vector extends BaseResource {
|
|
13
|
+
constructor(
|
|
14
|
+
options: ClientOptions,
|
|
15
|
+
private vectorName: string,
|
|
16
|
+
) {
|
|
17
|
+
super(options);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves details about a specific vector index
|
|
22
|
+
* @param indexName - Name of the index to get details for
|
|
23
|
+
* @returns Promise containing vector index details
|
|
24
|
+
*/
|
|
25
|
+
details(indexName: string): Promise<GetVectorIndexResponse> {
|
|
26
|
+
return this.request(`/api/vector/${this.vectorName}/indexes/${indexName}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Deletes a vector index
|
|
31
|
+
* @param indexName - Name of the index to delete
|
|
32
|
+
* @returns Promise indicating deletion success
|
|
33
|
+
*/
|
|
34
|
+
delete(indexName: string): Promise<{ success: boolean }> {
|
|
35
|
+
return this.request(`/api/vector/${this.vectorName}/indexes/${indexName}`, {
|
|
36
|
+
method: 'DELETE',
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves a list of all available indexes
|
|
42
|
+
* @returns Promise containing array of index names
|
|
43
|
+
*/
|
|
44
|
+
getIndexes(): Promise<{ indexes: string[] }> {
|
|
45
|
+
return this.request(`/api/vector/${this.vectorName}/indexes`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new vector index
|
|
50
|
+
* @param params - Parameters for index creation including dimension and metric
|
|
51
|
+
* @returns Promise indicating creation success
|
|
52
|
+
*/
|
|
53
|
+
createIndex(params: CreateIndexParams): Promise<{ success: boolean }> {
|
|
54
|
+
return this.request(`/api/vector/${this.vectorName}/create-index`, {
|
|
55
|
+
method: 'POST',
|
|
56
|
+
body: params,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Upserts vectors into an index
|
|
62
|
+
* @param params - Parameters containing vectors, metadata, and optional IDs
|
|
63
|
+
* @returns Promise containing array of vector IDs
|
|
64
|
+
*/
|
|
65
|
+
upsert(params: UpsertVectorParams): Promise<string[]> {
|
|
66
|
+
return this.request(`/api/vector/${this.vectorName}/upsert`, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
body: params,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Queries vectors in an index
|
|
74
|
+
* @param params - Query parameters including query vector and search options
|
|
75
|
+
* @returns Promise containing query results
|
|
76
|
+
*/
|
|
77
|
+
query(params: QueryVectorParams): Promise<QueryVectorResponse> {
|
|
78
|
+
return this.request(`/api/vector/${this.vectorName}/query`, {
|
|
79
|
+
method: 'POST',
|
|
80
|
+
body: params,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { GetWorkflowResponse, ClientOptions } from '../types';
|
|
2
|
+
|
|
3
|
+
import { BaseResource } from './base';
|
|
4
|
+
|
|
5
|
+
export class Workflow extends BaseResource {
|
|
6
|
+
constructor(
|
|
7
|
+
options: ClientOptions,
|
|
8
|
+
private workflowId: string,
|
|
9
|
+
) {
|
|
10
|
+
super(options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Retrieves details about the workflow
|
|
15
|
+
* @returns Promise containing workflow details including steps and graphs
|
|
16
|
+
*/
|
|
17
|
+
details(): Promise<GetWorkflowResponse> {
|
|
18
|
+
return this.request(`/api/workflows/${this.workflowId}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Executes the workflow with the provided parameters
|
|
23
|
+
* @param params - Parameters required for workflow execution
|
|
24
|
+
* @returns Promise containing the workflow execution results
|
|
25
|
+
*/
|
|
26
|
+
execute(params: Record<string, any>): Promise<Record<string, any>> {
|
|
27
|
+
return this.request(`/api/workflows/${this.workflowId}/execute`, {
|
|
28
|
+
method: 'POST',
|
|
29
|
+
body: params,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resumes a suspended workflow step
|
|
35
|
+
* @param stepId - ID of the step to resume
|
|
36
|
+
* @param runId - ID of the workflow run
|
|
37
|
+
* @param context - Context to resume the workflow with
|
|
38
|
+
* @returns Promise containing the workflow resume results
|
|
39
|
+
*/
|
|
40
|
+
resume({
|
|
41
|
+
stepId,
|
|
42
|
+
runId,
|
|
43
|
+
context,
|
|
44
|
+
}: {
|
|
45
|
+
stepId: string;
|
|
46
|
+
runId: string;
|
|
47
|
+
context: Record<string, any>;
|
|
48
|
+
}): Promise<Record<string, any>> {
|
|
49
|
+
return this.request(`/api/workflows/${this.workflowId}/resume`, {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
body: {
|
|
52
|
+
stepId,
|
|
53
|
+
runId,
|
|
54
|
+
context,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Watches workflow transitions in real-time
|
|
61
|
+
* @returns Promise containing the workflow watch stream
|
|
62
|
+
*/
|
|
63
|
+
watch(): Promise<Response> {
|
|
64
|
+
return this.request(`/api/workflows/${this.workflowId}/watch`, {
|
|
65
|
+
stream: true,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
MessageType,
|
|
3
|
+
AiMessageType,
|
|
4
|
+
CoreMessage,
|
|
5
|
+
QueryResult,
|
|
6
|
+
StepAction,
|
|
7
|
+
StepGraph,
|
|
8
|
+
StorageThreadType,
|
|
9
|
+
BaseLogMessage,
|
|
10
|
+
OutputType,
|
|
11
|
+
} from '@mastra/core';
|
|
12
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
13
|
+
import { ZodSchema } from 'zod';
|
|
14
|
+
|
|
15
|
+
export interface ClientOptions {
|
|
16
|
+
/** Base URL for API requests */
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
/** Number of retry attempts for failed requests */
|
|
19
|
+
retries?: number;
|
|
20
|
+
/** Initial backoff time in milliseconds between retries */
|
|
21
|
+
backoffMs?: number;
|
|
22
|
+
/** Maximum backoff time in milliseconds between retries */
|
|
23
|
+
maxBackoffMs?: number;
|
|
24
|
+
/** Custom headers to include with requests */
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RequestOptions {
|
|
29
|
+
method?: string;
|
|
30
|
+
headers?: Record<string, string>;
|
|
31
|
+
body?: any;
|
|
32
|
+
stream?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface GetAgentResponse {
|
|
36
|
+
name: string;
|
|
37
|
+
instructions: string;
|
|
38
|
+
tools: Record<string, GetToolResponse>;
|
|
39
|
+
provider: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> {
|
|
43
|
+
messages: string | string[] | CoreMessage[];
|
|
44
|
+
threadId?: string;
|
|
45
|
+
resourceid?: string;
|
|
46
|
+
output?: OutputType | T;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> {
|
|
50
|
+
messages: string | string[] | CoreMessage[];
|
|
51
|
+
threadId?: string;
|
|
52
|
+
resourceid?: string;
|
|
53
|
+
output?: OutputType | T;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
57
|
+
evals: any[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface GetToolResponse {
|
|
61
|
+
id: string;
|
|
62
|
+
description: string;
|
|
63
|
+
inputSchema: string;
|
|
64
|
+
outputSchema: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface GetWorkflowResponse {
|
|
68
|
+
name: string;
|
|
69
|
+
triggerSchema: string;
|
|
70
|
+
steps: Record<string, StepAction<any, any, any, any>>;
|
|
71
|
+
stepGraph: StepGraph;
|
|
72
|
+
stepSubscriberGraph: Record<string, StepGraph>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface UpsertVectorParams {
|
|
76
|
+
indexName: string;
|
|
77
|
+
vectors: number[][];
|
|
78
|
+
metadata?: Record<string, any>[];
|
|
79
|
+
ids?: string[];
|
|
80
|
+
}
|
|
81
|
+
export interface CreateIndexParams {
|
|
82
|
+
indexName: string;
|
|
83
|
+
dimension: number;
|
|
84
|
+
metric?: 'cosine' | 'euclidean' | 'dotproduct';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface QueryVectorParams {
|
|
88
|
+
indexName: string;
|
|
89
|
+
queryVector: number[];
|
|
90
|
+
topK?: number;
|
|
91
|
+
filter?: Record<string, any>;
|
|
92
|
+
includeVector?: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface QueryVectorResponse {
|
|
96
|
+
results: QueryResult[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface GetVectorIndexResponse {
|
|
100
|
+
dimension: number;
|
|
101
|
+
metric: 'cosine' | 'euclidean' | 'dotproduct';
|
|
102
|
+
count: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface SaveMessageToMemoryParams {
|
|
106
|
+
messages: MessageType[];
|
|
107
|
+
agentId: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type SaveMessageToMemoryResponse = MessageType[];
|
|
111
|
+
|
|
112
|
+
export interface CreateMemoryThreadParams {
|
|
113
|
+
title: string;
|
|
114
|
+
metadata: Record<string, any>;
|
|
115
|
+
resourceid: string;
|
|
116
|
+
threadId: string;
|
|
117
|
+
agentId: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type CreateMemoryThreadResponse = StorageThreadType;
|
|
121
|
+
|
|
122
|
+
export interface GetMemoryThreadParams {
|
|
123
|
+
resourceId: string;
|
|
124
|
+
agentId: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export type GetMemoryThreadResponse = StorageThreadType[];
|
|
128
|
+
|
|
129
|
+
export interface UpdateMemoryThreadParams {
|
|
130
|
+
title: string;
|
|
131
|
+
metadata: Record<string, any>;
|
|
132
|
+
resourceid: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface GetMemoryThreadMessagesResponse {
|
|
136
|
+
messages: CoreMessage[];
|
|
137
|
+
uiMessages: AiMessageType[];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface GetLogsParams {
|
|
141
|
+
transportId: string;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface GetLogParams {
|
|
145
|
+
runId: string;
|
|
146
|
+
transportId: string;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export type GetLogsResponse = BaseLogMessage[];
|
|
150
|
+
|
|
151
|
+
export type RequestFunction = (path: string, options?: RequestOptions) => Promise<any>;
|
|
152
|
+
|
|
153
|
+
export interface GetTelemetryResponse {
|
|
154
|
+
traces: any[];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface GetTelemetryParams {
|
|
158
|
+
name?: string;
|
|
159
|
+
scope?: string;
|
|
160
|
+
page?: number;
|
|
161
|
+
perPage?: number;
|
|
162
|
+
attribute?: Record<string, string>;
|
|
163
|
+
}
|
package/tsconfig.json
ADDED