diff options
| author | aura <nw@moneybot.cc> | 2026-02-17 22:39:42 +0100 |
|---|---|---|
| committer | aura <nw@moneybot.cc> | 2026-02-17 22:39:42 +0100 |
| commit | 636b0323075225c584b62719ed51e75521bb7ffb (patch) | |
| tree | 61b02271b6d0695a4beffc23fb6eb062a7da22c3 /backend/instance/notes.ts | |
push source
Diffstat (limited to 'backend/instance/notes.ts')
| -rw-r--r-- | backend/instance/notes.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/backend/instance/notes.ts b/backend/instance/notes.ts new file mode 100644 index 0000000..5bb5e37 --- /dev/null +++ b/backend/instance/notes.ts @@ -0,0 +1,76 @@ +import fs from 'fs'; +import { v4 as uuidv4 } from 'uuid'; + +import { ToolNote, ToolCall } from './api-defs.js'; +import * as chat from './chat.js'; +import * as u from './utils.js'; + +export type Note = ToolNote; + +export function run( toolCall: ToolCall, notes: Note[], uuid: string ) : chat.Msg | null { + if( !toolCall.parameters ) + return null; + + let note = toolCall.parameters.contents; + if( note.length > 1 ) { + save( note, notes, uuid ); + console.log( "saved note: " + note ); + } + + return { timestamp: u.getTimestamp(), role: "tool", content: "the note has been saved" }; +} + +export function getPromptStr( notes: Note[] ) : string { + if( !notes.length ) + return ''; + + let promptStr = "here is a list of your (the assistant's) saved notes:\n"; + for( let i = 0; i < notes.length; ++i ) { + promptStr += `${i+1}. ${notes[i].content}\n` + } + + return promptStr; +} + +export function load( uuid: string ) : Note[] { + try { + let file = `../data/notes/${uuid}-notes.json`; + const data = fs.readFileSync( file, 'utf8' ); + return JSON.parse( data ); + } catch( err ) { + return []; + } +} + +export function save( newNote: string, notes: Note[], uuid: string ) { + let txt = newNote.slice( 0, 75 ); + let foundNote = false; + for( let [key, savedNote] of Object.entries( notes ) ) { + let split = savedNote.content.split( ' : ' ); + if( split.length < 2 ) + continue; + + let savedWords = split[1].split( " " ); + let words = txt.split( " " ); + + let totalWords = savedWords.length; + let matchingWords = 0; + for( let i = 0; i < savedWords.length; ++i ) { + if( words.find( ( e ) => e === savedWords[i] ) ) + ++matchingWords; + } + + if( matchingWords >= Math.floor( totalWords * 0.95 ) ) { + notes[key].content = `${u.getTimestamp()} : ${txt}`; + foundNote = true; + break; + } + } + if( !foundNote ) + notes.push( { id: uuidv4(), content: `${u.getTimestamp()} : ${newNote.slice( 0, 75 )}` } ); + if( notes.length > 25 ) + notes = notes.slice( 1 ); + + let file = `../data/notes/${uuid}-notes.json`; + fs.writeFileSync( file, JSON.stringify( notes ) ); +} |
