mastra 0.1.57-alpha.95 → 0.1.57-alpha.97
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/commands/create/utils.d.ts.map +1 -1
- package/dist/commands/create/utils.js +1 -0
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +93 -89
- package/dist/templates/express-server.d.ts.map +1 -1
- package/dist/templates/express-server.js +54 -119
- package/dist/templates/netlify.d.ts.map +1 -1
- package/dist/templates/netlify.js +16 -59
- package/dist/templates/worker.d.ts.map +1 -1
- package/dist/templates/worker.js +11 -95
- package/package.json +3 -51
- package/src/playground/dist/assets/{index-OyF976_j.js → index-C50mb3yU.js} +1 -1
- package/src/playground/dist/assets/{index-Bc0C61_9.js → index-VTk9kLaV.js} +87 -87
- package/src/playground/dist/index.html +23 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/commands/create/utils.ts"],"names":[],"mappings":"AAYA,eAAO,MAAM,mBAAmB;;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/commands/create/utils.ts"],"names":[],"mappings":"AAYA,eAAO,MAAM,mBAAmB;;EA8D/B,CAAC"}
|
|
@@ -51,6 +51,7 @@ export const createMastraProject = async () => {
|
|
|
51
51
|
await exec(`echo output.txt >> .gitignore`);
|
|
52
52
|
await exec(`echo node_modules >> .gitignore`);
|
|
53
53
|
await exec(`echo dist >> .gitignore`);
|
|
54
|
+
await exec(`echo .mastra >> .gitignore`);
|
|
54
55
|
s.stop('.gitignore added');
|
|
55
56
|
p.outro('Project created successfully');
|
|
56
57
|
logger.break();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/commands/dev.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/commands/dev.ts"],"names":[],"mappings":"AA0KA,wBAAsB,GAAG,CAAC,EACxB,IAAI,EACJ,GAAG,EACH,GAAG,EACH,SAAS,GACV,EAAE;IACD,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,iBAkEA"}
|
package/dist/commands/dev.js
CHANGED
|
@@ -13,19 +13,11 @@ import { EXPRESS_SERVER } from './deploy/server.js';
|
|
|
13
13
|
const __filename = fileURLToPath(import.meta.url);
|
|
14
14
|
const __dirname = path.dirname(__filename);
|
|
15
15
|
let currentServerProcess;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
console.log('Stopping current server...');
|
|
19
|
-
currentServerProcess.kill();
|
|
20
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
21
|
-
}
|
|
22
|
-
/*
|
|
23
|
-
Bundle mastra
|
|
24
|
-
*/
|
|
16
|
+
let isRestarting = false;
|
|
17
|
+
const bundleMastra = async (dirPath) => {
|
|
25
18
|
await bundle(dirPath, { buildName: 'Mastra' });
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*/
|
|
19
|
+
};
|
|
20
|
+
const bundleTools = async (dirPath, dotMastraPath, toolsDirs) => {
|
|
29
21
|
const defaultToolsPath = path.join(dirPath, 'tools');
|
|
30
22
|
const toolsPaths = [...(toolsDirs?.split(',').map(tool => path.join(process.cwd(), tool)) || []), defaultToolsPath];
|
|
31
23
|
const toolPathsWithFileNames = (await Promise.all(toolsPaths.map(async (toolPath) => {
|
|
@@ -60,8 +52,12 @@ async function rebundleAndRestart(dirPath, dotMastraPath, port, toolsDirs) {
|
|
|
60
52
|
const MASTRA_TOOLS_PATH = toolPathsWithFileNames?.length
|
|
61
53
|
? toolPathsWithFileNames.map(tool => path.join(dotMastraPath, 'tools', `${tool.name}.mjs`)).join(',')
|
|
62
54
|
: undefined;
|
|
55
|
+
return MASTRA_TOOLS_PATH;
|
|
56
|
+
};
|
|
57
|
+
const startServer = async (dotMastraPath, port, MASTRA_TOOLS_PATH) => {
|
|
63
58
|
try {
|
|
64
|
-
|
|
59
|
+
// Restart server
|
|
60
|
+
console.log('Starting server...');
|
|
65
61
|
currentServerProcess = execa('node', ['server.mjs'], {
|
|
66
62
|
cwd: dotMastraPath,
|
|
67
63
|
env: {
|
|
@@ -71,8 +67,34 @@ async function rebundleAndRestart(dirPath, dotMastraPath, port, toolsDirs) {
|
|
|
71
67
|
stdio: 'inherit',
|
|
72
68
|
reject: false,
|
|
73
69
|
});
|
|
74
|
-
|
|
75
|
-
|
|
70
|
+
// Wait for server to be ready
|
|
71
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
72
|
+
// Send refresh signal
|
|
73
|
+
try {
|
|
74
|
+
await fetch(`http://localhost:${port}/__refresh`, {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
headers: {
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
// Retry after another second
|
|
83
|
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
84
|
+
try {
|
|
85
|
+
await fetch(`http://localhost:${port}/__refresh`, {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
headers: {
|
|
88
|
+
'Content-Type': 'application/json',
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (retryErr) {
|
|
93
|
+
// Ignore retry errors
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (currentServerProcess.exitCode !== null) {
|
|
97
|
+
console.error('Server failed to start with error:', currentServerProcess.stderr);
|
|
76
98
|
return;
|
|
77
99
|
}
|
|
78
100
|
}
|
|
@@ -83,12 +105,48 @@ async function rebundleAndRestart(dirPath, dotMastraPath, port, toolsDirs) {
|
|
|
83
105
|
if (execaError.stdout)
|
|
84
106
|
console.error('Server output:', execaError.stdout);
|
|
85
107
|
}
|
|
108
|
+
};
|
|
109
|
+
async function rebundleAndRestart(dirPath, dotMastraPath, port, envFile, toolsDirs) {
|
|
110
|
+
if (isRestarting) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
isRestarting = true;
|
|
114
|
+
try {
|
|
115
|
+
// If current server process is running, stop it
|
|
116
|
+
if (currentServerProcess) {
|
|
117
|
+
console.log('Stopping current server...');
|
|
118
|
+
currentServerProcess.kill();
|
|
119
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
120
|
+
}
|
|
121
|
+
config({ path: envFile });
|
|
122
|
+
/*
|
|
123
|
+
Bundle mastra
|
|
124
|
+
*/
|
|
125
|
+
await bundleMastra(dirPath);
|
|
126
|
+
/*
|
|
127
|
+
Bundle tools
|
|
128
|
+
*/
|
|
129
|
+
const MASTRA_TOOLS_PATH = await bundleTools(dirPath, dotMastraPath, toolsDirs);
|
|
130
|
+
/*
|
|
131
|
+
Bundle server
|
|
132
|
+
*/
|
|
133
|
+
writeFileSync(join(dotMastraPath, 'index.mjs'), EXPRESS_SERVER);
|
|
134
|
+
await bundleServer(join(dotMastraPath, 'index.mjs'));
|
|
135
|
+
/*
|
|
136
|
+
Start server
|
|
137
|
+
*/
|
|
138
|
+
await startServer(dotMastraPath, port, MASTRA_TOOLS_PATH);
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
isRestarting = false;
|
|
142
|
+
}
|
|
86
143
|
}
|
|
87
144
|
export async function dev({ port, env, dir, toolsDirs, }) {
|
|
88
145
|
const dotMastraPath = join(process.cwd(), '.mastra');
|
|
89
146
|
const playgroundServePath = join(dotMastraPath, 'playground');
|
|
90
147
|
const key = env[0]?.name;
|
|
91
148
|
const value = env[0]?.value;
|
|
149
|
+
let envFile = '';
|
|
92
150
|
/*
|
|
93
151
|
Copy playground dist files
|
|
94
152
|
*/
|
|
@@ -100,7 +158,7 @@ export async function dev({ port, env, dir, toolsDirs, }) {
|
|
|
100
158
|
*/
|
|
101
159
|
try {
|
|
102
160
|
const fileService = new FileService();
|
|
103
|
-
|
|
161
|
+
envFile = fileService.getFirstExistingFile(['.env.development', '.env']);
|
|
104
162
|
config({ path: envFile });
|
|
105
163
|
}
|
|
106
164
|
catch (err) {
|
|
@@ -112,86 +170,32 @@ export async function dev({ port, env, dir, toolsDirs, }) {
|
|
|
112
170
|
Bundle mastra
|
|
113
171
|
*/
|
|
114
172
|
const dirPath = dir || path.join(process.cwd(), 'src/mastra');
|
|
115
|
-
|
|
173
|
+
const envPaths = [path.join(process.cwd(), '.env'), path.join(process.cwd(), '.env.development')];
|
|
174
|
+
await bundleMastra(dirPath);
|
|
116
175
|
/*
|
|
117
176
|
Bundle tools
|
|
118
177
|
*/
|
|
119
|
-
const
|
|
120
|
-
const toolsPaths = [...(toolsDirs?.split(',').map(tool => path.join(process.cwd(), tool)) || []), defaultToolsPath];
|
|
121
|
-
const toolPathsWithFileNames = (await Promise.all(toolsPaths.map(async (toolPath) => {
|
|
122
|
-
try {
|
|
123
|
-
const files = await fs.readdir(toolPath);
|
|
124
|
-
return files.map(file => {
|
|
125
|
-
const fullPath = path.join(toolPath, file);
|
|
126
|
-
const fileName = path.parse(file).name;
|
|
127
|
-
const name = fileName === 'index' ? path.basename(path.dirname(fullPath)) : fileName;
|
|
128
|
-
return {
|
|
129
|
-
path: toolPath,
|
|
130
|
-
name,
|
|
131
|
-
fileName,
|
|
132
|
-
};
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
catch (err) {
|
|
136
|
-
if (toolPath === defaultToolsPath) {
|
|
137
|
-
return [];
|
|
138
|
-
}
|
|
139
|
-
console.warn(`Error reading tools directory ${toolPath}:`, err);
|
|
140
|
-
return [];
|
|
141
|
-
}
|
|
142
|
-
}))).flat();
|
|
143
|
-
for (const { path, name, fileName } of toolPathsWithFileNames) {
|
|
144
|
-
await bundle(path, {
|
|
145
|
-
outfile: join(dotMastraPath, 'tools', `${name}.mjs`),
|
|
146
|
-
entryFile: fileName,
|
|
147
|
-
buildName: `${name}`,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
178
|
+
const MASTRA_TOOLS_PATH = await bundleTools(dirPath, dotMastraPath, toolsDirs);
|
|
150
179
|
/*
|
|
151
180
|
Bundle server
|
|
152
181
|
*/
|
|
153
182
|
writeFileSync(join(dotMastraPath, 'index.mjs'), EXPRESS_SERVER);
|
|
154
183
|
await bundleServer(join(dotMastraPath, 'index.mjs'));
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
:
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
});
|
|
169
|
-
if (currentServerProcess.failed) {
|
|
170
|
-
console.error('Server failed to start with error:', currentServerProcess.stderr);
|
|
171
|
-
process.exit(1);
|
|
184
|
+
await startServer(dotMastraPath, port, MASTRA_TOOLS_PATH);
|
|
185
|
+
const watcher = watch([dirPath, ...envPaths], {
|
|
186
|
+
persistent: true,
|
|
187
|
+
ignoreInitial: true,
|
|
188
|
+
});
|
|
189
|
+
watcher.on('change', async () => {
|
|
190
|
+
console.log(`Changes detected, rebundling and restarting server...`);
|
|
191
|
+
await rebundleAndRestart(dirPath, dotMastraPath, port, envFile, toolsDirs);
|
|
192
|
+
});
|
|
193
|
+
process.on('SIGINT', () => {
|
|
194
|
+
console.log('Stopping server...');
|
|
195
|
+
if (currentServerProcess) {
|
|
196
|
+
currentServerProcess.kill();
|
|
172
197
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
watcher.on('change', async () => {
|
|
177
|
-
console.log(`Changes detected`);
|
|
178
|
-
await rebundleAndRestart(dirPath, dotMastraPath, port, toolsDirs);
|
|
179
|
-
});
|
|
180
|
-
process.on('SIGINT', () => {
|
|
181
|
-
console.log('Stopping server...');
|
|
182
|
-
if (currentServerProcess) {
|
|
183
|
-
currentServerProcess.kill();
|
|
184
|
-
}
|
|
185
|
-
watcher.close();
|
|
186
|
-
process.exit(0);
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
catch (err) {
|
|
190
|
-
const execaError = err;
|
|
191
|
-
if (execaError.stderr)
|
|
192
|
-
console.error('Server error output:', execaError.stderr);
|
|
193
|
-
if (execaError.stdout)
|
|
194
|
-
console.error('Server output:', execaError.stdout);
|
|
195
|
-
process.exit(1);
|
|
196
|
-
}
|
|
198
|
+
watcher.close();
|
|
199
|
+
process.exit(0);
|
|
200
|
+
});
|
|
197
201
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"express-server.d.ts","sourceRoot":"","sources":["../../src/templates/express-server.ts"],"names":[],"mappings":"AAGA,OAAO,UAAU,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"express-server.d.ts","sourceRoot":"","sources":["../../src/templates/express-server.ts"],"names":[],"mappings":"AAGA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AA48BzC,eAAO,MAAM,OAAO,oBAAkB,CAAC;AAQvC,eAAe,OAAO,CAAC"}
|
|
@@ -24,6 +24,36 @@ const tools = toolImports.reduce((acc, toolModule) => {
|
|
|
24
24
|
}, {});
|
|
25
25
|
const app = express();
|
|
26
26
|
app.use(express.json());
|
|
27
|
+
// Store connected SSE clients
|
|
28
|
+
let clients = [];
|
|
29
|
+
// SSE endpoint for refresh notifications
|
|
30
|
+
app.get('/refresh-events', (req, res) => {
|
|
31
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
32
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
33
|
+
res.setHeader('Connection', 'keep-alive');
|
|
34
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
35
|
+
// Send initial connection message
|
|
36
|
+
res.write('data: connected\n\n');
|
|
37
|
+
// Add client to list
|
|
38
|
+
clients.push(res);
|
|
39
|
+
// Remove client when connection closes
|
|
40
|
+
req.on('close', () => {
|
|
41
|
+
clients = clients.filter(client => client !== res);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
// Trigger refresh for all clients
|
|
45
|
+
app.post('/__refresh', (_req, res) => {
|
|
46
|
+
clients.forEach(client => {
|
|
47
|
+
try {
|
|
48
|
+
client.write('data: refresh\n\n');
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
// Remove failed clients
|
|
52
|
+
clients = clients.filter(c => c !== client);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
res.sendStatus(200);
|
|
56
|
+
});
|
|
27
57
|
// Swagger configuration
|
|
28
58
|
const options = {
|
|
29
59
|
info: {
|
|
@@ -133,11 +163,14 @@ app.get('/api/agents', async (_req, res) => {
|
|
|
133
163
|
}
|
|
134
164
|
});
|
|
135
165
|
/**
|
|
136
|
-
* POST /api/agents
|
|
137
|
-
* @summary Send
|
|
166
|
+
* POST /api/agents/:agentId/generate
|
|
167
|
+
* @summary Send messages to agent
|
|
138
168
|
* @tags Agent
|
|
139
169
|
* @param {string} agentId.path.required - Agent identifier
|
|
140
170
|
* @param {Messages} request.body.required - Messages to send
|
|
171
|
+
* @param {string} request.body.optional - Thread ID
|
|
172
|
+
* @param {string} request.body.optional - Resource ID
|
|
173
|
+
* @param {string} request.body.optional - Output schema
|
|
141
174
|
* @return {object} 200 - Agent response
|
|
142
175
|
* @return {Error} 400 - Validation error
|
|
143
176
|
* @return {Error} 500 - Server error
|
|
@@ -146,7 +179,7 @@ app.post('/api/agents/:agentId/generate', async (req, res) => {
|
|
|
146
179
|
try {
|
|
147
180
|
const agentId = req.params.agentId;
|
|
148
181
|
const agent = mastra.getAgent(agentId);
|
|
149
|
-
const { messages, threadId, resourceid } = req.body;
|
|
182
|
+
const { messages, threadId, resourceid, output } = req.body;
|
|
150
183
|
const { ok, errorResponse } = await validateBody({
|
|
151
184
|
messages,
|
|
152
185
|
});
|
|
@@ -154,26 +187,33 @@ app.post('/api/agents/:agentId/generate', async (req, res) => {
|
|
|
154
187
|
res.status(400).json({ error: errorResponse });
|
|
155
188
|
return;
|
|
156
189
|
}
|
|
157
|
-
const result = await agent.generate(messages, { threadId, resourceid });
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
190
|
+
const result = await agent.generate(messages, { output, threadId, resourceid });
|
|
191
|
+
if (output) {
|
|
192
|
+
res.json({
|
|
193
|
+
object: result.object,
|
|
194
|
+
agent: result.agent,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
res.json({ text: result.text, agent: agent.name });
|
|
199
|
+
}
|
|
163
200
|
}
|
|
164
201
|
catch (error) {
|
|
165
202
|
const apiError = error;
|
|
166
|
-
console.error('Error
|
|
167
|
-
res.status(apiError.status || 500).json({ error: apiError.message || 'Error
|
|
203
|
+
console.error('Error generating from agent', apiError);
|
|
204
|
+
res.status(apiError.status || 500).json({ error: apiError.message || 'Error generating from agent' });
|
|
168
205
|
return;
|
|
169
206
|
}
|
|
170
207
|
});
|
|
171
208
|
/**
|
|
172
|
-
* POST /api/agents
|
|
209
|
+
* POST /api/agents/:agentId/stream
|
|
173
210
|
* @summary Stream messages to agent
|
|
174
211
|
* @tags Agent
|
|
175
212
|
* @param {string} agentId.path.required - Agent identifier
|
|
176
213
|
* @param {Messages} request.body.required - Messages to stream
|
|
214
|
+
* @param {string} request.body.optional - Thread ID
|
|
215
|
+
* @param {string} request.body.optional - Resource ID
|
|
216
|
+
* @param {string} request.body.optional - Output schema
|
|
177
217
|
* @return {stream} 200 - Agent response stream
|
|
178
218
|
* @return {Error} 400 - Validation error
|
|
179
219
|
* @return {Error} 500 - Server error
|
|
@@ -182,7 +222,7 @@ app.post('/api/agents/:agentId/stream', async (req, res) => {
|
|
|
182
222
|
try {
|
|
183
223
|
const agentId = req.params.agentId;
|
|
184
224
|
const agent = mastra.getAgent(agentId);
|
|
185
|
-
const { messages, threadId, resourceid } = req.body;
|
|
225
|
+
const { messages, threadId, resourceid, output } = req.body;
|
|
186
226
|
const { ok, errorResponse } = await validateBody({
|
|
187
227
|
messages,
|
|
188
228
|
});
|
|
@@ -191,6 +231,7 @@ app.post('/api/agents/:agentId/stream', async (req, res) => {
|
|
|
191
231
|
return;
|
|
192
232
|
}
|
|
193
233
|
const streamResult = await agent.stream(messages, {
|
|
234
|
+
output,
|
|
194
235
|
threadId,
|
|
195
236
|
resourceid,
|
|
196
237
|
});
|
|
@@ -203,112 +244,6 @@ app.post('/api/agents/:agentId/stream', async (req, res) => {
|
|
|
203
244
|
return;
|
|
204
245
|
}
|
|
205
246
|
});
|
|
206
|
-
/**
|
|
207
|
-
* POST /api/agents/{agentId}/text-object
|
|
208
|
-
* @summary Get structured output from agent
|
|
209
|
-
* @tags Agent
|
|
210
|
-
* @param {string} agentId.path.required - Agent identifier
|
|
211
|
-
* @param {TextObjectRequest} request.body.required - Request with messages and schema spec
|
|
212
|
-
* @return {object} 200 - Structured output response
|
|
213
|
-
* @return {Error} 400 - Validation error
|
|
214
|
-
* @return {Error} 500 - Server error
|
|
215
|
-
*/
|
|
216
|
-
app.post('/api/agents/:agentId/text-object', async (req, res) => {
|
|
217
|
-
try {
|
|
218
|
-
const agentId = req.params.agentId;
|
|
219
|
-
const agent = mastra.getAgent(agentId);
|
|
220
|
-
const { messages, schema, threadId, resourceid } = req.body;
|
|
221
|
-
const { ok, errorResponse } = await validateBody({
|
|
222
|
-
messages,
|
|
223
|
-
schema,
|
|
224
|
-
});
|
|
225
|
-
if (!ok) {
|
|
226
|
-
res.status(400).json({ error: errorResponse });
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
const result = await agent.generate(messages, { output: schema, threadId, resourceid });
|
|
230
|
-
const objectResult = {
|
|
231
|
-
object: result.object,
|
|
232
|
-
agent: result.agent,
|
|
233
|
-
};
|
|
234
|
-
res.json(objectResult);
|
|
235
|
-
}
|
|
236
|
-
catch (error) {
|
|
237
|
-
const apiError = error;
|
|
238
|
-
console.error('Error getting structured output from agent', apiError);
|
|
239
|
-
res
|
|
240
|
-
.status(apiError.status || 500)
|
|
241
|
-
.json({ error: apiError.message || 'Error getting structured output from agent' });
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
/**
|
|
246
|
-
* POST /api/agents/{agentId}/stream-object
|
|
247
|
-
* @summary Stream structured output from agent
|
|
248
|
-
* @tags Agent
|
|
249
|
-
* @param {string} agentId.path.required - Agent identifier
|
|
250
|
-
* @param {TextObjectRequest} request.body.required - Request with messages and schema spec
|
|
251
|
-
* @return {stream} 200 - Structured output stream
|
|
252
|
-
* @return {Error} 400 - Validation error
|
|
253
|
-
* @return {Error} 500 - Server error
|
|
254
|
-
*/
|
|
255
|
-
app.post('/api/agents/:agentId/stream-object', async (req, res) => {
|
|
256
|
-
try {
|
|
257
|
-
const agentId = req.params.agentId;
|
|
258
|
-
const agent = mastra.getAgent(agentId);
|
|
259
|
-
const { messages, schema, threadId, resourceid } = req.body;
|
|
260
|
-
const { ok, errorResponse } = await validateBody({
|
|
261
|
-
messages,
|
|
262
|
-
schema,
|
|
263
|
-
});
|
|
264
|
-
if (!ok) {
|
|
265
|
-
res.status(400).json({ error: errorResponse });
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
const streamResult = await agent.stream(messages, { output: schema, threadId, resourceid });
|
|
269
|
-
streamResult.pipeTextStreamToResponse(res);
|
|
270
|
-
}
|
|
271
|
-
catch (error) {
|
|
272
|
-
const apiError = error;
|
|
273
|
-
console.error('Error streaming structured output from agent', apiError);
|
|
274
|
-
res
|
|
275
|
-
.status(apiError.status || 500)
|
|
276
|
-
.json({ error: apiError.message || 'Error streaming structured output from agent' });
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
/**
|
|
281
|
-
* POST /api/agents/{agentId}/tools/{toolId}/execute
|
|
282
|
-
* @summary Execute an Agent tool
|
|
283
|
-
* @tags Agent
|
|
284
|
-
* @param {string} agentId.path.required - Agent identifier
|
|
285
|
-
* @param {string} toolId.path.required - Tool identifier
|
|
286
|
-
* @param {object} request.body.required - Tool input data
|
|
287
|
-
* @return {object} 200 - Tool execution result
|
|
288
|
-
* @return {Error} 500 - Server error
|
|
289
|
-
*/
|
|
290
|
-
app.post('/api/agents/:agentId/tools/:toolId/execute', async (req, res) => {
|
|
291
|
-
try {
|
|
292
|
-
const agentId = req.params.agentId;
|
|
293
|
-
const toolId = req.params.toolId;
|
|
294
|
-
const agent = mastra.getAgent(agentId);
|
|
295
|
-
const tool = Object.values(agent?.tools || {}).find((tool) => tool.id === toolId);
|
|
296
|
-
const result = await tool.execute({
|
|
297
|
-
context: {
|
|
298
|
-
...req.body,
|
|
299
|
-
},
|
|
300
|
-
mastra,
|
|
301
|
-
runId: agentId,
|
|
302
|
-
});
|
|
303
|
-
res.json(result);
|
|
304
|
-
}
|
|
305
|
-
catch (error) {
|
|
306
|
-
const apiError = error;
|
|
307
|
-
console.error('Error executing tool', apiError);
|
|
308
|
-
res.status(apiError.status || 500).json({ error: apiError.message || 'Error executing tool' });
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
247
|
/**
|
|
313
248
|
* GET /api/workflows
|
|
314
249
|
* @summary Get all workflows
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"netlify.d.ts","sourceRoot":"","sources":["../../src/templates/netlify.ts"],"names":[],"mappings":"AAGA,OAAO,UAAU,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"netlify.d.ts","sourceRoot":"","sources":["../../src/templates/netlify.ts"],"names":[],"mappings":"AAGA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAuqBzC,eAAO,MAAM,OAAO,oBAAkB,CAAC"}
|
|
@@ -92,21 +92,27 @@ app.post('/api/agents/:agentId/generate', async (req, res) => {
|
|
|
92
92
|
try {
|
|
93
93
|
const agentId = req.params.agentId;
|
|
94
94
|
const agent = mastra.getAgent(agentId);
|
|
95
|
-
const { messages, threadId, resourceid } = req.body;
|
|
96
|
-
const { ok, errorResponse } = await validateBody({
|
|
97
|
-
messages,
|
|
98
|
-
});
|
|
95
|
+
const { messages, threadId, resourceid, output } = req.body;
|
|
96
|
+
const { ok, errorResponse } = await validateBody({ messages });
|
|
99
97
|
if (!ok) {
|
|
100
98
|
res.status(400).json({ error: errorResponse });
|
|
101
99
|
return;
|
|
102
100
|
}
|
|
103
|
-
const result = await agent.generate(messages, { threadId, resourceid });
|
|
104
|
-
|
|
101
|
+
const result = await agent.generate(messages, { output, threadId, resourceid });
|
|
102
|
+
if (output) {
|
|
103
|
+
res.json({
|
|
104
|
+
object: result.object,
|
|
105
|
+
agent: result.agent,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
res.json({ text: result.text, agent: agent.name });
|
|
110
|
+
}
|
|
105
111
|
}
|
|
106
112
|
catch (error) {
|
|
107
113
|
const apiError = error;
|
|
108
|
-
console.error('Error
|
|
109
|
-
res.status(apiError.status || 500).json({ error: apiError.message || 'Error
|
|
114
|
+
console.error('Error generating from agent', apiError);
|
|
115
|
+
res.status(apiError.status || 500).json({ error: apiError.message || 'Error generating from agent' });
|
|
110
116
|
return;
|
|
111
117
|
}
|
|
112
118
|
});
|
|
@@ -114,7 +120,7 @@ app.post('/api/agents/:agentId/stream', async (req, res) => {
|
|
|
114
120
|
try {
|
|
115
121
|
const agentId = req.params.agentId;
|
|
116
122
|
const agent = mastra.getAgent(agentId);
|
|
117
|
-
const { messages, threadId, resourceid } = req.body;
|
|
123
|
+
const { messages, threadId, resourceid, output } = req.body;
|
|
118
124
|
const { ok, errorResponse } = await validateBody({
|
|
119
125
|
messages,
|
|
120
126
|
});
|
|
@@ -123,6 +129,7 @@ app.post('/api/agents/:agentId/stream', async (req, res) => {
|
|
|
123
129
|
return;
|
|
124
130
|
}
|
|
125
131
|
const streamResult = await agent.stream(messages, {
|
|
132
|
+
output,
|
|
126
133
|
threadId,
|
|
127
134
|
resourceid,
|
|
128
135
|
});
|
|
@@ -135,56 +142,6 @@ app.post('/api/agents/:agentId/stream', async (req, res) => {
|
|
|
135
142
|
return;
|
|
136
143
|
}
|
|
137
144
|
});
|
|
138
|
-
app.post('/api/agents/:agentId/text-object', async (req, res) => {
|
|
139
|
-
try {
|
|
140
|
-
const agentId = req.params.agentId;
|
|
141
|
-
const agent = mastra.getAgent(agentId);
|
|
142
|
-
const { messages, schema, threadId, resourceid } = req.body;
|
|
143
|
-
const { ok, errorResponse } = await validateBody({
|
|
144
|
-
messages,
|
|
145
|
-
schema,
|
|
146
|
-
});
|
|
147
|
-
if (!ok) {
|
|
148
|
-
res.status(400).json({ error: errorResponse });
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
const result = await agent.generate(messages, { output: schema, threadId, resourceid });
|
|
152
|
-
res.json(result);
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
const apiError = error;
|
|
156
|
-
console.error('Error getting structured output from agent', apiError);
|
|
157
|
-
res
|
|
158
|
-
.status(apiError.status || 500)
|
|
159
|
-
.json({ error: apiError.message || 'Error getting structured output from agent' });
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
app.post('/api/agents/:agentId/stream-object', async (req, res) => {
|
|
164
|
-
try {
|
|
165
|
-
const agentId = req.params.agentId;
|
|
166
|
-
const agent = mastra.getAgent(agentId);
|
|
167
|
-
const { messages, schema, threadId, resourceid } = req.body;
|
|
168
|
-
const { ok, errorResponse } = await validateBody({
|
|
169
|
-
messages,
|
|
170
|
-
schema,
|
|
171
|
-
});
|
|
172
|
-
if (!ok) {
|
|
173
|
-
res.status(400).json({ error: errorResponse });
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const streamResult = await agent.stream(messages, { output: schema, threadId, resourceid });
|
|
177
|
-
streamResult.pipeTextStreamToResponse(res);
|
|
178
|
-
}
|
|
179
|
-
catch (error) {
|
|
180
|
-
const apiError = error;
|
|
181
|
-
console.error('Error streaming structured output from agent', apiError);
|
|
182
|
-
res
|
|
183
|
-
.status(apiError.status || 500)
|
|
184
|
-
.json({ error: apiError.message || 'Error streaming structured output from agent' });
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
145
|
app.post('/api/agents/:agentId/tools/:toolId/execute', async (req, res) => {
|
|
189
146
|
try {
|
|
190
147
|
const agentId = req.params.agentId;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/templates/worker.ts"],"names":[],"mappings":"AA8CA,UAAU,gBAAgB;IACxB,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IACvC,sBAAsB,IAAI,IAAI,CAAC;CAChC;;
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/templates/worker.ts"],"names":[],"mappings":"AA8CA,UAAU,gBAAgB;IACxB,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IACvC,sBAAsB,IAAI,IAAI,CAAC;CAChC;;mBAm8BsB,OAAO,OAAO,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,gBAAgB;;AADlF,wBAQE"}
|