wired in project isolation
This commit is contained in:
@@ -1,21 +1,21 @@
|
|||||||
const memory = require('../services/memory');
|
const memory = require("../services/memory");
|
||||||
const inference = require('../services/inference');
|
const inference = require("../services/inference");
|
||||||
const embedding = require('../services/embedding');
|
const embedding = require("../services/embedding");
|
||||||
const qdrant = require('../services/qdrant');
|
const qdrant = require("../services/qdrant");
|
||||||
const { ORCHESTRATION } = require('@nexusai/shared')
|
const { ORCHESTRATION } = require("@nexusai/shared");
|
||||||
|
|
||||||
const { RECENT_EPISODE_LIMIT, SEMANTIC_LIMIT, SCORE_THRESHOLD, SYSTEM_PROMPT } = ORCHESTRATION;
|
const { RECENT_EPISODE_LIMIT, SEMANTIC_LIMIT, SCORE_THRESHOLD, SYSTEM_PROMPT } =
|
||||||
|
ORCHESTRATION;
|
||||||
|
|
||||||
function buildPrompt(recentEpisodes, semanticEpisodes, userMessage) {
|
function buildPrompt(recentEpisodes, semanticEpisodes, userMessage) {
|
||||||
const parts = [SYSTEM_PROMPT];
|
const parts = [SYSTEM_PROMPT];
|
||||||
|
|
||||||
if (semanticEpisodes.length > 0 )
|
if (semanticEpisodes.length > 0) {
|
||||||
{
|
parts.push("Here are some relevant memories from earlier conversations:");
|
||||||
parts.push('Here are some relevant memories from earlier conversations:')
|
|
||||||
for (const ep of semanticEpisodes) {
|
for (const ep of semanticEpisodes) {
|
||||||
parts.push(`User: ${ep.user_message}\nAssistant: ${ep.ai_response}`);
|
parts.push(`User: ${ep.user_message}\nAssistant: ${ep.ai_response}`);
|
||||||
}
|
}
|
||||||
parts.push('---')
|
parts.push("---");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recentEpisodes.length > 0) {
|
if (recentEpisodes.length > 0) {
|
||||||
@@ -23,26 +23,26 @@ function buildPrompt(recentEpisodes, semanticEpisodes, userMessage) {
|
|||||||
for (const ep of recentEpisodes) {
|
for (const ep of recentEpisodes) {
|
||||||
parts.push(`User: ${ep.user_message}\nAssistant: ${ep.ai_response}`);
|
parts.push(`User: ${ep.user_message}\nAssistant: ${ep.ai_response}`);
|
||||||
}
|
}
|
||||||
parts.push('--- End of recent memories ---\n');
|
parts.push("--- End of recent memories ---\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
parts.push(`User: ${userMessage}`);
|
parts.push(`User: ${userMessage}`);
|
||||||
parts.push('Assistant:');
|
parts.push("Assistant:");
|
||||||
|
|
||||||
return parts.join('\n');
|
return parts.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildNamingPrompt(userMessage, aiResponse) {
|
function buildNamingPrompt(userMessage, aiResponse) {
|
||||||
return [
|
return [
|
||||||
'Your task is to generate a short title for a conversation based on its first exchange.',
|
"Your task is to generate a short title for a conversation based on its first exchange.",
|
||||||
'Rules: maximum 5 words, no punctuation, no quotes, plain text only.',
|
"Rules: maximum 5 words, no punctuation, no quotes, plain text only.",
|
||||||
'Examples: "Setting up a Raspberry Pi", "Help with Python list comprehension", "Planning a trip to Japan"',
|
'Examples: "Setting up a Raspberry Pi", "Help with Python list comprehension", "Planning a trip to Japan"',
|
||||||
'',
|
"",
|
||||||
`User: ${userMessage}`,
|
`User: ${userMessage}`,
|
||||||
`Assistant: ${aiResponse}`,
|
`Assistant: ${aiResponse}`,
|
||||||
'',
|
"",
|
||||||
'Title:',
|
"Title:",
|
||||||
].join('\n');
|
].join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function autoNameSession(externalId, userMessage, aiResponse) {
|
async function autoNameSession(externalId, userMessage, aiResponse) {
|
||||||
@@ -52,33 +52,47 @@ async function autoNameSession(externalId, userMessage, aiResponse) {
|
|||||||
maxTokens: 20, // title only needs a handful of tokens
|
maxTokens: 20, // title only needs a handful of tokens
|
||||||
temperature: 0.3, // low temperature for consistent, factual naming
|
temperature: 0.3, // low temperature for consistent, factual naming
|
||||||
});
|
});
|
||||||
const name = result.text?.trim().replace(/^["']|["']$/g, ''); // strip any quotes the model adds
|
const name = result.text?.trim().replace(/^["']|["']$/g, ""); // strip any quotes the model adds
|
||||||
if (name) {
|
if (name) {
|
||||||
await memory.updateSession(externalId, { name });
|
await memory.updateSession(externalId, { name });
|
||||||
console.log(`[orchestration] Auto-named session "${externalId}": "${name}"`);
|
console.log(
|
||||||
|
`[orchestration] Auto-named session "${externalId}": "${name}"`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('[orchestration] Auto-naming failed (non-critical):', err.message);
|
console.warn(
|
||||||
|
"[orchestration] Auto-naming failed (non-critical):",
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getSemanticEpisodes(userMessage, sessionId, recentIds) {
|
async function getSemanticEpisodes(
|
||||||
|
userMessage,
|
||||||
|
sessionId,
|
||||||
|
recentIds,
|
||||||
|
projectSessionIds = null,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const vector = await embedding.embed(userMessage);
|
const vector = await embedding.embed(userMessage);
|
||||||
const results = await qdrant.searchEpisodes(vector, {
|
const results = await qdrant.searchEpisodes(vector, {
|
||||||
limit: SEMANTIC_LIMIT,
|
limit: SEMANTIC_LIMIT,
|
||||||
scoreThreshold: SCORE_THRESHOLD,
|
scoreThreshold: SCORE_THRESHOLD,
|
||||||
sessionId,
|
sessionId: projectSessionIds ? null : sessionId,
|
||||||
|
projectSessionIds,
|
||||||
});
|
});
|
||||||
|
|
||||||
const fetched = await Promise.all(
|
const fetched = await Promise.all(
|
||||||
results
|
results
|
||||||
.filter(r => !recentIds.has(r.id))
|
.filter((r) => !recentIds.has(r.id))
|
||||||
.map(r => memory.getEpisodeById(r.id))
|
.map((r) => memory.getEpisodeById(r.id)),
|
||||||
);
|
);
|
||||||
return fetched.filter(Boolean);
|
return fetched.filter(Boolean);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(`[orchestration] Semantic search failed, continuing without: `, err.message);
|
console.warn(
|
||||||
|
`[orchestration] Semantic search failed, continuing without: `,
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,13 +102,42 @@ async function chat(externalId, userMessage, options = {}) {
|
|||||||
let session = await memory.getSessionByExternalId(externalId);
|
let session = await memory.getSessionByExternalId(externalId);
|
||||||
if (!session) session = await memory.createSession(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?.isolated === 1) {
|
||||||
|
const projectSessions = await memory.getProjectSessions(
|
||||||
|
session.project_id,
|
||||||
|
);
|
||||||
|
projectSessionIds = projectSessions.map((s) => s.id);
|
||||||
|
console.log(
|
||||||
|
`[orchestration] Isolated project ${session.project_id} — restricting search to ${projectSessionIds.length} sessions`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(
|
||||||
|
"[orchestration] Failed to resolve isolation context:",
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Fetch recent episodes for context
|
// 2. Fetch recent episodes for context
|
||||||
const recentEpisodes = await memory.getRecentEpisodes(session.id, RECENT_EPISODE_LIMIT );
|
const recentEpisodes = await memory.getRecentEpisodes(
|
||||||
|
session.id,
|
||||||
|
RECENT_EPISODE_LIMIT,
|
||||||
|
);
|
||||||
const isFirstMessage = recentEpisodes.length === 0;
|
const isFirstMessage = recentEpisodes.length === 0;
|
||||||
const recentIds = new Set(recentEpisodes.map(e => e.id));
|
const recentIds = new Set(recentEpisodes.map((e) => e.id));
|
||||||
|
|
||||||
// 3. Semantic Search
|
// 3. Semantic Search
|
||||||
const semanticEpisodes = await getSemanticEpisodes(userMessage, session.id, recentIds);
|
const semanticEpisodes = await getSemanticEpisodes(
|
||||||
|
userMessage,
|
||||||
|
session.id,
|
||||||
|
recentIds,
|
||||||
|
projectSessionIds
|
||||||
|
);
|
||||||
|
|
||||||
// 4. Assemble prompt
|
// 4. Assemble prompt
|
||||||
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, userMessage);
|
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, userMessage);
|
||||||
@@ -103,17 +146,20 @@ async function chat(externalId, userMessage, options = {}) {
|
|||||||
const result = await inference.complete(prompt, options);
|
const result = await inference.complete(prompt, options);
|
||||||
|
|
||||||
// 6. Write episode back to memory
|
// 6. Write episode back to memory
|
||||||
memory.createEpisode(
|
memory
|
||||||
|
.createEpisode(
|
||||||
session.id,
|
session.id,
|
||||||
userMessage,
|
userMessage,
|
||||||
result.text,
|
result.text,
|
||||||
(result.evalCount || 0) + (result.promptEvalCount || 0 )
|
(result.evalCount || 0) + (result.promptEvalCount || 0),
|
||||||
).catch(err => console.error(`[orchestration] Failed to save episode`, err.message));
|
)
|
||||||
|
.catch((err) =>
|
||||||
|
console.error(`[orchestration] Failed to save episode`, err.message),
|
||||||
|
);
|
||||||
|
|
||||||
// 7. Auto-name on first message
|
// 7. Auto-name on first message
|
||||||
if (isFirstMessage && !session.name) {
|
if (isFirstMessage && !session.name) {
|
||||||
autoNameSession(externalId, userMessage, result.text)
|
autoNameSession(externalId, userMessage, result.text).catch(() => {}); // already logged inside autoNameSession
|
||||||
.catch(() => {}); // already logged inside autoNameSession
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 8. Return response
|
// 8. Return response
|
||||||
@@ -126,39 +172,71 @@ async function chat(externalId, userMessage, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function chatStream(externalId, userMessage, onChunk, options = {}) {
|
async function chatStream(externalId, userMessage, onChunk, options = {}) {
|
||||||
console.log('[orchestration] chatStream called:', { externalId, userMessage: userMessage.slice(0, 50) });
|
console.log("[orchestration] chatStream called:", {
|
||||||
|
externalId,
|
||||||
|
userMessage: userMessage.slice(0, 50),
|
||||||
|
});
|
||||||
let session = await memory.getSessionByExternalId(externalId);
|
let session = await memory.getSessionByExternalId(externalId);
|
||||||
if (!session) session = await memory.createSession(externalId);
|
if (!session) session = await memory.createSession(externalId);
|
||||||
|
|
||||||
const recentEpisodes = await memory.getRecentEpisodes(session.id, RECENT_EPISODE_LIMIT);
|
let projectSessionIds = null;
|
||||||
|
if (session.project_id) {
|
||||||
|
try {
|
||||||
|
const project = await memory.getProject(session.project_id);
|
||||||
|
if (project?.isolated === 1) {
|
||||||
|
const projectSessions = await memory.getProjectSessions(
|
||||||
|
session.project_id,
|
||||||
|
);
|
||||||
|
projectSessionIds = projectSessions.map((s) => s.id);
|
||||||
|
console.log(
|
||||||
|
`[orchestration] Isolated project ${session.project_id} — restricting search to ${projectSessionIds.length} sessions`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(
|
||||||
|
"[orchestration] Failed to resolve isolation context:",
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const recentEpisodes = await memory.getRecentEpisodes(
|
||||||
|
session.id,
|
||||||
|
RECENT_EPISODE_LIMIT,
|
||||||
|
);
|
||||||
const isFirstMessage = recentEpisodes.length === 0;
|
const isFirstMessage = recentEpisodes.length === 0;
|
||||||
const recentIds = new Set(recentEpisodes.map(e => e.id));
|
const recentIds = new Set(recentEpisodes.map((e) => e.id));
|
||||||
const semanticEpisodes = await getSemanticEpisodes(userMessage, session.id, recentIds);
|
const semanticEpisodes = await getSemanticEpisodes(
|
||||||
|
userMessage,
|
||||||
|
session.id,
|
||||||
|
recentIds,
|
||||||
|
projectSessionIds
|
||||||
|
);
|
||||||
|
|
||||||
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, userMessage);
|
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, userMessage);
|
||||||
const res = await inference.completeStream(prompt, options);
|
const res = await inference.completeStream(prompt, options);
|
||||||
|
|
||||||
let fullText = '';
|
let fullText = "";
|
||||||
let model = '';
|
let model = "";
|
||||||
let tokenCount = 0;
|
let tokenCount = 0;
|
||||||
let buffer = '';
|
let buffer = "";
|
||||||
|
|
||||||
for await (const chunk of res.body) {
|
for await (const chunk of res.body) {
|
||||||
buffer += Buffer.from(chunk).toString('utf8');
|
buffer += Buffer.from(chunk).toString("utf8");
|
||||||
|
|
||||||
const events = buffer.split('\n\n');
|
const events = buffer.split("\n\n");
|
||||||
buffer = events.pop() || '';
|
buffer = events.pop() || "";
|
||||||
|
|
||||||
for (const event of events) {
|
for (const event of events) {
|
||||||
const lines = event.split('\n');
|
const lines = event.split("\n");
|
||||||
const dataLines = lines
|
const dataLines = lines
|
||||||
.filter(line => line.startsWith('data: '))
|
.filter((line) => line.startsWith("data: "))
|
||||||
.map(line => line.slice(6));
|
.map((line) => line.slice(6));
|
||||||
|
|
||||||
if (dataLines.length === 0) continue;
|
if (dataLines.length === 0) continue;
|
||||||
|
|
||||||
const raw = dataLines.join('\n').trim();
|
const raw = dataLines.join("\n").trim();
|
||||||
if (raw === '[DONE]') continue;
|
if (raw === "[DONE]") continue;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(raw);
|
const data = JSON.parse(raw);
|
||||||
@@ -177,17 +255,23 @@ async function chatStream(externalId, userMessage, onChunk, options = {}) {
|
|||||||
throw new Error(data.error);
|
throw new Error(data.error);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[orchestration] Failed to parse inference SSE event:', raw, err.message);
|
console.error(
|
||||||
|
"[orchestration] Failed to parse inference SSE event:",
|
||||||
|
raw,
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[orchestration] final streamed text length:', fullText.length);
|
console.log("[orchestration] final streamed text length:", fullText.length);
|
||||||
|
|
||||||
if (fullText.trim()) {
|
if (fullText.trim()) {
|
||||||
await memory.createEpisode(session.id, userMessage, fullText, tokenCount);
|
await memory.createEpisode(session.id, userMessage, fullText, tokenCount);
|
||||||
} else {
|
} else {
|
||||||
console.warn('[orchestration] Stream finished with no assistant text; episode not saved');
|
console.warn(
|
||||||
|
"[orchestration] Stream finished with no assistant text; episode not saved",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFirstMessage && !session.name) {
|
if (isFirstMessage && !session.name) {
|
||||||
|
|||||||
@@ -115,6 +115,24 @@ async function deleteProject(id) {
|
|||||||
if (!res.ok) throw new Error(`Failed to delete project: ${res.status}`);
|
if (!res.ok) throw new Error(`Failed to delete project: ${res.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getProjectSessions(projectId) {
|
||||||
|
const url = new URL(`${BASE_URL}/sessions`);
|
||||||
|
url.searchParams.set('limit', 200); // generous upper bound
|
||||||
|
url.searchParams.set('offset', 0);
|
||||||
|
url.searchParams.set('projectId', projectId);
|
||||||
|
|
||||||
|
const res = await fetch(url.toString());
|
||||||
|
if (!res.ok) throw new Error(`Failed to fetch project sessions: ${res.status}`);
|
||||||
|
return res.json(); // returns array of session objects
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getProject(id) {
|
||||||
|
const res = await fetch(`${BASE_URL}/projects/${id}`);
|
||||||
|
if (res.status === 404) return null;
|
||||||
|
if (!res.ok) throw new Error(`Failed to fetch project: ${res.status}`);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getSessionByExternalId,
|
getSessionByExternalId,
|
||||||
createSession,
|
createSession,
|
||||||
@@ -129,4 +147,6 @@ module.exports = {
|
|||||||
getProjects,
|
getProjects,
|
||||||
updateProject,
|
updateProject,
|
||||||
deleteProject,
|
deleteProject,
|
||||||
|
getProjectSessions,
|
||||||
|
getProject,
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,16 @@ const {getEnv, QDRANT, COLLECTIONS, ORCHESTRATION } = require('@nexusai/shared')
|
|||||||
|
|
||||||
const BASE_URL = getEnv('QDRANT_URL', QDRANT.DEFAULT_URL);
|
const BASE_URL = getEnv('QDRANT_URL', QDRANT.DEFAULT_URL);
|
||||||
|
|
||||||
async function searchEpisodes( vector, {limit = ORCHESTRATION.RECENT_EPISODE_LIMIT, scoreThreshold = ORCHESTRATION.SCORE_THRESHOLD, sessionId } = {}) {
|
async function searchEpisodes( vector, {limit = ORCHESTRATION.RECENT_EPISODE_LIMIT, scoreThreshold = ORCHESTRATION.SCORE_THRESHOLD, sessionId, projectSessionIds } = {}) {
|
||||||
const body = {vector, limit, score_threshold: scoreThreshold, with_payload: true};
|
const body = {vector, limit, score_threshold: scoreThreshold, with_payload: true};
|
||||||
|
|
||||||
if (sessionId) {
|
if(projectSessionIds) {
|
||||||
|
body.filter = {
|
||||||
|
should: projectSessionIds.map(id => ({
|
||||||
|
key: 'sessionId', match: { value: id }
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
} else if (sessionId) {
|
||||||
body.filter = { must: [{key: 'sessionId', match: {value: sessionId} }] };
|
body.filter = { must: [{key: 'sessionId', match: {value: sessionId} }] };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user