DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
feat(plugins): add defineHostRuntime and defineService to dsh-tauri
hairyf4c0a1340a771
diff --git a/AGENTS.md b/AGENTS.mdindex 7500164e..4379f4d9 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -10,3 +10,6 @@若同时涉及桌面端与内置插件,必须同时参照并整合 `docs/AGENTS.desktop.md` 和 `docs/AGENTS.plugins.md` 的规范。- 你不被允许查看 archive/ 的所有内容,除非有特殊要求。+- 编写代码时,必须保持 0 注释,只被允许在关键节点添加,避免大面积污染代码。+- 如果你看到大量的冗余描述与注释,你应该精简 / 删除它们,以保持代码的简洁性和可读性。+- 如果有什么你想临时记录一下,你可以编写在 .temp 文件中,在这里你是自由的。\ No newline at end of filediff --git a/docs/AGENTS.desktop.md b/docs/AGENTS.desktop.mdindex 06aa05f3..d5822f3d 100644--- a/docs/AGENTS.desktop.md+++ b/docs/AGENTS.desktop.md@@ -1,3 +1,5 @@+> 该文档大部分已固定,仅允许添加少量注释。+# DeepSeek Harness Desktop 开发规范文档> **架构概览**:基于 Tauri 2 + React 19,内嵌服务运行于 `[http://127.0.0.1:3080](http://127.0.0.1:3080)` (Debug 环境默认 `3081`)。diff --git a/docs/PLUGIN_CLIENT.spec.md b/docs/PLUGIN_CLIENT.spec.mdindex deb848d4..a0378291 100644--- a/docs/PLUGIN_CLIENT.spec.md+++ b/docs/PLUGIN_CLIENT.spec.md@@ -1,3 +1,5 @@+> 该文档已固定,禁止修改。+# 插件客户端架构规范 (Plugin Client Architecture Protocol)> 本规范是 [通用软件开发规范与协议 (DEVELOPMENT.spec.md)](./DEVELOPMENT.spec.md) 在 **DeepSeek Harness 内置插件客户端半区(Plugin Client Half / Browser Runtime)** 的具象化工程落地协议。所有插件的 `src/client` 实现必须严格遵循本规范。diff --git a/docs/PLUGIN_HOST.spec.md b/docs/PLUGIN_HOST.spec.mdindex 3400d8c8..c94a927e 100644--- a/docs/PLUGIN_HOST.spec.md+++ b/docs/PLUGIN_HOST.spec.md@@ -1,3 +1,5 @@+> 该文档已固定,禁止修改。+# 插件宿主端架构规范 (Plugin Host Architecture Protocol)> 本规范为 [DEVELOPMENT.spec.md](./DEVELOPMENT.spec.md) 在 **DeepSeek Harness 插件宿主端(Node Runtime)** 的落地协议。插件 `src/host` 实现必须严格遵守本规范。@@ -25,7 +27,7 @@ $$\text{apply.ts (装配)} \longrightarrow \begin{bmatrix} \text{routes/} \\ \te| 目录/文件 | 职责说明 | 关键约束 || --- | --- | --- || **`apply.ts`** | 装配入口 | 仅做声明式组装(工具/事件/提示词/路由),控制在 30~50 行以内,不含业务逻辑。 |-| **`config/`** | 配置与单例 | `runtime.ts`: 导出内存单例及 `bindHost`/`useHost` 宿主绑定。<br>+| **`config/`** | 配置与单例 | `runtime.ts`: 导出内存单例及 `setCurrentHostInstance`/`getCurrentHostInstance` 宿主绑定。<br><br>`constants.ts`: 静态常量与配置,严禁硬编码 Magic Number/String。 || **`types/`** | 类型定义 | 导出领域模型、DTO、输入输出接口(纯类型定义)。 |@@ -71,7 +73,7 @@ packages/dsh-tauri-worktree/src/host/```typescriptexport function apply(ctx: HostContext): void {- bindHost(ctx) // 1. 绑定宿主能力(仅 service 层可通过 useHost() 读取)+ setCurrentHostInstance(ctx) // 1. 绑定宿主能力(仅 service 层可通过 getCurrentHostInstance() 读取)ctx.tools.register(createWorktreeTool())ctx.tools.register(checkoutWorktreeTool())@@ -87,7 +89,7 @@ export function apply(ctx: HostContext): void {**2. 配置与状态层 (`config/`)**-* `runtime.ts` 导出内存单例与宿主绑定函数 (`bindHost` / `useHost`)。宿主进程按 `--profile` 启动,单进程内插件仅挂载一次,模块级单例安全。必须包含清除/销毁机制以防内存泄漏。+* `runtime.ts` 导出内存单例与宿主绑定函数 (`setCurrentHostInstance` / `getCurrentHostInstance`)。宿主进程按 `--profile` 启动,单进程内插件仅挂载一次,模块级单例安全。必须包含清除/销毁机制以防内存泄漏。**3. 持久化层 (`storage/`)**@@ -106,7 +108,7 @@ export function apply(ctx: HostContext): void {* [ ] **装配精简**:`apply.ts` 是否仅包含声明式注册(无逻辑内联/过度嵌套)?* [ ] **状态收口**:内存 Map/Set 是否收拢于 `config/runtime.ts`?是否存在参数击穿透传?* [ ] **服务约束**:是否全员使用 `defineService`?文件名与导出标识符是否一致?动词是否符合白名单?-* [ ] **签名收口**:服务方法参数是否按需声明且不含 `ctx`/`host`?是否仅 `service/` 内部使用 `useHost()`?+* [ ] **签名收口**:服务方法参数是否按需声明且不含 `ctx`/`host`?是否仅 `service/` 内部使用 `getCurrentHostInstance()`?* [ ] **路由纯度**:`routes/` 是否仅负责协议解析与 DTO 校验?无直接操作 `storage`/宿主对象/系统命令行为?* [ ] **存储抽象**:`storage/index.ts` 是否仅导出驱动实例?* [ ] **工具解耦**:`utils/` 是否无状态、脱离业务上下文且未引入契约宏?diff --git a/docs/PLUGIN_HOST_SERVICE.spec.md b/docs/PLUGIN_HOST_SERVICE.spec.mdindex e4380a84..322ef12f 100644--- a/docs/PLUGIN_HOST_SERVICE.spec.md+++ b/docs/PLUGIN_HOST_SERVICE.spec.md@@ -1,3 +1,5 @@+> 该文档已固定,禁止修改。+# 插件宿主端领域服务协议 (Plugin Host Service Protocol)> 本规范为 [PLUGIN_HOST.spec.md](./PLUGIN_HOST.spec.md) 的**子协议**,整体受 [DEVELOPMENT.spec.md](./DEVELOPMENT.spec.md) 约束[cite: 1]。@@ -40,8 +42,8 @@ import { defineService } from 'dsh-tauri'### 3. 签名与宿主隔离 (Signatures & Host Isolation)* **参数扁平**:按业务需求直接传参,不做无意义的单对象包装[cite: 1]。-* **无 `ctx`/`host` 参**:服务方法不得接收 `ctx` 或 `host` 参数[cite: 1]。宿主能力由 `apply.ts` 调用 `bindHost(ctx)` 绑定后,通过 `useHost()` 按需获取[cite: 1]。-* **隔离访问**:**只有 `service/` 允许调用 `useHost()**`;`routes/`、`tools/` 等必须经由服务层间接访问宿主能力[cite: 1]。+* **无 `ctx`/`host` 参**:服务方法不得接收 `ctx` 或 `host` 参数[cite: 1]。宿主能力由 `apply.ts` 调用 `setCurrentHostInstance(ctx)` 绑定后,通过 `getCurrentHostInstance()` 按需获取[cite: 1]。+* **隔离访问**:**只有 `service/` 允许调用 `getCurrentHostInstance()**`;`routes/`、`tools/` 等必须经由服务层间接访问宿主能力[cite: 1]。---@@ -126,7 +128,7 @@ export const workspace = defineService({```typescript// service/session-context.ts-import { defineService, useHost } from 'dsh-tauri'+import { defineService, getCurrentHostInstance } from 'dsh-tauri'export const sessionContext = defineService({resolve(sessionId: string): string | null {@@ -157,6 +159,6 @@ export const sessionContext = defineService({* [ ] 服务对象是否全部由函数组成(无散装状态/常量)[cite: 1]?* [ ] 所有方法名是否**完全符合所属角色的动词白名单**[cite: 1]?* [ ] 方法参数是否扁平,且未包含 `ctx`/`host` 形参[cite: 1]?-* [ ] 是否仅在 `service/` 内部使用 `useHost()`,其它层绝不接触宿主对象[cite: 1]?+* [ ] 是否仅在 `service/` 内部使用 `getCurrentHostInstance()`,其它层绝不接触宿主对象[cite: 1]?* [ ] 私有函数与常量是否收纳于 `// --- internal ---` 且未导出[cite: 1]?* [ ] 类型与常量是否已剥离至 `types/` 与 `config/constants.ts`[cite: 1]?\ No newline at end of filediff --git a/packages/dsh-tauri/src/host/config/runtime.test.ts b/packages/dsh-tauri/src/host/config/runtime.test.tsnew file mode 100644index 00000000..c3664416--- /dev/null+++ b/packages/dsh-tauri/src/host/config/runtime.test.ts@@ -0,0 +1,43 @@+import type { HostContext } from '../types'+import { describe, expect, it } from 'vitest'+import { defineHostRuntime } from './runtime'++function createHost(): HostContext {+ return { sessions: {} } as unknown as HostContext+}++describe('defineHostRuntime', () => {+ it('未绑定时读取直接抛错', () => {+ const { getCurrentHostInstance } = defineHostRuntime()+ expect(() => getCurrentHostInstance()).toThrow(/尚未绑定/)+ })++ it('绑定后取回同一实例,传 undefined 后回到未绑定', () => {+ const { getCurrentHostInstance, setCurrentHostInstance } = defineHostRuntime()+ const host = createHost()++ setCurrentHostInstance(host)+ expect(getCurrentHostInstance()).toBe(host)++ setCurrentHostInstance(undefined)+ expect(() => getCurrentHostInstance()).toThrow(/尚未绑定/)+ })++ it('每个槽位彼此隔离', () => {+ const first = defineHostRuntime()+ const second = defineHostRuntime()+ const host = createHost()++ first.setCurrentHostInstance(host)+ expect(first.getCurrentHostInstance()).toBe(host)+ expect(() => second.getCurrentHostInstance()).toThrow(/尚未绑定/)+ })++ it('支持收窄到插件自有宿主面', () => {+ const runtime = defineHostRuntime<HostContext & { stopSessionProcesses: () => void }>()+ const stopSessionProcesses = () => {}+ runtime.setCurrentHostInstance({ ...createHost(), stopSessionProcesses })++ expect(runtime.getCurrentHostInstance().stopSessionProcesses).toBe(stopSessionProcesses)+ })+})diff --git a/packages/dsh-tauri/src/host/config/runtime.ts b/packages/dsh-tauri/src/host/config/runtime.tsnew file mode 100644index 00000000..c72d3b43--- /dev/null+++ b/packages/dsh-tauri/src/host/config/runtime.ts@@ -0,0 +1,22 @@+import type { HostContext } from '../types'++export interface HostRuntime<C = HostContext> {+ setCurrentHostInstance: (ctx: C | undefined) => void+ getCurrentHostInstance: () => C+}++/** 每个插件的 `config/runtime.ts` 各调用一次,得到插件私有的绑定槽位。 */+export function defineHostRuntime<C = HostContext>(): HostRuntime<C> {+ let current: C | undefined++ return {+ setCurrentHostInstance: (ctx) => {+ current = ctx+ },+ getCurrentHostInstance: () => {+ if (current === undefined)+ throw new TypeError('getCurrentHostInstance: 宿主实例尚未绑定,apply.ts 需先调用 setCurrentHostInstance(ctx)')+ return current+ },+ }+}diff --git a/packages/dsh-tauri/src/host/modules/h3.ts b/packages/dsh-tauri/src/host/modules/h3.tsindex e6217a78..ba0468db 100644--- a/packages/dsh-tauri/src/host/modules/h3.ts+++ b/packages/dsh-tauri/src/host/modules/h3.ts@@ -1,16 +1,2 @@-/**- * host/modules/h3.ts — h3 v2 面的统一转出(插件不直接依赖 `h3` 包)。- *- * 只转出**未被 h3 v2 废弃**的成员;插件侧对应写法:- * - 状态码:`event.res.status = 400`(`event.res.statusText` 写文案)——- * 不要用已废弃的 `setResponseStatus`;- * - 抛错:`new HTTPError({ status: 400, statusText: '...' })`——- * 不要用已废弃的 `createError` / `H3Error`;- * - 读头:`event.req.headers.get(name)`——不要用已废弃的 `getHeader`;- * - 请求体 / 查询串仍用 h3 内置的 `readBody` / `getQuery`(v2 未废弃);- * - SSE:`new EventStream(event)` + `push` / `pushComment` / `onClosed` / `close`,- * 处理器直接 `return stream`(`EventStream` 继承 `HTTPResponse`)。- */-export { bodyLimit, defineEventHandler, EventStream, getQuery, HTTPError, readBody, readRawBody } from 'h3'export type { EventHandler, EventStreamMessage, EventStreamOptions, H3Event } from 'h3'diff --git a/packages/dsh-tauri/src/host/routes/index.test.ts b/packages/dsh-tauri/src/host/routes/index.test.tsindex fcbff6be..5257c4d8 100644--- a/packages/dsh-tauri/src/host/routes/index.test.ts+++ b/packages/dsh-tauri/src/host/routes/index.test.ts@@ -126,7 +126,6 @@ function fakeResponse(): { response: ServerResponse, state: { status: number, hereturn { response: response as unknown as ServerResponse, state }}-/** 取某一行宿主的 handler;缺失即测试数据错误。 */function routeOf(harness: Harness, kind: string, path: string): HostRoute {const route = harness.routes.get(`${kind}\u0000${path}`)if (!route)@@ -134,14 +133,6 @@ function routeOf(harness: Harness, kind: string, path: string): HostRoute {return route}-/**- * 声明 + 立即注册的语法糖:等价于 `defineRoutes(setup)(ctx)`,返回卸载函数。- * 协议本体(`defineRoutes` 与 `registerRoutes(ctx)` 分成两步调用)见「运行期注册」用例。- *- * deps 透传给注册期第二参数;省略即零依赖注册(与迁移前的 `routes(ctx)` 等价)。- * `Deps` 是未解析泛型时 TS 无法收窄 `RoutesRegistration<Deps>` 这个条件类型,故此处断言一次;- * 真实调用点(如 `routes(ctx, deps)`)的 deps 是具体类型,静态保护照常生效。- */function mountRoutes<Deps = undefined>(ctx: RoutesContext, setup: RoutesSetup, deps?: Deps): () => void {const register = defineRoutes<Deps>(setup) as (ctx: RoutesContext, deps?: Deps) => () => voidreturn register(ctx, deps)diff --git a/packages/dsh-tauri/src/host/service/index.test.ts b/packages/dsh-tauri/src/host/service/index.test.tsnew file mode 100644index 00000000..186b2c32--- /dev/null+++ b/packages/dsh-tauri/src/host/service/index.test.ts@@ -0,0 +1,20 @@+import { describe, expect, it } from 'vitest'+import { defineService } from './index'++describe('defineService', () => {+ it('原样返回传入对象(零包装)', () => {+ const ledger = {+ load: (sessionId: string) => sessionId,+ }+ expect(defineService(ledger)).toBe(ledger)+ })++ it('方法保持原有签名', () => {+ const cleaner = defineService({+ start: (sessionId: string) => `start:${sessionId}`,+ unsettled: () => [],+ })+ expect(cleaner.start('s1')).toBe('start:s1')+ expect(cleaner.unsettled()).toEqual([])+ })+})diff --git a/packages/dsh-tauri/src/host/service/index.ts b/packages/dsh-tauri/src/host/service/index.tsnew file mode 100644index 00000000..205f6739--- /dev/null+++ b/packages/dsh-tauri/src/host/service/index.ts@@ -0,0 +1,6 @@+type ServiceMethod = (...args: any[]) => any++/** 编译期约束成员全为函数(散装状态留在模块作用域);运行时原样返回,零开销。 */+export function defineService<T extends Record<string, ServiceMethod>>(service: T): T {+ return service+}diff --git a/packages/dsh-tauri/src/index.ts b/packages/dsh-tauri/src/index.tsindex f054117d..016cee36 100644--- a/packages/dsh-tauri/src/index.ts+++ b/packages/dsh-tauri/src/index.ts@@ -1,8 +1,8 @@export * from './host/config/constants'+export * from './host/config/runtime'export * from './host/modules/h3'export * from './host/routes'-export type {HttpMethod,RouteDefinition,@@ -15,6 +15,8 @@ export type {RoutesSetup,} from './host/routes/index.type'+export * from './host/service'+export * from './host/types'export * from './host/utils/atomic'export * from './host/utils/driver'