Skip to content

Commit

Permalink
history merge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Oct 29, 2024
1 parent 1ef705e commit c98c298
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/main/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const saveHistory = (app: App, content: Chat[]) => {
// local
const filepath = historyFilePath(app)
fs.writeFileSync(filepath, JSON.stringify(content, null, 2))
monitor.notify(filepath)

} catch (error) {
console.log('Error saving history data', error)
Expand Down
46 changes: 27 additions & 19 deletions src/services/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,33 @@ const mergeHistory = (jsonChats: any[]) => {
// need to know
let patched = false

try {
for (const jsonChat of jsonChats) {
const chat: any = store.chats.find((chat) => chat.uuid === jsonChat.uuid)
if (chat) {
if (jsonChat.deleted) {
store.chats = store.chats.filter((chat) => chat.uuid !== jsonChat.uuid)
patched = true
} else {
patched = patched || chat.patchFromJson(jsonChat)
}
} else {
const chat = new Chat(jsonChat)
store.chats.push(chat)
patched = true
}
}
} catch (error) {
if (error.code !== 'ENOENT') {
console.log('Error patching history data', error)
// get the current ids and new ids
const currentIds = store.chats.map((chat) => chat.uuid)
const newIds = jsonChats.map((chat) => chat.uuid)

// remove deleted chats
const deletedIds = currentIds.filter((id) => !newIds.includes(id))
//console.log(`Deleting ${deletedIds.length} chats`)
if (deletedIds.length > 0) {
store.chats = store.chats.filter((chat) => !deletedIds.includes(chat.uuid))
patched = true
}

// add the new chats
const addedIds = newIds.filter((id) => !currentIds.includes(id))
//console.log(`Adding ${addedIds.length} chats`)
for (const addedId of addedIds) {
const chat = new Chat(jsonChats.find((chat) => chat.uuid === addedId))
store.chats.push(chat)
patched = true
}

// patch the existing chats
for (const chat of store.chats) {
const jsonChat = jsonChats.find((c) => c.uuid === chat.uuid)
if (jsonChat) {
//console.log(`Patching chat ${chat.uuid}`)
patched = chat.patchFromJson(jsonChat) || patched
}
}

Expand Down

0 comments on commit c98c298

Please sign in to comment.