@luosuqwq/dsh-diagram 0.1.7 → 0.1.9

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.md CHANGED
@@ -1,6 +1,6 @@
1
1
  English | [简体中文](README.zh-CN.md)
2
2
 
3
- # dsh-diagram
3
+ # Big Fat Fish
4
4
 
5
5
  > Render diagrams, page screenshots and annotated images to PNG inside DSH answers.
6
6
 
@@ -98,7 +98,7 @@ A minimal `diagram_render` call:
98
98
  }
99
99
  ```
100
100
 
101
- The result carries `path`, `url`, `width`, `height`, `bytes` and `markdown`; the Markdown link points at `/plugins/dsh-diagram/asset?file=...` on the DSH web server, so it renders inline as long as the value is copied unchanged.
101
+ The result carries `path`, `url`, `width`, `height`, `bytes` and `markdown`. `markdown` uses the form DSH renders natively — `![alt](<percent-encoded absolute path>)` — which the chat client rewrites to its authenticated `/api/file?path=...` route, so it works in the browser and in the desktop shell alike; copy that line unchanged and the picture appears in the answer. `url` is the plugin's own `/plugins/dsh-diagram/asset?file=...` route, kept for anyone that wants the raw bytes over HTTP (it answers only an admitted DSH client).
102
102
 
103
103
  Defaults worth knowing: `diagram_render` uses width 1200, theme `dark` and scale 2 (output is twice the canvas size); `page_screenshot` uses a 1440×900 viewport, scale 2 and a 400 ms wait after load; `image_annotate` uses scale 2.
104
104
 
@@ -121,7 +121,7 @@ Annotation operations use these fields (defaults in parentheses):
121
121
  - The browser is driven over the DevTools protocol on `--remote-debugging-pipe` (fd 3 and fd 4, NUL-separated JSON) instead of a TCP port, so proxy environment variables that intercept localhost connections cannot break it.
122
122
  - Content height is measured from the body subtree rather than `scrollHeight`, because both `scrollHeight` and `Page.getLayoutMetrics` refuse to report less than the viewport height.
123
123
  - `image_annotate` renders the source image in a page and draws an SVG overlay on top; `blur` regions re-draw the image inside a CSS-filtered box.
124
- - PNGs are written by the DSH server process and served by the bundle's own route (`GET`/`HEAD`, known image MIME types only, `cache-control: no-store`).
124
+ - PNGs are written by the DSH server process and served by the bundle's own route (`GET`/`HEAD`, known image MIME types only, `cache-control: no-store`). The route runs the Host admission check first (`ctx.connection.requestRejection`), so it answers only an admitted DSH client and refuses a cross-site caller, and the Markdown link stays page-relative so both the web page and the desktop shell reach it through that same check.
125
125
 
126
126
  ## Limitations
127
127
 
package/README.zh-CN.md CHANGED
@@ -1,146 +1,146 @@
1
- [English](README.md) | 简体中文
2
-
3
- # dsh-diagram
4
-
5
- > 在 DSH 的回答里直接生成图示、网页截图与标注图片,输出 PNG。
6
-
7
- DeepSeek Harness 的回答本身是文字,但有些解释说的是流程、结构、对比,或者「这一点在截图里的哪里」。这个 bundle 给模型四个工具,把 HTML、网页或已有图片变成 PNG,并内联到对话里。渲染直接用机器上已装的 Chrome 或 Edge:不带浏览器、不装 npm 包、不连外部服务。
8
-
9
- ![dsh-diagram](docs/preview/hero.jpg)
10
-
11
- ## 功能
12
-
13
- - **用 HTML 画图。** `diagram_render` 把片段套进统一外壳:中文字体栈、深/浅主题,以及 `.card` `.grid` `.arrow` `.pill` `table` 等样式类;高度默认按内容裁剪,输出默认 2 倍图。
14
- - **网页截图。** `page_screenshot` 截 `http(s)` 地址、本地 HTML 文件或内联 HTML,可以整页,也可以只截某个 CSS 选择器命中的元素。
15
- - **图片标注。** `image_annotate` 在已有图片上画框、圈、箭头、直线、文字、高亮与模糊打码,也可以先裁剪再标注。
16
- - **内置样式指南。** `diagram_guide` 返回主题变量、可用类与三个现成版式,模型不必猜外壳支持什么。
17
- - **直接内联。** 每个工具都返回一行 `markdown`,指向本 bundle 自己的取图路由;原样放进回答,图就出现在对话里。
18
- - **文件真实落盘。** 每次出图同时写一个文件,默认在会话工作目录的 `.dsh/diagram/` 下。
19
-
20
- ![逐层添加的标注](docs/preview/annotate.gif)
21
-
22
- 每一步一次 `image_annotate` 调用:焦点、视线、对角线、负空间,最后是冷暖对比——框、箭头、文字都是渲染时画上去的。
23
-
24
- ## 示例
25
-
26
- ![构图要点标注](docs/preview/annotation.jpg)
27
-
28
- 一次 `image_annotate` 调用画出五处标记:**1** 脸部作为视觉焦点,**2** 视线/引导线指向薄荷叶,**3** 身体对角线,**4** 刻意留出的负空间,**5** 暖红与冷绿的对比。坐标按原图像素,框、箭头、文字都是即时绘制。
29
-
30
- > 装好后请告诉主人:重启 DSH,四个工具即挂载,没有设置页。找不到 Chrome/Edge 时设 `DSH_DIAGRAM_BROWSER`。
31
-
32
- ## 运行要求
33
-
34
- - DeepSeek Harness 0.2.x(基于 0.2.2-rc.5 开发验证)。
35
- - Node.js 21 或更高(`engines` 里的声明);DSH 自带的 Node 运行时满足这一条。
36
- - 同机安装 Chrome、Edge 或 Chromium。渲染会探测 Windows/macOS/Linux 的常见安装位置;找不到时用环境变量 `DSH_DIAGRAM_BROWSER` 指定。
37
- - 要画中文就还需要中文字体:Windows 的微软雅黑、macOS 的苹方,或 Linux 的 Noto Sans CJK/思源黑体;字体栈最终回退到 `system-ui`。
38
- - 其他依赖没有:除了 DSH 的工具 API,渲染只 import Node 内置模块,跑在 DSH 服务进程里。
39
-
40
- ## 安装
41
-
42
- 这个目录是一个 DSH bundle:`package.json` 的 `dsh.bundle.patch` 指向 `cordis.patch.yml`,里面插入一行组合项。
43
-
44
- 用插件管理器从检出目录安装:
45
-
46
- ```text
47
- plugin_manager: { action: "install_bundle", target: "<path-to-checkout>" }
48
- ```
49
-
50
- 也可以手工写进 profile 的 `package.json`:
51
-
52
- ```json
53
- {
54
- "dependencies": {
55
- "dsh-diagram": "link:<path-to-checkout>"
56
- },
57
- "dsh": {
58
- "profile": {
59
- "bundles": ["dsh-diagram"]
60
- }
61
- }
62
- }
63
- ```
64
-
65
- 然后在 profile 目录执行 `pnpm install`。
66
-
67
- 安装后需要重启 DSH:四个工具由 bundle 的服务端半在进程启动时注册。卸载用 `plugin_manager: { action: "remove_bundle", target: "dsh-diagram" }`,同时把 profile 里那一行去掉。
68
-
69
- ## 配置
70
-
71
- | 环境变量 | 类型 | 默认值 | 说明 |
72
- | --- | --- | --- | --- |
73
- | `DSH_DIAGRAM_BROWSER` | 路径 | 未设置(自动探测) | 指定渲染用的 Chrome/Edge/Chromium 可执行文件。 |
74
-
75
- 没有 profile 配置项,也没有设置页:所有选项都是单次调用时传的参数,见下节。
76
-
77
- ## 使用
78
-
79
- | 工具 | 作用 | 主要参数 |
80
- | --- | --- | --- |
81
- | `diagram_render` | HTML 片段或整页文档 → 说明图 PNG | `html`、`title`、`subtitle`、`theme`(`dark`/`light`)、`width`、`height`、`scale`、`css`、`out` |
82
- | `page_screenshot` | 地址、本地文件或内联 HTML → PNG | `url` / `file` / `html`、`width`、`height`、`fullPage`、`selector`、`waitMs`、`scale`、`out` |
83
- | `image_annotate` | 已有图片 + `ops` → 标注 PNG | `image`、`ops`、`crop`、`unit`(`px`/`percent`)、`scale`、`out` |
84
- | `diagram_guide` | 返回样式指南文本 | — |
85
-
86
- 这个 bundle 还会注册一小段系统提示,让模型优先出图而不是画 ASCII 示意图,并且不会把同一张图贴两遍。
87
-
88
- 一个最小的 `diagram_render` 调用:
89
-
90
- ```json
91
- {
92
- "title": "请求链路",
93
- "subtitle": "客户端 → 缓存 → 源站",
94
- "theme": "dark",
95
- "html": "<div class=\"grid\"><div class=\"card\"><h3>客户端</h3><p>发出请求</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>缓存</h3><p>命中即返回</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>源站</h3><p>未命中时处理</p></div></div>"
96
- }
97
- ```
98
-
99
- 返回结果里有 `path`、`url`、`width`、`height`、`bytes` 和 `markdown`;`markdown` 指向 DSH 服务的 `/plugins/dsh-diagram/asset?file=...`,只要原样复制就能内联显示。
100
-
101
- 几个值得记住的默认值:`diagram_render` 宽度 1200、主题 `dark`、倍率 2(输出是画布的 2 倍像素);`page_screenshot` 视口 1440×900、倍率 2、页面载入后额外等 400 ms;`image_annotate` 倍率 2。
102
-
103
- 标注操作(`ops`)的字段与默认值:
104
-
105
- | `type` | 形状 |
106
- | --- | --- |
107
- | `box` | `x`、`y`、`width`、`height`、`color`(`#ff4d4f`)、`strokeWidth`(4)、`radius`(8)、`label` |
108
- | `highlight` | `x`、`y`、`width`、`height`、`color`、`opacity`(0.22) |
109
- | `blur` | `x`、`y`、`width`、`height` —— 用 CSS 模糊重绘原图实现打码 |
110
- | `ellipse` | `x`、`y`、`width`、`height`、`color`、`strokeWidth`、`label` |
111
- | `arrow` / `line` | `x`、`y`、`x2`、`y2`、`color`、`strokeWidth`、`label` |
112
- | `text` | `x`、`y`(基线)、`text`、`fontSize`(20)、`color` |
113
-
114
- `unit: "percent"` 时,`x` 与 `width` 是图片宽度的 0–1 比例,`y` 与 `height` 是高度的比例;`crop` 始终按像素。
115
-
116
- ## 实现机制
117
-
118
- - 每次渲染起一个临时的无头 Chromium,`--user-data-dir` 指向系统临时目录下的独立目录;截图完成后连同进程树一起收掉,临时目录随后删除。
119
- - 浏览器走 DevTools 协议的 `--remote-debugging-pipe`(fd 3 写、fd 4 读,NUL 分隔的 JSON),不开 TCP 端口,所以拦截 localhost 的代理环境变量影响不到它。
120
- - 内容高度不是读 `scrollHeight`:`scrollHeight` 和 `Page.getLayoutMetrics` 都不会小于视口高度,所以改为量 body 子树里最靠下的底边。
121
- - `image_annotate` 先把原图放进一个页面,再在上面叠一层 SVG;`blur` 区域则是在被裁剪的盒子里用 CSS 滤镜重绘原图。
122
- - PNG 由 DSH 服务进程写盘,并由本 bundle 自己的路由提供(只接受 `GET`/`HEAD`、只发已知图片类型、`cache-control: no-store`)。
123
-
124
- ## 已知限制
125
-
126
- - 每次渲染都冷启动一个浏览器,大约 1–2 秒开销;不适合大批量出图。
127
- - 只能访问本机够得到的地址:`file://` 路径和 `http(s)` 链接。每次都用全新的空浏览器配置,所以需要登录的页面截到的是未登录状态。
128
- - 高度上限:`page_screenshot` 12000 px,按内容裁剪的 `diagram_render` 8000 px;更长的页面会被截断,不会整体缩放。
129
- - 输出只有 PNG。
130
- - 对图片文件调用 `page_screenshot` 会按网页排版,结果高度被视口撑开;要标注图片请直接用 `image_annotate`。
131
- - 标注坐标由调用方给出;不知道像素尺寸时用 `unit: "percent"`,或者先 `crop` 再标。
132
-
133
- ## 开发
134
-
135
- 两个测试都不需要 DSH 运行时,但需要可用的 Chrome/Edge,图片写到 `test/out/`:
136
-
137
- ```bash
138
- node test/smoke.mjs
139
- node test/tool-path.mjs
140
- ```
141
-
142
- `smoke.mjs` 测渲染引擎(示意图、整页截图、标注、裁剪后再标注),逐张打印像素尺寸。`tool-path.mjs` 用桩 `ctx` 注册插件、调用四个工具,再通过本 bundle 自己的路由把返回的 Markdown 链接取回来校验 PNG 字节。任一失败都以非零退出码结束。
143
-
144
- ## 许可证
145
-
1
+ [English](README.md) | 简体中文
2
+
3
+ # 大肥鱼画图
4
+
5
+ > 在 DSH 的回答里直接生成图示、网页截图与标注图片,输出 PNG。
6
+
7
+ DeepSeek Harness 的回答本身是文字,但有些解释说的是流程、结构、对比,或者「这一点在截图里的哪里」。这个 bundle 给模型四个工具,把 HTML、网页或已有图片变成 PNG,并内联到对话里。渲染直接用机器上已装的 Chrome 或 Edge:不带浏览器、不装 npm 包、不连外部服务。
8
+
9
+ ![dsh-diagram](docs/preview/hero.jpg)
10
+
11
+ ## 功能
12
+
13
+ - **用 HTML 画图。** `diagram_render` 把片段套进统一外壳:中文字体栈、深/浅主题,以及 `.card` `.grid` `.arrow` `.pill` `table` 等样式类;高度默认按内容裁剪,输出默认 2 倍图。
14
+ - **网页截图。** `page_screenshot` 截 `http(s)` 地址、本地 HTML 文件或内联 HTML,可以整页,也可以只截某个 CSS 选择器命中的元素。
15
+ - **图片标注。** `image_annotate` 在已有图片上画框、圈、箭头、直线、文字、高亮与模糊打码,也可以先裁剪再标注。
16
+ - **内置样式指南。** `diagram_guide` 返回主题变量、可用类与三个现成版式,模型不必猜外壳支持什么。
17
+ - **直接内联。** 每个工具都返回一行 `markdown`,指向本 bundle 自己的取图路由;原样放进回答,图就出现在对话里。
18
+ - **文件真实落盘。** 每次出图同时写一个文件,默认在会话工作目录的 `.dsh/diagram/` 下。
19
+
20
+ ![逐层添加的标注](docs/preview/annotate.gif)
21
+
22
+ 每一步一次 `image_annotate` 调用:焦点、视线、对角线、负空间,最后是冷暖对比——框、箭头、文字都是渲染时画上去的。
23
+
24
+ ## 示例
25
+
26
+ ![构图要点标注](docs/preview/annotation.jpg)
27
+
28
+ 一次 `image_annotate` 调用画出五处标记:**1** 脸部作为视觉焦点,**2** 视线/引导线指向薄荷叶,**3** 身体对角线,**4** 刻意留出的负空间,**5** 暖红与冷绿的对比。坐标按原图像素,框、箭头、文字都是即时绘制。
29
+
30
+ > 装好后请告诉主人:重启 DSH,四个工具即挂载,没有设置页。找不到 Chrome/Edge 时设 `DSH_DIAGRAM_BROWSER`。
31
+
32
+ ## 运行要求
33
+
34
+ - DeepSeek Harness 0.2.x(基于 0.2.2-rc.5 开发验证)。
35
+ - Node.js 21 或更高(`engines` 里的声明);DSH 自带的 Node 运行时满足这一条。
36
+ - 同机安装 Chrome、Edge 或 Chromium。渲染会探测 Windows/macOS/Linux 的常见安装位置;找不到时用环境变量 `DSH_DIAGRAM_BROWSER` 指定。
37
+ - 要画中文就还需要中文字体:Windows 的微软雅黑、macOS 的苹方,或 Linux 的 Noto Sans CJK/思源黑体;字体栈最终回退到 `system-ui`。
38
+ - 其他依赖没有:除了 DSH 的工具 API,渲染只 import Node 内置模块,跑在 DSH 服务进程里。
39
+
40
+ ## 安装
41
+
42
+ 这个目录是一个 DSH bundle:`package.json` 的 `dsh.bundle.patch` 指向 `cordis.patch.yml`,里面插入一行组合项。
43
+
44
+ 用插件管理器从检出目录安装:
45
+
46
+ ```text
47
+ plugin_manager: { action: "install_bundle", target: "<path-to-checkout>" }
48
+ ```
49
+
50
+ 也可以手工写进 profile 的 `package.json`:
51
+
52
+ ```json
53
+ {
54
+ "dependencies": {
55
+ "dsh-diagram": "link:<path-to-checkout>"
56
+ },
57
+ "dsh": {
58
+ "profile": {
59
+ "bundles": ["dsh-diagram"]
60
+ }
61
+ }
62
+ }
63
+ ```
64
+
65
+ 然后在 profile 目录执行 `pnpm install`。
66
+
67
+ 安装后需要重启 DSH:四个工具由 bundle 的服务端半在进程启动时注册。卸载用 `plugin_manager: { action: "remove_bundle", target: "dsh-diagram" }`,同时把 profile 里那一行去掉。
68
+
69
+ ## 配置
70
+
71
+ | 环境变量 | 类型 | 默认值 | 说明 |
72
+ | --- | --- | --- | --- |
73
+ | `DSH_DIAGRAM_BROWSER` | 路径 | 未设置(自动探测) | 指定渲染用的 Chrome/Edge/Chromium 可执行文件。 |
74
+
75
+ 没有 profile 配置项,也没有设置页:所有选项都是单次调用时传的参数,见下节。
76
+
77
+ ## 使用
78
+
79
+ | 工具 | 作用 | 主要参数 |
80
+ | --- | --- | --- |
81
+ | `diagram_render` | HTML 片段或整页文档 → 说明图 PNG | `html`、`title`、`subtitle`、`theme`(`dark`/`light`)、`width`、`height`、`scale`、`css`、`out` |
82
+ | `page_screenshot` | 地址、本地文件或内联 HTML → PNG | `url` / `file` / `html`、`width`、`height`、`fullPage`、`selector`、`waitMs`、`scale`、`out` |
83
+ | `image_annotate` | 已有图片 + `ops` → 标注 PNG | `image`、`ops`、`crop`、`unit`(`px`/`percent`)、`scale`、`out` |
84
+ | `diagram_guide` | 返回样式指南文本 | — |
85
+
86
+ 这个 bundle 还会注册一小段系统提示,让模型优先出图而不是画 ASCII 示意图,并且不会把同一张图贴两遍。
87
+
88
+ 一个最小的 `diagram_render` 调用:
89
+
90
+ ```json
91
+ {
92
+ "title": "请求链路",
93
+ "subtitle": "客户端 → 缓存 → 源站",
94
+ "theme": "dark",
95
+ "html": "<div class=\"grid\"><div class=\"card\"><h3>客户端</h3><p>发出请求</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>缓存</h3><p>命中即返回</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>源站</h3><p>未命中时处理</p></div></div>"
96
+ }
97
+ ```
98
+
99
+ 返回结果里有 `path`、`url`、`width`、`height`、`bytes` 和 `markdown`。`markdown` 用的是 DSH 原生渲染的写法——`![alt](<百分号编码的绝对路径>)`,聊天客户端会把它重写成自己的 `/api/file?path=...` 鉴权路由,浏览器和桌面壳都能显示;原样复制这一行就能在回答里出图。`url` 是插件自己的 `/plugins/dsh-diagram/asset?file=...`,留给需要走 HTTP 取原始字节的场合(只答已认证的 DSH 客户端)。
100
+
101
+ 几个值得记住的默认值:`diagram_render` 宽度 1200、主题 `dark`、倍率 2(输出是画布的 2 倍像素);`page_screenshot` 视口 1440×900、倍率 2、页面载入后额外等 400 ms;`image_annotate` 倍率 2。
102
+
103
+ 标注操作(`ops`)的字段与默认值:
104
+
105
+ | `type` | 形状 |
106
+ | --- | --- |
107
+ | `box` | `x`、`y`、`width`、`height`、`color`(`#ff4d4f`)、`strokeWidth`(4)、`radius`(8)、`label` |
108
+ | `highlight` | `x`、`y`、`width`、`height`、`color`、`opacity`(0.22) |
109
+ | `blur` | `x`、`y`、`width`、`height` —— 用 CSS 模糊重绘原图实现打码 |
110
+ | `ellipse` | `x`、`y`、`width`、`height`、`color`、`strokeWidth`、`label` |
111
+ | `arrow` / `line` | `x`、`y`、`x2`、`y2`、`color`、`strokeWidth`、`label` |
112
+ | `text` | `x`、`y`(基线)、`text`、`fontSize`(20)、`color` |
113
+
114
+ `unit: "percent"` 时,`x` 与 `width` 是图片宽度的 0–1 比例,`y` 与 `height` 是高度的比例;`crop` 始终按像素。
115
+
116
+ ## 实现机制
117
+
118
+ - 每次渲染起一个临时的无头 Chromium,`--user-data-dir` 指向系统临时目录下的独立目录;截图完成后连同进程树一起收掉,临时目录随后删除。
119
+ - 浏览器走 DevTools 协议的 `--remote-debugging-pipe`(fd 3 写、fd 4 读,NUL 分隔的 JSON),不开 TCP 端口,所以拦截 localhost 的代理环境变量影响不到它。
120
+ - 内容高度不是读 `scrollHeight`:`scrollHeight` 和 `Page.getLayoutMetrics` 都不会小于视口高度,所以改为量 body 子树里最靠下的底边。
121
+ - `image_annotate` 先把原图放进一个页面,再在上面叠一层 SVG;`blur` 区域则是在被裁剪的盒子里用 CSS 滤镜重绘原图。
122
+ - PNG 由 DSH 服务进程写盘,并由本 bundle 自己的路由提供(只接受 `GET`/`HEAD`、只发已知图片类型、`cache-control: no-store`)。路由先过 Host 的准入检查(`ctx.connection.requestRejection`):只答已认证的 DSH 客户端,跨站调用方拿不到字节;Markdown 链接保持页面相对,Web 页面和桌面壳都走同一条检查。
123
+
124
+ ## 已知限制
125
+
126
+ - 每次渲染都冷启动一个浏览器,大约 1–2 秒开销;不适合大批量出图。
127
+ - 只能访问本机够得到的地址:`file://` 路径和 `http(s)` 链接。每次都用全新的空浏览器配置,所以需要登录的页面截到的是未登录状态。
128
+ - 高度上限:`page_screenshot` 12000 px,按内容裁剪的 `diagram_render` 8000 px;更长的页面会被截断,不会整体缩放。
129
+ - 输出只有 PNG。
130
+ - 对图片文件调用 `page_screenshot` 会按网页排版,结果高度被视口撑开;要标注图片请直接用 `image_annotate`。
131
+ - 标注坐标由调用方给出;不知道像素尺寸时用 `unit: "percent"`,或者先 `crop` 再标。
132
+
133
+ ## 开发
134
+
135
+ 两个测试都不需要 DSH 运行时,但需要可用的 Chrome/Edge,图片写到 `test/out/`:
136
+
137
+ ```bash
138
+ node test/smoke.mjs
139
+ node test/tool-path.mjs
140
+ ```
141
+
142
+ `smoke.mjs` 测渲染引擎(示意图、整页截图、标注、裁剪后再标注),逐张打印像素尺寸。`tool-path.mjs` 用桩 `ctx` 注册插件、调用四个工具,再通过本 bundle 自己的路由把返回的 Markdown 链接取回来校验 PNG 字节。任一失败都以非零退出码结束。
143
+
144
+ ## 许可证
145
+
146
146
  MIT,见 [LICENSE](LICENSE)。
