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

169 lines
4.4 KiB
TypeScript

import { eq, and, or, ilike, inArray } from 'drizzle-orm';
import { teamEntities, teamRelations } from '../db/schema.js';
import type { Db } from '../db/connection.js';
export interface EntityFilters {
entityType?: string;
search?: string;
}
export class KnowledgeService {
constructor(private db: Db) {}
async createEntity(
teamId: string,
userId: string,
data: {
entityType: string;
name: string;
properties?: Record<string, unknown>;
validFrom?: string;
validTo?: string;
},
) {
const values: typeof teamEntities.$inferInsert = {
teamId,
entityType: data.entityType,
name: data.name,
properties: data.properties ?? {},
sharedBy: userId,
};
if (data.validFrom) values.validFrom = new Date(data.validFrom);
if (data.validTo) values.validTo = new Date(data.validTo);
const [entity] = await this.db.insert(teamEntities).values(values).returning();
return entity;
}
async listEntities(teamId: string, filters?: EntityFilters) {
const conditions = [eq(teamEntities.teamId, teamId)];
if (filters?.entityType) {
conditions.push(eq(teamEntities.entityType, filters.entityType));
}
if (filters?.search) {
conditions.push(ilike(teamEntities.name, `%${filters.search}%`));
}
return this.db
.select()
.from(teamEntities)
.where(and(...conditions));
}
async getEntity(entityId: string) {
const [entity] = await this.db
.select()
.from(teamEntities)
.where(eq(teamEntities.id, entityId))
.limit(1);
return entity ?? null;
}
async createRelation(
teamId: string,
data: {
sourceId: string;
targetId: string;
relationType: string;
confidence?: number;
properties?: Record<string, unknown>;
},
) {
const endpointIds = [...new Set([data.sourceId, data.targetId])];
const ownedEndpoints = await this.db
.select({ id: teamEntities.id })
.from(teamEntities)
.where(and(
eq(teamEntities.teamId, teamId),
inArray(teamEntities.id, endpointIds),
));
if (ownedEndpoints.length !== endpointIds.length) return null;
const [relation] = await this.db.insert(teamRelations).values({
teamId,
sourceId: data.sourceId,
targetId: data.targetId,
relationType: data.relationType,
confidence: data.confidence ?? 1.0,
properties: data.properties ?? {},
}).returning();
return relation;
}
async queryGraph(
teamId: string,
startEntityId: string,
depth: number = 2,
relationTypes?: string[],
) {
const [startEntity] = await this.db
.select()
.from(teamEntities)
.where(and(
eq(teamEntities.id, startEntityId),
eq(teamEntities.teamId, teamId),
))
.limit(1);
if (!startEntity) return null;
const visited = new Set<string>([startEntityId]);
const resultEntities: Array<typeof teamEntities.$inferSelect> = [startEntity];
const resultRelations: Array<typeof teamRelations.$inferSelect> = [];
let frontier = [startEntityId];
for (let d = 0; d < depth && frontier.length > 0; d++) {
// Find relations connected to frontier entities
const rels = await this.db
.select()
.from(teamRelations)
.where(
and(
eq(teamRelations.teamId, teamId),
or(
inArray(teamRelations.sourceId, frontier),
inArray(teamRelations.targetId, frontier),
),
),
);
// Apply relationTypes filter if provided
const filteredRels = relationTypes
? rels.filter((r) => relationTypes.includes(r.relationType))
: rels;
const nextFrontier: string[] = [];
for (const rel of filteredRels) {
resultRelations.push(rel);
for (const id of [rel.sourceId, rel.targetId]) {
if (!visited.has(id)) {
visited.add(id);
nextFrontier.push(id);
}
}
}
if (nextFrontier.length > 0) {
const entities = await this.db
.select()
.from(teamEntities)
.where(
and(
eq(teamEntities.teamId, teamId),
inArray(teamEntities.id, nextFrontier),
),
);
resultEntities.push(...entities);
}
frontier = nextFrontier;
}
return {
entities: resultEntities,
relations: resultRelations,
};
}
}