@mastra/server 0.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +44 -0
  2. package/README.md +159 -0
  3. package/dist/_tsup-dts-rollup.d.cts +592 -0
  4. package/dist/_tsup-dts-rollup.d.ts +592 -0
  5. package/dist/chunk-2FJURXCL.cjs +213 -0
  6. package/dist/chunk-2JQC6JWP.js +46 -0
  7. package/dist/chunk-2YONKUWB.js +117 -0
  8. package/dist/chunk-3AHQ5RGN.js +11 -0
  9. package/dist/chunk-3RVHWGWO.js +95 -0
  10. package/dist/chunk-4C3EPMMF.cjs +122 -0
  11. package/dist/chunk-4JINXASC.js +120 -0
  12. package/dist/chunk-5JOF627H.cjs +123 -0
  13. package/dist/chunk-5MCUD7TP.js +142 -0
  14. package/dist/chunk-5WF7YIIE.cjs +150 -0
  15. package/dist/chunk-A4DCRMIV.js +125 -0
  16. package/dist/chunk-A7DF4ETD.cjs +100 -0
  17. package/dist/chunk-FV45V6WC.cjs +43 -0
  18. package/dist/chunk-L7XE5QTW.js +16 -0
  19. package/dist/chunk-M56ECCHK.cjs +128 -0
  20. package/dist/chunk-QH6XWSXP.cjs +5579 -0
  21. package/dist/chunk-QN4KF3BH.cjs +18 -0
  22. package/dist/chunk-RBQASTUP.js +203 -0
  23. package/dist/chunk-S6GTZWMY.js +275 -0
  24. package/dist/chunk-SKKC3EDG.cjs +286 -0
  25. package/dist/chunk-TRDNDNGQ.js +40 -0
  26. package/dist/chunk-VB7KH62D.cjs +51 -0
  27. package/dist/chunk-VK6FX47H.js +5576 -0
  28. package/dist/chunk-YCJ2OJTL.cjs +129 -0
  29. package/dist/chunk-YNSGUC2O.js +116 -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 +42 -0
  76. package/dist/server/handlers/workflows.d.cts +9 -0
  77. package/dist/server/handlers/workflows.d.ts +9 -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,18 @@
