@luosuqwq/dsh-diagram 0.1.4 → 0.1.5
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 +153 -147
- package/index.js +355 -352
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,148 +1,154 @@
|
|
|
1
|
-
English | [简体中文](README.zh-CN.md)
|
|
2
|
-
|
|
3
|
-
# dsh-diagram
|
|
4
|
-
|
|
5
|
-
> Render diagrams, page screenshots and annotated images to PNG inside DSH answers.
|
|
6
|
-
|
|
7
|
-
A DeepSeek Harness answer is text, but some explanations are really about a flow, a structure, a comparison or "where exactly in this screenshot". This bundle gives the agent four tools that turn HTML, a web page or an existing image into a PNG file and inline it in the conversation. Rendering uses the Chrome or Edge already installed on the machine: no bundled browser, no npm packages, no external service.
|
|
8
|
-
|
|
9
|
-

|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- **Diagrams from HTML.** `diagram_render` wraps a fragment in a shell with a CJK font stack, a dark or light theme and `.card` `.grid` `.arrow` `.pill` `table` utility classes, then crops to the content height and renders at 2× by default.
|
|
14
|
-
- **Page screenshots.** `page_screenshot` captures an `http(s)` URL, a local HTML file or inline HTML — the whole page or just the element matched by a CSS selector.
|
|
15
|
-
- **Annotations.** `image_annotate` draws boxes, ellipses, arrows, lines, text labels, highlights and blur redaction on an existing image, optionally after cropping it.
|
|
16
|
-
- **Built-in style guide.** `diagram_guide` returns the theme variables, available classes and three ready-made layouts, so the agent does not have to guess the shell.
|
|
17
|
-
- **Inline in chat.** Every tool returns a `markdown` line served from the bundle's own asset route; paste it as-is and the picture appears in the answer.
|
|
18
|
-
- **Real files on disk.** Each render is also saved as a file, by default under `.dsh/diagram/` in the session working directory.
|
|
19
|
-
|
|
20
|
-

|
|
21
|
-
|
|
22
|
-
One `image_annotate` call per step: the focal point, the gaze line, the body diagonal, negative space, then the warm/cool accent — all shapes, arrows and labels are drawn at render time.
|
|
23
|
-
|
|
24
|
-
## Example
|
|
25
|
-
|
|
26
|
-

