Files
waggle-os/packages/team-server/src/index.ts
shumeiko.it c25efe6c44 refactor(server): move the team runtime into a postponed package (P1.1)
Team mode stays in the repository but is not built or shipped (ADR-0003).

- The team server (index.ts, config, db, daemons, plugins, proactive,
  routes, scheduler, services, ws, drizzle migrations) and its tests move
  from @waggle/server to packages/team-server (@waggle/team-server,
  private). It keeps its own copies of the logger and the agent-group
  executor, so it does not import @waggle/server. @clerk/fastify, bullmq,
  ioredis, postgres, drizzle-orm, drizzle-kit, cron-parser and
  @waggle/waggle-dance leave @waggle/server; the launcher drops the same
  team-only dependencies.
- build:packages no longer builds waggle-dance or the worker: nothing in
  the community edition imports them, and the worker's project reference
  would otherwise build the team server. The worker now imports from
  team-server; the team Dockerfile builds both explicitly.
- The sidecar no longer starts the team server when DATABASE_URL is set,
  and loses the team routes (/api/team/*, /api/teams), the WaggleDance
  routes and bridge (/api/waggle-dance/*), the Team server binding and
  egress guard, the WebSocket team client, the TeamSync cache and pushes,
  team audit forwarding, team workspace registration and the team
  capability-policy lookup in chat. The in-process room signal bus stays:
  agent groups, fleet runs and external tool runs use it.
  middleware/audit.ts was dead and is deleted.
- @waggle/core loses TeamSync and the teamServer part of WaggleConfig. A
  teamServer key in config.json is ignored and kept on save, as P0.1 does
  for the tier keys. Workspaces lose teamServerUrl and teamUserId; teamId
  and teamRole stay because workspaces from a shared data directory still
  carry them and the viewer write guards read them. POST /api/workspaces
  ignores team fields.
- The sidecar bundle no longer marks the team packages as external.

The agent still references the TeamSync type in orchestrator.ts and
pattern-write-back.ts; those files are being rewritten in P1.5 and drop
it there.

Logs UF-030 and UF-031 (team sync and team policy defects found while
removing the code).
2026-10-24 12:17:41 +02:00

99 lines
3.6 KiB
TypeScript

import Fastify from 'fastify';
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import cors from '@fastify/cors';
import websocket from '@fastify/websocket';
import { loadConfig, type ServerConfig } from './config.js';
import { createDb, type Db } from './db/connection.js';
import redisPlugin from './plugins/redis.js';
import authPlugin from './plugins/auth.js';
import { webhookRoutes } from './routes/webhooks.js';
import { teamRoutes } from './routes/teams.js';
import { agentRoutes } from './routes/agents.js';
import { taskRoutes } from './routes/tasks.js';
import { messageRoutes } from './routes/messages.js';
import { knowledgeRoutes } from './routes/knowledge.js';
import { resourceRoutes } from './routes/resources.js';
import { jobRoutes } from './routes/jobs.js';
import { cronRoutes } from './routes/cron.js';
import { suggestionRoutes } from './routes/suggestions.js';
import { scoutRoutes } from './routes/scout.js';
import { auditRoutes } from './routes/audit.js';
import { capabilityGovernanceRoutes } from './routes/capability-governance.js';
import { analyticsRoutes } from './routes/analytics.js';
import { wsGateway } from './ws/gateway.js';
import { JobService } from './services/job-service.js';
import { CronRunner } from './scheduler/cron-runner.js';
import { createLogger } from './logger.js';
const log = createLogger('server');
declare module 'fastify' {
interface FastifyInstance {
config: ServerConfig;
db: Db;
jobService: JobService;
}
}
export async function buildServer(configOverrides?: Partial<ServerConfig>) {
const config = { ...loadConfig(), ...configOverrides };
const server = Fastify({ logger: true });
server.decorate('config', config);
const db = createDb(config.databaseUrl);
server.decorate('db', db);
await server.register(cors, { origin: config.corsOrigin });
await server.register(websocket);
await server.register(redisPlugin);
await server.register(authPlugin);
await server.register(webhookRoutes);
await server.register(teamRoutes);
await server.register(agentRoutes);
await server.register(taskRoutes);
await server.register(messageRoutes);
await server.register(knowledgeRoutes);
// Job service (must be decorated before job routes)
const jobService = new JobService(db, config.redisUrl);
server.decorate('jobService', jobService);
const cronRunner = new CronRunner(db, jobService, error => {
server.log.error({ err: error }, 'Cron scheduler tick failed');
});
server.addHook('onReady', async () => {
cronRunner.start();
});
server.addHook('onClose', async () => {
await cronRunner.stop();
await jobService.close();
});
await server.register(resourceRoutes);
await server.register(jobRoutes);
await server.register(cronRoutes);
await server.register(suggestionRoutes);
await server.register(scoutRoutes);
await server.register(auditRoutes);
await server.register(capabilityGovernanceRoutes);
await server.register(analyticsRoutes);
await server.register(wsGateway);
// Health check
server.get('/health', async () => ({ status: 'ok', timestamp: new Date().toISOString() }));
return server;
}
// Start server if run directly
export function isDirectModule(entryPath: string | undefined, moduleUrl: string): boolean {
return entryPath !== undefined && pathToFileURL(resolve(entryPath)).href === moduleUrl;
}
if (isDirectModule(process.argv[1], import.meta.url)) {
const server = await buildServer();
await server.listen({ port: server.config.port, host: server.config.host });
log.info(`Waggle server listening on ${server.config.host}:${server.config.port}`);
}