957 lines
51 KiB
JavaScript
957 lines
51 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.ASSISTANT_MCP_DISCOVERY_TURN_INPUT_SCHEMA_VERSION = void 0;
|
||
exports.buildAssistantMcpDiscoveryTurnInput = buildAssistantMcpDiscoveryTurnInput;
|
||
exports.ASSISTANT_MCP_DISCOVERY_TURN_INPUT_SCHEMA_VERSION = "assistant_mcp_discovery_turn_input_v1";
|
||
function toRecordObject(value) {
|
||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||
return null;
|
||
}
|
||
return value;
|
||
}
|
||
function toNonEmptyString(value) {
|
||
if (value === null || value === undefined) {
|
||
return null;
|
||
}
|
||
const text = String(value).trim();
|
||
return text.length > 0 ? text : null;
|
||
}
|
||
function normalizeReasonCode(value) {
|
||
const normalized = value
|
||
.trim()
|
||
.replace(/[^\p{L}\p{N}_.:-]+/gu, "_")
|
||
.replace(/^_+|_+$/g, "")
|
||
.toLowerCase();
|
||
return normalized.length > 0 ? normalized.slice(0, 120) : null;
|
||
}
|
||
function pushReason(target, value) {
|
||
const normalized = normalizeReasonCode(value);
|
||
if (normalized && !target.includes(normalized)) {
|
||
target.push(normalized);
|
||
}
|
||
}
|
||
function pushUnique(target, value) {
|
||
const text = toNonEmptyString(value);
|
||
if (text && !target.includes(text)) {
|
||
target.push(text);
|
||
}
|
||
}
|
||
function isReferentialEntityPlaceholder(value) {
|
||
return /^(?:\u043d\u0435\u043c\u0443|\u043d\u0435\u0439|\u043d\u0438\u043c|\u043d\u0438\u043c\u0438|\u0435\u0433\u043e|\u0435\u0435|\u0435\u0451|\u0438\u0445|\u044d\u0442\u043e\u043c\u0443|\u044d\u0442\u043e\u0439|\u044d\u0442\u0438\u043c|\u044d\u0442\u0438\u043c\u0438|\u044d\u0442\u043e\u043c)$/iu.test(value.trim());
|
||
}
|
||
function pushScopedEntityCandidate(target, value, groundedFollowupEntity) {
|
||
const text = candidateValue(value);
|
||
if (!text) {
|
||
return;
|
||
}
|
||
if (groundedFollowupEntity && isReferentialEntityPlaceholder(text)) {
|
||
return;
|
||
}
|
||
pushUnique(target, text);
|
||
}
|
||
function canonicalizeEntityResolutionCandidate(value) {
|
||
return normalizeEntityResolutionCandidate(value)
|
||
.replace(/^(?:\u0441\s+\u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435\u043c\s+)/iu, "")
|
||
.replace(/\s+(?:\u0432\s+\u0441\u0438\u0441\u0442\u0435\u043c\u0435\s*1\u0421|\u0432\s+1c|in\s+(?:the\s+)?1c\s+system|in\s+1c)\s*$/iu, "")
|
||
.trim();
|
||
}
|
||
function pushNormalizedEntityResolutionCandidate(target, value) {
|
||
const text = toNonEmptyString(value);
|
||
if (!text) {
|
||
return;
|
||
}
|
||
const normalized = canonicalizeEntityResolutionCandidate(text);
|
||
if (normalized && !target.includes(normalized)) {
|
||
target.push(normalized);
|
||
}
|
||
}
|
||
function compactLower(value) {
|
||
return String(value ?? "")
|
||
.toLowerCase()
|
||
.replace(/\s+/g, " ")
|
||
.trim();
|
||
}
|
||
function candidateValue(value) {
|
||
const direct = toNonEmptyString(value);
|
||
if (direct && direct !== "[object Object]") {
|
||
return direct;
|
||
}
|
||
const record = toRecordObject(value);
|
||
if (!record) {
|
||
return null;
|
||
}
|
||
return (toNonEmptyString(record.value) ??
|
||
toNonEmptyString(record.name) ??
|
||
toNonEmptyString(record.ref) ??
|
||
toNonEmptyString(record.text));
|
||
}
|
||
function collectEntityCandidates(value) {
|
||
const result = [];
|
||
if (Array.isArray(value)) {
|
||
for (const item of value) {
|
||
pushUnique(result, candidateValue(item));
|
||
}
|
||
return result;
|
||
}
|
||
pushUnique(result, candidateValue(value));
|
||
return result;
|
||
}
|
||
function collectPredecomposeEntities(predecompose) {
|
||
const entities = toRecordObject(predecompose?.entities);
|
||
return {
|
||
counterparty: toNonEmptyString(entities?.counterparty),
|
||
organization: toNonEmptyString(entities?.organization)
|
||
};
|
||
}
|
||
function collectDateScope(predecompose) {
|
||
const period = toRecordObject(predecompose?.period);
|
||
const asOfDate = toNonEmptyString(period?.as_of_date);
|
||
const periodFrom = toNonEmptyString(period?.period_from);
|
||
const periodTo = toNonEmptyString(period?.period_to);
|
||
if (asOfDate) {
|
||
return asOfDate;
|
||
}
|
||
const yearFrom = periodFrom?.match(/^(\d{4})-01-01$/);
|
||
const yearTo = periodTo?.match(/^(\d{4})-12-31$/);
|
||
if (yearFrom && yearTo && yearFrom[1] === yearTo[1]) {
|
||
return yearFrom[1];
|
||
}
|
||
if (periodFrom && periodTo) {
|
||
return `${periodFrom}..${periodTo}`;
|
||
}
|
||
return periodFrom ?? periodTo ?? null;
|
||
}
|
||
function collectDateScopeFromFilters(filters) {
|
||
if (!filters) {
|
||
return null;
|
||
}
|
||
const asOfDate = toNonEmptyString(filters.as_of_date);
|
||
const periodFrom = toNonEmptyString(filters.period_from);
|
||
const periodTo = toNonEmptyString(filters.period_to);
|
||
if (asOfDate) {
|
||
return asOfDate;
|
||
}
|
||
const yearFrom = periodFrom?.match(/^(\d{4})-01-01$/);
|
||
const yearTo = periodTo?.match(/^(\d{4})-12-31$/);
|
||
if (yearFrom && yearTo && yearFrom[1] === yearTo[1]) {
|
||
return yearFrom[1];
|
||
}
|
||
if (periodFrom && periodTo) {
|
||
return `${periodFrom}..${periodTo}`;
|
||
}
|
||
return periodFrom ?? periodTo ?? null;
|
||
}
|
||
function mapPilotScopeToFollowupMeaning(pilotScope) {
|
||
if (pilotScope === "counterparty_lifecycle_query_documents_v1") {
|
||
return {
|
||
domain: "counterparty_lifecycle",
|
||
action: "activity_duration",
|
||
unsupported: "counterparty_lifecycle"
|
||
};
|
||
}
|
||
if (pilotScope === "counterparty_movement_evidence_query_movements_v1") {
|
||
return {
|
||
domain: "movements",
|
||
action: "list_movements",
|
||
unsupported: "movement_evidence"
|
||
};
|
||
}
|
||
if (pilotScope === "counterparty_document_evidence_query_documents_v1") {
|
||
return {
|
||
domain: "documents",
|
||
action: "list_documents",
|
||
unsupported: "document_evidence"
|
||
};
|
||
}
|
||
if (pilotScope === "counterparty_supplier_payout_query_movements_v1") {
|
||
return {
|
||
domain: "counterparty_value",
|
||
action: "payout",
|
||
unsupported: "counterparty_payouts_or_outflow"
|
||
};
|
||
}
|
||
if (pilotScope === "counterparty_value_flow_query_movements_v1") {
|
||
return {
|
||
domain: "counterparty_value",
|
||
action: "turnover",
|
||
unsupported: "counterparty_value_or_turnover"
|
||
};
|
||
}
|
||
if (pilotScope === "counterparty_bidirectional_value_flow_query_movements_v1") {
|
||
return {
|
||
domain: "counterparty_value",
|
||
action: "net_value_flow",
|
||
unsupported: "counterparty_bidirectional_value_flow_or_netting"
|
||
};
|
||
}
|
||
if (pilotScope === "metadata_inspection_v1") {
|
||
return {
|
||
domain: "metadata",
|
||
action: "inspect_catalog",
|
||
unsupported: "1c_metadata_surface"
|
||
};
|
||
}
|
||
return {
|
||
domain: null,
|
||
action: null,
|
||
unsupported: null
|
||
};
|
||
}
|
||
function mapAddressIntentToFollowupMeaning(intent) {
|
||
if (intent === "counterparty_activity_lifecycle") {
|
||
return {
|
||
domain: "counterparty_lifecycle",
|
||
action: "activity_duration",
|
||
unsupported: "counterparty_lifecycle"
|
||
};
|
||
}
|
||
if (intent === "supplier_payouts_profile") {
|
||
return {
|
||
domain: "counterparty_value",
|
||
action: "payout",
|
||
unsupported: "counterparty_payouts_or_outflow"
|
||
};
|
||
}
|
||
if (intent === "customer_revenue_and_payments") {
|
||
return {
|
||
domain: "counterparty_value",
|
||
action: "turnover",
|
||
unsupported: "counterparty_value_or_turnover"
|
||
};
|
||
}
|
||
if (intent === "bank_operations_by_counterparty") {
|
||
return {
|
||
domain: "movements",
|
||
action: "list_movements",
|
||
unsupported: "movement_evidence"
|
||
};
|
||
}
|
||
return {
|
||
domain: null,
|
||
action: null,
|
||
unsupported: null
|
||
};
|
||
}
|
||
function collectFollowupDiscoverySeed(followupContext) {
|
||
const previousFilters = toRecordObject(followupContext?.previous_filters);
|
||
const rootFilters = toRecordObject(followupContext?.root_filters);
|
||
const pilotScope = toNonEmptyString(followupContext?.previous_discovery_pilot_scope);
|
||
const previousIntent = toNonEmptyString(followupContext?.target_intent) ?? toNonEmptyString(followupContext?.previous_intent);
|
||
const mapped = mapPilotScopeToFollowupMeaning(pilotScope).domain !== null
|
||
? mapPilotScopeToFollowupMeaning(pilotScope)
|
||
: mapAddressIntentToFollowupMeaning(previousIntent);
|
||
const discoveryEntities = collectEntityCandidates(followupContext?.previous_discovery_entity_candidates);
|
||
const counterparty = toNonEmptyString(previousFilters?.counterparty) ??
|
||
toNonEmptyString(rootFilters?.counterparty) ??
|
||
(toNonEmptyString(followupContext?.previous_anchor_type) === "counterparty"
|
||
? toNonEmptyString(followupContext?.previous_anchor_value)
|
||
: null) ??
|
||
(discoveryEntities[0] ?? null);
|
||
const organization = toNonEmptyString(previousFilters?.organization) ??
|
||
toNonEmptyString(rootFilters?.organization) ??
|
||
(toNonEmptyString(followupContext?.previous_anchor_type) === "organization"
|
||
? toNonEmptyString(followupContext?.previous_anchor_value)
|
||
: null);
|
||
const dateScope = collectDateScopeFromFilters(previousFilters) ??
|
||
collectDateScopeFromFilters(rootFilters);
|
||
return {
|
||
pilotScope,
|
||
domain: mapped.domain,
|
||
action: mapped.action,
|
||
unsupported: mapped.unsupported,
|
||
counterparty,
|
||
discoveryEntity: discoveryEntities[0] ?? null,
|
||
organization,
|
||
dateScope,
|
||
metadataRouteFamily: toNonEmptyString(followupContext?.previous_discovery_metadata_route_family),
|
||
metadataSelectedEntitySet: toNonEmptyString(followupContext?.previous_discovery_metadata_selected_entity_set),
|
||
metadataAmbiguityDetected: followupContext?.previous_discovery_metadata_ambiguity_detected === true,
|
||
metadataAmbiguityEntitySets: collectEntityCandidates(followupContext?.previous_discovery_metadata_ambiguity_entity_sets)
|
||
};
|
||
}
|
||
function metadataEntitySetsSuggestDocumentLane(values) {
|
||
return values.some((value) => /(?:документ|document|invoice|waybill|накладн|счет[- ]?фактур|акт)/iu.test(value));
|
||
}
|
||
function metadataEntitySetsSuggestMovementLane(values) {
|
||
return values.some((value) => /(?:регистр|register|movement|движени|операц|проводк|bank)/iu.test(value));
|
||
}
|
||
function metadataAmbiguityCollapsesToDocumentLane(values) {
|
||
return values.length > 0 && metadataEntitySetsSuggestDocumentLane(values) && !metadataEntitySetsSuggestMovementLane(values);
|
||
}
|
||
function metadataAmbiguityCollapsesToMovementLane(values) {
|
||
return values.length > 0 && metadataEntitySetsSuggestMovementLane(values) && !metadataEntitySetsSuggestDocumentLane(values);
|
||
}
|
||
function hasLifecycleSignal(text) {
|
||
return /(?:сколько\s+лет|как\s+давно|давно\s+ли|возраст|перв(?:ая|ый)\s+актив|когда\s+начал|когда\s+появ|lifecycle|activity\s+duration|business\s+age|how\s+long)/iu.test(text);
|
||
}
|
||
function hasValueFlowSignal(text) {
|
||
return /(?:оборот|выручк|оплат|плат[её]ж|заплат|перечисл|списан|расход|исходящ|входящ|получ(?:ил|ено|ен)|поступил|поступлен|денежн[а-яёa-z0-9_-]*\s+поток|supplier|value[-\s]?flow|turnover|revenue|payment|payout|outflow|cash\s+flow)/iu.test(text);
|
||
}
|
||
function hasPayoutSignal(text) {
|
||
return /(?:\bмы\s+(?:за)?плат|(?:за)?платил|оплатил|перечисл|списан|расход|поставщик|исходящ|supplier|payout|outflow|paid\s+to|payment\s+to)/iu.test(text);
|
||
}
|
||
function hasBidirectionalValueFlowSignal(text) {
|
||
return /(?:нетто|сальдо|баланс\s+(?:плат|денег|денеж)|взаиморасч[её]т|получил[иа]?.*(?:за)?платил|(?:за)?платил[иа]?.*получил|входящ.*исходящ|исходящ.*входящ|дебет.*кредит|кредит.*дебет|net\s+(?:flow|cash|payment)|cash\s+net|incoming\s+and\s+outgoing|received\s+and\s+paid|paid\s+and\s+received)/iu.test(text);
|
||
}
|
||
function hasMonthlyAggregationSignal(text) {
|
||
return /(?:\u043f\u043e\s+\u043c\u0435\u0441\u044f\u0446\u0430\u043c|\u043f\u043e\u043c\u0435\u0441\u044f\u0447\u043d\u043e|\u0435\u0436\u0435\u043c\u0435\u0441\u044f\u0447\u043d\u043e|month\s+by\s+month|by\s+month|monthly)/iu.test(text);
|
||
}
|
||
function hasMetadataSignal(text) {
|
||
if (/(?:\u043c\u0435\u0442\u0430\u0434\u0430\u043d|schema|catalog|metadata\s+surface|\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440[\u0430\u044b]\s+1\u0441|\u0441\u0445\u0435\u043c[\u0430\u044b]\s+1\u0441)/iu.test(text)) {
|
||
return true;
|
||
}
|
||
return (/(?:\u043e\u0431\u044a\u0435\u043a\u0442(?:\u044b|\u0430|\u043e\u0432)?|\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u044b|\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b|\u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0438|\u043f\u043e\u043b(?:\u0435|\u044f)|objects?|registers?|documents?|catalogs?|fields?)/iu.test(text) &&
|
||
/(?:\u0435\u0441\u0442\u044c|\u043a\u0430\u043a\u0438\u0435|\u0434\u043e\u0441\u0442\u0443\u043f\u043d|\u0432\s+1\u0441|1\u0441|available|exist|which)/iu.test(text));
|
||
}
|
||
function hasMetadataObjectHint(text) {
|
||
return /(?:\u043e\u0431\u044a\u0435\u043a\u0442(?:\u044b|\u0430|\u043e\u0432)?|\u0440\u0435\u0433\u0438\u0441\u0442\u0440(?:\u044b)?|\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442(?:\u044b)?|\u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a(?:\u0438)?|\u043f\u043e\u043b(?:\u0435|\u044f)|objects?|registers?|documents?|catalogs?|fields?)/iu.test(text);
|
||
}
|
||
function hasDocumentEvidenceFollowupSignal(text) {
|
||
return /(?:\u043f\u043e\s+\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442(?:\u0430\u043c|\u044b)?|\u0434\u0430\u0432\u0430\u0439\s+\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442(?:\u044b)?|\u0438\u0449\u0438\s+\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442(?:\u044b)?|\u043f\u043e\u043a\u0430\u0436\u0438\s+\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442(?:\u044b)?|(?:\u043f\u043e\u043a\u0430\u0436\u0438|\u043a\u0430\u043a\u0438\u0435|\u0441\u043f\u0438\u0441\u043e\u043a|\u0434\u0430\u0439|\u0438\u0449\u0438)\s+(?:\u0441\u0447(?:[еe]т|\u0435\u0442)[-\u2011 ]?\u0444\u0430\u043a\u0442\u0443\u0440(?:\u044b|\u0430)?|\u043d\u0430\u043a\u043b\u0430\u0434\u043d(?:\u044b\u0435|\u0430\u044f)?|\u0430\u043a\u0442(?:\u044b)?|\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446(?:\u0438\u0438|\u0438\u044e)|invoice(?:s)?|bill(?:s)?|waybill(?:s)?)|document(?:s)?\s+(?:then|next)?|(?:then|next)\s+documents?|go\s+to\s+documents?)/iu.test(text);
|
||
}
|
||
function hasMovementEvidenceFollowupSignal(text) {
|
||
return /(?:\u043f\u043e\s+\u0434\u0432\u0438\u0436\u0435\u043d(?:\u0438\u044f\u043c|\u0438\u044f)?|\u0434\u0430\u0432\u0430\u0439\s+\u0434\u0432\u0438\u0436\u0435\u043d(?:\u0438\u044f|\u0438\u0435)|\u0438\u0449\u0438\s+\u0434\u0432\u0438\u0436\u0435\u043d(?:\u0438\u044f|\u0438\u0435)|\u043f\u043e\u043a\u0430\u0436\u0438\s+\u0434\u0432\u0438\u0436\u0435\u043d(?:\u0438\u044f|\u0438\u0435)|\u0431\u0430\u043d\u043a\u043e\u0432\u0441\u043a(?:\u0438\u0435|\u0438\u0439)\s+\u0434\u0432\u0438\u0436\u0435\u043d(?:\u0438\u044f|\u0438\u0435)|(?:\u043f\u043e\u043a\u0430\u0436\u0438|\u043a\u0430\u043a\u0438\u0435|\u0441\u043f\u0438\u0441\u043e\u043a|\u0434\u0430\u0439|\u0438\u0449\u0438)\s+(?:\u043f\u043b\u0430\u0442[еe]\u0436(?:\u0438|\u0438)?|\u043e\u043f\u0435\u0440\u0430\u0446(?:\u0438\u0438|\u0438\u044e)|\u043f\u0440\u043e\u0432\u043e\u0434\u043a(?:\u0438|\u0430)|\u0441\u043f\u0438\u0441\u0430\u043d(?:\u0438\u044f|\u0438\u0435)|\u043f\u043e\u0441\u0442\u0443\u043f\u043b\u0435\u043d(?:\u0438\u044f|\u0438\u0435)|payment(?:s)?|transaction(?:s)?|operation(?:s)?|posting(?:s)?|bank\s+operation(?:s)?)|movement(?:s)?\s+(?:then|next)?|(?:then|next)\s+movements?|go\s+to\s+movements?)/iu.test(text);
|
||
}
|
||
function hasPronounDocumentEvidenceFollowupSignal(text) {
|
||
return /(?:\u043f\u043e\s+(?:\u043d\u0435\u043c\u0443|\u043d\u0435\u0439|\u044d\u0442\u043e\u043c\u0443|\u044d\u0442\u043e\u0439)\s+(?:\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442(?:\u0430\u043c|\u044b)?|\u0441\u0447(?:[Рµe]С‚|\u0435\u0442)[-\u2011 ]?\u0444\u0430\u043a\u0442\u0443\u0440(?:\u044b|\u0430)?|\u043d\u0430\u043a\u043b\u0430\u0434\u043d(?:\u044b\u0435|\u0430\u044f)?|\u0430\u043a\u0442(?:\u044b)?))/iu.test(text);
|
||
}
|
||
function hasPronounMovementEvidenceFollowupSignal(text) {
|
||
return /(?:\u043f\u043e\s+(?:\u043d\u0435\u043c\u0443|\u043d\u0435\u0439|\u044d\u0442\u043e\u043c\u0443|\u044d\u0442\u043e\u0439)\s+(?:\u0434\u0432\u0438\u0436\u0435\u043d(?:\u0438\u044f\u043c|\u0438\u044f)?|\u043f\u043b\u0430\u0442[Рµe]\u0436(?:\u0430\u043c|\u0438)?|\u043e\u043f\u0435\u0440\u0430\u0446(?:\u0438\u044f\u043c|\u0438\u0438|\u0438\u044e)|\u043f\u0440\u043e\u0432\u043e\u0434\u043a(?:\u0430\u043c|\u0438)|\u0441\u043f\u0438\u0441\u0430\u043d(?:\u0438\u044f\u043c|\u0438\u0435)|\u043f\u043e\u0441\u0442\u0443\u043f\u043b\u0435\u043d(?:\u0438\u044f\u043c|\u0438\u0435)))/iu.test(text);
|
||
}
|
||
function hasMetadataDownstreamContinuationSignal(text) {
|
||
return /(?:\u0434\u0430\u0432\u0430\u0439\s+\u0434\u0430\u043b\u044c\u0448\u0435|\u0438\u0434(?:\u0435|\u0451)\u043c\s+\u0434\u0430\u043b\u044c\u0448\u0435|\u043f\u043e\u0448\u043b(?:\u0438|\u0451\u043c)\s+\u0434\u0430\u043b\u044c\u0448\u0435|\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0439|\u0438\u0449\u0438\s+\u0434\u0430\u043b\u044c\u0448\u0435|\u0438\u0449\u0438\s+\u0434\u0430\u043d\u043d\u044b\u0435|\u043f\u043e\u043a\u0430\u0436\u0438\s+\u0434\u0430\u043d\u043d\u044b\u0435|\u043f\u043e\u043a\u0430\u0436\u0438\s+\u0441\u0442\u0440\u043e\u043a\u0438|\u0433\u043b\u0443\u0431\u0436\u0435|\u0447\u0442\u043e\s+\u0434\u0430\u043b\u044c\u0448\u0435|continue|go\s+ahead|go\s+deeper|look\s+deeper|drill\s+down|show\s+(?:data|rows))/iu.test(text);
|
||
}
|
||
function hasEntityResolutionSignal(text) {
|
||
const hasSearchVerb = /(?:найд(?:и|ите|ем|у)|поищ(?:и|ите|ем)|найти|поиск|search|find|look\s*up)/iu.test(text);
|
||
const hasEntityNoun = /(?:контрагент(?:а|ов)?|поставщик(?:а|ов)?|клиент(?:а|ов)?|counterpart(?:y|ies)|supplier(?:s)?|customer(?:s)?)/iu.test(text);
|
||
return hasSearchVerb && hasEntityNoun;
|
||
}
|
||
function normalizeEntityResolutionCandidate(value) {
|
||
return value
|
||
.replace(/^(?:в\s*1с\s+|в\s+1c\s+|по\s+имени\s+)/iu, "")
|
||
.replace(/[?!.]+$/gu, "")
|
||
.replace(/^(?:контрагент(?:а|ов)?|поставщик(?:а|ов)?|клиент(?:а|ов)?)\s+/iu, "")
|
||
.replace(/^(?:counterpart(?:y|ies)|supplier(?:s)?|customer(?:s)?)\s+/iu, "")
|
||
.replace(/^[«"'\s]+|[»"'\s]+$/gu, "")
|
||
.replace(/\s+/g, " ")
|
||
.trim();
|
||
}
|
||
function rawEntityResolutionCandidate(text) {
|
||
const patterns = [
|
||
/(?:найд(?:и|ите|ем|у)|поищ(?:и|ите|ем)|найти|search|find|look\s*up)\s+(?:в\s*1с\s+|в\s+1c\s+)?(?:контрагент(?:а|ов)?|поставщик(?:а|ов)?|клиент(?:а|ов)?|counterpart(?:y|ies)|supplier(?:s)?|customer(?:s)?)\s+(.+)$/iu,
|
||
/(?:контрагент(?:а|ов)?|поставщик(?:а|ов)?|клиент(?:а|ов)?|counterpart(?:y|ies)|supplier(?:s)?|customer(?:s)?)\s+(.+?)\s+(?:найд(?:и|ите|ем|у)|поищ(?:и|ите|ем)|найти|search|find|look\s*up)\b/iu
|
||
];
|
||
for (const pattern of patterns) {
|
||
const match = text.match(pattern);
|
||
const candidate = normalizeEntityResolutionCandidate(match?.[1] ?? "");
|
||
if (candidate.length >= 2) {
|
||
return candidate;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
function metadataActionFromRawText(text) {
|
||
if (/(?:\u043e\u0431\u044a\u0435\u043a\u0442(?:\u044b|\u0430|\u043e\u0432)?|objects?)/iu.test(text)) {
|
||
return "inspect_surface";
|
||
}
|
||
if (/(?:\u043f\u043e\u043b(?:\u0435|\u044f)|field)/iu.test(text)) {
|
||
return "inspect_fields";
|
||
}
|
||
if (/(?:\u0440\u0435\u0433\u0438\u0441\u0442\u0440|register)/iu.test(text)) {
|
||
return "inspect_registers";
|
||
}
|
||
if (/(?:\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442|document)/iu.test(text)) {
|
||
return "inspect_documents";
|
||
}
|
||
if (/(?:\u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a|directory|catalog)/iu.test(text)) {
|
||
return "inspect_catalog";
|
||
}
|
||
return "inspect_catalog";
|
||
}
|
||
function metadataScopeHintFromRawText(text) {
|
||
if (/(?:\u043d\u0434\u0441|vat)/iu.test(text)) {
|
||
return "\u041d\u0414\u0421";
|
||
}
|
||
if (/(?:\u0441\u043a\u043b\u0430\u0434|inventory|stock|warehouse|\u043d\u043e\u043c\u0435\u043d\u043a\u043b\u0430\u0442\u0443\u0440)/iu.test(text)) {
|
||
return "\u0441\u043a\u043b\u0430\u0434";
|
||
}
|
||
if (/(?:\u043a\u043e\u043d\u0442\u0440\u0430\u0433\u0435\u043d\u0442|counterparty|customer|client|supplier|vendor)/iu.test(text)) {
|
||
return "\u043a\u043e\u043d\u0442\u0440\u0430\u0433\u0435\u043d\u0442";
|
||
}
|
||
return null;
|
||
}
|
||
function hasExplicitDateScopeLiteral(text) {
|
||
return /(?:\b(?:19|20)\d{2}\b|\b\d{4}-\d{2}-\d{2}\b|\b\d{4}-\d{2}\b)/iu.test(text);
|
||
}
|
||
function collectDateScopeFromRawText(text) {
|
||
const isoDate = text.match(/\b(\d{4}-\d{2}-\d{2})\b/u);
|
||
if (isoDate?.[1]) {
|
||
return isoDate[1];
|
||
}
|
||
const yearMonth = text.match(/\b(\d{4}-\d{2})\b/u);
|
||
if (yearMonth?.[1]) {
|
||
return yearMonth[1];
|
||
}
|
||
const year = text.match(/\b((?:19|20)\d{2})\b/u);
|
||
if (year?.[1]) {
|
||
return year[1];
|
||
}
|
||
return null;
|
||
}
|
||
function semanticNeedFor(input) {
|
||
const combined = compactLower(`${input.domain ?? ""} ${input.action ?? ""} ${input.unsupported ?? ""}`);
|
||
if (input.metadataSignal || /(?:metadata|schema|catalog|inspect_(?:catalog|documents|registers|fields))/iu.test(combined)) {
|
||
return "1C metadata evidence";
|
||
}
|
||
if (input.lifecycleSignal || /(?:lifecycle|activity|duration|age)/iu.test(combined)) {
|
||
return "counterparty lifecycle evidence";
|
||
}
|
||
if (input.valueFlowSignal || /(?:turnover|revenue|payment|payout|value|net|netting|balance|cashflow)/iu.test(combined)) {
|
||
return "counterparty value-flow evidence";
|
||
}
|
||
if (input.entityResolutionSignal ||
|
||
/(?:entity_resolution|search_business_entity|resolve_entity_reference|entity\s+discovery|counterparty\s+search)/iu.test(combined)) {
|
||
return "entity discovery evidence";
|
||
}
|
||
if (/(?:movement|movements|bank_operations|movement_evidence|list_movements)/iu.test(combined)) {
|
||
return "movement evidence";
|
||
}
|
||
if (/(?:document|documents|list_documents)/iu.test(combined)) {
|
||
return "document evidence";
|
||
}
|
||
return null;
|
||
}
|
||
function shouldRunDiscovery(input) {
|
||
if (input.forceDiscoveryOverExplicitIntent && input.semanticDataNeed) {
|
||
return true;
|
||
}
|
||
if (input.lifecycleSignal || input.unsupported) {
|
||
return true;
|
||
}
|
||
if (input.metadataSignal) {
|
||
return true;
|
||
}
|
||
if (input.entityResolutionSignal) {
|
||
return true;
|
||
}
|
||
if (input.valueFlowSignal && !input.explicitIntentCandidate) {
|
||
return true;
|
||
}
|
||
if (input.followupDiscoverySeedApplicable && !input.explicitIntentCandidate && input.semanticDataNeed) {
|
||
return true;
|
||
}
|
||
if (!input.explicitIntentCandidate && input.semanticDataNeed) {
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
function buildAssistantMcpDiscoveryTurnInput(input) {
|
||
const assistantTurnMeaning = toRecordObject(input.assistantTurnMeaning);
|
||
const predecomposeContract = toRecordObject(input.predecomposeContract);
|
||
const followupContext = toRecordObject(input.followupContext);
|
||
const predecomposeEntities = collectPredecomposeEntities(predecomposeContract);
|
||
const followupSeed = collectFollowupDiscoverySeed(followupContext);
|
||
const reasonCodes = [];
|
||
const rawUserText = toNonEmptyString(input.userMessage);
|
||
const rawEffectiveText = toNonEmptyString(input.effectiveMessage);
|
||
const rawSignalSourceText = `${rawUserText ?? ""} ${rawEffectiveText ?? ""}`.trim();
|
||
const rawEntitySourceText = rawUserText ?? rawEffectiveText ?? rawSignalSourceText;
|
||
const rawText = compactLower(rawSignalSourceText);
|
||
const rawLifecycleSignal = hasLifecycleSignal(rawText);
|
||
const rawBidirectionalValueFlowSignal = !rawLifecycleSignal && hasBidirectionalValueFlowSignal(rawText);
|
||
const rawValueFlowSignal = !rawLifecycleSignal && (hasValueFlowSignal(rawText) || rawBidirectionalValueFlowSignal);
|
||
const rawMetadataSignal = !rawLifecycleSignal && !rawValueFlowSignal && hasMetadataSignal(rawText);
|
||
const rawEntityResolutionSignal = !rawLifecycleSignal && !rawValueFlowSignal && !rawMetadataSignal && hasEntityResolutionSignal(rawText);
|
||
const rawPayoutSignal = rawValueFlowSignal && !rawBidirectionalValueFlowSignal && hasPayoutSignal(rawText);
|
||
const monthlyAggregationSignal = hasMonthlyAggregationSignal(rawText);
|
||
const explicitDateScopeLiteralDetected = hasExplicitDateScopeLiteral(rawText);
|
||
const rawDateScope = collectDateScopeFromRawText(rawText);
|
||
const rawMetadataScopeHint = rawMetadataSignal ? metadataScopeHintFromRawText(rawText) : null;
|
||
const rawEntityCandidate = rawEntityResolutionSignal ? rawEntityResolutionCandidate(rawEntitySourceText) : null;
|
||
const entityResolutionSignal = rawEntityResolutionSignal || Boolean(rawEntityCandidate);
|
||
const metadataDocumentHintSignal = hasDocumentEvidenceFollowupSignal(rawText) || hasPronounDocumentEvidenceFollowupSignal(rawText);
|
||
const metadataMovementHintSignal = hasMovementEvidenceFollowupSignal(rawText) || hasPronounMovementEvidenceFollowupSignal(rawText);
|
||
const rawDomain = toNonEmptyString(assistantTurnMeaning?.asked_domain_family);
|
||
const rawAction = toNonEmptyString(assistantTurnMeaning?.asked_action_family);
|
||
const rawAggregationAxis = toNonEmptyString(assistantTurnMeaning?.asked_aggregation_axis);
|
||
const unsupported = toNonEmptyString(assistantTurnMeaning?.unsupported_but_understood_family);
|
||
const explicitIntentCandidate = toNonEmptyString(assistantTurnMeaning?.explicit_intent_candidate);
|
||
const assistantTurnMeaningDateScope = toNonEmptyString(assistantTurnMeaning?.explicit_date_scope);
|
||
const assistantTurnMeaningOrganizationScope = toNonEmptyString(assistantTurnMeaning?.explicit_organization_scope);
|
||
const predecomposeDateScope = collectDateScope(predecomposeContract);
|
||
const followupDiscoverySeedApplicable = Boolean(followupSeed.domain &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
(monthlyAggregationSignal || explicitDateScopeLiteralDetected || predecomposeDateScope));
|
||
const metadataFollowupSeedApplicable = Boolean(followupSeed.domain === "metadata" &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
hasMetadataObjectHint(rawText));
|
||
const metadataGroundedDocumentFollowupApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
followupSeed.metadataRouteFamily === "document_evidence" &&
|
||
!followupSeed.metadataAmbiguityDetected &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
metadataDocumentHintSignal);
|
||
const metadataAmbiguityResolvedDocumentFollowupApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
followupSeed.metadataAmbiguityDetected &&
|
||
(followupSeed.metadataAmbiguityEntitySets.length === 0 ||
|
||
metadataEntitySetsSuggestDocumentLane(followupSeed.metadataAmbiguityEntitySets)) &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
metadataDocumentHintSignal);
|
||
const metadataGroundedMovementFollowupApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
followupSeed.metadataRouteFamily === "movement_evidence" &&
|
||
!followupSeed.metadataAmbiguityDetected &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
metadataMovementHintSignal);
|
||
const metadataAmbiguityResolvedMovementFollowupApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
followupSeed.metadataAmbiguityDetected &&
|
||
(followupSeed.metadataAmbiguityEntitySets.length === 0 ||
|
||
metadataEntitySetsSuggestMovementLane(followupSeed.metadataAmbiguityEntitySets)) &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
metadataMovementHintSignal);
|
||
const entityResolutionGroundedDocumentFollowupApplicable = Boolean(followupSeed.pilotScope === "entity_resolution_search_v1" &&
|
||
(followupSeed.counterparty || followupSeed.discoveryEntity) &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
metadataDocumentHintSignal);
|
||
const entityResolutionGroundedMovementFollowupApplicable = Boolean(followupSeed.pilotScope === "entity_resolution_search_v1" &&
|
||
(followupSeed.counterparty || followupSeed.discoveryEntity) &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
metadataMovementHintSignal);
|
||
const groundedValueFlowFollowupApplicable = Boolean(rawValueFlowSignal &&
|
||
!rawLifecycleSignal &&
|
||
!rawMetadataSignal &&
|
||
(followupSeed.counterparty || followupSeed.discoveryEntity) &&
|
||
(followupSeed.pilotScope === "entity_resolution_search_v1" ||
|
||
followupSeed.pilotScope === "counterparty_document_evidence_query_documents_v1" ||
|
||
followupSeed.pilotScope === "counterparty_movement_evidence_query_movements_v1" ||
|
||
followupSeed.pilotScope === "counterparty_value_flow_query_movements_v1" ||
|
||
followupSeed.pilotScope === "counterparty_supplier_payout_query_movements_v1" ||
|
||
followupSeed.pilotScope === "counterparty_bidirectional_value_flow_query_movements_v1"));
|
||
const groundedValueFlowEvidenceSourceApplicable = Boolean((followupSeed.counterparty || followupSeed.discoveryEntity) &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
(followupSeed.domain === "counterparty_value" ||
|
||
followupSeed.action === "turnover" ||
|
||
followupSeed.action === "payout" ||
|
||
followupSeed.action === "net_value_flow" ||
|
||
followupSeed.pilotScope === "counterparty_value_flow_query_movements_v1" ||
|
||
followupSeed.pilotScope === "counterparty_supplier_payout_query_movements_v1" ||
|
||
followupSeed.pilotScope === "counterparty_bidirectional_value_flow_query_movements_v1"));
|
||
const valueFlowGroundedDocumentFollowupApplicable = Boolean(groundedValueFlowEvidenceSourceApplicable && metadataDocumentHintSignal);
|
||
const valueFlowGroundedMovementFollowupApplicable = Boolean(groundedValueFlowEvidenceSourceApplicable && metadataMovementHintSignal);
|
||
const documentEvidenceGroundedMovementFollowupApplicable = Boolean(followupSeed.pilotScope === "counterparty_document_evidence_query_documents_v1" &&
|
||
(followupSeed.counterparty || followupSeed.discoveryEntity) &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
metadataMovementHintSignal);
|
||
const movementEvidenceGroundedDocumentFollowupApplicable = Boolean(followupSeed.pilotScope === "counterparty_movement_evidence_query_movements_v1" &&
|
||
(followupSeed.counterparty || followupSeed.discoveryEntity) &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
metadataDocumentHintSignal);
|
||
const metadataGroundedLaneContinuationApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
(followupSeed.metadataRouteFamily === "document_evidence" ||
|
||
followupSeed.metadataRouteFamily === "movement_evidence") &&
|
||
!followupSeed.metadataAmbiguityDetected &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
!metadataDocumentHintSignal &&
|
||
!metadataMovementHintSignal &&
|
||
hasMetadataDownstreamContinuationSignal(rawText));
|
||
const metadataAmbiguityCollapsedDocumentLaneContinuationApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
followupSeed.metadataAmbiguityDetected &&
|
||
metadataAmbiguityCollapsesToDocumentLane(followupSeed.metadataAmbiguityEntitySets) &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
!metadataDocumentHintSignal &&
|
||
!metadataMovementHintSignal &&
|
||
hasMetadataDownstreamContinuationSignal(rawText));
|
||
const metadataAmbiguityCollapsedMovementLaneContinuationApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
followupSeed.metadataAmbiguityDetected &&
|
||
metadataAmbiguityCollapsesToMovementLane(followupSeed.metadataAmbiguityEntitySets) &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
!metadataDocumentHintSignal &&
|
||
!metadataMovementHintSignal &&
|
||
hasMetadataDownstreamContinuationSignal(rawText));
|
||
const metadataAmbiguityLaneClarificationApplicable = Boolean(followupSeed.pilotScope === "metadata_inspection_v1" &&
|
||
followupSeed.metadataAmbiguityDetected &&
|
||
!metadataAmbiguityCollapsesToDocumentLane(followupSeed.metadataAmbiguityEntitySets) &&
|
||
!metadataAmbiguityCollapsesToMovementLane(followupSeed.metadataAmbiguityEntitySets) &&
|
||
followupSeed.counterparty &&
|
||
!rawLifecycleSignal &&
|
||
!rawValueFlowSignal &&
|
||
!rawMetadataSignal &&
|
||
!metadataDocumentHintSignal &&
|
||
!metadataMovementHintSignal &&
|
||
hasMetadataDownstreamContinuationSignal(rawText));
|
||
const metadataGroundedDocumentLaneApplicable = metadataGroundedDocumentFollowupApplicable ||
|
||
metadataAmbiguityResolvedDocumentFollowupApplicable ||
|
||
entityResolutionGroundedDocumentFollowupApplicable ||
|
||
valueFlowGroundedDocumentFollowupApplicable ||
|
||
movementEvidenceGroundedDocumentFollowupApplicable ||
|
||
(metadataGroundedLaneContinuationApplicable && followupSeed.metadataRouteFamily === "document_evidence") ||
|
||
metadataAmbiguityCollapsedDocumentLaneContinuationApplicable;
|
||
const metadataGroundedMovementLaneApplicable = metadataGroundedMovementFollowupApplicable ||
|
||
metadataAmbiguityResolvedMovementFollowupApplicable ||
|
||
entityResolutionGroundedMovementFollowupApplicable ||
|
||
valueFlowGroundedMovementFollowupApplicable ||
|
||
documentEvidenceGroundedMovementFollowupApplicable ||
|
||
(metadataGroundedLaneContinuationApplicable && followupSeed.metadataRouteFamily === "movement_evidence") ||
|
||
metadataAmbiguityCollapsedMovementLaneContinuationApplicable;
|
||
const effectiveMetadataFollowupSeedApplicable = metadataFollowupSeedApplicable &&
|
||
!metadataAmbiguityLaneClarificationApplicable &&
|
||
!metadataGroundedDocumentLaneApplicable &&
|
||
!metadataGroundedMovementLaneApplicable;
|
||
const seededDomain = metadataAmbiguityLaneClarificationApplicable
|
||
? "metadata"
|
||
: metadataGroundedDocumentLaneApplicable
|
||
? "documents"
|
||
: metadataGroundedMovementLaneApplicable
|
||
? "movements"
|
||
: followupDiscoverySeedApplicable || effectiveMetadataFollowupSeedApplicable
|
||
? followupSeed.domain
|
||
: null;
|
||
const seededAction = metadataAmbiguityLaneClarificationApplicable
|
||
? "resolve_next_lane"
|
||
: metadataGroundedDocumentLaneApplicable
|
||
? "list_documents"
|
||
: metadataGroundedMovementLaneApplicable
|
||
? "list_movements"
|
||
: followupDiscoverySeedApplicable || effectiveMetadataFollowupSeedApplicable
|
||
? followupSeed.action
|
||
: null;
|
||
const seededUnsupported = metadataAmbiguityLaneClarificationApplicable
|
||
? "metadata_lane_choice_clarification"
|
||
: metadataGroundedDocumentLaneApplicable
|
||
? "document_evidence"
|
||
: metadataGroundedMovementLaneApplicable
|
||
? "movement_evidence"
|
||
: followupDiscoverySeedApplicable || effectiveMetadataFollowupSeedApplicable
|
||
? followupSeed.unsupported
|
||
: null;
|
||
const lifecycleSignal = rawLifecycleSignal || seededDomain === "counterparty_lifecycle";
|
||
const bidirectionalValueFlowSignal = !lifecycleSignal &&
|
||
(rawBidirectionalValueFlowSignal || seededAction === "net_value_flow");
|
||
const valueFlowSignal = !lifecycleSignal &&
|
||
!metadataGroundedMovementLaneApplicable &&
|
||
(rawValueFlowSignal || seededDomain === "counterparty_value");
|
||
const payoutSignal = valueFlowSignal &&
|
||
!bidirectionalValueFlowSignal &&
|
||
(rawPayoutSignal || seededAction === "payout");
|
||
const semanticDataNeed = metadataAmbiguityLaneClarificationApplicable
|
||
? "metadata lane clarification"
|
||
: semanticNeedFor({
|
||
domain: rawDomain ?? seededDomain,
|
||
action: rawAction ?? seededAction,
|
||
unsupported: unsupported ?? seededUnsupported,
|
||
lifecycleSignal,
|
||
valueFlowSignal,
|
||
metadataSignal: rawMetadataSignal || effectiveMetadataFollowupSeedApplicable,
|
||
entityResolutionSignal
|
||
});
|
||
const groundedFollowupEntity = followupSeed.counterparty ?? followupSeed.discoveryEntity;
|
||
const entityCandidates = entityResolutionSignal ? [] : [];
|
||
if (entityResolutionSignal) {
|
||
pushNormalizedEntityResolutionCandidate(entityCandidates, rawEntityCandidate);
|
||
for (const candidate of collectEntityCandidates(assistantTurnMeaning?.explicit_entity_candidates)) {
|
||
pushNormalizedEntityResolutionCandidate(entityCandidates, candidate);
|
||
}
|
||
pushNormalizedEntityResolutionCandidate(entityCandidates, predecomposeEntities.counterparty);
|
||
pushNormalizedEntityResolutionCandidate(entityCandidates, followupSeed.counterparty);
|
||
}
|
||
else {
|
||
if (groundedFollowupEntity) {
|
||
pushUnique(entityCandidates, groundedFollowupEntity);
|
||
}
|
||
for (const candidate of collectEntityCandidates(assistantTurnMeaning?.explicit_entity_candidates)) {
|
||
pushScopedEntityCandidate(entityCandidates, candidate, groundedFollowupEntity);
|
||
}
|
||
pushScopedEntityCandidate(entityCandidates, predecomposeEntities.counterparty, groundedFollowupEntity);
|
||
if (!groundedFollowupEntity) {
|
||
pushScopedEntityCandidate(entityCandidates, followupSeed.counterparty, null);
|
||
pushScopedEntityCandidate(entityCandidates, followupSeed.discoveryEntity, null);
|
||
}
|
||
pushScopedEntityCandidate(entityCandidates, rawEntityCandidate, groundedFollowupEntity);
|
||
}
|
||
if ((rawMetadataSignal || metadataFollowupSeedApplicable) && !groundedFollowupEntity) {
|
||
pushUnique(entityCandidates, followupSeed.discoveryEntity);
|
||
pushUnique(entityCandidates, rawMetadataScopeHint);
|
||
}
|
||
if (valueFlowSignal && !predecomposeEntities.counterparty && !followupSeed.counterparty) {
|
||
pushUnique(entityCandidates, predecomposeEntities.organization);
|
||
pushUnique(entityCandidates, followupSeed.organization);
|
||
}
|
||
const explicitOrganizationScope = valueFlowSignal && !predecomposeEntities.counterparty && !followupSeed.counterparty
|
||
? null
|
||
: predecomposeEntities.organization ?? assistantTurnMeaningOrganizationScope ?? followupSeed.organization;
|
||
const explicitDateScope = assistantTurnMeaningDateScope ?? predecomposeDateScope ?? rawDateScope ?? followupSeed.dateScope;
|
||
const turnMeaning = {
|
||
asked_domain_family: lifecycleSignal
|
||
? "counterparty_lifecycle"
|
||
: valueFlowSignal
|
||
? "counterparty_value"
|
||
: metadataGroundedMovementLaneApplicable
|
||
? "movements"
|
||
: metadataGroundedDocumentLaneApplicable
|
||
? "documents"
|
||
: entityResolutionSignal
|
||
? "entity_resolution"
|
||
: rawMetadataSignal || effectiveMetadataFollowupSeedApplicable
|
||
? "metadata"
|
||
: rawDomain ?? seededDomain,
|
||
asked_action_family: lifecycleSignal
|
||
? "activity_duration"
|
||
: valueFlowSignal
|
||
? bidirectionalValueFlowSignal
|
||
? "net_value_flow"
|
||
: payoutSignal
|
||
? "payout"
|
||
: rawAction ?? seededAction ?? "turnover"
|
||
: metadataGroundedMovementLaneApplicable
|
||
? "list_movements"
|
||
: metadataGroundedDocumentLaneApplicable
|
||
? "list_documents"
|
||
: entityResolutionSignal
|
||
? "search_business_entity"
|
||
: rawMetadataSignal || effectiveMetadataFollowupSeedApplicable
|
||
? metadataActionFromRawText(rawText) ?? seededAction
|
||
: rawAction ?? seededAction,
|
||
asked_aggregation_axis: monthlyAggregationSignal ? "month" : rawAggregationAxis,
|
||
explicit_entity_candidates: entityCandidates,
|
||
metadata_ambiguity_entity_sets: metadataAmbiguityLaneClarificationApplicable && followupSeed.metadataAmbiguityEntitySets.length > 0
|
||
? followupSeed.metadataAmbiguityEntitySets
|
||
: undefined,
|
||
explicit_organization_scope: explicitOrganizationScope,
|
||
explicit_date_scope: explicitDateScope,
|
||
unsupported_but_understood_family: unsupported ??
|
||
(lifecycleSignal
|
||
? "counterparty_lifecycle"
|
||
: valueFlowSignal
|
||
? bidirectionalValueFlowSignal
|
||
? "counterparty_bidirectional_value_flow_or_netting"
|
||
: payoutSignal
|
||
? "counterparty_payouts_or_outflow"
|
||
: seededUnsupported ?? "counterparty_value_or_turnover"
|
||
: metadataGroundedMovementLaneApplicable
|
||
? "movement_evidence"
|
||
: metadataGroundedDocumentLaneApplicable
|
||
? "document_evidence"
|
||
: metadataAmbiguityLaneClarificationApplicable
|
||
? "metadata_lane_choice_clarification"
|
||
: entityResolutionSignal
|
||
? "entity_resolution"
|
||
: rawMetadataSignal || effectiveMetadataFollowupSeedApplicable
|
||
? "1c_metadata_surface"
|
||
: followupDiscoverySeedApplicable
|
||
? seededUnsupported
|
||
: null),
|
||
stale_replay_forbidden: Boolean(assistantTurnMeaning?.stale_replay_forbidden ||
|
||
unsupported ||
|
||
lifecycleSignal ||
|
||
valueFlowSignal ||
|
||
metadataGroundedMovementLaneApplicable ||
|
||
metadataGroundedDocumentLaneApplicable ||
|
||
metadataAmbiguityLaneClarificationApplicable ||
|
||
entityResolutionSignal ||
|
||
rawMetadataSignal ||
|
||
effectiveMetadataFollowupSeedApplicable ||
|
||
followupDiscoverySeedApplicable)
|
||
};
|
||
const cleanTurnMeaning = {};
|
||
if (toNonEmptyString(turnMeaning.asked_domain_family)) {
|
||
cleanTurnMeaning.asked_domain_family = turnMeaning.asked_domain_family;
|
||
}
|
||
if (toNonEmptyString(turnMeaning.asked_action_family)) {
|
||
cleanTurnMeaning.asked_action_family = turnMeaning.asked_action_family;
|
||
}
|
||
if (toNonEmptyString(turnMeaning.asked_aggregation_axis)) {
|
||
cleanTurnMeaning.asked_aggregation_axis = turnMeaning.asked_aggregation_axis;
|
||
}
|
||
if ((turnMeaning.explicit_entity_candidates?.length ?? 0) > 0) {
|
||
cleanTurnMeaning.explicit_entity_candidates = turnMeaning.explicit_entity_candidates;
|
||
}
|
||
if ((turnMeaning.metadata_ambiguity_entity_sets?.length ?? 0) > 0) {
|
||
cleanTurnMeaning.metadata_ambiguity_entity_sets = turnMeaning.metadata_ambiguity_entity_sets;
|
||
}
|
||
if (toNonEmptyString(turnMeaning.explicit_organization_scope)) {
|
||
cleanTurnMeaning.explicit_organization_scope = turnMeaning.explicit_organization_scope;
|
||
}
|
||
if (toNonEmptyString(turnMeaning.explicit_date_scope)) {
|
||
cleanTurnMeaning.explicit_date_scope = turnMeaning.explicit_date_scope;
|
||
}
|
||
if (toNonEmptyString(turnMeaning.unsupported_but_understood_family)) {
|
||
cleanTurnMeaning.unsupported_but_understood_family = turnMeaning.unsupported_but_understood_family;
|
||
}
|
||
if (turnMeaning.stale_replay_forbidden) {
|
||
cleanTurnMeaning.stale_replay_forbidden = true;
|
||
}
|
||
const runDiscovery = shouldRunDiscovery({
|
||
unsupported: unsupported ?? seededUnsupported,
|
||
lifecycleSignal,
|
||
valueFlowSignal,
|
||
metadataSignal: rawMetadataSignal || effectiveMetadataFollowupSeedApplicable,
|
||
entityResolutionSignal,
|
||
semanticDataNeed,
|
||
explicitIntentCandidate,
|
||
followupDiscoverySeedApplicable: followupDiscoverySeedApplicable ||
|
||
effectiveMetadataFollowupSeedApplicable ||
|
||
metadataAmbiguityLaneClarificationApplicable ||
|
||
metadataGroundedMovementLaneApplicable ||
|
||
metadataGroundedDocumentLaneApplicable ||
|
||
groundedValueFlowFollowupApplicable,
|
||
forceDiscoveryOverExplicitIntent: metadataAmbiguityLaneClarificationApplicable ||
|
||
metadataGroundedMovementLaneApplicable ||
|
||
metadataGroundedDocumentLaneApplicable ||
|
||
groundedValueFlowFollowupApplicable
|
||
});
|
||
const hasTurnMeaning = Object.keys(cleanTurnMeaning).length > 0;
|
||
const sourceSignal = assistantTurnMeaning
|
||
? "assistant_turn_meaning"
|
||
: followupDiscoverySeedApplicable || effectiveMetadataFollowupSeedApplicable || metadataAmbiguityLaneClarificationApplicable
|
||
? "followup_context"
|
||
: metadataGroundedMovementLaneApplicable
|
||
? "followup_context"
|
||
: metadataGroundedDocumentLaneApplicable
|
||
? "followup_context"
|
||
: predecomposeContract
|
||
? "predecompose_contract"
|
||
: lifecycleSignal
|
||
? "raw_text"
|
||
: valueFlowSignal
|
||
? "raw_text"
|
||
: entityResolutionSignal
|
||
? "raw_text"
|
||
: rawMetadataSignal || effectiveMetadataFollowupSeedApplicable
|
||
? "raw_text"
|
||
: "none";
|
||
if (lifecycleSignal) {
|
||
pushReason(reasonCodes, "mcp_discovery_lifecycle_signal_detected");
|
||
}
|
||
if (valueFlowSignal) {
|
||
pushReason(reasonCodes, "mcp_discovery_value_flow_signal_detected");
|
||
}
|
||
if (rawMetadataSignal) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_signal_detected");
|
||
}
|
||
if (entityResolutionSignal) {
|
||
pushReason(reasonCodes, "mcp_discovery_entity_resolution_signal_detected");
|
||
}
|
||
if (rawMetadataScopeHint) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_scope_hint_from_raw_text");
|
||
}
|
||
if (rawEntityCandidate) {
|
||
pushReason(reasonCodes, "mcp_discovery_entity_scope_from_raw_entity_search");
|
||
}
|
||
if (payoutSignal) {
|
||
pushReason(reasonCodes, "mcp_discovery_payout_signal_detected");
|
||
}
|
||
if (bidirectionalValueFlowSignal) {
|
||
pushReason(reasonCodes, "mcp_discovery_bidirectional_value_flow_signal_detected");
|
||
}
|
||
if (monthlyAggregationSignal) {
|
||
pushReason(reasonCodes, "mcp_discovery_monthly_aggregation_signal_detected");
|
||
}
|
||
if (followupDiscoverySeedApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_seeded_from_followup_context");
|
||
}
|
||
if (effectiveMetadataFollowupSeedApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_seeded_from_followup_context");
|
||
}
|
||
if (metadataGroundedDocumentFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_grounded_document_followup");
|
||
}
|
||
if (metadataAmbiguityResolvedDocumentFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_ambiguity_resolved_to_document_lane");
|
||
}
|
||
if (metadataGroundedMovementFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_grounded_movement_followup");
|
||
}
|
||
if (metadataAmbiguityResolvedMovementFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_ambiguity_resolved_to_movement_lane");
|
||
}
|
||
if (entityResolutionGroundedDocumentFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_entity_resolution_grounded_document_followup");
|
||
}
|
||
if (entityResolutionGroundedMovementFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_entity_resolution_grounded_movement_followup");
|
||
}
|
||
if (valueFlowGroundedDocumentFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_value_flow_grounded_document_followup");
|
||
}
|
||
if (valueFlowGroundedMovementFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_value_flow_grounded_movement_followup");
|
||
}
|
||
if (groundedValueFlowFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_grounded_value_flow_followup");
|
||
}
|
||
if (documentEvidenceGroundedMovementFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_document_evidence_grounded_movement_followup");
|
||
}
|
||
if (movementEvidenceGroundedDocumentFollowupApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_movement_evidence_grounded_document_followup");
|
||
}
|
||
if (metadataGroundedLaneContinuationApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_grounded_lane_continuation");
|
||
}
|
||
if (metadataAmbiguityCollapsedDocumentLaneContinuationApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_ambiguity_collapsed_to_document_lane");
|
||
}
|
||
if (metadataAmbiguityCollapsedMovementLaneContinuationApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_ambiguity_collapsed_to_movement_lane");
|
||
}
|
||
if (metadataAmbiguityLaneClarificationApplicable) {
|
||
pushReason(reasonCodes, "mcp_discovery_metadata_ambiguity_requires_lane_choice");
|
||
}
|
||
if (unsupported) {
|
||
pushReason(reasonCodes, "mcp_discovery_unsupported_but_understood_turn");
|
||
}
|
||
if (predecomposeEntities.counterparty) {
|
||
pushReason(reasonCodes, "mcp_discovery_counterparty_from_predecompose");
|
||
}
|
||
if (followupSeed.counterparty) {
|
||
pushReason(reasonCodes, "mcp_discovery_counterparty_from_followup_context");
|
||
}
|
||
if (followupSeed.dateScope) {
|
||
pushReason(reasonCodes, "mcp_discovery_date_scope_from_followup_context");
|
||
}
|
||
if (entityCandidates.length > 0) {
|
||
pushReason(reasonCodes, "mcp_discovery_entity_scope_available");
|
||
}
|
||
if (!runDiscovery) {
|
||
pushReason(reasonCodes, "mcp_discovery_not_applicable_for_supported_exact_turn");
|
||
}
|
||
if (runDiscovery && !hasTurnMeaning) {
|
||
pushReason(reasonCodes, "mcp_discovery_turn_meaning_missing");
|
||
}
|
||
return {
|
||
schema_version: exports.ASSISTANT_MCP_DISCOVERY_TURN_INPUT_SCHEMA_VERSION,
|
||
policy_owner: "assistantMcpDiscoveryTurnInputAdapter",
|
||
adapter_status: !runDiscovery ? "not_applicable" : hasTurnMeaning ? "ready" : "needs_more_context",
|
||
should_run_discovery: runDiscovery,
|
||
semantic_data_need: runDiscovery ? semanticDataNeed : null,
|
||
turn_meaning_ref: runDiscovery && hasTurnMeaning ? cleanTurnMeaning : null,
|
||
source_signal: sourceSignal,
|
||
reason_codes: reasonCodes
|
||
};
|
||
}
|