File size: 3,647 Bytes
b63786a
 
 
8df51fe
 
 
 
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const fs = require('fs');
const path = require('path');

const isProduction = process.env.NODE_ENV === 'production';
const STORE_FILE_PATH = isProduction
  ? path.join('/data', 'assistant-memory-store.json')
  : path.join(__dirname, 'assistant-memory-store.json');
const MAX_NOTES_PER_PROFILE = 24;

const state = {
  profiles: {},
};

const trimText = (value = '') => String(value).replace(/\s+/g, ' ').trim();

const normalizeMemoryKey = (memoryKey = '') => trimText(memoryKey).slice(0, 120);

const normalizeProfile = (profile = {}) => ({
  notes: Array.isArray(profile.notes)
    ? profile.notes
        .map((note) => ({
          content: trimText(note?.content).slice(0, 280),
          source: trimText(note?.source) || 'user-message',
          createdAt: note?.createdAt || new Date().toISOString(),
          updatedAt: note?.updatedAt || note?.createdAt || new Date().toISOString(),
        }))
        .filter((note) => note.content)
        .slice(0, MAX_NOTES_PER_PROFILE)
    : [],
  updatedAt: profile.updatedAt || '',
});

const loadStore = () => {
  try {
    if (!fs.existsSync(STORE_FILE_PATH)) {
      return;
    }

    const rawStore = fs.readFileSync(STORE_FILE_PATH, 'utf8');
    if (!rawStore.trim()) {
      return;
    }

    const parsedStore = JSON.parse(rawStore);
    if (!parsedStore || typeof parsedStore !== 'object') {
      return;
    }

    state.profiles = Object.entries(parsedStore.profiles || {}).reduce((result, [memoryKey, profile]) => {
      const normalizedKey = normalizeMemoryKey(memoryKey);
      if (!normalizedKey) {
        return result;
      }

      result[normalizedKey] = normalizeProfile(profile);
      return result;
    }, {});
  } catch (error) {
    console.error('Could not load assistant memory store:', error.message);
  }
};

const saveStore = () => {
  try {
    fs.writeFileSync(
      STORE_FILE_PATH,
      JSON.stringify({ profiles: state.profiles }, null, 2),
      'utf8'
    );
  } catch (error) {
    console.error('Could not save assistant memory store:', error.message);
  }
};

loadStore();

const ensureProfile = (memoryKey = '') => {
  const normalizedKey = normalizeMemoryKey(memoryKey);
  if (!normalizedKey) {
    return null;
  }

  if (!state.profiles[normalizedKey]) {
    state.profiles[normalizedKey] = normalizeProfile();
  }

  return {
    key: normalizedKey,
    profile: state.profiles[normalizedKey],
  };
};

const listNotes = (memoryKey = '') => {
  const normalizedKey = normalizeMemoryKey(memoryKey);
  if (!normalizedKey) {
    return [];
  }

  return normalizeProfile(state.profiles[normalizedKey]).notes;
};

const rememberNote = ({ memoryKey = '', content = '', source = 'user-message' } = {}) => {
  const memoryProfile = ensureProfile(memoryKey);
  const normalizedContent = trimText(content).slice(0, 280);

  if (!memoryProfile || !normalizedContent) {
    return null;
  }

  const now = new Date().toISOString();
  const { profile } = memoryProfile;
  const existingIndex = profile.notes.findIndex(
    (note) => note.content.toLowerCase() === normalizedContent.toLowerCase()
  );
  const previousEntry = existingIndex >= 0 ? profile.notes[existingIndex] : null;

  const nextNote = {
    content: normalizedContent,
    source: trimText(source) || 'user-message',
    createdAt: previousEntry?.createdAt || now,
    updatedAt: now,
  };

  if (existingIndex >= 0) {
    profile.notes.splice(existingIndex, 1);
  }

  profile.notes.unshift(nextNote);
  profile.notes = profile.notes.slice(0, MAX_NOTES_PER_PROFILE);
  profile.updatedAt = now;
  saveStore();

  return nextNote;
};

module.exports = {
  listNotes,
  rememberNote,
};