АРЧ АП11 - Архитектура после ге :

This commit is contained in:
2026-04-17 23:49:21 +03:00
parent 8f9364e7c9
commit a5ea9adf53
72 changed files with 7353 additions and 4027 deletions
@@ -1,6 +1,48 @@
// @ts-nocheck
export function createAssistantTransitionPolicy(deps) {
function hasInventoryRootRestatementLikeSignal(userMessage, sourceIntentHint, hasInventoryRootFrame) {
if (!hasInventoryRootFrame) {
return false;
}
const normalized = deps
.compactWhitespace(deps.repairAddressMojibake(String(userMessage ?? "")).toLowerCase())
.replace(/ё/g, "е");
if (!normalized) {
return false;
}
if (deps.countTokens(normalized) > 10) {
return false;
}
const hasInventoryLexeme = /(?:остат|склад|товар|номенклатур|позиц)/iu.test(normalized);
const hasRestatementCue =
/(?:еще\s+раз|снова|повтори|повтори\s+еще\s+раз|верни|покажи)/iu.test(normalized) &&
/(?:на\s+ту\s+же\s+дат[ауеы]|на\s+эту\s+же\s+дат[ауеы]|на\s+эту\s+дат[ауеы]|эту\s+дат[ауеы]|та\s+же\s+дата|тот\s+же\s+период|этот\s+же\s+период)/iu.test(
normalized
);
const hasBareSnapshotSameDateCue =
hasInventoryLexeme &&
/(?:РЅР°\s+ту\s+РРµ\s+дат[ауеы]|РЅР°\s+эту\s+РРµ\s+дат[ауеы]|РЅР°\s+эту\s+дат[ауеы]|эту\s+дат[ауеы]|та\s+РРµ\s+дата|тот\s+РРµ\s+период|этот\s+РРµ\s+период)/iu.test(
normalized
);
const bareSnapshotSameDatePhrases = [
"\u043d\u0430 \u0442\u0443 \u0436\u0435 \u0434\u0430\u0442",
"\u043d\u0430 \u044d\u0442\u0443 \u0436\u0435 \u0434\u0430\u0442",
"\u043d\u0430 \u044d\u0442\u0443 \u0434\u0430\u0442",
"\u044d\u0442\u0443 \u0434\u0430\u0442",
"\u0442\u0430 \u0436\u0435 \u0434\u0430\u0442\u0430",
"\u0442\u043e\u0442 \u0436\u0435 \u043f\u0435\u0440\u0438\u043e\u0434",
"\u044d\u0442\u043e\u0442 \u0436\u0435 \u043f\u0435\u0440\u0438\u043e\u0434"
];
const hasBareSnapshotSameDatePhraseCue =
hasInventoryLexeme && bareSnapshotSameDatePhrases.some((phrase) => normalized.includes(phrase));
return (
hasInventoryLexeme &&
(hasRestatementCue || hasBareSnapshotSameDateCue || hasBareSnapshotSameDatePhraseCue) &&
!deps.hasForeignAccountingPivotOverInventoryMessage(normalized)
);
}
function shouldKeepPreviousIntentForShortCounterpartyRetarget(userMessage, sourceIntent) {
const normalized = deps.compactWhitespace(
deps.repairAddressMojibake(String(userMessage ?? "")).toLowerCase()
@@ -21,6 +63,71 @@ export function createAssistantTransitionPolicy(deps) {
return /^(?:Р°|Рё|РЅСѓ)?\s*РїРѕ\s+[a-zР°-СЏС‘0-9._-]{2,}(?:\s+[a-zР°-СЏС‘0-9._-]{2,})?$/iu.test(normalized);
}
function shouldKeepPreviousIntentForShortCounterpartyRetargetV2(userMessage, sourceIntent) {
const normalized = deps.compactWhitespace(
deps.repairAddressMojibake(String(userMessage ?? "")).toLowerCase()
);
if (!normalized || deps.countTokens(normalized) > 4) {
return false;
}
if (sourceIntent !== "list_documents_by_counterparty" && sourceIntent !== "list_documents_by_contract") {
return false;
}
if (/(?:bank|payment|wire|statement)/iu.test(normalized)) {
return false;
}
return /^(?:а|и|ну)?\s*(?:покажи\s+)?по\s+[a-zа-яё0-9._-]{2,}(?:\s+[a-zа-яё0-9._-]{2,})?$/iu.test(normalized);
}
function inferStandaloneAddressTopicFamily(userMessage) {
const normalized = deps.compactWhitespace(
deps.repairAddressMojibake(String(userMessage ?? "")).toLowerCase()
);
if (!normalized) {
return null;
}
if (/(?:ндс|vat)/iu.test(normalized)) {
return "vat";
}
if (/(?:остат|склад|товар|номенклатур|позици)/iu.test(normalized)) {
return "inventory";
}
return null;
}
function resolveDisplayedEntityRetargetIntent(userMessage, entityType) {
const normalized = deps.compactWhitespace(
deps.repairAddressMojibake(String(userMessage ?? "")).toLowerCase()
);
if (!normalized) {
return null;
}
if (entityType === "counterparty") {
if (/(?:договор|контракт)/iu.test(normalized)) {
return "list_contracts_by_counterparty";
}
if (/(?:банк|выписк|плат[её]ж|оплат|statement|payment|wire)/iu.test(normalized)) {
return "bank_operations_by_counterparty";
}
if (/(?:документ|накладн|счет|сч[её]т|акт|реализац|поступл)/iu.test(normalized)) {
return "list_documents_by_counterparty";
}
if (/(?:сколько\s+денег|сколько\s+принес|выручк|сумм[аы]?|оплатил|продаж)/iu.test(normalized)) {
return "customer_revenue_and_payments";
}
return null;
}
if (entityType === "contract") {
if (/(?:банк|выписк|плат[её]ж|оплат|statement|payment|wire)/iu.test(normalized)) {
return "bank_operations_by_contract";
}
if (/(?:документ|накладн|счет|сч[её]т|акт|реализац|поступл)/iu.test(normalized)) {
return "list_documents_by_contract";
}
}
return null;
}
function resolveAddressFollowupCarryoverContext(
userMessage,
items,
@@ -108,6 +215,18 @@ export function createAssistantTransitionPolicy(deps) {
Boolean(recentInventoryRootFrame)
)
: false;
const hasInventoryRootRestatementPrimary = hasInventoryRootRestatementLikeSignal(
userMessage,
sourceIntentHint,
Boolean(recentInventoryRootFrame)
);
const hasInventoryRootRestatementAlternate = deps.toNonEmptyString(alternateMessage)
? hasInventoryRootRestatementLikeSignal(
String(alternateMessage ?? ""),
sourceIntentHint,
Boolean(recentInventoryRootFrame)
)
: false;
let hasStrongFollowupReference =
hasPrimaryIndexReferenceSignal ||
hasAlternateIndexReferenceSignal ||
@@ -117,6 +236,8 @@ export function createAssistantTransitionPolicy(deps) {
inventoryShortFollowupAlternate ||
hasInventoryRootTemporalFollowupPrimary ||
hasInventoryRootTemporalFollowupAlternate ||
hasInventoryRootRestatementPrimary ||
hasInventoryRootRestatementAlternate ||
Boolean(debtRoleSwapIntent) ||
deps.hasFollowupMarker(userMessage) ||
deps.hasReferentialPointer(userMessage) ||
@@ -133,6 +254,8 @@ export function createAssistantTransitionPolicy(deps) {
!hasAlternateFollowupSignal &&
!hasInventoryRootTemporalFollowupPrimary &&
!hasInventoryRootTemporalFollowupAlternate &&
!hasInventoryRootRestatementPrimary &&
!hasInventoryRootRestatementAlternate &&
!hasImplicitContinuationSignal &&
!hasOrganizationClarificationContinuation &&
!hasIndexReferenceSignal
@@ -144,6 +267,8 @@ export function createAssistantTransitionPolicy(deps) {
!hasAlternateFollowupSignal &&
!hasInventoryRootTemporalFollowupPrimary &&
!hasInventoryRootTemporalFollowupAlternate &&
!hasInventoryRootRestatementPrimary &&
!hasInventoryRootRestatementAlternate &&
!hasImplicitContinuationSignal &&
!hasOrganizationClarificationContinuation &&
!hasIndexReferenceSignal
@@ -168,8 +293,46 @@ export function createAssistantTransitionPolicy(deps) {
? resolvedAlternateIntent
: null;
const sourceIntentFamily = deps.resolveAddressIntentFamily(sourceIntent);
const explicitIntentFamily = deps.resolveAddressIntentFamily(explicitIntent);
if (sourceIntentFamily && explicitIntentFamily && sourceIntentFamily !== explicitIntentFamily && !hasStrongFollowupReference) {
const explicitIntentFamily =
deps.resolveAddressIntentFamily(explicitIntent) ??
inferStandaloneAddressTopicFamily(userMessage) ??
(deps.toNonEmptyString(alternateMessage) ? inferStandaloneAddressTopicFamily(String(alternateMessage ?? "")) : null);
const foreignAccountingPivotOverInventory = deps.hasForeignAccountingPivotOverInventoryMessage(
userMessage,
alternateMessage
);
const familyMismatchStandaloneTopic = Boolean(
hasStandaloneAddressTopic &&
sourceIntentFamily &&
explicitIntentFamily &&
sourceIntentFamily !== explicitIntentFamily &&
!hasOrganizationClarificationContinuation &&
!hasImplicitContinuationSignal &&
!hasIndexReferenceSignal &&
!hasInventoryRootTemporalFollowupPrimary &&
!hasInventoryRootTemporalFollowupAlternate &&
!hasInventoryRootRestatementPrimary &&
!hasInventoryRootRestatementAlternate &&
!inventoryShortFollowupPrimary &&
!inventoryShortFollowupAlternate &&
!foreignAccountingPivotOverInventory &&
!deps.hasFollowupMarker(userMessage) &&
!deps.hasReferentialPointer(userMessage) &&
(!deps.toNonEmptyString(alternateMessage)
? true
: !deps.hasFollowupMarker(String(alternateMessage ?? "")) &&
!deps.hasReferentialPointer(String(alternateMessage ?? "")))
);
if (familyMismatchStandaloneTopic) {
return null;
}
if (
sourceIntentFamily &&
explicitIntentFamily &&
sourceIntentFamily !== explicitIntentFamily &&
!hasStrongFollowupReference &&
!foreignAccountingPivotOverInventory
) {
return null;
}
let previousIntent = sourceIntent;
@@ -181,7 +344,7 @@ export function createAssistantTransitionPolicy(deps) {
const suggestedIntent = Array.isArray(followupOffer?.suggested_intents)
? deps.toNonEmptyString(followupOffer.suggested_intents[0])
: null;
const keepPreviousIntent = shouldKeepPreviousIntentForShortCounterpartyRetarget(userMessage, sourceIntent);
const keepPreviousIntent = shouldKeepPreviousIntentForShortCounterpartyRetargetV2(userMessage, sourceIntent);
if (suggestedIntent && !keepPreviousIntent) {
previousIntent = suggestedIntent;
followupSelectionMode = "switch_to_suggested_intent";
@@ -297,6 +460,7 @@ export function createAssistantTransitionPolicy(deps) {
: "generic"
: null;
let resolvedCounterpartyFromDisplay = false;
let displayedEntityTargetIntent = null;
const previousFiltersRaw = previousAddressDebug.extracted_filters;
let previousFilters =
previousFiltersRaw && typeof previousFiltersRaw === "object" ? { ...previousFiltersRaw } : {};
@@ -376,7 +540,17 @@ export function createAssistantTransitionPolicy(deps) {
(hasInventoryRootTemporalFollowupPrimary || hasInventoryRootTemporalFollowupAlternate) &&
!deps.hasForeignAccountingPivotOverInventoryMessage(userMessage, alternateMessage)
);
const rootScopedPivot = rootContextOnlyPivot || inventoryRootTemporalPivot;
const inventoryRootRestatementPivot = Boolean(
inventoryRootFrame &&
(deps.isInventorySelectedObjectIntent(sourceIntentHint) ||
deps.isInventoryRootFrameIntent(sourceIntentHint) ||
currentFrameKind === "inventory_drilldown" ||
currentFrameKind === "inventory_root" ||
currentFrameKind === "generic") &&
(hasInventoryRootRestatementPrimary || hasInventoryRootRestatementAlternate) &&
!deps.hasForeignAccountingPivotOverInventoryMessage(userMessage, alternateMessage)
);
const rootScopedPivot = rootContextOnlyPivot || inventoryRootTemporalPivot || inventoryRootRestatementPivot;
if (rootScopedPivot) {
previousIntent = null;
previousAnchorType = null;
@@ -396,6 +570,10 @@ export function createAssistantTransitionPolicy(deps) {
? deps.resolveDisplayedAddressEntityMention(String(alternateMessage ?? ""), displayedEntities)
: null);
if (resolvedEntityFromFollowup && !rootScopedPivot) {
displayedEntityTargetIntent = resolveDisplayedEntityRetargetIntent(
userMessage,
resolvedEntityFromFollowup.entityType
);
if (resolvedEntityFromFollowup.entityType === "counterparty") {
previousFilters.counterparty = resolvedEntityFromFollowup.value;
previousAnchorType = "counterparty";
@@ -465,13 +643,15 @@ export function createAssistantTransitionPolicy(deps) {
inventoryShortFollowupAlternate ||
hasInventoryRootTemporalFollowupPrimary ||
hasInventoryRootTemporalFollowupAlternate ||
hasInventoryRootRestatementPrimary ||
hasInventoryRootRestatementAlternate ||
hasSelectedObjectInventorySignalPrimary ||
hasSelectedObjectInventorySignalAlternate)
);
const carryoverTargetIntent =
followupSelectionMode === "carry_root_context"
? inventoryRootFrame?.intent ?? explicitIntent ?? previousIntent ?? undefined
: explicitIntent ?? previousIntent ?? undefined;
? inventoryRootFrame?.intent ?? displayedEntityTargetIntent ?? explicitIntent ?? previousIntent ?? undefined
: displayedEntityTargetIntent ?? explicitIntent ?? previousIntent ?? undefined;
return {
followupContext: {
previous_intent: previousIntent ?? undefined,
@@ -507,6 +687,7 @@ export function createAssistantTransitionPolicy(deps) {
const previousIntent = deps.toNonEmptyString(carryoverMeta?.previousSourceIntent) ?? null;
const selectionMode = deps.toNonEmptyString(carryoverMeta?.followupSelectionMode) ?? null;
const rootContextOnly = selectionMode === "carry_root_context";
const carryoverTargetIntent = deps.toNonEmptyString(carryoverMeta?.followupContext?.target_intent) ?? null;
const explicitIntentRaw = deps.toNonEmptyString(llmPreDecomposeMeta?.predecomposeContract?.intent);
const explicitIntent = explicitIntentRaw === "unknown" ? null : explicitIntentRaw;
const rootIntent = deps.toNonEmptyString(carryoverMeta?.followupContext?.root_intent) ?? null;
@@ -514,8 +695,8 @@ export function createAssistantTransitionPolicy(deps) {
selectionMode === "switch_to_suggested_intent"
? deps.toNonEmptyString(carryoverMeta?.previousAddressIntent) ?? null
: rootContextOnly
? rootIntent ?? explicitIntent ?? null
: explicitIntent ?? deps.toNonEmptyString(carryoverMeta?.previousAddressIntent) ?? null;
? carryoverTargetIntent ?? rootIntent ?? explicitIntent ?? null
: carryoverTargetIntent ?? explicitIntent ?? deps.toNonEmptyString(carryoverMeta?.previousAddressIntent) ?? null;
const hasImplicitContinuationSignal = Boolean(carryoverMeta?.hasImplicitContinuationSignal);
const rewrittenByPredecompose =
deps.compactWhitespace(sourceMessage.toLowerCase()) !== deps.compactWhitespace(canonicalMessage.toLowerCase());