"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runAssistantAddressLaneResponseRuntime = runAssistantAddressLaneResponseRuntime; const assistantAddressTurnFinalizeRuntimeAdapter_1 = require("./assistantAddressTurnFinalizeRuntimeAdapter"); const assistantRuntimeContractResolver_1 = require("./assistantRuntimeContractResolver"); function toRecordObject(value) { if (!value || typeof value !== "object") { return null; } return value; } function toNullableString(value) { if (typeof value !== "string") { return null; } const trimmed = value.trim(); return trimmed.length > 0 ? trimmed : null; } function toNullableBoolean(value) { if (typeof value === "boolean") { return value; } return undefined; } function normalizeAddressReplyType(value) { return value === "factual" || value === "partial_coverage" ? value : "partial_coverage"; } function normalizeAddressLaneDebug(value) { return (toRecordObject(value) ?? {}); } function normalizeCarryoverMeta(value) { const source = toRecordObject(value); if (!source) { return null; } const followupContext = toRecordObject(source.followupContext); const previousAddressIntent = toNullableString(source.previousAddressIntent) ?? toNullableString(followupContext?.previous_intent); const previousAddressAnchor = toNullableString(source.previousAddressAnchor) ?? toNullableString(followupContext?.previous_anchor); if (!previousAddressIntent && !previousAddressAnchor) { return null; } return { previousAddressIntent, previousAddressAnchor }; } function normalizeLlmPreDecomposeMeta(value) { const source = toRecordObject(value); if (!source) { return null; } const dialogContinuationContractRaw = toRecordObject(source.dialogContinuationContract); const addressRetryAuditRaw = toRecordObject(source.addressRetryAudit); const predecomposeContractRaw = toRecordObject(source.predecomposeContract); const predecomposePeriodRaw = toRecordObject(predecomposeContractRaw?.period); const semanticExtractionContractRaw = toRecordObject(source.semanticExtractionContract); const normalized = {}; const attempted = toNullableBoolean(source.attempted); if (attempted !== undefined) normalized.attempted = attempted; const applied = toNullableBoolean(source.applied); if (applied !== undefined) normalized.applied = applied; const provider = toNullableString(source.provider); if (provider) normalized.provider = provider; const traceId = toNullableString(source.traceId); if (traceId) normalized.traceId = traceId; const reason = toNullableString(source.reason); if (reason) normalized.reason = reason; const fallbackRuleHit = toNullableString(source.fallbackRuleHit); if (fallbackRuleHit) normalized.fallbackRuleHit = fallbackRuleHit; const sanitizedUserMessage = toNullableString(source.sanitizedUserMessage); if (sanitizedUserMessage) normalized.sanitizedUserMessage = sanitizedUserMessage; const toolGateDecision = toNullableString(source.toolGateDecision); if (toolGateDecision) normalized.toolGateDecision = toolGateDecision; const toolGateReason = toNullableString(source.toolGateReason); if (toolGateReason) normalized.toolGateReason = toolGateReason; if (dialogContinuationContractRaw) { const decision = toNullableString(dialogContinuationContractRaw.decision); const targetIntent = toNullableString(dialogContinuationContractRaw.target_intent); if (decision || targetIntent) { normalized.dialogContinuationContract = { decision, target_intent: targetIntent }; } } if (addressRetryAuditRaw) { const retryAttempted = toNullableBoolean(addressRetryAuditRaw.attempted); const retryReason = toNullableString(addressRetryAuditRaw.reason); const initialLimitedCategory = toNullableString(addressRetryAuditRaw.initial_limited_category); const retryResultCategory = toNullableString(addressRetryAuditRaw.retry_result_category); if (retryAttempted !== undefined || retryReason || initialLimitedCategory || retryResultCategory) { normalized.addressRetryAudit = { attempted: retryAttempted, reason: retryReason, initial_limited_category: initialLimitedCategory, retry_result_category: retryResultCategory }; } } if (predecomposeContractRaw) { const intent = toNullableString(predecomposeContractRaw.intent); const aggregationProfile = toNullableString(predecomposeContractRaw.aggregation_profile); const periodScope = toNullableString(predecomposePeriodRaw?.scope); if (intent || aggregationProfile || periodScope) { normalized.predecomposeContract = { intent, aggregation_profile: aggregationProfile, period: periodScope ? { scope: periodScope } : null }; } } if (semanticExtractionContractRaw) { const valid = toNullableBoolean(semanticExtractionContractRaw.valid); const quality = toNullableString(semanticExtractionContractRaw.quality); const applyCanonicalRecommended = toNullableBoolean(semanticExtractionContractRaw.apply_canonical_recommended); const reasonCodes = Array.isArray(semanticExtractionContractRaw.reason_codes) ? semanticExtractionContractRaw.reason_codes .map((item) => toNullableString(item)) .filter((item) => Boolean(item)) : []; if (valid !== undefined || quality || applyCanonicalRecommended !== undefined || reasonCodes.length > 0) { normalized.semanticExtractionContract = { valid: valid ?? null, quality, apply_canonical_recommended: applyCanonicalRecommended ?? null, reason_codes: reasonCodes }; } } const hasUsefulField = Object.values(normalized).some((item) => item !== undefined && item !== null); return hasUsefulField ? normalized : null; } function runAssistantAddressLaneResponseRuntime(input) { const finalizeAddressTurnSafe = input.finalizeAddressTurn ?? assistantAddressTurnFinalizeRuntimeAdapter_1.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 laneOrganizationCandidates = Array.isArray(input.addressLane.debug?.organization_candidates) ? input.addressLane.debug.organization_candidates : []; const debugKnownOrganizations = input.mergeKnownOrganizations([ ...input.knownOrganizations, ...laneOrganizationCandidates ]); const debugFilters = debug?.extracted_filters && typeof debug.extracted_filters === "object" ? debug.extracted_filters : null; const debugActiveOrganization = input.toNonEmptyString(debugFilters?.organization) ?? input.toNonEmptyString(input.activeOrganization); const followupContextSource = input.carryoverMeta?.followupContext && typeof input.carryoverMeta.followupContext === "object" ? input.carryoverMeta.followupContext : null; if (debugKnownOrganizations.length > 0) { debug.assistant_known_organizations = debugKnownOrganizations; } if (debugActiveOrganization) { debug.assistant_active_organization = debugActiveOrganization; } const rootIntent = input.toNonEmptyString(followupContextSource?.root_intent); const currentFrameKind = input.toNonEmptyString(followupContextSource?.current_frame_kind); const rootFilters = followupContextSource?.root_filters && typeof followupContextSource.root_filters === "object" ? followupContextSource.root_filters : null; if (rootIntent || currentFrameKind) { debug.address_root_frame_context = { root_intent: rootIntent, current_frame_kind: currentFrameKind, organization: input.toNonEmptyString(rootFilters?.organization), as_of_date: input.toNonEmptyString(rootFilters?.as_of_date), period_from: input.toNonEmptyString(rootFilters?.period_from), period_to: input.toNonEmptyString(rootFilters?.period_to) }; } const debugWithRuntimeContracts = (0, assistantRuntimeContractResolver_1.attachAssistantRuntimeContractShadow)(debug, { userMessage: input.userMessage, addressRuntimeMeta: input.llmPreDecomposeMeta }); const finalization = finalizeAddressTurnSafe({ sessionId: input.sessionId, userMessage: input.userMessage, effectiveAddressUserMessage: input.effectiveAddressUserMessage, assistantReply: safeAddressReply, replyType: normalizeAddressReplyType(input.addressLane.reply_type), addressLaneDebug: normalizeAddressLaneDebug(input.addressLane.debug), debug: debugWithRuntimeContracts, carryoverMeta: normalizeCarryoverMeta(input.carryoverMeta), llmPreDecomposeMeta: normalizeLlmPreDecomposeMeta(input.llmPreDecomposeMeta), appendItem: input.appendItem, getSession: input.getSession, persistSession: input.persistSession, cloneConversation: input.cloneConversation, logEvent: input.logEvent, messageIdFactory: input.messageIdFactory }); return { response: finalization.response, debug: debugWithRuntimeContracts }; }