@mastra/deployer 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 (79) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +44 -0
  3. package/dist/build/bundle.d.ts +8 -0
  4. package/dist/build/bundle.d.ts.map +1 -0
  5. package/dist/build/deps.d.ts +21 -0
  6. package/dist/build/deps.d.ts.map +1 -0
  7. package/dist/build/env.d.ts +14 -0
  8. package/dist/build/env.d.ts.map +1 -0
  9. package/dist/build/fs.d.ts +22 -0
  10. package/dist/build/fs.d.ts.map +1 -0
  11. package/dist/build/index.d.ts +6 -0
  12. package/dist/build/index.d.ts.map +1 -0
  13. package/dist/build/utils.d.ts +2 -0
  14. package/dist/build/utils.d.ts.map +1 -0
  15. package/dist/deploy/base.d.ts +36 -0
  16. package/dist/deploy/base.d.ts.map +1 -0
  17. package/dist/deploy/index.d.ts +2 -0
  18. package/dist/deploy/index.d.ts.map +1 -0
  19. package/dist/deployer.cjs.development.js +2872 -0
  20. package/dist/deployer.cjs.development.js.map +1 -0
  21. package/dist/deployer.cjs.production.min.js +2 -0
  22. package/dist/deployer.cjs.production.min.js.map +1 -0
  23. package/dist/deployer.esm.js +2840 -0
  24. package/dist/deployer.esm.js.map +1 -0
  25. package/dist/index.d.ts +4 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +8 -0
  28. package/dist/server/handlers/agents.d.ts +6 -0
  29. package/dist/server/handlers/agents.d.ts.map +1 -0
  30. package/dist/server/handlers/client.d.ts +7 -0
  31. package/dist/server/handlers/client.d.ts.map +1 -0
  32. package/dist/server/handlers/error.d.ts +7 -0
  33. package/dist/server/handlers/error.d.ts.map +1 -0
  34. package/dist/server/handlers/logs.d.ts +4 -0
  35. package/dist/server/handlers/logs.d.ts.map +1 -0
  36. package/dist/server/handlers/memory.d.ts +17 -0
  37. package/dist/server/handlers/memory.d.ts.map +1 -0
  38. package/dist/server/handlers/root.d.ts +3 -0
  39. package/dist/server/handlers/root.d.ts.map +1 -0
  40. package/dist/server/handlers/syncs.d.ts +3 -0
  41. package/dist/server/handlers/syncs.d.ts.map +1 -0
  42. package/dist/server/handlers/tools.d.ts +8 -0
  43. package/dist/server/handlers/tools.d.ts.map +1 -0
  44. package/dist/server/handlers/utils.d.ts +2 -0
  45. package/dist/server/handlers/utils.d.ts.map +1 -0
  46. package/dist/server/handlers/workflows.d.ts +11 -0
  47. package/dist/server/handlers/workflows.d.ts.map +1 -0
  48. package/dist/server/index.d.ts +21 -0
  49. package/dist/server/index.d.ts.map +1 -0
  50. package/dist/server/index.js +8964 -0
  51. package/dist/server/types.d.ts +5 -0
  52. package/dist/server/types.d.ts.map +1 -0
  53. package/dist/server/welcome.d.ts +2 -0
  54. package/dist/server/welcome.d.ts.map +1 -0
  55. package/package.json +61 -0
  56. package/src/build/bundle.ts +224 -0
  57. package/src/build/deps.ts +128 -0
  58. package/src/build/env.ts +76 -0
  59. package/src/build/fs.ts +66 -0
  60. package/src/build/index.ts +5 -0
  61. package/src/build/utils.ts +12 -0
  62. package/src/deploy/base.ts +171 -0
  63. package/src/deploy/index.ts +1 -0
  64. package/src/index.ts +3 -0
  65. package/src/server/handlers/agents.ts +121 -0
  66. package/src/server/handlers/client.ts +36 -0
  67. package/src/server/handlers/error.ts +29 -0
  68. package/src/server/handlers/logs.ts +24 -0
  69. package/src/server/handlers/memory.ts +208 -0
  70. package/src/server/handlers/root.ts +6 -0
  71. package/src/server/handlers/syncs.ts +19 -0
  72. package/src/server/handlers/tools.ts +156 -0
  73. package/src/server/handlers/utils.ts +15 -0
  74. package/src/server/handlers/workflows.ts +60 -0
  75. package/src/server/index.ts +185 -0
  76. package/src/server/types.ts +4 -0
  77. package/src/server/welcome.ts +105 -0
  78. package/tsconfig.json +10 -0
  79. package/vitest.config.ts +8 -0
