АРЧ АП11 - Архитектура после регресса: Архитектура: обновить статус pre-multidomain readiness и протянуть continuity snapshot в transition hot path

This commit is contained in:
2026-04-18 14:28:01 +03:00
parent f58ef9ad55
commit 578f6643e6
7 changed files with 418 additions and 43 deletions
@@ -5,6 +5,7 @@ exports.readAddressDebugFilters = readAddressDebugFilters;
exports.readAddressDebugItem = readAddressDebugItem;
exports.readAddressDebugOrganization = readAddressDebugOrganization;
exports.readAddressDebugScopedDate = readAddressDebugScopedDate;
exports.buildInventoryRootFrameFromAddressDebug = buildInventoryRootFrameFromAddressDebug;
exports.isGroundedAddressDebug = isGroundedAddressDebug;
exports.resolveAssistantContinuitySnapshot = resolveAssistantContinuitySnapshot;
function fallbackToNonEmptyString(value) {
@@ -50,6 +51,53 @@ function readAddressDebugScopedDate(debug) {
formatIsoDateForReply(rootFrameContext?.as_of_date) ??
formatIsoDateForReply(extractedFilters?.period_to));
}
function buildInventoryRootFrameFromAddressDebug(debug, toNonEmptyString = fallbackToNonEmptyString) {
if (!debug || typeof debug !== "object") {
return null;
}
const rootFrameContext = toRecordObject(debug.address_root_frame_context);
const extractedFilters = readAddressDebugFilters(debug) ?? {};
const detectedIntent = toNonEmptyString(debug.detected_intent);
const rootIntent = toNonEmptyString(rootFrameContext?.root_intent);
const effectiveIntent = rootIntent ?? detectedIntent;
if (effectiveIntent !== "inventory_on_hand_as_of_date") {
return null;
}
const rootFiltersCandidate = toRecordObject(rootFrameContext?.root_filters);
const filters = {
...(rootFiltersCandidate ?? {}),
...(rootFiltersCandidate ? {} : extractedFilters)
};
if (!filters.organization) {
const organization = readAddressDebugOrganization(debug, toNonEmptyString);
if (organization) {
filters.organization = organization;
}
}
if (!filters.as_of_date) {
const scopedDate = formatIsoDateForReply(readAddressScopedIso(debug));
if (scopedDate) {
const parts = scopedDate.split(".");
filters.as_of_date = `${parts[2]}-${parts[1]}-${parts[0]}`;
}
}
return {
intent: "inventory_on_hand_as_of_date",
filters,
anchorType: toNonEmptyString(rootFrameContext?.root_anchor_type) ?? toNonEmptyString(debug.anchor_type),
anchorValue: toNonEmptyString(rootFrameContext?.root_anchor_value) ??
toNonEmptyString(debug.anchor_value_resolved) ??
toNonEmptyString(debug.anchor_value_raw),
currentFrameKind: toNonEmptyString(rootFrameContext?.current_frame_kind) ?? "inventory_root"
};
}
function readAddressScopedIso(debug) {
const extractedFilters = readAddressDebugFilters(debug);
const rootFrameContext = toRecordObject(debug?.address_root_frame_context);
return (fallbackToNonEmptyString(extractedFilters?.as_of_date) ??
fallbackToNonEmptyString(rootFrameContext?.as_of_date) ??
fallbackToNonEmptyString(extractedFilters?.period_to));
}
function isGroundedAddressDebug(debug, toNonEmptyString = fallbackToNonEmptyString) {
if (!debug || typeof debug !== "object") {
return false;
@@ -103,10 +151,13 @@ function resolveAssistantContinuitySnapshot(input) {
}
}
const primaryDebug = lastGroundedItemAddressDebug ?? lastGroundedAddressDebug;
const inventoryRootFrame = buildInventoryRootFrameFromAddressDebug(lastGroundedInventoryAddressDebug, toNonEmptyString) ??
buildInventoryRootFrameFromAddressDebug(lastGroundedAddressDebug, toNonEmptyString);
return {
lastGroundedAddressDebug,
lastGroundedItemAddressDebug,
lastGroundedInventoryAddressDebug,
inventoryRootFrame,
activeItem: readAddressDebugItem(primaryDebug, toNonEmptyString),
activeOrganization: readAddressDebugOrganization(primaryDebug, toNonEmptyString),
activeScopedDate: readAddressDebugScopedDate(primaryDebug),
@@ -1,7 +1,8 @@
"use strict";
// @ts-nocheck
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAssistantTransitionPolicy = createAssistantTransitionPolicy;
// @ts-nocheck
const assistantContinuityPolicy_1 = require("./assistantContinuityPolicy");
function createAssistantTransitionPolicy(deps) {
function normalizeFollowupText(value) {
return deps.compactWhitespace(deps.repairAddressMojibake(String(value ?? "")).toLowerCase()).replace(/ё/g, "е");
@@ -154,16 +155,6 @@ function createAssistantTransitionPolicy(deps) {
}
return null;
}
function readAddressDebugItemHint(debug) {
if (!debug || typeof debug !== "object") {
return null;
}
const extractedFilters = debug.extracted_filters && typeof debug.extracted_filters === "object" ? debug.extracted_filters : null;
return (deps.toNonEmptyString(extractedFilters?.item) ??
(deps.toNonEmptyString(debug.anchor_type) === "item"
? deps.toNonEmptyString(debug.anchor_value_resolved) ?? deps.toNonEmptyString(debug.anchor_value_raw)
: null));
}
function findRecentInventoryPurchaseProvenanceItem(items, itemHint = null) {
const normalizedItemHint = deps.toNonEmptyString(itemHint);
for (let index = Array.isArray(items) ? items.length - 1 : -1; index >= 0; index -= 1) {
@@ -178,7 +169,7 @@ function createAssistantTransitionPolicy(deps) {
if (!normalizedItemHint) {
return item;
}
const candidateItem = readAddressDebugItemHint(debug);
const candidateItem = (0, assistantContinuityPolicy_1.readAddressDebugItem)(debug, deps.toNonEmptyString);
if (candidateItem && candidateItem === normalizedItemHint) {
return item;
}
@@ -340,6 +331,10 @@ function createAssistantTransitionPolicy(deps) {
? latestAddressItem
: findRecentUsableAddressAssistantItem(items)) ?? latestAddressItem;
const previousAddressDebug = previousAddressItem?.debug ?? null;
const continuitySnapshot = (0, assistantContinuityPolicy_1.resolveAssistantContinuitySnapshot)({
sessionItems: items,
toNonEmptyString: deps.toNonEmptyString
});
const lastOrganizationClarificationDebug = deps.findLastOrganizationClarificationAddressDebug(items);
const organizationClarificationCandidates = Array.isArray(lastOrganizationClarificationDebug?.organization_candidates)
? deps.mergeKnownOrganizations(lastOrganizationClarificationDebug.organization_candidates)
@@ -591,7 +586,9 @@ function createAssistantTransitionPolicy(deps) {
const selectedObjectRetargetIntent = hasSelectedObjectInventorySignalPrimary || hasSelectedObjectInventorySignalAlternate
? inferSelectedObjectInventoryFollowupIntent(userMessage, alternateMessage)
: null;
let inventoryRootFrame = deps.findRecentInventoryRootFrame(items);
let inventoryRootFrame = deps.findRecentInventoryRootFrame(items) ??
continuitySnapshot.inventoryRootFrame ??
(0, assistantContinuityPolicy_1.buildInventoryRootFrameFromAddressDebug)(continuitySnapshot.lastGroundedAddressDebug, deps.toNonEmptyString);
if (inventoryRootFrame && navigationOrganization && !deps.toNonEmptyString(inventoryRootFrame.filters?.organization)) {
inventoryRootFrame = {
...inventoryRootFrame,
@@ -653,6 +650,9 @@ function createAssistantTransitionPolicy(deps) {
previousFilters.organization = historicalOrganization;
}
}
if (!deps.toNonEmptyString(previousFilters.organization) && continuitySnapshot.activeOrganization) {
previousFilters.organization = continuitySnapshot.activeOrganization;
}
if (!deps.toNonEmptyString(previousFilters.organization) && navigationOrganization) {
previousFilters.organization = navigationOrganization;
}
@@ -664,7 +664,7 @@ function createAssistantTransitionPolicy(deps) {
deps.toNonEmptyString(previousAddressDebug?.detected_intent) === "inventory_purchase_provenance_for_item"
? previousAddressItem
: findRecentInventoryPurchaseProvenanceItem(items, deps.toNonEmptyString(navigationFocusObjectLabel) ??
readAddressDebugItemHint(previousAddressDebug) ??
(0, assistantContinuityPolicy_1.readAddressDebugItem)(previousAddressDebug, deps.toNonEmptyString) ??
deps.toNonEmptyString(previousFilters.item)) ?? previousAddressItem;
const purchaseBridgeWindow = extractPurchaseDateBridgeWindow(purchaseBridgeItem, addressNavigationState);
if (purchaseBridgeWindow) {
@@ -687,16 +687,31 @@ function createAssistantTransitionPolicy(deps) {
deps.toNonEmptyString(navigationDateScope?.as_of_date)) {
previousFilters.as_of_date = deps.toNonEmptyString(navigationDateScope?.as_of_date);
}
if (shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.as_of_date) &&
(0, assistantContinuityPolicy_1.readAddressDebugFilters)(continuitySnapshot.lastGroundedAddressDebug)?.as_of_date) {
previousFilters.as_of_date = deps.toNonEmptyString((0, assistantContinuityPolicy_1.readAddressDebugFilters)(continuitySnapshot.lastGroundedAddressDebug)?.as_of_date);
}
if (shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_from) &&
deps.toNonEmptyString(navigationDateScope?.period_from)) {
previousFilters.period_from = deps.toNonEmptyString(navigationDateScope?.period_from);
}
if (shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_from) &&
(0, assistantContinuityPolicy_1.readAddressDebugFilters)(continuitySnapshot.lastGroundedAddressDebug)?.period_from) {
previousFilters.period_from = deps.toNonEmptyString((0, assistantContinuityPolicy_1.readAddressDebugFilters)(continuitySnapshot.lastGroundedAddressDebug)?.period_from);
}
if (shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_to) &&
deps.toNonEmptyString(navigationDateScope?.period_to)) {
previousFilters.period_to = deps.toNonEmptyString(navigationDateScope?.period_to);
}
if (shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_to) &&
(0, assistantContinuityPolicy_1.readAddressDebugFilters)(continuitySnapshot.lastGroundedAddressDebug)?.period_to) {
previousFilters.period_to = deps.toNonEmptyString((0, assistantContinuityPolicy_1.readAddressDebugFilters)(continuitySnapshot.lastGroundedAddressDebug)?.period_to);
}
const rootContextOnlyPivot = Boolean((deps.isInventorySelectedObjectIntent(sourceIntentHint) || currentFrameKind === "inventory_drilldown") &&
deps.hasForeignAccountingPivotOverInventoryMessage(userMessage, alternateMessage) &&
!inventoryPurchaseDateVatBridge);
@@ -767,6 +782,7 @@ function createAssistantTransitionPolicy(deps) {
hasSelectedObjectInventorySignalPrimary ||
hasSelectedObjectInventorySignalAlternate)) {
const selectedObjectLabel = (navigationFocusObjectType === "item" ? navigationFocusObjectLabel : null) ??
continuitySnapshot.activeItem ??
extractSelectedObjectLabel(userMessage) ??
(deps.toNonEmptyString(alternateMessage) ? extractSelectedObjectLabel(String(alternateMessage ?? "")) : null);
if (selectedObjectLabel) {
@@ -7,6 +7,13 @@ export interface AssistantContinuitySnapshot {
lastGroundedAddressDebug: Record<string, unknown> | null;
lastGroundedItemAddressDebug: Record<string, unknown> | null;
lastGroundedInventoryAddressDebug: Record<string, unknown> | null;
inventoryRootFrame: {
intent: string;
filters: Record<string, unknown>;
anchorType: string | null;
anchorValue: string | null;
currentFrameKind: string;
} | null;
activeItem: string | null;
activeOrganization: string | null;
activeScopedDate: string | null;
@@ -75,6 +82,69 @@ export function readAddressDebugScopedDate(debug: Record<string, unknown> | null
);
}
export function buildInventoryRootFrameFromAddressDebug(
debug: Record<string, unknown> | null,
toNonEmptyString: (value: unknown) => string | null = fallbackToNonEmptyString
): {
intent: string;
filters: Record<string, unknown>;
anchorType: string | null;
anchorValue: string | null;
currentFrameKind: string;
} | null {
if (!debug || typeof debug !== "object") {
return null;
}
const rootFrameContext = toRecordObject(debug.address_root_frame_context);
const extractedFilters = readAddressDebugFilters(debug) ?? {};
const detectedIntent = toNonEmptyString(debug.detected_intent);
const rootIntent = toNonEmptyString(rootFrameContext?.root_intent);
const effectiveIntent = rootIntent ?? detectedIntent;
if (effectiveIntent !== "inventory_on_hand_as_of_date") {
return null;
}
const rootFiltersCandidate = toRecordObject(rootFrameContext?.root_filters);
const filters = {
...(rootFiltersCandidate ?? {}),
...(rootFiltersCandidate ? {} : extractedFilters)
};
if (!filters.organization) {
const organization = readAddressDebugOrganization(debug, toNonEmptyString);
if (organization) {
filters.organization = organization;
}
}
if (!filters.as_of_date) {
const scopedDate = formatIsoDateForReply(readAddressScopedIso(debug));
if (scopedDate) {
const parts = scopedDate.split(".");
filters.as_of_date = `${parts[2]}-${parts[1]}-${parts[0]}`;
}
}
return {
intent: "inventory_on_hand_as_of_date",
filters,
anchorType: toNonEmptyString(rootFrameContext?.root_anchor_type) ?? toNonEmptyString(debug.anchor_type),
anchorValue:
toNonEmptyString(rootFrameContext?.root_anchor_value) ??
toNonEmptyString(debug.anchor_value_resolved) ??
toNonEmptyString(debug.anchor_value_raw),
currentFrameKind: toNonEmptyString(rootFrameContext?.current_frame_kind) ?? "inventory_root"
};
}
function readAddressScopedIso(debug: Record<string, unknown> | null): string | null {
const extractedFilters = readAddressDebugFilters(debug);
const rootFrameContext = toRecordObject(debug?.address_root_frame_context);
return (
fallbackToNonEmptyString(extractedFilters?.as_of_date) ??
fallbackToNonEmptyString(rootFrameContext?.as_of_date) ??
fallbackToNonEmptyString(extractedFilters?.period_to)
);
}
export function isGroundedAddressDebug(
debug: Record<string, unknown> | null,
toNonEmptyString: (value: unknown) => string | null = fallbackToNonEmptyString
@@ -142,10 +212,14 @@ export function resolveAssistantContinuitySnapshot(
}
const primaryDebug = lastGroundedItemAddressDebug ?? lastGroundedAddressDebug;
const inventoryRootFrame =
buildInventoryRootFrameFromAddressDebug(lastGroundedInventoryAddressDebug, toNonEmptyString) ??
buildInventoryRootFrameFromAddressDebug(lastGroundedAddressDebug, toNonEmptyString);
return {
lastGroundedAddressDebug,
lastGroundedItemAddressDebug,
lastGroundedInventoryAddressDebug,
inventoryRootFrame,
activeItem: readAddressDebugItem(primaryDebug, toNonEmptyString),
activeOrganization: readAddressDebugOrganization(primaryDebug, toNonEmptyString),
activeScopedDate: readAddressDebugScopedDate(primaryDebug),
@@ -1,4 +1,10 @@
// @ts-nocheck
import {
buildInventoryRootFrameFromAddressDebug,
readAddressDebugFilters,
readAddressDebugItem,
resolveAssistantContinuitySnapshot
} from "./assistantContinuityPolicy";
export function createAssistantTransitionPolicy(deps) {
function normalizeFollowupText(value) {
@@ -186,20 +192,6 @@ export function createAssistantTransitionPolicy(deps) {
return null;
}
function readAddressDebugItemHint(debug) {
if (!debug || typeof debug !== "object") {
return null;
}
const extractedFilters =
debug.extracted_filters && typeof debug.extracted_filters === "object" ? debug.extracted_filters : null;
return (
deps.toNonEmptyString(extractedFilters?.item) ??
(deps.toNonEmptyString(debug.anchor_type) === "item"
? deps.toNonEmptyString(debug.anchor_value_resolved) ?? deps.toNonEmptyString(debug.anchor_value_raw)
: null)
);
}
function findRecentInventoryPurchaseProvenanceItem(items, itemHint = null) {
const normalizedItemHint = deps.toNonEmptyString(itemHint);
for (let index = Array.isArray(items) ? items.length - 1 : -1; index >= 0; index -= 1) {
@@ -214,7 +206,7 @@ export function createAssistantTransitionPolicy(deps) {
if (!normalizedItemHint) {
return item;
}
const candidateItem = readAddressDebugItemHint(debug);
const candidateItem = readAddressDebugItem(debug, deps.toNonEmptyString);
if (candidateItem && candidateItem === normalizedItemHint) {
return item;
}
@@ -421,6 +413,10 @@ export function createAssistantTransitionPolicy(deps) {
? latestAddressItem
: findRecentUsableAddressAssistantItem(items)) ?? latestAddressItem;
const previousAddressDebug = previousAddressItem?.debug ?? null;
const continuitySnapshot = resolveAssistantContinuitySnapshot({
sessionItems: items,
toNonEmptyString: deps.toNonEmptyString
});
const lastOrganizationClarificationDebug = deps.findLastOrganizationClarificationAddressDebug(items);
const organizationClarificationCandidates = Array.isArray(lastOrganizationClarificationDebug?.organization_candidates)
? deps.mergeKnownOrganizations(lastOrganizationClarificationDebug.organization_candidates)
@@ -727,7 +723,10 @@ export function createAssistantTransitionPolicy(deps) {
hasSelectedObjectInventorySignalPrimary || hasSelectedObjectInventorySignalAlternate
? inferSelectedObjectInventoryFollowupIntent(userMessage, alternateMessage)
: null;
let inventoryRootFrame = deps.findRecentInventoryRootFrame(items);
let inventoryRootFrame =
deps.findRecentInventoryRootFrame(items) ??
continuitySnapshot.inventoryRootFrame ??
buildInventoryRootFrameFromAddressDebug(continuitySnapshot.lastGroundedAddressDebug, deps.toNonEmptyString);
if (inventoryRootFrame && navigationOrganization && !deps.toNonEmptyString(inventoryRootFrame.filters?.organization)) {
inventoryRootFrame = {
...inventoryRootFrame,
@@ -794,6 +793,9 @@ export function createAssistantTransitionPolicy(deps) {
previousFilters.organization = historicalOrganization;
}
}
if (!deps.toNonEmptyString(previousFilters.organization) && continuitySnapshot.activeOrganization) {
previousFilters.organization = continuitySnapshot.activeOrganization;
}
if (!deps.toNonEmptyString(previousFilters.organization) && navigationOrganization) {
previousFilters.organization = navigationOrganization;
}
@@ -808,7 +810,7 @@ export function createAssistantTransitionPolicy(deps) {
: findRecentInventoryPurchaseProvenanceItem(
items,
deps.toNonEmptyString(navigationFocusObjectLabel) ??
readAddressDebugItemHint(previousAddressDebug) ??
readAddressDebugItem(previousAddressDebug, deps.toNonEmptyString) ??
deps.toNonEmptyString(previousFilters.item)
) ?? previousAddressItem;
const purchaseBridgeWindow = extractPurchaseDateBridgeWindow(purchaseBridgeItem, addressNavigationState);
@@ -835,6 +837,15 @@ export function createAssistantTransitionPolicy(deps) {
) {
previousFilters.as_of_date = deps.toNonEmptyString(navigationDateScope?.as_of_date);
}
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.as_of_date) &&
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.as_of_date
) {
previousFilters.as_of_date = deps.toNonEmptyString(
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.as_of_date
);
}
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_from) &&
@@ -842,6 +853,15 @@ export function createAssistantTransitionPolicy(deps) {
) {
previousFilters.period_from = deps.toNonEmptyString(navigationDateScope?.period_from);
}
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_from) &&
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_from
) {
previousFilters.period_from = deps.toNonEmptyString(
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_from
);
}
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_to) &&
@@ -849,6 +869,15 @@ export function createAssistantTransitionPolicy(deps) {
) {
previousFilters.period_to = deps.toNonEmptyString(navigationDateScope?.period_to);
}
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_to) &&
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_to
) {
previousFilters.period_to = deps.toNonEmptyString(
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_to
);
}
const rootContextOnlyPivot = Boolean(
(deps.isInventorySelectedObjectIntent(sourceIntentHint) || currentFrameKind === "inventory_drilldown") &&
deps.hasForeignAccountingPivotOverInventoryMessage(userMessage, alternateMessage) &&
@@ -935,6 +964,7 @@ export function createAssistantTransitionPolicy(deps) {
) {
const selectedObjectLabel =
(navigationFocusObjectType === "item" ? navigationFocusObjectLabel : null) ??
continuitySnapshot.activeItem ??
extractSelectedObjectLabel(userMessage) ??
(deps.toNonEmptyString(alternateMessage) ? extractSelectedObjectLabel(String(alternateMessage ?? "")) : null);
if (selectedObjectLabel) {