Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(chat): do not save a duplicate memory note from Remember

Akarshan Biswas284c31f9f7f7
diff --git a/web-app/src/containers/RememberButton.tsx b/web-app/src/containers/RememberButton.tsx
index d04e78c48..67739f859 100644
--- a/web-app/src/containers/RememberButton.tsx
+++ b/web-app/src/containers/RememberButton.tsx
@@ -25,10 +25,17 @@ export const RememberButton = ({ text }: { text: string }) => {
setSaving(true)
try {
const title = useThreads.getState().getCurrentThread()?.title ?? ''
- const name = await rememberNote(title, text)
+ const { name, duplicate } = await rememberNote(title, text)
setSaved(true)
setTimeout(() => setSaved(false), 2000)
- toast.success(t('chat:actions.rememberSaved', { name }))
+ toast.success(
+ t(
+ duplicate
+ ? 'chat:actions.rememberExists'
+ : 'chat:actions.rememberSaved',
+ { name }
+ )
+ )
} catch {
toast.error(t('chat:actions.rememberFailed'))
} finally {
diff --git a/web-app/src/lib/__tests__/agentWorkspace.test.ts b/web-app/src/lib/__tests__/agentWorkspace.test.ts
index 033541713..db68aac63 100644
--- a/web-app/src/lib/__tests__/agentWorkspace.test.ts
+++ b/web-app/src/lib/__tests__/agentWorkspace.test.ts
@@ -93,10 +93,12 @@ describe('agentWorkspace', () => {
it('remembers under the thread title, deduplicating taken names', async () => {
api.memoryList.mockResolvedValue(['chat-notes', 'chat-notes-2'])
+ api.memoryRead.mockResolvedValue('something else')
const ws = await import('../agentWorkspace')
- await expect(ws.rememberNote('Chat Notes', 'the fact')).resolves.toBe(
- 'chat-notes-3'
- )
+ await expect(ws.rememberNote('Chat Notes', 'the fact')).resolves.toEqual({
+ name: 'chat-notes-3',
+ duplicate: false,
+ })
// Suffixing, never overwriting: memory_write replaces by name, and the
// existing notes are not this action's to replace.
expect(api.memoryWrite).toHaveBeenCalledWith(
@@ -109,9 +111,36 @@ describe('agentWorkspace', () => {
it('uses the plain slug when the name is free', async () => {
api.memoryList.mockResolvedValue([])
const ws = await import('../agentWorkspace')
- await expect(ws.rememberNote('Fresh Topic', 'body')).resolves.toBe(
- 'fresh-topic'
+ await expect(ws.rememberNote('Fresh Topic', 'body')).resolves.toEqual({
+ name: 'fresh-topic',
+ duplicate: false,
+ })
+ expect(api.memoryRead).not.toHaveBeenCalled()
+ })
+
+ // Clicking Remember twice on one message must not mint `-2`, `-3`, ... copies
+ // of the same text: the suffix is for a *different* fact under a reused title.
+ it('does not write a second copy of a note that already exists', async () => {
+ api.memoryList.mockResolvedValue(['chat-notes', 'chat-notes-2'])
+ api.memoryRead.mockImplementation(async (_: string, name: string) =>
+ name === 'chat-notes-2' ? 'the fact' : 'other'
)
+ const ws = await import('../agentWorkspace')
+ await expect(ws.rememberNote('Chat Notes', 'the fact')).resolves.toEqual({
+ name: 'chat-notes-2',
+ duplicate: true,
+ })
+ expect(api.memoryWrite).not.toHaveBeenCalled()
+ })
+
+ it('treats a note that fails to read as different rather than failing', async () => {
+ api.memoryList.mockResolvedValue(['chat-notes'])
+ api.memoryRead.mockRejectedValue(new Error('gone'))
+ const ws = await import('../agentWorkspace')
+ await expect(ws.rememberNote('Chat Notes', 'the fact')).resolves.toEqual({
+ name: 'chat-notes-2',
+ duplicate: false,
+ })
})
/// Unlike a tool call, a failed edit has a user waiting on it, so these throw
diff --git a/web-app/src/lib/agentWorkspace.ts b/web-app/src/lib/agentWorkspace.ts
index 83ae86655..84e3e9bfe 100644
--- a/web-app/src/lib/agentWorkspace.ts
+++ b/web-app/src/lib/agentWorkspace.ts
@@ -75,22 +75,35 @@ export function slugifyMemoryName(title: string): string {
return slug || 'memory'
}
+export type RememberResult = {
+ name: string
+ /** True when a note with this exact content already existed; nothing was written. */
+ duplicate: boolean
+}
+
/**
* The chat surface's "Remember" action: save `content` as a note named after
* the thread title. `memory_write` replaces a note by name, and this is a new
* fact rather than a curated topic, so an existing name gets a `-2`, `-3`, ...
- * suffix instead of silently overwriting it. Returns the name written.
+ * suffix instead of silently overwriting it. A sibling that already holds this
+ * exact content is returned instead of being copied again, so a second click
+ * on the same message is a no-op. A sibling that cannot be read counts as
+ * different: the save must not fail over one unreadable note.
*/
export async function rememberNote(
title: string,
content: string
-): Promise<string> {
+): Promise<RememberResult> {
const base = slugifyMemoryName(title)
const taken = new Set(await listMemories())
let name = base
- for (let n = 2; taken.has(name); n++) name = `${base}-${n}`
+ for (let n = 2; taken.has(name); n++) {
+ const existing = await readMemory(name).catch(() => null)
+ if (existing === content) return { name, duplicate: true }
+ name = `${base}-${n}`
+ }
await writeMemory(name, content)
- return name
+ return { name, duplicate: false }
}
/** Open the store in the OS file manager. */
diff --git a/web-app/src/locales/en/chat.json b/web-app/src/locales/en/chat.json
index 8c6693ded..5774b35e4 100644
--- a/web-app/src/locales/en/chat.json
+++ b/web-app/src/locales/en/chat.json
@@ -19,6 +19,7 @@
"continue": "Continue generating",
"remember": "Remember this",
"rememberSaved": "Saved to memory as \"{{name}}\"",
+ "rememberExists": "Already in memory as \"{{name}}\"",
"rememberFailed": "Could not save to memory"
},
"done": "Done",