ГЛОБАЛЬНЫЙ РЕФАКТОРИНГ АРХИТЕКТУРЫ - Рефакторинг этапов 2.34 вынос address-ветку (orchestration + lane + finalize) в единый runtime-оркестратор, чтобы handleMessage стал почти плоским.

This commit is contained in:
2026-04-10 22:26:05 +03:00
parent 353cbc1763
commit 9c22460e8b
9 changed files with 869 additions and 185 deletions
@@ -0,0 +1,89 @@
import type { AssistantAddressLaneLike, AssistantAddressFollowupCarryoverLike } from "./assistantAddressLaneRuntimeAdapter";
import type { AssistantMessageResponsePayload } from "../types/assistant";
import {
finalizeAssistantAddressTurn,
type FinalizeAssistantAddressTurnInput
} from "./assistantAddressTurnFinalizeRuntimeAdapter";
export interface RunAssistantAddressLaneResponseRuntimeInput<ResponseType = AssistantMessageResponsePayload> {
sessionId: string;
userMessage: string;
effectiveAddressUserMessage: string;
addressLane: AssistantAddressLaneLike;
carryoverMeta?: AssistantAddressFollowupCarryoverLike | null;
llmPreDecomposeMeta?: Record<string, unknown> | null;
knownOrganizations: string[];
activeOrganization: string | null;
sanitizeOutgoingAssistantText: (text: unknown, fallback?: string) => string;
buildAddressDebugPayload: (
addressDebug: unknown,
llmPreDecomposeMeta?: Record<string, unknown> | null
) => Record<string, unknown>;
buildAddressFollowupOffer: (addressDebug: Record<string, unknown>) => unknown;
mergeKnownOrganizations: (organizations: string[]) => string[];
toNonEmptyString: (value: unknown) => string | null;
appendItem: FinalizeAssistantAddressTurnInput["appendItem"];
getSession: FinalizeAssistantAddressTurnInput["getSession"];
persistSession: FinalizeAssistantAddressTurnInput["persistSession"];
cloneConversation: FinalizeAssistantAddressTurnInput["cloneConversation"];
logEvent: FinalizeAssistantAddressTurnInput["logEvent"];
messageIdFactory: FinalizeAssistantAddressTurnInput["messageIdFactory"];
finalizeAddressTurn?: (
input: FinalizeAssistantAddressTurnInput
) => {
response: ResponseType;
};
}
export interface RunAssistantAddressLaneResponseRuntimeOutput<ResponseType = AssistantMessageResponsePayload> {
response: ResponseType;
debug: Record<string, unknown>;
}
export function runAssistantAddressLaneResponseRuntime<ResponseType = AssistantMessageResponsePayload>(
input: RunAssistantAddressLaneResponseRuntimeInput<ResponseType>
): RunAssistantAddressLaneResponseRuntimeOutput<ResponseType> {
const finalizeAddressTurnSafe = input.finalizeAddressTurn ?? finalizeAssistantAddressTurn;
const safeAddressReply = input.sanitizeOutgoingAssistantText(input.addressLane.reply_text);
const debug = input.buildAddressDebugPayload(input.addressLane.debug, input.llmPreDecomposeMeta);
const followupOffer = input.buildAddressFollowupOffer(debug);
if (followupOffer) {
debug.address_followup_offer = followupOffer;
}
const debugKnownOrganizations = input.mergeKnownOrganizations(input.knownOrganizations);
const debugFilters =
debug?.extracted_filters && typeof debug.extracted_filters === "object"
? (debug.extracted_filters as Record<string, unknown>)
: null;
const debugActiveOrganization =
input.toNonEmptyString(debugFilters?.organization) ??
input.toNonEmptyString(input.activeOrganization);
if (debugKnownOrganizations.length > 0) {
debug.assistant_known_organizations = debugKnownOrganizations;
}
if (debugActiveOrganization) {
debug.assistant_active_organization = debugActiveOrganization;
}
const finalization = finalizeAddressTurnSafe({
sessionId: input.sessionId,
userMessage: input.userMessage,
effectiveAddressUserMessage: input.effectiveAddressUserMessage,
assistantReply: safeAddressReply,
replyType: input.addressLane.reply_type as any,
addressLaneDebug: (input.addressLane.debug ?? null) as any,
debug,
carryoverMeta: (input.carryoverMeta ?? null) as any,
llmPreDecomposeMeta: (input.llmPreDecomposeMeta ?? null) as any,
appendItem: input.appendItem,
getSession: input.getSession,
persistSession: input.persistSession,
cloneConversation: input.cloneConversation,
logEvent: input.logEvent,
messageIdFactory: input.messageIdFactory
});
return {
response: finalization.response as ResponseType,
debug
};
}
@@ -0,0 +1,189 @@
import {
buildAssistantAddressOrchestrationRuntime,
type AssistantAddressCarryoverLike,
type BuildAssistantAddressOrchestrationRuntimeInput,
type BuildAssistantAddressOrchestrationRuntimeOutput
} from "./assistantAddressOrchestrationRuntimeAdapter";
import {
runAssistantAddressLaneRuntime,
type AssistantAddressLaneLike,
type RunAssistantAddressLaneRuntimeOutput
} from "./assistantAddressLaneRuntimeAdapter";
import {
runAssistantAddressToolGateRuntime,
type AssistantAddressToolGateRuntimeOutput
} from "./assistantAddressToolGateRuntimeAdapter";
export interface RunAssistantAddressRuntimeInput<ResponseType = unknown> {
featureAssistantAddressQueryV1: boolean;
sessionId: string;
userMessage: string;
sessionItems: unknown[];
llmProvider: unknown;
useMock: boolean;
featureAddressLlmPredecomposeV1: boolean;
runAddressLlmPreDecompose: () => Promise<Record<string, unknown>>;
buildAddressLlmPredecomposeContractV1: BuildAssistantAddressOrchestrationRuntimeInput["buildAddressLlmPredecomposeContractV1"];
sanitizeAddressMessageForFallback: BuildAssistantAddressOrchestrationRuntimeInput["sanitizeAddressMessageForFallback"];
toNonEmptyString: (value: unknown) => string | null;
resolveAddressFollowupCarryoverContext: BuildAssistantAddressOrchestrationRuntimeInput["resolveAddressFollowupCarryoverContext"];
resolveAssistantOrchestrationDecision: BuildAssistantAddressOrchestrationRuntimeInput["resolveAssistantOrchestrationDecision"];
buildAddressDialogContinuationContractV2: BuildAssistantAddressOrchestrationRuntimeInput["buildAddressDialogContinuationContractV2"];
runtimeAnalysisContextAsOfDate: string | null;
payloadContextPeriodHint: unknown;
compactWhitespace: (value: string) => string;
runAddressLaneAttempt: (
messageUsed: string,
carryMeta: AssistantAddressCarryoverLike | null,
analysisDateHint: string | null
) => Promise<AssistantAddressLaneLike | null>;
isRetryableAddressLimitedResult: (addressLane: AssistantAddressLaneLike | null | undefined) => boolean;
finalizeAddressLaneResponse: (
addressLane: AssistantAddressLaneLike,
effectiveAddressUserMessage: string,
carryoverMeta?: AssistantAddressCarryoverLike | null,
llmPreDecomposeMeta?: Record<string, unknown> | null
) => ResponseType;
tryHandleLivingChat: (
modeDecision: { mode?: unknown; reason?: unknown },
addressRuntimeMeta: Record<string, unknown> | null
) => Promise<ResponseType | null>;
logEvent: (payload: Record<string, unknown>) => void;
nowIso: () => string;
runAddressOrchestrationRuntime?: (
input: BuildAssistantAddressOrchestrationRuntimeInput
) => Promise<BuildAssistantAddressOrchestrationRuntimeOutput>;
runAddressToolGateRuntime?: (
input: {
sessionId: string;
userMessage: string;
addressInputMessage: string;
orchestrationDecision: BuildAssistantAddressOrchestrationRuntimeOutput["orchestrationDecision"];
livingModeDecision: BuildAssistantAddressOrchestrationRuntimeOutput["livingModeDecision"];
addressRuntimeMeta: BuildAssistantAddressOrchestrationRuntimeOutput["addressRuntimeMeta"];
logEvent: (payload: Record<string, unknown>) => void;
tryHandleLivingChat: (
modeDecision: { mode?: unknown; reason?: unknown },
addressRuntimeMeta: Record<string, unknown> | null
) => Promise<ResponseType | null>;
nowIso: () => string;
}
) => Promise<AssistantAddressToolGateRuntimeOutput<ResponseType>>;
runAddressLaneRuntime?: (
input: {
userMessage: string;
addressInputMessage: string;
carryover: AssistantAddressCarryoverLike | null;
shouldPreferContextualLane: boolean;
canRetryWithRawUserMessage: boolean;
runAddressLaneAttempt: (
messageUsed: string,
carryMeta: AssistantAddressCarryoverLike | null
) => Promise<AssistantAddressLaneLike | null>;
isRetryableAddressLimitedResult: (addressLane: AssistantAddressLaneLike | null | undefined) => boolean;
}
) => Promise<RunAssistantAddressLaneRuntimeOutput>;
}
export interface RunAssistantAddressRuntimeOutput<ResponseType = unknown> {
handled: boolean;
response: ResponseType | null;
addressRuntimeMetaForDeep: Record<string, unknown> | null;
}
export async function runAssistantAddressRuntime<ResponseType = unknown>(
input: RunAssistantAddressRuntimeInput<ResponseType>
): Promise<RunAssistantAddressRuntimeOutput<ResponseType>> {
if (!input.featureAssistantAddressQueryV1) {
return {
handled: false,
response: null,
addressRuntimeMetaForDeep: null
};
}
const runAddressOrchestrationRuntimeSafe =
input.runAddressOrchestrationRuntime ?? buildAssistantAddressOrchestrationRuntime;
const runAddressToolGateRuntimeSafe = input.runAddressToolGateRuntime ?? runAssistantAddressToolGateRuntime;
const runAddressLaneRuntimeSafe = input.runAddressLaneRuntime ?? runAssistantAddressLaneRuntime;
const addressOrchestrationRuntime = await runAddressOrchestrationRuntimeSafe({
userMessage: input.userMessage,
sessionItems: input.sessionItems,
llmProvider: input.llmProvider,
useMock: input.useMock,
featureAddressLlmPredecomposeV1: input.featureAddressLlmPredecomposeV1,
runAddressLlmPreDecompose: input.runAddressLlmPreDecompose,
buildAddressLlmPredecomposeContractV1: input.buildAddressLlmPredecomposeContractV1,
sanitizeAddressMessageForFallback: input.sanitizeAddressMessageForFallback,
toNonEmptyString: input.toNonEmptyString,
resolveAddressFollowupCarryoverContext: input.resolveAddressFollowupCarryoverContext,
resolveAssistantOrchestrationDecision: input.resolveAssistantOrchestrationDecision,
buildAddressDialogContinuationContractV2: input.buildAddressDialogContinuationContractV2
});
const addressInputMessage = addressOrchestrationRuntime.addressInputMessage;
const carryover = addressOrchestrationRuntime.carryover;
const orchestrationDecision = addressOrchestrationRuntime.orchestrationDecision;
const addressRuntimeMeta = addressOrchestrationRuntime.addressRuntimeMeta;
const livingModeDecision = addressOrchestrationRuntime.livingModeDecision;
const addressRuntimeMetaForDeep = addressRuntimeMeta;
const toolGateRuntime = await runAddressToolGateRuntimeSafe({
sessionId: input.sessionId,
userMessage: input.userMessage,
addressInputMessage,
orchestrationDecision,
livingModeDecision,
addressRuntimeMeta,
logEvent: input.logEvent,
tryHandleLivingChat: input.tryHandleLivingChat,
nowIso: input.nowIso
});
if (toolGateRuntime.handled && toolGateRuntime.response) {
return {
handled: true,
response: toolGateRuntime.response,
addressRuntimeMetaForDeep
};
}
if (Boolean(orchestrationDecision.runAddressLane)) {
const shouldPreferContextualLane = Boolean(carryover?.followupContext);
const analysisDateHint = input.runtimeAnalysisContextAsOfDate ?? input.toNonEmptyString(input.payloadContextPeriodHint);
const canRetryWithRawUserMessage =
input.compactWhitespace(String(addressInputMessage ?? "").toLowerCase()) !==
input.compactWhitespace(String(input.userMessage ?? "").toLowerCase());
const addressLaneRuntime = await runAddressLaneRuntimeSafe({
userMessage: input.userMessage,
addressInputMessage,
carryover,
shouldPreferContextualLane,
canRetryWithRawUserMessage,
runAddressLaneAttempt: (messageUsed, carryMeta) =>
input.runAddressLaneAttempt(messageUsed, carryMeta, analysisDateHint),
isRetryableAddressLimitedResult: input.isRetryableAddressLimitedResult
});
if (addressLaneRuntime.handled && addressLaneRuntime.selection) {
const response = input.finalizeAddressLaneResponse(
addressLaneRuntime.selection.addressLane,
addressLaneRuntime.selection.messageUsed,
addressLaneRuntime.selection.carryMeta,
{
...addressRuntimeMeta,
addressRetryAudit: { ...addressLaneRuntime.retryAudit }
}
);
return {
handled: true,
response,
addressRuntimeMetaForDeep
};
}
}
return {
handled: false,
response: null,
addressRuntimeMetaForDeep
};
}
@@ -19,7 +19,7 @@ import * as openaiResponsesClient_1 from "./openaiResponsesClient";
import * as addressMcpClient_1 from "./addressMcpClient";
import * as capabilitiesRegistry_1 from "./capabilitiesRegistry";
import * as assistantCanon_1 from "./assistantCanon";
import * as assistantAddressTurnFinalizeRuntimeAdapter_1 from "./assistantAddressTurnFinalizeRuntimeAdapter";
import * as assistantAddressLaneResponseRuntimeAdapter_1 from "./assistantAddressLaneResponseRuntimeAdapter";
import * as assistantCoverageGrounding_1 from "./assistantCoverageGrounding";
import * as assistantDeepTurnAnalysisRuntimeAdapter_1 from "./assistantDeepTurnAnalysisRuntimeAdapter";
import * as assistantDeepTurnCompositionRuntimeAdapter_1 from "./assistantDeepTurnCompositionRuntimeAdapter";
@@ -32,9 +32,7 @@ import * as assistantDeepTurnPlanRuntimeAdapter_1 from "./assistantDeepTurnPlanR
import * as assistantDeepTurnNormalizationRuntimeAdapter_1 from "./assistantDeepTurnNormalizationRuntimeAdapter";
import * as assistantDeepTurnResponseRuntimeAdapter_1 from "./assistantDeepTurnResponseRuntimeAdapter";
import * as assistantDeepTurnRetrievalRuntimeAdapter_1 from "./assistantDeepTurnRetrievalRuntimeAdapter";
import * as assistantAddressLaneRuntimeAdapter_1 from "./assistantAddressLaneRuntimeAdapter";
import * as assistantAddressOrchestrationRuntimeAdapter_1 from "./assistantAddressOrchestrationRuntimeAdapter";
import * as assistantAddressToolGateRuntimeAdapter_1 from "./assistantAddressToolGateRuntimeAdapter";
import * as assistantAddressRuntimeAdapter_1 from "./assistantAddressRuntimeAdapter";
import * as assistantLivingChatTurnFinalizeRuntimeAdapter_1 from "./assistantLivingChatTurnFinalizeRuntimeAdapter";
import * as assistantLivingChatRuntimeAdapter_1 from "./assistantLivingChatRuntimeAdapter";
import * as assistantQueryPlanning_1 from "./assistantQueryPlanning";
@@ -4387,31 +4385,20 @@ export class AssistantService {
}
const sessionOrganizationScope = resolveSessionOrganizationScopeContext(userMessage, session.items);
const finalizeAddressLaneResponse = (addressLane, effectiveAddressUserMessage, carryoverMeta = null, llmPreDecomposeMeta = null) => {
const safeAddressReply = sanitizeOutgoingAssistantText(addressLane.reply_text);
const debug = buildAddressDebugPayload(addressLane.debug, llmPreDecomposeMeta);
const followupOffer = buildAddressFollowupOffer(debug);
if (followupOffer) {
debug.address_followup_offer = followupOffer;
}
const debugKnownOrganizations = mergeKnownOrganizations(sessionOrganizationScope.knownOrganizations);
const debugActiveOrganization = toNonEmptyString(debug?.extracted_filters?.organization) ??
toNonEmptyString(sessionOrganizationScope.activeOrganization);
if (debugKnownOrganizations.length > 0) {
debug.assistant_known_organizations = debugKnownOrganizations;
}
if (debugActiveOrganization) {
debug.assistant_active_organization = debugActiveOrganization;
}
const finalization = (0, assistantAddressTurnFinalizeRuntimeAdapter_1.finalizeAssistantAddressTurn)({
const runtime = (0, assistantAddressLaneResponseRuntimeAdapter_1.runAssistantAddressLaneResponseRuntime)({
sessionId,
userMessage,
effectiveAddressUserMessage,
assistantReply: safeAddressReply,
replyType: addressLane.reply_type,
addressLaneDebug: addressLane.debug,
debug,
addressLane,
carryoverMeta,
llmPreDecomposeMeta,
knownOrganizations: sessionOrganizationScope.knownOrganizations,
activeOrganization: sessionOrganizationScope.activeOrganization,
sanitizeOutgoingAssistantText,
buildAddressDebugPayload,
buildAddressFollowupOffer,
mergeKnownOrganizations,
toNonEmptyString,
appendItem: (targetSessionId, item) => this.sessions.appendItem(targetSessionId, item),
getSession: (targetSessionId) => this.sessions.getSession(targetSessionId),
persistSession: (sessionState) => this.sessionLogger.persistSession(sessionState),
@@ -4419,7 +4406,7 @@ export class AssistantService {
logEvent: (payload) => (0, log_1.logJson)(payload),
messageIdFactory: () => `msg-${(0, nanoid_1.nanoid)(10)}`
});
return finalization.response;
return runtime.response;
};
const tryHandleLivingChat = async (modeDecision, addressRuntimeMeta = null) => {
try {
@@ -4520,75 +4507,46 @@ export class AssistantService {
}
};
let addressRuntimeMetaForDeep = null;
if (config_1.FEATURE_ASSISTANT_ADDRESS_QUERY_V1) {
const addressOrchestrationRuntime = await (0, assistantAddressOrchestrationRuntimeAdapter_1.buildAssistantAddressOrchestrationRuntime)({
userMessage,
sessionItems: session.items,
llmProvider: payload?.llmProvider,
useMock: Boolean(payload.useMock),
featureAddressLlmPredecomposeV1: config_1.FEATURE_ASSISTANT_ADDRESS_QUERY_LLM_PREDECOMPOSE_V1,
runAddressLlmPreDecompose: async () => runAddressLlmPreDecompose(this.normalizerService, payload, userMessage),
buildAddressLlmPredecomposeContractV1: predecomposeContract_1.buildAddressLlmPredecomposeContractV1,
sanitizeAddressMessageForFallback,
toNonEmptyString,
resolveAddressFollowupCarryoverContext,
resolveAssistantOrchestrationDecision,
buildAddressDialogContinuationContractV2
});
const addressPreDecompose = addressOrchestrationRuntime.addressPreDecompose;
const addressInputMessage = addressOrchestrationRuntime.addressInputMessage;
const carryover = addressOrchestrationRuntime.carryover;
const orchestrationDecision = addressOrchestrationRuntime.orchestrationDecision;
const addressRuntimeMeta = addressOrchestrationRuntime.addressRuntimeMeta;
addressRuntimeMetaForDeep = addressRuntimeMeta;
const livingModeDecision = addressOrchestrationRuntime.livingModeDecision;
const toolGateRuntime = await (0, assistantAddressToolGateRuntimeAdapter_1.runAssistantAddressToolGateRuntime)({
sessionId,
userMessage,
addressInputMessage,
orchestrationDecision,
livingModeDecision,
addressRuntimeMeta,
logEvent: (payload) => (0, log_1.logJson)(payload),
tryHandleLivingChat: (modeDecision, runtimeMeta) => tryHandleLivingChat(modeDecision, runtimeMeta),
nowIso: () => new Date().toISOString()
});
if (toolGateRuntime.handled && toolGateRuntime.response) {
return toolGateRuntime.response;
}
if (orchestrationDecision.runAddressLane) {
const shouldPreferContextualLane = Boolean(carryover?.followupContext);
const analysisDateHint = runtimeAnalysisContext.as_of_date ?? toNonEmptyString(payload?.context?.period_hint);
const canRetryWithRawUserMessage = compactWhitespace(String(addressInputMessage ?? "").toLowerCase()) !==
compactWhitespace(String(userMessage ?? "").toLowerCase());
const runAddressLaneAttempt = async (messageUsed, carryMeta) => {
const scopedFollowupContext = mergeFollowupContextWithOrganizationScope(carryMeta?.followupContext ?? null, sessionOrganizationScope.activeOrganization);
if (scopedFollowupContext) {
return this.addressQueryService.tryHandle(messageUsed, {
followupContext: scopedFollowupContext,
analysisDateHint
});
}
return this.addressQueryService.tryHandle(messageUsed, {
analysisDateHint
});
};
const addressLaneRuntime = await (0, assistantAddressLaneRuntimeAdapter_1.runAssistantAddressLaneRuntime)({
userMessage,
addressInputMessage,
carryover,
shouldPreferContextualLane,
canRetryWithRawUserMessage,
runAddressLaneAttempt,
isRetryableAddressLimitedResult
const runAddressLaneAttempt = async (messageUsed, carryMeta, analysisDateHint) => {
const scopedFollowupContext = mergeFollowupContextWithOrganizationScope(carryMeta?.followupContext ?? null, sessionOrganizationScope.activeOrganization);
if (scopedFollowupContext) {
return this.addressQueryService.tryHandle(messageUsed, {
followupContext: scopedFollowupContext,
analysisDateHint
});
if (addressLaneRuntime.handled && addressLaneRuntime.selection) {
return finalizeAddressLaneResponse(addressLaneRuntime.selection.addressLane, addressLaneRuntime.selection.messageUsed, addressLaneRuntime.selection.carryMeta, {
...addressRuntimeMeta,
addressRetryAudit: { ...addressLaneRuntime.retryAudit }
});
}
}
return this.addressQueryService.tryHandle(messageUsed, {
analysisDateHint
});
};
const addressRuntime = await (0, assistantAddressRuntimeAdapter_1.runAssistantAddressRuntime)({
featureAssistantAddressQueryV1: config_1.FEATURE_ASSISTANT_ADDRESS_QUERY_V1,
sessionId,
userMessage,
sessionItems: session.items,
llmProvider: payload?.llmProvider,
useMock: Boolean(payload.useMock),
featureAddressLlmPredecomposeV1: config_1.FEATURE_ASSISTANT_ADDRESS_QUERY_LLM_PREDECOMPOSE_V1,
runAddressLlmPreDecompose: async () => runAddressLlmPreDecompose(this.normalizerService, payload, userMessage),
buildAddressLlmPredecomposeContractV1: predecomposeContract_1.buildAddressLlmPredecomposeContractV1,
sanitizeAddressMessageForFallback,
toNonEmptyString,
resolveAddressFollowupCarryoverContext,
resolveAssistantOrchestrationDecision,
buildAddressDialogContinuationContractV2,
runtimeAnalysisContextAsOfDate: runtimeAnalysisContext.as_of_date,
payloadContextPeriodHint: payload?.context?.period_hint,
compactWhitespace,
runAddressLaneAttempt,
isRetryableAddressLimitedResult,
finalizeAddressLaneResponse,
tryHandleLivingChat: (modeDecision, runtimeMeta) => tryHandleLivingChat(modeDecision, runtimeMeta),
logEvent: (payload) => (0, log_1.logJson)(payload),
nowIso: () => new Date().toISOString()
});
addressRuntimeMetaForDeep = addressRuntime.addressRuntimeMetaForDeep;
if (addressRuntime.handled && addressRuntime.response) {
return addressRuntime.response;
}
const normalizationRuntime = await (0, assistantDeepTurnNormalizationRuntimeAdapter_1.buildAssistantDeepTurnNormalizationRuntime)({
userMessage,