dsh-document-reader 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +84 -2
- package/client.js +3652 -0
- package/client.pdfjs.js +21278 -0
- package/client.vendor.js +41 -0
- package/cordis.patch.yml +10 -0
- package/icon.svg +11 -0
- package/index.js +1414 -0
- package/locale/en.json +41 -0
- package/locale/zh.json +41 -0
- package/package.json +50 -3
package/index.js
ADDED
|
@@ -0,0 +1,1414 @@
|
|
|
1
|
+
// Host half of the DSH doc-reader plugin.
|
|
2
|
+
//
|
|
3
|
+
// Registers two read-only loopback endpoints on the DSH web server:
|
|
4
|
+
// GET /doc-reader/dsh-document-reader/file/<encoded absolute path>
|
|
5
|
+
// -> file bytes for the supported document extensions
|
|
6
|
+
// GET /doc-reader/dsh-document-reader/list?path=<encoded absolute dir>
|
|
7
|
+
// -> JSON directory listing (files + directories)
|
|
8
|
+
//
|
|
9
|
+
// This mirrors the VSCode extension behaviour (open any local file), served
|
|
10
|
+
// only to same-host requests of the loopback web GUI. Only whitelisted
|
|
11
|
+
// document extensions are readable; arbitrary code/config files stay out.
|
|
12
|
+
|
|
13
|
+
import { createReadStream } from 'node:fs';
|
|
14
|
+
import { readFile, stat, lstat } from 'node:fs/promises';
|
|
15
|
+
import { basename, dirname, extname, isAbsolute, join, normalize } from 'node:path';
|
|
16
|
+
import { inflateRawSync } from 'node:zlib';
|
|
17
|
+
import { createRequire } from 'node:module';
|
|
18
|
+
import { homedir } from 'node:os';
|
|
19
|
+
import { mkdirSync, readFileSync, renameSync, watch, writeFileSync } from 'node:fs';
|
|
20
|
+
|
|
21
|
+
const ROUTE = '/doc-reader/dsh-document-reader';
|
|
22
|
+
|
|
23
|
+
const TEXTUAL = new Set(['.md', '.markdown', '.txt', '.log', '.json', '.yaml', '.yml', '.csv', '.tsv', '.xml', '.ini', '.conf', '.toml']);
|
|
24
|
+
const IMAGES = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.bmp', '.ico', '.avif', '.jfif']);
|
|
25
|
+
const OFFICE = new Set(['.pptx', '.xlsx', '.ppt', '.xls', '.doc']);
|
|
26
|
+
const BINARY = new Set(['.pdf', '.docx', '.epub', '.html', '.htm', ...OFFICE]);
|
|
27
|
+
const READABLE = new Set([...TEXTUAL, ...IMAGES, ...BINARY]);
|
|
28
|
+
const LISTABLE = new Set([...READABLE, '']); // directories have no extension
|
|
29
|
+
|
|
30
|
+
const CONTENT_TYPES = new Map([
|
|
31
|
+
['.md', 'text/markdown; charset=utf-8'], ['.markdown', 'text/markdown; charset=utf-8'],
|
|
32
|
+
['.txt', 'text/plain; charset=utf-8'], ['.log', 'text/plain; charset=utf-8'],
|
|
33
|
+
['.json', 'application/json; charset=utf-8'], ['.yaml', 'text/plain; charset=utf-8'],
|
|
34
|
+
['.yml', 'text/plain; charset=utf-8'], ['.csv', 'text/csv; charset=utf-8'],
|
|
35
|
+
['.tsv', 'text/tab-separated-values; charset=utf-8'], ['.xml', 'text/xml; charset=utf-8'],
|
|
36
|
+
['.ini', 'text/plain; charset=utf-8'], ['.conf', 'text/plain; charset=utf-8'],
|
|
37
|
+
['.toml', 'text/plain; charset=utf-8'],
|
|
38
|
+
['.png', 'image/png'], ['.jpg', 'image/jpeg'], ['.jpeg', 'image/jpeg'],
|
|
39
|
+
['.gif', 'image/gif'], ['.webp', 'image/webp'], ['.svg', 'image/svg+xml'],
|
|
40
|
+
['.bmp', 'image/bmp'], ['.ico', 'image/x-icon'],
|
|
41
|
+
['.avif', 'image/avif'], ['.jfif', 'image/jpeg'],
|
|
42
|
+
['.pdf', 'application/pdf'], ['.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
|
|
43
|
+
['.epub', 'application/epub+zip'],
|
|
44
|
+
['.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
|
|
45
|
+
['.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
|
|
46
|
+
['.ppt', 'application/vnd.ms-powerpoint'], ['.xls', 'application/vnd.ms-excel'], ['.doc', 'application/msword'],
|
|
47
|
+
['.html', 'text/html; charset=utf-8'], ['.htm', 'text/html; charset=utf-8'],
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
const MAX_LIST = 2000;
|
|
51
|
+
const MAX_TEXT_BYTES = 24 * 1024 * 1024;
|
|
52
|
+
// save bodies carry baseContent AND content (each up to MAX_TEXT_BYTES after
|
|
53
|
+
// JSON escaping can ~2x), so the transport cap must exceed 2x that
|
|
54
|
+
const MAX_SAVE_BODY = 4 * MAX_TEXT_BYTES;
|
|
55
|
+
// container documents render fully in the client — cap them to bound memory
|
|
56
|
+
const MAX_DOC_BYTES = 100 * 1024 * 1024;
|
|
57
|
+
// served documents must never run script when navigated to directly from the
|
|
58
|
+
// address bar (same-origin context) — a blanket CSP closes that hole while
|
|
59
|
+
// leaving fetch-based consumption (arrayBuffer -> parse in the reader) intact
|
|
60
|
+
const DOC_CSP = "default-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; script-src 'none'";
|
|
61
|
+
|
|
62
|
+
// ---------- text extraction engine ----------
|
|
63
|
+
// Shared by the `doc_reader` agent tool and the system-prompt context. Turns
|
|
64
|
+
// any supported document into plain UTF-8 text plus an outline, on the Host
|
|
65
|
+
// side (no browser involved). Cache is keyed by path + mtime + size.
|
|
66
|
+
|
|
67
|
+
function decodeTextHost(buf) {
|
|
68
|
+
const bytes = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
|
|
69
|
+
// strict UTF-8 first, then common CJK legacy encodings (full-ICU Node),
|
|
70
|
+
// else lenient UTF-8. Mirrors the client's decodeText.
|
|
71
|
+
for (const enc of ['utf-8', 'gbk', 'big5', 'shift_jis']) {
|
|
72
|
+
try {
|
|
73
|
+
const out = new TextDecoder(enc, enc === 'utf-8' ? { fatal: true, ignoreBOM: false } : {}).decode(bytes);
|
|
74
|
+
if (typeof out === 'string') return out.replace(/^\uFEFF/u, '');
|
|
75
|
+
} catch { /* try the next encoding */ }
|
|
76
|
+
}
|
|
77
|
+
return new TextDecoder('utf-8').decode(bytes).replace(/^\uFEFF/u, '');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Minimal zip reader (store + deflate). Good enough for epub/docx packages. */
|
|
81
|
+
function unzipEntries(buf) {
|
|
82
|
+
if (!(buf instanceof Buffer)) buf = Buffer.from(buf);
|
|
83
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
84
|
+
let eocd = -1;
|
|
85
|
+
for (let i = buf.byteLength - 22; i >= 0; i--) {
|
|
86
|
+
if (view.getUint32(i, true) === 0x06054b50) { eocd = i; break; }
|
|
87
|
+
}
|
|
88
|
+
if (eocd < 0) throw new Error('not a zip archive');
|
|
89
|
+
const count = view.getUint16(eocd + 10, true);
|
|
90
|
+
let ptr = view.getUint32(eocd + 16, true);
|
|
91
|
+
const out = new Map();
|
|
92
|
+
for (let n = 0; n < count; n++) {
|
|
93
|
+
if (ptr + 46 > buf.byteLength || view.getUint32(ptr, true) !== 0x02014b50) break;
|
|
94
|
+
const method = view.getUint16(ptr + 10, true);
|
|
95
|
+
const csize = view.getUint32(ptr + 20, true);
|
|
96
|
+
const nameLen = view.getUint16(ptr + 28, true);
|
|
97
|
+
const extraLen = view.getUint16(ptr + 30, true);
|
|
98
|
+
const commentLen = view.getUint16(ptr + 32, true);
|
|
99
|
+
const lho = view.getUint32(ptr + 42, true);
|
|
100
|
+
const name = buf.subarray(ptr + 46, ptr + 46 + nameLen).toString('utf8');
|
|
101
|
+
if (view.getUint32(lho, true) === 0x04034b50) {
|
|
102
|
+
const lNameLen = view.getUint16(lho + 26, true);
|
|
103
|
+
const lExtraLen = view.getUint16(lho + 28, true);
|
|
104
|
+
const start = lho + 30 + lNameLen + lExtraLen;
|
|
105
|
+
const data = buf.subarray(start, start + csize);
|
|
106
|
+
out.set(name, method === 0 ? Buffer.from(data) : inflateRawSync(data));
|
|
107
|
+
}
|
|
108
|
+
ptr += 46 + nameLen + extraLen + commentLen;
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function decodeEntities(s) {
|
|
114
|
+
return s
|
|
115
|
+
.replace(/&#x([0-9a-f]+);/giu, (_, h) => { try { return String.fromCodePoint(Number.parseInt(h, 16)); } catch { return ''; } })
|
|
116
|
+
.replace(/&#(\d+);/gu, (_, d) => { try { return String.fromCodePoint(Number.parseInt(d, 10)); } catch { return ''; } })
|
|
117
|
+
.replace(/</gu, '<').replace(/>/gu, '>').replace(/"/gu, '"')
|
|
118
|
+
.replace(/'/gu, "'").replace(/ /gu, ' ').replace(/&/gu, '&');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function htmlToText(html) {
|
|
122
|
+
const s = html
|
|
123
|
+
.replace(/<script[\s\S]*?<\/script>/giu, ' ').replace(/<style[\s\S]*?<\/style>/giu, ' ')
|
|
124
|
+
.replace(/<!--[\s\S]*?-->/gu, ' ')
|
|
125
|
+
.replace(/<\/(p|div|section|article|header|footer|aside|h[1-6]|li|tr|blockquote|pre|table)>/giu, '\n')
|
|
126
|
+
.replace(/<br\s*\/?>/giu, '\n')
|
|
127
|
+
.replace(/<[^>]*>/gu, ' ');
|
|
128
|
+
return decodeEntities(s).split('\n')
|
|
129
|
+
.map((line) => line.replace(/[ \t\u00a0]+/gu, ' ').trim())
|
|
130
|
+
.join('\n').replace(/\n{3,}/gu, '\n\n').trim();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function htmlOutline(html) {
|
|
134
|
+
const outline = [];
|
|
135
|
+
const re = /<h([1-6])[^>]*>([\s\S]*?)<\/h\1>/giu;
|
|
136
|
+
for (const m of html.matchAll(re)) {
|
|
137
|
+
const text = htmlToText(m[2]);
|
|
138
|
+
if (text) outline.push({ level: Number(m[1]), text });
|
|
139
|
+
}
|
|
140
|
+
return outline;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function markdownOutline(text) {
|
|
144
|
+
const outline = [];
|
|
145
|
+
let offset = 0;
|
|
146
|
+
for (const line of String(text).split('\n')) {
|
|
147
|
+
const m = /^(#{1,6})\s+(.*)$/u.exec(line);
|
|
148
|
+
if (m && m[2].trim()) outline.push({ level: m[1].length, text: m[2].trim(), line: offset + 1 });
|
|
149
|
+
offset += line.length + 1;
|
|
150
|
+
if (outline.length >= 500) break;
|
|
151
|
+
}
|
|
152
|
+
return outline;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function epubExtract(bytes) {
|
|
156
|
+
const entries = unzipEntries(bytes);
|
|
157
|
+
const norm = new Map();
|
|
158
|
+
for (const k of entries.keys()) norm.set(k.toLowerCase(), k);
|
|
159
|
+
const get = (p) => { const k = norm.get(String(p).toLowerCase()); return k === undefined ? undefined : entries.get(k); };
|
|
160
|
+
let opfPath = null;
|
|
161
|
+
const container = get('META-INF/container.xml');
|
|
162
|
+
if (container) opfPath = /<rootfile[^>]*full-path="([^"]+)"/iu.exec(container.toString('utf8'))?.[1] ?? null;
|
|
163
|
+
const files = [];
|
|
164
|
+
let base = '';
|
|
165
|
+
if (opfPath && get(opfPath)) {
|
|
166
|
+
base = opfPath.includes('/') ? opfPath.slice(0, opfPath.lastIndexOf('/') + 1) : '';
|
|
167
|
+
const opf = get(opfPath).toString('utf8');
|
|
168
|
+
const manifest = new Map();
|
|
169
|
+
for (const m of opf.matchAll(/<item\s[^>]*>/giu)) {
|
|
170
|
+
const id = /\bid="([^"]+)"/iu.exec(m[0])?.[1];
|
|
171
|
+
const href = /\bhref="([^"]+)"/iu.exec(m[0])?.[1];
|
|
172
|
+
if (id && href) manifest.set(id, href);
|
|
173
|
+
}
|
|
174
|
+
for (const m of opf.matchAll(/<itemref\s[^>]*idref="([^"]+)"/giu)) {
|
|
175
|
+
const href = manifest.get(m[1]);
|
|
176
|
+
if (href) { try { files.push(decodeURIComponent(href)); } catch { files.push(href); } }
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (!files.length) files.push(...[...entries.keys()].filter((k) => /\.x?html?$/iu.test(k)).sort());
|
|
180
|
+
const chapters = [];
|
|
181
|
+
for (const href of files.slice(0, 500)) {
|
|
182
|
+
const data = get(base + href) ?? get(href);
|
|
183
|
+
if (!data) continue;
|
|
184
|
+
const text = htmlToText(data.toString('utf8'));
|
|
185
|
+
if (text) chapters.push(text);
|
|
186
|
+
}
|
|
187
|
+
// outline: EPUB2 toc.ncx navMap, else EPUB3 nav document
|
|
188
|
+
const outline = [];
|
|
189
|
+
const ncxKey = [...norm.keys()].find((k) => k.endsWith('.ncx'));
|
|
190
|
+
if (ncxKey) {
|
|
191
|
+
const ncx = entries.get(norm.get(ncxKey)).toString('utf8');
|
|
192
|
+
for (const m of ncx.matchAll(/<navPoint[^>]*depth="(\d+)"[^>]*>[\s\S]*?<\/navPoint>/giu)) {
|
|
193
|
+
const label = htmlToText(/<navLabel[^>]*>([\s\S]*?)<\/navLabel>/iu.exec(m[0])?.[1] ?? '');
|
|
194
|
+
if (label) outline.push({ level: Math.max(1, Math.min(6, Number(m[1]) || 1)), text: label });
|
|
195
|
+
}
|
|
196
|
+
// depth attribute may sit on ancestors; fall back to flat navPoint order
|
|
197
|
+
if (!outline.length) {
|
|
198
|
+
for (const m of ncx.matchAll(/<navLabel[^>]*>([\s\S]*?)<\/navLabel>/giu)) {
|
|
199
|
+
const label = htmlToText(m[1]);
|
|
200
|
+
if (label) outline.push({ level: 1, text: label });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
const navKey = [...norm.keys()].find((k) => /nav\.x?html?$/iu.test(k));
|
|
205
|
+
if (navKey) {
|
|
206
|
+
const nav = entries.get(norm.get(navKey)).toString('utf8');
|
|
207
|
+
const toc = /epub:type="toc"[^>]*>([\s\S]*?)<\/nav>/iu.exec(nav)?.[1] ?? nav;
|
|
208
|
+
for (const m of toc.matchAll(/<a\s[^>]*>([\s\S]*?)<\/a>/giu)) {
|
|
209
|
+
const label = htmlToText(m[1]);
|
|
210
|
+
if (label) outline.push({ level: 1, text: label });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return { text: chapters.join('\n\n'), outline };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function docxExtract(bytes) {
|
|
218
|
+
const entries = unzipEntries(bytes);
|
|
219
|
+
const doc = entries.get('word/document.xml');
|
|
220
|
+
if (!doc) throw new Error('not a docx package (missing word/document.xml)');
|
|
221
|
+
const xml = doc.toString('utf8');
|
|
222
|
+
const lines = [];
|
|
223
|
+
const outline = [];
|
|
224
|
+
for (const para of xml.split(/<\/w:p>/iu)) {
|
|
225
|
+
const style = /<w:pStyle\s[^>]*w:val="([^"]+)"/iu.exec(para)?.[1] ?? '';
|
|
226
|
+
const headingLevel = /^heading([1-6])$/iu.exec(style)?.[1];
|
|
227
|
+
const outlineLvl = /<w:outlineLvl\s[^>]*w:val="([0-5])"/iu.exec(para)?.[1];
|
|
228
|
+
const level = headingLevel ?? (outlineLvl !== undefined ? String(Number(outlineLvl) + 1) : null);
|
|
229
|
+
const text = decodeEntities(para
|
|
230
|
+
.replace(/<w:tab[^>]*\/>/giu, '\t')
|
|
231
|
+
.replace(/<w:br[^>]*\/>/giu, '\n')
|
|
232
|
+
.replace(/<[^>]*>/gu, '')).trim();
|
|
233
|
+
if (level !== null && text) outline.push({ level: Number(level), text });
|
|
234
|
+
lines.push(text);
|
|
235
|
+
}
|
|
236
|
+
return { text: lines.join('\n').replace(/\n{3,}/gu, '\n\n').trim(), outline };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let pdfjsPromise = null;
|
|
240
|
+
function loadPdfjs() {
|
|
241
|
+
pdfjsPromise ??= import('pdfjs-dist/legacy/build/pdf.mjs').catch((error) => { pdfjsPromise = null; throw error; });
|
|
242
|
+
return pdfjsPromise;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function pdfExtract(bytes, maxPages = 800) {
|
|
246
|
+
const pdfjs = await loadPdfjs();
|
|
247
|
+
const doc = await pdfjs.getDocument({
|
|
248
|
+
// pdfjs 4.x insists on a plain Uint8Array (not a Buffer view)
|
|
249
|
+
data: new Uint8Array(bytes), isEvalSupported: false, disableFontFace: true, useSystemFonts: false, verbosity: 0,
|
|
250
|
+
}).promise;
|
|
251
|
+
const pages = Math.min(doc.numPages, maxPages);
|
|
252
|
+
const parts = [];
|
|
253
|
+
for (let i = 1; i <= pages; i++) {
|
|
254
|
+
const page = await doc.getPage(i);
|
|
255
|
+
const content = await page.getTextContent();
|
|
256
|
+
parts.push(content.items.map((item) => item.str + (item.hasEOL ? '\n' : '')).join(''));
|
|
257
|
+
page.cleanup?.();
|
|
258
|
+
}
|
|
259
|
+
let outline = [];
|
|
260
|
+
try {
|
|
261
|
+
const bookmarks = await doc.getOutline();
|
|
262
|
+
const walk = (items, level) => {
|
|
263
|
+
for (const it of items ?? []) {
|
|
264
|
+
if (it.title) outline.push({ level: Math.min(6, level), text: it.title });
|
|
265
|
+
walk(it.items ?? [], level + 1);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
walk(bookmarks, 1);
|
|
269
|
+
} catch { /* bookmarks are optional */ }
|
|
270
|
+
const text = parts.join('\n\n');
|
|
271
|
+
return { text, outline: outline.slice(0, 200), truncated: doc.numPages > pages };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ---------- office → PDF conversion (official libreoffice-kit) ----------
|
|
275
|
+
// The kit ships inside the DSH installation (Electron resolves it from the
|
|
276
|
+
// app archive); a plain-node test run finds neither and conversion reports
|
|
277
|
+
// unavailable instead of breaking the reader.
|
|
278
|
+
let kitState = 'idle'; // 'idle' | 'loading' | 'ready' | 'unavailable'
|
|
279
|
+
let kitPromise = null;
|
|
280
|
+
let kitConverter = null;
|
|
281
|
+
const convertInflight = new Map(); // cache pdf path -> Promise<pdfPath>
|
|
282
|
+
|
|
283
|
+
async function loadKit() {
|
|
284
|
+
if (kitState === 'ready') return kitConverter;
|
|
285
|
+
if (kitState === 'unavailable') throw new Error('文档转换引擎不可用(未找到 LibreOffice Kit)');
|
|
286
|
+
if (kitPromise) return kitPromise;
|
|
287
|
+
kitState = 'loading';
|
|
288
|
+
kitPromise = (async () => {
|
|
289
|
+
let mod = null;
|
|
290
|
+
const roots = [];
|
|
291
|
+
if (process.env.DSH_INSTALL_DIR) roots.push(join(dirname(process.env.DSH_INSTALL_DIR), 'libreoffice-kit', 'lib', 'index.js'));
|
|
292
|
+
// Electron main process anchor: resourcesPath points at Contents/Resources
|
|
293
|
+
// (DSH_INSTALL_DIR is a shell convenience and absent in the Host process)
|
|
294
|
+
if (process.resourcesPath) roots.push(join(process.resourcesPath, 'app.asar', 'dsh', 'node_modules', '@deepseek-ai', 'libreoffice-kit', 'lib', 'index.js'));
|
|
295
|
+
roots.push('@deepseek-ai/libreoffice-kit'); // plugin-local install fallback
|
|
296
|
+
let lastError = '';
|
|
297
|
+
let kitEntry = null;
|
|
298
|
+
for (const spec of roots) {
|
|
299
|
+
try {
|
|
300
|
+
if (spec.startsWith('/')) {
|
|
301
|
+
// Electron resolves CJS inside the app archive through require;
|
|
302
|
+
// dynamic import of archive paths is unreliable — use createRequire
|
|
303
|
+
const requireKit = createRequire(import.meta.url);
|
|
304
|
+
mod = requireKit(spec);
|
|
305
|
+
kitEntry = spec;
|
|
306
|
+
} else {
|
|
307
|
+
mod = await import(spec);
|
|
308
|
+
try { kitEntry = import.meta.resolve ? import.meta.resolve(spec) : null; } catch { kitEntry = null; }
|
|
309
|
+
}
|
|
310
|
+
const api = mod?.createConverter ?? mod?.default?.createConverter;
|
|
311
|
+
if (api) { mod = { createConverter: api }; break; }
|
|
312
|
+
mod = null;
|
|
313
|
+
kitEntry = null;
|
|
314
|
+
lastError = 'no createConverter export';
|
|
315
|
+
} catch (error) { lastError = `${error?.code ?? ''} ${error?.message ?? error}`.slice(0, 160); }
|
|
316
|
+
}
|
|
317
|
+
if (!mod?.createConverter || !kitEntry) throw new Error(`文档转换引擎不可用(未找到 LibreOffice Kit;${lastError})`);
|
|
318
|
+
// Engine packages live inside the app archive but native executables only
|
|
319
|
+
// run from the unpacked copy — the same resolver the official office-kit
|
|
320
|
+
// passes (map the first .asar segment to .asar.unpacked).
|
|
321
|
+
const fromKit = createRequire(kitEntry.startsWith('file:') ? new URL(kitEntry) : kitEntry);
|
|
322
|
+
const resolveEnginePackage = (name) => {
|
|
323
|
+
const manifest = fromKit.resolve(`${name}/package.json`);
|
|
324
|
+
const hit = /^(.*?\.asar)([/\\].*)$/.exec(manifest);
|
|
325
|
+
return hit ? `${hit[1]}.unpacked${hit[2]}` : manifest;
|
|
326
|
+
};
|
|
327
|
+
kitConverter = await mod.createConverter({ timeoutMs: 120_000, resolveEnginePackage });
|
|
328
|
+
kitState = 'ready';
|
|
329
|
+
return kitConverter;
|
|
330
|
+
})();
|
|
331
|
+
try {
|
|
332
|
+
return await kitPromise;
|
|
333
|
+
} catch (error) {
|
|
334
|
+
kitState = 'unavailable';
|
|
335
|
+
kitPromise = null;
|
|
336
|
+
throw error;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function convertCacheDir() {
|
|
341
|
+
const dir = join(pluginDataDir(), 'convert');
|
|
342
|
+
mkdirSync(dir, { recursive: true });
|
|
343
|
+
return dir;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Convert an office document to a cached PDF; returns the absolute pdf path. */
|
|
347
|
+
async function convertToPdf(file, info) {
|
|
348
|
+
const { createHash } = await import('node:crypto');
|
|
349
|
+
const key = createHash('sha1').update(`${file}:${info.mtimeMs}:${info.size}:v1`).digest('hex').slice(0, 24);
|
|
350
|
+
const pdfPath = join(convertCacheDir(), `${key}.pdf`);
|
|
351
|
+
if (await stat(pdfPath).then(() => true).catch(() => false)) return pdfPath;
|
|
352
|
+
const running = convertInflight.get(pdfPath);
|
|
353
|
+
if (running) return running;
|
|
354
|
+
const job = (async () => {
|
|
355
|
+
const converter = await loadKit();
|
|
356
|
+
const { writeFile, rename: renameFile, unlink } = await import('node:fs/promises');
|
|
357
|
+
const tmp = `${pdfPath}.tmp-${process.pid}-${Date.now()}`;
|
|
358
|
+
try {
|
|
359
|
+
// adversarial: a stuck converter process must not hang the request
|
|
360
|
+
// forever (and pin the inflight entry). Env var is a test seam.
|
|
361
|
+
const timeoutMs = Number(process.env.DOC_READER_CONVERT_TIMEOUT_MS) || 120_000;
|
|
362
|
+
const result = await Promise.race([
|
|
363
|
+
converter.render({ inputPath: file, outputPath: tmp }),
|
|
364
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error(`文档转换超时(${Math.round(timeoutMs / 1000)}s)`)), timeoutMs)),
|
|
365
|
+
]);
|
|
366
|
+
if (!result || typeof result !== 'object') throw new Error('转换结果无效');
|
|
367
|
+
await renameFile(tmp, pdfPath);
|
|
368
|
+
return pdfPath;
|
|
369
|
+
} catch (error) {
|
|
370
|
+
await unlink(tmp).catch(() => {});
|
|
371
|
+
throw new Error(`文档转换失败:${error?.message ?? error}`);
|
|
372
|
+
} finally {
|
|
373
|
+
void pruneConvertCache();
|
|
374
|
+
}
|
|
375
|
+
})();
|
|
376
|
+
convertInflight.set(pdfPath, job);
|
|
377
|
+
try {
|
|
378
|
+
return await job;
|
|
379
|
+
} finally {
|
|
380
|
+
convertInflight.delete(pdfPath);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
let pruneRunning = false;
|
|
385
|
+
async function pruneConvertCache() {
|
|
386
|
+
if (pruneRunning) return;
|
|
387
|
+
pruneRunning = true;
|
|
388
|
+
try {
|
|
389
|
+
const { readdir } = await import('node:fs/promises');
|
|
390
|
+
const dir = convertCacheDir();
|
|
391
|
+
const names = (await readdir(dir).catch(() => [])).filter((n) => n.endsWith('.pdf'));
|
|
392
|
+
if (names.length <= 16) return;
|
|
393
|
+
const entries = [];
|
|
394
|
+
for (const name of names) {
|
|
395
|
+
const info = await stat(join(dir, name)).catch(() => null);
|
|
396
|
+
if (info) entries.push({ name, mtime: info.mtimeMs, size: info.size });
|
|
397
|
+
}
|
|
398
|
+
const total = entries.reduce((n, e) => n + e.size, 0);
|
|
399
|
+
if (total <= 512 * 1024 * 1024) return;
|
|
400
|
+
const { unlink } = await import('node:fs/promises');
|
|
401
|
+
for (const e of entries.sort((a, b) => a.mtime - b.mtime).slice(0, Math.ceil(names.length / 4))) {
|
|
402
|
+
await unlink(join(dir, e.name)).catch(() => {});
|
|
403
|
+
}
|
|
404
|
+
} finally {
|
|
405
|
+
pruneRunning = false;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const extractCache = new Map(); // absolute path -> { key, text, outline, format, bytes, note }
|
|
410
|
+
async function extractDocument(file, info, ext) {
|
|
411
|
+
const key = `${info.mtimeMs}:${info.size}`;
|
|
412
|
+
const hit = extractCache.get(file);
|
|
413
|
+
if (hit && hit.key === key) return hit;
|
|
414
|
+
const bytes = OFFICE.has(ext) ? null : await readFile(file);
|
|
415
|
+
let out;
|
|
416
|
+
let note = '';
|
|
417
|
+
if (OFFICE.has(ext)) {
|
|
418
|
+
// deck/spreadsheet: reuse the converted PDF, then extract its text layer
|
|
419
|
+
const pdfPath = await convertToPdf(file, info);
|
|
420
|
+
out = await pdfExtract(await readFile(pdfPath));
|
|
421
|
+
}
|
|
422
|
+
else if (ext === '.epub') out = epubExtract(bytes);
|
|
423
|
+
else if (ext === '.docx') out = docxExtract(bytes);
|
|
424
|
+
else if (ext === '.pdf') {
|
|
425
|
+
out = await pdfExtract(bytes);
|
|
426
|
+
if (out.truncated) note = `仅提取前 800 页(全书 ${'>'}${800} 页)`;
|
|
427
|
+
}
|
|
428
|
+
else if (ext === '.html' || ext === '.htm') {
|
|
429
|
+
const html = decodeTextHost(bytes);
|
|
430
|
+
out = { text: htmlToText(html), outline: htmlOutline(html) };
|
|
431
|
+
} else {
|
|
432
|
+
const text = decodeTextHost(bytes);
|
|
433
|
+
out = { text, outline: markdownOutline(text) };
|
|
434
|
+
}
|
|
435
|
+
const value = { key, text: out.text ?? '', outline: out.outline ?? [], format: ext, bytes: info.size, note };
|
|
436
|
+
if (extractCache.size >= 8) extractCache.delete(extractCache.keys().next().value);
|
|
437
|
+
extractCache.set(file, value);
|
|
438
|
+
return value;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function extensionOf(path) {
|
|
442
|
+
const ext = extname(path).toLowerCase();
|
|
443
|
+
return ext.startsWith('.') ? ext : `.${ext}`;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function fail(res, status, message, req) {
|
|
447
|
+
if (res.headersSent) { res.destroy(); return; }
|
|
448
|
+
const body = JSON.stringify({ error: message });
|
|
449
|
+
// HEAD responses carry no body, so surface the error via a header for the
|
|
450
|
+
// client probe path (open()/RecentList HEAD checks)
|
|
451
|
+
const extra = req?.method === 'HEAD' ? { 'x-doc-reader-error': String(message).replace(/[^\x20-\x7e]/gu, '').slice(0, 200) } : {};
|
|
452
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'content-length': Buffer.byteLength(body), ...extra });
|
|
453
|
+
res.end(body);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** Same-host guard: reject cross-origin/DNS-rebinding requests (Host header must stay loopback). */
|
|
457
|
+
function hostAllowed(req) {
|
|
458
|
+
const host = req.headers.host ?? '';
|
|
459
|
+
const hostname = host.replace(/:\d+$/u, '').replace(/^\[/u, '').replace(/\]$/u).toLowerCase();
|
|
460
|
+
return hostname === '127.0.0.1' || hostname === 'localhost' || hostname === '::1' || hostname === '[::1]';
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Cross-site guard (CSRF): a malicious page in the browser can send requests
|
|
465
|
+
* straight to 127.0.0.1 with a perfectly valid Host header, including
|
|
466
|
+
* navigation-level `text/plain` form POSTs that bypass CORS entirely. Host
|
|
467
|
+
* checks cannot see that; Sec-Fetch-Site / Origin can. Reject anything that
|
|
468
|
+
* is provably cross-site; allow same-origin, same-none (address bar, Electron
|
|
469
|
+
* internals) and legacy clients that send neither header.
|
|
470
|
+
*/
|
|
471
|
+
function requestAllowed(req) {
|
|
472
|
+
const site = req.headers['sec-fetch-site'];
|
|
473
|
+
if (site !== undefined && site !== 'same-origin' && site !== 'same-site' && site !== 'none') return false;
|
|
474
|
+
const origin = req.headers.origin;
|
|
475
|
+
if (origin !== undefined) {
|
|
476
|
+
try {
|
|
477
|
+
const { hostname } = new URL(origin);
|
|
478
|
+
const h = (hostname ?? '').toLowerCase();
|
|
479
|
+
if (h !== '127.0.0.1' && h !== 'localhost' && h !== '::1') return false;
|
|
480
|
+
} catch {
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export const inject = ['webServer', 'tools', 'systemPrompt', 'commands'];
|
|
488
|
+
|
|
489
|
+
// ---------- plugin-owned configuration ----------
|
|
490
|
+
// Stored as plugin data (not a DSH Config row) so toggling a switch never
|
|
491
|
+
// restarts the plugin — the same pattern the weread plugin uses. Fields are
|
|
492
|
+
// validated on write; unknown keys are dropped.
|
|
493
|
+
const DEFAULT_CONFIG = {
|
|
494
|
+
ctxInjectEnabled: true, // inject the active document's outline into the agent context
|
|
495
|
+
ctxOutlineCount: 30, // max outline entries in that injection
|
|
496
|
+
autoReloadEnabled: true, // push external file changes to the reader via SSE
|
|
497
|
+
toolEnabled: true, // expose the doc_reader agent tool
|
|
498
|
+
commandEnabled: true, // expose the /doc slash command
|
|
499
|
+
};
|
|
500
|
+
const CONFIG_FIELDS = {
|
|
501
|
+
ctxInjectEnabled: 'boolean', ctxOutlineCount: 'number', autoReloadEnabled: 'boolean',
|
|
502
|
+
toolEnabled: 'boolean', commandEnabled: 'boolean',
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
function pluginDataDir() {
|
|
506
|
+
// DOC_READER_HOME is a test seam: the suite redirects writes away from $HOME
|
|
507
|
+
return process.env.DOC_READER_HOME ?? join(homedir(), '.dsh', 'doc-reader');
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function makeConfigStore() {
|
|
511
|
+
const dir = pluginDataDir();
|
|
512
|
+
const file = join(dir, 'config.json');
|
|
513
|
+
let current = { ...DEFAULT_CONFIG };
|
|
514
|
+
try {
|
|
515
|
+
const raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
516
|
+
if (raw && typeof raw === 'object') {
|
|
517
|
+
for (const key of Object.keys(CONFIG_FIELDS)) {
|
|
518
|
+
const value = raw[key];
|
|
519
|
+
const want = CONFIG_FIELDS[key];
|
|
520
|
+
if (want === 'boolean' && typeof value === 'boolean') current[key] = value;
|
|
521
|
+
if (want === 'number' && typeof value === 'number' && Number.isFinite(value)) current[key] = value;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
} catch { /* no config yet — defaults */ }
|
|
525
|
+
const save = async () => {
|
|
526
|
+
try {
|
|
527
|
+
mkdirSync(dir, { recursive: true });
|
|
528
|
+
const tmp = join(dir, `.config.json.tmp-${process.pid}`);
|
|
529
|
+
writeFileSync(tmp, JSON.stringify(current, null, '\t'), 'utf8');
|
|
530
|
+
renameSync(tmp, file);
|
|
531
|
+
} catch { /* read-only home: config stays in memory */ }
|
|
532
|
+
};
|
|
533
|
+
return {
|
|
534
|
+
get: () => current,
|
|
535
|
+
update(patch) {
|
|
536
|
+
const next = { ...current };
|
|
537
|
+
for (const [key, value] of Object.entries(patch ?? {})) {
|
|
538
|
+
if (!(key in CONFIG_FIELDS)) continue;
|
|
539
|
+
const want = CONFIG_FIELDS[key];
|
|
540
|
+
if (want === 'boolean' && typeof value === 'boolean') next[key] = value;
|
|
541
|
+
else if (want === 'number' && typeof value === 'number' && Number.isFinite(value)) next[key] = Math.max(1, Math.min(100, Math.round(value)));
|
|
542
|
+
}
|
|
543
|
+
current = next;
|
|
544
|
+
void save();
|
|
545
|
+
return { ...current };
|
|
546
|
+
},
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** Runtime shared by HTTP endpoints, the agent tool and the slash command. */
|
|
551
|
+
function makeRuntime(config) {
|
|
552
|
+
const state = {
|
|
553
|
+
active: null, // { path, at } — the document currently open in the reader
|
|
554
|
+
recent: [], // [{ path, at }] capped, persisted alongside the config
|
|
555
|
+
};
|
|
556
|
+
const recentFile = join(pluginDataDir(), 'recent.json');
|
|
557
|
+
try {
|
|
558
|
+
const raw = JSON.parse(readFileSync(recentFile, 'utf8'));
|
|
559
|
+
if (Array.isArray(raw)) {
|
|
560
|
+
state.recent = raw.filter((e) => e && typeof e.path === 'string').slice(0, 20);
|
|
561
|
+
}
|
|
562
|
+
} catch { /* start empty */ }
|
|
563
|
+
const persistRecent = () => {
|
|
564
|
+
try {
|
|
565
|
+
mkdirSync(dirname(recentFile), { recursive: true });
|
|
566
|
+
const tmp = join(dirname(recentFile), `.recent.json.tmp-${process.pid}`);
|
|
567
|
+
writeFileSync(tmp, JSON.stringify(state.recent), 'utf8');
|
|
568
|
+
renameSync(tmp, recentFile);
|
|
569
|
+
} catch { /* best effort */ }
|
|
570
|
+
};
|
|
571
|
+
const setActive = (path) => {
|
|
572
|
+
state.active = { path, at: Date.now() };
|
|
573
|
+
state.recent = [{ path, at: state.active.at }, ...state.recent.filter((e) => e.path !== path)].slice(0, 20);
|
|
574
|
+
persistRecent();
|
|
575
|
+
ctxCache = { at: 0, path: null, text: '' }; // invalidate the prompt context
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
// ---------- SSE event stream ----------
|
|
579
|
+
const sseClients = new Set(); // { res, watchPath }
|
|
580
|
+
const watchStore = new Map(); // path -> { refs, watcher, last, timer }
|
|
581
|
+
const statKey = async (path) => {
|
|
582
|
+
const info = await lstat(path).catch(() => null);
|
|
583
|
+
return info && info.isFile() ? `${info.mtimeMs}:${info.size}` : null;
|
|
584
|
+
};
|
|
585
|
+
const ensureWatch = (path) => {
|
|
586
|
+
const existing = watchStore.get(path);
|
|
587
|
+
if (existing) { existing.refs += 1; return; }
|
|
588
|
+
const entry = { refs: 1, watcher: null, last: null, timer: null };
|
|
589
|
+
watchStore.set(path, entry);
|
|
590
|
+
const check = async () => {
|
|
591
|
+
entry.timer = null;
|
|
592
|
+
const key = await statKey(path);
|
|
593
|
+
if (key === null || key === entry.last) return;
|
|
594
|
+
entry.last = key;
|
|
595
|
+
broadcast({ type: 'change', path });
|
|
596
|
+
};
|
|
597
|
+
const attach = async () => {
|
|
598
|
+
entry.last = await statKey(path);
|
|
599
|
+
try {
|
|
600
|
+
// watch the parent directory: atomic save (temp + rename) swaps
|
|
601
|
+
// the directory entry, which would invalidate a file-level watch
|
|
602
|
+
entry.watcher = watch(dirname(path), { persistent: false }, (_event, filename) => {
|
|
603
|
+
if (typeof filename === 'string' && filename !== basename(path)) return;
|
|
604
|
+
if (entry.timer) clearTimeout(entry.timer);
|
|
605
|
+
entry.timer = setTimeout(() => { void check(); }, 120);
|
|
606
|
+
});
|
|
607
|
+
entry.watcher.on('error', () => { /* transient (rename swap): re-attach lazily */ });
|
|
608
|
+
} catch { /* unwatchable fs — events simply never fire */ }
|
|
609
|
+
};
|
|
610
|
+
void attach();
|
|
611
|
+
};
|
|
612
|
+
const releaseWatch = (path) => {
|
|
613
|
+
const entry = watchStore.get(path);
|
|
614
|
+
if (!entry) return;
|
|
615
|
+
entry.refs -= 1;
|
|
616
|
+
if (entry.refs > 0) return;
|
|
617
|
+
watchStore.delete(path);
|
|
618
|
+
if (entry.timer) clearTimeout(entry.timer);
|
|
619
|
+
try { entry.watcher?.close(); } catch { /* already closed */ }
|
|
620
|
+
};
|
|
621
|
+
/** Re-baseline a watched file after this plugin itself saved it (no echo event). */
|
|
622
|
+
const touchWatch = (path) => {
|
|
623
|
+
const entry = watchStore.get(path);
|
|
624
|
+
if (!entry) return;
|
|
625
|
+
void statKey(path).then((key) => { if (key !== null) entry.last = key; });
|
|
626
|
+
};
|
|
627
|
+
const broadcast = (event) => {
|
|
628
|
+
if (!sseClients.size) return;
|
|
629
|
+
const frame = `data: ${JSON.stringify(event)}\n\n`;
|
|
630
|
+
for (const client of sseClients) {
|
|
631
|
+
try { client.res.write(frame); } catch { /* drop on next close event */ }
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
// ---------- system-prompt context ----------
|
|
636
|
+
let ctxCache = { at: 0, path: null, text: '' };
|
|
637
|
+
let ctxBuilding = false;
|
|
638
|
+
const rebuildCtx = async () => {
|
|
639
|
+
if (ctxBuilding) return;
|
|
640
|
+
ctxBuilding = true;
|
|
641
|
+
try {
|
|
642
|
+
const cfg = config.get();
|
|
643
|
+
let text = '';
|
|
644
|
+
if (cfg.ctxInjectEnabled && state.active) {
|
|
645
|
+
const path = state.active.path;
|
|
646
|
+
try {
|
|
647
|
+
const info = await lstat(path).catch(() => null);
|
|
648
|
+
if (info && info.isFile()) {
|
|
649
|
+
const doc = await extractDocument(path, info, extensionOf(path)).catch(() => null);
|
|
650
|
+
if (doc) {
|
|
651
|
+
const count = Math.max(1, Math.min(100, cfg.ctxOutlineCount));
|
|
652
|
+
const outline = doc.outline.slice(0, count)
|
|
653
|
+
.map((item) => `${' '.repeat(Math.max(0, item.level - 1))}- ${item.text}`)
|
|
654
|
+
.join('\n');
|
|
655
|
+
text = `用户当前在文档阅读器打开(供对话参考;用户未提及时不必主动展开):\n${path}(${doc.format.slice(1)} 文档,${formatBytes(doc.bytes)})`
|
|
656
|
+
+ (outline ? `\n大纲:\n${outline}` : '');
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
} catch { /* unreadable: inject nothing */ }
|
|
660
|
+
}
|
|
661
|
+
ctxCache = { at: Date.now(), path: state.active?.path ?? null, text };
|
|
662
|
+
} finally { ctxBuilding = false; }
|
|
663
|
+
};
|
|
664
|
+
const contextText = () => {
|
|
665
|
+
if (!state.active) return '';
|
|
666
|
+
if (ctxCache.path === state.active.path && Date.now() - ctxCache.at < 10 * 60_000) return ctxCache.text;
|
|
667
|
+
if (ctxCache.path !== state.active.path) ctxCache = { at: 0, path: null, text: '' };
|
|
668
|
+
void rebuildCtx();
|
|
669
|
+
return ctxCache.text;
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
const updateConfig = (patch) => {
|
|
673
|
+
const result = config.update(patch);
|
|
674
|
+
ctxCache = { at: 0, path: null, text: '' }; // context flags may have flipped
|
|
675
|
+
return result;
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
return { config, updateConfig, state, setActive, sseClients, ensureWatch, releaseWatch, touchWatch, broadcast, contextText };
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function formatBytes(n) {
|
|
682
|
+
if (!Number.isFinite(n)) return '';
|
|
683
|
+
if (n < 1024) return `${n} B`;
|
|
684
|
+
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
|
|
685
|
+
return `${(n / (1024 * 1024)).toFixed(1)} MB`;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** Resolve + validate a readable document path shared by routes, tool and command. */
|
|
689
|
+
async function resolveReadable(target) {
|
|
690
|
+
const file = normalize(String(target ?? ''));
|
|
691
|
+
if (!isAbsolute(file)) return { error: '需要绝对路径' };
|
|
692
|
+
const ext = extensionOf(file);
|
|
693
|
+
if (!READABLE.has(ext)) return { error: `不支持的扩展名:${ext || '(无)'}` };
|
|
694
|
+
const info = await lstat(file).catch(() => null);
|
|
695
|
+
if (!info) return { error: '文件不存在' };
|
|
696
|
+
if (info.isSymbolicLink()) return { error: '符号链接不可读' };
|
|
697
|
+
if (!info.isFile()) return { error: '不是普通文件' };
|
|
698
|
+
if (TEXTUAL.has(ext) && info.size > MAX_TEXT_BYTES) return { error: '文本文件过大(>24MB)' };
|
|
699
|
+
if ((ext === '.html' || ext === '.htm' || ext === '.epub') && info.size > MAX_DOC_BYTES) return { error: '文档过大(>100MB)' };
|
|
700
|
+
if (OFFICE.has(ext) && info.size > MAX_DOC_BYTES) return { error: '文档过大(>100MB)' };
|
|
701
|
+
if (info.size > 2 * MAX_DOC_BYTES) return { error: '文件过大' };
|
|
702
|
+
return { file, ext, info };
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export function apply(ctx) {
|
|
706
|
+
const config = makeConfigStore();
|
|
707
|
+
const runtime = makeRuntime(config);
|
|
708
|
+
pluginRuntime = runtime; // test seam: tools/commands reach the live instance
|
|
709
|
+
|
|
710
|
+
ctx.effect(() => ctx.webServer.register({
|
|
711
|
+
kind: 'prefix',
|
|
712
|
+
path: ROUTE,
|
|
713
|
+
handler: (req, res) => {
|
|
714
|
+
handleRequest(ctx, req, res, runtime).catch((error) => {
|
|
715
|
+
ctx.logger.warn(`doc-reader route error: ${error?.message ?? error}`);
|
|
716
|
+
if (!res.headersSent) fail(res, 500, 'internal error');
|
|
717
|
+
else res.destroy();
|
|
718
|
+
});
|
|
719
|
+
},
|
|
720
|
+
}), 'dsh-doc-reader: file routes');
|
|
721
|
+
|
|
722
|
+
// SSE heartbeat: keeps proxies from idling the stream out and lets dead
|
|
723
|
+
// connections surface via the close event.
|
|
724
|
+
const heartbeat = setInterval(() => {
|
|
725
|
+
for (const client of runtime.sseClients) {
|
|
726
|
+
try { client.res.write(':hb\n\n'); } catch { /* cleaned up on close */ }
|
|
727
|
+
}
|
|
728
|
+
}, 25_000);
|
|
729
|
+
heartbeat.unref?.();
|
|
730
|
+
ctx.effect(() => () => clearInterval(heartbeat), 'dsh-doc-reader: sse heartbeat');
|
|
731
|
+
|
|
732
|
+
// ---------- agent tool: read / outline / search any local document ----------
|
|
733
|
+
ctx.effect(() => ctx.tools.register({
|
|
734
|
+
name: 'doc_reader',
|
|
735
|
+
description: '读取本地文档:提取纯文本(read,分段+offset 续读)、标题大纲(outline)、单篇全文搜索(search)、跨最近打开的文档检索(search_docs)。支持 md/txt/log/json/yaml/csv/tsv/xml/ini/toml/html/epub/docx/pdf;回答涉及本地文档内容的问题时调用。',
|
|
736
|
+
parameters: {
|
|
737
|
+
type: 'object',
|
|
738
|
+
properties: {
|
|
739
|
+
action: { type: 'string', enum: ['read', 'outline', 'search', 'search_docs'], description: 'read=读取单篇文本;outline=单篇大纲;search=单篇搜索;search_docs=跨最近文档搜索(无需 path)' },
|
|
740
|
+
path: { type: 'string', description: '文档的绝对路径(search_docs 不需要)' },
|
|
741
|
+
offset: { type: 'number', description: 'read:起始字符偏移(默认 0,续读传上次返回的 offset)' },
|
|
742
|
+
limit: { type: 'number', description: 'read:最多返回字符数(默认 16000,上限 60000)' },
|
|
743
|
+
query: { type: 'string', description: 'search/search_docs:关键词(大小写不敏感)' },
|
|
744
|
+
maxMatches: { type: 'number', description: 'search:最多命中条数(默认 20,上限 50)' },
|
|
745
|
+
contextLines: { type: 'number', description: 'search:每条命中带几行上下文(默认 1,上限 3)' },
|
|
746
|
+
},
|
|
747
|
+
required: ['action'],
|
|
748
|
+
},
|
|
749
|
+
output: textOutput,
|
|
750
|
+
async execute(args) {
|
|
751
|
+
try {
|
|
752
|
+
return await runToolAction(runtime, args);
|
|
753
|
+
} catch (error) {
|
|
754
|
+
return `执行失败:${error?.message ?? error}`;
|
|
755
|
+
}
|
|
756
|
+
},
|
|
757
|
+
}), 'dsh-doc-reader: agent tool');
|
|
758
|
+
|
|
759
|
+
// ---------- system-prompt context: what the user is reading ----------
|
|
760
|
+
try {
|
|
761
|
+
ctx.effect(() => ctx.systemPrompt.context({
|
|
762
|
+
name: 'doc-reader:active',
|
|
763
|
+
order: 225, // just after weread's reading context (220)
|
|
764
|
+
text: () => runtime.contextText(),
|
|
765
|
+
}), 'dsh-doc-reader: prompt context');
|
|
766
|
+
} catch (error) {
|
|
767
|
+
ctx.logger.warn(`doc-reader systemPrompt registration failed: ${error?.message ?? error}`);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// ---------- human slash command: /doc ----------
|
|
771
|
+
try {
|
|
772
|
+
ctx.effect(() => ctx.commands.register({
|
|
773
|
+
name: 'doc',
|
|
774
|
+
description: '文档阅读器:打开最近文档(/doc 关键词);无参数列出最近 5 个',
|
|
775
|
+
input: { hint: '关键词(留空列出最近 5 个)' },
|
|
776
|
+
async handler(arg) {
|
|
777
|
+
if (!config.get().commandEnabled) return { kind: 'error', text: '命令已在插件配置中停用。' };
|
|
778
|
+
const q = typeof arg === 'string' ? arg : String(arg?.rawInput ?? '').trim();
|
|
779
|
+
if (!runtime.state.recent.length) {
|
|
780
|
+
return { kind: 'error', text: '最近文档为空:先在文档阅读器面板打开一个文件。' };
|
|
781
|
+
}
|
|
782
|
+
if (!q) {
|
|
783
|
+
const list = runtime.state.recent.slice(0, 5).map((e, i) => `${i + 1}. ${basename(e.path)}\n ${e.path}`).join('\n');
|
|
784
|
+
return { kind: 'success', text: `最近文档:\n${list}\n输入 /doc 名称 打开。` };
|
|
785
|
+
}
|
|
786
|
+
const lower = q.toLowerCase();
|
|
787
|
+
const hit = runtime.state.recent.find((e) => basename(e.path).toLowerCase().includes(lower))
|
|
788
|
+
?? runtime.state.recent.find((e) => e.path.toLowerCase().includes(lower));
|
|
789
|
+
if (!hit) {
|
|
790
|
+
const names = runtime.state.recent.slice(0, 5).map((e) => basename(e.path)).join('、');
|
|
791
|
+
return { kind: 'error', text: `最近文档中没有匹配「${q}」的条目。最近:${names}` };
|
|
792
|
+
}
|
|
793
|
+
runtime.broadcast({ type: 'open', path: hit.path });
|
|
794
|
+
return { kind: 'success', text: `已在文档阅读器打开:${hit.path}` };
|
|
795
|
+
},
|
|
796
|
+
}), 'dsh-doc-reader: /doc command');
|
|
797
|
+
} catch (error) {
|
|
798
|
+
ctx.logger.warn(`doc-reader command registration failed: ${error?.message ?? error}`);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
const textOutput = {
|
|
803
|
+
schema: { type: 'string' },
|
|
804
|
+
render: (_args, value) => [{ type: 'text', text: String(value) }],
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
let pluginRuntime = null; // set by apply(); test seam for captured registrations
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Cross-document search over the documents the reader has opened recently
|
|
811
|
+
* (plus the active one). Extraction reuses the cached engine, so this stays
|
|
812
|
+
* cheap after the first read of each file.
|
|
813
|
+
*/
|
|
814
|
+
async function searchRecentDocs(runtime, args) {
|
|
815
|
+
const query = String(args?.query ?? '').trim();
|
|
816
|
+
if (!query) return 'search_docs 需要非空 query。';
|
|
817
|
+
const lower = query.toLowerCase();
|
|
818
|
+
const paths = [...new Set([
|
|
819
|
+
runtime.state.active?.path,
|
|
820
|
+
...runtime.state.recent.map((e) => e.path),
|
|
821
|
+
].filter((p) => typeof p === 'string'))].slice(0, 12);
|
|
822
|
+
const groups = [];
|
|
823
|
+
let scanned = 0;
|
|
824
|
+
for (const path of paths) {
|
|
825
|
+
if (groups.length >= 8) break;
|
|
826
|
+
const resolved = await resolveReadable(path);
|
|
827
|
+
if (resolved.error) continue;
|
|
828
|
+
let doc;
|
|
829
|
+
try { doc = await extractDocument(resolved.file, resolved.info, resolved.ext); } catch { continue; }
|
|
830
|
+
scanned += 1;
|
|
831
|
+
const lines = doc.text.split('\n');
|
|
832
|
+
const hits = [];
|
|
833
|
+
for (let i = 0; i < lines.length && hits.length < 6; i++) {
|
|
834
|
+
if (lines[i].toLowerCase().includes(lower)) hits.push(i);
|
|
835
|
+
}
|
|
836
|
+
if (hits.length) groups.push({ file: resolved.file, lines, hits, format: doc.format.slice(1), bytes: doc.bytes });
|
|
837
|
+
}
|
|
838
|
+
if (!groups.length) return `在 ${scanned} 篇最近文档中搜索「${query}」:0 命中。`;
|
|
839
|
+
const body = groups.map((g) => {
|
|
840
|
+
const head = `${g.file}(${g.format},${formatBytes(g.bytes)})`;
|
|
841
|
+
const rows = g.hits.map((i) => `>>[L${i + 1}] ${g.lines[i]}`).join('\n');
|
|
842
|
+
return `${head}\n${rows}`;
|
|
843
|
+
}).join('\n---\n');
|
|
844
|
+
return `跨最近文档搜索「${query}」:${groups.length}/${scanned} 篇命中(每篇最多 6 条):\n${body}`;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
async function runToolAction(runtime, args) {
|
|
848
|
+
const cfg = runtime.config.get();
|
|
849
|
+
if (!cfg.toolEnabled) return 'doc_reader 工具已在插件配置中停用。';
|
|
850
|
+
const action = args?.action;
|
|
851
|
+
if (!['read', 'outline', 'search', 'search_docs'].includes(action)) return 'action 必须是 read / outline / search / search_docs。';
|
|
852
|
+
if (action === 'search_docs') return searchRecentDocs(runtime, args);
|
|
853
|
+
const resolved = await resolveReadable(args?.path);
|
|
854
|
+
if (resolved.error) return `无法读取:${resolved.error}(${args?.path ?? ''})`;
|
|
855
|
+
const doc = await extractDocument(resolved.file, resolved.info, resolved.ext);
|
|
856
|
+
const head = `${resolved.file}(${doc.format.slice(1)},${formatBytes(doc.bytes)}${doc.note ? `,${doc.note}` : ''})`;
|
|
857
|
+
if (action === 'outline') {
|
|
858
|
+
if (!doc.outline.length) return `${head}\n没有可用大纲(md 依赖 # 标题;epub 依赖目录;docx 依赖标题样式;pdf 依赖书签)。可用 read 阅读全文。`;
|
|
859
|
+
const cap = doc.outline.length > 100;
|
|
860
|
+
const lines = doc.outline.slice(0, 100)
|
|
861
|
+
.map((item) => `${' '.repeat(Math.max(0, item.level - 1))}- ${item.text}${item.line ? `(L${item.line})` : ''}`)
|
|
862
|
+
.join('\n');
|
|
863
|
+
return `${head}\n大纲(共 ${doc.outline.length} 条${cap ? ',仅显示前 100' : ''}):\n${lines}`;
|
|
864
|
+
}
|
|
865
|
+
if (action === 'search') {
|
|
866
|
+
const query = String(args?.query ?? '').trim();
|
|
867
|
+
if (!query) return 'search 需要非空 query。';
|
|
868
|
+
const maxMatches = Math.max(1, Math.min(50, Math.round(Number(args?.maxMatches) || 20)));
|
|
869
|
+
const contextLines = Math.max(0, Math.min(3, Math.round(Number(args?.contextLines ?? 1))));
|
|
870
|
+
const lines = doc.text.split('\n');
|
|
871
|
+
const lower = query.toLowerCase();
|
|
872
|
+
const hits = [];
|
|
873
|
+
for (let i = 0; i < lines.length && hits.length < maxMatches; i++) {
|
|
874
|
+
if (lines[i].toLowerCase().includes(lower)) hits.push(i);
|
|
875
|
+
}
|
|
876
|
+
const total = hits.length === maxMatches
|
|
877
|
+
? lines.reduce((n, line, i) => n + (i > hits[hits.length - 1] && line.toLowerCase().includes(lower) ? 1 : 0), maxMatches)
|
|
878
|
+
: hits.length;
|
|
879
|
+
if (!hits.length) return `${head}\n搜索「${query}」:0 条命中。`;
|
|
880
|
+
const shown = hits.map((i) => {
|
|
881
|
+
const from = Math.max(0, i - contextLines);
|
|
882
|
+
const to = Math.min(lines.length - 1, i + contextLines);
|
|
883
|
+
const block = [];
|
|
884
|
+
for (let j = from; j <= to; j++) block.push(`${j === i ? '>>' : ' '}[L${j + 1}] ${lines[j]}`);
|
|
885
|
+
return block.join('\n');
|
|
886
|
+
}).join('\n---\n');
|
|
887
|
+
return `${head}\n搜索「${query}」:${total} 条命中(显示前 ${hits.length}):\n${shown}`;
|
|
888
|
+
}
|
|
889
|
+
// read
|
|
890
|
+
const offset = Math.max(0, Math.round(Number(args?.offset) || 0));
|
|
891
|
+
const limit = Math.max(1, Math.min(60_000, Math.round(Number(args?.limit) || 16_000)));
|
|
892
|
+
const text = doc.text;
|
|
893
|
+
if (!text.trim()) return `${head}\n文档没有可提取的文本(可能是纯图片 PDF/空文件)。`;
|
|
894
|
+
if (offset >= text.length) return `${head}\noffset=${offset} 超出全文长度 ${text.length}。`;
|
|
895
|
+
const slice = text.slice(offset, offset + limit);
|
|
896
|
+
const rest = text.length - offset - slice.length;
|
|
897
|
+
return `${head}(全文 ${text.length} 字符)\n—— 内容 [${offset}, ${offset + slice.length}) ——\n${slice}`
|
|
898
|
+
+ (rest > 0 ? `\n—— 未完:剩余 ${rest} 字符,传 offset=${offset + slice.length} 继续 ——` : '\n—— 已到文末 ——');
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
async function handleRequest(ctx, req, res, runtime) {
|
|
902
|
+
if (!hostAllowed(req) || !requestAllowed(req)) return fail(res, 403, 'forbidden', req);
|
|
903
|
+
const url = new URL(req.url, 'http://dsh.invalid');
|
|
904
|
+
const tail = url.pathname.slice(ROUTE.length); // "/list" | "/file/<encoded>" | "/rename" | "/reveal" | ...
|
|
905
|
+
const kind = tail.replace(/^\//u, '').split('/')[0];
|
|
906
|
+
// config is read-write (GET reads, POST updates); handleConfig splits itself
|
|
907
|
+
const write = kind === 'rename' || kind === 'save' || kind === 'active' || kind === 'translate';
|
|
908
|
+
if (!write && kind !== 'config' && req.method !== 'GET' && req.method !== 'HEAD') {
|
|
909
|
+
res.writeHead(405, { allow: 'GET, HEAD' });
|
|
910
|
+
return res.end();
|
|
911
|
+
}
|
|
912
|
+
if (write && req.method !== 'POST') {
|
|
913
|
+
res.writeHead(405, { allow: 'POST' });
|
|
914
|
+
return res.end();
|
|
915
|
+
}
|
|
916
|
+
if (kind === 'list') return handleList(ctx, req, res, url);
|
|
917
|
+
if (kind === 'file') return handleFile(ctx, req, res, url);
|
|
918
|
+
if (kind === 'rename') return handleRename(ctx, req, res, runtime);
|
|
919
|
+
if (kind === 'save') return handleSave(ctx, req, res, runtime);
|
|
920
|
+
if (kind === 'reveal') return handleReveal(ctx, req, res, url);
|
|
921
|
+
if (kind === 'config') return handleConfig(ctx, req, res, runtime);
|
|
922
|
+
if (kind === 'active') return handleActive(ctx, req, res, runtime);
|
|
923
|
+
if (kind === 'convert') return handleConvert(ctx, req, res, url);
|
|
924
|
+
if (kind === 'translate') return handleTranslate(ctx, req, res);
|
|
925
|
+
if (kind === 'translate-models') {
|
|
926
|
+
if (req.method !== 'GET') { res.writeHead(405, { allow: 'GET' }); return res.end(); }
|
|
927
|
+
const catalog = await llmCatalog(ctx);
|
|
928
|
+
const payload = JSON.stringify(catalog);
|
|
929
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
930
|
+
return res.end(payload);
|
|
931
|
+
}
|
|
932
|
+
if (kind === 'events') return handleEvents(ctx, req, res, url, runtime);
|
|
933
|
+
return fail(res, 404, 'not found');
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
async function readJsonBody(req, limit = 64 * 1024) {
|
|
937
|
+
let size = 0;
|
|
938
|
+
const chunks = [];
|
|
939
|
+
for await (const chunk of req) {
|
|
940
|
+
size += chunk.length;
|
|
941
|
+
if (size > limit) throw new Error('body too large');
|
|
942
|
+
chunks.push(chunk);
|
|
943
|
+
}
|
|
944
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/**
|
|
948
|
+
* POST /save { path, baseContent, content } — save an edited textual document.
|
|
949
|
+
* Three-way conflict check like the VSCode extension: if the file on disk
|
|
950
|
+
* differs from BOTH the base the editor started from and the new content,
|
|
951
|
+
* someone else changed it and the caller must reload (409).
|
|
952
|
+
*/
|
|
953
|
+
async function handleSave(ctx, req, res, runtime) {
|
|
954
|
+
let body;
|
|
955
|
+
try {
|
|
956
|
+
body = await readJsonBody(req, MAX_SAVE_BODY);
|
|
957
|
+
} catch {
|
|
958
|
+
return fail(res, 400, 'invalid JSON body');
|
|
959
|
+
}
|
|
960
|
+
const { path: target, baseContent, content } = body ?? {};
|
|
961
|
+
if (typeof target !== 'string' || typeof content !== 'string' || typeof baseContent !== 'string') {
|
|
962
|
+
return fail(res, 400, 'path, baseContent and content required');
|
|
963
|
+
}
|
|
964
|
+
if (content.includes('\0')) return fail(res, 400, 'invalid content');
|
|
965
|
+
if (Buffer.byteLength(content, 'utf8') > MAX_TEXT_BYTES) return fail(res, 413, 'content too large');
|
|
966
|
+
const file = normalize(target);
|
|
967
|
+
if (!isAbsolute(file)) return fail(res, 400, 'absolute path required');
|
|
968
|
+
const ext = extensionOf(file);
|
|
969
|
+
if (!TEXTUAL.has(ext)) return fail(res, 403, 'only textual files are editable');
|
|
970
|
+
const { lstat, readFile, writeFile, rename } = await import('node:fs/promises');
|
|
971
|
+
const { basename, dirname } = await import('node:path');
|
|
972
|
+
// lstat: never follow a symlinked file (read or write) — the extension
|
|
973
|
+
// whitelist must apply to the real file, and writing must not punch through
|
|
974
|
+
// a link to an arbitrary target.
|
|
975
|
+
const info = await lstat(file).catch(() => null);
|
|
976
|
+
if (!info) return fail(res, 404, 'file not found');
|
|
977
|
+
if (info.isSymbolicLink()) return fail(res, 403, 'symbolic links are not editable');
|
|
978
|
+
if (!info.isFile()) return fail(res, 400, 'path is not a file');
|
|
979
|
+
if (info.size > MAX_TEXT_BYTES) return fail(res, 413, 'file too large');
|
|
980
|
+
const current = await readFile(file, 'utf8').catch(() => null);
|
|
981
|
+
if (current === null) return fail(res, 404, 'file not found');
|
|
982
|
+
if (current !== baseContent && current !== content) {
|
|
983
|
+
return fail(res, 409, 'file changed on disk — reopen it first');
|
|
984
|
+
}
|
|
985
|
+
if (current === content) {
|
|
986
|
+
const payload = JSON.stringify({ ok: true, unchanged: true });
|
|
987
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });
|
|
988
|
+
return res.end(payload);
|
|
989
|
+
}
|
|
990
|
+
// atomic write: temp file in the same directory + rename. A crash or race
|
|
991
|
+
// can never leave a truncated document behind, and rename replaces the
|
|
992
|
+
// path itself rather than following it.
|
|
993
|
+
const tmp = join(dirname(file), `.${basename(file)}.dsh-save-${process.pid}-${Date.now()}`);
|
|
994
|
+
await writeFile(tmp, content, 'utf8');
|
|
995
|
+
const { chmod } = await import('node:fs/promises');
|
|
996
|
+
await chmod(tmp, info.mode).catch(() => {}); // keep original permissions (0600 notes must not become 0644)
|
|
997
|
+
await rename(tmp, file);
|
|
998
|
+
runtime?.touchWatch(file); // our own save must not echo back as an external change
|
|
999
|
+
const payload = JSON.stringify({ ok: true });
|
|
1000
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });
|
|
1001
|
+
res.end(payload);
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* POST /rename { path, name } — rename a readable file within its directory,
|
|
1006
|
+
* keeping the extension. Mirrors the VSCode extension's rename semantics:
|
|
1007
|
+
* stem-only input (extension auto-stripped when retyped), no separators,
|
|
1008
|
+
* target extension must stay in the whitelist, no overwrite.
|
|
1009
|
+
*/
|
|
1010
|
+
async function handleRename(ctx, req, res) {
|
|
1011
|
+
let body;
|
|
1012
|
+
try {
|
|
1013
|
+
body = await readJsonBody(req);
|
|
1014
|
+
} catch {
|
|
1015
|
+
return fail(res, 400, 'invalid JSON body');
|
|
1016
|
+
}
|
|
1017
|
+
const { path: from, name } = body ?? {};
|
|
1018
|
+
if (typeof from !== 'string' || typeof name !== 'string') return fail(res, 400, 'path and name required');
|
|
1019
|
+
if (name.includes('\0')) return fail(res, 400, 'invalid name');
|
|
1020
|
+
const target = normalize(from);
|
|
1021
|
+
if (!isAbsolute(target)) return fail(res, 400, 'absolute path required');
|
|
1022
|
+
const ext = extensionOf(target);
|
|
1023
|
+
if (!READABLE.has(ext)) return fail(res, 403, 'unsupported file type');
|
|
1024
|
+
// strip the extension when the user retyped it
|
|
1025
|
+
let stem = name.trim();
|
|
1026
|
+
if (ext && stem.toLowerCase().endsWith(ext)) stem = stem.slice(0, -ext.length).trim();
|
|
1027
|
+
if (!stem) return fail(res, 400, 'name required');
|
|
1028
|
+
if (stem === '.' || stem === '..' || stem.includes('/') || stem.includes('\\')) return fail(res, 400, 'invalid name');
|
|
1029
|
+
const { basename, dirname } = await import('node:path');
|
|
1030
|
+
if (basename(stem) !== stem) return fail(res, 400, 'invalid name');
|
|
1031
|
+
if (Buffer.byteLength(`${stem}${ext}`, 'utf8') > 255) return fail(res, 400, 'name too long');
|
|
1032
|
+
const fromName = basename(target);
|
|
1033
|
+
const currentStem = ext && fromName.toLowerCase().endsWith(ext) ? fromName.slice(0, -ext.length) : fromName;
|
|
1034
|
+
if (stem === currentStem) return fail(res, 400, 'unchanged');
|
|
1035
|
+
const to = join(dirname(target), `${stem}${ext}`);
|
|
1036
|
+
const { lstat, link, unlink, rename, access } = await import('node:fs/promises');
|
|
1037
|
+
const info = await lstat(target).catch(() => null);
|
|
1038
|
+
if (!info) return fail(res, 404, 'file not found');
|
|
1039
|
+
if (info.isSymbolicLink()) return fail(res, 403, 'symbolic links are not editable');
|
|
1040
|
+
if (!info.isFile()) return fail(res, 400, 'path is not a file');
|
|
1041
|
+
// case-only rename on a case-insensitive volume needs a two-step hop
|
|
1042
|
+
// (link/EEXIST cannot distinguish "same file, different case" there)
|
|
1043
|
+
const caseOnly = (process.platform === 'darwin' || process.platform === 'win32') && to !== target && to.toLowerCase() === target.toLowerCase();
|
|
1044
|
+
if (caseOnly) {
|
|
1045
|
+
const hop = join(dirname(target), `.dsh-rename-${process.pid}-${Date.now()}${ext}`);
|
|
1046
|
+
await rename(target, hop);
|
|
1047
|
+
await rename(hop, to).catch(async (error) => { await rename(hop, target).catch(() => {}); throw error; });
|
|
1048
|
+
} else if (process.platform === 'win32') {
|
|
1049
|
+
try { await access(to); return fail(res, 409, 'target exists'); } catch { /* good: not exists */ }
|
|
1050
|
+
await rename(target, to);
|
|
1051
|
+
} else {
|
|
1052
|
+
// hardlink+unlink: kernel-atomic no-clobber rename (link() fails with
|
|
1053
|
+
// EEXIST if the target appeared, closing the access/rename TOCTOU gap)
|
|
1054
|
+
try {
|
|
1055
|
+
await link(target, to);
|
|
1056
|
+
} catch (error) {
|
|
1057
|
+
if (error?.code === 'EEXIST') return fail(res, 409, 'target exists');
|
|
1058
|
+
throw error;
|
|
1059
|
+
}
|
|
1060
|
+
await unlink(target).catch(async (error) => { await unlink(to).catch(() => {}); throw error; });
|
|
1061
|
+
}
|
|
1062
|
+
const payload = JSON.stringify({ ok: true, path: to });
|
|
1063
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });
|
|
1064
|
+
res.end(payload);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/** GET /reveal?path=<encoded> — reveal a file or its parent directory in the OS file manager. */
|
|
1068
|
+
async function handleReveal(ctx, req, res, url) {
|
|
1069
|
+
const raw = url.searchParams.get('path'); // already percent-decoded by URLSearchParams
|
|
1070
|
+
const target = normalize(raw ?? '');
|
|
1071
|
+
if (!target) return fail(res, 400, 'path required');
|
|
1072
|
+
if (!isAbsolute(target)) return fail(res, 400, 'absolute path required');
|
|
1073
|
+
const info = await stat(target).catch(() => undefined);
|
|
1074
|
+
if (!info) return fail(res, 404, 'path not found', req);
|
|
1075
|
+
const { spawn } = await import('node:child_process');
|
|
1076
|
+
// Always reveal, never launch: bare `open <dir>` would *execute* bundle
|
|
1077
|
+
// directories such as Foo.app. `open -R` selects in the parent window for
|
|
1078
|
+
// files and directories alike; explorer /select, does the same on Windows.
|
|
1079
|
+
const child = process.platform === 'win32'
|
|
1080
|
+
? spawn('explorer', [`/select,${target}`], { stdio: 'ignore' })
|
|
1081
|
+
: spawn('open', ['-R', target], { stdio: 'ignore' });
|
|
1082
|
+
child.on('spawn', () => {
|
|
1083
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });
|
|
1084
|
+
res.end(JSON.stringify({ ok: true }));
|
|
1085
|
+
});
|
|
1086
|
+
child.on('error', (error) => fail(res, 500, `reveal failed: ${error.message}`));
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
async function handleList(ctx, req, res, url) {
|
|
1090
|
+
const raw = url.searchParams.get('path'); // already percent-decoded by URLSearchParams
|
|
1091
|
+
const dir = raw === null || raw === '' ? (await import('node:os')).homedir() : normalize(raw);
|
|
1092
|
+
if (!isAbsolute(dir)) return fail(res, 400, 'absolute path required');
|
|
1093
|
+
const info = await stat(dir).catch(() => undefined);
|
|
1094
|
+
if (!info) return fail(res, 404, 'directory not found');
|
|
1095
|
+
if (!info.isDirectory()) return fail(res, 400, 'path is not a directory');
|
|
1096
|
+
const { readdir } = await import('node:fs/promises');
|
|
1097
|
+
const showHidden = url.searchParams.get('hidden') === '1';
|
|
1098
|
+
const all = await readdir(dir).catch(() => null);
|
|
1099
|
+
if (all === null) return fail(res, 403, 'cannot read directory');
|
|
1100
|
+
const names = all.filter((name) => showHidden || !name.startsWith('.'));
|
|
1101
|
+
const truncated = names.length > MAX_LIST;
|
|
1102
|
+
const entries = [];
|
|
1103
|
+
for (const name of names.slice(0, MAX_LIST)) {
|
|
1104
|
+
let childInfo;
|
|
1105
|
+
try {
|
|
1106
|
+
childInfo = await lstat(join(dir, name));
|
|
1107
|
+
} catch {
|
|
1108
|
+
continue; // broken symlink or racing deletion
|
|
1109
|
+
}
|
|
1110
|
+
if (childInfo.isSymbolicLink()) continue; // whitelist must apply to real files only
|
|
1111
|
+
const isDir = childInfo.isDirectory();
|
|
1112
|
+
const ext = isDir ? '' : extensionOf(name);
|
|
1113
|
+
if (!LISTABLE.has(ext)) continue;
|
|
1114
|
+
entries.push({ name, path: join(dir, name), directory: isDir, size: isDir ? null : childInfo.size });
|
|
1115
|
+
}
|
|
1116
|
+
entries.sort((a, b) => (b.directory - a.directory) || a.name.localeCompare(b.name));
|
|
1117
|
+
const body = JSON.stringify({ path: dir, entries, truncated });
|
|
1118
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1119
|
+
res.end(req.method === 'HEAD' ? undefined : body);
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
async function handleFile(ctx, req, res, url) {
|
|
1123
|
+
const encoded = url.pathname.slice(ROUTE.length + '/file/'.length);
|
|
1124
|
+
let target;
|
|
1125
|
+
try {
|
|
1126
|
+
target = normalize(decodeURIComponent(encoded));
|
|
1127
|
+
} catch {
|
|
1128
|
+
return fail(res, 400, 'bad path encoding');
|
|
1129
|
+
}
|
|
1130
|
+
if (!isAbsolute(target)) return fail(res, 400, 'absolute path required');
|
|
1131
|
+
const ext = extensionOf(target);
|
|
1132
|
+
if (!READABLE.has(ext)) return fail(res, 403, `unsupported extension: ${ext || '(none)'}`);
|
|
1133
|
+
// lstat: the extension whitelist must apply to the file itself — a symlink
|
|
1134
|
+
// named notes.md pointing at ~/.ssh/id_ed25519 must not be readable.
|
|
1135
|
+
const info = await lstat(target).catch(() => undefined);
|
|
1136
|
+
if (!info) return fail(res, 404, 'file not found');
|
|
1137
|
+
if (info.isSymbolicLink()) return fail(res, 403, 'symbolic links are not readable');
|
|
1138
|
+
if (!info.isFile()) return fail(res, 400, 'path is not a file');
|
|
1139
|
+
if (TEXTUAL.has(ext) && info.size > MAX_TEXT_BYTES) return fail(res, 413, 'text file too large');
|
|
1140
|
+
if ((ext === '.html' || ext === '.htm' || ext === '.epub') && info.size > MAX_DOC_BYTES) return fail(res, 413, 'document too large');
|
|
1141
|
+
const type = CONTENT_TYPES.get(ext) ?? 'application/octet-stream';
|
|
1142
|
+
res.writeHead(200, {
|
|
1143
|
+
'content-type': type,
|
|
1144
|
+
'content-length': info.size,
|
|
1145
|
+
'cache-control': 'no-store',
|
|
1146
|
+
'x-content-type-options': 'nosniff',
|
|
1147
|
+
'content-security-policy': DOC_CSP,
|
|
1148
|
+
});
|
|
1149
|
+
if (req.method === 'HEAD') return res.end();
|
|
1150
|
+
createReadStream(target).on('error', () => res.destroy()).pipe(res);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
/** GET/POST /config — plugin-owned settings edited from the plugin row config UI. */
|
|
1154
|
+
async function handleConfig(ctx, req, res, runtime) {
|
|
1155
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
1156
|
+
const body = JSON.stringify(runtime.config.get());
|
|
1157
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1158
|
+
return res.end(body);
|
|
1159
|
+
}
|
|
1160
|
+
let body;
|
|
1161
|
+
try {
|
|
1162
|
+
body = await readJsonBody(req);
|
|
1163
|
+
} catch {
|
|
1164
|
+
return fail(res, 400, 'invalid JSON body');
|
|
1165
|
+
}
|
|
1166
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) return fail(res, 400, 'object body required');
|
|
1167
|
+
const updated = runtime.updateConfig(body);
|
|
1168
|
+
const payload = JSON.stringify(updated);
|
|
1169
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1170
|
+
res.end(payload);
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
/** POST /active { path } — the reader reports the document it just opened. */
|
|
1174
|
+
async function handleActive(ctx, req, res, runtime) {
|
|
1175
|
+
let body;
|
|
1176
|
+
try {
|
|
1177
|
+
body = await readJsonBody(req);
|
|
1178
|
+
} catch {
|
|
1179
|
+
return fail(res, 400, 'invalid JSON body');
|
|
1180
|
+
}
|
|
1181
|
+
const resolved = await resolveReadable(body?.path);
|
|
1182
|
+
if (resolved.error) return fail(res, 400, resolved.error);
|
|
1183
|
+
runtime.setActive(resolved.file);
|
|
1184
|
+
const payload = JSON.stringify({ ok: true, active: resolved.file, recent: runtime.state.recent.length });
|
|
1185
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1186
|
+
res.end(payload);
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* GET /events[?path=<encoded>] — Server-Sent Events stream.
|
|
1191
|
+
* Every client receives global {type:'open'} broadcasts (slash command); a
|
|
1192
|
+
* client that names a path also receives {type:'change'} when that file
|
|
1193
|
+
* changes on disk (external edit), suppressed for this plugin's own saves.
|
|
1194
|
+
*/
|
|
1195
|
+
/** Split text for translation on paragraph boundaries, hard-cut as a fallback. */
|
|
1196
|
+
function splitForTranslate(text, max) {
|
|
1197
|
+
if (text.length <= max) return [text];
|
|
1198
|
+
const paragraphs = String(text).split(/\n{2,}/u);
|
|
1199
|
+
const chunks = [];
|
|
1200
|
+
let current = '';
|
|
1201
|
+
for (const para of paragraphs) {
|
|
1202
|
+
const candidate = current ? `${current}\n\n${para}` : para;
|
|
1203
|
+
if (candidate.length <= max) { current = candidate; continue; }
|
|
1204
|
+
if (current) { chunks.push(current); current = ''; }
|
|
1205
|
+
if (para.length <= max) { current = para; continue; }
|
|
1206
|
+
for (let i = 0; i < para.length; i += max) chunks.push(para.slice(i, i + max));
|
|
1207
|
+
}
|
|
1208
|
+
if (current) chunks.push(current);
|
|
1209
|
+
return chunks.length ? chunks : [''];
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/** Discover the DSH LLM provider/model catalog for the translate settings. */
|
|
1213
|
+
let llmCatalogCache = { at: 0, value: null };
|
|
1214
|
+
async function llmCatalog(ctx) {
|
|
1215
|
+
if (llmCatalogCache.value && Date.now() - llmCatalogCache.at < 60_000) return llmCatalogCache.value;
|
|
1216
|
+
let providers = [];
|
|
1217
|
+
const llm = typeof ctx.get === 'function' ? ctx.get('llm') : undefined;
|
|
1218
|
+
if (llm && typeof llm.listProviders === 'function') {
|
|
1219
|
+
try {
|
|
1220
|
+
const names = await llm.listProviders();
|
|
1221
|
+
providers = await Promise.all((Array.isArray(names) ? names : []).map(async (entry) => {
|
|
1222
|
+
const name = typeof entry === 'string' ? entry : entry?.name ?? entry?.id;
|
|
1223
|
+
if (!name) return null;
|
|
1224
|
+
let models = [];
|
|
1225
|
+
try {
|
|
1226
|
+
if (typeof llm.listModels === 'function') {
|
|
1227
|
+
const listed = await llm.listModels(name);
|
|
1228
|
+
models = (Array.isArray(listed) ? listed : [])
|
|
1229
|
+
.map((m) => (typeof m === 'string' ? m : m?.id ?? m?.model))
|
|
1230
|
+
.filter((m) => typeof m === 'string' && m);
|
|
1231
|
+
}
|
|
1232
|
+
} catch { /* catalog is optional */ }
|
|
1233
|
+
return { name, models };
|
|
1234
|
+
}));
|
|
1235
|
+
providers = providers.filter(Boolean);
|
|
1236
|
+
} catch { /* no providers */ }
|
|
1237
|
+
}
|
|
1238
|
+
llmCatalogCache = { at: Date.now(), value: { providers } };
|
|
1239
|
+
return llmCatalogCache.value;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
/** Anonymous Google translate_a/single endpoint (the "gtx" web client).
|
|
1243
|
+
* Any compatible reverse proxy base URL can be supplied by the user. */
|
|
1244
|
+
const GOOGLE_TL = { 'zh-Hans': 'zh-CN', 'zh-Hant': 'zh-TW' };
|
|
1245
|
+
async function translateWithGoogle(text, targetLanguage, endpoint) {
|
|
1246
|
+
const base = (endpoint || 'https://translate.googleapis.com').replace(/\/+$/u, '');
|
|
1247
|
+
const tl = GOOGLE_TL[targetLanguage] || targetLanguage;
|
|
1248
|
+
// keep the whole request URL comfortably below transport limits
|
|
1249
|
+
const chunks = splitForTranslate(text, 1200);
|
|
1250
|
+
const parts = [];
|
|
1251
|
+
for (const chunk of chunks) {
|
|
1252
|
+
const url = `${base}/translate_a/single?client=gtx&sl=auto&tl=${encodeURIComponent(tl)}&dt=t&q=${encodeURIComponent(chunk)}`;
|
|
1253
|
+
const res = await fetch(url, { headers: { 'user-agent': 'Mozilla/5.0' }, signal: AbortSignal.timeout(15_000) });
|
|
1254
|
+
if (!res.ok) throw new Error(`Google 端点返回 ${res.status}`);
|
|
1255
|
+
const data = await res.json().catch(() => null);
|
|
1256
|
+
const segments = Array.isArray(data?.[0]) ? data[0] : null;
|
|
1257
|
+
if (!segments) throw new Error('Google 端点返回了无法解析的内容');
|
|
1258
|
+
const out = segments.map((seg) => (Array.isArray(seg) && typeof seg[0] === 'string' ? seg[0] : '')).join('');
|
|
1259
|
+
if (!out.trim()) throw new Error('Google 端点未返回译文');
|
|
1260
|
+
parts.push(out);
|
|
1261
|
+
}
|
|
1262
|
+
return parts.join('\n\n');
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
/** Translate through the configured DSH model (ctx.llm.stream). */
|
|
1266
|
+
async function translateWithLlm(ctx, text, targetLanguage, provider, model) {
|
|
1267
|
+
const llm = typeof ctx.get === 'function' ? ctx.get('llm') : undefined;
|
|
1268
|
+
if (!llm || typeof llm.stream !== 'function') throw new Error('翻译服务不可用(DSH LLM 服务缺席)');
|
|
1269
|
+
const catalog = await llmCatalog(ctx);
|
|
1270
|
+
if (!provider) provider = catalog.providers[0]?.name;
|
|
1271
|
+
if (!model) model = catalog.providers.find((p) => p.name === provider)?.models[0];
|
|
1272
|
+
if (!provider || !model) throw new Error('未找到可用的 DSH 模型(请在设置中选择)');
|
|
1273
|
+
const languageName = { 'zh-Hans': '简体中文', 'zh-Hant': '繁体中文', en: 'English', ja: '日本語', ko: '한국어', fr: 'Français', de: 'Deutsch', es: 'Español' }[targetLanguage] || targetLanguage;
|
|
1274
|
+
// adversarial: a stream that never yields (hung provider) must not hang the
|
|
1275
|
+
// request — and the accumulating output — forever. Env var is a test seam.
|
|
1276
|
+
const timeoutMs = Number(process.env.DOC_READER_LLM_TIMEOUT_MS) || 90_000;
|
|
1277
|
+
const max = 4000;
|
|
1278
|
+
const parts = [];
|
|
1279
|
+
for (const chunk of splitForTranslate(text, max)) {
|
|
1280
|
+
const prompt = `你是一位专业翻译。把用户提供的文本翻译成${languageName}。只输出译文本身,不要任何解释、注释或原文。如果文本已经是${languageName},原样输出。\n\n<待翻译文本>\n${chunk}\n</待翻译文本>`;
|
|
1281
|
+
let out = '';
|
|
1282
|
+
let failure = null;
|
|
1283
|
+
const iterator = llm.stream({
|
|
1284
|
+
provider, model,
|
|
1285
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: prompt }] }],
|
|
1286
|
+
})[Symbol.asyncIterator]();
|
|
1287
|
+
let pendingTimer = null;
|
|
1288
|
+
try {
|
|
1289
|
+
for (;;) {
|
|
1290
|
+
const next = await Promise.race([
|
|
1291
|
+
iterator.next(),
|
|
1292
|
+
new Promise((_, reject) => { pendingTimer = setTimeout(() => reject(new Error(`模型响应超时(${Math.round(timeoutMs / 1000)}s)`)), timeoutMs); }),
|
|
1293
|
+
]);
|
|
1294
|
+
clearTimeout(pendingTimer);
|
|
1295
|
+
if (next.done) break;
|
|
1296
|
+
const chunkEvent = next.value;
|
|
1297
|
+
if (!chunkEvent || typeof chunkEvent !== 'object') continue;
|
|
1298
|
+
if (chunkEvent.type === 'text-delta' && typeof chunkEvent.text === 'string') out += chunkEvent.text;
|
|
1299
|
+
else if (chunkEvent.type === 'finish') failure = chunkEvent.error?.message ?? null;
|
|
1300
|
+
}
|
|
1301
|
+
} finally {
|
|
1302
|
+
clearTimeout(pendingTimer);
|
|
1303
|
+
iterator.return?.().catch?.(() => {});
|
|
1304
|
+
}
|
|
1305
|
+
if (!out.trim()) throw new Error(failure || '模型未返回译文');
|
|
1306
|
+
parts.push(out.trim());
|
|
1307
|
+
}
|
|
1308
|
+
return parts.join('\n\n');
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
/** POST /translate { text, targetLanguage? } — optional ctx.translator bridge. */
|
|
1312
|
+
async function handleTranslate(ctx, req, res) {
|
|
1313
|
+
let body;
|
|
1314
|
+
try {
|
|
1315
|
+
body = await readJsonBody(req, 256 * 1024);
|
|
1316
|
+
} catch {
|
|
1317
|
+
return fail(res, 400, 'invalid JSON body');
|
|
1318
|
+
}
|
|
1319
|
+
const text = String(body?.text ?? '');
|
|
1320
|
+
const targetLanguage = String(body?.targetLanguage || 'zh-Hans');
|
|
1321
|
+
if (!text.trim()) return fail(res, 400, 'text required');
|
|
1322
|
+
if (text.length > 200_000) return fail(res, 413, 'text too large');
|
|
1323
|
+
// adversarial: targetLanguage flows into URLs and prompts — whitelist it
|
|
1324
|
+
// instead of trusting the client, and the custom Google endpoint base URL
|
|
1325
|
+
// must be http(s) (blocks file:// and other exotic schemes from fetch).
|
|
1326
|
+
const LANGS = new Set(['zh-Hans', 'zh-Hant', 'en', 'ja', 'ko', 'fr', 'de', 'es']);
|
|
1327
|
+
if (!LANGS.has(targetLanguage)) return fail(res, 400, 'unsupported targetLanguage');
|
|
1328
|
+
if (body?.endpoint !== undefined && body?.endpoint !== '' && !/^https?:\/\/\S+$/u.test(String(body.endpoint))) {
|
|
1329
|
+
return fail(res, 400, 'endpoint must be an http(s) URL');
|
|
1330
|
+
}
|
|
1331
|
+
if (body?.engine === 'google') {
|
|
1332
|
+
try {
|
|
1333
|
+
const translated = await translateWithGoogle(text, targetLanguage, typeof body?.endpoint === 'string' ? body.endpoint : '');
|
|
1334
|
+
const payload = JSON.stringify({ ok: true, text: translated, engine: 'google' });
|
|
1335
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1336
|
+
return res.end(payload);
|
|
1337
|
+
} catch (error) {
|
|
1338
|
+
return fail(res, 502, error?.message ?? String(error));
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
if (body?.engine === 'dsh') {
|
|
1342
|
+
try {
|
|
1343
|
+
const translated = await translateWithLlm(ctx, text, targetLanguage, body?.provider, body?.model);
|
|
1344
|
+
const payload = JSON.stringify({ ok: true, text: translated, engine: 'dsh' });
|
|
1345
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1346
|
+
return res.end(payload);
|
|
1347
|
+
} catch (error) {
|
|
1348
|
+
return fail(res, 502, error?.message ?? String(error));
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
const translator = typeof ctx.get === 'function' ? ctx.get('translator') : undefined;
|
|
1352
|
+
if (!translator || typeof translator.resolve !== 'function' || typeof translator.translate !== 'function') {
|
|
1353
|
+
return fail(res, 503, '翻译服务不可用(experimental-translator 未挂载)');
|
|
1354
|
+
}
|
|
1355
|
+
try {
|
|
1356
|
+
const max = Math.max(1, Number(translator.maxTextChars) || 4000);
|
|
1357
|
+
const chunks = splitForTranslate(text, max);
|
|
1358
|
+
const parts = [];
|
|
1359
|
+
for (const chunk of chunks) {
|
|
1360
|
+
const spec = await translator.resolve({ text: chunk, targetLanguage });
|
|
1361
|
+
parts.push(await translator.translate(spec));
|
|
1362
|
+
}
|
|
1363
|
+
const payload = JSON.stringify({ ok: true, text: parts.join('\n\n') });
|
|
1364
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1365
|
+
res.end(payload);
|
|
1366
|
+
} catch (error) {
|
|
1367
|
+
return fail(res, 502, `翻译失败:${error?.message ?? error}`);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
/** GET /convert?path=<encoded> — office document → cached PDF (libreoffice-kit). */
|
|
1372
|
+
async function handleConvert(ctx, req, res, url) {
|
|
1373
|
+
const raw = url.searchParams.get('path');
|
|
1374
|
+
const resolved = await resolveReadable(raw);
|
|
1375
|
+
if (resolved.error) return fail(res, 400, resolved.error);
|
|
1376
|
+
if (!OFFICE.has(resolved.ext)) return fail(res, 400, 'not an office document');
|
|
1377
|
+
try {
|
|
1378
|
+
const pdfPath = await convertToPdf(resolved.file, resolved.info);
|
|
1379
|
+
const payload = JSON.stringify({ ok: true, pdfPath });
|
|
1380
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1381
|
+
return res.end(payload);
|
|
1382
|
+
} catch (error) {
|
|
1383
|
+
return fail(res, 502, error?.message ?? 'conversion failed');
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
async function handleEvents(ctx, req, res, url, runtime) {
|
|
1388
|
+
if (req.method === 'HEAD') return fail(res, 405, 'GET only', req);
|
|
1389
|
+
let watchPath = null;
|
|
1390
|
+
const raw = url.searchParams.get('path');
|
|
1391
|
+
if (raw) {
|
|
1392
|
+
const resolved = await resolveReadable(raw);
|
|
1393
|
+
if (resolved.error) return fail(res, 400, resolved.error);
|
|
1394
|
+
watchPath = resolved.file;
|
|
1395
|
+
}
|
|
1396
|
+
// adversarial: cap concurrent SSE streams — a leaky client (or a script in
|
|
1397
|
+
// the app's own origin) must not be able to pile up unbounded connections
|
|
1398
|
+
const SSE_CLIENT_CAP = 16;
|
|
1399
|
+
if (runtime.sseClients.size >= SSE_CLIENT_CAP) return fail(res, 503, 'too many event streams');
|
|
1400
|
+
res.writeHead(200, {
|
|
1401
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
1402
|
+
'cache-control': 'no-store',
|
|
1403
|
+
'x-accel-buffering': 'no',
|
|
1404
|
+
'connection': 'keep-alive',
|
|
1405
|
+
});
|
|
1406
|
+
res.write('retry: 3000\n\n');
|
|
1407
|
+
const client = { res, watchPath };
|
|
1408
|
+
runtime.sseClients.add(client);
|
|
1409
|
+
if (watchPath && runtime.config.get().autoReloadEnabled) runtime.ensureWatch(watchPath);
|
|
1410
|
+
req.on('close', () => {
|
|
1411
|
+
runtime.sseClients.delete(client);
|
|
1412
|
+
if (watchPath) runtime.releaseWatch(watchPath);
|
|
1413
|
+
});
|
|
1414
|
+
}
|