Files
ST-Amily2-Chat-Optimisation/assets/Amily2-optimization.html
Jenkins CI 0d7e3b799e release: v2.2.6 [2026-06-13 01:02:05]
### 新功能
- **翰林院向量化质量升级**:
  - **边界感知切块**:替换四个来源(聊天记录/小说/世界书/手动)的纯字符硬切——优先在段落边界断开,其次句末标点(含中文引号闭合),极端长串才硬切;句子/对话不再被拦腰截断,embedding 质量同步受益。仅影响新录入,已有向量无需重建
  - **注入时序重排**:检索结果注入提示词前按时序重排(聊天记录按楼层、小说按卷/章/节——中文数字章节号可解析),rerank 只决定"选哪些块",不再决定呈现顺序;修复"不打不相识的剧情之后紧跟关系亲密"这类因按相关度排序导致的认知时间错乱
  - **断层提示**:聊天记录相邻块楼层跳跃时自动插入"与上文相隔约 N 楼,并非连续发生"提示行,消除中间剧情缺失造成的割裂感
  - **时间标识**:新录入的聊天记录块在来源标识中带上消息发送时间(ST 向量存储不持久化元数据,时间必须写入块文本才能在检索后取回;旧格式块兼容解析)
- **记忆块工作流(memory-blocks)**:剧情优化新增"自定义记忆块"体系——占位符驱动的并发工作流框架
  - 在剧情优化面板「匹配替换 (sulv)」下方可增删自定义块:每个块定义一个占位符,执行剧情优化时主/拦截提示词中的占位符会被块的产出替换
  - **静态块**:直接输出固定内容;**AI 调用块**:用所选 API 功能槽独立请求一次,把回复(或其中指定 `<标签>` 的内容)作为替换值
  - 原有 sulv1-4 速率占位符迁入同一框架,行为与旧版逐字节一致
  - 块定义为纯 JSON、随设置持久化,为后续导入导出与战斗系统接入预留扩展点
  - 框架层新增**顺序拼接式 Chain**(`composeChain`):与占位符替换并列的第二种组合范式——同链的块并发执行后按 `order` 排序、以 `separator` 拼接并可选 `header/footer` 包裹,产出一个完整注入块;为记忆注入合成块与战斗系统"底部战报块"预留的承载结构,本版本暂无 UI 入口
- **API 连接配置**:
  - 角色世界书(cwb)与一键生卡(autoCharCard)纳入旧配置自动迁移:老用户首次加载会把旧 URL / Key / 模型自动迁移为连接配置并分配槽位(一键生卡仅在规划者与执行者配置一致或规划者为空时迁移,避免悄悄改变行为)
  - **profile 已分配时参数控件 informational 化**:主面板 / 并发剧情优化 / 角色世界书 / 术语表的温度、maxTokens 控件在槽位分配 profile 后自动禁用并显示"由连接配置控制"提示,消除"改了没效果"的用户陷阱
  - **profile 状态卡新增"本设备无 Key"警示**:API Key 仅保存在最初填写它的设备/浏览器上(安全设计,不随云端设置同步),换设备后状态卡会直接亮出警示徽标,不必等到调用报错才发现
### 修复
- **独立聊天记忆从摆设变真功能(原作遗留坑)**:此前向量数据"随卡不随聊天"——开启"独立聊天记忆"后录入仍存进角色库、查询却去查一个从未被写入过的聊天集合、计数恒为 0,整体静默失效。现已重构为聊天级分桶:
  - 独立模式下,聊天记录类向量按当前聊天隔离存储与检索,同一张卡开多个聊天(不同剧情线)的记忆互不污染
  - 小说 / 世界书 / 手动录入属于"知识",仍随角色卡跨聊天共享;全局库不受影响
  - 知识管理列表为聊天专属库显示"聊天级"徽标;聊天级库禁止移动到全局
  - 统一模式(默认关闭独立记忆)的存量数据与行为完全不变
  - 已知限制:聊天专属记忆跟随聊天文件,重命名聊天文件会使其失联(与 ST 官方向量扩展同等限制)
