@mastra/deployer 0.0.0-commonjs-20250227130920

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 (86) hide show
  1. package/.turbo/turbo-build.log +41 -0
  2. package/CHANGELOG.md +897 -0
  3. package/LICENSE +44 -0
  4. package/README.md +159 -0
  5. package/dist/_tsup-dts-rollup.d.cts +360 -0
  6. package/dist/_tsup-dts-rollup.d.ts +360 -0
  7. package/dist/build/analyze.cjs +367 -0
  8. package/dist/build/analyze.d.cts +1 -0
  9. package/dist/build/analyze.d.ts +1 -0
  10. package/dist/build/analyze.js +2 -0
  11. package/dist/build/bundler.cjs +353 -0
  12. package/dist/build/bundler.d.cts +2 -0
  13. package/dist/build/bundler.d.ts +2 -0
  14. package/dist/build/bundler.js +2 -0
  15. package/dist/build/index.cjs +1146 -0
  16. package/dist/build/index.d.cts +10 -0
  17. package/dist/build/index.d.ts +10 -0
  18. package/dist/build/index.js +5 -0
  19. package/dist/bundler/index.cjs +999 -0
  20. package/dist/bundler/index.d.cts +1 -0
  21. package/dist/bundler/index.d.ts +1 -0
  22. package/dist/bundler/index.js +5 -0
  23. package/dist/chunk-3ONBKVC4.js +113 -0
  24. package/dist/chunk-AXS5WSIK.js +290 -0
  25. package/dist/chunk-DTSFVNIF.js +260 -0
  26. package/dist/chunk-JMH7HCD6.js +274 -0
  27. package/dist/chunk-SGK37ZWD.js +254 -0
  28. package/dist/chunk-YNXJO2XU.js +69 -0
  29. package/dist/index.cjs +1229 -0
  30. package/dist/index.d.cts +6 -0
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +131 -0
  33. package/dist/server/index.cjs +4930 -0
  34. package/dist/server/index.d.cts +2 -0
  35. package/dist/server/index.d.ts +2 -0
  36. package/dist/server/index.js +4923 -0
  37. package/dist/templates/instrumentation-template.js +86 -0
  38. package/eslint.config.js +6 -0
  39. package/global.d.ts +0 -0
  40. package/package.json +112 -0
  41. package/public/templates/instrumentation-template.js +86 -0
  42. package/src/build/analyze.ts +279 -0
  43. package/src/build/babel/fix-libsql.ts +41 -0
  44. package/src/build/babel/get-deployer.ts +54 -0
  45. package/src/build/babel/get-telemetry-config.ts +62 -0
  46. package/src/build/babel/remove-deployer.ts +43 -0
  47. package/src/build/bundle.ts +139 -0
  48. package/src/build/bundler.ts +135 -0
  49. package/src/build/deployer.ts +67 -0
  50. package/src/build/deps.ts +149 -0
  51. package/src/build/env.ts +76 -0
  52. package/src/build/fs.ts +66 -0
  53. package/src/build/index.ts +7 -0
  54. package/src/build/isNodeBuiltin.ts +7 -0
  55. package/src/build/plugins/fix-libsql.ts +69 -0
  56. package/src/build/plugins/hono-alias.ts +17 -0
  57. package/src/build/plugins/pino.ts +93 -0
  58. package/src/build/plugins/remove-deployer.ts +37 -0
  59. package/src/build/plugins/telemetry-fix.ts +54 -0
  60. package/src/build/telemetry.ts +76 -0
  61. package/src/build/utils.ts +12 -0
  62. package/src/build/watcher.ts +43 -0
  63. package/src/bundler/index.ts +144 -0
  64. package/src/deploy/base.ts +30 -0
  65. package/src/deploy/index.ts +2 -0
  66. package/src/deploy/log.ts +61 -0
  67. package/src/index.ts +3 -0
  68. package/src/server/handlers/agents.ts +209 -0
  69. package/src/server/handlers/client.ts +36 -0
  70. package/src/server/handlers/error.ts +29 -0
  71. package/src/server/handlers/logs.ts +53 -0
  72. package/src/server/handlers/memory.ts +196 -0
  73. package/src/server/handlers/prompt.ts +128 -0
  74. package/src/server/handlers/root.ts +6 -0
  75. package/src/server/handlers/telemetry.ts +48 -0
  76. package/src/server/handlers/tools.ts +114 -0
  77. package/src/server/handlers/utils.ts +15 -0
  78. package/src/server/handlers/vector.ts +149 -0
  79. package/src/server/handlers/workflows.ts +119 -0
  80. package/src/server/index.ts +1355 -0
  81. package/src/server/openapi.json +434 -0
  82. package/src/server/openapi.script.js +22 -0
  83. package/src/server/types.ts +4 -0
  84. package/src/server/welcome.ts +105 -0
  85. package/tsconfig.json +5 -0
  86. package/vitest.config.ts +8 -0
