digdig 0.0.1 → 0.0.3
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 +437 -2
- package/digdig.config.example.json +14 -0
- package/dist/claims/deterministic-checks.d.ts +48 -0
- package/dist/claims/deterministic-checks.d.ts.map +1 -0
- package/dist/claims/deterministic-checks.js +162 -0
- package/dist/claims/deterministic-checks.js.map +1 -0
- package/dist/claims/policy.d.ts +25 -0
- package/dist/claims/policy.d.ts.map +1 -0
- package/dist/claims/policy.js +168 -0
- package/dist/claims/policy.js.map +1 -0
- package/dist/claims/store.d.ts +37 -0
- package/dist/claims/store.d.ts.map +1 -0
- package/dist/claims/store.js +152 -0
- package/dist/claims/store.js.map +1 -0
- package/dist/claims/types.d.ts +98 -0
- package/dist/claims/types.d.ts.map +1 -0
- package/dist/claims/types.js +9 -0
- package/dist/claims/types.js.map +1 -0
- package/dist/commands/claim.d.ts +13 -0
- package/dist/commands/claim.d.ts.map +1 -0
- package/dist/commands/claim.js +131 -0
- package/dist/commands/claim.js.map +1 -0
- package/dist/commands/coverage.d.ts +7 -0
- package/dist/commands/coverage.d.ts.map +1 -0
- package/dist/commands/coverage.js +78 -0
- package/dist/commands/coverage.js.map +1 -0
- package/dist/commands/doc-index.d.ts +12 -0
- package/dist/commands/doc-index.d.ts.map +1 -0
- package/dist/commands/doc-index.js +49 -0
- package/dist/commands/doc-index.js.map +1 -0
- package/dist/commands/doc-search.d.ts +16 -0
- package/dist/commands/doc-search.d.ts.map +1 -0
- package/dist/commands/doc-search.js +41 -0
- package/dist/commands/doc-search.js.map +1 -0
- package/dist/commands/evidence.d.ts +20 -0
- package/dist/commands/evidence.d.ts.map +1 -0
- package/dist/commands/evidence.js +215 -0
- package/dist/commands/evidence.js.map +1 -0
- package/dist/commands/extract.d.ts +10 -0
- package/dist/commands/extract.d.ts.map +1 -0
- package/dist/commands/extract.js +77 -0
- package/dist/commands/extract.js.map +1 -0
- package/dist/commands/graph-add.d.ts +7 -0
- package/dist/commands/graph-add.d.ts.map +1 -0
- package/dist/commands/graph-add.js +93 -0
- package/dist/commands/graph-add.js.map +1 -0
- package/dist/commands/graph-drill.d.ts +11 -0
- package/dist/commands/graph-drill.d.ts.map +1 -0
- package/dist/commands/graph-drill.js +26 -0
- package/dist/commands/graph-drill.js.map +1 -0
- package/dist/commands/graph-explore.d.ts +10 -0
- package/dist/commands/graph-explore.d.ts.map +1 -0
- package/dist/commands/graph-explore.js +32 -0
- package/dist/commands/graph-explore.js.map +1 -0
- package/dist/commands/graph-obfuscate.d.ts +17 -0
- package/dist/commands/graph-obfuscate.d.ts.map +1 -0
- package/dist/commands/graph-obfuscate.js +110 -0
- package/dist/commands/graph-obfuscate.js.map +1 -0
- package/dist/commands/graph-search.d.ts +11 -0
- package/dist/commands/graph-search.d.ts.map +1 -0
- package/dist/commands/graph-search.js +19 -0
- package/dist/commands/graph-search.js.map +1 -0
- package/dist/commands/graph-traverse.d.ts +10 -0
- package/dist/commands/graph-traverse.d.ts.map +1 -0
- package/dist/commands/graph-traverse.js +39 -0
- package/dist/commands/graph-traverse.js.map +1 -0
- package/dist/commands/json-input.d.ts +18 -0
- package/dist/commands/json-input.d.ts.map +1 -0
- package/dist/commands/json-input.js +50 -0
- package/dist/commands/json-input.js.map +1 -0
- package/dist/commands/query-graph.d.ts +13 -0
- package/dist/commands/query-graph.d.ts.map +1 -0
- package/dist/commands/query-graph.js +219 -0
- package/dist/commands/query-graph.js.map +1 -0
- package/dist/commands/results-add.d.ts +10 -0
- package/dist/commands/results-add.d.ts.map +1 -0
- package/dist/commands/results-add.js +80 -0
- package/dist/commands/results-add.js.map +1 -0
- package/dist/commands/review.d.ts +12 -0
- package/dist/commands/review.d.ts.map +1 -0
- package/dist/commands/review.js +119 -0
- package/dist/commands/review.js.map +1 -0
- package/dist/commands/session.d.ts +28 -0
- package/dist/commands/session.d.ts.map +1 -0
- package/dist/commands/session.js +149 -0
- package/dist/commands/session.js.map +1 -0
- package/dist/commands/verdict.d.ts +9 -0
- package/dist/commands/verdict.d.ts.map +1 -0
- package/dist/commands/verdict.js +40 -0
- package/dist/commands/verdict.js.map +1 -0
- package/dist/config.d.ts +28 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +148 -0
- package/dist/config.js.map +1 -0
- package/dist/deep/co-occurrence.d.ts +50 -0
- package/dist/deep/co-occurrence.d.ts.map +1 -0
- package/dist/deep/co-occurrence.js +95 -0
- package/dist/deep/co-occurrence.js.map +1 -0
- package/dist/deep/coverage.d.ts +81 -0
- package/dist/deep/coverage.d.ts.map +1 -0
- package/dist/deep/coverage.js +183 -0
- package/dist/deep/coverage.js.map +1 -0
- package/dist/deep/dedupe.d.ts +27 -0
- package/dist/deep/dedupe.d.ts.map +1 -0
- package/dist/deep/dedupe.js +115 -0
- package/dist/deep/dedupe.js.map +1 -0
- package/dist/deep/degree-utils.d.ts +39 -0
- package/dist/deep/degree-utils.d.ts.map +1 -0
- package/dist/deep/degree-utils.js +42 -0
- package/dist/deep/degree-utils.js.map +1 -0
- package/dist/deep/extractor.d.ts +42 -0
- package/dist/deep/extractor.d.ts.map +1 -0
- package/dist/deep/extractor.js +392 -0
- package/dist/deep/extractor.js.map +1 -0
- package/dist/deep/fuse.d.ts +63 -0
- package/dist/deep/fuse.d.ts.map +1 -0
- package/dist/deep/fuse.js +108 -0
- package/dist/deep/fuse.js.map +1 -0
- package/dist/deep/graph-explore.d.ts +115 -0
- package/dist/deep/graph-explore.d.ts.map +1 -0
- package/dist/deep/graph-explore.js +588 -0
- package/dist/deep/graph-explore.js.map +1 -0
- package/dist/deep/graph-obfuscate.d.ts +51 -0
- package/dist/deep/graph-obfuscate.d.ts.map +1 -0
- package/dist/deep/graph-obfuscate.js +226 -0
- package/dist/deep/graph-obfuscate.js.map +1 -0
- package/dist/deep/graph-preprocess.d.ts +48 -0
- package/dist/deep/graph-preprocess.d.ts.map +1 -0
- package/dist/deep/graph-preprocess.js +40 -0
- package/dist/deep/graph-preprocess.js.map +1 -0
- package/dist/deep/graph-sampling.d.ts +51 -0
- package/dist/deep/graph-sampling.d.ts.map +1 -0
- package/dist/deep/graph-sampling.js +410 -0
- package/dist/deep/graph-sampling.js.map +1 -0
- package/dist/deep/graph.d.ts +87 -0
- package/dist/deep/graph.d.ts.map +1 -0
- package/dist/deep/graph.js +200 -0
- package/dist/deep/graph.js.map +1 -0
- package/dist/deep/index.d.ts +10 -0
- package/dist/deep/index.d.ts.map +1 -0
- package/dist/deep/index.js +10 -0
- package/dist/deep/index.js.map +1 -0
- package/dist/deep/iteration-data.d.ts +34 -0
- package/dist/deep/iteration-data.d.ts.map +1 -0
- package/dist/deep/iteration-data.js +45 -0
- package/dist/deep/iteration-data.js.map +1 -0
- package/dist/deep/local-doc/indexer.d.ts +26 -0
- package/dist/deep/local-doc/indexer.d.ts.map +1 -0
- package/dist/deep/local-doc/indexer.js +420 -0
- package/dist/deep/local-doc/indexer.js.map +1 -0
- package/dist/deep/local-doc/scanner.d.ts +10 -0
- package/dist/deep/local-doc/scanner.d.ts.map +1 -0
- package/dist/deep/local-doc/scanner.js +165 -0
- package/dist/deep/local-doc/scanner.js.map +1 -0
- package/dist/deep/local-doc/searcher.d.ts +38 -0
- package/dist/deep/local-doc/searcher.d.ts.map +1 -0
- package/dist/deep/local-doc/searcher.js +211 -0
- package/dist/deep/local-doc/searcher.js.map +1 -0
- package/dist/deep/local-doc/types.d.ts +61 -0
- package/dist/deep/local-doc/types.d.ts.map +1 -0
- package/dist/deep/local-doc/types.js +18 -0
- package/dist/deep/local-doc/types.js.map +1 -0
- package/dist/deep/path-extraction.d.ts +53 -0
- package/dist/deep/path-extraction.d.ts.map +1 -0
- package/dist/deep/path-extraction.js +229 -0
- package/dist/deep/path-extraction.js.map +1 -0
- package/dist/deep/pending-facts.d.ts +64 -0
- package/dist/deep/pending-facts.d.ts.map +1 -0
- package/dist/deep/pending-facts.js +74 -0
- package/dist/deep/pending-facts.js.map +1 -0
- package/dist/deep/query-redundancy.d.ts +52 -0
- package/dist/deep/query-redundancy.d.ts.map +1 -0
- package/dist/deep/query-redundancy.js +122 -0
- package/dist/deep/query-redundancy.js.map +1 -0
- package/dist/deep/query-suggest.d.ts +33 -0
- package/dist/deep/query-suggest.d.ts.map +1 -0
- package/dist/deep/query-suggest.js +90 -0
- package/dist/deep/query-suggest.js.map +1 -0
- package/dist/deep/rrf.d.ts +13 -0
- package/dist/deep/rrf.d.ts.map +1 -0
- package/dist/deep/rrf.js +23 -0
- package/dist/deep/rrf.js.map +1 -0
- package/dist/deep/search-strategy.d.ts +37 -0
- package/dist/deep/search-strategy.d.ts.map +1 -0
- package/dist/deep/search-strategy.js +106 -0
- package/dist/deep/search-strategy.js.map +1 -0
- package/dist/deep/session.d.ts +182 -0
- package/dist/deep/session.d.ts.map +1 -0
- package/dist/deep/session.js +663 -0
- package/dist/deep/session.js.map +1 -0
- package/dist/deep/simhash.d.ts +17 -0
- package/dist/deep/simhash.d.ts.map +1 -0
- package/dist/deep/simhash.js +72 -0
- package/dist/deep/simhash.js.map +1 -0
- package/dist/deep/tfidf.d.ts +38 -0
- package/dist/deep/tfidf.d.ts.map +1 -0
- package/dist/deep/tfidf.js +103 -0
- package/dist/deep/tfidf.js.map +1 -0
- package/dist/deepseek-service.d.ts +22 -0
- package/dist/deepseek-service.d.ts.map +1 -0
- package/dist/deepseek-service.js +84 -0
- package/dist/deepseek-service.js.map +1 -0
- package/dist/entry.d.ts +14 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +29 -0
- package/dist/entry.js.map +1 -0
- package/dist/errors.d.ts +76 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +117 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +16 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +143 -0
- package/dist/init.js.map +1 -0
- package/dist/legacy.d.ts +60 -0
- package/dist/legacy.d.ts.map +1 -0
- package/dist/legacy.js +119 -0
- package/dist/legacy.js.map +1 -0
- package/dist/ollama-service.d.ts +22 -0
- package/dist/ollama-service.d.ts.map +1 -0
- package/dist/ollama-service.js +71 -0
- package/dist/ollama-service.js.map +1 -0
- package/dist/protocol.d.ts +23 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +26 -0
- package/dist/protocol.js.map +1 -0
- package/dist/runCli.d.ts +8 -0
- package/dist/runCli.d.ts.map +1 -0
- package/dist/runCli.js +1356 -0
- package/dist/runCli.js.map +1 -0
- package/dist/service.d.ts +73 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +145 -0
- package/dist/service.js.map +1 -0
- package/dist/sxng.d.ts +11 -0
- package/dist/sxng.d.ts.map +1 -0
- package/dist/sxng.js +14 -0
- package/dist/sxng.js.map +1 -0
- package/package.json +56 -12
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content extraction: Defuddle (linkedom) → optional Obscura fallback, or explicit Jina Reader
|
|
3
|
+
*/
|
|
4
|
+
import { parseHTML } from 'linkedom';
|
|
5
|
+
import { Defuddle } from 'defuddle/node';
|
|
6
|
+
import { execFile } from 'child_process';
|
|
7
|
+
import { promisify } from 'util';
|
|
8
|
+
import { homedir, arch, platform } from 'os';
|
|
9
|
+
import { join } from 'path';
|
|
10
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'fs';
|
|
11
|
+
import { mkdir, chmod, writeFile, rm } from 'fs/promises';
|
|
12
|
+
import { resolveStateRoot } from '../legacy.js';
|
|
13
|
+
const execFileAsync = promisify(execFile);
|
|
14
|
+
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
15
|
+
const DEFAULT_CONCURRENCY = 3;
|
|
16
|
+
const MAX_RESPONSE_BYTES = 5 * 1024 * 1024;
|
|
17
|
+
const MIN_CONTENT_LENGTH = 100;
|
|
18
|
+
const OBSCURA_THRESHOLD = 50;
|
|
19
|
+
const OBSCURA_SEARCH_PATHS = [
|
|
20
|
+
'obscura',
|
|
21
|
+
join(homedir(), '.local/bin/obscura'),
|
|
22
|
+
'/usr/local/bin/obscura',
|
|
23
|
+
];
|
|
24
|
+
const OBSCURA_INSTALL_DIR = join(homedir(), '.local', 'bin');
|
|
25
|
+
const OBSCURA_INSTALL_PATH = join(OBSCURA_INSTALL_DIR, 'obscura');
|
|
26
|
+
const OBSCURA_RELEASE_DOWNLOAD_BASE = 'https://github.com/h4ckf0r0day/obscura/releases/latest/download';
|
|
27
|
+
const JINA_READER_BASE = 'https://r.jina.ai';
|
|
28
|
+
const JINA_RPM = 20; // ponytail: free tier rate limit, 1 request per 3s average
|
|
29
|
+
const JINA_WINDOW_MS = 60_000;
|
|
30
|
+
let _obscuraAvailable = null;
|
|
31
|
+
let _obscuraInstallAttempted = false;
|
|
32
|
+
function getObscuraTarballName() {
|
|
33
|
+
const p = platform();
|
|
34
|
+
const a = arch();
|
|
35
|
+
if (p === 'linux' && a === 'x64')
|
|
36
|
+
return 'obscura-x86_64-linux.tar.gz';
|
|
37
|
+
if (p === 'linux' && a === 'arm64')
|
|
38
|
+
return 'obscura-aarch64-linux.tar.gz';
|
|
39
|
+
if (p === 'darwin' && a === 'x64')
|
|
40
|
+
return 'obscura-x86_64-macos.tar.gz';
|
|
41
|
+
if (p === 'darwin' && a === 'arm64')
|
|
42
|
+
return 'obscura-aarch64-macos.tar.gz';
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
export function obscuraDownloadUrl(tarball) {
|
|
46
|
+
return `${OBSCURA_RELEASE_DOWNLOAD_BASE}/${tarball}`;
|
|
47
|
+
}
|
|
48
|
+
async function installObscura() {
|
|
49
|
+
if (_obscuraInstallAttempted)
|
|
50
|
+
return null;
|
|
51
|
+
_obscuraInstallAttempted = true;
|
|
52
|
+
const tarball = getObscuraTarballName();
|
|
53
|
+
if (!tarball)
|
|
54
|
+
return null;
|
|
55
|
+
try {
|
|
56
|
+
const response = await fetch(obscuraDownloadUrl(tarball), { signal: AbortSignal.timeout(60_000) });
|
|
57
|
+
if (!response.ok)
|
|
58
|
+
return null;
|
|
59
|
+
const archive = Buffer.from(await response.arrayBuffer());
|
|
60
|
+
await mkdir(OBSCURA_INSTALL_DIR, { recursive: true });
|
|
61
|
+
const tmpTarball = join(OBSCURA_INSTALL_DIR, `${tarball}.downloading`);
|
|
62
|
+
await writeFile(tmpTarball, archive);
|
|
63
|
+
await execFileAsync('tar', ['-xzf', tmpTarball, '-C', OBSCURA_INSTALL_DIR], { timeout: 15_000 });
|
|
64
|
+
await rm(tmpTarball, { force: true });
|
|
65
|
+
await chmod(OBSCURA_INSTALL_PATH, 0o755);
|
|
66
|
+
// ponytail: chmod worker best-effort, may not exist in future releases
|
|
67
|
+
await chmod(join(OBSCURA_INSTALL_DIR, 'obscura-worker'), 0o755).catch(() => { });
|
|
68
|
+
await execFileAsync(OBSCURA_INSTALL_PATH, ['--version'], { timeout: 5_000 });
|
|
69
|
+
_obscuraAvailable = true;
|
|
70
|
+
return OBSCURA_INSTALL_PATH;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// Clean up partial artifacts
|
|
74
|
+
try {
|
|
75
|
+
await rm(OBSCURA_INSTALL_PATH, { force: true });
|
|
76
|
+
const tmpTarball = join(OBSCURA_INSTALL_DIR, `${tarball}.downloading`);
|
|
77
|
+
await rm(tmpTarball, { force: true });
|
|
78
|
+
}
|
|
79
|
+
catch { /* best-effort */ }
|
|
80
|
+
_obscuraAvailable = false;
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function findObscura(path) {
|
|
85
|
+
const candidates = path ? [path] : OBSCURA_SEARCH_PATHS;
|
|
86
|
+
for (const candidate of candidates) {
|
|
87
|
+
try {
|
|
88
|
+
await execFileAsync(candidate, ['--version'], { timeout: 5_000 });
|
|
89
|
+
return candidate;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Auto-install if no explicit path was given and not already attempted
|
|
96
|
+
if (!path && !_obscuraInstallAttempted) {
|
|
97
|
+
const installed = await installObscura();
|
|
98
|
+
if (installed)
|
|
99
|
+
return installed;
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
async function isObscuraAvailable(path) {
|
|
104
|
+
if (_obscuraAvailable !== null && !path)
|
|
105
|
+
return _obscuraAvailable;
|
|
106
|
+
const found = await findObscura(path);
|
|
107
|
+
const available = found !== null;
|
|
108
|
+
if (!path)
|
|
109
|
+
_obscuraAvailable = available;
|
|
110
|
+
return available;
|
|
111
|
+
}
|
|
112
|
+
async function defuddleExtract(html, url) {
|
|
113
|
+
try {
|
|
114
|
+
const { document } = parseHTML(html);
|
|
115
|
+
const result = await Defuddle(document, url, {
|
|
116
|
+
markdown: true,
|
|
117
|
+
useAsync: false,
|
|
118
|
+
});
|
|
119
|
+
const content = (result.content || '').trim();
|
|
120
|
+
if (!content) {
|
|
121
|
+
return {
|
|
122
|
+
title: result.title || '',
|
|
123
|
+
content: '',
|
|
124
|
+
excerpt: result.description || '',
|
|
125
|
+
url,
|
|
126
|
+
byline: result.author || undefined,
|
|
127
|
+
siteName: result.site || undefined,
|
|
128
|
+
length: 0,
|
|
129
|
+
extractedAt: Date.now(),
|
|
130
|
+
method: 'defuddle',
|
|
131
|
+
error: 'Defuddle could not extract content',
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
title: result.title || '',
|
|
136
|
+
content,
|
|
137
|
+
excerpt: result.description || '',
|
|
138
|
+
url,
|
|
139
|
+
byline: result.author || undefined,
|
|
140
|
+
siteName: result.site || undefined,
|
|
141
|
+
length: result.wordCount || content.length,
|
|
142
|
+
extractedAt: Date.now(),
|
|
143
|
+
method: 'defuddle',
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
return {
|
|
148
|
+
title: '',
|
|
149
|
+
content: '',
|
|
150
|
+
excerpt: '',
|
|
151
|
+
url,
|
|
152
|
+
length: 0,
|
|
153
|
+
extractedAt: Date.now(),
|
|
154
|
+
method: 'defuddle',
|
|
155
|
+
error: error instanceof Error ? error.message : 'Defuddle parsing failed',
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async function obscuraExtract(url, options) {
|
|
160
|
+
const obscuraBin = await findObscura(options.obscuraPath);
|
|
161
|
+
if (!obscuraBin)
|
|
162
|
+
return null;
|
|
163
|
+
try {
|
|
164
|
+
const dumpFormat = options.dumpFormat ?? 'html';
|
|
165
|
+
const timeoutSec = Math.ceil(options.timeoutMs / 1000);
|
|
166
|
+
const { stdout } = await execFileAsync(obscuraBin, ['fetch', url, '--dump', dumpFormat, '--timeout', String(timeoutSec)], { timeout: options.timeoutMs + 5_000, maxBuffer: 10 * 1024 * 1024 });
|
|
167
|
+
if (!stdout || !stdout.trim())
|
|
168
|
+
return null;
|
|
169
|
+
if (dumpFormat === 'markdown') {
|
|
170
|
+
return {
|
|
171
|
+
title: '',
|
|
172
|
+
content: stdout.trim(),
|
|
173
|
+
excerpt: '',
|
|
174
|
+
url,
|
|
175
|
+
length: stdout.trim().length,
|
|
176
|
+
extractedAt: Date.now(),
|
|
177
|
+
method: 'obscura',
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
const html = stdout.trim();
|
|
181
|
+
const result = await defuddleExtract(html, url);
|
|
182
|
+
result.method = 'obscura';
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// --- Jina Reader (r.jina.ai) with 20rpm rate limiting ---
|
|
190
|
+
const JINA_LOCK_STALE_MS = 30_000;
|
|
191
|
+
function jinaAcquireSlot() {
|
|
192
|
+
// Same root as sessions on purpose: a second rate-limit slot under the other
|
|
193
|
+
// name would double the 20 RPM budget instead of sharing it.
|
|
194
|
+
const dir = resolveStateRoot();
|
|
195
|
+
const file = join(dir, 'jina-rate-limit.json');
|
|
196
|
+
const lock = join(dir, '.jina-rate-limit.lock');
|
|
197
|
+
mkdirSync(dir, { recursive: true });
|
|
198
|
+
let acquired = false;
|
|
199
|
+
try {
|
|
200
|
+
try {
|
|
201
|
+
mkdirSync(lock);
|
|
202
|
+
acquired = true;
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
try {
|
|
206
|
+
if (Date.now() - statSync(lock).mtimeMs > JINA_LOCK_STALE_MS)
|
|
207
|
+
rmSync(lock, { recursive: true, force: true });
|
|
208
|
+
else
|
|
209
|
+
throw new Error('busy');
|
|
210
|
+
mkdirSync(lock);
|
|
211
|
+
acquired = true;
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
const retryAfterMs = 1_000;
|
|
215
|
+
return { ok: false, retryAfterMs, retryAt: Date.now() + retryAfterMs };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const now = Date.now();
|
|
219
|
+
let timestamps = [];
|
|
220
|
+
try {
|
|
221
|
+
timestamps = JSON.parse(readFileSync(file, 'utf-8')).timestamps;
|
|
222
|
+
}
|
|
223
|
+
catch { /* corrupt state is reset by this reservation */ }
|
|
224
|
+
timestamps = Array.isArray(timestamps) ? timestamps.filter(value => typeof value === 'number' && now - value < JINA_WINDOW_MS) : [];
|
|
225
|
+
if (timestamps.length >= JINA_RPM) {
|
|
226
|
+
const retryAt = Math.min(...timestamps) + JINA_WINDOW_MS;
|
|
227
|
+
return { ok: false, retryAfterMs: Math.max(0, retryAt - now), retryAt };
|
|
228
|
+
}
|
|
229
|
+
timestamps.push(now); // Reserve before network; failures still consume a sent request.
|
|
230
|
+
const temp = `${file}.${process.pid}.${now}.tmp`;
|
|
231
|
+
writeFileSync(temp, JSON.stringify({ timestamps }), 'utf-8');
|
|
232
|
+
renameSync(temp, file);
|
|
233
|
+
return { ok: true };
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
if (acquired) {
|
|
237
|
+
try {
|
|
238
|
+
rmSync(lock, { recursive: true, force: true });
|
|
239
|
+
}
|
|
240
|
+
catch { /* best effort */ }
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async function jinaExtract(url, options) {
|
|
245
|
+
const slot = jinaAcquireSlot();
|
|
246
|
+
if (!slot.ok) {
|
|
247
|
+
return {
|
|
248
|
+
title: '', content: '', excerpt: '', url, length: 0, extractedAt: Date.now(), method: 'jina',
|
|
249
|
+
error: 'Jina Reader rate limit reached', retryAfterMs: slot.retryAfterMs, retryAt: slot.retryAt,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
const response = await fetch(`${JINA_READER_BASE}/${url}`, {
|
|
254
|
+
headers: {
|
|
255
|
+
'Accept': 'text/plain',
|
|
256
|
+
'X-Respond-With': 'frontmatter',
|
|
257
|
+
},
|
|
258
|
+
signal: AbortSignal.timeout(options.timeoutMs),
|
|
259
|
+
});
|
|
260
|
+
if (!response.ok)
|
|
261
|
+
return null;
|
|
262
|
+
const text = await response.text();
|
|
263
|
+
if (!text.trim())
|
|
264
|
+
return null;
|
|
265
|
+
// Parse frontmatter: ---\nkey: value\n---\ncontent
|
|
266
|
+
let title = '';
|
|
267
|
+
let content = text.trim();
|
|
268
|
+
const fmMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
|
|
269
|
+
if (fmMatch) {
|
|
270
|
+
const meta = fmMatch[1];
|
|
271
|
+
content = fmMatch[2].trim();
|
|
272
|
+
const titleMatch = meta.match(/^title:\s*"?(.+?)"?\s*$/m);
|
|
273
|
+
if (titleMatch)
|
|
274
|
+
title = titleMatch[1];
|
|
275
|
+
}
|
|
276
|
+
if (!content)
|
|
277
|
+
return null;
|
|
278
|
+
return {
|
|
279
|
+
title,
|
|
280
|
+
content,
|
|
281
|
+
excerpt: '',
|
|
282
|
+
url,
|
|
283
|
+
length: content.length,
|
|
284
|
+
extractedAt: Date.now(),
|
|
285
|
+
method: 'jina',
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
export class ContentExtractor {
|
|
293
|
+
timeoutMs;
|
|
294
|
+
concurrency;
|
|
295
|
+
maxResponseBytes;
|
|
296
|
+
useObscura;
|
|
297
|
+
obscuraPath;
|
|
298
|
+
obscuraDumpFormat;
|
|
299
|
+
useJina;
|
|
300
|
+
constructor(options) {
|
|
301
|
+
this.timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
302
|
+
this.concurrency = options?.concurrency ?? DEFAULT_CONCURRENCY;
|
|
303
|
+
this.maxResponseBytes = options?.maxResponseBytes ?? MAX_RESPONSE_BYTES;
|
|
304
|
+
this.useObscura = options?.obscura ?? false;
|
|
305
|
+
this.obscuraPath = options?.obscuraPath;
|
|
306
|
+
this.obscuraDumpFormat = options?.obscuraDumpFormat ?? 'html';
|
|
307
|
+
this.useJina = options?.jina ?? false;
|
|
308
|
+
}
|
|
309
|
+
async extract(url) {
|
|
310
|
+
if (this.useJina) {
|
|
311
|
+
const jinaResult = await jinaExtract(url, { timeoutMs: this.timeoutMs });
|
|
312
|
+
if (jinaResult)
|
|
313
|
+
return jinaResult;
|
|
314
|
+
return {
|
|
315
|
+
title: '',
|
|
316
|
+
content: '',
|
|
317
|
+
excerpt: '',
|
|
318
|
+
url,
|
|
319
|
+
length: 0,
|
|
320
|
+
extractedAt: Date.now(),
|
|
321
|
+
method: 'jina',
|
|
322
|
+
error: 'Jina Reader could not extract content',
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
let result = null;
|
|
326
|
+
try {
|
|
327
|
+
const html = await this.fetchHtml(url);
|
|
328
|
+
result = await defuddleExtract(html, url);
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
result = {
|
|
332
|
+
title: '',
|
|
333
|
+
content: '',
|
|
334
|
+
excerpt: '',
|
|
335
|
+
url,
|
|
336
|
+
length: 0,
|
|
337
|
+
extractedAt: Date.now(),
|
|
338
|
+
error: error instanceof Error ? error.message : String(error),
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
if (result.content.length >= MIN_CONTENT_LENGTH)
|
|
342
|
+
return result;
|
|
343
|
+
// Defuddle insufficient → Obscura fallback
|
|
344
|
+
if (this.useObscura && result.content.length < OBSCURA_THRESHOLD) {
|
|
345
|
+
const obsResult = await obscuraExtract(url, {
|
|
346
|
+
timeoutMs: this.timeoutMs,
|
|
347
|
+
obscuraPath: this.obscuraPath,
|
|
348
|
+
dumpFormat: this.obscuraDumpFormat,
|
|
349
|
+
});
|
|
350
|
+
if (obsResult && obsResult.content.length > result.content.length)
|
|
351
|
+
return obsResult;
|
|
352
|
+
}
|
|
353
|
+
if (result.content.length < MIN_CONTENT_LENGTH && !result.error) {
|
|
354
|
+
result.error = 'Extracted content too short';
|
|
355
|
+
}
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
async fetchHtml(url) {
|
|
359
|
+
const response = await fetch(url, {
|
|
360
|
+
headers: {
|
|
361
|
+
'User-Agent': 'Mozilla/5.0 (compatible; SxngDeepSearch/1.0)',
|
|
362
|
+
'Accept': 'text/html,application/xhtml+xml',
|
|
363
|
+
},
|
|
364
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
365
|
+
});
|
|
366
|
+
if (!response.ok) {
|
|
367
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
368
|
+
}
|
|
369
|
+
const contentLength = response.headers.get('content-length');
|
|
370
|
+
if (contentLength && parseInt(contentLength, 10) > this.maxResponseBytes) {
|
|
371
|
+
throw new Error(`Response too large: ${contentLength} bytes (max ${this.maxResponseBytes})`);
|
|
372
|
+
}
|
|
373
|
+
const html = await response.text();
|
|
374
|
+
if (html.length > this.maxResponseBytes) {
|
|
375
|
+
throw new Error(`Response too large: ${html.length} bytes (max ${this.maxResponseBytes})`);
|
|
376
|
+
}
|
|
377
|
+
return html;
|
|
378
|
+
}
|
|
379
|
+
async extractFromHtml(html, url) {
|
|
380
|
+
return defuddleExtract(html, url);
|
|
381
|
+
}
|
|
382
|
+
async extractBatch(urls) {
|
|
383
|
+
const results = [];
|
|
384
|
+
for (let i = 0; i < urls.length; i += this.concurrency) {
|
|
385
|
+
const batch = urls.slice(i, i + this.concurrency);
|
|
386
|
+
const batchResults = await Promise.all(batch.map(u => this.extract(u)));
|
|
387
|
+
results.push(...batchResults);
|
|
388
|
+
}
|
|
389
|
+
return results;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
//# sourceMappingURL=extractor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractor.js","sourceRoot":"","sources":["../../src/deep/extractor.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAC1F,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD,MAAM,aAAa,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AAE1C,MAAM,kBAAkB,GAAG,MAAM,CAAC;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,OAAO,CAAC,IAAI,CAAC,GAAG,YAAY,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,OAAO,CAAC;IACnB,CAAC;CACJ"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn one search invocation's raw provider responses into the single ordered
|
|
3
|
+
* result set the caller prints and stores.
|
|
4
|
+
*
|
|
5
|
+
* Both search paths used to do this inline and disagreed: one fused against the
|
|
6
|
+
* whole session (including the batch it had just written) and displayed every
|
|
7
|
+
* historical result, the other fused only this run and keyed on a different
|
|
8
|
+
* identity function. This module is the one place that decides order, so
|
|
9
|
+
* `queries.length === 1` is just the case where one response came back.
|
|
10
|
+
*/
|
|
11
|
+
import type { SearchResponse, SearchResult } from '../service.js';
|
|
12
|
+
import type { ResultOrigin } from './session.js';
|
|
13
|
+
export interface FusionInput {
|
|
14
|
+
/** Every response this invocation produced, each carrying its own query. */
|
|
15
|
+
responses: SearchResponse[];
|
|
16
|
+
/** Results imported from a `--merge` file; they rank like any other list. */
|
|
17
|
+
mergeResults?: SearchResult[];
|
|
18
|
+
/**
|
|
19
|
+
* Session contents read *before* this invocation writes anything.
|
|
20
|
+
*
|
|
21
|
+
* Only identities are read: history contributes a vote to the fusion and is
|
|
22
|
+
* never displayed, so the stored body, status and round on a `SessionResult`
|
|
23
|
+
* are irrelevant here and the type stays minimal.
|
|
24
|
+
*/
|
|
25
|
+
sessionSnapshot?: Array<{
|
|
26
|
+
url: string;
|
|
27
|
+
source?: string;
|
|
28
|
+
}>;
|
|
29
|
+
/** When true, only results this invocation produced are displayed. */
|
|
30
|
+
onlyCurrent: boolean;
|
|
31
|
+
limit: number;
|
|
32
|
+
/** Maps a result's engine to the tool that should be recorded as its origin. */
|
|
33
|
+
toolFor?: (engine: string) => string;
|
|
34
|
+
}
|
|
35
|
+
export interface FusionOutput {
|
|
36
|
+
/** What to print, already truncated to `limit`. */
|
|
37
|
+
display: SearchResult[];
|
|
38
|
+
/** The full fused ranking, untruncated — this is what `totalResults` means. */
|
|
39
|
+
fused: Array<{
|
|
40
|
+
id: string;
|
|
41
|
+
score: number;
|
|
42
|
+
}>;
|
|
43
|
+
/** Key → every origin this invocation recorded for that result. */
|
|
44
|
+
originsByKey: Map<string, ResultOrigin[]>;
|
|
45
|
+
/** Keys that this invocation produced (as opposed to history). */
|
|
46
|
+
currentKeys: Set<string>;
|
|
47
|
+
}
|
|
48
|
+
/** Attribute a result to the tool that produced it, for session origin records. */
|
|
49
|
+
export declare function originTool(engine: string, externalProviders: readonly string[]): string;
|
|
50
|
+
/**
|
|
51
|
+
* Fuse responses into one ranking.
|
|
52
|
+
*
|
|
53
|
+
* The order of the steps is the contract:
|
|
54
|
+
* 1. dedupe inside every ranking, so no id votes twice in the same list;
|
|
55
|
+
* 2. rank the extra lists (merge file, then the session snapshot);
|
|
56
|
+
* 3. fuse;
|
|
57
|
+
* 4. project — current results win over history for content, and truncation
|
|
58
|
+
* happens once, at the end.
|
|
59
|
+
*/
|
|
60
|
+
export declare function fuseResults(input: FusionInput): FusionOutput;
|
|
61
|
+
/** Merge envelope metadata, which no ranking participates in. */
|
|
62
|
+
export declare function mergeEnvelopeMetadata(responses: SearchResponse[], base: SearchResponse | null): Pick<SearchResponse, 'answers' | 'suggestions' | 'unresponsiveEngines'>;
|
|
63
|
+
//# sourceMappingURL=fuse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fuse.d.ts","sourceRoot":"","sources":["../../src/deep/fuse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,MAAM,WAAW,WAAW;IACxB,4EAA4E;IAC5E,SAAS,EAAE,cAAc,EAAE,CAAC;IAC5B,6EAA6E;IAC7E,YAAY,CAAC,EAAE,YAAY,EAAE,CAAC;IAC9B;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,KAAK,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1D,sEAAsE;IACtE,WAAW,EAAE,OAAO,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,gFAAgF;IAChF,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;CACxC;AAED,MAAM,WAAW,YAAY;IACzB,mDAAmD;IACnD,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,+EAA+E;IAC/E,KAAK,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC5C,mEAAmE;IACnE,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,EAAE,CAAC,CAAC;IAC1C,kEAAkE;IAClE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAC5B;AAED,mFAAmF;AACnF,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,iBAAiB,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAEvF;AAgBD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,YAAY,CA4C5D;AAED,iEAAiE;AACjE,wBAAgB,qBAAqB,CACjC,SAAS,EAAE,cAAc,EAAE,EAC3B,IAAI,EAAE,cAAc,GAAG,IAAI,GAC5B,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,aAAa,GAAG,qBAAqB,CAAC,CAkBzE"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn one search invocation's raw provider responses into the single ordered
|
|
3
|
+
* result set the caller prints and stores.
|
|
4
|
+
*
|
|
5
|
+
* Both search paths used to do this inline and disagreed: one fused against the
|
|
6
|
+
* whole session (including the batch it had just written) and displayed every
|
|
7
|
+
* historical result, the other fused only this run and keyed on a different
|
|
8
|
+
* identity function. This module is the one place that decides order, so
|
|
9
|
+
* `queries.length === 1` is just the case where one response came back.
|
|
10
|
+
*/
|
|
11
|
+
import { resultUrlKey } from './dedupe.js';
|
|
12
|
+
import { rrf } from './rrf.js';
|
|
13
|
+
/** Attribute a result to the tool that produced it, for session origin records. */
|
|
14
|
+
export function originTool(engine, externalProviders) {
|
|
15
|
+
return externalProviders.includes(engine) ? engine : 'digdig';
|
|
16
|
+
}
|
|
17
|
+
function toRanking(results) {
|
|
18
|
+
// Within one ranking an id must be unique, or RRF counts it twice: it scores
|
|
19
|
+
// by array position, and the same id at two positions earns two terms.
|
|
20
|
+
const seen = new Set();
|
|
21
|
+
const ranking = [];
|
|
22
|
+
for (const result of results) {
|
|
23
|
+
const id = resultUrlKey(result);
|
|
24
|
+
if (seen.has(id))
|
|
25
|
+
continue;
|
|
26
|
+
seen.add(id);
|
|
27
|
+
ranking.push({ id });
|
|
28
|
+
}
|
|
29
|
+
return ranking;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Fuse responses into one ranking.
|
|
33
|
+
*
|
|
34
|
+
* The order of the steps is the contract:
|
|
35
|
+
* 1. dedupe inside every ranking, so no id votes twice in the same list;
|
|
36
|
+
* 2. rank the extra lists (merge file, then the session snapshot);
|
|
37
|
+
* 3. fuse;
|
|
38
|
+
* 4. project — current results win over history for content, and truncation
|
|
39
|
+
* happens once, at the end.
|
|
40
|
+
*/
|
|
41
|
+
export function fuseResults(input) {
|
|
42
|
+
const rankings = input.responses.map(response => toRanking(response.results));
|
|
43
|
+
if (input.mergeResults?.length)
|
|
44
|
+
rankings.push(toRanking(input.mergeResults));
|
|
45
|
+
if (input.sessionSnapshot?.length)
|
|
46
|
+
rankings.push(toRanking(input.sessionSnapshot));
|
|
47
|
+
const fused = rankings.length > 0 ? rrf(rankings) : [];
|
|
48
|
+
const originsByKey = new Map();
|
|
49
|
+
const currentKeys = new Set();
|
|
50
|
+
const current = new Map();
|
|
51
|
+
for (const response of input.responses) {
|
|
52
|
+
for (const result of response.results) {
|
|
53
|
+
const key = resultUrlKey(result);
|
|
54
|
+
currentKeys.add(key);
|
|
55
|
+
// First provider to return a URL supplies its body and its source score;
|
|
56
|
+
// the others still contribute an origin, and RRF weighs the repeat.
|
|
57
|
+
if (!current.has(key))
|
|
58
|
+
current.set(key, result);
|
|
59
|
+
const origins = originsByKey.get(key) ?? [];
|
|
60
|
+
origins.push({
|
|
61
|
+
tool: (input.toolFor ?? (() => 'digdig'))(result.engine),
|
|
62
|
+
engine: result.engine,
|
|
63
|
+
query: response.query,
|
|
64
|
+
});
|
|
65
|
+
originsByKey.set(key, origins);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Only a current result can be displayed, so only a current result needs a body.
|
|
69
|
+
for (const result of input.mergeResults ?? []) {
|
|
70
|
+
const key = resultUrlKey(result);
|
|
71
|
+
if (!current.has(key))
|
|
72
|
+
current.set(key, result);
|
|
73
|
+
}
|
|
74
|
+
const projected = [];
|
|
75
|
+
for (const item of fused) {
|
|
76
|
+
if (input.onlyCurrent && !currentKeys.has(item.id))
|
|
77
|
+
continue;
|
|
78
|
+
const original = current.get(item.id);
|
|
79
|
+
if (!original)
|
|
80
|
+
continue;
|
|
81
|
+
projected.push({ ...original, fusionScore: item.score });
|
|
82
|
+
}
|
|
83
|
+
const display = input.limit > 0 ? projected.slice(0, input.limit) : projected;
|
|
84
|
+
return { display, fused, originsByKey, currentKeys };
|
|
85
|
+
}
|
|
86
|
+
/** Merge envelope metadata, which no ranking participates in. */
|
|
87
|
+
export function mergeEnvelopeMetadata(responses, base) {
|
|
88
|
+
const answers = [];
|
|
89
|
+
const suggestions = [];
|
|
90
|
+
const unresponsiveEngines = [];
|
|
91
|
+
const add = (target, values) => {
|
|
92
|
+
for (const value of values)
|
|
93
|
+
if (!target.includes(value))
|
|
94
|
+
target.push(value);
|
|
95
|
+
};
|
|
96
|
+
for (const response of responses) {
|
|
97
|
+
add(answers, response.answers ?? []);
|
|
98
|
+
add(suggestions, response.suggestions ?? []);
|
|
99
|
+
add(unresponsiveEngines, (response.unresponsiveEngines ?? []).map(item => Array.isArray(item) ? item[0] : item));
|
|
100
|
+
}
|
|
101
|
+
if (base) {
|
|
102
|
+
add(answers, base.answers ?? []);
|
|
103
|
+
add(suggestions, base.suggestions ?? []);
|
|
104
|
+
add(unresponsiveEngines, (base.unresponsiveEngines ?? []).map(item => Array.isArray(item) ? item[0] : item));
|
|
105
|
+
}
|
|
106
|
+
return { answers, suggestions, unresponsiveEngines };
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=fuse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fuse.js","sourceRoot":"","sources":["../../src/deep/fuse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAgB,MAAM,UAAU,CAAC;AAmC7C,mFAAmF;AACnF,MAAM,UAAU,UAAU,CAAC,MAAc,EAAE,iBAAoC;IAC3E,OAAO,iBAAiB,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC;AAClE,CAAC;AAED,SAAS,SAAS,CAAC,OAAwD;IACvE,6EAA6E;IAC7E,uEAAuE;IACvE,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,MAAM,OAAO,GAAmB,EAAE,CAAC;IACnC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC3B,MAAM,EAAE,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;QAChC,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,SAAS;QAC3B,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACb,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,CAAC,C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