@mastra/server 0.0.0-commonjs-20250414101718

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.
Files changed (83) hide show
  1. package/LICENSE.md +46 -0
  2. package/README.md +159 -0
  3. package/dist/_tsup-dts-rollup.d.cts +571 -0
  4. package/dist/_tsup-dts-rollup.d.ts +571 -0
  5. package/dist/chunk-2FJURXCL.cjs +213 -0
  6. package/dist/chunk-3AHQ5RGN.js +11 -0
  7. package/dist/chunk-3EJZQ6TQ.js +49 -0
  8. package/dist/chunk-3RVHWGWO.js +95 -0
  9. package/dist/chunk-4C3EPMMF.cjs +122 -0
  10. package/dist/chunk-4JINXASC.js +120 -0
  11. package/dist/chunk-67WTHYAV.js +156 -0
  12. package/dist/chunk-A7DF4ETD.cjs +100 -0
  13. package/dist/chunk-B64YROKU.cjs +317 -0
  14. package/dist/chunk-CWSDZEZG.cjs +139 -0
  15. package/dist/chunk-DVPP5S6I.js +135 -0
  16. package/dist/chunk-FV45V6WC.cjs +43 -0
  17. package/dist/chunk-JLDXUWK7.cjs +126 -0
  18. package/dist/chunk-L7XE5QTW.js +16 -0
  19. package/dist/chunk-M56ECCHK.cjs +128 -0
  20. package/dist/chunk-OPP7H5TW.js +305 -0
  21. package/dist/chunk-QH6XWSXP.cjs +5579 -0
  22. package/dist/chunk-QN4KF3BH.cjs +18 -0
  23. package/dist/chunk-RBQASTUP.js +203 -0
  24. package/dist/chunk-SKBVVI24.cjs +54 -0
  25. package/dist/chunk-TRDNDNGQ.js +40 -0
  26. package/dist/chunk-VK6FX47H.js +5576 -0
  27. package/dist/chunk-YANVFOYA.js +120 -0
  28. package/dist/chunk-YNSGUC2O.js +116 -0
  29. package/dist/chunk-Z46X3YCB.cjs +164 -0
  30. package/dist/chunk-ZLBRQFDD.cjs +13 -0
  31. package/dist/index.cjs +2 -0
  32. package/dist/index.d.cts +1 -0
  33. package/dist/index.d.ts +1 -0
  34. package/dist/index.js +1 -0
  35. package/dist/server/handlers/agents.cjs +30 -0
  36. package/dist/server/handlers/agents.d.cts +6 -0
  37. package/dist/server/handlers/agents.d.ts +6 -0
  38. package/dist/server/handlers/agents.js +1 -0
  39. package/dist/server/handlers/error.cjs +10 -0
  40. package/dist/server/handlers/error.d.cts +1 -0
  41. package/dist/server/handlers/error.d.ts +1 -0
  42. package/dist/server/handlers/error.js +1 -0
  43. package/dist/server/handlers/logs.cjs +18 -0
  44. package/dist/server/handlers/logs.d.cts +3 -0
  45. package/dist/server/handlers/logs.d.ts +3 -0
  46. package/dist/server/handlers/logs.js +1 -0
  47. package/dist/server/handlers/memory.cjs +38 -0
  48. package/dist/server/handlers/memory.d.cts +8 -0
  49. package/dist/server/handlers/memory.d.ts +8 -0
  50. package/dist/server/handlers/memory.js +1 -0
  51. package/dist/server/handlers/network.cjs +22 -0
  52. package/dist/server/handlers/network.d.cts +4 -0
  53. package/dist/server/handlers/network.d.ts +4 -0
  54. package/dist/server/handlers/network.js +1 -0
  55. package/dist/server/handlers/telemetry.cjs +14 -0
  56. package/dist/server/handlers/telemetry.d.cts +2 -0
  57. package/dist/server/handlers/telemetry.d.ts +2 -0
  58. package/dist/server/handlers/telemetry.js +1 -0
  59. package/dist/server/handlers/tools.cjs +22 -0
  60. package/dist/server/handlers/tools.d.cts +4 -0
  61. package/dist/server/handlers/tools.d.ts +4 -0
  62. package/dist/server/handlers/tools.js +1 -0
  63. package/dist/server/handlers/utils.cjs +10 -0
  64. package/dist/server/handlers/utils.d.cts +1 -0
  65. package/dist/server/handlers/utils.d.ts +1 -0
  66. package/dist/server/handlers/utils.js +1 -0
  67. package/dist/server/handlers/vector.cjs +30 -0
  68. package/dist/server/handlers/vector.d.cts +6 -0
  69. package/dist/server/handlers/vector.d.ts +6 -0
  70. package/dist/server/handlers/vector.js +1 -0
  71. package/dist/server/handlers/voice.cjs +18 -0
  72. package/dist/server/handlers/voice.d.cts +3 -0
  73. package/dist/server/handlers/voice.d.ts +3 -0
  74. package/dist/server/handlers/voice.js +1 -0
  75. package/dist/server/handlers/workflows.cjs +46 -0
  76. package/dist/server/handlers/workflows.d.cts +10 -0
  77. package/dist/server/handlers/workflows.d.ts +10 -0
  78. package/dist/server/handlers/workflows.js +1 -0
  79. package/dist/server/handlers.cjs +50 -0
  80. package/dist/server/handlers.d.cts +9 -0
  81. package/dist/server/handlers.d.ts +9 -0
  82. package/dist/server/handlers.js +9 -0
  83. package/package.json +71 -0
