АРЧ АП11 - Архитектура: вынести reply packaging из composeStage в отдельный owner
This commit is contained in:
@@ -4,6 +4,7 @@ exports.contractCandidatesFromRows = contractCandidatesFromRows;
|
||||
exports.composeFactualReply = composeFactualReply;
|
||||
exports.inferReplyType = inferReplyType;
|
||||
const assistantOrganizationMatcher_1 = require("../assistantOrganizationMatcher");
|
||||
const replyPackaging_1 = require("./replyPackaging");
|
||||
function uniqueStrings(values) {
|
||||
return Array.from(new Set(values
|
||||
.map((item) => item.trim())
|
||||
@@ -2106,8 +2107,13 @@ function buildOpenContractRiskAggregate(rows) {
|
||||
});
|
||||
}
|
||||
function composeFactualReply(intent, rows, options = {}) {
|
||||
const applyNumericEmphasis = (line) => (options.emphasizeNumbers ? emphasizeNumericTokens(line) : line);
|
||||
const joinLines = (lines) => lines.map(applyNumericEmphasis).join("\n");
|
||||
return (0, replyPackaging_1.finalizeComposeReplyResult)(composeFactualReplyBody(intent, rows, options), {
|
||||
emphasizeNumbers: options.emphasizeNumbers,
|
||||
emphasizeNumericTokens
|
||||
});
|
||||
}
|
||||
function composeFactualReplyBody(intent, rows, options = {}) {
|
||||
const joinLines = replyPackaging_1.joinComposeReplyLines;
|
||||
if (intent === "document_type_and_account_section_profile") {
|
||||
const rowsByMarker = new Map();
|
||||
for (const row of rows) {
|
||||
@@ -4370,8 +4376,5 @@ function composeFactualReply(intent, rows, options = {}) {
|
||||
};
|
||||
}
|
||||
function inferReplyType(responseType) {
|
||||
if (responseType === "FACTUAL_LIST" || responseType === "FACTUAL_SUMMARY") {
|
||||
return "factual";
|
||||
}
|
||||
return "partial_coverage";
|
||||
return (0, replyPackaging_1.inferAddressReplyType)(responseType);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.joinComposeReplyLines = joinComposeReplyLines;
|
||||
exports.finalizeComposeReplyResult = finalizeComposeReplyResult;
|
||||
exports.inferAddressReplyType = inferAddressReplyType;
|
||||
function joinComposeReplyLines(lines) {
|
||||
return lines.join("\n");
|
||||
}
|
||||
function finalizeComposeReplyResult(result, options = {}) {
|
||||
if (!options.emphasizeNumbers || typeof options.emphasizeNumericTokens !== "function") {
|
||||
return result;
|
||||
}
|
||||
return {
|
||||
...result,
|
||||
text: String(result.text ?? "")
|
||||
.split("\n")
|
||||
.map((line) => options.emphasizeNumericTokens(line))
|
||||
.join("\n")
|
||||
};
|
||||
}
|
||||
function inferAddressReplyType(responseType) {
|
||||
if (responseType === "FACTUAL_LIST" || responseType === "FACTUAL_SUMMARY") {
|
||||
return "factual";
|
||||
}
|
||||
return "partial_coverage";
|
||||
}
|
||||
@@ -5,6 +5,16 @@ import type {
|
||||
AddressResultMode
|
||||
} from "../../types/addressQuery";
|
||||
import { normalizeOrganizationScopeValue } from "../assistantOrganizationMatcher";
|
||||
import {
|
||||
finalizeComposeReplyResult,
|
||||
inferAddressReplyType,
|
||||
joinComposeReplyLines,
|
||||
type ComposeFactualReplyOptions as ComposeFactualReplyOptionsBase,
|
||||
type ComposeReplyResult,
|
||||
type ComposeReplySemantics
|
||||
} from "./replyPackaging";
|
||||
|
||||
export type { ComposeFactualReplyOptions, ComposeReplySemantics } from "./replyPackaging";
|
||||
|
||||
export interface ComposeStageRow {
|
||||
period: string | null;
|
||||
@@ -39,26 +49,7 @@ export interface VatDirectSourceProbeSummary {
|
||||
errors: string[];
|
||||
}
|
||||
|
||||
interface ComposeFactualReplyOptions {
|
||||
userMessage?: string;
|
||||
itemHint?: string;
|
||||
counterpartyHint?: string;
|
||||
organizationHint?: string;
|
||||
accountHint?: string;
|
||||
periodFrom?: string;
|
||||
periodTo?: string;
|
||||
asOfDate?: string;
|
||||
requestedResultMode?: AddressResultMode;
|
||||
vatDirectSourceProbe?: VatDirectSourceProbeSummary | null;
|
||||
emphasizeNumbers?: boolean;
|
||||
useRubCurrency?: boolean;
|
||||
}
|
||||
|
||||
export interface ComposeReplySemantics {
|
||||
result_mode?: AddressResultMode;
|
||||
evidence_strength?: AddressEvidenceStrength;
|
||||
balance_confirmed?: boolean;
|
||||
}
|
||||
type ComposeFactualReplyOptions = ComposeFactualReplyOptionsBase<VatDirectSourceProbeSummary>;
|
||||
|
||||
type PeriodProfileFocus =
|
||||
| "full_profile"
|
||||
@@ -2725,9 +2716,19 @@ export function composeFactualReply(
|
||||
intent: AddressIntent,
|
||||
rows: ComposeStageRow[],
|
||||
options: ComposeFactualReplyOptions = {}
|
||||
): { responseType: AddressResponseType; text: string; semantics?: ComposeReplySemantics } {
|
||||
const applyNumericEmphasis = (line: string): string => (options.emphasizeNumbers ? emphasizeNumericTokens(line) : line);
|
||||
const joinLines = (lines: string[]): string => lines.map(applyNumericEmphasis).join("\n");
|
||||
) {
|
||||
return finalizeComposeReplyResult(composeFactualReplyBody(intent, rows, options), {
|
||||
emphasizeNumbers: options.emphasizeNumbers,
|
||||
emphasizeNumericTokens
|
||||
});
|
||||
}
|
||||
|
||||
function composeFactualReplyBody(
|
||||
intent: AddressIntent,
|
||||
rows: ComposeStageRow[],
|
||||
options: ComposeFactualReplyOptions = {}
|
||||
): ComposeReplyResult {
|
||||
const joinLines = joinComposeReplyLines;
|
||||
|
||||
if (intent === "document_type_and_account_section_profile") {
|
||||
const rowsByMarker = new Map<string, ComposeStageRow[]>();
|
||||
@@ -5496,8 +5497,5 @@ export function composeFactualReply(
|
||||
}
|
||||
|
||||
export function inferReplyType(responseType: AddressResponseType): "factual" | "partial_coverage" {
|
||||
if (responseType === "FACTUAL_LIST" || responseType === "FACTUAL_SUMMARY") {
|
||||
return "factual";
|
||||
}
|
||||
return "partial_coverage";
|
||||
return inferAddressReplyType(responseType);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import type { AddressEvidenceStrength, AddressResponseType, AddressResultMode } from "../../types/addressQuery";
|
||||
|
||||
export interface ComposeFactualReplyOptions<TVatDirectSourceProbe = unknown> {
|
||||
userMessage?: string;
|
||||
itemHint?: string;
|
||||
counterpartyHint?: string;
|
||||
organizationHint?: string;
|
||||
accountHint?: string;
|
||||
periodFrom?: string;
|
||||
periodTo?: string;
|
||||
asOfDate?: string;
|
||||
requestedResultMode?: AddressResultMode;
|
||||
vatDirectSourceProbe?: TVatDirectSourceProbe | null;
|
||||
emphasizeNumbers?: boolean;
|
||||
useRubCurrency?: boolean;
|
||||
}
|
||||
|
||||
export interface ComposeReplySemantics {
|
||||
result_mode?: AddressResultMode;
|
||||
evidence_strength?: AddressEvidenceStrength;
|
||||
balance_confirmed?: boolean;
|
||||
}
|
||||
|
||||
export interface ComposeReplyResult {
|
||||
responseType: AddressResponseType;
|
||||
text: string;
|
||||
semantics?: ComposeReplySemantics;
|
||||
}
|
||||
|
||||
export interface FinalizeComposeReplyOptions {
|
||||
emphasizeNumbers?: boolean;
|
||||
emphasizeNumericTokens?: (line: string) => string;
|
||||
}
|
||||
|
||||
export function joinComposeReplyLines(lines: string[]): string {
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function finalizeComposeReplyResult(
|
||||
result: ComposeReplyResult,
|
||||
options: FinalizeComposeReplyOptions = {}
|
||||
): ComposeReplyResult {
|
||||
if (!options.emphasizeNumbers || typeof options.emphasizeNumericTokens !== "function") {
|
||||
return result;
|
||||
}
|
||||
|
||||
return {
|
||||
...result,
|
||||
text: String(result.text ?? "")
|
||||
.split("\n")
|
||||
.map((line) => options.emphasizeNumericTokens!(line))
|
||||
.join("\n")
|
||||
};
|
||||
}
|
||||
|
||||
export function inferAddressReplyType(responseType: AddressResponseType): "factual" | "partial_coverage" {
|
||||
if (responseType === "FACTUAL_LIST" || responseType === "FACTUAL_SUMMARY") {
|
||||
return "factual";
|
||||
}
|
||||
return "partial_coverage";
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
finalizeComposeReplyResult,
|
||||
inferAddressReplyType,
|
||||
joinComposeReplyLines
|
||||
} from "../src/services/address_runtime/replyPackaging";
|
||||
|
||||
describe("replyPackaging", () => {
|
||||
it("joins reply lines without changing their order", () => {
|
||||
expect(joinComposeReplyLines(["Первая строка", "Вторая строка"])).toBe("Первая строка\nВторая строка");
|
||||
});
|
||||
|
||||
it("applies numeric emphasis line-by-line only when requested", () => {
|
||||
const result = finalizeComposeReplyResult(
|
||||
{
|
||||
responseType: "FACTUAL_SUMMARY",
|
||||
text: "Сумма 1500\nОстаток 22",
|
||||
semantics: { result_mode: "aggregate" }
|
||||
},
|
||||
{
|
||||
emphasizeNumbers: true,
|
||||
emphasizeNumericTokens: (line) => line.replace(/\d+/g, (match) => `**${match}**`)
|
||||
}
|
||||
);
|
||||
|
||||
expect(result.text).toBe("Сумма **1500**\nОстаток **22**");
|
||||
expect(result.semantics).toEqual({ result_mode: "aggregate" });
|
||||
});
|
||||
|
||||
it("keeps reply text unchanged when emphasis is disabled", () => {
|
||||
const result = finalizeComposeReplyResult(
|
||||
{
|
||||
responseType: "FACTUAL_SUMMARY",
|
||||
text: "Сумма 1500"
|
||||
},
|
||||
{
|
||||
emphasizeNumbers: false,
|
||||
emphasizeNumericTokens: () => "не должно примениться"
|
||||
}
|
||||
);
|
||||
|
||||
expect(result.text).toBe("Сумма 1500");
|
||||
});
|
||||
|
||||
it("maps factual response types away from partial coverage", () => {
|
||||
expect(inferAddressReplyType("FACTUAL_LIST")).toBe("factual");
|
||||
expect(inferAddressReplyType("FACTUAL_SUMMARY")).toBe("factual");
|
||||
expect(inferAddressReplyType("SOFT_REFUSAL")).toBe("partial_coverage");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user