dsh-vision-router 3.2.1 → 3.3.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.
Files changed (37) hide show
  1. package/README.md +3 -3
  2. package/README.zh.md +18 -3
  3. package/THIRD_PARTY_NOTICES.md +60 -0
  4. package/assets/licenses/llvm-mingw-LICENSE.txt +279 -0
  5. package/assets/licenses/mingw-w64-runtime-COPYING.txt +265 -0
  6. package/assets/licenses/ncnn-LICENSE.txt +86 -0
  7. package/docs/architecture/dsh-support-window.md +25 -6
  8. package/docs/compatibility/dsh-0.2.2-rc3-audit.md +131 -0
  9. package/docs/releases/v3.2.2.md +25 -0
  10. package/docs/releases/v3.3.0.md +34 -0
  11. package/index.js +265 -159
  12. package/lib/client.js +4 -4
  13. package/lib/core-primitives.js +115 -56
  14. package/lib/dsh-support-window.js +6 -3
  15. package/lib/native-ocr-annotation-risk.js +33 -0
  16. package/lib/native-ocr-canary.js +156 -0
  17. package/lib/native-ocr-chemistry-review.js +37 -0
  18. package/lib/native-ocr-document.js +243 -0
  19. package/lib/native-ocr-installation.js +17 -0
  20. package/lib/native-ocr-integrity.js +93 -0
  21. package/lib/native-ocr-question-labels.js +167 -0
  22. package/lib/native-ocr-supervisor.js +268 -0
  23. package/lib/runtime-reliability.js +7 -0
  24. package/lib/session-query-readers.js +8 -2
  25. package/lib/session-vision-attachment-index.js +61 -14
  26. package/lib/settings-ia-client-prelude.js +1 -1
  27. package/lib/tesseract-image-preprocess.js +35 -0
  28. package/lib/trace-vtracer-process-runtime.js +133 -0
  29. package/lib/trace-vtracer-process.js +43 -0
  30. package/lib/vision-attachment-handle-runtime.js +11 -2
  31. package/lib/vision-backend-runtime-policy.js +5 -3
  32. package/lib/vision-image-input-reader.js +67 -0
  33. package/lib/vision-image-input-verdict.js +24 -3
  34. package/lib/vision-tool-runtime-boundary.js +7 -10
  35. package/lib/vision-tool-stage.js +58 -0
  36. package/package.json +5 -4
  37. package/scripts/install-native-ocr.mjs +144 -0
