1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 ) );
}
|