Stage 2 завершён: problem-first ответы и follow-up continuity - ассистент переведён от entity-heavy логики к problem-first ответам с problem-unit слоем, удержанием контекста в follow-up и очисткой пользовательского ответа от сырых технических ссылок.
This commit is contained in:
@@ -8,6 +8,9 @@ import type {
|
||||
} from "../types/assistant";
|
||||
import type { RouteHintSummary } from "../types/normalizer";
|
||||
import type { AnswerStructureV11, EvidenceConfidence, EvidenceItem, EvidenceLimitationReasonCode } from "../types/stage1Contracts";
|
||||
import type { ProblemUnit, ProblemUnitSummary, ProblemUnitType } from "../types/stage2ProblemUnits";
|
||||
|
||||
type ProblemAnswerMode = "stage1_policy_v11" | "stage2_problem_centric_v1";
|
||||
|
||||
interface ComposeAnswerInput {
|
||||
userMessage: string;
|
||||
@@ -17,6 +20,7 @@ interface ComposeAnswerInput {
|
||||
coverageReport: RequirementCoverageReport;
|
||||
groundingCheck: AnswerGroundingCheck;
|
||||
enableAnswerPolicyV11?: boolean;
|
||||
enableProblemCentricAnswerV1?: boolean;
|
||||
}
|
||||
|
||||
interface ComposeAnswerOutput {
|
||||
@@ -24,6 +28,10 @@ interface ComposeAnswerOutput {
|
||||
fallback_type: AssistantFallbackType;
|
||||
reply_type: AssistantReplyType;
|
||||
answer_structure_v11?: AnswerStructureV11;
|
||||
problem_centric_answer_applied?: boolean;
|
||||
problem_units_used_count?: number;
|
||||
problem_answer_mode?: ProblemAnswerMode;
|
||||
problem_unit_ids_used?: string[];
|
||||
}
|
||||
|
||||
function fallbackFromSummary(routeSummary: RouteHintSummary | null): AssistantFallbackType {
|
||||
@@ -37,6 +45,84 @@ function uniqueStrings(values: string[], limit = 6): string[] {
|
||||
return Array.from(new Set(values.map((item) => item.trim()).filter(Boolean))).slice(0, limit);
|
||||
}
|
||||
|
||||
const UUID_PATTERN = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi;
|
||||
const LONG_HEX_PATTERN = /\b[0-9a-f]{24,}\b/gi;
|
||||
const RAW_REF_BLOB_PATTERN = /\bevidence_source_ref_v1\|[^\s,;]+/gi;
|
||||
const RAW_REF_TOKEN_PATTERN = /\b(?:source_ref|canonical_ref|entity_id|fragment_id|guid|uuid)\b/gi;
|
||||
|
||||
function looksLikeMojibake(value: string): boolean {
|
||||
const text = String(value ?? "");
|
||||
if (!text.trim()) {
|
||||
return false;
|
||||
}
|
||||
if (/(?:Р.|С.){5,}/u.test(text)) {
|
||||
return true;
|
||||
}
|
||||
if (/[ЃѓЂђЌќЎў]/u.test(text)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function looksLikeTechnicalIdentifier(value: string): boolean {
|
||||
const text = String(value ?? "").trim();
|
||||
if (!text) {
|
||||
return false;
|
||||
}
|
||||
if (UUID_PATTERN.test(text)) {
|
||||
UUID_PATTERN.lastIndex = 0;
|
||||
return true;
|
||||
}
|
||||
UUID_PATTERN.lastIndex = 0;
|
||||
if (LONG_HEX_PATTERN.test(text)) {
|
||||
LONG_HEX_PATTERN.lastIndex = 0;
|
||||
return true;
|
||||
}
|
||||
LONG_HEX_PATTERN.lastIndex = 0;
|
||||
return /(?:evidence_source_ref_v1\||cmp%3a|batch_refresh_then_store:|^cmp:)/i.test(text);
|
||||
}
|
||||
|
||||
function scrubRawTechnicalRefs(value: string): string {
|
||||
const raw = String(value ?? "").trim();
|
||||
if (!raw) {
|
||||
return "";
|
||||
}
|
||||
return raw
|
||||
.replace(RAW_REF_BLOB_PATTERN, "linked source")
|
||||
.replace(UUID_PATTERN, "[id]")
|
||||
.replace(LONG_HEX_PATTERN, "[id]")
|
||||
.replace(RAW_REF_TOKEN_PATTERN, "reference")
|
||||
.replace(/\(\s*\[id\]\s*\)/g, "")
|
||||
.replace(/\[\s*id\s*\](?:\s*,\s*\[\s*id\s*\])+/g, "[id]")
|
||||
.replace(/\s{2,}/g, " ")
|
||||
.trim();
|
||||
}
|
||||
|
||||
function sanitizeUserFacingReply(value: string): string {
|
||||
return scrubRawTechnicalRefs(value)
|
||||
.replace(/[ \t]+\n/g, "\n")
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trim();
|
||||
}
|
||||
|
||||
function sanitizeUserText(value: string): string | null {
|
||||
const normalized = scrubRawTechnicalRefs(String(value ?? "").replace(/\s+/g, " ").trim());
|
||||
if (!normalized) {
|
||||
return null;
|
||||
}
|
||||
if (looksLikeMojibake(normalized)) {
|
||||
return null;
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function sanitizeUserLines(values: string[], limit = 6): string[] {
|
||||
const cleaned = values
|
||||
.map((item) => sanitizeUserText(item))
|
||||
.filter((item): item is string => Boolean(item));
|
||||
return uniqueStrings(cleaned, limit);
|
||||
}
|
||||
|
||||
function formatList(items: string[]): string {
|
||||
if (items.length === 0) {
|
||||
return "";
|
||||
@@ -44,15 +130,28 @@ function formatList(items: string[]): string {
|
||||
return items.map((item) => `- ${item}`).join("\n");
|
||||
}
|
||||
|
||||
function formatSafeItemLine(entity: unknown, sourceId: unknown, riskScore?: unknown): string {
|
||||
const entityLabel = sanitizeUserText(String(entity ?? "")) ?? "Record";
|
||||
const idRaw = String(sourceId ?? "").trim();
|
||||
const exposeId = idRaw.length > 0 && !looksLikeTechnicalIdentifier(idRaw);
|
||||
const subject = exposeId ? `${entityLabel} (${idRaw})` : entityLabel;
|
||||
if (riskScore !== undefined) {
|
||||
return `${subject} - risk ${String(riskScore)}.`;
|
||||
}
|
||||
return `${subject}.`;
|
||||
}
|
||||
|
||||
function extractTopFacts(results: UnifiedRetrievalResult[]): string[] {
|
||||
const lines: string[] = [];
|
||||
for (const result of results.filter((item) => item.status === "ok").slice(0, 3)) {
|
||||
if (result.result_type === "chain") {
|
||||
const top = result.items.slice(0, 3).map((item) => {
|
||||
const counterparty = String(item.counterparty_id ?? "не указан");
|
||||
const counterparty = String(item.counterparty_id ?? "").trim();
|
||||
const operations = String(item.operations_count ?? "0");
|
||||
const docs = String(item.document_refs_count ?? "0");
|
||||
return `Контрагент ${counterparty}: операций ${operations}, документов в связке ${docs}.`;
|
||||
const counterpartyLabel =
|
||||
counterparty.length > 0 && !looksLikeTechnicalIdentifier(counterparty) ? `Counterparty ${counterparty}` : "Counterparty";
|
||||
return `${counterpartyLabel}: operations ${operations}, linked docs ${docs}.`;
|
||||
});
|
||||
lines.push(...top);
|
||||
continue;
|
||||
@@ -60,46 +159,46 @@ function extractTopFacts(results: UnifiedRetrievalResult[]): string[] {
|
||||
if (result.result_type === "ranking") {
|
||||
const top = result.items
|
||||
.slice(0, 5)
|
||||
.map((item) => `${item.rank ?? "•"}. ${String(item.entity ?? "Сущность")} — ${String(item.records_count ?? 0)}.`);
|
||||
.map((item) => `${item.rank ?? "*"}. ${String(item.entity ?? "Entity")} - ${String(item.records_count ?? 0)}.`);
|
||||
lines.push(...top);
|
||||
continue;
|
||||
}
|
||||
if (result.result_type === "list") {
|
||||
const top = result.items.slice(0, 5).map((item) => {
|
||||
if (item.risk_score !== undefined) {
|
||||
return `${String(item.source_entity ?? "Запись")} (${String(item.source_id ?? "")}) — риск ${String(item.risk_score)}.`;
|
||||
return formatSafeItemLine(item.source_entity ?? "Record", item.source_id ?? "", item.risk_score);
|
||||
}
|
||||
return `${String(item.source_entity ?? "Запись")} (${String(item.source_id ?? "")}).`;
|
||||
return formatSafeItemLine(item.source_entity ?? "Record", item.source_id ?? "");
|
||||
});
|
||||
lines.push(...top);
|
||||
continue;
|
||||
}
|
||||
const top = result.items
|
||||
.slice(0, 3)
|
||||
.map((item) => `${String(item.source_entity ?? "Запись")} (${String(item.source_id ?? "")}).`);
|
||||
.map((item) => formatSafeItemLine(item.source_entity ?? "Record", item.source_id ?? ""));
|
||||
lines.push(...top);
|
||||
}
|
||||
return lines;
|
||||
return sanitizeUserLines(lines, 8);
|
||||
}
|
||||
|
||||
function extractWhyIncluded(results: UnifiedRetrievalResult[]): string[] {
|
||||
return uniqueStrings(results.flatMap((item) => item.why_included));
|
||||
return sanitizeUserLines(results.flatMap((item) => item.why_included));
|
||||
}
|
||||
|
||||
function extractSelectionReasons(results: UnifiedRetrievalResult[]): string[] {
|
||||
return uniqueStrings(results.flatMap((item) => item.selection_reason));
|
||||
return sanitizeUserLines(results.flatMap((item) => item.selection_reason));
|
||||
}
|
||||
|
||||
function extractRiskFactors(results: UnifiedRetrievalResult[]): string[] {
|
||||
return uniqueStrings(results.flatMap((item) => item.risk_factors));
|
||||
return sanitizeUserLines(results.flatMap((item) => item.risk_factors));
|
||||
}
|
||||
|
||||
function extractBusinessInterpretation(results: UnifiedRetrievalResult[]): string[] {
|
||||
return uniqueStrings(results.flatMap((item) => item.business_interpretation));
|
||||
return sanitizeUserLines(results.flatMap((item) => item.business_interpretation));
|
||||
}
|
||||
|
||||
function extractLimitations(results: UnifiedRetrievalResult[]): string[] {
|
||||
return uniqueStrings(results.flatMap((item) => item.limitations));
|
||||
return sanitizeUserLines(results.flatMap((item) => item.limitations), 10);
|
||||
}
|
||||
|
||||
function summaryValue(result: UnifiedRetrievalResult, key: string): unknown {
|
||||
@@ -116,6 +215,63 @@ function summaryString(result: UnifiedRetrievalResult, key: string): string | nu
|
||||
return typeof value === "string" ? value : null;
|
||||
}
|
||||
|
||||
function summaryNumber(result: UnifiedRetrievalResult, key: string): number | null {
|
||||
const value = summaryValue(result, key);
|
||||
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
||||
}
|
||||
|
||||
function summaryStringArray(result: UnifiedRetrievalResult, key: string): string[] {
|
||||
const value = summaryValue(result, key);
|
||||
if (!Array.isArray(value)) {
|
||||
return [];
|
||||
}
|
||||
return sanitizeUserLines(value.map((item) => String(item)), 6);
|
||||
}
|
||||
|
||||
function buildFallbackWhyIncluded(results: UnifiedRetrievalResult[]): string[] {
|
||||
const lines: string[] = [];
|
||||
for (const result of results.slice(0, 2)) {
|
||||
const routeFocus = summaryString(result, "route_focus");
|
||||
const sourceRecords = summaryNumber(result, "source_records");
|
||||
const filteredRecords = summaryNumber(result, "filtered_records_after_narrowing");
|
||||
const checkedRecords = summaryNumber(result, "checked_records");
|
||||
|
||||
if (routeFocus) {
|
||||
lines.push(`Проверка выполнена по профилю ${routeFocus}.`);
|
||||
}
|
||||
if (sourceRecords !== null && filteredRecords !== null && filteredRecords < sourceRecords) {
|
||||
lines.push(`Применено сужение выборки: ${filteredRecords} из ${sourceRecords} записей.`);
|
||||
}
|
||||
if (checkedRecords !== null) {
|
||||
lines.push(`Проверено записей в текущем проходе: ${checkedRecords}.`);
|
||||
}
|
||||
}
|
||||
|
||||
return sanitizeUserLines(lines, 4);
|
||||
}
|
||||
|
||||
function buildFallbackSelectionReasons(results: UnifiedRetrievalResult[]): string[] {
|
||||
const lines: string[] = [];
|
||||
for (const result of results.slice(0, 2)) {
|
||||
if (summaryBoolean(result, "semantic_narrowing_applied")) {
|
||||
lines.push("Отбор выполнен по семантическому сужению предметной области.");
|
||||
}
|
||||
const rankingBasis = summaryStringArray(result, "ranking_basis");
|
||||
if (rankingBasis.length > 0) {
|
||||
lines.push(`Ранжирование основано на: ${rankingBasis.join(", ")}.`);
|
||||
}
|
||||
if (summaryBoolean(result, "broad_guard_applied")) {
|
||||
lines.push("Применен broad-query guard для контроля ложной точности.");
|
||||
}
|
||||
}
|
||||
|
||||
if (lines.length === 0) {
|
||||
lines.push("Отбор выполнен по совпадению предметных сигналов и доступной evidence-опоры.");
|
||||
}
|
||||
|
||||
return sanitizeUserLines(lines, 4);
|
||||
}
|
||||
|
||||
function suggestNextStep(requirements: AssistantRequirement[], coverage: RequirementCoverageReport): string[] {
|
||||
const next: string[] = [];
|
||||
if (coverage.clarification_needed_for.length > 0) {
|
||||
@@ -165,10 +321,155 @@ interface MissingAnchors {
|
||||
anomalyType: boolean;
|
||||
}
|
||||
|
||||
const PROBLEM_HEAVY_TYPES = new Set<ProblemUnitType>([
|
||||
"document_conflict",
|
||||
"broken_chain_segment",
|
||||
"lifecycle_anomaly_node",
|
||||
"unresolved_settlement_cluster",
|
||||
"period_risk_cluster",
|
||||
"cross_branch_inconsistency_cluster"
|
||||
]);
|
||||
|
||||
function flattenEvidence(results: UnifiedRetrievalResult[]): EvidenceItem[] {
|
||||
return results.flatMap((item) => item.evidence);
|
||||
}
|
||||
|
||||
function flattenProblemUnits(results: UnifiedRetrievalResult[]): ProblemUnit[] {
|
||||
const units: ProblemUnit[] = [];
|
||||
for (const result of results) {
|
||||
if (!Array.isArray(result.problem_units)) {
|
||||
continue;
|
||||
}
|
||||
units.push(...result.problem_units);
|
||||
}
|
||||
const byId = new Map<string, ProblemUnit>();
|
||||
for (const unit of units) {
|
||||
byId.set(unit.problem_unit_id, unit);
|
||||
}
|
||||
return Array.from(byId.values());
|
||||
}
|
||||
|
||||
function selectProblemUnitSummary(results: UnifiedRetrievalResult[]): ProblemUnitSummary | null {
|
||||
let selected: ProblemUnitSummary | null = null;
|
||||
for (const result of results) {
|
||||
if (!result.problem_unit_summary) {
|
||||
continue;
|
||||
}
|
||||
if (!selected || result.problem_unit_summary.units_total > selected.units_total) {
|
||||
selected = result.problem_unit_summary;
|
||||
}
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
function formatAffectedScope(unit: ProblemUnit): string {
|
||||
const scopeParts: string[] = [];
|
||||
if (unit.affected_accounts.length > 0) {
|
||||
scopeParts.push(`счета: ${unit.affected_accounts.slice(0, 2).join(", ")}`);
|
||||
}
|
||||
if (unit.affected_counterparties.length > 0) {
|
||||
scopeParts.push(`контрагенты: ${unit.affected_counterparties.slice(0, 2).join(", ")}`);
|
||||
}
|
||||
if (unit.affected_documents.length > 0) {
|
||||
scopeParts.push(`документы: ${unit.affected_documents.slice(0, 2).join(", ")}`);
|
||||
}
|
||||
if (scopeParts.length === 0 && unit.affected_entities.length > 0) {
|
||||
scopeParts.push(`объекты: ${unit.affected_entities.slice(0, 2).join(", ")}`);
|
||||
}
|
||||
if (scopeParts.length === 0) {
|
||||
return "затронутый контур требует уточнения";
|
||||
}
|
||||
return scopeParts.join("; ");
|
||||
}
|
||||
|
||||
function buildProblemCentricActions(input: {
|
||||
units: ProblemUnit[];
|
||||
mode: PolicyMode;
|
||||
missingAnchors: MissingAnchors;
|
||||
coverageReport: RequirementCoverageReport;
|
||||
}): string[] {
|
||||
const actions: string[] = [];
|
||||
const unitTypes = new Set(input.units.map((item) => item.problem_unit_type));
|
||||
|
||||
if (unitTypes.has("broken_chain_segment")) {
|
||||
actions.push("Проверьте связку выписка -> документ -> проводка по проблемным участкам цепочки.");
|
||||
}
|
||||
if (unitTypes.has("unresolved_settlement_cluster")) {
|
||||
actions.push("Сверьте хвосты по расчетам: закрылся ли документ оплаты корректным закрывающим документом.");
|
||||
}
|
||||
if (unitTypes.has("period_risk_cluster")) {
|
||||
actions.push("Оцените влияние дефекта на закрытие периода и корректность регламентных операций.");
|
||||
}
|
||||
if (unitTypes.has("cross_branch_inconsistency_cluster")) {
|
||||
actions.push("Сверьте противоречия между документами, проводками и регистрами по НДС/межконтурным связям.");
|
||||
}
|
||||
if (unitTypes.has("lifecycle_anomaly_node")) {
|
||||
actions.push("Проверьте lifecycle объекта: ожидаемый этап не должен оставаться в partially_linked состоянии.");
|
||||
}
|
||||
|
||||
if (input.mode === "clarification_required") {
|
||||
if (input.missingAnchors.period) {
|
||||
actions.push("Уточните период проверки, чтобы зафиксировать границы проблемного контура.");
|
||||
}
|
||||
if (input.missingAnchors.account) {
|
||||
actions.push("Уточните счет или группу счетов для предметной локализации дефекта.");
|
||||
}
|
||||
if (input.missingAnchors.documentOrObject) {
|
||||
actions.push("Укажите конкретный документ или объект трассировки для проверки механизма отклонения.");
|
||||
}
|
||||
if (input.missingAnchors.counterparty) {
|
||||
actions.push("Укажите контрагента/договор, чтобы проверить хвосты и разрывы на конкретной связке.");
|
||||
}
|
||||
}
|
||||
|
||||
if (input.coverageReport.requirements_uncovered.length > 0) {
|
||||
actions.push(`Закройте непокрытые требования: ${input.coverageReport.requirements_uncovered.join(", ")}.`);
|
||||
}
|
||||
|
||||
return uniqueStrings(actions, 6);
|
||||
}
|
||||
|
||||
function buildProblemCentricClarifications(input: {
|
||||
units: ProblemUnit[];
|
||||
missingAnchors: MissingAnchors;
|
||||
coverageReport: RequirementCoverageReport;
|
||||
mode: PolicyMode;
|
||||
}): string[] {
|
||||
if (input.mode !== "clarification_required") {
|
||||
return [];
|
||||
}
|
||||
|
||||
const questions: string[] = [];
|
||||
const unitTypes = new Set(input.units.map((item) => item.problem_unit_type));
|
||||
|
||||
if (input.missingAnchors.period) {
|
||||
questions.push("Уточните период (например, 2020-06), в котором нужно проверить проблемный кластер.");
|
||||
}
|
||||
if (input.missingAnchors.account) {
|
||||
questions.push("Уточните счет или связку счетов (например, 51/60), где вы ожидаете дефект.");
|
||||
}
|
||||
if (input.missingAnchors.documentOrObject) {
|
||||
questions.push("Укажите документ/объект, от которого нужно строить проверку цепочки.");
|
||||
}
|
||||
if (input.missingAnchors.counterparty) {
|
||||
questions.push("Укажите контрагента или договор, по которому проверить незакрытую экспозицию.");
|
||||
}
|
||||
if (unitTypes.has("broken_chain_segment")) {
|
||||
questions.push("Уточните участок цепочки: выписка, платежный документ или проводка.");
|
||||
}
|
||||
if (unitTypes.has("period_risk_cluster")) {
|
||||
questions.push("Уточните, какой этап закрытия периода критичен: начисление, закрытие счетов или НДС-блок.");
|
||||
}
|
||||
if (unitTypes.has("unresolved_settlement_cluster")) {
|
||||
questions.push("Уточните, интересуют хвосты поставщиков, покупателей или оба направления.");
|
||||
}
|
||||
if (input.coverageReport.clarification_needed_for.length > 0) {
|
||||
questions.push(`Закройте уточнения для требований: ${input.coverageReport.clarification_needed_for.join(", ")}.`);
|
||||
}
|
||||
|
||||
return uniqueStrings(questions, 6);
|
||||
}
|
||||
|
||||
function buildClaimEvidenceLinks(results: UnifiedRetrievalResult[]): NonNullable<AnswerStructureV11["evidence_block"]["claim_evidence_links"]> {
|
||||
const byClaim = new Map<string, string[]>();
|
||||
for (const evidence of flattenEvidence(results)) {
|
||||
@@ -333,7 +634,7 @@ function buildRecommendedActions(input: {
|
||||
}): string[] {
|
||||
const actions: string[] = [];
|
||||
if (input.mode === "focused_grounded") {
|
||||
actions.push("Проверьте 1-2 ключевые записи по source_ref и зафиксируйте итог в рабочем файле проверки.");
|
||||
actions.push("Проверьте 1-2 ключевые записи в учетной базе и зафиксируйте итог в рабочем файле проверки.");
|
||||
}
|
||||
if (input.mode === "broad_partial") {
|
||||
actions.push("Сузьте запрос до периода + счета или периода + документа и повторите проверку.");
|
||||
@@ -357,7 +658,7 @@ function buildRecommendedActions(input: {
|
||||
actions.push("Проверьте source mapping для связей document/register по указанным ref.");
|
||||
}
|
||||
if (input.sourceRefs.length > 0) {
|
||||
actions.push(`Начните проверку с source_ref: ${input.sourceRefs.slice(0, 2).join(", ")}.`);
|
||||
actions.push(`Начните проверку с ${input.sourceRefs.length} подтвержденных записей и сверьте их с первичными документами.`);
|
||||
}
|
||||
|
||||
return uniqueStrings(actions, 6);
|
||||
@@ -520,6 +821,167 @@ function buildDirectAnswer(input: {
|
||||
return "Не удалось сформировать обоснованный ответ; нужно уточнение запроса.";
|
||||
}
|
||||
|
||||
function buildProblemCentricAnswerSummary(input: {
|
||||
mode: PolicyMode;
|
||||
weakUnits: boolean;
|
||||
summary: ProblemUnitSummary | null;
|
||||
}): string {
|
||||
if (input.mode === "clarification_required") {
|
||||
return "Выявлены проблемные кластеры, но для надежного вывода требуется предметное уточнение фокуса.";
|
||||
}
|
||||
if (input.weakUnits) {
|
||||
return "Сформирован problem-centric срез с ограниченной опорой; вывод предварительный и требует до-проверки.";
|
||||
}
|
||||
if (input.summary?.units_total && input.summary.units_total > 1) {
|
||||
return `Сформирован problem-centric срез: выделено ${input.summary.units_total} проблемных кластера с приоритетами.`;
|
||||
}
|
||||
return "Сформирован problem-centric срез: выделен ключевой проблемный кластер и затронутый контур.";
|
||||
}
|
||||
|
||||
function buildProblemCentricDirectAnswer(input: {
|
||||
mode: PolicyMode;
|
||||
units: ProblemUnit[];
|
||||
weakUnits: boolean;
|
||||
}): string {
|
||||
const lead =
|
||||
input.mode === "clarification_required"
|
||||
? "Обнаружены проблемные зоны, но без уточнения якорей сильный factual-вывод преждевременен."
|
||||
: input.weakUnits
|
||||
? "Выделены проблемные зоны с ограниченной надежностью; вывод дан в ограниченном режиме."
|
||||
: "Выделены ключевые проблемные зоны и их влияние на учетный контур.";
|
||||
|
||||
const unitLines = input.units.map((unit) => {
|
||||
const scope = formatAffectedScope(unit);
|
||||
return `- ${unit.title}: ${unit.business_defect_class}; ${scope}; severity=${unit.severity.grade}, confidence=${unit.confidence.grade}.`;
|
||||
});
|
||||
|
||||
if (unitLines.length === 0) {
|
||||
return `${lead}\nПроблемные кластеры не удалось детализировать в текущем срезе.`;
|
||||
}
|
||||
|
||||
return [lead, "Проблемные кластеры:", ...unitLines].join("\n");
|
||||
}
|
||||
|
||||
function buildProblemCentricAnswerStructure(input: {
|
||||
mode: PolicyMode;
|
||||
selectedUnits: ProblemUnit[];
|
||||
problemSummary: ProblemUnitSummary | null;
|
||||
evidenceItems: EvidenceItem[];
|
||||
claimEvidenceLinks: NonNullable<AnswerStructureV11["evidence_block"]["claim_evidence_links"]>;
|
||||
limitationReasonCodes: EvidenceLimitationReasonCode[];
|
||||
groundingCheck: AnswerGroundingCheck;
|
||||
retrievalResults: UnifiedRetrievalResult[];
|
||||
missingAnchors: MissingAnchors;
|
||||
coverageReport: RequirementCoverageReport;
|
||||
}): AnswerStructureV11 {
|
||||
const weakUnits = input.selectedUnits.every((item) => item.confidence.grade === "low");
|
||||
const unitMechanismNotes = uniqueStrings(
|
||||
input.selectedUnits
|
||||
.map((item) => item.mechanism_summary)
|
||||
.filter((item) => typeof item === "string" && item.trim().length > 0),
|
||||
6
|
||||
);
|
||||
const sourceRefs = uniqueStrings(
|
||||
input.evidenceItems
|
||||
.map((item) => item.source_ref?.canonical_ref)
|
||||
.filter((item): item is string => typeof item === "string" && item.trim().length > 0),
|
||||
6
|
||||
);
|
||||
const evidenceIds = uniqueStrings(input.evidenceItems.map((item) => item.evidence_id), 10);
|
||||
|
||||
const mechanismStatus: AnswerStructureV11["mechanism_block"]["status"] =
|
||||
unitMechanismNotes.length === 0
|
||||
? "unresolved"
|
||||
: weakUnits || input.limitationReasonCodes.includes("missing_mechanism")
|
||||
? "limited"
|
||||
: "grounded";
|
||||
|
||||
const problemSpecificLimitations: string[] = [];
|
||||
if (weakUnits) {
|
||||
problemSpecificLimitations.push("Problem units remain weak-confidence; conclusions are intentionally limited.");
|
||||
}
|
||||
if (input.problemSummary?.duplicate_collapses && input.problemSummary.duplicate_collapses > 0) {
|
||||
problemSpecificLimitations.push("Part of the problem signal was merged due to duplicate collapse.");
|
||||
}
|
||||
|
||||
const limitations = uniqueStrings(
|
||||
[
|
||||
...problemSpecificLimitations,
|
||||
...input.limitationReasonCodes.map((code) => limitationReasonToText(code)),
|
||||
...extractLimitations(input.retrievalResults),
|
||||
...input.groundingCheck.reasons
|
||||
],
|
||||
10
|
||||
);
|
||||
|
||||
const openUncertainties = uniqueStrings(
|
||||
[
|
||||
...input.groundingCheck.missing_requirements,
|
||||
...(input.mode === "clarification_required" && input.missingAnchors.period ? ["missing_anchor:period"] : []),
|
||||
...(input.mode === "clarification_required" && input.missingAnchors.account ? ["missing_anchor:account"] : []),
|
||||
...(input.mode === "clarification_required" && input.missingAnchors.documentOrObject
|
||||
? ["missing_anchor:document_or_object"]
|
||||
: []),
|
||||
...(input.mode === "clarification_required" && input.missingAnchors.counterparty ? ["missing_anchor:counterparty"] : [])
|
||||
],
|
||||
8
|
||||
);
|
||||
|
||||
return {
|
||||
schema_version: "answer_structure_v1_1",
|
||||
answer_summary: buildProblemCentricAnswerSummary({
|
||||
mode: input.mode,
|
||||
weakUnits,
|
||||
summary: input.problemSummary
|
||||
}),
|
||||
direct_answer: buildProblemCentricDirectAnswer({
|
||||
mode: input.mode,
|
||||
units: input.selectedUnits,
|
||||
weakUnits
|
||||
}),
|
||||
mechanism_block: {
|
||||
status: mechanismStatus,
|
||||
mechanism_notes: unitMechanismNotes,
|
||||
limitation_reason_codes: input.limitationReasonCodes
|
||||
},
|
||||
evidence_block: {
|
||||
evidence_ids: evidenceIds,
|
||||
source_refs: sourceRefs,
|
||||
mechanism_notes: unitMechanismNotes,
|
||||
coverage_note:
|
||||
input.coverageReport.requirements_total > 0 &&
|
||||
input.coverageReport.requirements_total === input.coverageReport.requirements_covered &&
|
||||
input.coverageReport.requirements_uncovered.length === 0 &&
|
||||
input.coverageReport.requirements_partially_covered.length === 0
|
||||
? "coverage_full_or_near_full"
|
||||
: "coverage_partial_or_limited",
|
||||
...(input.claimEvidenceLinks.length > 0
|
||||
? {
|
||||
claim_evidence_links: input.claimEvidenceLinks
|
||||
}
|
||||
: {})
|
||||
},
|
||||
uncertainty_block: {
|
||||
open_uncertainties: openUncertainties,
|
||||
limitations
|
||||
},
|
||||
next_step_block: {
|
||||
recommended_actions: buildProblemCentricActions({
|
||||
units: input.selectedUnits,
|
||||
mode: input.mode,
|
||||
missingAnchors: input.missingAnchors,
|
||||
coverageReport: input.coverageReport
|
||||
}),
|
||||
clarification_questions: buildProblemCentricClarifications({
|
||||
units: input.selectedUnits,
|
||||
missingAnchors: input.missingAnchors,
|
||||
coverageReport: input.coverageReport,
|
||||
mode: input.mode
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function renderPolicyReply(structure: AnswerStructureV11): string {
|
||||
const mechanismLines: string[] = [`status=${structure.mechanism_block.status}`];
|
||||
if (structure.mechanism_block.mechanism_notes.length > 0) {
|
||||
@@ -532,18 +994,18 @@ function renderPolicyReply(structure: AnswerStructureV11): string {
|
||||
mechanismLines.push("mechanism_note is intentionally omitted due to weak or missing mechanism evidence");
|
||||
}
|
||||
|
||||
const sourceRefCount = Array.isArray(structure.evidence_block.source_refs) ? structure.evidence_block.source_refs.length : 0;
|
||||
const claimLinkCount = Array.isArray(structure.evidence_block.claim_evidence_links)
|
||||
? structure.evidence_block.claim_evidence_links.length
|
||||
: 0;
|
||||
const evidenceLines: string[] = [
|
||||
`coverage=${structure.evidence_block.coverage_note}`,
|
||||
`evidence_ids=${structure.evidence_block.evidence_ids.length > 0 ? structure.evidence_block.evidence_ids.join(", ") : "none"}`
|
||||
`supporting_evidence_count=${structure.evidence_block.evidence_ids.length}`,
|
||||
`supporting_source_count=${sourceRefCount}`,
|
||||
`claim_support_links=${claimLinkCount}`
|
||||
];
|
||||
if (Array.isArray(structure.evidence_block.source_refs) && structure.evidence_block.source_refs.length > 0) {
|
||||
evidenceLines.push(`source_refs=${structure.evidence_block.source_refs.join(", ")}`);
|
||||
}
|
||||
if (Array.isArray(structure.evidence_block.claim_evidence_links) && structure.evidence_block.claim_evidence_links.length > 0) {
|
||||
const compactLinks = structure.evidence_block.claim_evidence_links
|
||||
.slice(0, 4)
|
||||
.map((item) => `${item.claim_ref}:${item.evidence_ids.join("|")}`);
|
||||
evidenceLines.push(`claim_evidence_links=${compactLinks.join("; ")}`);
|
||||
if (sourceRefCount > 0) {
|
||||
evidenceLines.push("Detailed source references are available in debug payload.");
|
||||
}
|
||||
|
||||
const uncertaintyLines = [
|
||||
@@ -562,16 +1024,18 @@ function renderPolicyReply(structure: AnswerStructureV11): string {
|
||||
nextStepLines.push("No additional action is required for this scoped answer.");
|
||||
}
|
||||
|
||||
return [
|
||||
`Answer summary: ${structure.answer_summary}`,
|
||||
`Direct answer:\n${structure.direct_answer}`,
|
||||
`Mechanism block:\n${formatList(mechanismLines)}`,
|
||||
`Evidence block:\n${formatList(evidenceLines)}`,
|
||||
`Uncertainty block:\n${formatList(uncertaintyLines)}`,
|
||||
`Next step block:\n${formatList(nextStepLines)}`
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n\n");
|
||||
return sanitizeUserFacingReply(
|
||||
[
|
||||
`Answer summary: ${structure.answer_summary}`,
|
||||
`Direct answer:\n${structure.direct_answer}`,
|
||||
`Mechanism block:\n${formatList(mechanismLines)}`,
|
||||
`Evidence block:\n${formatList(evidenceLines)}`,
|
||||
`Uncertainty block:\n${formatList(uncertaintyLines)}`,
|
||||
`Next step block:\n${formatList(nextStepLines)}`
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n\n")
|
||||
);
|
||||
}
|
||||
|
||||
function composeAssistantAnswerV11(input: ComposeAnswerInput): ComposeAnswerOutput {
|
||||
@@ -595,8 +1059,15 @@ function composeAssistantAnswerV11(input: ComposeAnswerInput): ComposeAnswerOutp
|
||||
.filter((item): item is string => typeof item === "string" && item.trim().length > 0),
|
||||
6
|
||||
);
|
||||
const problemUnits = flattenProblemUnits(input.retrievalResults);
|
||||
const problemUnitSummary = selectProblemUnitSummary(input.retrievalResults);
|
||||
const problemHeavyUnits = problemUnits.filter((item) => PROBLEM_HEAVY_TYPES.has(item.problem_unit_type));
|
||||
const selectedProblemUnits = problemHeavyUnits.slice(0, 4);
|
||||
const claimEvidenceLinks = buildClaimEvidenceLinks(input.retrievalResults);
|
||||
const aggregateEvidenceConfidence = aggregateConfidence(input.retrievalResults, evidenceItems);
|
||||
const lowConfidenceSignals = evidenceItems.filter((item) => item.confidence === "low").length;
|
||||
const lowConfidenceShare = evidenceItems.length > 0 ? lowConfidenceSignals / evidenceItems.length : 0;
|
||||
const lowConfidenceConcentration = lowConfidenceShare >= 0.6;
|
||||
const hasSupport =
|
||||
okResults.length > 0 ||
|
||||
partialResults.length > 0 ||
|
||||
@@ -637,6 +1108,51 @@ function composeAssistantAnswerV11(input: ComposeAnswerInput): ComposeAnswerOutp
|
||||
});
|
||||
|
||||
const missingAnchors = detectMissingAnchors(input.userMessage);
|
||||
const hasProblemWeakSignal =
|
||||
policySignals.narrowing_strength !== "strong" ||
|
||||
policySignals.minimum_evidence_failed ||
|
||||
limitationReasonCodes.includes("missing_mechanism") ||
|
||||
limitationReasonCodes.includes("weak_source_mapping") ||
|
||||
aggregateEvidenceConfidence === "low" ||
|
||||
lowConfidenceConcentration;
|
||||
const hardBlockedMode = decision.mode === "out_of_scope" || decision.mode === "route_mismatch" || decision.mode === "backend_error";
|
||||
const problemCentricModeEligible =
|
||||
decision.mode === "broad_partial" ||
|
||||
decision.mode === "clarification_required" ||
|
||||
(decision.mode === "focused_grounded" && hasProblemWeakSignal);
|
||||
const shouldUseProblemCentricAnswer =
|
||||
Boolean(input.enableProblemCentricAnswerV1) &&
|
||||
!hardBlockedMode &&
|
||||
problemCentricModeEligible &&
|
||||
(!focusedStrong || hasProblemWeakSignal) &&
|
||||
selectedProblemUnits.length > 0;
|
||||
|
||||
if (shouldUseProblemCentricAnswer) {
|
||||
const problemCentricStructure = buildProblemCentricAnswerStructure({
|
||||
mode: decision.mode,
|
||||
selectedUnits: selectedProblemUnits,
|
||||
problemSummary: problemUnitSummary,
|
||||
evidenceItems,
|
||||
claimEvidenceLinks,
|
||||
limitationReasonCodes,
|
||||
groundingCheck: input.groundingCheck,
|
||||
retrievalResults: input.retrievalResults,
|
||||
missingAnchors,
|
||||
coverageReport: input.coverageReport
|
||||
});
|
||||
|
||||
return {
|
||||
assistant_reply: renderPolicyReply(problemCentricStructure),
|
||||
fallback_type: decision.fallback_type,
|
||||
reply_type: decision.reply_type,
|
||||
answer_structure_v11: problemCentricStructure,
|
||||
problem_centric_answer_applied: true,
|
||||
problem_units_used_count: selectedProblemUnits.length,
|
||||
problem_answer_mode: "stage2_problem_centric_v1",
|
||||
problem_unit_ids_used: selectedProblemUnits.map((item) => item.problem_unit_id)
|
||||
};
|
||||
}
|
||||
|
||||
const clarificationQuestions = buildClarificationQuestions({
|
||||
mode: decision.mode,
|
||||
missingAnchors,
|
||||
@@ -725,14 +1241,20 @@ function composeAssistantAnswerV11(input: ComposeAnswerInput): ComposeAnswerOutp
|
||||
assistant_reply: renderPolicyReply(answerStructure),
|
||||
fallback_type: decision.fallback_type,
|
||||
reply_type: decision.reply_type,
|
||||
answer_structure_v11: answerStructure
|
||||
answer_structure_v11: answerStructure,
|
||||
problem_centric_answer_applied: false,
|
||||
problem_units_used_count: 0,
|
||||
problem_answer_mode: "stage1_policy_v11"
|
||||
};
|
||||
}
|
||||
|
||||
function composeExplainableAnswer(input: ComposeAnswerInput, scopeLabel: "full" | "partial"): string {
|
||||
const facts = extractTopFacts(input.retrievalResults);
|
||||
const whyIncluded = extractWhyIncluded(input.retrievalResults);
|
||||
const selectionReasons = extractSelectionReasons(input.retrievalResults);
|
||||
const whyIncludedRaw = extractWhyIncluded(input.retrievalResults);
|
||||
const selectionReasonsRaw = extractSelectionReasons(input.retrievalResults);
|
||||
const whyIncluded = whyIncludedRaw.length > 0 ? whyIncludedRaw : buildFallbackWhyIncluded(input.retrievalResults);
|
||||
const selectionReasons =
|
||||
selectionReasonsRaw.length > 0 ? selectionReasonsRaw : buildFallbackSelectionReasons(input.retrievalResults);
|
||||
const riskFactors = extractRiskFactors(input.retrievalResults);
|
||||
const interpretation = extractBusinessInterpretation(input.retrievalResults);
|
||||
const limitations = uniqueStrings([...extractLimitations(input.retrievalResults), ...input.groundingCheck.reasons]);
|
||||
@@ -877,3 +1399,4 @@ export function composeAssistantAnswer(input: ComposeAnswerInput): ComposeAnswer
|
||||
reply_type: "backend_error"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ import {
|
||||
FEATURE_ASSISTANT_CONTRACTS_V11,
|
||||
FEATURE_ASSISTANT_EVIDENCE_ENRICHMENT_V1,
|
||||
FEATURE_ASSISTANT_INVESTIGATION_STATE_V1,
|
||||
FEATURE_ASSISTANT_PROBLEM_CENTRIC_ANSWER_V1,
|
||||
FEATURE_ASSISTANT_PROBLEM_UNIT_CONTINUITY_V1,
|
||||
FEATURE_ASSISTANT_STATE_FOLLOWUP_BINDING_V1
|
||||
} from "../config";
|
||||
import { logJson } from "../utils/log";
|
||||
@@ -807,22 +809,28 @@ function hasAccountingSignal(text: string): boolean {
|
||||
if (/(?:^|[\s,;:])\d{2}(?:\.\d{2})?(?=$|[\s,.;:])/i.test(lower)) {
|
||||
return true;
|
||||
}
|
||||
return /(проводк|документ|реализац|поступлен|взаиморасчет|сальдо|остатк|счет|ндс|амортиз|рбп|ос|контрагент|поставщик|покупател|оплат|банк|выписк|склад|товар|материал|counterparty|supplier|invoice|posting|ledger|account|anomaly|risk)/i.test(
|
||||
return /(РїСЂРѕРІРѕРґРє|документ|реализац|поступлен|взаиморасчет|сальдо|остатк|счет|РЅРґСЃ|амортиз|СЂР±Рї|РѕСЃ|контрагент|поставщик|покупател|оплат|банк|выписк|склад|товар|материал|проводк|документ|реализац|поступлен|взаиморасчет|сальдо|остатк|счет|счёт|ндс|амортиз|рбп|контрагент|поставщик|покупател|оплат|банк|выписк|склад|товар|материал|закрыти|период|postavshchik|kontragent|schet|schetu|period|counterparty|supplier|invoice|posting|ledger|account|anomaly|risk)/i.test(
|
||||
lower
|
||||
);
|
||||
}
|
||||
|
||||
function hasFollowupMarker(text: string): boolean {
|
||||
const compact = compactWhitespace(text.toLowerCase());
|
||||
return /^(и|а еще|а ещё|еще|ещё|добав|уточн|продолж|также|plus|also|dobav|utochn|prodolzh)/i.test(compact);
|
||||
return /^(Рё|Р° еще|Р° ещё|еще|ещё|добав|уточн|продолж|также|и|а если|а еще|а ещё|еще|ещё|добав|уточн|продолж|также|plus|also|dobav|utochn|prodolzh)/i.test(
|
||||
compact
|
||||
);
|
||||
}
|
||||
|
||||
function hasReferentialPointer(text: string): boolean {
|
||||
return /(по этому|по тому|это же|этой|этим|тому|same thing|that one|po etomu|po tomu)/i.test(text.toLowerCase());
|
||||
return /(РїРѕ этому|РїРѕ тому|это Р¶Рµ|этой|этим|тому|по этому|по тому|это же|этой|этим|этому|из этого|в этом|тот же|same thing|that one|po etomu|po tomu)/i.test(
|
||||
text.toLowerCase()
|
||||
);
|
||||
}
|
||||
|
||||
function hasSmallTalkSignal(text: string): boolean {
|
||||
return /(привет|как дела|спасибо|thanks|thank you|hello|hi)\b/i.test(text.toLowerCase());
|
||||
return /(привет|как дела|спасибо|привет|как дела|спасибо|благодарю|thanks|thank you|hello|hi)\b/i.test(
|
||||
text.toLowerCase()
|
||||
);
|
||||
}
|
||||
|
||||
function countTokens(text: string): number {
|
||||
@@ -835,6 +843,44 @@ function hasPeriodLiteral(text: string): boolean {
|
||||
return /\b(20\d{2}(?:[-/.](?:0[1-9]|1[0-2]))?)\b/.test(text);
|
||||
}
|
||||
|
||||
function extractNormalizedPeriodLiteral(text: string): string | null {
|
||||
const monthly = text.match(/\b(20\d{2})[-/.](0[1-9]|1[0-2])\b/);
|
||||
if (monthly) {
|
||||
return `${monthly[1]}-${monthly[2]}`;
|
||||
}
|
||||
const yearly = text.match(/\b(20\d{2})\b/);
|
||||
if (yearly) {
|
||||
return yearly[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function hasStrongFollowupAnchors(
|
||||
userMessage: string,
|
||||
state: NonNullable<AssistantSessionState["investigation_state"]>
|
||||
): boolean {
|
||||
const explicitPeriod = extractNormalizedPeriodLiteral(userMessage);
|
||||
if (explicitPeriod && state.focus.period && explicitPeriod !== state.focus.period) {
|
||||
const periodLooksLikeFollowupRefinement = hasFollowupMarker(userMessage) || hasReferentialPointer(userMessage);
|
||||
if (!periodLooksLikeFollowupRefinement) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const explicitAccounts = extractAccountTokens(userMessage);
|
||||
if (explicitAccounts.length > 0) {
|
||||
const knownAccounts = new Set(state.focus.primary_accounts.map((item) => item.trim()));
|
||||
if (knownAccounts.size === 0) {
|
||||
return true;
|
||||
}
|
||||
if (explicitAccounts.some((item) => !knownAccounts.has(item))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function routeFromInvestigationState(state: NonNullable<AssistantSessionState["investigation_state"]>): RouteHint | null {
|
||||
const rawDomain = compactWhitespace(state.focus.domain ?? "");
|
||||
if (!rawDomain) {
|
||||
@@ -890,7 +936,17 @@ function buildFollowupStateBinding(input: {
|
||||
const referentialPointer = hasReferentialPointer(userMessage);
|
||||
const shortPrompt = countTokens(userMessage) <= 10;
|
||||
const smallTalkSignal = hasSmallTalkSignal(userMessage);
|
||||
const shouldBind = !smallTalkSignal && (followupMarker || referentialPointer || (!strongSignal && shortPrompt));
|
||||
const problemState = input.investigationState.problem_unit_state;
|
||||
const problemContinuityAvailable =
|
||||
FEATURE_ASSISTANT_PROBLEM_UNIT_CONTINUITY_V1 &&
|
||||
Boolean(problemState) &&
|
||||
((problemState?.active_problem_units.length ?? 0) > 0 || (problemState?.focus_problem_types.length ?? 0) > 0);
|
||||
const strongNewAnchorDetected = hasStrongFollowupAnchors(userMessage, input.investigationState);
|
||||
const periodRefinementFollowup = hasPeriodLiteral(userMessage) && problemContinuityAvailable;
|
||||
const shouldBind =
|
||||
!smallTalkSignal &&
|
||||
!strongNewAnchorDetected &&
|
||||
(followupMarker || referentialPointer || periodRefinementFollowup || (!strongSignal && shortPrompt));
|
||||
|
||||
if (!shouldBind) {
|
||||
return {
|
||||
@@ -903,6 +959,7 @@ function buildFollowupStateBinding(input: {
|
||||
const context: NormalizeRequestPayload["context"] = {
|
||||
...(input.payloadContext ?? {})
|
||||
};
|
||||
const hasExplicitExpectedRoute = Boolean(input.payloadContext?.expected_route);
|
||||
const expectedRouteFromState = !context?.expected_route ? routeFromInvestigationState(input.investigationState) : null;
|
||||
const periodHintFromState = !context?.period_hint ? input.investigationState.focus.period : null;
|
||||
|
||||
@@ -915,6 +972,9 @@ function buildFollowupStateBinding(input: {
|
||||
|
||||
const subject = withCappedLength(compactWhitespace(input.investigationState.focus.active_query_subject ?? ""), FOLLOWUP_SUBJECT_MAX);
|
||||
const businessContextPatch: string[] = ["followup_state_binding_v1"];
|
||||
let problemContinuityApplied = false;
|
||||
let problemContinuitySkippedReason: string | null = null;
|
||||
|
||||
if (input.investigationState.focus.period) {
|
||||
businessContextPatch.push("active_period");
|
||||
}
|
||||
@@ -924,6 +984,20 @@ function buildFollowupStateBinding(input: {
|
||||
if (input.investigationState.focus.primary_accounts.length > 0) {
|
||||
businessContextPatch.push(`focus_accounts:${input.investigationState.focus.primary_accounts.join(",")}`);
|
||||
}
|
||||
if (problemContinuityAvailable) {
|
||||
if (hasExplicitExpectedRoute) {
|
||||
problemContinuitySkippedReason = "explicit_expected_route";
|
||||
} else {
|
||||
const focusTypes = (problemState?.focus_problem_types ?? []).slice(0, 3);
|
||||
const activeCount = problemState?.active_problem_units.length ?? 0;
|
||||
businessContextPatch.push("problem_unit_continuity_v1");
|
||||
if (focusTypes.length > 0) {
|
||||
businessContextPatch.push(`problem_focus_types:${focusTypes.join(",")}`);
|
||||
}
|
||||
businessContextPatch.push(`problem_active_count:${activeCount}`);
|
||||
problemContinuityApplied = true;
|
||||
}
|
||||
}
|
||||
|
||||
const mergedBusinessContext = mergeBusinessContext(context?.business_context, businessContextPatch);
|
||||
if (mergedBusinessContext) {
|
||||
@@ -940,6 +1014,9 @@ function buildFollowupStateBinding(input: {
|
||||
if (periodHintFromState && !hasPeriodLiteral(userMessage)) {
|
||||
appendParts.push(`Период фокуса: ${periodHintFromState}`);
|
||||
}
|
||||
if (problemContinuityApplied && (problemState?.focus_problem_types.length ?? 0) > 0) {
|
||||
appendParts.push(`Problem focus types: ${(problemState?.focus_problem_types ?? []).slice(0, 3).join(", ")}`);
|
||||
}
|
||||
const appendBlock = withCappedLength(compactWhitespace(appendParts.join("; ")), FOLLOWUP_QUESTION_APPEND_MAX);
|
||||
normalizedQuestion = `${userMessage}\n${appendBlock}`.trim();
|
||||
}
|
||||
@@ -961,7 +1038,11 @@ function buildFollowupStateBinding(input: {
|
||||
period_hint_from_state: Boolean(periodHintFromState),
|
||||
expected_route_from_state: Boolean(expectedRouteFromState),
|
||||
business_context_from_state: Boolean(mergedBusinessContext),
|
||||
question_augmented: shouldAugmentQuestion
|
||||
question_augmented: shouldAugmentQuestion,
|
||||
problem_continuity_available: problemContinuityAvailable,
|
||||
problem_continuity_applied: problemContinuityApplied,
|
||||
problem_continuity_skipped_reason: problemContinuityApplied ? null : problemContinuitySkippedReason,
|
||||
strong_new_anchor_detected: strongNewAnchorDetected
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1121,7 +1202,8 @@ export class AssistantService {
|
||||
requirements: coverageEvaluation.requirements,
|
||||
coverageReport: coverageEvaluation.coverage,
|
||||
groundingCheck,
|
||||
enableAnswerPolicyV11: FEATURE_ASSISTANT_ANSWER_POLICY_V11
|
||||
enableAnswerPolicyV11: FEATURE_ASSISTANT_ANSWER_POLICY_V11,
|
||||
enableProblemCentricAnswerV1: FEATURE_ASSISTANT_PROBLEM_CENTRIC_ANSWER_V1
|
||||
});
|
||||
|
||||
const answerStructureV11 = FEATURE_ASSISTANT_CONTRACTS_V11
|
||||
@@ -1175,6 +1257,14 @@ export class AssistantService {
|
||||
answer_grounding_check: groundingCheck,
|
||||
dropped_intent_segments: extractDiscardedIntentSegments(normalized.normalized),
|
||||
...(followupBinding.usage ? { followup_state_usage: followupBinding.usage } : {}),
|
||||
problem_centric_answer_applied: composition.problem_centric_answer_applied ?? false,
|
||||
problem_units_used_count: composition.problem_units_used_count ?? 0,
|
||||
problem_answer_mode: composition.problem_answer_mode ?? "stage1_policy_v11",
|
||||
...(Array.isArray(composition.problem_unit_ids_used) && composition.problem_unit_ids_used.length > 0
|
||||
? {
|
||||
problem_unit_ids_used: composition.problem_unit_ids_used
|
||||
}
|
||||
: {}),
|
||||
answer_structure_v11: answerStructureV11,
|
||||
investigation_state_snapshot: investigationStateSnapshot,
|
||||
normalized: normalized.normalized
|
||||
@@ -1234,6 +1324,14 @@ export class AssistantService {
|
||||
clarification_target: coverageEvaluation.coverage.clarification_needed_for,
|
||||
dropped_intent_segments: extractDiscardedIntentSegments(normalized.normalized),
|
||||
...(followupBinding.usage ? { followup_state_usage: followupBinding.usage } : {}),
|
||||
problem_centric_answer_applied: composition.problem_centric_answer_applied ?? false,
|
||||
problem_units_used_count: composition.problem_units_used_count ?? 0,
|
||||
problem_answer_mode: composition.problem_answer_mode ?? "stage1_policy_v11",
|
||||
...(Array.isArray(composition.problem_unit_ids_used) && composition.problem_unit_ids_used.length > 0
|
||||
? {
|
||||
problem_unit_ids_used: composition.problem_unit_ids_used
|
||||
}
|
||||
: {}),
|
||||
answer_structure_v11: answerStructureV11,
|
||||
investigation_state_snapshot: investigationStateSnapshot,
|
||||
fallback_type: composition.fallback_type,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,9 +16,21 @@ import {
|
||||
INVESTIGATION_MAX_UNCERTAINTIES,
|
||||
INVESTIGATION_STATE_SCHEMA_VERSION
|
||||
} from "../types/stage1Contracts";
|
||||
import type {
|
||||
InvestigationProblemUnitState,
|
||||
InvestigationStateWithProblemUnits,
|
||||
ProblemUnit,
|
||||
ProblemUnitEntityBacklink
|
||||
} from "../types/stage2ProblemUnits";
|
||||
import {
|
||||
INVESTIGATION_MAX_ACTIVE_PROBLEM_UNITS,
|
||||
INVESTIGATION_MAX_FOCUS_PROBLEM_TYPES,
|
||||
INVESTIGATION_MAX_PROBLEM_UNIT_BACKLINKS,
|
||||
INVESTIGATION_MAX_RESOLVED_PROBLEM_UNITS
|
||||
} from "../types/stage2ProblemUnits";
|
||||
|
||||
interface UpdateInvestigationStateInput {
|
||||
previous: InvestigationState;
|
||||
previous: InvestigationStateWithProblemUnits;
|
||||
timestamp: string;
|
||||
questionId: string;
|
||||
userMessage: string;
|
||||
@@ -115,9 +127,142 @@ function collectOpenUncertainties(
|
||||
return capStrings([...requirementNotes, ...limitationNotes], INVESTIGATION_MAX_UNCERTAINTIES);
|
||||
}
|
||||
|
||||
export function cloneInvestigationState(state: InvestigationState | null): InvestigationState | null {
|
||||
if (!state) return null;
|
||||
function normalizeEntityBacklinks(values: ProblemUnitEntityBacklink[]): ProblemUnitEntityBacklink[] {
|
||||
const result: ProblemUnitEntityBacklink[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const item of values) {
|
||||
const entity = String(item.entity ?? "").trim();
|
||||
const id = String(item.id ?? "").trim();
|
||||
if (!entity || !id) {
|
||||
continue;
|
||||
}
|
||||
const key = `${entity}::${id}`;
|
||||
if (seen.has(key)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(key);
|
||||
result.push({
|
||||
entity,
|
||||
id
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function collectProblemUnits(retrievalResults: UnifiedRetrievalResult[]): ProblemUnit[] {
|
||||
return retrievalResults.flatMap((result) => result.problem_units ?? []);
|
||||
}
|
||||
|
||||
function capProblemUnitState(state: InvestigationProblemUnitState): InvestigationProblemUnitState {
|
||||
return {
|
||||
active_problem_units: capStrings(state.active_problem_units, INVESTIGATION_MAX_ACTIVE_PROBLEM_UNITS),
|
||||
resolved_problem_units: capStrings(state.resolved_problem_units, INVESTIGATION_MAX_RESOLVED_PROBLEM_UNITS),
|
||||
problem_unit_backlinks: state.problem_unit_backlinks
|
||||
.map((item) => ({
|
||||
problem_unit_id: String(item.problem_unit_id ?? "").trim(),
|
||||
entity_backlinks: normalizeEntityBacklinks(item.entity_backlinks ?? [])
|
||||
}))
|
||||
.filter((item) => Boolean(item.problem_unit_id) && item.entity_backlinks.length > 0)
|
||||
.slice(0, INVESTIGATION_MAX_PROBLEM_UNIT_BACKLINKS),
|
||||
focus_problem_types: capStrings(
|
||||
state.focus_problem_types.map((item) => String(item)),
|
||||
INVESTIGATION_MAX_FOCUS_PROBLEM_TYPES
|
||||
) as InvestigationProblemUnitState["focus_problem_types"]
|
||||
};
|
||||
}
|
||||
|
||||
function updateProblemUnitState(
|
||||
previous: InvestigationStateWithProblemUnits,
|
||||
retrievalResults: UnifiedRetrievalResult[]
|
||||
): InvestigationProblemUnitState | undefined {
|
||||
const previousState = previous.problem_unit_state;
|
||||
const currentProblemUnits = collectProblemUnits(retrievalResults);
|
||||
const currentIds = capStrings(
|
||||
currentProblemUnits.map((item) => String(item.problem_unit_id ?? "")),
|
||||
INVESTIGATION_MAX_ACTIVE_PROBLEM_UNITS
|
||||
);
|
||||
const currentTypes = capStrings(
|
||||
currentProblemUnits.map((item) => String(item.problem_unit_type ?? "")),
|
||||
INVESTIGATION_MAX_FOCUS_PROBLEM_TYPES
|
||||
) as InvestigationProblemUnitState["focus_problem_types"];
|
||||
|
||||
const currentBacklinksRaw = currentProblemUnits
|
||||
.filter((item) => currentIds.includes(item.problem_unit_id))
|
||||
.map((item) => ({
|
||||
problem_unit_id: item.problem_unit_id,
|
||||
entity_backlinks: normalizeEntityBacklinks(item.entity_backlinks ?? [])
|
||||
}))
|
||||
.filter((item) => item.entity_backlinks.length > 0);
|
||||
|
||||
const currentBacklinksById = new Map(
|
||||
currentBacklinksRaw.map((item) => [item.problem_unit_id, item.entity_backlinks] as const)
|
||||
);
|
||||
const previousBacklinksById = new Map(
|
||||
(previousState?.problem_unit_backlinks ?? []).map((item) => [item.problem_unit_id, item.entity_backlinks] as const)
|
||||
);
|
||||
|
||||
const active_problem_units =
|
||||
currentIds.length > 0
|
||||
? currentIds
|
||||
: capStrings(previousState?.active_problem_units ?? [], INVESTIGATION_MAX_ACTIVE_PROBLEM_UNITS);
|
||||
|
||||
const resolved_problem_units =
|
||||
currentIds.length > 0
|
||||
? capStrings(
|
||||
[
|
||||
...(previousState?.active_problem_units ?? []).filter((item) => !currentIds.includes(item)),
|
||||
...(previousState?.resolved_problem_units ?? [])
|
||||
],
|
||||
INVESTIGATION_MAX_RESOLVED_PROBLEM_UNITS
|
||||
)
|
||||
: capStrings(previousState?.resolved_problem_units ?? [], INVESTIGATION_MAX_RESOLVED_PROBLEM_UNITS);
|
||||
|
||||
const problem_unit_backlinks = active_problem_units
|
||||
.map((problemUnitId) => {
|
||||
const entity_backlinks = normalizeEntityBacklinks(
|
||||
currentBacklinksById.get(problemUnitId) ?? previousBacklinksById.get(problemUnitId) ?? []
|
||||
);
|
||||
if (entity_backlinks.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
problem_unit_id: problemUnitId,
|
||||
entity_backlinks
|
||||
};
|
||||
})
|
||||
.filter((item): item is NonNullable<typeof item> => item !== null)
|
||||
.slice(0, INVESTIGATION_MAX_PROBLEM_UNIT_BACKLINKS);
|
||||
|
||||
const focus_problem_types =
|
||||
currentTypes.length > 0
|
||||
? currentTypes
|
||||
: capStrings(
|
||||
(previousState?.focus_problem_types ?? []).map((item) => String(item)),
|
||||
INVESTIGATION_MAX_FOCUS_PROBLEM_TYPES
|
||||
) as InvestigationProblemUnitState["focus_problem_types"];
|
||||
|
||||
const nextState = capProblemUnitState({
|
||||
active_problem_units,
|
||||
resolved_problem_units,
|
||||
problem_unit_backlinks,
|
||||
focus_problem_types
|
||||
});
|
||||
|
||||
if (
|
||||
nextState.active_problem_units.length === 0 &&
|
||||
nextState.resolved_problem_units.length === 0 &&
|
||||
nextState.problem_unit_backlinks.length === 0 &&
|
||||
nextState.focus_problem_types.length === 0
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return nextState;
|
||||
}
|
||||
|
||||
export function cloneInvestigationState(state: InvestigationStateWithProblemUnits | null): InvestigationStateWithProblemUnits | null {
|
||||
if (!state) return null;
|
||||
const cloned: InvestigationStateWithProblemUnits = {
|
||||
...state,
|
||||
focus: {
|
||||
...state.focus,
|
||||
@@ -132,9 +277,24 @@ export function cloneInvestigationState(state: InvestigationState | null): Inves
|
||||
}
|
||||
: null
|
||||
};
|
||||
if (state.problem_unit_state) {
|
||||
cloned.problem_unit_state = capProblemUnitState({
|
||||
active_problem_units: [...state.problem_unit_state.active_problem_units],
|
||||
resolved_problem_units: [...state.problem_unit_state.resolved_problem_units],
|
||||
problem_unit_backlinks: state.problem_unit_state.problem_unit_backlinks.map((item) => ({
|
||||
problem_unit_id: item.problem_unit_id,
|
||||
entity_backlinks: [...item.entity_backlinks]
|
||||
})),
|
||||
focus_problem_types: [...state.problem_unit_state.focus_problem_types]
|
||||
});
|
||||
}
|
||||
return cloned;
|
||||
}
|
||||
|
||||
export function createEmptyInvestigationState(sessionId: string, timestamp = new Date().toISOString()): InvestigationState {
|
||||
export function createEmptyInvestigationState(
|
||||
sessionId: string,
|
||||
timestamp = new Date().toISOString()
|
||||
): InvestigationStateWithProblemUnits {
|
||||
return {
|
||||
schema_version: INVESTIGATION_STATE_SCHEMA_VERSION,
|
||||
session_id: sessionId,
|
||||
@@ -157,7 +317,7 @@ export function createEmptyInvestigationState(sessionId: string, timestamp = new
|
||||
};
|
||||
}
|
||||
|
||||
export function updateInvestigationState(input: UpdateInvestigationStateInput): InvestigationState {
|
||||
export function updateInvestigationState(input: UpdateInvestigationStateInput): InvestigationStateWithProblemUnits {
|
||||
const previous = input.previous;
|
||||
const focusFromMessage = capStrings(detectAccounts(input.userMessage), INVESTIGATION_MAX_PRIMARY_ACCOUNTS);
|
||||
const requirementIds = capStrings(
|
||||
@@ -165,6 +325,7 @@ export function updateInvestigationState(input: UpdateInvestigationStateInput):
|
||||
INVESTIGATION_MAX_REQUIREMENT_LINKS
|
||||
);
|
||||
const mainRequirement = input.requirements[0]?.requirement_text ?? input.userMessage;
|
||||
const problemUnitState = updateProblemUnitState(previous, input.retrievalResults);
|
||||
|
||||
return {
|
||||
schema_version: INVESTIGATION_STATE_SCHEMA_VERSION,
|
||||
@@ -194,6 +355,11 @@ export function updateInvestigationState(input: UpdateInvestigationStateInput):
|
||||
last_user_message: input.userMessage.slice(0, 240),
|
||||
referenced_requirement_ids: requirementIds
|
||||
},
|
||||
query_mode_hint: deriveQueryModeHint(input.routeSummary)
|
||||
query_mode_hint: deriveQueryModeHint(input.routeSummary),
|
||||
...(problemUnitState
|
||||
? {
|
||||
problem_unit_state: problemUnitState
|
||||
}
|
||||
: {})
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,592 @@
|
||||
import type { EvidenceItem } from "../types/stage1Contracts";
|
||||
import type {
|
||||
CandidateEvidenceItem,
|
||||
ProblemConfidence,
|
||||
ProblemScore,
|
||||
ProblemUnit,
|
||||
ProblemUnitEntityBacklink,
|
||||
ProblemUnitSummary,
|
||||
ProblemUnitType
|
||||
} from "../types/stage2ProblemUnits";
|
||||
import {
|
||||
CANDIDATE_EVIDENCE_SCHEMA_VERSION,
|
||||
PROBLEM_UNIT_SCHEMA_VERSION,
|
||||
PROBLEM_UNIT_SUMMARY_SCHEMA_VERSION
|
||||
} from "../types/stage2ProblemUnits";
|
||||
|
||||
type RetrievalResultType = "list" | "summary" | "object" | "chain" | "ranking";
|
||||
|
||||
interface AssembleProblemUnitsInput {
|
||||
route: string;
|
||||
result_type?: RetrievalResultType;
|
||||
evidence: EvidenceItem[];
|
||||
raw_entities?: Array<Record<string, unknown>>;
|
||||
summary?: Record<string, unknown>;
|
||||
risk_factors?: string[];
|
||||
selection_reason?: string[];
|
||||
business_interpretation?: string[];
|
||||
}
|
||||
|
||||
interface CandidateCluster {
|
||||
cluster_id: string;
|
||||
candidates: CandidateEvidenceItem[];
|
||||
}
|
||||
|
||||
interface SeverityResult {
|
||||
severity: ProblemScore;
|
||||
confidence: ProblemConfidence;
|
||||
}
|
||||
|
||||
interface CandidateBuildContext {
|
||||
route: string;
|
||||
result_type?: RetrievalResultType;
|
||||
raw_entities: Array<Record<string, unknown>>;
|
||||
summary_relation_patterns: string[];
|
||||
summary_anomaly_patterns: string[];
|
||||
risk_factors: string[];
|
||||
}
|
||||
|
||||
function toObject(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function uniqueStrings(values: string[]): string[] {
|
||||
return Array.from(new Set(values.map((item) => item.trim()).filter(Boolean)));
|
||||
}
|
||||
|
||||
function clampUnitScore(value: number): number {
|
||||
if (!Number.isFinite(value)) {
|
||||
return 0;
|
||||
}
|
||||
if (value <= 0) return 0;
|
||||
if (value >= 1) return 1;
|
||||
return Number(value.toFixed(2));
|
||||
}
|
||||
|
||||
function gradeForScore(score: number): "low" | "medium" | "high" {
|
||||
if (score >= 0.7) return "high";
|
||||
if (score >= 0.4) return "medium";
|
||||
return "low";
|
||||
}
|
||||
|
||||
function confidenceGradeForScore(score: number): "low" | "medium" | "high" {
|
||||
if (score >= 0.75) return "high";
|
||||
if (score >= 0.45) return "medium";
|
||||
return "low";
|
||||
}
|
||||
|
||||
function confidenceToScore(value: CandidateEvidenceItem["confidence_hint"]): number {
|
||||
if (value === "high") return 1;
|
||||
if (value === "medium") return 0.6;
|
||||
return 0.3;
|
||||
}
|
||||
|
||||
function valueFromPayload(item: EvidenceItem, key: string): string | null {
|
||||
const value = item.payload[key];
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? trimmed : null;
|
||||
}
|
||||
|
||||
function stringArrayFromUnknown(value: unknown): string[] {
|
||||
if (!Array.isArray(value)) {
|
||||
return [];
|
||||
}
|
||||
return uniqueStrings(value.map((entry) => String(entry)));
|
||||
}
|
||||
|
||||
function stringArrayFromPayload(item: EvidenceItem, key: string): string[] {
|
||||
return stringArrayFromUnknown(item.payload[key]);
|
||||
}
|
||||
|
||||
function extractSemanticProfile(summary: Record<string, unknown>): {
|
||||
relation_patterns: string[];
|
||||
anomaly_patterns: string[];
|
||||
} {
|
||||
const semanticProfile = toObject(summary.semantic_profile);
|
||||
return {
|
||||
relation_patterns: stringArrayFromUnknown(semanticProfile?.relation_patterns),
|
||||
anomaly_patterns: stringArrayFromUnknown(semanticProfile?.anomaly_patterns)
|
||||
};
|
||||
}
|
||||
|
||||
function resolveEntityOverlay(item: EvidenceItem, rawEntities: Array<Record<string, unknown>>): Record<string, unknown> | null {
|
||||
const sourceId = String(item.pointer.source.id ?? "").toLowerCase();
|
||||
if (!sourceId) {
|
||||
return null;
|
||||
}
|
||||
for (const entity of rawEntities) {
|
||||
const candidates = [
|
||||
String(entity.source_id ?? ""),
|
||||
String(entity.entity_id ?? ""),
|
||||
String(entity.id ?? "")
|
||||
]
|
||||
.map((entry) => entry.toLowerCase())
|
||||
.filter(Boolean);
|
||||
if (candidates.includes(sourceId)) {
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function detectRelationPatternHits(
|
||||
item: EvidenceItem,
|
||||
overlay: Record<string, unknown> | null,
|
||||
context: CandidateBuildContext
|
||||
): string[] {
|
||||
const hits: string[] = [];
|
||||
const failedEdge = valueFromPayload(item, "failed_expected_edge");
|
||||
if (failedEdge) {
|
||||
hits.push(`failed_edge:${failedEdge}`);
|
||||
}
|
||||
const expectedStep = valueFromPayload(item, "expected_next_step");
|
||||
if (expectedStep) {
|
||||
hits.push(`expected_step:${expectedStep}`);
|
||||
}
|
||||
const relationPattern = valueFromPayload(item, "relation_pattern");
|
||||
if (relationPattern) {
|
||||
hits.push(relationPattern);
|
||||
}
|
||||
|
||||
hits.push(...stringArrayFromPayload(item, "relation_patterns"));
|
||||
hits.push(...stringArrayFromPayload(item, "relation_pattern_hits"));
|
||||
|
||||
if (overlay) {
|
||||
hits.push(...stringArrayFromUnknown(overlay.relation_pattern_hits));
|
||||
hits.push(...stringArrayFromUnknown(overlay.relation_types));
|
||||
}
|
||||
|
||||
hits.push(...context.summary_relation_patterns);
|
||||
if (context.route === "hybrid_store_plus_live" && hits.length === 0) {
|
||||
hits.push("chain_scope");
|
||||
}
|
||||
|
||||
return uniqueStrings(hits);
|
||||
}
|
||||
|
||||
function detectAnomalyPatterns(
|
||||
item: EvidenceItem,
|
||||
overlay: Record<string, unknown> | null,
|
||||
context: CandidateBuildContext
|
||||
): string[] {
|
||||
const patterns: string[] = [];
|
||||
patterns.push(...stringArrayFromPayload(item, "anomaly_patterns"));
|
||||
patterns.push(...stringArrayFromPayload(item, "risk_factors"));
|
||||
patterns.push(...stringArrayFromPayload(item, "lifecycle_gaps"));
|
||||
patterns.push(...stringArrayFromPayload(item, "lifecycle_markers"));
|
||||
|
||||
const explicit = valueFromPayload(item, "anomaly_pattern");
|
||||
if (explicit) {
|
||||
patterns.push(explicit);
|
||||
}
|
||||
|
||||
if (overlay) {
|
||||
patterns.push(...stringArrayFromUnknown(overlay.risk_factors));
|
||||
patterns.push(...stringArrayFromUnknown(overlay.lifecycle_gaps));
|
||||
patterns.push(...stringArrayFromUnknown(overlay.anomaly_patterns));
|
||||
}
|
||||
|
||||
patterns.push(...context.summary_anomaly_patterns);
|
||||
patterns.push(...context.risk_factors);
|
||||
|
||||
if (item.evidence_kind === "anomaly_signal") {
|
||||
patterns.push("anomaly_signal");
|
||||
}
|
||||
if (item.limitation?.reason_code === "missing_mechanism") {
|
||||
patterns.push("missing_mechanism");
|
||||
}
|
||||
if (item.limitation?.reason_code === "insufficient_detail") {
|
||||
patterns.push("insufficient_detail");
|
||||
}
|
||||
|
||||
const defectClass = valueFromPayload(item, "business_defect_class");
|
||||
if (defectClass) {
|
||||
patterns.push(defectClass);
|
||||
}
|
||||
|
||||
if (context.route === "store_feature_risk") {
|
||||
patterns.push("risk_route");
|
||||
}
|
||||
|
||||
return uniqueStrings(patterns);
|
||||
}
|
||||
|
||||
function buildEntityBacklinks(item: EvidenceItem, overlay: Record<string, unknown> | null): ProblemUnitEntityBacklink[] {
|
||||
const backlinks: ProblemUnitEntityBacklink[] = [];
|
||||
const sourceEntity = String(item.pointer.source.entity ?? "").trim();
|
||||
const sourceId = String(item.pointer.source.id ?? "").trim();
|
||||
if (sourceEntity && sourceId) {
|
||||
backlinks.push({
|
||||
entity: sourceEntity,
|
||||
id: sourceId
|
||||
});
|
||||
}
|
||||
|
||||
const payloadEntity = valueFromPayload(item, "source_entity");
|
||||
const payloadId = valueFromPayload(item, "source_id");
|
||||
if (payloadEntity && payloadId) {
|
||||
backlinks.push({
|
||||
entity: payloadEntity,
|
||||
id: payloadId
|
||||
});
|
||||
}
|
||||
|
||||
const overlayEntity = overlay ? String(overlay.source_entity ?? "").trim() : "";
|
||||
const overlayId = overlay ? String(overlay.source_id ?? "").trim() : "";
|
||||
if (overlayEntity && overlayId) {
|
||||
backlinks.push({
|
||||
entity: overlayEntity,
|
||||
id: overlayId
|
||||
});
|
||||
}
|
||||
|
||||
return Array.from(
|
||||
new Map(backlinks.map((entry) => [`${entry.entity.toLowerCase()}|${entry.id.toLowerCase()}`, entry])).values()
|
||||
);
|
||||
}
|
||||
|
||||
function inferExpectedState(item: EvidenceItem): string | undefined {
|
||||
return valueFromPayload(item, "expected_state") ?? valueFromPayload(item, "expected_next_step") ?? undefined;
|
||||
}
|
||||
|
||||
function inferActualState(item: EvidenceItem): string | undefined {
|
||||
return valueFromPayload(item, "actual_state") ?? valueFromPayload(item, "mechanism_of_failure") ?? undefined;
|
||||
}
|
||||
|
||||
export function buildCandidateEvidence(
|
||||
items: EvidenceItem[],
|
||||
route: string,
|
||||
contextInput?: Partial<CandidateBuildContext>
|
||||
): CandidateEvidenceItem[] {
|
||||
const context: CandidateBuildContext = {
|
||||
route,
|
||||
result_type: contextInput?.result_type,
|
||||
raw_entities: contextInput?.raw_entities ?? [],
|
||||
summary_relation_patterns: contextInput?.summary_relation_patterns ?? [],
|
||||
summary_anomaly_patterns: contextInput?.summary_anomaly_patterns ?? [],
|
||||
risk_factors: contextInput?.risk_factors ?? []
|
||||
};
|
||||
|
||||
return items.map((item, index) => {
|
||||
const overlay = resolveEntityOverlay(item, context.raw_entities);
|
||||
return {
|
||||
schema_version: CANDIDATE_EVIDENCE_SCHEMA_VERSION,
|
||||
candidate_id: `cand-${item.evidence_id || `${route}-${index + 1}`}`,
|
||||
route,
|
||||
source_ref: item.source_ref,
|
||||
expected_state: inferExpectedState(item),
|
||||
actual_state: inferActualState(item),
|
||||
relation_pattern_hits: detectRelationPatternHits(item, overlay, context),
|
||||
anomaly_patterns: detectAnomalyPatterns(item, overlay, context),
|
||||
entity_backlinks: buildEntityBacklinks(item, overlay),
|
||||
confidence_hint: item.confidence
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function clusterSignature(candidate: CandidateEvidenceItem): string {
|
||||
const relation = candidate.relation_pattern_hits[0] ?? "none";
|
||||
const anomaly = candidate.anomaly_patterns[0] ?? "none";
|
||||
return [candidate.route, candidate.source_ref.canonical_ref, relation, anomaly].join("|");
|
||||
}
|
||||
|
||||
export function clusterCandidateEvidence(candidates: CandidateEvidenceItem[]): CandidateCluster[] {
|
||||
const byCluster = new Map<string, CandidateEvidenceItem[]>();
|
||||
for (const candidate of candidates) {
|
||||
const signature = clusterSignature(candidate);
|
||||
const current = byCluster.get(signature) ?? [];
|
||||
current.push(candidate);
|
||||
byCluster.set(signature, current);
|
||||
}
|
||||
return Array.from(byCluster.entries()).map(([cluster_id, clusterCandidates]) => ({
|
||||
cluster_id,
|
||||
candidates: clusterCandidates
|
||||
}));
|
||||
}
|
||||
|
||||
function hasAny(value: string, pattern: RegExp): boolean {
|
||||
return pattern.test(value);
|
||||
}
|
||||
|
||||
export function detectProblemUnitType(cluster: CandidateCluster): ProblemUnitType {
|
||||
const relationText = cluster.candidates.flatMap((item) => item.relation_pattern_hits).join(" ").toLowerCase();
|
||||
const anomalyText = cluster.candidates.flatMap((item) => item.anomaly_patterns).join(" ").toLowerCase();
|
||||
const sourceText = cluster.candidates
|
||||
.map((item) => `${item.source_ref.entity} ${item.source_ref.id}`)
|
||||
.join(" ")
|
||||
.toLowerCase();
|
||||
const routeText = cluster.candidates.map((item) => item.route).join(" ").toLowerCase();
|
||||
|
||||
if (hasAny(`${anomalyText} ${sourceText}`, /cross[_\s-]?branch|vat|nds|tax|ндс/)) {
|
||||
return "cross_branch_inconsistency_cluster";
|
||||
}
|
||||
if (hasAny(`${anomalyText} ${relationText}`, /period|close[_\s-]?risk|reporting|закрыт|period_close/)) {
|
||||
return "period_risk_cluster";
|
||||
}
|
||||
if (hasAny(anomalyText, /settlement|tail|unresolved|хвост|незакрыт/)) {
|
||||
return "unresolved_settlement_cluster";
|
||||
}
|
||||
if (hasAny(anomalyText, /lifecycle|deferred|broken_lifecycle|списани|амортиз|рбп/)) {
|
||||
return "lifecycle_anomaly_node";
|
||||
}
|
||||
if (
|
||||
hasAny(relationText, /failed_edge|statement_to_document|payment_to_settlement|chain|broken|цепоч|разрыв/)
|
||||
|| (routeText.includes("hybrid_store_plus_live") && relationText.length > 0)
|
||||
) {
|
||||
return "broken_chain_segment";
|
||||
}
|
||||
return "document_conflict";
|
||||
}
|
||||
|
||||
export function scoreProblemSeverity(cluster: CandidateCluster): SeverityResult {
|
||||
const candidates = cluster.candidates;
|
||||
const averageConfidence =
|
||||
candidates.length > 0
|
||||
? candidates.reduce((acc, item) => acc + confidenceToScore(item.confidence_hint), 0) / candidates.length
|
||||
: 0;
|
||||
const hasEdgeBreak = candidates.some((item) =>
|
||||
item.relation_pattern_hits.some((pattern) => /failed_edge|chain|statement_to_document|payment_to_settlement/i.test(pattern))
|
||||
);
|
||||
const hasAnomaly = candidates.some((item) => item.anomaly_patterns.length > 0);
|
||||
const hasPeriodRisk = candidates.some((item) => item.anomaly_patterns.some((pattern) => /period|close|reporting|закрыт/i.test(pattern)));
|
||||
const candidateBoost = Math.min(candidates.length, 5) * 0.08;
|
||||
|
||||
let severityScore = 0.25 + candidateBoost;
|
||||
if (hasEdgeBreak) severityScore += 0.2;
|
||||
if (hasAnomaly) severityScore += 0.15;
|
||||
if (hasPeriodRisk) severityScore += 0.1;
|
||||
const normalizedSeverity = clampUnitScore(severityScore);
|
||||
|
||||
let confidenceScore = averageConfidence;
|
||||
if (hasEdgeBreak) confidenceScore += 0.05;
|
||||
const normalizedConfidence = clampUnitScore(confidenceScore);
|
||||
|
||||
return {
|
||||
severity: {
|
||||
score: normalizedSeverity,
|
||||
grade: gradeForScore(normalizedSeverity)
|
||||
},
|
||||
confidence: {
|
||||
score: normalizedConfidence,
|
||||
grade: confidenceGradeForScore(normalizedConfidence)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function unitTitle(type: ProblemUnitType): string {
|
||||
if (type === "document_conflict") return "Document conflict detected";
|
||||
if (type === "broken_chain_segment") return "Broken chain segment detected";
|
||||
if (type === "lifecycle_anomaly_node") return "Lifecycle anomaly node detected";
|
||||
if (type === "unresolved_settlement_cluster") return "Unresolved settlement cluster detected";
|
||||
if (type === "period_risk_cluster") return "Period risk cluster detected";
|
||||
return "Cross-branch inconsistency cluster detected";
|
||||
}
|
||||
|
||||
function mechanismSummary(cluster: CandidateCluster, type: ProblemUnitType): string {
|
||||
const relationHints = uniqueStrings(cluster.candidates.flatMap((item) => item.relation_pattern_hits));
|
||||
const anomalyHints = uniqueStrings(cluster.candidates.flatMap((item) => item.anomaly_patterns));
|
||||
const primaryRelation = relationHints[0];
|
||||
const primaryAnomaly = anomalyHints[0];
|
||||
|
||||
if (primaryRelation) {
|
||||
return `Mechanism candidate: ${primaryRelation}.`;
|
||||
}
|
||||
if (primaryAnomaly) {
|
||||
return `Mechanism inferred from anomaly pattern: ${primaryAnomaly}.`;
|
||||
}
|
||||
return `Mechanism is currently inferred at baseline level for ${type}.`;
|
||||
}
|
||||
|
||||
function businessDefectClass(cluster: CandidateCluster, type: ProblemUnitType): string {
|
||||
const patterns = uniqueStrings([
|
||||
...cluster.candidates.flatMap((item) => item.relation_pattern_hits),
|
||||
...cluster.candidates.flatMap((item) => item.anomaly_patterns)
|
||||
]);
|
||||
return patterns[0] ?? type;
|
||||
}
|
||||
|
||||
function collectAffectedByEntity(backlinks: ProblemUnitEntityBacklink[], pattern: RegExp): string[] {
|
||||
return uniqueStrings(
|
||||
backlinks.filter((entry) => pattern.test(entry.entity)).map((entry) => `${entry.entity}:${entry.id}`)
|
||||
);
|
||||
}
|
||||
|
||||
function parseFailedExpectedEdge(cluster: CandidateCluster): string | undefined {
|
||||
const withEdge = cluster.candidates.flatMap((item) => item.relation_pattern_hits).find((pattern) => pattern.startsWith("failed_edge:"));
|
||||
if (!withEdge) {
|
||||
return undefined;
|
||||
}
|
||||
return withEdge.replace(/^failed_edge:/, "").trim() || undefined;
|
||||
}
|
||||
|
||||
function mergeBacklinks(candidates: CandidateEvidenceItem[]): ProblemUnitEntityBacklink[] {
|
||||
return Array.from(
|
||||
new Map(
|
||||
candidates
|
||||
.flatMap((item) => item.entity_backlinks)
|
||||
.map((entry) => [`${entry.entity.toLowerCase()}|${entry.id.toLowerCase()}`, entry] as const)
|
||||
).values()
|
||||
);
|
||||
}
|
||||
|
||||
export function buildProblemUnit(cluster: CandidateCluster, index: number): ProblemUnit {
|
||||
const type = detectProblemUnitType(cluster);
|
||||
const scored = scoreProblemSeverity(cluster);
|
||||
const backlinks = mergeBacklinks(cluster.candidates);
|
||||
const expectedState = cluster.candidates.find((item) => typeof item.expected_state === "string")?.expected_state;
|
||||
const actualState = cluster.candidates.find((item) => typeof item.actual_state === "string")?.actual_state;
|
||||
const failedExpectedEdge = parseFailedExpectedEdge(cluster);
|
||||
const periodSensitive = cluster.candidates.some((item) =>
|
||||
item.anomaly_patterns.some((pattern) => /period|close|reporting|закрыт/i.test(pattern))
|
||||
);
|
||||
const hasLowConfidence = cluster.candidates.some((item) => item.confidence_hint === "low");
|
||||
|
||||
return {
|
||||
schema_version: PROBLEM_UNIT_SCHEMA_VERSION,
|
||||
problem_unit_id: `pu-${type}-${index + 1}`,
|
||||
problem_unit_type: type,
|
||||
title: unitTitle(type),
|
||||
mechanism_summary: mechanismSummary(cluster, type),
|
||||
business_defect_class: businessDefectClass(cluster, type),
|
||||
severity: scored.severity,
|
||||
confidence: scored.confidence,
|
||||
affected_entities: uniqueStrings(backlinks.map((entry) => `${entry.entity}:${entry.id}`)),
|
||||
affected_documents: collectAffectedByEntity(backlinks, /doc|document|invoice|плат|реал|поступ/i),
|
||||
affected_postings: collectAffectedByEntity(backlinks, /posting|journal|провод/i),
|
||||
affected_accounts: collectAffectedByEntity(backlinks, /account|счет|сч/i),
|
||||
affected_counterparties: collectAffectedByEntity(backlinks, /counterparty|supplier|buyer|контраг|постав|покуп/i),
|
||||
affected_contracts: collectAffectedByEntity(backlinks, /contract|договор/i),
|
||||
...(expectedState ? { expected_state: expectedState } : {}),
|
||||
...(actualState ? { actual_state: actualState } : {}),
|
||||
...(failedExpectedEdge ? { failed_expected_edge: failedExpectedEdge } : {}),
|
||||
...(periodSensitive
|
||||
? {
|
||||
period_impact: {
|
||||
is_period_sensitive: true,
|
||||
impact_class: "close_risk" as const
|
||||
}
|
||||
}
|
||||
: {}),
|
||||
evidence_pack: uniqueStrings(cluster.candidates.map((item) => item.candidate_id)),
|
||||
entity_backlinks: backlinks,
|
||||
snapshot_limitations: uniqueStrings(
|
||||
hasLowConfidence ? ["low_confidence_candidates_present"] : []
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
function collapseSignature(unit: ProblemUnit): string {
|
||||
const backlink = unit.entity_backlinks[0] ? `${unit.entity_backlinks[0].entity}|${unit.entity_backlinks[0].id}` : "none";
|
||||
return [unit.problem_unit_type, unit.business_defect_class, unit.failed_expected_edge ?? "none", backlink].join("|");
|
||||
}
|
||||
|
||||
export function collapseDuplicates(units: ProblemUnit[]): {
|
||||
problem_units: ProblemUnit[];
|
||||
duplicate_collapses: number;
|
||||
} {
|
||||
const bySignature = new Map<string, ProblemUnit>();
|
||||
let duplicateCollapses = 0;
|
||||
|
||||
for (const unit of units) {
|
||||
const signature = collapseSignature(unit);
|
||||
const existing = bySignature.get(signature);
|
||||
if (!existing) {
|
||||
bySignature.set(signature, unit);
|
||||
continue;
|
||||
}
|
||||
|
||||
duplicateCollapses += 1;
|
||||
bySignature.set(signature, {
|
||||
...existing,
|
||||
evidence_pack: uniqueStrings([...existing.evidence_pack, ...unit.evidence_pack]),
|
||||
entity_backlinks: Array.from(
|
||||
new Map(
|
||||
[...existing.entity_backlinks, ...unit.entity_backlinks].map((entry) => [
|
||||
`${entry.entity.toLowerCase()}|${entry.id.toLowerCase()}`,
|
||||
entry
|
||||
])
|
||||
).values()
|
||||
),
|
||||
affected_entities: uniqueStrings([...existing.affected_entities, ...unit.affected_entities]),
|
||||
affected_documents: uniqueStrings([...existing.affected_documents, ...unit.affected_documents]),
|
||||
affected_postings: uniqueStrings([...existing.affected_postings, ...unit.affected_postings]),
|
||||
affected_accounts: uniqueStrings([...existing.affected_accounts, ...unit.affected_accounts]),
|
||||
affected_counterparties: uniqueStrings([...existing.affected_counterparties, ...unit.affected_counterparties]),
|
||||
affected_contracts: uniqueStrings([...existing.affected_contracts, ...unit.affected_contracts]),
|
||||
snapshot_limitations: uniqueStrings([...existing.snapshot_limitations, ...unit.snapshot_limitations]),
|
||||
severity: unit.severity.score > existing.severity.score ? unit.severity : existing.severity,
|
||||
confidence: unit.confidence.score > existing.confidence.score ? unit.confidence : existing.confidence
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
problem_units: Array.from(bySignature.values()),
|
||||
duplicate_collapses: duplicateCollapses
|
||||
};
|
||||
}
|
||||
|
||||
function buildSummary(units: ProblemUnit[], duplicateCollapses: number): ProblemUnitSummary {
|
||||
const unitTypes = uniqueStrings(units.map((item) => item.problem_unit_type)) as ProblemUnitType[];
|
||||
const typeDistribution: Partial<Record<ProblemUnitType, number>> = {};
|
||||
const severityDistribution: Record<"low" | "medium" | "high", number> = {
|
||||
low: 0,
|
||||
medium: 0,
|
||||
high: 0
|
||||
};
|
||||
const confidenceDistribution: Record<"low" | "medium" | "high", number> = {
|
||||
low: 0,
|
||||
medium: 0,
|
||||
high: 0
|
||||
};
|
||||
|
||||
for (const unit of units) {
|
||||
typeDistribution[unit.problem_unit_type] = (typeDistribution[unit.problem_unit_type] ?? 0) + 1;
|
||||
severityDistribution[unit.severity.grade] += 1;
|
||||
confidenceDistribution[unit.confidence.grade] += 1;
|
||||
}
|
||||
|
||||
return {
|
||||
schema_version: PROBLEM_UNIT_SUMMARY_SCHEMA_VERSION,
|
||||
units_total: units.length,
|
||||
duplicate_collapses: duplicateCollapses,
|
||||
unit_types: unitTypes,
|
||||
type_distribution: typeDistribution,
|
||||
severity_distribution: severityDistribution,
|
||||
confidence_distribution: confidenceDistribution,
|
||||
primary_unit_type: units[0]?.problem_unit_type ?? null
|
||||
};
|
||||
}
|
||||
|
||||
export function assembleProblemUnits(input: AssembleProblemUnitsInput): {
|
||||
candidate_evidence: CandidateEvidenceItem[];
|
||||
problem_units: ProblemUnit[];
|
||||
problem_unit_summary: ProblemUnitSummary;
|
||||
} {
|
||||
const summary = input.summary ?? {};
|
||||
const semanticProfile = extractSemanticProfile(summary);
|
||||
|
||||
const candidates = buildCandidateEvidence(input.evidence, input.route, {
|
||||
route: input.route,
|
||||
result_type: input.result_type,
|
||||
raw_entities: input.raw_entities ?? [],
|
||||
summary_relation_patterns: semanticProfile.relation_patterns,
|
||||
summary_anomaly_patterns: semanticProfile.anomaly_patterns,
|
||||
risk_factors: uniqueStrings(input.risk_factors ?? [])
|
||||
});
|
||||
const clusters = clusterCandidateEvidence(candidates);
|
||||
const units = clusters.map((cluster, index) => buildProblemUnit(cluster, index));
|
||||
const collapsed = collapseDuplicates(units);
|
||||
|
||||
return {
|
||||
candidate_evidence: candidates,
|
||||
problem_units: collapsed.problem_units,
|
||||
problem_unit_summary: buildSummary(collapsed.problem_units, collapsed.duplicate_collapses)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import type {
|
||||
RetrievalResultType,
|
||||
UnifiedRetrievalResult
|
||||
} from "../types/assistant";
|
||||
import { FEATURE_ASSISTANT_EVIDENCE_ENRICHMENT_V1 } from "../config";
|
||||
import { FEATURE_ASSISTANT_EVIDENCE_ENRICHMENT_V1, FEATURE_ASSISTANT_PROBLEM_UNITS_V1 } from "../config";
|
||||
import { EVIDENCE_SOURCE_REF_SCHEMA_VERSION } from "../types/stage1Contracts";
|
||||
import type {
|
||||
EvidenceConfidence,
|
||||
@@ -14,6 +14,7 @@ import type {
|
||||
EvidencePointer,
|
||||
EvidenceSourceRef
|
||||
} from "../types/stage1Contracts";
|
||||
import { assembleProblemUnits } from "./problemUnitAssembler";
|
||||
|
||||
interface RawRetrievalResult {
|
||||
status?: string;
|
||||
@@ -94,6 +95,29 @@ function normalizeStringArray(value: unknown): string[] {
|
||||
return value.map((item) => String(item));
|
||||
}
|
||||
|
||||
function mergeSummaryWithProblemUnitMeta(
|
||||
summary: Record<string, unknown>,
|
||||
input: {
|
||||
candidateEvidenceCount: number;
|
||||
problemUnitsCount: number;
|
||||
unitTypes: string[];
|
||||
duplicateCollapses: number;
|
||||
severityDistribution: Record<string, number>;
|
||||
confidenceDistribution: Record<string, number>;
|
||||
}
|
||||
): Record<string, unknown> {
|
||||
return {
|
||||
...summary,
|
||||
problem_units_enabled: true,
|
||||
candidate_evidence_count: input.candidateEvidenceCount,
|
||||
problem_units_count: input.problemUnitsCount,
|
||||
problem_unit_types: input.unitTypes,
|
||||
problem_unit_duplicate_collapses: input.duplicateCollapses,
|
||||
problem_unit_severity_distribution: input.severityDistribution,
|
||||
problem_unit_confidence_distribution: input.confidenceDistribution
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeConfidence(value: unknown): RetrievalConfidence {
|
||||
if (value === "high" || value === "medium" || value === "low") {
|
||||
return value;
|
||||
@@ -459,15 +483,19 @@ export function normalizeRetrievalResult(
|
||||
route: string,
|
||||
raw: RawRetrievalResult
|
||||
): UnifiedRetrievalResult {
|
||||
return {
|
||||
const items = normalizeObjectArray(raw.items);
|
||||
const summary = normalizeSummary(raw.summary);
|
||||
const evidence = normalizeEvidenceItems(fragmentId, requirementIds, route, raw.evidence);
|
||||
|
||||
const baseResult: UnifiedRetrievalResult = {
|
||||
fragment_id: fragmentId,
|
||||
requirement_ids: requirementIds,
|
||||
route,
|
||||
status: normalizeStatus(raw.status),
|
||||
result_type: normalizeResultType(raw.result_type),
|
||||
items: normalizeObjectArray(raw.items),
|
||||
summary: normalizeSummary(raw.summary),
|
||||
evidence: normalizeEvidenceItems(fragmentId, requirementIds, route, raw.evidence),
|
||||
items,
|
||||
summary,
|
||||
evidence,
|
||||
why_included: normalizeStringArray(raw.why_included),
|
||||
selection_reason: normalizeStringArray(raw.selection_reason),
|
||||
risk_factors: normalizeStringArray(raw.risk_factors),
|
||||
@@ -476,4 +504,37 @@ export function normalizeRetrievalResult(
|
||||
limitations: normalizeStringArray(raw.limitations),
|
||||
errors: normalizeErrors(raw.errors)
|
||||
};
|
||||
|
||||
if (!FEATURE_ASSISTANT_PROBLEM_UNITS_V1) {
|
||||
return baseResult;
|
||||
}
|
||||
|
||||
const assembled = assembleProblemUnits({
|
||||
route,
|
||||
result_type: baseResult.result_type,
|
||||
evidence,
|
||||
raw_entities: items,
|
||||
summary,
|
||||
risk_factors: baseResult.risk_factors,
|
||||
selection_reason: baseResult.selection_reason,
|
||||
business_interpretation: baseResult.business_interpretation
|
||||
});
|
||||
|
||||
const enrichedSummary = mergeSummaryWithProblemUnitMeta(summary, {
|
||||
candidateEvidenceCount: assembled.candidate_evidence.length,
|
||||
problemUnitsCount: assembled.problem_units.length,
|
||||
unitTypes: assembled.problem_unit_summary.unit_types,
|
||||
duplicateCollapses: assembled.problem_unit_summary.duplicate_collapses,
|
||||
severityDistribution: assembled.problem_unit_summary.severity_distribution,
|
||||
confidenceDistribution: assembled.problem_unit_summary.confidence_distribution
|
||||
});
|
||||
|
||||
return {
|
||||
...baseResult,
|
||||
summary: enrichedSummary,
|
||||
raw_entities: items,
|
||||
candidate_evidence: assembled.candidate_evidence,
|
||||
problem_units: assembled.problem_units,
|
||||
problem_unit_summary: assembled.problem_unit_summary
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user