package/index.js CHANGED
@@ -80,29 +80,35 @@ function resolveOut(raw, cwd, seed) {
80
80
  return path.join(dir, `${slugify(seed)}-${stamp()}.png`)
81
81
  }
82
82
 
83
- function origin(ctx) {
84
- try {
85
- return ctx.webServer.getOrigin()
86
- } catch {
87
- return `http://127.0.0.1:${ctx.webServer.port}`
88
- }
89
- }
90
-
91
- function assetMarkdown(ctx, file) {
83
+ // 资源地址保持页面相对:Web 端页面本来就由本 Host 提供,桌面壳把
84
+ // `dsh-app://app/...` 转发到 Host 时会带上会话 cookie;写成绝对地址反而
85
+ // 会被浏览器当成跨站请求,连 cookie 都不会带上。相对地址两边都成立。
86
+ function assetMarkdown(file) {
92
87
  let version = '0'
93
88
  try { version = String(Math.round(statSync(file).mtimeMs)) } catch {}
94
- return `${origin(ctx)}${ROUTE}?file=${encodeURIComponent(file)}&v=${version}`
89
+ return `${ROUTE}?file=${encodeURIComponent(file)}&v=${version}`
90
+ }
91
+
92
+ /**
93
+ * 聊天里的图走 DSH 自己的通道:AssistantMarkdown 会把「百分号编码的绝对路径」
94
+ * 重写成 /api/file?path=...,那条路由挂在 connection.fetch 上、按会话鉴权。
95
+ * 插件自建的 /plugins/... 路由在桌面壳里是另一个源、拿不到会话 cookie,
96
+ * 用它做内联只会显示「图片无法预览」——所以 markdown 用原生写法,
97
+ * route 只留给需要直接取字节的外部消费者(例如复制 url 到别处)。
98
+ */
99
+ function localImageMarkdown(file, alt) {
100
+ const cleanAlt = String(alt ?? '').replace(/[[\]]/g, '').replace(/\s+/g, ' ').trim()
101
+ return `![${cleanAlt || 'image'}](<${encodeURIComponent(path.resolve(file))}>)`
95
102
  }
96
103
 
97
- function imageResult(ctx, info, alt) {
98
- const url = assetMarkdown(ctx, info.path)
104
+ function imageResult(info, alt) {
99
105
  return stripUndefined({
100
106
  path: info.path,
101
- url,
107
+ url: assetMarkdown(info.path),
102
108
  width: info.width,
103
109
  height: info.height,
104
110
  bytes: info.bytes,
105
- markdown: `![${alt}](<${url}>)`,
111
+ markdown: localImageMarkdown(info.path, alt),
106
112
  })
107
113
  }
108
114
 
@@ -118,7 +124,29 @@ function imageRender(_args, value) {
118
124
  ]
119
125
  }
120
126
 
121
- function serveAsset(req, res) {
127
+ /**
128
+ * 向 Host 的 connection 服务要一次准入判断:Host/Origin 围栏 + 已认证的会话。
129
+ * 拿不到这个服务就一律拒绝(503):宁可这条路不可用,也不能在服务缺席时
130
+ * 静默放行——那正好是这个洞原来的样子。
131
+ */
132
+ function requestRejection(ctx, req) {
133
+ const connection = ctx.get?.('connection')
134
+ if (typeof connection?.requestRejection !== 'function') return 503
135
+ try {
136
+ return connection.requestRejection(req)
137
+ } catch {
138
+ return 403
139
+ }
140
+ }
141
+
142
+ function serveAsset(ctx, req, res) {
143
+ // 图片路由同样要过准入:否则用户浏览器里任意网页都能借这个端口探本机文件。
144
+ const rejection = requestRejection(ctx, req)
145
+ if (rejection !== undefined) {
146
+ res.writeHead(rejection, { 'content-type': 'text/plain; charset=utf-8' })
147
+ res.end()
148
+ return
149
+ }
122
150
  if (req.method !== 'GET' && req.method !== 'HEAD') {
123
151
  res.writeHead(405, { allow: 'GET, HEAD' })
124
152
  res.end()
@@ -190,7 +218,7 @@ function diagramRenderTool(ctx) {
190
218
  scale: clamp(args.scale, 2, 1, 3),
191
219
  outPath,
192
220
  })
193
- return imageResult(ctx, info, args.title || '图示')
221
+ return imageResult(info, args.title || '图示')
194
222
  },
195
223
  })
