@musnows/scriverse 0.8.6 → 0.8.8

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
@@ -5,9 +5,9 @@ import { documentParagraphLineRanges } from "./hybrid-search.js";
5
5
  import { logger, sanitizeError } from "./logger.js";
6
6
  import { documentShortSearchTerms, normalizeDocumentSearchText, splitDocumentParagraphs } from "./utils.js";
7
7
  export const PLATFORM_AI_WORK_ID = "__scriverse_platform_ai__";
8
- // 版本 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
8
+ // 版本 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 对话会话级场景钉。
9
9
  export const ENTITY_VERSION_BASELINE_MIGRATION_VERSION = 82;
10
- export const DATABASE_SCHEMA_VERSION = 110;
10
+ export const DATABASE_SCHEMA_VERSION = 115;
11
11
  export const SQLITE_IOERR_SHMSIZE = 4874;
12
12
  export function isSqliteDiskIoError(error) {
13
13
  if (!(error instanceof Error))
@@ -278,6 +278,7 @@ export class Database {
278
278
  tags_json TEXT NOT NULL DEFAULT '[]',
279
279
  status TEXT NOT NULL DEFAULT 'draft',
280
280
  locked INTEGER NOT NULL DEFAULT 0,
281
+ is_favorite INTEGER NOT NULL DEFAULT 0 CHECK(is_favorite IN (0, 1)),
281
282
  evidence_json TEXT NOT NULL DEFAULT '[]',
282
283
  scope_json TEXT NOT NULL DEFAULT '{}',
283
284
  author_note TEXT NOT NULL DEFAULT '',
@@ -293,6 +294,7 @@ export class Database {
293
294
  setting_module TEXT,
294
295
  title TEXT NOT NULL,
295
296
  content TEXT NOT NULL DEFAULT '',
297
+ is_favorite INTEGER NOT NULL DEFAULT 0 CHECK(is_favorite IN (0, 1)),
296
298
  created_at TEXT NOT NULL,
297
299
  updated_at TEXT NOT NULL,
298
300
  CHECK(setting_module IS NULL OR setting_module IN ('settings', 'characters', 'races', 'organizations', 'timeline', 'relationships', 'outlines')),
@@ -326,6 +328,7 @@ export class Database {
326
328
  profile_json TEXT NOT NULL DEFAULT '{}',
327
329
  current_state_json TEXT NOT NULL DEFAULT '{}',
328
330
  is_dead INTEGER NOT NULL DEFAULT 0 CHECK(is_dead IN (0, 1)),
331
+ is_favorite INTEGER NOT NULL DEFAULT 0 CHECK(is_favorite IN (0, 1)),
329
332
  locked_fields_json TEXT NOT NULL DEFAULT '[]',
330
333
  first_chapter_id TEXT REFERENCES chapters(id) ON DELETE SET NULL,
331
334
  merged_into_character_id TEXT,
@@ -481,7 +484,7 @@ export class Database {
481
484
  output_note TEXT NOT NULL DEFAULT '',
482
485
  preset_json TEXT NOT NULL DEFAULT '{}',
483
486
  thinking_enabled INTEGER NOT NULL DEFAULT 1,
484
- thinking_effort TEXT NOT NULL DEFAULT 'default' CHECK(thinking_effort IN ('default', 'low', 'medium', 'high', 'xhigh', 'max')),
487
+ thinking_effort TEXT NOT NULL DEFAULT 'default' CHECK(thinking_effort IN ('default', 'auto', 'low', 'medium', 'high', 'xhigh', 'max')),
485
488
  multimodal_enabled INTEGER NOT NULL DEFAULT 0 CHECK(multimodal_enabled IN (0, 1)),
486
489
  enabled INTEGER NOT NULL DEFAULT 1,
487
490
  note TEXT NOT NULL DEFAULT '',
@@ -613,6 +616,7 @@ export class Database {
613
616
  agent_tools_json TEXT,
614
617
  injected_entities_json TEXT NOT NULL DEFAULT '{"characters":[],"races":[],"organizations":[]}',
615
618
  system_clock_text TEXT NOT NULL DEFAULT '',
619
+ scene_pin_json TEXT NOT NULL DEFAULT '{}',
616
620
  created_at TEXT NOT NULL,
617
621
  updated_at TEXT NOT NULL
618
622
  );
@@ -724,6 +728,7 @@ export class Database {
724
728
  normalized_name TEXT NOT NULL,
725
729
  description TEXT NOT NULL DEFAULT '',
726
730
  is_dissolved INTEGER NOT NULL DEFAULT 0 CHECK(is_dissolved IN (0, 1)),
731
+ is_favorite INTEGER NOT NULL DEFAULT 0 CHECK(is_favorite IN (0, 1)),
727
732
  settings_json TEXT NOT NULL DEFAULT '[]',
728
733
  created_at TEXT NOT NULL,
729
734
  updated_at TEXT NOT NULL,
@@ -4071,6 +4076,149 @@ export class Database {
4071
4076
  if (foreignKeys.length > 0)
4072
4077
  throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4073
4078
  }
4079
+ if (!applied.has(111)) {
4080
+ const modelsSql = String(this.get("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'models'")?.sql ?? "");
4081
+ if (!modelsSql.includes("'auto'")) {
4082
+ this.raw.exec("PRAGMA foreign_keys = OFF");
4083
+ try {
4084
+ this.transaction(() => {
4085
+ this.run(`CREATE TABLE models_v111 (
4086
+ id TEXT PRIMARY KEY,
4087
+ provider_id TEXT NOT NULL REFERENCES providers(id) ON DELETE CASCADE,
4088
+ display_name TEXT NOT NULL,
4089
+ model_id TEXT NOT NULL,
4090
+ purposes_json TEXT NOT NULL DEFAULT '[]',
4091
+ context_note TEXT NOT NULL DEFAULT '',
4092
+ context_window INTEGER NOT NULL DEFAULT 128000 CHECK(context_window BETWEEN 1024 AND 2000000),
4093
+ output_note TEXT NOT NULL DEFAULT '',
4094
+ preset_json TEXT NOT NULL DEFAULT '{}',
4095
+ thinking_enabled INTEGER NOT NULL DEFAULT 1,
4096
+ thinking_effort TEXT NOT NULL DEFAULT 'default' CHECK(thinking_effort IN ('default', 'auto', 'low', 'medium', 'high', 'xhigh', 'max')),
4097
+ multimodal_enabled INTEGER NOT NULL DEFAULT 0 CHECK(multimodal_enabled IN (0, 1)),
4098
+ enabled INTEGER NOT NULL DEFAULT 1,
4099
+ note TEXT NOT NULL DEFAULT '',
4100
+ created_at TEXT NOT NULL,
4101
+ updated_at TEXT NOT NULL,
4102
+ UNIQUE(provider_id, model_id)
4103
+ )`);
4104
+ this.run(`INSERT INTO models_v111 (
4105
+ id, provider_id, display_name, model_id, purposes_json, context_note, context_window, output_note,
4106
+ preset_json, thinking_enabled, thinking_effort, multimodal_enabled, enabled, note, created_at, updated_at
4107
+ )
4108
+ SELECT
4109
+ id, provider_id, display_name, model_id, purposes_json, context_note, context_window, output_note,
4110
+ preset_json, thinking_enabled, thinking_effort, multimodal_enabled, enabled, note, created_at, updated_at
4111
+ FROM models`);
4112
+ this.run("DROP TABLE models");
4113
+ this.run("ALTER TABLE models_v111 RENAME TO models");
4114
+ this.run(`CREATE TRIGGER IF NOT EXISTS ai_connectivity_test_states_model_delete
4115
+ AFTER DELETE ON models BEGIN
4116
+ DELETE FROM ai_connectivity_test_states WHERE object_type = 'model' AND object_id = OLD.id;
4117
+ END`);
4118
+ });
4119
+ }
4120
+ finally {
4121
+ this.raw.exec("PRAGMA foreign_keys = ON");
4122
+ }
4123
+ }
4124
+ this.transaction(() => {
4125
+ this.run("INSERT OR IGNORE INTO schema_migrations (version, applied_at) VALUES (111, ?)", new Date().toISOString());
4126
+ });
4127
+ const integrity = this.all("PRAGMA integrity_check");
4128
+ if (integrity.some((row) => row.integrity_check !== "ok")) {
4129
+ throw new Error(`数据库完整性检查失败:${integrity.map((row) => row.integrity_check).join(";")}`);
4130
+ }
4131
+ const foreignKeys = this.all("PRAGMA foreign_key_check");
4132
+ if (foreignKeys.length > 0)
4133
+ throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4134
+ }
4135
+ if (!applied.has(112)) {
4136
+ this.transaction(() => {
4137
+ const columns = new Set(this.all("PRAGMA table_info(user_api_keys)").map((row) => String(row.name)));
4138
+ if (!columns.has("key_encrypted")) {
4139
+ this.run("ALTER TABLE user_api_keys ADD COLUMN key_encrypted TEXT");
4140
+ }
4141
+ if (!columns.has("key_iv")) {
4142
+ this.run("ALTER TABLE user_api_keys ADD COLUMN key_iv TEXT");
4143
+ }
4144
+ if (!columns.has("key_tag")) {
4145
+ this.run("ALTER TABLE user_api_keys ADD COLUMN key_tag TEXT");
4146
+ }
4147
+ this.run("INSERT OR IGNORE INTO schema_migrations (version, applied_at) VALUES (112, ?)", new Date().toISOString());
4148
+ });
4149
+ const integrity = this.all("PRAGMA integrity_check");
4150
+ if (integrity.some((row) => row.integrity_check !== "ok")) {
4151
+ throw new Error(`数据库完整性检查失败:${integrity.map((row) => row.integrity_check).join(";")}`);
4152
+ }
4153
+ const foreignKeys = this.all("PRAGMA foreign_key_check");
4154
+ if (foreignKeys.length > 0)
4155
+ throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4156
+ }
4157
+ const characterFavoritePresent = this.all("PRAGMA table_info(characters)").some((row) => String(row.name) === "is_favorite");
4158
+ const characterFavoriteIndexPresent = this.all("PRAGMA index_list(characters)").some((row) => String(row.name) === "idx_characters_favorite");
4159
+ if (!applied.has(113) || !characterFavoritePresent || !characterFavoriteIndexPresent) {
4160
+ this.transaction(() => {
4161
+ const columns = new Set(this.all("PRAGMA table_info(characters)").map((row) => String(row.name)));
4162
+ if (!columns.has("is_favorite")) {
4163
+ this.run("ALTER TABLE characters ADD COLUMN is_favorite INTEGER NOT NULL DEFAULT 0 CHECK(is_favorite IN (0, 1))");
4164
+ }
4165
+ this.run("CREATE INDEX IF NOT EXISTS idx_characters_favorite ON characters(work_id, is_favorite DESC, name)");
4166
+ this.run("INSERT OR IGNORE INTO schema_migrations (version, applied_at) VALUES (113, ?)", new Date().toISOString());
4167
+ });
4168
+ const integrity = this.all("PRAGMA integrity_check");
4169
+ if (integrity.some((row) => row.integrity_check !== "ok")) {
4170
+ throw new Error(`数据库完整性检查失败:${integrity.map((row) => row.integrity_check).join(";")}`);
4171
+ }
4172
+ const foreignKeys = this.all("PRAGMA foreign_key_check");
4173
+ if (foreignKeys.length > 0)
4174
+ throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4175
+ }
4176
+ const favoriteRecordTables = ["drafts", "settings", "organizations"];
4177
+ const favoriteRecordColumnsPresent = favoriteRecordTables.every((table) => (this.all(`PRAGMA table_info(${table})`).some((row) => String(row.name) === "is_favorite")));
4178
+ const favoriteRecordIndexes = [
4179
+ ["drafts", "idx_drafts_favorite"],
4180
+ ["settings", "idx_settings_favorite"],
4181
+ ["organizations", "idx_organizations_favorite"]
4182
+ ];
4183
+ const favoriteRecordIndexesPresent = favoriteRecordIndexes.every(([table, index]) => (this.all(`PRAGMA index_list(${table})`).some((row) => String(row.name) === index)));
4184
+ if (!applied.has(114) || !favoriteRecordColumnsPresent || !favoriteRecordIndexesPresent) {
4185
+ this.transaction(() => {
4186
+ for (const table of favoriteRecordTables) {
4187
+ const columns = new Set(this.all(`PRAGMA table_info(${table})`).map((row) => String(row.name)));
4188
+ if (!columns.has("is_favorite")) {
4189
+ this.run(`ALTER TABLE ${table} ADD COLUMN is_favorite INTEGER NOT NULL DEFAULT 0 CHECK(is_favorite IN (0, 1))`);
4190
+ }
4191
+ }
4192
+ this.run("CREATE INDEX IF NOT EXISTS idx_drafts_favorite ON drafts(work_id, is_favorite DESC, updated_at DESC, title)");
4193
+ this.run("CREATE INDEX IF NOT EXISTS idx_settings_favorite ON settings(work_id, is_favorite DESC, locked DESC, category, title)");
4194
+ this.run("CREATE INDEX IF NOT EXISTS idx_organizations_favorite ON organizations(work_id, is_favorite DESC, name)");
4195
+ this.run("INSERT OR IGNORE INTO schema_migrations (version, applied_at) VALUES (114, ?)", new Date().toISOString());
4196
+ });
4197
+ const integrity = this.all("PRAGMA integrity_check");
4198
+ if (integrity.some((row) => row.integrity_check !== "ok")) {
4199
+ throw new Error(`数据库完整性检查失败:${integrity.map((row) => row.integrity_check).join(";")}`);
4200
+ }
4201
+ const foreignKeys = this.all("PRAGMA foreign_key_check");
4202
+ if (foreignKeys.length > 0)
4203
+ throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4204
+ }
4205
+ const scenePinPresent = this.all("PRAGMA table_info(ai_conversations)").some((row) => String(row.name) === "scene_pin_json");
4206
+ if (!applied.has(115) || !scenePinPresent) {
4207
+ this.transaction(() => {
4208
+ const columns = new Set(this.all("PRAGMA table_info(ai_conversations)").map((row) => String(row.name)));
4209
+ if (!columns.has("scene_pin_json")) {
4210
+ this.run("ALTER TABLE ai_conversations ADD COLUMN scene_pin_json TEXT NOT NULL DEFAULT '{}'");
4211
+ }
4212
+ this.run("INSERT OR IGNORE INTO schema_migrations (version, applied_at) VALUES (115, ?)", new Date().toISOString());
4213
+ });
4214
+ const integrity = this.all("PRAGMA integrity_check");
4215
+ if (integrity.some((row) => row.integrity_check !== "ok")) {
4216
+ throw new Error(`数据库完整性检查失败:${integrity.map((row) => row.integrity_check).join(";")}`);
4217
+ }
4218
+ const foreignKeys = this.all("PRAGMA foreign_key_check");
4219
+ if (foreignKeys.length > 0)
4220
+ throw new Error(`数据库外键检查失败:发现 ${foreignKeys.length} 条异常记录`);
4221
+ }
4074
4222
  }
4075
4223
  normalizeCharacterName(value) {
4076
4224
  return value.normalize("NFKC").trim().replace(/\s+/gu, " ").toLocaleLowerCase("zh-CN");