@mastra/server 0.0.0-a2a-20250421213654
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/LICENSE.md +46 -0
- package/README.md +159 -0
- package/dist/_tsup-dts-rollup.d.cts +897 -0
- package/dist/_tsup-dts-rollup.d.ts +897 -0
- package/dist/chunk-3KWAWPRU.cjs +127 -0
- package/dist/chunk-4VNS5WPM.js +37 -0
- package/dist/chunk-57CJTIPW.cjs +18 -0
- package/dist/chunk-5SWCVTNL.cjs +5579 -0
- package/dist/chunk-64U3UDTH.cjs +13 -0
- package/dist/chunk-6QEBCWQG.js +136 -0
- package/dist/chunk-75KYFUMD.cjs +165 -0
- package/dist/chunk-B2TWGIVC.cjs +101 -0
- package/dist/chunk-CVPOREIE.cjs +42 -0
- package/dist/chunk-CZOSYERC.js +312 -0
- package/dist/chunk-FFRES7WX.js +157 -0
- package/dist/chunk-H5PTF3Y4.js +16 -0
- package/dist/chunk-IDPBXKRV.js +121 -0
- package/dist/chunk-J2MCTUD4.js +121 -0
- package/dist/chunk-JQCFFVSK.cjs +127 -0
- package/dist/chunk-LGJ46RCS.js +96 -0
- package/dist/chunk-LTP7PHVE.cjs +321 -0
- package/dist/chunk-M5ABIP7D.js +11 -0
- package/dist/chunk-NJF275KR.js +49 -0
- package/dist/chunk-NYN7KFXL.js +34 -0
- package/dist/chunk-O7X557PN.cjs +140 -0
- package/dist/chunk-OCWPVYNI.cjs +36 -0
- package/dist/chunk-OMN3UI6X.js +5576 -0
- package/dist/chunk-P7UZNROR.js +306 -0
- package/dist/chunk-PJHJORJL.js +204 -0
- package/dist/chunk-RXH2S65Z.cjs +54 -0
- package/dist/chunk-UVVUDJBW.cjs +214 -0
- package/dist/chunk-Y7WN2UYV.cjs +318 -0
- package/dist/chunk-YB2FGKQ5.js +121 -0
- package/dist/chunk-ZCRMOXNW.cjs +129 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/magic-string.es-5UDOWOAZ.js +1294 -0
- package/dist/magic-string.es-IL2775P6.cjs +1300 -0
- package/dist/server/handlers/a2a.cjs +34 -0
- package/dist/server/handlers/a2a.d.cts +7 -0
- package/dist/server/handlers/a2a.d.ts +7 -0
- package/dist/server/handlers/a2a.js +1 -0
- package/dist/server/handlers/agents.cjs +30 -0
- package/dist/server/handlers/agents.d.cts +6 -0
- package/dist/server/handlers/agents.d.ts +6 -0
- package/dist/server/handlers/agents.js +1 -0
- package/dist/server/handlers/error.cjs +10 -0
- package/dist/server/handlers/error.d.cts +1 -0
- package/dist/server/handlers/error.d.ts +1 -0
- package/dist/server/handlers/error.js +1 -0
- package/dist/server/handlers/logs.cjs +18 -0
- package/dist/server/handlers/logs.d.cts +3 -0
- package/dist/server/handlers/logs.d.ts +3 -0
- package/dist/server/handlers/logs.js +1 -0
- package/dist/server/handlers/memory.cjs +38 -0
- package/dist/server/handlers/memory.d.cts +8 -0
- package/dist/server/handlers/memory.d.ts +8 -0
- package/dist/server/handlers/memory.js +1 -0
- package/dist/server/handlers/network.cjs +22 -0
- package/dist/server/handlers/network.d.cts +4 -0
- package/dist/server/handlers/network.d.ts +4 -0
- package/dist/server/handlers/network.js +1 -0
- package/dist/server/handlers/telemetry.cjs +14 -0
- package/dist/server/handlers/telemetry.d.cts +2 -0
- package/dist/server/handlers/telemetry.d.ts +2 -0
- package/dist/server/handlers/telemetry.js +1 -0
- package/dist/server/handlers/test-utils.cjs +16209 -0
- package/dist/server/handlers/test-utils.d.cts +1 -0
- package/dist/server/handlers/test-utils.d.ts +1 -0
- package/dist/server/handlers/test-utils.js +16207 -0
- package/dist/server/handlers/tools.cjs +22 -0
- package/dist/server/handlers/tools.d.cts +4 -0
- package/dist/server/handlers/tools.d.ts +4 -0
- package/dist/server/handlers/tools.js +1 -0
- package/dist/server/handlers/utils.cjs +10 -0
- package/dist/server/handlers/utils.d.cts +1 -0
- package/dist/server/handlers/utils.d.ts +1 -0
- package/dist/server/handlers/utils.js +1 -0
- package/dist/server/handlers/vector.cjs +30 -0
- package/dist/server/handlers/vector.d.cts +6 -0
- package/dist/server/handlers/vector.d.ts +6 -0
- package/dist/server/handlers/vector.js +1 -0
- package/dist/server/handlers/voice.cjs +18 -0
- package/dist/server/handlers/voice.d.cts +3 -0
- package/dist/server/handlers/voice.d.ts +3 -0
- package/dist/server/handlers/voice.js +1 -0
- package/dist/server/handlers/workflows.cjs +46 -0
- package/dist/server/handlers/workflows.d.cts +10 -0
- package/dist/server/handlers/workflows.d.ts +10 -0
- package/dist/server/handlers/workflows.js +1 -0
- package/dist/server/handlers.cjs +55 -0
- package/dist/server/handlers.d.cts +10 -0
- package/dist/server/handlers.d.ts +10 -0
- package/dist/server/handlers.js +10 -0
- package/package.json +71 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { validateBody } from './chunk-H5PTF3Y4.js';
|
|
2
|
+
import { handleError } from './chunk-M5ABIP7D.js';
|
|
3
|
+
import { HTTPException } from './chunk-NYN7KFXL.js';
|
|
4
|
+
import { __export } from './chunk-4VNS5WPM.js';
|
|
5
|
+
|
|
6
|
+
// src/server/handlers/network.ts
|
|
7
|
+
var network_exports = {};
|
|
8
|
+
__export(network_exports, {
|
|
9
|
+
generateHandler: () => generateHandler,
|
|
10
|
+
getNetworkByIdHandler: () => getNetworkByIdHandler,
|
|
11
|
+
getNetworksHandler: () => getNetworksHandler,
|
|
12
|
+
streamGenerateHandler: () => streamGenerateHandler
|
|
13
|
+
});
|
|
14
|
+
async function getNetworksHandler({ mastra }) {
|
|
15
|
+
try {
|
|
16
|
+
const networks = mastra.getNetworks();
|
|
17
|
+
const serializedNetworks = networks.map((network) => {
|
|
18
|
+
const routingAgent = network.getRoutingAgent();
|
|
19
|
+
const agents = network.getAgents();
|
|
20
|
+
return {
|
|
21
|
+
id: network.formatAgentId(routingAgent.name),
|
|
22
|
+
name: routingAgent.name,
|
|
23
|
+
instructions: routingAgent.instructions,
|
|
24
|
+
agents: agents.map((agent) => ({
|
|
25
|
+
name: agent.name,
|
|
26
|
+
provider: agent.llm?.getProvider(),
|
|
27
|
+
modelId: agent.llm?.getModelId()
|
|
28
|
+
})),
|
|
29
|
+
routingModel: {
|
|
30
|
+
provider: routingAgent.llm?.getProvider(),
|
|
31
|
+
modelId: routingAgent.llm?.getModelId()
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
return serializedNetworks;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return handleError(error, "Error getting networks");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function getNetworkByIdHandler({ mastra, networkId }) {
|
|
41
|
+
try {
|
|
42
|
+
const networks = mastra.getNetworks();
|
|
43
|
+
const network = networks.find((network2) => {
|
|
44
|
+
const routingAgent2 = network2.getRoutingAgent();
|
|
45
|
+
return network2.formatAgentId(routingAgent2.name) === networkId;
|
|
46
|
+
});
|
|
47
|
+
if (!network) {
|
|
48
|
+
throw new HTTPException(404, { message: "Network not found" });
|
|
49
|
+
}
|
|
50
|
+
const routingAgent = network.getRoutingAgent();
|
|
51
|
+
const agents = network.getAgents();
|
|
52
|
+
const serializedNetwork = {
|
|
53
|
+
id: network.formatAgentId(routingAgent.name),
|
|
54
|
+
name: routingAgent.name,
|
|
55
|
+
instructions: routingAgent.instructions,
|
|
56
|
+
agents: agents.map((agent) => ({
|
|
57
|
+
name: agent.name,
|
|
58
|
+
provider: agent.llm?.getProvider(),
|
|
59
|
+
modelId: agent.llm?.getModelId()
|
|
60
|
+
})),
|
|
61
|
+
routingModel: {
|
|
62
|
+
provider: routingAgent.llm?.getProvider(),
|
|
63
|
+
modelId: routingAgent.llm?.getModelId()
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return serializedNetwork;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
return handleError(error, "Error getting network by ID");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function generateHandler({
|
|
72
|
+
mastra,
|
|
73
|
+
container,
|
|
74
|
+
networkId,
|
|
75
|
+
body
|
|
76
|
+
}) {
|
|
77
|
+
try {
|
|
78
|
+
const network = mastra.getNetwork(networkId);
|
|
79
|
+
if (!network) {
|
|
80
|
+
throw new HTTPException(404, { message: "Network not found" });
|
|
81
|
+
}
|
|
82
|
+
validateBody({ messages: body.messages });
|
|
83
|
+
const { messages, ...rest } = body;
|
|
84
|
+
const result = await network.generate(messages, { ...rest, container });
|
|
85
|
+
return result;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return handleError(error, "Error generating from network");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function streamGenerateHandler({
|
|
91
|
+
mastra,
|
|
92
|
+
networkId,
|
|
93
|
+
body,
|
|
94
|
+
container
|
|
95
|
+
}) {
|
|
96
|
+
try {
|
|
97
|
+
const network = mastra.getNetwork(networkId);
|
|
98
|
+
if (!network) {
|
|
99
|
+
throw new HTTPException(404, { message: "Network not found" });
|
|
100
|
+
}
|
|
101
|
+
validateBody({ messages: body.messages });
|
|
102
|
+
const { messages, output, ...rest } = body;
|
|
103
|
+
const streamResult = await network.stream(messages, {
|
|
104
|
+
output,
|
|
105
|
+
...rest,
|
|
106
|
+
container
|
|
107
|
+
});
|
|
108
|
+
const streamResponse = output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
|
|
109
|
+
sendUsage: true,
|
|
110
|
+
sendReasoning: true,
|
|
111
|
+
getErrorMessage: (error) => {
|
|
112
|
+
return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
return streamResponse;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
return handleError(error, "Error streaming from network");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { generateHandler, getNetworkByIdHandler, getNetworksHandler, network_exports, streamGenerateHandler };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk57CJTIPW_cjs = require('./chunk-57CJTIPW.cjs');
|
|
4
|
+
var chunk64U3UDTH_cjs = require('./chunk-64U3UDTH.cjs');
|
|
5
|
+
var chunkOCWPVYNI_cjs = require('./chunk-OCWPVYNI.cjs');
|
|
6
|
+
var chunkCVPOREIE_cjs = require('./chunk-CVPOREIE.cjs');
|
|
7
|
+
|
|
8
|
+
// src/server/handlers/network.ts
|
|
9
|
+
var network_exports = {};
|
|
10
|
+
chunkCVPOREIE_cjs.__export(network_exports, {
|
|
11
|
+
generateHandler: () => generateHandler,
|
|
12
|
+
getNetworkByIdHandler: () => getNetworkByIdHandler,
|
|
13
|
+
getNetworksHandler: () => getNetworksHandler,
|
|
14
|
+
streamGenerateHandler: () => streamGenerateHandler
|
|
15
|
+
});
|
|
16
|
+
async function getNetworksHandler({ mastra }) {
|
|
17
|
+
try {
|
|
18
|
+
const networks = mastra.getNetworks();
|
|
19
|
+
const serializedNetworks = networks.map((network) => {
|
|
20
|
+
const routingAgent = network.getRoutingAgent();
|
|
21
|
+
const agents = network.getAgents();
|
|
22
|
+
return {
|
|
23
|
+
id: network.formatAgentId(routingAgent.name),
|
|
24
|
+
name: routingAgent.name,
|
|
25
|
+
instructions: routingAgent.instructions,
|
|
26
|
+
agents: agents.map((agent) => ({
|
|
27
|
+
name: agent.name,
|
|
28
|
+
provider: agent.llm?.getProvider(),
|
|
29
|
+
modelId: agent.llm?.getModelId()
|
|
30
|
+
})),
|
|
31
|
+
routingModel: {
|
|
32
|
+
provider: routingAgent.llm?.getProvider(),
|
|
33
|
+
modelId: routingAgent.llm?.getModelId()
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
return serializedNetworks;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error getting networks");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function getNetworkByIdHandler({ mastra, networkId }) {
|
|
43
|
+
try {
|
|
44
|
+
const networks = mastra.getNetworks();
|
|
45
|
+
const network = networks.find((network2) => {
|
|
46
|
+
const routingAgent2 = network2.getRoutingAgent();
|
|
47
|
+
return network2.formatAgentId(routingAgent2.name) === networkId;
|
|
48
|
+
});
|
|
49
|
+
if (!network) {
|
|
50
|
+
throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Network not found" });
|
|
51
|
+
}
|
|
52
|
+
const routingAgent = network.getRoutingAgent();
|
|
53
|
+
const agents = network.getAgents();
|
|
54
|
+
const serializedNetwork = {
|
|
55
|
+
id: network.formatAgentId(routingAgent.name),
|
|
56
|
+
name: routingAgent.name,
|
|
57
|
+
instructions: routingAgent.instructions,
|
|
58
|
+
agents: agents.map((agent) => ({
|
|
59
|
+
name: agent.name,
|
|
60
|
+
provider: agent.llm?.getProvider(),
|
|
61
|
+
modelId: agent.llm?.getModelId()
|
|
62
|
+
})),
|
|
63
|
+
routingModel: {
|
|
64
|
+
provider: routingAgent.llm?.getProvider(),
|
|
65
|
+
modelId: routingAgent.llm?.getModelId()
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return serializedNetwork;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error getting network by ID");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function generateHandler({
|
|
74
|
+
mastra,
|
|
75
|
+
container,
|
|
76
|
+
networkId,
|
|
77
|
+
body
|
|
78
|
+
}) {
|
|
79
|
+
try {
|
|
80
|
+
const network = mastra.getNetwork(networkId);
|
|
81
|
+
if (!network) {
|
|
82
|
+
throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Network not found" });
|
|
83
|
+
}
|
|
84
|
+
chunk57CJTIPW_cjs.validateBody({ messages: body.messages });
|
|
85
|
+
const { messages, ...rest } = body;
|
|
86
|
+
const result = await network.generate(messages, { ...rest, container });
|
|
87
|
+
return result;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error generating from network");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function streamGenerateHandler({
|
|
93
|
+
mastra,
|
|
94
|
+
networkId,
|
|
95
|
+
body,
|
|
96
|
+
container
|
|
97
|
+
}) {
|
|
98
|
+
try {
|
|
99
|
+
const network = mastra.getNetwork(networkId);
|
|
100
|
+
if (!network) {
|
|
101
|
+
throw new chunkOCWPVYNI_cjs.HTTPException(404, { message: "Network not found" });
|
|
102
|
+
}
|
|
103
|
+
chunk57CJTIPW_cjs.validateBody({ messages: body.messages });
|
|
104
|
+
const { messages, output, ...rest } = body;
|
|
105
|
+
const streamResult = await network.stream(messages, {
|
|
106
|
+
output,
|
|
107
|
+
...rest,
|
|
108
|
+
container
|
|
109
|
+
});
|
|
110
|
+
const streamResponse = output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
|
|
111
|
+
sendUsage: true,
|
|
112
|
+
sendReasoning: true,
|
|
113
|
+
getErrorMessage: (error) => {
|
|
114
|
+
return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return streamResponse;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return chunk64U3UDTH_cjs.handleError(error, "Error streaming from network");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
exports.generateHandler = generateHandler;
|
|
124
|
+
exports.getNetworkByIdHandler = getNetworkByIdHandler;
|
|
125
|
+
exports.getNetworksHandler = getNetworksHandler;
|
|
126
|
+
exports.network_exports = network_exports;
|
|
127
|
+
exports.streamGenerateHandler = streamGenerateHandler;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { validateBody } from './chunk-H5PTF3Y4.js';
|
|
2
|
+
import { handleError } from './chunk-M5ABIP7D.js';
|
|
3
|
+
import { HTTPException } from './chunk-NYN7KFXL.js';
|
|
4
|
+
import { __export } from './chunk-4VNS5WPM.js';
|
|
5
|
+
import { Readable } from 'stream';
|
|
6
|
+
|
|
7
|
+
// src/server/handlers/voice.ts
|
|
8
|
+
var voice_exports = {};
|
|
9
|
+
__export(voice_exports, {
|
|
10
|
+
generateSpeechHandler: () => generateSpeechHandler,
|
|
11
|
+
getSpeakersHandler: () => getSpeakersHandler,
|
|
12
|
+
transcribeSpeechHandler: () => transcribeSpeechHandler
|
|
13
|
+
});
|
|
14
|
+
async function getSpeakersHandler({ mastra, agentId }) {
|
|
15
|
+
try {
|
|
16
|
+
if (!agentId) {
|
|
17
|
+
throw new HTTPException(400, { message: "Agent ID is required" });
|
|
18
|
+
}
|
|
19
|
+
const agent = mastra.getAgent(agentId);
|
|
20
|
+
if (!agent) {
|
|
21
|
+
throw new HTTPException(404, { message: "Agent not found" });
|
|
22
|
+
}
|
|
23
|
+
if (!agent.voice) {
|
|
24
|
+
throw new HTTPException(400, { message: "Agent does not have voice capabilities" });
|
|
25
|
+
}
|
|
26
|
+
const speakers = await agent.voice.getSpeakers();
|
|
27
|
+
return speakers;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return handleError(error, "Error getting speakers");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function generateSpeechHandler({
|
|
33
|
+
mastra,
|
|
34
|
+
agentId,
|
|
35
|
+
body
|
|
36
|
+
}) {
|
|
37
|
+
try {
|
|
38
|
+
if (!agentId) {
|
|
39
|
+
throw new HTTPException(400, { message: "Agent ID is required" });
|
|
40
|
+
}
|
|
41
|
+
validateBody({
|
|
42
|
+
text: void 0,
|
|
43
|
+
speakerId: void 0,
|
|
44
|
+
...body
|
|
45
|
+
});
|
|
46
|
+
const agent = mastra.getAgent(agentId);
|
|
47
|
+
if (!agent) {
|
|
48
|
+
throw new HTTPException(404, { message: "Agent not found" });
|
|
49
|
+
}
|
|
50
|
+
if (!agent.voice) {
|
|
51
|
+
throw new HTTPException(400, { message: "Agent does not have voice capabilities" });
|
|
52
|
+
}
|
|
53
|
+
const audioStream = await agent.voice.speak(body.text, { speaker: body.speakerId });
|
|
54
|
+
if (!audioStream) {
|
|
55
|
+
throw new HTTPException(500, { message: "Failed to generate speech" });
|
|
56
|
+
}
|
|
57
|
+
const chunks = [];
|
|
58
|
+
for await (const chunk of audioStream) {
|
|
59
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
60
|
+
}
|
|
61
|
+
const audioData = Buffer.concat(chunks);
|
|
62
|
+
return { audioData };
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return handleError(error, "Error generating speech");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function transcribeSpeechHandler({
|
|
68
|
+
mastra,
|
|
69
|
+
agentId,
|
|
70
|
+
body
|
|
71
|
+
}) {
|
|
72
|
+
try {
|
|
73
|
+
if (!agentId) {
|
|
74
|
+
throw new HTTPException(400, { message: "Agent ID is required" });
|
|
75
|
+
}
|
|
76
|
+
if (!body?.audioData) {
|
|
77
|
+
throw new HTTPException(400, { message: "Audio data is required" });
|
|
78
|
+
}
|
|
79
|
+
const agent = mastra.getAgent(agentId);
|
|
80
|
+
if (!agent) {
|
|
81
|
+
throw new HTTPException(404, { message: "Agent not found" });
|
|
82
|
+
}
|
|
83
|
+
if (!agent.voice) {
|
|
84
|
+
throw new HTTPException(400, { message: "Agent does not have voice capabilities" });
|
|
85
|
+
}
|
|
86
|
+
const audioStream = new Readable();
|
|
87
|
+
audioStream.push(body.audioData);
|
|
88
|
+
audioStream.push(null);
|
|
89
|
+
const text = await agent.voice.listen(audioStream, body.options);
|
|
90
|
+
return { text };
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return handleError(error, "Error transcribing speech");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { generateSpeechHandler, getSpeakersHandler, transcribeSpeechHandler, voice_exports };
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkCVPOREIE_cjs = require('./chunk-CVPOREIE.cjs');
|
|
4
|
+
|
|
5
|
+
// src/server/handlers/a2a.ts
|
|
6
|
+
var a2a_exports = {};
|
|
7
|
+
chunkCVPOREIE_cjs.__export(a2a_exports, {
|
|
8
|
+
generateAgentCard: () => generateAgentCard,
|
|
9
|
+
handleA2ARequest: () => handleA2ARequest,
|
|
10
|
+
handleAgentCardRequest: () => handleAgentCardRequest,
|
|
11
|
+
handleCancelTask: () => handleCancelTask,
|
|
12
|
+
handleGetTask: () => handleGetTask,
|
|
13
|
+
handlePushNotification: () => handlePushNotification,
|
|
14
|
+
handleSendTask: () => handleSendTask
|
|
15
|
+
});
|
|
16
|
+
var tasks = /* @__PURE__ */ new Map();
|
|
17
|
+
function convertToA2AMessage({ message, role }) {
|
|
18
|
+
if (typeof message === "string") {
|
|
19
|
+
return {
|
|
20
|
+
role,
|
|
21
|
+
parts: [
|
|
22
|
+
{
|
|
23
|
+
type: "text",
|
|
24
|
+
text: message
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (message && message.text) {
|
|
30
|
+
return {
|
|
31
|
+
role,
|
|
32
|
+
parts: [
|
|
33
|
+
{
|
|
34
|
+
type: "text",
|
|
35
|
+
text: message.text
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
let parts = [];
|
|
41
|
+
if (message && message.content) {
|
|
42
|
+
if (typeof message.content === "string") {
|
|
43
|
+
parts.push({
|
|
44
|
+
type: "text",
|
|
45
|
+
text: message.content
|
|
46
|
+
});
|
|
47
|
+
} else if (Array.isArray(message.content)) {
|
|
48
|
+
parts = message.content.map((item) => {
|
|
49
|
+
if (typeof item === "string") {
|
|
50
|
+
return {
|
|
51
|
+
type: "text",
|
|
52
|
+
text: item
|
|
53
|
+
};
|
|
54
|
+
} else if (item && item.type === "text") {
|
|
55
|
+
return {
|
|
56
|
+
type: "text",
|
|
57
|
+
text: item.text || ""
|
|
58
|
+
};
|
|
59
|
+
} else if (item && item.type === "file") {
|
|
60
|
+
return {
|
|
61
|
+
type: "file",
|
|
62
|
+
file: {
|
|
63
|
+
name: item.name || null,
|
|
64
|
+
mimeType: item.mimeType || null,
|
|
65
|
+
uri: item.uri || null
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
} else {
|
|
69
|
+
return {
|
|
70
|
+
type: "data",
|
|
71
|
+
data: item || {}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (parts.length === 0 && typeof message === "object" && message !== null) {
|
|
78
|
+
try {
|
|
79
|
+
const textRepresentation = JSON.stringify(message, null, 2);
|
|
80
|
+
parts.push({
|
|
81
|
+
type: "text",
|
|
82
|
+
text: textRepresentation
|
|
83
|
+
});
|
|
84
|
+
} catch (_e) {
|
|
85
|
+
parts.push({
|
|
86
|
+
type: "text",
|
|
87
|
+
text: "Unable to convert agent response to text"
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
role,
|
|
93
|
+
parts
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function convertFromA2AMessage({ message }) {
|
|
97
|
+
if (message.parts && message.parts.length === 1 && message.parts[0]?.type === "text") {
|
|
98
|
+
return message.parts[0]?.text || "";
|
|
99
|
+
}
|
|
100
|
+
const content = (message.parts || []).map((part) => {
|
|
101
|
+
if (part.type === "text") {
|
|
102
|
+
return part.text || "";
|
|
103
|
+
} else if (part.type === "file" && part.file) {
|
|
104
|
+
return {
|
|
105
|
+
type: "file",
|
|
106
|
+
name: part.file.name || "",
|
|
107
|
+
mimeType: part.file.mimeType || "",
|
|
108
|
+
uri: part.file.uri || ""
|
|
109
|
+
};
|
|
110
|
+
} else if (part.type === "data") {
|
|
111
|
+
return part.data || {};
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}).filter(Boolean);
|
|
115
|
+
return {
|
|
116
|
+
role: message.role || "user",
|
|
117
|
+
content: content.length === 1 ? content[0] : content
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function createErrorResponse({
|
|
121
|
+
id,
|
|
122
|
+
code,
|
|
123
|
+
message,
|
|
124
|
+
data
|
|
125
|
+
}) {
|
|
126
|
+
return {
|
|
127
|
+
jsonrpc: "2.0",
|
|
128
|
+
id,
|
|
129
|
+
error: {
|
|
130
|
+
code,
|
|
131
|
+
message,
|
|
132
|
+
data
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function createSuccessResponse({ id, result }) {
|
|
137
|
+
return {
|
|
138
|
+
jsonrpc: "2.0",
|
|
139
|
+
id,
|
|
140
|
+
result
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
async function handleSendTask({
|
|
144
|
+
request,
|
|
145
|
+
agent
|
|
146
|
+
}) {
|
|
147
|
+
try {
|
|
148
|
+
const { id, message, sessionId } = request.params;
|
|
149
|
+
const agentMessage = convertFromA2AMessage({ message });
|
|
150
|
+
const response = await agent.generate(agentMessage);
|
|
151
|
+
const a2aResponse = convertToA2AMessage({ message: response, role: "agent" });
|
|
152
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
153
|
+
const task = {
|
|
154
|
+
id,
|
|
155
|
+
sessionId: sessionId || null,
|
|
156
|
+
status: {
|
|
157
|
+
state: "completed" /* COMPLETED */,
|
|
158
|
+
message: a2aResponse,
|
|
159
|
+
timestamp: now
|
|
160
|
+
},
|
|
161
|
+
history: [message, a2aResponse]
|
|
162
|
+
};
|
|
163
|
+
tasks.set(id, task);
|
|
164
|
+
return createSuccessResponse({ id: request.id || null, result: task });
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error("Error handling send task:", error);
|
|
167
|
+
return createErrorResponse({
|
|
168
|
+
id: request.id || null,
|
|
169
|
+
code: -32603 /* INTERNAL_ERROR */,
|
|
170
|
+
message: "Internal error processing task",
|
|
171
|
+
data: { error: String(error) }
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
async function handleGetTask({ request }) {
|
|
176
|
+
try {
|
|
177
|
+
const { id, historyLength } = request.params;
|
|
178
|
+
const task = tasks.get(id);
|
|
179
|
+
if (!task) {
|
|
180
|
+
return createErrorResponse({
|
|
181
|
+
id: request.id || null,
|
|
182
|
+
code: -32001 /* TASK_NOT_FOUND */,
|
|
183
|
+
message: "Task not found"
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
if (historyLength !== void 0 && historyLength !== null && task.history) {
|
|
187
|
+
task.history = task.history.slice(-historyLength);
|
|
188
|
+
}
|
|
189
|
+
return createSuccessResponse({ id: request.id || null, result: task });
|
|
190
|
+
} catch (error) {
|
|
191
|
+
console.error("Error handling get task:", error);
|
|
192
|
+
return createErrorResponse({
|
|
193
|
+
id: request.id || null,
|
|
194
|
+
code: -32603 /* INTERNAL_ERROR */,
|
|
195
|
+
message: "Internal error retrieving task",
|
|
196
|
+
data: { error: String(error) }
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async function handleCancelTask({ request }) {
|
|
201
|
+
try {
|
|
202
|
+
const { id } = request.params;
|
|
203
|
+
const task = tasks.get(id);
|
|
204
|
+
if (!task) {
|
|
205
|
+
return createErrorResponse({
|
|
206
|
+
id: request.id || null,
|
|
207
|
+
code: -32001 /* TASK_NOT_FOUND */,
|
|
208
|
+
message: "Task not found"
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
if (task.status.state === "completed" /* COMPLETED */ || task.status.state === "canceled" /* CANCELED */ || task.status.state === "failed" /* FAILED */) {
|
|
212
|
+
return createErrorResponse({
|
|
213
|
+
id: request.id || null,
|
|
214
|
+
code: -32002 /* TASK_NOT_CANCELABLE */,
|
|
215
|
+
message: "Task cannot be canceled"
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
task.status = {
|
|
219
|
+
state: "canceled" /* CANCELED */,
|
|
220
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
221
|
+
};
|
|
222
|
+
tasks.set(id, task);
|
|
223
|
+
return createSuccessResponse({ id: request.id || null, result: task });
|
|
224
|
+
} catch (error) {
|
|
225
|
+
console.error("Error handling cancel task:", error);
|
|
226
|
+
return createErrorResponse({
|
|
227
|
+
id: request.id || null,
|
|
228
|
+
code: -32603 /* INTERNAL_ERROR */,
|
|
229
|
+
message: "Internal error canceling task",
|
|
230
|
+
data: { error: String(error) }
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function handlePushNotification() {
|
|
235
|
+
return createErrorResponse({
|
|
236
|
+
id: null,
|
|
237
|
+
code: -32003 /* PUSH_NOTIFICATION_NOT_SUPPORTED */,
|
|
238
|
+
message: "Push Notification is not supported"
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
async function handleA2ARequest({
|
|
242
|
+
request,
|
|
243
|
+
agent
|
|
244
|
+
}) {
|
|
245
|
+
try {
|
|
246
|
+
const requestId = "id" in request ? request.id : null;
|
|
247
|
+
switch (request.method) {
|
|
248
|
+
case "tasks/send":
|
|
249
|
+
return await handleSendTask({ request, agent });
|
|
250
|
+
case "tasks/get":
|
|
251
|
+
return await handleGetTask({ request });
|
|
252
|
+
case "tasks/cancel":
|
|
253
|
+
return await handleCancelTask({ request });
|
|
254
|
+
case "tasks/pushNotification/set":
|
|
255
|
+
case "tasks/pushNotification/get":
|
|
256
|
+
case "tasks/resubscribe":
|
|
257
|
+
return handlePushNotification();
|
|
258
|
+
default:
|
|
259
|
+
return createErrorResponse({
|
|
260
|
+
id: requestId,
|
|
261
|
+
code: -32601 /* METHOD_NOT_FOUND */,
|
|
262
|
+
message: "Method not found"
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
} catch (error) {
|
|
266
|
+
console.error("Error handling A2A request:", error);
|
|
267
|
+
const errorRequestId = "id" in request && request.id !== void 0 ? request.id : null;
|
|
268
|
+
return createErrorResponse({
|
|
269
|
+
id: errorRequestId,
|
|
270
|
+
code: -32603 /* INTERNAL_ERROR */,
|
|
271
|
+
message: "Internal error",
|
|
272
|
+
data: { error: String(error) }
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function generateAgentCard({ agent, baseUrl }) {
|
|
277
|
+
const skills = [];
|
|
278
|
+
if (agent.tools) {
|
|
279
|
+
Object.entries(agent.tools).forEach(([id, tool]) => {
|
|
280
|
+
const toolName = typeof tool === "object" && tool !== null ? tool.id || id : id;
|
|
281
|
+
const toolDescription = typeof tool === "object" && tool !== null ? tool.description || null : null;
|
|
282
|
+
skills.push({
|
|
283
|
+
id,
|
|
284
|
+
name: toolName,
|
|
285
|
+
description: toolDescription
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
if (skills.length === 0) {
|
|
290
|
+
skills.push({
|
|
291
|
+
id: "conversation",
|
|
292
|
+
name: "Conversation",
|
|
293
|
+
description: "General conversation capabilities"
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
const capabilities = {
|
|
297
|
+
streaming: false,
|
|
298
|
+
pushNotifications: false,
|
|
299
|
+
stateTransitionHistory: false
|
|
300
|
+
};
|
|
301
|
+
return {
|
|
302
|
+
name: agent.name || "Mastra Agent",
|
|
303
|
+
description: typeof agent.instructions === "string" ? agent.instructions : null,
|
|
304
|
+
url: `${baseUrl}/a2a`,
|
|
305
|
+
version: "1.0.0",
|
|
306
|
+
capabilities,
|
|
307
|
+
skills
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function handleAgentCardRequest({ agent, baseUrl }) {
|
|
311
|
+
return generateAgentCard({ agent, baseUrl });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
exports.a2a_exports = a2a_exports;
|
|
315
|
+
exports.generateAgentCard = generateAgentCard;
|
|
316
|
+
exports.handleA2ARequest = handleA2ARequest;
|
|
317
|
+
exports.handleAgentCardRequest = handleAgentCardRequest;
|
|
318
|
+
exports.handleCancelTask = handleCancelTask;
|
|
319
|
+
exports.handleGetTask = handleGetTask;
|
|
320
|
+
exports.handlePushNotification = handlePushNotification;
|
|
321
|
+
exports.handleSendTask = handleSendTask;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { HTTPException } from './chunk-NYN7KFXL.js';
|
|
2
|
+
|
|
3
|
+
// src/server/handlers/error.ts
|
|
4
|
+
function handleError(error, defaultMessage) {
|
|
5
|
+
const apiError = error;
|
|
6
|
+
throw new HTTPException(apiError.status || 500, {
|
|
7
|
+
message: apiError.message || defaultMessage
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { handleError };
|