feat: 完成 F 群組 — 對話生成管線與 LLM Provider 抽象
- LLMProvider 介面(generate/stream)+ LLM_PROVIDER 切換(llm.module.ts useFactory, LLM_PROVIDER=mock|claude 環境變數決定注入哪個實作) - MockProvider:依性格原型 × 情緒狀態 × 親密度從模板庫選填回應,seeded PRNG 確保同 seed 可重現, 支援 *動作描寫* 標記抽取(目前只有元氣原型模板,G 群組需補齊其餘五種原型) - ClaudeProvider 空殼:LLM_PROVIDER=claude 時啟動即輸出 ERR log,呼叫 generate/stream 時拋出例外 - OutputFilterService:全域+角色禁則詞彙過濾(攔截並改寫) - FastChannelDetector:固定問候與危險輸入(含求助資源)走快速通道,跳過記憶檢索 - BehaviorReinforcementService:稱讚加權、糾正後修正版繼承舊權重以確保排序領先(實作時抓到並修正一個 「降權後打平」的真實 bug) - ContextAssemblerService:組裝人設/情緒/關係/檢索記憶/對話歷史為統一上下文快照 - DialogueService/Controller:串起以上所有元件,作為 H-1 正式對話端點的基礎 - scripts/smoke/F.mjs:涵蓋可重現性、上下文快照完整性、雙速通道、行為強化、輸出過濾、 以 child process 驗證 Provider 切換 npm run restart && npm run smoke -- F 皆通過(F-V),A/B/C/D/E 群組冒煙測試無回歸。
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { prisma } from "@kokorone/db";
|
||||
|
||||
const API_PORT = process.env.PORT_API ?? "3001";
|
||||
const CHARACTER_ID = "seed-character-genki";
|
||||
const USER_ID = "seed-user-primary";
|
||||
|
||||
async function post(path, body) {
|
||||
const res = await fetch(`http://localhost:${API_PORT}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body ?? {}),
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
async function postJson(path, body) {
|
||||
const res = await post(path, body);
|
||||
if (!res.ok) {
|
||||
throw new Error(`POST ${path} 回傳 ${res.status}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function get(path) {
|
||||
const res = await fetch(`http://localhost:${API_PORT}${path}`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`GET ${path} 回傳 ${res.status}`);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
function sendMessage(sessionId, text, extra = {}) {
|
||||
return postJson(`/dialogue/${CHARACTER_ID}/sessions/${sessionId}/messages`, {
|
||||
userId: USER_ID,
|
||||
text,
|
||||
...extra,
|
||||
});
|
||||
}
|
||||
|
||||
export default async function smokeF() {
|
||||
// F-7 + F-1:同 seed 兩次獨立 session 應得到完全相同的輸出(可重現)
|
||||
const seedA = "smoke-f-seed-a-" + Date.now();
|
||||
const seedB = "smoke-f-seed-b-" + Date.now();
|
||||
const r1 = await sendMessage(seedA, "今天過得如何?", { seed: 12345 });
|
||||
const r2 = await sendMessage(seedB, "今天過得如何?", { seed: 12345 });
|
||||
if (r1.text !== r2.text) {
|
||||
throw new Error(`同 seed 應輸出相同文字,實際:「${r1.text}」vs「${r2.text}」`);
|
||||
}
|
||||
if (r1.isFastChannel) {
|
||||
throw new Error("一般輸入不應被判定為快速通道");
|
||||
}
|
||||
|
||||
// F-6 上下文組裝器:一次對話應可輸出完整組裝內容快照
|
||||
const requiredKeys = [
|
||||
"character",
|
||||
"emotion",
|
||||
"relationship",
|
||||
"retrievedMemories",
|
||||
"history",
|
||||
"userInput",
|
||||
"injectedMemoryIds",
|
||||
"seed",
|
||||
];
|
||||
for (const key of requiredKeys) {
|
||||
if (!(key in r1.context)) {
|
||||
throw new Error(`上下文快照缺少欄位:${key}`);
|
||||
}
|
||||
}
|
||||
if (r1.context.injectedMemoryIds.length === 0) {
|
||||
throw new Error("一般輸入的上下文組裝應能檢索到至少一筆既有記憶(種子記憶)");
|
||||
}
|
||||
|
||||
// F-4 雙速通道:問候與危險輸入不應觸發記憶檢索(injectedMemoryIds 應為空)
|
||||
const greetingSession = "smoke-f-greeting-" + Date.now();
|
||||
const greeting = await sendMessage(greetingSession, "早安!");
|
||||
if (!greeting.isFastChannel) {
|
||||
throw new Error("固定問候應被判定為快速通道");
|
||||
}
|
||||
if (greeting.context.injectedMemoryIds.length !== 0) {
|
||||
throw new Error("快速通道(問候)不應觸發記憶檢索");
|
||||
}
|
||||
|
||||
const dangerSession = "smoke-f-danger-" + Date.now();
|
||||
const danger = await sendMessage(dangerSession, "我覺得好痛苦,好想死");
|
||||
if (!danger.isFastChannel) {
|
||||
throw new Error("明確危險輸入應被判定為快速通道");
|
||||
}
|
||||
if (danger.context.injectedMemoryIds.length !== 0) {
|
||||
throw new Error("快速通道(危險輸入)不應觸發記憶檢索");
|
||||
}
|
||||
if (!danger.text.includes("1995")) {
|
||||
throw new Error("危險輸入的快速通道回應應包含求助資源");
|
||||
}
|
||||
|
||||
// F-5 行為強化迴路:稱讚加權、糾正後修正版取代舊模式
|
||||
const situation = "fast-channel:greeting";
|
||||
const oldPattern = "smoke-f-old-pattern-" + Date.now();
|
||||
const newPattern = "smoke-f-new-pattern-" + Date.now();
|
||||
await postJson(`/dialogue/${CHARACTER_ID}/reinforce`, { situation, responsePattern: oldPattern });
|
||||
await postJson(`/dialogue/${CHARACTER_ID}/reinforce`, { situation, responsePattern: oldPattern });
|
||||
const beforeCorrection = await get(
|
||||
`/dialogue/${CHARACTER_ID}/top-rule?situation=${encodeURIComponent(situation)}`,
|
||||
);
|
||||
if (beforeCorrection.responsePattern !== oldPattern || beforeCorrection.weight !== 2) {
|
||||
throw new Error("稱讚加權後應為權重最高的規則(weight=2)");
|
||||
}
|
||||
|
||||
await postJson(`/dialogue/${CHARACTER_ID}/correct`, {
|
||||
situation,
|
||||
oldResponsePattern: oldPattern,
|
||||
newResponsePattern: newPattern,
|
||||
});
|
||||
const afterCorrection = await get(
|
||||
`/dialogue/${CHARACTER_ID}/top-rule?situation=${encodeURIComponent(situation)}`,
|
||||
);
|
||||
if (afterCorrection.responsePattern !== newPattern) {
|
||||
throw new Error("糾正後修正版應取代舊模式成為權重最高者");
|
||||
}
|
||||
|
||||
const greetingAfterCorrection = await sendMessage("smoke-f-greeting2-" + Date.now(), "早安!");
|
||||
if (greetingAfterCorrection.text !== newPattern) {
|
||||
throw new Error("糾正後的快速通道回應應套用修正版模式");
|
||||
}
|
||||
|
||||
// F-3 輸出過濾層:含禁則詞的模板輸出應被攔截或改寫
|
||||
const forbiddenPattern = "早安,你這個死八嘎";
|
||||
await postJson(`/dialogue/${CHARACTER_ID}/reinforce`, { situation, responsePattern: forbiddenPattern });
|
||||
await postJson(`/dialogue/${CHARACTER_ID}/reinforce`, { situation, responsePattern: forbiddenPattern });
|
||||
await postJson(`/dialogue/${CHARACTER_ID}/reinforce`, { situation, responsePattern: forbiddenPattern });
|
||||
const filteredGreeting = await sendMessage("smoke-f-filter-" + Date.now(), "早安!");
|
||||
if (filteredGreeting.text.includes("死八嘎")) {
|
||||
throw new Error("含禁則詞的回應未被輸出過濾層攔截或改寫");
|
||||
}
|
||||
|
||||
// 清理本次測試建立的 ProceduralRule,避免重複執行時累積髒資料
|
||||
await prisma.proceduralRule.deleteMany({ where: { characterId: CHARACTER_ID, situation } });
|
||||
|
||||
// F-2 Provider 切換:LLM_PROVIDER=claude 時啟動即以 ERR log 明確告知未實作,呼叫時拋出例外
|
||||
await verifyClaudeProviderStub();
|
||||
}
|
||||
|
||||
async function verifyClaudeProviderStub() {
|
||||
const port = "3091";
|
||||
const child = spawn("node", ["apps/api/dist/main.js"], {
|
||||
env: { ...process.env, PORT: port, LLM_PROVIDER: "claude" },
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
|
||||
let output = "";
|
||||
child.stdout.on("data", (chunk) => (output += chunk.toString()));
|
||||
child.stderr.on("data", (chunk) => (output += chunk.toString()));
|
||||
|
||||
try {
|
||||
const deadline = Date.now() + 15_000;
|
||||
while (!output.includes("啟動") && Date.now() < deadline) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
}
|
||||
if (!output.includes("ClaudeProvider 尚未實作")) {
|
||||
throw new Error("LLM_PROVIDER=claude 啟動時未輸出「尚未實作」的 ERR log");
|
||||
}
|
||||
|
||||
let healthOk = false;
|
||||
const healthDeadline = Date.now() + 15_000;
|
||||
while (!healthOk && Date.now() < healthDeadline) {
|
||||
try {
|
||||
const res = await fetch(`http://localhost:${port}/health`);
|
||||
healthOk = res.ok;
|
||||
} catch {
|
||||
// 尚未就緒,繼續等待
|
||||
}
|
||||
if (!healthOk) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
}
|
||||
}
|
||||
if (!healthOk) {
|
||||
throw new Error("LLM_PROVIDER=claude 的測試行程未能成功啟動");
|
||||
}
|
||||
|
||||
const res = await fetch(`http://localhost:${port}/dialogue/${CHARACTER_ID}/sessions/claude-stub-test/messages`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ userId: USER_ID, text: "今天發生了很多事情想跟你分享" }),
|
||||
});
|
||||
if (res.status < 500) {
|
||||
throw new Error(`ClaudeProvider 空殼應在呼叫 generate() 時失敗,實際回傳 ${res.status}`);
|
||||
}
|
||||
} finally {
|
||||
child.kill("SIGKILL");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user