@musnows/scriverse 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/database.js CHANGED
@@ -6,9 +6,9 @@ import { documentParagraphLineRanges } from "./hybrid-search.js";
6
6
  import { logger, sanitizeError } from "./logger.js";
7
7
  import { documentShortSearchTerms, normalizeDocumentSearchText, splitDocumentParagraphs } from "./utils.js";
8
8
  export const PLATFORM_AI_WORK_ID = "__scriverse_platform_ai__";
9
- // 版本 81 用于列表查询索引;版本 82 由 Store 写入实体版本基线标记;版本 83 创建协作状态表;版本 84 创建备份加密表;版本 85 持久化协作变更动作;版本 86 扩展直接图片上传格式;版本 87 增加作品与分卷回收站;版本 88 持久化 AI 对话分支幂等键;版本 89 持久化 AI 对话流请求锁与幂等状态;版本 90 持久化 AI 连通性测试冷却状态;版本 91 建立章节段落行号索引;版本 92 增加 AI 对话收藏状态;版本 93 优化伏笔计划回收章节查询;版本 94 增加模型思考强度;版本 95 增加供应商最大输出参数选择;版本 96 扩展模型思考强度档位;版本 97 增加人物性别字段;版本 98 增加正文稳定等待配置;版本 99 持久化关系扮演中的用户角色;版本 100 增加平台 AI 流事件空闲超时配置;版本 101 将平台 AI 流事件空闲超时上限提升至 600 秒;版本 102 创建角色头像元数据表;版本 103 回填历史 AI 对话归属并建立用户列表索引;版本 104 扩大供应商协议约束以支持 OpenAI Responses;版本 105 增加独立分卷剧情顺序;版本 106 增加供应商思考类型配置;版本 107 增加 AI Cache Write 输入 Token 统计;版本 108 增加作品 AI 每月 Token 额度;版本 109 增加供应商日、月 Token 额度;版本 110 将日、月 Token 额度下限调整为大于 0;版本 111 扩展模型思考强度为 auto;版本 112 为 CLI API Key 增加可复制的加密密文;版本 113 增加角色收藏状态与列表索引;版本 114 增加组织、设定档案与想法收藏状态及列表索引;版本 115 增加 AI 对话会话级场景钉;版本 116 增加可持久化且可撤销的 Desktop Bearer 会话;版本 117 增加作品离线授权、同步变更游标与幂等变更结果;版本 118 增加供应商分析请求超时配置;版本 119 记录分析任务是否由 API Key 创建。
9
+ // 版本 81 用于列表查询索引;版本 82 由 Store 写入实体版本基线标记;版本 83 创建协作状态表;版本 84 创建备份加密表;版本 85 持久化协作变更动作;版本 86 扩展直接图片上传格式;版本 87 增加作品与分卷回收站;版本 88 持久化 AI 对话分支幂等键;版本 89 持久化 AI 对话流请求锁与幂等状态;版本 90 持久化 AI 连通性测试冷却状态;版本 91 建立章节段落行号索引;版本 92 增加 AI 对话收藏状态;版本 93 优化伏笔计划回收章节查询;版本 94 增加模型思考强度;版本 95 增加供应商最大输出参数选择;版本 96 扩展模型思考强度档位;版本 97 增加人物性别字段;版本 98 增加正文稳定等待配置;版本 99 持久化关系扮演中的用户角色;版本 100 增加平台 AI 流事件空闲超时配置;版本 101 将平台 AI 流事件空闲超时上限提升至 600 秒;版本 102 创建角色头像元数据表;版本 103 回填历史 AI 对话归属并建立用户列表索引;版本 104 扩大供应商协议约束以支持 OpenAI Responses;版本 105 增加独立分卷剧情顺序;版本 106 增加供应商思考类型配置;版本 107 增加 AI Cache Write 输入 Token 统计;版本 108 增加作品 AI 每月 Token 额度;版本 109 增加供应商日、月 Token 额度;版本 110 将日、月 Token 额度下限调整为大于 0;版本 111 扩展模型思考强度为 auto;版本 112 为 CLI API Key 增加可复制的加密密文;版本 113 增加角色收藏状态与列表索引;版本 114 增加组织、设定档案与想法收藏状态及列表索引;版本 115 增加 AI 对话会话级场景钉;版本 116 增加可持久化且可撤销的 Desktop Bearer 会话;版本 117 增加作品离线授权、同步变更游标与幂等变更结果;版本 118 增加供应商分析请求超时配置;版本 119 记录分析任务是否由 API Key 创建;版本 120 将书籍资料收藏按用户隔离并增加书籍级共享置顶。
10
10
  export const ENTITY_VERSION_BASELINE_MIGRATION_VERSION = 82;