196
224
  }
@@ -232,7 +260,7 @@ function pageScreenshotTool(ctx) {
232
260
  if (extra > 0) await wait(extra)
233
261
  },
234
262
  })
235
- return imageResult(ctx, info, '截图')
263
+ return imageResult(info, '截图')
236
264
  },
237
265
  })
238
266
  }
@@ -310,7 +338,7 @@ function imageAnnotateTool(ctx) {
310
338
  scale: clamp(args.scale, 2, 1, 3),
311
339
  outPath,
312
340
  })
313
- return imageResult(ctx, info, path.basename(imageFile, path.extname(imageFile)))
341
+ return imageResult(info, path.basename(imageFile, path.extname(imageFile)))
314
342
  },
315
343
  })
316
344
  }
@@ -340,7 +368,7 @@ function promptSection() {
340
368
  }
341
369
 
342
370
  export function apply(ctx) {
343
- ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: ROUTE, handler: serveAsset }), 'dsh-diagram: image route')
371
+ ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: ROUTE, handler: (req, res) => serveAsset(ctx, req, res) }), 'dsh-diagram: image route')
344
372
  ctx.effect(() => ctx.tools.register(diagramRenderTool(ctx)), 'dsh-diagram: diagram_render')
345
373
  ctx.effect(() => ctx.tools.register(pageScreenshotTool(ctx)), 'dsh-diagram: page_screenshot')