@@ -0,0 +1,268 @@
1
+ import { spawn } from 'node:child_process'
2
+ import { isAbsolute } from 'node:path'
3
+ import { performance } from 'node:perf_hooks'
4
+
5
+ const DEFAULT_TIMEOUT_MS = 12_000
6
+ const MAX_TIMEOUT_MS = 30_000
7
+ const MAX_INPUT_BYTES = 24 * 1024 * 1024
8
+ const MAX_OUTPUT_BYTES = 2 * 1024 * 1024
9
+ const MAX_IMAGE_PIXELS = 4_000_000
10
+ const MAX_IMAGE_SIDE = 4096
11
+ const MAX_ACTIVE_WORKERS = 2
12
+ const MAX_QUEUED_WORKERS = 16
13
+ let activeWorkers = 0
14
+ const waitingWorkers = []
15
+
16
+ function releaseNextWorker() {
17
+ while (waitingWorkers.length > 0) {
18
+ const next = waitingWorkers.shift()
19
+ if (next.finished) continue
20
+ next.finished = true
21
+ clearTimeout(next.timer)
22
+ next.signal?.removeEventListener('abort', next.abort)
23
+ activeWorkers++
24
+ next.resolve(makeReleaseWorker())
25
+ break
26
+ }
27
+ }
28
+ function makeReleaseWorker() {
29
+ let used = false
30
+ return () => {
31
+ if (used) return
32
+ used = true
33
+ activeWorkers--
34
+ releaseNextWorker()
35
+ }
36
+ }
37
+ function acquireWorker(signal, waitMs) {
38
+ if (signal?.aborted) return Promise.reject(error('OCR_CANCELLED', 'OCR cancelled'))
39
+ if (activeWorkers < MAX_ACTIVE_WORKERS) {
40
+ activeWorkers++
41
+ return Promise.resolve(makeReleaseWorker())
42
+ }
43
+ if (waitingWorkers.length >= MAX_QUEUED_WORKERS) {
44
+ return Promise.reject(error('OCR_BUSY', 'local OCR worker queue is full'))
45
+ }
46
+ return new Promise((resolve, reject) => {
47
+ const task = { resolve, reject, signal, finished: false, timer: undefined, abort: undefined }
48
+ const fail = (code, message) => {
49
+ if (task.finished) return
50
+ task.finished = true
51
+ clearTimeout(task.timer)
52
+ signal?.removeEventListener('abort', task.abort)
53
+ const index = waitingWorkers.indexOf(task)
54
+ if (index !== -1) waitingWorkers.splice(index, 1)
55
+ reject(error(code, message))
56
+ }
57
+ task.abort = () => fail('OCR_CANCELLED', 'OCR cancelled')
58
+ task.timer = setTimeout(() => fail('OCR_QUEUE_TIMEOUT', 'local OCR queue deadline exhausted'), waitMs)
59
+ task.timer.unref?.()
60
+ waitingWorkers.push(task)
61
+ signal?.addEventListener('abort', task.abort, { once: true })
62
+ if (signal?.aborted) task.abort()
63
+ })
64
+ }
65
+
66
+ /**
67
+ * In-memory, validated RGB24 transport for the native worker.
68
+ * Image decoders remain Host-owned (sharp); native inference never parses
69
+ * arbitrary PNG/JPEG/WebP bytes or reads user-controlled filesystem paths.
70
+ */
71
+ export function encodeNativeOcrRgbFrame(rgb, width, height) {
72
+ if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) ||
73
+ width < 1 || height < 1 ||
74
+ width > MAX_IMAGE_SIDE || height > MAX_IMAGE_SIDE ||
75
+ width * height > MAX_IMAGE_PIXELS) {
76
+ throw error('OCR_INVALID_DIMENSIONS', 'invalid local OCR image dimensions')
77
+ }
78
+ if (!(rgb instanceof Uint8Array) || rgb.byteLength !== width * height * 3) {
79
+ throw error('OCR_INVALID_RGB', 'invalid local OCR RGB pixel buffer')
80
+ }
81
+ const frame = Buffer.allocUnsafe(20 + rgb.byteLength)
82
+ frame.write('DVR3OCR1', 0, 'ascii')
83
+ frame.writeUInt32LE(width, 8)
84
+ frame.writeUInt32LE(height, 12)
85
+ frame.writeUInt32LE(rgb.byteLength, 16)
86
+ frame.set(rgb, 20)
87
+ return frame
88
+ }
89
+
90
+ export class LocalOcrProcessError extends Error {
91
+ constructor(code, message) {
92
+ super(message)
93
+ this.name = 'LocalOcrProcessError'
94
+ this.code = code
95
+ }
96
+ }
97
+
98
+ function error(code, message) {
99
+ return new LocalOcrProcessError(code, message)
100
+ }
101
+
102
+ function isValidOcrResult(value) {
103
+ return value !== null &&
104
+ typeof value === 'object' &&
105
+ !Array.isArray(value) &&
106
+ typeof value.text === 'string' &&
107
+ value.text.length <= 250_000 &&
108
+ (value.confidence === undefined || (
109
+ typeof value.confidence === 'number' &&
110
+ Number.isFinite(value.confidence) &&
111
+ value.confidence >= 0 && value.confidence <= 1
112
+ )) &&
113
+ (value.uncertain === undefined || typeof value.uncertain === 'boolean')
114
+ }
115
+
116
+ /**
117
+ * Experimental stability boundary. Does not enable/replace the production OCR engine.
118
+ * The caller must provide a trusted, package-owned absolute executable path.
119
+ * The native executable reads image bytes from stdin and writes one JSON result to stdout.
120
+ *
121
+ * No shell, no image path, no implicit cloud fallback, no inherited credentials.
122
+ */
123
+ function runIsolatedLocalOcrProcess(imageBytes, {
124
+ executable,
125
+ args = [],
126
+ timeoutMs = DEFAULT_TIMEOUT_MS,
127
+ signal,
128
+ maxOutputBytes = MAX_OUTPUT_BYTES,
129
+ } = {}) {
130
+ if (!Buffer.isBuffer(imageBytes) || imageBytes.length === 0 || imageBytes.length > MAX_INPUT_BYTES) {
131
+ return Promise.reject(error('OCR_INVALID_INPUT', 'invalid OCR image size'))
132
+ }
133
+ if (typeof executable !== 'string' || !isAbsolute(executable) || executable.includes('\0')) {
134
+ return Promise.reject(error('OCR_UNTRUSTED_RUNNER', 'OCR executable path must be absolute and trusted'))
135
+ }
136
+ if (!Array.isArray(args) || args.some((value) => typeof value !== 'string' || value.includes('\0'))) {
137
+ return Promise.reject(error('OCR_INVALID_ARGUMENTS', 'invalid OCR executable arguments'))
138
+ }
139
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
140
+ return Promise.reject(error('OCR_INVALID_TIMEOUT', 'invalid OCR timeout'))
141
+ }
142
+ const deadlineMs = Math.max(1, Math.min(Math.floor(timeoutMs), MAX_TIMEOUT_MS))
143
+ const limit = Number.isSafeInteger(maxOutputBytes) && maxOutputBytes > 0
144
+ ? Math.min(maxOutputBytes, MAX_OUTPUT_BYTES)
145
+ : MAX_OUTPUT_BYTES
146
+ if (signal?.aborted) return Promise.reject(error('OCR_CANCELLED', 'OCR cancelled'))
147
+
148
+ return new Promise((resolve, reject) => {
149
+ let done = false
150
+ let child
151
+ let timer
152
+ let size = 0
153
+ const buffers = []
154
+ let spawnFailure = false
155
+
156
+ const terminate = () => {
157
+ try { child?.kill('SIGKILL') } catch {}
158
+ }
159
+ const abort = () => {
160
+ terminate()
161
+ settle(error('OCR_CANCELLED', 'OCR cancelled'))
162
+ }
163
+ const settle = (failure, result) => {
164
+ if (done) return
165
+ done = true
166
+ if (timer !== undefined) clearTimeout(timer)
167
+ signal?.removeEventListener('abort', abort)
168
+ if (failure) reject(failure)
169
+ else resolve(result)
170
+ }
171
+
172
+ try {
173
+ // Limit the environment to OS runtime variables, not secrets or user-provided
174
+ // proxy/configuration variables. The executable MUST be bundled and self-contained.
175
+ const env = {}
176
+ for (const name of ['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'TMPDIR', 'HOME', 'LANG', 'LC_ALL']) {
177
+ if (typeof process.env[name] === 'string') env[name] = process.env[name]
178
+ }
179
+ // Linux worker registers PR_SET_PDEATHSIG before reading RGB input.
180
+ // A non-secret expected PID closes the fork/exec vs parent-SIGKILL race.
181
+ if (process.platform === 'linux') env.DVR_OCR_PARENT_PID = String(process.pid)
182
+ child = spawn(executable, args, {
183
+ shell: false,
184
+ windowsHide: true,
185
+ stdio: ['pipe', 'pipe', 'pipe'],
186
+ env,
187
+ })
188
+ } catch {
189
+ settle(error('OCR_SPAWN_FAILED', 'unable to start local OCR worker'))
190
+ return
191
+ }
192
+
193
+ signal?.addEventListener('abort', abort, { once: true })
194
+ timer = setTimeout(() => {
195
+ terminate()
196
+ settle(error('OCR_TIMEOUT', 'local OCR worker exceeded its deadline'))
197
+ }, deadlineMs)
198
+ timer.unref?.()
199
+
200
+ child.on('error', () => {
201
+ spawnFailure = true
202
+ settle(error('OCR_SPAWN_FAILED', 'unable to start local OCR worker'))
203
+ })
204
+ // Some platforms write EPIPE after the worker exits early. Its exit status,
205
+ // not this stream error, determines the OCR result.
206
+ child.stdin.on('error', () => {})
207
+ child.stderr.on('data', () => {}) // Never leak native stderr or sensitive images.
208
+ child.stdout.on('data', (chunk) => {
209
+ if (done) return
210
+ size += chunk.length
211
+ if (size > limit) {
212
+ terminate()
213
+ settle(error('OCR_OUTPUT_LIMIT', 'local OCR output exceeded the size limit'))
214
+ return
215
+ }
216
+ buffers.push(chunk)
217
+ })
218
+ child.on('close', (exitCode, exitSignal) => {
219
+ if (done) return
220
+ if (spawnFailure || exitCode !== 0 || exitSignal !== null) {
221
+ settle(error('OCR_WORKER_FAILED', 'local OCR worker exited unsuccessfully'))
222
+ return
223
+ }
224
+ try {
225
+ const result = JSON.parse(Buffer.concat(buffers, size).toString('utf8'))
226
+ if (!isValidOcrResult(result)) {
227
+ settle(error('OCR_INVALID_RESULT', 'local OCR returned an invalid result'))
228
+ return
229
+ }
230
+ settle(undefined, { ...result, engine: 'ncnn' })
231
+ } catch {
232
+ settle(error('OCR_INVALID_RESULT', 'local OCR returned invalid JSON'))
233
+ }
234
+ })
235
+ if (signal?.aborted) {
236
+ abort()
237
+ return
238
+ }
239
+ child.stdin.end(imageBytes)
240
+ })
241
+ }
242
+
243
+ /**
244
+ * At most two native model instances can execute simultaneously. All other
245
+ * requests wait in a bounded, abortable FIFO. This protects the Host against
246
+ * a burst of 100+ concurrent images creating hundreds of native model copies.
247
+ */
248
+ export async function runIsolatedLocalOcr(imageBytes, options = {}) {
249
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS
250
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
251
+ throw error('OCR_INVALID_TIMEOUT', 'invalid OCR timeout')
252
+ }
253
+ const budgetMs = Math.max(1, Math.min(Math.floor(timeoutMs), MAX_TIMEOUT_MS))
254
+ const started = performance.now()
255
+ const release = await acquireWorker(options.signal, budgetMs)
256
+ try {
257
+ const remainingMs = budgetMs - (performance.now() - started)
258
+ if (remainingMs <= 0) {
259
+ throw error('OCR_QUEUE_TIMEOUT', 'local OCR queue deadline exhausted')
260
+ }
261
+ return await runIsolatedLocalOcrProcess(imageBytes, {
262
+ ...options,
263
+ timeoutMs: remainingMs,
264
+ })
265
+ } finally {
266
+ release()
267
+ }
268
+ }
@@ -165,6 +165,13 @@ export async function describeCacheKey(ctx, args = {}, exec, revision = 0) {
165
165
  hash.update(`revision:${revision}\n`)
166
166
  const paths = Array.isArray(args.paths) ? args.paths : []
167
167
  const attachments = Array.isArray(args.attachmentIds) ? args.attachmentIds : []
168
+ // Cache hits bypass the actual tool executor, including its Session-owned
169
+ // attachment authorization. Until authorization can be proved at this outer
170
+ // boundary, never cache any upload-identity input (including the paths alias).
171
+ // Ordinary paths remain cacheable only after fs.resolve/readBytes succeeds.
172
+ if (attachments.length > 0 || paths.some((input) => /^sha256:/i.test(String(input).trim()))) {
173
+ return undefined
174
+ }
168
175
  const fs = ctx?.get?.('fs')
169
176
  const cwd = exec?.agent?.session?.header?.cwd
170
177
  const signal = exec?.signal
@@ -214,7 +214,8 @@ export function createSessionEventTailReader(ctx) {
214
214
  * the released readSession() path.
215
215
  */
216
216
  export function createSessionLogReader(ctx) {
217
- return async function readSessionLog(session) {
217
+ return async function readSessionLog(session, signal) {
218
+ signal?.throwIfAborted();
218
219
  const sessionId = sessionIdOf(session);
219
220
  if (sessionId === undefined || sessionId === null)
220
221
  return { supported: false };
@@ -225,7 +226,8 @@ export function createSessionLogReader(ctx) {
225
226
  if (typeof observeSession === 'function') {
226
227
  let observation;
227
228
  try {
228
- observation = await observeSession.call(query, sessionId, { projectionMode: 'none' });
229
+ observation = await observeSession.call(query, sessionId, signal === undefined ? { projectionMode: 'none' } : { projectionMode: 'none', signal });
230
+ signal?.throwIfAborted();
229
231
  const observationBag = propertyBag(observation);
230
232
  const events = observationBag?.events;
231
233
  if (!Array.isArray(events)) {
@@ -249,6 +251,10 @@ export function createSessionLogReader(ctx) {
249
251
  if (typeof readSession !== 'function')
250
252
  return { supported: false };
251
253
  const snapshot = await readSession.call(query, sessionId);
254
+ // Older Hosts expose readSession without a signal parameter. They cannot
255
+ // interrupt the underlying read, but aborted reads must never publish
256
+ // recovered attachment authority after the result eventually arrives.
257
+ signal?.throwIfAborted();
252
258
  const snapshotBag = propertyBag(snapshot);
253
259
  const events = snapshotBag?.events;
254
260
  if (!Array.isArray(events)) {
@@ -1,4 +1,5 @@
1
1
  import { legacySessionEvents } from './session-event-history-compat.js';
2
+ import { sessionImageAttachmentRefs } from './vision-attachment-handle-runtime.js';
2
3
  function propertyBag(value) {
3
4
  return value !== null && (typeof value === 'object' || typeof value === 'function')
4
5
  ? value
@@ -23,10 +24,21 @@ function iterableValues(value) {
23
24
  }
24
25
  return value;
25
26
  }
26
- function boundedMessage(error) {
27
- const record = propertyBag(error);
28
- const text = record?.message ?? error;
29
- return String(text ?? '').slice(0, 400);
27
+ // Host error messages may contain file paths, image ids or provider credentials.
28
+ // Only log a controlled diagnostic category, never arbitrary exception text.
29
+ function recoveryFailureKind(error) {
30
+ const code = propertyBag(error)?.code;
31
+ if (code === 'ENOENT')
32
+ return 'not-found';
33
+ if (code === 'EACCES' || code === 'EPERM')
34
+ return 'permission';
35
+ if (code === 'EBUSY')
36
+ return 'busy';
37
+ if (code === 'ETIMEDOUT')
38
+ return 'timeout';
39
+ if (code === 'SESSION_QUERY_CORRUPT_SESSION')
40
+ return 'corrupt-session';
41
+ return 'session-query-error';
30
42
  }
31
43
  /**
32
44
  * Attachment access boundary for one SessionVisionStateStore.
@@ -76,16 +88,18 @@ export function createSessionVisionAttachmentIndex({ stateStore, collectEventAtt
76
88
  return found;
77
89
  };
78
90
  const warnAttachmentRecoveryFailure = (session, attachmentId, error) => {
79
- const message = boundedMessage(error);
80
- const warningKey = `${String(attachmentId)}:${message}`;
91
+ const kind = recoveryFailureKind(error);
92
+ // The full attachment id is used only for in-memory de-duplication.
93
+ const warningKey = `${String(attachmentId)}:${kind}`;
81
94
  const key = weakKey(session);
82
95
  if (key !== undefined && attachmentRecoveryWarnings.get(key) === warningKey)
83
96
  return;
84
97
  if (key !== undefined)
85
98
  attachmentRecoveryWarnings.set(key, warningKey);
86
- logger?.warn?.('vision-router: session attachment recovery failed id=%s error=%s', String(attachmentId), message);
99
+ logger?.warn?.('vision-router: session attachment recovery failed kind=%s', kind);
87
100
  };
88
- const resolveAttachments = async (session, attachmentIds) => {
101
+ const resolveAttachments = async (session, attachmentIds, signal) => {
102
+ signal?.throwIfAborted();
89
103
  const requested = [
90
104
  ...new Set((Array.isArray(attachmentIds) ? attachmentIds : []).map((id) => String(id))),
91
105
  ];
@@ -100,14 +114,45 @@ export function createSessionVisionAttachmentIndex({ stateStore, collectEventAtt
100
114
  }
101
115
  if (missing.length === 0 || session === undefined)
102
116
  return resolved;
117
+ // Modern Session.deriveMessages() gives Session-owned image references
118
+ // without acquiring the Host SessionQuery observation lease. This is the
119
+ // same authorized source already used for projected short image handles.
120
+ // Especially on a cold resumed Session, avoid waiting on full-log I/O
121
+ // when all requested images are already present in derived messages.
122
+ const wanted = new Set(missing);
123
+ const fromSession = [];
124
+ for (const value of sessionImageAttachmentRefs(session)) {
125
+ const ref = attachmentRef(value);
126
+ if (ref === undefined)
127
+ continue;
128
+ const id = attachmentIdOf(ref);
129
+ if (!wanted.has(id))
130
+ continue;
131
+ wanted.delete(id);
132
+ resolved.set(id, ref);
133
+ fromSession.push(ref);
134
+ if (wanted.size === 0)
135
+ break;
136
+ }
137
+ signal?.throwIfAborted();
138
+ if (fromSession.length > 0)
139
+ stateStore.recordAttachments(session, fromSession);
140
+ if (wanted.size === 0)
141
+ return resolved;
142
+ const remaining = missing.filter((id) => wanted.has(id));
103
143
  let events;
104
144
  if (typeof readSessionLog === 'function') {
105
145
  let result;
106
146
  try {
107
- result = await readSessionLog(session);
147
+ result = await readSessionLog(session, signal);
148
+ signal?.throwIfAborted();
108
149
  }
109
150
  catch (error) {
110
- warnAttachmentRecoveryFailure(session, missing[0], error);
151
+ // Cancellation is not a corrupted or missing Session. Never convert
152
+ // it to a successful partial lookup or log an attachment identifier.
153
+ if (signal?.aborted)
154
+ throw signal.reason ?? error;
155
+ warnAttachmentRecoveryFailure(session, remaining[0], error);
111
156
  return resolved;
112
157
  }
113
158
  const resultRecord = propertyBag(result);
@@ -118,21 +163,23 @@ export function createSessionVisionAttachmentIndex({ stateStore, collectEventAtt
118
163
  events = resultRecord.events;
119
164
  }
120
165
  else if (resultRecord?.supported !== false) {
121
- warnAttachmentRecoveryFailure(session, missing[0], new Error('Session log reader returned an invalid capability result'));
166
+ warnAttachmentRecoveryFailure(session, remaining[0], new Error('Session log reader returned an invalid capability result'));
122
167
  return resolved;
123
168
  }
124
169
  }
125
170
  // Explicitly unsupported SessionQuery keeps the released Session-local
126
171
  // compatibility fallback. Advertised reader failures above never fall back.
172
+ signal?.throwIfAborted();
127
173
  if (events === undefined)
128
174
  events = legacySessionEvents(session);
129
- const recovered = recoverAttachmentsFromEvents(session, missing, events);
175
+ signal?.throwIfAborted();
176
+ const recovered = recoverAttachmentsFromEvents(session, remaining, events);
130
177
  for (const [id, ref] of recovered)
131
178
  resolved.set(id, ref);
132
179
  return resolved;
133
180
  };
134
- const resolveAttachment = async (session, attachmentId) => {
135
- return (await resolveAttachments(session, [attachmentId])).get(String(attachmentId));
181
+ const resolveAttachment = async (session, attachmentId, signal) => {
182
+ return (await resolveAttachments(session, [attachmentId], signal)).get(String(attachmentId));
136
183
  };
137
184
  return Object.freeze({
138
185
  recordAttachments,
@@ -363,7 +363,7 @@ export const SETTINGS_IA_CLIENT_PRELUDE = String.raw`(function(){
363
363
  function localPageContent(){return h(React.Fragment,null,title(tx('本地与设备','Local & device'),tx('本地视觉可让图像像素留在本机;但若云端识图行或免费兜底仍开启,失败时仍可能出网,识图结果也会交给当前聊天模型。','Local vision can keep image pixels on this machine. Cloud vision rows or the free fallback may still send them off-device after a failure, and vision result text is still passed to the current chat model.')),localProviderCard('localOllama','Ollama',{enabled:false,baseURL:'http://127.0.0.1:11434/v1',model:'qwen2.5vl',format:'openai',maxTokens:4096,reasoningEffort:'none'},'ollama'),localProviderCard('localLmStudio','LM Studio',{enabled:false,baseURL:'http://localhost:1234/v1',model:'',format:'openai',maxTokens:4096,reasoningEffort:'none'},'lmstudio'),card([toggle('desktopScreenshot',tx('允许 Agent 读取桌面截图','Allow the agent to capture the desktop'),tx('这是独立的隐私权限;macOS 上保存为开启后会立即触发屏幕录制权限检查。','This is a separate privacy permission; on macOS saving it enabled immediately triggers the screen-recording permission check.'))]));}
364
364
  function wrappersEditor(){var rows=wrapperRows();return h('div',{className:'vr-field'},fieldHead('wrappedProviders',tx('哪些聊天模型可以开启识图','Which chat models can use Vision mode')),h('p',{className:'vr-hint'},tx('通常无需修改;模型留空表示整个 Provider。','Usually leave this alone; an empty model means the whole provider.')),rows.map(function(row,index){return modelRow(row,index,rows,setWrapperDraft,true);}),h('button',{type:'button',className:'vr-btn',disabled:!writable||saving,onClick:function(){setWrapperDraft(rows.concat([{provider:'',model:''}]))}},tx('+ 添加范围','+ Add scope')));}
365
365
  function textProviderEditor(){var current=Object.assign({provider:'',model:''},obj(value('textProvider',{}))),ready=groups.length>0;return h('div',{className:'vr-field'},fieldHead('textProvider',tx('文字回退模型','Text fallback model')),h('div',{className:'vr-chain-row'},ready?h('select',{className:'vr-input',value:current.provider||'',disabled:!writable||saving,onChange:function(event){setValue('textProvider',{provider:event.target.value,model:''});}},providerOptions(current.provider||'')):h('input',{className:'vr-input',value:current.provider||'',disabled:!writable||saving,onChange:function(event){setValue('textProvider',{provider:event.target.value,model:current.model||''});}}),ready?h('select',{className:'vr-input',value:current.model||'',disabled:!writable||saving||!current.provider,onChange:function(event){setValue('textProvider',{provider:current.provider||'',model:event.target.value});}},modelOptions(current.provider||'',current.model||'',false)):h('input',{className:'vr-input',value:current.model||'',disabled:!writable||saving,onChange:function(event){setValue('textProvider',{provider:current.provider||'',model:event.target.value});}})),invalidKeys.includes('textProvider')?h('p',{className:'vr-failed'},tx('Provider 和 model 必须同时填写,或同时留空恢复默认。','Provider and model must both be filled, or both left empty to restore the default.')):null);}
366
- function advancedPage(){var routing=toggleValue('routing',false),turnBudget=Number(value('visionTurnBudgetMs',0))||0,customBudget=turnBudget>0;return h(React.Fragment,null,title(tx('高级','Advanced'),tx('这些默认值对大多数用户已经合适。','Defaults are suitable for most users.')),card([h('h4',{className:'vr-ia-subtitle',key:'p'},tx('性能与稳定性','Performance & stability')),toggle('downscale',tx('自动缩放','Auto downscale')),numberField('downscaleMaxPixels',tx('图片像素上限','Image pixel limit'),null,1000,100000000),toggle('cache',tx('识图缓存','Vision answer cache')),numberField('cacheTtlSeconds',tx('缓存有效期(秒)','Cache TTL (seconds)'),null,0,31536000),numberField('cacheMaxEntries',tx('最大缓存数量','Maximum cached answers'),null,1,100000),h('h4',{className:'vr-ia-subtitle',key:'t'},tx('超时','Timeouts')),numberField('timeoutMs',tx('单次模型请求','Single model request'),tx('毫秒。','Milliseconds.'),1000,600000),numberField('visionTaskTimeoutMs',tx('单个视觉任务','Single visual task'),tx('包含该任务内部的重试和备用模型;不是每个后端各自一份。','Includes retries and fallbacks inside the task; it is not a fresh budget per backend.'),1000,180000),numberField('ocrTimeoutMs',tx('OCR 任务','OCR task'),null,1000,120000),h('div',{className:'vr-field',key:'ocr-engine'},fieldHead('ocrEngine',tx('OCR 默认引擎','Default OCR engine')),h('select',{className:'vr-input',value:String(value('ocrEngine','auto')||'auto'),disabled:!writable||saving,onChange:function(event){setValue('ocrEngine',event.target.value);}},h('option',{value:'auto'},tx('自动(Tesseract → 视觉模型)','Auto (Tesseract → vision)')),h('option',{value:'tesseract'},tx('仅 Tesseract','Tesseract only')),h('option',{value:'vision'},tx('仅视觉模型','Vision model only'))),h('p',{className:'vr-hint'},tx('vision_ocr 未显式指定 engine 时使用;单次调用的 engine=tesseract/vision 始终优先。','Used when vision_ocr does not explicitly choose an engine; per-call engine=tesseract/vision always wins.'))),h('div',{className:'vr-field',key:'budget'},fieldHead('visionTurnBudgetMs',tx('整轮视觉工具上限','Whole-turn vision-tool limit')),h('select',{className:'vr-input',value:customBudget?'custom':'unlimited',disabled:!writable||saving,onChange:function(event){setValue('visionTurnBudgetMs',event.target.value==='unlimited'?0:(turnBudget>0?turnBudget:180000));}},h('option',{value:'unlimited'},tx('不限制(推荐)','Unlimited (recommended)')),h('option',{value:'custom'},tx('自定义','Custom'))),customBudget?numberField('visionTurnBudgetMs',tx('上限(毫秒)','Limit (ms)'),null,10000,600000):null)]),card([h('h4',{className:'vr-ia-subtitle',key:'cost'},tx('模型顺序与成本','Model order & cost')),toggle('freeCloudFirst',tx('免费云模型优先','Try free cloud models first'))]),card([h('h4',{className:'vr-ia-subtitle',key:'scope'},tx('识图模式范围','Vision mode scope')),toggle('autoWrapProviders',tx('自动允许已启用模型使用识图','Automatically allow enabled models to use Vision mode')),wrappersEditor()]),card([h('h4',{className:'vr-ia-subtitle',key:'network'},tx('网络与远程','Network & remote')),h('p',{className:'vr-hint',key:'network-authority'},tx('默认沿用 DSH/Host 的网络出口;只有需要让视觉请求走不同出口时才填写下方覆盖。','By default Vision Router follows the DSH/Host network path. Use the override below only when vision requests need a different egress route.')),local?toggle('allowRemoteSettings',tx('允许可信 Host 远程修改设置','Allow trusted-host remote settings'),tx('默认关闭;trustedHosts 不是身份认证。','Off by default; trustedHosts is not authentication.')):null,textField('proxy',tx('视觉专用代理覆盖','Vision-only proxy override'),tx('留空即沿用 DSH/Host;支持 http(s)://、socks5://,并兼容旧 socks5h://。','Leave empty to follow DSH/Host; supports http(s):// and socks5://, with legacy socks5h:// compatibility.')),textareaArray('proxyHosts',tx('视觉代理覆盖域名','Vision proxy override hosts'),tx('仅在上方覆盖非空时生效;每行一个,其余请求继续沿用 DSH/Host。','Used only when the override above is non-empty; one host per line. All other requests keep the DSH/Host path.'))]),card([h('h4',{className:'vr-ia-subtitle',key:'compat'},tx('兼容模式','Compatibility')),toggle('rewriteImages',tx('保护纯文本模型','Protect text-only models')),toggle('routing',tx('整轮视觉路由(旧工作流)','Whole-turn vision routing (legacy workflow)')),routing?toggle('reverseRouting',tx('纯文字消息继续使用聊天模型','Keep text-only messages on the chat model')):null,routing?textProviderEditor():null]),card([h('button',{type:'button',className:'vr-btn',key:'dev',onClick:function(){setOpen(Object.assign({},open,{developer:!open.developer}));}},open.developer?tx('隐藏开发者设置','Hide developer settings'):tx('显示开发者设置','Show developer settings')),open.developer?h('div',{className:'vr-ia-dev',key:'body'},toggle('progressiveTools',tx('渐进式工具暴露','Progressive tool exposure')),h('p',{className:'vr-hint',key:'progressive-restart'},tx('保存后需重启 DSH 才生效。','Restart DSH after saving for this change to take effect.')),toggle('stealth','Stealth'),textField('wrapperRoute',tx('包装路由名','Wrapper route name')),textField('chainRoute',tx('视觉链路由名','Vision chain route name')),textareaArray('extraVisionModels',tx('额外视觉能力标记','Extra vision capability labels'),tx('只有诊断发现模型未声明图片能力时才需要。','Needed only when diagnostics show missing image-capability metadata.'))):null]));}
366
+ function advancedPage(){var routing=toggleValue('routing',false),turnBudget=Number(value('visionTurnBudgetMs',0))||0,customBudget=turnBudget>0;return h(React.Fragment,null,title(tx('高级','Advanced'),tx('这些默认值对大多数用户已经合适。','Defaults are suitable for most users.')),card([h('h4',{className:'vr-ia-subtitle',key:'p'},tx('性能与稳定性','Performance & stability')),toggle('downscale',tx('自动缩放','Auto downscale')),numberField('downscaleMaxPixels',tx('图片像素上限','Image pixel limit'),null,1000,100000000),toggle('cache',tx('识图缓存','Vision answer cache')),numberField('cacheTtlSeconds',tx('缓存有效期(秒)','Cache TTL (seconds)'),null,0,31536000),numberField('cacheMaxEntries',tx('最大缓存数量','Maximum cached answers'),null,1,100000),h('h4',{className:'vr-ia-subtitle',key:'t'},tx('超时','Timeouts')),numberField('timeoutMs',tx('单次模型请求','Single model request'),tx('毫秒。','Milliseconds.'),1000,600000),numberField('visionTaskTimeoutMs',tx('单个视觉任务','Single visual task'),tx('包含该任务内部的重试和备用模型;不是每个后端各自一份。','Includes retries and fallbacks inside the task; it is not a fresh budget per backend.'),1000,180000),numberField('ocrTimeoutMs',tx('OCR 任务','OCR task'),null,1000,120000),h('div',{className:'vr-field',key:'ocr-engine'},fieldHead('ocrEngine',tx('OCR 默认引擎','Default OCR engine')),h('select',{className:'vr-input',value:String(value('ocrEngine','auto')||'auto'),disabled:!writable||saving,onChange:function(event){setValue('ocrEngine',event.target.value);}},h('option',{value:'auto'},tx('自动(PP-OCRv6 → Tesseract → 视觉模型)','Auto (PP-OCRv6 → Tesseract → vision)')),h('option',{value:'tesseract'},tx('仅 Tesseract','Tesseract only')),h('option',{value:'vision'},tx('仅视觉模型','Vision model only'))),h('p',{className:'vr-hint'},tx('vision_ocr 未显式指定 engine 时使用;单次调用的 engine=tesseract/vision 始终优先。','Used when vision_ocr does not explicitly choose an engine; per-call engine=tesseract/vision always wins.'))),h('div',{className:'vr-field',key:'budget'},fieldHead('visionTurnBudgetMs',tx('整轮视觉工具上限','Whole-turn vision-tool limit')),h('select',{className:'vr-input',value:customBudget?'custom':'unlimited',disabled:!writable||saving,onChange:function(event){setValue('visionTurnBudgetMs',event.target.value==='unlimited'?0:(turnBudget>0?turnBudget:180000));}},h('option',{value:'unlimited'},tx('不限制(推荐)','Unlimited (recommended)')),h('option',{value:'custom'},tx('自定义','Custom'))),customBudget?numberField('visionTurnBudgetMs',tx('上限(毫秒)','Limit (ms)'),null,10000,600000):null)]),card([h('h4',{className:'vr-ia-subtitle',key:'cost'},tx('模型顺序与成本','Model order & cost')),toggle('freeCloudFirst',tx('免费云模型优先','Try free cloud models first'))]),card([h('h4',{className:'vr-ia-subtitle',key:'scope'},tx('识图模式范围','Vision mode scope')),toggle('autoWrapProviders',tx('自动允许已启用模型使用识图','Automatically allow enabled models to use Vision mode')),wrappersEditor()]),card([h('h4',{className:'vr-ia-subtitle',key:'network'},tx('网络与远程','Network & remote')),h('p',{className:'vr-hint',key:'network-authority'},tx('默认沿用 DSH/Host 的网络出口;只有需要让视觉请求走不同出口时才填写下方覆盖。','By default Vision Router follows the DSH/Host network path. Use the override below only when vision requests need a different egress route.')),local?toggle('allowRemoteSettings',tx('允许可信 Host 远程修改设置','Allow trusted-host remote settings'),tx('默认关闭;trustedHosts 不是身份认证。','Off by default; trustedHosts is not authentication.')):null,textField('proxy',tx('视觉专用代理覆盖','Vision-only proxy override'),tx('留空即沿用 DSH/Host;支持 http(s)://、socks5://,并兼容旧 socks5h://。','Leave empty to follow DSH/Host; supports http(s):// and socks5://, with legacy socks5h:// compatibility.')),textareaArray('proxyHosts',tx('视觉代理覆盖域名','Vision proxy override hosts'),tx('仅在上方覆盖非空时生效;每行一个,其余请求继续沿用 DSH/Host。','Used only when the override above is non-empty; one host per line. All other requests keep the DSH/Host path.'))]),card([h('h4',{className:'vr-ia-subtitle',key:'compat'},tx('兼容模式','Compatibility')),toggle('rewriteImages',tx('保护纯文本模型','Protect text-only models')),toggle('routing',tx('整轮视觉路由(旧工作流)','Whole-turn vision routing (legacy workflow)')),routing?toggle('reverseRouting',tx('纯文字消息继续使用聊天模型','Keep text-only messages on the chat model')):null,routing?textProviderEditor():null]),card([h('button',{type:'button',className:'vr-btn',key:'dev',onClick:function(){setOpen(Object.assign({},open,{developer:!open.developer}));}},open.developer?tx('隐藏开发者设置','Hide developer settings'):tx('显示开发者设置','Show developer settings')),open.developer?h('div',{className:'vr-ia-dev',key:'body'},toggle('progressiveTools',tx('渐进式工具暴露','Progressive tool exposure')),h('p',{className:'vr-hint',key:'progressive-restart'},tx('保存后需重启 DSH 才生效。','Restart DSH after saving for this change to take effect.')),toggle('stealth','Stealth'),textField('wrapperRoute',tx('包装路由名','Wrapper route name')),textField('chainRoute',tx('视觉链路由名','Vision chain route name')),textareaArray('extraVisionModels',tx('额外视觉能力标记','Extra vision capability labels'),tx('只有诊断发现模型未声明图片能力时才需要。','Needed only when diagnostics show missing image-capability metadata.'))):null]));}
367
367
  function diagnosticValue(label,valueText){return h('div',{className:'vr-ia-diag-row',key:label},h('span',null,label),h('strong',null,valueText));}
368
368
  function updatePanel(){if(!local)return null;var result=updateState.result,auto=result&&result.autoUpdate,current=result&&result.currentVersion?result.currentVersion:tx('检测中','Checking'),latest=result&&result.latestVersion?result.latestVersion:'—',available=result&&result.ok===true&&result.updateAvailable===true,profile=auto&&auto.profile?auto.profile:'web',manualVersion=result&&result.latestVersion?result.latestVersion:'<version>',spec='dsh-vision-router@'+manualVersion,pnpm='pnpm dsh plugin --profile '+profile+' add '+spec,npx='npx @deepseek-ai/dsh plugin --profile '+profile+' add '+spec;return card([h('h4',{className:'vr-ia-subtitle',key:'title'},tx('版本更新','Updates')),h('p',{className:'vr-hint',key:'status'},updateState.status==='running'?tx('正在检查更新…','Checking for updates…'):result&&result.ok===false?tx('更新检查失败:','Update check failed: ')+String(result.error||'unknown'):available?tx('发现新版本:v','Update available: v')+latest+tx('(当前 v',' (current v')+current+')':result&&result.ok===true?tx('已是最新版本 v','Up to date: v')+current:tx('尚未检查','Not checked yet')),h('div',{className:'vr-ia-actions',key:'actions'},h('button',{type:'button',className:'vr-btn',disabled:updateState.status==='running',onClick:function(){void runUpdateCheck(true);}},tx('检查更新','Check for updates')),available&&auto&&auto.supported===true&&auto.token?h('button',{type:'button',className:'vr-btn vr-btn-save',disabled:selfUpdateState.status==='running',onClick:function(){void runSelfUpdate();}},selfUpdateState.status==='running'?tx('更新中…','Updating…'):tx('一键更新','Update now')):null),selfUpdateState.status==='done'&&selfUpdateState.result?h('p',{className:'vr-hint',key:'updated'},tx('更新完成,请重启 DSH。','Update complete. Restart DSH.')):selfUpdateState.error?h('p',{className:'vr-failed',key:'uerr'},String(selfUpdateState.error)):null,available&&(!auto||auto.supported!==true)?h('div',{className:'vr-ia-manual',key:'manual'},h('p',{className:'vr-hint'},tx('当前安装方式不支持安全的一键更新,请使用与你当前 DSH 安装方式一致的命令:','This install method cannot be safely auto-updated. Use the command matching your DSH installation:')),h('code',{className:'vr-ia-code'},pnpm),h('code',{className:'vr-ia-code'},npx)):null]);}
369
369
  function diagnosticsPage(){var providerCount=groups.length,modelCount=groups.reduce(function(total,group){return total+arr(group&&group.models).length;},0),configured=chainRows().filter(function(row){return row&&row.provider&&row.model;}).length,ollama=obj(value('localOllama',{})),lm=obj(value('localLmStudio',{})),testText=!local?tx('仅本机可见','Local only'):testState.status==='running'?tx('检测中','Checking'):testState.status==='done'&&testState.result&&testState.result.ok===true?tx('连接正常','Connected'):testState.status==='done'?tx('连接失败','Failed'):tx('未检测','Not checked'),capsText=caps.status==='ready'?tx('正常','Ready'):caps.status==='loading'?tx('检测中','Checking'):caps.status==='error'?tx('不可用','Unavailable'):tx('未检测','Not checked');var rows=[[tx('设置协议','Settings contract'),String(value('settingsContractRevision','—'))],[tx('模型目录','Model catalog'),catalog.status==='ready'?tx('正常','Ready'):catalog.status==='loading'?tx('检测中','Checking'):tx('不可用','Unavailable')],[tx('图片能力元数据','Image capability metadata'),capsText],[tx('可选 Provider','Selectable providers'),String(providerCount)],[tx('可选模型','Selectable models'),String(modelCount)],[tx('已配置识图模型','Configured vision models'),String(configured)],[tx('内置免费兜底','Built-in free fallback'),toggleValue('freeFallback',true)?(caps.builtinFallback.length?tx('已启用,','Enabled, ')+caps.builtinFallback.length+tx(' 个模型',' models'):tx('已启用','Enabled')):tx('已关闭','Disabled')],[tx('后端连接','Backend connection'),testText],[tx('Ollama','Ollama'),(ollama.enabled===true?tx('已启用','Enabled'):tx('未启用','Disabled'))],[tx('LM Studio','LM Studio'),(lm.enabled===true?tx('已启用','Enabled'):tx('未启用','Disabled'))],[tx('桌面截图','Desktop capture'),(toggleValue('desktopScreenshot',false)?tx('已启用','Enabled'):tx('未启用','Disabled'))],[tx('网络出口','Network egress'),(String(value('proxy','')).trim()?tx('视觉专用覆盖','Vision-only override'):tx('DSH/Host(默认)','DSH/Host (default)'))],[tx('识图结果去向','Vision result destination'),tx('当前聊天模型(可能为云端)','Current chat model (may be remote)')],[tx('远程设置','Remote settings'),local?(toggleValue('allowRemoteSettings',false)?tx('已启用','Enabled'):tx('未启用','Disabled')):tx('已通过风险确认','Risk acknowledged')]];function reportText(){return ['Vision Router diagnostics'].concat(rows.map(function(row){return row[0]+': '+row[1];}),['doctor: dsh-vision-router doctor']).join('\n');}async function copyReport(){try{if(typeof navigator!=='undefined'&&navigator.clipboard&&typeof navigator.clipboard.writeText==='function')await navigator.clipboard.writeText(reportText());setActionState({status:'copied'});}catch(error){setActionState({status:'error',error:error&&error.message?error.message:String(error)});}}return h(React.Fragment,null,title(tx('诊断','Diagnostics'),tx('状态页不会修改你的模型/路由设置;连接测试和更新操作会明确由按钮触发。','The status page does not change model or routing settings; connection tests and updates are explicit actions.')),card(rows.map(function(row){return diagnosticValue(row[0],row[1]);})),card([h('div',{className:'vr-ia-actions',key:'actions'},local?h('button',{type:'button',className:'vr-btn',disabled:testState.status==='running',onClick:function(){void runTestConnection();}},tx('测试连接','Test connection')):null,h('button',{type:'button',className:'vr-btn',onClick:function(){invalidateCatalog();}},tx('重新检测模型','Re-detect models')),local?h('button',{type:'button',className:'vr-btn',onClick:function(){void openLogs();}},tx('打开日志文件夹','Open logs folder')):null,h('button',{type:'button',className:'vr-btn',onClick:function(){void copyReport();}},tx('复制诊断信息','Copy diagnostics'))),h('p',{className:'vr-hint',key:'doctor'},tx('需要完整 DSH 版本、安装、Profile、运行时路由和会话诊断时执行:dsh-vision-router doctor','For the full DSH version, installation, profile, runtime-route, and session report, run: dsh-vision-router doctor')),testState.status==='done'&&testState.result&&testState.result.ok!==true?h('p',{className:'vr-failed',key:'testerr'},String(testState.result.error||tx('连接测试失败','Connection test failed'))):null,actionState.status==='error'?h('p',{className:'vr-failed',key:'aerr'},String(actionState.error||'unknown')):null]),updatePanel());}
@@ -0,0 +1,35 @@
1
+ // Optional local-only subtitle recovery, entered only after weak Tesseract OCR.
2
+ // Derives one lower-region greyscale crop without storing the source image.
3
+ export async function prepareTesseractSubtitleRetry(bytes, {
4
+ loadSharp, signal, remainingMs = 0,
5
+ } = {}) {
6
+ if (signal?.aborted || remainingMs < 900 || typeof loadSharp !== 'function') return null
7
+ if (!(bytes instanceof Uint8Array) || bytes.byteLength > 24 * 1024 * 1024) return null
8
+ const sharp = await loadSharp()
9
+ if (signal?.aborted) return null
10
+ const image = sharp(bytes, { failOn: 'error', limitInputPixels: 16_000_000, animated: false })
11
+ const metadata = await image.metadata()
12
+ const width = metadata.width, height = metadata.height
13
+ if (!Number.isInteger(width) || !Number.isInteger(height) ||
14
+ width < 120 || height < 120 || width * height > 16_000_000) return null
15
+ const top = Math.floor(height * 0.55)
16
+ const crop = { left: 0, top, width, height: height - top }
17
+ const sample = await sharp(bytes, { failOn: 'error', limitInputPixels: 16_000_000, animated: false })
18
+ .extract(crop).resize(64, 32, { fit: 'fill' }).grayscale().raw().toBuffer()
19
+ if (signal?.aborted) return null
20
+ const mean = sample.reduce((sum, value) => sum + value, 0) / sample.length
21
+ // Invert only when the actual subtitle region is predominantly dark.
22
+ const inverted = mean < 108
23
+ const scale = Math.min(3.2, 1000 / width, 1500 / crop.height)
24
+ const outWidth = Math.max(1, Math.round(width * scale))
25
+ const outHeight = Math.max(1, Math.round(crop.height * scale))
26
+ let pipeline = sharp(bytes, { failOn: 'error', limitInputPixels: 16_000_000, animated: false })
27
+ .extract(crop).resize(outWidth, outHeight).grayscale().normalize()
28
+ if (inverted) pipeline = pipeline.negate()
29
+ const prepared = await pipeline.png().toBuffer()
30
+ if (signal?.aborted) return null
31
+ if (prepared.length > 16 * 1024 * 1024) return null
32
+ return {
33
+ bytes: prepared, variant: inverted ? 'subtitle-inverted' : 'subtitle-grayscale',
34
+ }
35
+ }