feat(server): inference state-graph + supervisor, memory tools, MCP client, schema, routes
- Add state-graph.ts: typed state machine for inference lifecycle - Add supervisor.ts: agent supervisor pattern for multi-agent coordination - Add export-formatter.ts: structured export formatting - Add manage_memory.ts: memory CRUD tool for agent persistence - Add get_wiki_article.ts: codecontext wiki article retrieval - Extend memory/index.ts: 3-tier memory (context/daily/core) - Extend MCP client: mcp-config.ts env-var substitution - Update schema.sql: agent_sessions, tasks, pending_changes extensions - Update API types: MessageMetadata, ErrorReason, AgentSessionConfig - Update routes: chats, messages, sessions — column renames and agent_session_id - Update inference: error handler, payload builder, stream phase, turn orchestrator
This commit is contained in:
@@ -3,12 +3,13 @@ import { z } from 'zod';
|
||||
import type { Sql } from '../db.js';
|
||||
import type { Config } from '../config.js';
|
||||
import type { Broker } from '../services/broker.js';
|
||||
import type { Chat, Message, Session, ToolCall } from '../types/api.js';
|
||||
import type { Chat, Message, MessageMetadata, Session, ToolCall } from '../types/api.js';
|
||||
// v1.13.17-cross-repo-reads: grant_read_access resolves the grant root at
|
||||
// decision time (not at request time) so concurrent project changes don't
|
||||
// stale-bind the resolution.
|
||||
import { resolveGrantRoot } from '../services/grant_resolver.js';
|
||||
import { MESSAGE_COLUMNS } from '../services/message-columns.js';
|
||||
import { setServerPermission, getServerName } from '../services/mcp-client.js';
|
||||
|
||||
// Shared lookup for the answer_user_input + grant_read_access pause-resume
|
||||
// endpoints. Finds the originating assistant tool_call by id in message_parts,
|
||||
@@ -846,4 +847,117 @@ export function registerMessageRoutes(
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// v1.15.0-mcp-permission: approve/deny MCP tool calls for 'ask' state servers.
|
||||
const McpApproveBody = z.object({
|
||||
tool_call_id: z.string().min(1),
|
||||
permission: z.enum(['allow_once', 'allow_always', 'deny']),
|
||||
});
|
||||
|
||||
app.post<{ Params: { id: string } }>(
|
||||
'/api/chats/:id/mcp-approve',
|
||||
async (req, reply) => {
|
||||
const parsed = McpApproveBody.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
reply.code(400);
|
||||
return { error: 'invalid body', details: parsed.error.flatten() };
|
||||
}
|
||||
const { tool_call_id, permission } = parsed.data;
|
||||
|
||||
const chatRows = await sql<{ id: string }[]>`
|
||||
SELECT id FROM chats WHERE id = ${req.params.id} AND status = 'open'
|
||||
`;
|
||||
if (chatRows.length === 0) {
|
||||
reply.code(404);
|
||||
return { error: 'chat_not_found' };
|
||||
}
|
||||
|
||||
// Look up the tool call to get the prefixed tool name
|
||||
const callerRows = await sql<{
|
||||
payload: { name: string };
|
||||
}[]>`
|
||||
SELECT p.payload
|
||||
FROM message_parts p
|
||||
JOIN messages m ON m.id = p.message_id
|
||||
WHERE m.chat_id = ${req.params.id}
|
||||
AND m.role = 'assistant'
|
||||
AND p.kind = 'tool_call'
|
||||
AND p.payload->>'id' = ${tool_call_id}
|
||||
ORDER BY m.created_at DESC
|
||||
LIMIT 1
|
||||
`;
|
||||
const callerRow = callerRows[0];
|
||||
if (!callerRow) {
|
||||
reply.code(404);
|
||||
return { error: 'tool_call_not_found' };
|
||||
}
|
||||
|
||||
const toolName = callerRow.payload.name;
|
||||
const serverName = getServerName(toolName);
|
||||
if (!serverName) {
|
||||
reply.code(400);
|
||||
return { error: 'not_an_mcp_tool', detail: `tool '${toolName}' is not from an MCP server` };
|
||||
}
|
||||
|
||||
if (permission === 'allow_always' || permission === 'allow_once') {
|
||||
setServerPermission(serverName, 'allow');
|
||||
} else if (permission === 'deny') {
|
||||
setServerPermission(serverName, 'deny');
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
},
|
||||
);
|
||||
|
||||
const FeedbackBody = z.object({
|
||||
value: z.enum(['up', 'down']),
|
||||
});
|
||||
|
||||
app.post<{ Params: { id: string; message_id: string } }>(
|
||||
'/api/chats/:id/messages/:message_id/feedback',
|
||||
async (req, reply) => {
|
||||
const parsed = FeedbackBody.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
reply.code(400);
|
||||
return { error: 'invalid body', details: parsed.error.flatten() };
|
||||
}
|
||||
const { id: chatId, message_id: messageId } = req.params;
|
||||
const { value } = parsed.data;
|
||||
|
||||
const msg = await sql<{ id: string; role: string; metadata: MessageMetadata | null }[]>`
|
||||
SELECT id, role, metadata FROM messages WHERE id = ${messageId} AND chat_id = ${chatId}
|
||||
`;
|
||||
if (msg.length === 0) {
|
||||
reply.code(404);
|
||||
return { error: 'message not found' };
|
||||
}
|
||||
|
||||
// Only allow feedback on assistant messages.
|
||||
if (msg[0]!.role !== 'assistant') {
|
||||
reply.code(400);
|
||||
return { error: 'only assistant messages can receive feedback' };
|
||||
}
|
||||
|
||||
// Check if feedback already exists
|
||||
const existingMeta = msg[0]!.metadata;
|
||||
if (existingMeta && existingMeta.kind === 'feedback') {
|
||||
reply.code(409);
|
||||
return { error: 'feedback already recorded' };
|
||||
}
|
||||
|
||||
const feedbackMeta: MessageMetadata = {
|
||||
kind: 'feedback',
|
||||
value,
|
||||
chat_id: chatId,
|
||||
};
|
||||
|
||||
await sql`
|
||||
UPDATE messages
|
||||
SET metadata = ${sql.json(feedbackMeta as never)}, updated_at = clock_timestamp()
|
||||
WHERE id = ${messageId}
|
||||
`;
|
||||
|
||||
return { ok: true };
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user