Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(patch): apply renderer SlotOutlet patch to the active dsh core

hairyfd320d4d1a8e5
diff --git a/src-tauri/src/service/workflow/renderer_patch.rs b/src-tauri/src/service/workflow/renderer_patch.rs
index c32dc9a3..0518e7d7 100644
--- a/src-tauri/src/service/workflow/renderer_patch.rs
+++ b/src-tauri/src/service/workflow/renderer_patch.rs
@@ -3,13 +3,23 @@
//! dsh-tauri-ui 插件的设置侧边栏依赖 `<SlotOutlet>`(任意槽渲染入口)。官方
//! renderer 的 `lib/client.js` 只导出 `{apply, inject}`——SlotOutlet 实现完整
//! 却未公开,上游也没提供跨键渲染 API(插件只能 shadow 官方条目再自己渲染他人
-//! 声明的槽,而这必须经过 SlotOutlet)。补丁在文件末尾(`\treturn module.exports;`
-//! 前)插入一行 `\texports.SlotOutlet = SlotOutlet;`,与开发期手工补丁逐字节一致。
+//! 声明的槽,而这必须经过 SlotOutlet)。补丁在文件末尾的 `return module.exports;`
+//! 前插入一行 `exports.SlotOutlet = SlotOutlet;`,与开发期手工补丁逐字节一致。
+//!
+//! 目标选择:**活动核心**,而不是永远指向预打包核心。桌面端支持「本地核心」
+//! (用户在 PATH / 全局安装的 dsh,`CoreSource::Local`)与「预打包核心」
+//! (`CoreSource::App`)两种来源,运行时 web 应用通过 `$DSH_HOME/profiles/<档案>/
+//! node_modules/@deepseek-ai/dsh-client-ui-renderer`(对活动核心 renderer 的链接)
+//! 解析该模块。旧实现恒用 [`config::get_dsh_install_path`](预打包目录),本地核心
+//! 激活时会补到一个**永远不会被加载**的文件上——日志显示已注入,web 应用却仍拿到
+//! 未打补丁的 renderer,dsh-tauri-ui 于是降级为官方设置 dialog。因此这里按
+//! [`crate::service::core::active_source`] 选目录:本地核心用其包目录,预打包用
+//! 桌面端目录;两者回退逻辑与 [`crate::service::core::active_dsh_binary`] 一致。
//!
//! 幂等与容错:
//! - 目标已含 `exports.SlotOutlet` 即跳过——上游将来自己导出(或已提的一行 PR
//! 合入)后本模块自动退休,零维护;
-//! - 文件缺失或锚点(`\treturn module.exports;`)因上游布局变更而找不到 → 跳过
+//! - 文件缺失或锚点(`return module.exports;`)因上游布局变更而找不到 → 跳过
//! 并告警,不阻断启动——插件侧另有降级(SlotOutlet 不可用时保留官方设置 dialog,
//! 绝不白屏)。
//!
@@ -20,14 +30,12 @@ use std::fs;
use std::path::PathBuf;
use crate::config;
+use crate::service::core::{active_source, local_core_package_dir, CoreSource};
-/// 唯一导出锚点:renderer 打包产物模块工厂末尾的返回语句(tab 缩进)。
-const ANCHOR: &str = "\treturn module.exports;";
-
-/// 插入的导出行(与锚点同 tab 缩进)。
-const PATCH_LINE: &str = "\texports.SlotOutlet = SlotOutlet;\n";
+/// 导出锚点的关键字(所在行的前导缩进随版本变化,不做硬编码)。
+const ANCHOR_KEYWORD: &str = "return module.exports;";
-/// 单个 renderer client.js 内容的补丁结果。
+/// 单条 renderer client.js 内容的补丁结果。
#[derive(Debug, PartialEq, Eq)]
enum PatchOutcome {
/// 已含 `exports.SlotOutlet`,无需补丁(本补丁已生效或上游官方导出)。
@@ -39,23 +47,43 @@ enum PatchOutcome {
}
/// 幂等补丁逻辑的纯函数部分(便于单测,不触碰文件系统)。
+///
+/// 定位 `return module.exports;` 所在行,按其**实际前导缩进**(tab/空格数随上游
+/// 打包产物变化)插入同缩进的导出行——旧实现硬编码单 tab 锚点,upstream 产物
+/// 用双 tab 时会在字符串内部命中、插出缩进错乱的坏行。
fn patch_source(source: &str) -> PatchOutcome {
if source.contains("exports.SlotOutlet") {
return PatchOutcome::AlreadyPatched;
}
- let Some(pos) = source.find(ANCHOR) else {
+ let Some((line_start, indent)) = locate_return_module_exports(source) else {
return PatchOutcome::AnchorMissing;
};
let mut patched = source.to_string();
- patched.insert_str(pos, PATCH_LINE);
+ patched.insert_str(line_start, &format!("{indent}exports.SlotOutlet = SlotOutlet;\n"));
PatchOutcome::Patched(patched)
}
+/// 定位 `return module.exports;` 所在行的行首字节偏移与其前导缩进。
+///
+/// 返回 `(行首偏移, 前导空白)`;行首到关键字之间必须是纯空白,否则不是锚行
+/// (防御上游后续把 `return module.exports;` 挪进表达式里)。
+fn locate_return_module_exports(source: &str) -> Option<(usize, &str)> {
+ let match_start = source.find(ANCHOR_KEYWORD)?;
+ let line_start = source[..match_start]
+ .rfind('\n')
+ .map(|i| i + 1)
+ .unwrap_or(0);
+ let indent = &source[line_start..match_start];
+ if !indent.chars().all(|c| c == '\t' || c == ' ') {
+ return None;
+ }
+ Some((line_start, indent))
+}
+
/// 对活动核心的 dsh-client-ui-renderer `lib/client.js` 应用补丁(幂等)。
/// 返回 Err 表示读/写失败;文件缺失、已打过、锚点变更均静默跳过(Ok)。
pub fn apply(app_handle: &tauri::AppHandle) -> Result<(), String> {
- let client_js: PathBuf = config::get_dsh_install_path(app_handle)
- .join("node_modules/@deepseek-ai/dsh-client-ui-renderer/lib/client.js");
+ let client_js: PathBuf = active_core_renderer_client_js(app_handle);
if !client_js.exists() {
log::info!(
"renderer client.js not found, skip SlotOutlet patch: {}",
@@ -71,7 +99,8 @@ pub fn apply(app_handle: &tauri::AppHandle) -> Result<(), String> {
}
PatchOutcome::AnchorMissing => {
log::warn!(
- "renderer client.js anchor missing, skip SlotOutlet patch — plugin degrades to official settings dialog"
+ "renderer client.js anchor missing, skip SlotOutlet patch — plugin degrades to official settings dialog: {}",
+ client_js.display()
);
}
PatchOutcome::Patched(patched) => {
@@ -84,21 +113,58 @@ pub fn apply(app_handle: &tauri::AppHandle) -> Result<(), String> {
Ok(())
}
+/// 活动核心安装目录:本地核心用其包目录(全局安装路径),预打包用桌面端目录。
+///
+/// 与 [`crate::service::core::active_dsh_binary`] 的取舍一致——本地核心解析在
+/// 调用瞬间失效时回退预打包目录,绝不让旧实现那样恒打在一个不加载的文件上。
+fn active_core_install_dir(app_handle: &tauri::AppHandle) -> PathBuf {
+ match active_source(app_handle) {
+ CoreSource::Local => local_core_package_dir(app_handle)
+ .unwrap_or_else(|| config::get_dsh_install_path(app_handle)),
+ CoreSource::App => config::get_dsh_install_path(app_handle),
+ }
+}
+
+/// 活动核心 renderer 的 `lib/client.js` 路径。
+fn active_core_renderer_client_js(app_handle: &tauri::AppHandle) -> PathBuf {
+ active_core_install_dir(app_handle)
+ .join("node_modules/@deepseek-ai/dsh-client-ui-renderer/lib/client.js")
+}
+
#[cfg(test)]
mod tests {
use super::*;
- /// 真实打包产物末尾的五行(与运行时 client.js L982-986 一致)。
+ /// 真实打包产物末尾的五行(单 tab 缩进;与旧实现测试锚点一致)。
const TAIL: &str = "\t\t//#endregion\n\t\texports.apply = apply;\n\t\texports.inject = inject;\n\treturn module.exports;\n\t}\n});\n";
+ /// 上游产物双 tab 缩进(回归:旧硬编码单 tab 锚点在此处命中坏位置)。
+ const TAIL_DOUBLE_TAB: &str = "\t\t//#endregion\n\t\texports.apply = apply;\n\t\texports.inject = inject;\n\t\treturn module.exports;\n\t}\n});\n";
+
#[test]
fn patch_inserts_export_before_return() {
match patch_source(TAIL) {
PatchOutcome::Patched(patched) => {
- assert!(patched.contains(PATCH_LINE.trim_end()));
- assert!(patched.starts_with(
- "\t\t//#endregion\n\t\texports.apply = apply;\n\t\texports.inject = inject;\n\texports.SlotOutlet = SlotOutlet;\n\treturn module.exports;\n"
+ assert!(patched.contains("exports.SlotOutlet = SlotOutlet;"));
+ assert!(patched.contains(
+ "\t\texports.inject = inject;\n\texports.SlotOutlet = SlotOutlet;\n\treturn module.exports;\n"
+ ));
+ }
+ other => panic!("expected Patched, got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn patch_matches_double_tab_indentation() {
+ // 回归:upstream 产物用双 tab 缩进,旧实现会命中 `\treturn` 子串、插出
+ // 缩进错乱的坏行。新实现按行首缩进插入,导出行与 `return` 对齐(双 tab)。
+ match patch_source(TAIL_DOUBLE_TAB) {
+ PatchOutcome::Patched(patched) => {
+ assert!(patched.contains(
+ "\t\texports.inject = inject;\n\t\texports.SlotOutlet = SlotOutlet;\n\t\treturn module.exports;\n"
));
+ // 不产生把 return 缩进削掉一格的坏行
+ assert!(!patched.contains("\n\texports.SlotOutlet = SlotOutlet;\n\treturn"));
}
other => panic!("expected Patched, got {other:?}"),
}
@@ -117,4 +183,4 @@ mod tests {
let altered = "\t\texports.apply = apply;\n\t\texports.inject = inject;\n";
assert_eq!(patch_source(altered), PatchOutcome::AnchorMissing);
}
-}
\ No newline at end of file
+}