Six builtin defaults (Code Reviewer, Debugger, Refactorer, Architect, Security Auditor, Prompt Builder) with no model field so session.model wins. Project root AGENTS.md parsed on demand with mtime cache; when present, only its agents are shown. sessions.agent_id resolves per turn into effective system prompt, temperature, and a tool whitelist applied in inference. AgentPicker mounts in the ChatInput toolbar; SettingsDrawer agent surface deferred to Batch 7. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
161 lines
5.1 KiB
TypeScript
161 lines
5.1 KiB
TypeScript
import Fastify from 'fastify';
|
|
import fastifyStatic from '@fastify/static';
|
|
import fastifyWebsocket from '@fastify/websocket';
|
|
import { existsSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
import { loadConfig } from './config.js';
|
|
import { getSql, applySchema, pingDb, closeDb } from './db.js';
|
|
|
|
import { registerProjectRoutes } from './routes/projects.js';
|
|
import { registerSessionRoutes } from './routes/sessions.js';
|
|
import { registerSettingsRoutes } from './routes/settings.js';
|
|
import { registerMessageRoutes } from './routes/messages.js';
|
|
import { registerChatRoutes } from './routes/chats.js';
|
|
import { registerSidebarRoutes } from './routes/sidebar.js';
|
|
import { registerWebSocket } from './routes/ws.js';
|
|
import { registerModelRoutes } from './routes/models.js';
|
|
import { registerAgentRoutes } from './routes/agents.js';
|
|
import { createInferenceRunner } from './services/inference.js';
|
|
import { createBroker } from './services/broker.js';
|
|
|
|
async function main() {
|
|
const config = loadConfig();
|
|
|
|
const app = Fastify({
|
|
logger: { level: config.LOG_LEVEL },
|
|
});
|
|
|
|
// Allow empty JSON bodies on POSTs that don't take a body (archive, unarchive, stop, etc.).
|
|
// Default Fastify parser throws FST_ERR_CTP_EMPTY_JSON_BODY on empty string.
|
|
app.removeContentTypeParser(['application/json']);
|
|
app.addContentTypeParser('application/json', { parseAs: 'string' }, (_req, body, done) => {
|
|
const str = (body as string) ?? '';
|
|
if (str.trim().length === 0) {
|
|
done(null, {});
|
|
return;
|
|
}
|
|
try {
|
|
done(null, JSON.parse(str));
|
|
} catch (err) {
|
|
done(err as Error, undefined);
|
|
}
|
|
});
|
|
|
|
const sql = getSql(config);
|
|
await applySchema(sql);
|
|
app.log.info('database schema applied');
|
|
|
|
await app.register(fastifyWebsocket);
|
|
|
|
app.get('/api/health', async () => {
|
|
const dbOk = await pingDb(sql);
|
|
return { status: dbOk ? 'ok' : 'degraded', db: dbOk };
|
|
});
|
|
|
|
const broker = createBroker();
|
|
|
|
registerProjectRoutes(app, sql, config, broker);
|
|
registerSessionRoutes(app, sql, config, broker);
|
|
registerSettingsRoutes(app, sql);
|
|
registerModelRoutes(app, config);
|
|
registerAgentRoutes(app, sql);
|
|
registerSidebarRoutes(app, sql);
|
|
registerChatRoutes(app, sql, broker);
|
|
|
|
const inference = createInferenceRunner(
|
|
{
|
|
sql,
|
|
config,
|
|
log: app.log,
|
|
publish: (sessionId, frame) => {
|
|
broker.publish(sessionId, frame as unknown as Record<string, unknown> & { type: string });
|
|
},
|
|
},
|
|
(user, frame) => {
|
|
broker.publishUser(user, frame as unknown as Record<string, unknown> & { type: string });
|
|
}
|
|
);
|
|
registerMessageRoutes(app, sql, {
|
|
enqueueInference: (sessionId, chatId, assistantId, user) => {
|
|
inference.enqueue(sessionId, chatId, assistantId, user);
|
|
},
|
|
enqueueCompact: (sessionId, chatId, compactId, user) => {
|
|
inference.enqueueCompact(sessionId, chatId, compactId, user);
|
|
},
|
|
cancelInference: async (sessionId, chatId) => {
|
|
return inference.cancel(sessionId, chatId);
|
|
},
|
|
hasActiveInference: (chatId) => inference.hasActive(chatId),
|
|
publishUserMessage: (sessionId, chatId, userMessageId, content) => {
|
|
broker.publish(sessionId, {
|
|
type: 'message_started',
|
|
message_id: userMessageId,
|
|
chat_id: chatId,
|
|
role: 'user',
|
|
});
|
|
broker.publish(sessionId, {
|
|
type: 'delta',
|
|
message_id: userMessageId,
|
|
chat_id: chatId,
|
|
content,
|
|
});
|
|
broker.publish(sessionId, {
|
|
type: 'message_complete',
|
|
message_id: userMessageId,
|
|
chat_id: chatId,
|
|
});
|
|
},
|
|
publishMessagesDeleted: (sessionId, chatId, messageIds) => {
|
|
broker.publish(sessionId, {
|
|
type: 'messages_deleted',
|
|
message_ids: messageIds,
|
|
chat_id: chatId,
|
|
});
|
|
},
|
|
});
|
|
registerWebSocket(app, sql, broker);
|
|
|
|
const webDist = process.env.WEB_DIST_PATH ?? resolve(process.cwd(), '../web/dist');
|
|
if (existsSync(webDist)) {
|
|
await app.register(fastifyStatic, {
|
|
root: webDist,
|
|
prefix: '/',
|
|
wildcard: false,
|
|
});
|
|
app.setNotFoundHandler((req, reply) => {
|
|
if (req.url.startsWith('/api')) {
|
|
reply.code(404).send({ error: 'not found' });
|
|
return;
|
|
}
|
|
reply.sendFile('index.html');
|
|
});
|
|
app.log.info(`serving static frontend from ${webDist}`);
|
|
}
|
|
|
|
const shutdown = async (signal: string) => {
|
|
app.log.info(`received ${signal}, shutting down`);
|
|
try {
|
|
await app.close();
|
|
await closeDb();
|
|
process.exit(0);
|
|
} catch (err) {
|
|
app.log.error(err);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
process.on('SIGINT', () => void shutdown('SIGINT'));
|
|
process.on('SIGTERM', () => void shutdown('SIGTERM'));
|
|
|
|
// Bound to 0.0.0.0 intentionally. Public access goes through Caddy → Authelia.
|
|
// Direct Tailscale access (100.114.205.53:9500) is unauthenticated by design;
|
|
// the threat model treats Tailnet membership as the trust boundary.
|
|
await app.listen({ port: config.PORT, host: config.HOST });
|
|
app.log.info(`boocode server listening on http://${config.HOST}:${config.PORT}`);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error('Fatal startup error:', err);
|
|
process.exit(1);
|
|
});
|