@@ -0,0 +1,156 @@
1
+ import { stringify, esm_default } from './chunk-VK6FX47H.js';
2
+ import { validateBody } from './chunk-L7XE5QTW.js';
3
+ import { handleError } from './chunk-3AHQ5RGN.js';
4
+ import { __export, HTTPException } from './chunk-TRDNDNGQ.js';
5
+
6
+ // src/server/handlers/agents.ts
7
+ var agents_exports = {};
8
+ __export(agents_exports, {
9
+ generateHandler: () => generateHandler,
10
+ getAgentByIdHandler: () => getAgentByIdHandler,
11
+ getAgentsHandler: () => getAgentsHandler,
12
+ getEvalsByAgentIdHandler: () => getEvalsByAgentIdHandler,
13
+ getLiveEvalsByAgentIdHandler: () => getLiveEvalsByAgentIdHandler,
14
+ streamGenerateHandler: () => streamGenerateHandler
15
+ });
16
+ async function getAgentsHandler({ mastra }) {
17
+ try {
18
+ const agents = mastra.getAgents();
19
+ const serializedAgents = Object.entries(agents).reduce((acc, [_id, _agent]) => {
20
+ const agent = _agent;
21
+ const serializedAgentTools = Object.entries(agent?.tools || {}).reduce((acc2, [key, tool]) => {
22
+ const _tool = tool;
23
+ acc2[key] = {
24
+ ..._tool,
25
+ inputSchema: _tool.inputSchema ? stringify(esm_default(_tool.inputSchema)) : void 0,
26
+ outputSchema: _tool.outputSchema ? stringify(esm_default(_tool.outputSchema)) : void 0
27
+ };
28
+ return acc2;
29
+ }, {});
30
+ acc[_id] = {
31
+ name: agent.name,
32
+ instructions: agent.instructions,
33
+ tools: serializedAgentTools,
34
+ provider: agent.llm?.getProvider(),
35
+ modelId: agent.llm?.getModelId()
36
+ };
37
+ return acc;
38
+ }, {});
39
+ return serializedAgents;
40
+ } catch (error) {
41
+ return handleError(error, "Error getting agents");
42
+ }
43
+ }
44
+ async function getAgentByIdHandler({ mastra, agentId }) {
45
+ try {
46
+ const agent = mastra.getAgent(agentId);
47
+ if (!agent) {
48
+ throw new HTTPException(404, { message: "Agent not found" });
49
+ }
50
+ const serializedAgentTools = Object.entries(agent?.tools || {}).reduce((acc, [key, tool]) => {
51
+ const _tool = tool;
52
+ acc[key] = {
53
+ ..._tool,
54
+ inputSchema: _tool.inputSchema ? stringify(esm_default(_tool.inputSchema)) : void 0,
55
+ outputSchema: _tool.outputSchema ? stringify(esm_default(_tool.outputSchema)) : void 0
56
+ };
57
+ return acc;
58
+ }, {});
59
+ return {
60
+ name: agent.name,
61
+ instructions: agent.instructions,
62
+ tools: serializedAgentTools,
63
+ provider: agent.llm?.getProvider(),
64
+ modelId: agent.llm?.getModelId()
65
+ };
66
+ } catch (error) {
67
+ return handleError(error, "Error getting agent");
68
+ }
69
+ }
70
+ async function getEvalsByAgentIdHandler({ mastra, agentId }) {
71
+ try {
72
+ const agent = mastra.getAgent(agentId);
73
+ const evals = await mastra.getStorage()?.__getEvalsByAgentName?.(agent.name, "test") || [];
74
+ return {
75
+ id: agentId,
76
+ name: agent.name,
77
+ instructions: agent.instructions,
78
+ evals
79
+ };
80
+ } catch (error) {
81
+ return handleError(error, "Error getting test evals");
82
+ }
83
+ }
84
+ async function getLiveEvalsByAgentIdHandler({ mastra, agentId }) {
85
+ try {
86
+ const agent = mastra.getAgent(agentId);
87
+ const evals = await mastra.getStorage()?.__getEvalsByAgentName?.(agent.name, "live") || [];
88
+ return {
89
+ id: agentId,
90
+ name: agent.name,
91
+ instructions: agent.instructions,
92
+ evals
93
+ };
94
+ } catch (error) {
95
+ return handleError(error, "Error getting live evals");
96
+ }
97
+ }
98
+ async function generateHandler({
99
+ mastra,
100
+ container,
101
+ agentId,
102
+ body
103
+ }) {
104
+ try {
105
+ const agent = mastra.getAgent(agentId);
106
+ if (!agent) {
107
+ throw new HTTPException(404, { message: "Agent not found" });
108
+ }
109
+ const { messages, resourceId, resourceid, ...rest } = body;
110
+ const finalResourceId = resourceId ?? resourceid;
111
+ validateBody({ messages });
112
+ const result = await agent.generate(messages, {
113
+ ...rest,
114
+ // @ts-expect-error TODO fix types
115
+ resourceId: finalResourceId,
116
+ container
117
+ });
118
+ return result;
119
+ } catch (error) {
120
+ return handleError(error, "Error generating from agent");
121
+ }
122
+ }
123
+ async function streamGenerateHandler({
124
+ mastra,
125
+ container,
126
+ agentId,
127
+ body
128
+ }) {
129
+ try {
130
+ const agent = mastra.getAgent(agentId);
131
+ if (!agent) {
132
+ throw new HTTPException(404, { message: "Agent not found" });
133
+ }
134
+ const { messages, resourceId, resourceid, ...rest } = body;
135
+ const finalResourceId = resourceId ?? resourceid;
136
+ validateBody({ messages });
137
+ const streamResult = await agent.stream(messages, {
138
+ ...rest,
139
+ // @ts-expect-error TODO fix types
140
+ resourceId: finalResourceId,
141
+ container
142
+ });
143
+ const streamResponse = rest.output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
144
+ sendUsage: true,
145
+ sendReasoning: true,
146
+ getErrorMessage: (error) => {
147
+ return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
148
+ }
149
+ });
150
+ return streamResponse;
151
+ } catch (error) {
152
+ throw new HTTPException(error?.status ?? 500, { message: error?.message ?? "Error streaming from agent" });
153
+ }
154
+ }
155
+
156
+ export { agents_exports, generateHandler, getAgentByIdHandler, getAgentsHandler, getEvalsByAgentIdHandler, getLiveEvalsByAgentIdHandler, streamGenerateHandler };
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ var chunkQN4KF3BH_cjs = require('./chunk-QN4KF3BH.cjs');
4
+ var chunkZLBRQFDD_cjs = require('./chunk-ZLBRQFDD.cjs');
5
+ var chunkFV45V6WC_cjs = require('./chunk-FV45V6WC.cjs');
6
+ var stream = require('stream');
7
+
8
+ // src/server/handlers/voice.ts
9
+ var voice_exports = {};
10
+ chunkFV45V6WC_cjs.__export(voice_exports, {
11
+ generateSpeechHandler: () => generateSpeechHandler,
12
+ getSpeakersHandler: () => getSpeakersHandler,
13
+ transcribeSpeechHandler: () => transcribeSpeechHandler
14
+ });
15
+ async function getSpeakersHandler({ mastra, agentId }) {
16
+ try {
17
+ if (!agentId) {
18
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Agent ID is required" });
19
+ }
20
+ const agent = mastra.getAgent(agentId);
21
+ if (!agent) {
22
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Agent not found" });
23
+ }
24
+ if (!agent.voice) {
25
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Agent does not have voice capabilities" });
26
+ }
27
+ const speakers = await agent.voice.getSpeakers();
28
+ return speakers;
29
+ } catch (error) {
30
+ return chunkZLBRQFDD_cjs.handleError(error, "Error getting speakers");
31
+ }
32
+ }
33
+ async function generateSpeechHandler({
34
+ mastra,
35
+ agentId,
36
+ body
37
+ }) {
38
+ try {
39
+ if (!agentId) {
40
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Agent ID is required" });
41
+ }
42
+ chunkQN4KF3BH_cjs.validateBody({
43
+ text: void 0,
44
+ speakerId: void 0,
45
+ ...body
46
+ });
47
+ const agent = mastra.getAgent(agentId);
48
+ if (!agent) {
49
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Agent not found" });
50
+ }
51
+ if (!agent.voice) {
52
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Agent does not have voice capabilities" });
53
+ }
54
+ const audioStream = await agent.voice.speak(body.text, { speaker: body.speakerId });
55
+ if (!audioStream) {
56
+ throw new chunkFV45V6WC_cjs.HTTPException(500, { message: "Failed to generate speech" });
57
+ }
58
+ const chunks = [];
59
+ for await (const chunk of audioStream) {
60
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
61
+ }
62
+ const audioData = Buffer.concat(chunks);
63
+ return { audioData };
64
+ } catch (error) {
65
+ return chunkZLBRQFDD_cjs.handleError(error, "Error generating speech");
66
+ }
67
+ }
68
+ async function transcribeSpeechHandler({
69
+ mastra,
70
+ agentId,
71
+ body
72
+ }) {
73
+ try {
74
+ if (!agentId) {
75
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Agent ID is required" });
76
+ }
77
+ if (!body?.audioData) {
78
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Audio data is required" });
79
+ }
80
+ const agent = mastra.getAgent(agentId);
81
+ if (!agent) {
82
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Agent not found" });
83
+ }
84
+ if (!agent.voice) {
85
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Agent does not have voice capabilities" });
86
+ }
87
+ const audioStream = new stream.Readable();
88
+ audioStream.push(body.audioData);
89
+ audioStream.push(null);
90
+ const text = await agent.voice.listen(audioStream, body.options);
91
+ return { text };
92
+ } catch (error) {
93
+ return chunkZLBRQFDD_cjs.handleError(error, "Error transcribing speech");
94
+ }
95
+ }
96
+
97
+ exports.generateSpeechHandler = generateSpeechHandler;
98
+ exports.getSpeakersHandler = getSpeakersHandler;
99
+ exports.transcribeSpeechHandler = transcribeSpeechHandler;
100
+ exports.voice_exports = voice_exports;
@@ -0,0 +1,317 @@
1
+ 'use strict';
2
+
3
+ var chunkQH6XWSXP_cjs = require('./chunk-QH6XWSXP.cjs');
4
+ var chunkZLBRQFDD_cjs = require('./chunk-ZLBRQFDD.cjs');
5
+ var chunkFV45V6WC_cjs = require('./chunk-FV45V6WC.cjs');
6
+ var web = require('stream/web');
7
+
8
+ // src/server/handlers/workflows.ts
9
+ var workflows_exports = {};
10
+ chunkFV45V6WC_cjs.__export(workflows_exports, {
11
+ createRunHandler: () => createRunHandler,
12
+ getWorkflowByIdHandler: () => getWorkflowByIdHandler,
13
+ getWorkflowRunHandler: () => getWorkflowRunHandler,
14
+ getWorkflowRunsHandler: () => getWorkflowRunsHandler,
15
+ getWorkflowsHandler: () => getWorkflowsHandler,
16
+ resumeAsyncWorkflowHandler: () => resumeAsyncWorkflowHandler,
17
+ resumeWorkflowHandler: () => resumeWorkflowHandler,
18
+ startAsyncWorkflowHandler: () => startAsyncWorkflowHandler,
19
+ startWorkflowRunHandler: () => startWorkflowRunHandler,
20
+ watchWorkflowHandler: () => watchWorkflowHandler
21
+ });
22
+ async function getWorkflowsHandler({ mastra }) {
23
+ try {
24
+ const workflows = mastra.getWorkflows({ serialized: false });
25
+ const _workflows = Object.entries(workflows).reduce((acc, [key, workflow]) => {
26
+ if (workflow.isNested) return acc;
27
+ acc[key] = {
28
+ stepGraph: workflow.stepGraph,
29
+ stepSubscriberGraph: workflow.stepSubscriberGraph,
30
+ serializedStepGraph: workflow.serializedStepGraph,
31
+ serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
32
+ name: workflow.name,
33
+ triggerSchema: workflow.triggerSchema ? chunkQH6XWSXP_cjs.stringify(chunkQH6XWSXP_cjs.esm_default(workflow.triggerSchema)) : void 0,
34
+ steps: Object.entries(workflow.steps).reduce((acc2, [key2, step]) => {
35
+ const _step = step;
36
+ acc2[key2] = {
37
+ ..._step,
38
+ inputSchema: _step.inputSchema ? chunkQH6XWSXP_cjs.stringify(chunkQH6XWSXP_cjs.esm_default(_step.inputSchema)) : void 0,
39
+ outputSchema: _step.outputSchema ? chunkQH6XWSXP_cjs.stringify(chunkQH6XWSXP_cjs.esm_default(_step.outputSchema)) : void 0
40
+ };
41
+ return acc2;
42
+ }, {})
43
+ };
44
+ return acc;
45
+ }, {});
46
+ return _workflows;
47
+ } catch (error) {
48
+ throw new chunkFV45V6WC_cjs.HTTPException(500, { message: error?.message || "Error getting workflows" });
49
+ }
50
+ }
51
+ async function getWorkflowByIdHandler({ mastra, workflowId }) {
52
+ try {
53
+ if (!workflowId) {
54
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
55
+ }
56
+ const workflow = mastra.getWorkflow(workflowId);
57
+ if (!workflow) {
58
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow not found" });
59
+ }
60
+ return {
61
+ stepGraph: workflow.stepGraph,
62
+ stepSubscriberGraph: workflow.stepSubscriberGraph,
63
+ serializedStepGraph: workflow.serializedStepGraph,
64
+ serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
65
+ name: workflow.name,
66
+ triggerSchema: workflow.triggerSchema ? chunkQH6XWSXP_cjs.stringify(chunkQH6XWSXP_cjs.esm_default(workflow.triggerSchema)) : void 0,
67
+ steps: Object.entries(workflow.steps).reduce((acc, [key, step]) => {
68
+ const _step = step;
69
+ acc[key] = {
70
+ ..._step,
71
+ inputSchema: _step.inputSchema ? chunkQH6XWSXP_cjs.stringify(chunkQH6XWSXP_cjs.esm_default(_step.inputSchema)) : void 0,
72
+ outputSchema: _step.outputSchema ? chunkQH6XWSXP_cjs.stringify(chunkQH6XWSXP_cjs.esm_default(_step.outputSchema)) : void 0
73
+ };
74
+ return acc;
75
+ }, {})
76
+ };
77
+ } catch (error) {
78
+ throw new chunkFV45V6WC_cjs.HTTPException(500, { message: error?.message || "Error getting workflow" });
79
+ }
80
+ }
81
+ async function startAsyncWorkflowHandler({
82
+ mastra,
83
+ container,
84
+ workflowId,
85
+ runId,
86
+ triggerData
87
+ }) {
88
+ try {
89
+ if (!workflowId) {
90
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
91
+ }
92
+ const workflow = mastra.getWorkflow(workflowId);
93
+ if (!workflow) {
94
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow not found" });
95
+ }
96
+ if (!runId) {
97
+ const { start } = workflow.createRun();
98
+ const result2 = await start({
99
+ triggerData,
100
+ container
101
+ });
102
+ return result2;
103
+ }
104
+ const run = workflow.getRun(runId);
105
+ if (!run) {
106
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow run not found" });
107
+ }
108
+ const result = await run.start({
109
+ triggerData,
110
+ container
111
+ });
112
+ return result;
113
+ } catch (error) {
114
+ throw new chunkFV45V6WC_cjs.HTTPException(500, { message: error?.message || "Error executing workflow" });
115
+ }
116
+ }
117
+ async function getWorkflowRunHandler({
118
+ mastra,
119
+ workflowId,
120
+ runId
121
+ }) {
122
+ try {
123
+ if (!workflowId) {
124
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
125
+ }
126
+ if (!runId) {
127
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Run ID is required" });
128
+ }
129
+ const workflow = mastra.getWorkflow(workflowId);
130
+ if (!workflow) {
131
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow not found" });
132
+ }
133
+ const run = workflow.getRun(runId);
134
+ if (!run) {
135
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow run not found" });
136
+ }
137
+ return run;
138
+ } catch (error) {
139
+ throw new chunkFV45V6WC_cjs.HTTPException(500, { message: error?.message || "Error getting workflow run" });
140
+ }
141
+ }
142
+ async function createRunHandler({
143
+ mastra,
144
+ workflowId,
145
+ runId: prevRunId
146
+ }) {
147
+ try {
148
+ if (!workflowId) {
149
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
150
+ }
151
+ const workflow = mastra.getWorkflow(workflowId);
152
+ if (!workflow) {
153
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow not found" });
154
+ }
155
+ const { runId } = workflow.createRun({ runId: prevRunId });
156
+ return { runId };
157
+ } catch (error) {
158
+ throw new chunkFV45V6WC_cjs.HTTPException(500, { message: error?.message || "Error creating workflow run" });
159
+ }
160
+ }
161
+ async function startWorkflowRunHandler({
162
+ mastra,
163
+ container,
164
+ workflowId,
165
+ runId,
166
+ triggerData
167
+ }) {
168
+ try {
169
+ if (!workflowId) {
170
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
171
+ }
172
+ if (!runId) {
173
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "runId required to start run" });
174
+ }
175
+ const workflow = mastra.getWorkflow(workflowId);
176
+ const run = workflow.getRun(runId);
177
+ if (!run) {
178
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow run not found" });
179
+ }
180
+ await run.start({
181
+ triggerData,
182
+ container
183
+ });
184
+ return { message: "Workflow run started" };
185
+ } catch (e) {
186
+ return chunkZLBRQFDD_cjs.handleError(e, "Error starting workflow run");
187
+ }
188
+ }
189
+ async function watchWorkflowHandler({
190
+ mastra,
191
+ workflowId,
192
+ runId
193
+ }) {
194
+ try {
195
+ if (!workflowId) {
196
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
197
+ }
198
+ if (!runId) {
199
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "runId required to watch workflow" });
200
+ }
201
+ const workflow = mastra.getWorkflow(workflowId);
202
+ const run = workflow.getRun(runId);
203
+ if (!run) {
204
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow run not found" });
205
+ }
206
+ let unwatch;
207
+ let asyncRef = null;
208
+ const stream = new web.ReadableStream({
209
+ start(controller) {
210
+ unwatch = run.watch(({ activePaths, runId: runId2, timestamp, results }) => {
211
+ const activePathsObj = Object.fromEntries(activePaths);
212
+ controller.enqueue(JSON.stringify({ activePaths: activePathsObj, runId: runId2, timestamp, results }));
213
+ if (asyncRef) {
214
+ clearImmediate(asyncRef);
215
+ asyncRef = null;
216
+ }
217
+ asyncRef = setImmediate(() => {
218
+ if (!workflow.getRun(runId2)) {
219
+ controller.close();
220
+ }
221
+ });
222
+ });
223
+ },
224
+ cancel() {
225
+ unwatch?.();
226
+ }
227
+ });
228
+ return stream;
229
+ } catch (error) {
230
+ return chunkZLBRQFDD_cjs.handleError(error, "Error watching workflow");
231
+ }
232
+ }
233
+ async function resumeAsyncWorkflowHandler({
234
+ mastra,
235
+ workflowId,
236
+ runId,
237
+ body,
238
+ container
239
+ }) {
240
+ try {
241
+ if (!workflowId) {
242
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
243
+ }
244
+ if (!runId) {
245
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "runId required to resume workflow" });
246
+ }
247
+ const workflow = mastra.getWorkflow(workflowId);
248
+ const run = workflow.getRun(runId);
249
+ if (!run) {
250
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow run not found" });
251
+ }
252
+ const result = await run.resume({
253
+ stepId: body.stepId,
254
+ context: body.context,
255
+ container
256
+ });
257
+ return result;
258
+ } catch (error) {
259
+ return chunkZLBRQFDD_cjs.handleError(error, "Error resuming workflow step");
260
+ }
261
+ }
262
+ async function resumeWorkflowHandler({
263
+ mastra,
264
+ workflowId,
265
+ runId,
266
+ body,
267
+ container
268
+ }) {
269
+ try {
270
+ if (!workflowId) {
271
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
272
+ }
273
+ if (!runId) {
274
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "runId required to resume workflow" });
275
+ }
276
+ const workflow = mastra.getWorkflow(workflowId);
277
+ const run = workflow.getRun(runId);
278
+ if (!run) {
279
+ throw new chunkFV45V6WC_cjs.HTTPException(404, { message: "Workflow run not found" });
280
+ }
281
+ await run.resume({
282
+ stepId: body.stepId,
283
+ context: body.context,
284
+ container
285
+ });
286
+ return { message: "Workflow run resumed" };
287
+ } catch (error) {
288
+ return chunkZLBRQFDD_cjs.handleError(error, "Error resuming workflow");
289
+ }
290
+ }
291
+ async function getWorkflowRunsHandler({ mastra, workflowId }) {
292
+ try {
293
+ if (!workflowId) {
294
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Workflow ID is required" });
295
+ }
296
+ const workflow = mastra.getWorkflow(workflowId);
297
+ const workflowRuns = await workflow.getWorkflowRuns() || {
298
+ runs: [],
299
+ total: 0
300
+ };
301
+ return workflowRuns;
302
+ } catch (error) {
303
+ return chunkZLBRQFDD_cjs.handleError(error, "Error getting workflow runs");
304
+ }
305
+ }
306
+
307
+ exports.createRunHandler = createRunHandler;
308
+ exports.getWorkflowByIdHandler = getWorkflowByIdHandler;
309
+ exports.getWorkflowRunHandler = getWorkflowRunHandler;
310
+ exports.getWorkflowRunsHandler = getWorkflowRunsHandler;
311
+ exports.getWorkflowsHandler = getWorkflowsHandler;
312
+ exports.resumeAsyncWorkflowHandler = resumeAsyncWorkflowHandler;
313
+ exports.resumeWorkflowHandler = resumeWorkflowHandler;
314
+ exports.startAsyncWorkflowHandler = startAsyncWorkflowHandler;
315
+ exports.startWorkflowRunHandler = startWorkflowRunHandler;
316
+ exports.watchWorkflowHandler = watchWorkflowHandler;
317
+ exports.workflows_exports = workflows_exports;
@@ -0,0 +1,139 @@
1
+ 'use strict';
2
+
3
+ var chunkZLBRQFDD_cjs = require('./chunk-ZLBRQFDD.cjs');
4
+ var chunkFV45V6WC_cjs = require('./chunk-FV45V6WC.cjs');
5
+
6
+ // src/server/handlers/telemetry.ts
7
+ var telemetry_exports = {};
8
+ chunkFV45V6WC_cjs.__export(telemetry_exports, {
9
+ getTelemetryHandler: () => getTelemetryHandler,
10
+ storeTelemetryHandler: () => storeTelemetryHandler
11
+ });
12
+ async function getTelemetryHandler({ mastra, body }) {
13
+ try {
14
+ const telemetry = mastra.getTelemetry();
15
+ const storage = mastra.getStorage();
16
+ if (!telemetry) {
17
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Telemetry is not initialized" });
18
+ }
19
+ if (!storage) {
20
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Storage is not initialized" });
21
+ }
22
+ if (!body) {
23
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Body is required" });
24
+ }
25
+ const { name, scope, page, perPage, attribute } = body;
26
+ const attributes = attribute ? Object.fromEntries(
27
+ (Array.isArray(attribute) ? attribute : [attribute]).map((attr) => {
28
+ const [key, value] = attr.split(":");
29
+ return [key, value];
30
+ })
31
+ ) : void 0;
32
+ const traces = await storage.__getTraces({
33
+ name,
34
+ scope,
35
+ page: Number(page ?? 0),
36
+ perPage: Number(perPage ?? 100),
37
+ attributes
38
+ });
39
+ return traces;
40
+ } catch (error2) {
41
+ return chunkZLBRQFDD_cjs.handleError(error2, "Error getting telemetry");
42
+ }
43
+ }
44
+ async function storeTelemetryHandler({ mastra, body }) {
45
+ try {
46
+ const storage = mastra.getStorage();
47
+ const logger = mastra.getLogger();
48
+ if (!storage) {
49
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: "Storage is not initialized" });
50
+ }
51
+ const now = /* @__PURE__ */ new Date();
52
+ const items = body?.resourceSpans?.[0]?.scopeSpans;
53
+ logger.debug("[Telemetry Handler] Received spans:", {
54
+ totalSpans: items?.reduce((acc, scope) => acc + scope.spans.length, 0) || 0,
55
+ timestamp: now.toISOString()
56
+ });
57
+ if (!items?.length) {
58
+ return {
59
+ status: "success",
60
+ message: "No spans to process",
61
+ traceCount: 0
62
+ };
63
+ }
64
+ const allSpans = items.reduce((acc, scopedSpans) => {
65
+ const { scope, spans } = scopedSpans;
66
+ for (const span of spans) {
67
+ const {
68
+ spanId,
69
+ parentSpanId,
70
+ traceId,
71
+ name,
72
+ kind,
73
+ attributes,
74
+ status,
75
+ events,
76
+ links,
77
+ startTimeUnixNano,
78
+ endTimeUnixNano,
79
+ ...rest
80
+ } = span;
81
+ const startTime = Number(BigInt(startTimeUnixNano) / 1000n);
82
+ const endTime = Number(BigInt(endTimeUnixNano) / 1000n);
83
+ acc.push({
84
+ id: spanId,
85
+ parentSpanId,
86
+ traceId,
87
+ name,
88
+ scope: scope.name,
89
+ kind,
90
+ status: JSON.stringify(status),
91
+ events: JSON.stringify(events),
92
+ links: JSON.stringify(links),
93
+ attributes: JSON.stringify(
94
+ attributes.reduce((acc2, attr) => {
95
+ const valueKey = Object.keys(attr.value)[0];
96
+ if (valueKey) {
97
+ acc2[attr.key] = attr.value[valueKey];
98
+ }
99
+ return acc2;
100
+ }, {})
101
+ ),
102
+ startTime,
103
+ endTime,
104
+ other: JSON.stringify(rest),
105
+ createdAt: now
106
+ });
107
+ }
108
+ return acc;
109
+ }, []);
110
+ return storage.__batchTraceInsert({
111
+ records: allSpans
112
+ }).then(() => {
113
+ return {
114
+ status: "success",
115
+ message: "Traces received and processed successfully",
116
+ traceCount: body.resourceSpans?.length || 0
117
+ };
118
+ }).catch(() => {
119
+ return {
120
+ status: "error",
121
+ message: "Failed to process traces",
122
+ // @ts-ignore
123
+ error: error.message
124
+ };
125
+ });
126
+ } catch (error2) {
127
+ console.error("Error processing traces:", error2);
128
+ return {
129
+ status: "error",
130
+ message: "Failed to process traces",
131
+ // @ts-ignore
132
+ error: error2.message
133
+ };
134
+ }
135
+ }
136
+
137
+ exports.getTelemetryHandler = getTelemetryHandler;
138
+ exports.storeTelemetryHandler = storeTelemetryHandler;
139
+ exports.telemetry_exports = telemetry_exports;