feat: Upgrade database schema and migrate web search metadata

- Add database version 5 with schema updates
- Create `upgradeToV5` function to migrate Tavily web search metadata to new format
- Update types to support new web search metadata structure
- Minor code cleanup and formatting improvements
This commit is contained in:
kangfenmao 2025-03-06 22:31:04 +08:00
parent e5664048d9
commit a4b5ef9bde
6 changed files with 46 additions and 1 deletions

View File

@ -1,6 +1,8 @@
import { FileType, KnowledgeItem, Topic, TranslateHistory } from '@renderer/types' import { FileType, KnowledgeItem, Topic, TranslateHistory } from '@renderer/types'
import { Dexie, type EntityTable } from 'dexie' import { Dexie, type EntityTable } from 'dexie'
import { upgradeToV5 } from './upgrades'
// Database declaration (move this to its own module also) // Database declaration (move this to its own module also)
export const db = new Dexie('CherryStudio') as Dexie & { export const db = new Dexie('CherryStudio') as Dexie & {
files: EntityTable<FileType, 'id'> files: EntityTable<FileType, 'id'>
@ -35,4 +37,14 @@ db.version(4).stores({
translate_history: '&id, sourceText, targetText, sourceLanguage, targetLanguage, createdAt' translate_history: '&id, sourceText, targetText, sourceLanguage, targetLanguage, createdAt'
}) })
db.version(5)
.stores({
files: 'id, name, origin_name, path, size, ext, type, created_at, count',
topics: '&id, messages',
settings: '&id, value',
knowledge_notes: '&id, baseId, type, content, created_at, updated_at',
translate_history: '&id, sourceText, targetText, sourceLanguage, targetLanguage, createdAt'
})
.upgrade((tx) => upgradeToV5(tx))
export default db export default db

View File

@ -0,0 +1,30 @@
import { Transaction } from 'dexie'
export async function upgradeToV5(tx: Transaction): Promise<void> {
const topics = await tx.table('topics').toArray()
for (const topic of topics) {
let hasChanges = false
for (const message of topic.messages) {
if (message?.metadata?.tavily) {
hasChanges = true
const tavily = message.metadata.tavily
delete message.metadata.tavily
message.metadata.webSearch = {
query: tavily.query,
results:
tavily.results?.map((i) => ({
title: i.title,
url: i.url,
content: i.content
})) || []
}
}
}
if (hasChanges) {
await tx.table('topics').put(topic)
}
}
}

View File

@ -415,6 +415,7 @@ export default class OpenAIProvider extends BaseProvider {
}) })
} }
} }
const stream = await this.sdk.chat.completions const stream = await this.sdk.chat.completions
// @ts-ignore key is not typed // @ts-ignore key is not typed
.create( .create(

View File

@ -34,7 +34,7 @@ const persistedReducer = persistReducer(
{ {
key: 'cherry-studio', key: 'cherry-studio',
storage, storage,
version: 76, version: 77,
blacklist: ['runtime'], blacklist: ['runtime'],
migrate migrate
}, },

View File

@ -1230,6 +1230,7 @@ const migrateConfig = {
delete p.enabled delete p.enabled
}) })
} }
return state return state
} }
} }

View File

@ -299,6 +299,7 @@ export type WebSearchResponse = {
query?: string query?: string
results: WebSearchResult[] results: WebSearchResult[]
} }
export type WebSearchResult = { export type WebSearchResult = {
title: string title: string
content: string content: string