@customize-agent/knowledge 4.0.25 → 4.0.26
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.
|
@@ -11,7 +11,11 @@ import { fileURLToPath } from 'node:url';
|
|
|
11
11
|
import { resolveAndImport, resolvePackage } from './module-resolver.js';
|
|
12
12
|
// ─── 路径工具 ───────────────────────────────────────────────────
|
|
13
13
|
const knowledgeDir = path.dirname(fileURLToPath(import.meta.url));
|
|
14
|
-
const OCR_NATIVE_NOISE_PATTERNS = [
|
|
14
|
+
const OCR_NATIVE_NOISE_PATTERNS = [
|
|
15
|
+
/Image too small to scale/u,
|
|
16
|
+
/Line cannot be recognized/u,
|
|
17
|
+
/empty image/iu
|
|
18
|
+
];
|
|
15
19
|
const OCR_NOISE_SUPPRESSION_KEY = Symbol.for('customize-agent.ocr-noise-suppression');
|
|
16
20
|
function ocrNoiseSuppressionState() {
|
|
17
21
|
const globalState = globalThis;
|
|
@@ -30,8 +34,7 @@ function isNativeOcrNoise(chunk) {
|
|
|
30
34
|
const text = Buffer.isBuffer(chunk) ? chunk.toString('utf8') : chunk instanceof Uint8Array ? Buffer.from(chunk).toString('utf8') : typeof chunk === 'string' ? chunk : '';
|
|
31
35
|
if (!text)
|
|
32
36
|
return false;
|
|
33
|
-
|
|
34
|
-
return lines.length > 0 && lines.every(line => OCR_NATIVE_NOISE_PATTERNS.some(pattern => pattern.test(line)));
|
|
37
|
+
return OCR_NATIVE_NOISE_PATTERNS.some(pattern => pattern.test(text));
|
|
35
38
|
}
|
|
36
39
|
async function suppressNativeOcrNoise(operation) {
|
|
37
40
|
const state = ocrNoiseSuppressionState();
|
|
@@ -64,12 +67,48 @@ async function suppressNativeOcrNoise(operation) {
|
|
|
64
67
|
}
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
|
-
//
|
|
70
|
+
// 临时屏蔽 Tesseract 底层 WASM 污染日志
|
|
71
|
+
function withSilencedConsole(fn) {
|
|
72
|
+
const originalLog = console.log;
|
|
73
|
+
const originalWarn = console.warn;
|
|
74
|
+
const originalError = console.error;
|
|
75
|
+
const filter = (orig) => (...args) => {
|
|
76
|
+
const msg = String(args[0] || '');
|
|
77
|
+
if (msg.includes('Line cannot be recognized') || msg.includes('Image too small to scale')) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
orig(...args);
|
|
81
|
+
};
|
|
82
|
+
console.log = filter(originalLog);
|
|
83
|
+
console.warn = filter(originalWarn);
|
|
84
|
+
console.error = filter(originalError);
|
|
85
|
+
try {
|
|
86
|
+
const result = fn();
|
|
87
|
+
if (result instanceof Promise) {
|
|
88
|
+
return result.finally(() => {
|
|
89
|
+
console.log = originalLog;
|
|
90
|
+
console.warn = originalWarn;
|
|
91
|
+
console.error = originalError;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
console.log = originalLog;
|
|
95
|
+
console.warn = originalWarn;
|
|
96
|
+
console.error = originalError;
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
console.log = originalLog;
|
|
101
|
+
console.warn = originalWarn;
|
|
102
|
+
console.error = originalError;
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
68
106
|
export class TesseractJsProvider {
|
|
69
107
|
id = 'tesseract.js';
|
|
70
108
|
_available = null;
|
|
71
109
|
worker = null;
|
|
72
110
|
workerPromise = null;
|
|
111
|
+
workerLock = Promise.resolve();
|
|
73
112
|
warnings = [];
|
|
74
113
|
get available() {
|
|
75
114
|
if (this._available !== null)
|
|
@@ -115,8 +154,19 @@ export class TesseractJsProvider {
|
|
|
115
154
|
return { text: '', confidence: 0, regions: [], warnings: [`image too small for OCR: ${width}x${height}`] };
|
|
116
155
|
}
|
|
117
156
|
try {
|
|
118
|
-
const worker = await this.getWorker();
|
|
119
|
-
|
|
157
|
+
const worker = await withSilencedConsole(() => this.getWorker());
|
|
158
|
+
let unlock;
|
|
159
|
+
const nextLock = new Promise(resolve => { unlock = resolve; });
|
|
160
|
+
const currentLock = this.workerLock;
|
|
161
|
+
this.workerLock = currentLock.then(() => nextLock).catch(() => nextLock);
|
|
162
|
+
await currentLock;
|
|
163
|
+
let result;
|
|
164
|
+
try {
|
|
165
|
+
result = await withSilencedConsole(() => suppressNativeOcrNoise(() => worker.recognize(pngPath)));
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
unlock();
|
|
169
|
+
}
|
|
120
170
|
const text = (result.data.text ?? '').trim();
|
|
121
171
|
const lines = (result.data.lines ?? []);
|
|
122
172
|
const regions = lines
|