@fish-under-sea/dsh-visual-companion 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md ADDED
@@ -0,0 +1,163 @@
1
+ <div align="center">
2
+
3
+ # @fish-under-sea/dsh-visual-companion
4
+
5
+ **Click on the page and move on — give your DSH session a visual companion: pick, annotate, submit, and the session continues on its own.**
6
+
7
+ [![npm](https://img.shields.io/npm/v/@fish-under-sea/dsh-visual-companion?style=flat-square&label=npm&color=cb3837)](https://www.npmjs.com/package/@fish-under-sea/dsh-visual-companion)
8
+ ![license](https://img.shields.io/badge/license-MIT-green?style=flat-square)
9
+ ![node](https://img.shields.io/badge/node-%5E22.19.0%20%7C%7C%20%3E%3D24.0.0-339933?style=flat-square)
10
+ ![DSH](https://img.shields.io/badge/DSH-%3E%3D0.2.0--rc.2-4b6ef6?style=flat-square)
11
+ ![plugin](https://img.shields.io/badge/plugin-host%20only-6b7280?style=flat-square)
12
+
13
+ [简体中文](README.md) · **English**
14
+
15
+ </div>
16
+
17
+ ## What problem does this solve
18
+
19
+ When reviewing prototypes, comparing layouts, or picking options in a browser, two things get in the way:
20
+
21
+ 1. **You have to switch back to the terminal and retype your choices**: "I pick B, and make the colors cooler" — the person reviewing the mockup shouldn't also be a typist;
22
+ 2. **Or the opposite: every click wakes the assistant**: pick three options, interrupt the assistant three times — noisy and token-wasteful.
23
+
24
+ This plugin solves both: **picking stays completely silent** (only local page state changes), and **only pressing "Submit to assistant" wakes the session once**, delivering the full set of choices and notes in a single message.
25
+
26
+ ## What you get
27
+
28
+ - **Silent picking**: clicking, unclicking, and typing notes never fire a request or wake the session;
29
+ - **One submit = one wake**: all selections plus notes are composed into a single user message, e.g.
30
+ `[Visual Companion] User picked 2 items on the page: A (single-column layout), C (hybrid). Note: more spacing. Synced to you — just keep going (no need to ask).`
31
+ - **Auto-continue**: the session receives the message and starts a new turn immediately, no retyping needed;
32
+ - **Adaptive UI**: single-select / multi-select (add `data-multiselect` to the container), note field, waiting screen; the submit bar hides automatically when there are no options on the page.
33
+
34
+ ## Installation
35
+
36
+ ```sh
37
+ # Recommended: install from npm (--profile: desktop for the desktop app, web for the web app)
38
+ dsh plugin --profile <profile> add @fish-under-sea/dsh-visual-companion
39
+
40
+ # For source changes only: use link: (repo source is the install source; restart DSH to pick up changes)
41
+ dsh plugin --profile <profile> add "link:<repo-path>/packages/dsh-visual-companion"
42
+ ```
43
+
44
+ **Restart the profile's host** after installing (new plugin packages require a restart to enter the composition; afterwards, only changes to `cordis.patch.yml` content can be hot-reloaded).
45
+
46
+ ## Usage
47
+
48
+ This package consists of **two halves**, neither of which touches the model API:
49
+
50
+ - **Local service** (`bin/visual-companion.mjs`): zero-dependency HTTP server that renders pages, receives clicks, and writes `events.jsonl` and `pending.json`;
51
+ - **Host plugin** (`lib/index.js`): watches `state/pending.json` and, on each submit, delivers a user message to the target session and starts a new turn.
52
+
53
+ ### Start the local service
54
+
55
+ A managed background task; the directory lives in a temporary location inside the workspace:
56
+
57
+ ```sh
58
+ node "$DSH_HOME/profiles/<profile>/node_modules/@fish-under-sea/dsh-visual-companion/bin/visual-companion.mjs" \
59
+ --dir "<workspace>/.dsh-visual" --port 0 --session "$DSH_SESSION_ID"
60
+ # → prints { url, port, key, screenDir, stateDir, pendingFile, session }
61
+ ```
62
+
63
+ ### Write a screen
64
+
65
+ Write HTML fragments into `<dir>/screen/` (use semantic filenames, don't reuse; write a full document — starting with `<!DOCTYPE` or `<html` — only when you need full page control).
66
+
67
+ ### Open the page
68
+
69
+ Hand the `url` together with `?key=` to the user (in DSH, use `sidebar_open` to open it in the sidebar). Requests without the key are rejected with 403.
70
+
71
+ ### Built-in styles
72
+
73
+ The service ships a framework stylesheet with these classes — just use them in your fragments:
74
+
75
+ | Purpose | Class |
76
+ | --- | --- |
77
+ | Options (single-select; add `data-multiselect` to the container for multi-select) | `.options` > `.option[data-choice]`, click calls `toggleSelect(this)`; `.cards` > `.card` works the same way |
78
+ | Visual blocks | `.mockup`, `.split`, `.pros-cons`, `.mock-nav`, `.mock-sidebar`, `.mock-content`, `.mock-button`, `.mock-input`, `.placeholder` |
79
+ | Text hierarchy | `h2` / `h3` / `.subtitle` / `.section` / `.label` |
80
+
81
+ ### `visual_companion` tool
82
+
83
+ The plugin registers a `visual_companion` tool:
84
+
85
+ | Action | Parameters | Effect |
86
+ | --- | --- | --- |
87
+ | `arm` | `dir`, `session_id?` | Bind a directory and target session; `session_id` may be omitted (supplied later by `pending.json`) |
88
+ | `disarm` | — | Unbind and close the watcher |
89
+ | `status` | — | Binding state, wake count, last result, current `pending.json` |
90
+ | `wake` | `session_id`, `text` | Immediately probe the wake pipeline (bypasses the page) |
91
+
92
+ ### On-disk files
93
+
94
+ All persisted files live in `<dir>/state/`:
95
+
96
+ | File | Content |
97
+ | --- | --- |
98
+ | `pending.json` | **Written only on submit**: `{type:"submit", selections:[{choice,text}], note, at, sessionId}` — the plugin watches this |
99
+ | `events.jsonl` | Appended per event: `click` is the picking process (no wake), `submit` is a full submission (wakes once) |
100
+ | `server-info` / `server-stopped` | Service startup info and shutdown reason |
101
+
102
+ ### Slash command `/companion`
103
+
104
+ Type directly in a session:
105
+
106
+ ```text
107
+ /companion [topic to review]
108
+ ```
109
+
110
+ The plugin will: **①** start the local service in the background if it isn't running (zero dependencies, auto-assigned port); **②** hand the full URL with the session key to the current session's assistant, which opens it in the sidebar via `sidebar_open`; **③** have the assistant bind the watcher and write the first prototype screen into `screen/`. After that, just pick, write notes, and press "Submit to assistant".
111
+
112
+ > The command only handles "start the service + hand off" — what appears on the page is still decided by the assistant's HTML fragments. So it's more convenient than running `bin/visual-companion.mjs` by hand, but it won't decide what to show.
113
+
114
+ ## Configuration
115
+
116
+ | Key | Default | Description |
117
+ | --- | --- | --- |
118
+ | `watchDir` | `""` | When non-empty, **auto-binds** that directory on load (config hot-reload / restart won't drop the binding); leave empty to `arm` manually via `visual_companion` |
119
+
120
+ The target session is **not stored in config**: it comes from the `sessionId` written with each submit (set by the service's `--session` at startup), so the same service can serve different sessions and no machine-specific session id ends up in shared config.
121
+
122
+ Machine-specific absolute paths don't go into the shared bundle. To enable auto-binding, override with the same id in your own profile patch layer:
123
+
124
+ ```yaml
125
+ - id: visual-companion
126
+ name: '@fish-under-sea/dsh-visual-companion'
127
+ config:
128
+ watchDir: D:/some/workspace/.dsh-visual
129
+ ```
130
+
131
+ ## Compatibility and boundaries
132
+
133
+ Delivery tries each strategy in order (closest to the public surface first) and returns the one that actually succeeds:
134
+
135
+ 1. `agent.followup(createUserMessage(...))` — the same public path used by slash-command user-message injection;
136
+ 2. `agent.steer(...)` — when the target is currently running;
137
+ 3. `ctx.subagents[Symbol.for('dsh.subagent.queuePrompt' | 'deliverPrompt')]` — the host-internal delivery hook for renewable subagents.
138
+
139
+ - **host-only**: a host plugin (no `dsh.client`, no browser half, no frontend build) plus a local service — a zero-dependency HTTP server that serves the interactive browser page. That page comes from the local service, and is **not** a DSH client plugin;
140
+ - Zero-dependency service (Node stdlib only), auto-assigned port, URL includes a session key; `--host 0.0.0.0 --url-host <hostname>` supports remote / container use, but **`?key=` must be preserved**;
141
+ - Service **auto-exits after 4 hours idle** (tunable via `--idle-minutes`); screen files are temporary artifacts, don't treat them as deliverables;
142
+ - Degradation chain: no Node → self-contained HTML + `present`; no sidebar browser → hand the full URL to the user to open manually; plugin not installed → manually read `events.jsonl` next turn;
143
+ - Requires Node `^22.19.0 || >=24`, DSH `>=0.2.0-rc.2`; no API key needed;
144
+ - **Binding is fault-tolerant** (since 0.1.5): when `<watchDir>/state` doesn't exist yet, it is **created recursively**; if watching fails (path is not a directory, insufficient permissions), only a warning is logged and the state stays honestly "unbound" — **the plugin loads normally**, the tool and `/companion` don't disappear together, and `visual_companion({action:"arm"})` can retry once fixed;
145
+ - **Known limitation**: host plugin code is not hot-reloaded — changing `lib/index.js` requires restarting the host; only `cordis.patch.yml` content changes can be hot-reloaded.
146
+
147
+ ## Development and testing
148
+
149
+ ```sh
150
+ # Fresh clone: run pnpm install in the package directory first (zero runtime deps, but tests need the 3 DSH packages declared as devDependencies)
151
+ node packages/dsh-visual-companion/test/visual-companion.test.mjs # message rendering + manifest contract + binding fault tolerance (10 cases)
152
+ pnpm pack # artifact check: files cover bin / lib / patch / README
153
+ ```
154
+
155
+ `lib/message.js` is a **zero-dependency pure function** (message rendering), so tests and callers can verify the copy without installing `@deepseek-ai/*`; only `lib/index.js` depends on host-provided `@deepseek-ai/dsh-llm`, `dsh-tools`, and `schemastery` (**optional peers at runtime**, provided by DSH; **the same packages are separately declared as `devDependencies` for testing**, so a fresh clone can run tests right after `pnpm install` — no manual stuffing of `node_modules` needed).
156
+
157
+ ## Relationship with the umbrella package
158
+
159
+ This package is one member of the [`@fish-under-sea/dsh-fish`](https://github.com/Fish-under-sea/dsh-fish) umbrella package: the umbrella's bundle layer inserts this plugin line into the profile, so installing the umbrella is equivalent to installing all Fish-built plugins at once. Installing this package alone affects only this one entry.
160
+
161
+ ## License
162
+
163
+ MIT © Fish-under-sea
package/README.md CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  # @fish-under-sea/dsh-visual-companion
4
4
 
5
- **网页上点完就继续** —— 用户在视觉伴侣页面选好选项、写两句备注、按下「提交给助手」,DSH 会话立刻收到一条用户消息并自动起一轮。
5
+ **网页上点完就继续 —— 给 DSH 会话配一个视觉伴侣,点选、备注、提交,会话自动起一轮。**
6
6
 
7
7
  [![npm](https://img.shields.io/npm/v/@fish-under-sea/dsh-visual-companion?style=flat-square&label=npm&color=cb3837)](https://www.npmjs.com/package/@fish-under-sea/dsh-visual-companion)
8
8
  ![license](https://img.shields.io/badge/license-MIT-green?style=flat-square)
9
- ![node](https://img.shields.io/badge/node-%5E22.19%20%7C%7C%20%3E%3D24-339933?style=flat-square)
9
+ ![node](https://img.shields.io/badge/node-%5E22.19.0%20%7C%7C%20%3E%3D24.0.0-339933?style=flat-square)
10
10
  ![DSH](https://img.shields.io/badge/DSH-%3E%3D0.2.0--rc.2-4b6ef6?style=flat-square)
11
- ![host-only](https://img.shields.io/badge/plugin-host--only-6b7280?style=flat-square)
11
+ ![plugin](https://img.shields.io/badge/plugin-host%20only-6b7280?style=flat-square)
12
+
13
+ **简体中文** · [English](README.en.md)
12
14
 
13
15
  </div>
14
16
 
@@ -43,19 +45,30 @@ dsh plugin --profile <profile> add "link:<仓库路径>/packages/dsh-visual-comp
43
45
 
44
46
  ## 使用
45
47
 
46
- 本包由**两部分**组成,都不接触模型 API:宿主插件(`lib/index.js`)只投消息,本地服务(`bin/visual-companion.mjs`)只写文件。
48
+ 本包由**两部分**组成,都不接触模型 API:
49
+
50
+ - **本地服务**(`bin/visual-companion.mjs`):零依赖 HTTP 服务,负责渲染页面、接收点击、写 `events.jsonl` 与 `pending.json`;
51
+ - **宿主插件**(`lib/index.js`):观察 `state/pending.json`,一有提交就给目标会话投一条用户消息并起一轮。
52
+
53
+ ### 起本地服务
47
54
 
48
- **1)起本地服务**(受管后台任务;目录放工作区内的临时目录):
55
+ 受管后台任务;目录放工作区内的临时目录:
49
56
 
50
- ```powershell
51
- node "$env:DSH_HOME\profiles\<profile>\node_modules\@fish-under-sea\dsh-visual-companion\bin\visual-companion.mjs" `
52
- --dir "<工作区>\.dsh-visual" --port 0 --session "$env:DSH_SESSION_ID"
57
+ ```sh
58
+ node "$DSH_HOME/profiles/<profile>/node_modules/@fish-under-sea/dsh-visual-companion/bin/visual-companion.mjs" \
59
+ --dir "<工作区>/.dsh-visual" --port 0 --session "$DSH_SESSION_ID"
53
60
  # → 打印 { url, port, key, screenDir, stateDir, pendingFile, session }
54
61
  ```
55
62
 
56
- **2)写一屏 HTML 片段**到 `<dir>\screen\`(语义化文件名、别复用;需要完全控制页面时才写完整文档,即以 `<!DOCTYPE` / `<html` 开头)。
63
+ ### 写屏
64
+
65
+ 把 HTML 片段写到 `<dir>/screen/`(语义化文件名、别复用;需要完全控制页面时才写完整文档,即以 `<!DOCTYPE` / `<html` 开头)。
57
66
 
58
- **3)打开页面**:把 `url` 连同 `?key=` 一起交给用户(DSH 里用 `sidebar_open` 开在右侧栏)。密钥缺失会被 403 拒绝。
67
+ ### 打开页面
68
+
69
+ 把 `url` 连同 `?key=` 一起交给用户(DSH 里用 `sidebar_open` 开在右侧栏)。密钥缺失会被 403 拒绝。
70
+
71
+ ### 框架样式
59
72
 
60
73
  服务自带的框架样式提供这些类,写片段即可用:
61
74
 
@@ -65,6 +78,8 @@ node "$env:DSH_HOME\profiles\<profile>\node_modules\@fish-under-sea\dsh-visual-c
65
78
  | 视觉块 | `.mockup`、`.split`、`.pros-cons`、`.mock-nav`、`.mock-sidebar`、`.mock-content`、`.mock-button`、`.mock-input`、`.placeholder` |
66
79
  | 文字层级 | `h2` / `h3` / `.subtitle` / `.section` / `.label` |
67
80
 
81
+ ### `visual_companion` 工具
82
+
68
83
  插件注册一个工具 `visual_companion`:
69
84
 
70
85
  | action | 参数 | 作用 |
@@ -74,7 +89,9 @@ node "$env:DSH_HOME\profiles\<profile>\node_modules\@fish-under-sea\dsh-visual-c
74
89
  | `status` | — | 绑定状态、唤醒次数、最近一次结果、当前 `pending.json` |
75
90
  | `wake` | `session_id`、`text` | 立刻探测唤醒链路(不经过页面) |
76
91
 
77
- 落盘文件都在 `<dir>\state\`:
92
+ ### 落盘文件
93
+
94
+ 落盘文件都在 `<dir>/state/`:
78
95
 
79
96
  | 文件 | 内容 |
80
97
  | --- | --- |
@@ -119,7 +136,7 @@ node "$env:DSH_HOME\profiles\<profile>\node_modules\@fish-under-sea\dsh-visual-c
119
136
  2. `agent.steer(...)` —— 目标正在跑时;
120
137
  3. `ctx.subagents[Symbol.for('dsh.subagent.queuePrompt' | 'deliverPrompt')]` —— 可续期子代理的宿主内部投递钩子。
121
138
 
122
- - **host-only**:不声明 `dsh.client`,无前端、无构建;
139
+ - **host-only**:宿主插件(不声明 `dsh.client`、没有浏览器半区、没有前端构建)+ 本地服务(零依赖 HTTP 服务,提供带交互的浏览器页面)。那个页面由本地服务自己提供,**不是** DSH 客户端插件;
123
140
  - 服务零依赖(只用 Node 标准库)、端口自动分配、URL 带会话密钥;`--host 0.0.0.0 --url-host <主机名>` 可用于远程 / 容器,但**必须保留 `?key=`**;
124
141
  - 服务**闲置 4 小时自动退出**(`--idle-minutes` 可调),原型文件是临时产物,别当交付物;
125
142
  - 降级链:无 Node → 自包含 HTML + `present`;无侧边栏浏览器 → 把完整 URL 交给用户手动打开;连插件都没装 → 下一轮手工读 `events.jsonl`;
@@ -130,6 +147,7 @@ node "$env:DSH_HOME\profiles\<profile>\node_modules\@fish-under-sea\dsh-visual-c
130
147
  ## 开发与测试
131
148
 
132
149
  ```sh
150
+ # 全新 clone 需先在包目录 pnpm install(运行时零依赖,但测试需要 devDependencies 里的 3 个 DSH 包)
133
151
  node packages/dsh-visual-companion/test/visual-companion.test.mjs # 语料渲染 + 清单契约 + 绑定容错(10 用例)
134
152
  pnpm pack # 产物检查:files 覆盖 bin / lib / patch / README
135
153
  ```
@@ -142,4 +160,4 @@ pnpm pack # 产物检
142
160
 
143
161
  ## 许可
144
162
 
145
- MIT © Fish-under-sea
163
+ MIT © Fish-under-sea
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * DSH 视觉伴侣(visual companion)—— 单文件、零依赖、Node 18+
3
+ * DSH 视觉伴侣(visual companion)—— 单文件、零依赖、Node `^22.19.0 || >=24.0.0`(与 package.json 的 engines 一致)
4
4
  *
5
5
  * 用途:头脑风暴时把原型 / 线框 / 并排对比渲染到浏览器(DSH 里用 sidebar_open 开在右侧栏),
6
6
  * 用户在页面上点选 A/B/C,点击事件落到 events.jsonl,模型下一轮读它。
package/lib/index.js CHANGED
@@ -21,7 +21,7 @@
21
21
  * @module @fish-under-sea/dsh-visual-companion
22
22
  */
23
23
  import { spawn } from 'node:child_process'
24
- import { mkdirSync, watch } from 'node:fs'
24
+ import { mkdirSync, readFileSync, watch } from 'node:fs'
25
25
  import { mkdir, readFile } from 'node:fs/promises'
26
26
  import { basename, join } from 'node:path'
27
27
  import { fileURLToPath } from 'node:url'
@@ -30,6 +30,20 @@ import { createUserMessage } from '@deepseek-ai/dsh-llm'
30
30
  import { defineTool } from '@deepseek-ai/dsh-tools'
31
31
  import z from '@deepseek-ai/schemastery'
32
32
 
33
+ /**
34
+ * 包版本:**从 package.json 现读**,不在代码里写死。
35
+ *
36
+ * 曾经这里写死 `v0.1.2`,之后连升三个版本它一次都没跟上 —— 用户拿加载日志去对版本时
37
+ * 会被指向错误的版本号。读不到就退化成 `unknown`(只是日志,不该因为读文件失败而影响加载)。
38
+ */
39
+ const VERSION = (() => {
40
+ try {
41
+ return JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version ?? 'unknown'
42
+ } catch {
43
+ return 'unknown'
44
+ }
45
+ })()
46
+
33
47
  import { companionMessage } from './message.js'
34
48
 
35
49
  export { companionMessage }
@@ -332,5 +346,5 @@ export function apply(ctx, config) {
332
346
  const watchDir = clean(config?.watchDir)
333
347
  // 目标会话不在这里钉死:由 pending.json 自带(伴侣服务用 --session 写入)。
334
348
  if (watchDir !== '') arm(ctx, watchDir, null)
335
- ctx.logger?.info?.(`visual-companion: 已加载(v0.1.2)${watchDir === '' ? ',未自动绑定' : `,自动绑定 ${watchDir}`}`)
349
+ ctx.logger?.info?.(`visual-companion: 已加载(v${VERSION})${watchDir === '' ? ',未自动绑定' : `,自动绑定 ${watchDir}`}`)
336
350
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fish-under-sea/dsh-visual-companion",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "把「视觉伴侣页面上的提交」变成一条用户消息:用户在网页上点选、写备注、按提交,DSH 会话自动起一轮继续工作,不必回终端复述。",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -14,7 +14,8 @@
14
14
  "bin",
15
15
  "lib",
16
16
  "cordis.patch.yml",
17
- "README.md"
17
+ "README.md",
18
+ "README.en.md"
18
19
  ],
19
20
  "license": "MIT",
20
21
  "publishConfig": {