ГЛОБАЛЬНЫЙ РЕФАКТОРИНГ АРХИТЕКТУРЫ - Рефакторинг этапов 2.652.68 - сборка входов turn attempt в новый builder: assistantTurnAttemptInputBuilder.ts / адаптер на builder (убрал inline-сборку payload для address/deep): assistantTurnAttemptRuntimeAdapter.ts / сборку followupContext/options для address lane attempt в новый builder: assistantAddressLaneAttemptQueryOptionsBuilder.ts / адаптер на builder для query options: assistantAddressLaneAttemptRuntimeAdapter.ts

This commit is contained in:
2026-04-11 10:00:05 +03:00
parent 79b636bfe6
commit bf16309a29
11 changed files with 324 additions and 56 deletions
@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveAssistantAddressLaneAttemptFollowupContext = resolveAssistantAddressLaneAttemptFollowupContext;
exports.buildAssistantAddressLaneAttemptQueryOptions = buildAssistantAddressLaneAttemptQueryOptions;
function resolveAssistantAddressLaneAttemptFollowupContext(carryMeta) {
return carryMeta?.followupContext && typeof carryMeta.followupContext === "object"
? carryMeta.followupContext
: null;
}
function buildAssistantAddressLaneAttemptQueryOptions(input) {
if (input.scopedFollowupContext) {
return {
followupContext: input.scopedFollowupContext,
analysisDateHint: input.analysisDateHint
};
}
return {
analysisDateHint: input.analysisDateHint
};
}
@@ -1,18 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runAssistantAddressLaneAttemptRuntime = runAssistantAddressLaneAttemptRuntime;
const assistantAddressLaneAttemptQueryOptionsBuilder_1 = require("./assistantAddressLaneAttemptQueryOptionsBuilder");
async function runAssistantAddressLaneAttemptRuntime(input) {
const followupContext = input.carryMeta?.followupContext && typeof input.carryMeta.followupContext === "object"
? input.carryMeta.followupContext
: null;
const followupContext = (0, assistantAddressLaneAttemptQueryOptionsBuilder_1.resolveAssistantAddressLaneAttemptFollowupContext)(input.carryMeta);
const scopedFollowupContext = input.mergeFollowupContextWithOrganizationScope(followupContext, input.activeOrganization);
if (scopedFollowupContext) {
return input.runAddressQueryTryHandle(input.messageUsed, {
followupContext: scopedFollowupContext,
analysisDateHint: input.analysisDateHint
});
}
return input.runAddressQueryTryHandle(input.messageUsed, {
analysisDateHint: input.analysisDateHint
});
return input.runAddressQueryTryHandle(input.messageUsed, (0, assistantAddressLaneAttemptQueryOptionsBuilder_1.buildAssistantAddressLaneAttemptQueryOptions)({
analysisDateHint: input.analysisDateHint,
scopedFollowupContext
}));
}
@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAssistantTurnAttemptAddressRuntimeInput = buildAssistantTurnAttemptAddressRuntimeInput;
exports.buildAssistantTurnAttemptDeepRuntimeInput = buildAssistantTurnAttemptDeepRuntimeInput;
function buildAssistantTurnAttemptAddressRuntimeInput(input) {
return {
payload: input.payload,
sessionId: input.userTurn.sessionId,
userMessage: input.userTurn.userMessage,
sessionItems: input.userTurn.session.items,
runtimeAnalysisContext: input.userTurn.runtimeAnalysisContext,
sessionOrganizationScope: input.sessionOrganizationScope
};
}
function buildAssistantTurnAttemptDeepRuntimeInput(input) {
return {
payload: input.payload,
sessionId: input.userTurn.sessionId,
questionId: input.userTurn.userItem.message_id,
userMessage: input.userTurn.userMessage,
runtimeAnalysisContext: input.userTurn.runtimeAnalysisContext,
sessionInvestigationState: input.userTurn.session.investigation_state,
addressRuntimeMetaForDeep: input.addressRuntimeMetaForDeep
};
}
@@ -1,17 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runAssistantTurnAttemptRuntime = runAssistantTurnAttemptRuntime;
const assistantTurnAttemptInputBuilder_1 = require("./assistantTurnAttemptInputBuilder");
async function runAssistantTurnAttemptRuntime(input) {
const userTurn = input.runUserTurnBootstrapRuntime(input.payload);
const sessionOrganizationScope = input.resolveSessionOrganizationScopeContext(userTurn.userMessage, userTurn.session.items);
const addressRuntime = await input.runAddressAttemptRuntime({
const addressRuntime = await input.runAddressAttemptRuntime((0, assistantTurnAttemptInputBuilder_1.buildAssistantTurnAttemptAddressRuntimeInput)({
payload: input.payload,
sessionId: userTurn.sessionId,
userMessage: userTurn.userMessage,
sessionItems: userTurn.session.items,
runtimeAnalysisContext: userTurn.runtimeAnalysisContext,
userTurn,
sessionOrganizationScope
});
}));
const addressRuntimeMetaForDeep = addressRuntime.addressRuntimeMetaForDeep ?? null;
if (addressRuntime.handled && addressRuntime.response) {
return {
@@ -22,15 +20,11 @@ async function runAssistantTurnAttemptRuntime(input) {
sessionOrganizationScope
};
}
const deepTurnRuntime = await input.runDeepTurnAttemptRuntime({
const deepTurnRuntime = await input.runDeepTurnAttemptRuntime((0, assistantTurnAttemptInputBuilder_1.buildAssistantTurnAttemptDeepRuntimeInput)({
payload: input.payload,
sessionId: userTurn.sessionId,
questionId: userTurn.userItem.message_id,
userMessage: userTurn.userMessage,
runtimeAnalysisContext: userTurn.runtimeAnalysisContext,
sessionInvestigationState: userTurn.session.investigation_state,
userTurn,
addressRuntimeMetaForDeep
});
}));
return {
response: deepTurnRuntime.response,
source: "deep",
@@ -0,0 +1,28 @@
import type { RunAssistantAddressLaneAttemptRuntimeInput } from "./assistantAddressLaneAttemptRuntimeAdapter";
export function resolveAssistantAddressLaneAttemptFollowupContext(
carryMeta: RunAssistantAddressLaneAttemptRuntimeInput["carryMeta"]
): Record<string, unknown> | null {
return carryMeta?.followupContext && typeof carryMeta.followupContext === "object"
? (carryMeta.followupContext as Record<string, unknown>)
: null;
}
export interface BuildAssistantAddressLaneAttemptQueryOptionsInput {
analysisDateHint: RunAssistantAddressLaneAttemptRuntimeInput["analysisDateHint"];
scopedFollowupContext: Record<string, unknown> | null;
}
export function buildAssistantAddressLaneAttemptQueryOptions(
input: BuildAssistantAddressLaneAttemptQueryOptionsInput
): Parameters<RunAssistantAddressLaneAttemptRuntimeInput["runAddressQueryTryHandle"]>[1] {
if (input.scopedFollowupContext) {
return {
followupContext: input.scopedFollowupContext,
analysisDateHint: input.analysisDateHint
};
}
return {
analysisDateHint: input.analysisDateHint
};
}
@@ -1,5 +1,9 @@
import type { AssistantAddressLaneLike } from "./assistantAddressLaneRuntimeAdapter";
import type { AssistantAddressCarryoverLike } from "./assistantAddressOrchestrationRuntimeAdapter";
import {
buildAssistantAddressLaneAttemptQueryOptions,
resolveAssistantAddressLaneAttemptFollowupContext
} from "./assistantAddressLaneAttemptQueryOptionsBuilder";
export interface RunAssistantAddressLaneAttemptRuntimeInput {
messageUsed: string;
@@ -22,21 +26,16 @@ export interface RunAssistantAddressLaneAttemptRuntimeInput {
export async function runAssistantAddressLaneAttemptRuntime(
input: RunAssistantAddressLaneAttemptRuntimeInput
): Promise<AssistantAddressLaneLike | null> {
const followupContext =
input.carryMeta?.followupContext && typeof input.carryMeta.followupContext === "object"
? (input.carryMeta.followupContext as Record<string, unknown>)
: null;
const followupContext = resolveAssistantAddressLaneAttemptFollowupContext(input.carryMeta);
const scopedFollowupContext = input.mergeFollowupContextWithOrganizationScope(
followupContext,
input.activeOrganization
);
if (scopedFollowupContext) {
return input.runAddressQueryTryHandle(input.messageUsed, {
followupContext: scopedFollowupContext,
analysisDateHint: input.analysisDateHint
});
}
return input.runAddressQueryTryHandle(input.messageUsed, {
analysisDateHint: input.analysisDateHint
});
return input.runAddressQueryTryHandle(
input.messageUsed,
buildAssistantAddressLaneAttemptQueryOptions({
analysisDateHint: input.analysisDateHint,
scopedFollowupContext
})
);
}
@@ -0,0 +1,45 @@
import type {
RunAssistantTurnAttemptRuntimeAddressInput,
RunAssistantTurnAttemptRuntimeDeepInput,
AssistantSessionOrganizationScopeContext
} from "./assistantTurnAttemptRuntimeAdapter";
import type { RunAssistantUserTurnBootstrapRuntimeOutput } from "./assistantUserTurnBootstrapRuntimeAdapter";
export interface BuildAssistantTurnAttemptAddressRuntimeInputInput<PayloadType = unknown> {
payload: PayloadType;
userTurn: RunAssistantUserTurnBootstrapRuntimeOutput;
sessionOrganizationScope: AssistantSessionOrganizationScopeContext;
}
export function buildAssistantTurnAttemptAddressRuntimeInput<PayloadType = unknown>(
input: BuildAssistantTurnAttemptAddressRuntimeInputInput<PayloadType>
): RunAssistantTurnAttemptRuntimeAddressInput<PayloadType> {
return {
payload: input.payload,
sessionId: input.userTurn.sessionId,
userMessage: input.userTurn.userMessage,
sessionItems: input.userTurn.session.items,
runtimeAnalysisContext: input.userTurn.runtimeAnalysisContext,
sessionOrganizationScope: input.sessionOrganizationScope
};
}
export interface BuildAssistantTurnAttemptDeepRuntimeInputInput<PayloadType = unknown> {
payload: PayloadType;
userTurn: RunAssistantUserTurnBootstrapRuntimeOutput;
addressRuntimeMetaForDeep: Record<string, unknown> | null;
}
export function buildAssistantTurnAttemptDeepRuntimeInput<PayloadType = unknown>(
input: BuildAssistantTurnAttemptDeepRuntimeInputInput<PayloadType>
): RunAssistantTurnAttemptRuntimeDeepInput<PayloadType> {
return {
payload: input.payload,
sessionId: input.userTurn.sessionId,
questionId: input.userTurn.userItem.message_id,
userMessage: input.userTurn.userMessage,
runtimeAnalysisContext: input.userTurn.runtimeAnalysisContext,
sessionInvestigationState: input.userTurn.session.investigation_state,
addressRuntimeMetaForDeep: input.addressRuntimeMetaForDeep
};
}
@@ -1,5 +1,9 @@
import type { RunAssistantAddressRuntimeOutput } from "./assistantAddressRuntimeAdapter";
import type { RunAssistantUserTurnBootstrapRuntimeOutput } from "./assistantUserTurnBootstrapRuntimeAdapter";
import {
buildAssistantTurnAttemptAddressRuntimeInput,
buildAssistantTurnAttemptDeepRuntimeInput
} from "./assistantTurnAttemptInputBuilder";
export interface AssistantSessionOrganizationScopeContext {
knownOrganizations: string[];
@@ -57,14 +61,13 @@ export async function runAssistantTurnAttemptRuntime<ResponseType = unknown, Pay
userTurn.userMessage,
userTurn.session.items
);
const addressRuntime = await input.runAddressAttemptRuntime({
payload: input.payload,
sessionId: userTurn.sessionId,
userMessage: userTurn.userMessage,
sessionItems: userTurn.session.items,
runtimeAnalysisContext: userTurn.runtimeAnalysisContext,
sessionOrganizationScope
});
const addressRuntime = await input.runAddressAttemptRuntime(
buildAssistantTurnAttemptAddressRuntimeInput({
payload: input.payload,
userTurn,
sessionOrganizationScope
})
);
const addressRuntimeMetaForDeep = addressRuntime.addressRuntimeMetaForDeep ?? null;
if (addressRuntime.handled && addressRuntime.response) {
@@ -77,15 +80,13 @@ export async function runAssistantTurnAttemptRuntime<ResponseType = unknown, Pay
};
}
const deepTurnRuntime = await input.runDeepTurnAttemptRuntime({
payload: input.payload,
sessionId: userTurn.sessionId,
questionId: userTurn.userItem.message_id,
userMessage: userTurn.userMessage,
runtimeAnalysisContext: userTurn.runtimeAnalysisContext,
sessionInvestigationState: userTurn.session.investigation_state,
addressRuntimeMetaForDeep
});
const deepTurnRuntime = await input.runDeepTurnAttemptRuntime(
buildAssistantTurnAttemptDeepRuntimeInput({
payload: input.payload,
userTurn,
addressRuntimeMetaForDeep
})
);
return {
response: deepTurnRuntime.response,
@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import {
buildAssistantAddressLaneAttemptQueryOptions,
resolveAssistantAddressLaneAttemptFollowupContext
} from "../src/services/assistantAddressLaneAttemptQueryOptionsBuilder";
describe("assistant address lane attempt query options builder", () => {
it("extracts followup context object from carry meta", () => {
const followupContext = resolveAssistantAddressLaneAttemptFollowupContext({
followupContext: {
previous_intent: "docs_by_counterparty"
}
} as any);
expect(followupContext).toEqual({
previous_intent: "docs_by_counterparty"
});
});
it("builds query options with scoped followup context when present", () => {
const options = buildAssistantAddressLaneAttemptQueryOptions({
analysisDateHint: "2020-07-31",
scopedFollowupContext: {
previous_intent: "docs_by_counterparty",
active_organization: "Org A"
}
});
expect(options).toEqual({
followupContext: {
previous_intent: "docs_by_counterparty",
active_organization: "Org A"
},
analysisDateHint: "2020-07-31"
});
});
it("builds query options with only analysis date when scoped context is missing", () => {
const options = buildAssistantAddressLaneAttemptQueryOptions({
analysisDateHint: null,
scopedFollowupContext: null
});
expect(options).toEqual({
analysisDateHint: null
});
});
});
@@ -0,0 +1,63 @@
import { describe, expect, it } from "vitest";
import {
buildAssistantTurnAttemptAddressRuntimeInput,
buildAssistantTurnAttemptDeepRuntimeInput
} from "../src/services/assistantTurnAttemptInputBuilder";
function buildUserTurn(overrides: Record<string, unknown> = {}) {
return {
session: {
session_id: "asst-1",
updated_at: "2026-04-11T00:00:00.000Z",
items: [{ role: "user", text: "msg" }],
investigation_state: { focus: "settlements_60_62" }
},
sessionId: "asst-1",
userMessageRaw: "where tail",
userMessage: "where tail",
runtimeAnalysisContext: {
as_of_date: "2020-07-31"
},
userItem: {
message_id: "msg-q1"
},
...overrides
} as any;
}
describe("assistant turn attempt input builder", () => {
it("builds address runtime input from user turn and organization scope", () => {
const runtimeInput = buildAssistantTurnAttemptAddressRuntimeInput({
payload: { user_message: "where tail" },
userTurn: buildUserTurn(),
sessionOrganizationScope: {
knownOrganizations: ["Org A"],
selectedOrganization: "Org A",
activeOrganization: "Org A"
}
});
expect(runtimeInput.sessionId).toBe("asst-1");
expect(runtimeInput.userMessage).toBe("where tail");
expect(runtimeInput.runtimeAnalysisContext).toEqual({ as_of_date: "2020-07-31" });
expect(runtimeInput.sessionOrganizationScope).toEqual({
knownOrganizations: ["Org A"],
selectedOrganization: "Org A",
activeOrganization: "Org A"
});
});
it("builds deep runtime input with investigation state and address meta", () => {
const runtimeInput = buildAssistantTurnAttemptDeepRuntimeInput({
payload: { user_message: "where tail" },
userTurn: buildUserTurn(),
addressRuntimeMetaForDeep: { attempted: true }
});
expect(runtimeInput.sessionId).toBe("asst-1");
expect(runtimeInput.questionId).toBe("msg-q1");
expect(runtimeInput.userMessage).toBe("where tail");
expect(runtimeInput.addressRuntimeMetaForDeep).toEqual({ attempted: true });
expect(runtimeInput.sessionInvestigationState).toEqual({ focus: "settlements_60_62" });
});
});