169 lines
7.1 KiB
JavaScript
169 lines
7.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.runAssistantAddressLaneResponseRuntime = runAssistantAddressLaneResponseRuntime;
|
|
const assistantAddressTurnFinalizeRuntimeAdapter_1 = require("./assistantAddressTurnFinalizeRuntimeAdapter");
|
|
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 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
|
|
};
|
|
}
|
|
}
|
|
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 debugKnownOrganizations = input.mergeKnownOrganizations(input.knownOrganizations);
|
|
const debugFilters = debug?.extracted_filters && typeof debug.extracted_filters === "object"
|
|
? debug.extracted_filters
|
|
: 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: normalizeAddressReplyType(input.addressLane.reply_type),
|
|
addressLaneDebug: normalizeAddressLaneDebug(input.addressLane.debug),
|
|
debug,
|
|
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
|
|
};
|
|
}
|