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.
- package/README.md +3 -3
- package/README.zh.md +18 -3
- package/THIRD_PARTY_NOTICES.md +60 -0
- package/assets/licenses/llvm-mingw-LICENSE.txt +279 -0
- package/assets/licenses/mingw-w64-runtime-COPYING.txt +265 -0
- package/assets/licenses/ncnn-LICENSE.txt +86 -0
- package/docs/architecture/dsh-support-window.md +25 -6
- package/docs/compatibility/dsh-0.2.2-rc3-audit.md +131 -0
- package/docs/releases/v3.2.2.md +25 -0
- package/docs/releases/v3.3.0.md +34 -0
- package/index.js +265 -159
- package/lib/client.js +4 -4
- package/lib/core-primitives.js +115 -56
- package/lib/dsh-support-window.js +6 -3
- package/lib/native-ocr-annotation-risk.js +33 -0
- package/lib/native-ocr-canary.js +156 -0
- package/lib/native-ocr-chemistry-review.js +37 -0
- package/lib/native-ocr-document.js +243 -0
- package/lib/native-ocr-installation.js +17 -0
- package/lib/native-ocr-integrity.js +93 -0
- package/lib/native-ocr-question-labels.js +167 -0
- package/lib/native-ocr-supervisor.js +268 -0
- package/lib/runtime-reliability.js +7 -0
- package/lib/session-query-readers.js +8 -2
- package/lib/session-vision-attachment-index.js +61 -14
- package/lib/settings-ia-client-prelude.js +1 -1
- package/lib/tesseract-image-preprocess.js +35 -0
- package/lib/trace-vtracer-process-runtime.js +133 -0
- package/lib/trace-vtracer-process.js +43 -0
- package/lib/vision-attachment-handle-runtime.js +11 -2
- package/lib/vision-backend-runtime-policy.js +5 -3
- package/lib/vision-image-input-reader.js +67 -0
- package/lib/vision-image-input-verdict.js +24 -3
- package/lib/vision-tool-runtime-boundary.js +7 -10
- package/lib/vision-tool-stage.js +58 -0
- package/package.json +5 -4
- package/scripts/install-native-ocr.mjs +144 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { fork } from 'node:child_process'
|
|
2
|
+
|
|
3
|
+
// Avoid unbounded Host-memory retention and process storms under hostile
|
|
4
|
+
// concurrent tool calls. Waiting consumes the caller's original deadline.
|
|
5
|
+
const MAX_ACTIVE_TRACE_PROCESSES = 3
|
|
6
|
+
const MAX_WAITING_TRACE_PROCESSES = 8
|
|
7
|
+
let active = 0
|
|
8
|
+
const waiting = []
|
|
9
|
+
|
|
10
|
+
function dispatchWaiting() {
|
|
11
|
+
while (active < MAX_ACTIVE_TRACE_PROCESSES && waiting.length > 0) {
|
|
12
|
+
const next = waiting.shift()
|
|
13
|
+
if (next.finished()) continue
|
|
14
|
+
active++
|
|
15
|
+
next.start()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Own one VTracer child process without sharing native/WASM address space
|
|
21
|
+
* with the Host. The injected childUrl is an internal test seam, never a
|
|
22
|
+
* user-provided path or executable.
|
|
23
|
+
*/
|
|
24
|
+
export function runIsolatedTraceProcess({
|
|
25
|
+
mode,
|
|
26
|
+
payload,
|
|
27
|
+
timeoutMs,
|
|
28
|
+
signal,
|
|
29
|
+
abortReason,
|
|
30
|
+
childUrl = new URL('./trace-vtracer-process.js', import.meta.url),
|
|
31
|
+
}) {
|
|
32
|
+
const aborted = () => abortReason?.() ?? new Error('vector trace aborted')
|
|
33
|
+
if (signal?.aborted) return Promise.reject(aborted())
|
|
34
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
35
|
+
return Promise.reject(new Error('vector trace timed out'))
|
|
36
|
+
}
|
|
37
|
+
if (waiting.length >= MAX_WAITING_TRACE_PROCESSES && active >= MAX_ACTIVE_TRACE_PROCESSES) {
|
|
38
|
+
return Promise.reject(new Error('vector trace process capacity exceeded'))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
let child
|
|
43
|
+
let timer
|
|
44
|
+
let finished = false
|
|
45
|
+
let started = false
|
|
46
|
+
let slotHeld = false
|
|
47
|
+
let pendingSvg
|
|
48
|
+
|
|
49
|
+
const release = () => {
|
|
50
|
+
if (!slotHeld) return
|
|
51
|
+
slotHeld = false
|
|
52
|
+
active--
|
|
53
|
+
dispatchWaiting()
|
|
54
|
+
}
|
|
55
|
+
const finish = (error, svg) => {
|
|
56
|
+
if (finished) return
|
|
57
|
+
finished = true
|
|
58
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
59
|
+
signal?.removeEventListener?.('abort', onAbort)
|
|
60
|
+
if (!started) {
|
|
61
|
+
const index = waiting.indexOf(entry)
|
|
62
|
+
if (index >= 0) waiting.splice(index, 1)
|
|
63
|
+
}
|
|
64
|
+
// Cancellation, timeouts, and native crashes stay inside the child.
|
|
65
|
+
if (error && child && !child.killed) {
|
|
66
|
+
try { child.kill() } catch { /* a closing child is already terminal */ }
|
|
67
|
+
}
|
|
68
|
+
if (error) reject(error)
|
|
69
|
+
else resolve(svg)
|
|
70
|
+
}
|
|
71
|
+
const onAbort = () => finish(aborted())
|
|
72
|
+
|
|
73
|
+
const entry = {
|
|
74
|
+
finished: () => finished,
|
|
75
|
+
start: () => {
|
|
76
|
+
if (finished) { release(); return }
|
|
77
|
+
started = true
|
|
78
|
+
slotHeld = true
|
|
79
|
+
try {
|
|
80
|
+
// No inherited Host CLI/test loader flags or NODE_OPTIONS preload.
|
|
81
|
+
const childEnv = { ...process.env, NODE_OPTIONS: '' }
|
|
82
|
+
if (process.versions?.electron) childEnv.ELECTRON_RUN_AS_NODE = '1'
|
|
83
|
+
child = fork(childUrl, [], {
|
|
84
|
+
execArgv: [],
|
|
85
|
+
serialization: 'advanced',
|
|
86
|
+
stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
|
|
87
|
+
windowsHide: true,
|
|
88
|
+
env: childEnv,
|
|
89
|
+
})
|
|
90
|
+
child.once('message', (message) => {
|
|
91
|
+
const svg = message?.svg
|
|
92
|
+
// The subprocess is a trust boundary, not an output validator.
|
|
93
|
+
// Native faults must never let bogus or oversized markup reach
|
|
94
|
+
// the Host artifact writer, even if IPC closes with status zero.
|
|
95
|
+
if (
|
|
96
|
+
message?.ok === true && typeof svg === 'string' &&
|
|
97
|
+
svg.length > 0 && svg.length <= 16 * 1024 * 1024 &&
|
|
98
|
+
/<svg[\s>]/.test(svg) && Buffer.byteLength(svg) <= 16 * 1024 * 1024
|
|
99
|
+
) {
|
|
100
|
+
pendingSvg = svg
|
|
101
|
+
} else {
|
|
102
|
+
finish(new Error('vector trace process returned an invalid result'))
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
child.once('error', () => finish(new Error('vector trace process startup failed')))
|
|
106
|
+
child.once('close', (code, termSignal) => {
|
|
107
|
+
// Do not resolve a healthy result before the IPC pipe is closed
|
|
108
|
+
// and this process' concurrency slot has actually been released.
|
|
109
|
+
release()
|
|
110
|
+
if (finished) return
|
|
111
|
+
if (code === 0 && typeof pendingSvg === 'string') finish(undefined, pendingSvg)
|
|
112
|
+
else finish(new Error('vector trace process exited before producing SVG (' +
|
|
113
|
+
(code === null ? String(termSignal ?? 'signal') : String(code)) + ')'))
|
|
114
|
+
})
|
|
115
|
+
child.send({ type: 'trace', mode, payload }, (error) => {
|
|
116
|
+
if (error) finish(new Error('vector trace process IPC failed'))
|
|
117
|
+
})
|
|
118
|
+
} catch {
|
|
119
|
+
finish(new Error('vector trace process launch failed'))
|
|
120
|
+
if (!child) release()
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
signal?.addEventListener?.('abort', onAbort, { once: true })
|
|
126
|
+
if (signal?.aborted) { finish(aborted()); return }
|
|
127
|
+
timer = setTimeout(() => finish(new Error(
|
|
128
|
+
'vector trace timed out — crop the image or reduce its complexity',
|
|
129
|
+
)), timeoutMs)
|
|
130
|
+
waiting.push(entry)
|
|
131
|
+
dispatchWaiting()
|
|
132
|
+
})
|
|
133
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Worker } from 'node:worker_threads'
|
|
2
|
+
|
|
3
|
+
// Trusted, one-shot VTracer child. All native/WASM work lives outside the Host
|
|
4
|
+
// process; a native access violation kills this child, not the Host.
|
|
5
|
+
let sent = false
|
|
6
|
+
function reply(message) {
|
|
7
|
+
if (sent) return
|
|
8
|
+
sent = true
|
|
9
|
+
if (typeof process.send !== 'function') {
|
|
10
|
+
process.exitCode = 1
|
|
11
|
+
return
|
|
12
|
+
}
|
|
13
|
+
process.send(message, (error) => {
|
|
14
|
+
if (error) process.exitCode = 1
|
|
15
|
+
process.disconnect?.()
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
process.once('message', (task) => {
|
|
20
|
+
if (task?.type !== 'trace' || !['grayscale', 'color'].includes(task.mode)) {
|
|
21
|
+
reply({ ok: false, error: 'invalid trace task' })
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
let pendingSvg
|
|
25
|
+
try {
|
|
26
|
+
const worker = new Worker(new URL('./trace-vtracer-worker.js', import.meta.url), {
|
|
27
|
+
execArgv: [],
|
|
28
|
+
workerData: { mode: task.mode, ...task.payload },
|
|
29
|
+
resourceLimits: { maxOldGenerationSizeMb: 192, maxYoungGenerationSizeMb: 32 },
|
|
30
|
+
})
|
|
31
|
+
worker.once('message', (message) => {
|
|
32
|
+
if (message?.ok === true && typeof message.svg === 'string') pendingSvg = message.svg
|
|
33
|
+
else reply({ ok: false, error: 'vector trace worker returned an error' })
|
|
34
|
+
})
|
|
35
|
+
worker.once('error', () => reply({ ok: false, error: 'vector trace worker failed' }))
|
|
36
|
+
worker.once('exit', (code) => {
|
|
37
|
+
if (code === 0 && typeof pendingSvg === 'string') reply({ ok: true, svg: pendingSvg })
|
|
38
|
+
else reply({ ok: false, error: 'vector trace worker exited before producing SVG' })
|
|
39
|
+
})
|
|
40
|
+
} catch {
|
|
41
|
+
reply({ ok: false, error: 'vector trace worker startup failed' })
|
|
42
|
+
}
|
|
43
|
+
})
|
|
@@ -64,9 +64,18 @@ function collectImageRefsFromMessages(messages, out) {
|
|
|
64
64
|
collectImageRefsFromBlocks(objectRecord(message)?.content, out);
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
/**
|
|
68
|
+
* The current Session's derived image blocks are already authorized as that
|
|
69
|
+
* Session's content. Reuse this same collector for projected short handles
|
|
70
|
+
* and target-only cold attachment recovery; neither may scan other Sessions.
|
|
71
|
+
*/
|
|
72
|
+
export function sessionImageAttachmentRefs(session) {
|
|
68
73
|
const refs = [];
|
|
69
|
-
collectImageRefsFromMessages(sessionMessages(
|
|
74
|
+
collectImageRefsFromMessages(sessionMessages(session), refs);
|
|
75
|
+
return refs;
|
|
76
|
+
}
|
|
77
|
+
function authorizedRefs(agent) {
|
|
78
|
+
const refs = sessionImageAttachmentRefs(agent?.session);
|
|
70
79
|
collectImageRefsFromMessages(agent?.inbox?.nextTurn, refs);
|
|
71
80
|
collectImageRefsFromMessages(agent?.inbox?.nextStep, refs);
|
|
72
81
|
const unique = new Map();
|
|
@@ -132,7 +132,7 @@ function contentHasImage(_core, messages) {
|
|
|
132
132
|
return (messages ?? []).some((message) => blocksHaveRetainedImage(message?.content))
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
async function toOpenAIMessages(ctx, messages) {
|
|
135
|
+
async function toOpenAIMessages(ctx, messages, signal) {
|
|
136
136
|
const attachments = ctx?.get?.('attachments')
|
|
137
137
|
if (!attachments || typeof attachments.readImage !== 'function') {
|
|
138
138
|
const error = new Error('vision preflight bridge unavailable: attachment service is not registered')
|
|
@@ -152,7 +152,9 @@ async function toOpenAIMessages(ctx, messages) {
|
|
|
152
152
|
converted.push({ type: 'text', text: offloadedImagePlaceholder(block) })
|
|
153
153
|
continue
|
|
154
154
|
}
|
|
155
|
-
|
|
155
|
+
if (signal?.aborted) throw signal.reason ?? new Error('vision delivery aborted')
|
|
156
|
+
const stored = await attachments.readImage(block.attachment, signal)
|
|
157
|
+
if (signal?.aborted) throw signal.reason ?? new Error('vision delivery aborted')
|
|
156
158
|
const mediaType = block.attachment.mediaType || stored?.mediaType || 'image/png'
|
|
157
159
|
const data = Buffer.from(stored.data).toString('base64')
|
|
158
160
|
converted.push({ type: 'image_url', image_url: { url: `data:${mediaType};base64,${data}` } })
|
|
@@ -235,7 +237,7 @@ function directStream(ctx, core, plan, call, logger, source, providerTransport)
|
|
|
235
237
|
if (typeof plan.transport?.apiKeyEnv === 'string' && plan.transport.apiKeyEnv !== '' && !apiKey) {
|
|
236
238
|
throw new Error('vision preflight bridge unavailable: channel credential could not be resolved')
|
|
237
239
|
}
|
|
238
|
-
const messages = await toOpenAIMessages(ctx, call.messages)
|
|
240
|
+
const messages = await toOpenAIMessages(ctx, call.messages, call.signal)
|
|
239
241
|
if (messages.length === 0) {
|
|
240
242
|
throw new Error('vision preflight bridge unavailable: no representable image request content')
|
|
241
243
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { combineSignals } from './vision-resilience.js'
|
|
2
|
+
import { currentVisionTurnBudgetSignal } from './turn-budget-context.js'
|
|
3
|
+
import { isAttachmentIdInput, sniffMediaType, mediaTypeOf } from './core-primitives.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* One authorized image-source reader for all Vision Router tools.
|
|
7
|
+
* Source identity belongs to the caller's Session, not to the global
|
|
8
|
+
* content-addressed attachment store. Neither the Host's private path nor
|
|
9
|
+
* untrusted model-facing short attachment aliases become filesystem inputs.
|
|
10
|
+
*/
|
|
11
|
+
export function createVisionImageInputReader(ctx, resolveAttachment, throwIfVisionTaskAborted) {
|
|
12
|
+
return async function readImageBytes(exec, imagePath, resolvedAttachmentRefs, attachmentInput = false) {
|
|
13
|
+
const input = String(imagePath ?? '')
|
|
14
|
+
const signal = combineSignals(currentVisionTurnBudgetSignal(), exec?.signal)
|
|
15
|
+
throwIfVisionTaskAborted(signal)
|
|
16
|
+
let bytes
|
|
17
|
+
let storedMediaType
|
|
18
|
+
if (attachmentInput || isAttachmentIdInput(input)) {
|
|
19
|
+
// Uploaded images reach the tool arguments as durable attachment ids
|
|
20
|
+
// ("sha256:..."); resolve them through the session's recorded upload
|
|
21
|
+
// index instead of treating the id as a filesystem path.
|
|
22
|
+
const attachments = ctx.get('attachments')
|
|
23
|
+
if (attachments === undefined) {
|
|
24
|
+
throw new Error('vision-router: the attachment service is not available in this deployment')
|
|
25
|
+
}
|
|
26
|
+
const session = exec && exec.agent && exec.agent.session
|
|
27
|
+
const canonicalInput = input.trim()
|
|
28
|
+
const ref = resolvedAttachmentRefs instanceof Map
|
|
29
|
+
? resolvedAttachmentRefs.get(canonicalInput)
|
|
30
|
+
: await resolveAttachment(session, canonicalInput, signal)
|
|
31
|
+
if (ref === undefined) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`vision-router: unknown attachment id "${input}" (it must come from an image uploaded in this conversation)`,
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
throwIfVisionTaskAborted(signal)
|
|
37
|
+
let stored
|
|
38
|
+
try {
|
|
39
|
+
stored = await attachments.readImage(ref, signal)
|
|
40
|
+
throwIfVisionTaskAborted(signal)
|
|
41
|
+
} catch (error) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`vision-router: failed to read attachment ${input} (${error && error.message ? error.message : String(error)})`,
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
bytes = stored.data
|
|
47
|
+
if (stored.ref && typeof stored.ref.mediaType === 'string') {
|
|
48
|
+
storedMediaType = stored.ref.mediaType
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
const fs = ctx.get('fs')
|
|
52
|
+
if (fs === undefined) throw new Error('vision-router: the fs service is not available')
|
|
53
|
+
const target = await fs.resolve(input)
|
|
54
|
+
throwIfVisionTaskAborted(signal)
|
|
55
|
+
bytes = await fs.readBytes(target, signal, 20 * 1024 * 1024)
|
|
56
|
+
throwIfVisionTaskAborted(signal)
|
|
57
|
+
}
|
|
58
|
+
// Attachments are stored as content-addressed files without an
|
|
59
|
+
// extension: sniff the format from the bytes, and fall back to the
|
|
60
|
+
// stored ref / extension only when sniffing cannot decide.
|
|
61
|
+
const mediaType = sniffMediaType(bytes) ?? storedMediaType ?? mediaTypeOf(input)
|
|
62
|
+
if (mediaType === undefined) {
|
|
63
|
+
throw new Error(`unsupported image format ${input} (png/jpeg/webp/gif only)`)
|
|
64
|
+
}
|
|
65
|
+
return { bytes, mediaType }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto'
|
|
2
|
-
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
2
|
+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
|
|
3
3
|
import path from 'node:path'
|
|
4
4
|
import process from 'node:process'
|
|
5
5
|
import { resolveDshHome } from './doctor.js'
|
|
@@ -74,8 +74,28 @@ async function save(file, records, fsOps) {
|
|
|
74
74
|
const temporary = `${file}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`
|
|
75
75
|
const verdicts = selectRetainedVerdicts(records.values())
|
|
76
76
|
await fsOps.mkdir(path.dirname(file), { recursive: true })
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
try {
|
|
78
|
+
await fsOps.writeFile(temporary, JSON.stringify({ version: CACHE_VERSION, verdicts }), { encoding: 'utf8', mode: 0o600 })
|
|
79
|
+
// Windows antivirus/indexing and concurrent writers can temporarily lock
|
|
80
|
+
// an existing destination during replace. Keep the unique temporary file
|
|
81
|
+
// and retry only transient filesystem errors; never unlink the destination.
|
|
82
|
+
const delays = [5, 10, 20, 40, 80, 160]
|
|
83
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
84
|
+
try {
|
|
85
|
+
await fsOps.rename(temporary, file)
|
|
86
|
+
break
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!['EACCES', 'EBUSY', 'EPERM'].includes(error?.code) || attempt >= delays.length) {
|
|
89
|
+
throw error
|
|
90
|
+
}
|
|
91
|
+
await new Promise((resolve) => setTimeout(resolve, delays[attempt]))
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
// On a permanent error, don't accumulate orphaned cache snapshots.
|
|
96
|
+
try { await fsOps.rm(temporary, { force: true }) } catch {}
|
|
97
|
+
throw error
|
|
98
|
+
}
|
|
79
99
|
}
|
|
80
100
|
|
|
81
101
|
export function createImageInputVerdictStore(options = {}) {
|
|
@@ -85,6 +105,7 @@ export function createImageInputVerdictStore(options = {}) {
|
|
|
85
105
|
mkdir: options.fsOps?.mkdir ?? mkdir,
|
|
86
106
|
writeFile: options.fsOps?.writeFile ?? writeFile,
|
|
87
107
|
rename: options.fsOps?.rename ?? rename,
|
|
108
|
+
rm: options.fsOps?.rm ?? rm,
|
|
88
109
|
}
|
|
89
110
|
const logger = options.logger
|
|
90
111
|
let records = new Map()
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto'
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
2
2
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
3
3
|
import { retainArtifactRun } from './artifact-retention.js'
|
|
4
4
|
import {
|
|
@@ -100,16 +100,13 @@ function sessionCwd(exec) {
|
|
|
100
100
|
function stableRuntimeConfigSignature(value) {
|
|
101
101
|
if (!value || typeof value !== 'object') return '{}'
|
|
102
102
|
try {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
httpProviders: value.httpProviders,
|
|
108
|
-
proxy: value.proxy,
|
|
109
|
-
proxyHosts: value.proxyHosts,
|
|
110
|
-
})
|
|
103
|
+
// Every live setting may change the tool's answer (model pairs, fallback
|
|
104
|
+
// order, downscale policy, prompt mode, etc.). Hash the full configuration
|
|
105
|
+
// rather than retaining its secrets or relying on a partial field whitelist.
|
|
106
|
+
return createHash('sha256').update(JSON.stringify(value)).digest('hex')
|
|
111
107
|
} catch {
|
|
112
|
-
|
|
108
|
+
// Cyclic/unserializable settings must never keep a stale cache revision.
|
|
109
|
+
return randomUUID()
|
|
113
110
|
}
|
|
114
111
|
}
|
|
115
112
|
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-invocation, privacy-safe stage timings. Never log paths, attachment IDs,
|
|
5
|
+
* prompt text, response contents, headers or credentials. A pending Host
|
|
6
|
+
* Promise gets an abort-stage log even if its own operation never settles.
|
|
7
|
+
*/
|
|
8
|
+
export function createVisionToolStageTrace(logger, toolName, signal) {
|
|
9
|
+
const traceId = randomUUID().slice(0, 12)
|
|
10
|
+
const report = (level, phase, status, elapsed) => {
|
|
11
|
+
try {
|
|
12
|
+
logger?.[level]?.(
|
|
13
|
+
'vision-router: tool=%s trace=%s stage=%s status=%s elapsedMs=%d',
|
|
14
|
+
toolName, traceId, phase, status, elapsed,
|
|
15
|
+
)
|
|
16
|
+
} catch {
|
|
17
|
+
// Diagnostics cannot change the outcome of an image/tool operation.
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return async (phase, operation) => {
|
|
22
|
+
const started = Date.now()
|
|
23
|
+
let completed = false
|
|
24
|
+
let abortReported = false
|
|
25
|
+
let rejectAborted
|
|
26
|
+
const canceled = signal && new Promise((_, reject) => { rejectAborted = reject })
|
|
27
|
+
// Pre-aborted signals can reject before Promise.race attaches a handler.
|
|
28
|
+
// Keep that intentional cancellation rejection observed in every race.
|
|
29
|
+
if (canceled) void canceled.catch(() => {})
|
|
30
|
+
const onAbort = () => {
|
|
31
|
+
if (completed || abortReported) return
|
|
32
|
+
abortReported = true
|
|
33
|
+
report('warn', phase, 'aborted', Date.now() - started)
|
|
34
|
+
rejectAborted?.(signal?.reason ?? new Error('vision stage aborted'))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
report('info', phase, 'begin', 0)
|
|
38
|
+
if (signal?.aborted) onAbort()
|
|
39
|
+
else signal?.addEventListener?.('abort', onAbort, { once: true })
|
|
40
|
+
try {
|
|
41
|
+
if (signal?.aborted) throw signal.reason ?? new Error('vision stage aborted')
|
|
42
|
+
// A legacy or broken Host Promise may ignore AbortSignal indefinitely.
|
|
43
|
+
// Race the stage *without* publishing its late result, while still
|
|
44
|
+
// observing the detached Promise and releasing our abort listener.
|
|
45
|
+
const running = Promise.resolve(operation())
|
|
46
|
+
const value = await (canceled ? Promise.race([running, canceled]) : running)
|
|
47
|
+
if (signal?.aborted) throw signal.reason ?? new Error('vision stage aborted')
|
|
48
|
+
report('info', phase, 'ok', Date.now() - started)
|
|
49
|
+
return value
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (!abortReported) report('warn', phase, 'error', Date.now() - started)
|
|
52
|
+
throw error
|
|
53
|
+
} finally {
|
|
54
|
+
completed = true
|
|
55
|
+
signal?.removeEventListener?.('abort', onAbort)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-vision-router",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "Eyes for text-only DeepSeek Harness agents: built-in free vision chain + pixel-level tools. Remote vision providers, including the default free fallback, receive image content unless strict local-only use is configured.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"THIRD_PARTY_NOTICES.md",
|
|
30
30
|
"README.md",
|
|
31
31
|
"README.zh.md",
|
|
32
|
+
"scripts/install-native-ocr.mjs",
|
|
32
33
|
"assets",
|
|
33
34
|
"cordis.patch.yml",
|
|
34
35
|
"docs",
|
|
@@ -85,8 +86,8 @@
|
|
|
85
86
|
"typescript": "6.0.3"
|
|
86
87
|
},
|
|
87
88
|
"scripts": {
|
|
88
|
-
"test": "node --test tests/dsh-alpha-pin.test.js tests/dsh-rc1-pin.test.js tests/dsh-rc2-pin.test.js tests/agent-prestep-model-routing.test.js tests/native-plugin-icons.test.js tests/optional-peer-import-contract.test.js tests/index-modularization.test.js tests/adapter-prepare-call-compat.test.js tests/core.test.js tests/issue-374-delegated-call-config.test.js tests/grounding-coordinate-frame.test.js tests/grounding-coordinate-runtime.test.js tests/desktop-e2e-watchdog.test.js tests/capability-advisory.test.js tests/vision-execution-policy.test.js tests/vision-backend-diagnostics.test.js tests/live-model-discovery.test.js tests/cache-atomic-write-concurrency.test.js tests/live-model-credential-resolution.test.js tests/vision-model-registry.test.js tests/live-model-client-stability.test.js tests/providers-persistence.test.js tests/provider-directory-fallback.test.js tests/model-catalog-policy.test.js tests/settings-ia-client-prelude.test.js tests/settings-ia-fj.test.js tests/settings-ia-acceptance-regression.test.js tests/settings-migration.test.js tests/settings-section-order.test.js tests/attachment-admission-policy.test.js tests/vision-resilience.test.js tests/degraded-local-evidence.test.js tests/vision-resilience-lifecycle.test.js tests/vision-breaker-readonly.test.js tests/alpha1-browser-lifecycle-integration.test.js tests/issue-367-remote-session-inject.test.js tests/vision-mode-toggle-injection.test.js tests/dev-staged-build.test.js tests/dev-web-reload.test.js tests/client.test.js tests/issue-307-regression.test.js tests/issue-284-vision-mode-toggle.test.js tests/issue-284-vision-mode-hardening.test.js tests/issue-284-attachment-id-hint.test.js tests/issue-284-model-visibility.test.js tests/issue-284-vision-selection-effort.test.js tests/client-presentation-boundary.test.js tests/clipboard-image-paste-compat.test.js tests/remote-settings-bridge.test.js tests/remote-settings-v2-contract.test.js tests/remote-settings-risk-confirmation.test.js tests/local-remote-settings-permission.test.js tests/artifact-path-security.test.js tests/http-compat.test.js tests/catalog-corrections.test.js tests/release-registry-identity.test.js tests/update-check.test.js tests/update-check-signal-lifecycle.test.js tests/self-update.test.js tests/doctor.test.js tests/doctor-cli.test.js tests/legacy-session-repair.test.js tests/profile-pnpm-diagnostics.test.js tests/doctor-v2.test.js tests/doctor-cli-v2.test.js tests/doctor-runtime.test.js tests/dsh-host-capabilities.test.js tests/doctor-host-capabilities.test.js tests/session-repair-v2.test.js tests/activation-shim-contract.test.js tests/bundle-defaults.test.js tests/required-ci-contract.test.js tests/ci-electron-download-retry.test.js tests/file-logger.test.js tests/official-deepseek-catalog.test.js tests/replay-delegation.test.js tests/twin-image-capability-fallback.test.js tests/twin-image-custom-wrapper.test.js tests/adversarial-hardening.test.js tests/vision-adversarial-hardening-v2.test.js tests/vision-capability-benchmark-visual-proof.test.js tests/wrapper-directory.test.js tests/logging-ui.test.js tests/manifest-dependencies.test.js tests/provider-transport-threading.test.js tests/architecture-2.3-gate.test.js tests/host-settings-seam.test.js tests/drift-canary-settings-seam.test.js tests/esm-cycle-audit.test.js tests/structured-bootstrap.test.js tests/structured-bootstrap-gate.test.js tests/vision-model-guidance-contract.test.js tests/local-ollama.test.js tests/local-only-vision.test.js tests/ollama-cold-start.test.js tests/ollama-cold-start-loaded.test.js tests/zero-regression-gate.test.js tests/runtime-e2e.test.js tests/local-vision-stabilizer.test.js tests/local-connection-probe.test.js tests/vision-diagnostics-routes.test.js tests/repetition-guard.test.js tests/guide-scroll-gate.test.js tests/settings-scroll-jank-regression.test.js tests/free-cloud-first.test.js tests/dsh-host-compat.test.js tests/native-image-coexistence.test.js tests/issue-276-session-image-ownership.test.js tests/issue-276-entry-policy-bridge.test.js tests/issue-276-policy-hardening.test.js tests/issue-289-native-nonintervention.test.js tests/issue-512-tool-restriction.test.js tests/issue-606-session-source-ownership.test.js tests/pi-ai-bridge-wire-compat.test.js tests/fetch-wrapper-lifecycle.test.js tests/fetch-wrapper-composition.test.js tests/provider-probe-proxy-scope.test.js tests/lifecycle-invariants.test.js tests/vision-tool-settings-generation.test.js tests/release-package-content-identity.test.js tests/release-state-machine.test.js tests/mixed-router.test.js tests/depth-tier.test.js tests/depth-quota-behavior.test.js tests/structured-flow-hardening.test.js tests/structured-flow-adversarial.test.js tests/vision-budget-activation.test.js tests/runtime-boundary-fixes.test.js tests/vision-stream-iterator-cancellation.test.js tests/security-resource-boundaries.test.js tests/security-property-fuzz.test.js tests/security-adversarial-fuzz-contract.test.js tests/runtime-i18n.test.js tests/long-ocr-empty-retry.test.js tests/tesseract-adaptive-ocr.test.js tests/tesseract-node24-boot.test.js tests/session-vision-state.test.js tests/session-vision-state-integration.test.js tests/session-vision-event-feed.test.js tests/session-vision-policy.test.js tests/session-vision-policy-domain.test.js tests/session-vision-delegation.test.js tests/session-vision-delegation-boundary.test.js tests/subagent-vision-inheritance-characterization.test.js tests/subagent-vision-runtime-integration.test.js tests/subagent-vision-cold-resume.test.js tests/image-resource-governor.test.js tests/resource-retention-lifecycle.test.js tests/pixel-diff-stream.test.js tests/large-image-resource-integration.test.js tests/structured-guard-idempotency.test.js tests/vision-routing-product.test.js tests/vision-runtime-performance.test.js tests/vision-routing-settings-prelude.test.js tests/v2-settings-ia-integration.test.js tests/vision-capability-router.test.js tests/vision-capability-reference.test.js tests/vision-capability-benchmark.test.js tests/vision-capability-grounding-proof-contract.test.js tests/vision-capability-probe.test.js tests/vision-capability-axis-freshness.test.js tests/vision-capability-shadow-health.test.js tests/vision-capability-benchmark-service.test.js tests/vision-capability-preflight-hardening.test.js tests/vision-capability-benchmark-contract.test.js tests/vision-capability-benchmark-client.test.js tests/vision-capability-benchmark-client-productization.test.js tests/vision-capability-adapter-route.test.js tests/vision-background-benchmark.test.js tests/vision-background-lifecycle.test.js tests/vision-background-benchmark-productization.test.js tests/vision-capability-benchmark-budget.test.js tests/vision-image-input-verdict.test.js tests/vision-capability-failure-classification.test.js tests/vision-capability-gate-client.test.js tests/v2-product-review-fixes.test.js tests/qa-top5-reliability.test.js tests/qa-screenshot-runtime.test.js tests/qa-runtime-identity.test.js tests/qa-turn-budget-cancellation.test.js tests/qa-cancellation-publication.test.js tests/qa-reliability-tail.test.js tests/qa-endpoint-route-alias.test.js tests/settings-native-card-semantics.test.js tests/settings-native-card-lazy-data.test.js tests/settings-invalid-staged-dirty.test.js tests/vision-toggle-root-hardening.test.js && pnpm test:regression-closure && node --test tests/vision-backend-runtime-policy.test.js tests/vision-router-adversarial-review-fixes.test.js tests/web-panel-delegation.test.js && node --test tests/vision-trace-vtracer.test.js tests/vision-trace-image-quality.test.js && pnpm license:check",
|
|
89
|
-
"test:core": "node --test tests/core.test.js tests/degraded-local-evidence.test.js tests/runtime-e2e.test.js tests/structured-bootstrap.test.js tests/structured-flow-hardening.test.js tests/structured-flow-adversarial.test.js tests/vision-execution-policy.test.js tests/vision-backend-runtime-policy.test.js",
|
|
89
|
+
"test": "node --test tests/dsh-alpha-pin.test.js tests/dsh-rc1-pin.test.js tests/dsh-rc2-pin.test.js tests/dsh-rc3-pin.test.js tests/agent-prestep-model-routing.test.js tests/native-plugin-icons.test.js tests/optional-peer-import-contract.test.js tests/index-modularization.test.js tests/adapter-prepare-call-compat.test.js tests/core.test.js tests/issue-374-delegated-call-config.test.js tests/grounding-coordinate-frame.test.js tests/grounding-coordinate-runtime.test.js tests/desktop-e2e-watchdog.test.js tests/capability-advisory.test.js tests/vision-execution-policy.test.js tests/vision-backend-diagnostics.test.js tests/live-model-discovery.test.js tests/cache-atomic-write-concurrency.test.js tests/live-model-credential-resolution.test.js tests/vision-model-registry.test.js tests/live-model-client-stability.test.js tests/providers-persistence.test.js tests/provider-directory-fallback.test.js tests/model-catalog-policy.test.js tests/settings-ia-client-prelude.test.js tests/settings-ia-fj.test.js tests/settings-ia-acceptance-regression.test.js tests/settings-migration.test.js tests/settings-section-order.test.js tests/attachment-admission-policy.test.js tests/vision-resilience.test.js tests/degraded-local-evidence.test.js tests/vision-resilience-lifecycle.test.js tests/vision-breaker-readonly.test.js tests/alpha1-browser-lifecycle-integration.test.js tests/issue-367-remote-session-inject.test.js tests/vision-mode-toggle-injection.test.js tests/dev-staged-build.test.js tests/dev-web-reload.test.js tests/client.test.js tests/issue-307-regression.test.js tests/issue-284-vision-mode-toggle.test.js tests/issue-284-vision-mode-hardening.test.js tests/issue-284-attachment-id-hint.test.js tests/issue-284-model-visibility.test.js tests/issue-284-vision-selection-effort.test.js tests/client-presentation-boundary.test.js tests/clipboard-image-paste-compat.test.js tests/remote-settings-bridge.test.js tests/remote-settings-v2-contract.test.js tests/remote-settings-risk-confirmation.test.js tests/local-remote-settings-permission.test.js tests/artifact-path-security.test.js tests/http-compat.test.js tests/catalog-corrections.test.js tests/release-registry-identity.test.js tests/update-check.test.js tests/update-check-signal-lifecycle.test.js tests/self-update.test.js tests/doctor.test.js tests/doctor-cli.test.js tests/legacy-session-repair.test.js tests/profile-pnpm-diagnostics.test.js tests/doctor-v2.test.js tests/doctor-cli-v2.test.js tests/doctor-runtime.test.js tests/dsh-host-capabilities.test.js tests/doctor-host-capabilities.test.js tests/session-repair-v2.test.js tests/activation-shim-contract.test.js tests/bundle-defaults.test.js tests/required-ci-contract.test.js tests/ci-electron-download-retry.test.js tests/file-logger.test.js tests/official-deepseek-catalog.test.js tests/replay-delegation.test.js tests/twin-image-capability-fallback.test.js tests/twin-image-custom-wrapper.test.js tests/adversarial-hardening.test.js tests/vision-adversarial-hardening-v2.test.js tests/vision-capability-benchmark-visual-proof.test.js tests/wrapper-directory.test.js tests/logging-ui.test.js tests/manifest-dependencies.test.js tests/provider-transport-threading.test.js tests/architecture-2.3-gate.test.js tests/host-settings-seam.test.js tests/drift-canary-settings-seam.test.js tests/esm-cycle-audit.test.js tests/structured-bootstrap.test.js tests/structured-bootstrap-gate.test.js tests/vision-model-guidance-contract.test.js tests/local-ollama.test.js tests/local-only-vision.test.js tests/ollama-cold-start.test.js tests/ollama-cold-start-loaded.test.js tests/zero-regression-gate.test.js tests/runtime-e2e.test.js tests/local-vision-stabilizer.test.js tests/local-connection-probe.test.js tests/vision-diagnostics-routes.test.js tests/repetition-guard.test.js tests/guide-scroll-gate.test.js tests/settings-scroll-jank-regression.test.js tests/free-cloud-first.test.js tests/dsh-host-compat.test.js tests/native-image-coexistence.test.js tests/issue-276-session-image-ownership.test.js tests/issue-276-entry-policy-bridge.test.js tests/issue-276-policy-hardening.test.js tests/issue-289-native-nonintervention.test.js tests/issue-512-tool-restriction.test.js tests/issue-606-session-source-ownership.test.js tests/pi-ai-bridge-wire-compat.test.js tests/fetch-wrapper-lifecycle.test.js tests/fetch-wrapper-composition.test.js tests/provider-probe-proxy-scope.test.js tests/lifecycle-invariants.test.js tests/vision-tool-settings-generation.test.js tests/release-package-content-identity.test.js tests/release-state-machine.test.js tests/mixed-router.test.js tests/depth-tier.test.js tests/depth-quota-behavior.test.js tests/structured-flow-hardening.test.js tests/structured-flow-adversarial.test.js tests/vision-budget-activation.test.js tests/runtime-boundary-fixes.test.js tests/vision-stream-iterator-cancellation.test.js tests/security-resource-boundaries.test.js tests/security-property-fuzz.test.js tests/security-adversarial-fuzz-contract.test.js tests/runtime-i18n.test.js tests/long-ocr-empty-retry.test.js tests/tesseract-adaptive-ocr.test.js tests/tesseract-node24-boot.test.js tests/session-vision-state.test.js tests/session-vision-state-integration.test.js tests/session-vision-event-feed.test.js tests/session-vision-policy.test.js tests/session-vision-policy-domain.test.js tests/session-vision-delegation.test.js tests/session-vision-delegation-boundary.test.js tests/subagent-vision-inheritance-characterization.test.js tests/subagent-vision-runtime-integration.test.js tests/subagent-vision-cold-resume.test.js tests/image-resource-governor.test.js tests/resource-retention-lifecycle.test.js tests/pixel-diff-stream.test.js tests/large-image-resource-integration.test.js tests/structured-guard-idempotency.test.js tests/vision-routing-product.test.js tests/vision-runtime-performance.test.js tests/vision-routing-settings-prelude.test.js tests/v2-settings-ia-integration.test.js tests/vision-capability-router.test.js tests/vision-capability-reference.test.js tests/vision-capability-benchmark.test.js tests/vision-capability-grounding-proof-contract.test.js tests/vision-capability-probe.test.js tests/vision-capability-axis-freshness.test.js tests/vision-capability-shadow-health.test.js tests/vision-capability-benchmark-service.test.js tests/vision-capability-preflight-hardening.test.js tests/vision-capability-benchmark-contract.test.js tests/vision-capability-benchmark-client.test.js tests/vision-capability-benchmark-client-productization.test.js tests/vision-capability-adapter-route.test.js tests/vision-background-benchmark.test.js tests/vision-background-lifecycle.test.js tests/vision-background-benchmark-productization.test.js tests/vision-capability-benchmark-budget.test.js tests/vision-image-input-verdict.test.js tests/vision-capability-failure-classification.test.js tests/vision-capability-gate-client.test.js tests/v2-product-review-fixes.test.js tests/qa-top5-reliability.test.js tests/qa-screenshot-runtime.test.js tests/qa-runtime-identity.test.js tests/qa-turn-budget-cancellation.test.js tests/qa-cancellation-publication.test.js tests/vision-tool-stage.test.js tests/vision-image-input-reader.test.js tests/qa-reliability-tail.test.js tests/qa-endpoint-route-alias.test.js tests/settings-native-card-semantics.test.js tests/settings-native-card-lazy-data.test.js tests/settings-invalid-staged-dirty.test.js tests/vision-toggle-root-hardening.test.js && pnpm test:regression-closure && node --test tests/vision-backend-runtime-policy.test.js tests/vision-router-adversarial-review-fixes.test.js tests/web-panel-delegation.test.js && node --test tests/vision-trace-vtracer.test.js tests/vision-trace-image-quality.test.js && pnpm license:check && node --test tests/native-ocr-annotation-risk.test.js tests/native-ocr-bundle.test.js tests/native-ocr-canary.test.js tests/native-ocr-chemistry-review.test.js tests/native-ocr-document.test.js tests/native-ocr-host-canary.test.js tests/native-ocr-installation.test.js tests/native-ocr-integrity.test.js tests/native-ocr-provenance.test.js tests/native-ocr-question-labels.test.js tests/native-ocr-supervisor.test.js tests/issue-836-ocr-regression.test.js",
|
|
90
|
+
"test:core": "node --test tests/vision-image-input-reader.test.js tests/vision-tool-stage.test.js tests/core.test.js tests/degraded-local-evidence.test.js tests/runtime-e2e.test.js tests/structured-bootstrap.test.js tests/structured-flow-hardening.test.js tests/structured-flow-adversarial.test.js tests/vision-execution-policy.test.js tests/vision-backend-runtime-policy.test.js",
|
|
90
91
|
"test:routing": "node --test tests/issue-374-delegated-call-config.test.js tests/twin-image-capability-fallback.test.js tests/twin-image-custom-wrapper.test.js tests/vision-routing-product.test.js tests/vision-capability-router.test.js tests/vision-capability-probe.test.js tests/vision-capability-shadow-health.test.js tests/vision-runtime-performance.test.js tests/vision-background-benchmark.test.js tests/vision-image-input-verdict.test.js",
|
|
91
92
|
"test:session": "node --test tests/session-vision-state.test.js tests/session-vision-state-integration.test.js tests/session-vision-policy.test.js tests/session-vision-delegation.test.js tests/session-vision-delegation-boundary.test.js tests/subagent-vision-inheritance-characterization.test.js tests/subagent-vision-runtime-integration.test.js tests/subagent-vision-cold-resume.test.js tests/session-vision-policy-domain.test.js tests/replay-delegation.test.js tests/legacy-session-repair.test.js tests/session-repair-v2.test.js tests/issue-276-session-image-ownership.test.js",
|
|
92
93
|
"test:resources": "node --test tests/artifact-path-security.test.js tests/image-resource-governor.test.js tests/resource-retention-lifecycle.test.js tests/pixel-diff-stream.test.js tests/large-image-resource-integration.test.js tests/qa-turn-budget-cancellation.test.js tests/qa-cancellation-publication.test.js",
|
|
@@ -119,7 +120,7 @@
|
|
|
119
120
|
"source:artifact:check": "node scripts/verify-source-artifact-map.mjs",
|
|
120
121
|
"test:regression-closure": "node --test tests/auto-wrap-model-removal.test.js tests/guard-stop-surface-shadow.test.js tests/release-package-content-identity.test.js tests/settings-card-race-safety.test.js tests/settings-guide-replay-safety.test.js tests/settings-ia-targeted-adversarial.test.js tests/v2-release-acceptance-regressions.test.js tests/vision-turn-budget-client-prelude.test.js tests/wrapper-scope-client-prelude.test.js",
|
|
121
122
|
"test:trace": "node --test tests/vision-trace-vtracer.test.js tests/vision-trace-image-quality.test.js",
|
|
122
|
-
"license:check": "node scripts/verify-runtime-licenses.mjs"
|
|
123
|
+
"license:check": "node scripts/verify-runtime-licenses.mjs && node scripts/verify-native-ocr-licenses.mjs"
|
|
123
124
|
},
|
|
124
125
|
"dsh": {
|
|
125
126
|
"client": {
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Installs owner-provided, already license-reviewed PP-OCRv6/ncnn artifacts.
|
|
3
|
+
// It does not download, build, or publish any native executable/model.
|
|
4
|
+
import { copyFile, chmod, mkdir, rename, rm, lstat, writeFile, mkdtemp, readFile } from 'node:fs/promises'
|
|
5
|
+
import { randomUUID } from 'node:crypto'
|
|
6
|
+
import { spawnSync } from 'node:child_process'
|
|
7
|
+
import { tmpdir } from 'node:os'
|
|
8
|
+
import { join, dirname, resolve } from 'node:path'
|
|
9
|
+
import { fileURLToPath } from 'node:url'
|
|
10
|
+
import { localNativeOcrInstallPaths } from '../lib/native-ocr-installation.js'
|
|
11
|
+
import { verifyExperimentalNativeOcrBinary } from '../lib/native-ocr-canary.js'
|
|
12
|
+
import { EXPERIMENTAL_OCR_MODEL_FILES, verifyNativeOcrAssets } from '../lib/native-ocr-integrity.js'
|
|
13
|
+
|
|
14
|
+
const args = process.argv.slice(2)
|
|
15
|
+
const target = process.platform + '-' + process.arch
|
|
16
|
+
const onWindows = target === 'win32-x64'
|
|
17
|
+
const workerName = onWindows ? 'worker.exe' : 'worker'
|
|
18
|
+
const additionalNotices = onWindows
|
|
19
|
+
? ['LLVM-LICENSE.txt', 'MINGW-RUNTIME-COPYING.txt']
|
|
20
|
+
: []
|
|
21
|
+
if (args.includes('--help')) {
|
|
22
|
+
console.log('Usage: node scripts/install-native-ocr.mjs --bundle ABSOLUTE_ARCHIVE.tar.gz\n node scripts/install-native-ocr.mjs --binary ABSOLUTE_WORKER --models ABSOLUTE_MODEL_DIR')
|
|
23
|
+
process.exit(0)
|
|
24
|
+
}
|
|
25
|
+
const readOption = key => {
|
|
26
|
+
const i = args.indexOf(key)
|
|
27
|
+
if (i < 0 || i + 1 >= args.length) throw Error('missing ' + key)
|
|
28
|
+
return resolve(args[i + 1])
|
|
29
|
+
}
|
|
30
|
+
async function installFromSources(binary, sourceModels) {
|
|
31
|
+
const dest = localNativeOcrInstallPaths()
|
|
32
|
+
if (!dest) throw Error('Only darwin-arm64 and win32-x64 are approved for this local installer')
|
|
33
|
+
await verifyExperimentalNativeOcrBinary(binary)
|
|
34
|
+
await verifyNativeOcrAssets(sourceModels)
|
|
35
|
+
|
|
36
|
+
const base = dirname(dest.root)
|
|
37
|
+
await mkdir(base, { recursive: true, mode: 0o700 })
|
|
38
|
+
const staged = join(base, '.ppocrv6-stage-' + randomUUID())
|
|
39
|
+
const previous = join(base, '.ppocrv6-old-' + randomUUID())
|
|
40
|
+
let movedPrevious = false
|
|
41
|
+
let promoted = false
|
|
42
|
+
try {
|
|
43
|
+
await mkdir(join(staged, 'models'), { recursive: true, mode: 0o700 })
|
|
44
|
+
await copyFile(binary, join(staged, workerName))
|
|
45
|
+
await chmod(join(staged, workerName), 0o700)
|
|
46
|
+
for (const name of Object.keys(EXPERIMENTAL_OCR_MODEL_FILES)) {
|
|
47
|
+
await copyFile(join(sourceModels, name), join(staged, 'models', name))
|
|
48
|
+
await chmod(join(staged, 'models', name), 0o600)
|
|
49
|
+
}
|
|
50
|
+
const assetBase = fileURLToPath(new URL('../assets/licenses/', import.meta.url))
|
|
51
|
+
await copyFile(join(assetBase, 'ncnn-LICENSE.txt'), join(staged, 'NCNN-LICENSE.txt'))
|
|
52
|
+
await copyFile(join(assetBase, 'apache-2.0.txt'), join(staged, 'APACHE-2.0.txt'))
|
|
53
|
+
for (const [name, destName] of [
|
|
54
|
+
['llvm-mingw-LICENSE.txt', 'LLVM-LICENSE.txt'],
|
|
55
|
+
['mingw-w64-runtime-COPYING.txt', 'MINGW-RUNTIME-COPYING.txt'],
|
|
56
|
+
].filter(() => onWindows)) {
|
|
57
|
+
await copyFile(join(assetBase, name), join(staged, destName))
|
|
58
|
+
}
|
|
59
|
+
await writeFile(join(staged, 'SOURCE-NOTICE.md'), [
|
|
60
|
+
'# Locally installed native OCR assets',
|
|
61
|
+
'',
|
|
62
|
+
'Detector: PaddlePaddle/PP-OCRv6_small_det_onnx, Apache-2.0.',
|
|
63
|
+
'Recognizer: PaddlePaddle/PP-OCRv6_small_rec_onnx, Apache-2.0.',
|
|
64
|
+
'Dictionary: PaddlePaddle/PaddleOCR ppocrv6_dict.txt, Apache-2.0.',
|
|
65
|
+
'Runtime: Tencent/ncnn @ d1f6ac67e9fc91390cc70f7168573fd2626f1d66, BSD-3-Clause',
|
|
66
|
+
'with its stated BSD-2-Clause and zlib components.',
|
|
67
|
+
'Models are owner-provided and checksum-gated, never bundled in the npm package.',
|
|
68
|
+
onWindows
|
|
69
|
+
? 'This Windows build statically links LLVM libc++/unwind, with Apache-2.0 WITH LLVM exceptions and MinGW-w64 runtime notices.'
|
|
70
|
+
: 'This Mac build links only OS-supplied libc++ and libSystem; no platform CRT is redistributed.',
|
|
71
|
+
'',
|
|
72
|
+
].join('\n'), { mode: 0o600 })
|
|
73
|
+
await verifyExperimentalNativeOcrBinary(join(staged, workerName))
|
|
74
|
+
await verifyNativeOcrAssets(join(staged, 'models'))
|
|
75
|
+
try {
|
|
76
|
+
const existing = await lstat(dest.root)
|
|
77
|
+
if (!existing.isDirectory() || existing.isSymbolicLink()) throw Error('unsafe existing OCR install directory')
|
|
78
|
+
await rename(dest.root, previous)
|
|
79
|
+
movedPrevious = true
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (err?.code !== 'ENOENT') throw err
|
|
82
|
+
}
|
|
83
|
+
await rename(staged, dest.root)
|
|
84
|
+
promoted = true
|
|
85
|
+
if (movedPrevious) await rm(previous, { recursive: true, force: true })
|
|
86
|
+
console.log('PP_OCRV6_LOCAL_INSTALL_OK (verified '+target+' assets; auto mode now prefers ncnn)')
|
|
87
|
+
} catch (err) {
|
|
88
|
+
if (!promoted && movedPrevious) await rename(previous, dest.root)
|
|
89
|
+
throw err
|
|
90
|
+
} finally {
|
|
91
|
+
if (!promoted) await rm(staged, { recursive: true, force: true })
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function install() {
|
|
95
|
+
if (args.length === 4 && args.includes('--binary') && args.includes('--models')) {
|
|
96
|
+
return installFromSources(readOption('--binary'), readOption('--models'))
|
|
97
|
+
}
|
|
98
|
+
if (args.length !== 2 || args[0] !== '--bundle') {
|
|
99
|
+
throw Error('required: --bundle ARCHIVE or --binary WORKER --models DIR')
|
|
100
|
+
}
|
|
101
|
+
if (!localNativeOcrInstallPaths()) throw Error('Only darwin-arm64 and win32-x64 are approved')
|
|
102
|
+
const archive = resolve(args[1])
|
|
103
|
+
const info = await lstat(archive)
|
|
104
|
+
if (!info.isFile() || info.isSymbolicLink() || info.size > 64 * 1024 * 1024) {
|
|
105
|
+
throw Error('unsafe native OCR bundle')
|
|
106
|
+
}
|
|
107
|
+
const prefix = 'ppocrv6-small/'
|
|
108
|
+
const members = [
|
|
109
|
+
workerName, 'MANIFEST.json', 'NCNN-LICENSE.txt', 'APACHE-2.0.txt',
|
|
110
|
+
...additionalNotices,
|
|
111
|
+
...Object.keys(EXPERIMENTAL_OCR_MODEL_FILES).map(x => 'models/' + x),
|
|
112
|
+
].map(x => prefix + x).sort()
|
|
113
|
+
const listed = spawnSync('tar', ['-tzf', archive], { encoding:'utf8', timeout:30_000, maxBuffer: 64 * 1024 })
|
|
114
|
+
if (listed.status !== 0 || JSON.stringify(listed.stdout.trim().split(/\r?\n/).sort()) !== JSON.stringify(members)) {
|
|
115
|
+
throw Error('native OCR bundle has unknown/missing entries')
|
|
116
|
+
}
|
|
117
|
+
// Read ONLY individually named entries into memory. Never call 'tar -x':
|
|
118
|
+
// traversal, links and metadata from an external archive cannot write files.
|
|
119
|
+
const tmp = await mkdtemp(join(tmpdir(), 'dvr-ocr-unpack-'))
|
|
120
|
+
try {
|
|
121
|
+
for (const entry of members) {
|
|
122
|
+
const suffix = entry.slice(prefix.length)
|
|
123
|
+
const destPath = join(tmp, suffix)
|
|
124
|
+
await mkdir(dirname(destPath), { recursive: true, mode: 0o700 })
|
|
125
|
+
const data = spawnSync('tar', ['-xOzf', archive, entry], {
|
|
126
|
+
timeout: 45_000, maxBuffer: 32 * 1024 * 1024,
|
|
127
|
+
})
|
|
128
|
+
if (data.status !== 0 || !Buffer.isBuffer(data.stdout)) {
|
|
129
|
+
throw Error('native OCR archive member unreadable: ' + suffix)
|
|
130
|
+
}
|
|
131
|
+
await writeFile(destPath, data.stdout, { mode: 0o600 })
|
|
132
|
+
}
|
|
133
|
+
const manifest = JSON.parse(await readFile(join(tmp,'MANIFEST.json'),'utf8'))
|
|
134
|
+
if (manifest.schemaVersion !== 1 ||
|
|
135
|
+
manifest.platform !== process.platform || manifest.arch !== process.arch) {
|
|
136
|
+
throw Error('unsupported native OCR bundle manifest')
|
|
137
|
+
}
|
|
138
|
+
return await installFromSources(join(tmp,workerName), join(tmp,'models'))
|
|
139
|
+
} finally { await rm(tmp, { recursive:true, force:true }) }
|
|
140
|
+
}
|
|
141
|
+
install().catch(err => {
|
|
142
|
+
console.error('PP_OCRV6_LOCAL_INSTALL_FAILED:', String(err?.code ?? err?.message ?? err))
|
|
143
|
+
process.exitCode = 1
|
|
144
|
+
})
|