handle updates with version number instead of weird check

This commit is contained in:
Andrew Pareles 2025-02-15 00:01:37 -08:00
parent d91ec9da2e
commit 05d8b3a982

View file

@ -154,40 +154,46 @@ class ChatThreadService extends Disposable implements IChatThreadService {
) { ) {
super() super()
const oldVersionNum = this._storageService.get(THREAD_VERSION_KEY, StorageScope.APPLICATION)
const readThreads = this._readAllThreads()
const updatedThreads = this._updatedThreadsToVersion(readThreads, oldVersionNum)
if (updatedThreads !== null) {
this._storeAllThreads(updatedThreads)
}
const allThreads = updatedThreads ?? readThreads
this.state = { this.state = {
allThreads: this._readAllThreads(), allThreads: allThreads,
currentThreadId: null as unknown as string, // gets set in startNewThread() currentThreadId: null as unknown as string, // gets set in startNewThread()
} }
// always be in a thread // always be in a thread
this.openNewThread() this.openNewThread()
// for now just write the version, anticipating bigger changes in the future where we'll want to access this
this._storageService.store(THREAD_VERSION_KEY, THREAD_VERSION, StorageScope.APPLICATION, StorageTarget.USER) this._storageService.store(THREAD_VERSION_KEY, THREAD_VERSION, StorageScope.APPLICATION, StorageTarget.USER)
} }
private _readAllThreads(): ChatThreads { private _readAllThreads(): ChatThreads {
// PUT ANY VERSION CHANGE FORMAT CONVERSION CODE HERE
// CAN ADD "v0" TAG IN STORAGE AND CONVERT
const threadsStr = this._storageService.get(THREAD_STORAGE_KEY, StorageScope.APPLICATION) const threadsStr = this._storageService.get(THREAD_STORAGE_KEY, StorageScope.APPLICATION)
const threads: ChatThreads = threadsStr ? JSON.parse(threadsStr) : {} const threads: ChatThreads = threadsStr ? JSON.parse(threadsStr) : {}
this._updateThreadsToVersion(threads, THREAD_VERSION)
return threads return threads
} }
private _updateThreadsToVersion(oldThreadsObject: any, toVersion: string) { // returns if should update
private _updatedThreadsToVersion(oldThreadsObject: any, oldVersion: string | undefined): ChatThreads | null {
if (toVersion === 'v2') { if (!oldVersion) {
const threads: ChatThreads = oldThreadsObject // unknown, just reset chat?
return null
}
/** v1 -> v2 /** v1 -> v2
- threadsState.currentStagingSelections: CodeStagingSelection[] | null; - threadsState.currentStagingSelections: CodeStagingSelection[] | null;
@ -196,22 +202,8 @@ class ChatThreadService extends Disposable implements IChatThreadService {
+ chatMessage.staging: StagingInfo | null + chatMessage.staging: StagingInfo | null
*/ */
else if (oldVersion === 'v1') {
// check if we need to update const threads = oldThreadsObject as Omit<ChatThreads, 'staging' | 'focusedMessageIdx'>
let shouldUpdate = false
for (const thread of Object.values(threads)) {
if (!thread.staging) {
shouldUpdate = true
}
for (const chatMessage of Object.values(thread.messages)) {
if (chatMessage.role === 'user' && !chatMessage.staging) {
shouldUpdate = true
}
}
}
if (!shouldUpdate) return;
// update the threads // update the threads
for (const thread of Object.values(threads)) { for (const thread of Object.values(threads)) {
if (!thread.staging) { if (!thread.staging) {
@ -226,8 +218,14 @@ class ChatThreadService extends Disposable implements IChatThreadService {
} }
// push the update // push the update
this._storeAllThreads(threads) return threads
} }
else if (oldVersion === 'v2') {
return null
}
// up to date
return null
} }