init
This commit is contained in:
commit
7fa293c9e8
59 files changed
+15526
No files matched your search
@@ -0,0 +1,5 @@
|
||||
export { OnboardingSkill } from "./skill.js";
|
||||
export type {
|
||||
OnboardingSession,
|
||||
OnboardingStatus,
|
||||
} from "./types.js";
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* @deprecated Phase 2에서 classifier 내부 호환용으로만 유지한다.
|
||||
* Phase 2 PR 2b에서 golden set 확보 후 제거 예정.
|
||||
*/
|
||||
/**
|
||||
* 명확하게 봇에게 향하지 않은 메시지를 사전 필터링한다.
|
||||
* 보수적으로 동작 — 확실한 경우만 true를 반환한다.
|
||||
*/
|
||||
export function shouldSkipMessage(userMessage: string): boolean {
|
||||
const trimmed = userMessage.trim();
|
||||
if (!trimmed) return true;
|
||||
|
||||
// @멘션으로 시작하는 메시지 (다른 유저에게 하는 말)
|
||||
if (/^@\S+/u.test(trimmed)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 순수 리액션/감탄 문자열 (ㅋㅋㅋ, ㅎㅎ, ㅡㅡ, ㅜㅜ, ㅇㅇ 등)
|
||||
if (/^[ㅋㅎㅡㅜㅠㅇㄴㄷㅈ.?!~ㅉ]+$/u.test(trimmed)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export type LightweightIntent = "greeting" | "acknowledgement";
|
||||
|
||||
const ONBOARDING_KEYWORDS = [
|
||||
"닉네임",
|
||||
"닉변",
|
||||
"이름",
|
||||
"형식",
|
||||
"하트",
|
||||
"인증",
|
||||
"프로필",
|
||||
"사진",
|
||||
"스크린샷",
|
||||
"스샷",
|
||||
"디스코드",
|
||||
"discord",
|
||||
"디코",
|
||||
"온보딩",
|
||||
"가입",
|
||||
"등록",
|
||||
"절차",
|
||||
"방법",
|
||||
"어떻게",
|
||||
"뭐해",
|
||||
"뭐 해",
|
||||
"해야",
|
||||
"오류",
|
||||
"왜",
|
||||
"어디",
|
||||
"질문",
|
||||
"도움",
|
||||
];
|
||||
|
||||
function hasOnboardingKeyword(userMessage: string): boolean {
|
||||
const lower = userMessage.toLowerCase();
|
||||
return ONBOARDING_KEYWORDS.some((keyword) => lower.includes(keyword));
|
||||
}
|
||||
|
||||
export function detectLightweightIntent(
|
||||
userMessage: string,
|
||||
): LightweightIntent | null {
|
||||
const trimmed = userMessage.trim();
|
||||
if (!trimmed || hasOnboardingKeyword(trimmed)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (trimmed.length > 24) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const greetingPattern =
|
||||
/^(안녕(?:하세요)?|안뇽|하이|반갑(?:습니다|어요)|처음\s*왔(?:어요|습니다)|잘\s*부탁(?:드려요|드립니다))(?:[\s!?.~,ㅋㅎㅠㅜ]*)$/u;
|
||||
if (greetingPattern.test(trimmed)) {
|
||||
return "greeting";
|
||||
}
|
||||
|
||||
const acknowledgementPattern =
|
||||
/^(감사(?:합니다|해요|해용|해)|고마워(?:요)?|감사요|넵|네|넹|예|알겠(?:어요|습니다)|확인했(?:어요|습니다)|오케이|오키|ㅇㅋ)(?:[\s!?.~,ㅋㅎㅠㅜ]*)$/u;
|
||||
if (acknowledgementPattern.test(trimmed)) {
|
||||
return "acknowledgement";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function isRemainingStatusQuestion(userMessage: string): boolean {
|
||||
const trimmed = userMessage.trim();
|
||||
if (!trimmed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const detailRequestPattern =
|
||||
/(어떻게|어디|방법|어케|뭔지|뭐야|무슨\s*뜻|왜|예시|형식)/u;
|
||||
if (detailRequestPattern.test(trimmed)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const statusPattern =
|
||||
/(진행\s*상황|현황|상태|남은\s*(거|것|게|절차|단계|일)|뭐\s*남|뭐가\s*남|이제\s*뭐|얼마나\s*남|남았(?:지|나요|어요)?)/u;
|
||||
return statusPattern.test(trimmed);
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
import type { NicknameIssue, OnboardingSession } from "./types.js";
|
||||
|
||||
function pickRandom(items: [string, ...string[]]): string {
|
||||
return items[Math.floor(Math.random() * items.length)] ?? items[0];
|
||||
}
|
||||
|
||||
const VALID_GENDERS = new Set(["남", "여"]);
|
||||
const VALID_TIERS = new Set(["GM", "I", "B", "S", "G", "P", "E", "D", "M", "C", "U"]);
|
||||
|
||||
export function analyzeNicknameIssues(nickname: string): NicknameIssue[] {
|
||||
const parts = nickname.trim().split("/");
|
||||
const issues: NicknameIssue[] = [];
|
||||
|
||||
if (parts.length === 3) {
|
||||
const [, age, third] = parts;
|
||||
if (age && !/^\d{2}$/u.test(age)) {
|
||||
issues.push({
|
||||
kind: "invalid-age",
|
||||
message: "나이를 두 자리 숫자로 입력해주세요 (예: 25)",
|
||||
suggestion: "95",
|
||||
});
|
||||
}
|
||||
if (third) {
|
||||
const hasGender = VALID_GENDERS.has(third.charAt(0));
|
||||
const rest = third.slice(1);
|
||||
if (hasGender && rest.length > 0 && !VALID_TIERS.has(rest)) {
|
||||
issues.push({
|
||||
kind: "unknown-tier",
|
||||
message: `성별 뒤의 '${rest}'를 제거하고, 티어 코드를 '/'로 구분해서 추가해주세요 (예: ${third.charAt(0)}/D)`,
|
||||
suggestion: `${third.charAt(0)}/D`,
|
||||
});
|
||||
} else if (hasGender && rest.length > 0) {
|
||||
issues.push({
|
||||
kind: "wrong-separator",
|
||||
message: `성별과 티어 코드는 '/'로 구분해서 입력해주세요 (예: ${third.charAt(0)}/${rest})`,
|
||||
suggestion: `${third.charAt(0)}/${rest}`,
|
||||
});
|
||||
} else if (!hasGender) {
|
||||
issues.push({
|
||||
kind: "missing-gender",
|
||||
message: "성별(남/여)과 티어 코드를 '/'로 구분해서 입력해주세요",
|
||||
});
|
||||
} else {
|
||||
issues.push({
|
||||
kind: "missing-tier",
|
||||
message: `티어 코드를 '/'로 구분해서 추가해주세요 (예: ${third}/D)`,
|
||||
suggestion: `${third}/D`,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
issues.push({
|
||||
kind: "other",
|
||||
message: "성별과 티어 코드가 누락되었어요",
|
||||
});
|
||||
}
|
||||
return issues;
|
||||
}
|
||||
|
||||
if (parts.length !== 4) {
|
||||
issues.push({
|
||||
kind: "wrong-separator",
|
||||
message: `'/'로 구분된 4개 항목(이름/나이/성별/티어)이 필요해요. 현재 ${parts.length}개입니다.`,
|
||||
});
|
||||
return issues;
|
||||
}
|
||||
|
||||
const [, age, gender, tier] = parts;
|
||||
|
||||
if (!age || !/^\d{2}$/u.test(age)) {
|
||||
issues.push({
|
||||
kind: "invalid-age",
|
||||
message: "나이를 두 자리 숫자로 입력해주세요 (예: 25)",
|
||||
suggestion: "95",
|
||||
});
|
||||
}
|
||||
|
||||
if (!gender || !VALID_GENDERS.has(gender)) {
|
||||
issues.push({
|
||||
kind: "missing-gender",
|
||||
message: "성별을 '남' 또는 '여'로 입력해주세요",
|
||||
});
|
||||
}
|
||||
|
||||
if (!tier || !VALID_TIERS.has(tier)) {
|
||||
issues.push({
|
||||
kind: "unknown-tier",
|
||||
message: "티어 코드(GM,I,B,S,G,P,E,D,M,C,U 중 하나)를 입력해주세요",
|
||||
});
|
||||
}
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
export function getWelcomeMessage(nickname: string): string {
|
||||
return pickRandom([
|
||||
`${nickname}님 환영합니다! 새내기 등록은 닉네임 변경과 하트 인증 두 가지를 진행하시면 돼요.\n\n` +
|
||||
`1. 닉네임 변경: 이름/나이/성별/티어 형식 (예: 홍길동/25/남/G)\n` +
|
||||
`2. 하트 인증: 채팅방 프로필 하트를 빨간색으로 만들고 원본 스크린샷 전송\n\n` +
|
||||
`궁금한 점 있으시면 편하게 물어봐 주세요!`,
|
||||
`${nickname}님 반갑습니다! 톡 게시글의 공지를 참고하셔서 닉네임 변경과 하트 인증을 진행해 주세요.\n\n` +
|
||||
`닉네임: 이름/나이/성별/티어 형식으로 변경 (예: 홍길동/25/남/G)\n` +
|
||||
`하트 인증: 프로필 하트 빨간색 상태 원본 스크린샷 전송\n\n` +
|
||||
`어떤 것부터 시작하시겠어요?`,
|
||||
`${nickname}님 롤림푸스에 오신 걸 환영해요! 등록 절차를 안내해 드릴게요.\n\n` +
|
||||
`닉네임을 이름/나이/성별/티어 형식(예: 홍길동/25/남/G)으로 바꿔주시고, ` +
|
||||
`채팅방 프로필 하트를 빨간색으로 만든 원본 스크린샷을 보내주시면 됩니다. 순서는 상관없어요!`,
|
||||
]);
|
||||
}
|
||||
|
||||
export function getGreetingMessage(): string {
|
||||
return pickRandom([
|
||||
"환영합니다! 반갑습니다.",
|
||||
"반갑습니다! 어서 오세요.",
|
||||
"환영해요! 반갑습니다.",
|
||||
]);
|
||||
}
|
||||
|
||||
export function getAcknowledgementMessage(): string {
|
||||
return pickRandom([
|
||||
"네, 편하게 진행해 보시면 돼요.",
|
||||
"좋아요! 천천히 진행해 주세요.",
|
||||
"네, 그렇게 해주시면 돼요.",
|
||||
]);
|
||||
}
|
||||
|
||||
export function getNicknameInvalidMessage(
|
||||
nickname: string,
|
||||
_session: OnboardingSession | null,
|
||||
): string {
|
||||
const issues = analyzeNicknameIssues(nickname);
|
||||
|
||||
if (issues.length === 0) {
|
||||
return pickRandom([
|
||||
`@${nickname}님 닉네임 형식을 한번 확인해 주세요. 올바른 형식은 이름/나이/성별/티어예요. (예: 홍길동/25/남/G)`,
|
||||
`@${nickname}님 닉네임이 형식에 맞지 않는 것 같아요. 이름/나이(두자리)/성별(남,여)/티어코드 형식으로 변경해 주세요!`,
|
||||
]);
|
||||
}
|
||||
|
||||
const prefix = pickRandom([
|
||||
`@${nickname}님 닉네임에서 수정할 부분이 있어요!`,
|
||||
`@${nickname}님 닉네임을 조금만 수정해 주세요!`,
|
||||
]);
|
||||
|
||||
return `${prefix}\n${issues.map((issue) => issue.message).join("\n")}`;
|
||||
}
|
||||
|
||||
export function getNicknameFormatMessage(): string {
|
||||
return pickRandom([
|
||||
"닉네임 형식은 이름/출생 연도 두 자리/성별/티어예요. 예를 들면 홍길동/95/여/D처럼 쓰시면 돼요.",
|
||||
"닉네임은 이름/출생 연도 두 자리/성별/티어 형식으로 입력해 주시면 돼요! 예: 홍길동/95/남/G",
|
||||
]);
|
||||
}
|
||||
|
||||
export function getHeartGuideMessage(): string {
|
||||
return pickRandom([
|
||||
'채팅방 오른쪽 상단 메뉴 버튼에서 "커버 보기"로 들어가 하트를 빨간색으로 바꾼 뒤, 편집 없이 원본 스크린샷을 올려주시면 돼요.',
|
||||
"채팅방 프로필에서 하트를 눌러 빨간색으로 만든 다음, 그 화면이 보이게 원본 스크린샷을 보내주시면 인증돼요.",
|
||||
]);
|
||||
}
|
||||
|
||||
export function getHeartLocationMessage(): string {
|
||||
return pickRandom([
|
||||
'채팅방 오른쪽 상단 메뉴 버튼을 누르면 "커버 보기"가 보여요. 거기서 프로필과 하트를 확인하실 수 있어요.',
|
||||
'오른쪽 상단 메뉴 버튼으로 들어가 "커버 보기"를 열면 하트 위치를 확인하실 수 있어요.',
|
||||
]);
|
||||
}
|
||||
|
||||
export function getAgeRequirementMessage(): string {
|
||||
return pickRandom([
|
||||
"롤림푸스는 27세 이상만 가입이 가능해요.",
|
||||
"가입 가능 연령은 27세 이상이에요.",
|
||||
]);
|
||||
}
|
||||
|
||||
export function getPhotoFailedMessage(nickname: string): string {
|
||||
return pickRandom([
|
||||
`${nickname}님 하트 인증 사진이 인식되지 않았어요. ` +
|
||||
`채팅방 프로필에서 하트를 눌러 빨간색으로 만든 후, 편집 없이 원본 스크린샷을 보내주시면 됩니다!`,
|
||||
`${nickname}님 하트 인증이 아직 확인되지 않았어요. ` +
|
||||
`프로필 하트가 빨간색인 상태의 스크린샷을 편집 없이 원본 그대로 다시 보내주시겠어요?`,
|
||||
]);
|
||||
}
|
||||
|
||||
export function getProgressMessage(
|
||||
nickname: string,
|
||||
session: OnboardingSession,
|
||||
): string {
|
||||
const nicknameIcon = session.nicknameVerified ? "v" : "x";
|
||||
const photoIcon = session.photoVerified ? "v" : "x";
|
||||
|
||||
let nextStep = "";
|
||||
if (!session.nicknameVerified && !session.photoVerified) {
|
||||
nextStep = pickRandom([
|
||||
"\n\n톡 게시글의 공지를 참고하셔서 닉네임 변경과 하트 인증을 진행해 주세요!",
|
||||
"\n\n아직 두 가지 모두 남아있어요. 어떤 것부터 시작하시겠어요?",
|
||||
]);
|
||||
} else if (!session.nicknameVerified) {
|
||||
nextStep = pickRandom([
|
||||
"\n\n닉네임 변경만 완료하시면 돼요! 이름/나이/성별/티어 형식으로 변경해 주세요.",
|
||||
"\n\n하트 인증은 완료됐어요! 닉네임만 변경해 주시면 끝이에요.",
|
||||
]);
|
||||
} else if (!session.photoVerified) {
|
||||
nextStep = pickRandom([
|
||||
"\n\n하트 인증만 남았어요! 프로필 하트를 빨간색으로 만든 스크린샷을 보내주세요.",
|
||||
"\n\n닉네임은 완료됐어요! 하트 인증 스크린샷만 보내주시면 끝이에요.",
|
||||
]);
|
||||
}
|
||||
|
||||
return (
|
||||
`${nickname}님 현재 진행상황: 닉네임 [${nicknameIcon}] | 하트인증 [${photoIcon}]` +
|
||||
nextStep
|
||||
);
|
||||
}
|
||||
|
||||
export function getTimeoutWarningMessage(nickname: string): string {
|
||||
return pickRandom([
|
||||
`${nickname}님 등록 시간이 얼마 남지 않았어요! 아직 완료하지 못한 절차가 있다면 서둘러 주세요.`,
|
||||
`${nickname}님 등록 마감이 가까워지고 있어요. 남은 절차를 빠르게 진행해 주세요!`,
|
||||
]);
|
||||
}
|
||||
|
||||
export function getCompletedMessage(nickname: string): string {
|
||||
return pickRandom([
|
||||
`${nickname}님 모든 절차가 완료되었어요! 즐거운 롤림푸스 생활 되세요.`,
|
||||
`${nickname}님 등록이 모두 끝났어요! 앞으로 롤림푸스에서 즐거운 시간 보내세요.`,
|
||||
]);
|
||||
}
|
||||
|
||||
export function getGenericHelpMessage(
|
||||
nickname: string,
|
||||
session: OnboardingSession | null,
|
||||
): string {
|
||||
if (!session || (!session.nicknameVerified && !session.photoVerified)) {
|
||||
return pickRandom([
|
||||
`톡 게시글의 공지를 참고하셔서 닉네임 변경과 하트 인증을 진행해 주세요!`,
|
||||
`닉네임 변경과 하트 인증 두 가지를 완료하시면 돼요. 톡 게시글의 공지를 확인해 주세요!`,
|
||||
]);
|
||||
}
|
||||
|
||||
if (!session.nicknameVerified) {
|
||||
return pickRandom([
|
||||
`닉네임을 이름/나이/성별/티어 형식으로 변경해 주세요! (예: 홍길동/25/남/G)`,
|
||||
`닉네임 변경이 아직 안 됐어요. 이름/나이/성별/티어 형식으로 바꿔주시면 돼요!`,
|
||||
]);
|
||||
}
|
||||
|
||||
if (!session.photoVerified) {
|
||||
return pickRandom([
|
||||
`하트 인증만 남았어요! 채팅방 프로필 하트를 빨간색으로 만든 스크린샷을 보내주세요.`,
|
||||
`하트 인증 스크린샷만 보내주시면 끝이에요! 프로필 하트를 빨간색으로 만들어 주세요.`,
|
||||
]);
|
||||
}
|
||||
|
||||
return `${nickname}님 두 절차가 모두 확인되면 디스코드 권한이 자동으로 부여돼요!`;
|
||||
}
|
||||
|
||||
export function getRemainingStatusMessage(
|
||||
session: OnboardingSession | null,
|
||||
): string {
|
||||
if (!session || (!session.nicknameVerified && !session.photoVerified)) {
|
||||
return "이제 닉네임 변경과 방 하트 인증 두 가지가 남아 있어요.";
|
||||
}
|
||||
|
||||
if (!session.nicknameVerified) {
|
||||
return "이제 닉네임 변경만 남았어요.";
|
||||
}
|
||||
|
||||
if (!session.photoVerified) {
|
||||
return "이제 방 하트 인증만 남았어요.";
|
||||
}
|
||||
|
||||
return "이제 새내기 등록 절차는 모두 완료됐어요.";
|
||||
}
|
||||
|
||||
export function getDiscordMessage(
|
||||
nickname: string,
|
||||
session: OnboardingSession | null,
|
||||
): string {
|
||||
if (session?.nicknameVerified && session?.photoVerified) {
|
||||
return pickRandom([
|
||||
`${nickname}님 두 절차 모두 완료되셨어요! 디스코드 서버에 입장해서 서버 프로필을 설정해 주시면 봇이 자동으로 권한을 부여해 드려요.`,
|
||||
`${nickname}님 닉네임과 하트 인증 모두 완료됐어요! 이제 디스코드 서버에 입장하신 후 서버 프로필을 설정하시면 권한이 자동 부여돼요.`,
|
||||
]);
|
||||
}
|
||||
|
||||
return pickRandom([
|
||||
`${nickname}님 닉네임 변경과 하트 인증을 먼저 완료해 주셔야 디스코드 안내를 드릴 수 있어요!`,
|
||||
`${nickname}님 디스코드 권한은 닉네임 변경과 하트 인증을 모두 완료하신 후에 안내드릴게요!`,
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { PromptBuilder } from "./builder.js";
|
||||
import type { DerivedState } from "../state/onboarding-state.js";
|
||||
import type { OnboardingSession, PromptContext } from "../types.js";
|
||||
|
||||
function makeDerived(
|
||||
overrides?: Partial<DerivedState>,
|
||||
): DerivedState {
|
||||
return {
|
||||
phase: { kind: "photo-pending" },
|
||||
intent: { kind: "photo-question" },
|
||||
shouldRespond: true,
|
||||
responseMode: "llm",
|
||||
suggestedMaxTokens: 256,
|
||||
allowShortResponse: false,
|
||||
tier: 0,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeSession(
|
||||
overrides?: Partial<OnboardingSession>,
|
||||
): OnboardingSession {
|
||||
return {
|
||||
chatroomId: "room-1",
|
||||
deadlineAt: "2026-04-16T12:00:00.000Z",
|
||||
joinedAt: "2026-04-16T10:00:00.000Z",
|
||||
lastEventAt: "2026-04-16T10:30:00.000Z",
|
||||
nickname: "프리리/95/여/D",
|
||||
nicknameVerified: true,
|
||||
photoVerified: false,
|
||||
status: "pending",
|
||||
userId: "user-1",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeContext(
|
||||
overrides?: Partial<PromptContext>,
|
||||
): PromptContext {
|
||||
return {
|
||||
blackboard: {
|
||||
flaggedIssues: [],
|
||||
openQuestions: [],
|
||||
recentNicknameAttempts: [],
|
||||
toneMemory: {},
|
||||
},
|
||||
conversationHistory: Array.from({ length: 7 }, (_, index) => ({
|
||||
role: index % 2 === 0 ? "user" : "assistant",
|
||||
content: `message-${index + 1}`,
|
||||
timestamp: `2026-04-16T10:0${index}:00.000Z`,
|
||||
})),
|
||||
summary: "이전 대화 요약",
|
||||
userMessage: "하트 인증은 어떻게 해요?",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("PromptBuilder", () => {
|
||||
it("includes intent-matched examples in system prompt", () => {
|
||||
const messages = PromptBuilder.forDerived(
|
||||
makeDerived(),
|
||||
makeSession(),
|
||||
makeContext(),
|
||||
).build();
|
||||
|
||||
expect(messages[0]?.role).toBe("system");
|
||||
expect(messages[0]?.content).toContain("하트 인증이 뭐예요?");
|
||||
expect(messages[0]?.content).not.toContain("나이 제한 있나요?");
|
||||
});
|
||||
|
||||
it("limits prompt history to the latest five turns when summary exists", () => {
|
||||
const messages = PromptBuilder.forDerived(
|
||||
makeDerived(),
|
||||
makeSession(),
|
||||
makeContext(),
|
||||
).build();
|
||||
|
||||
expect(messages).toHaveLength(7);
|
||||
expect(messages[1]?.content).toBe("message-3");
|
||||
expect(messages[5]?.content).toBe("message-7");
|
||||
expect(messages[0]?.content).toContain("이전 대화 요약");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { ChatMessage } from "@llm/types";
|
||||
import type { DerivedState } from "../state/onboarding-state.js";
|
||||
import type { OnboardingSession, PromptContext } from "../types.js";
|
||||
import {
|
||||
contextLayer,
|
||||
examplesLayer,
|
||||
personaLayer,
|
||||
policyLayer,
|
||||
stateLayer,
|
||||
} from "./layers.js";
|
||||
|
||||
export class PromptBuilder {
|
||||
private constructor(
|
||||
private readonly derived: DerivedState,
|
||||
private readonly session: OnboardingSession | null,
|
||||
private readonly context: PromptContext,
|
||||
) {}
|
||||
|
||||
static forDerived(
|
||||
derived: DerivedState,
|
||||
session: OnboardingSession | null,
|
||||
context: PromptContext,
|
||||
): PromptBuilder {
|
||||
return new PromptBuilder(derived, session, context);
|
||||
}
|
||||
|
||||
build(): ChatMessage[] {
|
||||
const promptHistory = this.context.summary
|
||||
? this.context.conversationHistory.slice(-5)
|
||||
: this.context.conversationHistory;
|
||||
const promptContext: PromptContext = {
|
||||
...this.context,
|
||||
conversationHistory: promptHistory,
|
||||
};
|
||||
|
||||
const systemPrompt = [
|
||||
personaLayer(),
|
||||
policyLayer(this.derived.intent),
|
||||
stateLayer(this.session, this.derived),
|
||||
examplesLayer(this.derived.intent),
|
||||
contextLayer(promptContext),
|
||||
].join("\n\n");
|
||||
|
||||
return [
|
||||
{ role: "system", content: systemPrompt },
|
||||
...promptHistory.map((message) => ({
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
})),
|
||||
{ role: "user", content: this.context.userMessage },
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import type { Intent } from "../state/onboarding-state.js";
|
||||
import type { Example } from "../types.js";
|
||||
|
||||
export const EXAMPLES_BY_INTENT: Record<Intent["kind"], Example[]> = {
|
||||
greeting: [
|
||||
{
|
||||
user: "안녕하세요!",
|
||||
assistant: "환영합니다! 반갑습니다.",
|
||||
},
|
||||
{
|
||||
user: "반갑습니다",
|
||||
assistant: "반갑습니다! 어서 오세요.",
|
||||
},
|
||||
],
|
||||
acknowledgement: [
|
||||
{
|
||||
user: "넵 확인했어요",
|
||||
assistant: "네, 편하게 진행해 보시면 돼요.",
|
||||
},
|
||||
{
|
||||
user: "감사합니다",
|
||||
assistant: "좋아요! 천천히 진행해 주세요.",
|
||||
},
|
||||
],
|
||||
"status-question": [
|
||||
{
|
||||
user: "이제 뭐 남았지?",
|
||||
assistant: "이제 방 하트 인증만 남았어요.",
|
||||
},
|
||||
{
|
||||
user: "진행상황 알려줘",
|
||||
assistant: "아직 닉네임 변경과 방 하트 인증 두 가지가 남아 있어요.",
|
||||
},
|
||||
],
|
||||
"help-how": [
|
||||
{
|
||||
user: "하트 인증은 어떻게 해요?",
|
||||
assistant:
|
||||
'채팅방 오른쪽 상단 메뉴 버튼에서 "커버 보기"로 들어가 하트를 빨간색으로 바꾼 뒤, 편집 없이 원본 스크린샷을 올려주시면 돼요.',
|
||||
},
|
||||
{
|
||||
user: "닉네임 형식 예시 알려줘",
|
||||
assistant:
|
||||
"닉네임은 이름/출생 연도 두 자리/성별/티어 형식이에요. 예를 들면 홍길동/95/여/D처럼 쓰시면 돼요.",
|
||||
},
|
||||
],
|
||||
"help-where": [
|
||||
{
|
||||
user: "하트 어디서 바꿔요?",
|
||||
assistant:
|
||||
'채팅방 오른쪽 상단 메뉴 버튼을 누르면 "커버 보기"가 보여요. 거기서 프로필과 하트를 확인하실 수 있어요.',
|
||||
},
|
||||
],
|
||||
"nickname-question": [
|
||||
{
|
||||
user: "닉네임이 왜 안 돼요?",
|
||||
assistant:
|
||||
"티어는 한 글자 코드로 넣어야 해요. 예를 들면 D처럼 바꿔주시면 돼요.",
|
||||
},
|
||||
{
|
||||
user: "닉네임 형식이 뭐예요?",
|
||||
assistant:
|
||||
"닉네임은 이름/출생 연도 두 자리/성별/티어 형식으로 입력해 주시면 돼요.",
|
||||
},
|
||||
],
|
||||
"photo-question": [
|
||||
{
|
||||
user: "하트 인증이 뭐예요?",
|
||||
assistant:
|
||||
"채팅방 프로필에서 하트를 빨간색으로 바꾼 뒤, 그 화면이 보이게 원본 스크린샷을 보내주시면 돼요.",
|
||||
},
|
||||
],
|
||||
"discord-question": [
|
||||
{
|
||||
user: "디스코드는 언제 들어가요?",
|
||||
assistant:
|
||||
"닉네임 변경과 하트 인증을 모두 완료하신 뒤에 디스코드 안내를 받으실 수 있어요.",
|
||||
},
|
||||
],
|
||||
"age-question": [
|
||||
{
|
||||
user: "나이 제한 있나요?",
|
||||
assistant: "롤림푸스는 27세 이상만 가입이 가능해요.",
|
||||
},
|
||||
],
|
||||
chitchat: [
|
||||
{
|
||||
user: "오늘 날씨 좋네요",
|
||||
assistant: "[NO_RESPONSE]",
|
||||
},
|
||||
],
|
||||
"off-topic-to-other": [
|
||||
{
|
||||
user: "@프리리 이거 확인해줘",
|
||||
assistant: "[NO_RESPONSE]",
|
||||
},
|
||||
],
|
||||
unknown: [
|
||||
{
|
||||
user: "가입은 어떻게 하나요?",
|
||||
assistant:
|
||||
"톡 게시글의 공지를 참고하셔서 닉네임 변경과 하트 인증을 진행해 주세요!",
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,194 @@
|
||||
import type { ConversationMessage } from "@agent/types";
|
||||
import type { RoomMessage } from "@room/types";
|
||||
import type { DerivedState, Intent } from "../state/onboarding-state.js";
|
||||
import type {
|
||||
Blackboard,
|
||||
Example,
|
||||
OnboardingSession,
|
||||
PromptContext,
|
||||
} from "../types.js";
|
||||
import { EXAMPLES_BY_INTENT } from "./examples.js";
|
||||
|
||||
function formatKstTimestamp(value: string | undefined): string {
|
||||
if (!value) {
|
||||
return "없음";
|
||||
}
|
||||
|
||||
const timestamp = Date.parse(value);
|
||||
if (!Number.isFinite(timestamp)) {
|
||||
return "없음";
|
||||
}
|
||||
|
||||
return new Intl.DateTimeFormat("ko-KR", {
|
||||
dateStyle: "short",
|
||||
timeStyle: "short",
|
||||
timeZone: "Asia/Seoul",
|
||||
}).format(new Date(timestamp));
|
||||
}
|
||||
|
||||
function formatNicknameStatus(session: OnboardingSession | null): string {
|
||||
if (!session) {
|
||||
return "미완료";
|
||||
}
|
||||
|
||||
return session.nicknameVerified
|
||||
? `완료 (${session.nickname})`
|
||||
: `미완료 (현재: ${session.nickname})`;
|
||||
}
|
||||
|
||||
function formatPhotoStatus(session: OnboardingSession | null): string {
|
||||
if (!session) {
|
||||
return "미완료";
|
||||
}
|
||||
|
||||
return session.photoVerified ? "완료" : "미완료";
|
||||
}
|
||||
|
||||
function formatHistory(history: ConversationMessage[]): string {
|
||||
if (history.length === 0) {
|
||||
return "(최근 대화 없음)";
|
||||
}
|
||||
|
||||
return history.map((message) => `${message.role}: ${message.content}`).join("\n");
|
||||
}
|
||||
|
||||
function formatRoomMessages(roomContext?: RoomMessage[]): string {
|
||||
if (!roomContext || roomContext.length === 0) {
|
||||
return "(방 분위기 정보 없음)";
|
||||
}
|
||||
|
||||
return roomContext
|
||||
.slice(-5)
|
||||
.map((message) => `${message.nickname}: ${message.message}`)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function formatBlackboard(blackboard: Blackboard): string {
|
||||
const openQuestions =
|
||||
blackboard.openQuestions.length > 0
|
||||
? blackboard.openQuestions.join(", ")
|
||||
: "(없음)";
|
||||
const flaggedIssues =
|
||||
blackboard.flaggedIssues.length > 0
|
||||
? blackboard.flaggedIssues.join(", ")
|
||||
: "(없음)";
|
||||
const nicknameAttempts =
|
||||
blackboard.recentNicknameAttempts.length > 0
|
||||
? blackboard.recentNicknameAttempts.join(", ")
|
||||
: "(없음)";
|
||||
|
||||
return [
|
||||
`- lastIntent: ${blackboard.lastIntent ?? "(없음)"}`,
|
||||
`- openQuestions: ${openQuestions}`,
|
||||
`- flaggedIssues: ${flaggedIssues}`,
|
||||
`- recentNicknameAttempts: ${nicknameAttempts}`,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function formatExample(example: Example): string {
|
||||
return `유저: ${example.user}\n응답: ${example.assistant}`;
|
||||
}
|
||||
|
||||
export function personaLayer(): string {
|
||||
return `너는 롤림푸스(lolympus) 카카오톡 오픈채팅에서 새내기 등록을 도와주는 친절한 선배야.
|
||||
처음 온 멤버가 편안하게 등록을 마칠 수 있도록 자연스럽고 정확하게 안내해줘.
|
||||
|
||||
[커뮤니티 규칙]
|
||||
- 가입 가능 연령은 27세 이상이야.
|
||||
- 새내기 등록은 닉네임 변경과 방 하트 인증 두 가지를 모두 완료하면 돼.
|
||||
- 닉네임 형식은 이름/출생 연도 두 자리/성별/티어야. 예: 홍길동/95/남/G
|
||||
- 성별은 남 또는 여, 티어 코드는 GM, I, B, S, G, P, E, D, M, C, U 중 하나야.
|
||||
- 방 하트 인증은 채팅방 프로필에서 하트를 빨간색으로 바꾼 뒤 편집 없는 원본 스크린샷을 보내는 절차야.
|
||||
- 디스코드 안내는 닉네임 변경과 하트 인증이 모두 완료된 뒤에만 해줘.`;
|
||||
}
|
||||
|
||||
export function policyLayer(
|
||||
intent: Intent,
|
||||
options?: { toolPolicy?: string | undefined },
|
||||
): string {
|
||||
const lines = [
|
||||
"[답변 원칙]",
|
||||
"- 마지막 메시지에서 직접 물은 내용에만 정확히 답해.",
|
||||
"- 사용자가 요청하지 않은 다음 단계, 방법, 위치, 예시는 먼저 붙이지 마.",
|
||||
"- 마크다운 문법, 링크, URL은 쓰지 마.",
|
||||
'- 온보딩과 무관한 잡담이거나 다른 사람에게 하는 말이면 "[NO_RESPONSE]"만 출력해.',
|
||||
];
|
||||
|
||||
switch (intent.kind) {
|
||||
case "greeting":
|
||||
lines.push("- 짧은 인사만 1문장으로 답해.");
|
||||
break;
|
||||
case "acknowledgement":
|
||||
lines.push("- 짧은 확인이나 마무리만 1문장으로 답해.");
|
||||
break;
|
||||
case "status-question":
|
||||
lines.push("- 현재 남은 절차나 완료 상태만 1문장으로 짧게 답해.");
|
||||
lines.push("- 방법 설명, 위치 설명, 예시는 넣지 마.");
|
||||
break;
|
||||
case "help-where":
|
||||
lines.push("- 위치나 경로만 답하고, 요청하지 않은 다음 단계는 붙이지 마.");
|
||||
break;
|
||||
case "help-how":
|
||||
lines.push("- 사용자가 물은 한 가지 절차의 방법만 답해.");
|
||||
break;
|
||||
case "nickname-question":
|
||||
lines.push("- 닉네임 형식 또는 오류 원인만 답하고 다른 절차는 언급하지 마.");
|
||||
break;
|
||||
case "photo-question":
|
||||
lines.push("- 하트 인증에 대해서만 답하고 다른 절차는 언급하지 마.");
|
||||
break;
|
||||
case "discord-question":
|
||||
lines.push("- 디스코드 질문에만 답해. 완료 전이라면 먼저 온보딩 완료가 필요하다고만 알려줘.");
|
||||
break;
|
||||
case "age-question":
|
||||
lines.push("- 나이 제한만 간단히 답해.");
|
||||
break;
|
||||
default:
|
||||
lines.push("- 질문 범위를 넘어서 먼저 설명하지 마.");
|
||||
break;
|
||||
}
|
||||
|
||||
if (options?.toolPolicy) {
|
||||
lines.push(options.toolPolicy);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function stateLayer(
|
||||
session: OnboardingSession | null,
|
||||
derived: DerivedState,
|
||||
): string {
|
||||
return [
|
||||
"[현재 유저 상태]",
|
||||
`- phase: ${derived.phase.kind}`,
|
||||
`- nickname: ${formatNicknameStatus(session)}`,
|
||||
`- photo: ${formatPhotoStatus(session)}`,
|
||||
`- deadline(KST): ${formatKstTimestamp(session?.deadlineAt)}`,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function examplesLayer(intent: Intent, count = 3): string {
|
||||
const examples = EXAMPLES_BY_INTENT[intent.kind].slice(0, count);
|
||||
const rendered = examples.length > 0
|
||||
? examples.map(formatExample).join("\n\n")
|
||||
: "(예시 없음)";
|
||||
|
||||
return `[응답 예시]\n${rendered}`;
|
||||
}
|
||||
|
||||
export function contextLayer(context: PromptContext): string {
|
||||
return [
|
||||
"[최근 요약]",
|
||||
context.summary ?? "(요약 없음)",
|
||||
"",
|
||||
"[최근 대화]",
|
||||
formatHistory(context.conversationHistory),
|
||||
"",
|
||||
"[방 분위기]",
|
||||
formatRoomMessages(context.roomContext),
|
||||
"",
|
||||
"[메모]",
|
||||
formatBlackboard(context.blackboard),
|
||||
].join("\n");
|
||||
}
|
||||
@@ -0,0 +1,577 @@
|
||||
import type { AgentRequest, AgentResponse, ConversationMessage } from "@agent/types";
|
||||
import { getConversationSummaryKey } from "@conv/conversation-store";
|
||||
import type { GarnetClient } from "@common/garnet";
|
||||
import { getRequestLogger } from "@obs/context";
|
||||
import type { LlmClient } from "@llm/llm-client";
|
||||
import type { ChatMessage } from "@llm/types";
|
||||
import type { RoomContextService } from "@room/room-context-service";
|
||||
import type { Skill } from "@skills/skill";
|
||||
import { PromptBuilder } from "./prompts/builder.js";
|
||||
import {
|
||||
getAcknowledgementMessage,
|
||||
getAgeRequirementMessage,
|
||||
getCompletedMessage,
|
||||
getDiscordMessage,
|
||||
getGreetingMessage,
|
||||
getGenericHelpMessage,
|
||||
getHeartGuideMessage,
|
||||
getHeartLocationMessage,
|
||||
getNicknameInvalidMessage,
|
||||
getNicknameFormatMessage,
|
||||
getPhotoFailedMessage,
|
||||
getProgressMessage,
|
||||
getRemainingStatusMessage,
|
||||
getTimeoutWarningMessage,
|
||||
} from "./onboarding-templates.js";
|
||||
import { getOnboardingSessionKeyPrefix, getMaxResponseLength, getLlmTemperature } from "../../config.js";
|
||||
import { OnboardingBlackboardStore } from "./state/blackboard.js";
|
||||
import { deriveState } from "./state/derive.js";
|
||||
import { OnboardingIntentClassifier } from "./state/classifier.js";
|
||||
import type { DerivedState, Intent } from "./state/onboarding-state.js";
|
||||
import type { OnboardingSession, PromptContext } from "./types.js";
|
||||
|
||||
const ALLOWED_URL_PATTERNS = [
|
||||
/discord\.gg\/lolympus/u,
|
||||
/open\.kakao\.com\/o\/gOscBAEh/u,
|
||||
];
|
||||
|
||||
type LlmTextResult =
|
||||
| { status: "success"; text: string }
|
||||
| { status: "no_response" }
|
||||
| { status: "failed" };
|
||||
|
||||
function postProcessResponse(text: string): string {
|
||||
let processed = text.trim();
|
||||
|
||||
// Remove <think> blocks (Qwen thinking mode artifacts)
|
||||
processed = processed.replace(/<think>[\s\S]*?<\/think>/gu, "").trim();
|
||||
|
||||
// Strip URLs not in allowlist
|
||||
processed = processed.replace(
|
||||
/https?:\/\/[^\s)}\]]+/gu,
|
||||
(match) => {
|
||||
if (ALLOWED_URL_PATTERNS.some((pattern) => pattern.test(match))) {
|
||||
return match;
|
||||
}
|
||||
return "";
|
||||
},
|
||||
);
|
||||
|
||||
// Clean up double spaces from URL removal
|
||||
processed = processed.replace(/ +/gu, " ").trim();
|
||||
|
||||
// Strip markdown formatting (KakaoTalk is plaintext)
|
||||
processed = processed.replace(/\*\*(.+?)\*\*/gu, "$1"); // **bold**
|
||||
processed = processed.replace(/\*(.+?)\*/gu, "$1"); // *italic*
|
||||
processed = processed.replace(/__(.+?)__/gu, "$1"); // __underline__
|
||||
processed = processed.replace(/~~(.+?)~~/gu, "$1"); // ~~strike~~
|
||||
processed = processed.replace(/^#{1,6}\s+/gmu, ""); // ## headings
|
||||
processed = processed.replace(/^[-*]\s+/gmu, ""); // - or * list items
|
||||
processed = processed.replace(/`(.+?)`/gu, "$1"); // `inline code`
|
||||
|
||||
// Truncate to max length with Korean sentence boundary awareness
|
||||
const maxLen = getMaxResponseLength();
|
||||
if (processed.length > maxLen) {
|
||||
processed = processed.slice(0, maxLen).trimEnd();
|
||||
// Search backwards for a Korean sentence-ending pattern
|
||||
const sentenceEndPattern = /(?:세요[.!]?|해요[.!]?|돼요[.!]?|어요[.!]?|아요[.!]?|거든요[.!]?|습니다[.!]?|됩니다[.!]?|니다[.!]?|요[.!]|다[.!]|[.!?])\s*$/u;
|
||||
// Try progressively shorter substrings to find a sentence boundary
|
||||
for (let i = processed.length; i > maxLen * 0.5; i--) {
|
||||
const sub = processed.slice(0, i);
|
||||
if (sentenceEndPattern.test(sub)) {
|
||||
processed = sub;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return processed.trim();
|
||||
}
|
||||
|
||||
function isLowQualityResponse(
|
||||
text: string,
|
||||
options?: { allowShortResponse?: boolean },
|
||||
): boolean {
|
||||
if (!options?.allowShortResponse && text.length < 10) return true;
|
||||
// Check for excessive repetition (same phrase 3+ times)
|
||||
const words = text.split(/\s+/u);
|
||||
if (words.length >= 6) {
|
||||
const phrases = new Map<string, number>();
|
||||
for (let i = 0; i < words.length - 1; i++) {
|
||||
const bigram = `${words[i]} ${words[i + 1]}`;
|
||||
phrases.set(bigram, (phrases.get(bigram) ?? 0) + 1);
|
||||
}
|
||||
for (const count of phrases.values()) {
|
||||
if (count >= 3) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export class OnboardingSkill implements Skill {
|
||||
readonly name = "onboarding";
|
||||
private readonly classifier: OnboardingIntentClassifier;
|
||||
private readonly blackboardStore: OnboardingBlackboardStore;
|
||||
|
||||
constructor(
|
||||
private readonly garnetClient: GarnetClient,
|
||||
private readonly llmClient: LlmClient,
|
||||
private readonly roomContextService?: RoomContextService,
|
||||
) {
|
||||
this.classifier = new OnboardingIntentClassifier(llmClient);
|
||||
this.blackboardStore = new OnboardingBlackboardStore(garnetClient);
|
||||
}
|
||||
|
||||
canHandle(request: AgentRequest): boolean {
|
||||
return request.skill === "onboarding";
|
||||
}
|
||||
|
||||
async handle(
|
||||
request: AgentRequest,
|
||||
conversationHistory: ConversationMessage[],
|
||||
): Promise<AgentResponse> {
|
||||
const logger = getRequestLogger();
|
||||
const session = await this.loadSession(
|
||||
request.chatroomId,
|
||||
request.userId,
|
||||
);
|
||||
|
||||
if (request.trigger === "user-message" && !request.userMessage?.trim()) {
|
||||
return this.makeEmptyResponse(request.requestId);
|
||||
}
|
||||
|
||||
const classified = await this.classifier.classify(
|
||||
request.userMessage,
|
||||
request.trigger,
|
||||
);
|
||||
const derived = deriveState(session, request.trigger, classified.intent);
|
||||
|
||||
if (!derived.shouldRespond) {
|
||||
return this.makeEmptyResponse(request.requestId);
|
||||
}
|
||||
|
||||
let response: AgentResponse;
|
||||
|
||||
if (derived.responseMode === "template") {
|
||||
response = this.makeTemplateFallbackResponse(
|
||||
request,
|
||||
session,
|
||||
derived.intent,
|
||||
derived,
|
||||
);
|
||||
} else {
|
||||
const llmResult = await this.handleLlm(
|
||||
request,
|
||||
session,
|
||||
conversationHistory,
|
||||
derived,
|
||||
);
|
||||
|
||||
if (llmResult.status === "success") {
|
||||
response = this.makeLlmResponse(request.requestId, llmResult.text);
|
||||
} else if (llmResult.status === "no_response") {
|
||||
response = this.makeEmptyResponse(request.requestId);
|
||||
} else {
|
||||
response = this.makeTemplateFallbackResponse(
|
||||
request,
|
||||
session,
|
||||
derived.intent,
|
||||
derived,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.text.trim()) {
|
||||
await this.rememberTurn(request, derived).catch((error) => {
|
||||
logger.warn({ err: error }, "[OnboardingSkill] Failed to update blackboard");
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private async loadSession(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
): Promise<OnboardingSession | null> {
|
||||
const prefix = getOnboardingSessionKeyPrefix();
|
||||
const key = `${prefix}:onboarding:${chatroomId}:${userId}`;
|
||||
return this.garnetClient.getJson<OnboardingSession>(key);
|
||||
}
|
||||
|
||||
private async handleLlm(
|
||||
request: AgentRequest,
|
||||
session: OnboardingSession | null,
|
||||
conversationHistory: ConversationMessage[],
|
||||
derived: DerivedState,
|
||||
): Promise<LlmTextResult> {
|
||||
const messages = await this.buildLlmMessages(
|
||||
request,
|
||||
session,
|
||||
conversationHistory,
|
||||
derived,
|
||||
);
|
||||
|
||||
return this.generateLlmText(messages, {
|
||||
allowShortResponse: derived.allowShortResponse,
|
||||
maxTokens: derived.suggestedMaxTokens,
|
||||
retryOnLowQuality:
|
||||
request.trigger === "user-message" && !derived.allowShortResponse,
|
||||
});
|
||||
}
|
||||
|
||||
private async buildLlmMessages(
|
||||
request: AgentRequest,
|
||||
session: OnboardingSession | null,
|
||||
conversationHistory: ConversationMessage[],
|
||||
derived: DerivedState,
|
||||
): Promise<ChatMessage[]> {
|
||||
const logger = getRequestLogger();
|
||||
const roomContextPromise =
|
||||
request.trigger === "user-message" && this.roomContextService
|
||||
? this.roomContextService
|
||||
.getRecentMessages(request.chatroomId)
|
||||
.catch((error) => {
|
||||
logger.warn({ err: error }, "[OnboardingSkill] Failed to fetch room context");
|
||||
return undefined;
|
||||
})
|
||||
: Promise.resolve(undefined);
|
||||
const [blackboard, summary, roomContext] = await Promise.all([
|
||||
this.blackboardStore.load(request.chatroomId, request.userId),
|
||||
this.loadConversationSummary(request.chatroomId, request.userId),
|
||||
roomContextPromise,
|
||||
]);
|
||||
|
||||
const context: PromptContext = {
|
||||
blackboard,
|
||||
conversationHistory,
|
||||
roomContext,
|
||||
summary,
|
||||
userMessage: this.buildPromptUserMessage(request, derived),
|
||||
};
|
||||
|
||||
return PromptBuilder.forDerived(derived, session, context).build();
|
||||
}
|
||||
|
||||
private buildPromptUserMessage(
|
||||
request: AgentRequest,
|
||||
derived: DerivedState,
|
||||
): string {
|
||||
switch (request.trigger) {
|
||||
case "nickname-invalid":
|
||||
return `현재 닉네임은 "${request.nickname}"예요. 왜 안 되는지와 어디를 고치면 되는지만 알려줘.`;
|
||||
case "photo-failed":
|
||||
return "하트 인증 사진이 실패했어요. 다시 시도할 수 있게 필요한 내용만 안내해줘.";
|
||||
case "progress-update":
|
||||
return "현재 새내기 등록 진행상황만 짧게 알려줘.";
|
||||
case "timeout-warning":
|
||||
return "등록 마감이 가까워졌어요. 남은 절차만 짧고 따뜻하게 안내해줘.";
|
||||
case "completed":
|
||||
return "새내기 등록을 모두 마친 유저에게 짧고 따뜻하게 축하 인사를 해줘.";
|
||||
case "user-message":
|
||||
if (derived.intent.kind === "status-question") {
|
||||
return "현재 남은 절차나 완료 상태만 1문장으로 아주 짧게 알려줘.";
|
||||
}
|
||||
return request.userMessage?.trim() || "지금 필요한 온보딩 안내만 도와줘.";
|
||||
default:
|
||||
return "지금 필요한 온보딩 안내만 짧게 도와줘.";
|
||||
}
|
||||
}
|
||||
|
||||
private async loadConversationSummary(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
): Promise<string | undefined> {
|
||||
const key = getConversationSummaryKey(chatroomId, userId);
|
||||
return (await this.garnetClient.getString(key)) ?? undefined;
|
||||
}
|
||||
|
||||
private async rememberTurn(
|
||||
request: AgentRequest,
|
||||
derived: DerivedState,
|
||||
): Promise<void> {
|
||||
const now = new Date().toISOString();
|
||||
const userMessage = request.userMessage?.trim();
|
||||
|
||||
await this.blackboardStore.update(
|
||||
request.chatroomId,
|
||||
request.userId,
|
||||
(current) => {
|
||||
const openQuestions =
|
||||
userMessage &&
|
||||
(
|
||||
derived.intent.kind === "help-how" ||
|
||||
derived.intent.kind === "help-where" ||
|
||||
derived.intent.kind === "nickname-question" ||
|
||||
derived.intent.kind === "photo-question" ||
|
||||
derived.intent.kind === "discord-question"
|
||||
)
|
||||
? [...current.openQuestions, userMessage].slice(-3)
|
||||
: current.openQuestions;
|
||||
|
||||
const recentNicknameAttempts =
|
||||
request.trigger === "nickname-invalid" ||
|
||||
derived.intent.kind === "nickname-question"
|
||||
? Array.from(
|
||||
new Set([
|
||||
...current.recentNicknameAttempts,
|
||||
request.nickname,
|
||||
]),
|
||||
).slice(-5)
|
||||
: current.recentNicknameAttempts;
|
||||
|
||||
return {
|
||||
...current,
|
||||
flaggedIssues:
|
||||
derived.phase.kind === "nickname-rejected"
|
||||
? derived.phase.issues.map((issue) => issue.message)
|
||||
: derived.phase.kind === "completed"
|
||||
? []
|
||||
: current.flaggedIssues,
|
||||
lastIntent: derived.intent.kind,
|
||||
lastIntentAt: now,
|
||||
openQuestions:
|
||||
derived.phase.kind === "completed" ? [] : openQuestions,
|
||||
recentNicknameAttempts,
|
||||
toneMemory: {
|
||||
...current.toneMemory,
|
||||
greetedAt:
|
||||
derived.intent.kind === "greeting"
|
||||
? now
|
||||
: current.toneMemory.greetedAt,
|
||||
thankedAt:
|
||||
derived.intent.kind === "acknowledgement"
|
||||
? now
|
||||
: current.toneMemory.thankedAt,
|
||||
},
|
||||
};
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private makeTemplateFallbackResponse(
|
||||
request: AgentRequest,
|
||||
session: OnboardingSession | null,
|
||||
intent: Intent,
|
||||
derived: DerivedState,
|
||||
): AgentResponse {
|
||||
switch (request.trigger) {
|
||||
case "new-member":
|
||||
return this.makeEmptyResponse(request.requestId);
|
||||
case "nickname-invalid":
|
||||
return this.makeTemplateResponse(
|
||||
request.requestId,
|
||||
getNicknameInvalidMessage(request.nickname, session),
|
||||
);
|
||||
case "photo-failed":
|
||||
return this.makeTemplateResponse(
|
||||
request.requestId,
|
||||
getPhotoFailedMessage(request.nickname),
|
||||
);
|
||||
case "progress-update":
|
||||
return this.makeTemplateResponse(
|
||||
request.requestId,
|
||||
session
|
||||
? getProgressMessage(request.nickname, session)
|
||||
: getGenericHelpMessage(request.nickname, session),
|
||||
);
|
||||
case "timeout-warning":
|
||||
return this.makeTemplateResponse(
|
||||
request.requestId,
|
||||
getTimeoutWarningMessage(request.nickname),
|
||||
);
|
||||
case "completed":
|
||||
return this.makeTemplateResponse(
|
||||
request.requestId,
|
||||
getCompletedMessage(request.nickname),
|
||||
);
|
||||
default:
|
||||
return this.makeTemplateResponse(
|
||||
request.requestId,
|
||||
this.getIntentFallbackText(request.nickname, session, intent, derived),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private getIntentFallbackText(
|
||||
nickname: string,
|
||||
session: OnboardingSession | null,
|
||||
intent: Intent,
|
||||
derived: DerivedState,
|
||||
): string {
|
||||
switch (intent.kind) {
|
||||
case "greeting":
|
||||
return getGreetingMessage();
|
||||
case "acknowledgement":
|
||||
return getAcknowledgementMessage();
|
||||
case "status-question":
|
||||
return getRemainingStatusMessage(session);
|
||||
case "nickname-question":
|
||||
return derived.phase.kind === "nickname-rejected"
|
||||
? getNicknameInvalidMessage(nickname, session)
|
||||
: getNicknameFormatMessage();
|
||||
case "photo-question":
|
||||
return derived.phase.kind === "photo-rejected"
|
||||
? getPhotoFailedMessage(nickname)
|
||||
: getHeartGuideMessage();
|
||||
case "help-where":
|
||||
if (
|
||||
derived.phase.kind === "photo-pending" ||
|
||||
derived.phase.kind === "photo-rejected" ||
|
||||
(derived.phase.kind === "deadline-near" &&
|
||||
derived.phase.remaining.includes("photo"))
|
||||
) {
|
||||
return getHeartLocationMessage();
|
||||
}
|
||||
return getGenericHelpMessage(nickname, session);
|
||||
case "help-how":
|
||||
if (
|
||||
derived.phase.kind === "nickname-pending" ||
|
||||
derived.phase.kind === "nickname-rejected" ||
|
||||
(derived.phase.kind === "deadline-near" &&
|
||||
derived.phase.remaining.includes("nickname"))
|
||||
) {
|
||||
return getNicknameFormatMessage();
|
||||
}
|
||||
if (
|
||||
derived.phase.kind === "photo-pending" ||
|
||||
derived.phase.kind === "photo-rejected" ||
|
||||
(derived.phase.kind === "deadline-near" &&
|
||||
derived.phase.remaining.includes("photo"))
|
||||
) {
|
||||
return getHeartGuideMessage();
|
||||
}
|
||||
return getGenericHelpMessage(nickname, session);
|
||||
case "discord-question":
|
||||
return getDiscordMessage(nickname, session);
|
||||
case "age-question":
|
||||
return getAgeRequirementMessage();
|
||||
case "chitchat":
|
||||
case "off-topic-to-other":
|
||||
return "";
|
||||
case "unknown":
|
||||
return getGenericHelpMessage(nickname, session);
|
||||
}
|
||||
}
|
||||
|
||||
private async generateLlmText(
|
||||
messages: ChatMessage[],
|
||||
options?: {
|
||||
allowShortResponse?: boolean;
|
||||
maxTokens?: number;
|
||||
retryOnLowQuality?: boolean;
|
||||
temperature?: number;
|
||||
},
|
||||
): Promise<LlmTextResult> {
|
||||
if (!this.llmClient.hasEndpoints()) {
|
||||
return { status: "failed" };
|
||||
}
|
||||
|
||||
const attempts: Array<{
|
||||
maxTokens?: number;
|
||||
temperature?: number;
|
||||
}> = [];
|
||||
|
||||
const firstAttempt: {
|
||||
maxTokens?: number;
|
||||
temperature?: number;
|
||||
} = {};
|
||||
if (options?.maxTokens !== undefined) {
|
||||
firstAttempt.maxTokens = options.maxTokens;
|
||||
}
|
||||
if (options?.temperature !== undefined) {
|
||||
firstAttempt.temperature = options.temperature;
|
||||
}
|
||||
attempts.push(firstAttempt);
|
||||
|
||||
if (options?.retryOnLowQuality) {
|
||||
const retryAttempt: {
|
||||
maxTokens?: number;
|
||||
temperature?: number;
|
||||
} = {
|
||||
temperature: getLlmTemperature() + 0.1,
|
||||
};
|
||||
if (options.maxTokens !== undefined) {
|
||||
retryAttempt.maxTokens = options.maxTokens;
|
||||
}
|
||||
attempts.push(retryAttempt);
|
||||
}
|
||||
|
||||
for (const attempt of attempts) {
|
||||
const requestOptions: {
|
||||
maxTokens?: number;
|
||||
temperature?: number;
|
||||
} = {};
|
||||
if (attempt.maxTokens !== undefined) {
|
||||
requestOptions.maxTokens = attempt.maxTokens;
|
||||
}
|
||||
if (attempt.temperature !== undefined) {
|
||||
requestOptions.temperature = attempt.temperature;
|
||||
}
|
||||
|
||||
const llmResult = await this.llmClient.chatCompletion(
|
||||
messages,
|
||||
requestOptions,
|
||||
);
|
||||
|
||||
if (!llmResult) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const processed = postProcessResponse(llmResult.content);
|
||||
if (!processed) {
|
||||
continue;
|
||||
}
|
||||
if (processed.includes("[NO_RESPONSE]")) {
|
||||
return { status: "no_response" };
|
||||
}
|
||||
const qualityOptions =
|
||||
options?.allowShortResponse !== undefined
|
||||
? { allowShortResponse: options.allowShortResponse }
|
||||
: undefined;
|
||||
if (isLowQualityResponse(processed, qualityOptions)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
text: processed,
|
||||
};
|
||||
}
|
||||
|
||||
return { status: "failed" };
|
||||
}
|
||||
|
||||
private makeTemplateResponse(
|
||||
requestId: string,
|
||||
text: string,
|
||||
): AgentResponse {
|
||||
return {
|
||||
requestId,
|
||||
text,
|
||||
confidence: 0.8,
|
||||
source: "template",
|
||||
};
|
||||
}
|
||||
|
||||
private makeLlmResponse(
|
||||
requestId: string,
|
||||
text: string,
|
||||
): AgentResponse {
|
||||
return {
|
||||
requestId,
|
||||
text,
|
||||
confidence: 0.9,
|
||||
source: "llm",
|
||||
};
|
||||
}
|
||||
|
||||
private makeEmptyResponse(requestId: string): AgentResponse {
|
||||
return {
|
||||
requestId,
|
||||
text: "",
|
||||
confidence: 0,
|
||||
source: "template",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { GarnetClient } from "@common/garnet";
|
||||
import { getBlackboardTtlSeconds } from "../../../config.js";
|
||||
import type { Blackboard } from "../types.js";
|
||||
|
||||
function getBlackboardKey(chatroomId: string, userId: string): string {
|
||||
return `lolympus-agent:bb:${chatroomId}:${userId}`;
|
||||
}
|
||||
|
||||
function createDefaultBlackboard(): Blackboard {
|
||||
return {
|
||||
flaggedIssues: [],
|
||||
openQuestions: [],
|
||||
recentNicknameAttempts: [],
|
||||
toneMemory: {},
|
||||
};
|
||||
}
|
||||
|
||||
function mergeBlackboard(
|
||||
current: Blackboard | null,
|
||||
next: Partial<Blackboard>,
|
||||
): Blackboard {
|
||||
const base = current ?? createDefaultBlackboard();
|
||||
|
||||
return {
|
||||
flaggedIssues: next.flaggedIssues ?? base.flaggedIssues,
|
||||
lastIntent: next.lastIntent ?? base.lastIntent,
|
||||
lastIntentAt: next.lastIntentAt ?? base.lastIntentAt,
|
||||
openQuestions: next.openQuestions ?? base.openQuestions,
|
||||
recentNicknameAttempts:
|
||||
next.recentNicknameAttempts ?? base.recentNicknameAttempts,
|
||||
toneMemory: {
|
||||
...base.toneMemory,
|
||||
...next.toneMemory,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export class OnboardingBlackboardStore {
|
||||
constructor(private readonly garnetClient: GarnetClient) {}
|
||||
|
||||
async load(chatroomId: string, userId: string): Promise<Blackboard> {
|
||||
const key = getBlackboardKey(chatroomId, userId);
|
||||
const saved = await this.garnetClient.getJson<Blackboard>(key);
|
||||
return mergeBlackboard(saved, {});
|
||||
}
|
||||
|
||||
async save(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
value: Blackboard,
|
||||
): Promise<void> {
|
||||
const key = getBlackboardKey(chatroomId, userId);
|
||||
await this.garnetClient.setJson(
|
||||
key,
|
||||
value,
|
||||
getBlackboardTtlSeconds(),
|
||||
);
|
||||
}
|
||||
|
||||
async update(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
updater: (current: Blackboard) => Blackboard,
|
||||
): Promise<Blackboard> {
|
||||
const current = await this.load(chatroomId, userId);
|
||||
const next = updater(current);
|
||||
await this.save(chatroomId, userId, next);
|
||||
return next;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { OnboardingIntentClassifier } from "./classifier.js";
|
||||
|
||||
const llmClientStub = {
|
||||
chatCompletion: () => Promise.resolve(null),
|
||||
hasEndpoints: () => false,
|
||||
};
|
||||
|
||||
describe("OnboardingIntentClassifier", () => {
|
||||
const classifier = new OnboardingIntentClassifier(
|
||||
llmClientStub as never,
|
||||
);
|
||||
|
||||
it("classifies greeting via regex", async () => {
|
||||
const result = await classifier.classify("안녕하세요", "user-message");
|
||||
expect(result.intent.kind).toBe("greeting");
|
||||
expect(result.source).toBe("regex");
|
||||
});
|
||||
|
||||
it("classifies status question via regex", async () => {
|
||||
const result = await classifier.classify("이제 뭐 남았지", "user-message");
|
||||
expect(result.intent.kind).toBe("status-question");
|
||||
});
|
||||
|
||||
it("classifies mention as off-topic-to-other", async () => {
|
||||
const result = await classifier.classify("@프리리 이거 뭐야", "user-message");
|
||||
expect(result.intent.kind).toBe("off-topic-to-other");
|
||||
});
|
||||
|
||||
it("maps trigger based intents", async () => {
|
||||
const result = await classifier.classify(undefined, "nickname-invalid");
|
||||
expect(result.intent.kind).toBe("nickname-question");
|
||||
expect(result.source).toBe("regex");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,233 @@
|
||||
import { getClassifierMaxTokens } from "../../../config.js";
|
||||
import { withClassifierSpan } from "@obs/spans";
|
||||
import type { LlmClient } from "@llm/llm-client";
|
||||
import {
|
||||
detectLightweightIntent,
|
||||
isRemainingStatusQuestion,
|
||||
shouldSkipMessage,
|
||||
} from "../message-filter.js";
|
||||
import type { ClassifiedIntent, Intent } from "./onboarding-state.js";
|
||||
|
||||
interface ClassifierLlmShape {
|
||||
intent?: string;
|
||||
}
|
||||
|
||||
function intent(kind: Intent["kind"]): Intent {
|
||||
return { kind };
|
||||
}
|
||||
|
||||
function mapTriggerIntent(trigger: string): Intent | null {
|
||||
switch (trigger) {
|
||||
case "nickname-invalid":
|
||||
return intent("nickname-question");
|
||||
case "photo-failed":
|
||||
return intent("photo-question");
|
||||
case "progress-update":
|
||||
case "timeout-warning":
|
||||
return intent("status-question");
|
||||
case "completed":
|
||||
return intent("acknowledgement");
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function classifyByRegex(userMessage: string): ClassifiedIntent {
|
||||
const trimmed = userMessage.trim();
|
||||
const lower = trimmed.toLowerCase();
|
||||
|
||||
if (!trimmed) {
|
||||
return { intent: intent("unknown"), confidence: 0, source: "regex" };
|
||||
}
|
||||
|
||||
if (/^@\S+/u.test(trimmed)) {
|
||||
return { intent: intent("off-topic-to-other"), confidence: 0.99, source: "regex" };
|
||||
}
|
||||
|
||||
if (shouldSkipMessage(trimmed)) {
|
||||
return { intent: intent("chitchat"), confidence: 0.95, source: "regex" };
|
||||
}
|
||||
|
||||
const lightweightIntent = detectLightweightIntent(trimmed);
|
||||
if (lightweightIntent === "greeting") {
|
||||
return { intent: intent("greeting"), confidence: 0.99, source: "regex" };
|
||||
}
|
||||
if (lightweightIntent === "acknowledgement") {
|
||||
return {
|
||||
intent: intent("acknowledgement"),
|
||||
confidence: 0.99,
|
||||
source: "regex",
|
||||
};
|
||||
}
|
||||
|
||||
if (isRemainingStatusQuestion(trimmed)) {
|
||||
return {
|
||||
intent: intent("status-question"),
|
||||
confidence: 0.96,
|
||||
source: "regex",
|
||||
};
|
||||
}
|
||||
|
||||
if (/(어디|위치|어딨어|찾겠|찾을)/u.test(trimmed)) {
|
||||
return { intent: intent("help-where"), confidence: 0.9, source: "regex" };
|
||||
}
|
||||
|
||||
if (/(어떻게|방법|어케|어케해|어케 함|어케함)/u.test(trimmed)) {
|
||||
return { intent: intent("help-how"), confidence: 0.9, source: "regex" };
|
||||
}
|
||||
|
||||
if (/(디스코드|디코|discord)/u.test(lower)) {
|
||||
return {
|
||||
intent: intent("discord-question"),
|
||||
confidence: 0.95,
|
||||
source: "regex",
|
||||
};
|
||||
}
|
||||
|
||||
if (/(나이|연령|27세|나이\s*제한|몇\s*살|몇살)/u.test(trimmed)) {
|
||||
return { intent: intent("age-question"), confidence: 0.93, source: "regex" };
|
||||
}
|
||||
|
||||
if (/(닉네임|닉변|형식|이름)/u.test(trimmed)) {
|
||||
return {
|
||||
intent: intent("nickname-question"),
|
||||
confidence: 0.9,
|
||||
source: "regex",
|
||||
};
|
||||
}
|
||||
|
||||
if (/(하트|인증|사진|스크린샷|스샷|프로필)/u.test(trimmed)) {
|
||||
return {
|
||||
intent: intent("photo-question"),
|
||||
confidence: 0.88,
|
||||
source: "regex",
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
/(가입|등록|절차|도움|도와|뭐해야|뭐 해야|해야 돼|해야해|무엇을 해야)/u.test(trimmed)
|
||||
) {
|
||||
return { intent: intent("help-how"), confidence: 0.72, source: "regex" };
|
||||
}
|
||||
|
||||
return { intent: intent("unknown"), confidence: 0, source: "regex" };
|
||||
}
|
||||
|
||||
function parseIntent(raw: unknown): Intent {
|
||||
if (
|
||||
raw === "greeting" ||
|
||||
raw === "acknowledgement" ||
|
||||
raw === "status-question" ||
|
||||
raw === "help-how" ||
|
||||
raw === "help-where" ||
|
||||
raw === "nickname-question" ||
|
||||
raw === "photo-question" ||
|
||||
raw === "discord-question" ||
|
||||
raw === "age-question" ||
|
||||
raw === "chitchat" ||
|
||||
raw === "off-topic-to-other"
|
||||
) {
|
||||
return { kind: raw };
|
||||
}
|
||||
|
||||
return { kind: "unknown" };
|
||||
}
|
||||
|
||||
async function classifyByLlm(
|
||||
llmClient: LlmClient,
|
||||
trigger: string,
|
||||
userMessage: string,
|
||||
): Promise<ClassifiedIntent> {
|
||||
if (!llmClient.hasEndpoints()) {
|
||||
return { intent: intent("unknown"), confidence: 0, source: "llm" };
|
||||
}
|
||||
|
||||
const response = await llmClient.chatCompletion(
|
||||
[
|
||||
{
|
||||
role: "system",
|
||||
content:
|
||||
'다음 유저 메시지의 intent를 JSON으로만 분류해. {"intent":"..."} 형식만 출력하고, intent는 greeting, acknowledgement, status-question, help-how, help-where, nickname-question, photo-question, discord-question, age-question, chitchat, off-topic-to-other, unknown 중 하나만 사용해.',
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: `trigger=${trigger}\nmessage=${userMessage}`,
|
||||
},
|
||||
],
|
||||
{
|
||||
jsonMode: true,
|
||||
maxTokens: getClassifierMaxTokens(),
|
||||
temperature: 0,
|
||||
},
|
||||
);
|
||||
|
||||
if (!response) {
|
||||
return { intent: intent("unknown"), confidence: 0, source: "llm" };
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(response.content) as ClassifierLlmShape;
|
||||
return {
|
||||
intent: parseIntent(parsed.intent),
|
||||
confidence: parsed.intent ? 0.75 : 0,
|
||||
source: "llm",
|
||||
};
|
||||
} catch {
|
||||
return { intent: intent("unknown"), confidence: 0, source: "llm" };
|
||||
}
|
||||
}
|
||||
|
||||
export class OnboardingIntentClassifier {
|
||||
constructor(private readonly llmClient: LlmClient) {}
|
||||
|
||||
async classify(
|
||||
userMessage: string | undefined,
|
||||
trigger: string,
|
||||
): Promise<ClassifiedIntent> {
|
||||
return withClassifierSpan(
|
||||
{
|
||||
"agent.trigger": trigger,
|
||||
},
|
||||
async (span) => {
|
||||
const triggerIntent = mapTriggerIntent(trigger);
|
||||
if (triggerIntent) {
|
||||
span.setAttributes({
|
||||
"agent.classifier.confidence": 1,
|
||||
"agent.classifier.source": "regex",
|
||||
"agent.intent": triggerIntent.kind,
|
||||
});
|
||||
return {
|
||||
intent: triggerIntent,
|
||||
confidence: 1,
|
||||
source: "regex",
|
||||
};
|
||||
}
|
||||
|
||||
const regexResult = classifyByRegex(userMessage ?? "");
|
||||
if (regexResult.confidence >= 0.75) {
|
||||
span.setAttributes({
|
||||
"agent.classifier.confidence": regexResult.confidence,
|
||||
"agent.classifier.source": regexResult.source,
|
||||
"agent.intent": regexResult.intent.kind,
|
||||
});
|
||||
return regexResult;
|
||||
}
|
||||
|
||||
const llmResult = await classifyByLlm(
|
||||
this.llmClient,
|
||||
trigger,
|
||||
userMessage ?? "",
|
||||
);
|
||||
|
||||
const result =
|
||||
llmResult.intent.kind === "unknown" ? regexResult : llmResult;
|
||||
span.setAttributes({
|
||||
"agent.classifier.confidence": result.confidence,
|
||||
"agent.classifier.source": result.source,
|
||||
"agent.intent": result.intent.kind,
|
||||
});
|
||||
return result;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { deriveState } from "./derive.js";
|
||||
import type { Intent } from "./onboarding-state.js";
|
||||
import type { OnboardingSession } from "../types.js";
|
||||
|
||||
function makeSession(
|
||||
overrides?: Partial<OnboardingSession>,
|
||||
): OnboardingSession {
|
||||
return {
|
||||
chatroomId: "room-1",
|
||||
deadlineAt: new Date(Date.now() + 2 * 60 * 60 * 1000).toISOString(),
|
||||
joinedAt: new Date().toISOString(),
|
||||
lastEventAt: new Date().toISOString(),
|
||||
nickname: "프리리/95/여/D",
|
||||
nicknameVerified: false,
|
||||
photoVerified: false,
|
||||
status: "pending",
|
||||
userId: "user-1",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function intent(kind: Intent["kind"]): Intent {
|
||||
return { kind };
|
||||
}
|
||||
|
||||
describe("deriveState", () => {
|
||||
it("skips new-member trigger", () => {
|
||||
const derived = deriveState(null, "new-member", intent("unknown"));
|
||||
expect(derived.shouldRespond).toBe(false);
|
||||
expect(derived.responseMode).toBe("skip");
|
||||
expect(derived.tier).toBe(0);
|
||||
});
|
||||
|
||||
it("skips chitchat intent", () => {
|
||||
const derived = deriveState(makeSession(), "user-message", intent("chitchat"));
|
||||
expect(derived.shouldRespond).toBe(false);
|
||||
expect(derived.responseMode).toBe("skip");
|
||||
});
|
||||
|
||||
it("promotes nickname-invalid to llm-with-tools tier1", () => {
|
||||
const derived = deriveState(
|
||||
makeSession(),
|
||||
"nickname-invalid",
|
||||
intent("nickname-question"),
|
||||
);
|
||||
expect(derived.responseMode).toBe("llm-with-tools");
|
||||
expect(derived.tier).toBe(1);
|
||||
});
|
||||
|
||||
it("allows short response for status questions", () => {
|
||||
const derived = deriveState(
|
||||
makeSession(),
|
||||
"user-message",
|
||||
intent("status-question"),
|
||||
);
|
||||
expect(derived.allowShortResponse).toBe(true);
|
||||
expect(derived.suggestedMaxTokens).toBe(96);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
import type { OnboardingSession } from "../types.js";
|
||||
import { derivePhaseFromSession } from "./transitions.js";
|
||||
import type { DerivedState, Intent } from "./onboarding-state.js";
|
||||
|
||||
function getSuggestedMaxTokens(
|
||||
trigger: string,
|
||||
intent: Intent,
|
||||
): number {
|
||||
if (intent.kind === "greeting" || intent.kind === "acknowledgement") {
|
||||
return 96;
|
||||
}
|
||||
|
||||
if (intent.kind === "status-question") {
|
||||
return 96;
|
||||
}
|
||||
|
||||
if (
|
||||
trigger === "progress-update" ||
|
||||
trigger === "timeout-warning" ||
|
||||
trigger === "completed"
|
||||
) {
|
||||
return 256;
|
||||
}
|
||||
|
||||
if (
|
||||
intent.kind === "help-how" ||
|
||||
intent.kind === "help-where" ||
|
||||
intent.kind === "nickname-question" ||
|
||||
intent.kind === "photo-question" ||
|
||||
trigger === "nickname-invalid" ||
|
||||
trigger === "photo-failed"
|
||||
) {
|
||||
return 384;
|
||||
}
|
||||
|
||||
return 256;
|
||||
}
|
||||
|
||||
function shouldAllowShortResponse(trigger: string, intent: Intent): boolean {
|
||||
return (
|
||||
trigger === "progress-update" ||
|
||||
trigger === "timeout-warning" ||
|
||||
trigger === "completed" ||
|
||||
intent.kind === "greeting" ||
|
||||
intent.kind === "acknowledgement" ||
|
||||
intent.kind === "status-question"
|
||||
);
|
||||
}
|
||||
|
||||
export function deriveState(
|
||||
session: OnboardingSession | null,
|
||||
trigger: string,
|
||||
intent: Intent,
|
||||
): DerivedState {
|
||||
const phase = derivePhaseFromSession(session, trigger);
|
||||
|
||||
if (
|
||||
trigger === "new-member" ||
|
||||
intent.kind === "chitchat" ||
|
||||
intent.kind === "off-topic-to-other"
|
||||
) {
|
||||
return {
|
||||
phase,
|
||||
intent,
|
||||
shouldRespond: false,
|
||||
responseMode: "skip",
|
||||
suggestedMaxTokens: 0,
|
||||
allowShortResponse: true,
|
||||
tier: 0,
|
||||
};
|
||||
}
|
||||
|
||||
let responseMode: DerivedState["responseMode"] = "llm";
|
||||
let tier: DerivedState["tier"] = 0;
|
||||
|
||||
if (phase.kind === "nickname-rejected") {
|
||||
responseMode = "llm-with-tools";
|
||||
tier = 1;
|
||||
} else if (phase.kind === "deadline-near") {
|
||||
responseMode = "llm";
|
||||
tier = 1;
|
||||
}
|
||||
|
||||
return {
|
||||
phase,
|
||||
intent,
|
||||
shouldRespond: true,
|
||||
responseMode,
|
||||
suggestedMaxTokens: getSuggestedMaxTokens(trigger, intent),
|
||||
allowShortResponse: shouldAllowShortResponse(trigger, intent),
|
||||
tier,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { Intent } from "./onboarding-state.js";
|
||||
|
||||
export const EXPECTED_KEYWORDS: Record<Intent["kind"], string[]> = {
|
||||
acknowledgement: ["감사", "알겠", "확인"],
|
||||
"age-question": ["27", "나이", "가입"],
|
||||
chitchat: [],
|
||||
"discord-question": ["디스코드", "디코", "discord"],
|
||||
greeting: ["안녕", "반갑", "환영"],
|
||||
"help-how": ["어떻게", "방법", "해"],
|
||||
"help-where": ["어디", "위치", "찾"],
|
||||
"nickname-question": ["닉네임", "닉변", "형식", "이름"],
|
||||
"off-topic-to-other": [],
|
||||
"photo-question": ["하트", "인증", "스크린샷", "프로필"],
|
||||
"status-question": ["남았", "완료", "진행", "상태"],
|
||||
unknown: [],
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { NicknameIssue } from "../types.js";
|
||||
|
||||
export type RemainingStep = "nickname" | "photo";
|
||||
|
||||
export type OnboardingPhase =
|
||||
| { kind: "fresh" }
|
||||
| { kind: "nickname-pending"; current: string }
|
||||
| { kind: "photo-pending" }
|
||||
| { kind: "both-pending" }
|
||||
| { kind: "nickname-rejected"; current: string; issues: NicknameIssue[] }
|
||||
| { kind: "photo-rejected"; reason?: string | undefined }
|
||||
| { kind: "deadline-near"; remaining: RemainingStep[] }
|
||||
| { kind: "completed" }
|
||||
| { kind: "timed-out" };
|
||||
|
||||
export type Intent =
|
||||
| { kind: "greeting" }
|
||||
| { kind: "acknowledgement" }
|
||||
| { kind: "status-question" }
|
||||
| { kind: "help-how" }
|
||||
| { kind: "help-where" }
|
||||
| { kind: "nickname-question" }
|
||||
| { kind: "photo-question" }
|
||||
| { kind: "discord-question" }
|
||||
| { kind: "age-question" }
|
||||
| { kind: "chitchat" }
|
||||
| { kind: "off-topic-to-other" }
|
||||
| { kind: "unknown" };
|
||||
|
||||
export interface DerivedState {
|
||||
phase: OnboardingPhase;
|
||||
intent: Intent;
|
||||
shouldRespond: boolean;
|
||||
responseMode: "skip" | "template" | "llm" | "llm-with-tools";
|
||||
suggestedMaxTokens: number;
|
||||
allowShortResponse: boolean;
|
||||
tier: 0 | 1 | 2;
|
||||
}
|
||||
|
||||
export interface ClassifiedIntent {
|
||||
intent: Intent;
|
||||
confidence: number;
|
||||
source: "regex" | "llm";
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { getDeadlineNearThresholdMin } from "../../../config.js";
|
||||
import { analyzeNicknameIssues } from "../onboarding-templates.js";
|
||||
import type { OnboardingSession } from "../types.js";
|
||||
import type { OnboardingPhase, RemainingStep } from "./onboarding-state.js";
|
||||
|
||||
function getRemainingSteps(
|
||||
session: OnboardingSession | null,
|
||||
): RemainingStep[] {
|
||||
if (!session) {
|
||||
return ["nickname", "photo"];
|
||||
}
|
||||
|
||||
const remaining: RemainingStep[] = [];
|
||||
if (!session.nicknameVerified) {
|
||||
remaining.push("nickname");
|
||||
}
|
||||
if (!session.photoVerified) {
|
||||
remaining.push("photo");
|
||||
}
|
||||
return remaining;
|
||||
}
|
||||
|
||||
function isDeadlineNear(session: OnboardingSession | null): boolean {
|
||||
if (!session?.deadlineAt) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const deadlineAt = Date.parse(session.deadlineAt);
|
||||
if (!Number.isFinite(deadlineAt)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return deadlineAt - Date.now() <= getDeadlineNearThresholdMin() * 60_000;
|
||||
}
|
||||
|
||||
export function derivePhaseFromSession(
|
||||
session: OnboardingSession | null,
|
||||
trigger: string,
|
||||
): OnboardingPhase {
|
||||
if (!session) {
|
||||
return { kind: "fresh" };
|
||||
}
|
||||
|
||||
if (session.status === "timed_out") {
|
||||
return { kind: "timed-out" };
|
||||
}
|
||||
|
||||
if (session.status === "completed" || (session.nicknameVerified && session.photoVerified)) {
|
||||
return { kind: "completed" };
|
||||
}
|
||||
|
||||
if (trigger === "nickname-invalid") {
|
||||
return {
|
||||
kind: "nickname-rejected",
|
||||
current: session.nickname,
|
||||
issues: analyzeNicknameIssues(session.nickname),
|
||||
};
|
||||
}
|
||||
|
||||
if (trigger === "photo-failed") {
|
||||
return { kind: "photo-rejected" };
|
||||
}
|
||||
|
||||
const remaining = getRemainingSteps(session);
|
||||
if (remaining.length > 0 && isDeadlineNear(session)) {
|
||||
return { kind: "deadline-near", remaining };
|
||||
}
|
||||
|
||||
if (!session.nicknameVerified && !session.photoVerified) {
|
||||
return { kind: "both-pending" };
|
||||
}
|
||||
|
||||
if (!session.nicknameVerified) {
|
||||
return {
|
||||
kind: "nickname-pending",
|
||||
current: session.nickname,
|
||||
};
|
||||
}
|
||||
|
||||
if (!session.photoVerified) {
|
||||
return { kind: "photo-pending" };
|
||||
}
|
||||
|
||||
return { kind: "fresh" };
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { ConversationMessage } from "@agent/types";
|
||||
import type { RoomMessage } from "@room/types";
|
||||
|
||||
export type OnboardingStatus = "pending" | "completed" | "timed_out";
|
||||
|
||||
export type NicknameIssueKind =
|
||||
| "missing-tier"
|
||||
| "invalid-age"
|
||||
| "missing-gender"
|
||||
| "wrong-separator"
|
||||
| "unknown-tier"
|
||||
| "other";
|
||||
|
||||
export interface NicknameIssue {
|
||||
kind: NicknameIssueKind;
|
||||
message: string;
|
||||
suggestion?: string | undefined;
|
||||
}
|
||||
|
||||
export interface Example {
|
||||
assistant: string;
|
||||
user: string;
|
||||
}
|
||||
|
||||
export interface PromptContext {
|
||||
blackboard: Blackboard;
|
||||
conversationHistory: ConversationMessage[];
|
||||
roomContext?: RoomMessage[] | undefined;
|
||||
summary?: string | undefined;
|
||||
userMessage: string;
|
||||
}
|
||||
|
||||
export interface Blackboard {
|
||||
flaggedIssues: string[];
|
||||
lastIntent?: string | undefined;
|
||||
lastIntentAt?: string | undefined;
|
||||
openQuestions: string[];
|
||||
recentNicknameAttempts: string[];
|
||||
toneMemory: {
|
||||
greetedAt?: string | undefined;
|
||||
thankedAt?: string | undefined;
|
||||
};
|
||||
}
|
||||
|
||||
export interface OnboardingSession {
|
||||
chatroomId: string;
|
||||
completedAt?: string;
|
||||
deadlineAt: string;
|
||||
discordMemberId?: string;
|
||||
joinedAt: string;
|
||||
lastEventAt: string;
|
||||
nickname: string;
|
||||
nicknameVerified: boolean;
|
||||
nicknameVerifiedAt?: string;
|
||||
photoVerified: boolean;
|
||||
photoVerifiedAt?: string;
|
||||
status: OnboardingStatus;
|
||||
timedOutAt?: string;
|
||||
userId: string;
|
||||
}
|
||||
Reference in new issue
Block a user