ГЛОБАЛЬНЫЙ РЕФАКТОРИНГ АРХИТЕКТУРЫ - Рефакторинг этапов 2.522.55 - Перевод finalizeAddressLaneResponse на builder вместо inline-сборки в assistantAddressAttemptRuntimeAdapter.ts \ Добавлен builder для lane-attempt input: assistantAddressLaneAttemptInputBuilder.ts. \ Добавлен builder для финального address-runtime input: assistantAddressRuntimeInputBuilder.ts.

This commit is contained in:
2026-04-11 01:09:53 +03:00
parent b612615219
commit c849eb5f5b
12 changed files with 504 additions and 67 deletions
@@ -0,0 +1,53 @@
import { describe, expect, it, vi } from "vitest";
import { buildAssistantAddressLaneAttemptRuntimeInput } from "../src/services/assistantAddressLaneAttemptInputBuilder";
function buildInput(overrides: Record<string, unknown> = {}) {
const mergeFollowupContextWithOrganizationScope = vi.fn((followupContext: Record<string, unknown> | null) =>
followupContext
);
const runAddressQueryTryHandle = vi.fn(async () => ({ response_type: "READY" }));
return {
messageUsed: "Show overdue docs",
carryMeta: { followupContext: { previous_intent: "docs_by_counterparty" } },
analysisDateHint: "2020-08-31",
activeOrganization: "Org A",
mergeFollowupContextWithOrganizationScope,
runAddressQueryTryHandle,
...overrides
} as any;
}
describe("assistant address lane attempt input builder", () => {
it("builds lane-attempt runtime input with message, carry meta and analysis date", () => {
const runtimeInput = buildAssistantAddressLaneAttemptRuntimeInput(buildInput());
expect(runtimeInput.messageUsed).toBe("Show overdue docs");
expect(runtimeInput.analysisDateHint).toBe("2020-08-31");
expect(runtimeInput.activeOrganization).toBe("Org A");
expect(runtimeInput.carryMeta).toEqual({
followupContext: { previous_intent: "docs_by_counterparty" }
});
});
it("preserves injected callbacks and nullable fields", async () => {
const mergeFollowupContextWithOrganizationScope = vi.fn(() => null);
const runAddressQueryTryHandle = vi.fn(async () => ({ response_type: "NEEDS_CONTEXT" }));
const runtimeInput = buildAssistantAddressLaneAttemptRuntimeInput(
buildInput({
carryMeta: null,
analysisDateHint: null,
activeOrganization: null,
mergeFollowupContextWithOrganizationScope,
runAddressQueryTryHandle
})
);
await runtimeInput.runAddressQueryTryHandle("message", { analysisDateHint: null });
runtimeInput.mergeFollowupContextWithOrganizationScope(null, null);
expect(runAddressQueryTryHandle).toHaveBeenCalledWith("message", { analysisDateHint: null });
expect(mergeFollowupContextWithOrganizationScope).toHaveBeenCalledWith(null, null);
expect(runtimeInput.carryMeta).toBeNull();
expect(runtimeInput.analysisDateHint).toBeNull();
});
});
@@ -0,0 +1,66 @@
import { describe, expect, it, vi } from "vitest";
import { buildAssistantAddressLaneResponseAttemptRuntimeInput } from "../src/services/assistantAddressLaneResponseAttemptInputBuilder";
function buildInput(overrides: Record<string, unknown> = {}) {
return {
sessionId: "asst-1",
userMessage: "Show overdue docs",
effectiveAddressUserMessage: "Show overdue docs for Org A",
addressLane: {
handled: true,
reply_text: "ok",
reply_type: "factual_with_explanation",
debug: {}
},
knownOrganizations: ["Org A"],
activeOrganization: "Org A",
sanitizeOutgoingAssistantText: (value: unknown, fallback = "") =>
typeof value === "string" && value.trim().length > 0 ? value.trim() : fallback,
buildAddressDebugPayload: () => ({}),
buildAddressFollowupOffer: () => null,
mergeKnownOrganizations: (organizations: string[]) => organizations,
toNonEmptyString: (value: unknown) =>
typeof value === "string" && value.trim().length > 0 ? value.trim() : null,
appendItem: vi.fn(),
getSession: vi.fn(),
persistSession: vi.fn(),
cloneConversation: vi.fn((items: unknown[]) => items),
logEvent: vi.fn(),
messageIdFactory: vi.fn(() => "msg-1"),
...overrides
} as any;
}
describe("assistant address lane response attempt input builder", () => {
it("normalizes optional carryover and predecompose meta to null", () => {
const runtimeInput = buildAssistantAddressLaneResponseAttemptRuntimeInput(
buildInput({
carryoverMeta: undefined,
llmPreDecomposeMeta: undefined
})
);
expect(runtimeInput.carryoverMeta).toBeNull();
expect(runtimeInput.llmPreDecomposeMeta).toBeNull();
expect(runtimeInput.effectiveAddressUserMessage).toBe("Show overdue docs for Org A");
expect(runtimeInput.knownOrganizations).toEqual(["Org A"]);
});
it("preserves explicit metadata and callback wiring", () => {
const logEvent = vi.fn();
const runtimeInput = buildAssistantAddressLaneResponseAttemptRuntimeInput(
buildInput({
carryoverMeta: { followupContext: { previous_intent: "docs_by_counterparty" } },
llmPreDecomposeMeta: { mode: "supported" },
logEvent
})
);
expect(runtimeInput.carryoverMeta).toEqual({
followupContext: { previous_intent: "docs_by_counterparty" }
});
expect(runtimeInput.llmPreDecomposeMeta).toEqual({ mode: "supported" });
runtimeInput.logEvent({ event: "address_done" });
expect(logEvent).toHaveBeenCalledWith({ event: "address_done" });
});
});
@@ -0,0 +1,70 @@
import { describe, expect, it, vi } from "vitest";
import { buildAssistantAddressRuntimeInput } from "../src/services/assistantAddressRuntimeInputBuilder";
function buildInput(overrides: Record<string, unknown> = {}) {
return {
featureAssistantAddressQueryV1: true,
sessionId: "asst-1",
userMessage: "Show overdue docs",
sessionItems: [],
payload: {
llmProvider: "openai",
useMock: 1,
context: {
period_hint: "2020-07-31"
}
},
featureAddressLlmPredecomposeV1: true,
runAddressLlmPreDecompose: vi.fn(async () => ({})),
buildAddressLlmPredecomposeContractV1: vi.fn(() => ({})),
sanitizeAddressMessageForFallback: vi.fn((value: string) => value),
toNonEmptyString: vi.fn((value: unknown) =>
typeof value === "string" && value.trim().length > 0 ? value.trim() : null
),
resolveAddressFollowupCarryoverContext: vi.fn(() => null),
resolveAssistantOrchestrationDecision: vi.fn(() => ({ mode: "address_query", runAddressLane: true })),
buildAddressDialogContinuationContractV2: vi.fn(() => ({})),
runtimeAnalysisContextAsOfDate: "2020-07-31",
compactWhitespace: vi.fn((value: string) => value.trim()),
runAddressLaneAttempt: vi.fn(async () => null),
isRetryableAddressLimitedResult: vi.fn(() => false),
finalizeAddressLaneResponse: vi.fn(() => ({ kind: "address" })),
tryHandleLivingChat: vi.fn(async () => null),
logEvent: vi.fn(),
nowIso: vi.fn(() => "2026-04-11T00:00:00.000Z"),
runAddressOrchestrationRuntime: vi.fn(async () => ({})),
runAddressToolGateRuntime: vi.fn(async () => ({ handled: false, response: null })),
runAddressLaneRuntime: vi.fn(async () => ({ handled: false, selection: null, retryAudit: {} })),
...overrides
} as any;
}
describe("assistant address runtime input builder", () => {
it("maps payload provider/useMock/period hint into address runtime input", () => {
const runtimeInput = buildAssistantAddressRuntimeInput(buildInput());
expect(runtimeInput.llmProvider).toBe("openai");
expect(runtimeInput.useMock).toBe(true);
expect(runtimeInput.payloadContextPeriodHint).toBe("2020-07-31");
expect(runtimeInput.sessionId).toBe("asst-1");
expect(runtimeInput.userMessage).toBe("Show overdue docs");
});
it("keeps empty payload fields safe and preserves runtime callbacks", () => {
const runAddressLaneAttempt = vi.fn(async () => null);
const tryHandleLivingChat = vi.fn(async () => null);
const runtimeInput = buildAssistantAddressRuntimeInput(
buildInput({
payload: {},
runAddressLaneAttempt,
tryHandleLivingChat
})
);
expect(runtimeInput.llmProvider).toBeUndefined();
expect(runtimeInput.useMock).toBe(false);
expect(runtimeInput.payloadContextPeriodHint).toBeUndefined();
expect(runtimeInput.runAddressLaneAttempt).toBe(runAddressLaneAttempt);
expect(runtimeInput.tryHandleLivingChat).toBe(tryHandleLivingChat);
});
});