@mastra/client-js 0.1.11 → 0.1.12-alpha.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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +19 -0
- package/dist/index.cjs +52 -2
- package/dist/index.d.cts +33 -3
- package/dist/index.d.ts +33 -3
- package/dist/index.js +52 -2
- package/package.json +2 -2
- package/src/index.test.ts +138 -21
- package/src/resources/agent.ts +58 -0
- package/src/resources/base.ts +7 -2
- package/src/types.ts +2 -2
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/client-js@0.1.
|
|
2
|
+
> @mastra/client-js@0.1.12-alpha.1 build /home/runner/work/mastra/mastra/client-sdks/client-js
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
[34mCLI[39m Cleaning output folder
|
|
10
10
|
[34mESM[39m Build start
|
|
11
11
|
[34mCJS[39m Build start
|
|
12
|
-
[32mESM[39m [1mdist/index.js [22m[
|
|
13
|
-
[32mESM[39m ⚡️ Build success in
|
|
14
|
-
[32mCJS[39m [1mdist/index.cjs [22m[
|
|
15
|
-
[32mCJS[39m ⚡️ Build success in
|
|
12
|
+
[32mESM[39m [1mdist/index.js [22m[32m21.01 KB[39m
|
|
13
|
+
[32mESM[39m ⚡️ Build success in 1132ms
|
|
14
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m21.19 KB[39m
|
|
15
|
+
[32mCJS[39m ⚡️ Build success in 1171ms
|
|
16
16
|
[34mDTS[39m Build start
|
|
17
|
-
[32mDTS[39m ⚡️ Build success in
|
|
18
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
19
|
-
[32mDTS[39m [1mdist/index.d.cts [22m[
|
|
17
|
+
[32mDTS[39m ⚡️ Build success in 10321ms
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m18.78 KB[39m
|
|
19
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m18.78 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @mastra/client-js
|
|
2
2
|
|
|
3
|
+
## 0.1.12-alpha.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [6794797]
|
|
8
|
+
- @mastra/core@0.6.4-alpha.1
|
|
9
|
+
|
|
10
|
+
## 0.1.12-alpha.0
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 05ef3e0: Support voice for mastra client
|
|
15
|
+
- 248cb07: Allow ai-sdk Message type for messages in agent generate and stream
|
|
16
|
+
Fix sidebar horizontal overflow in playground
|
|
17
|
+
- Updated dependencies [fb68a80]
|
|
18
|
+
- Updated dependencies [b56a681]
|
|
19
|
+
- Updated dependencies [248cb07]
|
|
20
|
+
- @mastra/core@0.6.4-alpha.0
|
|
21
|
+
|
|
3
22
|
## 0.1.11
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -23,14 +23,15 @@ var BaseResource = class {
|
|
|
23
23
|
let delay = backoffMs;
|
|
24
24
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
25
25
|
try {
|
|
26
|
+
const defaultHeaders = options.body instanceof FormData ? {} : { "Content-Type": "application/json" };
|
|
26
27
|
const response = await fetch(`${baseUrl}${path}`, {
|
|
27
28
|
...options,
|
|
28
29
|
headers: {
|
|
29
|
-
|
|
30
|
+
...defaultHeaders,
|
|
30
31
|
...headers,
|
|
31
32
|
...options.headers
|
|
32
33
|
},
|
|
33
|
-
body: options.body ? JSON.stringify(options.body) : void 0
|
|
34
|
+
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
34
35
|
});
|
|
35
36
|
if (!response.ok) {
|
|
36
37
|
const errorBody = await response.text();
|
|
@@ -64,11 +65,60 @@ var BaseResource = class {
|
|
|
64
65
|
};
|
|
65
66
|
|
|
66
67
|
// src/resources/agent.ts
|
|
68
|
+
var AgentVoice = class extends BaseResource {
|
|
69
|
+
constructor(options, agentId) {
|
|
70
|
+
super(options);
|
|
71
|
+
this.agentId = agentId;
|
|
72
|
+
this.agentId = agentId;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Convert text to speech using the agent's voice provider
|
|
76
|
+
* @param text - Text to convert to speech
|
|
77
|
+
* @param options - Optional provider-specific options for speech generation
|
|
78
|
+
* @returns Promise containing the audio data
|
|
79
|
+
*/
|
|
80
|
+
async speak(text, options) {
|
|
81
|
+
return this.request(`/api/agents/${this.agentId}/voice/speak`, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
"Content-Type": "application/json"
|
|
85
|
+
},
|
|
86
|
+
body: { input: text, options },
|
|
87
|
+
stream: true
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Convert speech to text using the agent's voice provider
|
|
92
|
+
* @param audio - Audio data to transcribe
|
|
93
|
+
* @param options - Optional provider-specific options
|
|
94
|
+
* @returns Promise containing the transcribed text
|
|
95
|
+
*/
|
|
96
|
+
listen(audio, options) {
|
|
97
|
+
const formData = new FormData();
|
|
98
|
+
formData.append("audio", audio);
|
|
99
|
+
if (options) {
|
|
100
|
+
formData.append("options", JSON.stringify(options));
|
|
101
|
+
}
|
|
102
|
+
return this.request(`/api/agents/${this.agentId}/voice/listen`, {
|
|
103
|
+
method: "POST",
|
|
104
|
+
body: formData
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get available speakers for the agent's voice provider
|
|
109
|
+
* @returns Promise containing list of available speakers
|
|
110
|
+
*/
|
|
111
|
+
getSpeakers() {
|
|
112
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
67
115
|
var Agent = class extends BaseResource {
|
|
68
116
|
constructor(options, agentId) {
|
|
69
117
|
super(options);
|
|
70
118
|
this.agentId = agentId;
|
|
119
|
+
this.voice = new AgentVoice(options, this.agentId);
|
|
71
120
|
}
|
|
121
|
+
voice;
|
|
72
122
|
/**
|
|
73
123
|
* Retrieves details about the agent
|
|
74
124
|
* @returns Promise containing agent details including model and instructions
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CoreMessage,
|
|
1
|
+
import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
|
|
2
2
|
import { JSONSchema7 } from 'json-schema';
|
|
3
3
|
import { ZodSchema } from 'zod';
|
|
4
4
|
import { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
|
|
@@ -30,10 +30,10 @@ interface GetAgentResponse {
|
|
|
30
30
|
modelId: string;
|
|
31
31
|
}
|
|
32
32
|
type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
33
|
-
messages: string | string[] | CoreMessage[];
|
|
33
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
34
34
|
} & Partial<AgentGenerateOptions<T>>;
|
|
35
35
|
type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
36
|
-
messages: string | string[] | CoreMessage[];
|
|
36
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
37
37
|
} & Partial<AgentStreamOptions<T>>;
|
|
38
38
|
interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
39
39
|
evals: any[];
|
|
@@ -174,8 +174,38 @@ declare class BaseResource {
|
|
|
174
174
|
request<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
declare class AgentVoice extends BaseResource {
|
|
178
|
+
private agentId;
|
|
179
|
+
constructor(options: ClientOptions, agentId: string);
|
|
180
|
+
/**
|
|
181
|
+
* Convert text to speech using the agent's voice provider
|
|
182
|
+
* @param text - Text to convert to speech
|
|
183
|
+
* @param options - Optional provider-specific options for speech generation
|
|
184
|
+
* @returns Promise containing the audio data
|
|
185
|
+
*/
|
|
186
|
+
speak(text: string, options?: {
|
|
187
|
+
speaker?: string;
|
|
188
|
+
[key: string]: any;
|
|
189
|
+
}): Promise<Response>;
|
|
190
|
+
/**
|
|
191
|
+
* Convert speech to text using the agent's voice provider
|
|
192
|
+
* @param audio - Audio data to transcribe
|
|
193
|
+
* @param options - Optional provider-specific options
|
|
194
|
+
* @returns Promise containing the transcribed text
|
|
195
|
+
*/
|
|
196
|
+
listen(audio: Blob, options?: Record<string, any>): Promise<Response>;
|
|
197
|
+
/**
|
|
198
|
+
* Get available speakers for the agent's voice provider
|
|
199
|
+
* @returns Promise containing list of available speakers
|
|
200
|
+
*/
|
|
201
|
+
getSpeakers(): Promise<Array<{
|
|
202
|
+
voiceId: string;
|
|
203
|
+
[key: string]: any;
|
|
204
|
+
}>>;
|
|
205
|
+
}
|
|
177
206
|
declare class Agent extends BaseResource {
|
|
178
207
|
private agentId;
|
|
208
|
+
readonly voice: AgentVoice;
|
|
179
209
|
constructor(options: ClientOptions, agentId: string);
|
|
180
210
|
/**
|
|
181
211
|
* Retrieves details about the agent
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CoreMessage,
|
|
1
|
+
import { CoreMessage, AiMessageType, StorageThreadType, MessageType, StepAction, StepGraph, QueryResult, BaseLogMessage, GenerateReturn } from '@mastra/core';
|
|
2
2
|
import { JSONSchema7 } from 'json-schema';
|
|
3
3
|
import { ZodSchema } from 'zod';
|
|
4
4
|
import { AgentGenerateOptions, AgentStreamOptions } from '@mastra/core/agent';
|
|
@@ -30,10 +30,10 @@ interface GetAgentResponse {
|
|
|
30
30
|
modelId: string;
|
|
31
31
|
}
|
|
32
32
|
type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
33
|
-
messages: string | string[] | CoreMessage[];
|
|
33
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
34
34
|
} & Partial<AgentGenerateOptions<T>>;
|
|
35
35
|
type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
36
|
-
messages: string | string[] | CoreMessage[];
|
|
36
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
37
37
|
} & Partial<AgentStreamOptions<T>>;
|
|
38
38
|
interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|
|
39
39
|
evals: any[];
|
|
@@ -174,8 +174,38 @@ declare class BaseResource {
|
|
|
174
174
|
request<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
declare class AgentVoice extends BaseResource {
|
|
178
|
+
private agentId;
|
|
179
|
+
constructor(options: ClientOptions, agentId: string);
|
|
180
|
+
/**
|
|
181
|
+
* Convert text to speech using the agent's voice provider
|
|
182
|
+
* @param text - Text to convert to speech
|
|
183
|
+
* @param options - Optional provider-specific options for speech generation
|
|
184
|
+
* @returns Promise containing the audio data
|
|
185
|
+
*/
|
|
186
|
+
speak(text: string, options?: {
|
|
187
|
+
speaker?: string;
|
|
188
|
+
[key: string]: any;
|
|
189
|
+
}): Promise<Response>;
|
|
190
|
+
/**
|
|
191
|
+
* Convert speech to text using the agent's voice provider
|
|
192
|
+
* @param audio - Audio data to transcribe
|
|
193
|
+
* @param options - Optional provider-specific options
|
|
194
|
+
* @returns Promise containing the transcribed text
|
|
195
|
+
*/
|
|
196
|
+
listen(audio: Blob, options?: Record<string, any>): Promise<Response>;
|
|
197
|
+
/**
|
|
198
|
+
* Get available speakers for the agent's voice provider
|
|
199
|
+
* @returns Promise containing list of available speakers
|
|
200
|
+
*/
|
|
201
|
+
getSpeakers(): Promise<Array<{
|
|
202
|
+
voiceId: string;
|
|
203
|
+
[key: string]: any;
|
|
204
|
+
}>>;
|
|
205
|
+
}
|
|
177
206
|
declare class Agent extends BaseResource {
|
|
178
207
|
private agentId;
|
|
208
|
+
readonly voice: AgentVoice;
|
|
179
209
|
constructor(options: ClientOptions, agentId: string);
|
|
180
210
|
/**
|
|
181
211
|
* Retrieves details about the agent
|
package/dist/index.js
CHANGED
|
@@ -21,14 +21,15 @@ var BaseResource = class {
|
|
|
21
21
|
let delay = backoffMs;
|
|
22
22
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
23
23
|
try {
|
|
24
|
+
const defaultHeaders = options.body instanceof FormData ? {} : { "Content-Type": "application/json" };
|
|
24
25
|
const response = await fetch(`${baseUrl}${path}`, {
|
|
25
26
|
...options,
|
|
26
27
|
headers: {
|
|
27
|
-
|
|
28
|
+
...defaultHeaders,
|
|
28
29
|
...headers,
|
|
29
30
|
...options.headers
|
|
30
31
|
},
|
|
31
|
-
body: options.body ? JSON.stringify(options.body) : void 0
|
|
32
|
+
body: options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : void 0
|
|
32
33
|
});
|
|
33
34
|
if (!response.ok) {
|
|
34
35
|
const errorBody = await response.text();
|
|
@@ -62,11 +63,60 @@ var BaseResource = class {
|
|
|
62
63
|
};
|
|
63
64
|
|
|
64
65
|
// src/resources/agent.ts
|
|
66
|
+
var AgentVoice = class extends BaseResource {
|
|
67
|
+
constructor(options, agentId) {
|
|
68
|
+
super(options);
|
|
69
|
+
this.agentId = agentId;
|
|
70
|
+
this.agentId = agentId;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Convert text to speech using the agent's voice provider
|
|
74
|
+
* @param text - Text to convert to speech
|
|
75
|
+
* @param options - Optional provider-specific options for speech generation
|
|
76
|
+
* @returns Promise containing the audio data
|
|
77
|
+
*/
|
|
78
|
+
async speak(text, options) {
|
|
79
|
+
return this.request(`/api/agents/${this.agentId}/voice/speak`, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: {
|
|
82
|
+
"Content-Type": "application/json"
|
|
83
|
+
},
|
|
84
|
+
body: { input: text, options },
|
|
85
|
+
stream: true
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Convert speech to text using the agent's voice provider
|
|
90
|
+
* @param audio - Audio data to transcribe
|
|
91
|
+
* @param options - Optional provider-specific options
|
|
92
|
+
* @returns Promise containing the transcribed text
|
|
93
|
+
*/
|
|
94
|
+
listen(audio, options) {
|
|
95
|
+
const formData = new FormData();
|
|
96
|
+
formData.append("audio", audio);
|
|
97
|
+
if (options) {
|
|
98
|
+
formData.append("options", JSON.stringify(options));
|
|
99
|
+
}
|
|
100
|
+
return this.request(`/api/agents/${this.agentId}/voice/listen`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
body: formData
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get available speakers for the agent's voice provider
|
|
107
|
+
* @returns Promise containing list of available speakers
|
|
108
|
+
*/
|
|
109
|
+
getSpeakers() {
|
|
110
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
65
113
|
var Agent = class extends BaseResource {
|
|
66
114
|
constructor(options, agentId) {
|
|
67
115
|
super(options);
|
|
68
116
|
this.agentId = agentId;
|
|
117
|
+
this.voice = new AgentVoice(options, this.agentId);
|
|
69
118
|
}
|
|
119
|
+
voice;
|
|
70
120
|
/**
|
|
71
121
|
* Retrieves details about the agent
|
|
72
122
|
* @returns Promise containing agent details including model and instructions
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/client-js",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12-alpha.1",
|
|
4
4
|
"description": "The official TypeScript library for the Mastra Client API",
|
|
5
5
|
"author": "",
|
|
6
6
|
"type": "module",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"json-schema": "^0.4.0",
|
|
26
26
|
"zod": "^3.24.2",
|
|
27
27
|
"zod-to-json-schema": "^3.24.3",
|
|
28
|
-
"@mastra/core": "^0.6.
|
|
28
|
+
"@mastra/core": "^0.6.4-alpha.1"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@babel/preset-env": "^7.26.9",
|
package/src/index.test.ts
CHANGED
|
@@ -18,27 +18,44 @@ describe('MastraClient Resources', () => {
|
|
|
18
18
|
// Helper to mock successful API responses
|
|
19
19
|
const mockFetchResponse = (data: any, options: { isStream?: boolean } = {}) => {
|
|
20
20
|
if (options.isStream) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
let contentType = 'text/event-stream';
|
|
22
|
+
let responseBody: ReadableStream;
|
|
23
|
+
|
|
24
|
+
if (data instanceof ReadableStream) {
|
|
25
|
+
responseBody = data;
|
|
26
|
+
contentType = 'audio/mp3';
|
|
27
|
+
} else {
|
|
28
|
+
responseBody = new ReadableStream({
|
|
29
|
+
start(controller) {
|
|
30
|
+
controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
|
|
31
|
+
controller.close();
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const headers = new Headers();
|
|
37
|
+
if (contentType === 'audio/mp3') {
|
|
38
|
+
headers.set('Transfer-Encoding', 'chunked');
|
|
39
|
+
}
|
|
40
|
+
headers.set('Content-Type', contentType);
|
|
41
|
+
|
|
42
|
+
(global.fetch as any).mockResolvedValueOnce(
|
|
43
|
+
new Response(responseBody, {
|
|
44
|
+
status: 200,
|
|
45
|
+
statusText: 'OK',
|
|
46
|
+
headers,
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
34
49
|
} else {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
const response = new Response(undefined, {
|
|
51
|
+
status: 200,
|
|
52
|
+
statusText: 'OK',
|
|
53
|
+
headers: new Headers({
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
}),
|
|
41
56
|
});
|
|
57
|
+
response.json = () => Promise.resolve(data);
|
|
58
|
+
(global.fetch as any).mockResolvedValueOnce(response);
|
|
42
59
|
}
|
|
43
60
|
};
|
|
44
61
|
|
|
@@ -241,7 +258,7 @@ describe('MastraClient Resources', () => {
|
|
|
241
258
|
const result = await agent.generate({
|
|
242
259
|
messages: [],
|
|
243
260
|
threadId: 'test-thread',
|
|
244
|
-
|
|
261
|
+
resourceId: 'test-resource',
|
|
245
262
|
output: {},
|
|
246
263
|
});
|
|
247
264
|
expect(result).toEqual(mockResponse);
|
|
@@ -253,7 +270,7 @@ describe('MastraClient Resources', () => {
|
|
|
253
270
|
body: JSON.stringify({
|
|
254
271
|
messages: [],
|
|
255
272
|
threadId: 'test-thread',
|
|
256
|
-
|
|
273
|
+
resourceId: 'test-resource',
|
|
257
274
|
output: {},
|
|
258
275
|
}),
|
|
259
276
|
}),
|
|
@@ -333,6 +350,106 @@ describe('MastraClient Resources', () => {
|
|
|
333
350
|
});
|
|
334
351
|
});
|
|
335
352
|
|
|
353
|
+
describe('Agent Voice Resource', () => {
|
|
354
|
+
const agentId = 'test-agent';
|
|
355
|
+
let agent: ReturnType<typeof client.getAgent>;
|
|
356
|
+
beforeEach(() => {
|
|
357
|
+
agent = client.getAgent(agentId);
|
|
358
|
+
});
|
|
359
|
+
it('should get available speakers', async () => {
|
|
360
|
+
const mockResponse = [{ voiceId: 'speaker1' }];
|
|
361
|
+
mockFetchResponse(mockResponse);
|
|
362
|
+
|
|
363
|
+
const result = await agent.voice.getSpeakers();
|
|
364
|
+
|
|
365
|
+
expect(result).toEqual(mockResponse);
|
|
366
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
367
|
+
`${clientOptions.baseUrl}/api/agents/test-agent/voice/speakers`,
|
|
368
|
+
expect.objectContaining({
|
|
369
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
370
|
+
}),
|
|
371
|
+
);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it(`should call speak without options`, async () => {
|
|
375
|
+
const mockAudioStream = new ReadableStream();
|
|
376
|
+
mockFetchResponse(mockAudioStream, { isStream: true });
|
|
377
|
+
|
|
378
|
+
const result = await agent.voice.speak('test');
|
|
379
|
+
|
|
380
|
+
expect(result).toBeInstanceOf(Response);
|
|
381
|
+
expect(result.body).toBeInstanceOf(ReadableStream);
|
|
382
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
383
|
+
`${clientOptions.baseUrl}/api/agents/test-agent/voice/speak`,
|
|
384
|
+
expect.objectContaining({
|
|
385
|
+
method: 'POST',
|
|
386
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
387
|
+
}),
|
|
388
|
+
);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it(`should call speak with options`, async () => {
|
|
392
|
+
const mockAudioStream = new ReadableStream();
|
|
393
|
+
mockFetchResponse(mockAudioStream, { isStream: true });
|
|
394
|
+
|
|
395
|
+
const result = await agent.voice.speak('test', { speaker: 'speaker1' });
|
|
396
|
+
expect(result).toBeInstanceOf(Response);
|
|
397
|
+
expect(result.body).toBeInstanceOf(ReadableStream);
|
|
398
|
+
expect(global.fetch).toHaveBeenCalledWith(
|
|
399
|
+
`${clientOptions.baseUrl}/api/agents/test-agent/voice/speak`,
|
|
400
|
+
expect.objectContaining({
|
|
401
|
+
method: 'POST',
|
|
402
|
+
headers: expect.objectContaining(clientOptions.headers),
|
|
403
|
+
}),
|
|
404
|
+
);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it(`should call listen with audio file`, async () => {
|
|
408
|
+
const transcriptionResponse = { text: 'Hello world' };
|
|
409
|
+
mockFetchResponse(transcriptionResponse);
|
|
410
|
+
|
|
411
|
+
const audioBlob = new Blob(['test audio data'], { type: 'audio/wav' });
|
|
412
|
+
|
|
413
|
+
const result = await agent.voice.listen(audioBlob, { filetype: 'wav' });
|
|
414
|
+
expect(result).toEqual(transcriptionResponse);
|
|
415
|
+
|
|
416
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
417
|
+
const [url, config] = (global.fetch as any).mock.calls[0];
|
|
418
|
+
expect(url).toBe(`${clientOptions.baseUrl}/api/agents/test-agent/voice/listen`);
|
|
419
|
+
expect(config.method).toBe('POST');
|
|
420
|
+
expect(config.headers).toMatchObject(clientOptions.headers);
|
|
421
|
+
|
|
422
|
+
const formData = config.body;
|
|
423
|
+
expect(formData).toBeInstanceOf(FormData);
|
|
424
|
+
const audioContent = formData.get('audio');
|
|
425
|
+
expect(audioContent).toBeInstanceOf(Blob);
|
|
426
|
+
expect(audioContent.type).toBe('audio/wav');
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it(`should call listen with audio blob and options`, async () => {
|
|
430
|
+
const transcriptionResponse = { text: 'Hello world' };
|
|
431
|
+
mockFetchResponse(transcriptionResponse);
|
|
432
|
+
|
|
433
|
+
const audioBlob = new Blob(['test audio data'], { type: 'audio/mp3' });
|
|
434
|
+
|
|
435
|
+
const result = await agent.voice.listen(audioBlob, { filetype: 'mp3' });
|
|
436
|
+
|
|
437
|
+
expect(result).toEqual(transcriptionResponse);
|
|
438
|
+
|
|
439
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
440
|
+
const [url, config] = (global.fetch as any).mock.calls[0];
|
|
441
|
+
expect(url).toBe(`${clientOptions.baseUrl}/api/agents/test-agent/voice/listen`);
|
|
442
|
+
expect(config.method).toBe('POST');
|
|
443
|
+
expect(config.headers).toMatchObject(clientOptions.headers);
|
|
444
|
+
|
|
445
|
+
const formData = config.body as FormData;
|
|
446
|
+
expect(formData).toBeInstanceOf(FormData);
|
|
447
|
+
const audioContent = formData.get('audio');
|
|
448
|
+
expect(audioContent).toBeInstanceOf(Blob);
|
|
449
|
+
expect(formData.get('options')).toBe(JSON.stringify({ filetype: 'mp3' }));
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
|
|
336
453
|
const agentId = 'test-agent';
|
|
337
454
|
|
|
338
455
|
describe('Memory Thread Resource', () => {
|
package/src/resources/agent.ts
CHANGED
|
@@ -36,12 +36,70 @@ export class AgentTool extends BaseResource {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
export class AgentVoice extends BaseResource {
|
|
40
|
+
constructor(
|
|
41
|
+
options: ClientOptions,
|
|
42
|
+
private agentId: string,
|
|
43
|
+
) {
|
|
44
|
+
super(options);
|
|
45
|
+
this.agentId = agentId;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Convert text to speech using the agent's voice provider
|
|
50
|
+
* @param text - Text to convert to speech
|
|
51
|
+
* @param options - Optional provider-specific options for speech generation
|
|
52
|
+
* @returns Promise containing the audio data
|
|
53
|
+
*/
|
|
54
|
+
async speak(text: string, options?: { speaker?: string; [key: string]: any }): Promise<Response> {
|
|
55
|
+
return this.request<Response>(`/api/agents/${this.agentId}/voice/speak`, {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers: {
|
|
58
|
+
'Content-Type': 'application/json',
|
|
59
|
+
},
|
|
60
|
+
body: { input: text, options },
|
|
61
|
+
stream: true,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Convert speech to text using the agent's voice provider
|
|
67
|
+
* @param audio - Audio data to transcribe
|
|
68
|
+
* @param options - Optional provider-specific options
|
|
69
|
+
* @returns Promise containing the transcribed text
|
|
70
|
+
*/
|
|
71
|
+
listen(audio: Blob, options?: Record<string, any>): Promise<Response> {
|
|
72
|
+
const formData = new FormData();
|
|
73
|
+
formData.append('audio', audio);
|
|
74
|
+
|
|
75
|
+
if (options) {
|
|
76
|
+
formData.append('options', JSON.stringify(options));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return this.request(`/api/agents/${this.agentId}/voice/listen`, {
|
|
80
|
+
method: 'POST',
|
|
81
|
+
body: formData,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get available speakers for the agent's voice provider
|
|
87
|
+
* @returns Promise containing list of available speakers
|
|
88
|
+
*/
|
|
89
|
+
getSpeakers(): Promise<Array<{ voiceId: string; [key: string]: any }>> {
|
|
90
|
+
return this.request(`/api/agents/${this.agentId}/voice/speakers`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
39
94
|
export class Agent extends BaseResource {
|
|
95
|
+
public readonly voice: AgentVoice;
|
|
96
|
+
|
|
40
97
|
constructor(
|
|
41
98
|
options: ClientOptions,
|
|
42
99
|
private agentId: string,
|
|
43
100
|
) {
|
|
44
101
|
super(options);
|
|
102
|
+
this.voice = new AgentVoice(options, this.agentId);
|
|
45
103
|
}
|
|
46
104
|
|
|
47
105
|
/**
|
package/src/resources/base.ts
CHANGED
|
@@ -21,14 +21,19 @@ export class BaseResource {
|
|
|
21
21
|
|
|
22
22
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
23
23
|
try {
|
|
24
|
+
const defaultHeaders: Record<string, string> =
|
|
25
|
+
options.body instanceof FormData
|
|
26
|
+
? {} // Let browser set correct Content-Type for FormData which is required for audio
|
|
27
|
+
: { 'Content-Type': 'application/json' };
|
|
24
28
|
const response = await fetch(`${baseUrl}${path}`, {
|
|
25
29
|
...options,
|
|
26
30
|
headers: {
|
|
27
|
-
|
|
31
|
+
...defaultHeaders,
|
|
28
32
|
...headers,
|
|
29
33
|
...options.headers,
|
|
30
34
|
},
|
|
31
|
-
body:
|
|
35
|
+
body:
|
|
36
|
+
options.body instanceof FormData ? options.body : options.body ? JSON.stringify(options.body) : undefined,
|
|
32
37
|
});
|
|
33
38
|
|
|
34
39
|
if (!response.ok) {
|
package/src/types.ts
CHANGED
|
@@ -44,11 +44,11 @@ export interface GetAgentResponse {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export type GenerateParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
47
|
-
messages: string | string[] | CoreMessage[];
|
|
47
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
48
48
|
} & Partial<AgentGenerateOptions<T>>;
|
|
49
49
|
|
|
50
50
|
export type StreamParams<T extends JSONSchema7 | ZodSchema | undefined = undefined> = {
|
|
51
|
-
messages: string | string[] | CoreMessage[];
|
|
51
|
+
messages: string | string[] | CoreMessage[] | AiMessageType[];
|
|
52
52
|
} & Partial<AgentStreamOptions<T>>;
|
|
53
53
|
|
|
54
54
|
export interface GetEvalsByAgentIdResponse extends GetAgentResponse {
|