48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import type { SkillRegistry } from "./skill-loader.js";
|
|
|
|
export class TransitionMatrix {
|
|
private constructor(private readonly transitions: Map<string, Set<string>>) {}
|
|
|
|
static fromRegistry(registry: SkillRegistry): TransitionMatrix {
|
|
const transitions = new Map<string, Set<string>>();
|
|
for (const manifest of registry.list()) {
|
|
const targets =
|
|
manifest.name === "supervisor"
|
|
? manifest.handoffTargets
|
|
: [...manifest.handoffTargets, "supervisor"];
|
|
transitions.set(
|
|
manifest.name,
|
|
new Set(targets),
|
|
);
|
|
}
|
|
return new TransitionMatrix(transitions);
|
|
}
|
|
|
|
isAllowed(from: string, to: string): boolean {
|
|
if (from === to) {
|
|
return false;
|
|
}
|
|
return this.transitions.get(from)?.has(to) ?? true;
|
|
}
|
|
|
|
targetsOf(from: string): string[] {
|
|
return [...(this.transitions.get(from) ?? new Set<string>())];
|
|
}
|
|
|
|
validateGraph(knownSkills?: readonly string[]): string[] {
|
|
const known = new Set([...(knownSkills ?? this.transitions.keys()), "supervisor"]);
|
|
const errors: string[] = [];
|
|
for (const [from, targets] of this.transitions.entries()) {
|
|
if (targets.has(from)) {
|
|
errors.push(`${from}: handoffTargets must not include itself`);
|
|
}
|
|
for (const target of targets) {
|
|
if (!known.has(target)) {
|
|
errors.push(`${from}: unknown handoff target ${target}`);
|
|
}
|
|
}
|
|
}
|
|
return errors;
|
|
}
|
|
}
|