149 lines
6.4 KiB
JavaScript
149 lines
6.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.classifyAssistantOutcomeClassV1 = classifyAssistantOutcomeClassV1;
|
|
exports.buildAssistantQueryFrameContractV1 = buildAssistantQueryFrameContractV1;
|
|
exports.buildAssistantExecutionPlanContractV1 = buildAssistantExecutionPlanContractV1;
|
|
exports.buildAssistantEvidenceBundleContractV1 = buildAssistantEvidenceBundleContractV1;
|
|
exports.buildAssistantCoverageContractV1 = buildAssistantCoverageContractV1;
|
|
function normalizeSnapshotMode(value) {
|
|
const token = String(value ?? "").trim();
|
|
if (token === "force_snapshot" || token === "force_live") {
|
|
return token;
|
|
}
|
|
return "auto";
|
|
}
|
|
function extractFragmentsTotal(normalized) {
|
|
if (!normalized || typeof normalized !== "object") {
|
|
return 0;
|
|
}
|
|
const source = normalized;
|
|
const fragments = source.fragments;
|
|
return Array.isArray(fragments) ? fragments.length : 0;
|
|
}
|
|
function collectEvidenceTotals(retrievalResults) {
|
|
let evidenceTotal = 0;
|
|
const sourceRefs = new Set();
|
|
let limitationTotal = 0;
|
|
let errorTotal = 0;
|
|
for (const result of retrievalResults) {
|
|
evidenceTotal += Array.isArray(result.evidence) ? result.evidence.length : 0;
|
|
limitationTotal += Array.isArray(result.limitations) ? result.limitations.length : 0;
|
|
errorTotal += Array.isArray(result.errors) ? result.errors.length : 0;
|
|
for (const evidence of result.evidence ?? []) {
|
|
const ref = String(evidence?.source_ref?.canonical_ref ?? "").trim();
|
|
if (ref) {
|
|
sourceRefs.add(ref);
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
evidence_total: evidenceTotal,
|
|
source_refs_total: sourceRefs.size,
|
|
limitation_total: limitationTotal,
|
|
error_total: errorTotal
|
|
};
|
|
}
|
|
function classifyAssistantOutcomeClassV1(input) {
|
|
const replyType = input.replyType;
|
|
const grounding = input.grounding;
|
|
const coverage = input.coverageReport;
|
|
const hasOnlyErrors = input.retrievalResults.length > 0 &&
|
|
input.retrievalResults.every((item) => item.status === "error");
|
|
if (replyType === "backend_error" || hasOnlyErrors) {
|
|
return "BLOCKED_BY_TOOLING";
|
|
}
|
|
if (grounding.status === "route_mismatch_blocked" || replyType === "route_mismatch_blocked") {
|
|
return "MISROUTED";
|
|
}
|
|
if (replyType === "clarification_required" || coverage.clarification_needed_for.length > 0) {
|
|
return "BLOCKED_BY_AMBIGUITY";
|
|
}
|
|
if (replyType === "out_of_scope") {
|
|
return "BLOCKED_BY_AMBIGUITY";
|
|
}
|
|
const fullCoverage = coverage.requirements_total > 0 &&
|
|
coverage.requirements_total === coverage.requirements_covered &&
|
|
coverage.requirements_uncovered.length === 0 &&
|
|
coverage.requirements_partially_covered.length === 0 &&
|
|
coverage.clarification_needed_for.length === 0 &&
|
|
coverage.out_of_scope_requirements.length === 0;
|
|
if (fullCoverage && grounding.status === "grounded") {
|
|
return "FULLY_ANSWERED";
|
|
}
|
|
const hasAnyCoverage = coverage.requirements_covered > 0 ||
|
|
coverage.requirements_partially_covered.length > 0 ||
|
|
grounding.status === "partial";
|
|
if (hasAnyCoverage) {
|
|
return "PARTIALLY_ANSWERED";
|
|
}
|
|
const missingRequirementSignal = grounding.missing_requirements.length > 0 ||
|
|
coverage.requirements_uncovered.length > 0 ||
|
|
coverage.requirements_total > 0;
|
|
const possibleBindingFailure = replyType === "no_grounded_answer" &&
|
|
missingRequirementSignal &&
|
|
grounding.route_subject_match;
|
|
if (possibleBindingFailure) {
|
|
return "FAILED_TO_BIND_ENTITIES";
|
|
}
|
|
return "BLOCKED_BY_MISSING_DATA";
|
|
}
|
|
function buildAssistantQueryFrameContractV1(input) {
|
|
const analysis = input.analysisContext
|
|
? {
|
|
as_of_date: input.analysisContext.as_of_date ?? null,
|
|
period_from: input.analysisContext.period_from ?? null,
|
|
period_to: input.analysisContext.period_to ?? null,
|
|
source: input.analysisContext.source ?? null,
|
|
snapshot_mode: normalizeSnapshotMode(input.analysisContext.snapshot_mode)
|
|
}
|
|
: null;
|
|
return {
|
|
schema_version: "assistant_query_frame_v1",
|
|
original_user_question: String(input.userMessage ?? ""),
|
|
normalized_question: String(input.normalizedQuestion ?? ""),
|
|
route_summary_mode: input.routeSummary?.mode ?? "none",
|
|
fragments_total: extractFragmentsTotal(input.normalized),
|
|
dropped_intent_segments: Array.isArray(input.droppedIntentSegments) ? [...input.droppedIntentSegments] : [],
|
|
analysis_context: analysis
|
|
};
|
|
}
|
|
function buildAssistantExecutionPlanContractV1(input) {
|
|
return {
|
|
schema_version: "assistant_execution_plan_v1",
|
|
steps: (Array.isArray(input.executionPlan) ? input.executionPlan : []).map((item) => ({
|
|
fragment_id: String(item.fragment_id ?? ""),
|
|
route: String(item.route ?? ""),
|
|
should_execute: Boolean(item.should_execute),
|
|
requirement_ids: Array.isArray(item.requirement_ids) ? [...item.requirement_ids] : [],
|
|
no_route_reason: item.no_route_reason ?? null,
|
|
clarification_reason: item.clarification_reason ?? null
|
|
})),
|
|
requirements_total: Array.isArray(input.requirements) ? input.requirements.length : 0
|
|
};
|
|
}
|
|
function buildAssistantEvidenceBundleContractV1(input) {
|
|
const retrievalResults = Array.isArray(input.retrievalResults) ? input.retrievalResults : [];
|
|
const breakdown = {
|
|
ok: retrievalResults.filter((item) => item.status === "ok").length,
|
|
partial: retrievalResults.filter((item) => item.status === "partial").length,
|
|
empty: retrievalResults.filter((item) => item.status === "empty").length,
|
|
error: retrievalResults.filter((item) => item.status === "error").length
|
|
};
|
|
const totals = collectEvidenceTotals(retrievalResults);
|
|
return {
|
|
schema_version: "assistant_evidence_bundle_v1",
|
|
retrieval_calls_total: Array.isArray(input.retrievalCalls) ? input.retrievalCalls.length : 0,
|
|
retrieval_results_total: retrievalResults.length,
|
|
retrieval_status_breakdown: breakdown,
|
|
...totals
|
|
};
|
|
}
|
|
function buildAssistantCoverageContractV1(input) {
|
|
return {
|
|
schema_version: "assistant_coverage_contract_v1",
|
|
coverage_report: input.coverageReport,
|
|
grounding: input.grounding,
|
|
outcome_class: input.outcomeClass
|
|
};
|
|
}
|