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