1
+ 'use strict';
2
+
3
+ var chunkFV45V6WC_cjs = require('./chunk-FV45V6WC.cjs');
4
+
5
+ // src/server/handlers/utils.ts
6
+ function validateBody(body) {
7
+ const errorResponse = Object.entries(body).reduce((acc, [key, value]) => {
8
+ if (!value) {
9
+ acc[key] = `Argument "${key}" is required`;
10
+ }
11
+ return acc;
12
+ }, {});
13
+ if (Object.keys(errorResponse).length > 0) {
14
+ throw new chunkFV45V6WC_cjs.HTTPException(400, { message: Object.values(errorResponse)[0] });
15
+ }
16
+ }
17
+
18
+ exports.validateBody = validateBody;
@@ -0,0 +1,203 @@
1
+ import { validateBody } from './chunk-L7XE5QTW.js';
2
+ import { handleError } from './chunk-3AHQ5RGN.js';
3
+ import { __export, HTTPException } from './chunk-TRDNDNGQ.js';
4
+
5
+ // src/server/handlers/memory.ts
6
+ var memory_exports = {};
7
+ __export(memory_exports, {
8
+ createThreadHandler: () => createThreadHandler,
9
+ deleteThreadHandler: () => deleteThreadHandler,
10
+ getMemoryStatusHandler: () => getMemoryStatusHandler,
11
+ getMessagesHandler: () => getMessagesHandler,
12
+ getThreadByIdHandler: () => getThreadByIdHandler,
13
+ getThreadsHandler: () => getThreadsHandler,
14
+ saveMessagesHandler: () => saveMessagesHandler,
15
+ updateThreadHandler: () => updateThreadHandler
16
+ });
17
+ function getMemoryFromContext({
18
+ mastra,
19
+ agentId
20
+ }) {
21
+ const agent = agentId ? mastra.getAgent(agentId) : null;
22
+ if (agentId && !agent) {
23
+ throw new HTTPException(404, { message: "Agent not found" });
24
+ }
25
+ const memory = agent?.getMemory?.() || mastra.getMemory();
26
+ return memory;
27
+ }
28
+ async function getMemoryStatusHandler({ mastra, agentId }) {
29
+ try {
30
+ const memory = getMemoryFromContext({ mastra, agentId });
31
+ if (!memory) {
32
+ return { result: false };
33
+ }
34
+ return { result: true };
35
+ } catch (error) {
36
+ return handleError(error, "Error getting memory status");
37
+ }
38
+ }
39
+ async function getThreadsHandler({
40
+ mastra,
41
+ agentId,
42
+ resourceId
43
+ }) {
44
+ try {
45
+ const memory = getMemoryFromContext({ mastra, agentId });
46
+ if (!memory) {
47
+ throw new HTTPException(400, { message: "Memory is not initialized" });
48
+ }
49
+ validateBody({ resourceId });
50
+ const threads = await memory.getThreadsByResourceId({ resourceId });
51
+ return threads;
52
+ } catch (error) {
53
+ return handleError(error, "Error getting threads");
54
+ }
55
+ }
56
+ async function getThreadByIdHandler({
57
+ mastra,
58
+ agentId,
59
+ threadId
60
+ }) {
61
+ try {
62
+ validateBody({ threadId });
63
+ const memory = getMemoryFromContext({ mastra, agentId });
64
+ if (!memory) {
65
+ throw new HTTPException(400, { message: "Memory is not initialized" });
66
+ }
67
+ const thread = await memory.getThreadById({ threadId });
68
+ if (!thread) {
69
+ throw new HTTPException(404, { message: "Thread not found" });
70
+ }
71
+ return thread;
72
+ } catch (error) {
73
+ return handleError(error, "Error getting thread");
74
+ }
75
+ }
76
+ async function saveMessagesHandler({
77
+ mastra,
78
+ agentId,
79
+ body
80
+ }) {
81
+ try {
82
+ const memory = getMemoryFromContext({ mastra, agentId });
83
+ if (!memory) {
84
+ throw new HTTPException(400, { message: "Memory is not initialized" });
85
+ }
86
+ if (!body?.messages) {
87
+ throw new HTTPException(400, { message: "Messages are required" });
88
+ }
89
+ if (!Array.isArray(body.messages)) {
90
+ throw new HTTPException(400, { message: "Messages should be an array" });
91
+ }
92
+ const processedMessages = body.messages.map((message) => ({
93
+ ...message,
94
+ id: memory.generateId(),
95
+ createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
96
+ }));
97
+ const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
98
+ return result;
99
+ } catch (error) {
100
+ return handleError(error, "Error saving messages");
101
+ }
102
+ }
103
+ async function createThreadHandler({
104
+ mastra,
105
+ agentId,
106
+ body
107
+ }) {
108
+ try {
109
+ const memory = getMemoryFromContext({ mastra, agentId });
110
+ if (!memory) {
111
+ throw new HTTPException(400, { message: "Memory is not initialized" });
112
+ }
113
+ validateBody({ resourceId: body?.resourceId });
114
+ const result = await memory.createThread({
115
+ resourceId: body?.resourceId,
116
+ title: body?.title,
117
+ metadata: body?.metadata,
118
+ threadId: body?.threadId
119
+ });
120
+ return result;
121
+ } catch (error) {
122
+ return handleError(error, "Error saving thread to memory");
123
+ }
124
+ }
125
+ async function updateThreadHandler({
126
+ mastra,
127
+ agentId,
128
+ threadId,
129
+ body
130
+ }) {
131
+ try {
132
+ const memory = getMemoryFromContext({ mastra, agentId });
133
+ if (!body) {
134
+ throw new HTTPException(400, { message: "Body is required" });
135
+ }
136
+ const { title, metadata, resourceId } = body;
137
+ const updatedAt = /* @__PURE__ */ new Date();
138
+ validateBody({ threadId });
139
+ if (!memory) {
140
+ throw new HTTPException(400, { message: "Memory is not initialized" });
141
+ }
142
+ const thread = await memory.getThreadById({ threadId });
143
+ if (!thread) {
144
+ throw new HTTPException(404, { message: "Thread not found" });
145
+ }
146
+ const updatedThread = {
147
+ ...thread,
148
+ title: title || thread.title,
149
+ metadata: metadata || thread.metadata,
150
+ resourceId: resourceId || thread.resourceId,
151
+ createdAt: thread.createdAt,
152
+ updatedAt
153
+ };
154
+ const result = await memory.saveThread({ thread: updatedThread });
155
+ return result;
156
+ } catch (error) {
157
+ return handleError(error, "Error updating thread");
158
+ }
159
+ }
160
+ async function deleteThreadHandler({
161
+ mastra,
162
+ agentId,
163
+ threadId
164
+ }) {
165
+ try {
166
+ validateBody({ threadId });
167
+ const memory = getMemoryFromContext({ mastra, agentId });
168
+ if (!memory) {
169
+ throw new HTTPException(400, { message: "Memory is not initialized" });
170
+ }
171
+ const thread = await memory.getThreadById({ threadId });
172
+ if (!thread) {
173
+ throw new HTTPException(404, { message: "Thread not found" });
174
+ }
175
+ await memory.deleteThread(threadId);
176
+ return { result: "Thread deleted" };
177
+ } catch (error) {
178
+ return handleError(error, "Error deleting thread");
179
+ }
180
+ }
181
+ async function getMessagesHandler({
182
+ mastra,
183
+ agentId,
184
+ threadId
185
+ }) {
186
+ try {
187
+ validateBody({ threadId });
188
+ const memory = getMemoryFromContext({ mastra, agentId });
189
+ if (!memory) {
190
+ throw new HTTPException(400, { message: "Memory is not initialized" });
191
+ }
192
+ const thread = await memory.getThreadById({ threadId });
193
+ if (!thread) {
194
+ throw new HTTPException(404, { message: "Thread not found" });
195
+ }
196
+ const result = await memory.query({ threadId });
197
+ return result;
198
+ } catch (error) {
199
+ return handleError(error, "Error getting messages");
200
+ }
201
+ }
202
+
203
+ export { createThreadHandler, deleteThreadHandler, getMemoryStatusHandler, getMessagesHandler, getThreadByIdHandler, getThreadsHandler, memory_exports, saveMessagesHandler, updateThreadHandler };
@@ -0,0 +1,275 @@
1
+ import { stringify, esm_default } from './chunk-VK6FX47H.js';
2
+ import { handleError } from './chunk-3AHQ5RGN.js';
3
+ import { __export, HTTPException } from './chunk-TRDNDNGQ.js';
4
+ import { ReadableStream } from 'node:stream/web';
5
+
6
+ // src/server/handlers/workflows.ts
7
+ var workflows_exports = {};
8
+ __export(workflows_exports, {
9
+ createRunHandler: () => createRunHandler,
10
+ getWorkflowByIdHandler: () => getWorkflowByIdHandler,
11
+ getWorkflowRunHandler: () => getWorkflowRunHandler,
12
+ getWorkflowsHandler: () => getWorkflowsHandler,
13
+ resumeAsyncWorkflowHandler: () => resumeAsyncWorkflowHandler,
14
+ resumeWorkflowHandler: () => resumeWorkflowHandler,
15
+ startAsyncWorkflowHandler: () => startAsyncWorkflowHandler,
16
+ startWorkflowRunHandler: () => startWorkflowRunHandler,
17
+ watchWorkflowHandler: () => watchWorkflowHandler
18
+ });
19
+ async function getWorkflowsHandler({ mastra }) {
20
+ try {
21
+ const workflows = mastra.getWorkflows({ serialized: false });
22
+ const _workflows = Object.entries(workflows).reduce((acc, [key, workflow]) => {
23
+ acc[key] = {
24
+ stepGraph: workflow.stepGraph,
25
+ stepSubscriberGraph: workflow.stepSubscriberGraph,
26
+ serializedStepGraph: workflow.serializedStepGraph,
27
+ serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
28
+ name: workflow.name,
29
+ triggerSchema: workflow.triggerSchema ? stringify(esm_default(workflow.triggerSchema)) : void 0,
30
+ steps: Object.entries(workflow.steps).reduce((acc2, [key2, step]) => {
31
+ const _step = step;
32
+ acc2[key2] = {
33
+ ..._step,
34
+ inputSchema: _step.inputSchema ? stringify(esm_default(_step.inputSchema)) : void 0,
35
+ outputSchema: _step.outputSchema ? stringify(esm_default(_step.outputSchema)) : void 0
36
+ };
37
+ return acc2;
38
+ }, {})
39
+ };
40
+ return acc;
41
+ }, {});
42
+ return _workflows;
43
+ } catch (error) {
44
+ throw new HTTPException(500, { message: error?.message || "Error getting workflows" });
45
+ }
46
+ }
47
+ async function getWorkflowByIdHandler({ mastra, workflowId }) {
48
+ try {
49
+ if (!workflowId) {
50
+ throw new HTTPException(400, { message: "Workflow ID is required" });
51
+ }
52
+ const workflow = mastra.getWorkflow(workflowId);
53
+ if (!workflow) {
54
+ throw new HTTPException(404, { message: "Workflow not found" });
55
+ }
56
+ return {
57
+ stepGraph: workflow.stepGraph,
58
+ stepSubscriberGraph: workflow.stepSubscriberGraph,
59
+ serializedStepGraph: workflow.serializedStepGraph,
60
+ serializedStepSubscriberGraph: workflow.serializedStepSubscriberGraph,
61
+ name: workflow.name,
62
+ triggerSchema: workflow.triggerSchema ? stringify(esm_default(workflow.triggerSchema)) : void 0,
63
+ steps: Object.entries(workflow.steps).reduce((acc, [key, step]) => {
64
+ const _step = step;
65
+ acc[key] = {
66
+ ..._step,
67
+ inputSchema: _step.inputSchema ? stringify(esm_default(_step.inputSchema)) : void 0,
68
+ outputSchema: _step.outputSchema ? stringify(esm_default(_step.outputSchema)) : void 0
69
+ };
70
+ return acc;
71
+ }, {})
72
+ };
73
+ } catch (error) {
74
+ throw new HTTPException(500, { message: error?.message || "Error getting workflow" });
75
+ }
76
+ }
77
+ async function startAsyncWorkflowHandler({
78
+ mastra,
79
+ workflowId,
80
+ runId,
81
+ triggerData
82
+ }) {
83
+ try {
84
+ if (!workflowId) {
85
+ throw new HTTPException(400, { message: "Workflow ID is required" });
86
+ }
87
+ const workflow = mastra.getWorkflow(workflowId);
88
+ if (!runId) {
89
+ throw new HTTPException(400, { message: "runId required to start run" });
90
+ }
91
+ if (!workflow) {
92
+ throw new HTTPException(404, { message: "Workflow not found" });
93
+ }
94
+ const run = workflow.getRun(runId);
95
+ if (!run) {
96
+ throw new HTTPException(404, { message: "Workflow run not found" });
97
+ }
98
+ const result = await run.start({
99
+ triggerData
100
+ });
101
+ return result;
102
+ } catch (error) {
103
+ throw new HTTPException(500, { message: error?.message || "Error executing workflow" });
104
+ }
105
+ }
106
+ async function getWorkflowRunHandler({
107
+ mastra,
108
+ workflowId,
109
+ runId
110
+ }) {
111
+ try {
112
+ if (!workflowId) {
113
+ throw new HTTPException(400, { message: "Workflow ID is required" });
114
+ }
115
+ if (!runId) {
116
+ throw new HTTPException(400, { message: "Run ID is required" });
117
+ }
118
+ const workflow = mastra.getWorkflow(workflowId);
119
+ if (!workflow) {
120
+ throw new HTTPException(404, { message: "Workflow not found" });
121
+ }
122
+ const run = workflow.getRun(runId);
123
+ if (!run) {
124
+ throw new HTTPException(404, { message: "Workflow run not found" });
125
+ }
126
+ return run;
127
+ } catch (error) {
128
+ throw new HTTPException(500, { message: error?.message || "Error getting workflow run" });
129
+ }
130
+ }
131
+ async function createRunHandler({
132
+ mastra,
133
+ workflowId,
134
+ runId: prevRunId
135
+ }) {
136
+ try {
137
+ if (!workflowId) {
138
+ throw new HTTPException(400, { message: "Workflow ID is required" });
139
+ }
140
+ const workflow = mastra.getWorkflow(workflowId);
141
+ if (!workflow) {
142
+ throw new HTTPException(404, { message: "Workflow not found" });
143
+ }
144
+ const { runId } = workflow.createRun({ runId: prevRunId });
145
+ return { runId };
146
+ } catch (error) {
147
+ throw new HTTPException(500, { message: error?.message || "Error creating workflow run" });
148
+ }
149
+ }
150
+ async function startWorkflowRunHandler({
151
+ mastra,
152
+ workflowId,
153
+ runId,
154
+ triggerData
155
+ }) {
156
+ try {
157
+ if (!workflowId) {
158
+ throw new HTTPException(400, { message: "Workflow ID is required" });
159
+ }
160
+ if (!runId) {
161
+ throw new HTTPException(400, { message: "runId required to start run" });
162
+ }
163
+ const workflow = mastra.getWorkflow(workflowId);
164
+ const run = workflow.getRun(runId);
165
+ if (!run) {
166
+ throw new HTTPException(404, { message: "Workflow run not found" });
167
+ }
168
+ await run.start({
169
+ triggerData
170
+ });
171
+ return { message: "Workflow run started" };
172
+ } catch (e) {
173
+ return handleError(e, "Error starting workflow run");
174
+ }
175
+ }
176
+ async function watchWorkflowHandler({
177
+ mastra,
178
+ workflowId,
179
+ runId
180
+ }) {
181
+ try {
182
+ if (!workflowId) {
183
+ throw new HTTPException(400, { message: "Workflow ID is required" });
184
+ }
185
+ if (!runId) {
186
+ throw new HTTPException(400, { message: "runId required to watch workflow" });
187
+ }
188
+ const workflow = mastra.getWorkflow(workflowId);
189
+ const run = workflow.getRun(runId);
190
+ if (!run) {
191
+ throw new HTTPException(404, { message: "Workflow run not found" });
192
+ }
193
+ let unwatch;
194
+ let asyncRef = null;
195
+ const stream = new ReadableStream({
196
+ start(controller) {
197
+ unwatch = run.watch(({ activePaths, runId: runId2, timestamp, results }) => {
198
+ const activePathsObj = Object.fromEntries(activePaths);
199
+ controller.enqueue(JSON.stringify({ activePaths: activePathsObj, runId: runId2, timestamp, results }));
200
+ if (asyncRef) {
201
+ clearImmediate(asyncRef);
202
+ asyncRef = null;
203
+ }
204
+ asyncRef = setImmediate(() => {
205
+ if (!workflow.getRun(runId2)) {
206
+ controller.close();
207
+ }
208
+ });
209
+ });
210
+ },
211
+ cancel() {
212
+ unwatch?.();
213
+ }
214
+ });
215
+ return stream;
216
+ } catch (error) {
217
+ return handleError(error, "Error watching workflow");
218
+ }
219
+ }
220
+ async function resumeAsyncWorkflowHandler({
221
+ mastra,
222
+ workflowId,
223
+ runId,
224
+ body
225
+ }) {
226
+ try {
227
+ if (!workflowId) {
228
+ throw new HTTPException(400, { message: "Workflow ID is required" });
229
+ }
230
+ if (!runId) {
231
+ throw new HTTPException(400, { message: "runId required to resume workflow" });
232
+ }
233
+ const workflow = mastra.getWorkflow(workflowId);
234
+ const run = workflow.getRun(runId);
235
+ if (!run) {
236
+ throw new HTTPException(404, { message: "Workflow run not found" });
237
+ }
238
+ const result = await run.resume({
239
+ stepId: body.stepId,
240
+ context: body.context
241
+ });
242
+ return result;
243
+ } catch (error) {
244
+ return handleError(error, "Error resuming workflow step");
245
+ }
246
+ }
247
+ async function resumeWorkflowHandler({
248
+ mastra,
249
+ workflowId,
250
+ runId,
251
+ body
252
+ }) {
253
+ try {
254
+ if (!workflowId) {
255
+ throw new HTTPException(400, { message: "Workflow ID is required" });
256
+ }
257
+ if (!runId) {
258
+ throw new HTTPException(400, { message: "runId required to resume workflow" });
259
+ }
260
+ const workflow = mastra.getWorkflow(workflowId);
261
+ const run = workflow.getRun(runId);
262
+ if (!run) {
263
+ throw new HTTPException(404, { message: "Workflow run not found" });
264
+ }
265
+ await run.resume({
266
+ stepId: body.stepId,
267
+ context: body.context
268
+ });
269
+ return { message: "Workflow run resumed" };
270
+ } catch (error) {
271
+ return handleError(error, "Error resuming workflow");
272
+ }
273
+ }
274
+
275
+ export { createRunHandler, getWorkflowByIdHandler, getWorkflowRunHandler, getWorkflowsHandler, resumeAsyncWorkflowHandler, resumeWorkflowHandler, startAsyncWorkflowHandler, startWorkflowRunHandler, watchWorkflowHandler, workflows_exports };