diff --git a/models/chaite/storage/sqlite/channel_storage.js b/models/chaite/storage/sqlite/channel_storage.js index 544b4ec..8e159d7 100644 --- a/models/chaite/storage/sqlite/channel_storage.js +++ b/models/chaite/storage/sqlite/channel_storage.js @@ -249,7 +249,16 @@ export class SQLiteChannelStorage extends ChaiteStorage { */ async setItem (id, channel) { await this.ensureInitialized() + if (!id) { + id = this._generateId() + } + // 加上时间戳 + if (!channel.createdAt) { + channel.createdAt = new Date().toISOString() + } + + channel.updatedAt = new Date().toISOString() // 转换为数据库记录 const record = this._channelToRecord(channel) record.id = id // 确保ID是指定的ID diff --git a/models/chaite/storage/sqlite/chat_preset_storage.js b/models/chaite/storage/sqlite/chat_preset_storage.js index 50cdcd4..7f99d62 100644 --- a/models/chaite/storage/sqlite/chat_preset_storage.js +++ b/models/chaite/storage/sqlite/chat_preset_storage.js @@ -221,7 +221,16 @@ export class SQLiteChatPresetStorage extends ChaiteStorage { */ async setItem (id, preset) { await this.ensureInitialized() + if (!id) { + id = this._generateId() + } + // 加上时间戳 + if (!preset.createdAt) { + preset.createdAt = new Date().toISOString() + } + + preset.updatedAt = new Date().toISOString() // 转换为数据库记录 const record = this._presetToRecord(preset) record.id = id // 确保ID是指定的ID diff --git a/models/chaite/storage/sqlite/migrate.js b/models/chaite/storage/sqlite/migrate.js index cbb17b2..33c47cc 100644 --- a/models/chaite/storage/sqlite/migrate.js +++ b/models/chaite/storage/sqlite/migrate.js @@ -24,6 +24,45 @@ export async function checkMigrate () { const dbPath = path.join(dataDir, 'data.db') + // 删除所有id为空的行 + logger.debug('开始修复id为空的数据行...') + const collectionsToClean = ['channel', 'chat_presets', 'tools', 'processors'] + for (const collectionName of collectionsToClean) { + try { + const collection = ChatGPTStorage.collection(collectionName) + const allItems = await collection.findAll() + const invalidItems = allItems.filter(item => !item.id) + + if (invalidItems.length > 0) { + logger.info(`在${collectionName}中发现${invalidItems.length}条id为空的数据,正在修复...`) + + for (const item of invalidItems) { + // 生成一个新的唯一ID + const newId = `generated_${Date.now()}_${Math.random().toString(36).substring(2, 9)}` + // 更新时间戳 + const now = new Date().toISOString() + + // 更新项目 + item.id = newId + item.createdAt = now + item.updatedAt = now + + // 保存更新后的项目 + await collection.set(newId, item) + + // 移除旧的无ID项 + await collection.remove(item) + } + + logger.info(`已成功修复${collectionName}中的${invalidItems.length}条无效数据`) + } else { + logger.debug(`${collectionName}中没有发现id为空的数据`) + } + } catch (err) { + logger.error(`修复${collectionName}中id为空的数据时出错:`, err) + } + } + // 定义要检查的存储对 const storagePairs = [ { diff --git a/models/chaite/storage/sqlite/processors_storage.js b/models/chaite/storage/sqlite/processors_storage.js index e80ce59..8f2f7c2 100644 --- a/models/chaite/storage/sqlite/processors_storage.js +++ b/models/chaite/storage/sqlite/processors_storage.js @@ -185,7 +185,16 @@ export class SQLiteProcessorsStorage extends ChaiteStorage { */ async setItem (id, processor) { await this.ensureInitialized() + if (!id) { + id = this._generateId() + } + // 加上时间戳 + if (!processor.createdAt) { + processor.createdAt = new Date().toISOString() + } + + processor.updatedAt = new Date().toISOString() // 转换为数据库记录 const record = this._processorToRecord(processor) record.id = id // 确保ID是指定的ID diff --git a/models/chaite/storage/sqlite/tool_groups_storage.js b/models/chaite/storage/sqlite/tool_groups_storage.js index e5b1133..24e6f74 100644 --- a/models/chaite/storage/sqlite/tool_groups_storage.js +++ b/models/chaite/storage/sqlite/tool_groups_storage.js @@ -100,7 +100,16 @@ export class SQLiteToolsGroupStorage extends ChaiteStorage { */ async setItem (id, data) { await this.ensureInitialized() + if (!id) { + id = this._generateId() + } + // 加上时间戳 + if (!data.createdAt) { + data.createdAt = new Date().toISOString() + } + + data.updatedAt = new Date().toISOString() // 提取工具组数据 const { name, description, tools } = data const updatedAt = Date.now() diff --git a/models/chaite/storage/sqlite/tools_storage.js b/models/chaite/storage/sqlite/tools_storage.js index 4517ed5..c3aaf78 100644 --- a/models/chaite/storage/sqlite/tools_storage.js +++ b/models/chaite/storage/sqlite/tools_storage.js @@ -211,6 +211,17 @@ export class SQLiteToolsStorage extends ChaiteStorage { async setItem (id, tool) { await this.ensureInitialized() + if (!id) { + id = this._generateId() + } + + // 加上时间戳 + if (!tool.createdAt) { + tool.createdAt = new Date().toISOString() + } + + tool.updatedAt = new Date().toISOString() + // 转换为数据库记录 const record = this._toolToRecord(tool) record.id = id // 确保ID是指定的ID