@locode2/harness 0.1.0-preview.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/README.md +27 -0
- package/THIRD_PARTY_LICENSES.txt +32769 -0
- package/THIRD_PARTY_NOTICES.md +13 -0
- package/apps/host/dist/document-parser-worker.js +215 -0
- package/apps/host/dist/frontend-runtime-worker.js +1755 -0
- package/apps/host/dist/index.js +23380 -0
- package/apps/web/dist/assets/LocalMonacoEditor-CXkKwxiP.css +1 -0
- package/apps/web/dist/assets/LocalMonacoEditor-WKAHQmzG.js +940 -0
- package/apps/web/dist/assets/LogsView-BFFFSFB-.js +1 -0
- package/apps/web/dist/assets/PluginsView-DM_ZnENS.js +1 -0
- package/apps/web/dist/assets/SettingsView-Cb5Q4ile.js +1 -0
- package/apps/web/dist/assets/abap-DLDM7-KI.js +1 -0
- package/apps/web/dist/assets/apex-DNDY2TF8.js +1 -0
- package/apps/web/dist/assets/azcli-Y6nb8tq_.js +1 -0
- package/apps/web/dist/assets/bat-BwHxbl9M.js +1 -0
- package/apps/web/dist/assets/bicep-CFznDFnq.js +2 -0
- package/apps/web/dist/assets/cameligo-Bf6VGUru.js +1 -0
- package/apps/web/dist/assets/clojure-Dnu-v4kV.js +1 -0
- package/apps/web/dist/assets/codicon-Brq4_Ui5.ttf +0 -0
- package/apps/web/dist/assets/coffee-Bd8akH9Z.js +1 -0
- package/apps/web/dist/assets/cpp-BbWJElDN.js +1 -0
- package/apps/web/dist/assets/csharp-Co3qMtFm.js +1 -0
- package/apps/web/dist/assets/csp-D-4FJmMZ.js +1 -0
- package/apps/web/dist/assets/css-DdJfP1eB.js +3 -0
- package/apps/web/dist/assets/css.worker-11mhRRLC.js +97 -0
- package/apps/web/dist/assets/cssMode-L5f5LAEn.js +1 -0
- package/apps/web/dist/assets/cypher-cTPe9QuQ.js +1 -0
- package/apps/web/dist/assets/dart-BOtBlQCF.js +1 -0
- package/apps/web/dist/assets/dockerfile-BG73LgW2.js +1 -0
- package/apps/web/dist/assets/ecl-BEgZUVRK.js +1 -0
- package/apps/web/dist/assets/editor.worker-DysRFSaV.js +30 -0
- package/apps/web/dist/assets/elixir-BkW5O-1t.js +1 -0
- package/apps/web/dist/assets/flow9-BeJ5waoc.js +1 -0
- package/apps/web/dist/assets/freemarker2-UOwftS75.js +3 -0
- package/apps/web/dist/assets/fsharp-PahG7c26.js +1 -0
- package/apps/web/dist/assets/go-acbASCJo.js +1 -0
- package/apps/web/dist/assets/graphql-BxJiqAUM.js +1 -0
- package/apps/web/dist/assets/handlebars-C_E41ffr.js +1 -0
- package/apps/web/dist/assets/hcl-DtV1sZF8.js +1 -0
- package/apps/web/dist/assets/html-HsrixH2x.js +1 -0
- package/apps/web/dist/assets/html.worker-C8Tw8ncf.js +474 -0
- package/apps/web/dist/assets/htmlMode-DipGmEOi.js +1 -0
- package/apps/web/dist/assets/index-COUF63cT.js +213 -0
- package/apps/web/dist/assets/index-SNSmF2-c.css +1 -0
- package/apps/web/dist/assets/ini-Kd9XrMLS.js +1 -0
- package/apps/web/dist/assets/java-CXBNlu9o.js +1 -0
- package/apps/web/dist/assets/javascript-D36WEqF9.js +1 -0
- package/apps/web/dist/assets/json.worker-BzkYZgJ7.js +62 -0
- package/apps/web/dist/assets/jsonMode-D9BrIats.js +7 -0
- package/apps/web/dist/assets/julia-cl7-CwDS.js +1 -0
- package/apps/web/dist/assets/kotlin-s7OhZKlX.js +1 -0
- package/apps/web/dist/assets/less-9HpZscsL.js +2 -0
- package/apps/web/dist/assets/lexon-OrD6JF1K.js +1 -0
- package/apps/web/dist/assets/liquid-DES89Tjc.js +1 -0
- package/apps/web/dist/assets/lock-keyhole-jo-FJcGq.js +1 -0
- package/apps/web/dist/assets/lspLanguageFeatures-YKkgcn4q.js +4 -0
- package/apps/web/dist/assets/lua-Cyyb5UIc.js +1 -0
- package/apps/web/dist/assets/m3-B8OfTtLu.js +1 -0
- package/apps/web/dist/assets/markdown-BFxVWTOG.js +1 -0
- package/apps/web/dist/assets/mdx-D1Bfax8e.js +1 -0
- package/apps/web/dist/assets/mips-CiqrrVzr.js +1 -0
- package/apps/web/dist/assets/msdax-DmeGPVcC.js +1 -0
- package/apps/web/dist/assets/mysql-C_tMU-Nz.js +1 -0
- package/apps/web/dist/assets/objective-c-BDtDVThU.js +1 -0
- package/apps/web/dist/assets/pascal-vHIfCaH5.js +1 -0
- package/apps/web/dist/assets/pascaligo-DtZ0uQbO.js +1 -0
- package/apps/web/dist/assets/perl-Ub6l9XKa.js +1 -0
- package/apps/web/dist/assets/pgsql-BlNEE0v7.js +1 -0
- package/apps/web/dist/assets/php-BBUBE1dy.js +1 -0
- package/apps/web/dist/assets/pla-DSh2-awV.js +1 -0
- package/apps/web/dist/assets/postiats-CocnycG-.js +1 -0
- package/apps/web/dist/assets/powerquery-tScXyioY.js +1 -0
- package/apps/web/dist/assets/powershell-COWaemsV.js +1 -0
- package/apps/web/dist/assets/protobuf-Brw8urJB.js +2 -0
- package/apps/web/dist/assets/pug-8SOpv6rk.js +1 -0
- package/apps/web/dist/assets/python-DKhPhY0N.js +1 -0
- package/apps/web/dist/assets/qsharp-Bw9ernYp.js +1 -0
- package/apps/web/dist/assets/r-j7ic8hl3.js +1 -0
- package/apps/web/dist/assets/razor-Cha8qrI6.js +1 -0
- package/apps/web/dist/assets/redis-Bu5POkcn.js +1 -0
- package/apps/web/dist/assets/redshift-Bs9aos_-.js +1 -0
- package/apps/web/dist/assets/restructuredtext-CqXO7rUv.js +1 -0
- package/apps/web/dist/assets/ruby-zBfavPgS.js +1 -0
- package/apps/web/dist/assets/rust-BzKRNQWT.js +1 -0
- package/apps/web/dist/assets/sb-BBc9UKZt.js +1 -0
- package/apps/web/dist/assets/scala-D9hQfWCl.js +1 -0
- package/apps/web/dist/assets/scheme-BPhDTwHR.js +1 -0
- package/apps/web/dist/assets/scss-CBJaRo0y.js +3 -0
- package/apps/web/dist/assets/shell-DiJ1NA_G.js +1 -0
- package/apps/web/dist/assets/solidity-Db0IVjzk.js +1 -0
- package/apps/web/dist/assets/sophia-CnS9iZB_.js +1 -0
- package/apps/web/dist/assets/sparql-CJmd_6j2.js +1 -0
- package/apps/web/dist/assets/sql-ClhHkBeG.js +1 -0
- package/apps/web/dist/assets/st-CHwy0fLd.js +1 -0
- package/apps/web/dist/assets/swift-CnmFD0ga.js +1 -0
- package/apps/web/dist/assets/systemverilog-Bs9z6M-B.js +1 -0
- package/apps/web/dist/assets/tcl-Dm6ycUr_.js +1 -0
- package/apps/web/dist/assets/ts.worker-Bupg6llt.js +67722 -0
- package/apps/web/dist/assets/tsMode-CEhssXv8.js +11 -0
- package/apps/web/dist/assets/twig-Csy3S7wG.js +1 -0
- package/apps/web/dist/assets/typescript-DWqmw58R.js +1 -0
- package/apps/web/dist/assets/typespec-Btyra-wh.js +1 -0
- package/apps/web/dist/assets/vb-Db0cS2oM.js +1 -0
- package/apps/web/dist/assets/wgsl-DumH7NcR.js +298 -0
- package/apps/web/dist/assets/workers-BmZvNFRq.js +1 -0
- package/apps/web/dist/assets/xml-WQTAFV9b.js +1 -0
- package/apps/web/dist/assets/yaml-Ke2Lf79d.js +1 -0
- package/apps/web/dist/index.html +15 -0
- package/bin/launch.mjs +71 -0
- package/bin/locode-mini.mjs +12 -0
- package/bin/options.mjs +49 -0
- package/bin/restore-assets.mjs +40 -0
- package/bundled-plugins/fast-frontend/.gitignore +2 -0
- package/bundled-plugins/fast-frontend/README.md +29 -0
- package/bundled-plugins/fast-frontend/harness.plugin.json +123 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/PUBLIC_CONTRACT.md +139 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/contracts/app.schema.json +24 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/contracts/route.schema.json +52 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/examples/app.json +9 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/examples/route.json +8 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/index.html +17 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/package.json +31 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/scripts/smoke-build.mjs +40 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/src/inspector/index.ts +612 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/src/main.tsx +17 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/src/sdk/index.ts +1028 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/src/shell/RuntimeApp.tsx +380 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/src/shell/runtime.css +246 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/tsconfig.json +20 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/types/runtime.consumer-smoke.ts +70 -0
- package/bundled-plugins/fast-frontend/runtimes/antd-admin/types/runtime.d.ts +227 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/.gitignore +9 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/.harness/template.json +10 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/index.html +13 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/package.json +34 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/pnpm-lock.yaml +1451 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/pnpm-workspace.yaml +4 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/public/generated/.gitkeep +1 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/scripts/validate-routes.mjs +153 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/app/App.tsx +27 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/app/NotFoundPage.tsx +15 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/features/.gitkeep +1 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/layouts/MainLayout.tsx +77 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/main.tsx +14 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/mocks/dashboard.ts +48 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/pages/dashboard/page.tsx +151 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/pages/dashboard/route.ts +9 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/routes/definition.ts +21 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/routes/menu.tsx +44 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/routes/registry.ts +48 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/routes/router.tsx +34 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/shared/components/business/.gitkeep +1 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/shared/components/business/BusinessProviders.tsx +9 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/styles/global.css +180 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/src/vite-env.d.ts +1 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/tsconfig.app.json +24 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/tsconfig.json +7 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/tsconfig.node.json +17 -0
- package/bundled-plugins/fast-frontend/templates/antd-admin/vite.config.ts +39 -0
- package/npm-shrinkwrap.json +9737 -0
- package/package.json +80 -0
- package/release-integrity.json +168 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
Harness Studio is an independent TypeScript implementation. The following fixed upstream revisions were studied for architecture and interoperability; their source code was not translated into this repository.
|
|
4
|
+
|
|
5
|
+
| Project | Revision | License | Use in Harness Studio |
|
|
6
|
+
|---|---|---|---|
|
|
7
|
+
| [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness/tree/b150a551b8d465e31e418e1b2eaf5e79bbb7d28e) | `b150a55` | MIT, copyright DeepSeek | Plugin lifecycle, capability and effect-cleanup concepts |
|
|
8
|
+
| [Pi](https://github.com/earendil-works/pi/tree/c49906ec77788625aacbdc53ebca6fbe65bd20f5) | `c49906e` | MIT, copyright Mario Zechner | Provider abstraction and prompt-cache concepts; `@earendil-works/pi-ai` is used as a package dependency |
|
|
9
|
+
| [OpenAI Codex](https://github.com/openai/codex/tree/343074d4207d572809bd8cea15f4be1d09d98e0b) | `343074d` | Apache-2.0, copyright OpenAI | Thread/Turn/Item, approval and event-protocol concepts |
|
|
10
|
+
|
|
11
|
+
The referenced Codex revision's upstream `NOTICE` identifies OpenAI Codex and also attributes Ratatui (MIT) to Florian Dehau and the Ratatui Developers. Harness Studio does not incorporate the referenced Rust/Ratatui source, but retains this upstream attribution here for clarity.
|
|
12
|
+
|
|
13
|
+
Redistributions must also retain the licenses and notices of packages resolved by `pnpm-lock.yaml`. This document is informational and does not replace any dependency's license text.
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// apps/host/src/document-parser-worker.ts
|
|
2
|
+
import { parentPort } from "node:worker_threads";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { createCanvas } from "@napi-rs/canvas";
|
|
5
|
+
import ExcelJS from "exceljs";
|
|
6
|
+
import mammoth from "mammoth";
|
|
7
|
+
import { getDocument, OPS } from "pdfjs-dist/legacy/build/pdf.mjs";
|
|
8
|
+
var MAX_TEXT_BYTES = 5 * 1024 * 1024;
|
|
9
|
+
var MAX_PDF_PAGES = 100;
|
|
10
|
+
var MAX_EXCEL_SHEETS = 32;
|
|
11
|
+
var MAX_EXCEL_CELLS = 1e5;
|
|
12
|
+
var MAX_VISUALS = 4;
|
|
13
|
+
var SAFE_EMBEDDED_IMAGE_TYPES = /* @__PURE__ */ new Set(["image/png", "image/jpeg", "image/webp", "image/gif"]);
|
|
14
|
+
var PDF_STANDARD_FONT_DATA_URL = `${fileURLToPath(new URL(
|
|
15
|
+
"../../standard_fonts/",
|
|
16
|
+
import.meta.resolve("pdfjs-dist/legacy/build/pdf.mjs")
|
|
17
|
+
)).replaceAll("\\", "/").replace(/\/+$/u, "")}/`;
|
|
18
|
+
function truncateUtf8(value) {
|
|
19
|
+
const bytes = Buffer.from(value, "utf8");
|
|
20
|
+
if (bytes.byteLength <= MAX_TEXT_BYTES) return { text: value, truncated: false };
|
|
21
|
+
let end = MAX_TEXT_BYTES;
|
|
22
|
+
while (end > 0 && (bytes[end] & 192) === 128) end -= 1;
|
|
23
|
+
return { text: bytes.subarray(0, end).toString("utf8"), truncated: true };
|
|
24
|
+
}
|
|
25
|
+
function mediaTypeForExtension(extension) {
|
|
26
|
+
return {
|
|
27
|
+
png: "image/png",
|
|
28
|
+
jpeg: "image/jpeg",
|
|
29
|
+
jpg: "image/jpeg",
|
|
30
|
+
webp: "image/webp",
|
|
31
|
+
gif: "image/gif"
|
|
32
|
+
}[extension.toLowerCase()];
|
|
33
|
+
}
|
|
34
|
+
async function parseDocx(data) {
|
|
35
|
+
const visuals = [];
|
|
36
|
+
let discoveredVisuals = 0;
|
|
37
|
+
const options = {
|
|
38
|
+
externalFileAccess: false,
|
|
39
|
+
convertImage: mammoth.images.imgElement(async (image) => {
|
|
40
|
+
discoveredVisuals += 1;
|
|
41
|
+
if (visuals.length < MAX_VISUALS && SAFE_EMBEDDED_IMAGE_TYPES.has(image.contentType.toLowerCase())) {
|
|
42
|
+
visuals.push({
|
|
43
|
+
data: await image.readAsBuffer(),
|
|
44
|
+
mediaType: image.contentType,
|
|
45
|
+
label: `Word \u5D4C\u5165\u56FE\u7247 ${discoveredVisuals}`
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return { src: "" };
|
|
49
|
+
})
|
|
50
|
+
};
|
|
51
|
+
const [raw] = await Promise.all([
|
|
52
|
+
mammoth.extractRawText({ buffer: data }),
|
|
53
|
+
mammoth.convertToHtml({ buffer: data }, options)
|
|
54
|
+
]);
|
|
55
|
+
const text = truncateUtf8(raw.value.trim());
|
|
56
|
+
return {
|
|
57
|
+
text: text.text,
|
|
58
|
+
textTruncated: text.truncated,
|
|
59
|
+
visuals,
|
|
60
|
+
visualsTruncated: discoveredVisuals > visuals.length
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function excelCellText(value) {
|
|
64
|
+
if (value === null || value === void 0) return "";
|
|
65
|
+
if (value instanceof Date) return value.toISOString();
|
|
66
|
+
if (typeof value === "object") {
|
|
67
|
+
const record = value;
|
|
68
|
+
if (typeof record.formula === "string") {
|
|
69
|
+
const result = record.result === void 0 ? "" : excelCellText(record.result);
|
|
70
|
+
return `=${record.formula}${result ? ` => ${result}` : ""}`;
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(record.richText)) {
|
|
73
|
+
return record.richText.map((part) => typeof part === "object" && part !== null && "text" in part ? String(part.text) : "").join("");
|
|
74
|
+
}
|
|
75
|
+
if (typeof record.text === "string") return record.text;
|
|
76
|
+
if (record.hyperlink !== void 0) return String(record.text ?? record.hyperlink);
|
|
77
|
+
try {
|
|
78
|
+
return JSON.stringify(value);
|
|
79
|
+
} catch {
|
|
80
|
+
return String(value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return String(value);
|
|
84
|
+
}
|
|
85
|
+
async function parseXlsx(data) {
|
|
86
|
+
const workbook = new ExcelJS.Workbook();
|
|
87
|
+
await workbook.xlsx.load(data, {
|
|
88
|
+
ignoreNodes: ["dataValidations", "extLst", "headerFooter", "pageMargins", "pageSetup", "printOptions"]
|
|
89
|
+
});
|
|
90
|
+
const availableSheets = workbook.worksheets.length;
|
|
91
|
+
const sheets = workbook.worksheets.slice(0, MAX_EXCEL_SHEETS);
|
|
92
|
+
const lines = [];
|
|
93
|
+
let textBytes = 0;
|
|
94
|
+
let cellCount = 0;
|
|
95
|
+
let textTruncated = availableSheets > sheets.length;
|
|
96
|
+
const append = (line) => {
|
|
97
|
+
const bytes = Buffer.byteLength(`${line}
|
|
98
|
+
`, "utf8");
|
|
99
|
+
if (textBytes + bytes > MAX_TEXT_BYTES) {
|
|
100
|
+
textTruncated = true;
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
lines.push(line);
|
|
104
|
+
textBytes += bytes;
|
|
105
|
+
return true;
|
|
106
|
+
};
|
|
107
|
+
let stopped = false;
|
|
108
|
+
for (const sheet of sheets) {
|
|
109
|
+
if (stopped || !append(`## \u5DE5\u4F5C\u8868\uFF1A${sheet.name}`)) break;
|
|
110
|
+
sheet.eachRow({ includeEmpty: false }, (row, rowNumber) => {
|
|
111
|
+
if (stopped) return;
|
|
112
|
+
const cells = [];
|
|
113
|
+
row.eachCell({ includeEmpty: false }, (cell, columnNumber) => {
|
|
114
|
+
if (cellCount >= MAX_EXCEL_CELLS) return;
|
|
115
|
+
const value = excelCellText(cell.value);
|
|
116
|
+
if (!value) return;
|
|
117
|
+
cellCount += 1;
|
|
118
|
+
cells.push(`${sheet.getColumn(columnNumber).letter}${rowNumber}=${value}`);
|
|
119
|
+
});
|
|
120
|
+
if (cells.length > 0 && !append(cells.join(" "))) stopped = true;
|
|
121
|
+
if (cellCount >= MAX_EXCEL_CELLS) {
|
|
122
|
+
textTruncated = true;
|
|
123
|
+
stopped = true;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const media = workbook.model.media.filter((entry) => entry.type === "image" && entry.buffer?.byteLength > 0);
|
|
128
|
+
const visuals = media.slice(0, MAX_VISUALS).flatMap((entry, index) => {
|
|
129
|
+
const mediaType = mediaTypeForExtension(entry.extension);
|
|
130
|
+
return mediaType ? [{ data: new Uint8Array(entry.buffer), mediaType, label: `Excel \u5D4C\u5165\u56FE\u7247 ${index + 1}` }] : [];
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
text: lines.join("\n"),
|
|
134
|
+
textTruncated,
|
|
135
|
+
visuals,
|
|
136
|
+
visualsTruncated: media.length > visuals.length,
|
|
137
|
+
sheetCount: availableSheets
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
async function parsePdf(data) {
|
|
141
|
+
const loadingTask = getDocument({
|
|
142
|
+
data: new Uint8Array(data),
|
|
143
|
+
disableAutoFetch: true,
|
|
144
|
+
disableStream: true,
|
|
145
|
+
useSystemFonts: false,
|
|
146
|
+
standardFontDataUrl: PDF_STANDARD_FONT_DATA_URL
|
|
147
|
+
});
|
|
148
|
+
const document = await loadingTask.promise;
|
|
149
|
+
try {
|
|
150
|
+
const pageCount = Math.min(document.numPages, MAX_PDF_PAGES);
|
|
151
|
+
const pages = [];
|
|
152
|
+
for (let pageNumber = 1; pageNumber <= pageCount; pageNumber += 1) {
|
|
153
|
+
const page = await document.getPage(pageNumber);
|
|
154
|
+
const [content, operators] = await Promise.all([page.getTextContent(), page.getOperatorList()]);
|
|
155
|
+
const text2 = content.items.map((item) => "str" in item ? item.str : "").join(" ").replace(/\s+/gu, " ").trim();
|
|
156
|
+
const hasImage = operators.fnArray.some((fn) => fn === OPS.paintImageXObject || fn === OPS.paintInlineImageXObject || fn === OPS.paintImageMaskXObject);
|
|
157
|
+
pages.push({ page: pageNumber, text: text2, hasImage });
|
|
158
|
+
page.cleanup();
|
|
159
|
+
}
|
|
160
|
+
const fullText = pages.map((page) => `## \u7B2C ${page.page} \u9875
|
|
161
|
+
${page.text}`).join("\n\n");
|
|
162
|
+
const text = truncateUtf8(fullText);
|
|
163
|
+
const selected = [...pages].sort((left, right) => {
|
|
164
|
+
if (left.page === 1) return -1;
|
|
165
|
+
if (right.page === 1) return 1;
|
|
166
|
+
if (left.hasImage !== right.hasImage) return left.hasImage ? -1 : 1;
|
|
167
|
+
return left.text.length - right.text.length;
|
|
168
|
+
}).slice(0, MAX_VISUALS).sort((left, right) => left.page - right.page);
|
|
169
|
+
const visuals = [];
|
|
170
|
+
for (const selectedPage of selected) {
|
|
171
|
+
const page = await document.getPage(selectedPage.page);
|
|
172
|
+
const unscaled = page.getViewport({ scale: 1 });
|
|
173
|
+
const scale = Math.min(2, 1600 / Math.max(unscaled.width, unscaled.height));
|
|
174
|
+
const viewport = page.getViewport({ scale });
|
|
175
|
+
const canvas = createCanvas(Math.ceil(viewport.width), Math.ceil(viewport.height));
|
|
176
|
+
await page.render({ canvas, viewport }).promise;
|
|
177
|
+
visuals.push({
|
|
178
|
+
data: canvas.toBuffer("image/png"),
|
|
179
|
+
mediaType: "image/png",
|
|
180
|
+
label: `PDF \u7B2C ${selectedPage.page} \u9875`,
|
|
181
|
+
page: selectedPage.page
|
|
182
|
+
});
|
|
183
|
+
page.cleanup();
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
text: text.text,
|
|
187
|
+
textTruncated: text.truncated || document.numPages > MAX_PDF_PAGES,
|
|
188
|
+
visuals,
|
|
189
|
+
visualsTruncated: pages.length > visuals.length,
|
|
190
|
+
pageCount: document.numPages
|
|
191
|
+
};
|
|
192
|
+
} finally {
|
|
193
|
+
await loadingTask.destroy();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
async function parseDocument(input) {
|
|
197
|
+
const data = Buffer.from(input.data);
|
|
198
|
+
if (input.format === "docx") return parseDocx(data);
|
|
199
|
+
if (input.format === "xlsx") return parseXlsx(data);
|
|
200
|
+
return parsePdf(data);
|
|
201
|
+
}
|
|
202
|
+
if (parentPort) {
|
|
203
|
+
parentPort.once("message", (input) => {
|
|
204
|
+
void parseDocument(input).then(
|
|
205
|
+
(result) => parentPort.postMessage({ ok: true, result }),
|
|
206
|
+
(error) => parentPort.postMessage({
|
|
207
|
+
ok: false,
|
|
208
|
+
error: error instanceof Error ? error.message : String(error)
|
|
209
|
+
})
|
|
210
|
+
);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
export {
|
|
214
|
+
parseDocument
|
|
215
|
+
};
|