Закрепить складской provenance и supplier-overlap ассистента

This commit is contained in:
2026-06-03 07:38:35 +03:00
parent 4d32b7fad5
commit 2cec57888a
28 changed files with 2274 additions and 162 deletions
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLowQualityCounterpartyAnchorValue = isLowQualityCounterpartyAnchorValue;
exports.isLowQualityInventoryItemAnchorValue = isLowQualityInventoryItemAnchorValue;
exports.isInventoryItemAnchorDegradation = isInventoryItemAnchorDegradation;
exports.extractSelectedObjectQuotedValue = extractSelectedObjectQuotedValue;
@@ -642,13 +643,17 @@ function isLowQualityCounterpartyAnchorValue(rawValue) {
const value = String(rawValue ?? "")
.trim()
.toLowerCase()
.replace(/ё/g, "е");
.replace(/ё/g, "е")
.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, "");
if (!value) {
return true;
}
if (/(?:за\s+вс[её]\s+время|за\s+всю\s+истори(?:ю|и)|all\s+time|entire\s+period|full\s+history)/iu.test(value)) {
return true;
}
if (/^(?:товар(?:ы|а|ов|у|ом|ами|ах)?|номенклатур(?:а|ы|у|ой|ами)?|позици(?:я|и|ю|ей)|остат(?:ок|ки|ка|ков|кам|ками)?|склад|складе|складу)$/iu.test(value)) {
return true;
}
if (/^(?:или|это|там|может|можно|обычн\p{L}*|клиентск\p{L}*|банковск\p{L}*(?:\/|\s+и\s+)?финансов\p{L}*)\b/iu.test(value)) {
return true;
}
+97 -5
View File
@@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressQueryService = void 0;
exports.ensureInventorySupplierOverlapLeadLine = ensureInventorySupplierOverlapLeadLine;
const config_1 = require("../config");
const addressRecipeCatalog_1 = require("./addressRecipeCatalog");
const addressMcpClient_1 = require("./addressMcpClient");
@@ -1988,6 +1989,10 @@ function hasExplicitPeriodWindow(filters) {
function asksForUnresolvedInventorySupplierLink(userMessage) {
return /(?:\u0431\u0435\u0437\s+\u043f\u043e\u043d\u044f\u0442\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u0431\u0435\u0437\s+(?:\u044f\u0432\u043d[^\s]*\s+)?\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043d\u0435\s+\u0438\u043c\u0435\u044e\u0442\s+\u044f\u0432\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043d\u0435\u0442\s+\u044f\u0432\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|unresolved\s+supplier\s+link)/iu.test(String(userMessage ?? ""));
}
function messagePairAsksForUnresolvedInventorySupplierLink(userMessage, rawUserMessage) {
return (asksForUnresolvedInventorySupplierLink(userMessage) ||
asksForUnresolvedInventorySupplierLink(rawUserMessage));
}
function canAutoBroadenPeriodWindow(intent, filters) {
if (Array.isArray(filters.warnings) && filters.warnings?.includes("exact_historical_period_window_requested")) {
return false;
@@ -2037,7 +2042,11 @@ function shouldDetachLifecycleExecutionFromSnapshotContext(intent, reasons) {
intent !== "inventory_purchase_to_sale_chain") {
return false;
}
return (reasons.includes("period_window_semantic_from_inventory_snapshot_context") ||
const historyIntent = intent === "inventory_sale_trace_for_item" ||
intent === "inventory_profitability_for_item" ||
intent === "inventory_purchase_to_sale_chain";
return ((historyIntent && reasons.includes("as_of_date_from_analysis_context")) ||
reasons.includes("period_window_semantic_from_inventory_snapshot_context") ||
reasons.includes("period_window_semantic_from_inventory_as_of_month") ||
reasons.includes("as_of_date_from_followup_context") ||
reasons.includes("period_from_followup_context") ||
@@ -2119,6 +2128,75 @@ function injectNoticeAfterLeadLine(text, notice) {
}
return [lines[0], normalizedNotice, ...lines.slice(1)].join("\n");
}
function injectNoticeBeforeLeadLine(text, notice) {
const normalizedText = typeof text === "string" ? text : "";
const normalizedNotice = typeof notice === "string" ? notice.trim() : "";
if (!normalizedText.trim()) {
return normalizedNotice;
}
if (!normalizedNotice) {
return normalizedText;
}
return [normalizedNotice, normalizedText].join("\n");
}
function injectAutoBroadenedPeriodNotice(intent, text, notice) {
if (intent === "inventory_supplier_stock_overlap_as_of_date") {
return injectNoticeBeforeLeadLine(text, notice);
}
return injectNoticeAfterLeadLine(text, notice);
}
function extractSupplierOverlapReplyCounterparties(text) {
const counterparties = [];
const pattern = /\u043a\u043e\u043d\u0442\u0440\u0430\u0433\u0435\u043d\u0442:\s*([^|\n]+)/giu;
for (const match of String(text ?? "").matchAll(pattern)) {
const value = String(match[1] ?? "").replace(/\s+/gu, " ").trim();
if (!value || /^(?:\u043d\u0435\s+\u0432\u044b\u0434\u0435\u043b\u0435\u043d|\u043d\/\u0434|unknown)$/iu.test(value)) {
continue;
}
counterparties.push(value);
}
return uniqueStrings(counterparties);
}
function formatSupplierOverlapCounterpartyLead(counterparties, limit = 6) {
const visible = counterparties.slice(0, limit);
const remaining = counterparties.length - visible.length;
const suffix = remaining > 0 ? `; и еще ${remaining}` : "";
return `${visible.join("; ")}${suffix}`;
}
function hasInventorySupplierOverlapDefensiveLead(firstLine) {
const normalized = String(firstLine ?? "").toLocaleLowerCase("ru-RU").replace(/\u0451/gu, "\u0435");
const startsWithStockScope = /^\u043f\u043e\s+(?:\u0441\u043a\u043b\u0430\u0434\u0441\u043a\u043e\u043c\u0443\s+\u043e\u0441\u0442\u0430\u0442\u043a\u0443|\u043e\u043a\u043d\u0443)/u.test(normalized);
const hasDefensiveAttribution = /\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d(?:\u044b\u0439\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u0430\u044f\s+\u0430\u0442\u0440\u0438\u0431\u0443\u0446\u0438\u044f).*?\u043d\u0435\s+\u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d/u.test(normalized);
return startsWithStockScope && hasDefensiveAttribution;
}
function ensureInventorySupplierOverlapLeadLine(intent, text) {
if (intent !== "inventory_supplier_stock_overlap_as_of_date") {
return text;
}
const normalizedText = typeof text === "string" ? text : "";
const lines = normalizedText.split(/\r?\n/u);
const firstLine = lines[0]?.trim() ?? "";
const displayedCounterparties = extractSupplierOverlapReplyCounterparties(normalizedText);
if (displayedCounterparties.length > 0 &&
hasInventorySupplierOverlapDefensiveLead(firstLine)) {
const repairedLead = displayedCounterparties.length === 1
? `По складскому остатку найден поставщик закупочного следа: ${displayedCounterparties[0]}.`
: `По складскому остатку найдено несколько поставщиков закупочного следа: ${formatSupplierOverlapCounterpartyLead(displayedCounterparties)}.`;
return [repairedLead, firstLine, ...lines.slice(1)].join("\n");
}
if (!normalizedText.trim() ||
/^По\s+(?:складскому\s+остатку|окну)\b/iu.test(firstLine) ||
/^В\s+текущем\s+складском\s+срезе\b/iu.test(firstLine)) {
return normalizedText;
}
if (!/^(?:Что\s+проверили:|Часть\s+закупочных\s+операций|Опорные\s+документы:)/iu.test(firstLine)) {
return normalizedText;
}
return [
"По складскому остатку однозначный поставщик текущего остатка не подтвержден; ниже показан проверенный закупочный след и ограничения.",
normalizedText
].join("\n");
}
function runtimeReadinessForLimitedCategory(category) {
if (category === "empty_match" || category === "missing_anchor") {
return "LIVE_QUERYABLE_WITH_LIMITS";
@@ -2971,7 +3049,7 @@ class AddressQueryService {
if (intent.intent === "inventory_supplier_stock_overlap_as_of_date" &&
!toNonEmptyFilterValue(filters.extracted_filters.period_from) &&
!toNonEmptyFilterValue(filters.extracted_filters.period_to) &&
asksForUnresolvedInventorySupplierLink(userMessage) &&
messagePairAsksForUnresolvedInventorySupplierLink(userMessage, options.rawUserMessage) &&
!/(?:за\s+вс[её]\s+время|за\s+любой\s+период|all[\s-]?time|all\s+periods?)/iu.test(userMessage)) {
const monthWindow = deriveMonthWindowForDate(filters.extracted_filters.as_of_date);
if (monthWindow) {
@@ -3079,6 +3157,19 @@ class AddressQueryService {
receivablesConfirmedExecution?.executionFilters ??
vatPayableConfirmedExecution?.executionFilters ??
filters.extracted_filters;
if (intent.intent === "inventory_aging_by_purchase_date" &&
toNonEmptyFilterValue(executionFilters.as_of_date) &&
(toNonEmptyFilterValue(executionFilters.period_from) || toNonEmptyFilterValue(executionFilters.period_to))) {
executionFilters = { ...executionFilters };
delete executionFilters.period_from;
delete executionFilters.period_to;
if (!filters.warnings.includes("period_window_detached_for_inventory_aging_execution")) {
filters.warnings.push("period_window_detached_for_inventory_aging_execution");
}
if (!baseReasons.includes("period_window_detached_for_inventory_aging_execution")) {
baseReasons.push("period_window_detached_for_inventory_aging_execution");
}
}
if (intent.intent === "counterparty_activity_lifecycle" &&
typeof executionFilters.counterparty === "string" &&
sameOrganizationEntityReference(executionFilters.counterparty, executionFilters.organization ?? activeOrganization)) {
@@ -3233,6 +3324,7 @@ class AddressQueryService {
: typeof filterSet.account === "string"
? filterSet.account
: undefined,
warehouseHint: typeof filterSet.warehouse === "string" ? filterSet.warehouse : undefined,
periodFrom: typeof filterSet.period_from === "string" ? filterSet.period_from : undefined,
periodTo: typeof filterSet.period_to === "string" ? filterSet.period_to : undefined,
asOfDate: typeof filterSet.as_of_date === "string" ? filterSet.as_of_date : undefined,
@@ -3861,7 +3953,7 @@ class AddressQueryService {
});
return {
handled: true,
reply_text: repairCounterpartyReplyLabel(replyText, intent.intent, filters.extracted_filters),
reply_text: repairCounterpartyReplyLabel(ensureInventorySupplierOverlapLeadLine(intent.intent, replyText), intent.intent, filters.extracted_filters),
reply_type: (0, composeStage_1.inferReplyType)(responseType),
response_type: responseType,
debug: debugPayload
@@ -3983,7 +4075,7 @@ class AddressQueryService {
});
return {
handled: true,
reply_text: repairCounterpartyReplyLabel(input.replyText, intent.intent, (input.extractedFilters ?? filters.extracted_filters)),
reply_text: repairCounterpartyReplyLabel(ensureInventorySupplierOverlapLeadLine(intent.intent, input.replyText), intent.intent, (input.extractedFilters ?? filters.extracted_filters)),
reply_type: (0, composeStage_1.inferReplyType)(input.responseType),
response_type: input.responseType,
debug: debugPayload
@@ -4263,7 +4355,7 @@ class AddressQueryService {
resultMode: broadenedCoverageEvidence.result_mode ?? undefined
});
return buildFactualExecutionResult({
replyText: injectNoticeAfterLeadLine(broadenedFactual.text, broadenedPrefix),
replyText: injectAutoBroadenedPeriodNotice(intent.intent, broadenedFactual.text, broadenedPrefix),
responseType: broadenedFactual.responseType,
responseSemantics: broadenedFactual.semantics,
selectedRecipe: broadenedSelection.selected_recipe.recipe_id,
@@ -1441,20 +1441,11 @@ function buildInventoryAgingByPurchaseDateQuery(filters, resolvedLimit) {
buildInventoryItemReferenceCondition(filters, ["Остатки.Субконто1"])
];
const onHandWhereClause = buildStaticWhereClause(onHandScopeConditions);
const currentStockItemSubquery = [
"(ВЫБРАТЬ РАЗЛИЧНЫЕ",
" Остатки.Субконто1",
" ИЗ",
` РегистрБухгалтерии.Хозрасчетный.Остатки(${asOfExpr}, , , ) КАК Остатки`,
buildStaticWhereClause(onHandScopeConditions, " "),
")"
].join("\n");
const purchaseWhereClause = buildStaticWhereClause([
"Товары.Ссылка.Проведен = ИСТИНА",
`Товары.Ссылка.Дата <= ${asOfExpr}`,
buildOrganizationReferenceCondition(filters, ["Товары.Ссылка.Организация"]),
buildInventoryItemReferenceCondition(filters, ["Товары.Номенклатура"]),
`Товары.Номенклатура В ${currentStockItemSubquery}`
buildInventoryItemReferenceCondition(filters, ["Товары.Номенклатура"])
]);
return `
ВЫБРАТЬ ПЕРВЫЕ ${resolvedLimit}
@@ -559,6 +559,9 @@ function shouldRestoreInventoryRootFrame(userMessage, intent, extractedFilters,
if (!canReenterInventoryRoot) {
return false;
}
if (intent === "inventory_aging_by_purchase_date") {
return false;
}
if (intent !== "unknown" && !isInventoryIntent(intent) && !hasInventoryRootRestatementCue) {
return false;
}
@@ -584,13 +587,13 @@ function shouldRestoreInventoryRootFrame(userMessage, intent, extractedFilters,
}
function hasSelectedObjectInventorySignal(text) {
const repairedSelectedObjectText = textWithRepairedVariant(String(text ?? ""));
if (/(?:по\s+выбранному\s+объекту|по\s+этой\s+позиции|по\s+этому\s+товару|по\s+ней|по\s+нему|по\s+ним|for\s+selected\s+object|selected\s+object)/iu.test(repairedSelectedObjectText)) {
if (/(?:по\s+выбранному\s+объекту|выбранн(?:ому|ым)\s+(?:объекту|товару|товаром|позиции|позицией)|по\s+этой\s+позиции|по\s+этому\s+товару|по\s+ней|по\s+нему|по\s+ним|for\s+selected\s+object|selected\s+object)/iu.test(repairedSelectedObjectText)) {
return true;
}
if (/(?:по\s+выбранному\s+объекту|по\s+этой\s+позиции|по\s+этому\s+товару|по\s+ней|по\s+нему|по\s+ним|for\s+selected\s+object|selected\s+object)/iu.test(String(text ?? ""))) {
if (/(?:по\s+выбранному\s+объекту|выбранн(?:ому|ым)\s+(?:объекту|товару|товаром|позиции|позицией)|по\s+этой\s+позиции|по\s+этому\s+товару|по\s+ней|по\s+нему|по\s+ним|for\s+selected\s+object|selected\s+object)/iu.test(String(text ?? ""))) {
return true;
}
return /(?:по\s+выбранному\s+объекту|по\s+этой\s+позиции|по\s+этому\s+товару|по\s+ней|по\s+нему|по\s+ним|for\s+selected\s+object|selected\s+object)/iu.test(String(text ?? ""));
return /(?:по\s+выбранному\s+объекту|выбранн(?:ому|ым)\s+(?:объекту|товару|товаром|позиции|позицией)|по\s+этой\s+позиции|по\s+этому\s+товару|по\s+ней|по\s+нему|по\s+ним|for\s+selected\s+object|selected\s+object)/iu.test(String(text ?? ""));
}
function hasSelectedObjectInlineSnapshotMetadata(text) {
return /(?:дата\s+строки|строка\s+от|количество\s*:|стоимость\s*:|склад\s*:|организация\s*:|\|\s*(?:склад|количество|стоимость|организация|дата\s+строки)\s*:)/iu.test(String(text ?? ""));
@@ -618,6 +621,7 @@ function hasInventorySupplierFollowupCue(text) {
function hasInventoryPurchaseDocumentsFollowupCue(text) {
const value = textWithRepairedVariant(String(text ?? ""));
return (/(?:по\s+каким\s+документам\s+(?:это|его|ее|её|этот\s+товар|эту\s+позицию)\s+купили|по\s+каким\s+документам\s+(?:был\s+)?куплен|какими\s+документами\s+(?:это|его|этот\s+товар|эту\s+позицию)\s+купили|какими\s+документами\s+(?:был\s+)?куплен|каким\s+документом\s+(?:это|его|ее|её|она|он|этот\s+товар|эта\s+позиция|эту\s+позицию)?\s*(?:подтвержден[оа]?|подтверждена\s+закупка|доказан[оа]?|закрыт[оа]?)|закупк[аи]\s+подтвержден[аы]?\s+документ|покажи\s+документы\s+по\s+(?:этой\s+позиции|этому\s+товару|ней|нему)|документы\s+по\s+(?:этой\s+позиции|этому\s+товару|ней|нему))/iu.test(value) ||
/(?:документ[а-яё]*[\s\S]{0,40}поступлен[а-яё]*[\s\S]{0,140}(?:выбранн|товар|позици)|какие\s+документ[а-яё]*[\s\S]{0,40}поступлен[а-яё]*[\s\S]{0,140}(?:выбранн|товар|позици))/iu.test(value) ||
/(?:по\s+каким\s+документам\s+(?:это|его|этот\s+товар|эту\s+позицию)\s+купили|по\s+каким\s+документам\s+(?:был\s+)?куплен|какими\s+документами\s+(?:это|его|этот\s+товар|эту\s+позицию)\s+купили|какими\s+документами\s+(?:был\s+)?куплен|каким\s+документом\s+(?:это|его|её|ее|она|он|этот\s+товар|эта\s+позиция|эту\s+позицию)?\s*(?:подтвержден[оа]?|доказан[оа]?|закрыт[оа]?)|покажи\s+документы\s+по\s+(?:этой\s+позиции|этому\s+товару|ней|нему)|документы\s+по\s+(?:этой\s+позиции|этому\s+товару|ней|нему)|purchase\s+documents|documents\s+of\s+purchase|through\s+which\s+documents)/iu.test(value) ||
/(?:(?:покажи|показать|выведи|дай)?[\s\S]{0,30}док(?:и|умент[а-яё]*)[\s\S]{0,80}(?:по\s+(?:ним|ней|нему|этой\s+позиции|этому\s+товару)|операци)|(?:по\s+(?:ним|ней|нему|этой\s+позиции|этому\s+товару))[\s\S]{0,80}док(?:и|умент[а-яё]*))/iu.test(value));
}
@@ -823,6 +827,22 @@ function mergeFollowupFilters(current, intent, userMessage, followupContext) {
reasons.push("item_cleared_for_stock_slice_aging");
}
};
const clearInventoryOnHandWarehouseAliasItem = () => {
if (intent !== "inventory_on_hand_as_of_date") {
return;
}
const currentItem = toNonEmptyString(merged.item);
const currentWarehouse = toNonEmptyString(merged.warehouse);
if (!currentItem || !currentWarehouse) {
return;
}
const normalizedItem = (0, assistantOrganizationMatcher_1.normalizeOrganizationScopeSearchText)(currentItem);
const normalizedWarehouse = (0, assistantOrganizationMatcher_1.normalizeOrganizationScopeSearchText)(currentWarehouse);
if (normalizedItem && normalizedWarehouse && normalizedItem === normalizedWarehouse) {
delete merged.item;
reasons.push("item_cleared_as_warehouse_scope_alias_for_inventory_snapshot");
}
};
if (!followupContext) {
if ((intent === "list_open_contracts" ||
intent === "open_contracts_confirmed_as_of_date" ||
@@ -838,17 +858,20 @@ function mergeFollowupFilters(current, intent, userMessage, followupContext) {
: "as_of_date_derived_from_period_for_open_contracts");
}
}
clearInventoryOnHandWarehouseAliasItem();
clearInventoryAgingOrganizationAliasItem(null);
return { filters: merged, reasons };
}
const previous = followupContext.previous_filters ?? {};
const root = followupContext.root_filters ?? {};
const previousAnchorValue = toNonEmptyString(followupContext.previous_anchor_value);
const previousCounterparty = toNonEmptyString(previous.counterparty);
const previousContract = toNonEmptyString(previous.contract);
const previousAccount = toNonEmptyString(previous.account);
const previousItem = toNonEmptyString(previous.item);
const previousAnchorItem = followupContext.previous_anchor_type === "item" ? previousAnchorValue : null;
const previousOrganization = toNonEmptyString(previous.organization);
const previousOrganization = toNonEmptyString(previous.organization) ?? toNonEmptyString(root.organization);
const previousWarehouse = toNonEmptyString(previous.warehouse) ?? toNonEmptyString(root.warehouse);
const previousAsOfDate = toNonEmptyString(previous.as_of_date);
const previousPeriodFrom = toNonEmptyString(previous.period_from);
const previousPeriodTo = toNonEmptyString(previous.period_to);
@@ -1231,6 +1254,10 @@ function mergeFollowupFilters(current, intent, userMessage, followupContext) {
reasons.push("period_derived_from_inventory_root_frame_year");
}
if (intent === "inventory_aging_by_purchase_date") {
if (previousWarehouse && !toNonEmptyString(merged.warehouse)) {
merged.warehouse = previousWarehouse;
reasons.push("warehouse_from_followup_context");
}
clearInventoryAgingOrganizationAliasItem(previousOrganization);
}
if (!sameDateRequested &&
@@ -1254,6 +1281,7 @@ function mergeFollowupFilters(current, intent, userMessage, followupContext) {
delete merged.period_to;
reasons.push("period_cleared_by_all_time_followup");
}
clearInventoryOnHandWarehouseAliasItem();
return { filters: merged, reasons };
}
if (intent === "counterparty_activity_lifecycle" &&
@@ -1414,6 +1442,7 @@ function mergeFollowupFilters(current, intent, userMessage, followupContext) {
reasons.push("counterparty_cleared_as_organization_scope_alias");
}
}
clearInventoryOnHandWarehouseAliasItem();
return { filters: merged, reasons };
}
function resolveMissingRequiredFilters(intent, filters) {
@@ -1441,16 +1470,50 @@ function resolveMissingRequiredFilters(intent, filters) {
});
}
function deriveIntentWithFollowupContext(detectedIntent, userMessage, followupContext) {
const normalizedMessage = String(userMessage ?? "");
const hasSelectedObjectReference = hasSelectedObjectInventorySignal(normalizedMessage);
if (!followupContext || (!followupContext.previous_intent && !followupContext.target_intent)) {
if (hasSelectedObjectReference && hasInventoryPurchaseDocumentsFollowupCue(normalizedMessage)) {
if (detectedIntent.intent === "unknown" ||
detectedIntent.intent === "list_documents_by_counterparty" ||
detectedIntent.intent === "list_documents_by_contract" ||
detectedIntent.intent === "inventory_on_hand_as_of_date") {
return {
intent: "inventory_purchase_documents_for_item",
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_adjusted_to_inline_selected_object_inventory_documents"]
};
}
}
if (hasSelectedObjectReference && hasInventorySupplierFollowupCue(normalizedMessage)) {
if (detectedIntent.intent === "unknown" ||
detectedIntent.intent === "list_documents_by_counterparty" ||
detectedIntent.intent === "list_documents_by_contract" ||
detectedIntent.intent === "inventory_on_hand_as_of_date") {
return {
intent: "inventory_purchase_provenance_for_item",
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_adjusted_to_inline_selected_object_inventory_supplier"]
};
}
}
if (hasSelectedObjectReference && hasInventoryPurchaseDateFollowupCue(normalizedMessage)) {
if (detectedIntent.intent === "unknown" || detectedIntent.intent === "inventory_on_hand_as_of_date") {
return {
intent: "inventory_purchase_provenance_for_item",
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_adjusted_to_inline_selected_object_inventory_purchase_date"]
};
}
}
return detectedIntent;
}
const normalizedMessage = String(userMessage ?? "");
const genericFollowupSignal = hasAddressFollowupContextSignal(normalizedMessage);
const previousFilters = followupContext.previous_filters ?? {};
const purchaseBridgeContinuationSignal = followupContext.previous_intent === "vat_liability_confirmed_for_tax_period" &&
Boolean(toNonEmptyString(previousFilters.purchase_date_bridge_selected)) &&
hasInventoryPurchaseDateVatBridgeContinuationCue(normalizedMessage);
const hasFollowupSignal = genericFollowupSignal || purchaseBridgeContinuationSignal;
const hasFollowupSignal = genericFollowupSignal || purchaseBridgeContinuationSignal || hasSelectedObjectReference;
if (!hasFollowupSignal) {
return detectedIntent;
}
@@ -1481,7 +1544,6 @@ function deriveIntentWithFollowupContext(detectedIntent, userMessage, followupCo
followupContext.root_anchor_type === "item" ||
followupContext.current_frame_kind === "inventory_root" ||
followupContext.current_frame_kind === "inventory_drilldown";
const hasSelectedObjectReference = hasSelectedObjectInventorySignal(normalizedMessage);
const inventorySelectedObjectFollowup = inventoryLineageActive && (hasSelectedObjectReference || (previousIsInventoryFamily && hasFollowupSignal));
const previousCounterpartyLaneActive = hasPreviousCounterparty &&
(followupContext.previous_anchor_type === "counterparty" ||
@@ -57,6 +57,53 @@ function inventoryRequestedPartyMatches(requested, actualParties) {
function inventoryPartyListOrUnknown(parties) {
return parties.length > 0 ? parties.slice(0, 4).join("; ") : "не выделен отдельным полем";
}
function inventoryPartyLeadList(parties, limit = 6) {
const visible = parties.slice(0, limit);
const remaining = parties.length - visible.length;
const suffix = remaining > 0 ? `; и еще ${remaining}` : "";
return `${visible.join("; ")}${suffix}`;
}
function inventoryItemLeadList(items, limit = 6) {
const visible = items.slice(0, limit);
const remaining = items.length - visible.length;
const suffix = remaining > 0 ? `; и еще ${remaining}` : "";
return `${visible.join("; ")}${suffix}`;
}
function collectInventoryTraceItems(rows) {
const result = [];
const seen = new Set();
for (const row of rows) {
const item = String(row.item ?? "").trim();
if (!item) {
continue;
}
const comparable = normalizeInventoryReplyEntityToken(item);
if (!comparable || seen.has(comparable)) {
continue;
}
seen.add(comparable);
result.push(item);
}
return result;
}
function extractInventoryCounterpartiesFromEvidenceLines(lines) {
const counterparties = [];
for (const line of lines) {
const match = /\u043a\u043e\u043d\u0442\u0440\u0430\u0433\u0435\u043d\u0442:\s*([^|]+)/iu.exec(line);
const value = String(match?.[1] ?? "").replace(/\s+/gu, " ").trim();
if (value) {
counterparties.push(value);
}
}
return counterparties;
}
function hasSupplierScopedItemListQuestion(options) {
if (!String(options.counterpartyHint ?? "").trim()) {
return false;
}
const text = `${options.rawUserMessage ?? ""} ${options.userMessage ?? ""}`;
return /(?:какие?\s+товар|товар(?:ы|ов)?\s+(?:от|у)\s+поставщик|позици(?:и|я|ю)|номенклатур|лежат|остат(?:ок|ки|ка|ков))/iu.test(text);
}
function omitAmountFromInventoryTraceEvidence(lines) {
return lines.map((line) => String(line ?? "").replace(/\s+\|\s+сумма:\s+[^|]+(?=\s+\||$)/gu, ""));
}
@@ -131,6 +178,36 @@ function sumInventoryRowAmount(rows) {
function sumInventoryRowQuantity(rows) {
return rows.reduce((sum, row) => sum + (typeof row.quantity === "number" && Number.isFinite(row.quantity) ? row.quantity : 0), 0);
}
function cleanInventoryScopeHint(value) {
const cleaned = String(value ?? "").replace(/\s+/gu, " ").trim();
return cleaned.length > 0 ? cleaned : null;
}
function inventoryOnHandAccountScopeLabel(options) {
const accountHint = cleanInventoryScopeHint(options.accountHint);
if (accountHint) {
return `по счету ${accountHint}`;
}
const text = `${options.rawUserMessage ?? ""} ${options.userMessage ?? ""}`;
return /(?:^|[^\d])41(?:[.,]\d+)?(?:[^\d]|$)|41\s*сч[её]т/iu.test(text) ? "по счету 41" : null;
}
function inventoryOnHandWarehouseScopeLabel(options, uniqueWarehouses) {
const warehouseHint = cleanInventoryScopeHint(options.warehouseHint);
if (warehouseHint) {
return `по складу ${warehouseHint}`;
}
const text = `${options.rawUserMessage ?? ""} ${options.userMessage ?? ""}`;
if (uniqueWarehouses.length === 1 && /(?:по|на)\s+склад[уе]?/iu.test(text)) {
return `по складу ${uniqueWarehouses[0]}`;
}
return null;
}
function inventoryOnHandScopePhrase(options, uniqueWarehouses) {
const scopes = [
inventoryOnHandAccountScopeLabel(options),
inventoryOnHandWarehouseScopeLabel(options, uniqueWarehouses)
].filter((item) => Boolean(item));
return scopes.length > 0 ? scopes.join(" и ") : "на складе";
}
function formatInventoryPercent(value, formatNumberWithDots) {
return value === null || !Number.isFinite(value) ? "не подтверждена" : `${formatNumberWithDots(value, 2)}%`;
}
@@ -222,9 +299,10 @@ function composeInventoryReply(intent, rows, options, deps) {
const uniqueItems = deps.uniqueStrings(positions.map((item) => item.item));
const uniqueWarehouses = deps.uniqueStrings(positions.map((item) => String(item.warehouse ?? "").trim()).filter((item) => item.length > 0));
const totalAmount = positions.reduce((sum, item) => sum + item.amount, 0);
const scopePhrase = inventoryOnHandScopePhrase(options, uniqueWarehouses);
const directAnswerLine = positions.length > 0
? `На ${deps.formatDateRu(asOfDate)} на складе подтверждено ${deps.formatNumberWithDots(positions.length)} позиций на ${deps.formatMoneyRub(totalAmount)}.`
: `На ${deps.formatDateRu(asOfDate)} подтвержденных товарных остатков по счету 41.01 не найдено.`;
? `На ${deps.formatDateRu(asOfDate)} ${scopePhrase} подтверждено ${deps.formatNumberWithDots(positions.length)} позиций на ${deps.formatMoneyRub(totalAmount)}.`
: `На ${deps.formatDateRu(asOfDate)} ${scopePhrase} подтвержденных товарных остатков не найдено.`;
const lines = [directAnswerLine];
if (positions.length > 0) {
const visiblePositionsLimit = 6;
@@ -250,13 +328,22 @@ function composeInventoryReply(intent, rows, options, deps) {
"- На дату среза товары с ненулевым остатком не найдены."
]);
}
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Сводка:", [
const summaryBullets = [
`Дата среза: ${deps.formatDateRu(asOfDate)}.`,
`Позиции с остатком: ${deps.formatNumberWithDots(positions.length)}.`,
`Уникальных товаров: ${deps.formatNumberWithDots(uniqueItems.length)}.`,
`Уникальных складов: ${deps.formatNumberWithDots(uniqueWarehouses.length)}.`,
"Общее количество не свожу в один управленческий показатель, потому что в остатках смешаны разнородные позиции."
]);
];
const accountScope = inventoryOnHandAccountScopeLabel(options);
const warehouseScope = inventoryOnHandWarehouseScopeLabel(options, uniqueWarehouses);
if (accountScope) {
summaryBullets.splice(1, 0, `Срез ${accountScope}.`);
}
if (warehouseScope) {
summaryBullets.splice(accountScope ? 2 : 1, 0, `Срез ${warehouseScope}.`);
}
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Сводка:", summaryBullets);
if (rows.length !== positions.length) {
lines.push(`- Проверенных строк движения: ${deps.formatNumberWithDots(rows.length)}.`);
}
@@ -398,49 +485,111 @@ function composeInventoryReply(intent, rows, options, deps) {
const purchaseRows = rows.filter((row) => deps.isInventoryPurchaseMovement(row));
const summary = deps.summarizeInventoryTraceRows(purchaseRows);
normalizeInventoryTraceSummaryCounterparties(summary);
const unresolvedRows = purchaseRows.filter((row) => deps.extractInventoryCounterpartyCandidates(row).length === 0);
const unresolvedSupplierQuestion = /(?:\u0431\u0435\u0437\s+\u043f\u043e\u043d\u044f\u0442\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u0431\u0435\u0437\s+(?:\u044f\u0432\u043d[^\s]*\s+)?\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043d\u0435\s+\u0438\u043c\u0435\u044e\u0442\s+\u044f\u0432\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043d\u0435\u0442\s+\u044f\u0432\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|unresolved\s+supplier\s+link)/iu.test(String(options.userMessage ?? ""));
const purchaseRowCounterparties = purchaseRows.map((row) => deps.uniqueStrings([
...deps.extractInventoryCounterpartyCandidates(row),
...extractInventoryCounterpartiesFromEvidenceLines(deps.formatInventoryTraceRows([row], 1))
]));
const observedCounterparties = deps.uniqueStrings([
...summary.counterparties,
...purchaseRowCounterparties.flat()
]);
const unresolvedRows = purchaseRows.filter((_, index) => (purchaseRowCounterparties[index]?.length ?? 0) === 0);
const unresolvedSupplierQuestionText = `${String(options.userMessage ?? "")}\n${String(options.rawUserMessage ?? "")}`;
const unresolvedSupplierQuestion = /(?:\u0431\u0435\u0437\s+\u043f\u043e\u043d\u044f\u0442\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u0431\u0435\u0437\s+(?:\u044f\u0432\u043d[^\s]*\s+)?\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043d\u0435\s+\u0438\u043c\u0435\u044e\u0442\s+\u044f\u0432\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043d\u0435\u0442\s+\u044f\u0432\u043d[^\s]*\s+\u043f\u0440\u0438\u0432\u044f\u0437\u043a[^\s]*\s+\u043a\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|unresolved\s+supplier\s+link)/iu.test(unresolvedSupplierQuestionText);
if (unresolvedSupplierQuestion) {
const unresolvedItems = collectInventoryTraceItems(unresolvedRows);
const unresolvedItemPreviewLimit = 3;
const unresolvedDocumentPreviewLimit = 2;
const directAnswerLine = unresolvedRows.length > 0
? `В текущем складском срезе найдено операций без явно выделенного поставщика: ${deps.formatNumberWithDots(unresolvedRows.length)}.`
? unresolvedItems.length > 0
? `В текущем складском срезе на ${deps.formatDateRu(asOfDate)} найдено ${deps.formatNumberWithDots(unresolvedItems.length)} позиций без надежной привязки к поставщику; первые примеры ниже.`
: `В текущем складском срезе на ${deps.formatDateRu(asOfDate)} найдено операций без явно выделенного поставщика: ${deps.formatNumberWithDots(unresolvedRows.length)}.`
: "В текущем складском срезе товары без явно выделенной привязки к поставщику в доступных данных не найдены.";
const lines = [directAnswerLine];
if (unresolvedItems.length > 0) {
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Позиции без надежной привязки к поставщику:", unresolvedItems.slice(0, unresolvedItemPreviewLimit).map((item, index) => `${index + 1}. ${item}`));
if (unresolvedItems.length > unresolvedItemPreviewLimit) {
lines.push(`- Показаны первые ${unresolvedItemPreviewLimit} из ${deps.formatNumberWithDots(unresolvedItems.length)} таких позиций.`);
}
}
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Что проверили:", [
`Дата среза: ${deps.formatDateRu(asOfDate)}.`,
`Закупочных операций в выборке: ${deps.formatNumberWithDots(purchaseRows.length)}.`,
`Операций без явно выделенного поставщика: ${deps.formatNumberWithDots(unresolvedRows.length)}.`,
`Поставщиков, выделенных в остальных операциях: ${deps.formatNumberWithDots(summary.counterparties.length)}.`
`Поставщиков, выделенных в остальных операциях: ${deps.formatNumberWithDots(observedCounterparties.length)}.`
]);
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Ограничения:", [
"Без партионного учета это проверка доступного закупочного следа по складскому срезу, а не юридическое доказательство владельца каждой партии."
]);
if (unresolvedRows.length > 0) {
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Позиции без явно выделенного поставщика:", deps.formatInventoryTraceRows(unresolvedRows, 12));
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Опорные документы:", deps.formatInventoryTraceRows(unresolvedRows, unresolvedDocumentPreviewLimit));
if (unresolvedRows.length > unresolvedDocumentPreviewLimit) {
lines.push(`- Показаны первые ${unresolvedDocumentPreviewLimit} из ${deps.formatNumberWithDots(unresolvedRows.length)} строк без выделенного поставщика.`);
}
}
else if (summary.counterparties.length > 0) {
lines.push(`- В доступном закупочном следе встречаются поставщики: ${summary.counterparties.slice(0, 6).join("; ")}.`);
else if (observedCounterparties.length > 0) {
lines.push(`- В доступном закупочном следе встречаются поставщики: ${observedCounterparties.slice(0, 6).join("; ")}.`);
}
return (0, replyContracts_1.buildFactualSummaryReply)(lines, (0, replyContracts_1.buildConfirmedBalanceSemantics)(unresolvedRows.length > 0 ? "medium" : "strong", true));
}
const warehouseLabel = summary.warehouses[0] ?? "не указанного склада";
const directAnswerLine = summary.counterparties.length === 1
? `По складскому остатку ${warehouseLabel} выявлен поставщик: ${summary.counterparties[0]}.`
: summary.counterparties.length > 1
? `По складскому остатку ${warehouseLabel} найдено несколько поставщиков: ${summary.counterparties.slice(0, 6).join("; ")}.`
: `По складскому остатку ${warehouseLabel} поставщик в доступных данных не выделен.`;
const purchasePeriodLabel = summary.firstPeriod && summary.lastPeriod
? `${deps.inventoryTraceDateLabel(summary.firstPeriod)}..${deps.inventoryTraceDateLabel(summary.lastPeriod)}`
: "даты закупки не выделены";
const supplierScopedItems = collectInventoryTraceItems(purchaseRows);
const supplierScopedItemQuestion = hasSupplierScopedItemListQuestion(options);
if (supplierScopedItemQuestion) {
const requestedSupplierLabel = summary.counterparties[0] ?? String(options.counterpartyHint ?? "").trim();
const directAnswerLine = supplierScopedItems.length > 0
? `По поставщику ${requestedSupplierLabel} в складском срезе ${warehouseLabel} на ${deps.formatDateRu(asOfDate)} подтверждены позиции: ${inventoryItemLeadList(supplierScopedItems)}.`
: `По поставщику ${requestedSupplierLabel} в складском срезе ${warehouseLabel} на ${deps.formatDateRu(asOfDate)} позиции не подтверждены в доступном закупочном следе.`;
const lines = [directAnswerLine];
if (supplierScopedItems.length > 0) {
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Позиции:", supplierScopedItems.slice(0, 8).map((item, index) => `${index + 1}. ${item}`));
if (supplierScopedItems.length > 8) {
lines.push(`- Показаны первые 8 из ${deps.formatNumberWithDots(supplierScopedItems.length)} позиций.`);
}
}
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Что проверили:", [
`Дата среза: ${deps.formatDateRu(asOfDate)}.`,
`Период найденного закупочного следа: ${purchasePeriodLabel}.`,
`Закупочных документов / операций в выборке: ${deps.formatNumberWithDots(summary.documents.length)} / ${deps.formatNumberWithDots(purchaseRows.length)}.`
]);
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Ограничения:", [
"Без партионного учета этот ответ показывает закупочный след текущего остатка, но не доказывает владельца каждой конкретной партии."
]);
if (observedCounterparties.length > 0) {
lines.push(`- Найденные поставщики: ${inventoryPartyLeadList(observedCounterparties)}.`);
}
if (purchaseRows.length > 0) {
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Опорные документы:", deps.formatInventoryTraceRows(purchaseRows, INVENTORY_TRACE_EVIDENCE_ROW_LIMIT));
if (purchaseRows.length > INVENTORY_TRACE_EVIDENCE_ROW_LIMIT) {
lines.push(`- Показаны первые ${INVENTORY_TRACE_EVIDENCE_ROW_LIMIT} из ${deps.formatNumberWithDots(purchaseRows.length)} найденных строк; полный след остается в подтвержденном срезе.`);
}
}
return (0, replyContracts_1.buildFactualSummaryReply)(lines, (0, replyContracts_1.buildConfirmedBalanceSemantics)(purchaseRows.length > 0 ? (supplierScopedItems.length > 0 ? "strong" : "medium") : "medium", purchaseRows.length > 0));
}
const directAnswerLine = observedCounterparties.length === 1
? `По складскому остатку ${warehouseLabel} на ${deps.formatDateRu(asOfDate)} выявлен поставщик закупочного следа: ${observedCounterparties[0]}.`
: observedCounterparties.length > 1
? `По складскому остатку ${warehouseLabel} на ${deps.formatDateRu(asOfDate)} найдено несколько поставщиков закупочного следа: ${inventoryPartyLeadList(observedCounterparties)}.`
: unresolvedRows.length > 0
? `По складскому остатку ${warehouseLabel} на ${deps.formatDateRu(asOfDate)} однозначный поставщик текущего остатка не подтвержден: в ${deps.formatNumberWithDots(unresolvedRows.length)} закупочных операциях поставщик не выделен.`
: `По складскому остатку ${warehouseLabel} на ${deps.formatDateRu(asOfDate)} поставщик в доступных данных не выделен.`;
const lines = [directAnswerLine];
if (unresolvedRows.length > 0) {
lines.push(`По складскому остатку ${warehouseLabel} на ${deps.formatDateRu(asOfDate)} однозначная атрибуция части текущего остатка не подтверждена: ${deps.formatNumberWithDots(unresolvedRows.length)} закупочных операций без явно выделенного поставщика.`);
}
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Что проверили:", [
`Дата среза: ${deps.formatDateRu(asOfDate)}.`,
`Первая найденная дата закупки: ${deps.inventoryTraceDateLabel(summary.firstPeriod)}.`,
`Последняя найденная дата закупки: ${deps.inventoryTraceDateLabel(summary.lastPeriod)}.`,
`Закупочных документов в выборке: ${deps.formatNumberWithDots(summary.documents.length)}.`,
`Закупочных операций в выборке: ${deps.formatNumberWithDots(purchaseRows.length)}.`
`Период найденного закупочного следа: ${purchasePeriodLabel}.`,
`Закупочных документов / операций в выборке: ${deps.formatNumberWithDots(summary.documents.length)} / ${deps.formatNumberWithDots(purchaseRows.length)}.`
]);
(0, inventoryReplyPresentation_1.appendInventoryBulletSection)(lines, "Ограничения:", [
"Без партионного учета этот ответ показывает закупочный след текущего остатка, но не доказывает владельца каждой конкретной партии."
]);
if (summary.counterparties.length > 0) {
lines.push(`- Найденные поставщики: ${summary.counterparties.slice(0, 6).join("; ")}.`);
if (observedCounterparties.length > 0) {
lines.push(`- Найденные поставщики: ${inventoryPartyLeadList(observedCounterparties)}.`);
}
else if (purchaseRows.length > 0) {
lines.push("- Закупочные движения найдены, но поставщик не выделен отдельным полем в доступных данных.");
@@ -448,11 +597,11 @@ function composeInventoryReply(intent, rows, options, deps) {
else {
lines.push("- В доступных данных не найдено закупочных движений по выбранному складскому срезу.");
}
if (unresolvedRows.length > 0) {
lines.push(`- Операций без явно выделенного поставщика: ${deps.formatNumberWithDots(unresolvedRows.length)}.`);
}
if (purchaseRows.length > 0) {
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Опорные документы:", deps.formatInventoryTraceRows(purchaseRows, 10));
(0, inventoryReplyPresentation_1.appendInventorySection)(lines, "Опорные документы:", deps.formatInventoryTraceRows(purchaseRows, INVENTORY_TRACE_EVIDENCE_ROW_LIMIT));
if (purchaseRows.length > INVENTORY_TRACE_EVIDENCE_ROW_LIMIT) {
lines.push(`- Показаны первые ${INVENTORY_TRACE_EVIDENCE_ROW_LIMIT} из ${deps.formatNumberWithDots(purchaseRows.length)} найденных строк; полный след остается в подтвержденном срезе.`);
}
}
return (0, replyContracts_1.buildFactualSummaryReply)(lines, (0, replyContracts_1.buildConfirmedBalanceSemantics)(purchaseRows.length > 0 ? (summary.counterparties.length > 0 ? "strong" : "medium") : "medium", purchaseRows.length > 0));
}
@@ -770,17 +919,29 @@ function composeInventoryReply(intent, rows, options, deps) {
const supplierMatches = inventoryRequestedPartyMatches(requestedSupplier, purchaseSummary.counterparties);
const buyerMatches = inventoryRequestedPartyMatches(requestedBuyer, saleSummary.counterparties);
const mismatchParts = [];
if (requestedSupplier && purchaseRows.length > 0 && !supplierMatches) {
if (requestedSupplier && purchaseRows.length === 0) {
mismatchParts.push(`закупка у запрошенного поставщика ${requestedSupplier} не найдена`);
}
else if (requestedSupplier && purchaseRows.length > 0 && !supplierMatches) {
mismatchParts.push(`запрошенный поставщик ${requestedSupplier} не совпал с найденным поставщиком: ${inventoryPartyListOrUnknown(purchaseSummary.counterparties)}`);
}
if (requestedBuyer && saleRows.length > 0 && !buyerMatches) {
if (requestedBuyer && saleRows.length === 0) {
mismatchParts.push(`продажа/выбытие запрошенному покупателю ${requestedBuyer} не найдены`);
}
else if (requestedBuyer && saleRows.length > 0 && !buyerMatches) {
mismatchParts.push(`запрошенный покупатель ${requestedBuyer} не совпал с найденным покупателем: ${inventoryPartyListOrUnknown(saleSummary.counterparties)}`);
}
const directAnswerLine = mismatchParts.length > 0
? `Запрошенная цепочка по товару ${itemLabel} полностью не подтверждена: ${mismatchParts.join("; ")}.`
: purchaseSummary.counterparties.length === 1 && saleSummary.counterparties.length === 1
: purchaseRows.length > 0 && saleRows.length > 0 && purchaseSummary.counterparties.length === 1 && saleSummary.counterparties.length === 1
? `По товару ${itemLabel} цепочка поставки и продажи связана с поставщиком ${purchaseSummary.counterparties[0]} и покупателем ${saleSummary.counterparties[0]}.`
: `По товару ${itemLabel} цепочка поставки и продажи подтверждена частично или разнообразно: детали идут следом.`;
: purchaseRows.length > 0 && saleRows.length > 0
? `По товару ${itemLabel} найдены обе стороны цепочки поставки и продажи; участников несколько или они неоднозначны, детали ниже.`
: purchaseRows.length > 0
? `По товару ${itemLabel} закупочная часть цепочки подтверждена, но продажа/выбытие со счета 41.01 в доступных данных не найдены.`
: saleRows.length > 0
? `По товару ${itemLabel} найдена продажа/выбытие со счета 41.01, но закупочная часть цепочки в доступных данных не подтверждена.`
: `По товару ${itemLabel} цепочка поставки и продажи не подтверждена: движений по счету 41.01 в доступных данных не найдено.`;
const lines = [directAnswerLine, "", "Подтверждение:"];
lines.push(`- Строк закупки на 41.01: ${deps.formatNumberWithDots(purchaseRows.length)}.`);
lines.push(`- Строк продажи со счета 41.01: ${deps.formatNumberWithDots(saleRows.length)}.`);
@@ -133,11 +133,17 @@ function applyAddressLlmSemanticHintsToExtraction(extraction, semanticHintsInput
semanticFrame.anchor_value = scopeTargetText;
}
if (semanticHints.scope_target_kind === "counterparty" && scopeTargetText) {
extractedFilters.counterparty = scopeTargetText;
pushWarning(warnings, "counterparty_from_llm_semantics");
semanticFrame.scope_kind = "explicit_anchor";
semanticFrame.anchor_kind = "counterparty";
semanticFrame.anchor_value = scopeTargetText;
if ((0, addressFilterExtractor_1.isLowQualityCounterpartyAnchorValue)(scopeTargetText)) {
delete extractedFilters.counterparty;
pushWarning(warnings, "counterparty_cleared_low_quality_llm_semantics");
}
else {
extractedFilters.counterparty = scopeTargetText;
pushWarning(warnings, "counterparty_from_llm_semantics");
semanticFrame.scope_kind = "explicit_anchor";
semanticFrame.anchor_kind = "counterparty";
semanticFrame.anchor_value = scopeTargetText;
}
}
if (semanticHints.scope_target_kind === "contract" && scopeTargetText) {
extractedFilters.contract = scopeTargetText;
@@ -159,6 +165,11 @@ function applyAddressLlmSemanticHintsToExtraction(extraction, semanticHintsInput
pushWarning(warnings, "item_llm_semantics_ignored");
}
}
if (toNonEmptyString(extractedFilters.counterparty) &&
(0, addressFilterExtractor_1.isLowQualityCounterpartyAnchorValue)(String(extractedFilters.counterparty))) {
delete extractedFilters.counterparty;
pushWarning(warnings, "counterparty_cleared_low_quality_llm_semantics");
}
return {
...extraction,
extracted_filters: extractedFilters,
@@ -31,6 +31,182 @@ function toNullableBoolean(value) {
function normalizeAddressReplyType(value) {
return value === "factual" || value === "partial_coverage" ? value : "partial_coverage";
}
function uniqueNormalizedStrings(values) {
const result = [];
const seen = new Set();
for (const value of values) {
const text = String(value ?? "").trim();
if (!text) {
continue;
}
const key = text.toLocaleLowerCase("ru-RU").replace(/ё/g, "е");
if (seen.has(key)) {
continue;
}
seen.add(key);
result.push(text);
}
return result;
}
function extractInventorySupplierOverlapCounterpartiesFromReply(replyText) {
const values = [];
const pattern = /\u043a\u043e\u043d\u0442\u0440\u0430\u0433\u0435\u043d\u0442:\s*([^|\r\n]+)/giu;
for (const match of replyText.matchAll(pattern)) {
const value = toNullableString(match[1]?.replace(/[.;,\s]+$/u, ""));
if (!value || /^(?:\u043d\u0435\s+\u0432\u044b\u0434\u0435\u043b\u0435\u043d|\u043d\/\u0434|unknown)$/iu.test(value)) {
continue;
}
values.push(value);
}
return uniqueNormalizedStrings(values);
}
function formatInventorySupplierOverlapCounterpartyLead(counterparties, limit = 6) {
const visible = counterparties.slice(0, limit);
const remaining = counterparties.length - visible.length;
const suffix = remaining > 0 ? `; и еще ${remaining}` : "";
return `${visible.join("; ")}${suffix}`;
}
function hasInventorySupplierOverlapDefensiveLead(firstLine) {
const normalized = String(firstLine ?? "").toLocaleLowerCase("ru-RU").replace(/\u0451/gu, "\u0435");
const startsWithStockScope = /^\u043f\u043e\s+(?:\u0441\u043a\u043b\u0430\u0434\u0441\u043a\u043e\u043c\u0443\s+\u043e\u0441\u0442\u0430\u0442\u043a\u0443|\u043e\u043a\u043d\u0443)/u.test(normalized);
const hasDefensiveAttribution = /\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d(?:\u044b\u0439\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u0430\u044f\s+\u0430\u0442\u0440\u0438\u0431\u0443\u0446\u0438\u044f).*?\u043d\u0435\s+\u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d/u.test(normalized);
return startsWithStockScope && hasDefensiveAttribution;
}
function firstNonEmptyReplyLine(text) {
return String(text ?? "")
.split(/\r?\n/u)
.map((line) => line.trim())
.find((line) => line.length > 0) ?? "";
}
function hasInventorySupplierOverlapCounterpartyLead(firstLine) {
const normalized = String(firstLine ?? "").toLocaleLowerCase("ru-RU").replace(/\u0451/gu, "\u0435");
return (/^\u043f\u043e\s+\u0441\u043a\u043b\u0430\u0434\u0441\u043a\u043e\u043c\u0443\s+\u043e\u0441\u0442\u0430\u0442\u043a\u0443/u.test(normalized) &&
/\u043d\u0430\u0439\u0434\u0435\u043d/u.test(normalized) &&
/\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a/u.test(normalized) &&
/\u0437\u0430\u043a\u0443\u043f\u043e\u0447\u043d\u043e\u0433\u043e\s+\u0441\u043b\u0435\u0434\u0430/u.test(normalized));
}
function preserveInventorySupplierOverlapCounterpartyLeadAfterSanitize(input) {
const debug = toRecordObject(input.addressDebug);
const detectedIntent = toNullableString(debug?.detected_intent);
if (detectedIntent !== "inventory_supplier_stock_overlap_as_of_date") {
return { text: input.sanitizedReply, applied: false };
}
const rawLead = firstNonEmptyReplyLine(input.rawReply);
if (!hasInventorySupplierOverlapCounterpartyLead(rawLead)) {
return { text: input.sanitizedReply, applied: false };
}
const sanitizedLead = firstNonEmptyReplyLine(input.sanitizedReply);
if (hasInventorySupplierOverlapCounterpartyLead(sanitizedLead)) {
return { text: input.sanitizedReply, applied: false };
}
const sanitizedText = String(input.sanitizedReply ?? "").trim();
return {
text: sanitizedText ? [rawLead, sanitizedText].join("\n\n") : rawLead,
applied: true
};
}
function inspectInventorySupplierOverlapDefensiveLead(firstLine) {
const normalized = String(firstLine ?? "").toLocaleLowerCase("ru-RU").replace(/\u0451/gu, "\u0435");
return {
stockScopeMatch: /^\u043f\u043e\s+(?:\u0441\u043a\u043b\u0430\u0434\u0441\u043a\u043e\u043c\u0443\s+\u043e\u0441\u0442\u0430\u0442\u043a\u0443|\u043e\u043a\u043d\u0443)/u.test(normalized),
defensiveMatch: /\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d(?:\u044b\u0439\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u0430\u044f\s+\u0430\u0442\u0440\u0438\u0431\u0443\u0446\u0438\u044f).*?\u043d\u0435\s+\u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d/u.test(normalized)
};
}
function buildInventorySupplierOverlapSurfaceRepairAudit(input) {
return {
schema_version: "inventory_supplier_overlap_surface_repair_v1",
applied: input.applied,
detected_intent: input.detectedIntent,
counterparties: input.counterparties ?? [],
first_line_sample: input.firstLineSample ?? null,
stock_scope_match: input.stockScopeMatch ?? null,
defensive_match: input.defensiveMatch ?? null,
reason_codes: input.reasonCodes
};
}
function repairInventorySupplierOverlapSurfaceReply(replyText, addressDebug) {
const text = String(replyText ?? "");
const debug = toRecordObject(addressDebug);
const detectedIntent = toNullableString(debug?.detected_intent);
if (detectedIntent !== "inventory_supplier_stock_overlap_as_of_date") {
return {
text,
audit: buildInventorySupplierOverlapSurfaceRepairAudit({
applied: false,
detectedIntent,
reasonCodes: ["inventory_supplier_overlap_surface_repair_intent_not_applicable"]
})
};
}
const lines = text.split(/\r?\n/u);
const firstLine = lines[0]?.trim() ?? "";
const leadInspection = inspectInventorySupplierOverlapDefensiveLead(firstLine);
if (!hasInventorySupplierOverlapDefensiveLead(firstLine)) {
return {
text,
audit: buildInventorySupplierOverlapSurfaceRepairAudit({
applied: false,
detectedIntent,
firstLineSample: firstLine.slice(0, 220),
stockScopeMatch: leadInspection.stockScopeMatch,
defensiveMatch: leadInspection.defensiveMatch,
reasonCodes: ["inventory_supplier_overlap_surface_repair_first_line_not_defensive"]
})
};
}
const counterparties = extractInventorySupplierOverlapCounterpartiesFromReply(text);
if (counterparties.length === 0) {
return {
text,
audit: buildInventorySupplierOverlapSurfaceRepairAudit({
applied: false,
detectedIntent,
counterparties,
firstLineSample: firstLine.slice(0, 220),
stockScopeMatch: leadInspection.stockScopeMatch,
defensiveMatch: leadInspection.defensiveMatch,
reasonCodes: ["inventory_supplier_overlap_surface_repair_no_counterparties"]
})
};
}
const repairedLead = counterparties.length === 1
? `По складскому остатку найден поставщик закупочного следа: ${counterparties[0]}.`
: `По складскому остатку найдено несколько поставщиков закупочного следа: ${formatInventorySupplierOverlapCounterpartyLead(counterparties)}.`;
return {
text: [repairedLead, firstLine, ...lines.slice(1)].join("\n"),
audit: buildInventorySupplierOverlapSurfaceRepairAudit({
applied: true,
detectedIntent,
counterparties,
firstLineSample: firstLine.slice(0, 220),
stockScopeMatch: leadInspection.stockScopeMatch,
defensiveMatch: leadInspection.defensiveMatch,
reasonCodes: ["inventory_supplier_overlap_surface_repair_applied"]
})
};
}
function extractSingleWarehouseScopeFromReply(replyText) {
const values = [];
const normalized = String(replyText ?? "");
const byFieldPattern = /(?:^|\|)\s*склад\s*:\s*([^|\r\n]+)/giu;
for (const match of normalized.matchAll(byFieldPattern)) {
const value = toNullableString(match[1]?.replace(/[.;,\s]+$/u, ""));
if (value) {
values.push(value);
}
}
const fieldUnique = uniqueNormalizedStrings(values);
if (fieldUnique.length > 0) {
return fieldUnique.length === 1 ? fieldUnique[0] : null;
}
const leadMatch = normalized.match(/(?:по|на)\s+склад[уе]?\s+([^.,;\r\n]+)/iu);
const leadWarehouse = toNullableString(leadMatch?.[1]?.replace(/[.;,\s]+$/u, ""));
if (leadWarehouse) {
values.push(leadWarehouse);
}
const unique = uniqueNormalizedStrings(values);
return unique.length === 1 ? unique[0] : null;
}
function sameBusinessLabel(left, right) {
const normalizedLeft = toNullableString(left)?.toLocaleLowerCase("ru-RU").replace(/ё/g, "е");
const normalizedRight = toNullableString(right)?.toLocaleLowerCase("ru-RU").replace(/ё/g, "е");
@@ -65,7 +241,7 @@ function cleanComparisonScopeCompanyLine(line, organization) {
.replace(/\bcompany-level\b/giu, "общий по компании")
.replace(/\breusable bundle\b/giu, "сохраненный подтвержденный срез");
if (organization) {
clean = clean.replace(/по компании\s+Альтернатива Плюс/iu, `по компании ${organization}`);
clean = clean.replace(/по компании\s+.+?(?=\s+(?:подтвержден|подтвержд[её]н|зафиксирован|показан|есть)\b|:|$)/iu, `по компании ${organization}`);
}
return clean.trim();
}
@@ -77,9 +253,7 @@ function hasComparisonScopeProofCue(value) {
return /(?:\bсравни\b|собери\s+коротк\p{L}*\s+итог|коротк\p{L}*\s+сравн|что\s+.*подтверд\p{L}*.*(?:компан|контрагент|свк)|что\s+.*отдельно\s+по\s+(?:группа\s+свк|выбранн\p{L}*\s+контрагент)|какие\s+выводы\s+можно\s+делать\s+и\s+какие\s+нельзя)/iu.test(text);
}
function buildComparisonScopeProofReply(input) {
if (!hasComparisonScopeProofCue(input.userMessage)) {
return null;
}
const legalOrganizationFromCurrentTurn = legalOrganizationLabelFromClarification(input.userMessage);
const entryPoint = toRecordObject(input.debug.assistant_mcp_discovery_entry_point_v1);
const turnInput = toRecordObject(entryPoint?.turn_input);
const turnMeaning = toRecordObject(turnInput?.turn_meaning_ref);
@@ -87,6 +261,9 @@ function buildComparisonScopeProofReply(input) {
if (!isBusinessOverview) {
return null;
}
if (!hasComparisonScopeProofCue(input.userMessage) && !legalOrganizationFromCurrentTurn) {
return null;
}
const separateCandidates = Array.isArray(turnMeaning?.business_overview_separate_entity_candidates)
? turnMeaning.business_overview_separate_entity_candidates
: [];
@@ -113,7 +290,7 @@ function buildComparisonScopeProofReply(input) {
if (!incomingAmount && !outgoingAmount && !netAmount) {
return null;
}
const organization = legalOrganizationLabelFromClarification(input.userMessage)
const organization = legalOrganizationFromCurrentTurn
?? toNullableString(turnMeaning?.explicit_organization_scope)
?? toNullableString(toRecordObject(comparisonScope?.organization)?.label);
const documentCount = Number(toRecordObject(documentBundle)?.document_count);
@@ -277,8 +454,21 @@ function normalizeLlmPreDecomposeMeta(value) {
}
function runAssistantAddressLaneResponseRuntime(input) {
const finalizeAddressTurnSafe = input.finalizeAddressTurn ?? assistantAddressTurnFinalizeRuntimeAdapter_1.finalizeAssistantAddressTurn;
const safeAddressReply = input.sanitizeOutgoingAssistantText(input.addressLane.reply_text);
const debug = input.buildAddressDebugPayload(input.addressLane.debug, input.llmPreDecomposeMeta);
const surfaceRepair = repairInventorySupplierOverlapSurfaceReply(input.addressLane.reply_text, debug);
const safeAddressReplyBase = input.sanitizeOutgoingAssistantText(surfaceRepair.text);
const preservedAddressReply = preserveInventorySupplierOverlapCounterpartyLeadAfterSanitize({
rawReply: surfaceRepair.text,
sanitizedReply: safeAddressReplyBase,
addressDebug: debug
});
const safeAddressReply = preservedAddressReply.text;
if (surfaceRepair.audit) {
debug.inventory_supplier_overlap_surface_repair_v1 = {
...surfaceRepair.audit,
lead_preserved_after_sanitize: preservedAddressReply.applied
};
}
const followupOffer = input.buildAddressFollowupOffer(debug);
if (followupOffer) {
debug.address_followup_offer = followupOffer;
@@ -309,14 +499,36 @@ function runAssistantAddressLaneResponseRuntime(input) {
const rootFilters = followupContextSource?.root_filters && typeof followupContextSource.root_filters === "object"
? followupContextSource.root_filters
: null;
if (rootIntent || currentFrameKind) {
const detectedIntent = input.toNonEmptyString(debug.detected_intent);
const rootFrameWarehouse = input.toNonEmptyString(rootFilters?.warehouse) ??
input.toNonEmptyString(debugFilters?.warehouse) ??
(detectedIntent === "inventory_on_hand_as_of_date" ? extractSingleWarehouseScopeFromReply(safeAddressReply) : null);
const shouldPersistInventoryRootFrame = rootIntent || currentFrameKind || detectedIntent === "inventory_on_hand_as_of_date";
if (shouldPersistInventoryRootFrame) {
const rootFiltersForDebug = {
...(rootFilters ?? {}),
organization: input.toNonEmptyString(rootFilters?.organization) ?? input.toNonEmptyString(debugFilters?.organization),
warehouse: rootFrameWarehouse,
as_of_date: input.toNonEmptyString(rootFilters?.as_of_date) ?? input.toNonEmptyString(debugFilters?.as_of_date),
period_from: input.toNonEmptyString(rootFilters?.period_from) ?? input.toNonEmptyString(debugFilters?.period_from),
period_to: input.toNonEmptyString(rootFilters?.period_to) ?? input.toNonEmptyString(debugFilters?.period_to)
};
for (const key of Object.keys(rootFiltersForDebug)) {
if (!input.toNonEmptyString(rootFiltersForDebug[key])) {
delete rootFiltersForDebug[key];
}
}
debug.address_root_frame_context = {
root_intent: rootIntent,
current_frame_kind: currentFrameKind,
organization: input.toNonEmptyString(rootFilters?.organization),
as_of_date: input.toNonEmptyString(rootFilters?.as_of_date),
period_from: input.toNonEmptyString(rootFilters?.period_from),
period_to: input.toNonEmptyString(rootFilters?.period_to)
root_intent: rootIntent ?? (detectedIntent === "inventory_on_hand_as_of_date" ? "inventory_on_hand_as_of_date" : null),
root_filters: rootFiltersForDebug,
root_anchor_type: input.toNonEmptyString(followupContextSource?.root_anchor_type) ?? undefined,
root_anchor_value: input.toNonEmptyString(followupContextSource?.root_anchor_value) ?? undefined,
current_frame_kind: currentFrameKind ?? (detectedIntent === "inventory_on_hand_as_of_date" ? "inventory_root" : null),
organization: input.toNonEmptyString(rootFiltersForDebug.organization),
warehouse: input.toNonEmptyString(rootFiltersForDebug.warehouse),
as_of_date: input.toNonEmptyString(rootFiltersForDebug.as_of_date),
period_from: input.toNonEmptyString(rootFiltersForDebug.period_from),
period_to: input.toNonEmptyString(rootFiltersForDebug.period_to)
};
}
const debugWithRuntimeContracts = (0, assistantRuntimeContractResolver_1.attachAssistantRuntimeContractShadow)(debug, {
@@ -948,9 +948,21 @@ function buildInventoryRootFrameFromAddressDebug(debug, toNonEmptyString = fallb
return null;
}
const rootFiltersCandidate = toRecordObject(rootFrameContext?.root_filters);
const rootFrameFiltersCandidate = rootFrameContext
? {
organization: toNonEmptyString(rootFrameContext.organization),
warehouse: toNonEmptyString(rootFrameContext.warehouse),
as_of_date: toNonEmptyString(rootFrameContext.as_of_date),
period_from: toNonEmptyString(rootFrameContext.period_from),
period_to: toNonEmptyString(rootFrameContext.period_to)
}
: null;
const rootFrameFilters = rootFrameFiltersCandidate
? Object.fromEntries(Object.entries(rootFrameFiltersCandidate).filter(([, value]) => Boolean(value)))
: null;
const filters = {
...(rootFiltersCandidate ?? {}),
...(rootFiltersCandidate ? {} : extractedFilters)
...(rootFiltersCandidate ? {} : rootFrameFilters && Object.keys(rootFrameFilters).length > 0 ? rootFrameFilters : extractedFilters)
};
if (!filters.organization) {
const organization = readAddressDebugOrganization(debug, toNonEmptyString);
@@ -405,6 +405,30 @@ function hasInventoryMarginRankingAddressReply(input, entryPoint) {
selectedRecipe === "address_inventory_margin_ranking_for_nomenclature_v1" ||
capabilityId === "inventory_inventory_margin_ranking_for_nomenclature");
}
function hasExactInventoryPurchaseToSaleChainAddressReply(input, entryPoint) {
if (!isDiscoveryReadyAddressCandidate(input, entryPoint)) {
return false;
}
if (!hasEffectivelyFactualAddressReply(input)) {
return false;
}
const source = String(input.currentReplySource ?? input.livingChatSource ?? "").trim().toLowerCase();
if (source !== "address_query_runtime_v1" && source !== "address_exact" && source !== "address_lane") {
return false;
}
const detectedIntent = toNonEmptyString(input.addressRuntimeMeta?.detected_intent);
const selectedRecipe = toNonEmptyString(input.addressRuntimeMeta?.selected_recipe);
const capabilityId = toNonEmptyString(input.addressRuntimeMeta?.capability_id) ??
toNonEmptyString(input.addressRuntimeMeta?.capability_contract_id);
if (detectedIntent !== "inventory_purchase_to_sale_chain" &&
selectedRecipe !== "address_inventory_purchase_to_sale_chain_v1" &&
capabilityId !== "inventory_inventory_purchase_to_sale_chain") {
return false;
}
const mcpCallStatus = toNonEmptyString(input.addressRuntimeMeta?.mcp_call_status);
const responseType = toNonEmptyString(input.addressRuntimeMeta?.response_type);
return Boolean(hasConfirmedAddressExecution(input) || mcpCallStatus === "matched_non_empty" || responseType === "FACTUAL_LIST");
}
function hasValueFlowActionConflictWithDiscoveryTurnMeaning(input, entryPoint) {
if (!isDiscoveryReadyAddressCandidate(input, entryPoint)) {
return false;
@@ -471,6 +495,14 @@ function hasExactMatchedFactualAddressReply(input, entryPoint) {
}
if (hasSemanticConflictWithDiscoveryTurnMeaning(input, entryPoint)) {
const detectedIntent = toNonEmptyString(input.addressRuntimeMeta?.detected_intent);
const turnMeaning = readDiscoveryTurnMeaning(entryPoint);
const askedDomain = toNonEmptyString(turnMeaning?.asked_domain_family);
const askedAction = toNonEmptyString(turnMeaning?.asked_action_family);
if (detectedIntent === "inventory_supplier_stock_overlap_as_of_date" &&
hasConfirmedAddressExecution(input) &&
(askedDomain === "entity_resolution" || askedAction === "search_business_entity")) {
return true;
}
if (!(isMetadataDiscoveryTurn(entryPoint) && isInventoryExactAddressIntent(detectedIntent))) {
return false;
}
@@ -682,6 +714,7 @@ function applyAssistantMcpDiscoveryResponsePolicy(input) {
const exactBankOperationsAddressReply = hasExactBankOperationsAddressReply(input, entryPoint);
const exactDocumentListAddressReply = hasExactDocumentListAddressReply(input, entryPoint);
const inventoryMarginRankingAddressReply = hasInventoryMarginRankingAddressReply(input, entryPoint);
const exactInventoryPurchaseToSaleChainAddressReply = hasExactInventoryPurchaseToSaleChainAddressReply(input, entryPoint);
const openScopeValueFlowDiscoveryPriority = hasOpenScopeValueFlowDiscoveryPriority(input, entryPoint);
const metadataDiscoveryPriority = hasMetadataDiscoveryPriority(input, entryPoint);
const valueFlowActionConflictWithDiscoveryTurnMeaning = hasValueFlowActionConflictWithDiscoveryTurnMeaning(input, entryPoint);
@@ -689,6 +722,9 @@ function applyAssistantMcpDiscoveryResponsePolicy(input) {
const exactBankOperationsProtectsCurrent = exactBankOperationsAddressReply &&
!semanticConflictWithDiscoveryTurnMeaning &&
!valueFlowActionConflictWithDiscoveryTurnMeaning;
const exactInventoryChainProtectsCurrent = exactInventoryPurchaseToSaleChainAddressReply &&
candidate.candidate_status === "clarification_candidate" &&
valueFlowActionConflictWithDiscoveryTurnMeaning;
if (!entryPoint) {
pushReason(reasonCodes, "mcp_discovery_response_policy_no_entry_point");
}
@@ -755,6 +791,9 @@ function applyAssistantMcpDiscoveryResponsePolicy(input) {
if (inventoryMarginRankingAddressReply) {
pushReason(reasonCodes, "mcp_discovery_response_policy_keep_inventory_margin_ranking_address_reply");
}
if (exactInventoryChainProtectsCurrent) {
pushReason(reasonCodes, "mcp_discovery_response_policy_keep_exact_inventory_chain_reply_over_value_flow_clarification");
}
if (deterministicBroadBusinessEvaluationReply && candidate.candidate_status === "clarification_candidate") {
pushReason(reasonCodes, "mcp_discovery_response_policy_keep_broad_business_summary_over_clarification_candidate");
}
@@ -787,6 +826,7 @@ function applyAssistantMcpDiscoveryResponsePolicy(input) {
!exactBankOperationsProtectsCurrent &&
!exactDocumentListAddressReply &&
!inventoryMarginRankingAddressReply &&
!exactInventoryChainProtectsCurrent &&
!(deterministicBroadBusinessEvaluationReply && candidate.candidate_status === "clarification_candidate") &&
ALLOWED_CANDIDATE_STATUSES.has(candidate.candidate_status) &&
candidate.eligible_for_future_hot_runtime &&
@@ -67,6 +67,13 @@ function detectSupportedIntent(text, deps) {
reason: "address_intent_resolver_current_turn_signal"
};
}
if (hasInventorySupplierStockOverlapExactSignal(text)) {
return {
intent: "inventory_supplier_stock_overlap_as_of_date",
confidence: "high",
reason: "inventory_supplier_stock_overlap_current_turn_signal"
};
}
if (/(?:\u043a\u0442\u043e\s+\u043d\u0430\u043c(?:\s+\p{L}+){0,4}\s+\u0434\u043e\u043b\u0436|\u043d\u0430\u043c\s+\u043a\u0442\u043e(?:\s+\p{L}+){0,4}\s+\u0434\u043e\u043b\u0436|\u0434\u0435\u0431\u0438\u0442\u043e\u0440|\u0434\u0435\u0431\u0438\u0442\u043e\u0440\u0441\u043a|\breceivables?\b)/iu.test(text)) {
return {
intent: "receivables_confirmed_as_of_date",
@@ -214,6 +221,17 @@ function hasSelectedObjectInventoryExactSignal(text) {
}
return /(?:\u0437\u0430\u0440\u0430\u0431\u043e\u0442|\u043f\u0440\u0438\u0431\u044b\u043b|\u043c\u0430\u0440\u0436|\u043f\u0440\u043e\u0434\u0430\u0436|\u043f\u0440\u043e\u0434\u0430\u043b|\u0437\u0430\u043a\u0443\u043f|\u043f\u043e\u043a\u0443\u043f|\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442|\u0446\u0435\u043f\u043e\u0447|profit|margin|sale|purchase|document)/iu.test(normalized);
}
function hasInventorySupplierStockOverlapExactSignal(text) {
const normalized = String(text ?? "");
if (!normalized) {
return false;
}
const hasStockScope = /(?:\u0441\u043a\u043b\u0430\u0434|\u043e\u0441\u0442\u0430\u0442|\u043b\u0435\u0436\p{L}*|\u043d\u0430\u0445\u043e\u0434\p{L}*|\u0447\u0438\u0441\u043b\p{L}*|warehouse|stock|inventory|on\s+hand)/iu.test(normalized);
const hasSupplierCue = /(?:\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043f\u043e\u0441\u0442\u0430\u0432\p{L}*|\u0437\u0430\u043a\u0443\u043f|\u043a\u0443\u043f\p{L}*|\u043f\u0440\u0438\u043e\u0431\u0440\p{L}*|supplier|vendor|purchase|bought|supplied|procurement)/iu.test(normalized);
const hasOverlapAction = /(?:\u0443\s+\u043a\u0430\u043a\u043e\u0433\u043e\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043f\u043e\s+\u043a\u0430\u043a\u043e\u043c\u0443\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043e\u0442\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043e\u043f\u0440\u0435\u0434\u0435\u043b\p{L}*[\s\S]{0,80}\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043d\u0430\u0439\u0442\p{L}*[\s\S]{0,80}\u0442\u043e\u0432\u0430\u0440|\u043a\u0430\u043a\u0438\u0435\s+\u0442\u043e\u0432\u0430\u0440|supplier[\s\S]{0,80}(?:stock|inventory)|(?:stock|inventory)[\s\S]{0,80}supplier)/iu.test(normalized);
const hasSupplierIdentityAsk = /(?:\u043a\u0430\u043a\u043e(?:\u0433\u043e|\u043c\u0443)\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|\u043e\u043f\u0440\u0435\u0434\u0435\u043b\p{L}*\s+\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a|which\s+supplier|what\s+supplier)/iu.test(normalized);
return hasStockScope && hasSupplierCue && (hasOverlapAction || hasSupplierIdentityAsk);
}
function hasOrganizationLevelEarningsOverviewSignal(text) {
const normalized = String(text ?? "");
if (!normalized || hasExplicitCounterpartyValueObject(normalized)) {
@@ -381,12 +399,15 @@ function createAssistantTurnMeaningPolicy(deps = {}) {
const counterpartyBidirectionalValueFlow = compactOrganizationCashflowDisplay ? null : detectCounterpartyBidirectionalValueFlowFamily(joinedText);
const counterpartyTurnover = compactOrganizationCashflowDisplay ? null : detectCounterpartyTurnoverFamily(joinedText);
const selectedObjectInventoryExact = hasSelectedObjectInventoryExactSignal(joinedText);
const llmIntent = toNonEmptyString(input?.llmPreDecomposeMeta?.predecomposeContract?.intent, deps);
const inventorySupplierStockOverlapExact = supportedIntent?.intent === "inventory_supplier_stock_overlap_as_of_date" ||
llmIntent === "inventory_supplier_stock_overlap_as_of_date" ||
hasInventorySupplierStockOverlapExactSignal(joinedText);
const broadBusinessEvaluation = compactOrganizationCashflowDisplay
? { family: "broad_business_evaluation" }
: selectedObjectInventoryExact || counterpartyBidirectionalValueFlow?.family
: selectedObjectInventoryExact || inventorySupplierStockOverlapExact || counterpartyBidirectionalValueFlow?.family
? null
: detectBroadBusinessEvaluation(joinedText);
const llmIntent = toNonEmptyString(input?.llmPreDecomposeMeta?.predecomposeContract?.intent, deps);
const explicitIntentCandidate = broadBusinessEvaluation?.family
? null
: supportedIntent?.intent ?? (llmIntent && llmIntent !== "unknown" ? llmIntent : null);
@@ -454,11 +475,13 @@ function createAssistantTurnMeaningPolicy(deps = {}) {
? "confirmed_snapshot"
: explicitIntentCandidate === "vat_payable_forecast"
? "forecast"
: explicitIntentCandidate === "list_documents_by_counterparty"
? "list_documents"
: counterpartyTurnover?.family
? "counterparty_value_or_turnover"
: null;
: explicitIntentCandidate === "inventory_supplier_stock_overlap_as_of_date"
? "supplier_overlap"
: explicitIntentCandidate === "list_documents_by_counterparty"
? "list_documents"
: counterpartyTurnover?.family
? "counterparty_value_or_turnover"
: null;
const staleReplayForbidden = Boolean(unsupportedFamily ||
broadBusinessEvaluation?.family ||
(counterpartyBidirectionalValueFlow?.entity && !explicitIntentCandidate) ||