写一个 DSH 插件:从空目录到右侧栏面板
折腾完腾讯广告插件之后,把跟业务无关的部分抽出来,记一份最小指南。DSH 的插件机制没有正式文档,下面的内容是从能跑的代码和打包产物里反推的,以 0.2.2 这一代为准,以后可能会变。
一个插件就是一个 npm 包
DSH 插件分两个半边:
Host 半:普通的 Cordis 插件,跑在 DSH 主进程里,负责数据、文件、网络、子进程;
Client 半:一份浏览器 bundle,跑在 DSH 界面里,负责挂载点和渲染。
两边用 package.json 里的 dsh 字段声明:
{
"name": "@local/hello-dsh",
"version": "0.1.0",
"type": "module",
"exports": {
".": "./index.js",
"./client": "./client.js"
},
"dsh": {
"bundle": { "patch": "./cordis.patch.yml" },
"client": {
"platform": "web",
"immediately": true,
"inject": [
"@deepseek-ai/dsh-client-locale",
"@deepseek-ai/dsh-client-ui-sidebar"
]
}
}
}bundle.patch 指向一个 patch 文件,把插件行插进 DSH 的 Cordis 配置:
- insert: - id: hello-dsh name: '@local/hello-dsh'
client.inject 写客户端依赖:界面用到的服务来自哪些包。immediately: true 是启动时就加载,不然可能等用到时才拉。
Host 半就是 index.js,导出 name 和 apply:
export const name = 'hello-dsh'
export function apply(ctx) {
// ...
}没有 class,没有 main 函数。DSH 启动时按 patch 里的行加载这个文件,调一次 apply。
Host 半:别自己起服务
最容易犯的错是照 Web 后端的习惯起个 HTTP server。不要。DSH 自己有 connection 服务,插件把路由挂上去:
export function apply(ctx) {
ctx.inject(['connection'], (c) => {
const connection = c.get('connection')
c.effect(() => connection.fetch.register({
path: '/api/hello/ping',
methods: ['POST'],
requestBody: 'buffered',
fetch: async () => Response.json({ ok: true, now: Date.now() }),
}), 'hello: ping route')
})
}三件事要记住:
ctx.inject(['connection'], cb)在服务就绪时回调。服务没就绪它会等,不报错,回调也就不执行——这点后面还会提。c.effect(fn, label)管生命周期。fn返回一个清理函数,插件卸载或 DSH 退出时调用。路由、定时器、子进程都应该包在里面。客户端用相对路径请求(
fetch('api/hello/ping')),不要写死http://localhost:端口。路由只挂在 DSH 自己的连接上。
如果插件会长期跑子进程,记得挂一个清理 effect 把它们收掉。DSH 退出不会替你杀 node。
Client 半:从 ModuleLoader 开始
客户端 bundle 里不能写 import React from 'react'。它是在 DSH 的模块加载器里跑的:
window.__ModuleLoader__.load({
id: '@local/hello-dsh',
factory(require) {
const React = require('react')
const h = React.createElement
function Panel() {
const [now, setNow] = React.useState('')
return h('div', null,
h('button', {
type: 'button',
onClick: async () => {
const r = await fetch('api/hello/ping', { method: 'POST' })
.then((x) => x.json())
setNow(new Date(r.now).toLocaleTimeString())
},
}, 'ping'),
h('span', null, now))
}
return {
inject: ['slots'],
apply(ctx) {
ctx.slots.inject('main', () => ctx.slots.register(
{ name: 'main', key: 'hello-dsh' },
Panel,
))
},
}
},
})id 要和 package name 对上。factory 返回的对象跟 Host 半同构:inject + apply。
Slot 决定面板长在哪
DSH 的界面是 slot 拼起来的。常用的几个:
| slot | 位置 | 注册字段 |
|---|---|---|
sidebar.panellist | 左侧栏那一列入口 | id、order、label |
main | 中间主内容区 | key |
sidebar.right.pane.tab | 右侧栏 tab 的正文 | key(跟 tab 定义的 id 对上) |
注册都走 ctx.slots.inject(slotName, () => ctx.slots.register(options, Component))。inject 负责等 slot 出现,register 才是真正挂上去。
左侧栏入口的组件一般只画一个图标,点它之后主区显示什么,由 main 席位的组件决定。
右侧栏多一层:tab 类型
右侧栏不是直接往 slot 里塞组件就完了。它按 tab 类型管理,要注册三样东西:tab 定义、正文席位、打开时用的 kind。
const TAB_ID = '@local/hello-dsh'
const TAB_KIND = 'hello'
ctx.inject(['sidebarRightTabs'], (scope) => {
const tabs = scope.get('sidebarRightTabs')
if (!tabs || typeof tabs.register !== 'function') return
scope.effect(() => tabs.register({
id: TAB_ID,
kind: TAB_KIND,
title: () => 'Hello',
guide: [{
id: 'open',
order: 45,
title: () => 'Hello',
description: () => '一个示例插件',
icon: HelloIcon,
}],
}), 'hello: right tab type')
})
ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register(
{ name: 'sidebar.right.pane.tab', key: TAB_ID },
Panel,
))对应关系别弄错:正文席位的 key 是 tab 定义的 id,不是 kind;openTab() 用的是 kind。
ctx.get('sidebarRight')?.openTab(TAB_KIND)guide 是「开始」页上的那张卡片。不是必需的,但没有它,用户只能从 tab 条的添加控件里翻。
另外,tab 定义里有个 keepMounted。面板切走再回来时状态丢了,可以打开它,让组件保持挂载;不想常驻,就自己把状态放到模块作用域。
让模型能调用你的插件
DSH 有 tools 服务,注册一个定义就会出现在主对话里:
ctx.inject(['tools'], (c) => {
const tools = c.get('tools')
c.effect(() => tools.register({
name: 'hello_now',
description: '返回当前时间。',
parameters: {
type: 'object',
properties: {
tz: { type: 'string', description: '时区,默认 zh-CN。' },
},
additionalProperties: false,
},
output: {
schema: {
type: 'object',
properties: { text: { type: 'string' } },
required: ['text'],
additionalProperties: false,
},
render: (_args, value) => [{ type: 'text', text: value.text }],
},
async execute(args, exec) {
return { text: new Date().toLocaleString(args.tz || 'zh-CN') }
},
}), 'hello: now tool')
})几个细节:
output是必填的,注册时会校验output.schema。支持的 JSON Schema 是一个子集:type、properties、required、additionalProperties、items、enum、const、oneOf,加上description这类注解。别的关键字会被拒。parameters是给模型看的 JSON Schema。用@deepseek-ai/dsh-tools的defineTool可以顺带做参数校验和类型推导;不想引依赖,直接传普通对象也能注册。execute的第二个参数有signal。耗时操作(子进程、网络)应该接上,用户点停止时能收掉。
想用 DSH 自己的模型
llm 服务对插件开放:
const llm = ctx.get('llm')
const providers = llm.listProviders()
const provider = providers.some((p) => p.id === 'deepseek-official')
? 'deepseek-official'
: 'deepseek-account'
const stream = llm.stream({
provider,
model: 'deepseek-flash',
reasoningEffort: 'off',
temperature: 0.3,
maxTokens: 2048,
system: '你是一个助手。',
messages: [{ role: 'user', content: [{ type: 'text', text: '你好' }] }],
signal,
})
for await (const chunk of stream) {
if (chunk.type === 'text-delta') process.stdout.write(chunk.text)
else if (chunk.type === 'finish') console.log(chunk.reason)
}模型用的是 DSH 已经配好的凭证,插件不用再存一份 Key。流结束的 reason.kind 有 stop、max-tokens、aborted、error 这些,按需处理。
测试:假宿主
没有脚手架,两个土办法够用。
一是读源码。DSH 打包在 resources/app.asar 里,asar 就是个带头部的归档,解开能看到 TypeScript 源码。右侧栏的 slot 名、服务名、tab 定义字段,都是这么对出来的。
二是假宿主。Host 半的测试不需要真 DSH:
const routes = new Map()
const ctx = {
effect(fn) { return fn() },
inject(_deps, cb) {
return cb({
get: () => ({ fetch: { register: (o) => routes.set(o.path, o) } }),
effect(fn) { return fn() },
})
},
}
apply(ctx)
const route = routes.get('/api/hello/ping')
const res = await route.fetch(new requestInit('http://localhost/api/hello/ping', {
method: 'POST',
}))Client 半麻烦一点:假造 window.__ModuleLoader__,再写一个迷你 React 把组件跑起来。不用还原,够 useState、useRef、useEffect、useCallback 就能测大多数交互。
探针写多了会发现,真正省时间的是把配置目录用环境变量隔离出去,别让测试写到真实数据。
几个静默失败的坑
服务名写错:
ctx.inject不报错,回调干脆不执行。界面不出现,控制台也没有红字。slot 的 key 对不上:
register成功,界面没反应。客户端 bundle 里
import React:加载直接失败,通常只有一行很不显眼的错误。忘了声明
dsh.client.inject:运行时require拿不到东西。tab 的正文档位用了
kind而不是id:tab 能开,正文是空的。Host 半改完不重启:DSH 只在启动时加载 Host 插件,改
index.js必须重启;client.js一般刷新界面就能看到。
前面五个都不会报错,只会让功能静默消失。调试的时候先怀疑名字对不对,再怀疑逻辑。
装到本机
开发阶段最省事的是 link。把插件目录加进当前 profile 的依赖:
{
"dependencies": {
"@local/hello-dsh": "link:E:/path/to/hello-dsh"
},
"dsh": {
"profile": {
"bundles": ["@local/hello-dsh"]
}
}
}profile 在 ~/.dsh/profiles/desktop/package.json。node_modules 里要能解析到这个包(pnpm link 或者手动建链接),bundles 里再加一行。重启 DSH,左侧栏应该多一个入口。
写得多了会发现,DSH 插件本身没有多少代码,大部分时间花在确认「这个名字到底叫什么」上。先把最小骨架跑通,再往里加功能,比一次写全要快。