feat(server): add optional fastify api

This commit is contained in:
chahinebrini 2026-06-24 23:45:20 +02:00
parent 7c20dd91d9
commit 5adaaad216
3 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,5 @@
import { startServer } from '../../server/index.js';
export async function serverStart(cwd: string, options: { port?: number } = {}): Promise<void> {
await startServer(cwd, options.port ?? 3377);
}

15
src/server/index.ts Normal file
View File

@ -0,0 +1,15 @@
import Fastify from 'fastify';
import { registerRoutes } from './routes.js';
export async function startServer(cwd: string, port = 3377): Promise<void> {
const app = Fastify({ logger: false });
await registerRoutes(app, cwd);
try {
await app.listen({ port, host: '127.0.0.1' });
console.log(`AgentHub server listening on http://127.0.0.1:${port}`);
} catch (err) {
app.log.error(err);
process.exit(1);
}
}

27
src/server/routes.ts Normal file
View File

@ -0,0 +1,27 @@
import { FastifyInstance } from 'fastify';
import { Index } from '../core/index.js';
export async function registerRoutes(app: FastifyInstance, cwd: string): Promise<void> {
app.get('/status', async () => {
const index = new Index(cwd);
const open = index.list('task', { status: 'open' });
const inProgress = index.list('task', { status: 'in_progress' });
index.close();
return { open: open.length, inProgress: inProgress.length };
});
app.get('/tasks', async () => {
const index = new Index(cwd);
const tasks = index.list('task');
index.close();
return tasks;
});
app.get('/search', async (request) => {
const { q } = request.query as { q: string };
const index = new Index(cwd);
const results = index.search(q);
index.close();
return results;
});
}