ГЛОБАЛЬНЫЙ РЕФАКТОРИНГ АРХИТЕКТУРЫ - Рефакторинг этапов 2.26 вынос runtime-блок living chat из assistantService в отдельный adapter
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
"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
|
||||
};
|
||||
}
|
||||
+14
-41
@@ -75,6 +75,7 @@ const assistantDeepTurnGroundingRuntimeAdapter_1 = __importStar(require("./assis
|
||||
const assistantDeepTurnPackagingRuntimeAdapter_1 = __importStar(require("./assistantDeepTurnPackagingRuntimeAdapter"));
|
||||
const assistantDeepTurnPlanRuntimeAdapter_1 = __importStar(require("./assistantDeepTurnPlanRuntimeAdapter"));
|
||||
const assistantDeepTurnRetrievalRuntimeAdapter_1 = __importStar(require("./assistantDeepTurnRetrievalRuntimeAdapter"));
|
||||
const assistantLivingChatTurnFinalizeRuntimeAdapter_1 = __importStar(require("./assistantLivingChatTurnFinalizeRuntimeAdapter"));
|
||||
const assistantQueryPlanning_1 = __importStar(require("./assistantQueryPlanning"));
|
||||
const iconv_lite_1 = __importDefault(require("iconv-lite"));
|
||||
const DATA_SCOPE_CACHE_TTL_MS = 60_000;
|
||||
@@ -4604,49 +4605,21 @@ class AssistantService {
|
||||
normalized: null,
|
||||
normalizer_output: null
|
||||
};
|
||||
const assistantItem = {
|
||||
message_id: `msg-${(0, nanoid_1.nanoid)(10)}`,
|
||||
session_id: sessionId,
|
||||
role: "assistant",
|
||||
text: chatText,
|
||||
reply_type: "factual_with_explanation",
|
||||
created_at: new Date().toISOString(),
|
||||
trace_id: debug.trace_id,
|
||||
debug
|
||||
};
|
||||
this.sessions.appendItem(sessionId, assistantItem);
|
||||
const current = this.sessions.getSession(sessionId);
|
||||
if (current) {
|
||||
this.sessionLogger.persistSession(current);
|
||||
}
|
||||
const conversation = cloneItems(current?.items ?? []);
|
||||
(0, log_1.logJson)({
|
||||
timestamp: new Date().toISOString(),
|
||||
level: "info",
|
||||
service: "assistant_loop",
|
||||
message: "assistant_message_processed",
|
||||
const finalization = (0, assistantLivingChatTurnFinalizeRuntimeAdapter_1.finalizeAssistantLivingChatTurn)({
|
||||
sessionId,
|
||||
eventType: "assistant_message_chat",
|
||||
details: {
|
||||
session_id: sessionId,
|
||||
message_id: assistantItem.message_id,
|
||||
user_message: userMessage,
|
||||
living_router_mode: modeDecision?.mode ?? "chat",
|
||||
living_router_reason: modeDecision?.reason ?? "living_chat_signal_detected",
|
||||
assistant_reply: assistantItem.text,
|
||||
reply_type: assistantItem.reply_type,
|
||||
trace_id: assistantItem.trace_id
|
||||
}
|
||||
});
|
||||
return {
|
||||
ok: true,
|
||||
session_id: sessionId,
|
||||
assistant_reply: assistantItem.text,
|
||||
reply_type: assistantItem.reply_type,
|
||||
conversation_item: assistantItem,
|
||||
userMessage,
|
||||
assistantReply: chatText,
|
||||
replyType: "factual_with_explanation",
|
||||
debug,
|
||||
conversation
|
||||
};
|
||||
modeDecision,
|
||||
appendItem: (targetSessionId, item) => this.sessions.appendItem(targetSessionId, item),
|
||||
getSession: (targetSessionId) => this.sessions.getSession(targetSessionId),
|
||||
persistSession: (sessionState) => this.sessionLogger.persistSession(sessionState),
|
||||
cloneConversation: (items) => cloneItems(items),
|
||||
logEvent: (payload) => (0, log_1.logJson)(payload),
|
||||
messageIdFactory: () => `msg-${(0, nanoid_1.nanoid)(10)}`
|
||||
});
|
||||
return finalization.response;
|
||||
}
|
||||
catch (error) {
|
||||
(0, log_1.logJson)({
|
||||
|
||||
Reference in New Issue
Block a user