|
|
27
|
-
|
|
28
|
-
One call to `image_annotate` produced all five marks on this picture: **1** the face as the focal point, **2** the gaze / visual lead-in toward the mint, **3** the body diagonal, **4** deliberate negative space, **5** the warm-red vs cool-green accent. Coordinates are original pixels; shapes, arrows and labels are drawn on the fly.
|
|
29
|
-
|
|
30
|
-
> After installing, tell the owner: restart DSH; the four tools are mounted with no settings page. If no Chrome/Edge is found, set `DSH_DIAGRAM_BROWSER`.
|
|
31
|
-
|
|
32
|
-
## Requirements
|
|
33
|
-
|
|
34
|
-
- DeepSeek Harness 0.2.x (developed against 0.2.2-rc.5).
|
|
35
|
-
- Node.js 21 or newer, as declared in `engines`; the Node runtime that ships with DSH satisfies it.
|
|
36
|
-
- Chrome, Edge or Chromium installed on the same machine. The engine probes the usual Windows, macOS and Linux locations; set `DSH_DIAGRAM_BROWSER` when it cannot find one.
|
|
37
|
-
- A CJK font for Chinese labels — Microsoft YaHei on Windows, PingFang on macOS, Noto Sans CJK or Source Han Sans on Linux. The font stack falls back to `system-ui`.
|
|
38
|
-
- Nothing else: apart from the DSH tool API, the renderer imports only Node built-ins, and it runs inside the DSH server process.
|
|
39
|
-
|
|
40
|
-
## Install
|
|
41
|
-
|
|
42
|
-
This directory is a DSH bundle: `package.json` points `dsh.bundle.patch` at `cordis.patch.yml`, which inserts one composition row.
|
|
43
|
-
|
|
44
|
-
Install it from the checkout with the plugin manager:
|
|
45
|
-
|
|
46
|
-
```text
|
|
47
|
-
plugin_manager: { action: "install_bundle", target: "<path-to-checkout>" }
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
dsh plugin --profile <profile> add @luosuqwq/dsh-diagram
|
|
51
|
-
|
|
52
|
-
Or add it to a profile by hand — in the profile's `package.json`:
|
|
53
|
-
|
|
54
|
-
```json
|
|
55
|
-
{
|
|
56
|
-
"dependencies": {
|
|
57
|
-
"@luosuqwq/dsh-diagram": "link:<path-to-checkout>"
|
|
58
|
-
},
|
|
59
|
-
"dsh": {
|
|
60
|
-
"profile": {
|
|
61
|
-
"bundles": ["@luosuqwq/dsh-diagram"]
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
then run `pnpm install` in the profile directory.
|
|
68
|
-
|
|
69
|
-
Restart DSH after installing: the tools are registered by the server-side half of the bundle when the process starts. Removing the bundle is `plugin_manager: { action: "remove_bundle", target: "dsh-diagram" }`, plus dropping the row from the profile.
|
|
70
|
-
|
|
71
|
-
## Configuration
|
|
72
|
-
|
|
73
|
-
| Variable | Type | Default | Purpose |
|
|
74
|
-
| --- | --- | --- | --- |
|
|
75
|
-
| `DSH_DIAGRAM_BROWSER` | path | unset (auto-detect) | Full path to the Chrome/Edge/Chromium executable used for rendering. |
|
|
76
|
-
|
|
77
|
-
There are no profile config keys and no settings page: every option is a per-call tool argument, listed under Usage.
|
|
78
|
-
|
|
79
|
-
## Usage
|
|
80
|
-
|
|
81
|
-
| Tool | What it does | Key arguments |
|
|
82
|
-
| --- | --- | --- |
|
|
83
|
-
| `diagram_render` | HTML fragment or document → explanatory PNG | `html`, `title`, `subtitle`, `theme` (`dark`/`light`), `width`, `height`, `scale`, `css`, `out` |
|
|
84
|
-
| `page_screenshot` | URL, local file or inline HTML → PNG | `url` / `file` / `html`, `width`, `height`, `fullPage`, `selector`, `waitMs`, `scale`, `out` |
|
|
85
|
-
| `image_annotate` | Existing image + `ops` → annotated PNG | `image`, `ops`, `crop`, `unit` (`px`/`percent`), `scale`, `out` |
|
|
86
|
-
| `diagram_guide` | Style guide as text | — |
|
|
87
|
-
|
|
88
|
-
The bundle also adds a short system-prompt section, so the agent prefers a rendered figure over ASCII art and avoids posting the same image twice.
|
|
89
|
-
|
|
90
|
-
A minimal `diagram_render` call:
|
|
91
|
-
|
|
92
|
-
```json
|
|
93
|
-
{
|
|
94
|
-
"title": "Request path",
|
|
95
|
-
"subtitle": "client → cache → origin",
|
|
96
|
-
"theme": "dark",
|
|
97
|
-
"html": "<div class=\"grid\"><div class=\"card\"><h3>Client</h3><p>sends a request</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>Cache</h3><p>answers on a hit</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>Origin</h3><p>handles a miss</p></div></div>"
|
|
98
|
-
}
|
|
99
|
-
```
|
|
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.
|
|
102
|
-
|
|
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
|
-
|
|
105
|
-
Annotation operations use these fields (defaults in parentheses):
|
|
106
|
-
|
|
107
|
-
| `type` | Shape |
|
|
108
|
-
| --- | --- |
|
|
109
|
-
| `box` | `x`, `y`, `width`, `height`, `color` (`#ff4d4f`), `strokeWidth` (4), `radius` (8), `label` |
|
|
110
|
-
| `highlight` | `x`, `y`, `width`, `height`, `color`, `opacity` (0.22) |
|
|
111
|
-
| `blur` | `x`, `y`, `width`, `height` — redacts by re-drawing the source behind a CSS blur |
|
|
112
|
-
| `ellipse` | `x`, `y`, `width`, `height`, `color`, `strokeWidth`, `label` |
|
|
113
|
-
| `arrow` / `line` | `x`, `y`, `x2`, `y2`, `color`, `strokeWidth`, `label` |
|
|
114
|
-
| `text` | `x`, `y` (baseline), `text`, `fontSize` (20), `color` |
|
|
115
|
-
|
|
116
|
-
`unit: "percent"` reads `x` and `width` as fractions of the image width and `y`/`height` as fractions of its height; `crop` is always in pixels.
|
|
117
|
-
|
|
118
|
-
## How it works
|
|
119
|
-
|
|
120
|
-
- Every render starts a temporary headless Chromium with its own `--user-data-dir` under the OS temp directory; the process tree is killed and the directory removed when the capture finishes.
|
|
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
|
-
- 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
|
-
- `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`).
|
|
125
|
-
|
|
126
|
-
## Limitations
|
|
127
|
-
|
|
128
|
-
- A cold browser start costs roughly 1–2 seconds per render; this is not a tool for large batches.
|
|
129
|
-
- Only addresses this machine can reach: `file://` paths and `http(s)` URLs. Every render uses an empty browser profile, so pages that need a login are captured logged out.
|
|
130
|
-
- Height caps: `page_screenshot` stops at 12000 px, content-height `diagram_render` at 8000 px. Longer pages are cut off rather than scaled.
|
|
131
|
-
- Output is PNG only.
|
|
132
|
-
- `page_screenshot` on an image file lays that image out as a web page, so the result gets padded to the viewport; use `image_annotate` for images.
|
|
133
|
-
- Annotation coordinates come from the caller. When the pixel size is unknown, use `unit: "percent"` or crop first.
|
|
134
|
-
|
|
135
|
-
## Development
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
1
|
+
English | [简体中文](README.zh-CN.md)
|
|
2
|
+
|
|
3
|
+
# dsh-diagram
|
|
4
|
+
|
|
5
|
+
> Render diagrams, page screenshots and annotated images to PNG inside DSH answers.
|
|
6
|
+
|
|
7
|
+
A DeepSeek Harness answer is text, but some explanations are really about a flow, a structure, a comparison or "where exactly in this screenshot". This bundle gives the agent four tools that turn HTML, a web page or an existing image into a PNG file and inline it in the conversation. Rendering uses the Chrome or Edge already installed on the machine: no bundled browser, no npm packages, no external service.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Diagrams from HTML.** `diagram_render` wraps a fragment in a shell with a CJK font stack, a dark or light theme and `.card` `.grid` `.arrow` `.pill` `table` utility classes, then crops to the content height and renders at 2× by default.
|
|
14
|
+
- **Page screenshots.** `page_screenshot` captures an `http(s)` URL, a local HTML file or inline HTML — the whole page or just the element matched by a CSS selector.
|
|
15
|
+
- **Annotations.** `image_annotate` draws boxes, ellipses, arrows, lines, text labels, highlights and blur redaction on an existing image, optionally after cropping it.
|
|
16
|
+
- **Built-in style guide.** `diagram_guide` returns the theme variables, available classes and three ready-made layouts, so the agent does not have to guess the shell.
|
|
17
|
+
- **Inline in chat.** Every tool returns a `markdown` line served from the bundle's own asset route; paste it as-is and the picture appears in the answer.
|
|
18
|
+
- **Real files on disk.** Each render is also saved as a file, by default under `.dsh/diagram/` in the session working directory.
|
|
19
|
+
|
|
20
|
+

|
|
21
|
+
|
|
22
|
+
One `image_annotate` call per step: the focal point, the gaze line, the body diagonal, negative space, then the warm/cool accent — all shapes, arrows and labels are drawn at render time.
|
|
23
|
+
|
|
24
|
+
## Example
|
|
25
|
+
|
|
26
|
+

|
|
27
|
+
|
|
28
|
+
One call to `image_annotate` produced all five marks on this picture: **1** the face as the focal point, **2** the gaze / visual lead-in toward the mint, **3** the body diagonal, **4** deliberate negative space, **5** the warm-red vs cool-green accent. Coordinates are original pixels; shapes, arrows and labels are drawn on the fly.
|
|
29
|
+
|
|
30
|
+
> After installing, tell the owner: restart DSH; the four tools are mounted with no settings page. If no Chrome/Edge is found, set `DSH_DIAGRAM_BROWSER`.
|
|
31
|
+
|
|
32
|
+
## Requirements
|
|
33
|
+
|
|
34
|
+
- DeepSeek Harness 0.2.x (developed against 0.2.2-rc.5).
|
|
35
|
+
- Node.js 21 or newer, as declared in `engines`; the Node runtime that ships with DSH satisfies it.
|
|
36
|
+
- Chrome, Edge or Chromium installed on the same machine. The engine probes the usual Windows, macOS and Linux locations; set `DSH_DIAGRAM_BROWSER` when it cannot find one.
|
|
37
|
+
- A CJK font for Chinese labels — Microsoft YaHei on Windows, PingFang on macOS, Noto Sans CJK or Source Han Sans on Linux. The font stack falls back to `system-ui`.
|
|
38
|
+
- Nothing else: apart from the DSH tool API, the renderer imports only Node built-ins, and it runs inside the DSH server process.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
This directory is a DSH bundle: `package.json` points `dsh.bundle.patch` at `cordis.patch.yml`, which inserts one composition row.
|
|
43
|
+
|
|
44
|
+
Install it from the checkout with the plugin manager:
|
|
45
|
+
|
|
46
|
+
```text
|
|
47
|
+
plugin_manager: { action: "install_bundle", target: "<path-to-checkout>" }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
dsh plugin --profile <profile> add @luosuqwq/dsh-diagram
|
|
51
|
+
|
|
52
|
+
Or add it to a profile by hand — in the profile's `package.json`:
|
|
53
|
+
|
|
54
|
+
```json
|
|
55
|
+
{
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@luosuqwq/dsh-diagram": "link:<path-to-checkout>"
|
|
58
|
+
},
|
|
59
|
+
"dsh": {
|
|
60
|
+
"profile": {
|
|
61
|
+
"bundles": ["@luosuqwq/dsh-diagram"]
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
then run `pnpm install` in the profile directory.
|
|
68
|
+
|
|
69
|
+
Restart DSH after installing: the tools are registered by the server-side half of the bundle when the process starts. Removing the bundle is `plugin_manager: { action: "remove_bundle", target: "dsh-diagram" }`, plus dropping the row from the profile.
|
|
70
|
+
|
|
71
|
+
## Configuration
|
|
72
|
+
|
|
73
|
+
| Variable | Type | Default | Purpose |
|
|
74
|
+
| --- | --- | --- | --- |
|
|
75
|
+
| `DSH_DIAGRAM_BROWSER` | path | unset (auto-detect) | Full path to the Chrome/Edge/Chromium executable used for rendering. |
|
|
76
|
+
|
|
77
|
+
There are no profile config keys and no settings page: every option is a per-call tool argument, listed under Usage.
|
|
78
|
+
|
|
79
|
+
## Usage
|
|
80
|
+
|
|
81
|
+
| Tool | What it does | Key arguments |
|
|
82
|
+
| --- | --- | --- |
|
|
83
|
+
| `diagram_render` | HTML fragment or document → explanatory PNG | `html`, `title`, `subtitle`, `theme` (`dark`/`light`), `width`, `height`, `scale`, `css`, `out` |
|
|
84
|
+
| `page_screenshot` | URL, local file or inline HTML → PNG | `url` / `file` / `html`, `width`, `height`, `fullPage`, `selector`, `waitMs`, `scale`, `out` |
|
|
85
|
+
| `image_annotate` | Existing image + `ops` → annotated PNG | `image`, `ops`, `crop`, `unit` (`px`/`percent`), `scale`, `out` |
|
|
86
|
+
| `diagram_guide` | Style guide as text | — |
|
|
87
|
+
|
|
88
|
+
The bundle also adds a short system-prompt section, so the agent prefers a rendered figure over ASCII art and avoids posting the same image twice.
|
|
89
|
+
|
|
90
|
+
A minimal `diagram_render` call:
|
|
91
|
+
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"title": "Request path",
|
|
95
|
+
"subtitle": "client → cache → origin",
|
|
96
|
+
"theme": "dark",
|
|
97
|
+
"html": "<div class=\"grid\"><div class=\"card\"><h3>Client</h3><p>sends a request</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>Cache</h3><p>answers on a hit</p></div><div class=\"arrow\">→</div><div class=\"card\"><h3>Origin</h3><p>handles a miss</p></div></div>"
|
|
98
|
+
}
|
|
99
|
+
```
|
|
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.
|
|
102
|
+
|
|
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
|
+
|
|
105
|
+
Annotation operations use these fields (defaults in parentheses):
|
|
106
|
+
|
|
107
|
+
| `type` | Shape |
|
|
108
|
+
| --- | --- |
|
|
109
|
+
| `box` | `x`, `y`, `width`, `height`, `color` (`#ff4d4f`), `strokeWidth` (4), `radius` (8), `label` |
|
|
110
|
+
| `highlight` | `x`, `y`, `width`, `height`, `color`, `opacity` (0.22) |
|
|
111
|
+
| `blur` | `x`, `y`, `width`, `height` — redacts by re-drawing the source behind a CSS blur |
|
|
112
|
+
| `ellipse` | `x`, `y`, `width`, `height`, `color`, `strokeWidth`, `label` |
|
|
113
|
+
| `arrow` / `line` | `x`, `y`, `x2`, `y2`, `color`, `strokeWidth`, `label` |
|
|
114
|
+
| `text` | `x`, `y` (baseline), `text`, `fontSize` (20), `color` |
|
|
115
|
+
|
|
116
|
+
`unit: "percent"` reads `x` and `width` as fractions of the image width and `y`/`height` as fractions of its height; `crop` is always in pixels.
|
|
117
|
+
|
|
118
|
+
## How it works
|
|
119
|
+
|
|
120
|
+
- Every render starts a temporary headless Chromium with its own `--user-data-dir` under the OS temp directory; the process tree is killed and the directory removed when the capture finishes.
|
|
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
|
+
- 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
|
+
- `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`).
|
|
125
|
+
|
|
126
|
+
## Limitations
|
|
127
|
+
|
|
128
|
+
- A cold browser start costs roughly 1–2 seconds per render; this is not a tool for large batches.
|
|
129
|
+
- Only addresses this machine can reach: `file://` paths and `http(s)` URLs. Every render uses an empty browser profile, so pages that need a login are captured logged out.
|
|
130
|
+
- Height caps: `page_screenshot` stops at 12000 px, content-height `diagram_render` at 8000 px. Longer pages are cut off rather than scaled.
|
|
131
|
+
- Output is PNG only.
|
|
132
|
+
- `page_screenshot` on an image file lays that image out as a web page, so the result gets padded to the viewport; use `image_annotate` for images.
|
|
133
|
+
- Annotation coordinates come from the caller. When the pixel size is unknown, use `unit: "percent"` or crop first.
|
|
134
|
+
|
|
135
|
+
## Development
|
|
136
|
+
|
|
137
|
+
Install the dev dependency once (only needed by the render tests):
|
|
138
|
+
|
|
139
|
+
```sh
|
|
140
|
+
npm install
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Both suites run without the DSH runtime, need a working Chrome/Edge, and write PNGs into `test/out/`:
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
node test/smoke.mjs
|
|
147
|
+
node test/tool-path.mjs
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
`smoke.mjs` exercises the rendering engine (diagram, full-page screenshot, annotate, crop + annotate) and prints the pixel size of each image. `tool-path.mjs` registers the plugin against a stub `ctx`, calls all four tools, then fetches the returned Markdown URL through the bundle's own route and checks the PNG bytes. Both exit non-zero on failure.
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
148
154
|
MIT. See [LICENSE](LICENSE).
|
package/index.js
CHANGED
|
@@ -1,352 +1,355 @@
|
|
|
1
|
-
// dsh-diagram: turn explanations into images.
|
|
2
|
-
//
|
|
3
|
-
// Three model-facing tools share one rendering pipeline (headless Chrome over
|
|
4
|
-
// CDP, see lib/browser.js):
|
|
5
|
-
// diagram_render HTML fragment/document -> explanatory PNG
|
|
6
|
-
// page_screenshot http(s)/local page -> PNG (full page or one element)
|
|
7
|
-
// image_annotate existing image -> PNG with boxes/arrows/labels
|
|
8
|
-
// and a `diagram_guide` tool that hands over the style guide on demand.
|
|
9
|
-
|
|
10
|
-
import { existsSync, mkdirSync, readFileSync, statSync } from 'node:fs'
|
|
11
|
-
import { readFile } from 'node:fs/promises'
|
|
12
|
-
import path from 'node:path'
|
|
13
|
-
import { fileURLToPath } from 'node:url'
|
|
14
|
-
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
15
|
-
import { renderToPng } from './lib/browser.js'
|
|
16
|
-
import { wrapDocument, normalizeTheme } from './lib/shell.js'
|
|
17
|
-
import { annotateImage } from './lib/annotate.js'
|
|
18
|
-
|
|
19
|
-
export const name = 'dsh-diagram'
|
|
20
|
-
export const inject = ['tools', 'webServer', 'systemPrompt']
|
|
21
|
-
|
|
22
|
-
const ROUTE = '/plugins/dsh-diagram/asset'
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
['.
|
|
28
|
-
['.
|
|
29
|
-
['.
|
|
30
|
-
['.
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// ---------------------------------------------------------------------------
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
res.
|
|
146
|
-
res.end()
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
const
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
ctx.effect(() => ctx.
|
|
347
|
-
ctx.effect(() => ctx.tools.register(
|
|
348
|
-
ctx.effect(
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
1
|
+
// dsh-diagram: turn explanations into images.
|
|
2
|
+
//
|
|
3
|
+
// Three model-facing tools share one rendering pipeline (headless Chrome over
|
|
4
|
+
// CDP, see lib/browser.js):
|
|
5
|
+
// diagram_render HTML fragment/document -> explanatory PNG
|
|
6
|
+
// page_screenshot http(s)/local page -> PNG (full page or one element)
|
|
7
|
+
// image_annotate existing image -> PNG with boxes/arrows/labels
|
|
8
|
+
// and a `diagram_guide` tool that hands over the style guide on demand.
|
|
9
|
+
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, statSync } from 'node:fs'
|
|
11
|
+
import { readFile } from 'node:fs/promises'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import { fileURLToPath } from 'node:url'
|
|
14
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
15
|
+
import { renderToPng } from './lib/browser.js'
|
|
16
|
+
import { wrapDocument, normalizeTheme } from './lib/shell.js'
|
|
17
|
+
import { annotateImage } from './lib/annotate.js'
|
|
18
|
+
|
|
19
|
+
export const name = 'dsh-diagram'
|
|
20
|
+
export const inject = ['tools', 'webServer', 'systemPrompt']
|
|
21
|
+
|
|
22
|
+
const ROUTE = '/plugins/dsh-diagram/asset'
|
|
23
|
+
// Only files this process rendered are served; a request cannot name an arbitrary path.
|
|
24
|
+
const SERVED_ASSETS = new Set()
|
|
25
|
+
const OUTPUT_DIR = ['.dsh', 'diagram']
|
|
26
|
+
const MIME = new Map([
|
|
27
|
+
['.png', 'image/png'],
|
|
28
|
+
['.jpg', 'image/jpeg'],
|
|
29
|
+
['.jpeg', 'image/jpeg'],
|
|
30
|
+
['.webp', 'image/webp'],
|
|
31
|
+
['.gif', 'image/gif'],
|
|
32
|
+
['.svg', 'image/svg+xml'],
|
|
33
|
+
])
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Small helpers
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
function sessionCwd(exec) {
|
|
40
|
+
return exec?.agent?.session?.header?.cwd || process.cwd()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function clamp(value, fallback, min, max) {
|
|
44
|
+
const parsed = Number(value)
|
|
45
|
+
if (!Number.isFinite(parsed)) return fallback
|
|
46
|
+
return Math.min(max, Math.max(min, parsed))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function stripUndefined(value) {
|
|
50
|
+
if (Array.isArray(value)) return value.map(stripUndefined)
|
|
51
|
+
if (value && typeof value === 'object') {
|
|
52
|
+
const out = {}
|
|
53
|
+
for (const [key, item] of Object.entries(value)) {
|
|
54
|
+
if (item === undefined) continue
|
|
55
|
+
out[key] = stripUndefined(item)
|
|
56
|
+
}
|
|
57
|
+
return out
|
|
58
|
+
}
|
|
59
|
+
return value
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function slugify(text) {
|
|
63
|
+
const slug = String(text ?? 'image')
|
|
64
|
+
.replace(/[\\/:*?"<>|\s]+/g, '-')
|
|
65
|
+
.replace(/^-+|-+$/g, '')
|
|
66
|
+
.slice(0, 40)
|
|
67
|
+
return slug || 'image'
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function stamp() {
|
|
71
|
+
return new Date().toISOString().replace(/[-:]/g, '').replace('T', '-').slice(0, 15)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolveOut(raw, cwd, seed) {
|
|
75
|
+
if (raw) {
|
|
76
|
+
const file = path.resolve(cwd, String(raw))
|
|
77
|
+
mkdirSync(path.dirname(file), { recursive: true })
|
|
78
|
+
return file
|
|
79
|
+
}
|
|
80
|
+
const dir = path.join(cwd, ...OUTPUT_DIR)
|
|
81
|
+
mkdirSync(dir, { recursive: true })
|
|
82
|
+
return path.join(dir, `${slugify(seed)}-${stamp()}.png`)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function origin(ctx) {
|
|
86
|
+
try {
|
|
87
|
+
return ctx.webServer.getOrigin()
|
|
88
|
+
} catch {
|
|
89
|
+
return `http://127.0.0.1:${ctx.webServer.port}`
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function assetMarkdown(ctx, file) {
|
|
94
|
+
let version = '0'
|
|
95
|
+
try { version = String(Math.round(statSync(file).mtimeMs)) } catch {}
|
|
96
|
+
return `${origin(ctx)}${ROUTE}?file=${encodeURIComponent(file)}&v=${version}`
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function imageResult(ctx, info, alt) {
|
|
100
|
+
SERVED_ASSETS.add(path.resolve(info.path))
|
|
101
|
+
const url = assetMarkdown(ctx, info.path)
|
|
102
|
+
return stripUndefined({
|
|
103
|
+
path: info.path,
|
|
104
|
+
url,
|
|
105
|
+
width: info.width,
|
|
106
|
+
height: info.height,
|
|
107
|
+
bytes: info.bytes,
|
|
108
|
+
markdown: ``,
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const OUTPUT_SCHEMA = { type: 'object', additionalProperties: true }
|
|
113
|
+
|
|
114
|
+
function imageRender(_args, value) {
|
|
115
|
+
if (!value?.markdown) return [{ type: 'text', text: `已保存:${value?.path ?? ''}` }]
|
|
116
|
+
return [
|
|
117
|
+
{
|
|
118
|
+
type: 'text',
|
|
119
|
+
text: `已生成 ${value.width}×${value.height} px 图片,文件:${value.path}\n把下面这一行原样放进回答(不要改写链接):\n\n${value.markdown}`,
|
|
120
|
+
},
|
|
121
|
+
]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function serveAsset(req, res) {
|
|
125
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
126
|
+
res.writeHead(405, { allow: 'GET, HEAD' })
|
|
127
|
+
res.end()
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const requestUrl = new URL(req.url ?? ROUTE, 'http://127.0.0.1')
|
|
132
|
+
const file = requestUrl.searchParams.get('file')
|
|
133
|
+
const contentType = MIME.get(path.extname(file ?? '').toLowerCase())
|
|
134
|
+
if (!file || !contentType || !SERVED_ASSETS.has(path.resolve(file)) || !existsSync(file) || !statSync(file).isFile()) {
|
|
135
|
+
res.writeHead(404)
|
|
136
|
+
res.end()
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
const bytes = readFileSync(file)
|
|
140
|
+
res.writeHead(200, {
|
|
141
|
+
'content-type': contentType,
|
|
142
|
+
'content-length': String(bytes.length),
|
|
143
|
+
'cache-control': 'no-store',
|
|
144
|
+
})
|
|
145
|
+
if (req.method === 'HEAD') res.end()
|
|
146
|
+
else res.end(bytes)
|
|
147
|
+
} catch {
|
|
148
|
+
res.writeHead(404)
|
|
149
|
+
res.end()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// Tools
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
function diagramRenderTool(ctx) {
|
|
158
|
+
return defineTool({
|
|
159
|
+
name: 'diagram_render',
|
|
160
|
+
description:
|
|
161
|
+
'把一段 HTML 渲染成 PNG 并返回内联展示用的 Markdown:用图解释流程、结构、对比、层级或状态关系,比大段文字更快看懂。自带中文字体、深/浅主题和 .card/.grid/.arrow/.pill/table 等样式类;需要现成写法时先调 diagram_guide。',
|
|
162
|
+
parameters: {
|
|
163
|
+
html: {
|
|
164
|
+
type: 'string',
|
|
165
|
+
required: true,
|
|
166
|
+
description:
|
|
167
|
+
'HTML 片段(套上统一外壳,推荐)或完整 HTML 文档(以 <!doctype 或 <html 开头,原样渲染,负责自己的全部样式)。片段请把内容写成自适应的块级结构,不要给 body 设固定宽高。',
|
|
168
|
+
},
|
|
169
|
+
css: { type: 'string', description: '追加 CSS,可覆盖外壳主题变量与样式。' },
|
|
170
|
+
title: { type: 'string', description: '大标题;仅片段模式生效。' },
|
|
171
|
+
subtitle: { type: 'string', description: '标题下的一句话副标题。' },
|
|
172
|
+
theme: { type: 'string', enum: ['dark', 'light'], description: '配色主题,默认 dark。' },
|
|
173
|
+
width: { type: 'integer', description: '画布 CSS 宽度,默认 1200;信息密集时可用 1500。' },
|
|
174
|
+
height: { type: 'integer', description: '画布 CSS 高度;省略则按内容自动裁剪(推荐)。' },
|
|
175
|
+
scale: { type: 'number', description: '输出倍率 1–3,默认 2;2 倍图在聊天中更清晰。' },
|
|
176
|
+
out: { type: 'string', description: '输出 PNG 路径(相对当前工作目录);省略写入 .dsh/diagram/。' },
|
|
177
|
+
},
|
|
178
|
+
output: { schema: OUTPUT_SCHEMA, render: imageRender },
|
|
179
|
+
execute: async (args, exec) => {
|
|
180
|
+
const cwd = sessionCwd(exec)
|
|
181
|
+
const html = wrapDocument({
|
|
182
|
+
html: args.html,
|
|
183
|
+
css: args.css,
|
|
184
|
+
title: args.title,
|
|
185
|
+
subtitle: args.subtitle,
|
|
186
|
+
theme: normalizeTheme(args.theme),
|
|
187
|
+
})
|
|
188
|
+
const outPath = resolveOut(args.out, cwd, args.title ?? 'diagram')
|
|
189
|
+
const info = await renderToPng({
|
|
190
|
+
source: { html },
|
|
191
|
+
width: Math.round(clamp(args.width, 1200, 240, 2400)),
|
|
192
|
+
height: args.height === undefined ? undefined : Math.round(clamp(args.height, 800, 120, 4000)),
|
|
193
|
+
scale: clamp(args.scale, 2, 1, 3),
|
|
194
|
+
outPath,
|
|
195
|
+
})
|
|
196
|
+
return imageResult(ctx, info, args.title || '图示')
|
|
197
|
+
},
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function pageScreenshotTool(ctx) {
|
|
202
|
+
return defineTool({
|
|
203
|
+
name: 'page_screenshot',
|
|
204
|
+
description:
|
|
205
|
+
'把网页或本地 HTML 文件截成 PNG:可截整页、或只截某个 CSS 选择器对应的元素。用于引用外部页面、渲染已有 HTML 报告,或先截图再用 image_annotate 标注。',
|
|
206
|
+
parameters: {
|
|
207
|
+
url: { type: 'string', description: '要截图的 http(s) 地址。' },
|
|
208
|
+
file: { type: 'string', description: '本地 HTML 文件路径(相对当前工作目录或绝对路径)。' },
|
|
209
|
+
html: { type: 'string', description: '直接给出 HTML 源码来截图。' },
|
|
210
|
+
width: { type: 'integer', description: '视口宽度,默认 1440。' },
|
|
211
|
+
height: { type: 'integer', description: '视口高度,默认 900;配合 fullPage 时作为最小高度。' },
|
|
212
|
+
fullPage: { type: 'boolean', description: 'true 时截整页(含视口外内容),默认 false。' },
|
|
213
|
+
selector: { type: 'string', description: '只截该 CSS 选择器命中的元素,优先于 fullPage。' },
|
|
214
|
+
waitMs: { type: 'integer', description: '页面 load 后额外等待毫秒数,默认 400;异步渲染慢的页面可加大。' },
|
|
215
|
+
scale: { type: 'number', description: '输出倍率 1–3,默认 2。' },
|
|
216
|
+
out: { type: 'string', description: '输出 PNG 路径;省略写入 .dsh/diagram/。' },
|
|
217
|
+
},
|
|
218
|
+
output: { schema: OUTPUT_SCHEMA, render: imageRender },
|
|
219
|
+
execute: async (args, exec) => {
|
|
220
|
+
const cwd = sessionCwd(exec)
|
|
221
|
+
const source = args.url ? { url: args.url } : args.file ? { file: path.resolve(cwd, args.file) } : args.html ? { html: args.html } : null
|
|
222
|
+
if (!source) throw new Error('需要 url、file 或 html 之一')
|
|
223
|
+
const outPath = resolveOut(args.out, cwd, 'screenshot')
|
|
224
|
+
const info = await renderToPng({
|
|
225
|
+
source,
|
|
226
|
+
width: Math.round(clamp(args.width, 1440, 240, 3000)),
|
|
227
|
+
height: args.fullPage ? undefined : Math.round(clamp(args.height, 900, 200, 3000)),
|
|
228
|
+
fullPage: Boolean(args.fullPage),
|
|
229
|
+
selector: args.selector || undefined,
|
|
230
|
+
scale: clamp(args.scale, 2, 1, 3),
|
|
231
|
+
maxHeight: 12000,
|
|
232
|
+
outPath,
|
|
233
|
+
afterLoad: async ({ wait }) => {
|
|
234
|
+
const extra = clamp(args.waitMs, 400, 0, 8000)
|
|
235
|
+
if (extra > 0) await wait(extra)
|
|
236
|
+
},
|
|
237
|
+
})
|
|
238
|
+
return imageResult(ctx, info, '截图')
|
|
239
|
+
},
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function imageAnnotateTool(ctx) {
|
|
244
|
+
return defineTool({
|
|
245
|
+
name: 'image_annotate',
|
|
246
|
+
description:
|
|
247
|
+
'在已有图片上画框、圈、箭头、文字、高亮或模糊区域,输出新的 PNG。用于指着截图/照片说"这里"、标出重点或打码。坐标是原图像素。',
|
|
248
|
+
parameters: {
|
|
249
|
+
image: { type: 'string', required: true, description: '要标注的图片路径(相对当前工作目录或绝对路径)。' },
|
|
250
|
+
ops: {
|
|
251
|
+
type: 'array',
|
|
252
|
+
required: true,
|
|
253
|
+
description: '标注操作,按顺序绘制。坐标单位为原图像素。',
|
|
254
|
+
items: {
|
|
255
|
+
type: 'object',
|
|
256
|
+
additionalProperties: false,
|
|
257
|
+
properties: {
|
|
258
|
+
type: {
|
|
259
|
+
type: 'string',
|
|
260
|
+
required: true,
|
|
261
|
+
enum: ['box', 'ellipse', 'arrow', 'line', 'text', 'highlight', 'blur'],
|
|
262
|
+
description:
|
|
263
|
+
'box=矩形框;ellipse=椭圆圈;arrow=带箭头直线;line=直线;text=文字;highlight=半透明色块;blur=模糊打码。',
|
|
264
|
+
},
|
|
265
|
+
x: { type: 'number', description: '左上角 x;arrow/line 为起点 x;text 为文字左端 x。' },
|
|
266
|
+
y: { type: 'number', description: '左上角 y;arrow/line 为起点 y;text 为文字基线 y。' },
|
|
267
|
+
width: { type: 'number', description: 'box/ellipse/highlight/blur 的宽。' },
|
|
268
|
+
height: { type: 'number', description: 'box/ellipse/highlight/blur 的高。' },
|
|
269
|
+
x2: { type: 'number', description: 'arrow/line 终点 x。' },
|
|
270
|
+
y2: { type: 'number', description: 'arrow/line 终点 y。' },
|
|
271
|
+
text: { type: 'string', description: 'text 的正文。' },
|
|
272
|
+
label: { type: 'string', description: 'box/ellipse/arrow 上的小标签,自带底色。' },
|
|
273
|
+
color: { type: 'string', description: '颜色,默认 #ff4d4f。' },
|
|
274
|
+
strokeWidth: { type: 'number', description: '线宽,默认 4。' },
|
|
275
|
+
fontSize: { type: 'number', description: '字号,默认 20(label 默认 17)。' },
|
|
276
|
+
opacity: { type: 'number', description: '整体不透明度 0–1。' },
|
|
277
|
+
radius: { type: 'number', description: '矩形圆角,默认 8。' },
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
crop: {
|
|
282
|
+
type: 'object',
|
|
283
|
+
additionalProperties: false,
|
|
284
|
+
description: '先裁剪原图再标注;坐标与宽高都是原图像素。',
|
|
285
|
+
properties: {
|
|
286
|
+
x: { type: 'number' },
|
|
287
|
+
y: { type: 'number' },
|
|
288
|
+
width: { type: 'number' },
|
|
289
|
+
height: { type: 'number' },
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
unit: {
|
|
293
|
+
type: 'string',
|
|
294
|
+
enum: ['px', 'percent'],
|
|
295
|
+
description:
|
|
296
|
+
'ops 的坐标单位,默认 px(原图像素)。percent 时 x/width 用图片宽度的 0–1 比例、y/height 用高度的比例——不确定像素尺寸、只想"大致指这块区域"时用它。crop 始终是像素。',
|
|
297
|
+
},
|
|
298
|
+
scale: { type: 'number', description: '输出倍率 1–3,默认 2。' },
|
|
299
|
+
out: { type: 'string', description: '输出 PNG 路径;省略写入 .dsh/diagram/。' },
|
|
300
|
+
},
|
|
301
|
+
output: { schema: OUTPUT_SCHEMA, render: imageRender },
|
|
302
|
+
execute: async (args, exec) => {
|
|
303
|
+
const cwd = sessionCwd(exec)
|
|
304
|
+
const ops = Array.isArray(args.ops) ? args.ops : []
|
|
305
|
+
if (!ops.length) throw new Error('ops 不能为空')
|
|
306
|
+
const imageFile = path.resolve(cwd, String(args.image ?? ''))
|
|
307
|
+
const outPath = resolveOut(args.out, cwd, `${path.parse(imageFile).name}-annotated`)
|
|
308
|
+
const info = await annotateImage({
|
|
309
|
+
imageFile,
|
|
310
|
+
ops,
|
|
311
|
+
crop: args.crop,
|
|
312
|
+
unit: args.unit,
|
|
313
|
+
scale: clamp(args.scale, 2, 1, 3),
|
|
314
|
+
outPath,
|
|
315
|
+
})
|
|
316
|
+
return imageResult(ctx, info, path.basename(imageFile, path.extname(imageFile)))
|
|
317
|
+
},
|
|
318
|
+
})
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function guideTool() {
|
|
322
|
+
return defineTool({
|
|
323
|
+
name: 'diagram_guide',
|
|
324
|
+
description:
|
|
325
|
+
'返回图示渲染的样式指南:主题变量、可用的 CSS 类、可直接套用的版式片段,以及标注/截图的常用组合。第一次画图或想要更好看时调用一次。',
|
|
326
|
+
parameters: {},
|
|
327
|
+
output: { schema: OUTPUT_SCHEMA, render: (_args, value) => [{ type: 'text', text: value.text }] },
|
|
328
|
+
isConcurrencySafe: () => true,
|
|
329
|
+
execute: async () => {
|
|
330
|
+
const file = fileURLToPath(new URL('./guides/diagram.md', import.meta.url))
|
|
331
|
+
const text = await readFile(file, 'utf8')
|
|
332
|
+
return { text, path: file }
|
|
333
|
+
},
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ---------------------------------------------------------------------------
|
|
338
|
+
// Plugin
|
|
339
|
+
// ---------------------------------------------------------------------------
|
|
340
|
+
|
|
341
|
+
function promptSection() {
|
|
342
|
+
return `需要讲清结构、流程、对比、层级或“这一点在截图里的哪里”时,用 diagram_render / page_screenshot / image_annotate 出图,而不是画 ASCII 示意图。图片会自动内联展示;不要把同一张图既放 Markdown 又调 show_image 重复一次。`
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function apply(ctx) {
|
|
346
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: ROUTE, handler: serveAsset }), 'dsh-diagram: image route')
|
|
347
|
+
ctx.effect(() => ctx.tools.register(diagramRenderTool(ctx)), 'dsh-diagram: diagram_render')
|
|
348
|
+
ctx.effect(() => ctx.tools.register(pageScreenshotTool(ctx)), 'dsh-diagram: page_screenshot')
|
|
349
|
+
ctx.effect(() => ctx.tools.register(imageAnnotateTool(ctx)), 'dsh-diagram: image_annotate')
|
|
350
|
+
ctx.effect(() => ctx.tools.register(guideTool()), 'dsh-diagram: diagram_guide')
|
|
351
|
+
ctx.effect(
|
|
352
|
+
() => ctx.systemPrompt.section({ name: 'dsh-diagram', order: 186, text: () => promptSection() }),
|
|
353
|
+
'dsh-diagram: prompt section',
|
|
354
|
+
)
|
|
355
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luosuqwq/dsh-diagram",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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": [
|
|
@@ -56,5 +56,8 @@
|
|
|
56
56
|
"homepage": "https://github.com/LuosuQWQ/dsh-diagram#readme",
|
|
57
57
|
"bugs": {
|
|
58
58
|
"url": "https://github.com/LuosuQWQ/dsh-diagram/issues"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@deepseek-ai/dsh-tools": "0.2.2-rc.5"
|
|
59
62
|
}
|
|
60
63
|
}
|