@picassohq/cli 0.0.0-stage → 1.0.2
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/agents/browser_agent.yaml +53 -0
- package/agents/code_reviewer.yaml +49 -0
- package/agents/codebase_investigator.yaml +70 -0
- package/agents/design_auditor.yaml +50 -0
- package/agents/devex_auditor.yaml +50 -0
- package/agents/doc_writer.yaml +54 -0
- package/agents/engineering_architect.yaml +51 -0
- package/agents/generalist.yaml +55 -0
- package/agents/migration_planner.yaml +51 -0
- package/agents/product_reviewer.yaml +48 -0
- package/agents/qa_operator.yaml +52 -0
- package/agents/release_engineer.yaml +50 -0
- package/agents/security_auditor.yaml +62 -0
- package/agents/test_writer.yaml +52 -0
- package/assets/first-light-lockup.png +0 -0
- package/assets/first-light-tagline.png +0 -0
- package/assets/house/README.md +26 -0
- package/assets/house/auraone-banner.png +0 -0
- package/assets/house/auraone-logo.png +0 -0
- package/assets/house/kiro-prism-banner.png +0 -0
- package/assets/house/kiro-prism-logo.png +0 -0
- package/assets/house/picasso-banner.png +0 -0
- package/assets/house/picasso-logo.png +0 -0
- package/assets/wordmark-app.png +0 -0
- package/assets/wordmark-tight.png +0 -0
- package/assets/wordmark.png +0 -0
- package/design/styles.css +1042 -0
- package/dist/build-info.json +37 -0
- package/dist/index.js +3062 -0
- package/goals/cli-hardening.yaml +40 -0
- package/goals/repo-maintenance.yaml +40 -0
- package/package.json +49 -4
- package/runtime/dsh/picasso-ecosystem.community.amendments.yml +24 -0
- package/runtime/dsh/picasso-ecosystem.community.json +185 -0
- package/runtime/dsh/picasso-ecosystem.community.patch.yml +40 -0
- package/runtime/dsh/picasso-ecosystem.cordis.patch.yml +143 -0
- package/runtime/dsh/picasso-isolation.patch.yml +25 -0
- package/runtime/dsh/picasso-providers.patch.yml +373 -0
- package/runtime/dsh/picasso-runtime.cordis.yml +738 -0
- package/runtime/dsh/picasso-sdk.patch.yml +204 -0
- package/runtime/engine-manifest.json +215 -0
- package/runtime/package.json +6 -0
- package/runtime/packages/picasso-cache-probe/cordis.patch.yml +5 -0
- package/runtime/packages/picasso-cache-probe/index.js +477 -0
- package/runtime/packages/picasso-cache-probe/package.json +38 -0
- package/runtime/packages/picasso-commands/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-commands/fresh-review.js +573 -0
- package/runtime/packages/picasso-commands/index.js +497 -0
- package/runtime/packages/picasso-commands/package.json +28 -0
- package/runtime/packages/picasso-instructions/conflicts.js +115 -0
- package/runtime/packages/picasso-instructions/formats.js +145 -0
- package/runtime/packages/picasso-instructions/frontmatter.js +175 -0
- package/runtime/packages/picasso-instructions/glob.js +114 -0
- package/runtime/packages/picasso-instructions/globals.js +90 -0
- package/runtime/packages/picasso-instructions/imports.js +123 -0
- package/runtime/packages/picasso-instructions/index.js +326 -0
- package/runtime/packages/picasso-instructions/modes.js +64 -0
- package/runtime/packages/picasso-instructions/package.json +30 -0
- package/runtime/packages/picasso-instructions/parse.js +134 -0
- package/runtime/packages/picasso-instructions/report.js +161 -0
- package/runtime/packages/picasso-instructions/resolve.js +939 -0
- package/runtime/packages/picasso-instructions/scan.js +181 -0
- package/runtime/packages/picasso-instructions/settings.js +150 -0
- package/runtime/packages/picasso-instructions/skills.js +345 -0
- package/runtime/packages/picasso-lean-context/cordis.patch.yml +13 -0
- package/runtime/packages/picasso-lean-context/index.js +6280 -0
- package/runtime/packages/picasso-lean-context/package.json +26 -0
- package/runtime/packages/picasso-lean-context/read-image-recovery.js +306 -0
- package/runtime/packages/picasso-lean-context/timeline.js +418 -0
- package/runtime/packages/picasso-librarian/classify.js +146 -0
- package/runtime/packages/picasso-librarian/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-librarian/index.js +878 -0
- package/runtime/packages/picasso-librarian/notes.js +159 -0
- package/runtime/packages/picasso-librarian/package.json +34 -0
- package/runtime/packages/picasso-librarian/state.js +478 -0
- package/runtime/packages/picasso-memory/index.js +1184 -0
- package/runtime/packages/picasso-memory/package.json +21 -0
- package/runtime/packages/picasso-model-router/connections.js +912 -0
- package/runtime/packages/picasso-model-router/cordis.patch.yml +17 -0
- package/runtime/packages/picasso-model-router/index.js +1083 -0
- package/runtime/packages/picasso-model-router/package.json +30 -0
- package/runtime/packages/picasso-options/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-options/index.js +263 -0
- package/runtime/packages/picasso-options/package.json +28 -0
- package/runtime/packages/picasso-progress-reports/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-progress-reports/index.js +267 -0
- package/runtime/packages/picasso-progress-reports/package.json +30 -0
- package/runtime/packages/picasso-progress-reports/report.js +235 -0
- package/runtime/packages/picasso-sdk-server/aside.js +142 -0
- package/runtime/packages/picasso-sdk-server/connections.js +38 -0
- package/runtime/packages/picasso-sdk-server/context-timeline.js +27 -0
- package/runtime/packages/picasso-sdk-server/egress-gate.js +520 -0
- package/runtime/packages/picasso-sdk-server/failure-policy.js +246 -0
- package/runtime/packages/picasso-sdk-server/file-input.js +169 -0
- package/runtime/packages/picasso-sdk-server/goal.js +131 -0
- package/runtime/packages/picasso-sdk-server/ignore-file.js +458 -0
- package/runtime/packages/picasso-sdk-server/ignore-plugin.js +12 -0
- package/runtime/packages/picasso-sdk-server/ignore-sandbox.js +122 -0
- package/runtime/packages/picasso-sdk-server/ignore-snapshot.js +131 -0
- package/runtime/packages/picasso-sdk-server/image-input.js +44 -0
- package/runtime/packages/picasso-sdk-server/index.js +891 -0
- package/runtime/packages/picasso-sdk-server/instructions.js +34 -0
- package/runtime/packages/picasso-sdk-server/jobs.js +113 -0
- package/runtime/packages/picasso-sdk-server/mcp-gate.js +51 -0
- package/runtime/packages/picasso-sdk-server/model-selection.js +162 -0
- package/runtime/packages/picasso-sdk-server/package.json +50 -0
- package/runtime/packages/picasso-sdk-server/picasso-records.js +50 -0
- package/runtime/packages/picasso-sdk-server/provider-reload.js +84 -0
- package/runtime/packages/picasso-sdk-server/queue.js +322 -0
- package/runtime/packages/picasso-sdk-server/requester.js +43 -0
- package/runtime/packages/picasso-sdk-server/rewind.js +108 -0
- package/runtime/packages/picasso-sdk-server/schedule.js +187 -0
- package/runtime/packages/picasso-sdk-server/session-grants.js +35 -0
- package/runtime/packages/picasso-sdk-server/session-state.js +118 -0
- package/runtime/packages/picasso-sdk-server/shell-paths.js +869 -0
- package/runtime/packages/picasso-sdk-server/stream-deltas.js +211 -0
- package/runtime/packages/picasso-sdk-server/team-tasks.js +451 -0
- package/runtime/packages/picasso-sdk-server/tool-output.js +128 -0
- package/runtime/packages/picasso-sdk-server/tools-list.js +51 -0
- package/runtime/packages/picasso-sdk-server/wave2-hooks.js +612 -0
- package/runtime/packages/picasso-sdk-server/workspace-archive.js +170 -0
- package/runtime/packages/picasso-sdk-server/workspace-restore.js +180 -0
- package/runtime/packages/picasso-web-search/backends.js +412 -0
- package/runtime/packages/picasso-web-search/config.js +121 -0
- package/runtime/packages/picasso-web-search/cordis.patch.yml +7 -0
- package/runtime/packages/picasso-web-search/failure.js +116 -0
- package/runtime/packages/picasso-web-search/format.js +42 -0
- package/runtime/packages/picasso-web-search/index.js +71 -0
- package/runtime/packages/picasso-web-search/package.json +38 -0
- package/runtime/packages/picasso-web-search/router.js +156 -0
- package/runtime/packages/picasso-x-reader/circle.js +324 -0
- package/runtime/packages/picasso-x-reader/command.js +169 -0
- package/runtime/packages/picasso-x-reader/cordis.patch.yml +8 -0
- package/runtime/packages/picasso-x-reader/index.js +170 -0
- package/runtime/packages/picasso-x-reader/package.json +39 -0
- package/runtime/packages/picasso-x-reader/read.js +370 -0
- package/runtime/packages/picasso-x-reader/rewrite.js +38 -0
- package/runtime/packages/picasso-x-reader/settings.js +131 -0
- package/runtime/packages/picasso-x-reader/urls.js +154 -0
- package/runtime/packages/picasso-x-reader/xmd.js +512 -0
- package/runtime/plugins/picasso-auth-bridge/index.js +380 -0
- package/runtime/plugins/picasso-auth-bridge/package.json +28 -0
- package/runtime/plugins/picasso-auto-approve/index.js +1003 -0
- package/runtime/plugins/picasso-auto-approve/package.json +28 -0
- package/runtime/plugins/picasso-browser/index.js +424 -0
- package/runtime/plugins/picasso-browser/package.json +28 -0
- package/runtime/plugins/picasso-checkpoints/evidence.js +210 -0
- package/runtime/plugins/picasso-checkpoints/index.js +885 -0
- package/runtime/plugins/picasso-checkpoints/package.json +29 -0
- package/runtime/plugins/picasso-commands/fresh-review.js +573 -0
- package/runtime/plugins/picasso-commands/index.js +497 -0
- package/runtime/plugins/picasso-commands/package.json +28 -0
- package/runtime/plugins/picasso-instructions/conflicts.js +115 -0
- package/runtime/plugins/picasso-instructions/formats.js +145 -0
- package/runtime/plugins/picasso-instructions/frontmatter.js +175 -0
- package/runtime/plugins/picasso-instructions/glob.js +114 -0
- package/runtime/plugins/picasso-instructions/globals.js +90 -0
- package/runtime/plugins/picasso-instructions/imports.js +123 -0
- package/runtime/plugins/picasso-instructions/index.js +326 -0
- package/runtime/plugins/picasso-instructions/modes.js +64 -0
- package/runtime/plugins/picasso-instructions/package.json +30 -0
- package/runtime/plugins/picasso-instructions/parse.js +134 -0
- package/runtime/plugins/picasso-instructions/report.js +161 -0
- package/runtime/plugins/picasso-instructions/resolve.js +939 -0
- package/runtime/plugins/picasso-instructions/scan.js +181 -0
- package/runtime/plugins/picasso-instructions/settings.js +150 -0
- package/runtime/plugins/picasso-instructions/skills.js +345 -0
- package/runtime/plugins/picasso-lean-context/index.js +6280 -0
- package/runtime/plugins/picasso-lean-context/package.json +26 -0
- package/runtime/plugins/picasso-lean-context/read-image-recovery.js +306 -0
- package/runtime/plugins/picasso-lean-context/timeline.js +418 -0
- package/runtime/plugins/picasso-librarian/classify.js +146 -0
- package/runtime/plugins/picasso-librarian/index.js +878 -0
- package/runtime/plugins/picasso-librarian/notes.js +159 -0
- package/runtime/plugins/picasso-librarian/package.json +34 -0
- package/runtime/plugins/picasso-librarian/state.js +478 -0
- package/runtime/plugins/picasso-lifecycle-hooks/index.js +186 -0
- package/runtime/plugins/picasso-lifecycle-hooks/package.json +25 -0
- package/runtime/plugins/picasso-mail/inbox.js +207 -0
- package/runtime/plugins/picasso-mail/index.js +293 -0
- package/runtime/plugins/picasso-mail/package.json +22 -0
- package/runtime/plugins/picasso-memory/index.js +1184 -0
- package/runtime/plugins/picasso-memory/package.json +21 -0
- package/runtime/plugins/picasso-model-router/connections.js +912 -0
- package/runtime/plugins/picasso-model-router/index.js +1083 -0
- package/runtime/plugins/picasso-model-router/package.json +30 -0
- package/runtime/plugins/picasso-options/index.js +263 -0
- package/runtime/plugins/picasso-options/package.json +28 -0
- package/runtime/plugins/picasso-progress-reports/index.js +267 -0
- package/runtime/plugins/picasso-progress-reports/package.json +30 -0
- package/runtime/plugins/picasso-progress-reports/report.js +235 -0
- package/runtime/plugins/picasso-provider-models/index.js +436 -0
- package/runtime/plugins/picasso-provider-models/package.json +28 -0
- package/runtime/plugins/picasso-repo-index/index.js +1850 -0
- package/runtime/plugins/picasso-repo-index/package.json +28 -0
- package/runtime/plugins/picasso-schedule-pause/index.js +344 -0
- package/runtime/plugins/picasso-schedule-pause/package.json +28 -0
- package/runtime/plugins/picasso-session-lifecycle/index.js +796 -0
- package/runtime/plugins/picasso-session-lifecycle/package.json +28 -0
- package/runtime/plugins/picasso-trackers/index.js +895 -0
- package/runtime/plugins/picasso-trackers/package.json +28 -0
- package/runtime/plugins/picasso-web-search/backends.js +412 -0
- package/runtime/plugins/picasso-web-search/config.js +121 -0
- package/runtime/plugins/picasso-web-search/failure.js +116 -0
- package/runtime/plugins/picasso-web-search/format.js +42 -0
- package/runtime/plugins/picasso-web-search/index.js +71 -0
- package/runtime/plugins/picasso-web-search/package.json +38 -0
- package/runtime/plugins/picasso-web-search/router.js +156 -0
- package/runtime/plugins/picasso-x-reader/circle.js +324 -0
- package/runtime/plugins/picasso-x-reader/command.js +169 -0
- package/runtime/plugins/picasso-x-reader/index.js +170 -0
- package/runtime/plugins/picasso-x-reader/package.json +39 -0
- package/runtime/plugins/picasso-x-reader/read.js +370 -0
- package/runtime/plugins/picasso-x-reader/rewrite.js +38 -0
- package/runtime/plugins/picasso-x-reader/settings.js +131 -0
- package/runtime/plugins/picasso-x-reader/urls.js +154 -0
- package/runtime/plugins/picasso-x-reader/xmd.js +512 -0
- package/runtime/relay/attachment-text.mjs +737 -0
- package/runtime/relay/companion/companion.css +126 -0
- package/runtime/relay/companion/companion.js +648 -0
- package/runtime/relay/companion/icon.svg +5 -0
- package/runtime/relay/companion/index.html +25 -0
- package/runtime/relay/companion/manifest.json +13 -0
- package/runtime/relay/companion/sw.js +41 -0
- package/runtime/relay/main.mjs +38580 -0
- package/runtime/relay/model-listing.mjs +614 -0
- package/runtime/relay/versions-routes.mjs +1400 -0
- package/runtime/verify-plugins.mjs +205 -0
- package/runtime-lock/package-lock.json +10779 -0
- package/runtime-lock/package.json +7 -0
- package/scripts/install-runtime.mjs +400 -0
- package/README.md +0 -3
|
@@ -0,0 +1,737 @@
|
|
|
1
|
+
import { createRequire as __picassoCreateRequire } from "node:module";
|
|
2
|
+
globalThis.require ??= __picassoCreateRequire(import.meta.url);
|
|
3
|
+
|
|
4
|
+
// src/attachment-text.ts
|
|
5
|
+
import { execFile } from "node:child_process";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import { lstat, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { basename, join } from "node:path";
|
|
11
|
+
import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
|
|
12
|
+
|
|
13
|
+
// src/ooxml-text.ts
|
|
14
|
+
import { inflateRawSync } from "node:zlib";
|
|
15
|
+
var OfficeReadError = class extends Error {
|
|
16
|
+
code;
|
|
17
|
+
reason;
|
|
18
|
+
params;
|
|
19
|
+
constructor(code, reason, message, params) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "OfficeReadError";
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
this.params = params;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var EOCD = 101010256;
|
|
28
|
+
var CENTRAL = 33639248;
|
|
29
|
+
var LOCAL = 67324752;
|
|
30
|
+
var OFFICE_DEADLINE = { at: Number.POSITIVE_INFINITY, seconds: 20 };
|
|
31
|
+
var deadlineMessage = (seconds) => `Reading this file took longer than ${Math.round(seconds)} seconds, so Picasso stopped.`;
|
|
32
|
+
var unreadable = (reason, message, params) => new OfficeReadError("attachment/unreadable", reason, message, params);
|
|
33
|
+
var tooLarge = (reason, message, params) => new OfficeReadError("attachment/too-large", reason, message, params);
|
|
34
|
+
function openZip(buffer, limits, label = "document") {
|
|
35
|
+
if (buffer.length < 22) throw unreadable("document-damaged", `This ${label} is damaged: it is not a valid zip container.`);
|
|
36
|
+
let eocd = -1;
|
|
37
|
+
for (let at2 = buffer.length - 22; at2 >= Math.max(0, buffer.length - 22 - 65535); at2 -= 1) {
|
|
38
|
+
if (buffer.readUInt32LE(at2) === EOCD) {
|
|
39
|
+
eocd = at2;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (eocd < 0) throw unreadable("document-damaged", `This ${label} is damaged: it is not a valid zip container.`);
|
|
44
|
+
const total = buffer.readUInt16LE(eocd + 10);
|
|
45
|
+
const centralSize = buffer.readUInt32LE(eocd + 12);
|
|
46
|
+
const centralStart = buffer.readUInt32LE(eocd + 16);
|
|
47
|
+
if (total === 65535 || centralStart === 4294967295 || centralSize === 4294967295) throw unreadable("zip64", `This ${label} uses zip64, which Picasso does not read.`);
|
|
48
|
+
if (total > limits.maxEntries) throw tooLarge("too-many-parts", `This ${label} holds more than ${limits.maxEntries.toLocaleString("en-US")} parts, which is too many to read safely.`, { limit: limits.maxEntries });
|
|
49
|
+
if (centralStart + centralSize > buffer.length) throw unreadable("document-damaged", `This ${label} is damaged: its directory is cut short.`);
|
|
50
|
+
const entries = /* @__PURE__ */ new Map();
|
|
51
|
+
let at = centralStart;
|
|
52
|
+
let declared = 0;
|
|
53
|
+
for (let index = 0; index < total; index += 1) {
|
|
54
|
+
if (at + 46 > buffer.length || buffer.readUInt32LE(at) !== CENTRAL) throw unreadable("document-damaged", `This ${label} is damaged: its directory is corrupt.`);
|
|
55
|
+
const flags = buffer.readUInt16LE(at + 8);
|
|
56
|
+
const method = buffer.readUInt16LE(at + 10);
|
|
57
|
+
const compressed = buffer.readUInt32LE(at + 20);
|
|
58
|
+
const size = buffer.readUInt32LE(at + 24);
|
|
59
|
+
const nameLength = buffer.readUInt16LE(at + 28);
|
|
60
|
+
const extraLength = buffer.readUInt16LE(at + 30);
|
|
61
|
+
const commentLength = buffer.readUInt16LE(at + 32);
|
|
62
|
+
const offset = buffer.readUInt32LE(at + 42);
|
|
63
|
+
if (at + 46 + nameLength > buffer.length) throw unreadable("document-damaged", `This ${label} is damaged: its directory is corrupt.`);
|
|
64
|
+
const name = buffer.toString("utf8", at + 46, at + 46 + nameLength);
|
|
65
|
+
declared += size;
|
|
66
|
+
if (declared > limits.maxInflatedBytes) throw tooLarge("too-large-expanded", `This ${label} would expand to more than ${Math.round(limits.maxInflatedBytes / 1048576)} MB, which is too large to read safely.`, { limit: `${Math.round(limits.maxInflatedBytes / 1048576)} MB` });
|
|
67
|
+
if (!name.endsWith("/")) entries.set(name, { method, flags, compressed, size, offset });
|
|
68
|
+
at += 46 + nameLength + extraLength + commentLength;
|
|
69
|
+
}
|
|
70
|
+
let budget = limits.maxInflatedBytes;
|
|
71
|
+
const read = (name) => {
|
|
72
|
+
const entry = entries.get(name);
|
|
73
|
+
if (entry === void 0) return void 0;
|
|
74
|
+
if ((entry.flags & 1) !== 0) throw unreadable("encrypted", `This ${label} is encrypted or password-protected.`);
|
|
75
|
+
if (entry.offset + 30 > buffer.length || buffer.readUInt32LE(entry.offset) !== LOCAL) throw unreadable("document-damaged", `This ${label} is damaged: a part is missing its header.`);
|
|
76
|
+
const start = entry.offset + 30 + buffer.readUInt16LE(entry.offset + 26) + buffer.readUInt16LE(entry.offset + 28);
|
|
77
|
+
if (start + entry.compressed > buffer.length) throw unreadable("document-damaged", `This ${label} is damaged: a part is cut short.`);
|
|
78
|
+
const raw = buffer.subarray(start, start + entry.compressed);
|
|
79
|
+
if (entry.method === 0) {
|
|
80
|
+
if (raw.length > budget) throw tooLarge("too-large-expanded", `This ${label} would expand to more than ${Math.round(limits.maxInflatedBytes / 1048576)} MB, which is too large to read safely.`, { limit: `${Math.round(limits.maxInflatedBytes / 1048576)} MB` });
|
|
81
|
+
budget -= raw.length;
|
|
82
|
+
return raw;
|
|
83
|
+
}
|
|
84
|
+
if (entry.method !== 8) throw unreadable("compression-unsupported", `This ${label} uses a compression method Picasso does not read.`);
|
|
85
|
+
try {
|
|
86
|
+
const inflated = inflateRawSync(raw, { maxOutputLength: Math.max(1, budget) });
|
|
87
|
+
budget -= inflated.length;
|
|
88
|
+
return inflated;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error.code === "ERR_BUFFER_TOO_LARGE") {
|
|
91
|
+
throw tooLarge("too-large-expanded", `This ${label} would expand to more than ${Math.round(limits.maxInflatedBytes / 1048576)} MB, which is too large to read safely.`, { limit: `${Math.round(limits.maxInflatedBytes / 1048576)} MB` });
|
|
92
|
+
}
|
|
93
|
+
throw unreadable("document-damaged", `This ${label} is damaged: a part could not be unpacked.`);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
names: [...entries.keys()],
|
|
98
|
+
has: (name) => entries.has(name),
|
|
99
|
+
text: (name) => read(name)?.toString("utf8")
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
var NAMED_ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
103
|
+
function decodeXmlText(text) {
|
|
104
|
+
if (!text.includes("&")) return text;
|
|
105
|
+
return text.replace(/&(#x[0-9a-fA-F]{1,6}|#[0-9]{1,7}|[a-z]{2,4});/g, (whole, body) => {
|
|
106
|
+
if (body.startsWith("#")) {
|
|
107
|
+
const code = body[1] === "x" ? Number.parseInt(body.slice(2), 16) : Number.parseInt(body.slice(1), 10);
|
|
108
|
+
return code > 0 && code <= 1114111 && !(code >= 55296 && code <= 57343) ? String.fromCodePoint(code) : "";
|
|
109
|
+
}
|
|
110
|
+
return NAMED_ENTITIES[body] ?? whole;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function* xmlEvents(xml) {
|
|
114
|
+
const length = xml.length;
|
|
115
|
+
let at = 0;
|
|
116
|
+
const malformed = () => {
|
|
117
|
+
throw unreadable("document-damaged", "This document is damaged: its text could not be parsed.");
|
|
118
|
+
};
|
|
119
|
+
let steps = 0;
|
|
120
|
+
while (at < length) {
|
|
121
|
+
if ((steps++ & 4095) === 0 && Date.now() > OFFICE_DEADLINE.at) throw unreadable("timed-out", deadlineMessage(OFFICE_DEADLINE.seconds), { seconds: Math.round(OFFICE_DEADLINE.seconds) });
|
|
122
|
+
const lt = xml.indexOf("<", at);
|
|
123
|
+
if (lt < 0) {
|
|
124
|
+
yield { t: "text", text: decodeXmlText(xml.slice(at)) };
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (lt > at) yield { t: "text", text: decodeXmlText(xml.slice(at, lt)) };
|
|
128
|
+
if (xml.startsWith("<!--", lt)) {
|
|
129
|
+
const end = xml.indexOf("-->", lt + 4);
|
|
130
|
+
if (end < 0) malformed();
|
|
131
|
+
at = end + 3;
|
|
132
|
+
} else if (xml.startsWith("<![CDATA[", lt)) {
|
|
133
|
+
const end = xml.indexOf("]]>", lt + 9);
|
|
134
|
+
if (end < 0) malformed();
|
|
135
|
+
yield { t: "text", text: xml.slice(lt + 9, end) };
|
|
136
|
+
at = end + 3;
|
|
137
|
+
} else if (xml.startsWith("<?", lt)) {
|
|
138
|
+
const end = xml.indexOf("?>", lt + 2);
|
|
139
|
+
if (end < 0) malformed();
|
|
140
|
+
at = end + 2;
|
|
141
|
+
} else if (xml.startsWith("<!", lt)) {
|
|
142
|
+
const end = xml.indexOf(">", lt + 2);
|
|
143
|
+
if (end < 0) malformed();
|
|
144
|
+
if (xml.slice(lt, end).includes("[") || /<!ENTITY/i.test(xml.slice(lt, Math.min(length, end + 1)))) {
|
|
145
|
+
throw unreadable("xml-entities", "This document declares XML entities, which Picasso does not read.");
|
|
146
|
+
}
|
|
147
|
+
at = end + 1;
|
|
148
|
+
} else {
|
|
149
|
+
let end = lt + 1;
|
|
150
|
+
let quote = "";
|
|
151
|
+
for (; end < length; end += 1) {
|
|
152
|
+
const ch = xml[end];
|
|
153
|
+
if (quote !== "") {
|
|
154
|
+
if (ch === quote) quote = "";
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (ch === '"' || ch === "'") quote = ch;
|
|
158
|
+
else if (ch === ">") break;
|
|
159
|
+
}
|
|
160
|
+
if (end >= length) malformed();
|
|
161
|
+
const raw = xml.slice(lt + 1, end);
|
|
162
|
+
at = end + 1;
|
|
163
|
+
if (raw.startsWith("/")) {
|
|
164
|
+
yield { t: "close", name: raw.slice(1).trim() };
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const self = raw.endsWith("/");
|
|
168
|
+
const body = self ? raw.slice(0, -1) : raw;
|
|
169
|
+
const space = body.search(/\s/);
|
|
170
|
+
yield { t: "open", name: space < 0 ? body : body.slice(0, space), attrs: space < 0 ? "" : body.slice(space), self };
|
|
171
|
+
if (self) yield { t: "close", name: space < 0 ? body : body.slice(0, space) };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function xmlAttr(attrs, name) {
|
|
176
|
+
const match = new RegExp(`(?:^|\\s)${name.replace(/[.*+?^${}()|[\]\\:]/g, "\\$&")}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`).exec(attrs);
|
|
177
|
+
const value = match?.[1] ?? match?.[2];
|
|
178
|
+
return value === void 0 ? void 0 : decodeXmlText(value);
|
|
179
|
+
}
|
|
180
|
+
var local = (name) => name.slice(name.indexOf(":") + 1);
|
|
181
|
+
var set = (...names) => new Set(names);
|
|
182
|
+
var DOCX = {
|
|
183
|
+
paragraph: set("w:p"),
|
|
184
|
+
textTags: set("w:t"),
|
|
185
|
+
tab: set("w:tab"),
|
|
186
|
+
lineBreak: set("w:br", "w:cr"),
|
|
187
|
+
cell: set("w:tc"),
|
|
188
|
+
row: set("w:tr"),
|
|
189
|
+
collapseWhitespace: false
|
|
190
|
+
};
|
|
191
|
+
var PPTX = {
|
|
192
|
+
paragraph: set("a:p"),
|
|
193
|
+
textTags: set("a:t"),
|
|
194
|
+
tab: set(),
|
|
195
|
+
lineBreak: set("a:br"),
|
|
196
|
+
cell: set("a:tc"),
|
|
197
|
+
row: set("a:tr"),
|
|
198
|
+
collapseWhitespace: false
|
|
199
|
+
};
|
|
200
|
+
var ODT = {
|
|
201
|
+
paragraph: set("text:p", "text:h"),
|
|
202
|
+
textTags: null,
|
|
203
|
+
tab: set("text:tab"),
|
|
204
|
+
lineBreak: set("text:line-break"),
|
|
205
|
+
spaces: "text:s",
|
|
206
|
+
cell: set("table:table-cell"),
|
|
207
|
+
row: set("table:table-row"),
|
|
208
|
+
collapseWhitespace: true
|
|
209
|
+
};
|
|
210
|
+
function paragraphLines(xml, dialect) {
|
|
211
|
+
const lines = [];
|
|
212
|
+
const stack = [];
|
|
213
|
+
let inText = 0;
|
|
214
|
+
let cellDepth = 0;
|
|
215
|
+
let cell = "";
|
|
216
|
+
let row = [];
|
|
217
|
+
for (const event of xmlEvents(xml)) {
|
|
218
|
+
if (event.t === "text") {
|
|
219
|
+
if (stack.length > 0 && (dialect.textTags === null || inText > 0)) stack[stack.length - 1] += dialect.collapseWhitespace ? event.text.replace(/\s+/g, " ") : event.text;
|
|
220
|
+
} else if (event.t === "open") {
|
|
221
|
+
if (dialect.paragraph.has(event.name)) stack.push("");
|
|
222
|
+
else if (dialect.textTags?.has(event.name)) inText += 1;
|
|
223
|
+
else if (stack.length > 0 && dialect.tab.has(event.name)) stack[stack.length - 1] += " ";
|
|
224
|
+
else if (stack.length > 0 && dialect.lineBreak.has(event.name)) stack[stack.length - 1] += "\n";
|
|
225
|
+
else if (stack.length > 0 && event.name === dialect.spaces) stack[stack.length - 1] += " ".repeat(Math.min(1e3, Math.max(1, Number.parseInt(xmlAttr(event.attrs, "text:c") ?? "1", 10) || 1)));
|
|
226
|
+
else if (dialect.cell.has(event.name)) {
|
|
227
|
+
cellDepth += 1;
|
|
228
|
+
if (cellDepth === 1) cell = "";
|
|
229
|
+
}
|
|
230
|
+
} else if (dialect.paragraph.has(event.name)) {
|
|
231
|
+
const text = stack.pop() ?? "";
|
|
232
|
+
if (cellDepth > 0) cell += (cell === "" ? "" : " ") + text.trim();
|
|
233
|
+
else lines.push(text.replace(/[ \t]+$/, ""));
|
|
234
|
+
} else if (dialect.textTags?.has(event.name)) {
|
|
235
|
+
inText = Math.max(0, inText - 1);
|
|
236
|
+
} else if (dialect.cell.has(event.name)) {
|
|
237
|
+
cellDepth = Math.max(0, cellDepth - 1);
|
|
238
|
+
if (cellDepth === 0) row.push(cell);
|
|
239
|
+
} else if (dialect.row.has(event.name) && cellDepth === 0) {
|
|
240
|
+
if (row.length > 0) lines.push(row.join(" "));
|
|
241
|
+
row = [];
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return lines;
|
|
245
|
+
}
|
|
246
|
+
var joinLines = (lines) => lines.join("\n").replace(/\n{3,}/g, "\n\n").trim();
|
|
247
|
+
function docxText(zip) {
|
|
248
|
+
const body = zip.text("word/document.xml");
|
|
249
|
+
if (body === void 0) throw unreadable("docx-invalid", "This file is not a valid Word document (it has no document body).");
|
|
250
|
+
const parts = [joinLines(paragraphLines(body, DOCX))];
|
|
251
|
+
for (const extra of ["word/footnotes.xml", "word/endnotes.xml"]) {
|
|
252
|
+
const xml = zip.text(extra);
|
|
253
|
+
const text = xml === void 0 ? "" : joinLines(paragraphLines(xml, DOCX));
|
|
254
|
+
if (text !== "") parts.push(text);
|
|
255
|
+
}
|
|
256
|
+
return parts.filter((part) => part !== "").join("\n\n");
|
|
257
|
+
}
|
|
258
|
+
function odtText(zip) {
|
|
259
|
+
const body = zip.text("content.xml");
|
|
260
|
+
if (body === void 0) throw unreadable("odt-invalid", "This file is not a valid OpenDocument text file (it has no content).");
|
|
261
|
+
return joinLines(paragraphLines(body, ODT));
|
|
262
|
+
}
|
|
263
|
+
function relationshipTargets(xml) {
|
|
264
|
+
const targets = /* @__PURE__ */ new Map();
|
|
265
|
+
if (xml === void 0) return targets;
|
|
266
|
+
for (const event of xmlEvents(xml)) {
|
|
267
|
+
if (event.t !== "open" || local(event.name) !== "Relationship") continue;
|
|
268
|
+
const id = xmlAttr(event.attrs, "Id");
|
|
269
|
+
const target = xmlAttr(event.attrs, "Target");
|
|
270
|
+
if (id !== void 0 && target !== void 0) targets.set(id, target);
|
|
271
|
+
}
|
|
272
|
+
return targets;
|
|
273
|
+
}
|
|
274
|
+
function resolveTarget(base, target) {
|
|
275
|
+
const parts = (target.startsWith("/") ? target.slice(1) : `${base}/${target}`).split("/");
|
|
276
|
+
const out = [];
|
|
277
|
+
for (const part of parts) {
|
|
278
|
+
if (part === "..") out.pop();
|
|
279
|
+
else if (part !== "." && part !== "") out.push(part);
|
|
280
|
+
}
|
|
281
|
+
return out.join("/");
|
|
282
|
+
}
|
|
283
|
+
function orderedParts(zip, mainPart, relsPart, element, base) {
|
|
284
|
+
const main = zip.text(mainPart);
|
|
285
|
+
if (main === void 0) return [];
|
|
286
|
+
const targets = relationshipTargets(zip.text(relsPart));
|
|
287
|
+
const parts = [];
|
|
288
|
+
for (const event of xmlEvents(main)) {
|
|
289
|
+
if (event.t !== "open" || local(event.name) !== element) continue;
|
|
290
|
+
const rid = xmlAttr(event.attrs, "r:id");
|
|
291
|
+
const target = rid === void 0 ? void 0 : targets.get(rid);
|
|
292
|
+
if (target !== void 0) parts.push({ name: xmlAttr(event.attrs, "name") ?? "", part: resolveTarget(base, target) });
|
|
293
|
+
}
|
|
294
|
+
return parts;
|
|
295
|
+
}
|
|
296
|
+
function pptxText(zip) {
|
|
297
|
+
let slides = orderedParts(zip, "ppt/presentation.xml", "ppt/_rels/presentation.xml.rels", "sldId", "ppt").map((entry) => entry.part).filter((part) => zip.has(part));
|
|
298
|
+
if (slides.length === 0) {
|
|
299
|
+
slides = zip.names.filter((name) => /^ppt\/slides\/slide\d+\.xml$/.test(name)).sort((a, b) => Number(/(\d+)\.xml$/.exec(a)?.[1]) - Number(/(\d+)\.xml$/.exec(b)?.[1]));
|
|
300
|
+
}
|
|
301
|
+
if (slides.length === 0) throw unreadable("pptx-invalid", "This file is not a valid PowerPoint presentation (it has no slides).");
|
|
302
|
+
const sections = slides.map((part, index) => `--- Slide ${index + 1} ---
|
|
303
|
+
${joinLines(paragraphLines(zip.text(part) ?? "", PPTX))}`.trimEnd());
|
|
304
|
+
return { slides: slides.length, text: sections.join("\n\n") };
|
|
305
|
+
}
|
|
306
|
+
function sharedStrings(xml) {
|
|
307
|
+
const strings = [];
|
|
308
|
+
if (xml === void 0) return strings;
|
|
309
|
+
let current = "";
|
|
310
|
+
let inItem = false;
|
|
311
|
+
let inText = false;
|
|
312
|
+
let phonetic = 0;
|
|
313
|
+
for (const event of xmlEvents(xml)) {
|
|
314
|
+
if (event.t === "open") {
|
|
315
|
+
const name = local(event.name);
|
|
316
|
+
if (name === "si") {
|
|
317
|
+
inItem = true;
|
|
318
|
+
current = "";
|
|
319
|
+
} else if (name === "rPh") phonetic += 1;
|
|
320
|
+
else if (name === "t") inText = true;
|
|
321
|
+
} else if (event.t === "close") {
|
|
322
|
+
const name = local(event.name);
|
|
323
|
+
if (name === "si") {
|
|
324
|
+
strings.push(current);
|
|
325
|
+
inItem = false;
|
|
326
|
+
} else if (name === "rPh") phonetic = Math.max(0, phonetic - 1);
|
|
327
|
+
else if (name === "t") inText = false;
|
|
328
|
+
} else if (inItem && inText && phonetic === 0) {
|
|
329
|
+
current += event.text;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return strings;
|
|
333
|
+
}
|
|
334
|
+
var columnIndex = (ref) => {
|
|
335
|
+
let index = 0;
|
|
336
|
+
for (const ch of ref) {
|
|
337
|
+
const code = ch.charCodeAt(0);
|
|
338
|
+
if (code < 65 || code > 90) break;
|
|
339
|
+
index = index * 26 + (code - 64);
|
|
340
|
+
}
|
|
341
|
+
return Math.max(0, index - 1);
|
|
342
|
+
};
|
|
343
|
+
function sheetRows(xml, strings) {
|
|
344
|
+
const rows = [];
|
|
345
|
+
let cells = [];
|
|
346
|
+
let column = 0;
|
|
347
|
+
let type = "";
|
|
348
|
+
let value = "";
|
|
349
|
+
let inValue = false;
|
|
350
|
+
let inInline = false;
|
|
351
|
+
let inInlineText = false;
|
|
352
|
+
let phonetic = 0;
|
|
353
|
+
let inCell = false;
|
|
354
|
+
const flushCell = () => {
|
|
355
|
+
let text = value;
|
|
356
|
+
if (type === "s") text = strings[Number.parseInt(value, 10)] ?? "";
|
|
357
|
+
else if (type === "b") text = value === "1" ? "TRUE" : value === "0" ? "FALSE" : value;
|
|
358
|
+
cells[column] = text;
|
|
359
|
+
};
|
|
360
|
+
for (const event of xmlEvents(xml)) {
|
|
361
|
+
if (event.t === "open") {
|
|
362
|
+
const name = local(event.name);
|
|
363
|
+
if (name === "row") cells = [];
|
|
364
|
+
else if (name === "c") {
|
|
365
|
+
inCell = true;
|
|
366
|
+
type = xmlAttr(event.attrs, "t") ?? "";
|
|
367
|
+
const ref = xmlAttr(event.attrs, "r");
|
|
368
|
+
column = ref === void 0 ? cells.length : columnIndex(ref);
|
|
369
|
+
value = "";
|
|
370
|
+
} else if (inCell && name === "v") inValue = true;
|
|
371
|
+
else if (inCell && name === "is") inInline = true;
|
|
372
|
+
else if (inInline && name === "rPh") phonetic += 1;
|
|
373
|
+
else if (inInline && name === "t") inInlineText = true;
|
|
374
|
+
} else if (event.t === "close") {
|
|
375
|
+
const name = local(event.name);
|
|
376
|
+
if (name === "c") {
|
|
377
|
+
flushCell();
|
|
378
|
+
inCell = false;
|
|
379
|
+
inValue = false;
|
|
380
|
+
inInline = false;
|
|
381
|
+
} else if (name === "v") inValue = false;
|
|
382
|
+
else if (name === "is") inInline = false;
|
|
383
|
+
else if (name === "rPh") phonetic = Math.max(0, phonetic - 1);
|
|
384
|
+
else if (name === "t") inInlineText = false;
|
|
385
|
+
else if (name === "row") {
|
|
386
|
+
let last = cells.length - 1;
|
|
387
|
+
while (last >= 0 && (cells[last] === void 0 || cells[last] === "")) last -= 1;
|
|
388
|
+
if (last >= 0) rows.push(Array.from({ length: last + 1 }, (_, index) => cells[index] ?? "").join(" "));
|
|
389
|
+
}
|
|
390
|
+
} else if (inCell && inValue) {
|
|
391
|
+
value += event.text;
|
|
392
|
+
} else if (inCell && inInline && inInlineText && phonetic === 0) {
|
|
393
|
+
value += event.text;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return rows;
|
|
397
|
+
}
|
|
398
|
+
function xlsxText(zip) {
|
|
399
|
+
let sheets = orderedParts(zip, "xl/workbook.xml", "xl/_rels/workbook.xml.rels", "sheet", "xl").filter((sheet) => zip.has(sheet.part));
|
|
400
|
+
if (sheets.length === 0) {
|
|
401
|
+
sheets = zip.names.filter((name) => /^xl\/worksheets\/sheet\d+\.xml$/.test(name)).sort().map((part, index) => ({ name: `Sheet${index + 1}`, part }));
|
|
402
|
+
}
|
|
403
|
+
if (sheets.length === 0) throw unreadable("xlsx-invalid", "This file is not a valid Excel workbook (it has no sheets).");
|
|
404
|
+
const strings = sharedStrings(zip.text("xl/sharedStrings.xml"));
|
|
405
|
+
const sections = [];
|
|
406
|
+
for (const sheet of sheets) {
|
|
407
|
+
const rows = sheetRows(zip.text(sheet.part) ?? "", strings);
|
|
408
|
+
if (rows.length > 0) sections.push(`## ${sheet.name}
|
|
409
|
+
${rows.join("\n")}`);
|
|
410
|
+
}
|
|
411
|
+
return { sheets: sheets.length, text: sections.join("\n\n") };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/attachment-text.ts
|
|
415
|
+
var ATTACHMENT_LIMITS = {
|
|
416
|
+
/** Input file size. */
|
|
417
|
+
maxInputBytes: 25 * 1048576,
|
|
418
|
+
/** Extracted text, the truncation marker included. */
|
|
419
|
+
maxChars: 4e5,
|
|
420
|
+
maxPdfPages: 300,
|
|
421
|
+
timeoutMs: 2e4,
|
|
422
|
+
/** Zip-bomb caps for docx/pptx/xlsx/odt. */
|
|
423
|
+
maxInflatedBytes: 100 * 1048576,
|
|
424
|
+
maxZipEntries: 2e3
|
|
425
|
+
};
|
|
426
|
+
var fail = (code, reason, message, extra = {}) => ({ ok: false, error: { code, message, reason, ...extra.params === void 0 ? {} : { params: extra.params }, ...extra.details === void 0 ? {} : { details: extra.details } } });
|
|
427
|
+
var tooLargeFailure = (size, limit) => fail("attachment/too-large", "too-large", `This file is ${megabytes(size)}; Picasso reads files up to ${megabytes(limit)}.`, { params: { size: megabytes(size), limit: megabytes(limit) } });
|
|
428
|
+
var megabytes = (bytes) => `${Math.round(bytes / 1048576 * 10) / 10} MB`;
|
|
429
|
+
function cleanAttachmentName(name) {
|
|
430
|
+
const base = (name ?? "").split(/[\\/]/).pop() ?? "";
|
|
431
|
+
const cleaned = base.replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, 160);
|
|
432
|
+
return cleaned === "" || cleaned === "." || cleaned === ".." ? "attachment" : cleaned;
|
|
433
|
+
}
|
|
434
|
+
var extensionOf = (name) => {
|
|
435
|
+
const dot = name.lastIndexOf(".");
|
|
436
|
+
return dot <= 0 ? "" : name.slice(dot + 1).toLowerCase();
|
|
437
|
+
};
|
|
438
|
+
var ZIP_KINDS = { docx: "docx", pptx: "pptx", xlsx: "xlsx", odt: "odt" };
|
|
439
|
+
var TEXTUTIL_KINDS = { rtf: "rtf", doc: "doc" };
|
|
440
|
+
var HTML_EXTENSIONS = /* @__PURE__ */ new Set(["html", "htm", "xhtml"]);
|
|
441
|
+
var DOCUMENT_EXTENSIONS = /* @__PURE__ */ new Set(["pdf", ...Object.keys(ZIP_KINDS), ...Object.keys(TEXTUTIL_KINDS), ...HTML_EXTENSIONS]);
|
|
442
|
+
var isDocumentName = (name) => DOCUMENT_EXTENSIONS.has(extensionOf(cleanAttachmentName(name)));
|
|
443
|
+
var marker = (more) => `
|
|
444
|
+
[\u2026 truncated: ${more.toLocaleString("en-US")} more characters]`;
|
|
445
|
+
function truncateText(text, max) {
|
|
446
|
+
if (text.length <= max) return { text, truncated: false };
|
|
447
|
+
let cut = max;
|
|
448
|
+
for (let pass = 0; pass < 4; pass += 1) {
|
|
449
|
+
const next = Math.max(0, max - marker(text.length - cut).length);
|
|
450
|
+
if (next === cut) break;
|
|
451
|
+
cut = next;
|
|
452
|
+
}
|
|
453
|
+
const high = text.charCodeAt(cut - 1);
|
|
454
|
+
if (cut > 0 && high >= 55296 && high <= 56319) cut -= 1;
|
|
455
|
+
return { text: `${text.slice(0, cut)}${marker(text.length - cut)}`, truncated: true };
|
|
456
|
+
}
|
|
457
|
+
var NO_TEXT_LAYER = "No text layer found (scanned pages are not read; no OCR is attempted).";
|
|
458
|
+
function finish(kind, rawText, pages, warnings, maxChars, forceTruncated = false) {
|
|
459
|
+
const normalized = rawText.replace(/\r\n?/g, "\n").split("\0").join("");
|
|
460
|
+
if (normalized.trim() === "") {
|
|
461
|
+
if (kind === "pdf") return fail("attachment/unreadable", "pdf-no-text-layer", NO_TEXT_LAYER, { details: { warnings: warnings.includes(NO_TEXT_LAYER) ? warnings : [...warnings, NO_TEXT_LAYER] } });
|
|
462
|
+
return fail("attachment/unreadable", "no-text", "No readable text was found in this file.", { details: { warnings } });
|
|
463
|
+
}
|
|
464
|
+
const capped = truncateText(normalized, maxChars);
|
|
465
|
+
return { ok: true, value: { kind, text: capped.text, ...pages === void 0 ? {} : { pages }, truncated: capped.truncated || forceTruncated, warnings } };
|
|
466
|
+
}
|
|
467
|
+
function htmlToText(source) {
|
|
468
|
+
const named = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " ", mdash: "\u2014", ndash: "\u2013", hellip: "\u2026", copy: "\xA9", rsquo: "\u2019", lsquo: "\u2018", rdquo: "\u201D", ldquo: "\u201C" };
|
|
469
|
+
return source.replace(/<!--[\s\S]*?-->/g, "").replace(/<(script|style|head|noscript|template)\b[\s\S]*?<\/\1\s*>/gi, "").replace(/<\/?(?:p|div|br|li|tr|h[1-6]|section|article|header|footer|table|ul|ol|pre|blockquote|hr|dt|dd|figure|figcaption)\b[^>]*>/gi, "\n").replace(/<\/?(?:td|th)\b[^>]*>/gi, " ").replace(/<[^>]*>/g, "").replace(/&(#x[0-9a-fA-F]{1,6}|#[0-9]{1,7}|[a-zA-Z]{2,8});/g, (whole, body) => {
|
|
470
|
+
if (body.startsWith("#")) {
|
|
471
|
+
const code = body[1]?.toLowerCase() === "x" ? Number.parseInt(body.slice(2), 16) : Number.parseInt(body.slice(1), 10);
|
|
472
|
+
return code > 0 && code <= 1114111 && !(code >= 55296 && code <= 57343) ? String.fromCodePoint(code) : "";
|
|
473
|
+
}
|
|
474
|
+
return named[body.toLowerCase()] ?? whole;
|
|
475
|
+
}).split("\n").map((line) => line.replace(/[ \t]+/g, " ").replace(/^ +| +$/g, "")).join("\n").replace(/\n{3,}/g, "\n\n").trim();
|
|
476
|
+
}
|
|
477
|
+
function decodeText(data) {
|
|
478
|
+
const buffer = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
479
|
+
if (buffer.length >= 2 && buffer[0] === 255 && buffer[1] === 254) return buffer.subarray(2).toString("utf16le");
|
|
480
|
+
if (buffer.length >= 2 && buffer[0] === 254 && buffer[1] === 255) return Buffer.from(buffer.subarray(2)).swap16().toString("utf16le");
|
|
481
|
+
const body = buffer.length >= 3 && buffer[0] === 239 && buffer[1] === 187 && buffer[2] === 191 ? buffer.subarray(3) : buffer;
|
|
482
|
+
if (body.subarray(0, 8192).includes(0)) return void 0;
|
|
483
|
+
try {
|
|
484
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(body);
|
|
485
|
+
} catch {
|
|
486
|
+
return void 0;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
async function extractPdf(data, limits, deadline = Number.POSITIVE_INFINITY) {
|
|
490
|
+
if (!Buffer.from(data.buffer, data.byteOffset, Math.min(data.byteLength, 1024)).includes("%PDF-")) {
|
|
491
|
+
return fail("attachment/unreadable", "pdf-invalid", "This file is not a valid PDF.");
|
|
492
|
+
}
|
|
493
|
+
const { getDocumentProxy } = await import("unpdf");
|
|
494
|
+
let pdf;
|
|
495
|
+
try {
|
|
496
|
+
pdf = await getDocumentProxy(Buffer.isBuffer(data) ? Uint8Array.from(data) : data, { useSystemFonts: false, verbosity: 0 });
|
|
497
|
+
} catch (error) {
|
|
498
|
+
if (error?.name === "PasswordException") return fail("attachment/unreadable", "pdf-password", "This PDF is password-protected.");
|
|
499
|
+
return fail("attachment/unreadable", "pdf-damaged", "This PDF is damaged and could not be opened.");
|
|
500
|
+
}
|
|
501
|
+
try {
|
|
502
|
+
const total = pdf.numPages;
|
|
503
|
+
const read = Math.min(total, limits.maxPdfPages);
|
|
504
|
+
const warnings = [];
|
|
505
|
+
const pages = [];
|
|
506
|
+
let empty = 0;
|
|
507
|
+
for (let number = 1; number <= read; number += 1) {
|
|
508
|
+
if (Date.now() > deadline) return timedOut(limits);
|
|
509
|
+
const page = await pdf.getPage(number);
|
|
510
|
+
const content = await page.getTextContent();
|
|
511
|
+
let text2 = "";
|
|
512
|
+
for (const item of content.items) {
|
|
513
|
+
if (!("str" in item)) continue;
|
|
514
|
+
text2 += item.str;
|
|
515
|
+
text2 += item.hasEOL ? "\n" : " ";
|
|
516
|
+
}
|
|
517
|
+
text2 = text2.replace(/[ \t]+\n/g, "\n").replace(/[ \t]{2,}/g, " ").trim();
|
|
518
|
+
if (text2 === "") empty += 1;
|
|
519
|
+
pages.push(text2);
|
|
520
|
+
page.cleanup();
|
|
521
|
+
}
|
|
522
|
+
if (total > read) warnings.push(`Only the first ${read} of ${total} pages were read.`);
|
|
523
|
+
if (empty > 0 && empty < read) warnings.push(`${empty} of ${read} pages have no text layer (scanned pages are not read; no OCR is attempted).`);
|
|
524
|
+
if (empty === read) warnings.push(NO_TEXT_LAYER);
|
|
525
|
+
const text = pages.map((page, index) => page === "" ? "" : `--- Page ${index + 1} ---
|
|
526
|
+
${page}`).filter((page) => page !== "").join("\n\n");
|
|
527
|
+
return finish("pdf", text, total, warnings, limits.maxChars, total > read);
|
|
528
|
+
} catch {
|
|
529
|
+
return fail("attachment/unreadable", "pdf-damaged", "This PDF is damaged and could not be read.");
|
|
530
|
+
} finally {
|
|
531
|
+
await pdf.loadingTask.destroy().catch(() => void 0);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
function extractZipDocument(kind, data, limits) {
|
|
535
|
+
const label = { docx: "Word document", pptx: "PowerPoint presentation", xlsx: "Excel workbook", odt: "OpenDocument text file" }[kind];
|
|
536
|
+
const zip = openZip(Buffer.from(data.buffer, data.byteOffset, data.byteLength), { maxInflatedBytes: limits.maxInflatedBytes, maxEntries: limits.maxZipEntries }, label);
|
|
537
|
+
if (kind === "docx") return finish("docx", docxText(zip), void 0, [], limits.maxChars);
|
|
538
|
+
if (kind === "odt") return finish("odt", odtText(zip), void 0, [], limits.maxChars);
|
|
539
|
+
if (kind === "pptx") {
|
|
540
|
+
const slides = pptxText(zip);
|
|
541
|
+
return finish("pptx", slides.text, slides.slides, [], limits.maxChars);
|
|
542
|
+
}
|
|
543
|
+
const sheets = xlsxText(zip);
|
|
544
|
+
return finish("xlsx", sheets.text, sheets.sheets, [], limits.maxChars);
|
|
545
|
+
}
|
|
546
|
+
async function runJob(job, deadline = Number.POSITIVE_INFINITY) {
|
|
547
|
+
const extension = extensionOf(job.name);
|
|
548
|
+
try {
|
|
549
|
+
if (extension === "pdf") return await extractPdf(job.data, job.limits, deadline);
|
|
550
|
+
const zipKind = ZIP_KINDS[extension];
|
|
551
|
+
if (zipKind !== void 0) return extractZipDocument(zipKind, job.data, job.limits);
|
|
552
|
+
const text = decodeText(job.data);
|
|
553
|
+
if (text === void 0) return fail("attachment/unsupported", "unsupported-type", `Picasso can't read ${extension === "" ? "this kind of file" : `.${extension} files`} as text.`, { params: { extension } });
|
|
554
|
+
return finish(HTML_EXTENSIONS.has(extension) ? "html" : "text", HTML_EXTENSIONS.has(extension) ? htmlToText(text) : text, void 0, [], job.limits.maxChars);
|
|
555
|
+
} catch (error) {
|
|
556
|
+
if (error instanceof OfficeReadError) return fail(error.code, error.reason, error.message, error.params === void 0 ? {} : { params: error.params });
|
|
557
|
+
return fail("attachment/unreadable", "read-failed", "This file could not be read.");
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (!isMainThread && workerData?.picassoAttachmentJob === true) {
|
|
561
|
+
void runJob(workerData).then((outcome) => parentPort?.postMessage(outcome));
|
|
562
|
+
}
|
|
563
|
+
function timedOut(limits) {
|
|
564
|
+
return fail("attachment/unreadable", "timed-out", deadlineMessage(limits.timeoutMs / 1e3), { params: { seconds: Math.round(limits.timeoutMs / 1e3) } });
|
|
565
|
+
}
|
|
566
|
+
var CAN_SPAWN_WORKER = /\/attachment-text\.(?:ts|mjs)$/u.test(import.meta.url);
|
|
567
|
+
async function inThread(job) {
|
|
568
|
+
const deadline = Date.now() + job.limits.timeoutMs;
|
|
569
|
+
let timer;
|
|
570
|
+
const expiry = new Promise((resolve) => {
|
|
571
|
+
timer = setTimeout(() => resolve(timedOut(job.limits)), job.limits.timeoutMs);
|
|
572
|
+
});
|
|
573
|
+
OFFICE_DEADLINE.at = deadline;
|
|
574
|
+
OFFICE_DEADLINE.seconds = job.limits.timeoutMs / 1e3;
|
|
575
|
+
try {
|
|
576
|
+
return await Promise.race([runJob(job, deadline), expiry]);
|
|
577
|
+
} finally {
|
|
578
|
+
OFFICE_DEADLINE.at = Number.POSITIVE_INFINITY;
|
|
579
|
+
clearTimeout(timer);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
function inWorker(job) {
|
|
583
|
+
if (!CAN_SPAWN_WORKER) return inThread(job);
|
|
584
|
+
return new Promise((resolve) => {
|
|
585
|
+
const copy = new Uint8Array(job.data.byteLength);
|
|
586
|
+
copy.set(job.data);
|
|
587
|
+
let settled = false;
|
|
588
|
+
const worker = new Worker(new URL(import.meta.url), {
|
|
589
|
+
workerData: { ...job, data: copy },
|
|
590
|
+
transferList: [copy.buffer],
|
|
591
|
+
resourceLimits: { maxOldGenerationSizeMb: 1024 },
|
|
592
|
+
stdout: true,
|
|
593
|
+
stderr: true
|
|
594
|
+
});
|
|
595
|
+
const done = (outcome) => {
|
|
596
|
+
if (settled) return;
|
|
597
|
+
settled = true;
|
|
598
|
+
clearTimeout(timer);
|
|
599
|
+
void worker.terminate();
|
|
600
|
+
resolve(outcome);
|
|
601
|
+
};
|
|
602
|
+
const timer = setTimeout(() => done(timedOut(job.limits)), job.limits.timeoutMs);
|
|
603
|
+
worker.once("message", (outcome) => done(outcome));
|
|
604
|
+
worker.once("error", (error) => done(error.code === "ERR_WORKER_OUT_OF_MEMORY" ? fail("attachment/unreadable", "out-of-memory", "This file needs more memory to read than Picasso allows.") : fail("attachment/unreadable", "read-failed", "This file could not be read.")));
|
|
605
|
+
worker.once("exit", () => done(fail("attachment/unreadable", "read-failed", "This file could not be read.")));
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
async function viaTextutil(kind, data, extension, limits) {
|
|
609
|
+
if (process.platform !== "darwin" || !existsSync("/usr/bin/textutil")) {
|
|
610
|
+
return fail("attachment/unsupported", "needs-macos", `Reading .${extension} files needs macOS. Save it as .docx or .pdf and attach that instead.`, { params: { extension } });
|
|
611
|
+
}
|
|
612
|
+
const dir = await mkdtemp(join(tmpdir(), "picasso-extract-"));
|
|
613
|
+
try {
|
|
614
|
+
const file = join(dir, `input.${extension}`);
|
|
615
|
+
await writeFile(file, data, { mode: 384 });
|
|
616
|
+
const stdout = await new Promise((resolve) => {
|
|
617
|
+
execFile("/usr/bin/textutil", ["-convert", "txt", "-noload", "-nostore", "-stdout", file], { timeout: limits.timeoutMs, killSignal: "SIGKILL", maxBuffer: 64 * 1048576, encoding: "utf8" }, (error, out) => resolve(error ? void 0 : out));
|
|
618
|
+
});
|
|
619
|
+
if (stdout === void 0) return fail("attachment/unreadable", "legacy-read-failed", `This .${extension} file could not be read (it may be damaged, or reading took longer than ${Math.round(limits.timeoutMs / 1e3)} seconds).`, { params: { extension, seconds: Math.round(limits.timeoutMs / 1e3) } });
|
|
620
|
+
return finish(kind, stdout, void 0, [], limits.maxChars);
|
|
621
|
+
} finally {
|
|
622
|
+
await rm(dir, { recursive: true, force: true }).catch(() => void 0);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
var cache = /* @__PURE__ */ new Map();
|
|
626
|
+
var CACHE_ENTRIES = 8;
|
|
627
|
+
async function extractAttachmentText(input, options = {}) {
|
|
628
|
+
const limits = { ...ATTACHMENT_LIMITS, ...options.limits };
|
|
629
|
+
const name = cleanAttachmentName(input.name || (input.path === void 0 ? void 0 : basename(input.path)));
|
|
630
|
+
let data = input.data;
|
|
631
|
+
if (data === void 0) {
|
|
632
|
+
if (input.path === void 0) return fail("attachment/unreadable", "no-file", "No file was provided to read.");
|
|
633
|
+
if (options.readPath === void 0) return fail("attachment/unreadable", "path-read-unavailable", "Reading a file by path is not available here.");
|
|
634
|
+
const read = await options.readPath(input.path);
|
|
635
|
+
if (!read.ok) return read;
|
|
636
|
+
data = read.value;
|
|
637
|
+
}
|
|
638
|
+
if (data.length > limits.maxInputBytes) {
|
|
639
|
+
return tooLargeFailure(data.length, limits.maxInputBytes);
|
|
640
|
+
}
|
|
641
|
+
if (data.length === 0) return fail("attachment/unreadable", "empty", "This file is empty.");
|
|
642
|
+
const extension = extensionOf(name);
|
|
643
|
+
const key = `${createHash("sha256").update(data).digest("hex")}:${extension}:${limits.maxChars}:${limits.maxPdfPages}`;
|
|
644
|
+
const hit = cache.get(key);
|
|
645
|
+
if (hit !== void 0) {
|
|
646
|
+
cache.delete(key);
|
|
647
|
+
cache.set(key, hit);
|
|
648
|
+
return { ok: true, value: structuredClone(hit) };
|
|
649
|
+
}
|
|
650
|
+
const textutilKind = TEXTUTIL_KINDS[extension];
|
|
651
|
+
const job = { picassoAttachmentJob: true, name, data, limits };
|
|
652
|
+
const outcome = textutilKind === void 0 ? await (options.inThread === true ? inThread(job) : inWorker(job)) : await viaTextutil(textutilKind, data, extension, limits);
|
|
653
|
+
if (!outcome.ok) return outcome;
|
|
654
|
+
cache.set(key, structuredClone(outcome.value));
|
|
655
|
+
while (cache.size > CACHE_ENTRIES) cache.delete(cache.keys().next().value);
|
|
656
|
+
return { ok: true, value: outcome.value };
|
|
657
|
+
}
|
|
658
|
+
function dshPathReader(get, maxBytes = ATTACHMENT_LIMITS.maxInputBytes) {
|
|
659
|
+
return async (path) => {
|
|
660
|
+
if (get === void 0) return fail("attachment/unreadable", "reads-unreachable", "File reads are not reachable on this engine connection.");
|
|
661
|
+
if (!path.startsWith("/") || path.includes("\0")) return fail("attachment/unreadable", "path-not-absolute", "A file path must be absolute.");
|
|
662
|
+
let resolved;
|
|
663
|
+
try {
|
|
664
|
+
resolved = await realpath(path);
|
|
665
|
+
} catch {
|
|
666
|
+
return fail("attachment/unreadable", "not-found", "That file could not be found.");
|
|
667
|
+
}
|
|
668
|
+
let response;
|
|
669
|
+
try {
|
|
670
|
+
response = await get(`file?path=${encodeURIComponent(resolved)}`);
|
|
671
|
+
} catch {
|
|
672
|
+
return fail("attachment/unreadable", "reads-unreachable", "File reads are not reachable on this engine connection.");
|
|
673
|
+
}
|
|
674
|
+
if (response.ok) {
|
|
675
|
+
const bytes = Buffer.from(await response.arrayBuffer());
|
|
676
|
+
return bytes.length > maxBytes ? tooLargeFailure(bytes.length, maxBytes) : { ok: true, value: bytes };
|
|
677
|
+
}
|
|
678
|
+
if (response.status !== 413) return response.status === 404 ? fail("attachment/unreadable", "not-found", "That file could not be found.") : fail("attachment/unreadable", "outside-folders", "That file is outside the folders Picasso can read.");
|
|
679
|
+
const info = await lstat(resolved).catch(() => void 0);
|
|
680
|
+
if (info === void 0 || !info.isFile()) return fail("attachment/unreadable", "not-regular-file", "That is not a regular file.");
|
|
681
|
+
if (info.size > maxBytes) return tooLargeFailure(info.size, maxBytes);
|
|
682
|
+
return { ok: true, value: await readFile(resolved) };
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
function rootsPathReader(roots, maxBytes = ATTACHMENT_LIMITS.maxInputBytes) {
|
|
686
|
+
return async (path) => {
|
|
687
|
+
if (!path.startsWith("/") || path.includes("\0")) return fail("attachment/unreadable", "path-not-absolute", "A file path must be absolute.");
|
|
688
|
+
let resolved;
|
|
689
|
+
try {
|
|
690
|
+
resolved = await realpath(path);
|
|
691
|
+
} catch {
|
|
692
|
+
return fail("attachment/unreadable", "not-found", "That file could not be found.");
|
|
693
|
+
}
|
|
694
|
+
const real = await Promise.all(roots.map((root) => realpath(root).catch(() => void 0)));
|
|
695
|
+
if (!real.some((root) => root !== void 0 && (resolved === root || resolved.startsWith(root.endsWith("/") ? root : `${root}/`)))) {
|
|
696
|
+
return fail("attachment/unreadable", "outside-folders", "That file is outside the folders Picasso can read.");
|
|
697
|
+
}
|
|
698
|
+
const info = await lstat(resolved).catch(() => void 0);
|
|
699
|
+
if (info === void 0 || !info.isFile()) return fail("attachment/unreadable", "not-regular-file", "That is not a regular file.");
|
|
700
|
+
if (info.size > maxBytes) return tooLargeFailure(info.size, maxBytes);
|
|
701
|
+
return { ok: true, value: await readFile(resolved) };
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
async function extractRequest(body, options = {}) {
|
|
705
|
+
const bad = (message) => ({ status: 400, body: { error: { code: "relay/bad-request", message } } });
|
|
706
|
+
const path = typeof body["path"] === "string" && body["path"] !== "" ? body["path"] : void 0;
|
|
707
|
+
const encoded = typeof body["dataBase64"] === "string" ? body["dataBase64"].replace(/^data:[^,]*;base64,/i, "") : void 0;
|
|
708
|
+
if (path === void 0 === (encoded === void 0)) return bad("Send exactly one of path or dataBase64.");
|
|
709
|
+
const name = typeof body["name"] === "string" ? body["name"] : path === void 0 ? "" : basename(path);
|
|
710
|
+
if (encoded !== void 0 && name.trim() === "") return bad("A name is required with dataBase64.");
|
|
711
|
+
const maxInput = options.limits?.maxInputBytes ?? ATTACHMENT_LIMITS.maxInputBytes;
|
|
712
|
+
if (encoded !== void 0 && encoded.length > Math.ceil(maxInput * 4 / 3) + 8) {
|
|
713
|
+
return { status: 422, body: { error: { code: "attachment/too-large", reason: "too-large-upload", params: { limit: megabytes(maxInput) }, message: `This file is larger than ${megabytes(maxInput)}; Picasso reads files up to ${megabytes(maxInput)}.` } } };
|
|
714
|
+
}
|
|
715
|
+
const result = await extractAttachmentText(encoded === void 0 ? { path, name } : { data: Buffer.from(encoded, "base64"), name }, options);
|
|
716
|
+
if (!result.ok) return { status: 422, body: { error: result.error } };
|
|
717
|
+
const { kind, text, pages, truncated, warnings } = result.value;
|
|
718
|
+
return { status: 200, body: { kind, text, ...pages === void 0 ? {} : { pages }, truncated, warnings } };
|
|
719
|
+
}
|
|
720
|
+
function documentTextPart(name, document) {
|
|
721
|
+
const label = cleanAttachmentName(name).replace(/"/g, "'");
|
|
722
|
+
const pages = document.pages === void 0 ? "" : `, ${document.pages} ${document.pages === 1 ? "page" : "pages"}`;
|
|
723
|
+
return { type: "text", text: `Attached document "${label}" (${document.kind}${pages}):
|
|
724
|
+
${document.text}` };
|
|
725
|
+
}
|
|
726
|
+
export {
|
|
727
|
+
ATTACHMENT_LIMITS,
|
|
728
|
+
DOCUMENT_EXTENSIONS,
|
|
729
|
+
cleanAttachmentName,
|
|
730
|
+
documentTextPart,
|
|
731
|
+
dshPathReader,
|
|
732
|
+
extractAttachmentText,
|
|
733
|
+
extractRequest,
|
|
734
|
+
isDocumentName,
|
|
735
|
+
rootsPathReader,
|
|
736
|
+
truncateText
|
|
737
|
+
};
|