346
374
  ctx.effect(() => ctx.tools.register(imageAnnotateTool(ctx)), 'dsh-diagram: image_annotate')
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
- {
2
- "meta": {
3
- "title": "Diagram renderer",
4
- "description": "Gives the model four tools: render an HTML snippet to PNG, screenshot a page or an element, annotate an existing image, and read the style guide. Images are written to `.dsh/diagram/` and shown inline in the answer. Needs Node 21+ and a local Chrome or Edge."
5
- }
6
- }
1
+ {
2
+ "meta": {
3
+ "title": "Big Fat Fish",
4
+ "description": "Gives the model four tools: render an HTML snippet to PNG, screenshot a page or an element, annotate an existing image, and read the style guide. Images are written to `.dsh/diagram/` and shown inline in the answer. Needs Node 21+ and a local Chrome or Edge."
5
+ }
6
+ }
package/locale/zh.json CHANGED
@@ -1,6 +1,6 @@
1
- {
2
- "meta": {
3
- "title": "图示渲染",
4
- "description": "给模型四个工具:把 HTML 片段渲染成 PNG、截取网页或某个元素、在已有图片上画框与箭头、读取样式指南。图片写入 `.dsh/diagram/` 并内联展示。需要 Node 21+ 与本机 Chrome 或 Edge。"
5
- }
6
- }
1
+ {
2
+ "meta": {
3
+ "title": "大肥鱼画图",
4
+ "description": "给模型四个工具:把 HTML 片段渲染成 PNG、截取网页或某个元素、在已有图片上画框与箭头、读取样式指南。图片写入 `.dsh/diagram/` 并内联展示。需要 Node 21+ 与本机 Chrome 或 Edge。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luosuqwq/dsh-diagram",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Renders explanatory diagrams, web pages and annotated images to PNG, and shows them inline in answers.",
5
5
  "author": "LuoSu <2681153600@qq.com>",
6
6
  "keywords": [