feat(v2): add strict schema validation for AI classification JSON
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
7dccb85efa
commit
c445e00fc5
@@ -0,0 +1,44 @@
|
||||
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',
|
||||
];
|
||||
|
||||
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.length > 200) errors.push('next_action invalido');
|
||||
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: raw.blocker_type ?? null,
|
||||
waiting_for: raw.waiting_for ?? null,
|
||||
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 };
|
||||
Reference in New Issue
Block a user