64 lines
2.3 KiB
JavaScript
64 lines
2.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.finalizeAssistantLivingChatTurn = finalizeAssistantLivingChatTurn;
|
|
const nanoid_1 = require("nanoid");
|
|
const assistantTurnCommitRuntimeAdapter_1 = require("./assistantTurnCommitRuntimeAdapter");
|
|
function toTraceId(debug) {
|
|
const value = debug?.trace_id;
|
|
if (typeof value !== "string") {
|
|
return null;
|
|
}
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
}
|
|
function finalizeAssistantLivingChatTurn(input) {
|
|
const nowIso = input.nowIso ?? (() => new Date().toISOString());
|
|
const messageIdFactory = input.messageIdFactory ?? (() => `msg-${(0, nanoid_1.nanoid)(10)}`);
|
|
const commitSafe = input.commitFn ?? assistantTurnCommitRuntimeAdapter_1.commitAssistantTurnAndLog;
|
|
const assistantItem = {
|
|
message_id: messageIdFactory(),
|
|
session_id: input.sessionId,
|
|
role: "assistant",
|
|
text: input.assistantReply,
|
|
reply_type: input.replyType,
|
|
created_at: nowIso(),
|
|
trace_id: toTraceId(input.debug),
|
|
debug: input.debug
|
|
};
|
|
const logDetails = {
|
|
session_id: input.sessionId,
|
|
message_id: assistantItem.message_id,
|
|
user_message: input.userMessage,
|
|
living_router_mode: input.modeDecision?.mode ?? "chat",
|
|
living_router_reason: input.modeDecision?.reason ?? "living_chat_signal_detected",
|
|
assistant_reply: assistantItem.text,
|
|
reply_type: assistantItem.reply_type,
|
|
trace_id: assistantItem.trace_id
|
|
};
|
|
const commitResult = commitSafe({
|
|
sessionId: input.sessionId,
|
|
assistantItem,
|
|
eventType: "assistant_message_chat",
|
|
logDetails,
|
|
appendItem: input.appendItem,
|
|
getSession: input.getSession,
|
|
persistSession: input.persistSession,
|
|
cloneConversation: input.cloneConversation,
|
|
logEvent: input.logEvent
|
|
});
|
|
const response = {
|
|
ok: true,
|
|
session_id: input.sessionId,
|
|
assistant_reply: assistantItem.text,
|
|
reply_type: assistantItem.reply_type,
|
|
conversation_item: assistantItem,
|
|
debug: input.debug,
|
|
conversation: commitResult.conversation
|
|
};
|
|
return {
|
|
assistantItem,
|
|
commitResult,
|
|
response
|
|
};
|
|
}
|