349 lines
12 KiB
TypeScript
349 lines
12 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import { z } from "zod";
|
|
import type { ChatCompletionOptions, ChatMessage } from "@llm/types";
|
|
import { OnboardingOrchestrator } from "./orchestrator.js";
|
|
import type { DerivedState } from "../state/onboarding-state.js";
|
|
import type { Tool } from "@tools/tool";
|
|
|
|
function createDerivedState(overrides?: Partial<DerivedState>): DerivedState {
|
|
return {
|
|
phase: { kind: "photo-pending" },
|
|
intent: { kind: "help-how" },
|
|
shouldRespond: true,
|
|
responseMode: "llm",
|
|
suggestedMaxTokens: 256,
|
|
allowShortResponse: false,
|
|
tier: 0,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("OnboardingOrchestrator", () => {
|
|
afterEach(() => {
|
|
delete process.env["AGENT_PROGRAMMATIC_TOOL_CALLING"];
|
|
delete process.env["AGENT_SKILL_MD_MODE"];
|
|
});
|
|
|
|
it("uses direct response path for tier 0", async () => {
|
|
const calls: Array<{
|
|
messages: ChatMessage[];
|
|
options: ChatCompletionOptions | undefined;
|
|
}> = [];
|
|
const llmClientStub = {
|
|
chatCompletion: (
|
|
messages: ChatMessage[],
|
|
options?: ChatCompletionOptions,
|
|
) => {
|
|
calls.push({ messages, options });
|
|
return Promise.resolve({
|
|
content: "안내 도와드릴게요.",
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "안내 도와드릴게요." } }] },
|
|
message: { role: "assistant" as const, content: "안내 도와드릴게요." },
|
|
});
|
|
},
|
|
};
|
|
|
|
const orchestrator = new OnboardingOrchestrator(llmClientStub as never);
|
|
const result = await orchestrator.run(
|
|
createDerivedState({ tier: 0 }),
|
|
[{ role: "user", content: "도와줘" }],
|
|
{ toolContext: {} },
|
|
);
|
|
|
|
expect(result).toBe("안내 도와드릴게요.");
|
|
expect(calls).toHaveLength(1);
|
|
expect(calls[0]?.options?.role).toBe("responder");
|
|
});
|
|
|
|
it("runs planner then responder for tier 1", async () => {
|
|
const calls: Array<{
|
|
messages: ChatMessage[];
|
|
options: ChatCompletionOptions | undefined;
|
|
}> = [];
|
|
const llmClientStub = {
|
|
chatCompletion: (
|
|
messages: ChatMessage[],
|
|
options?: ChatCompletionOptions,
|
|
) => {
|
|
calls.push({ messages, options });
|
|
|
|
if (calls.length === 1) {
|
|
return Promise.resolve({
|
|
content: JSON.stringify({
|
|
approach: "redirect-to-step",
|
|
targetPhase: "photo-pending",
|
|
mustInclude: ["하트 인증만 안내"],
|
|
mustAvoid: ["디스코드 절차"],
|
|
suggestedTools: [],
|
|
tone: "friendly",
|
|
targetLengthChars: 120,
|
|
rationale: "질문 범위만 답하기",
|
|
}),
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "{}" } }] },
|
|
message: { role: "assistant" as const, content: "{}" },
|
|
});
|
|
}
|
|
|
|
return Promise.resolve({
|
|
content: "하트 인증만 남았어요.",
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "하트 인증만 남았어요." } }] },
|
|
message: { role: "assistant" as const, content: "하트 인증만 남았어요." },
|
|
});
|
|
},
|
|
};
|
|
|
|
const orchestrator = new OnboardingOrchestrator(llmClientStub as never);
|
|
const result = await orchestrator.run(
|
|
createDerivedState({ tier: 1 }),
|
|
[
|
|
{ role: "system", content: "기본 시스템 프롬프트" },
|
|
{ role: "user", content: "이제 뭐 남았지" },
|
|
],
|
|
{ toolContext: {} },
|
|
);
|
|
|
|
expect(result).toBe("하트 인증만 남았어요.");
|
|
expect(calls).toHaveLength(2);
|
|
expect(calls[0]?.options?.role).toBe("planner");
|
|
expect(calls[1]?.messages[0]?.content).toContain("[PLAN]");
|
|
});
|
|
|
|
it("runs a planner toolPlan before the responder when enabled", async () => {
|
|
process.env["AGENT_PROGRAMMATIC_TOOL_CALLING"] = "true";
|
|
const calls: Array<{
|
|
messages: ChatMessage[];
|
|
options: ChatCompletionOptions | undefined;
|
|
}> = [];
|
|
const llmClientStub = {
|
|
chatCompletion: (
|
|
messages: ChatMessage[],
|
|
options?: ChatCompletionOptions,
|
|
) => {
|
|
calls.push({ messages, options });
|
|
|
|
if (calls.length === 1) {
|
|
return Promise.resolve({
|
|
content: JSON.stringify({
|
|
approach: "validate-then-explain",
|
|
targetPhase: "nickname-rejected",
|
|
mustInclude: ["검사 결과"],
|
|
mustAvoid: [],
|
|
suggestedTools: ["validate-nickname"],
|
|
toolPlan: {
|
|
composeWith: "all-then-llm",
|
|
steps: [
|
|
{
|
|
id: "validate",
|
|
tool: "validate-nickname",
|
|
argsFromContext: {
|
|
nickname: "$request.nickname",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
tone: "friendly",
|
|
targetLengthChars: 120,
|
|
rationale: "도구 선실행",
|
|
}),
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "{}" } }] },
|
|
message: { role: "assistant" as const, content: "{}" },
|
|
});
|
|
}
|
|
|
|
return Promise.resolve({
|
|
content: "검사 결과 형식이 맞아요.",
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "검사 결과 형식이 맞아요." } }] },
|
|
message: { role: "assistant" as const, content: "검사 결과 형식이 맞아요." },
|
|
});
|
|
},
|
|
};
|
|
const tool: Tool<{ nickname: string }, { valid: boolean }> = {
|
|
name: "validate-nickname",
|
|
description: "닉네임 검사",
|
|
argsSchema: z.object({
|
|
nickname: z.string(),
|
|
}),
|
|
resultSchema: z.object({
|
|
valid: z.boolean(),
|
|
}),
|
|
execute: (args) => Promise.resolve({ valid: args.nickname.includes("/") }),
|
|
};
|
|
|
|
const orchestrator = new OnboardingOrchestrator(llmClientStub as never);
|
|
const result = await orchestrator.run(
|
|
createDerivedState({ responseMode: "llm-with-tools", tier: 1 }),
|
|
[
|
|
{ role: "system", content: "기본 시스템 프롬프트" },
|
|
{ role: "user", content: "닉네임 봐줘" },
|
|
],
|
|
{
|
|
request: {
|
|
requestId: "req-1",
|
|
skill: "onboarding",
|
|
chatroomId: "room-1",
|
|
userId: "user-1",
|
|
nickname: "프리리/95/여/D",
|
|
trigger: "user-message",
|
|
},
|
|
toolContext: {},
|
|
tools: [tool],
|
|
},
|
|
);
|
|
|
|
expect(result).toBe("검사 결과 형식이 맞아요.");
|
|
expect(calls).toHaveLength(2);
|
|
expect(calls[1]?.messages[0]?.content).toContain("[TOOL_PLAN_RESULTS]");
|
|
expect(calls[1]?.messages[0]?.content).toContain('"valid":true');
|
|
expect(calls[1]?.options?.tools).toBeUndefined();
|
|
});
|
|
|
|
it("injects requested SKILL.md resources before responder when enabled", async () => {
|
|
process.env["AGENT_SKILL_MD_MODE"] = "true";
|
|
const calls: Array<{
|
|
messages: ChatMessage[];
|
|
options: ChatCompletionOptions | undefined;
|
|
}> = [];
|
|
const llmClientStub = {
|
|
chatCompletion: (
|
|
messages: ChatMessage[],
|
|
options?: ChatCompletionOptions,
|
|
) => {
|
|
calls.push({ messages, options });
|
|
|
|
if (calls.length === 1) {
|
|
return Promise.resolve({
|
|
content: JSON.stringify({
|
|
approach: "answer-direct",
|
|
targetPhase: "photo-pending",
|
|
mustInclude: ["하트"],
|
|
mustAvoid: [],
|
|
suggestedTools: [],
|
|
requestResources: ["heart-rule"],
|
|
tone: "friendly",
|
|
targetLengthChars: 120,
|
|
rationale: "리소스 참고",
|
|
}),
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "{}" } }] },
|
|
message: { role: "assistant" as const, content: "{}" },
|
|
});
|
|
}
|
|
|
|
return Promise.resolve({
|
|
content: "커버 보기에서 하트를 빨간색으로 바꿔주세요.",
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: {
|
|
choices: [
|
|
{ message: { content: "커버 보기에서 하트를 빨간색으로 바꿔주세요." } },
|
|
],
|
|
},
|
|
message: {
|
|
role: "assistant" as const,
|
|
content: "커버 보기에서 하트를 빨간색으로 바꿔주세요.",
|
|
},
|
|
});
|
|
},
|
|
};
|
|
|
|
const orchestrator = new OnboardingOrchestrator(llmClientStub as never);
|
|
const result = await orchestrator.run(
|
|
createDerivedState({ tier: 1 }),
|
|
[
|
|
{ role: "system", content: "기본 시스템 프롬프트" },
|
|
{ role: "user", content: "하트 어디서 해요?" },
|
|
],
|
|
{ toolContext: {} },
|
|
);
|
|
|
|
expect(result).toContain("커버 보기");
|
|
expect(calls[1]?.messages[0]?.content).toContain("[RESOURCE:heart-rule]");
|
|
});
|
|
|
|
|
|
|
|
it("runs critic after responder for tier 2 and applies rewrite", async () => {
|
|
const calls: Array<{
|
|
messages: ChatMessage[];
|
|
options: ChatCompletionOptions | undefined;
|
|
}> = [];
|
|
const llmClientStub = {
|
|
chatCompletion: (
|
|
messages: ChatMessage[],
|
|
options?: ChatCompletionOptions,
|
|
) => {
|
|
calls.push({ messages, options });
|
|
|
|
if (calls.length === 1) {
|
|
return Promise.resolve({
|
|
content: JSON.stringify({
|
|
approach: "answer-direct",
|
|
targetPhase: "photo-pending",
|
|
mustInclude: [],
|
|
mustAvoid: ["추가 절차 나열"],
|
|
suggestedTools: [],
|
|
tone: "concise",
|
|
targetLengthChars: 60,
|
|
rationale: "짧게 답하기",
|
|
}),
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "{}" } }] },
|
|
message: { role: "assistant" as const, content: "{}" },
|
|
});
|
|
}
|
|
|
|
if (calls.length === 2) {
|
|
return Promise.resolve({
|
|
content: "하트 인증만 남았어요. 커버 보기로 들어가서...",
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: {
|
|
choices: [
|
|
{ message: { content: "하트 인증만 남았어요. 커버 보기로 들어가서..." } },
|
|
],
|
|
},
|
|
message: {
|
|
role: "assistant" as const,
|
|
content: "하트 인증만 남았어요. 커버 보기로 들어가서...",
|
|
},
|
|
});
|
|
}
|
|
|
|
return Promise.resolve({
|
|
content: "하트 인증만 남았어요.",
|
|
endpoint: "mock://llm",
|
|
provider: "cliproxy" as const,
|
|
raw: { choices: [{ message: { content: "하트 인증만 남았어요." } }] },
|
|
message: { role: "assistant" as const, content: "하트 인증만 남았어요." },
|
|
});
|
|
},
|
|
};
|
|
|
|
const orchestrator = new OnboardingOrchestrator(llmClientStub as never);
|
|
const result = await orchestrator.run(
|
|
createDerivedState({ tier: 2 }),
|
|
[
|
|
{ role: "system", content: "기본 시스템 프롬프트" },
|
|
{ role: "user", content: "이제 뭐 남았지" },
|
|
],
|
|
{ toolContext: {} },
|
|
);
|
|
|
|
expect(result).toBe("하트 인증만 남았어요.");
|
|
expect(calls).toHaveLength(3);
|
|
expect(calls[2]?.options?.role).toBe("critic");
|
|
});
|
|
});
|