ГЛОБАЛЬНЫЙ РЕФАКТОРИНГ АРХИТЕКТУРЫ - Рефакторинг этапов 2.34 вынос address-ветку (orchestration + lane + finalize) в единый runtime-оркестратор, чтобы handleMessage стал почти плоским.
This commit is contained in:
@@ -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
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user