code cleanup/hardening

This commit is contained in:
Storme-bit
2026-04-26 21:53:33 -07:00
parent 43fa12899c
commit 4f3b18de08
7 changed files with 30 additions and 13 deletions

View File

@@ -5,9 +5,9 @@ const {getEnv, OLLAMA, PORTS} = require('@nexusai/shared');
const app = express();
app.use(express.json());
const PORT = getEnv('PORT', PORTS.EMBEDDING); // Default to 3003 if PORT is not set
const OLLAMA_URL = getEnv('OLLAMA_URL', OLLAMA.DEFAULT_URL); // URL for Ollama API
const EMBED_MODEL = getEnv('EMBEDDING_MODEL', OLLAMA.EMBED_MODEL); // Ollama model for embeddings
const PORT = getEnv('PORT', PORTS.EMBEDDING);
const OLLAMA_URL = getEnv('OLLAMA_URL', OLLAMA.DEFAULT_URL);
const EMBED_MODEL = getEnv('EMBEDDING_MODEL', OLLAMA.EMBED_MODEL);
//OLLAMA embedding helper function
async function embedText(text) {

View File

@@ -1,5 +1,5 @@
const {getDB} = require('../db');
const { EPISODIC, getEnv, SERVICES, parseRow, formatEpisodeText } = require('@nexusai/shared');
const { EPISODIC, getEnv, SERVICES, parseRow, formatEpisodeText, SUMMARIES } = require('@nexusai/shared');
const semantic = require('../semantic');
const { extractAndStoreEntities } = require('../entities/extraction')
@@ -207,7 +207,7 @@ async function getEpisodeEmbedding(userMessage, aiResponse){
return data.embedding;
}
function getEpisodesByProject(projectId, limit = 200) {
function getEpisodesByProject(projectId, limit = SUMMARIES.MAX_PROJECT_EPISODE_LIMIT) {
const db = getDB();
return db.prepare(`
SELECT e.* FROM episodes e

View File

@@ -1,4 +1,4 @@
const { SERVICES, getEnv } = require('@nexusai/shared');
const { SERVICES, getEnv, SUMMARIES } = require('@nexusai/shared');
const {
getSessionSummariesForProject,
getProjectOverviewSummary,
@@ -12,7 +12,7 @@ const {
const EXTRACTION_URL = getEnv('EXTRACTION_URL', 'http://localhost:11434');
const EXTRACTION_MODEL = getEnv('EXTRACTION_MODEL', 'qwen2.5:3b');
const MAX_SUMMARY_CHARS = 8000; // generous ceiling before we truncate input
const MAX_SUMMARY_CHARS = SUMMARIES.MAX_SUMMARY_CHARS; // generous ceiling before we truncate input
function buildProjectSummaryPrompt(projectName, sessionSummaries) {
let summaryBlock = sessionSummaries

View File

@@ -1,6 +1,6 @@
require ('dotenv').config();
const express = require('express');
const {getEnv, PORTS, SERVICES, ORCHESTRATION} = require('@nexusai/shared');
const {getEnv, PORTS, SERVICES, ORCHESTRATION, logger} = require('@nexusai/shared');
/**** ROUTERS *** */
const chatRouter = require('./routes/chat');
@@ -53,5 +53,5 @@ app.use('/summaries', summariesRouter)
/******* Start the server ************/
app.listen(PORT, () => {
console.log(`Orchestration Service is running on port ${PORT}`);
logger.info(`Orchestration Service is running on port ${PORT}`);
});

View File

@@ -26,7 +26,7 @@ const ORCHESTRATION = {
SEMANTIC_LIMIT: 5,
SCORE_THRESHOLD: 0.5,
ENTITIES_LIMIT: 5,
ENTITIES_THRESHOLD: 0.6,
ENTITIES_THRESHOLD: 0.55,
TEMPERATURE: 0.7,
CORS_ORIGIN: 'http://localhost:5173',
SYSTEM_PROMPT: `You are a helpful, context-aware AI assistant. You have access to memories of past conversations with the user. Use them to provide consistent, personalised responses.`
@@ -70,14 +70,17 @@ const SQLITE = {
}
const SUMMARIES = {
THRESHOLD_TOKENS: 200, //trigger summary when session hits this many tokens
THRESHOLD_TOKENS: 200, //trigger summary when session hits this many tokens
MAX_SUMMARY_TOKENS: 800, //if existing summary exceeds this, create new instead of update
MIN_EPISODES_SINCE: 5, // don't resummarize until N new episodes since last summary
MAX_SUMMARY_CHARS: 8000, // max chars to include from recent episodes when generating summary (to control prompt size)
MAX_PROJECT_EPISODE_LIMIT: 200, // max number of episodes to consider from the entire project when generating summary (to control prompt size)
}
const ENTITIES = {
TEMPERATURE: 0.1,
NUM_PREDICT: 1024,
TEMPERATURE: 0.1, // Low temperature, more precise extraction, less creative
NUM_PREDICT: 1024, // Max tokens to consider for entity extraction (e.g. recent conversation)
THRESHOLD: 0.55, // Minimum confidence score for an extracted entity to be included in the results
}
module.exports = {

View File

@@ -1,6 +1,7 @@
const {getEnv} = require('./config/env');
const {QDRANT, COLLECTIONS, EPISODIC, SERVICES, OLLAMA, PORTS, LLAMACPP, INFERENCE_DEFAULTS, SQLITE, ORCHESTRATION, SUMMARIES, ENTITIES } = require('./config/constants');
const {parseRow, formatEpisodeText} = require('./utils')
const {logger} = require('./utils/logger');
module.exports = {
getEnv,
@@ -18,4 +19,5 @@ module.exports = {
formatEpisodeText,
SUMMARIES,
ENTITIES,
logger,
};

View File

@@ -0,0 +1,12 @@
const LEVELS = { error: 0, warn: 1, info: 2, debug: 3 };
const current = LEVELS[process.env.LOG_LEVEL?.toLowerCase()] ?? LEVELS.info;
const logger = {
error: (...args) => current >= LEVELS.error && console.error('[ERROR]', ...args),
warn: (...args) => current >= LEVELS.warn && console.warn( '[WARN]', ...args),
info: (...args) => current >= LEVELS.info && console.log( '[INFO]', ...args),
debug: (...args) => current >= LEVELS.debug && console.log( '[DEBUG]', ...args),
};
module.exports = logger;