NODEDC_1C/llm_normalizer/backend/dist/services/assistantSessionStore.js

85 lines
3.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssistantSessionStore = void 0;
const nanoid_1 = require("nanoid");
const config_1 = require("../config");
const investigationState_1 = require("./investigationState");
const MAX_ITEMS_PER_SESSION = 200;
function cloneItem(item) {
return {
...item,
debug: item.debug ? { ...item.debug } : null
};
}
function cloneSession(state) {
return {
session_id: state.session_id,
updated_at: state.updated_at,
items: state.items.map(cloneItem),
investigation_state: (0, investigationState_1.cloneInvestigationState)(state.investigation_state)
};
}
function normalizeSessionShape(state) {
const legacy = state;
const normalizedItems = Array.isArray(legacy.items) ? legacy.items : [];
const investigationState = config_1.FEATURE_ASSISTANT_INVESTIGATION_STATE_V1
? legacy.investigation_state ?? (0, investigationState_1.createEmptyInvestigationState)(state.session_id)
: legacy.investigation_state ?? null;
state.items = normalizedItems;
state.updated_at = typeof legacy.updated_at === "string" && legacy.updated_at.trim() ? legacy.updated_at : new Date().toISOString();
state.investigation_state = investigationState;
return state;
}
class AssistantSessionStore {
sessions = new Map();
ensureSession(sessionId) {
const resolvedId = (sessionId ?? "").trim() || `asst-${(0, nanoid_1.nanoid)(10)}`;
const existing = this.sessions.get(resolvedId);
if (existing) {
return cloneSession(normalizeSessionShape(existing));
}
const created = {
session_id: resolvedId,
updated_at: new Date().toISOString(),
items: [],
investigation_state: config_1.FEATURE_ASSISTANT_INVESTIGATION_STATE_V1 ? (0, investigationState_1.createEmptyInvestigationState)(resolvedId) : null
};
this.sessions.set(resolvedId, created);
return cloneSession(created);
}
appendItem(sessionId, item) {
const session = this.ensureMutableSession(sessionId);
session.items.push(item);
if (session.items.length > MAX_ITEMS_PER_SESSION) {
session.items = session.items.slice(session.items.length - MAX_ITEMS_PER_SESSION);
}
session.updated_at = new Date().toISOString();
return cloneItem(item);
}
getSession(sessionId) {
const found = this.sessions.get(sessionId);
return found ? cloneSession(normalizeSessionShape(found)) : null;
}
setInvestigationState(sessionId, state) {
const session = this.ensureMutableSession(sessionId);
session.investigation_state = (0, investigationState_1.cloneInvestigationState)(state);
session.updated_at = new Date().toISOString();
return (0, investigationState_1.cloneInvestigationState)(session.investigation_state);
}
ensureMutableSession(sessionId) {
const existing = this.sessions.get(sessionId);
if (existing) {
return normalizeSessionShape(existing);
}
const created = {
session_id: sessionId,
updated_at: new Date().toISOString(),
items: [],
investigation_state: config_1.FEATURE_ASSISTANT_INVESTIGATION_STATE_V1 ? (0, investigationState_1.createEmptyInvestigationState)(sessionId) : null
};
this.sessions.set(sessionId, created);
return created;
}
}
exports.AssistantSessionStore = AssistantSessionStore;