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).
This commit is contained in:
61
packages/team-server/src/plugins/auth.ts
Normal file
61
packages/team-server/src/plugins/auth.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import fp from 'fastify-plugin';
|
||||
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { createClerkClient, verifyToken } from '@clerk/fastify';
|
||||
import { UserService } from '../services/user-service.js';
|
||||
|
||||
export type AuthenticateFn = (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
userId: string; // our internal user UUID
|
||||
clerkId: string;
|
||||
}
|
||||
interface FastifyInstance {
|
||||
authenticate: AuthenticateFn;
|
||||
_authHandler: { fn: AuthenticateFn };
|
||||
}
|
||||
}
|
||||
|
||||
export default fp(async function authPlugin(fastify: FastifyInstance) {
|
||||
const clerkSecretKey = fastify.config.clerkSecretKey;
|
||||
const clerk = createClerkClient({ secretKey: clerkSecretKey });
|
||||
const userService = new UserService(fastify.db);
|
||||
|
||||
const clerkAuth: AuthenticateFn = async function (request, reply) {
|
||||
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||
if (!token) {
|
||||
return reply.code(401).send({ error: 'Missing authorization token' });
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = await verifyToken(token, { secretKey: clerkSecretKey });
|
||||
request.clerkId = payload.sub;
|
||||
|
||||
// Look up internal user, auto-provision if not found
|
||||
let user = await userService.getByClerkId(payload.sub);
|
||||
|
||||
if (!user) {
|
||||
// Auto-create user from Clerk JWT claims (self-healing: works even if webhook was missed)
|
||||
const clerkUser = await clerk.users.getUser(payload.sub);
|
||||
user = await userService.upsertFromClerk({
|
||||
clerkId: payload.sub,
|
||||
displayName: `${clerkUser.firstName ?? ''} ${clerkUser.lastName ?? ''}`.trim() || payload.sub,
|
||||
email: clerkUser.emailAddresses?.[0]?.emailAddress ?? '',
|
||||
avatarUrl: clerkUser.imageUrl ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
request.userId = user.id;
|
||||
} catch {
|
||||
return reply.code(401).send({ error: 'Invalid token' });
|
||||
}
|
||||
};
|
||||
|
||||
// Use an indirection object so tests can swap the auth handler after routes are registered
|
||||
const handler = { fn: clerkAuth };
|
||||
fastify.decorate('_authHandler', handler);
|
||||
|
||||
fastify.decorate('authenticate', async function (request: FastifyRequest, reply: FastifyReply) {
|
||||
return handler.fn(request, reply);
|
||||
});
|
||||
});
|
||||
24
packages/team-server/src/plugins/redis.ts
Normal file
24
packages/team-server/src/plugins/redis.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import fp from 'fastify-plugin';
|
||||
import Redis from 'ioredis';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
redis: Redis;
|
||||
redisSub: Redis;
|
||||
}
|
||||
}
|
||||
|
||||
export default fp(async function redisPlugin(fastify: FastifyInstance) {
|
||||
const redisUrl = fastify.config.redisUrl;
|
||||
const redis = new Redis(redisUrl);
|
||||
const redisSub = new Redis(redisUrl);
|
||||
|
||||
fastify.decorate('redis', redis);
|
||||
fastify.decorate('redisSub', redisSub);
|
||||
|
||||
fastify.addHook('onClose', async () => {
|
||||
await redis.quit();
|
||||
await redisSub.quit();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user