Архитектура: централизовать root-frame-aware carryover filters в continuity policy и transition glue
This commit is contained in:
@@ -8,6 +8,7 @@ exports.readAddressDebugScopedDate = readAddressDebugScopedDate;
|
||||
exports.readAddressDebugTemporalScope = readAddressDebugTemporalScope;
|
||||
exports.resolveAddressDebugAnchorContext = resolveAddressDebugAnchorContext;
|
||||
exports.resolveAddressDebugContextFacts = resolveAddressDebugContextFacts;
|
||||
exports.resolveAddressDebugCarryoverFilters = resolveAddressDebugCarryoverFilters;
|
||||
exports.buildInventoryRootFrameFromAddressDebug = buildInventoryRootFrameFromAddressDebug;
|
||||
exports.isGroundedAddressDebug = isGroundedAddressDebug;
|
||||
exports.resolveAssistantContinuitySnapshot = resolveAssistantContinuitySnapshot;
|
||||
@@ -121,6 +122,37 @@ function resolveAddressDebugContextFacts(debug, toNonEmptyString = fallbackToNon
|
||||
scopedDate: readAddressDebugScopedDate(debug)
|
||||
};
|
||||
}
|
||||
function resolveAddressDebugCarryoverFilters(debug, toNonEmptyString = fallbackToNonEmptyString) {
|
||||
const extractedFilters = readAddressDebugFilters(debug);
|
||||
const nextFilters = extractedFilters ? { ...extractedFilters } : {};
|
||||
const inventoryRootFrame = buildInventoryRootFrameFromAddressDebug(debug, toNonEmptyString);
|
||||
const rootFilters = inventoryRootFrame?.filters && typeof inventoryRootFrame.filters === "object"
|
||||
? inventoryRootFrame.filters
|
||||
: null;
|
||||
if (rootFilters) {
|
||||
const organization = toNonEmptyString(rootFilters.organization);
|
||||
const warehouse = toNonEmptyString(rootFilters.warehouse);
|
||||
const asOfDate = toNonEmptyString(rootFilters.as_of_date);
|
||||
const periodFrom = toNonEmptyString(rootFilters.period_from);
|
||||
const periodTo = toNonEmptyString(rootFilters.period_to);
|
||||
if (organization) {
|
||||
nextFilters.organization = organization;
|
||||
}
|
||||
if (warehouse) {
|
||||
nextFilters.warehouse = warehouse;
|
||||
}
|
||||
if (asOfDate) {
|
||||
nextFilters.as_of_date = asOfDate;
|
||||
}
|
||||
if (periodFrom) {
|
||||
nextFilters.period_from = periodFrom;
|
||||
}
|
||||
if (periodTo) {
|
||||
nextFilters.period_to = periodTo;
|
||||
}
|
||||
}
|
||||
return nextFilters;
|
||||
}
|
||||
function buildInventoryRootFrameFromAddressDebug(debug, toNonEmptyString = fallbackToNonEmptyString) {
|
||||
if (!debug || typeof debug !== "object") {
|
||||
return null;
|
||||
|
||||
@@ -627,8 +627,7 @@ function createAssistantTransitionPolicy(deps) {
|
||||
: null;
|
||||
let resolvedCounterpartyFromDisplay = false;
|
||||
let displayedEntityTargetIntent = null;
|
||||
const previousFiltersRaw = previousAddressDebug.extracted_filters;
|
||||
let previousFilters = previousFiltersRaw && typeof previousFiltersRaw === "object" ? { ...previousFiltersRaw } : {};
|
||||
let previousFilters = (0, assistantContinuityPolicy_1.resolveAddressDebugCarryoverFilters)(previousAddressDebug, deps.toNonEmptyString);
|
||||
const shouldBackfillHistoricalPartyAnchors = sourceIntentHint === "list_contracts_by_counterparty" ||
|
||||
sourceIntentHint === "list_documents_by_counterparty" ||
|
||||
sourceIntentHint === "bank_operations_by_counterparty" ||
|
||||
|
||||
@@ -208,6 +208,42 @@ export function resolveAddressDebugContextFacts(
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveAddressDebugCarryoverFilters(
|
||||
debug: Record<string, unknown> | null,
|
||||
toNonEmptyString: (value: unknown) => string | null = fallbackToNonEmptyString
|
||||
): Record<string, unknown> {
|
||||
const extractedFilters = readAddressDebugFilters(debug);
|
||||
const nextFilters = extractedFilters ? { ...extractedFilters } : {};
|
||||
const inventoryRootFrame = buildInventoryRootFrameFromAddressDebug(debug, toNonEmptyString);
|
||||
const rootFilters =
|
||||
inventoryRootFrame?.filters && typeof inventoryRootFrame.filters === "object"
|
||||
? inventoryRootFrame.filters
|
||||
: null;
|
||||
if (rootFilters) {
|
||||
const organization = toNonEmptyString(rootFilters.organization);
|
||||
const warehouse = toNonEmptyString(rootFilters.warehouse);
|
||||
const asOfDate = toNonEmptyString(rootFilters.as_of_date);
|
||||
const periodFrom = toNonEmptyString(rootFilters.period_from);
|
||||
const periodTo = toNonEmptyString(rootFilters.period_to);
|
||||
if (organization) {
|
||||
nextFilters.organization = organization;
|
||||
}
|
||||
if (warehouse) {
|
||||
nextFilters.warehouse = warehouse;
|
||||
}
|
||||
if (asOfDate) {
|
||||
nextFilters.as_of_date = asOfDate;
|
||||
}
|
||||
if (periodFrom) {
|
||||
nextFilters.period_from = periodFrom;
|
||||
}
|
||||
if (periodTo) {
|
||||
nextFilters.period_to = periodTo;
|
||||
}
|
||||
}
|
||||
return nextFilters;
|
||||
}
|
||||
|
||||
export function buildInventoryRootFrameFromAddressDebug(
|
||||
debug: Record<string, unknown> | null,
|
||||
toNonEmptyString: (value: unknown) => string | null = fallbackToNonEmptyString
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
readAddressDebugFilters,
|
||||
readAddressDebugItem,
|
||||
readAddressDebugTemporalScope,
|
||||
resolveAddressDebugCarryoverFilters,
|
||||
resolveAddressDebugAnchorContext,
|
||||
resolveAssistantOrganizationAuthority
|
||||
} from "./assistantContinuityPolicy";
|
||||
@@ -773,9 +774,7 @@ export function createAssistantTransitionPolicy(deps) {
|
||||
: null;
|
||||
let resolvedCounterpartyFromDisplay = false;
|
||||
let displayedEntityTargetIntent = null;
|
||||
const previousFiltersRaw = previousAddressDebug.extracted_filters;
|
||||
let previousFilters =
|
||||
previousFiltersRaw && typeof previousFiltersRaw === "object" ? { ...previousFiltersRaw } : {};
|
||||
let previousFilters = resolveAddressDebugCarryoverFilters(previousAddressDebug, deps.toNonEmptyString);
|
||||
const shouldBackfillHistoricalPartyAnchors =
|
||||
sourceIntentHint === "list_contracts_by_counterparty" ||
|
||||
sourceIntentHint === "list_documents_by_counterparty" ||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
readAddressDebugTemporalScope,
|
||||
resolveAddressDebugCarryoverFilters,
|
||||
resolveAddressDebugContextFacts,
|
||||
resolveAddressDebugAnchorContext,
|
||||
resolveAssistantOrganizationAuthority
|
||||
@@ -100,4 +101,36 @@ describe("assistantContinuityPolicy organization authority", () => {
|
||||
anchorValue: "Рабочая станция"
|
||||
});
|
||||
});
|
||||
it("prefers inventory root-frame filters over stale drilldown date scope in carryover filters", () => {
|
||||
const filters = resolveAddressDebugCarryoverFilters({
|
||||
detected_intent: "inventory_purchase_documents_for_item",
|
||||
extracted_filters: {
|
||||
item: "Workstation",
|
||||
organization: "Org Alt",
|
||||
as_of_date: "2021-04-15"
|
||||
},
|
||||
address_root_frame_context: {
|
||||
root_intent: "inventory_on_hand_as_of_date",
|
||||
root_filters: {
|
||||
organization: "Org Alt",
|
||||
warehouse: "Main Warehouse",
|
||||
as_of_date: "2021-03-31",
|
||||
period_from: "2021-03-01",
|
||||
period_to: "2021-03-31"
|
||||
},
|
||||
root_anchor_type: "organization",
|
||||
root_anchor_value: "Org Alt",
|
||||
current_frame_kind: "inventory_drilldown"
|
||||
}
|
||||
});
|
||||
|
||||
expect(filters).toEqual({
|
||||
item: "Workstation",
|
||||
organization: "Org Alt",
|
||||
warehouse: "Main Warehouse",
|
||||
as_of_date: "2021-03-31",
|
||||
period_from: "2021-03-01",
|
||||
period_to: "2021-03-31"
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -754,4 +754,88 @@ describe("assistantTransitionPolicy", () => {
|
||||
});
|
||||
expect(carryover?.followupContext?.previous_anchor_type).toBe("item");
|
||||
});
|
||||
|
||||
it("prefers root-frame dates over stale drilldown filters when hydrating previous filters", () => {
|
||||
const organization = "Org Alt";
|
||||
const policy = buildPolicy({
|
||||
findLastAddressAssistantItem: (_items: unknown[]) => ({
|
||||
text: "Workstation drilldown",
|
||||
debug: {
|
||||
detected_intent: "inventory_purchase_documents_for_item",
|
||||
extracted_filters: {
|
||||
item: "Workstation",
|
||||
organization,
|
||||
as_of_date: "2021-04-15"
|
||||
},
|
||||
anchor_type: "item",
|
||||
anchor_value_resolved: "Workstation",
|
||||
address_root_frame_context: {
|
||||
root_intent: "inventory_on_hand_as_of_date",
|
||||
root_filters: {
|
||||
organization,
|
||||
warehouse: "Main Warehouse",
|
||||
as_of_date: "2021-03-31",
|
||||
period_from: "2021-03-01",
|
||||
period_to: "2021-03-31"
|
||||
},
|
||||
root_anchor_type: "organization",
|
||||
root_anchor_value: organization,
|
||||
current_frame_kind: "inventory_drilldown"
|
||||
}
|
||||
}
|
||||
}),
|
||||
findRecentInventoryRootFrame: () => null,
|
||||
hasInventoryRootTemporalFollowupSignal: (message: string) => /эту же дату/i.test(message)
|
||||
});
|
||||
|
||||
const items = [
|
||||
{
|
||||
role: "assistant",
|
||||
text: "Workstation drilldown",
|
||||
debug: {
|
||||
execution_lane: "address_query",
|
||||
answer_grounding_check: { status: "grounded" },
|
||||
detected_intent: "inventory_purchase_documents_for_item",
|
||||
extracted_filters: {
|
||||
item: "Workstation",
|
||||
organization,
|
||||
as_of_date: "2021-04-15"
|
||||
},
|
||||
anchor_type: "item",
|
||||
anchor_value_resolved: "Workstation",
|
||||
address_root_frame_context: {
|
||||
root_intent: "inventory_on_hand_as_of_date",
|
||||
root_filters: {
|
||||
organization,
|
||||
warehouse: "Main Warehouse",
|
||||
as_of_date: "2021-03-31",
|
||||
period_from: "2021-03-01",
|
||||
period_to: "2021-03-31"
|
||||
},
|
||||
root_anchor_type: "organization",
|
||||
root_anchor_value: organization,
|
||||
current_frame_kind: "inventory_drilldown"
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
const carryover = policy.resolveAddressFollowupCarryoverContext(
|
||||
"остатки на эту же дату",
|
||||
items as any,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
expect(carryover?.followupContext?.root_context_only).toBe(true);
|
||||
expect(carryover?.followupContext?.previous_filters).toMatchObject({
|
||||
organization,
|
||||
warehouse: "Main Warehouse",
|
||||
as_of_date: "2021-03-31",
|
||||
period_from: "2021-03-01",
|
||||
period_to: "2021-03-31"
|
||||
});
|
||||
expect(carryover?.followupContext?.previous_filters?.as_of_date).not.toBe("2021-04-15");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user