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

8
package-lock.json generated
View File

@@ -8,7 +8,7 @@
"name": "@ztimson/net-navi", "name": "@ztimson/net-navi",
"version": "1.0.0", "version": "1.0.0",
"dependencies": { "dependencies": {
"@ztimson/ai-utils": "^0.8.7", "@ztimson/ai-utils": "^0.8.8",
"@ztimson/utils": "^0.28.14", "@ztimson/utils": "^0.28.14",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
@@ -303,9 +303,9 @@
} }
}, },
"node_modules/@ztimson/ai-utils": { "node_modules/@ztimson/ai-utils": {
"version": "0.8.7", "version": "0.8.8",
"resolved": "https://registry.npmjs.org/@ztimson/ai-utils/-/ai-utils-0.8.7.tgz", "resolved": "https://registry.npmjs.org/@ztimson/ai-utils/-/ai-utils-0.8.8.tgz",
"integrity": "sha512-CVn7ku5eW41GuYOyh8DgqnRnceNYWboqWnfcNyNjq82/5NdeW6hu264zRDhSv2h2J57308AX7gZdM44GzOBEnw==", "integrity": "sha512-XzkKYM/oNxS7D373yeTG8VKdO9cOYU1+PK/ZM8Sz/v8w2hIO+Lg/VWO/524yZ6oMl/mjDwSIjqv3CxGVpJ6wRw==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@anthropic-ai/sdk": "^0.78.0", "@anthropic-ai/sdk": "^0.78.0",

View File

@@ -3,7 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@ztimson/ai-utils": "^0.8.7", "@ztimson/ai-utils": "^0.8.8",
"@ztimson/utils": "^0.28.14", "@ztimson/utils": "^0.28.14",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",

View File

@@ -1,7 +1,7 @@
import './btn.mjs'; import './btn.mjs';
class LlmComponent extends HTMLElement { class LlmComponent extends HTMLElement {
hideTools = ['adjust_personality', 'recall', 'remember'] hideTools = []//['adapt', 'recall', 'remember']
get isOpen() { return this.isDialogueOpen; }; get isOpen() { return this.isDialogueOpen; };

View File

@@ -6,7 +6,7 @@ import fs from 'fs';
import {join, dirname} from 'path'; import {join, dirname} from 'path';
import {fileURLToPath} from 'url'; import {fileURLToPath} from 'url';
import {Ai, DateTimeTool, ExecTool, FetchTool, ReadWebpageTool, WebSearchTool} from '@ztimson/ai-utils'; 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'; import * as os from 'node:os';
// ============================================ // ============================================
@@ -22,7 +22,6 @@ const worlds = join(storage, 'worlds');
const logoFile = join(navi, 'logo.png'); const logoFile = join(navi, 'logo.png');
const settingsFile = join(navi, 'settings.json'); const settingsFile = join(navi, 'settings.json');
const memoriesFile = join(navi, 'memories.json'); const memoriesFile = join(navi, 'memories.json');
let updated = false;
function calcColors(theme) { function calcColors(theme) {
return { return {
@@ -42,11 +41,9 @@ function calcColors(theme) {
}; };
} }
let memories = [], let orgSettings, orgMemories, memories = [], settings = {
settings = {
name: 'Navi', name: 'Navi',
personality: '- You are inquisitive about your user trying to best adjust your personally to fit them', personality: '- You are inquisitive about your user trying to best adjust your personally to fit them\n- Keep responses short',
instructions: '- Keep responses short',
theme: { theme: {
background: '#fff', background: '#fff',
border: '#000', border: '#000',
@@ -63,28 +60,27 @@ let memories = [],
function load() { function load() {
try { try {
settings = { orgSettings = JSON.parse(fs.readFileSync(settingsFile, 'utf-8'));
...settings, settings = {...settings, ...deepCopy(orgSettings)};
...JSON.parse(fs.readFileSync(settingsFile, 'utf-8'))
};
} catch { } } catch { }
try { try {
memories = JSON.parse(fs.readFileSync(memoriesFile, 'utf-8')); memories = JSON.parse(fs.readFileSync(memoriesFile, 'utf-8'));
orgMemories = deepCopy(memories.map(m => ({...m, embeddings: undefined})));
} catch { } } catch { }
} }
function save() { function save() {
if(!updated) return;
updated = false;
const dir = dirname(settingsFile); const dir = dirname(settingsFile);
if(!fs.existsSync(dir)) { if(!fs.existsSync(dir)) fs.mkdirSync(dir, {recursive: true});
fs.mkdir(dir, {recursive: true}, (err) => { if(!isEqual(orgSettings, settings)) {
if(err) throw err; // Fail loudly if dirs cant be made 💀 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, { tools: [DateTimeTool, ExecTool, FetchTool, ReadWebpageTool, WebSearchTool, {
name: 'adjust_personality', name: 'adapt',
description: 'Replace your current personality instructions', description: 'Replace your current personality',
args: { args: {
instructions: { instructions: {
type: 'string', type: 'string',
@@ -111,7 +107,6 @@ const ai = new Ai({
}, },
fn: (args) => { fn: (args) => {
settings.personality = args.instructions; settings.personality = args.instructions;
updated = true;
return 'done!'; return 'done!';
} }
}], }],
@@ -119,17 +114,14 @@ const ai = new Ai({
}); });
const systemPrompt = () => { 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. 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 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: Personality:
${settings.personality || ''} ${settings.personality || ''}`;
User Instructions:
${settings.instructions || ''}`;
}; };
// ============================================ // ============================================
@@ -183,7 +175,6 @@ io.on('connection', (socket) => {
}).then(resp => { }).then(resp => {
chatHistory.set(socket.id, history); chatHistory.set(socket.id, history);
socket.emit('llm-response', {message: resp}); socket.emit('llm-response', {message: resp});
updated = true;
}).catch(err => { }).catch(err => {
socket.emit('llm-error', {message: err.message || err.toString()}); socket.emit('llm-error', {message: err.message || err.toString()});
}).finally(() => { }).finally(() => {