АДРЕСНЫЙ РЕЖИМ -ADDRESS: стабилизация контекстных цепочек и live-якорей (договор 19/15)

This commit is contained in:
2026-04-02 15:03:53 +03:00
parent cc7fcabf05
commit 4cb9fc7021
90 changed files with 15954 additions and 1561 deletions
+1 -1
View File
@@ -108,7 +108,7 @@ export const ASSISTANT_MCP_PROXY_URL = (process.env.ASSISTANT_MCP_PROXY_URL ?? "
""
);
export const ASSISTANT_MCP_CHANNEL = process.env.ASSISTANT_MCP_CHANNEL ?? "default";
export const ASSISTANT_MCP_TIMEOUT_MS = toNumberFlag(process.env.ASSISTANT_MCP_TIMEOUT_MS, 1200);
export const ASSISTANT_MCP_TIMEOUT_MS = toNumberFlag(process.env.ASSISTANT_MCP_TIMEOUT_MS, 6000);
export const ASSISTANT_MCP_LIVE_LIMIT = Math.max(1, Math.trunc(toNumberFlag(process.env.ASSISTANT_MCP_LIVE_LIMIT, 24)));
export const DATA_DIR = process.env.DATA_DIR ?? path.resolve(MODULE_ROOT, "data");
@@ -415,7 +415,20 @@ function extractLooseByAnchorValue(text: string): string | undefined {
"документам",
"докам",
"взаиморасчетам",
"взаиморасчётам"
"взаиморасчётам",
"теперь",
"сейчас",
"вернись",
"вернуться",
"вернуть",
"раскрой",
"раскрыть",
"раскройте",
"нему",
"ней",
"ним",
"этому",
"тому"
]);
if (stopWords.has(lowered)) {
return undefined;
@@ -506,6 +519,19 @@ function isLikelyCounterpartyToken(rawToken: string): boolean {
"show",
"list",
"please",
"теперь",
"сейчас",
"вернись",
"вернуться",
"вернуть",
"раскрой",
"раскрыть",
"раскройте",
"нему",
"ней",
"ним",
"этому",
"тому",
"vse",
"all",
"kakie",
@@ -385,7 +385,7 @@ function hasContractAnchorSignal(text: string): boolean {
if (hasContractAnchorMention(text)) {
return true;
}
// Allow short forms like "15/24" for follow-up prompts if document/bank signal exists.
// Allow short forms like "19/15" for follow-up prompts if document/bank signal exists.
return hasContractNumberLikeToken(text) && hasDocsOrBankSignal(text);
}
@@ -630,3 +630,4 @@ export function resolveAddressIntent(userMessage: string): AddressIntentResoluti
reasons: ["intent_not_supported_in_v1"]
};
}
@@ -57,6 +57,93 @@ function hasSameDateHint(text: string): boolean {
);
}
function hasOpenItemsHint(text: string): boolean {
return /(?:open\s+items|unclosed\s+items|хвост|висят|незакрыт|не\s+закрыт|открыт|долг|задолж|позиц)/iu.test(String(text ?? ""));
}
function hasDocumentSignal(text: string): boolean {
return /(?:док(?:и|умент|ументы|ументов|ументами)|docs?|documents?)/iu.test(String(text ?? ""));
}
function hasBankSignal(text: string): boolean {
return /(?:банк|банков|операц|опер|выписк|платеж|платёж|оплат|списан|поступлен|движени|bank|payment|payments|transaction|transactions|statement|wire)/iu.test(
String(text ?? "")
);
}
function hasAccountSignal(text: string): boolean {
const normalized = String(text ?? "");
return (
/(?:сч[её]т|account)\D{0,12}\d{2}(?:[.,]\d{1,2})?/iu.test(normalized) ||
/\b\d{2}(?:[.,]\d{1,2})\b/u.test(normalized) ||
/(?:^|\s)по\s+\d{2}(?:[.,]\d{1,2})?(?=$|[\s,.;:!?])/iu.test(normalized)
);
}
function hasExplicitCounterpartyMention(text: string): boolean {
return /(?:контраг|counterparty|компан|организац|supplier|vendor|customer|client|partner|поставщик|клиент|покупател|партнер)/iu.test(
String(text ?? "")
);
}
function hasExplicitContractMention(text: string): boolean {
return /(?:договор|контракт|contract|\bдог\.?\b)/iu.test(String(text ?? ""));
}
function hasContractNumberLikeToken(text: string): boolean {
if (/(?:^|[\s([{])(?:№|#|n)\s*[a-zа-яё0-9][a-zа-яё0-9./_-]{1,}(?=$|[\s,.;:!?)\]}])/iu.test(String(text ?? ""))) {
return true;
}
return /\b\d{1,6}[./_-]\d{1,6}(?:[./_-]\d{1,6})?\b/iu.test(String(text ?? ""));
}
function hasExplicitLooseByAnchorToken(text: string): boolean {
const match = String(text ?? "").match(/(?:^|\s)по\s+([a-zа-яё][a-zа-яё0-9._-]{1,})(?=[\s,.;:!?)]|$)/iu);
if (!match) {
return false;
}
const token = String(match[1] ?? "").trim().toLowerCase();
if (!token) {
return false;
}
const pronounTokens = new Set([
"нему",
"ней",
"ним",
"немуже",
"нейже",
"этому",
"этомуже",
"этому-то",
"тому",
"томуже",
"этому",
"этой",
"той"
]);
const genericTokens = new Set([
"контрагенту",
"контрагента",
"договору",
"договора",
"счету",
"счёту",
"дате",
"периоду"
]);
return !pronounTokens.has(token) && !genericTokens.has(token);
}
function mapCounterpartyIntentToContractIntent(intent: AddressIntent): AddressIntent | null {
if (intent === "list_documents_by_counterparty") {
return "list_documents_by_contract";
}
if (intent === "bank_operations_by_counterparty") {
return "bank_operations_by_contract";
}
return null;
}
export function hasAddressFollowupContextSignal(text: string): boolean {
const normalized = String(text ?? "").trim();
if (!normalized) {
@@ -213,14 +300,88 @@ function deriveIntentWithFollowupContext(
if (!followupContext || !followupContext.previous_intent) {
return detectedIntent;
}
const normalizedMessage = String(userMessage ?? "");
const hasFollowupSignal = hasAddressFollowupContextSignal(normalizedMessage);
if (!hasFollowupSignal) {
return detectedIntent;
}
const previousIntent = followupContext.previous_intent;
const previousFilters = followupContext.previous_filters ?? {};
const previousContract = toNonEmptyString(previousFilters.contract);
const previousCounterparty = toNonEmptyString(previousFilters.counterparty);
const previousContractFromAnchor =
followupContext.previous_anchor_type === "contract" ? toNonEmptyString(followupContext.previous_anchor_value) : null;
const previousCounterpartyFromAnchor =
followupContext.previous_anchor_type === "counterparty" ? toNonEmptyString(followupContext.previous_anchor_value) : null;
const hasPreviousContract = Boolean(previousContract ?? previousContractFromAnchor);
const hasPreviousCounterparty = Boolean(previousCounterparty ?? previousCounterpartyFromAnchor);
const hasAnyPartyAnchor = hasPreviousContract || hasPreviousCounterparty;
if (hasOpenItemsHint(normalizedMessage) && hasAnyPartyAnchor) {
return {
intent: "open_items_by_counterparty_or_contract",
confidence: "low",
reasons: [...detectedIntent.reasons, "open_items_from_followup_context"]
};
}
const previousIsBalanceFamily = previousIntent === "account_balance_snapshot" || previousIntent === "documents_forming_balance";
if (
previousIsBalanceFamily &&
hasAccountSignal(normalizedMessage) &&
(detectedIntent.intent === "unknown" ||
detectedIntent.intent === "list_documents_by_counterparty" ||
detectedIntent.intent === "bank_operations_by_counterparty")
) {
const preferDocumentsForming =
hasDocumentSignal(normalizedMessage) &&
/(?:раскрой|раскры|формир|документами|по\s+документ)/iu.test(normalizedMessage);
return {
intent: preferDocumentsForming ? "documents_forming_balance" : "account_balance_snapshot",
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_adjusted_to_balance_followup_context"]
};
}
if (hasPreviousContract) {
if (detectedIntent.intent === "unknown") {
if (hasBankSignal(normalizedMessage)) {
return {
intent: "bank_operations_by_contract",
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_adjusted_to_contract_followup_context"]
};
}
if (hasDocumentSignal(normalizedMessage)) {
return {
intent: "list_documents_by_contract",
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_adjusted_to_contract_followup_context"]
};
}
}
const mappedIntent = mapCounterpartyIntentToContractIntent(detectedIntent.intent);
if (mappedIntent) {
const explicitCounterpartyTarget = hasExplicitCounterpartyMention(normalizedMessage) || hasExplicitLooseByAnchorToken(normalizedMessage);
const explicitContractTarget = hasExplicitContractMention(normalizedMessage) || hasContractNumberLikeToken(normalizedMessage);
if (!explicitCounterpartyTarget || explicitContractTarget) {
return {
intent: mappedIntent,
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_adjusted_to_contract_followup_context"]
};
}
}
}
if (detectedIntent.intent !== "unknown") {
return detectedIntent;
}
if (!hasAddressFollowupContextSignal(userMessage)) {
return detectedIntent;
}
return {
intent: followupContext.previous_intent,
intent: previousIntent,
confidence: "low",
reasons: [...detectedIntent.reasons, "intent_from_followup_context"]
};