@@ -0,0 +1,171 @@
1
+ import { Mastra } from '@mastra/core';
2
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
3
+ import path, { join } from 'path';
4
+ import { fileURLToPath } from 'url';
5
+
6
+ import { bundle } from '../build/bundle.js';
7
+ import { Deps } from '../build/deps.js';
8
+ import { upsertMastraDir } from '../build/utils.js';
9
+
10
+ export class Deployer {
11
+ deps: Deps = new Deps();
12
+ dotMastraPath: string;
13
+ name: string = '';
14
+ type: 'Deploy' | 'Dev';
15
+
16
+ log = (message: string) => {
17
+ console.log(`[Mastra ${this.type}] - ${message}`);
18
+ };
19
+
20
+ constructor({ dir, type }: { dir: string; type: 'Deploy' | 'Dev' }) {
21
+ this.dotMastraPath = join(dir, '.mastra');
22
+ this.type = type;
23
+ }
24
+
25
+ getMastraPath() {
26
+ return this.dotMastraPath;
27
+ }
28
+
29
+ async getMastra() {
30
+ return (await import(join(this.dotMastraPath, 'mastra.mjs'))) as { mastra: Mastra };
31
+ }
32
+
33
+ async writePackageJson() {
34
+ this.log('Writing package.json');
35
+ let projectPkg: any = {
36
+ dependencies: {},
37
+ };
38
+
39
+ try {
40
+ projectPkg = JSON.parse(readFileSync(join(this.dotMastraPath, '..', 'package.json'), 'utf-8'));
41
+ } catch (_e) {
42
+ // no-op
43
+ }
44
+
45
+ const mastraDeps = Object.entries(projectPkg.dependencies)
46
+ .filter(([name]) => name.startsWith('@mastra') && !name.startsWith('@mastra/deployer'))
47
+ .reduce((acc: any, [name, version]) => {
48
+ if (version === 'workspace:*') {
49
+ acc[name] = 'latest';
50
+ } else {
51
+ acc[name] = version;
52
+ }
53
+
54
+ return acc;
55
+ }, {});
56
+
57
+ const pkgPath = join(this.dotMastraPath, 'package.json');
58
+
59
+ if (this.type === 'Dev' && existsSync(pkgPath)) {
60
+ return;
61
+ }
62
+
63
+ writeFileSync(
64
+ pkgPath,
65
+ JSON.stringify(
66
+ {
67
+ name: 'server',
68
+ version: '1.0.0',
69
+ description: '',
70
+ type: 'module',
71
+ main: 'index.mjs',
72
+ scripts: {
73
+ start: 'node ./index.mjs',
74
+ },
75
+ author: '',
76
+ license: 'ISC',
77
+ dependencies: {
78
+ ...mastraDeps,
79
+ hono: '4.6.17',
80
+ execa: 'latest',
81
+ '@hono/node-server': '^1.13.7',
82
+ superjson: '^2.2.2',
83
+ 'zod-to-json-schema': '^3.24.1',
84
+ },
85
+ },
86
+ null,
87
+ 2,
88
+ ),
89
+ );
90
+ }
91
+
92
+ async install() {
93
+ this.log('Ensuring your dependencies up to date...');
94
+ await this.deps.install();
95
+ }
96
+
97
+ protected getEnvFiles(): string[] {
98
+ const envFiles = ['.env', '.env.development', '.env.local']
99
+ .map(file => join(process.cwd(), file))
100
+ .filter(file => existsSync(file));
101
+ return envFiles;
102
+ }
103
+
104
+ getEnvVars() {
105
+ // Get all env vars
106
+ const envFiles = this.getEnvFiles();
107
+ const envVars: Record<string, string> = {};
108
+
109
+ for (const file of envFiles) {
110
+ const vars = this.parseEnvFile(file);
111
+ for (const envVar of vars) {
112
+ const [key, value] = envVar.split('=');
113
+ if (key && value) {
114
+ envVars[key] = value;
115
+ }
116
+ }
117
+ }
118
+
119
+ return envVars;
120
+ }
121
+
122
+ protected parseEnvFile(filePath: string): string[] {
123
+ const content = readFileSync(filePath, 'utf-8');
124
+ return content
125
+ .split('\n')
126
+ .map(line => line.trim())
127
+ .filter(line => line && !line.startsWith('#'))
128
+ .filter(line => line.includes('=')); // Only include valid KEY=value pairs
129
+ }
130
+
131
+ async build({ dir, useBanner = true }: { dir: string; useBanner?: boolean }) {
132
+ if (!existsSync(this.dotMastraPath)) {
133
+ mkdirSync(this.dotMastraPath);
134
+ }
135
+
136
+ await bundle(dir, { useBanner, outfile: join(this.dotMastraPath, 'mastra.mjs') });
137
+ }
138
+
139
+ async buildServer({ playground = false }: { playground?: boolean } = { playground: false }) {
140
+ upsertMastraDir();
141
+
142
+ const templatePath = join(this.dotMastraPath, 'hono.mjs');
143
+
144
+ writeFileSync(
145
+ templatePath,
146
+ `
147
+ import { createHonoServer } from './server.mjs';
148
+ import { mastra } from './mastra.mjs';
149
+
150
+ export const app = await createHonoServer(mastra, { playground: ${playground} });
151
+ `,
152
+ );
153
+ }
154
+
155
+ async writeServerFile() {
156
+ const fileServerPath = await import.meta.resolve('@mastra/deployer/server');
157
+ const serverPath = fileURLToPath(fileServerPath);
158
+ copyFileSync(serverPath, join(this.dotMastraPath, 'server.mjs'));
159
+ }
160
+
161
+ async prepare({ dir, playground, useBanner = true }: { useBanner?: boolean; dir?: string; playground?: boolean }) {
162
+ this.log('Preparing .mastra directory');
163
+ upsertMastraDir();
164
+ const dirPath = dir || path.join(process.cwd(), 'src/mastra');
165
+ this.writePackageJson();
166
+ this.writeServerFile();
167
+ await this.install();
168
+ await this.build({ dir: dirPath, useBanner });
169
+ await this.buildServer({ playground });
170
+ }
171
+ }
@@ -0,0 +1 @@
1
+ export * from './base.js';
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './deploy/index.js';
2
+ export * from './build/index.js';
3
+ export * from './server/index.js';
@@ -0,0 +1,121 @@
1
+ import { Context } from 'hono';
2
+ import { stringify } from 'superjson';
3
+ import zodToJsonSchema from 'zod-to-json-schema';
4
+
5
+ import { HTTPException } from 'hono/http-exception';
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 = 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
+ ...agent,
29
+ tools: serializedAgentTools,
30
+ };
31
+ return acc;
32
+ }, {});
33
+
34
+ return c.json(serializedAgents);
35
+ } catch (error) {
36
+ return handleError(error, 'Error getting agents');
37
+ }
38
+ }
39
+
40
+ export async function getAgentByIdHandler(c: Context) {
41
+ try {
42
+ const mastra = c.get('mastra');
43
+ const agentId = c.req.param('agentId');
44
+ const agent = mastra.getAgent(agentId);
45
+
46
+ if (!agent) {
47
+ throw new HTTPException(404, { message: 'Agent not found' });
48
+ }
49
+
50
+ const serializedAgentTools = Object.entries(agent?.tools || {}).reduce<any>((acc, [key, tool]) => {
51
+ const _tool = tool as any;
52
+ acc[key] = {
53
+ ..._tool,
54
+ inputSchema: _tool.inputSchema ? stringify(zodToJsonSchema(_tool.inputSchema)) : undefined,
55
+ outputSchema: _tool.outputSchema ? stringify(zodToJsonSchema(_tool.outputSchema)) : undefined,
56
+ };
57
+ return acc;
58
+ }, {});
59
+
60
+ return c.json({
61
+ ...agent,
62
+ tools: serializedAgentTools,
63
+ });
64
+ } catch (error) {
65
+ return handleError(error, 'Error getting agent');
66
+ }
67
+ }
68
+
69
+ export async function generateHandler(c: Context) {
70
+ try {
71
+ const mastra = c.get('mastra');
72
+ const agentId = c.req.param('agentId');
73
+ const agent = mastra.getAgent(agentId);
74
+
75
+ if (!agent) {
76
+ throw new HTTPException(404, { message: 'Agent not found' });
77
+ }
78
+
79
+ const { messages, threadId, resourceid, output } = await c.req.json();
80
+ validateBody({ messages });
81
+
82
+ if (!Array.isArray(messages)) {
83
+ throw new HTTPException(400, { message: 'Messages should be an array' });
84
+ }
85
+
86
+ const result = await agent.generate(messages, { threadId, resourceid, output });
87
+ return c.json(result);
88
+ } catch (error) {
89
+ return handleError(error, 'Error generating from agent');
90
+ }
91
+ }
92
+
93
+ export async function streamGenerateHandler(c: Context) {
94
+ try {
95
+ const mastra = c.get('mastra');
96
+ const agentId = c.req.param('agentId');
97
+ const agent = mastra.getAgent(agentId);
98
+
99
+ if (!agent) {
100
+ throw new HTTPException(404, { message: 'Agent not found' });
101
+ }
102
+
103
+ const { messages, threadId, resourceid, output } = await c.req.json();
104
+ validateBody({ messages });
105
+
106
+ if (!Array.isArray(messages)) {
107
+ throw new HTTPException(400, { message: 'Messages should be an array' });
108
+ }
109
+
110
+ const streamResult = await agent.stream(messages, { threadId, resourceid, output });
111
+ return new Response(streamResult.toDataStream(), {
112
+ headers: {
113
+ 'Content-Type': 'text/x-unknown',
114
+ 'content-encoding': 'identity',
115
+ 'transfer-encoding': 'chunked',
116
+ },
117
+ });
118
+ } catch (error) {
119
+ return handleError(error, 'Error streaming from agent');
120
+ }
121
+ }
@@ -0,0 +1,36 @@
1
+ import { Context } from 'hono';
2
+
3
+ const clients = new Set<ReadableStreamDefaultController>();
4
+
5
+ export function handleClientsRefresh(c: Context) {
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 { Context } from 'hono';
2
+
3
+ import { HTTPException } from 'hono/http-exception';
4
+ import { ContentfulStatusCode } from 'hono/utils/http-status';
5
+
6
+ import { ApiError } from '../types';
7
+
8
+ // Helper to handle errors consistently
9
+ export function handleError(error: unknown, defaultMessage: string) {
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) {
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,24 @@
1
+ import { Context } from 'hono';
2
+
3
+ import { handleError } from './error';
4
+
5
+ export async function getLogsHandler(c: Context) {
6
+ try {
7
+ const mastra = c.get('mastra');
8
+ const logs = await mastra.getLogs();
9
+ return c.json(logs);
10
+ } catch (error) {
11
+ return handleError(error, 'Error getting logs');
12
+ }
13
+ }
14
+
15
+ export async function getLogsByRunIdHandler(c: Context) {
16
+ try {
17
+ const mastra = c.get('mastra');
18
+ const runId = c.req.param('runId');
19
+ const logs = await mastra.getLogsByRunId(runId);
20
+ return c.json(logs);
21
+ } catch (error) {
22
+ return handleError(error, 'Error getting logs by run ID');
23
+ }
24
+ }
@@ -0,0 +1,208 @@
1
+ import { Context } from 'hono';
2
+
3
+ import { HTTPException } from 'hono/http-exception';
4
+
5
+ import { handleError } from './error';
6
+ import { validateBody } from './utils';
7
+
8
+ // Memory handlers
9
+ export async function getMemoryStatusHandler(c: Context) {
10
+ try {
11
+ const mastra = c.get('mastra');
12
+ const memory = mastra.memory;
13
+
14
+ if (!memory) {
15
+ return c.json({ result: false });
16
+ }
17
+
18
+ return c.json({ result: true });
19
+ } catch (error) {
20
+ return handleError(error, 'Error getting memory status');
21
+ }
22
+ }
23
+
24
+ export async function getThreadsHandler(c: Context) {
25
+ try {
26
+ const mastra = c.get('mastra');
27
+ const memory = mastra.memory;
28
+ const { resourceid } = c.req.query();
29
+
30
+ if (!memory) {
31
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
32
+ }
33
+
34
+ const threads = await memory.getThreadsByResourceId({ resourceid });
35
+ return c.json(threads);
36
+ } catch (error) {
37
+ return handleError(error, 'Error getting threads');
38
+ }
39
+ }
40
+
41
+ export async function getThreadByIdHandler(c: Context) {
42
+ try {
43
+ const mastra = c.get('mastra');
44
+ const memory = mastra.memory;
45
+ const threadId = c.req.param('threadId');
46
+
47
+ if (!memory) {
48
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
49
+ }
50
+
51
+ const thread = await memory.getThreadById({ threadId });
52
+ if (!thread) {
53
+ throw new HTTPException(404, { message: 'Thread not found' });
54
+ }
55
+
56
+ return c.json(thread);
57
+ } catch (error) {
58
+ return handleError(error, 'Error getting thread');
59
+ }
60
+ }
61
+
62
+ export async function saveMessagesHandler(c: Context) {
63
+ try {
64
+ const mastra = c.get('mastra');
65
+ const memory = mastra.memory;
66
+ const { messages } = await c.req.json();
67
+
68
+ if (!memory) {
69
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
70
+ }
71
+
72
+ validateBody({ messages });
73
+
74
+ if (!Array.isArray(messages)) {
75
+ throw new HTTPException(400, { message: 'Messages should be an array' });
76
+ }
77
+
78
+ const processedMessages = messages.map(message => ({
79
+ ...message,
80
+ id: memory.generateId(),
81
+ createdAt: message.createdAt ? new Date(message.createdAt) : new Date(),
82
+ }));
83
+
84
+ const result = await memory.saveMessages({ messages: processedMessages });
85
+ return c.json(result);
86
+ } catch (error) {
87
+ return handleError(error, 'Error saving messages');
88
+ }
89
+ }
90
+
91
+ export async function getContextWindowHandler(c: Context) {
92
+ try {
93
+ const mastra = c.get('mastra');
94
+ const memory = mastra.memory;
95
+ const threadId = c.req.param('threadId');
96
+ const { startDate, endDate, format } = c.req.query();
97
+
98
+ if (!memory) {
99
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
100
+ }
101
+
102
+ const thread = await memory.getThreadById({ threadId });
103
+ if (!thread) {
104
+ throw new HTTPException(404, { message: 'Thread not found' });
105
+ }
106
+
107
+ const result = await memory.getContextWindow({ threadId, startDate, endDate, format });
108
+ return c.json(result);
109
+ } catch (error) {
110
+ return handleError(error, 'Error getting context window from memory');
111
+ }
112
+ }
113
+
114
+ export async function createThreadHandler(c: Context) {
115
+ try {
116
+ const mastra = c.get('mastra');
117
+ const memory = mastra.memory;
118
+ const { title, metadata, resourceid, threadId } = await c.req.json();
119
+
120
+ if (!memory) {
121
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
122
+ }
123
+
124
+ validateBody({ resourceid });
125
+
126
+ const result = await memory.createThread({ resourceid, title, metadata, threadId });
127
+ return c.json(result);
128
+ } catch (error) {
129
+ return handleError(error, 'Error saving thread to memory');
130
+ }
131
+ }
132
+
133
+ export async function updateThreadHandler(c: Context) {
134
+ try {
135
+ const mastra = c.get('mastra');
136
+ const memory = mastra.memory;
137
+ const threadId = c.req.param('threadId');
138
+ const { title, metadata, resourceid } = await c.req.json();
139
+ const updatedAt = new Date();
140
+
141
+ if (!memory) {
142
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
143
+ }
144
+
145
+ const thread = await memory.getThreadById({ threadId });
146
+ if (!thread) {
147
+ throw new HTTPException(404, { message: 'Thread not found' });
148
+ }
149
+
150
+ const updatedThread = {
151
+ ...thread,
152
+ title: title || thread.title,
153
+ metadata: metadata || thread.metadata,
154
+ resourceid: resourceid || thread.resourceid,
155
+ createdAt: thread.createdat,
156
+ updatedAt,
157
+ };
158
+
159
+ const result = await memory.saveThread({ thread: updatedThread });
160
+ return c.json(result);
161
+ } catch (error) {
162
+ return handleError(error, 'Error updating thread');
163
+ }
164
+ }
165
+
166
+ export async function deleteThreadHandler(c: Context) {
167
+ try {
168
+ const mastra = c.get('mastra');
169
+ const memory = mastra.memory;
170
+ const threadId = c.req.param('threadId');
171
+
172
+ if (!memory) {
173
+ throw new HTTPException(400, { message: 'Memory is not initialized' });
174
+ }
175
+
176
+ const thread = await memory.getThreadById({ threadId });
177
+ if (!thread) {
178
+ throw new HTTPException(404, { message: 'Thread not found' });
179
+ }
180
+
181
+ await memory.deleteThread(threadId);
182
+ return c.json({ result: 'Thread deleted' });
183
+ } catch (error) {
184
+ return handleError(error, 'Error deleting thread');
185
+ }
186
+ }
187
+
188
+ export async function getMessagesHandler(c: Context) {
189
+ try {
190
+ const mastra = c.get('mastra');
191
+ const memory = mastra.memory;
192
+ const threadId = c.req.param('threadId');
193
+
194
+ if (!memory) {
195
+ return c.json({ error: 'Memory is not initialized' }, 400);
196
+ }
197
+
198
+ const thread = await memory.getThreadById({ threadId });
199
+ if (!thread) {
200
+ return c.json({ error: 'Thread not found' }, 404);
201
+ }
202
+
203
+ const result = await memory.getMessages({ threadId });
204
+ return c.json(result);
205
+ } catch (error) {
206
+ return handleError(error, 'Error getting messages');
207
+ }
208
+ }
@@ -0,0 +1,6 @@
1
+ import { Context } from 'hono';
2
+
3
+ // Root handler
4
+ export async function rootHandler(c: Context) {
5
+ return c.text('Hello to the Mastra API!');
6
+ }
@@ -0,0 +1,19 @@
1
+ import { Context } from 'hono';
2
+
3
+ import { handleError } from './error';
4
+ import { validateBody } from './utils';
5
+
6
+ export async function executeSyncHandler(c: Context) {
7
+ try {
8
+ const mastra = c.get('mastra');
9
+ const syncId = c.req.param('syncId');
10
+ const { runId, params: syncParams } = await c.req.json();
11
+
12
+ validateBody({ params: syncParams });
13
+
14
+ const result = await mastra.sync(syncId, syncParams, runId);
15
+ return c.json(result);
16
+ } catch (error) {
17
+ return handleError(error, 'Error executing sync');
18
+ }
19
+ }