pi-quiver 4.2.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +11 -7
- package/dist/bin/pi-quiver.js +521 -13
- package/extensions/doc_to_md.ts +4 -382
- package/extensions/fetch.ts +1 -1
- package/lib/doc-to-md-core.ts +591 -0
- package/lib/fetch-core.ts +33 -6
- package/package.json +1 -1
package/extensions/doc_to_md.ts
CHANGED
|
@@ -10,354 +10,18 @@
|
|
|
10
10
|
* content is returned inline.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
14
|
-
import { spawn } from "node:child_process";
|
|
15
|
-
import { tmpdir } from "node:os";
|
|
16
|
-
import { basename, extname, join, resolve } from "node:path";
|
|
17
|
-
import { createHash } from "node:crypto";
|
|
18
|
-
import { fileURLToPath } from "node:url";
|
|
19
|
-
import { extractText, getDocumentProxy } from "unpdf";
|
|
20
13
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
21
14
|
import { formatSize, keyHint } from "@earendil-works/pi-coding-agent";
|
|
22
15
|
import { Text } from "@earendil-works/pi-tui";
|
|
23
16
|
import { Type } from "@sinclair/typebox";
|
|
24
|
-
|
|
25
|
-
// --- Types ---
|
|
26
|
-
|
|
27
|
-
export type InputType = "pdf" | "docx" | "pptx";
|
|
28
|
-
export type Engine = "pymupdf4llm" | "unpdf";
|
|
29
|
-
|
|
30
|
-
export interface DocToMdConfig {
|
|
31
|
-
pymupdfVersion: string;
|
|
32
|
-
warmTimeoutMs: number;
|
|
33
|
-
convertTimeoutMs: number;
|
|
34
|
-
sofficeTimeoutMs: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface DocToMdDetails {
|
|
38
|
-
path: string;
|
|
39
|
-
inputType: InputType;
|
|
40
|
-
engine: Engine;
|
|
41
|
-
degraded: boolean;
|
|
42
|
-
bytes: number;
|
|
43
|
-
lines: number;
|
|
44
|
-
spilled: boolean;
|
|
45
|
-
file?: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface CappedResult {
|
|
49
|
-
stdout: string;
|
|
50
|
-
stderr: string;
|
|
51
|
-
code: number | null;
|
|
52
|
-
timedOut: boolean;
|
|
53
|
-
capped: boolean;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// --- Constants ---
|
|
57
|
-
|
|
58
|
-
const DEFAULT_PYMUPDF_VERSION = "1.27.2.3";
|
|
59
|
-
const WARM_TIMEOUT_DEFAULT = 120_000;
|
|
60
|
-
const CONVERT_TIMEOUT_DEFAULT = 60_000;
|
|
61
|
-
const SOFFICE_TIMEOUT_DEFAULT = 120_000;
|
|
62
|
-
const STDERR_CAP = 1_000_000;
|
|
63
|
-
const INLINE_MAX_BYTES = 32_000;
|
|
64
|
-
const INLINE_MAX_LINES = 1_000;
|
|
65
|
-
const PREVIEW_LINES = 60;
|
|
66
|
-
const PREVIEW_MAX_BYTES = 4_000;
|
|
67
|
-
const VERSION_RE = /^\d+(\.\d+)*$/;
|
|
68
|
-
const PAGE_SEP = "\n\n---\n\n";
|
|
69
|
-
export const OUTPUT_MAX_BYTES = 20_000_000;
|
|
70
|
-
|
|
71
|
-
export const DEGRADED_MARKER =
|
|
72
|
-
"[Note: degraded extraction via unpdf — structure (tables/headings) not preserved]";
|
|
73
|
-
|
|
74
|
-
// --- Config ---
|
|
75
|
-
|
|
76
|
-
export function parseConfig(env: NodeJS.ProcessEnv): DocToMdConfig {
|
|
77
|
-
const version = env.PI_DOC_TO_MD_PYMUPDF_VERSION ?? DEFAULT_PYMUPDF_VERSION;
|
|
78
|
-
if (!VERSION_RE.test(version)) {
|
|
79
|
-
throw new Error(`PI_DOC_TO_MD_PYMUPDF_VERSION must be digits and dots (got "${version}")`);
|
|
80
|
-
}
|
|
81
|
-
const num = (key: string, def: number): number => {
|
|
82
|
-
const raw = env[key];
|
|
83
|
-
if (raw === undefined) return def;
|
|
84
|
-
const n = Number.parseInt(raw, 10);
|
|
85
|
-
if (!Number.isInteger(n) || n <= 0) throw new Error(`${key} must be a positive integer (got "${raw}")`);
|
|
86
|
-
return n;
|
|
87
|
-
};
|
|
88
|
-
return {
|
|
89
|
-
pymupdfVersion: version,
|
|
90
|
-
warmTimeoutMs: num("PI_DOC_TO_MD_WARM_TIMEOUT_MS", WARM_TIMEOUT_DEFAULT),
|
|
91
|
-
convertTimeoutMs: num("PI_DOC_TO_MD_CONVERT_TIMEOUT_MS", CONVERT_TIMEOUT_DEFAULT),
|
|
92
|
-
sofficeTimeoutMs: num("PI_DOC_TO_MD_SOFFICE_TIMEOUT_MS", SOFFICE_TIMEOUT_DEFAULT),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// --- Input classification ---
|
|
97
|
-
|
|
98
|
-
const SUPPORTED: Record<string, InputType> = { ".pdf": "pdf", ".docx": "docx", ".pptx": "pptx" };
|
|
99
|
-
|
|
100
|
-
export function classifyInput(filePath: string): InputType {
|
|
101
|
-
const ext = extname(filePath).toLowerCase();
|
|
102
|
-
const t = SUPPORTED[ext];
|
|
103
|
-
if (!t) throw new Error(`Unsupported file type "${ext || "(none)"}"; supported: .pdf, .docx, .pptx`);
|
|
104
|
-
return t;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// --- Size gate ---
|
|
108
|
-
|
|
109
|
-
export function applyGate(body: string): { spill: boolean; bytes: number; lines: number } {
|
|
110
|
-
const bytes = Buffer.byteLength(body, "utf8");
|
|
111
|
-
const lines = body.length ? body.split("\n").length : 0;
|
|
112
|
-
const spill = body.length > 0 && (bytes > INLINE_MAX_BYTES || lines > INLINE_MAX_LINES);
|
|
113
|
-
return { spill, bytes, lines };
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// --- Markdown helpers ---
|
|
117
|
-
|
|
118
|
-
export function withMarker(md: string, degraded: boolean): string {
|
|
119
|
-
return degraded ? `${DEGRADED_MARKER}\n\n${md}` : md;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function pagesToMarkdown(pages: string[]): string {
|
|
123
|
-
return pages.map((p) => p.trim()).filter((p) => p.length > 0).join(PAGE_SEP);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// --- Temp file helpers ---
|
|
127
|
-
|
|
128
|
-
export function tempFilePath(inputPath: string, ext: string): string {
|
|
129
|
-
const dir = join(tmpdir(), "pi-doc-to-md");
|
|
130
|
-
mkdirSync(dir, { recursive: true });
|
|
131
|
-
const base = basename(inputPath).replace(/\.[^.]+$/, "").replace(/[^\w.-]+/g, "-");
|
|
132
|
-
const hash = createHash("sha1").update(resolve(inputPath)).digest("hex").slice(0, 8);
|
|
133
|
-
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
134
|
-
return join(dir, `${stamp}-${base}-${hash}.${ext}`);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export function spillToFile(inputPath: string, body: string): string {
|
|
138
|
-
const file = tempFilePath(inputPath, "md");
|
|
139
|
-
writeFileSync(file, body, "utf8");
|
|
140
|
-
return file;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export function buildPreview(body: string): string {
|
|
144
|
-
let preview = body.split("\n").slice(0, PREVIEW_LINES).join("\n");
|
|
145
|
-
if (preview.length > PREVIEW_MAX_BYTES) {
|
|
146
|
-
preview = `${preview.slice(0, PREVIEW_MAX_BYTES)}\n…[preview truncated]`;
|
|
147
|
-
}
|
|
148
|
-
return preview;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// --- Subprocess argv builders ---
|
|
152
|
-
|
|
153
|
-
export function warmArgs(cfg: DocToMdConfig): string[] {
|
|
154
|
-
return ["run", "--with", `pymupdf4llm==${cfg.pymupdfVersion}`, "--python", "3.14", "python", "-c", "import pymupdf4llm"];
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export function convertArgs(cfg: DocToMdConfig, scriptPath: string, pdfPath: string): string[] {
|
|
158
|
-
return ["run", "--with", `pymupdf4llm==${cfg.pymupdfVersion}`, "--python", "3.14", "python", scriptPath, pdfPath];
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export function soffArgs(src: string, profileDir: string, outDir: string): string[] {
|
|
162
|
-
return [
|
|
163
|
-
"--headless", "--invisible", "--nocrashreport", "--nodefault", "--nofirststartwizard",
|
|
164
|
-
"--nolockcheck", "--nologo", "--norestore", "--quickstart=no",
|
|
165
|
-
`-env:UserInstallation=file://${profileDir}`,
|
|
166
|
-
"--convert-to", "pdf", "--outdir", outDir, src,
|
|
167
|
-
];
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// --- Subprocess runner ---
|
|
171
|
-
|
|
172
|
-
export async function runCapped(
|
|
173
|
-
cmd: string,
|
|
174
|
-
args: string[],
|
|
175
|
-
opts: { timeoutMs: number; capBytes: number; env?: NodeJS.ProcessEnv; signal?: AbortSignal },
|
|
176
|
-
): Promise<CappedResult> {
|
|
177
|
-
return new Promise((resolveP) => {
|
|
178
|
-
let settled = false;
|
|
179
|
-
let timedOut = false;
|
|
180
|
-
let capped = false;
|
|
181
|
-
const stdoutChunks: Buffer[] = [];
|
|
182
|
-
const stderrChunks: Buffer[] = [];
|
|
183
|
-
let stdoutBytes = 0;
|
|
184
|
-
let stderrBytes = 0;
|
|
185
|
-
|
|
186
|
-
const child = spawn(cmd, args, { env: opts.env ?? process.env });
|
|
187
|
-
|
|
188
|
-
const finish = (code: number | null) => {
|
|
189
|
-
if (settled) return;
|
|
190
|
-
settled = true;
|
|
191
|
-
clearTimeout(timer);
|
|
192
|
-
opts.signal?.removeEventListener("abort", onAbort);
|
|
193
|
-
resolveP({
|
|
194
|
-
stdout: Buffer.concat(stdoutChunks).toString("utf8"),
|
|
195
|
-
stderr: Buffer.concat(stderrChunks).toString("utf8"),
|
|
196
|
-
code,
|
|
197
|
-
timedOut,
|
|
198
|
-
capped,
|
|
199
|
-
});
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
const kill = () => child.kill("SIGKILL");
|
|
203
|
-
|
|
204
|
-
const onAbort = () => { kill(); };
|
|
205
|
-
if (opts.signal?.aborted) { kill(); }
|
|
206
|
-
else opts.signal?.addEventListener("abort", onAbort);
|
|
207
|
-
|
|
208
|
-
const timer = setTimeout(() => { timedOut = true; kill(); }, opts.timeoutMs);
|
|
209
|
-
|
|
210
|
-
child.stdout.on("data", (chunk: Buffer) => {
|
|
211
|
-
if (capped) return;
|
|
212
|
-
const remaining = opts.capBytes - stdoutBytes;
|
|
213
|
-
if (remaining <= 0) { capped = true; kill(); return; }
|
|
214
|
-
if (chunk.length > remaining) {
|
|
215
|
-
stdoutChunks.push(chunk.subarray(0, remaining));
|
|
216
|
-
stdoutBytes += remaining;
|
|
217
|
-
capped = true;
|
|
218
|
-
kill();
|
|
219
|
-
} else {
|
|
220
|
-
stdoutChunks.push(chunk);
|
|
221
|
-
stdoutBytes += chunk.length;
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
child.stderr.on("data", (chunk: Buffer) => {
|
|
226
|
-
if (stderrBytes >= STDERR_CAP) return;
|
|
227
|
-
const remaining = STDERR_CAP - stderrBytes;
|
|
228
|
-
const slice = chunk.length > remaining ? chunk.subarray(0, remaining) : chunk;
|
|
229
|
-
stderrChunks.push(slice);
|
|
230
|
-
stderrBytes += slice.length;
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
child.on("error", (err) => {
|
|
234
|
-
if (stderrBytes < STDERR_CAP) {
|
|
235
|
-
const msg = Buffer.from(err.message);
|
|
236
|
-
const remaining = STDERR_CAP - stderrBytes;
|
|
237
|
-
stderrChunks.push(msg.length > remaining ? msg.subarray(0, remaining) : msg);
|
|
238
|
-
}
|
|
239
|
-
finish(null);
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
child.on("close", (code) => finish(code));
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// --- Engine orchestration ---
|
|
247
|
-
|
|
248
|
-
let pymupdfState: "unknown" | "warm" | "unavailable" = "unknown";
|
|
249
|
-
let warmPromise: Promise<"warm" | "unavailable"> | null = null;
|
|
250
|
-
let uvAvailable: boolean | null = null;
|
|
251
|
-
|
|
252
|
-
function scriptPath(): string {
|
|
253
|
-
return fileURLToPath(new URL("../scripts/pdf_to_md.py", import.meta.url));
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
async function onPath(cmd: string): Promise<boolean> {
|
|
257
|
-
const r = await runCapped("which", [cmd], { timeoutMs: 5000, capBytes: 4000 });
|
|
258
|
-
return r.code === 0;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
async function warmPymupdf(cfg: DocToMdConfig): Promise<"warm" | "unavailable"> {
|
|
262
|
-
if (pymupdfState !== "unknown") return pymupdfState;
|
|
263
|
-
if (warmPromise === null) {
|
|
264
|
-
warmPromise = (async () => {
|
|
265
|
-
const r = await runCapped("uv", warmArgs(cfg), { timeoutMs: cfg.warmTimeoutMs, capBytes: OUTPUT_MAX_BYTES });
|
|
266
|
-
pymupdfState = (r.code === 0 && !r.timedOut) ? "warm" : "unavailable";
|
|
267
|
-
return pymupdfState;
|
|
268
|
-
})();
|
|
269
|
-
}
|
|
270
|
-
return warmPromise;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
async function convertViaPymupdf(cfg: DocToMdConfig, pdfPath: string, signal?: AbortSignal): Promise<string> {
|
|
274
|
-
const r = await runCapped("uv", convertArgs(cfg, scriptPath(), pdfPath), {
|
|
275
|
-
timeoutMs: cfg.convertTimeoutMs, capBytes: OUTPUT_MAX_BYTES, signal,
|
|
276
|
-
});
|
|
277
|
-
if (r.timedOut || r.capped || r.code !== 0) {
|
|
278
|
-
throw new Error(`pymupdf4llm convert failed (code=${r.code} timedOut=${r.timedOut} capped=${r.capped}): ${r.stderr.slice(0, 500)}`);
|
|
279
|
-
}
|
|
280
|
-
return r.stdout;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
async function convertViaUnpdf(pdfPath: string, cfg: DocToMdConfig, signal?: AbortSignal): Promise<string> {
|
|
284
|
-
if (signal?.aborted) throw new Error("aborted");
|
|
285
|
-
const buf = readFileSync(pdfPath);
|
|
286
|
-
const work = (async () => {
|
|
287
|
-
const pdf = await getDocumentProxy(new Uint8Array(buf));
|
|
288
|
-
const { text } = await extractText(pdf, { mergePages: false });
|
|
289
|
-
const md = pagesToMarkdown(text);
|
|
290
|
-
if (Buffer.byteLength(md, "utf8") > OUTPUT_MAX_BYTES) throw new Error("unpdf output exceeded cap");
|
|
291
|
-
return md;
|
|
292
|
-
})();
|
|
293
|
-
let timer: NodeJS.Timeout;
|
|
294
|
-
const timeout = new Promise<never>((_, rej) => {
|
|
295
|
-
timer = setTimeout(() => rej(new Error("unpdf conversion timed out")), cfg.convertTimeoutMs);
|
|
296
|
-
});
|
|
297
|
-
try { return await Promise.race([work, timeout]); } finally { clearTimeout(timer!); }
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
async function convertOffice(cfg: DocToMdConfig, src: string): Promise<{ pdfPath: string; cleanup: () => void }> {
|
|
301
|
-
const profileDir = mkdtempSync(join(tmpdir(), "pi-doc-soffice-prof-"));
|
|
302
|
-
const outDir = mkdtempSync(join(tmpdir(), "pi-doc-soffice-out-"));
|
|
303
|
-
const cleanup = () => {
|
|
304
|
-
for (const d of [profileDir, outDir]) {
|
|
305
|
-
try { rmSync(d, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
try {
|
|
309
|
-
const env = { ...process.env, SAL_USE_VCLPLUGIN: "svp", OOO_DISABLE_RECOVERY: "1", SAL_NO_MOUSEGRABS: "1" };
|
|
310
|
-
const r = await runCapped("soffice", soffArgs(src, profileDir, outDir), { timeoutMs: cfg.sofficeTimeoutMs, capBytes: OUTPUT_MAX_BYTES, env });
|
|
311
|
-
if (r.timedOut || r.code !== 0) throw new Error(`soffice failed (code=${r.code} timedOut=${r.timedOut}): ${r.stderr.slice(0, 500)}`);
|
|
312
|
-
const base = basename(src).replace(/\.[^.]+$/, "");
|
|
313
|
-
const pdfPath = join(outDir, `${base}.pdf`);
|
|
314
|
-
const st = statSync(pdfPath, { throwIfNoEntry: false });
|
|
315
|
-
if (!st || !st.isFile() || st.size === 0) throw new Error("soffice produced no usable PDF");
|
|
316
|
-
return { pdfPath, cleanup };
|
|
317
|
-
} catch (e) { cleanup(); throw e; }
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
export async function runPipeline(
|
|
321
|
-
cfg: DocToMdConfig,
|
|
322
|
-
inputPath: string,
|
|
323
|
-
type: InputType,
|
|
324
|
-
signal?: AbortSignal,
|
|
325
|
-
): Promise<{ markdown: string; engine: Engine; degraded: boolean; fallbackReason?: string }> {
|
|
326
|
-
let pdfPath = inputPath;
|
|
327
|
-
let cleanup: (() => void) | null = null;
|
|
328
|
-
if (type === "docx" || type === "pptx") {
|
|
329
|
-
const o = await convertOffice(cfg, inputPath);
|
|
330
|
-
pdfPath = o.pdfPath;
|
|
331
|
-
cleanup = o.cleanup;
|
|
332
|
-
}
|
|
333
|
-
try {
|
|
334
|
-
if (uvAvailable === null) uvAvailable = await onPath("uv");
|
|
335
|
-
if (!uvAvailable) {
|
|
336
|
-
return { markdown: await convertViaUnpdf(pdfPath, cfg, signal), engine: "unpdf", degraded: true, fallbackReason: "uv not on PATH" };
|
|
337
|
-
}
|
|
338
|
-
const state = await warmPymupdf(cfg);
|
|
339
|
-
if (state === "warm") {
|
|
340
|
-
try {
|
|
341
|
-
return { markdown: await convertViaPymupdf(cfg, pdfPath, signal), engine: "pymupdf4llm", degraded: false };
|
|
342
|
-
} catch (e) {
|
|
343
|
-
const fallbackReason = e instanceof Error ? e.message : String(e);
|
|
344
|
-
return { markdown: await convertViaUnpdf(pdfPath, cfg, signal), engine: "unpdf", degraded: true, fallbackReason };
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
return { markdown: await convertViaUnpdf(pdfPath, cfg, signal), engine: "unpdf", degraded: true, fallbackReason: "pymupdf4llm engine unavailable (warm probe failed)" };
|
|
348
|
-
} finally {
|
|
349
|
-
cleanup?.();
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// --- Extension entry point ---
|
|
17
|
+
import { convertDocument, parseConfig, type DocToMdDetails } from "../lib/doc-to-md-core.ts";
|
|
354
18
|
|
|
355
19
|
export default function docToMdExtension(pi: ExtensionAPI) {
|
|
356
20
|
pi.registerTool({
|
|
357
21
|
name: "doc_to_md",
|
|
358
22
|
label: "Convert doc to Markdown",
|
|
359
23
|
description:
|
|
360
|
-
"Convert a local PDF/DOCX/PPTX file to Markdown. High-fidelity conversion via pymupdf4llm
|
|
24
|
+
"Convert a local PDF/DOCX/PPTX file to Markdown. High-fidelity conversion via pymupdf4llm, resolved per process: uv (pinned, isolated) when available, else a system Python >= 3.12 with pymupdf4llm already installed, else a one-time managed venv bootstrapped into the user cache dir; falls back to a degraded pure-JS text extractor (unpdf) when no capable Python exists or conversion fails. DOCX/PPTX require LibreOffice (soffice) for the office->PDF step. Output over 32KB or 1000 lines is written to a temp .md file with a preview instead of inlined - grep it or read with offset/limit. A degraded result (marked in the output) means the fallback ran: tables and headings are NOT faithfully preserved, treat structure with suspicion. Input must be a local file path (use fetch first for URLs).",
|
|
361
25
|
promptSnippet: "Convert a local PDF/DOCX/PPTX to Markdown",
|
|
362
26
|
parameters: Type.Object({
|
|
363
27
|
path: Type.String({ description: "Local path to a .pdf, .docx, or .pptx file" }),
|
|
@@ -365,50 +29,8 @@ export default function docToMdExtension(pi: ExtensionAPI) {
|
|
|
365
29
|
|
|
366
30
|
async execute(_toolCallId, params, signal) {
|
|
367
31
|
const cfg = parseConfig(process.env);
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
if (!st || !st.isFile()) throw new Error(`Not a readable file: ${params.path}`);
|
|
371
|
-
const type = classifyInput(inputPath);
|
|
372
|
-
if (type !== "pdf") {
|
|
373
|
-
if (!(await onPath("soffice"))) {
|
|
374
|
-
throw new Error("LibreOffice (soffice) is required to convert .docx/.pptx but was not found on PATH. Install LibreOffice or convert the file to PDF first.");
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
const { markdown, engine, degraded, fallbackReason } = await runPipeline(cfg, inputPath, type, signal);
|
|
378
|
-
const body = withMarker(markdown, degraded);
|
|
379
|
-
const { spill, bytes, lines } = applyGate(body);
|
|
380
|
-
const details: DocToMdDetails = { path: inputPath, inputType: type, engine, degraded, bytes, lines, spilled: spill };
|
|
381
|
-
|
|
382
|
-
const header: string[] = [
|
|
383
|
-
`Source: ${inputPath}`,
|
|
384
|
-
`Type: ${type} Engine: ${engine}${degraded ? " (degraded fallback)" : ""}`,
|
|
385
|
-
];
|
|
386
|
-
if (fallbackReason) header.push(`Fallback-Reason: ${fallbackReason}`);
|
|
387
|
-
|
|
388
|
-
if (!spill) {
|
|
389
|
-
return {
|
|
390
|
-
content: [{ type: "text" as const, text: [...header, `Length: ${bytes} bytes, ${lines} lines`, "", body].join("\n") }],
|
|
391
|
-
details: { ...details, spilled: false },
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
const file = spillToFile(inputPath, body);
|
|
396
|
-
return {
|
|
397
|
-
content: [{
|
|
398
|
-
type: "text" as const,
|
|
399
|
-
text: [
|
|
400
|
-
...header,
|
|
401
|
-
`Body: ${formatSize(bytes)} across ${lines} lines — written to file (too large to inline)`,
|
|
402
|
-
`Saved-To: ${file}`,
|
|
403
|
-
"",
|
|
404
|
-
"Read slices of this file with the read tool (offset/limit) or grep it; do not read the whole file unless you must. Markdown is grep-able by heading (^#).",
|
|
405
|
-
"",
|
|
406
|
-
"----- preview (first 60 lines) -----",
|
|
407
|
-
buildPreview(body),
|
|
408
|
-
].join("\n"),
|
|
409
|
-
}],
|
|
410
|
-
details: { ...details, spilled: true, file },
|
|
411
|
-
};
|
|
32
|
+
const { output, details } = await convertDocument(params.path, cfg, signal);
|
|
33
|
+
return { content: [{ type: "text" as const, text: output }], details };
|
|
412
34
|
},
|
|
413
35
|
|
|
414
36
|
renderCall(args, theme, _context) {
|
package/extensions/fetch.ts
CHANGED
|
@@ -21,7 +21,7 @@ export default function fetchExtension(pi: ExtensionAPI) {
|
|
|
21
21
|
name: "fetch",
|
|
22
22
|
label: "Fetch URL",
|
|
23
23
|
description:
|
|
24
|
-
"Fetch a URL over HTTP(S). HTML is extracted to Markdown (readability + turndown). Binary content (images, PDFs, archives) is saved untouched to a temp file and only a path is returned. Text/Markdown/JSON over 32KB or 1000 lines is written to a temp file with a 60-line preview; smaller content is returned inline. Parsable downloads are capped at 1MB, binary at 50MB. When the body is written to a file, grep it or read with offset/limit; converted Markdown is grep-able by heading (^#). GitHub issue/PR/repo/actions-run URLs are served via the gh CLI when available (falls back to HTTP otherwise; raw=true forces the rendered HTML page).",
|
|
24
|
+
"Fetch a URL over HTTP(S). HTML is extracted to Markdown (readability + turndown). Binary content (images, PDFs, archives) is saved untouched to a temp file and only a path is returned. Text/Markdown/JSON over 32KB or 1000 lines is written to a temp file with a 60-line preview; smaller content is returned inline. Parsable downloads are capped at 1MB, binary at 50MB. When the body is written to a file, grep it or read with offset/limit; converted Markdown is grep-able by heading (^#). GitHub issue/PR/repo/actions-run/actions-job URLs are served via the gh CLI when available (falls back to HTTP otherwise; raw=true forces the rendered HTML page); failed runs/jobs append failed-step logs (best-effort, summary-only when nothing failed).",
|
|
25
25
|
promptSnippet: "Fetch the contents of a URL",
|
|
26
26
|
parameters: Type.Object({
|
|
27
27
|
url: Type.String({ description: "Absolute http(s) URL" }),
|