buildDataQualityReview extracts an auditable record per INSUFFICIENT_CONTEXT ticket (ticket_id, title, ai_reason, context_size, has_description, followup_count, task_count, has_solution, last_interaction_at) -- not sent anywhere, just for calibration. Also tightened validateAiClassification to reject an empty/whitespace-only next_action (was previously accepted). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
57 lines
2.5 KiB
JavaScript
57 lines
2.5 KiB
JavaScript
const VALID_CLASSIFICATIONS = [
|
|
'ACTIONABLE_NOW', 'UNASSIGNED_ACTIONABLE', 'WAITING_REQUESTER', 'WAITING_VENDOR',
|
|
'WAITING_APPROVAL', 'SCHEDULED', 'DEPENDENCY_BLOCKED', 'NEEDS_COORDINATION',
|
|
'PROJECT_LONG_TERM', 'DUPLICATE_OR_OBSOLETE', 'INSUFFICIENT_CONTEXT',
|
|
];
|
|
|
|
// O modelo, apesar do schema permitir null de verdade em blocker_type/waiting_for, as vezes
|
|
// devolve a STRING "null" (ou variantes equivalentes) em vez do valor JSON null. Normaliza
|
|
// aqui, na camada de validacao -- unico lugar que toca a resposta crua da IA.
|
|
const NULL_LIKE_STRINGS = new Set(['null', 'none', 'n/a', 'na', '']);
|
|
|
|
function normalizeNullableString(value) {
|
|
if (value === null || value === undefined) return null;
|
|
const trimmed = String(value).trim();
|
|
if (NULL_LIKE_STRINGS.has(trimmed.toLowerCase())) return null;
|
|
return trimmed;
|
|
}
|
|
|
|
function validateAiClassification(raw) {
|
|
const errors = [];
|
|
if (!raw || typeof raw !== 'object') {
|
|
return { valid: false, data: null, errors: ['resposta nao e um objeto JSON'] };
|
|
}
|
|
if (!VALID_CLASSIFICATIONS.includes(raw.classification)) {
|
|
errors.push(`classification invalida: ${raw.classification}`);
|
|
}
|
|
if (typeof raw.actionable_today !== 'boolean') errors.push('actionable_today deve ser boolean');
|
|
const confidence = Number(raw.confidence);
|
|
if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) errors.push('confidence deve estar entre 0 e 1');
|
|
if (typeof raw.next_action !== 'string' || raw.next_action.trim().length === 0 || raw.next_action.length > 200) errors.push('next_action invalido (vazio ou muito longo)');
|
|
if (raw.evidence != null && !Array.isArray(raw.evidence)) errors.push('evidence deve ser array');
|
|
if (typeof raw.reason !== 'string') errors.push('reason deve ser string');
|
|
|
|
if (errors.length) return { valid: false, data: null, errors };
|
|
|
|
return {
|
|
valid: true,
|
|
errors: [],
|
|
data: {
|
|
classification: raw.classification,
|
|
actionable_today: raw.actionable_today,
|
|
confidence,
|
|
blocker_type: normalizeNullableString(raw.blocker_type),
|
|
waiting_for: normalizeNullableString(raw.waiting_for),
|
|
next_action: raw.next_action,
|
|
suggested_action_type: raw.suggested_action_type ?? 'technical_action',
|
|
business_risk: raw.business_risk ?? 'medium',
|
|
reason: raw.reason,
|
|
evidence: Array.isArray(raw.evidence) ? raw.evidence.slice(0, 5) : [],
|
|
is_fallback: false,
|
|
fallback_reason: null,
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = { VALID_CLASSIFICATIONS, validateAiClassification, normalizeNullableString };
|