-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.ts
More file actions
809 lines (699 loc) · 24.4 KB
/
agents.ts
File metadata and controls
809 lines (699 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
import { Hono } from "@hono/hono";
import type { Context } from "@hono/hono";
import Anthropic from "@anthropic-ai/sdk";
import {
getApp,
createAgent,
getAgent,
listAgents,
updateAgent,
deleteAgent,
createGuideline,
getGuideline,
listGuidelines,
updateGuideline,
deleteGuideline,
createMcpServer,
getMcpServer,
listMcpServers,
updateMcpServer,
deleteMcpServer,
linkAgentGuideline,
unlinkAgentGuideline,
getAgentGuidelines,
linkAgentMcpServer,
unlinkAgentMcpServer,
getAgentMcpServers,
createAgentConversation,
listAgentConversations,
getConversation,
addMessage,
getMessages,
updateConversationTitle,
logUsage,
createAppConnection,
listAppConnections,
getAppConnection,
getAllAppSchemas,
getFullTableName,
insertRow,
} from "./db.ts";
import { buildSystemContext, resolveModel } from "./chat.ts";
import {
discoverAllTools,
mcpToolsToAnthropicTools,
resolveToolServer,
callTool,
type NamespacedMcpTool,
} from "./mcp.ts";
import { queryExternalDb, fetchExternalApi } from "./connections.ts";
const MAX_TOOL_ITERATIONS = 10;
// Native tool definitions for external data connections
export const NATIVE_TOOLS: Anthropic.Tool[] = [
{
name: "save_connection",
description: "Save an external database or API connection for this app. Supported types: postgresql, rest_api.",
input_schema: {
type: "object" as const,
properties: {
type: { type: "string", enum: ["postgresql", "rest_api"], description: "Connection type" },
name: { type: "string", description: "Human-readable name for this connection" },
connection_string: { type: "string", description: "Connection string (e.g. postgresql://user:pass@host/db) or base URL for REST API" },
config: { type: "object", description: "Optional config (api_key, headers, etc.)" },
},
required: ["type", "name", "connection_string"],
},
},
{
name: "list_connections",
description: "List all external data connections for this app.",
input_schema: { type: "object" as const, properties: {} },
},
{
name: "query_database",
description: "Execute a read-only SQL query against an external PostgreSQL database connection. Only SELECT queries are allowed.",
input_schema: {
type: "object" as const,
properties: {
connection_id: { type: "string", description: "The connection ID to query" },
sql: { type: "string", description: "SQL SELECT query to execute" },
},
required: ["connection_id", "sql"],
},
},
{
name: "fetch_api",
description: "Make a GET request to an external REST API connection.",
input_schema: {
type: "object" as const,
properties: {
connection_id: { type: "string", description: "The connection ID" },
path: { type: "string", description: "API path to fetch (e.g. /users or /orders?limit=10)" },
},
required: ["connection_id", "path"],
},
},
{
name: "import_data",
description: "Query an external database and import the results into this app's SQLite table. Maps columns automatically.",
input_schema: {
type: "object" as const,
properties: {
connection_id: { type: "string", description: "The database connection ID" },
sql: { type: "string", description: "SQL SELECT query (or omit for SELECT * FROM target_table LIMIT 200)" },
target_table: { type: "string", description: "Name of the app's table to insert data into" },
},
required: ["connection_id", "target_table"],
},
},
];
export const NATIVE_TOOL_NAMES = new Set(NATIVE_TOOLS.map((t) => t.name));
export async function handleNativeTool(
toolName: string,
input: Record<string, unknown>,
appId: string,
): Promise<string> {
switch (toolName) {
case "save_connection": {
const id = crypto.randomUUID();
const conn = createAppConnection(
id,
appId,
input.type as string,
input.name as string,
input.connection_string as string,
(input.config as Record<string, unknown>) ?? undefined,
);
return JSON.stringify({ success: true, connection: { id: conn.id, name: conn.name, type: conn.type, status: conn.status } });
}
case "list_connections": {
const connections = listAppConnections(appId);
return JSON.stringify(connections.map((c) => ({
id: c.id,
name: c.name,
type: c.type,
status: c.status,
connection_string: c.connection_string ? c.connection_string.replace(/\/\/[^@]+@/, "//***@") : null,
})));
}
case "query_database": {
const result = await queryExternalDb(
input.connection_id as string,
input.sql as string,
);
return JSON.stringify(result);
}
case "fetch_api": {
const result = await fetchExternalApi(
input.connection_id as string,
input.path as string,
);
return JSON.stringify(result);
}
case "import_data": {
const connId = input.connection_id as string;
const targetTable = input.target_table as string;
const sql = (input.sql as string) || `SELECT * FROM ${targetTable} LIMIT 200`;
// Query external DB
const result = await queryExternalDb(connId, sql);
if (result.rows.length === 0) {
return JSON.stringify({ success: true, imported: 0, message: "No rows returned from query" });
}
// Get app's table schema to validate target exists
const schemas = getAllAppSchemas(appId);
const targetSchema = schemas.find((s) => s.tableName === targetTable);
if (!targetSchema) {
return JSON.stringify({ success: false, error: `Table "${targetTable}" not found in this app` });
}
// Map and insert rows
const ftn = getFullTableName(appId, targetTable);
const validColumns = new Set(targetSchema.columns.map((c) => c.name));
let imported = 0;
let failed = 0;
for (const row of result.rows) {
// Filter to only columns that exist in target table
const mapped: Record<string, unknown> = {};
for (const [key, val] of Object.entries(row)) {
if (validColumns.has(key)) {
mapped[key] = val;
}
}
if (Object.keys(mapped).length === 0) continue;
try {
insertRow(ftn, mapped);
imported++;
} catch {
failed++;
}
}
return JSON.stringify({ success: true, imported, failed, total: result.rows.length });
}
default:
throw new Error(`Unknown native tool: ${toolName}`);
}
}
interface AgentLoopResult {
role: "assistant";
content: string;
tool_calls_made: number;
}
async function runAgentLoop(
client: Anthropic,
appId: string,
appTitle: string,
agentId: string,
messages: { role: "user" | "assistant"; content: string }[],
modelOverride?: string,
temperatureOverride?: number,
): Promise<AgentLoopResult> {
const agent = getAgent(agentId);
if (!agent) throw new Error("Agent not found");
// Build system prompt: static_prompt + guidelines + data context
const guidelines = getAgentGuidelines(agentId);
let systemBase = agent.static_prompt || "";
if (guidelines.length > 0) {
systemBase += "\n\nGUIDELINES:\n";
for (const g of guidelines) {
systemBase += `\n--- ${g.name} ---\n${g.content}\n`;
}
}
const system = buildSystemContext(appId, appTitle, systemBase || undefined);
// Discover MCP tools — deduplicate by name
const mcpServers = getAgentMcpServers(agentId);
let tools: NamespacedMcpTool[] = [];
const toolsByName = new Map<string, Anthropic.Tool>();
// Always include native connection tools
for (const t of NATIVE_TOOLS) toolsByName.set(t.name, t);
if (mcpServers.length > 0) {
tools = await discoverAllTools(mcpServers);
for (const t of mcpToolsToAnthropicTools(tools) as Anthropic.Tool[]) {
toolsByName.set(t.name, t);
}
}
const anthropicTools: Anthropic.Tool[] = Array.from(toolsByName.values());
const model = resolveModel(modelOverride || agent.model);
const temperature = temperatureOverride ?? agent.temperature ?? undefined;
let currentMessages: Anthropic.MessageParam[] = messages.map((m) => ({
role: m.role,
content: m.content,
}));
let toolCallsMade = 0;
for (let iteration = 0; iteration < MAX_TOOL_ITERATIONS + 1; iteration++) {
const response = await client.messages.create({
model,
max_tokens: 4096,
system,
messages: currentMessages,
tools: anthropicTools,
...(temperature !== undefined ? { temperature } : {}),
});
logUsage(appId, "agent", model, response.usage.input_tokens, response.usage.output_tokens);
// Check if response has tool_use blocks
const toolUseBlocks = response.content.filter(
(b): b is Anthropic.ToolUseBlock => b.type === "tool_use",
);
if (toolUseBlocks.length === 0 || iteration >= MAX_TOOL_ITERATIONS) {
// No tool use or max iterations — extract final text
const text = response.content
.filter((b): b is Anthropic.TextBlock => b.type === "text")
.map((b) => b.text)
.join("");
return {
role: "assistant",
content: text,
tool_calls_made: toolCallsMade,
};
}
// Execute tool calls
currentMessages = [
...currentMessages,
{ role: "assistant" as const, content: response.content },
];
const toolResults: Anthropic.ToolResultBlockParam[] = [];
for (const toolUse of toolUseBlocks) {
toolCallsMade++;
// Check if this is a native tool first
if (NATIVE_TOOL_NAMES.has(toolUse.name)) {
try {
const result = await handleNativeTool(
toolUse.name,
(toolUse.input as Record<string, unknown>) || {},
appId,
);
toolResults.push({
type: "tool_result",
tool_use_id: toolUse.id,
content: result,
});
} catch (e) {
toolResults.push({
type: "tool_result",
tool_use_id: toolUse.id,
content: `Tool call failed: ${e instanceof Error ? e.message : String(e)}`,
is_error: true,
});
}
continue;
}
// Otherwise try MCP tools
const resolved = resolveToolServer(toolUse.name, tools);
if (!resolved) {
toolResults.push({
type: "tool_result",
tool_use_id: toolUse.id,
content: `Error: Unknown tool "${toolUse.name}"`,
is_error: true,
});
continue;
}
try {
const result = await callTool(
{ url: resolved.serverUrl, api_key: resolved.serverApiKey },
resolved.toolName,
(toolUse.input as Record<string, unknown>) || {},
);
toolResults.push({
type: "tool_result",
tool_use_id: toolUse.id,
content: typeof result === "string" ? result : JSON.stringify(result),
});
} catch (e) {
toolResults.push({
type: "tool_result",
tool_use_id: toolUse.id,
content: `Tool call failed: ${e instanceof Error ? e.message : String(e)}`,
is_error: true,
});
}
}
currentMessages = [
...currentMessages,
{ role: "user" as const, content: toolResults },
];
}
// Should not reach here, but just in case
return { role: "assistant", content: "", tool_calls_made: toolCallsMade };
}
export function createAgentRoutes(client: Anthropic): Hono {
const router = new Hono();
// === Agent CRUD ===
router.post("/api/apps/:appId/agents", async (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
let body: {
name?: string;
static_prompt?: string;
model?: string;
temperature?: number;
};
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.name) return c.json({ error: "name is required" }, 400);
const id = crypto.randomUUID();
createAgent(
id,
appId,
body.name,
body.static_prompt || "",
body.model,
body.temperature,
);
return c.json(getAgent(id), 201);
});
router.get("/api/apps/:appId/agents", (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
return c.json(listAgents(appId));
});
router.get("/api/apps/:appId/agents/:agentId", (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
return c.json({
...agent,
guidelines: getAgentGuidelines(agent.id),
mcp_servers: getAgentMcpServers(agent.id),
});
});
router.post("/api/apps/:appId/agents/:agentId", async (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
let body: {
name?: string;
static_prompt?: string;
model?: string;
temperature?: number;
};
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
updateAgent(agent.id, body);
return c.json(getAgent(agent.id));
});
router.post("/api/apps/:appId/agents/:agentId/delete", (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
deleteAgent(agent.id);
return c.json({ ok: true });
});
// === Guideline CRUD ===
router.post("/api/apps/:appId/guidelines", async (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
let body: { name?: string; content?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.name) return c.json({ error: "name is required" }, 400);
const id = crypto.randomUUID();
createGuideline(id, appId, body.name, body.content || "");
return c.json(getGuideline(id), 201);
});
router.get("/api/apps/:appId/guidelines", (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
return c.json(listGuidelines(appId));
});
router.get("/api/apps/:appId/guidelines/:guidelineId", (c: Context) => {
const guideline = getGuideline(c.req.param("guidelineId"));
if (!guideline) return c.json({ error: "Guideline not found" }, 404);
return c.json(guideline);
});
router.post(
"/api/apps/:appId/guidelines/:guidelineId",
async (c: Context) => {
const guideline = getGuideline(c.req.param("guidelineId"));
if (!guideline) return c.json({ error: "Guideline not found" }, 404);
let body: { name?: string; content?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
updateGuideline(guideline.id, body);
return c.json(getGuideline(guideline.id));
},
);
router.post(
"/api/apps/:appId/guidelines/:guidelineId/delete",
(c: Context) => {
const guideline = getGuideline(c.req.param("guidelineId"));
if (!guideline) return c.json({ error: "Guideline not found" }, 404);
deleteGuideline(guideline.id);
return c.json({ ok: true });
},
);
// === MCP Server CRUD ===
router.post("/api/apps/:appId/mcp-servers", async (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
let body: { name?: string; url?: string; api_key?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.name) return c.json({ error: "name is required" }, 400);
if (!body.url) return c.json({ error: "url is required" }, 400);
const id = crypto.randomUUID();
createMcpServer(id, appId, body.name, body.url, body.api_key);
return c.json(getMcpServer(id), 201);
});
router.get("/api/apps/:appId/mcp-servers", (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
return c.json(listMcpServers(appId));
});
router.get("/api/apps/:appId/mcp-servers/:serverId", (c: Context) => {
const server = getMcpServer(c.req.param("serverId"));
if (!server) return c.json({ error: "MCP server not found" }, 404);
return c.json(server);
});
router.post("/api/apps/:appId/mcp-servers/:serverId", async (c: Context) => {
const server = getMcpServer(c.req.param("serverId"));
if (!server) return c.json({ error: "MCP server not found" }, 404);
let body: { name?: string; url?: string; api_key?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
updateMcpServer(server.id, body);
return c.json(getMcpServer(server.id));
});
router.post("/api/apps/:appId/mcp-servers/:serverId/delete", (c: Context) => {
const server = getMcpServer(c.req.param("serverId"));
if (!server) return c.json({ error: "MCP server not found" }, 404);
deleteMcpServer(server.id);
return c.json({ ok: true });
});
// === Junction Routes: Agent <-> Guidelines ===
router.post(
"/api/apps/:appId/agents/:agentId/guidelines",
async (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
let body: { guideline_id?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.guideline_id)
return c.json({ error: "guideline_id is required" }, 400);
const guideline = getGuideline(body.guideline_id);
if (!guideline) return c.json({ error: "Guideline not found" }, 404);
linkAgentGuideline(agent.id, guideline.id);
return c.json({ ok: true });
},
);
router.post(
"/api/apps/:appId/agents/:agentId/guidelines/delete",
async (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
let body: { guideline_id?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.guideline_id)
return c.json({ error: "guideline_id is required" }, 400);
unlinkAgentGuideline(agent.id, body.guideline_id);
return c.json({ ok: true });
},
);
router.get("/api/apps/:appId/agents/:agentId/guidelines", (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
return c.json(getAgentGuidelines(agent.id));
});
// === Junction Routes: Agent <-> MCP Servers ===
router.post(
"/api/apps/:appId/agents/:agentId/mcp-servers",
async (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
let body: { mcp_server_id?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.mcp_server_id)
return c.json({ error: "mcp_server_id is required" }, 400);
const server = getMcpServer(body.mcp_server_id);
if (!server) return c.json({ error: "MCP server not found" }, 404);
linkAgentMcpServer(agent.id, server.id);
return c.json({ ok: true });
},
);
router.post(
"/api/apps/:appId/agents/:agentId/mcp-servers/delete",
async (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
let body: { mcp_server_id?: string };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.mcp_server_id)
return c.json({ error: "mcp_server_id is required" }, 400);
unlinkAgentMcpServer(agent.id, body.mcp_server_id);
return c.json({ ok: true });
},
);
router.get("/api/apps/:appId/agents/:agentId/mcp-servers", (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
return c.json(getAgentMcpServers(agent.id));
});
// === Stateless Invocation ===
router.post("/api/apps/:appId/agents/:agentId/invoke", async (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
let body: { prompt?: string; model?: string; temperature?: number };
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
if (!body.prompt) return c.json({ error: "prompt is required" }, 400);
try {
const result = await runAgentLoop(
client,
appId,
app.title,
agent.id,
[{ role: "user", content: body.prompt }],
body.model,
body.temperature,
);
return c.json(result);
} catch (e) {
const msg = e instanceof Error ? e.message : "Agent invocation failed";
return c.json({ error: msg }, 500);
}
});
// === Persistent Conversations ===
router.post(
"/api/apps/:appId/agents/:agentId/conversations",
async (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
let body: { title?: string } = {};
try {
body = await c.req.json();
} catch {
// empty body is fine
}
const id = crypto.randomUUID();
const title = body.title || "New conversation";
createAgentConversation(id, appId, agent.id, title);
return c.json({ id, title }, 201);
},
);
router.get("/api/apps/:appId/agents/:agentId/conversations", (c: Context) => {
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
return c.json(listAgentConversations(agent.id));
});
router.post(
"/api/apps/:appId/agents/:agentId/conversations/:convId/messages",
async (c: Context) => {
const appId = c.req.param("appId");
const app = getApp(appId);
if (!app) return c.json({ error: "App not found" }, 404);
const agent = getAgent(c.req.param("agentId"));
if (!agent) return c.json({ error: "Agent not found" }, 404);
const convId = c.req.param("convId");
const convo = getConversation(convId);
if (!convo) return c.json({ error: "Conversation not found" }, 404);
let body: {
content?: string;
message?: string;
model?: string;
temperature?: number;
};
try {
body = await c.req.json();
} catch {
return c.json({ error: "Invalid JSON body" }, 400);
}
const userContent = (body.content || body.message || "").trim();
if (!userContent) return c.json({ error: "content is required" }, 400);
addMessage(convId, "user", userContent);
const allMsgs = getMessages(convId);
const messages = allMsgs.map((m) => ({
role: m.role as "user" | "assistant",
content: m.content,
}));
try {
const result = await runAgentLoop(
client,
appId,
app.title,
agent.id,
messages,
body.model,
body.temperature,
);
addMessage(convId, "assistant", result.content);
// Auto-title on first message
if (allMsgs.length <= 1) {
const shortTitle =
userContent.length > 60
? userContent.slice(0, 57) + "..."
: userContent;
updateConversationTitle(convId, shortTitle);
}
return c.json(result);
} catch (e) {
const msg = e instanceof Error ? e.message : "Agent invocation failed";
return c.json({ error: msg }, 500);
}
},
);
return router;
}