返回 CodeWhale
host-plugin.md
1 # Bundles and Host plugins
2
3 A bundle is a package whose `package.json` declares `dsh.bundle.patch`; the YAML patch inserts plugin entries. Give the package and rows unique names; use the Loader's existing YAML syntax, including `!!js` where expressions are needed. Read an existing patch before editing it: a matching override replaces the complete `config`.
4
5 ## Manifest
6
7 A Host-only bundle needs no dependencies, install scripts, or build tool:
8
9 ```json
10 {
11 "name": "@local/my-plugin",
12 "version": "1.0.0",
13 "private": true,
14 "type": "module",
15 "exports": { ".": "./index.js" },
16 "dsh": { "bundle": { "patch": "./cordis.patch.yml" } }
17 }
18 ```
19
20 `cordis.patch.yml`:
21
22 ```yaml
23 - insert:
24 - id: my-plugin
25 name: '@local/my-plugin'
26 config: {}
27 ```
28
29 ## Display metadata and icon
30
31 Plugin Manager and Settings read `meta.title` and `meta.description` from exported locale JSON and an icon from exported resources without activating plugins. Complete this checklist before installation:
32
33 - Write a recognizable title and a description of the capability in `locale/en.json` under `meta.title` and `meta.description`. Add the same fields for the user's language and other supported locales, such as `locale/zh.json`; do not leave template copy unrelated to the delivered plugin. Write English titles and other UI labels in sentence case: capitalize only the first word and proper nouns, as in `Git worktrees`.
34 - Create an original icon or use artwork whose license permits redistribution, retaining any required attribution.
35 - Export `./locale/*.json` and `./icon`; retain existing runtime exports. Keep the patch, locales, icon, and every runtime file in `files`, updating it whenever you add modules or assets. For a packed or published bundle, verify the actual packed file list. Local directory installation links the checkout instead, so check the files directly; `files` does not filter a linked directory.
36 - Check that the icon is a valid image of the declared format and satisfies the path and size limits below. An accepted filename alone does not establish that its bytes render.
37
38 For example, the locale file contains:
39
40 ```json
41 { "meta": { "title": "My decoration", "description": "Draws a badge under the composer." } }
42 ```
43
44 ```json
45 {
46 "exports": { "./locale/*.json": "./locale/*.json", "./icon": "./icon.svg" },
47 "files": ["index.js", "cordis.patch.yml", "locale/*.json", "icon.svg"]
48 }
49 ```
50
51 A package-root plugin may instead declare a top-level `icon` in `package.json`; it takes priority over `./icon` and is a path relative to the manifest directory. A subpath plugin such as `my-plugins/search` is not a package and never reads a `package.json`; it exports `./search/locale/*.json` and `./search/icon` instead. Icons may be SVG, PNG, JPEG, or WebP up to 256 KiB and must stay inside the package; absolute paths, URLs, and symlinks leaving it are rejected. Missing fields of a package-root plugin fall back to `package.json` `name` and `description`, and missing images use the panel's default artwork; malformed metadata produces a diagnostic and keeps the valid text.
52
53 After installation, verify the intended title and description in bundle details and plugin rows, plus the rendered icons, in English and the user's locale where supplied. Check for metadata diagnostics and unintended fallbacks, and that each plugin row shows its own exported icon or, when it has none, the generic artwork. Without browser control, inspect the installed resources and report that rendered display remains unverified; do not claim visual success from installation alone.
54
55 An existing plugin's metadata may remain unchanged when it still describes the requested result. A disposable test fixture or an explicitly requested metadata-free package may omit resources, but explain each omission in the delivery report. A Host-only or configuration-only bundle still has a visible inventory entry; neither is an exception by itself.
56
57 ## Host plugin export forms
58
59 `index.js` exports one of these forms; do not mix them:
60
61 - `export function apply(ctx, config) {}` with optional `export const inject = ['tools']` and `export const Config`.
62 - A service class as the default export.
63
64 Register every resource inside `apply` with `ctx.effect` or `ctx.on` and return its cleanup. A plugin that declares `Config` validates the row's `config` at activation; query `Config.listConfigs` for an installed plugin's schema before writing its `config`, and follow `$defs` references in the returned document.
65
66 ## Persist your own configuration
67
68 Plugin code never persists configuration by reading, filtering, and rewriting the active profile's `cordis.patch.yml`, and never locates that file by walking up from the package directory. That file is not your bundle's patch: it belongs to the user and holds other plugins' `insert` rows, comments, and `!!js` expressions. To change its own configuration after `apply` returns, a plugin uses the config editor:
69
70 ```js
71 const entry = ctx.fiber.entry
72 const editor = ctx.get('configEditor')
73 if (entry === undefined || editor === undefined) throw new Error('Configuration cannot be saved in this profile')
74 await editor.edit(entry, current => ({ ...current, field: value }))
75 ```
76
77 `change` returns the row's complete config. The editor keeps the rest of the file, validates the candidate, serializes the write with other profile changes, and rejects values that a higher layer overrides. Report a rejection or a missing editor to the user instead of claiming the value was saved. Editing an ordinary field restarts the plugin so `apply` rebuilds its state; a field declared `.volatile()` updates in place and is read with `config.field.get()`. Store secrets, such as a token from an in-app sign-in or binding flow, as `ctx.credentials.modifyRecord()` records, not configuration; declare `credentials` in `inject` or read it with `ctx.get('credentials')`, and see the `@deepseek-ai/dsh-credentials` README for record keys.
78
79 ## Install, enable, and observe
80
81 `plugin_manager` `install_bundle` performs package installation and bundle selection; do not reproduce those steps with shell commands. Only pass `approvedBuilds` after the user explicitly approves the reported pending build scripts. Preserve returned failures and pending states; report success only after observing the requested capability.
82
83 `list_plugins` and `list_bundles` return exact identifiers for existing installations. `set_plugin` and `set_bundle` toggle them; `remove_bundle` removes a bundle. Inspect saved-state and activation outcomes separately: `failed` requires diagnosis, `overridden` means a higher-priority layer wins, and `restart-required` means the change is not live. Installing a new bundle can activate through HMR; replacing an installed package requires restart to load a fresh JavaScript module generation. Do not infer updated browser code from an unchanged slot id.
84
84 lines MARKDOWN