gencow 0.1.298 → 0.2.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/bin/gencow.mjs +0 -6
- package/core/index.js +261 -1447
- package/core/package.json +1 -1
- package/dashboard/assets/{index-BnBn8uo4.js → index-DYqUzCmS.js} +3 -3
- package/dashboard/assets/index-OH2xW1ZG.css +2 -0
- package/dashboard/index.html +2 -2
- package/docs/packaging.md +14 -4
- package/lib/cli-artifact-guard.mjs +53 -3
- package/lib/cli-command-runner.d.mts +11 -0
- package/lib/cli-dev-runtime.mjs +1 -0
- package/lib/cli-project-runtime.mjs +26 -14
- package/lib/codegen-command.mjs +16 -0
- package/lib/coding-guardrails.mjs +2 -0
- package/lib/component-readme.mjs +4 -4
- package/lib/dashboard-artifact.mjs +65 -0
- package/lib/dashboard-assets.mjs +20 -0
- package/lib/db-command.mjs +121 -80
- package/lib/db-migration-command-steps.mjs +19 -16
- package/lib/db-push-command.mjs +1 -1
- package/lib/deploy-codegen-preflight.mjs +1 -0
- package/lib/deploy-cron-manifest-diagnostic.mjs +93 -0
- package/lib/deploy-failure-diagnostic.mjs +4 -43
- package/lib/deploy-route-target-rejection-diagnostic.mjs +43 -0
- package/lib/dev-cloud-command.mjs +1 -1
- package/lib/dev-local-command.mjs +19 -56
- package/lib/document-local-converter.mjs +11 -0
- package/lib/jobs-command.mjs +33 -1
- package/lib/local-dashboard-plan.mjs +60 -0
- package/lib/local-database-commands.mjs +127 -0
- package/lib/local-development-migrations.mjs +157 -0
- package/lib/local-drizzle-database.config.mjs +10 -0
- package/lib/local-runtime-features.mjs +12 -0
- package/lib/rag-selection.mjs +7 -0
- package/lib/readme-codegen.mjs +2 -2
- package/lib/runtime-local-worker.mjs +116 -0
- package/lib/runtime-mode.mjs +3 -0
- package/lib/standalone-codegen-adapter.mjs +91 -0
- package/lib/ts-codegen-import-extension.mjs +228 -19
- package/package.json +16 -9
- package/runtime/config.mjs +218 -2
- package/runtime/dependency-lock.mjs +5 -1
- package/runtime/engine/chunk-2HA4CUMS.mjs +90 -0
- package/runtime/engine/chunk-2JLO3KMM.mjs +305 -0
- package/runtime/engine/chunk-2SR2M7SR.mjs +1045 -0
- package/runtime/engine/chunk-3D7MHEUC.mjs +253 -0
- package/runtime/engine/chunk-3R7COBWG.mjs +309 -0
- package/runtime/engine/chunk-3ZKA54UW.mjs +1560 -0
- package/runtime/engine/chunk-45I4YR2J.mjs +9 -0
- package/runtime/engine/chunk-4MLRXO7F.mjs +74 -0
- package/runtime/engine/chunk-5575APCJ.mjs +215 -0
- package/runtime/engine/chunk-5GUJZQF3.mjs +114 -0
- package/runtime/engine/chunk-5MXY3T75.mjs +428 -0
- package/runtime/engine/chunk-5TACQ2US.mjs +44 -0
- package/runtime/engine/chunk-66G4YEDT.mjs +3104 -0
- package/runtime/engine/chunk-67YO53WW.mjs +1426 -0
- package/runtime/engine/chunk-6GQRM7XY.mjs +87 -0
- package/runtime/engine/chunk-6JO72SPP.mjs +68 -0
- package/runtime/engine/chunk-6W653LXI.mjs +13 -0
- package/runtime/engine/chunk-7ITSMI3S.mjs +69 -0
- package/runtime/engine/chunk-7KUJ2H2S.mjs +128 -0
- package/runtime/engine/chunk-A5BMQ5IC.mjs +45 -0
- package/runtime/engine/chunk-ADF3UR6L.mjs +428 -0
- package/runtime/engine/chunk-AEBW7XZJ.mjs +4510 -0
- package/runtime/engine/chunk-AGPQ5XIV.mjs +156 -0
- package/runtime/engine/chunk-CPU4OZSX.mjs +753 -0
- package/runtime/engine/chunk-CSWPPWBL.mjs +50 -0
- package/runtime/engine/chunk-DPWXSMWO.mjs +53 -0
- package/runtime/engine/chunk-EAIELFKU.mjs +151 -0
- package/runtime/engine/chunk-EDU5CBKT.mjs +10 -0
- package/runtime/engine/chunk-EQCTAET7.mjs +173 -0
- package/runtime/engine/chunk-EQCXDF2Z.mjs +70 -0
- package/runtime/engine/chunk-F3G4UGV4.mjs +330 -0
- package/runtime/engine/chunk-F62NUNOO.mjs +26 -0
- package/runtime/engine/chunk-FKPNIFRP.mjs +2492 -0
- package/runtime/engine/chunk-FM23RGVI.mjs +81 -0
- package/runtime/engine/chunk-FW5AK4YP.mjs +1041 -0
- package/runtime/engine/chunk-GDWYAGWV.mjs +23 -0
- package/runtime/engine/chunk-GO5PZUYW.mjs +53 -0
- package/runtime/engine/chunk-GOLP7OSG.mjs +340 -0
- package/runtime/engine/chunk-GSLM44EF.mjs +242 -0
- package/runtime/engine/chunk-GYAJ7OX2.mjs +125 -0
- package/runtime/engine/chunk-I3GWR43K.mjs +14 -0
- package/runtime/engine/chunk-IFMOTJNU.mjs +106 -0
- package/runtime/engine/chunk-IKQKBE2G.mjs +128 -0
- package/runtime/engine/chunk-ITH2NEKM.mjs +282 -0
- package/runtime/engine/chunk-J6SDRCG4.mjs +96 -0
- package/runtime/engine/chunk-JODRNM3S.mjs +29596 -0
- package/runtime/engine/chunk-JUDKX53X.mjs +137 -0
- package/runtime/engine/chunk-K2YZVPJ4.mjs +827 -0
- package/runtime/engine/chunk-KEZJUXEE.mjs +151 -0
- package/runtime/engine/chunk-KVLTJFCH.mjs +234 -0
- package/runtime/engine/chunk-KX2PRQPS.mjs +126 -0
- package/runtime/engine/chunk-LEW5ZMLR.mjs +76 -0
- package/runtime/engine/chunk-LINVSJEM.mjs +175 -0
- package/runtime/engine/chunk-M7K2ICSM.mjs +11 -0
- package/runtime/engine/chunk-MZXDXSDC.mjs +292 -0
- package/runtime/engine/chunk-N2TA2IK2.mjs +678 -0
- package/runtime/engine/chunk-N4LTCKJP.mjs +695 -0
- package/runtime/engine/chunk-N6GCLOUX.mjs +13 -0
- package/runtime/engine/chunk-NAG2OXS5.mjs +37 -0
- package/runtime/engine/chunk-NNZ2QJUG.mjs +381 -0
- package/runtime/engine/chunk-NZPPLDNW.mjs +15 -0
- package/runtime/engine/chunk-ODWM57W6.mjs +138 -0
- package/runtime/engine/chunk-P5IIXAIE.mjs +23 -0
- package/runtime/engine/chunk-PBOXB2BV.mjs +6 -0
- package/runtime/engine/chunk-PMOJLN2P.mjs +8 -0
- package/runtime/engine/chunk-PZFMKFHO.mjs +180 -0
- package/runtime/engine/chunk-QL2TQX2N.mjs +114 -0
- package/runtime/engine/chunk-RITT2RSJ.mjs +258 -0
- package/runtime/engine/chunk-RODBVJ5M.mjs +135 -0
- package/runtime/engine/chunk-RW65MPVY.mjs +261 -0
- package/runtime/engine/chunk-S67UYPB6.mjs +25 -0
- package/runtime/engine/chunk-SVJCOUI3.mjs +47 -0
- package/runtime/engine/chunk-TCN3ZASV.mjs +957 -0
- package/runtime/engine/chunk-U2NRG6KC.mjs +32 -0
- package/runtime/engine/chunk-UM3F7HUV.mjs +432 -0
- package/runtime/engine/chunk-VWU7JJ3P.mjs +146 -0
- package/runtime/engine/chunk-WDTKPPMU.mjs +310 -0
- package/runtime/engine/chunk-WJOO4XTB.mjs +31 -0
- package/runtime/engine/chunk-XNFVL2OY.mjs +63 -0
- package/runtime/engine/chunk-XPWJXP2J.mjs +58 -0
- package/runtime/engine/chunk-Y3HSBJ3I.mjs +1132 -0
- package/runtime/engine/chunk-YMH4RYSX.mjs +317 -0
- package/runtime/engine/chunk-YNOMPSGS.mjs +8 -0
- package/runtime/engine/chunk-YRBAYTEZ.mjs +251 -0
- package/runtime/engine/chunk-YRHM272W.mjs +528 -0
- package/runtime/engine/chunk-ZQUZAGIQ.mjs +149 -0
- package/runtime/engine/chunk-ZXMM7QFP.mjs +176 -0
- package/runtime/engine/chunk-ZZJSIPV2.mjs +262 -0
- package/runtime/engine/composition.mjs +325 -0
- package/runtime/engine/document-local.mjs +504 -0
- package/runtime/engine/graph.json +4134 -0
- package/runtime/engine/index.mjs +265 -0
- package/runtime/engine/local-postgres-support.mjs +69 -0
- package/runtime/engine/local.mjs +3275 -0
- package/runtime/engine/pglite-directory-lock.mjs +8 -0
- package/runtime/engine/rag.mjs +11 -0
- package/runtime/engine/search.mjs +18 -0
- package/runtime/framework-migrations/foundation-bootstrap/rag.sql +115 -0
- package/runtime/framework-migrations/foundation-bootstrap/workflow.sql +194 -0
- package/runtime/framework-migrations/journal-bootstrap/migration.sql +26 -0
- package/runtime/framework-migrations/manifest.json +56 -0
- package/runtime/framework-migrations/rag/20261006111509_mushy_warhawk/migration.sql +121 -0
- package/runtime/framework-migrations/rag/20261013234621_rag_v2_lifecycle_markers/migration.sql +6 -0
- package/runtime/framework-migrations/workflow/20261006111508_perpetual_star_brand/migration.sql +31 -0
- package/runtime/runtime-metadata.json +27 -0
- package/runtime/tooling.mjs +10549 -10543
- package/scripts/bundle-runtime-config.mjs +23 -0
- package/scripts/bundle-runtime.mjs +100 -0
- package/scripts/bundle-runtime.test.mjs +33 -0
- package/scripts/bundle-shared.mjs +3 -0
- package/scripts/copy-dashboard.mjs +16 -0
- package/scripts/pre-publish-check.mjs +23 -11
- package/templateFeature/ai/ai-embedding-result.ts +82 -0
- package/templateFeature/ai/ai-facade.ts +24 -7
- package/templateFeature/ai/ai-provider.ts +17 -1
- package/templateFeature/ai/ai.ts +1 -0
- package/templateFeature/ai/manifest.json +4 -3
- package/templateFeature/reranker/manifest.json +1 -1
- package/templateFeature/reranker/reranker.ts +4 -3
- package/templateFeature/sso/sso-auth-callback.tsx +37 -5
- package/templateFeature/tools/manifest.json +2 -1
- package/templateFeature/tools/tools.ts +3 -3
- package/dashboard/assets/index-C6-BpKKR.css +0 -2
- package/runtime/server.mjs +0 -14320
- package/scripts/bundle-server.mjs +0 -223
- package/server/config.js +0 -1
- package/server/index.js +0 -1
- package/server/package.json +0 -6
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DocumentConversionError
|
|
3
|
+
} from "./chunk-6JO72SPP.mjs";
|
|
4
|
+
import "./chunk-AEBW7XZJ.mjs";
|
|
5
|
+
|
|
6
|
+
// ../runtime/dist/adapters/document/local/peer-loaders.js
|
|
7
|
+
import { readFile } from "node:fs/promises";
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
import { pathToFileURL } from "node:url";
|
|
11
|
+
async function installedPeer(require2, name) {
|
|
12
|
+
let entry;
|
|
13
|
+
try {
|
|
14
|
+
entry = require2.resolve(name);
|
|
15
|
+
} catch {
|
|
16
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_MISSING", void 0, name);
|
|
17
|
+
}
|
|
18
|
+
let directory2 = dirname(entry);
|
|
19
|
+
for (let depth = 0; depth < 6; depth += 1) {
|
|
20
|
+
try {
|
|
21
|
+
const manifest = JSON.parse(await readFile(join(directory2, "package.json"), "utf8"));
|
|
22
|
+
if (manifest && typeof manifest === "object" && "name" in manifest && manifest.name === name && "version" in manifest && typeof manifest.version === "string")
|
|
23
|
+
return { version: manifest.version, entry };
|
|
24
|
+
} catch {
|
|
25
|
+
}
|
|
26
|
+
directory2 = dirname(directory2);
|
|
27
|
+
}
|
|
28
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", void 0, name);
|
|
29
|
+
}
|
|
30
|
+
async function importPeer(entry, name) {
|
|
31
|
+
try {
|
|
32
|
+
return await import(pathToFileURL(entry).href);
|
|
33
|
+
} catch {
|
|
34
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", void 0, name);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function isPdfModule(module) {
|
|
38
|
+
return !!module && typeof module === "object" && "PDFParse" in module && typeof module.PDFParse === "function";
|
|
39
|
+
}
|
|
40
|
+
function createLocalDocumentParserLoaders(anchor = import.meta.url) {
|
|
41
|
+
const require2 = createRequire(anchor);
|
|
42
|
+
return Object.freeze({
|
|
43
|
+
async pdf() {
|
|
44
|
+
const { version, entry } = await installedPeer(require2, "pdf-parse");
|
|
45
|
+
const module = await importPeer(entry, "pdf-parse");
|
|
46
|
+
if (!isPdfModule(module))
|
|
47
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", void 0, "pdf-parse");
|
|
48
|
+
return { version, PDFParse: module.PDFParse };
|
|
49
|
+
},
|
|
50
|
+
async docx() {
|
|
51
|
+
const { version, entry } = await installedPeer(require2, "mammoth");
|
|
52
|
+
const module = await importPeer(entry, "mammoth");
|
|
53
|
+
const mammoth = module && typeof module === "object" && "default" in module ? module.default : module;
|
|
54
|
+
if (!mammoth || typeof mammoth !== "object" || !("convertToMarkdown" in mammoth) || typeof mammoth.convertToMarkdown !== "function")
|
|
55
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", void 0, "mammoth");
|
|
56
|
+
const convert = mammoth.convertToMarkdown;
|
|
57
|
+
return {
|
|
58
|
+
version,
|
|
59
|
+
async convertToMarkdown(input) {
|
|
60
|
+
const result = await convert(input);
|
|
61
|
+
if (!result || typeof result !== "object" || !("value" in result) || typeof result.value !== "string")
|
|
62
|
+
throw new Error("Invalid converter result");
|
|
63
|
+
return { value: result.value };
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
var defaultParserLoaders = createLocalDocumentParserLoaders();
|
|
70
|
+
|
|
71
|
+
// ../runtime/dist/adapters/document/local/docx-budget.js
|
|
72
|
+
import { inflateRawSync } from "node:zlib";
|
|
73
|
+
var endSignature = 101010256;
|
|
74
|
+
var centralSignature = 33639248;
|
|
75
|
+
function requireZip(condition) {
|
|
76
|
+
if (!condition)
|
|
77
|
+
throw new Error("Invalid document archive");
|
|
78
|
+
}
|
|
79
|
+
function number64(buffer, offset) {
|
|
80
|
+
const value = buffer.readBigUInt64LE(offset);
|
|
81
|
+
requireZip(value <= BigInt(Number.MAX_SAFE_INTEGER));
|
|
82
|
+
return Number(value);
|
|
83
|
+
}
|
|
84
|
+
var checksumTable;
|
|
85
|
+
function decodedChecksum(buffer) {
|
|
86
|
+
checksumTable ??= Uint32Array.from({ length: 256 }, (_, value) => {
|
|
87
|
+
for (let bit = 0; bit < 8; bit++)
|
|
88
|
+
value = value >>> 1 ^ (value & 1 ? 3988292384 : 0);
|
|
89
|
+
return value >>> 0;
|
|
90
|
+
});
|
|
91
|
+
let checksum = 4294967295;
|
|
92
|
+
for (const byte of buffer)
|
|
93
|
+
checksum = checksum >>> 8 ^ checksumTable[(checksum ^ byte) & 255];
|
|
94
|
+
return (checksum ^ 4294967295) >>> 0;
|
|
95
|
+
}
|
|
96
|
+
function directory(buffer) {
|
|
97
|
+
let end = buffer.length - 22;
|
|
98
|
+
for (; end >= Math.max(0, buffer.length - 65557); end--)
|
|
99
|
+
if (buffer.readUInt32LE(end) === endSignature && end + 22 + buffer.readUInt16LE(end + 20) === buffer.length)
|
|
100
|
+
break;
|
|
101
|
+
requireZip(end >= Math.max(0, buffer.length - 65557));
|
|
102
|
+
requireZip(buffer.readUInt16LE(end + 4) === 0 && buffer.readUInt16LE(end + 6) === 0);
|
|
103
|
+
let count = buffer.readUInt16LE(end + 10), size = buffer.readUInt32LE(end + 12), start = buffer.readUInt32LE(end + 16);
|
|
104
|
+
requireZip(buffer.readUInt16LE(end + 8) === count);
|
|
105
|
+
if (count === 65535 || size === 4294967295 || start === 4294967295) {
|
|
106
|
+
requireZip(end >= 20 && buffer.readUInt32LE(end - 20) === 117853008);
|
|
107
|
+
requireZip(buffer.readUInt32LE(end - 16) === 0 && buffer.readUInt32LE(end - 4) === 1);
|
|
108
|
+
const record = number64(buffer, end - 12);
|
|
109
|
+
requireZip(record + 56 <= end - 20 && buffer.readUInt32LE(record) === 101075792);
|
|
110
|
+
requireZip(buffer.readUInt32LE(record + 16) === 0 && buffer.readUInt32LE(record + 20) === 0);
|
|
111
|
+
count = number64(buffer, record + 32);
|
|
112
|
+
requireZip(number64(buffer, record + 24) === count);
|
|
113
|
+
size = number64(buffer, record + 40);
|
|
114
|
+
start = number64(buffer, record + 48);
|
|
115
|
+
end = record;
|
|
116
|
+
}
|
|
117
|
+
requireZip(start + size <= end && count <= Math.floor(size / 46));
|
|
118
|
+
return { start, end: start + size, count };
|
|
119
|
+
}
|
|
120
|
+
function entrySizes(buffer, cursor, extraStart, extraEnd) {
|
|
121
|
+
let decoded = buffer.readUInt32LE(cursor + 24), encoded = buffer.readUInt32LE(cursor + 20), offset = buffer.readUInt32LE(cursor + 42);
|
|
122
|
+
if ([decoded, encoded, offset].includes(4294967295)) {
|
|
123
|
+
let extra = extraStart;
|
|
124
|
+
for (; extra + 4 <= extraEnd; extra += 4 + buffer.readUInt16LE(extra + 2)) {
|
|
125
|
+
let read2 = function() {
|
|
126
|
+
requireZip(at + 8 <= extra + 4 + length);
|
|
127
|
+
const value = number64(buffer, at);
|
|
128
|
+
at += 8;
|
|
129
|
+
return value;
|
|
130
|
+
};
|
|
131
|
+
var read = read2;
|
|
132
|
+
const length = buffer.readUInt16LE(extra + 2);
|
|
133
|
+
requireZip(extra + 4 + length <= extraEnd);
|
|
134
|
+
if (buffer.readUInt16LE(extra) !== 1)
|
|
135
|
+
continue;
|
|
136
|
+
let at = extra + 4;
|
|
137
|
+
if (decoded === 4294967295)
|
|
138
|
+
decoded = read2();
|
|
139
|
+
if (encoded === 4294967295)
|
|
140
|
+
encoded = read2();
|
|
141
|
+
if (offset === 4294967295)
|
|
142
|
+
offset = read2();
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
requireZip(![decoded, encoded, offset].includes(4294967295));
|
|
146
|
+
}
|
|
147
|
+
return { decoded, encoded, offset };
|
|
148
|
+
}
|
|
149
|
+
function assertDocxByteBudget(buffer, limit, correlationId) {
|
|
150
|
+
function oversized() {
|
|
151
|
+
throw new DocumentConversionError("DOCUMENT_SIZE_LIMIT_EXCEEDED", correlationId);
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
const catalog = directory(buffer);
|
|
155
|
+
let cursor = catalog.start, decodedTotal = 0, encodedTotal = 0;
|
|
156
|
+
for (let index = 0; index < catalog.count; index++) {
|
|
157
|
+
requireZip(cursor + 46 <= catalog.end && buffer.readUInt32LE(cursor) === centralSignature);
|
|
158
|
+
const nameLength = buffer.readUInt16LE(cursor + 28), extraLength = buffer.readUInt16LE(cursor + 30);
|
|
159
|
+
const extraStart = cursor + 46 + nameLength, next = extraStart + extraLength + buffer.readUInt16LE(cursor + 32);
|
|
160
|
+
requireZip(next <= catalog.end && buffer.readUInt16LE(cursor + 34) === 0);
|
|
161
|
+
const entry = entrySizes(buffer, cursor, extraStart, extraStart + extraLength);
|
|
162
|
+
const method = buffer.readUInt16LE(cursor + 10);
|
|
163
|
+
requireZip((buffer.readUInt16LE(cursor + 8) & 1) === 0 && (method === 0 || method === 8));
|
|
164
|
+
requireZip(entry.offset + 30 <= catalog.start && buffer.readUInt32LE(entry.offset) === 67324752);
|
|
165
|
+
const payload = entry.offset + 30 + buffer.readUInt16LE(entry.offset + 26) + buffer.readUInt16LE(entry.offset + 28);
|
|
166
|
+
requireZip(payload + entry.encoded <= catalog.start);
|
|
167
|
+
encodedTotal += entry.encoded;
|
|
168
|
+
requireZip(encodedTotal <= buffer.length);
|
|
169
|
+
const remaining = limit - decodedTotal;
|
|
170
|
+
if (entry.decoded > remaining)
|
|
171
|
+
oversized();
|
|
172
|
+
const compressed = buffer.subarray(payload, payload + entry.encoded);
|
|
173
|
+
let decoded;
|
|
174
|
+
try {
|
|
175
|
+
decoded = method === 0 ? compressed : inflateRawSync(compressed, { maxOutputLength: Math.max(1, remaining) });
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (error.code === "ERR_BUFFER_TOO_LARGE")
|
|
178
|
+
oversized();
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
if (decoded.length > remaining)
|
|
182
|
+
oversized();
|
|
183
|
+
requireZip(decoded.length === entry.decoded);
|
|
184
|
+
requireZip(decodedChecksum(decoded) === buffer.readUInt32LE(cursor + 16));
|
|
185
|
+
decodedTotal += decoded.length;
|
|
186
|
+
cursor = next;
|
|
187
|
+
}
|
|
188
|
+
requireZip(cursor === catalog.end);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (error instanceof DocumentConversionError)
|
|
191
|
+
throw error;
|
|
192
|
+
throw new DocumentConversionError("DOCUMENT_BASIC_EXTRACTION_FAILED", correlationId);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ../runtime/dist/adapters/document/local/formatting.js
|
|
197
|
+
function normalizeNewlines(value) {
|
|
198
|
+
return value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
199
|
+
}
|
|
200
|
+
function normalizeText(value) {
|
|
201
|
+
return normalizeNewlines(value).replace(/[ \t]+\n/g, "\n").replace(/[ \t]{2,}/g, " ").replace(/\s+([,.;:!?])/g, "$1").replace(/\n{3,}/g, "\n\n").trim();
|
|
202
|
+
}
|
|
203
|
+
function decodeHtmlEntities(value) {
|
|
204
|
+
return value.replace(/ /gi, " ").replace(/&/gi, "&").replace(/</gi, "<").replace(/>/gi, ">").replace(/"/gi, '"').replace(/'/gi, "'");
|
|
205
|
+
}
|
|
206
|
+
function htmlToMarkdown(html) {
|
|
207
|
+
let output = html;
|
|
208
|
+
output = output.replace(/<script[\s\S]*?<\/script>/gi, "");
|
|
209
|
+
output = output.replace(/<style[\s\S]*?<\/style>/gi, "");
|
|
210
|
+
output = output.replace(/<h([1-6])[^>]*>([\s\S]*?)<\/h\1>/gi, (_match, level, content) => {
|
|
211
|
+
const depth = Number(level);
|
|
212
|
+
return `${"#".repeat(depth)} ${decodeHtmlEntities(content.replace(/<[^>]+>/g, " ").trim())}
|
|
213
|
+
|
|
214
|
+
`;
|
|
215
|
+
});
|
|
216
|
+
output = output.replace(/<(br|hr)\s*\/?>/gi, "\n");
|
|
217
|
+
output = output.replace(/<\/(p|div|section|article|li|ul|ol|table|tr)>/gi, "\n");
|
|
218
|
+
output = output.replace(/<(p|div|section|article|li|ul|ol|table|tr|td|th)[^>]*>/gi, " ");
|
|
219
|
+
output = output.replace(/<[^>]+>/g, " ");
|
|
220
|
+
return normalizeText(decodeHtmlEntities(output));
|
|
221
|
+
}
|
|
222
|
+
function parseCsv(text) {
|
|
223
|
+
const rows = [];
|
|
224
|
+
let row = [];
|
|
225
|
+
let field = "";
|
|
226
|
+
let inQuotes = false;
|
|
227
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
228
|
+
const char = text[index];
|
|
229
|
+
if (inQuotes) {
|
|
230
|
+
if (char === '"') {
|
|
231
|
+
if (text[index + 1] === '"') {
|
|
232
|
+
field += '"';
|
|
233
|
+
index += 1;
|
|
234
|
+
} else {
|
|
235
|
+
inQuotes = false;
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
field += char;
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (char === '"') {
|
|
243
|
+
inQuotes = true;
|
|
244
|
+
} else if (char === ",") {
|
|
245
|
+
row.push(field.trim());
|
|
246
|
+
field = "";
|
|
247
|
+
} else if (char === "\n" || char === "\r" && text[index + 1] === "\n") {
|
|
248
|
+
row.push(field.trim());
|
|
249
|
+
if (row.some((value) => value.length > 0)) {
|
|
250
|
+
rows.push(row);
|
|
251
|
+
}
|
|
252
|
+
row = [];
|
|
253
|
+
field = "";
|
|
254
|
+
if (char === "\r") {
|
|
255
|
+
index += 1;
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
field += char;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
row.push(field.trim());
|
|
262
|
+
if (row.some((value) => value.length > 0)) {
|
|
263
|
+
rows.push(row);
|
|
264
|
+
}
|
|
265
|
+
return rows;
|
|
266
|
+
}
|
|
267
|
+
function csvToMarkdown(text) {
|
|
268
|
+
const rows = parseCsv(text);
|
|
269
|
+
if (rows.length === 0) {
|
|
270
|
+
return { markdown: "", plainText: "" };
|
|
271
|
+
}
|
|
272
|
+
if (rows.length === 1) {
|
|
273
|
+
const single = rows[0].join(", ");
|
|
274
|
+
return { markdown: single, plainText: single };
|
|
275
|
+
}
|
|
276
|
+
const header = rows[0];
|
|
277
|
+
const body = rows.slice(1);
|
|
278
|
+
const table = [
|
|
279
|
+
`| ${header.join(" | ")} |`,
|
|
280
|
+
`| ${header.map(() => "---").join(" | ")} |`,
|
|
281
|
+
...body.map((row) => `| ${header.map((_, index) => row[index] ?? "").join(" | ")} |`)
|
|
282
|
+
].join("\n");
|
|
283
|
+
const plainText = body.map((row) => header.map((title, index) => `${title}: ${row[index] ?? ""}`).join(", ")).join("\n");
|
|
284
|
+
return { markdown: table, plainText };
|
|
285
|
+
}
|
|
286
|
+
function buildPages(text) {
|
|
287
|
+
return [
|
|
288
|
+
{
|
|
289
|
+
page: 1,
|
|
290
|
+
charStart: 0,
|
|
291
|
+
charEnd: text.length,
|
|
292
|
+
previewText: text.slice(0, 160)
|
|
293
|
+
}
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
function buildFallbackSection(markdown, title) {
|
|
297
|
+
return [
|
|
298
|
+
{
|
|
299
|
+
id: "section-1",
|
|
300
|
+
title,
|
|
301
|
+
depth: 1,
|
|
302
|
+
path: title ? [title] : [],
|
|
303
|
+
pageStart: 1,
|
|
304
|
+
pageEnd: 1,
|
|
305
|
+
charStart: 0,
|
|
306
|
+
charEnd: markdown.length
|
|
307
|
+
}
|
|
308
|
+
];
|
|
309
|
+
}
|
|
310
|
+
function deriveSections(markdown) {
|
|
311
|
+
const headingRegex = /^(#{1,6})\s+(.+)$/gm;
|
|
312
|
+
const matches = Array.from(markdown.matchAll(headingRegex));
|
|
313
|
+
if (matches.length === 0) {
|
|
314
|
+
return buildFallbackSection(markdown);
|
|
315
|
+
}
|
|
316
|
+
const sections = [];
|
|
317
|
+
const pathStack = [];
|
|
318
|
+
for (let index = 0; index < matches.length; index += 1) {
|
|
319
|
+
const match = matches[index];
|
|
320
|
+
const depth = match[1].length;
|
|
321
|
+
const title = match[2].trim();
|
|
322
|
+
const charStart = match.index ?? 0;
|
|
323
|
+
const charEnd = index + 1 < matches.length ? matches[index + 1].index ?? markdown.length : markdown.length;
|
|
324
|
+
pathStack.splice(depth - 1);
|
|
325
|
+
pathStack[depth - 1] = title;
|
|
326
|
+
sections.push({
|
|
327
|
+
id: `section-${index + 1}`,
|
|
328
|
+
title,
|
|
329
|
+
depth,
|
|
330
|
+
path: pathStack.filter(Boolean),
|
|
331
|
+
pageStart: 1,
|
|
332
|
+
pageEnd: 1,
|
|
333
|
+
charStart,
|
|
334
|
+
charEnd
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
return sections;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// ../runtime/dist/adapters/document/local/extraction.js
|
|
341
|
+
var DOCX_MIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
342
|
+
var DEFAULT_MAX_INPUT_BYTES = 50 * 1024 * 1024;
|
|
343
|
+
function documentMimeType(input) {
|
|
344
|
+
if (typeof input.mimeType !== "string")
|
|
345
|
+
throw new DocumentConversionError("DOCUMENT_INPUT_INVALID", input.correlationId);
|
|
346
|
+
return input.mimeType.split(";", 1)[0].trim().toLowerCase();
|
|
347
|
+
}
|
|
348
|
+
function checkInput(input, options) {
|
|
349
|
+
const limit = options.maxInputBytes ?? DEFAULT_MAX_INPUT_BYTES;
|
|
350
|
+
if (!Number.isSafeInteger(limit) || limit <= 0)
|
|
351
|
+
throw new TypeError("maxInputBytes must be a positive safe integer");
|
|
352
|
+
const buffer = input.buffer;
|
|
353
|
+
if (!Buffer.isBuffer(buffer) || typeof input.mimeType !== "string" || typeof input.filename !== "string")
|
|
354
|
+
throw new DocumentConversionError("DOCUMENT_INPUT_INVALID", input.correlationId);
|
|
355
|
+
if (buffer.length > limit)
|
|
356
|
+
throw new DocumentConversionError("DOCUMENT_SIZE_LIMIT_EXCEEDED", input.correlationId);
|
|
357
|
+
if (input.signal?.aborted)
|
|
358
|
+
throw new DocumentConversionError("DOCUMENT_CONVERSION_CANCELLED", input.correlationId);
|
|
359
|
+
input = Object.freeze({
|
|
360
|
+
buffer: Buffer.from(buffer),
|
|
361
|
+
filename: input.filename,
|
|
362
|
+
mimeType: input.mimeType,
|
|
363
|
+
signal: input.signal,
|
|
364
|
+
correlationId: input.correlationId
|
|
365
|
+
});
|
|
366
|
+
const mime = documentMimeType(input);
|
|
367
|
+
if (!(mime.startsWith("text/") || mime === "application/pdf" || mime === DOCX_MIME))
|
|
368
|
+
throw new DocumentConversionError("DOCUMENT_INPUT_INVALID", input.correlationId);
|
|
369
|
+
if (mime === "application/pdf" && !input.buffer.subarray(0, 1024).includes(Buffer.from("%PDF-")) || mime === DOCX_MIME && !input.buffer.subarray(0, 4).equals(Buffer.from([80, 75, 3, 4])))
|
|
370
|
+
throw new DocumentConversionError("DOCUMENT_INPUT_INVALID", input.correlationId);
|
|
371
|
+
if (mime.startsWith("text/") && (input.buffer.includes(0) || input.buffer.subarray(0, 5).equals(Buffer.from("%PDF-")) || input.buffer.subarray(0, 4).equals(Buffer.from([80, 75, 3, 4]))))
|
|
372
|
+
throw new DocumentConversionError("DOCUMENT_INPUT_INVALID", input.correlationId);
|
|
373
|
+
if (mime === DOCX_MIME)
|
|
374
|
+
assertDocxByteBudget(input.buffer, limit, input.correlationId);
|
|
375
|
+
return { mime, input };
|
|
376
|
+
}
|
|
377
|
+
function compatibleVersion(version, major, minor, patch) {
|
|
378
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version);
|
|
379
|
+
return !!match && Number(match[1]) === major && (Number(match[2]) > minor || Number(match[2]) === minor && Number(match[3]) >= patch);
|
|
380
|
+
}
|
|
381
|
+
async function loadParser(kind, load, input) {
|
|
382
|
+
const dependency = kind === "pdf" ? "pdf-parse" : "mammoth";
|
|
383
|
+
try {
|
|
384
|
+
const module = await load();
|
|
385
|
+
if (!compatibleVersion(module.version, kind === "pdf" ? 2 : 1, kind === "pdf" ? 4 : 12, kind === "pdf" ? 5 : 0))
|
|
386
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", input.correlationId, dependency);
|
|
387
|
+
return module;
|
|
388
|
+
} catch (error) {
|
|
389
|
+
const missing = error instanceof DocumentConversionError && error.code === "DOCUMENT_LOCAL_DEPENDENCY_MISSING";
|
|
390
|
+
throw new DocumentConversionError(missing ? "DOCUMENT_LOCAL_DEPENDENCY_MISSING" : "DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", input.correlationId, dependency);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async function binaryText(input, mime, loaders) {
|
|
394
|
+
if (mime === "application/pdf") {
|
|
395
|
+
const { PDFParse } = await loadParser("pdf", () => loaders.pdf(), input);
|
|
396
|
+
if (typeof PDFParse !== "function")
|
|
397
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", input.correlationId, "pdf-parse");
|
|
398
|
+
const parser = new PDFParse({ data: input.buffer });
|
|
399
|
+
let completed = false;
|
|
400
|
+
try {
|
|
401
|
+
const result2 = await parser.getText();
|
|
402
|
+
const text = normalizeText(result2.text ?? "");
|
|
403
|
+
completed = true;
|
|
404
|
+
return { markdown: text, text };
|
|
405
|
+
} finally {
|
|
406
|
+
try {
|
|
407
|
+
await parser.destroy();
|
|
408
|
+
} catch (error) {
|
|
409
|
+
if (completed)
|
|
410
|
+
throw error;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const { convertToMarkdown } = await loadParser("docx", () => loaders.docx(), input);
|
|
415
|
+
if (typeof convertToMarkdown !== "function")
|
|
416
|
+
throw new DocumentConversionError("DOCUMENT_LOCAL_DEPENDENCY_INCOMPATIBLE", input.correlationId, "mammoth");
|
|
417
|
+
const result = await convertToMarkdown({ buffer: input.buffer });
|
|
418
|
+
const markdown = normalizeText(result.value ?? "");
|
|
419
|
+
return { markdown, text: normalizeText(markdown.replace(/^#{1,6}\s+/gm, "")) };
|
|
420
|
+
}
|
|
421
|
+
async function extractLocalDocument(input, options = {}) {
|
|
422
|
+
const captured = checkInput(input, options), mime = captured.mime;
|
|
423
|
+
input = captured.input;
|
|
424
|
+
let markdown;
|
|
425
|
+
let text;
|
|
426
|
+
try {
|
|
427
|
+
if (mime === "text/html") {
|
|
428
|
+
markdown = htmlToMarkdown(input.buffer.toString("utf8"));
|
|
429
|
+
text = normalizeText(markdown.replace(/^#{1,6}\s+/gm, ""));
|
|
430
|
+
} else if (mime === "text/csv") {
|
|
431
|
+
const csv = csvToMarkdown(input.buffer.toString("utf8"));
|
|
432
|
+
markdown = normalizeText(csv.markdown);
|
|
433
|
+
text = normalizeText(csv.plainText);
|
|
434
|
+
} else if (mime.startsWith("text/")) {
|
|
435
|
+
text = markdown = normalizeText(input.buffer.toString("utf8"));
|
|
436
|
+
} else {
|
|
437
|
+
({ markdown, text } = await binaryText(input, mime, options.loaders ?? defaultParserLoaders));
|
|
438
|
+
}
|
|
439
|
+
} catch (error) {
|
|
440
|
+
if (error instanceof DocumentConversionError)
|
|
441
|
+
throw error;
|
|
442
|
+
throw new DocumentConversionError("DOCUMENT_BASIC_EXTRACTION_FAILED", input.correlationId);
|
|
443
|
+
}
|
|
444
|
+
if (input.signal?.aborted)
|
|
445
|
+
throw new DocumentConversionError("DOCUMENT_CONVERSION_CANCELLED", input.correlationId);
|
|
446
|
+
if (!text.replace(/-- \d+ of \d+ --/g, "").trim())
|
|
447
|
+
throw new DocumentConversionError("DOCUMENT_BASIC_EXTRACTION_INSUFFICIENT", input.correlationId);
|
|
448
|
+
return {
|
|
449
|
+
text,
|
|
450
|
+
markdown,
|
|
451
|
+
pages: buildPages(text),
|
|
452
|
+
sections: deriveSections(markdown || text),
|
|
453
|
+
warnings: []
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// ../runtime/dist/adapters/document/local/index.js
|
|
458
|
+
function validatePolicy(input, automaticBinary) {
|
|
459
|
+
const mode = input.mode ?? "auto";
|
|
460
|
+
const provider = input.provider ?? "auto";
|
|
461
|
+
if (!["auto", "text-only", "no-external-provider"].includes(mode) || !["auto", "local_text"].includes(provider))
|
|
462
|
+
throw new DocumentConversionError("DOCUMENT_CONVERSION_POLICY_INVALID", input.correlationId);
|
|
463
|
+
if (!automaticBinary && mode === "auto" && !documentMimeType(input).startsWith("text/"))
|
|
464
|
+
throw new DocumentConversionError("DOCUMENT_CONVERSION_MODE_REQUIRED", input.correlationId);
|
|
465
|
+
}
|
|
466
|
+
function converter(options, automaticBinary) {
|
|
467
|
+
const captured = Object.freeze({
|
|
468
|
+
...options,
|
|
469
|
+
...options.loaders && { loaders: Object.freeze({ ...options.loaders }) }
|
|
470
|
+
});
|
|
471
|
+
return Object.freeze({
|
|
472
|
+
assertBasicSelection: (input) => validatePolicy(input, automaticBinary),
|
|
473
|
+
async convert(input) {
|
|
474
|
+
validatePolicy(input, automaticBinary);
|
|
475
|
+
const buffer = input.buffer;
|
|
476
|
+
const bytes = Buffer.isBuffer(buffer) ? buffer.length : 0;
|
|
477
|
+
const start = performance.now();
|
|
478
|
+
const result = await extractLocalDocument({ ...input, buffer }, captured);
|
|
479
|
+
return {
|
|
480
|
+
...result,
|
|
481
|
+
providerTrace: {
|
|
482
|
+
provider: "local_text",
|
|
483
|
+
route: "local_text",
|
|
484
|
+
external: false,
|
|
485
|
+
durationMs: Math.max(0, performance.now() - start),
|
|
486
|
+
bytes
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
function createLocalDocumentConverter(options = {}) {
|
|
493
|
+
return converter(options, false);
|
|
494
|
+
}
|
|
495
|
+
function createAutomaticLocalDocumentConverter(options = {}) {
|
|
496
|
+
return converter(options, true);
|
|
497
|
+
}
|
|
498
|
+
export {
|
|
499
|
+
DocumentConversionError,
|
|
500
|
+
createAutomaticLocalDocumentConverter,
|
|
501
|
+
createLocalDocumentConverter,
|
|
502
|
+
createLocalDocumentParserLoaders,
|
|
503
|
+
extractLocalDocument
|
|
504
|
+
};
|