DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
docs: add built-in (internal) plugin guide
hairyf4cb3ffda366b
diff --git a/docs/BUILTIN_PLUGINS.md b/docs/BUILTIN_PLUGINS.mdnew file mode 100644index 00000000..9156f19a--- /dev/null+++ b/docs/BUILTIN_PLUGINS.md@@ -0,0 +1,88 @@+# Built-in (Internal) Plugins++Built-in plugins(内置插件)are plugins bundled with the installed desktop app and treated as part of the application itself. They are marked with `"internal": true` in `src-tauri/resources/preset-plugins.json`, fetched at build time by `scripts/prebuild.ts` into `src-tauri/resources/preset-plugins/<id>/`, shipped with the installer via `bundle.resources`, and auto-installed (and auto-healed) at service start by `src-tauri/src/service/plugin/internal.rs`.++Examples today: `dsh-tauri` and `dsh-tauri-ui`.++## Built-in vs. normal preset plugin++| | Normal preset plugin | Built-in (internal) plugin |+| --- | --- | --- |+| Declared in `preset-plugins.json` | Yes | Yes, with `"internal": true` |+| Source | npm / GitHub, installed at first-launch onboarding | Bundled in the installer, auto-installed at startup |+| Shown in the first-launch checklist | Yes (`recommended` / `fix` / `defaultChecked` chips) | No — they are mandatory (filtered out in `installed.rs`) |+| User can uninstall | Yes | Effectively no — restored on the next launch |+| Version source | Resolved at install time from the declared `spec` | Bundled artifact shipped with the app |++## How the mechanism works++**Build time** — `scripts/prebuild.ts` is run automatically by `pnpm build` (the `prebuild` script, which Tauri invokes as its `beforeBuildCommand` = `pnpm build`). For each `internal: true` entry it produces `src-tauri/resources/preset-plugins/<id>/`:++- `github:owner/repo` — `git clone --depth 1` → `pnpm install` → `pnpm run build` (if a `build` script exists) → copy the build output plus `package.json`.+- `name[@version]` (npm, incl. scoped `@scope/name`) — `pnpm add <spec> --ignore-scripts` in a temporary project → copy `node_modules/<name>/`.++**Runtime** — before the harness service launches, `service::plugin::internal::ensure` checks each internal plugin: ① it is declared in the current profile's `package.json` `dependencies`, ② the declared dependency value still points at the current bundled dir (`link:<abspath>`), ③ `node_modules/<package>` actually exists. If any check fails (missing / path changed / user removed it / `node_modules` wiped), it forces a reinstall through the normal install flow with a `link:` spec.++## Add a new built-in plugin++### 1. Declare it in `src-tauri/resources/preset-plugins.json`++Append an entry:++```json+{+ "id": "dsh-my-plugin",+ "spec": "github:you/dsh-my-plugin",+ "internal": true,+ "name": "DSH My Plugin",+ "description": "What the plugin does",+ "repoUrl": "https://github.com/you/dsh-my-plugin"+}+```++Field reference:++- `id` — unique preset id (the repo jump / lookup key; also the default npm package name used for install-state detection). Ids must be unique across the list (enforced by the `preset_json_ids_are_unique` unit test).+- `spec` — the source. Either `github:owner/repo` (source form) or a bare npm package name `name[@version]` (published form, skips the build). This is what `prebuild.ts` feeds to git/pnpm.+- `internal` — `true` marks it as a built-in plugin (drives build-time bundling and runtime self-heal). Defaults to `false`.+- `name`, `description`, `repoUrl` — display metadata. `repoUrl` is used for the repo-jump link in the UI.+- `package` — optional. When the real npm package name differs from `id` (typical for scoped `@scope/name`), declare it here; it is used for install-state detection and self-heal matching. Defaults to `id`.+- Chip flags `recommended` / `fix` / `defaultChecked` — not meaningful for internal plugins (they never appear in the checklist) but harmless to keep.++### 2. Bundle it at build time++No manual step. `pnpm tauri build` runs `pnpm build` → `pnpm prebuild` → `tsx scripts/prebuild.ts`, which reads the manifest, picks all `internal: true`, and produces `src-tauri/resources/preset-plugins/<id>/`. The build machine needs `git` and `pnpm` on PATH and network access to GitHub (for `github:` sources) and npm (for package-name sources).++The bundled directory is shipped with the installer via `bundle.resources` (`"resources": ["resources/**/*"]` in `src-tauri/tauri.conf.json`).++The plugin's `package.json` should declare a `files` whitelist so prebuild copies only the runtime-necessary files; when absent it copies the whole directory minus `node_modules` / `.git` / `.npmrc` etc. `package.json` is always copied last so it is guaranteed present (it is the package-name/entry source for `pnpm add link:<dir>`).++### 3. Runtime auto-install handles the rest++On startup, `service::plugin::internal::ensure` verifies the plugin is installed and that the installed spec points at the bundled dir; if not, it reinstalls automatically. No UI work and no onboarding checkbox — a user can never be left without a built-in plugin.++## Development (debug) iteration++For fast iteration in a debug build, set the `DEV_INTERNAL_PLUGINS_DIR` key in the repo-root `.env` (see `.env.example`) to a local directory holding the plugin sources. For each internal plugin the runtime looks up `<dir>/<id>`; if present, that local source dir becomes the install target (a pnpm directory junction), so editing the source and restarting the service hot-reloads the plugin — no sub-plugin git commit and no `prebuild`.++Rules:++- `.env` is gitignored and local-only; the key is only read in `debug_assertions` builds (release always uses the packaged dir).+- If the id is missing in `<dir>`, the plugin is skipped rather than falling back to the packaged dir, so the misconfiguration is noticed explicitly.+- Setting it empty or deleting the key disables the override (falls back to the packaged `resources/preset-plugins/<id>`).++## Gotchas++- **Why `link:` and not `file:`** — pnpm resolves `file:D:/...` (a Windows drive-absolute path) as a *relative* path and fails (`scandir <cwd>\D:\... ENOENT`), while `link:<abspath>` resolves the absolute path correctly and creates a directory junction. `bundled_dep_spec` also strips the Windows `\\?\` verbatim prefix via `dunce::simplified` so the junction is not corrupted; otherwise the self-heal reinstalls on every launch (an infinite loop). The spec is compared case-insensitively on Windows, tolerating `link:`/`file:` mix and trailing-slash differences.+- **Paths with spaces** — the install dir (e.g. `G:\Deepseek Harness Desktop\...`) often contains spaces; `dsh plugin add` passes the spec through a shell, so `install.rs` wraps such specs in embedded double quotes (`shell_quote_spec`). Don't remove that.+- **prebuild fails loudly** — `scripts/prebuild.ts` exits non-zero on any failure so the build breaks rather than shipping a broken internal plugin. If a bundled dir is still missing at runtime in a release build, the log emits `INTERNAL_PLUGIN_BUNDLE_MISSING`.+- **Build machine access** — prebuild needs network access to GitHub/npm and `git`/`pnpm` on PATH; it only uses Node built-ins (no new deps).+- **Keep ids unique** — the manifest unit tests require unique ids. Publishing and version-bumping the plugin itself happens in its own repo, outside this one.++## Reference++- `src-tauri/resources/preset-plugins.json` — the preset manifest you edit.+- `scripts/prebuild.ts` — build-time bundling (git / npm sources).+- `src-tauri/src/service/plugin/preset.rs` — manifest parsing, bundled-dir discovery, and the `link:` spec builder.+- `src-tauri/src/service/plugin/internal.rs` — runtime self-heal.+- `src-tauri/src/service/plugin/install.rs` — the install orchestration reused by the self-heal.diff --git a/docs/BUILTIN_PLUGINS.zh.md b/docs/BUILTIN_PLUGINS.zh.mdnew file mode 100644index 00000000..794f3a4a--- /dev/null+++ b/docs/BUILTIN_PLUGINS.zh.md@@ -0,0 +1,88 @@+# 内置插件(Internal Plugins)++内置插件(built-in plugin)是指随安装包分发、被视作应用本身一部分的插件。它们在 `src-tauri/resources/preset-plugins.json` 中标记 `"internal": true`,由 `scripts/prebuild.ts` 在构建期拉取到 `src-tauri/resources/preset-plugins/<id>/`,随 `bundle.resources` 一并打进安装包,并在服务启动时由 `src-tauri/src/service/plugin/internal.rs` 自动安装(并自愈)。++目前的内置插件:`dsh-tauri`、`dsh-tauri-ui`。++## 内置插件 vs. 普通预装插件++| | 普通预装插件 | 内置(internal)插件 |+| --- | --- | --- |+| 在 `preset-plugins.json` 声明 | 是 | 是,且带 `"internal": true` |+| 来源 | npm / GitHub,首次引导时安装 | 随安装包分发,启动时自动安装 |+| 出现在首次引导清单 | 是(`recommended` / `fix` / `defaultChecked` chip) | 否——它们“必装”,在 `installed.rs` 里被过滤掉 |+| 用户可卸载 | 可以 | 基本不行——下次启动自动恢复 |+| 版本来源 | 安装时按声明的 `spec` 解析 | 随安装包分发的捆绑产物 |++## 机制是怎么运作的++**构建期** — `scripts/prebuild.ts` 由 `pnpm build` 自动触发(`prebuild` 脚本,而 Tauri 的 `beforeBuildCommand` 恰好是 `pnpm build`)。对每个 `internal: true` 条目,它产出 `src-tauri/resources/preset-plugins/<id>/`:++- `github:owner/repo` — `git clone --depth 1` → `pnpm install` → `pnpm run build`(存在 `build` 脚本时)→ 拷贝构建产物与 `package.json`。+- `name[@version]`(npm 包名,含 scoped `@scope/name`)— 在临时工程里 `pnpm add <spec> --ignore-scripts` → 拷贝 `node_modules/<name>/`。++**运行期** — 在 harness 服务启动前,`service::plugin::internal::ensure` 逐个核对内部插件:① 它是否在当前档案的 `package.json` `dependencies` 中声明;② 声明值是否仍指向当前捆绑目录(`link:<绝对路径>`);③ `node_modules/<package>` 是否真实存在。任一不满足(缺失 / 路径变更 / 用户卸载 / node_modules 被清空)→ 走常规安装流程,用 `link:` 依赖强制重装。++## 添加一个新的内置插件++### 1. 在 `src-tauri/resources/preset-plugins.json` 中声明++追加一条:++```json+{+ "id": "dsh-my-plugin",+ "spec": "github:you/dsh-my-plugin",+ "internal": true,+ "name": "DSH My Plugin",+ "description": "插件做什么",+ "repoUrl": "https://github.com/you/dsh-my-plugin"+}+```++字段说明:++- `id` — 预设唯一 id(仓库跳转 / 查找键;也是未显式声明 `package` 时的默认包名,用于“已安装”检测)。清单内 id 必须唯一(由 `preset_json_ids_are_unique` 单测保证)。+- `spec` — 来源。要么 `github:owner/repo`(源码形态),要么裸 npm 包名 `name[@version]`(已发布产物形态,跳过构建)。这是 `prebuild.ts` 喂给 git/pnpm 的值。+- `internal` — `true` 标记为内置插件(驱动构建期打包与运行期自愈)。默认 `false`。+- `name`、`description`、`repoUrl` — 展示元数据;`repoUrl` 供界面“仓库跳转”链接使用。+- `package` — 可选。当真实 npm 包名与 `id` 不一致(常见于 scoped 包 `@scope/name`)时在这里声明;用于“已安装”检测与自愈对账。缺省回落 `id`。+- chip 标记 `recommended` / `fix` / `defaultChecked` — 对内置插件无意义(它们不进清单),保留也无妨。++### 2. 构建期打包++无需手工操作。`pnpm tauri build` 会执行 `pnpm build` → `pnpm prebuild` → `tsx scripts/prebuild.ts`,读取清单、筛出所有 `internal: true` 并产出 `src-tauri/resources/preset-plugins/<id>/`。构建机需要 PATH 上有 `git` 与 `pnpm`,并能访问 GitHub(`github:` 来源)与 npm registry(包名来源)。++捆绑目录通过 `bundle.resources` 随安装包分发(`src-tauri/tauri.conf.json` 中的 `"resources": ["resources/**/*"]`)。++插件的 `package.json` 最好声明 `files` 白名单,让 prebuild 只拷贝运行必需文件;未声明时则拷贝整目录但排除 `node_modules` / `.git` / `.npmrc` 等。`package.json` 恒在最后拷贝,保证一定存在(它是 `pnpm add link:<目录>` 的包名/入口来源)。++### 3. 运行期自动安装接管后续++启动时 `service::plugin::internal::ensure` 核对插件已安装且安装 spec 指向捆绑目录;不满足则自动重装。无需任何前端/引导改动——用户不可能缺内置插件。++## 开发(debug)迭代++想在 debug 构建里快速迭代,可在仓库根 `.env`(参考 `.env.example`)里设置 `DEV_INTERNAL_PLUGINS_DIR`,指向存放插件源码的本地目录。对每个内置插件,运行时会查找 `<dir>/<id>`;命中则以该本地源码目录为安装目标(pnpm 目录联接,改源码后重启服务即热更新)——无需提交子插件 git、无需跑 prebuild。++规则:++- `.env` 已被 `.gitignore` 忽略,仅本地生效;该键只在 `debug_assertions` 构建读取(release 恒用随包目录)。+- 若 `<dir>` 缺该 id,则跳过(不回落随包目录),让开发者显式感知配置错误。+- 置空或删除该键 = 关闭覆盖(回落随包分发的 `resources/preset-plugins/<id>`)。++## 常见坑++- **为什么用 `link:` 而不是 `file:`** — pnpm 会把 `file:D:/...`(Windows 盘符绝对路径)当**相对路径**解析而失败(`scandir <cwd>\D:\... ENOENT`),`link:<绝对路径>` 才正确按绝对路径解析并建立目录联接。`bundled_dep_spec` 还会用 `dunce::simplified` 归一化 Windows `\\?\` verbatim 前缀,避免生成坏联接;否则自愈每轮都重装(死循环)。该 spec 在 Windows 上大小写不敏感比对,并容忍 `link:`/`file:` 混写与尾部斜杠差异。+- **路径含空格** — 应用安装目录(如 `G:\Deepseek Harness Desktop\...`)常含空格;`dsh plugin add` 经 shell 传参,`install.rs` 用 `shell_quote_spec` 给这类 spec 加内嵌双引号,不要去掉。+- **prebuild 响亮失败** — `scripts/prebuild.ts` 任何失败都会以非零退出码终止构建,宁可不发也不要发出损坏的内置插件。若 release 运行期仍缺捆绑目录,日志会记 `INTERNAL_PLUGIN_BUNDLE_MISSING`。+- **构建机访问** — prebuild 需要访问 GitHub/npm,PATH 上要有 `git`/`pnpm`;它只用 Node 内置模块(零新增依赖)。+- **id 保持唯一** — 清单单测要求 id 唯一。插件自身的发布与版本号变更在其自己的仓库进行,不在本仓库内。++## 参考++- `src-tauri/resources/preset-plugins.json` — 你要编辑的预设清单。+- `scripts/prebuild.ts` — 构建期打包(git / npm 两种来源)。+- `src-tauri/src/service/plugin/preset.rs` — 清单解析、捆绑目录发现、`link:` spec 构造。+- `src-tauri/src/service/plugin/internal.rs` — 运行期自愈。+- `src-tauri/src/service/plugin/install.rs` — 自愈复用的安装编排。diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.mdindex 02764409..5e729b1c 100644--- a/docs/DEVELOPMENT.md+++ b/docs/DEVELOPMENT.md@@ -35,6 +35,8 @@ cargo testFor Developer ID signing, notarization, and the required GitHub Actions secrets, see [macOS signing and notarization](./MACOS_SIGNING.md).+To add a new built-in (internal) plugin bundled with the app, see [Built-in (Internal) Plugins](./BUILTIN_PLUGINS.md).+## Tips- Debug mode serves on port **3081**, release builds on **3080** — the two never clash, so you can run an installed copy and a dev build side by side.diff --git a/docs/DEVELOPMENT.zh.md b/docs/DEVELOPMENT.zh.mdindex 3272d81d..83a0db34 100644--- a/docs/DEVELOPMENT.zh.md+++ b/docs/DEVELOPMENT.zh.md@@ -35,6 +35,8 @@ cargo testmacOS 的 Developer ID 签名、公证与 GitHub Actions Secrets 配置见 [macOS 签名与公证](./MACOS_SIGNING.zh.md)。+若要新增一个随安装包分发、内置在应用里的插件,请参阅 [内置插件(Internal Plugins)](./BUILTIN_PLUGINS.zh.md)。+## 小贴士- 调试模式使用 **3081** 端口,正式版使用 **3080** —— 两者互不冲突,可以同时运行已安装版本与开发构建。diff --git a/src-tauri/resources/README.md b/src-tauri/resources/README.mdindex 8fea3be3..9ff3a8b0 100644--- a/src-tauri/resources/README.md+++ b/src-tauri/resources/README.md@@ -71,4 +71,14 @@ that adds one entry to the JSON array:`id` must be unique across the file. The plugin itself is **not** vendored intothis repository — it is installed on the user's machine from `spec` at setup-time, so the PR only needs to add the JSON entry.\ No newline at end of file+time, so the PR only needs to add the JSON entry.++### Built-in (internal) plugins++For plugins that must ship *with* the installer and are treated as part of the+app (auto-installed and auto-healed at startup), set `"internal": true` and add+an extra `package` field when the real npm name differs from `id`. Those entries+are bundled at build time by `scripts/prebuild.ts` into+`resources/preset-plugins/<id>/` (via `bundle.resources`) and never appear in the+first-run checklist. See the [built-in plugin guide](../../docs/BUILTIN_PLUGINS.md)+for the full workflow.\ No newline at end of file