memory settings implementation

This commit is contained in:
Storme-bit
2026-04-17 23:13:36 -07:00
parent 1cc7b62d79
commit 77275cf476
7 changed files with 254 additions and 206 deletions

View File

@@ -3,20 +3,21 @@ const inference = require("../services/inference");
const embedding = require("../services/embedding");
const qdrant = require("../services/qdrant");
const { ORCHESTRATION } = require("@nexusai/shared");
const { RECENT_EPISODE_LIMIT, SEMANTIC_LIMIT, SCORE_THRESHOLD, SYSTEM_PROMPT } =
ORCHESTRATION;
const appSettings = require("../config/settings");
const { SYSTEM_PROMPT } = ORCHESTRATION;
function buildPrompt(recentEpisodes, semanticEpisodes, entities, userMessage) {
const parts = [SYSTEM_PROMPT];
if (entities.length > 0) {
parts.push('Here is what you know about entities relevant to this conversation:');
for (const e of entities) {
parts.push(`- ${e.name} (${e.type}): ${e.notes}`);
}
parts.push('---');
parts.push(
"Here is what you know about entities relevant to this conversation:",
);
for (const e of entities) {
parts.push(`- ${e.name} (${e.type}): ${e.notes}`);
}
parts.push("---");
}
if (semanticEpisodes.length > 0) {
parts.push("Here are some relevant memories from earlier conversations:");
@@ -80,12 +81,13 @@ async function getSemanticEpisodes(
sessionId,
recentIds,
projectSessionIds = null,
{ semanticLimit, scoreThreshold } = {},
) {
try {
const vector = await embedding.embed(userMessage);
const results = await qdrant.searchEpisodes(vector, {
limit: SEMANTIC_LIMIT,
scoreThreshold: SCORE_THRESHOLD,
limit: semanticLimit,
scoreThreshold: scoreThreshold,
sessionId: projectSessionIds ? null : sessionId,
projectSessionIds,
});
@@ -106,45 +108,60 @@ async function getSemanticEpisodes(
}
async function getRelevantEntities(userMessage) {
try {
const vector = await embedding.embed(userMessage);
const results = await qdrant.searchEntities(vector);
console.log('[orchestration] Entity search results:',
results.map(r => ({ name: r.payload?.name, score: r.score }))
);
return results.map(r => r.payload).filter(Boolean);
} catch (err) {
console.warn('[orchestration] Entity search failed, continuing without:', err.message);
return [];
}
try {
const vector = await embedding.embed(userMessage);
const results = await qdrant.searchEntities(vector);
console.log(
"[orchestration] Entity search results:",
results.map((r) => ({ name: r.payload?.name, score: r.score })),
);
return results.map((r) => r.payload).filter(Boolean);
} catch (err) {
console.warn(
"[orchestration] Entity search failed, continuing without:",
err.message,
);
return [];
}
}
async function chat(externalId, userMessage, options = {}) {
const { recentEpisodeLimit, semanticLimit, scoreThreshold } =
appSettings.load();
// 1. Resolve or create session
let session = await memory.getSessionByExternalId(externalId);
if (!session) session = await memory.createSession(externalId);
let projectSessionIds = null;
if (session.project_id) {
try {
const project = await memory.getProject(session.project_id);
if (project) {
const projectSessions = await memory.getProjectSessions(session.project_id);
projectSessionIds = projectSessions.map(s => s.id);
if (project.isolated === 1) {
console.log(`[orchestration] Isolated project — restricting to ${projectSessionIds.length} sessions`);
} else {
console.log(`[orchestration] Non-isolated project — expanding search to ${projectSessionIds.length} sessions`);
let projectSessionIds = null;
if (session.project_id) {
try {
const project = await memory.getProject(session.project_id);
if (project) {
const projectSessions = await memory.getProjectSessions(
session.project_id,
);
projectSessionIds = projectSessions.map((s) => s.id);
if (project.isolated === 1) {
console.log(
`[orchestration] Isolated project — restricting to ${projectSessionIds.length} sessions`,
);
} else {
console.log(
`[orchestration] Non-isolated project — expanding search to ${projectSessionIds.length} sessions`,
);
}
}
} catch (err) {
console.warn(
"[orchestration] Failed to resolve project context:",
err.message,
);
}
} catch (err) {
console.warn('[orchestration] Failed to resolve project context:', err.message);
}
}
// 2. Fetch recent episodes for context
const recentEpisodes = await memory.getRecentEpisodes(
session.id,
RECENT_EPISODE_LIMIT,
recentEpisodeLimit,
);
const isFirstMessage = recentEpisodes.length === 0;
const recentIds = new Set(recentEpisodes.map((e) => e.id));
@@ -154,14 +171,20 @@ if (session.project_id) {
userMessage,
session.id,
recentIds,
projectSessionIds
projectSessionIds,
{ semanticLimit, scoreThreshold },
);
// 3b. Entity Search
const entities = await getRelevantEntities(userMessage)
const entities = await getRelevantEntities(userMessage);
// 4. Assemble prompt
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, entities, userMessage);
const prompt = buildPrompt(
recentEpisodes,
semanticEpisodes,
entities,
userMessage,
);
// 5. Run inference
const result = await inference.complete(prompt, options);
@@ -193,118 +216,131 @@ if (session.project_id) {
}
async function chatStream(externalId, userMessage, onChunk, options = {}) {
console.log('[orchestration] chatStream entry');
try {
const { recentEpisodeLimit, semanticLimit, scoreThreshold } = appSettings.load();
let session = await memory.getSessionByExternalId(externalId);
if (!session) session = await memory.createSession(externalId);
console.log("[orchestration] chatStream called:", {
externalId,
userMessage: userMessage.slice(0, 50),
});
let session = await memory.getSessionByExternalId(externalId);
if (!session) session = await memory.createSession(externalId);
let projectSessionIds = null;
if (session.project_id) {
try {
const project = await memory.getProject(session.project_id);
if (project) {
const projectSessions = await memory.getProjectSessions(session.project_id);
projectSessionIds = projectSessions.map(s => s.id);
if (project.isolated === 1) {
console.log(`[orchestration] Isolated project — restricting to ${projectSessionIds.length} sessions`);
} else {
console.log(`[orchestration] Non-isolated project — expanding search to ${projectSessionIds.length} sessions`);
}
}
} catch (err) {
console.warn('[orchestration] Failed to resolve project context:', err.message);
}
}
const recentEpisodes = await memory.getRecentEpisodes(
session.id,
RECENT_EPISODE_LIMIT,
);
const isFirstMessage = recentEpisodes.length === 0;
const recentIds = new Set(recentEpisodes.map((e) => e.id));
const semanticEpisodes = await getSemanticEpisodes(
userMessage,
session.id,
recentIds,
projectSessionIds
);
const entities = await getRelevantEntities(userMessage);
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, entities, userMessage);
const res = await inference.completeStream(prompt, options);
let fullText = "";
let model = "";
let tokenCount = 0;
let buffer = "";
for await (const chunk of res.body) {
buffer += Buffer.from(chunk).toString("utf8");
const events = buffer.split("\n\n");
buffer = events.pop() || "";
for (const event of events) {
const lines = event.split("\n");
const dataLines = lines
.filter((line) => line.startsWith("data: "))
.map((line) => line.slice(6));
if (dataLines.length === 0) continue;
const raw = dataLines.join("\n").trim();
if (raw === "[DONE]") continue;
let projectSessionIds = null;
if (session.project_id) {
try {
const data = JSON.parse(raw);
if (data.response) {
fullText += data.response;
onChunk(data.response);
}
if (data.model) model = data.model;
if (data.done && data.tokenCount !== undefined) {
tokenCount = data.tokenCount;
}
if (data.error) {
throw new Error(data.error);
const project = await memory.getProject(session.project_id);
if (project) {
const projectSessions = await memory.getProjectSessions(
session.project_id,
);
projectSessionIds = projectSessions.map((s) => s.id);
if (project.isolated === 1) {
console.log(
`[orchestration] Isolated project — restricting to ${projectSessionIds.length} sessions`,
);
} else {
console.log(
`[orchestration] Non-isolated project — expanding search to ${projectSessionIds.length} sessions`,
);
}
}
} catch (err) {
console.error(
"[orchestration] Failed to parse inference SSE event:",
raw,
console.warn(
"[orchestration] Failed to resolve project context:",
err.message,
);
}
}
}
console.log("[orchestration] final streamed text length:", fullText.length);
if (fullText.trim()) {
await memory.createEpisode(session.id, userMessage, fullText, tokenCount);
} else {
console.warn(
"[orchestration] Stream finished with no assistant text; episode not saved",
const recentEpisodes = await memory.getRecentEpisodes(
session.id,
recentEpisodeLimit,
);
const isFirstMessage = recentEpisodes.length === 0;
const recentIds = new Set(recentEpisodes.map((e) => e.id));
const semanticEpisodes = await getSemanticEpisodes(
userMessage,
session.id,
recentIds,
projectSessionIds,
{semanticLimit, scoreThreshold }
);
}
if (isFirstMessage && !session.name) {
autoNameSession(externalId, userMessage, fullText).catch(() => {});
}
const entities = await getRelevantEntities(userMessage);
return { model, tokenCount };
} catch (err) {
console.error('[orchestration] chatStream fatal error:', err.message, err.stack);
const prompt = buildPrompt(
recentEpisodes,
semanticEpisodes,
entities,
userMessage,
);
const res = await inference.completeStream(prompt, options);
let fullText = "";
let model = "";
let tokenCount = 0;
let buffer = "";
for await (const chunk of res.body) {
buffer += Buffer.from(chunk).toString("utf8");
const events = buffer.split("\n\n");
buffer = events.pop() || "";
for (const event of events) {
const lines = event.split("\n");
const dataLines = lines
.filter((line) => line.startsWith("data: "))
.map((line) => line.slice(6));
if (dataLines.length === 0) continue;
const raw = dataLines.join("\n").trim();
if (raw === "[DONE]") continue;
try {
const data = JSON.parse(raw);
if (data.response) {
fullText += data.response;
onChunk(data.response);
}
if (data.model) model = data.model;
if (data.done && data.tokenCount !== undefined) {
tokenCount = data.tokenCount;
}
if (data.error) {
throw new Error(data.error);
}
} catch (err) {
console.error(
"[orchestration] Failed to parse inference SSE event:",
raw,
err.message,
);
}
}
}
console.log("[orchestration] final streamed text length:", fullText.length);
if (fullText.trim()) {
await memory.createEpisode(session.id, userMessage, fullText, tokenCount);
} else {
console.warn(
"[orchestration] Stream finished with no assistant text; episode not saved",
);
}
if (isFirstMessage && !session.name) {
autoNameSession(externalId, userMessage, fullText).catch(() => {});
}
return { model, tokenCount };
} catch (err) {
console.error(
"[orchestration] chatStream fatal error:",
err.message,
err.stack,
);
throw err;
}
}

View File

@@ -0,0 +1,30 @@
const fs = require('fs');
const path = require('path');
const { ORCHESTRATION } = require('@nexusai/shared');
const SETTINGS_PATH = path.join(__dirname, '../../data/settings.json');
const DEFAULTS = {
recentEpisodeLimit: ORCHESTRATION.RECENT_EPISODE_LIMIT,
semanticLimit: ORCHESTRATION.SEMANTIC_LIMIT,
scoreThreshold: ORCHESTRATION.SCORE_THRESHOLD,
};
function load() {
try {
const raw = fs.readFileSync(SETTINGS_PATH, 'utf8');
return { ...DEFAULTS, ...JSON.parse(raw) };
} catch {
return { ...DEFAULTS }; // file doesn't exist yet — use defaults
}
}
function save(updates) {
const current = load();
const next = { ...current, ...updates };
fs.mkdirSync(path.dirname(SETTINGS_PATH), { recursive: true });
fs.writeFileSync(SETTINGS_PATH, JSON.stringify(next, null, 2));
return next;
}
module.exports = { load, save, DEFAULTS };

View File

@@ -8,6 +8,7 @@ const sessionsRouter = require('./routes/sessions');
const modelsRouter = require('./routes/models');
const projectsRouter = require('./routes/projects');
const episodesRouter = require('./routes/episodes')
const settingsRouter = require('./routes/settings')
const cors = require('cors');
@@ -43,7 +44,8 @@ app.use('/chat', chatRouter);
app.use('/sessions', sessionsRouter);
app.use('/models', modelsRouter);
app.use('/projects', projectsRouter);
app.use('/episodes', episodesRouter)
app.use('/episodes', episodesRouter);
app.use('/settings', settingsRouter);
/******* Start the server ************/
app.listen(PORT, () => {

View File

@@ -0,0 +1,38 @@
const { Router } = require('express');
const settings = require('../config/settings');
const router = Router();
router.get('/', (req, res) => {
res.json(settings.load());
});
router.patch('/', (req, res) => {
const { recentEpisodeLimit, semanticLimit, scoreThreshold } = req.body;
const updates = {};
if (recentEpisodeLimit !== undefined) {
const val = Number(recentEpisodeLimit);
if (!Number.isInteger(val) || val < 1 || val > 20)
return res.status(400).json({ error: 'recentEpisodeLimit must be 120' });
updates.recentEpisodeLimit = val;
}
if (semanticLimit !== undefined) {
const val = Number(semanticLimit);
if (!Number.isInteger(val) || val < 1 || val > 20)
return res.status(400).json({ error: 'semanticLimit must be 120' });
updates.semanticLimit = val;
}
if (scoreThreshold !== undefined) {
const val = Number(scoreThreshold);
if (isNaN(val) || val < 0 || val > 1)
return res.status(400).json({ error: 'scoreThreshold must be 01' });
updates.scoreThreshold = val;
}
res.json(settings.save(updates));
});
module.exports = router;