@mastra/server 0.10.13 → 0.10.15-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.
- package/dist/_tsup-dts-rollup.d.cts +1107 -0
- package/dist/_tsup-dts-rollup.d.ts +1107 -0
- package/dist/chunk-2KZFMI6P.cjs +37 -0
- package/dist/chunk-2SLFAFTR.cjs +148 -0
- package/dist/chunk-3CNDE7QY.cjs +308 -0
- package/dist/chunk-4D66QEKC.js +81 -0
- package/dist/chunk-5PQQ42EZ.js +320 -0
- package/dist/chunk-75ZPJI57.cjs +9 -0
- package/dist/chunk-7TP2LX5L.cjs +129 -0
- package/dist/chunk-B2PAS2IB.cjs +514 -0
- package/dist/chunk-BK4XT6EG.js +270 -0
- package/dist/chunk-CCGRCYWJ.cjs +18 -0
- package/dist/chunk-CRCR3ZUK.js +142 -0
- package/dist/chunk-FRVBFMO2.cjs +128 -0
- package/dist/chunk-G7KH752Y.cjs +145 -0
- package/dist/chunk-GHC4YV6R.js +504 -0
- package/dist/chunk-H7DMHBKY.js +498 -0
- package/dist/chunk-JMLYCXMK.cjs +332 -0
- package/dist/chunk-KHZKYUNR.js +112 -0
- package/dist/chunk-KOHWJYJT.js +296 -0
- package/dist/chunk-LCM566I4.js +35 -0
- package/dist/chunk-LF7P5PLR.js +14 -0
- package/dist/chunk-LI436ITD.cjs +2042 -0
- package/dist/chunk-LRCAAFUA.js +122 -0
- package/dist/chunk-LZ3VJXSO.cjs +278 -0
- package/dist/chunk-MEGCYGBU.js +2039 -0
- package/dist/chunk-MLKGABMK.js +7 -0
- package/dist/chunk-PZQDCRPV.cjs +16 -0
- package/dist/chunk-QGX47B5D.cjs +86 -0
- package/dist/chunk-RG473F6Y.js +141 -0
- package/dist/chunk-RHSWAXKB.cjs +118 -0
- package/dist/chunk-RSEO4XPX.js +16 -0
- package/dist/chunk-SDPGVWQJ.cjs +512 -0
- package/dist/chunk-WNVFNNWN.js +121 -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/a2a.cjs +30 -0
- package/dist/server/handlers/a2a.d.cts +6 -0
- package/dist/server/handlers/a2a.d.ts +6 -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/legacyWorkflows.cjs +46 -0
- package/dist/server/handlers/legacyWorkflows.d.cts +10 -0
- package/dist/server/handlers/legacyWorkflows.d.ts +10 -0
- package/dist/server/handlers/legacyWorkflows.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 +46 -0
- package/dist/server/handlers/memory.d.cts +10 -0
- package/dist/server/handlers/memory.d.ts +10 -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/vNextNetwork.cjs +218 -0
- package/dist/server/handlers/vNextNetwork.d.cts +6 -0
- package/dist/server/handlers/vNextNetwork.d.ts +6 -0
- package/dist/server/handlers/vNextNetwork.js +211 -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 +22 -0
- package/dist/server/handlers/voice.d.cts +4 -0
- package/dist/server/handlers/voice.d.ts +4 -0
- package/dist/server/handlers/voice.js +1 -0
- package/dist/server/handlers/workflows.cjs +62 -0
- package/dist/server/handlers/workflows.d.cts +14 -0
- package/dist/server/handlers/workflows.d.ts +14 -0
- package/dist/server/handlers/workflows.js +1 -0
- package/dist/server/handlers.cjs +60 -0
- package/dist/server/handlers.d.cts +11 -0
- package/dist/server/handlers.d.ts +11 -0
- package/dist/server/handlers.js +11 -0
- package/package.json +3 -3
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/server/http-exception.ts
|
|
4
|
+
var HTTPException = class extends Error {
|
|
5
|
+
res;
|
|
6
|
+
status;
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of `HTTPException`.
|
|
9
|
+
* @param status - HTTP status code for the exception. Defaults to 500.
|
|
10
|
+
* @param options - Additional options for the exception.
|
|
11
|
+
*/
|
|
12
|
+
constructor(status = 500, options) {
|
|
13
|
+
super(options?.message, { cause: options?.cause });
|
|
14
|
+
this.res = options?.res;
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.stack = options?.stack || this.stack;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Returns the response object associated with the exception.
|
|
20
|
+
* If a response object is not provided, a new response is created with the error message and status code.
|
|
21
|
+
* @returns The response object.
|
|
22
|
+
*/
|
|
23
|
+
getResponse() {
|
|
24
|
+
if (this.res) {
|
|
25
|
+
const newResponse = new Response(this.res.body, {
|
|
26
|
+
status: this.status,
|
|
27
|
+
headers: this.res.headers
|
|
28
|
+
});
|
|
29
|
+
return newResponse;
|
|
30
|
+
}
|
|
31
|
+
return new Response(this.message, {
|
|
32
|
+
status: this.status
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
exports.HTTPException = HTTPException;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkCCGRCYWJ_cjs = require('./chunk-CCGRCYWJ.cjs');
|
|
4
|
+
var chunkPZQDCRPV_cjs = require('./chunk-PZQDCRPV.cjs');
|
|
5
|
+
var chunk2KZFMI6P_cjs = require('./chunk-2KZFMI6P.cjs');
|
|
6
|
+
var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
|
|
7
|
+
|
|
8
|
+
// src/server/handlers/network.ts
|
|
9
|
+
var network_exports = {};
|
|
10
|
+
chunk75ZPJI57_cjs.__export(network_exports, {
|
|
11
|
+
generateHandler: () => generateHandler,
|
|
12
|
+
getNetworkByIdHandler: () => getNetworkByIdHandler,
|
|
13
|
+
getNetworksHandler: () => getNetworksHandler,
|
|
14
|
+
streamGenerateHandler: () => streamGenerateHandler
|
|
15
|
+
});
|
|
16
|
+
async function getNetworksHandler({
|
|
17
|
+
mastra,
|
|
18
|
+
runtimeContext
|
|
19
|
+
}) {
|
|
20
|
+
try {
|
|
21
|
+
const networks = mastra.getNetworks();
|
|
22
|
+
const serializedNetworks = await Promise.all(
|
|
23
|
+
networks.map(async (network) => {
|
|
24
|
+
const routingAgent = network.getRoutingAgent();
|
|
25
|
+
const routingLLM = await routingAgent.getLLM({ runtimeContext });
|
|
26
|
+
const agents = network.getAgents();
|
|
27
|
+
return {
|
|
28
|
+
id: network.formatAgentId(routingAgent.name),
|
|
29
|
+
name: routingAgent.name,
|
|
30
|
+
instructions: routingAgent.instructions,
|
|
31
|
+
agents: await Promise.all(
|
|
32
|
+
agents.map(async (agent) => {
|
|
33
|
+
const llm = await agent.getLLM({ runtimeContext });
|
|
34
|
+
return {
|
|
35
|
+
name: agent.name,
|
|
36
|
+
provider: llm?.getProvider(),
|
|
37
|
+
modelId: llm?.getModelId()
|
|
38
|
+
};
|
|
39
|
+
})
|
|
40
|
+
),
|
|
41
|
+
routingModel: {
|
|
42
|
+
provider: routingLLM?.getProvider(),
|
|
43
|
+
modelId: routingLLM?.getModelId()
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
return serializedNetworks;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting networks");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function getNetworkByIdHandler({
|
|
54
|
+
mastra,
|
|
55
|
+
networkId,
|
|
56
|
+
runtimeContext
|
|
57
|
+
}) {
|
|
58
|
+
try {
|
|
59
|
+
const networks = mastra.getNetworks();
|
|
60
|
+
const network = networks.find((network2) => {
|
|
61
|
+
const routingAgent2 = network2.getRoutingAgent();
|
|
62
|
+
return network2.formatAgentId(routingAgent2.name) === networkId;
|
|
63
|
+
});
|
|
64
|
+
if (!network) {
|
|
65
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Network not found" });
|
|
66
|
+
}
|
|
67
|
+
const routingAgent = network.getRoutingAgent();
|
|
68
|
+
const routingLLM = await routingAgent.getLLM({ runtimeContext });
|
|
69
|
+
const agents = network.getAgents();
|
|
70
|
+
const serializedNetwork = {
|
|
71
|
+
id: network.formatAgentId(routingAgent.name),
|
|
72
|
+
name: routingAgent.name,
|
|
73
|
+
instructions: routingAgent.instructions,
|
|
74
|
+
agents: await Promise.all(
|
|
75
|
+
agents.map(async (agent) => {
|
|
76
|
+
const llm = await agent.getLLM({ runtimeContext });
|
|
77
|
+
return {
|
|
78
|
+
name: agent.name,
|
|
79
|
+
provider: llm?.getProvider(),
|
|
80
|
+
modelId: llm?.getModelId()
|
|
81
|
+
};
|
|
82
|
+
})
|
|
83
|
+
),
|
|
84
|
+
routingModel: {
|
|
85
|
+
provider: routingLLM?.getProvider(),
|
|
86
|
+
modelId: routingLLM?.getModelId()
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return serializedNetwork;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting network by ID");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function generateHandler({
|
|
95
|
+
mastra,
|
|
96
|
+
runtimeContext,
|
|
97
|
+
networkId,
|
|
98
|
+
body
|
|
99
|
+
}) {
|
|
100
|
+
try {
|
|
101
|
+
const network = mastra.getNetwork(networkId);
|
|
102
|
+
if (!network) {
|
|
103
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Network not found" });
|
|
104
|
+
}
|
|
105
|
+
chunkCCGRCYWJ_cjs.validateBody({ messages: body.messages });
|
|
106
|
+
const { messages, ...rest } = body;
|
|
107
|
+
const result = await network.generate(messages, { ...rest, runtimeContext });
|
|
108
|
+
return result;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error generating from network");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function streamGenerateHandler({
|
|
114
|
+
mastra,
|
|
115
|
+
networkId,
|
|
116
|
+
body,
|
|
117
|
+
runtimeContext
|
|
118
|
+
}) {
|
|
119
|
+
try {
|
|
120
|
+
const network = mastra.getNetwork(networkId);
|
|
121
|
+
if (!network) {
|
|
122
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Network not found" });
|
|
123
|
+
}
|
|
124
|
+
chunkCCGRCYWJ_cjs.validateBody({ messages: body.messages });
|
|
125
|
+
const { messages, output, ...rest } = body;
|
|
126
|
+
const streamResult = await network.stream(messages, {
|
|
127
|
+
output,
|
|
128
|
+
...rest,
|
|
129
|
+
runtimeContext
|
|
130
|
+
});
|
|
131
|
+
const streamResponse = output ? streamResult.toTextStreamResponse() : streamResult.toDataStreamResponse({
|
|
132
|
+
sendUsage: true,
|
|
133
|
+
sendReasoning: true,
|
|
134
|
+
getErrorMessage: (error) => {
|
|
135
|
+
return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
return streamResponse;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error streaming from network");
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
exports.generateHandler = generateHandler;
|
|
145
|
+
exports.getNetworkByIdHandler = getNetworkByIdHandler;
|
|
146
|
+
exports.getNetworksHandler = getNetworksHandler;
|
|
147
|
+
exports.network_exports = network_exports;
|
|
148
|
+
exports.streamGenerateHandler = streamGenerateHandler;
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkCCGRCYWJ_cjs = require('./chunk-CCGRCYWJ.cjs');
|
|
4
|
+
var chunkPZQDCRPV_cjs = require('./chunk-PZQDCRPV.cjs');
|
|
5
|
+
var chunk2KZFMI6P_cjs = require('./chunk-2KZFMI6P.cjs');
|
|
6
|
+
var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
|
|
7
|
+
var core = require('@mastra/core');
|
|
8
|
+
|
|
9
|
+
// src/server/handlers/memory.ts
|
|
10
|
+
var memory_exports = {};
|
|
11
|
+
chunk75ZPJI57_cjs.__export(memory_exports, {
|
|
12
|
+
createThreadHandler: () => createThreadHandler,
|
|
13
|
+
deleteThreadHandler: () => deleteThreadHandler,
|
|
14
|
+
getMemoryStatusHandler: () => getMemoryStatusHandler,
|
|
15
|
+
getMessagesHandler: () => getMessagesHandler,
|
|
16
|
+
getThreadByIdHandler: () => getThreadByIdHandler,
|
|
17
|
+
getThreadsHandler: () => getThreadsHandler,
|
|
18
|
+
getWorkingMemoryHandler: () => getWorkingMemoryHandler,
|
|
19
|
+
saveMessagesHandler: () => saveMessagesHandler,
|
|
20
|
+
updateThreadHandler: () => updateThreadHandler,
|
|
21
|
+
updateWorkingMemoryHandler: () => updateWorkingMemoryHandler
|
|
22
|
+
});
|
|
23
|
+
async function getMemoryFromContext({
|
|
24
|
+
mastra,
|
|
25
|
+
agentId,
|
|
26
|
+
networkId,
|
|
27
|
+
runtimeContext
|
|
28
|
+
}) {
|
|
29
|
+
const agent = agentId ? mastra.getAgent(agentId) : null;
|
|
30
|
+
if (agentId && !agent) {
|
|
31
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Agent not found" });
|
|
32
|
+
}
|
|
33
|
+
const network = networkId ? mastra.vnext_getNetwork(networkId) : null;
|
|
34
|
+
if (networkId && !network) {
|
|
35
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Network not found" });
|
|
36
|
+
}
|
|
37
|
+
if (agent) {
|
|
38
|
+
return agent?.getMemory() || mastra.getMemory();
|
|
39
|
+
}
|
|
40
|
+
if (network) {
|
|
41
|
+
return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
|
|
42
|
+
}
|
|
43
|
+
return mastra.getMemory();
|
|
44
|
+
}
|
|
45
|
+
async function getMemoryStatusHandler({
|
|
46
|
+
mastra,
|
|
47
|
+
agentId,
|
|
48
|
+
networkId,
|
|
49
|
+
runtimeContext
|
|
50
|
+
}) {
|
|
51
|
+
try {
|
|
52
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
53
|
+
if (!memory) {
|
|
54
|
+
return { result: false };
|
|
55
|
+
}
|
|
56
|
+
return { result: true };
|
|
57
|
+
} catch (error) {
|
|
58
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting memory status");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function getThreadsHandler({
|
|
62
|
+
mastra,
|
|
63
|
+
agentId,
|
|
64
|
+
resourceId,
|
|
65
|
+
networkId,
|
|
66
|
+
runtimeContext
|
|
67
|
+
}) {
|
|
68
|
+
try {
|
|
69
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
70
|
+
if (!memory) {
|
|
71
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
72
|
+
}
|
|
73
|
+
chunkCCGRCYWJ_cjs.validateBody({ resourceId });
|
|
74
|
+
const threads = await memory.getThreadsByResourceId({ resourceId });
|
|
75
|
+
return threads;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting threads");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function getThreadByIdHandler({
|
|
81
|
+
mastra,
|
|
82
|
+
agentId,
|
|
83
|
+
threadId,
|
|
84
|
+
networkId,
|
|
85
|
+
runtimeContext
|
|
86
|
+
}) {
|
|
87
|
+
try {
|
|
88
|
+
chunkCCGRCYWJ_cjs.validateBody({ threadId });
|
|
89
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
90
|
+
if (!memory) {
|
|
91
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
92
|
+
}
|
|
93
|
+
const thread = await memory.getThreadById({ threadId });
|
|
94
|
+
if (!thread) {
|
|
95
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
|
|
96
|
+
}
|
|
97
|
+
return thread;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting thread");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function saveMessagesHandler({
|
|
103
|
+
mastra,
|
|
104
|
+
agentId,
|
|
105
|
+
body,
|
|
106
|
+
networkId,
|
|
107
|
+
runtimeContext
|
|
108
|
+
}) {
|
|
109
|
+
try {
|
|
110
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
111
|
+
if (!memory) {
|
|
112
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
113
|
+
}
|
|
114
|
+
if (!body?.messages) {
|
|
115
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Messages are required" });
|
|
116
|
+
}
|
|
117
|
+
if (!Array.isArray(body.messages)) {
|
|
118
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Messages should be an array" });
|
|
119
|
+
}
|
|
120
|
+
const processedMessages = body.messages.map((message) => ({
|
|
121
|
+
...message,
|
|
122
|
+
id: memory.generateId(),
|
|
123
|
+
createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
|
|
124
|
+
}));
|
|
125
|
+
const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
|
|
126
|
+
return result;
|
|
127
|
+
} catch (error) {
|
|
128
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error saving messages");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async function createThreadHandler({
|
|
132
|
+
mastra,
|
|
133
|
+
agentId,
|
|
134
|
+
body,
|
|
135
|
+
networkId,
|
|
136
|
+
runtimeContext
|
|
137
|
+
}) {
|
|
138
|
+
try {
|
|
139
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
140
|
+
if (!memory) {
|
|
141
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
142
|
+
}
|
|
143
|
+
chunkCCGRCYWJ_cjs.validateBody({ resourceId: body?.resourceId });
|
|
144
|
+
const result = await memory.createThread({
|
|
145
|
+
resourceId: body?.resourceId,
|
|
146
|
+
title: body?.title,
|
|
147
|
+
metadata: body?.metadata,
|
|
148
|
+
threadId: body?.threadId
|
|
149
|
+
});
|
|
150
|
+
return result;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error saving thread to memory");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async function updateThreadHandler({
|
|
156
|
+
mastra,
|
|
157
|
+
agentId,
|
|
158
|
+
threadId,
|
|
159
|
+
body,
|
|
160
|
+
networkId,
|
|
161
|
+
runtimeContext
|
|
162
|
+
}) {
|
|
163
|
+
try {
|
|
164
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
165
|
+
if (!body) {
|
|
166
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Body is required" });
|
|
167
|
+
}
|
|
168
|
+
const { title, metadata, resourceId } = body;
|
|
169
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
170
|
+
chunkCCGRCYWJ_cjs.validateBody({ threadId });
|
|
171
|
+
if (!memory) {
|
|
172
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
173
|
+
}
|
|
174
|
+
const thread = await memory.getThreadById({ threadId });
|
|
175
|
+
if (!thread) {
|
|
176
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
|
|
177
|
+
}
|
|
178
|
+
const updatedThread = {
|
|
179
|
+
...thread,
|
|
180
|
+
title: title || thread.title,
|
|
181
|
+
metadata: metadata || thread.metadata,
|
|
182
|
+
resourceId: resourceId || thread.resourceId,
|
|
183
|
+
createdAt: thread.createdAt,
|
|
184
|
+
updatedAt
|
|
185
|
+
};
|
|
186
|
+
const result = await memory.saveThread({ thread: updatedThread });
|
|
187
|
+
return result;
|
|
188
|
+
} catch (error) {
|
|
189
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error updating thread");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async function deleteThreadHandler({
|
|
193
|
+
mastra,
|
|
194
|
+
agentId,
|
|
195
|
+
threadId,
|
|
196
|
+
networkId,
|
|
197
|
+
runtimeContext
|
|
198
|
+
}) {
|
|
199
|
+
try {
|
|
200
|
+
chunkCCGRCYWJ_cjs.validateBody({ threadId });
|
|
201
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
202
|
+
if (!memory) {
|
|
203
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
204
|
+
}
|
|
205
|
+
const thread = await memory.getThreadById({ threadId });
|
|
206
|
+
if (!thread) {
|
|
207
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
|
|
208
|
+
}
|
|
209
|
+
await memory.deleteThread(threadId);
|
|
210
|
+
return { result: "Thread deleted" };
|
|
211
|
+
} catch (error) {
|
|
212
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error deleting thread");
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
async function getMessagesHandler({
|
|
216
|
+
mastra,
|
|
217
|
+
agentId,
|
|
218
|
+
threadId,
|
|
219
|
+
limit,
|
|
220
|
+
networkId,
|
|
221
|
+
runtimeContext
|
|
222
|
+
}) {
|
|
223
|
+
if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
|
|
224
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Invalid limit: must be a positive integer" });
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
chunkCCGRCYWJ_cjs.validateBody({ threadId });
|
|
228
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
229
|
+
if (!memory) {
|
|
230
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
231
|
+
}
|
|
232
|
+
const thread = await memory.getThreadById({ threadId });
|
|
233
|
+
if (!thread) {
|
|
234
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
|
|
235
|
+
}
|
|
236
|
+
const result = await memory.query({
|
|
237
|
+
threadId,
|
|
238
|
+
...limit && { selectBy: { last: limit } }
|
|
239
|
+
});
|
|
240
|
+
return { messages: result.messages, uiMessages: result.uiMessages };
|
|
241
|
+
} catch (error) {
|
|
242
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting messages");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
async function getWorkingMemoryHandler({
|
|
246
|
+
mastra,
|
|
247
|
+
agentId,
|
|
248
|
+
threadId,
|
|
249
|
+
resourceId,
|
|
250
|
+
networkId,
|
|
251
|
+
runtimeContext,
|
|
252
|
+
memoryConfig
|
|
253
|
+
}) {
|
|
254
|
+
try {
|
|
255
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
256
|
+
chunkCCGRCYWJ_cjs.validateBody({ threadId });
|
|
257
|
+
if (!memory) {
|
|
258
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
259
|
+
}
|
|
260
|
+
const thread = await memory.getThreadById({ threadId });
|
|
261
|
+
const threadExists = !!thread;
|
|
262
|
+
const template = await memory.getWorkingMemoryTemplate({ memoryConfig });
|
|
263
|
+
const workingMemoryTemplate = template?.format === "json" ? { ...template, content: JSON.stringify(core.generateEmptyFromSchema(template.content)) } : template;
|
|
264
|
+
const workingMemory = await memory.getWorkingMemory({ threadId, resourceId, memoryConfig });
|
|
265
|
+
const config = memory.getMergedThreadConfig(memoryConfig || {});
|
|
266
|
+
const source = config.workingMemory?.scope === "resource" && resourceId ? "resource" : "thread";
|
|
267
|
+
return { workingMemory, source, workingMemoryTemplate, threadExists };
|
|
268
|
+
} catch (error) {
|
|
269
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting working memory");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async function updateWorkingMemoryHandler({
|
|
273
|
+
mastra,
|
|
274
|
+
agentId,
|
|
275
|
+
threadId,
|
|
276
|
+
body,
|
|
277
|
+
networkId,
|
|
278
|
+
runtimeContext
|
|
279
|
+
}) {
|
|
280
|
+
try {
|
|
281
|
+
chunkCCGRCYWJ_cjs.validateBody({ threadId });
|
|
282
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
283
|
+
const { resourceId, memoryConfig, workingMemory } = body;
|
|
284
|
+
if (!memory) {
|
|
285
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Memory is not initialized" });
|
|
286
|
+
}
|
|
287
|
+
const thread = await memory.getThreadById({ threadId });
|
|
288
|
+
if (!thread) {
|
|
289
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Thread not found" });
|
|
290
|
+
}
|
|
291
|
+
await memory.updateWorkingMemory({ threadId, resourceId, workingMemory, memoryConfig });
|
|
292
|
+
return { success: true };
|
|
293
|
+
} catch (error) {
|
|
294
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error updating working memory");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
exports.createThreadHandler = createThreadHandler;
|
|
299
|
+
exports.deleteThreadHandler = deleteThreadHandler;
|
|
300
|
+
exports.getMemoryStatusHandler = getMemoryStatusHandler;
|
|
301
|
+
exports.getMessagesHandler = getMessagesHandler;
|
|
302
|
+
exports.getThreadByIdHandler = getThreadByIdHandler;
|
|
303
|
+
exports.getThreadsHandler = getThreadsHandler;
|
|
304
|
+
exports.getWorkingMemoryHandler = getWorkingMemoryHandler;
|
|
305
|
+
exports.memory_exports = memory_exports;
|
|
306
|
+
exports.saveMessagesHandler = saveMessagesHandler;
|
|
307
|
+
exports.updateThreadHandler = updateThreadHandler;
|
|
308
|
+
exports.updateWorkingMemoryHandler = updateWorkingMemoryHandler;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { validateBody } from './chunk-RSEO4XPX.js';
|
|
2
|
+
import { handleError } from './chunk-LF7P5PLR.js';
|
|
3
|
+
import { __export } from './chunk-MLKGABMK.js';
|
|
4
|
+
|
|
5
|
+
// src/server/handlers/logs.ts
|
|
6
|
+
var logs_exports = {};
|
|
7
|
+
__export(logs_exports, {
|
|
8
|
+
getLogTransports: () => getLogTransports,
|
|
9
|
+
getLogsByRunIdHandler: () => getLogsByRunIdHandler,
|
|
10
|
+
getLogsHandler: () => getLogsHandler
|
|
11
|
+
});
|
|
12
|
+
async function getLogsHandler({
|
|
13
|
+
mastra,
|
|
14
|
+
transportId,
|
|
15
|
+
params
|
|
16
|
+
}) {
|
|
17
|
+
try {
|
|
18
|
+
validateBody({ transportId });
|
|
19
|
+
const { fromDate, toDate, logLevel, filters: _filters, page, perPage } = params || {};
|
|
20
|
+
const filters = _filters ? Object.fromEntries(
|
|
21
|
+
(Array.isArray(_filters) ? _filters : [_filters]).map((attr) => {
|
|
22
|
+
const [key, value] = attr.split(":");
|
|
23
|
+
return [key, value];
|
|
24
|
+
})
|
|
25
|
+
) : void 0;
|
|
26
|
+
const logs = await mastra.getLogs(transportId, {
|
|
27
|
+
fromDate,
|
|
28
|
+
toDate,
|
|
29
|
+
logLevel,
|
|
30
|
+
filters,
|
|
31
|
+
page: page ? Number(page) : void 0,
|
|
32
|
+
perPage: perPage ? Number(perPage) : void 0
|
|
33
|
+
});
|
|
34
|
+
return logs;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return handleError(error, "Error getting logs");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function getLogsByRunIdHandler({
|
|
40
|
+
mastra,
|
|
41
|
+
runId,
|
|
42
|
+
transportId,
|
|
43
|
+
params
|
|
44
|
+
}) {
|
|
45
|
+
try {
|
|
46
|
+
validateBody({ runId, transportId });
|
|
47
|
+
const { fromDate, toDate, logLevel, filters: _filters, page, perPage } = params || {};
|
|
48
|
+
const filters = _filters ? Object.fromEntries(
|
|
49
|
+
(Array.isArray(_filters) ? _filters : [_filters]).map((attr) => {
|
|
50
|
+
const [key, value] = attr.split(":");
|
|
51
|
+
return [key, value];
|
|
52
|
+
})
|
|
53
|
+
) : void 0;
|
|
54
|
+
const logs = await mastra.getLogsByRunId({
|
|
55
|
+
runId,
|
|
56
|
+
transportId,
|
|
57
|
+
fromDate,
|
|
58
|
+
toDate,
|
|
59
|
+
logLevel,
|
|
60
|
+
filters,
|
|
61
|
+
page: page ? Number(page) : void 0,
|
|
62
|
+
perPage: perPage ? Number(perPage) : void 0
|
|
63
|
+
});
|
|
64
|
+
return logs;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return handleError(error, "Error getting logs by run ID");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async function getLogTransports({ mastra }) {
|
|
70
|
+
try {
|
|
71
|
+
const logger = mastra.getLogger();
|
|
72
|
+
const transports = logger.getTransports();
|
|
73
|
+
return {
|
|
74
|
+
transports: transports ? [...transports.keys()] : []
|
|
75
|
+
};
|
|
76
|
+
} catch (error) {
|
|
77
|
+
return handleError(error, "Error getting log Transports");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { getLogTransports, getLogsByRunIdHandler, getLogsHandler, logs_exports };
|