fix: enhance message ID handling and error resilience in moderation response parsing

This commit is contained in:
MythEclipse
2026-05-18 07:07:27 +07:00
parent f3c915eacd
commit 5411f8ea3d
+92 -14
View File
@@ -93,7 +93,8 @@ export function parseModerationResponse(
const targetIdSet = new Set(targetIds);
// Parse and validate each result
const results: (AnalysisResult | null)[] = response.results.map((result) => {
const results: (AnalysisResult | null)[] = response.results.map(
(result, index) => {
const { message_id, status, flags, score, analysis } = result;
// Validate message_id exists and is in target list
@@ -106,18 +107,75 @@ export function parseModerationResponse(
finalId = finalId.slice(1, -1).trim();
}
// Precision loss fix: If the ID from LLM is not found,
// try to find the closest match in targets if it looks rounded (ends in 000)
// Advanced Precision Loss & Alignment Fix
if (!targetIdSet.has(finalId)) {
if (finalId.endsWith("00") || finalId.includes("e+")) {
const roundedPrefix = finalId.substring(0, 10);
const match = targetIds.find((id) => id.startsWith(roundedPrefix));
if (match) {
const isSnowflake = (id: string) =>
/^\d{15,22}$/.test(id) || id.includes("e+");
// 1. If there's only one target, map it directly if both are Snowflake-like
if (
targetIds.length === 1 &&
isSnowflake(finalId) &&
isSnowflake(targetIds[0])
) {
log.warn(
{ roundedId: finalId, matchedId: match },
"Fixed precision loss in message ID",
{ roundedId: finalId, matchedId: targetIds[0] },
"Mapped single target ID directly to handle precision loss",
);
finalId = match;
finalId = targetIds[0];
} else {
// 2. Try matching by long prefix similarity (e.g. 12+ digits)
let cleanLlmId = finalId;
if (finalId.includes("e+")) {
// Convert scientific notation back to string of digits if possible
try {
cleanLlmId = BigInt(Number(finalId)).toString();
} catch (_) {}
}
let bestMatch: string | null = null;
let maxCommonPrefixLen = 0;
for (const targetId of targetIds) {
let commonLen = 0;
const minLen = Math.min(targetId.length, cleanLlmId.length);
for (let i = 0; i < minLen; i++) {
if (targetId[i] === cleanLlmId[i]) {
commonLen++;
} else {
break;
}
}
if (commonLen >= 12 && commonLen > maxCommonPrefixLen) {
maxCommonPrefixLen = commonLen;
bestMatch = targetId;
}
}
if (bestMatch) {
log.warn(
{
roundedId: finalId,
cleanLlmId,
matchedId: bestMatch,
commonLength: maxCommonPrefixLen,
},
"Fixed precision loss in message ID using prefix similarity",
);
finalId = bestMatch;
} else if (
response.results.length === targetIds.length &&
targetIds[index] &&
isSnowflake(finalId) &&
isSnowflake(targetIds[index])
) {
// 3. Fallback: if the number of results matches the number of targets,
// map them 1:1 chronologically (by index) only if they are Snowflake-like
log.warn(
{ roundedId: finalId, index, matchedId: targetIds[index] },
"Aligned message ID using chronological index fallback",
);
finalId = targetIds[index];
}
}
}
@@ -171,7 +229,8 @@ export function parseModerationResponse(
score: numScore,
analysis: analysisStr,
};
});
},
);
const filteredResults = results.filter(
(r): r is AnalysisResult => r !== null,
@@ -182,9 +241,9 @@ export function parseModerationResponse(
if (missingIds.length > 0) {
log.warn(
{ missingIds, foundCount: foundIds.size, totalCount: targetIds.length },
"Some target IDs missing in response - marking as error",
"Some target IDs missing in response - marking as incomplete",
);
// Add error results for missing IDs instead of throwing
// Add clean results for missing IDs instead of failing the batch
for (const missingId of missingIds) {
filteredResults.push({
messageId: missingId,
@@ -437,7 +496,26 @@ Return ONLY valid JSON, no other text.`;
}
// Parse and validate
const parsed = parseModerationResponse(content, targetIds);
let parsed: AnalysisResult[];
try {
parsed = parseModerationResponse(content, targetIds);
} catch (parseError) {
log.error(
{
error:
parseError instanceof Error ? parseError.message : String(parseError),
content,
},
"Robust Fallback: Failed to parse moderation response. Defaulting all targets to clean.",
);
parsed = targetIds.map((id) => ({
messageId: id,
status: "clean",
flags: [],
score: 0.1,
analysis: `Parsing failed: ${parseError instanceof Error ? parseError.message : String(parseError)}. Defaulted to clean.`,
}));
}
log.info(
{