Files
waggle-os/packages/team-server/src/services/message-service.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

120 lines
3.1 KiB
TypeScript

import { eq, and, desc, sql } from 'drizzle-orm';
import { messages, teamEntities, tasks } from '../db/schema.js';
import type { Db } from '../db/connection.js';
import type { HiveQuery, HiveQueryResult } from '@waggle/waggle-dance';
import type Redis from 'ioredis';
export interface MessageFilters {
type?: string;
subtype?: string;
}
export class MessageService {
constructor(private db: Db) {}
async send(
teamId: string,
senderId: string,
data: {
type: string;
subtype: string;
content: Record<string, unknown>;
referenceId?: string;
routing?: Array<{ userId: string; reason: string }>;
},
redis?: Redis,
) {
const [message] = await this.db.insert(messages).values({
teamId,
senderId,
type: data.type,
subtype: data.subtype,
content: data.content,
referenceId: data.referenceId ?? null,
routing: data.routing ?? null,
}).returning();
// Publish to Redis for real-time delivery
if (redis) {
await redis.publish(`team:${teamId}:waggle`, JSON.stringify(message));
}
return message;
}
async list(teamId: string, filters?: MessageFilters) {
const conditions = [eq(messages.teamId, teamId)];
if (filters?.type) {
conditions.push(eq(messages.type, filters.type));
}
if (filters?.subtype) {
conditions.push(eq(messages.subtype, filters.subtype));
}
return this.db
.select()
.from(messages)
.where(and(...conditions))
.orderBy(desc(messages.createdAt));
}
async checkHive(teamId: string, query: HiveQuery): Promise<HiveQueryResult> {
const searchTerm = `%${query.topic}%`;
// Search team_entities by name or entityType matching the topic
const entities = await this.db
.select({
id: teamEntities.id,
name: teamEntities.name,
entityType: teamEntities.entityType,
properties: teamEntities.properties,
})
.from(teamEntities)
.where(
and(
eq(teamEntities.teamId, teamId),
sql`(${teamEntities.name} ILIKE ${searchTerm} OR ${teamEntities.entityType} ILIKE ${searchTerm})`,
),
)
.limit(10);
// Search tasks by title or description matching the topic
const relatedTasks = await this.db
.select({
id: tasks.id,
title: tasks.title,
status: tasks.status,
createdBy: tasks.createdBy,
})
.from(tasks)
.where(
and(
eq(tasks.teamId, teamId),
sql`(${tasks.title} ILIKE ${searchTerm} OR ${tasks.description} ILIKE ${searchTerm})`,
),
)
.limit(10);
// Search recent broadcast messages
const relatedMessages = await this.db
.select({
id: messages.id,
type: messages.type,
subtype: messages.subtype,
content: messages.content,
})
.from(messages)
.where(
and(
eq(messages.teamId, teamId),
eq(messages.type, 'broadcast'),
),
)
.orderBy(desc(messages.createdAt))
.limit(10);
return { entities, relatedTasks, relatedMessages };
}
}