- **超级排序截断顺序修正**:开启"超级排序"时,时序重排发生在 top_n 截断之前,导致保留的是"时序最早"而非"最相关"的块,检索结果长期偏向最旧的聊天记录。现改为先按相关度截取 top_n、再做时序排序
- **翰林院向量化失败("向量化块数量不识别"反馈)**:
  - 一次性清洗 profile-sync 历史污染:`retrieval/rerank.apiKey` 中的掩码占位符在持久层根治(此前仅读取侧防御);`apiEndpoint` / `rerank.apiMode` 的非法值(如被旧版写入的空字符串)归一化为 `custom`
  - 修复 `apiEndpoint` 为空/非法时请求被硬定向到 `api.openai.com`、无视用户自定义 URL 的问题(CSP 拦截 / 401 的元凶)
  - 修复**本地代理(LM Studio/Ollama)模式**自始就缺少 URL 分支、同样被错误定向到 openai.com 的问题
  - API 模式下拉补全 `OpenAI 官方` / `Azure` 选项;默认 API 模式改为 `custom`(与默认 URL 配套),新用户不再因选项缺失导致首次保存写入空值
  - profile-sync 给下拉框赋不存在选项值的污染源头修复(影响所有模块面板,不止翰林院)
- **Rerank "API Key 未提供"报错升级**:当原因是"连接配置在本设备没有可用 Key"时,报错会直接说明 Key 的设备本地性并指引到 API 连接配置重新填写(向量化 Google 直连、获取模型列表同步处理)
- **旧配置迁移**:一键生卡迁移时排除掩码占位符,避免把历史污染的假 Key 迁入新连接配置
- **超级记忆稳定性专项**(针对"工作不大稳定"反馈,4 处根因一次修复):
  - **切聊天竞态污染**:CHAT_CHANGED 时超级记忆立即全量同步,而表格系统延迟 100ms 才加载新聊天的表格,导致【旧聊天】的表格内容被写进【新角色】的记忆世界书;两边表名不同时旧表条目无 GC 兜底会**永久残留**("记忆串台"元凶)。现 CHAT_CHANGED 只确保世界书存在,新状态同步交由 `loadTables()` 完成后的自动推送,单次且时序正确
  - **死代码双轨存储拆除**:`saveStateToMetadata` / `tryRestoreStateFromMetadata` 把表格状态写到 `msg.metadata`——该字段非 ST 持久化位(同 v2.2.5 二次填表修过的坑),写入即蒸发、恢复永远为空,且每次同步还白调一次 `saveChat()`。整条链路删除,表格状态唯一信源为表格系统的 `msg.extra.amily2_tables_data`
  - **`awaitSync()` 穿透**:同步队列正忙时 `pushUpdate` 会用一个立即 resolve 的空 Promise 覆盖 `_syncPromise`,Pipeline Stage 4 等待形同虚设、后续阶段在同步未完成时被放行。现忙时不覆盖,正在运行的 drain 循环自然吃掉新入队项
  - **开关打开不生效**:启动时若总开关为关,初始化早退且不注册监听器;此后在 UI 勾选开关只写设置,超级记忆直到刷新页面前都是死的。现勾选即触发初始化(幂等)
  - 附带:`forceSyncAll` 的表格角色推断改为复用 `events-schema.inferTableRole`,消除两处重复逻辑漂移风险;每次切聊天的双倍全量同步(restore 路径一次 + 显式一次)随死代码移除归一
### 重构
- 表格核心 `manager.js` 瘦身(约 1050 → 600 行):19 个 UI 突变操作拆分至 `actions/ui-mutations.js`,SuperMemory 事件分发拆分至 `events-dispatch.js`;全部经 re-export 保持兼容,外部调用路径零改动
- 角色世界书最后 2 处散乱的厂商 URL 判断迁移至 `detectVendor` 统一入口,业务路径上不再有硬编码的 URL substring 判断
2026-06-13 01:02:05 +08:00

