Unify dispatch and add supervisor routing signals

This commit is contained in:
dahyeon committed 2026-05-18 18:42:35 -07:00
1 parent f0b00e5fd2
commit b63985694a
19 files changed
+1345 -812

No files matched your search

+75 -3
View File
@@ -4,14 +4,29 @@ import type {
ConversationMessage,
} from "@agent/types";
import { readHandoffPacket } from "@agent/handoff-packet";
import type { HandoffPacket } from "@agent/handoff-packet";
import type { GarnetClient } from "@common/garnet";
import type { NatsClient } from "@common/nats";
import type { LlmClient } from "@llm/llm-client";
import { AlertService } from "@obs/alert";
import { metrics } from "@obs/metrics";
import type { Skill } from "@skills/skill";
import type { SkillCapabilityCard } from "@skills/skill-loader";
import type { SkillCapabilityCard, SkillRegistry } from "@skills/skill-loader";
import { getLogger } from "@obs/logger";
import {
getSupervisorLlmConfidenceThreshold,
getSupervisorLlmEnabled,
} from "../../config.js";
import {
buildSupervisorSystemPrompt,
buildSupervisorUserPrompt,
} from "./capability-prompt.js";
import { appendSupervisorReview } from "./review-queue.js";
import { routeSupervisorTarget } from "./routing-rules.js";
import {
supervisorRoutingSchema,
type SupervisorRoutingDecision,
} from "./schema.js";
const CAPABILITY_CARD: SkillCapabilityCard = {
canHandle: ["*"],
@@ -34,6 +49,8 @@ export class SupervisorSkill implements Skill {
constructor(
private readonly garnetClient: Pick<GarnetClient, "appendStream" | "setStringIfAbsent">,
natsClient: Pick<NatsClient, "publish">,
private readonly llmClient?: Pick<LlmClient, "chatCompletion"> | undefined,
private readonly skillRegistry?: SkillRegistry | undefined,
) {
this.alertService = new AlertService(garnetClient, natsClient);
}
@@ -51,7 +68,11 @@ export class SupervisorSkill implements Skill {
_conversationHistory: ConversationMessage[],
): Promise<AgentResponse> {
const packet = readHandoffPacket(request);
const target = routeSupervisorTarget(packet);
const llmDecision = await this.routeByLlm(packet);
const useLlmRoute =
llmDecision !== null &&
llmDecision.confidence >= getSupervisorLlmConfidenceThreshold();
const target = useLlmRoute ? llmDecision.target : routeSupervisorTarget(packet);
const reason = packet?.handoffReason ?? "out-of-scope";
await appendSupervisorReview(this.garnetClient, {
@@ -83,7 +104,8 @@ export class SupervisorSkill implements Skill {
return {
confidence: 0.6,
metadata: {
supervisorReason: reason,
routedBy: useLlmRoute ? "llm" : "rules",
supervisorReason: useLlmRoute ? llmDecision.reason : reason,
suggestedTarget: target,
},
requestId: request.requestId,
@@ -91,4 +113,54 @@ export class SupervisorSkill implements Skill {
text: "확인이 필요한 내용이라 운영자에게 전달했어요.",
};
}
private async routeByLlm(
packet?: HandoffPacket,
): Promise<SupervisorRoutingDecision | null> {
if (!getSupervisorLlmEnabled() || !this.llmClient || !this.skillRegistry) {
return null;
}
try {
const result = await this.llmClient.chatCompletion(
[
{
content: buildSupervisorSystemPrompt(this.skillRegistry, packet),
role: "system",
},
{
content: buildSupervisorUserPrompt(packet),
role: "user",
},
],
{
jsonMode: true,
maxTokens: 150,
temperature: 0,
tier: 0,
},
);
if (!result?.content.trim()) {
return null;
}
const parsed = supervisorRoutingSchema.safeParse(
JSON.parse(result.content) as unknown,
);
if (!parsed.success || !this.isKnownTarget(parsed.data.target)) {
return null;
}
return parsed.data;
} catch (error) {
getLogger().warn(
{ err: error },
"[Supervisor] LLM routing failed, falling back to rules",
);
return null;
}
}
private isKnownTarget(target: string): boolean {
return target === "human-review" || Boolean(this.skillRegistry?.get(target));
}
}