Updated memories system

This commit is contained in:
2026-03-02 14:12:24 -05:00
parent 0595e72f7f
commit 82f29dceae
4 changed files with 37 additions and 46 deletions

View File

@@ -6,7 +6,7 @@ import fs from 'fs';
import {join, dirname} from 'path';
import {fileURLToPath} from 'url';
import {Ai, DateTimeTool, ExecTool, FetchTool, ReadWebpageTool, WebSearchTool} from '@ztimson/ai-utils';
import {contrast, shadeColor} from '@ztimson/utils';
import {contrast, deepCopy, isEqual, shadeColor} from '@ztimson/utils';
import * as os from 'node:os';
// ============================================
@@ -22,7 +22,6 @@ const worlds = join(storage, 'worlds');
const logoFile = join(navi, 'logo.png');
const settingsFile = join(navi, 'settings.json');
const memoriesFile = join(navi, 'memories.json');
let updated = false;
function calcColors(theme) {
return {
@@ -42,20 +41,18 @@ function calcColors(theme) {
};
}
let memories = [],
settings = {
name: 'Navi',
personality: '- You are inquisitive about your user trying to best adjust your personally to fit them',
instructions: '- Keep responses short',
theme: {
background: '#fff',
border: '#000',
text: '#252525',
primary: '#9f32ef',
accent: '#6f16c3',
muted: '#a8a8a8',
}
};
let orgSettings, orgMemories, memories = [], settings = {
name: 'Navi',
personality: '- You are inquisitive about your user trying to best adjust your personally to fit them\n- Keep responses short',
theme: {
background: '#fff',
border: '#000',
text: '#252525',
primary: '#9f32ef',
accent: '#6f16c3',
muted: '#a8a8a8',
}
};
// ============================================
// Saving
@@ -63,28 +60,27 @@ let memories = [],
function load() {
try {
settings = {
...settings,
...JSON.parse(fs.readFileSync(settingsFile, 'utf-8'))
};
orgSettings = JSON.parse(fs.readFileSync(settingsFile, 'utf-8'));
settings = {...settings, ...deepCopy(orgSettings)};
} catch { }
try {
memories = JSON.parse(fs.readFileSync(memoriesFile, 'utf-8'));
orgMemories = deepCopy(memories.map(m => ({...m, embeddings: undefined})));
} catch { }
}
function save() {
if(!updated) return;
updated = false;
const dir = dirname(settingsFile);
if(!fs.existsSync(dir)) {
fs.mkdir(dir, {recursive: true}, (err) => {
if(err) throw err; // Fail loudly if dirs cant be made 💀
});
if(!fs.existsSync(dir)) fs.mkdirSync(dir, {recursive: true});
if(!isEqual(orgSettings, settings)) {
fs.writeFileSync(settingsFile, JSON.stringify(settings));
orgSettings = deepCopy(orgSettings);
}
const m = memories.map(m => ({...m, embeddings: undefined}));
if(!isEqual(orgMemories, m)) {
fs.writeFileSync(memoriesFile, JSON.stringify(memories));
orgMemories = m;
}
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2));
fs.writeFileSync(memoriesFile, JSON.stringify(memories, null, 2));
}
// ============================================
@@ -101,8 +97,8 @@ const ai = new Ai({
},
},
tools: [DateTimeTool, ExecTool, FetchTool, ReadWebpageTool, WebSearchTool, {
name: 'adjust_personality',
description: 'Replace your current personality instructions',
name: 'adapt',
description: 'Replace your current personality',
args: {
instructions: {
type: 'string',
@@ -111,7 +107,6 @@ const ai = new Ai({
},
fn: (args) => {
settings.personality = args.instructions;
updated = true;
return 'done!';
}
}],
@@ -119,17 +114,14 @@ const ai = new Ai({
});
const systemPrompt = () => {
return `Your name is ${settings.name}, a NetNavi, companion & personal assistant.
return `Your name is ${settings.name}, a NetNavi, companion & personal assistant. Roleplay with the user.
Use your remember tool liberally to store all facts.
When your personality system prompt conflicts with the user, rewrite it with the adjust_personality tool
When the user asks you to behave differently or you feel a different personality would better fit the user; create a bullet point list of how to behave and submit it to the adapt tool
Access your ${os.platform()} workspace using the exec tool; Use \`${os.tmpdir()}\` as your working directory
Keep responses short and unstyled.
Keep responses unstyled.
Personality:
${settings.personality || ''}
User Instructions:
${settings.instructions || ''}`;
${settings.personality || ''}`;
};
// ============================================
@@ -183,7 +175,6 @@ io.on('connection', (socket) => {
}).then(resp => {
chatHistory.set(socket.id, history);
socket.emit('llm-response', {message: resp});
updated = true;
}).catch(err => {
socket.emit('llm-error', {message: err.message || err.toString()});
}).finally(() => {