picturereader-zcode 1.0.3 → 2.0.0

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,24 +1,33 @@
1
- # picturereader
1
+ # picturereader(ZCode 版)
2
2
 
3
- > 给纯文本模型(如 deepseek-v4-flash)的"读图"能力——**双版本发布**:
4
- > - **DSH 版**:DeepSeek Harness 插件(`dsh-plugin`,含 DSH EAC 桌面端)
5
- > - **ZCode 版**:ZCode 桌面端插件(`zcode-plugin`,经 MCP 暴露工具)
3
+ > **v2.0.0** —— 给纯文本模型(如 deepseek-v4-flash)的**全能"读图"能力**。
4
+ > 融合 **独立伪多模态识图** **外部视觉 API 接口**,一个插件搞定全部,**无需另装任何插件**。
5
+ > 本分支为 **ZCode 桌面端插件**(经 MCP 暴露工具);DSH 版见 [main 分支](https://github.com/jing-hy/picturereader)。
6
6
 
7
- > 把图片 **降分辨率 + 降色深 + 结构/色彩指纹提取**,渲染成文本网格喂回对话,
8
- > 让模型像多模态模型一样"看"图:描述场景、主体、环境、光线与语义内容。
9
- > **纯本地、零外部模型依赖、零 API key、零 Python(PaddleOCR 为可选增强)**。
7
+ > - **DSH 版**:DeepSeek Harness 插件(`dsh-plugin`,含 DSH EAC 桌面端)—— [main 分支](https://github.com/jing-hy/picturereader)
8
+ > - **ZCode 版**:ZCode 桌面端插件(`zcode-plugin`,经 MCP 暴露工具)—— 本分支
10
9
 
11
- [![dsh-plugin](https://awesome-dsh-plugin.com/badge.svg)](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)
10
+ ## 本轮工作(v2.0.0 新增)
11
+
12
+ 在原有的**本地伪多模态识图**(像素扫描 + OCR + 取样,零外部依赖)之上,融合了社区 `dsh-universal-vision` 的优势,形成一套**统一、可交叉验证的完整识图栈**:
13
+
14
+ 1. **新增外部视觉 API 接口**(`src/vlm.js`):桥接任意 OpenAI 兼容的视觉端点(本地 llama-server / LM Studio / vLLM / 云端网关),让模型获得真正的**语义理解**能力(场景、角色、界面、风格)。
15
+ 2. **新增统一分析工具 `vision_analyze`**:一次调用即可取回「低信息拦截 + 像素扫描 + OCR + VLM」多路证据。
16
+ 3. **新增低信息量拦截**(`src/guard.js`):自动识别空白/未渲染/简单图片,避免小 VLM 在空图上幻觉,也节省调用成本。
17
+ 4. **证据交叉验证**:VLM 描述与像素/OCR 实测冲突时,以实测为准——伪多模态与真 VLM 互为印证,抑制幻觉。
18
+ 5. **多次提问**:可对同一张图用 `vision_analyze` 以不同 `prompt` 反复提问,从多角度复核同一内容。
19
+
20
+ > **核心优势一句话**:**独立伪多模态识图(零依赖、可离线) + 外部 API 语义接口(可选、即插即用)** —— 简单图用像素就够,复杂图一键接 VLM,一个插件全包含,不需要再装 `dsh-universal-vision` 或任何其他读图插件。
12
21
 
13
22
  ## 版本总览
14
23
 
15
24
  | 版本 | 平台 | 形态 | 源码 | 安装 |
16
25
  |---|---|---|---|---|
17
- | **DSH 版** | DeepSeek Harness(含 EAC 桌面端) | npm 插件(`dsh.bundle`) | 仓库根目录 | `dsh plugin --profile web add picturereader` |
18
- | **ZCode 版** | ZCode 桌面端 | 本地 marketplace 插件(MCP server + skill) | [jing-hy/picturereader-zcode](https://github.com/jing-hy/picturereader-zcode)(独立仓库) | 见其 README |
26
+ | **DSH 版** | DeepSeek Harness(含 EAC 桌面端) | npm 插件(`dsh.bundle`) | [main 分支](https://github.com/jing-hy/picturereader) | `dsh plugin --profile web add picturereader` |
27
+ | **ZCode 版** | ZCode 桌面端 | 本地 marketplace 插件(MCP server + skill) | 本分支 | `npm install picturereader-zcode` |
19
28
 
20
29
  两个版本共用同一套业务核心(`src/core.js`)与读图方法论 skill(`image-reading`),
21
- 三个工具行为完全一致:`image_scan` / `image_ocr` / `image_sample`。
30
+ 四个工具行为完全一致:`image_scan` / `image_ocr` / `image_sample` / `vision_analyze`。
22
31
 
23
32
  > **ZCode 版性能说明**:ZCode 版通过 MCP(stdio 子进程)暴露工具,每次调用都要
24
33
  > 经历进程通信与序列化开销,**速度明显慢于 DSH 版**(DSH 版为插件内直接调用)。
@@ -26,13 +35,14 @@
26
35
 
27
36
  ## 这是什么
28
37
 
29
- DeepSeek 等纯文本模型没有视觉编码器,无法直接看图。picturereader 把"看图"翻译成**模型能理解的结构化文本证据**,并提供一套经过大量真实图片迭代验证的**读图方法论 skill(image-reading)**,让模型像人一样分步看图:
38
+ 纯文本模型没有视觉编码器,无法直接看图。picturereader 把"看图"翻译成**模型能理解的结构化文本证据**,并提供一套经过大量真实图片迭代验证的**读图方法论 skill(image-reading)**,让模型像人一样分步看图:
30
39
 
31
40
  1. **全局定调**:hue families(纯色指纹)→ structure(条纹/对称)→ texture(写实度)→ regions(色块结构)
32
41
  2. **主动找主体**:px_per_cell 定向放大(深色/低对比/小色块不会漏)
33
42
  3. **文字验证**:PaddleOCR 实读(防多模态幻觉)
34
43
  4. **材质判断**:image_sample 像素取样
35
- 5. **综合描述**:带证据等级的连贯画面描述
44
+ 5. **(可选)VLM 语义理解**:外部视觉 API 提供场景/角色/界面/风格的自然语言描述
45
+ 6. **综合描述**:带证据等级的连贯画面描述,伪多模态与 VLM 交叉验证
36
46
 
37
47
  ## 工具
38
48
 
@@ -41,50 +51,59 @@ DeepSeek 等纯文本模型没有视觉编码器,无法直接看图。picturer
41
51
  | `image_scan` | 全局/区域扫描:亮度/颜色网格 + regions 色块 + shade diversity + texture mix + structure(条纹/对称) + **像素级 colors** + **hue families 纯色指纹**;支持 `focus`/`region` 局部放大、`px_per_cell` 像素密度定向放大 |
42
52
  | `image_ocr` | 文字识别双引擎:`windows`(内置,默认)/ `paddle`(选装,发光/弯曲/游戏字更强),失败自动降级不崩溃 |
43
53
  | `image_sample` | 8×8 精确像素取样,判断材质/纹理(金属/木纹/织物/皮肤/噪点) |
54
+ | `vision_analyze` | **统一入口(v2.0.0 新增)**:低信息拦截 + 可选像素扫描/OCR/VLM,组合证据返回;VLM 可选配置 |
44
55
 
45
56
  ### 读图方法论 skill(image-reading)
46
57
 
47
- `skills/image-reading.md`(DSH 版)/ `skills/image-reading/SKILL.md`(ZCode 版)是一套**经大量真实图片场景迭代验证**的读图方法论
58
+ `skills/image-reading/SKILL.md`(ZCode 版)是一套**经大量真实图片场景迭代验证**的读图方法论
48
59
  (按 experience / skill / principle / insight 分层,教训有据可依、找得到主模型模式),
49
60
  安装后模型自动掌握:
50
61
  - **hue 场景指纹**:cyan 高=水/雾/湖泊,green 高=森林,orange/red 高=暖色人物/火光,blue 高=夜空科幻,achromatic+rough=废墟,green+yellow=翠绿能量/浮空仙境
51
62
  - **多模态模型校验规则**:游戏名/品牌等文字必须 OCR 实读(多模态模型会猜错);发光元素颜色以 hue 实测为准(多模态模型对发光色的描述系统性不可靠);低对比主体(暗色人物/小色块)必须放大确认
52
63
  - **主动验证**:低对比主体(暗色人物/小色块)必须放大确认
64
+ - **vision_analyze 使用**(v2.0.0):先 image_scan 自己看,简单图用像素,复杂图再调 VLM;描述与实测冲突时以实测为准
53
65
 
54
66
  ## 安装
55
67
 
56
- ### DSH
68
+ ZCode 版通过 **MCP server**(`mcp/server.js`,stdio)把四个工具暴露给 ZCode,
69
+ 读图方法论作为 **skill** 随插件分发,业务逻辑 `src/core.js` 与 DSH 版完全一致。
57
70
 
58
71
  ```sh
59
- # 1. 插件
60
- dsh plugin --profile web add picturereader # 或从源码: dsh plugin --profile web add .
61
- dsh plugin --profile headless add picturereader
62
-
63
- # 2. 读图方法论 skill(推荐)
64
- copy skills\image-reading.md %USERPROFILE%\.dsh\skills\ # Windows
65
- # macOS/Linux: cp skills/image-reading.md ~/.dsh/skills/
66
-
67
- # 3.(可选)PaddleOCR 增强引擎:node scripts/setup-ocr.mjs
72
+ npm install picturereader-zcode
68
73
  ```
69
74
 
70
- 重启 DSH Desktop 后,模型工具列表出现 `image_scan` / `image_ocr` / `image_sample`,
71
- 技能目录出现 `image-reading`。
72
-
73
- ### ZCode 版
75
+ ### (可选)PaddleOCR 增强引擎
74
76
 
75
- ZCode 版是**独立仓库**:[jing-hy/picturereader-zcode](https://github.com/jing-hy/picturereader-zcode)。
77
+ ```sh
78
+ node scripts/setup-ocr.mjs
79
+ ```
76
80
 
77
- ZCode 版通过 **MCP server**(`mcp/server.js`,stdio)把三个工具暴露给 ZCode,
78
- 读图方法论作为 **skill**(`skills/image-reading/`)随插件分发,业务逻辑 `src/core.js`
79
- 与本仓库完全一致。安装与使用请见其 README。
81
+ PaddleOCR 缺失时 `image_ocr` 自动降级为 Windows OCR。
80
82
 
81
83
  ## 使用
82
84
 
83
- 直接对模型说:
85
+ 对 ZCode 模型说:
84
86
 
85
87
  > 用 image_scan 看一下 <路径> 这张图,细看感兴趣的部分
88
+ > (复杂场景可接着用 vision_analyze 获取语义描述并交叉验证)
89
+
90
+ ### vision_analyze 用法(v2.0.0)
86
91
 
87
- 模型会加载 `image-reading` 方法论自动执行完整流程(定调 → 找主体 → 验证 → 描述)。
92
+ ```
93
+ vision_analyze(
94
+ file_path="C:/shot.png",
95
+ prompt="描述这个界面,有哪些元素?布局是否正常?",
96
+ include_scan=true, # 像素扫描证据(默认 true)
97
+ include_ocr=true, # OCR 文字证据(默认 false)
98
+ include_vlm=true, # 外部 VLM 语义描述(默认 true,但未配置 SEE_BASE 时自动跳过)
99
+ allow_low_info=false, # 空白/简单图是否强制调 VLM(默认 false)
100
+ stop_after=false # 调用后是否关闭本插件启动的本地服务器
101
+ )
102
+ ```
103
+
104
+ - **先自己看,再决定**:建议先用 `image_scan` 了解图片,简单图用像素就够;复杂/精密场景再开 VLM。
105
+ - **多次提问**:对同一张图换不同 `prompt` 反复调用,从多角度复核。
106
+ - **交叉验证**:VLM 描述与像素/OCR 冲突时,以实测为准。
88
107
 
89
108
  ## 输出示例
90
109
 
@@ -98,61 +117,56 @@ colors by area: ...(像素级真实占比)
98
117
  luminance grid / color grid
99
118
  ```
100
119
 
101
- ## 环境变量(ZCode 版)
120
+ ## 环境变量
121
+
122
+ ### PaddleOCR(可选)
102
123
 
103
124
  | 变量 | 默认值 | 作用 |
104
125
  |---|---|---|
105
126
  | `DSH_PADDLE_PYTHON` | `C:\Users\Administrator\paddle_venv\Scripts\python.exe` | PaddleOCR 解释器路径(与原插件同名,便于直接迁移) |
106
127
  | `DSH_PADDLE_CACHE` | `<插件目录>\.paddlex-cache` | PaddleX 模型缓存目录 |
107
128
 
129
+ ### 外部视觉 API / VLM(可选,**默认不配置**)
130
+
131
+ | 变量 | 默认值 | 作用 |
132
+ |---|---|---|
133
+ | `SEE_BASE` | `(空)` | OpenAI 兼容视觉端点(留空 = VLM 禁用;本地 llama-server / LM Studio / vLLM / 云端网关) |
134
+ | `SEE_MODEL` | `(空)` | 视觉模型名(如 `google/gemma-4-12b-qat`) |
135
+ | `SEE_API_KEY` | `(空)` | API key(本地端点可随便填,云端需要真实 key) |
136
+ | `SEE_SERVER_EXE` / `SEE_SERVER_MODEL` / `SEE_SERVER_MMPROJ` | `(空)` | 本地 llama-server 自启路径(可选,配置后插件可自动拉起本地视觉服务器) |
137
+ | `SEE_SERVER_PORT` / `SEE_SERVER_NGL` / `SEE_SERVER_CTX` | `8080` / `20` / `16384` | 本地服务器参数 |
138
+
139
+ > **VLM 配置说明**:默认不配置 VLM,`vision_analyze` 会跳过 VLM 调用,只返回像素扫描和 OCR 证据(保持零外部依赖)。需要语义理解时,设置 `SEE_BASE` + `SEE_MODEL` 即可,例如指向本地 LM Studio(`http://127.0.0.1:1234/v1`)。
140
+
108
141
  ## 开发
109
142
 
110
143
  ```sh
111
- # DSH 版(本仓库)
112
144
  npm install
113
- npm test # node:test76 个测试全绿
114
- node scripts/setup-ocr.mjs # 可选:装 PaddleOCR
115
- node scripts/preview.mjs # 生成 fixtures 并预览渲染
116
-
117
- # ZCode 版(独立仓库 jing-hy/picturereader-zcode)
118
- git clone https://github.com/jing-hy/picturereader-zcode.git
119
- cd picturereader-zcode
120
- npm install
121
- npm test # node:test
145
+ npm test # node:test(DSH 版 76 个测试全绿)
122
146
  node scripts/setup-ocr.mjs # 可选
123
147
  ```
124
148
 
125
- **热插拔(DSH 版)**:DSH 本身不支持代码热重载,但本插件自带执行层热加载——业务逻辑全在
126
- `src/core.js` 单文件,工具每次执行按 mtime 动态加载(cache-bust),**改 core.js
127
- 下次调用即生效**;工具定义(schema/描述)改动需重启桌面端。
128
-
129
149
  **热插拔(ZCode 版)**:MCP server 从所选目录运行,改 `src/core.js` 下次调用即生效
130
- (详见 picturereader-zcode 仓库 README)。
150
+ (详见 zcode 分支 README)。
131
151
 
132
152
  ## 优势
133
153
 
134
- - **零外部模型依赖**:核心链路(扫描/取样/解码)纯本地纯 JS,不调任何视觉 API
135
- 语义理解完全交给主模型(DeepSeek),不依赖 YOLO 等固定类别检测器(遇未知物体不失效)
136
- - **可追溯、可验证**:每个结论都有数据支撑(hue 占比、色块坐标、OCR 文本+置信度),
137
- 能主动识别并纠正多模态模型的幻觉(游戏名乱猜、发光颜色误标、小字脑补)
138
- - **隐私友好**:原始图片不出本机,只有降采样文本进模型上下文
154
+ - **一个插件全包含,无需另装**:独立的伪多模态识图 + 可选外部视觉 API,融合在一个包里,不需要再装 `dsh-universal-vision` 或其他读图插件
155
+ - **核心链路零外部依赖**:扫描/取样/解码纯本地纯 JS,不调任何视觉 API;语义理解要么交给主模型,要么按需桥接你自己的 VLM
156
+ - **可选 VLM / 外部 API**:默认不配置保持纯本地;配了 `SEE_BASE` 即接入语义理解,本地/云端 OpenAI 兼容端点通吃
157
+ - **低信息量拦截**(v2.0.0):自动识别空白/简单图,避免小 VLM 幻觉、省调用成本
158
+ - **可追溯、可验证**:每个结论都有数据支撑(hue 占比、色块坐标、OCR 文本+置信度),VLM 描述与像素/OCR 交叉验证
159
+ - **隐私友好**:默认模式下原始图片不出本机,只有降采样文本进模型上下文
139
160
  - **成本低**:一次扫描 ≈0.6–2.2K tokens;PaddleOCR 本地跑,无 API 费用
140
- - **可选增强**:PaddleOCR 一键安装(`scripts/setup-ocr.mjs`),缺失自动降级不崩溃
141
- - **方法论沉淀**:附带的 image-reading skill 把读图经验固化(场景指纹/校验规则),
142
- 模型每次看图都带着经过大量图片验证的经验
161
+ - **方法论沉淀**:附带的 image-reading skill 把读图经验固化(场景指纹/校验规则),模型每次看图都带着经过大量图片验证的经验
143
162
 
144
163
  ## 局限性(重要)
145
164
 
146
- - **不是真正的视觉模型**:文本网格信息量有限,**人脸/表情/花纹等像素级细节读不出**;
147
- 这是文本模态的硬上限,放大(px_per_cell)只能缩小差距,不能消除
148
- - **语义推断依赖主模型能力**:物体识别("这是树/空间站")是 LLM 基于结构证据的推测,
149
- 不是视觉模型的确证——复杂/罕见物体可能推断错误
150
- - **OCR 引擎边界**:Windows OCR 对发光/弯曲/艺术字失效;PaddleOCR 强很多但需选装,
151
- 且对极小文字/极端艺术字仍可能失败(可配合放大)
152
- - **性能**:4K 图解码 ~230ms;PaddleOCR 每次调用需 ~2s 加载模型;大图网格渲染
153
- token 随 size 增长(64×64 color ≈ 3–5K tokens)
154
- - **ZCode 版 MCP 开销**:ZCode 版工具经 MCP stdio 子进程通信,单次调用比 DSH 版慢
155
- (进程启动 + JSON-RPC 序列化);高频/批量看图建议用 DSH 版
165
+ - **不是真正的视觉模型**:伪多模态部分文本网格信息量有限,**人脸/表情/花纹等像素级细节读不出**;这是文本模态的硬上限,放大(px_per_cell)只能缩小差距,不能消除
166
+ - **语义推断依赖主模型或外部 API 能力**:物体识别是 LLM 基于结构证据的推测,不是视觉编码器的确证;接入 VLM 可大幅增强,但外部 API 也可能出错
167
+ - **VLM 可选即插即用**:未配置 `SEE_BASE` 时无语义描述;配置后依赖端点可用性与网络/本地资源
168
+ - **OCR 引擎边界**:Windows OCR 对发光/弯曲/艺术字失效;PaddleOCR 强很多但需选装,且对极小文字/极端艺术字仍可能失败(可配合放大)
169
+ - **性能 / MCP 开销**:ZCode 版工具经 MCP stdio 子进程通信,单次调用比 DSH 版慢;高频/批量看图建议用 DSH 版
156
170
  - **WebP 不支持**(提示转 PNG/JPEG);GIF 只读首帧
157
171
  - **多模态模型的描述不可全信**(本插件可交叉验证,但最终语义仍需人工判断关键场景)
158
172
 
package/mcp/server.js CHANGED
@@ -2,10 +2,11 @@
2
2
  * picturereader MCP server — exposes the image-reading tools to ZCode as a
3
3
  * Model Context Protocol stdio server.
4
4
  *
5
- * Three tools, identical in behavior to the original DSH plugin:
6
- * - `image_scan` coarse pixel grid + hue/color/structure analysis
7
- * - `image_ocr` text recognition (Windows OCR default, PaddleOCR optional)
8
- * - `image_sample` exact-pixel texture sampling for material judgment
5
+ * Four tools, identical in behavior to the original DSH plugin:
6
+ * - `image_scan` coarse pixel grid + hue/color/structure analysis
7
+ * - `image_ocr` text recognition (Windows OCR default, PaddleOCR optional)
8
+ * - `image_sample` exact-pixel texture sampling for material judgment
9
+ * - `vision_analyze` unified pipeline: low-info guard + optional scan/OCR/VLM
9
10
  *
10
11
  * The entire business logic lives in `src/core.js`, loaded dynamically with a
11
12
  * cache-busting query keyed on the file's mtime (see `importCore`), so editing
@@ -22,6 +23,8 @@ import { extname, isAbsolute, resolve as resolvePath } from 'node:path';
22
23
  import { stat, readFile } from 'node:fs/promises';
23
24
  import { createInterface } from 'node:readline';
24
25
  import { pathToFileURL } from 'node:url';
26
+ import { isLowInformationImage } from '../src/guard.js';
27
+ import { ensureServer, stopServer, sendVisionRequest, defaultVlmConfig, isVlmConfigured } from '../src/vlm.js';
25
28
 
26
29
  /** The MCP protocol version this server speaks. */
27
30
  export const PROTOCOL_VERSION = '2025-06-18';
@@ -314,9 +317,120 @@ export async function executeSample(args) {
314
317
  };
315
318
  }
316
319
 
320
+ /**
321
+ * Parse a boolean argument with fallback.
322
+ * @param {any} value - the argument value.
323
+ * @param {boolean} fallback - default value.
324
+ * @returns {boolean} parsed boolean.
325
+ */
326
+ function boolArg(value, fallback = false) {
327
+ if (value === undefined || value === null) return fallback;
328
+ if (typeof value === 'boolean') return value;
329
+ return String(value) === 'true' || String(value) === '1';
330
+ }
331
+
332
+ /**
333
+ * `vision_analyze`: unified image understanding pipeline.
334
+ * Runs low-information guard, optional scan, optional OCR, optional VLM.
335
+ * @param args - tool arguments.
336
+ * @returns the combined analysis result.
337
+ */
338
+ export async function executeVisionAnalyze(args) {
339
+ const { core, filePath, ext } = await loadImageArgs(args, 'vision_analyze');
340
+ const absolutePath = resolveImagePath(filePath);
341
+ const data = await readImageFile(absolutePath, 'vision_analyze');
342
+ const image = decodeBounded(core, data, ext, 'vision_analyze');
343
+
344
+ const includeScan = args.include_scan === undefined ? true : boolArg(args.include_scan, true);
345
+ const includeOcr = args.include_ocr === undefined ? false : boolArg(args.include_ocr, false);
346
+ const includeVlm = args.include_vlm === undefined ? true : boolArg(args.include_vlm, true);
347
+ const allowLowInfo = boolArg(args.allow_low_info, false);
348
+ const stopAfter = boolArg(args.stop_after, false);
349
+ const prompt = args.prompt ?? 'Describe this image in detail.';
350
+
351
+ // Check if VLM is configured
352
+ const vlmAvailable = isVlmConfigured();
353
+ const shouldCallVlm = includeVlm && vlmAvailable;
354
+
355
+ const lowInfo = isLowInformationImage(image.data, image.width, image.height);
356
+ const blocks = [];
357
+ let ocrText = '';
358
+ let scanText = '';
359
+ let vlmText = '';
360
+
361
+ if (lowInfo && !allowLowInfo) {
362
+ const message =
363
+ '[vision_analyze] 低信息量拦截:图片空白或内容极少,为避免 VLM 幻觉,未调用 VLM。' +
364
+ '请检查截图是否空白/未渲染/窗口在屏幕外;如确需识别请设置 allow_low_info=true。';
365
+ return { path: absolutePath, lowInformation: true, message, combined: message };
366
+ }
367
+
368
+ if (includeScan) {
369
+ const analysis = core.analyzeImage(image.data, image.width, image.height, {
370
+ size: 32,
371
+ mode: 'auto',
372
+ region: undefined,
373
+ palette: 'auto'
374
+ });
375
+ scanText = core.renderImageScan({
376
+ path: absolutePath,
377
+ width: image.width,
378
+ height: image.height,
379
+ ...analysis
380
+ });
381
+ blocks.push(`[scan]\n${scanText}`);
382
+ }
383
+
384
+ if (includeOcr) {
385
+ const engine = args.ocr_engine ?? 'windows';
386
+ const ocr = await core.ocrImage(data, ext, { engine });
387
+ ocrText = core.renderOcr({
388
+ path: absolutePath,
389
+ width: ocr.width,
390
+ height: ocr.height,
391
+ region: 'full',
392
+ engine: ocr.engine,
393
+ lines: ocr.lines
394
+ });
395
+ blocks.push(`[ocr]\n${ocrText}`);
396
+ }
397
+
398
+ if (shouldCallVlm) {
399
+ const config = defaultVlmConfig();
400
+ let startedByUs = false;
401
+ try {
402
+ const child = await ensureServer(config);
403
+ startedByUs = child !== null;
404
+ const base64 = data.toString('base64');
405
+ const mime = ext === '.jpg' || ext === '.jpeg' ? 'image/jpeg' : ext === '.png' ? 'image/png' : ext === '.gif' ? 'image/gif' : 'image/bmp';
406
+ const safePrompt =
407
+ prompt +
408
+ '\n\n重要:只描述图中明确可见的内容。如果图中没有明显物体/文字/界面元素,请直接回答:画面空白或内容极少。不要推测、不要脑补不存在的角色/场景/文字。';
409
+ vlmText = await sendVisionRequest(config, [{ mime, base64 }], safePrompt);
410
+ blocks.push(`[vlm]\n${vlmText}`);
411
+ } finally {
412
+ if (stopAfter && startedByUs) {
413
+ await stopServer();
414
+ }
415
+ }
416
+ } else if (includeVlm && !vlmAvailable) {
417
+ blocks.push('[vlm] VLM 未配置(SEE_BASE 环境变量为空)');
418
+ }
419
+
420
+ const combined = blocks.join('\n\n---\n\n');
421
+ return {
422
+ path: absolutePath,
423
+ lowInformation: false,
424
+ ...(scanText ? { scan: scanText } : {}),
425
+ ...(ocrText ? { ocr: ocrText } : {}),
426
+ ...(vlmText ? { vlm: vlmText } : {}),
427
+ combined
428
+ };
429
+ }
430
+
317
431
  /**
318
432
  * Dispatch a tool call to its implementation.
319
- * @param name - tool name ('image_scan' | 'image_ocr' | 'image_sample').
433
+ * @param name - tool name ('image_scan' | 'image_ocr' | 'image_sample' | 'vision_analyze').
320
434
  * @param args - tool arguments object.
321
435
  * @returns the structured tool result.
322
436
  */
@@ -326,6 +440,7 @@ export async function executeTool(name, args) {
326
440
  case 'image_scan': return executeScan(cleanArgs);
327
441
  case 'image_ocr': return executeOcr(cleanArgs);
328
442
  case 'image_sample': return executeSample(cleanArgs);
443
+ case 'vision_analyze': return executeVisionAnalyze(cleanArgs);
329
444
  default: throw new Error(`unknown tool: ${name}`);
330
445
  }
331
446
  }
@@ -453,11 +568,65 @@ export const TOOLS = [
453
568
  },
454
569
  required: ['file_path', 'region']
455
570
  }
571
+ },
572
+ {
573
+ name: 'vision_analyze',
574
+ description: [
575
+ 'Unified image understanding: decode an image, run a low-information guard, optionally scan pixels, OCR text, and/or ask the VLM for a semantic description.',
576
+ 'Use this when you need one call to both verify what is in the image and get a natural-language interpretation.',
577
+ 'Returns evidence blocks: scan (pixel stats), ocr (real text), vlm (model description). If low-information guard triggers and allow_low_info is false, it will not call the VLM.',
578
+ 'Supported formats: PNG, JPEG, GIF (first frame), BMP. WebP is not supported yet.',
579
+ 'VLM is optional: if SEE_BASE is not configured, VLM calls are skipped automatically.',
580
+ 'Smart API calling: simple images (low color diversity, high dominant color coverage) skip VLM automatically.',
581
+ 'Multiple questions: call this tool multiple times with different prompts on the same image for comprehensive analysis.',
582
+ 'Cross-validation: main model should verify VLM results against pixel scan and OCR evidence.'
583
+ ].join(' '),
584
+ inputSchema: {
585
+ type: 'object',
586
+ additionalProperties: true,
587
+ properties: {
588
+ file_path: { type: 'string', description: IMAGE_PATH_DESCRIPTION },
589
+ prompt: {
590
+ type: 'string',
591
+ description: 'Question/instruction for the VLM, e.g. "Describe this UI" or "What is wrong with this map rendering?"'
592
+ },
593
+ include_scan: {
594
+ type: 'boolean',
595
+ description: 'Include pixel scan evidence (default true).'
596
+ },
597
+ include_ocr: {
598
+ type: 'boolean',
599
+ description: 'Include OCR text evidence (default false; set true when text matters).'
600
+ },
601
+ ocr_engine: {
602
+ type: 'string',
603
+ enum: ['windows', 'paddle'],
604
+ description: 'OCR engine: windows (default) or paddle (better for glowing/curved/game text).'
605
+ },
606
+ include_vlm: {
607
+ type: 'boolean',
608
+ description: 'Include VLM description (default true, but skipped if SEE_BASE not configured).'
609
+ },
610
+ allow_low_info: {
611
+ type: 'boolean',
612
+ description: 'Skip the low-information guard and force VLM even on blank/simple images (default false).'
613
+ },
614
+ stop_after: {
615
+ type: 'boolean',
616
+ description: 'Stop the local llama-server after this call if this plugin started it (default false).'
617
+ }
618
+ },
619
+ required: ['file_path']
620
+ }
456
621
  }
457
622
  ];
458
623
 
459
624
  /** Render a structured tool result as the text block fed back to the model. */
460
625
  async function renderResult(name, value) {
626
+ // vision_analyze has its own combined field
627
+ if (name === 'vision_analyze') {
628
+ return value.combined ?? value.message ?? JSON.stringify(value);
629
+ }
461
630
  const core = await importCore();
462
631
  const renderer =
463
632
  name === 'image_scan' ? core.renderImageScan
@@ -571,7 +740,7 @@ export function runServer(input = process.stdin, output = process.stdout, log =
571
740
  };
572
741
  rl.on('line', onLine);
573
742
  rl.on('error', onError);
574
- log.write('picturereader MCP server ready (image_scan / image_ocr / image_sample)\n');
743
+ log.write('picturereader MCP server ready (image_scan / image_ocr / image_sample / vision_analyze)\n');
575
744
  return () => {
576
745
  rl.off('line', onLine);
577
746
  rl.off('error', onError);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "picturereader-zcode",
3
- "version": "1.0.3",
4
- "description": "ZCode plugin: pixel-to-text image reading for text-only models. Downscales and color-quantizes PNG/JPEG/GIF/BMP and feeds the coarse pixel grid to the model so it can \u0027see\u0027 layout, colors and rough shapes without a vision model. Ported from the picturereader DSH plugin.",
3
+ "version": "2.0.0",
4
+ "description": "ZCode plugin: unified image understanding for text-only models. Fuses frame-free pseudo-multimodal reading (pixel scan + OCR + sample) with an optional external vision API bridge (LM Studio / llama-server / any OpenAI-compatible endpoint) for full semantic understanding. No extra plugin needed.",
5
5
  "type": "module",
6
6
  "main": "src/core.js",
7
7
  "exports": {
@@ -33,7 +33,10 @@
33
33
  "paddleocr",
34
34
  "hue",
35
35
  "multimodal",
36
- "mcp"
36
+ "mcp",
37
+ "vlm",
38
+ "vision-analyze",
39
+ "llm-vision"
37
40
  ],
38
41
  "license": "MIT",
39
42
  "author": "picturereader",
@@ -94,3 +94,26 @@ Gogomoe 知识框架分类,教训均来自对真实图片的实测复盘)。
94
94
  → 教训沉淀为 insight 4。
95
95
 
96
96
  (新增经验会持续按以上分类沉淀进本 skill。)
97
+
98
+ ## vision_analyze 统一工具
99
+
100
+ 当需要一次性获取多种证据时,使用 `vision_analyze`:
101
+ - 自动检测空白/简单图片(低信息量拦截)
102
+ - 可选像素扫描(include_scan)
103
+ - 可选 OCR 文字识别(include_ocr)
104
+ - 可选 VLM 语义描述(include_vlm,需配置 SEE_BASE)
105
+ - 所有证据以文本形式返回,供主模型推理
106
+
107
+ ### 推荐的工作流程
108
+
109
+ 建议先用 `image_scan` 自己看,了解图片内容后再决定是否需要调用 VLM。简单图片用像素扫描就够了,复杂场景可以调用 VLM。
110
+
111
+ ### 多次提问
112
+
113
+ 对同一张图可以进行多次不同角度的提问,获取更全面的理解。
114
+
115
+ ### 交叉验证
116
+
117
+ VLM 描述与像素/OCR 证据冲突时,以实测为准。
118
+
119
+ 详细用法见 `skills/vision-analyze.md`。