107 lines
4.1 KiB
JavaScript
107 lines
4.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.buildFragmentTextById = buildFragmentTextById;
|
|
exports.buildExecutionPlanFromRoute = buildExecutionPlanFromRoute;
|
|
exports.buildDebugRoutesFromRoute = buildDebugRoutesFromRoute;
|
|
function escapeRegex(value) {
|
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
}
|
|
function enrichFragmentTextWithHints(fragment, text) {
|
|
const baseText = String(text ?? "").trim();
|
|
const accountHints = Array.isArray(fragment.account_hints)
|
|
? Array.from(new Set(fragment.account_hints.map((item) => String(item ?? "").trim()).filter((item) => item.length > 0)))
|
|
: [];
|
|
if (accountHints.length === 0) {
|
|
return baseText;
|
|
}
|
|
const hasAccountInText = accountHints.some((account) => new RegExp(`\\b${escapeRegex(account)}\\b`, "i").test(baseText));
|
|
if (hasAccountInText) {
|
|
return baseText;
|
|
}
|
|
return `${baseText}, по счету ${accountHints.join(", ")}`;
|
|
}
|
|
function buildFragmentTextById(fragments) {
|
|
const result = new Map();
|
|
for (const item of fragments) {
|
|
if (!item || typeof item !== "object") {
|
|
continue;
|
|
}
|
|
const fragment = item;
|
|
const fragmentId = typeof fragment.fragment_id === "string" ? fragment.fragment_id : "";
|
|
if (!fragmentId) {
|
|
continue;
|
|
}
|
|
const text = (typeof fragment.raw_fragment_text === "string" && fragment.raw_fragment_text.trim()) ||
|
|
(typeof fragment.normalized_fragment_text === "string" && fragment.normalized_fragment_text.trim()) ||
|
|
"";
|
|
result.set(fragmentId, enrichFragmentTextWithHints(fragment, text));
|
|
}
|
|
return result;
|
|
}
|
|
function buildExecutionPlanFromRoute(input) {
|
|
if (!input.routeSummary) {
|
|
return [];
|
|
}
|
|
if (input.routeSummary.mode === "legacy_v1") {
|
|
return [
|
|
{
|
|
fragment_id: "F1",
|
|
requirement_ids: input.requirementByFragment.get("F1") ?? ["R1"],
|
|
route: input.routeSummary.route_hint,
|
|
should_execute: true,
|
|
fragment_text: input.userMessage,
|
|
no_route_reason: null,
|
|
clarification_reason: null
|
|
}
|
|
];
|
|
}
|
|
return input.routeSummary.decisions.map((decision) => {
|
|
const text = input.fragmentTextById.get(decision.fragment_id) ?? input.userMessage;
|
|
if (decision.route === "no_route") {
|
|
return {
|
|
fragment_id: decision.fragment_id,
|
|
requirement_ids: input.requirementByFragment.get(decision.fragment_id) ?? [],
|
|
route: "no_route",
|
|
should_execute: false,
|
|
fragment_text: text,
|
|
no_route_reason: decision.no_route_reason ?? null,
|
|
clarification_reason: decision.clarification_reason ?? null
|
|
};
|
|
}
|
|
return {
|
|
fragment_id: decision.fragment_id,
|
|
requirement_ids: input.requirementByFragment.get(decision.fragment_id) ?? [],
|
|
route: decision.route,
|
|
should_execute: true,
|
|
fragment_text: text,
|
|
no_route_reason: null,
|
|
clarification_reason: decision.clarification_reason ?? null
|
|
};
|
|
});
|
|
}
|
|
function buildDebugRoutesFromRoute(input) {
|
|
if (!input.routeSummary) {
|
|
return [];
|
|
}
|
|
if (input.routeSummary.mode === "legacy_v1") {
|
|
return [
|
|
{
|
|
fragment_id: "F1",
|
|
route: input.routeSummary.route_hint,
|
|
reason: input.resolveLegacyRouteReason(input.routeSummary.route_hint),
|
|
confidence: input.routeSummary.confidence,
|
|
intent_class: input.routeSummary.intent_class
|
|
}
|
|
];
|
|
}
|
|
return input.routeSummary.decisions.map((decision) => ({
|
|
fragment_id: decision.fragment_id,
|
|
route: decision.route,
|
|
reason: decision.reason,
|
|
route_status: decision.route_status ?? null,
|
|
no_route_reason: decision.no_route_reason ?? null,
|
|
clarification_reason: decision.clarification_reason ?? null,
|
|
execution_readiness: decision.execution_readiness ?? null
|
|
}));
|
|
}
|