From 5adaaad216599c5b9b57903406f9139bd3558e13 Mon Sep 17 00:00:00 2001 From: chahinebrini Date: Wed, 24 Jun 2026 23:45:20 +0200 Subject: [PATCH] feat(server): add optional fastify api --- src/cli/commands/server.ts | 5 +++++ src/server/index.ts | 15 +++++++++++++++ src/server/routes.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/cli/commands/server.ts create mode 100644 src/server/index.ts create mode 100644 src/server/routes.ts diff --git a/src/cli/commands/server.ts b/src/cli/commands/server.ts new file mode 100644 index 0000000..0166940 --- /dev/null +++ b/src/cli/commands/server.ts @@ -0,0 +1,5 @@ +import { startServer } from '../../server/index.js'; + +export async function serverStart(cwd: string, options: { port?: number } = {}): Promise { + await startServer(cwd, options.port ?? 3377); +} diff --git a/src/server/index.ts b/src/server/index.ts new file mode 100644 index 0000000..f4a80d5 --- /dev/null +++ b/src/server/index.ts @@ -0,0 +1,15 @@ +import Fastify from 'fastify'; +import { registerRoutes } from './routes.js'; + +export async function startServer(cwd: string, port = 3377): Promise { + 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); + } +} diff --git a/src/server/routes.ts b/src/server/routes.ts new file mode 100644 index 0000000..a633888 --- /dev/null +++ b/src/server/routes.ts @@ -0,0 +1,27 @@ +import { FastifyInstance } from 'fastify'; +import { Index } from '../core/index.js'; + +export async function registerRoutes(app: FastifyInstance, cwd: string): Promise { + 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; + }); +}