264 lines
16 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<div class="amily2-header">
<div class="additional-features-title">
<i class="fas fa-feather-alt"></i> 记忆管理
</div>
<button id="amily2_back_to_main_from_optimization" class="menu_button secondary small_button interactable">
返回主殿 <i class="fas fa-arrow-right"></i>
</button>
</div>
<hr class="header-divider" style="margin-top: 5px; margin-bottom: 10px;">
<fieldset class="settings-group">
<legend><i class="fas fa-cogs"></i> 通用设置</legend>
<div class="control-block-with-switch">
<label for="amily2_opt_enabled"><strong>记忆管理开关</strong></label>
<label class="toggle-switch">
<input id="amily2_opt_enabled" type="checkbox" />
<span class="slider"></span>
</label>
</div>
<div class="control-block-with-switch">
<label for="amily2_opt_ejs_enabled">EJS 预处理 <small style="color: #ffc107;">功能友情提供Ducker</small></label>
<label class="toggle-switch">
<input id="amily2_opt_ejs_enabled" type="checkbox" />
<span class="slider"></span>
</label>
</div>
</fieldset>
<div class="sinan-navigation-deck">
<button class="sinan-nav-item active" data-tab="api-settings"><i class="fas fa-bolt"></i> API 设置</button>
<button class="sinan-nav-item" data-tab="prompt-settings"><i class="fas fa-edit"></i> 提示词指令</button>
<button class="sinan-nav-item" data-tab="context-settings"><i class="fas fa-book-open"></i> 上下文设置</button>
</div>
<div class="sinan-content-wrapper">
<!-- API Settings Tab -->
<div id="sinan-api-settings-tab" class="sinan-tab-pane active">
<fieldset class="settings-group">
<legend>剧情优化 API</legend>
<p class="notes" style="margin: 0;">
剧情优化所用的连接配置统一在
<strong>API 连接配置 → 功能分配 → 剧情优化 / JQYH</strong>
中指定,无需在此单独填写。
</p>
</fieldset>
<fieldset class="settings-group">
<legend>并发 API (第二个LLM)</legend>
<div class="control-block-with-switch">
<label for="amily2_plotOpt_concurrentEnabled"><strong>启用并发调用</strong></label>
<label class="toggle-switch">
<input id="amily2_plotOpt_concurrentEnabled" type="checkbox" />
<span class="slider"></span>
</label>
</div>
<div id="amily2_concurrent_content" style="display: none;" class="inline-settings-grid">
<label for="amily2_plotOpt_concurrentApiProvider">API 模式</label>
<select id="amily2_plotOpt_concurrentApiProvider" class="text_pole">
<option value="openai_test">全兼容模式</option>
<option value="openai">OpenAI 兼容</option>
</select>
<label for="amily2_plotOpt_concurrentApiUrl">API URL</label>
<input type="text" id="amily2_plotOpt_concurrentApiUrl" class="text_pole" placeholder="例如: https://api.openai.com/v1">
<label for="amily2_plotOpt_concurrentApiKey">API Key</label>
<input type="password" id="amily2_plotOpt_concurrentApiKey" class="text_pole" placeholder="请输入您的 API Key">
<label for="amily2_plotOpt_concurrentModel">模型</label>
<div class="amily2_opt_preset_selector_wrapper">
<input type="text" id="amily2_plotOpt_concurrentModel" class="text_pole" placeholder="请先获取模型列表或手动输入">
<select id="amily2_plotOpt_concurrentModel_select" class="text_pole" style="display: none;"></select>
</div>
<label for="amily2_plotOpt_concurrentMaxTokens">最大 Tokens: <span id="amily2_plotOpt_concurrentMaxTokens_value">8100</span></label>
<input type="number" class="text_pole" id="amily2_plotOpt_concurrentMaxTokens" min="100" max="100000" value="8100">
<div class="jqyh-button-row" style="grid-column: 1 / -1;">
<button id="amily2_plotOpt_concurrent_fetch_models" class="menu_button secondary" title="获取模型列表"><i class="fas fa-sync-alt"></i> 获取模型</button>
<button id="amily2_plotOpt_concurrent_test_connection" class="menu_button primary"><i class="fas fa-plug"></i> 测试连接</button>
</div>
</div>
</fieldset>
<fieldset class="settings-group">
<legend>并发 API 世界书</legend>
<div class="control-block-with-switch">
<label for="amily2_plotOpt_concurrentWorldbookEnabled">启用世界书</label>
<label class="toggle-switch">
<input id="amily2_plotOpt_concurrentWorldbookEnabled" type="checkbox" />
<span class="slider"></span>
</label>
</div>
<div id="amily2_concurrent_worldbook_content" style="display: none;">
<div class="control-block-with-switch">
<label>世界书来源</label>
<div class="radio-group">
<input type="radio" id="amily2_plotOpt_concurrentWorldbook_source_character" name="amily2_plotOpt_concurrentWorldbook_source" value="character" checked>
<label for="amily2_plotOpt_concurrentWorldbook_source_character">角色</label>
<input type="radio" id="amily2_plotOpt_concurrentWorldbook_source_manual" name="amily2_plotOpt_concurrentWorldbook_source" value="manual">
<label for="amily2_plotOpt_concurrentWorldbook_source_manual">自定</label>
</div>
</div>
<div id="amily2_plotOpt_concurrent_worldbook_select_wrapper" style="display: none;">
<div class="worldbook-column">
<div class="amily2_opt_label_with_button_wrapper">
<label>选择世界书</label>
<button id="amily2_plotOpt_concurrent_refresh_worldbooks" class="menu_button" title="刷新世界书列表"><i class="fa-solid fa-sync"></i></button>
</div>
<div id="amily2_plotOpt_concurrent_worldbook_checkbox_list" class="scrollable-container"></div>
</div>
</div>
<div class="inline-settings-grid">
<label for="amily2_plotOpt_concurrentWorldbookCharLimit">世界书最大字符数: <span id="amily2_plotOpt_concurrentWorldbookCharLimit_value">60000</span></label>
<input type="number" class="text_pole" id="amily2_plotOpt_concurrentWorldbookCharLimit" min="1000" max="200000" value="60000">
</div>
</div>
</fieldset>
</div>
<!-- Prompt Settings Tab -->
<div id="sinan-prompt-settings-tab" class="sinan-tab-pane">
<fieldset class="settings-group">
<legend>并发API提示词</legend>
<div class="unified-prompt-editor">
<label for="amily2_concurrent_prompt_selector">选择编辑的提示词:</label>
<select id="amily2_concurrent_prompt_selector" class="text_pole">
<option value="main">主系统提示词 (并发)</option>
<option value="system">拦截任务详细指令 (并发)</option>
</select>
<textarea id="amily2_concurrent_prompt_editor" class="text_pole" rows="6"></textarea>
<div class="prompt-editor-buttons">
<button id="amily2_opt_reset_concurrent_prompt" class="menu_button secondary">恢复当前并发提示词为默认</button>
</div>
</div>
</fieldset>
<fieldset class="settings-group">
<legend>提示词管理</legend>
<div class="inline-settings-grid">
<label for="amily2_opt_prompt_preset_select">加载预设</label>
<div class="amily2_opt_preset_selector_wrapper">
<select id="amily2_opt_prompt_preset_select" class="text_pole">
<option value="">-- 选择一个预设 --</option>
</select>
<button id="amily2_opt_import_prompt_presets" class="menu_button" title="导入预设"><i class="fa-solid fa-download"></i></button>
<button id="amily2_opt_export_prompt_presets" class="menu_button" title="导出预设"><i class="fa-solid fa-upload"></i></button>
<button id="amily2_opt_save_prompt_preset" class="menu_button" title="保存当前提示词为预设"><i class="fa-solid fa-save"></i></button>
<button id="amily2_opt_delete_prompt_preset" class="menu_button" title="删除当前选中的预设" style="display: none;"><i class="fa-solid fa-trash-alt"></i></button>
<input type="file" id="amily2_opt_preset_file_input" style="display: none;" accept=".json">
</div>
</div>
</fieldset>
<fieldset class="settings-group">
<legend>指令编辑</legend>
<div class="unified-prompt-editor">
<label for="amily2_opt_prompt_selector">选择编辑的提示词:</label>
<select id="amily2_opt_prompt_selector" class="text_pole">
<option value="main">主系统提示词 (主LLM)</option>
<option value="system">拦截任务详细指令 (主LLM)</option>
<option value="final_system">最终注入指令</option>
</select>
<textarea id="amily2_opt_prompt_editor" class="text_pole" rows="8"></textarea>
<div class="prompt-editor-buttons">
<button id="amily2_opt_reset_main_prompt" class="menu_button secondary">恢复主提示词</button>
<button id="amily2_opt_reset_system_prompt" class="menu_button secondary">恢复拦截任务</button>
<button id="amily2_opt_reset_final_system_directive" class="menu_button secondary">恢复注入指令</button>
</div>
</div>
</fieldset>
<fieldset class="settings-group">
<legend>匹配替换 (sulv)</legend>
<div class="inline-settings-grid">
<label for="amily2_opt_rate_main">主线剧情 (sulv1)</label>
<input id="amily2_opt_rate_main" type="number" class="text_pole" step="0.05" value="1.0">
<label for="amily2_opt_rate_personal">个人线 (sulv2)</label>
<input id="amily2_opt_rate_personal" type="number" class="text_pole" step="0.05" value="1.0">
<label for="amily2_opt_rate_erotic">留空 (sulv3)</label>
<input id="amily2_opt_rate_erotic" type="number" class="text_pole" step="0.05" value="1.0">
<label for="amily2_opt_rate_cuckold">留空 (sulv4)</label>
<input id="amily2_opt_rate_cuckold" type="number" class="text_pole" step="0.05" value="1.0">
</div>
</fieldset>
<fieldset class="settings-group">
<legend>自定义记忆块</legend>
<div style="font-size: 0.85em; opacity: 0.75; margin-bottom: 8px;">
每个块定义一个占位符:执行剧情优化时,主/拦截提示词中出现的占位符会被块的产出替换。
静态块直接输出固定内容AI 调用块会用所选 API 槽独立请求一次,把回复作为替换值。
</div>
<div id="amily2_opt_custom_blocks_list"></div>
<button id="amily2_opt_add_custom_block" class="menu_button" style="margin-top: 8px;">
<i class="fa-solid fa-plus"></i> 新增记忆块
</button>
</fieldset>
</div>
<!-- Context Settings Tab -->
<div id="sinan-context-settings-tab" class="sinan-tab-pane">
<fieldset class="settings-group">
<legend>内容源</legend>
<div class="control-block-with-switch">
<label for="amily2_opt_worldbook_enabled">启用世界书</label>
<label class="toggle-switch">
<input id="amily2_opt_worldbook_enabled" type="checkbox" />
<span class="slider"></span>
</label>
</div>
<div class="control-block-with-switch">
<label for="amily2_opt_table_enabled">表格发送目标</label>
<select id="amily2_opt_table_enabled" class="text_pole">
<option value="disabled">不发送</option>
<option value="main">发送给主API</option>
<option value="concurrent">发送给并发API</option>
</select>
</div>
</fieldset>
<fieldset class="settings-group">
<legend>上下文参数</legend>
<div class="inline-settings-grid">
<label for="amily2_opt_context_limit">上下文条数: <span id="amily2_opt_context_limit_value">10</span></label>
<input type="number" class="text_pole" id="amily2_opt_context_limit" min="1" max="50" value="10">
<label for="amily2_opt_worldbook_char_limit">世界书最大字符数: <span id="amily2_opt_worldbook_char_limit_value">60000</span></label>
<input type="number" class="text_pole" id="amily2_opt_worldbook_char_limit" min="1000" max="200000" value="60000">
</div>
</fieldset>
<fieldset class="settings-group">
<legend>世界书管理</legend>
<div class="control-block-with-switch">
<label for="amily2_opt_new_memory_logic_enabled">启用新记忆逻辑</label>
<label class="toggle-switch">
<input id="amily2_opt_new_memory_logic_enabled" type="checkbox" />
<span class="slider"></span>
</label>
</div>
<div class="control-block-with-switch">
<label>世界书来源</label>
<div class="radio-group">
<input type="radio" id="amily2_opt_worldbook_source_character" name="amily2_opt_worldbook_source" value="character" checked>
<label for="amily2_opt_worldbook_source_character">角色</label>
<input type="radio" id="amily2_opt_worldbook_source_manual" name="amily2_opt_worldbook_source" value="manual">
<label for="amily2_opt_worldbook_source_manual">自定</label>
</div>
</div>
<div id="amily2_opt_worldbook_select_wrapper" style="display: none;">
<div class="worldbook-column">
<div class="amily2_opt_label_with_button_wrapper">
<label>选择世界书</label>
<button id="amily2_opt_refresh_worldbooks" class="menu_button" title="刷新世界书列表"><i class="fa-solid fa-sync"></i></button>
</div>
<div id="amily2_opt_worldbook_checkbox_list" class="scrollable-container"></div>
</div>
</div>
<div class="worldbook-column">
<div class="amily2_opt_label_with_controls_wrapper">
<label>启用的世界书条目</label>
<div id="amily2_opt_worldbook_entry_controls">
<span id="amily2_opt_worldbook_entry_count"></span>
<button id="amily2_opt_worldbook_entry_select_all" class="menu_button">全选</button>
<button id="amily2_opt_worldbook_entry_deselect_all" class="menu_button">不选</button>
</div>
</div>
<div id="amily2_opt_worldbook_entry_list_container" class="scrollable-container"></div>
</div>
</fieldset>
</div>
</div>
<div class="amily2_opt_footer">
</div>