@@ -0,0 +1,61 @@
1
+ import { spawn } from 'child_process';
2
+ import { Transform } from 'stream';
3
+ import type { Logger } from '@mastra/core/logger';
4
+
5
+ export const createPinoStream = (logger: Logger) => {
6
+ return new Transform({
7
+ transform(chunk, _encoding, callback) {
8
+ // Convert Buffer/string to string and trim whitespace
9
+ const line = chunk.toString().trim();
10
+
11
+ if (line) {
12
+ console.log(line);
13
+ // Log each line through Pino
14
+ logger.info(line);
15
+ }
16
+
17
+ // Pass through the original data
18
+ callback(null, chunk);
19
+ },
20
+ });
21
+ };
22
+
23
+ export function createChildProcessLogger({ logger, root }: { logger: Logger; root: string }) {
24
+ const pinoStream = createPinoStream(logger);
25
+ return async ({ cmd, args, env }: { cmd: string; args: string[]; env: Record<string, string> }) => {
26
+ try {
27
+ const subprocess = spawn(cmd, args, {
28
+ cwd: root,
29
+ shell: true,
30
+ env,
31
+ });
32
+
33
+ // Pipe stdout and stderr through the Pino stream
34
+ subprocess.stdout?.pipe(pinoStream);
35
+ subprocess.stderr?.pipe(pinoStream);
36
+
37
+ // Wait for the process to complete
38
+ return new Promise((resolve, reject) => {
39
+ subprocess.on('close', code => {
40
+ pinoStream.end();
41
+ if (code === 0) {
42
+ resolve({ success: true });
43
+ } else {
44
+ reject(new Error(`Process exited with code ${code}`));
45
+ }
46
+ });
47
+
48
+ subprocess.on('error', error => {
49
+ pinoStream.end();
50
+ logger.error('Process failed', { error });
51
+ reject(error);
52
+ });
53
+ });
54
+ } catch (error) {
55
+ console.log(error);
56
+ logger.error('Process failed', { error });
57
+ pinoStream.end();
58
+ return { success: false, error };
59
+ }
60
+ };
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './deploy';
2
+ export { Deps, FileService } from './build';
3
+ export { getDeployer } from './build/deployer';
@@ -0,0 +1,209 @@
1
+ import type { Mastra } from '@mastra/core';
2
+ import type { Context } from 'hono';
3
+ import { HTTPException } from 'hono/http-exception';
4
+ import { stringify } from 'superjson';
5
+ import zodToJsonSchema from 'zod-to-json-schema';
6
+
7
+ import { handleError } from './error';
8
+ import { validateBody } from './utils';
9
+
10
+ // Agent handlers
11
+ export async function getAgentsHandler(c: Context) {
12
+ try {
13
+ const mastra: Mastra = c.get('mastra');
14
+ const agents = mastra.getAgents();
15
+
16
+ const serializedAgents = Object.entries(agents).reduce<any>((acc, [_id, _agent]) => {
17
+ const agent = _agent as any;
18
+ const serializedAgentTools = Object.entries(agent?.tools || {}).reduce<any>((acc, [key, tool]) => {
19
+ const _tool = tool as any;
20
+ acc[key] = {
21
+ ..._tool,
22
+ inputSchema: _tool.inputSchema ? stringify(zodToJsonSchema(_tool.inputSchema)) : undefined,
23
+ outputSchema: _tool.outputSchema ? stringify(zodToJsonSchema(_tool.outputSchema)) : undefined,
24
+ };
25
+ return acc;
26
+ }, {});
27
+ acc[_id] = {
28
+ name: agent.name,
29
+ instructions: agent.instructions,
30
+ tools: serializedAgentTools,
31
+ provider: agent.llm?.getProvider(),
32
+ modelId: agent.llm?.getModelId(),
33
+ };
34
+ return acc;
35
+ }, {});
36
+
37
+ return c.json(serializedAgents);
38
+ } catch (error) {
39
+ return handleError(error, 'Error getting agents');
40
+ }
41
+ }
42
+
43
+ export async function getAgentByIdHandler(c: Context) {
44
+ try {
45
+ const mastra: Mastra = c.get('mastra');
46
+ const agentId = c.req.param('agentId');
47
+ const agent = mastra.getAgent(agentId);
48
+
49
+ if (!agent) {
50
+ throw new HTTPException(404, { message: 'Agent not found' });
51
+ }
52
+
53
+ const serializedAgentTools = Object.entries(agent?.tools || {}).reduce<any>((acc, [key, tool]) => {
54
+ const _tool = tool as any;
55
+ acc[key] = {
56
+ ..._tool,
57
+ inputSchema: _tool.inputSchema ? stringify(zodToJsonSchema(_tool.inputSchema)) : undefined,
58
+ outputSchema: _tool.outputSchema ? stringify(zodToJsonSchema(_tool.outputSchema)) : undefined,
59
+ };
60
+ return acc;
61
+ }, {});
62
+
63
+ return c.json({
64
+ name: agent.name,
65
+ instructions: agent.instructions,
66
+ tools: serializedAgentTools,
67
+ provider: agent.llm?.getProvider(),
68
+ modelId: agent.llm?.getModelId(),
69
+ });
70
+ } catch (error) {
71
+ return handleError(error, 'Error getting agent');
72
+ }
73
+ }
74
+
75
+ export async function getEvalsByAgentIdHandler(c: Context) {
76
+ try {
77
+ const mastra: Mastra = c.get('mastra');
78
+ const agentId = c.req.param('agentId');
79
+ const agent = mastra.getAgent(agentId);
80
+ const evals = (await mastra.storage?.getEvalsByAgentName?.(agent.name, 'test')) || [];
81
+ return c.json({
82
+ id: agentId,
83
+ name: agent.name,
84
+ instructions: agent.instructions,
85
+ evals,
86
+ });
87
+ } catch (error) {
88
+ return handleError(error, 'Error getting test evals');
89
+ }
90
+ }
91
+
92
+ export async function getLiveEvalsByAgentIdHandler(c: Context) {
93
+ try {
94
+ const mastra: Mastra = c.get('mastra');
95
+ const agentId = c.req.param('agentId');
96
+ const agent = mastra.getAgent(agentId);
97
+ const evals = (await mastra.storage?.getEvalsByAgentName?.(agent.name, 'live')) || [];
98
+
99
+ return c.json({
100
+ id: agentId,
101
+ name: agent.name,
102
+ instructions: agent.instructions,
103
+ evals,
104
+ });
105
+ } catch (error) {
106
+ return handleError(error, 'Error getting live evals');
107
+ }
108
+ }
109
+
110
+ export async function generateHandler(c: Context) {
111
+ try {
112
+ const mastra = c.get('mastra');
113
+ const agentId = c.req.param('agentId');
114
+ const agent = mastra.getAgent(agentId);
115
+
116
+ if (!agent) {
117
+ throw new HTTPException(404, { message: 'Agent not found' });
118
+ }
119
+
120
+ const { messages, threadId, resourceid, resourceId, output, runId } = await c.req.json();
121
+ validateBody({ messages });
122
+
123
+ if (!Array.isArray(messages)) {
124
+ throw new HTTPException(400, { message: 'Messages should be an array' });
125
+ }
126
+
127
+ // Use resourceId if provided, fall back to resourceid (deprecated)
128
+ const finalResourceId = resourceId ?? resourceid;
129
+
130
+ const result = await agent.generate(messages, { threadId, resourceId: finalResourceId, output, runId });
131
+
132
+ return c.json(result);
133
+ } catch (error) {
134
+ return handleError(error, 'Error generating from agent');
135
+ }
136
+ }
137
+
138
+ export async function streamGenerateHandler(c: Context): Promise<Response | undefined> {
139
+ try {
140
+ const mastra = c.get('mastra');
141
+ const agentId = c.req.param('agentId');
142
+ const agent = mastra.getAgent(agentId);
143
+
144
+ if (!agent) {
145
+ throw new HTTPException(404, { message: 'Agent not found' });
146
+ }
147
+
148
+ const { messages, threadId, resourceid, resourceId, output, runId } = await c.req.json();
149
+
150
+ validateBody({ messages });
151
+
152
+ if (!Array.isArray(messages)) {
153
+ throw new HTTPException(400, { message: 'Messages should be an array' });
154
+ }
155
+
156
+ // Use resourceId if provided, fall back to resourceid (deprecated)
157
+ const finalResourceId = resourceId ?? resourceid;
158
+
159
+ const streamResult = await agent.stream(messages, { threadId, resourceId: finalResourceId, output, runId });
160
+
161
+ const streamResponse = output
162
+ ? streamResult.toTextStreamResponse()
163
+ : streamResult.toDataStreamResponse({
164
+ sendUsage: true,
165
+ sendReasoning: true,
166
+ getErrorMessage: (error: any) => {
167
+ return `An error occurred while processing your request. ${error instanceof Error ? error.message : JSON.stringify(error)}`;
168
+ },
169
+ });
170
+
171
+ return streamResponse;
172
+ } catch (error) {
173
+ return handleError(error, 'Error streaming from agent');
174
+ }
175
+ }
176
+
177
+ export async function setAgentInstructionsHandler(c: Context) {
178
+ try {
179
+ // Check if this is a playground request
180
+ const isPlayground = c.get('playground') === true;
181
+ if (!isPlayground) {
182
+ return c.json({ error: 'This API is only available in the playground environment' }, 403);
183
+ }
184
+
185
+ const agentId = c.req.param('agentId');
186
+ const { instructions } = await c.req.json();
187
+
188
+ if (!agentId || !instructions) {
189
+ return c.json({ error: 'Missing required fields' }, 400);
190
+ }
191
+
192
+ const mastra: Mastra = c.get('mastra');
193
+ const agent = mastra.getAgent(agentId);
194
+ if (!agent) {
195
+ return c.json({ error: 'Agent not found' }, 404);
196
+ }
197
+
198
+ agent.__updateInstructions(instructions);
199
+
200
+ return c.json(
201
+ {
202
+ instructions,
203
+ },
204
+ 200,
205
+ );
206
+ } catch (error) {
207
+ return handleError(error, 'Error setting agent instructions');
208
+ }
209
+ }
@@ -0,0 +1,36 @@
1
+ import type { Context } from 'hono';
2
+
3
+ const clients = new Set<ReadableStreamDefaultController>();
4
+
5
+ export function handleClientsRefresh(c: Context): Response {
6
+ const stream = new ReadableStream({
7
+ start(controller) {
8
+ clients.add(controller);
9
+ controller.enqueue('data: connected\n\n');
10
+
11
+ c.req.raw.signal.addEventListener('abort', () => {
12
+ clients.delete(controller);
13
+ });
14
+ },
15
+ });
16
+
17
+ return new Response(stream, {
18
+ headers: {
19
+ 'Content-Type': 'text/event-stream',
20
+ 'Cache-Control': 'no-cache',
21
+ Connection: 'keep-alive',
22
+ 'Access-Control-Allow-Origin': '*',
23
+ },
24
+ });
25
+ }
26
+
27
+ export function handleTriggerClientsRefresh(c: Context) {
28
+ clients.forEach(controller => {
29
+ try {
30
+ controller.enqueue('data: refresh\n\n');
31
+ } catch {
32
+ clients.delete(controller);
33
+ }
34
+ });
35
+ return c.json({ success: true, clients: clients.size });
36
+ }
@@ -0,0 +1,29 @@
1
+ import type { Context } from 'hono';
2
+
3
+ import { HTTPException } from 'hono/http-exception';
4
+ import type { ContentfulStatusCode } from 'hono/utils/http-status';
5
+
6
+ import type { ApiError } from '../types';
7
+
8
+ // Helper to handle errors consistently
9
+ export function handleError(error: unknown, defaultMessage: string): Promise<Response> {
10
+ console.error(defaultMessage, error);
11
+ const apiError = error as ApiError;
12
+ throw new HTTPException((apiError.status || 500) as ContentfulStatusCode, {
13
+ message: apiError.message || defaultMessage,
14
+ });
15
+ }
16
+
17
+ // Error handlers
18
+ export function notFoundHandler() {
19
+ throw new HTTPException(404, { message: 'Not Found' });
20
+ }
21
+
22
+ export function errorHandler(err: Error, c: Context): Response {
23
+ if (err instanceof HTTPException) {
24
+ return c.json({ error: err.message }, err.status);
25
+ }
26
+
27
+ console.error(err);
28
+ return c.json({ error: 'Internal Server Error' }, 500);
29
+ }
@@ -0,0 +1,53 @@
1
+ import type { Mastra } from '@mastra/core';
2
+ import type { Context } from 'hono';
3
+
4
+ import { HTTPException } from 'hono/http-exception';
5
+
6
+ import { handleError } from './error';
7
+
8
+ export async function getLogsHandler(c: Context) {
9
+ try {
10
+ const mastra: Mastra = c.get('mastra');
11
+ const transportId = c.req.query('transportId');
12
+
13
+ if (!transportId) {
14
+ throw new HTTPException(400, { message: 'transportId is required' });
15
+ }
16
+
17
+ const logs = await mastra.getLogs(transportId);
18
+ return c.json(logs);
19
+ } catch (error) {
20
+ return handleError(error, 'Error getting logs');
21
+ }
22
+ }
23
+
24
+ export async function getLogsByRunIdHandler(c: Context) {
25
+ try {
26
+ const mastra: Mastra = c.get('mastra');
27
+ const runId = c.req.param('runId');
28
+ const transportId = c.req.query('transportId');
29
+
30
+ if (!transportId) {
31
+ throw new HTTPException(400, { message: 'transportId is required' });
32
+ }
33
+
34
+ const logs = await mastra.getLogsByRunId({ runId, transportId });
35
+ return c.json(logs);
36
+ } catch (error) {
37
+ return handleError(error, 'Error getting logs by run ID');
38
+ }
39
+ }
40
+
41
+ export async function getLogTransports(c: Context) {
42
+ console.log('here');
43
+ try {
44
+ const mastra: Mastra = c.get('mastra');
45
+ const logger = mastra.getLogger();
46
+ const transports = logger.transports;
47
+ return c.json({
48
+ transports: Object.keys(transports),
49
+ });
50
+ } catch (e) {
51
+ return handleError(e, 'Error getting log Transports ');
52
+ }
53
+ }
@@ -0,0 +1,196 @@
1
+ import type { MastraMemory } from '@mastra/core/memory';
2
+ import type { Context } from 'hono';
3
+
4
+ import { HTTPException } from 'hono/http-exception';
5
+
6
+ import { handleError } from './error';
7
+ import { validateBody } from './utils';
8
+
9
+ function getMemoryFromContext(c: Context): MastraMemory | null | undefined {
10
+ const mastra = c.get('mastra');
11
+ const agentId = c.req.query('agentId');
12
+ const agent = agentId ? mastra.getAgent(agentId) : null;
13
+
14
+ if (agentId && !agent) {
15
+ throw new HTTPException(404, { message: 'Agent not found' });
16
+ }
17
+
18
+ const memory = agent?.getMemory?.() || mastra.memory;
19
+
20
+ return memory;
21
+ }
22
+
23
+ // Memory handlers
24
+ export async function getMemoryStatusHandler(c: Context) {
25
+ try {
26
+ const memory = getMemoryFromContext(c);
27
+
28
+ if (!memory) {
29
+ return c.json({ result: false });
30
+ }
31
+
32
+ return c.json({ result: true });
33
+ } catch (error) {
34
+ return handleError(error, 'Error getting memory status');
35
+ }
36
+ }
37
+
38
+ export async function getThreadsHandler(c: Context) {
39
+ try {
40
+ const { resourceid } = c.req.query();
41
+ const memory = getMemoryFromContext(c);
42
+
43
+ if (!memory) {
44
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
45
+ }
46
+
47
+ if (!resourceid) {
48
+ throw new HTTPException(400, { message: 'Resource ID is required' });
49
+ }
50
+
51
+ const threads = await memory.getThreadsByResourceId({ resourceId: resourceid });
52
+ return c.json(threads);
53
+ } catch (error) {
54
+ return handleError(error, 'Error getting threads');
55
+ }
56
+ }
57
+
58
+ export async function getThreadByIdHandler(c: Context) {
59
+ try {
60
+ const memory = getMemoryFromContext(c);
61
+ const threadId = c.req.param('threadId');
62
+
63
+ if (!memory) {
64
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
65
+ }
66
+
67
+ const thread = await memory.getThreadById({ threadId });
68
+ if (!thread) {
69
+ throw new HTTPException(404, { message: 'Thread not found' });
70
+ }
71
+
72
+ return c.json(thread);
73
+ } catch (error) {
74
+ return handleError(error, 'Error getting thread');
75
+ }
76
+ }
77
+
78
+ export async function saveMessagesHandler(c: Context) {
79
+ try {
80
+ const memory = getMemoryFromContext(c);
81
+ const { messages } = await c.req.json();
82
+
83
+ if (!memory) {
84
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
85
+ }
86
+
87
+ validateBody({ messages });
88
+
89
+ if (!Array.isArray(messages)) {
90
+ throw new HTTPException(400, { message: 'Messages should be an array' });
91
+ }
92
+
93
+ const processedMessages = messages.map(message => ({
94
+ ...message,
95
+ id: memory.generateId(),
96
+ createdAt: message.createdAt ? new Date(message.createdAt) : new Date(),
97
+ }));
98
+
99
+ const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
100
+ return c.json(result);
101
+ } catch (error) {
102
+ return handleError(error, 'Error saving messages');
103
+ }
104
+ }
105
+
106
+ export async function createThreadHandler(c: Context) {
107
+ try {
108
+ const memory = getMemoryFromContext(c);
109
+ const { title, metadata, resourceid, threadId } = await c.req.json();
110
+
111
+ if (!memory) {
112
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
113
+ }
114
+
115
+ validateBody({ resourceid });
116
+
117
+ const result = await memory.createThread({ resourceId: resourceid, title, metadata, threadId });
118
+ return c.json(result);
119
+ } catch (error) {
120
+ return handleError(error, 'Error saving thread to memory');
121
+ }
122
+ }
123
+
124
+ export async function updateThreadHandler(c: Context) {
125
+ try {
126
+ const memory = getMemoryFromContext(c);
127
+ const threadId = c.req.param('threadId');
128
+ const { title, metadata, resourceid } = await c.req.json();
129
+ const updatedAt = new Date();
130
+
131
+ if (!memory) {
132
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
133
+ }
134
+
135
+ const thread = await memory.getThreadById({ threadId });
136
+ if (!thread) {
137
+ throw new HTTPException(404, { message: 'Thread not found' });
138
+ }
139
+
140
+ const updatedThread = {
141
+ ...thread,
142
+ title: title || thread.title,
143
+ metadata: metadata || thread.metadata,
144
+ resourceId: resourceid || thread.resourceId,
145
+ createdAt: thread.createdAt,
146
+ updatedAt,
147
+ };
148
+
149
+ const result = await memory.saveThread({ thread: updatedThread });
150
+ return c.json(result);
151
+ } catch (error) {
152
+ return handleError(error, 'Error updating thread');
153
+ }
154
+ }
155
+
156
+ export async function deleteThreadHandler(c: Context) {
157
+ try {
158
+ const memory = getMemoryFromContext(c);
159
+ const threadId = c.req.param('threadId');
160
+
161
+ if (!memory) {
162
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
163
+ }
164
+
165
+ const thread = await memory.getThreadById({ threadId });
166
+ if (!thread) {
167
+ throw new HTTPException(404, { message: 'Thread not found' });
168
+ }
169
+
170
+ await memory.deleteThread(threadId);
171
+ return c.json({ result: 'Thread deleted' });
172
+ } catch (error) {
173
+ return handleError(error, 'Error deleting thread');
174
+ }
175
+ }
176
+
177
+ export async function getMessagesHandler(c: Context) {
178
+ try {
179
+ const memory = getMemoryFromContext(c);
180
+ const threadId = c.req.param('threadId');
181
+
182
+ if (!memory) {
183
+ return c.json({ error: 'Memory is not initialized' }, 400);
184
+ }
185
+
186
+ const thread = await memory.getThreadById({ threadId });
187
+ if (!thread) {
188
+ return c.json({ error: 'Thread not found' }, 404);
189
+ }
190
+
191
+ const result = await memory.query({ threadId });
192
+ return c.json(result);
193
+ } catch (error) {
194
+ return handleError(error, 'Error getting messages');
195
+ }
196
+ }