.
This commit is contained in:
1 parent
7fa293c9e8
commit
ae44aa03db
67 files changed
+6164
-243
No files matched your search
@@ -0,0 +1,180 @@
|
||||
import { trace } from "@opentelemetry/api";
|
||||
import { getMaxResponseLength } from "../../../config.js";
|
||||
import { VALID_TIER_SET } from "../constants.js";
|
||||
import { EXPECTED_KEYWORDS } from "../state/intent-keywords.js";
|
||||
import type { DerivedState } from "../state/onboarding-state.js";
|
||||
|
||||
const ALLOWED_URL_PATTERNS = [
|
||||
/discord\.gg\/lolympus/iu,
|
||||
/open\.kakao\.com\/o\/gOscBAEh/iu,
|
||||
];
|
||||
|
||||
export interface ValidationResult {
|
||||
status: "pass" | "soft-warn" | "reject";
|
||||
text: string;
|
||||
reasons: string[];
|
||||
}
|
||||
|
||||
function postProcessResponse(text: string): string {
|
||||
let processed = text.trim();
|
||||
|
||||
processed = processed.replace(/<think>[\s\S]*?<\/think>/gu, "").trim();
|
||||
|
||||
processed = processed.replace(
|
||||
/https?:\/\/[^\s)}\]]+/gu,
|
||||
(match) => {
|
||||
if (ALLOWED_URL_PATTERNS.some((pattern) => pattern.test(match))) {
|
||||
return match;
|
||||
}
|
||||
return "";
|
||||
},
|
||||
);
|
||||
|
||||
processed = processed.replace(/ +/gu, " ").trim();
|
||||
processed = processed.replace(/\*\*(.+?)\*\*/gu, "$1");
|
||||
processed = processed.replace(/\*(.+?)\*/gu, "$1");
|
||||
processed = processed.replace(/__(.+?)__/gu, "$1");
|
||||
processed = processed.replace(/~~(.+?)~~/gu, "$1");
|
||||
processed = processed.replace(/^#{1,6}\s+/gmu, "");
|
||||
processed = processed.replace(/^[-*]\s+/gmu, "");
|
||||
processed = processed.replace(/`(.+?)`/gu, "$1");
|
||||
|
||||
const maxLen = getMaxResponseLength();
|
||||
if (processed.length > maxLen) {
|
||||
processed = processed.slice(0, maxLen).trimEnd();
|
||||
const sentenceEndPattern =
|
||||
/(?:세요[.!]?|해요[.!]?|돼요[.!]?|어요[.!]?|아요[.!]?|거든요[.!]?|습니다[.!]?|됩니다[.!]?|니다[.!]?|요[.!]|다[.!]|[.!?])\s*$/u;
|
||||
|
||||
for (let index = processed.length; index > maxLen * 0.5; index--) {
|
||||
const sub = processed.slice(0, index);
|
||||
if (sentenceEndPattern.test(sub)) {
|
||||
processed = sub;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return processed.trim();
|
||||
}
|
||||
|
||||
function findInvalidNicknameExamples(text: string): string[] {
|
||||
const matches = [
|
||||
...text.matchAll(/([^\s/]{1,20})\/(\d{2})\/(남|여)\/([A-Z]{1,2})/gu),
|
||||
];
|
||||
|
||||
return matches.flatMap((match) => {
|
||||
const tier = match[4];
|
||||
if (!tier || VALID_TIER_SET.has(tier)) {
|
||||
return [];
|
||||
}
|
||||
return [tier];
|
||||
});
|
||||
}
|
||||
|
||||
function hasIntentKeywordMatch(text: string, derived: DerivedState): boolean {
|
||||
const keywords = EXPECTED_KEYWORDS[derived.intent.kind];
|
||||
if (keywords.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const lower = text.toLowerCase();
|
||||
return keywords.some((keyword) => lower.includes(keyword.toLowerCase()));
|
||||
}
|
||||
|
||||
function hasRepeatedPhrase(text: string): boolean {
|
||||
const words = text.split(/\s+/u);
|
||||
if (words.length < 6) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const phrases = new Map<string, number>();
|
||||
for (let index = 0; index < words.length - 1; index++) {
|
||||
const bigram = `${words[index]} ${words[index + 1]}`;
|
||||
phrases.set(bigram, (phrases.get(bigram) ?? 0) + 1);
|
||||
}
|
||||
|
||||
return [...phrases.values()].some((count) => count >= 3);
|
||||
}
|
||||
|
||||
function markValidation(result: ValidationResult): ValidationResult {
|
||||
const activeSpan = trace.getActiveSpan();
|
||||
activeSpan?.setAttributes({
|
||||
"agent.validator.status": result.status,
|
||||
"agent.validator.warnings": result.reasons,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export function validateOnboardingResponse(
|
||||
draft: string,
|
||||
derived: DerivedState,
|
||||
options?: { allowShortResponse?: boolean | undefined },
|
||||
): ValidationResult {
|
||||
const text = postProcessResponse(draft);
|
||||
const rejectReasons: string[] = [];
|
||||
const softWarnReasons: string[] = [];
|
||||
|
||||
if (!text) {
|
||||
rejectReasons.push("empty");
|
||||
}
|
||||
|
||||
if (text.includes("[NO_RESPONSE]")) {
|
||||
return markValidation({
|
||||
status: "pass",
|
||||
text: "[NO_RESPONSE]",
|
||||
reasons: [],
|
||||
});
|
||||
}
|
||||
|
||||
if (!options?.allowShortResponse && text.length < 10) {
|
||||
rejectReasons.push("minLength");
|
||||
}
|
||||
|
||||
const mentionsDiscord = /(디스코드|discord|디코)/iu.test(text);
|
||||
const advancesDiscordStep = /(서버|프로필|권한|입장)/iu.test(text);
|
||||
const defersDiscordStep =
|
||||
/(먼저|완료.*후|완료하신 뒤|완료하신 후|모두 완료|완료되면)/u.test(text);
|
||||
|
||||
if (
|
||||
mentionsDiscord &&
|
||||
derived.phase.kind !== "completed" &&
|
||||
advancesDiscordStep &&
|
||||
!defersDiscordStep
|
||||
) {
|
||||
rejectReasons.push("stateConsistency");
|
||||
}
|
||||
|
||||
if (findInvalidNicknameExamples(text).length > 0) {
|
||||
rejectReasons.push("nicknameExampleConsistency");
|
||||
}
|
||||
|
||||
if (!hasIntentKeywordMatch(text, derived)) {
|
||||
softWarnReasons.push("intentAlignment");
|
||||
}
|
||||
|
||||
if (hasRepeatedPhrase(text)) {
|
||||
softWarnReasons.push("phraseRepetition");
|
||||
}
|
||||
|
||||
if (rejectReasons.length > 0) {
|
||||
return markValidation({
|
||||
status: "reject",
|
||||
text,
|
||||
reasons: [...rejectReasons, ...softWarnReasons],
|
||||
});
|
||||
}
|
||||
|
||||
if (softWarnReasons.length > 0) {
|
||||
return markValidation({
|
||||
status: "soft-warn",
|
||||
text,
|
||||
reasons: softWarnReasons,
|
||||
});
|
||||
}
|
||||
|
||||
return markValidation({
|
||||
status: "pass",
|
||||
text,
|
||||
reasons: [],
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user