11
- export const DATABASE_SCHEMA_VERSION = 119;
11
+ export const DATABASE_SCHEMA_VERSION = 120;
12
12
  export const SQLITE_IOERR_SHMSIZE = 4874;
13
13
  export function isSqliteDiskIoError(error) {
14
14
  if (!(error instanceof Error))
@@ -4345,6 +4345,81 @@ export class Database {
4345
4345
  if (foreignKeys.length > 0)
4346
4346
  throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4347
4347
  }
4348
+ const workEntityFavoritesTablePresent = this.get("SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = 'work_entity_favorites'") !== undefined;
4349
+ const workEntityPinsTablePresent = this.get("SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = 'work_entity_pins'") !== undefined;
4350
+ const workEntityPreferenceIndexesPresent = [
4351
+ "idx_work_entity_favorites_user",
4352
+ "idx_work_entity_favorites_entity",
4353
+ "idx_work_entity_pins_entity"
4354
+ ].every((index) => this.get("SELECT 1 AS present FROM sqlite_master WHERE type = 'index' AND name = ?", index) !== undefined);
4355
+ if (!applied.has(120) || !workEntityFavoritesTablePresent || !workEntityPinsTablePresent || !workEntityPreferenceIndexesPresent) {
4356
+ this.transaction(() => {
4357
+ this.run(`CREATE TABLE IF NOT EXISTS work_entity_favorites (
4358
+ work_id TEXT NOT NULL REFERENCES works(id) ON DELETE CASCADE,
4359
+ entity_type TEXT NOT NULL CHECK(entity_type IN ('character', 'draft', 'setting', 'organization')),
4360
+ entity_id TEXT NOT NULL,
4361
+ user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
4362
+ is_favorite INTEGER NOT NULL DEFAULT 0 CHECK(is_favorite IN (0, 1)),
4363
+ created_at TEXT NOT NULL,
4364
+ updated_at TEXT NOT NULL,
4365
+ PRIMARY KEY(work_id, entity_type, entity_id, user_id)
4366
+ )`);
4367
+ this.run(`CREATE TABLE IF NOT EXISTS work_entity_pins (
4368
+ work_id TEXT NOT NULL REFERENCES works(id) ON DELETE CASCADE,
4369
+ entity_type TEXT NOT NULL CHECK(entity_type IN ('character', 'draft', 'setting', 'organization')),
4370
+ entity_id TEXT NOT NULL,
4371
+ is_pinned INTEGER NOT NULL DEFAULT 0 CHECK(is_pinned IN (0, 1)),
4372
+ pinned_by_user_id TEXT REFERENCES users(id) ON DELETE SET NULL,
4373
+ created_at TEXT NOT NULL,
4374
+ updated_at TEXT NOT NULL,
4375
+ PRIMARY KEY(work_id, entity_type, entity_id)
4376
+ )`);
4377
+ this.run("CREATE INDEX IF NOT EXISTS idx_work_entity_favorites_user ON work_entity_favorites(work_id, user_id, entity_type, is_favorite, entity_id)");
4378
+ this.run("CREATE INDEX IF NOT EXISTS idx_work_entity_favorites_entity ON work_entity_favorites(work_id, entity_type, entity_id, is_favorite)");
4379
+ this.run("CREATE INDEX IF NOT EXISTS idx_work_entity_pins_entity ON work_entity_pins(work_id, entity_type, is_pinned, entity_id)");
4380
+ const timestamp = new Date().toISOString();
4381
+ this.run(`
4382
+ INSERT INTO work_entity_favorites (work_id, entity_type, entity_id, user_id, is_favorite, created_at, updated_at)
4383
+ SELECT character.work_id, 'character', character.id, work.owner_user_id, 1,
4384
+ COALESCE(character.created_at, ?), COALESCE(character.updated_at, ?)
4385
+ FROM characters character
4386
+ JOIN works work ON work.id = character.work_id
4387
+ WHERE character.is_favorite = 1 AND work.owner_user_id IS NOT NULL
4388
+ ON CONFLICT(work_id, entity_type, entity_id, user_id) DO UPDATE SET is_favorite = excluded.is_favorite, updated_at = excluded.updated_at`, timestamp, timestamp);
4389
+ this.run(`
4390
+ INSERT INTO work_entity_favorites (work_id, entity_type, entity_id, user_id, is_favorite, created_at, updated_at)
4391
+ SELECT draft.work_id, 'draft', draft.id, work.owner_user_id, 1,
4392
+ COALESCE(draft.created_at, ?), COALESCE(draft.updated_at, ?)
4393
+ FROM drafts draft
4394
+ JOIN works work ON work.id = draft.work_id
4395
+ WHERE draft.is_favorite = 1 AND work.owner_user_id IS NOT NULL
4396
+ ON CONFLICT(work_id, entity_type, entity_id, user_id) DO UPDATE SET is_favorite = excluded.is_favorite, updated_at = excluded.updated_at`, timestamp, timestamp);
4397
+ this.run(`
4398
+ INSERT INTO work_entity_favorites (work_id, entity_type, entity_id, user_id, is_favorite, created_at, updated_at)
4399
+ SELECT setting.work_id, 'setting', setting.id, work.owner_user_id, 1,
4400
+ COALESCE(setting.created_at, ?), COALESCE(setting.updated_at, ?)
4401
+ FROM settings setting
4402
+ JOIN works work ON work.id = setting.work_id
4403
+ WHERE setting.is_favorite = 1 AND work.owner_user_id IS NOT NULL
4404
+ ON CONFLICT(work_id, entity_type, entity_id, user_id) DO UPDATE SET is_favorite = excluded.is_favorite, updated_at = excluded.updated_at`, timestamp, timestamp);
4405
+ this.run(`
4406
+ INSERT INTO work_entity_favorites (work_id, entity_type, entity_id, user_id, is_favorite, created_at, updated_at)
4407
+ SELECT organization.work_id, 'organization', organization.id, work.owner_user_id, 1,
4408
+ COALESCE(organization.created_at, ?), COALESCE(organization.updated_at, ?)
4409
+ FROM organizations organization
4410
+ JOIN works work ON work.id = organization.work_id
4411
+ WHERE organization.is_favorite = 1 AND work.owner_user_id IS NOT NULL
4412
+ ON CONFLICT(work_id, entity_type, entity_id, user_id) DO UPDATE SET is_favorite = excluded.is_favorite, updated_at = excluded.updated_at`, timestamp, timestamp);
4413
+ this.run("INSERT OR IGNORE INTO schema_migrations (version, applied_at) VALUES (120, ?)", timestamp);
4414
+ });
4415
+ const integrity = this.all("PRAGMA integrity_check");
4416
+ if (integrity.some((row) => row.integrity_check !== "ok")) {
4417
+ throw new Error(`数据库完整性检查失败:${integrity.map((row) => row.integrity_check).join(";")}`);
4418
+ }
4419
+ const foreignKeys = this.all("PRAGMA foreign_key_check");
4420
+ if (foreignKeys.length > 0)
4421
+ throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4422
+ }
4348
4423
  }
4349
4424
  normalizeCharacterName(value) {
4350
4425
  return value.normalize("NFKC").trim().replace(/\s+/gu, " ").toLocaleLowerCase("zh-CN");