@bevel-software/platform-core-backend 0.11.2 → 0.12.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/THIRD-PARTY-NOTICES.md +1163 -425
- package/dist/core/create-core-server.js +1 -1
- package/dist/core/create-core-server.js.map +1 -1
- package/dist/core/create-core-services.d.ts +2 -0
- package/dist/core/create-core-services.d.ts.map +1 -1
- package/dist/core/create-core-services.js +5 -0
- package/dist/core/create-core-services.js.map +1 -1
- package/dist/core-config.d.ts +7 -0
- package/dist/core-config.d.ts.map +1 -1
- package/dist/core-config.js +9 -0
- package/dist/core-config.js.map +1 -1
- package/dist/modules/code-mode/code-mode.tool.d.ts.map +1 -1
- package/dist/modules/code-mode/code-mode.tool.js +7 -1
- package/dist/modules/code-mode/code-mode.tool.js.map +1 -1
- package/dist/modules/workspace/file-readers/doc-extract.service.d.ts +71 -0
- package/dist/modules/workspace/file-readers/doc-extract.service.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/doc-extract.service.js +90 -0
- package/dist/modules/workspace/file-readers/doc-extract.service.js.map +1 -0
- package/dist/modules/workspace/file-readers/doc-extract.types.d.ts +55 -0
- package/dist/modules/workspace/file-readers/doc-extract.types.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/doc-extract.types.js +34 -0
- package/dist/modules/workspace/file-readers/doc-extract.types.js.map +1 -0
- package/dist/modules/workspace/file-readers/document-reader.d.ts +32 -0
- package/dist/modules/workspace/file-readers/document-reader.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/document-reader.js +59 -0
- package/dist/modules/workspace/file-readers/document-reader.js.map +1 -0
- package/dist/modules/workspace/file-readers/email-reader.d.ts +15 -0
- package/dist/modules/workspace/file-readers/email-reader.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/email-reader.js +19 -0
- package/dist/modules/workspace/file-readers/email-reader.js.map +1 -0
- package/dist/modules/workspace/file-readers/email-text.d.ts +51 -0
- package/dist/modules/workspace/file-readers/email-text.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/email-text.js +151 -0
- package/dist/modules/workspace/file-readers/email-text.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-docx.d.ts +13 -0
- package/dist/modules/workspace/file-readers/extract-docx.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-docx.js +67 -0
- package/dist/modules/workspace/file-readers/extract-docx.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-eml.d.ts +18 -0
- package/dist/modules/workspace/file-readers/extract-eml.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-eml.js +87 -0
- package/dist/modules/workspace/file-readers/extract-eml.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-msg.d.ts +17 -0
- package/dist/modules/workspace/file-readers/extract-msg.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-msg.js +121 -0
- package/dist/modules/workspace/file-readers/extract-msg.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-odp.d.ts +13 -0
- package/dist/modules/workspace/file-readers/extract-odp.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-odp.js +60 -0
- package/dist/modules/workspace/file-readers/extract-odp.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-ods.d.ts +10 -0
- package/dist/modules/workspace/file-readers/extract-ods.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-ods.js +173 -0
- package/dist/modules/workspace/file-readers/extract-ods.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-odt.d.ts +17 -0
- package/dist/modules/workspace/file-readers/extract-odt.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-odt.js +45 -0
- package/dist/modules/workspace/file-readers/extract-odt.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-pdf.d.ts +3 -0
- package/dist/modules/workspace/file-readers/extract-pdf.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-pdf.js +176 -0
- package/dist/modules/workspace/file-readers/extract-pdf.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-pptx.d.ts +37 -0
- package/dist/modules/workspace/file-readers/extract-pptx.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-pptx.js +288 -0
- package/dist/modules/workspace/file-readers/extract-pptx.js.map +1 -0
- package/dist/modules/workspace/file-readers/extract-xlsx.d.ts +10 -0
- package/dist/modules/workspace/file-readers/extract-xlsx.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extract-xlsx.js +98 -0
- package/dist/modules/workspace/file-readers/extract-xlsx.js.map +1 -0
- package/dist/modules/workspace/file-readers/extraction-cache.d.ts +61 -0
- package/dist/modules/workspace/file-readers/extraction-cache.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/extraction-cache.js +135 -0
- package/dist/modules/workspace/file-readers/extraction-cache.js.map +1 -0
- package/dist/modules/workspace/file-readers/file-reader.d.ts +76 -0
- package/dist/modules/workspace/file-readers/file-reader.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/file-reader.js +55 -0
- package/dist/modules/workspace/file-readers/file-reader.js.map +1 -0
- package/dist/modules/workspace/file-readers/file-reader.registry.d.ts +13 -0
- package/dist/modules/workspace/file-readers/file-reader.registry.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/file-reader.registry.js +41 -0
- package/dist/modules/workspace/file-readers/file-reader.registry.js.map +1 -0
- package/dist/modules/workspace/file-readers/image-read.d.ts +35 -0
- package/dist/modules/workspace/file-readers/image-read.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/image-read.js +108 -0
- package/dist/modules/workspace/file-readers/image-read.js.map +1 -0
- package/dist/modules/workspace/file-readers/image-reader.d.ts +19 -0
- package/dist/modules/workspace/file-readers/image-reader.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/image-reader.js +30 -0
- package/dist/modules/workspace/file-readers/image-reader.js.map +1 -0
- package/dist/modules/workspace/file-readers/odf-text.d.ts +26 -0
- package/dist/modules/workspace/file-readers/odf-text.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/odf-text.js +116 -0
- package/dist/modules/workspace/file-readers/odf-text.js.map +1 -0
- package/dist/modules/workspace/file-readers/ooxml-text.d.ts +172 -0
- package/dist/modules/workspace/file-readers/ooxml-text.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/ooxml-text.js +439 -0
- package/dist/modules/workspace/file-readers/ooxml-text.js.map +1 -0
- package/dist/modules/workspace/file-readers/text-reader.d.ts +47 -0
- package/dist/modules/workspace/file-readers/text-reader.d.ts.map +1 -0
- package/dist/modules/workspace/file-readers/text-reader.js +117 -0
- package/dist/modules/workspace/file-readers/text-reader.js.map +1 -0
- package/dist/modules/workspace/workspace.tools.d.ts +2 -1
- package/dist/modules/workspace/workspace.tools.d.ts.map +1 -1
- package/dist/modules/workspace/workspace.tools.js +158 -15
- package/dist/modules/workspace/workspace.tools.js.map +1 -1
- package/package.json +9 -4
- package/src/core/create-core-server.ts +1 -1
- package/src/core/create-core-services.ts +6 -0
- package/src/core-config.ts +9 -0
- package/src/modules/code-mode/__tests__/code-mode.tool.test.ts +30 -0
- package/src/modules/code-mode/code-mode.tool.ts +7 -1
- package/src/modules/tool-helpers/__tests__/phase4-tools.test.ts +2 -1
- package/src/modules/workspace/__tests__/workspace.tools.test.ts +500 -2
- package/src/modules/workspace/file-readers/__tests__/doc-extract.test.ts +1658 -0
- package/src/modules/workspace/file-readers/__tests__/email-extract.test.ts +485 -0
- package/src/modules/workspace/file-readers/__tests__/file-reader.registry.test.ts +97 -0
- package/src/modules/workspace/file-readers/__tests__/image-read.test.ts +100 -0
- package/src/modules/workspace/file-readers/doc-extract.service.ts +104 -0
- package/src/modules/workspace/file-readers/doc-extract.types.ts +63 -0
- package/src/modules/workspace/file-readers/document-reader.ts +64 -0
- package/src/modules/workspace/file-readers/email-reader.ts +21 -0
- package/src/modules/workspace/file-readers/email-text.ts +193 -0
- package/src/modules/workspace/file-readers/extract-docx.ts +67 -0
- package/src/modules/workspace/file-readers/extract-eml.ts +92 -0
- package/src/modules/workspace/file-readers/extract-msg.ts +134 -0
- package/src/modules/workspace/file-readers/extract-odp.ts +63 -0
- package/src/modules/workspace/file-readers/extract-ods.ts +182 -0
- package/src/modules/workspace/file-readers/extract-odt.ts +48 -0
- package/src/modules/workspace/file-readers/extract-pdf.ts +178 -0
- package/src/modules/workspace/file-readers/extract-pptx.ts +302 -0
- package/src/modules/workspace/file-readers/extract-xlsx.ts +96 -0
- package/src/modules/workspace/file-readers/extraction-cache.ts +142 -0
- package/src/modules/workspace/file-readers/file-reader.registry.ts +45 -0
- package/src/modules/workspace/file-readers/file-reader.ts +104 -0
- package/src/modules/workspace/file-readers/image-read.ts +122 -0
- package/src/modules/workspace/file-readers/image-reader.ts +39 -0
- package/src/modules/workspace/file-readers/odf-text.ts +123 -0
- package/src/modules/workspace/file-readers/ooxml-text.ts +477 -0
- package/src/modules/workspace/file-readers/text-reader.ts +131 -0
- package/src/modules/workspace/workspace.tools.ts +174 -12
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side document text extraction — shared types.
|
|
3
|
+
*
|
|
4
|
+
* The extractors turn an office document (`.docx` / `.pptx` / `.xlsx`), an
|
|
5
|
+
* OpenDocument file (`.odt` / `.odp` / `.ods`) or a PDF into plain text an
|
|
6
|
+
* agent can read and grep. They are HONEST about being
|
|
7
|
+
* lossy: every successful extraction carries a one-line `summary` the consumer
|
|
8
|
+
* turns into a marker header (`[extracted text of <path> — <summary>]`) so the
|
|
9
|
+
* reader knows it is looking at extracted text, not the file's bytes.
|
|
10
|
+
*
|
|
11
|
+
* The `summary` (not a full marker) is what extractors return and what the
|
|
12
|
+
* cache stores, because the cache is keyed by CONTENT (git blob sha): the same
|
|
13
|
+
* document at two workspace paths shares one cache entry, and baking a path
|
|
14
|
+
* into the cached value would surface the wrong path on the second read.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Build the honest ONE-LINE header consumers prepend to extracted text.
|
|
18
|
+
*
|
|
19
|
+
* One line is a promise the rest of the read path keeps: grep counts on the
|
|
20
|
+
* marker occupying exactly one, so read_file and grep agree on line numbers.
|
|
21
|
+
* A path may legally carry a CR or LF, which would forge extra lines and shift
|
|
22
|
+
* every number after it, so those are shown as escapes rather than obeyed.
|
|
23
|
+
*/
|
|
24
|
+
export function extractionMarker(path, summary) {
|
|
25
|
+
const oneLine = path.replace(/[\r\n]/g, (c) => (c === '\r' ? '\\r' : '\\n'));
|
|
26
|
+
return `[extracted text of ${oneLine} — ${summary}]`;
|
|
27
|
+
}
|
|
28
|
+
/** Lowercased extension of `path` including the dot, or '' when there is none. */
|
|
29
|
+
export function fileExtension(path) {
|
|
30
|
+
const name = path.slice(path.lastIndexOf('/') + 1);
|
|
31
|
+
const dot = name.lastIndexOf('.');
|
|
32
|
+
return dot > 0 ? name.slice(dot).toLowerCase() : '';
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=doc-extract.types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"doc-extract.types.js","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/doc-extract.types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AA8BH;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,OAAe;IAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7E,OAAO,sBAAsB,OAAO,MAAM,OAAO,GAAG,CAAC;AACvD,CAAC;AAED,kFAAkF;AAClF,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IACnD,MAAM,GAAG,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAClC,OAAO,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DocExtractService } from './doc-extract.service.js';
|
|
2
|
+
import type { ExtractFn } from './doc-extract.types.js';
|
|
3
|
+
import { type FileReader, type ReadResult } from './file-reader.js';
|
|
4
|
+
/**
|
|
5
|
+
* FileReader over one document format: a thin wrapper pairing the format's
|
|
6
|
+
* PURE extract function (extract-docx.ts and friends) with the shared
|
|
7
|
+
* content-hash extraction cache (`DocExtractService`). Reads return the
|
|
8
|
+
* extraction under its honest `[extracted text of …]` marker; a parse failure
|
|
9
|
+
* becomes a refusal message, never a 500.
|
|
10
|
+
*
|
|
11
|
+
* `grep` semantics: `greppableText` serves only the CACHED extraction (a hit
|
|
12
|
+
* costs one small JSON read), returning null for a cold document — whether to
|
|
13
|
+
* spend grep's per-walk extraction budget on a cold one is the walk's call,
|
|
14
|
+
* which then extracts through `read` (see grepWalk in workspace.tools.ts).
|
|
15
|
+
* grep also branches on `instanceof DocumentReader` for exactly that decision.
|
|
16
|
+
*/
|
|
17
|
+
export declare class DocumentReader implements FileReader {
|
|
18
|
+
private readonly extract;
|
|
19
|
+
private readonly service;
|
|
20
|
+
readonly extensions: readonly string[];
|
|
21
|
+
/**
|
|
22
|
+
* `read_file` returns an EXTRACTION for these types, so text written back
|
|
23
|
+
* could not round-trip — the write tools refuse (documents are replaced by
|
|
24
|
+
* uploading a new version).
|
|
25
|
+
*/
|
|
26
|
+
readonly textEditable = false;
|
|
27
|
+
constructor(extension: string, extract: ExtractFn, service: DocExtractService);
|
|
28
|
+
read(bytes: Buffer, path: string): Promise<ReadResult>;
|
|
29
|
+
/** The cached extraction (marker line included, so grep's line numbers match read_file's), or null when cold. */
|
|
30
|
+
greppableText(bytes: Buffer, path: string): Promise<string | null>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=document-reader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-reader.d.ts","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/document-reader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAwB,KAAK,UAAU,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE1F;;;;;;;;;;;;GAYG;AACH,qBAAa,cAAe,YAAW,UAAU;IAW7C,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAX1B,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC;;;;OAIG;IACH,QAAQ,CAAC,YAAY,SAAS;gBAG5B,SAAS,EAAE,MAAM,EACA,OAAO,EAAE,SAAS,EAClB,OAAO,EAAE,iBAAiB;IAKvC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAwB5D,iHAAiH;IAC3G,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAIzE"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { displayPath, oneLine } from './file-reader.js';
|
|
2
|
+
/**
|
|
3
|
+
* FileReader over one document format: a thin wrapper pairing the format's
|
|
4
|
+
* PURE extract function (extract-docx.ts and friends) with the shared
|
|
5
|
+
* content-hash extraction cache (`DocExtractService`). Reads return the
|
|
6
|
+
* extraction under its honest `[extracted text of …]` marker; a parse failure
|
|
7
|
+
* becomes a refusal message, never a 500.
|
|
8
|
+
*
|
|
9
|
+
* `grep` semantics: `greppableText` serves only the CACHED extraction (a hit
|
|
10
|
+
* costs one small JSON read), returning null for a cold document — whether to
|
|
11
|
+
* spend grep's per-walk extraction budget on a cold one is the walk's call,
|
|
12
|
+
* which then extracts through `read` (see grepWalk in workspace.tools.ts).
|
|
13
|
+
* grep also branches on `instanceof DocumentReader` for exactly that decision.
|
|
14
|
+
*/
|
|
15
|
+
export class DocumentReader {
|
|
16
|
+
extract;
|
|
17
|
+
service;
|
|
18
|
+
extensions;
|
|
19
|
+
/**
|
|
20
|
+
* `read_file` returns an EXTRACTION for these types, so text written back
|
|
21
|
+
* could not round-trip — the write tools refuse (documents are replaced by
|
|
22
|
+
* uploading a new version).
|
|
23
|
+
*/
|
|
24
|
+
textEditable = false;
|
|
25
|
+
constructor(extension, extract, service) {
|
|
26
|
+
this.extract = extract;
|
|
27
|
+
this.service = service;
|
|
28
|
+
this.extensions = [extension];
|
|
29
|
+
}
|
|
30
|
+
async read(bytes, path) {
|
|
31
|
+
// An extractor is contracted to ANSWER for bad content rather than throw,
|
|
32
|
+
// but it wraps third-party parsers, and one of those throwing past its own
|
|
33
|
+
// guard is a corrupt file — not a server fault. read_file says so instead
|
|
34
|
+
// of failing the whole tool call with a 500.
|
|
35
|
+
let res;
|
|
36
|
+
try {
|
|
37
|
+
res = await this.service.extract(path, bytes, this.extract);
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
41
|
+
return {
|
|
42
|
+
kind: 'refusal',
|
|
43
|
+
message: `[${displayPath(path)} could not be extracted (${oneLine(reason)}) — the file may be corrupt or mislabeled. To fix it, replace the document by uploading a new version.]`,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return res.ok
|
|
47
|
+
? { kind: 'text', text: `${res.marker}\n${res.text}` }
|
|
48
|
+
: {
|
|
49
|
+
kind: 'refusal',
|
|
50
|
+
message: `[${displayPath(path)} ${oneLine(res.message)} — the file may be corrupt or mislabeled. To fix it, replace the document by uploading a new version.]`,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** The cached extraction (marker line included, so grep's line numbers match read_file's), or null when cold. */
|
|
54
|
+
async greppableText(bytes, path) {
|
|
55
|
+
const hit = await this.service.getCached(path, bytes);
|
|
56
|
+
return hit ? `${hit.marker}\n${hit.text}` : null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=document-reader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-reader.js","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/document-reader.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAoC,MAAM,kBAAkB,CAAC;AAE1F;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,cAAc;IAWN;IACA;IAXV,UAAU,CAAoB;IACvC;;;;OAIG;IACM,YAAY,GAAG,KAAK,CAAC;IAE9B,YACE,SAAiB,EACA,OAAkB,EAClB,OAA0B;QAD1B,YAAO,GAAP,OAAO,CAAW;QAClB,YAAO,GAAP,OAAO,CAAmB;QAE3C,IAAI,CAAC,UAAU,GAAG,CAAC,SAAS,CAAC,CAAC;IAChC,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,KAAa,EAAE,IAAY;QACpC,0EAA0E;QAC1E,2EAA2E;QAC3E,0EAA0E;QAC1E,6CAA6C;QAC7C,IAAI,GAAqD,CAAC;QAC1D,IAAI,CAAC;YACH,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9D,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,MAAM,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAChE,OAAO;gBACL,IAAI,EAAE,SAAS;gBACf,OAAO,EAAE,IAAI,WAAW,CAAC,IAAI,CAAC,4BAA4B,OAAO,CAAC,MAAM,CAAC,yGAAyG;aACnL,CAAC;QACJ,CAAC;QAED,OAAO,GAAG,CAAC,EAAE;YACX,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC,MAAM,KAAK,GAAG,CAAC,IAAI,EAAE,EAAE;YACtD,CAAC,CAAC;gBACE,IAAI,EAAE,SAAS;gBACf,OAAO,EAAE,IAAI,WAAW,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,wGAAwG;aAC/J,CAAC;IACR,CAAC;IAED,iHAAiH;IACjH,KAAK,CAAC,aAAa,CAAC,KAAa,EAAE,IAAY;QAC7C,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACtD,OAAO,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,KAAK,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACnD,CAAC;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DocumentReader } from './document-reader.js';
|
|
2
|
+
/**
|
|
3
|
+
* FileReader over one email format (`.eml` / `.msg`): a `DocumentReader` in
|
|
4
|
+
* every mechanical respect — cache-wrapped extraction, greppable when cached,
|
|
5
|
+
* corrupt files answered with the typed could-not-be-parsed refusal — with
|
|
6
|
+
* only the WRITE-refusal copy specialized. The generic document refusal talks
|
|
7
|
+
* about round-tripping an office document; an email deserves the honest
|
|
8
|
+
* version of the same "no": the file is a snapshot of a message, and editing
|
|
9
|
+
* a snapshot's text is not a thing.
|
|
10
|
+
*/
|
|
11
|
+
export declare class EmailReader extends DocumentReader {
|
|
12
|
+
/** The write-refusal for the agent text-editing tools (see `assertNotDocumentEdit`). */
|
|
13
|
+
editRefusal(path: string): string;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=email-reader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-reader.d.ts","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/email-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;;;;;;;GAQG;AACH,qBAAa,WAAY,SAAQ,cAAc;IAC7C,wFAAwF;IACxF,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;CAOlC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DocumentReader } from './document-reader.js';
|
|
2
|
+
/**
|
|
3
|
+
* FileReader over one email format (`.eml` / `.msg`): a `DocumentReader` in
|
|
4
|
+
* every mechanical respect — cache-wrapped extraction, greppable when cached,
|
|
5
|
+
* corrupt files answered with the typed could-not-be-parsed refusal — with
|
|
6
|
+
* only the WRITE-refusal copy specialized. The generic document refusal talks
|
|
7
|
+
* about round-tripping an office document; an email deserves the honest
|
|
8
|
+
* version of the same "no": the file is a snapshot of a message, and editing
|
|
9
|
+
* a snapshot's text is not a thing.
|
|
10
|
+
*/
|
|
11
|
+
export class EmailReader extends DocumentReader {
|
|
12
|
+
/** The write-refusal for the agent text-editing tools (see `assertNotDocumentEdit`). */
|
|
13
|
+
editRefusal(path) {
|
|
14
|
+
return (`"${path}" is an email file — a snapshot of a message. read_file returns EXTRACTED text for it, ` +
|
|
15
|
+
'and a snapshot cannot be text-edited; to change what is stored, replace the file by uploading ' +
|
|
16
|
+
'a new version.');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=email-reader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-reader.js","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/email-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;;;;;;;GAQG;AACH,MAAM,OAAO,WAAY,SAAQ,cAAc;IAC7C,wFAAwF;IACxF,WAAW,CAAC,IAAY;QACtB,OAAO,CACL,IAAI,IAAI,yFAAyF;YACjG,gGAAgG;YAChG,gBAAgB,CACjB,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared shaping for the email extractors (`extract-eml.ts` / `extract-msg.ts`).
|
|
3
|
+
*
|
|
4
|
+
* Both formats extract to the SAME text shape, so an agent greps a mailbox
|
|
5
|
+
* without caring which client saved the file:
|
|
6
|
+
*
|
|
7
|
+
* [from] Ada Lovelace <ada@example.com>
|
|
8
|
+
* [to] Bob <bob@example.com>, carol@example.com
|
|
9
|
+
* [subject] Quarterly numbers
|
|
10
|
+
* [date] 2026-01-05T10:00:00.000Z
|
|
11
|
+
*
|
|
12
|
+
* the body…
|
|
13
|
+
*
|
|
14
|
+
* [attachments]
|
|
15
|
+
* report.pdf (application/pdf, 48211 bytes)
|
|
16
|
+
*
|
|
17
|
+
* Header lines are omitted when the message lacks the field (never printed
|
|
18
|
+
* empty). The body prefers the plain-text part; an HTML-only body is stripped
|
|
19
|
+
* to text (block tags become newlines so paragraphs survive) and the marker
|
|
20
|
+
* summary says so. Attachments are LISTED by name only — v1 does not extract
|
|
21
|
+
* inside them, and the summary says that too.
|
|
22
|
+
*/
|
|
23
|
+
import type { ExtractedDoc } from './doc-extract.types.js';
|
|
24
|
+
/** One listed attachment. Size/type are printed only when known. */
|
|
25
|
+
export interface EmailAttachment {
|
|
26
|
+
name: string;
|
|
27
|
+
mimeType?: string;
|
|
28
|
+
sizeBytes?: number;
|
|
29
|
+
}
|
|
30
|
+
/** Where the body text came from — drives the honest summary + body notes. */
|
|
31
|
+
export type EmailBodySource = 'text' | 'html' | 'rtf-only' | 'none';
|
|
32
|
+
/** The format-independent email, as far as the extraction cares. */
|
|
33
|
+
export interface EmailModel {
|
|
34
|
+
from?: string;
|
|
35
|
+
to?: string;
|
|
36
|
+
cc?: string;
|
|
37
|
+
bcc?: string;
|
|
38
|
+
subject?: string;
|
|
39
|
+
/** ISO timestamp when the date parsed, the raw header value otherwise. */
|
|
40
|
+
date?: string;
|
|
41
|
+
/** Body as plain text ('' when there is none or it is RTF-only). */
|
|
42
|
+
body: string;
|
|
43
|
+
bodySource: EmailBodySource;
|
|
44
|
+
attachments: EmailAttachment[];
|
|
45
|
+
}
|
|
46
|
+
/** The line a body that exists only as RTF gets INSTEAD of body text. */
|
|
47
|
+
export declare const RTF_ONLY_BODY_LINE = "[body is RTF; no plain-text part]";
|
|
48
|
+
export declare function htmlToEmailText(html: string): string;
|
|
49
|
+
/** Render the model into the marker summary + extraction text (see module doc). */
|
|
50
|
+
export declare function emailExtraction(model: EmailModel): ExtractedDoc;
|
|
51
|
+
//# sourceMappingURL=email-text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-text.d.ts","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/email-text.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAI3D,oEAAoE;AACpE,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,8EAA8E;AAC9E,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,MAAM,CAAC;AAEpE,oEAAoE;AACpE,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0EAA0E;IAC1E,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,eAAe,CAAC;IAC5B,WAAW,EAAE,eAAe,EAAE,CAAC;CAChC;AAED,yEAAyE;AACzE,eAAO,MAAM,kBAAkB,sCAAsC,CAAC;AAsCtE,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CA8CpD;AAsBD,mFAAmF;AACnF,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CAiC/D"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Parser } from 'htmlparser2';
|
|
2
|
+
import { MAX_ELEMENT_DEPTH, TOO_DEEP } from './ooxml-text.js';
|
|
3
|
+
/** The line a body that exists only as RTF gets INSTEAD of body text. */
|
|
4
|
+
export const RTF_ONLY_BODY_LINE = '[body is RTF; no plain-text part]';
|
|
5
|
+
/**
|
|
6
|
+
* Strip an HTML email body to plain text. Deliberately simple (the same
|
|
7
|
+
* stance as `ooxml-text.ts`): comments and `<style>`/`<script>`/`<head>`/
|
|
8
|
+
* `<title>` containers are dropped whole, `<br>` and block-level tag
|
|
9
|
+
* boundaries become newlines so paragraphs survive, every other tag is
|
|
10
|
+
* removed, and entities are decoded through the module's shared
|
|
11
|
+
* `decodeXmlEntities` (plus ` `, which HTML has and XML does not). Runs
|
|
12
|
+
* of blank lines collapse to one.
|
|
13
|
+
*
|
|
14
|
+
* Implemented as a SINGLE-PASS linear scanner, not regexes: the earlier
|
|
15
|
+
* quote-aware tag regexes re-scanned the remaining body from every `<` when a
|
|
16
|
+
* quoted attribute never closed — malformed input with many `<` characters
|
|
17
|
+
* plus one unterminated quote pinned the server quadratically. The scanner is
|
|
18
|
+
* quote-aware the same way (a `>` INSIDE a quoted attribute value, as in
|
|
19
|
+
* `<a title="a > b">`, never ends the tag early) but amortizes the failures:
|
|
20
|
+
* a scan that reaches end-of-input marks every `<` it passed OUTSIDE quotes
|
|
21
|
+
* as known-literal (their scans would be identical tails), so no position is
|
|
22
|
+
* rescanned from more than the three possible quote states. An unterminated
|
|
23
|
+
* tag is literal text, not a tag — and so is a `<…>` span that names no
|
|
24
|
+
* element at all, which is how `1 < 2 > 0` survives into the body.
|
|
25
|
+
*/
|
|
26
|
+
/** Elements whose CONTENT is not body text and is dropped with the element. */
|
|
27
|
+
const CONTAINER_TAGS = new Set(['script', 'style', 'head', 'title']);
|
|
28
|
+
/** Elements whose boundaries end a line, so paragraphs survive as paragraphs. */
|
|
29
|
+
const BLOCK_TAGS = new Set([
|
|
30
|
+
'p', 'div', 'section', 'article', 'header', 'footer', 'main',
|
|
31
|
+
'table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th',
|
|
32
|
+
'li', 'ul', 'ol', 'dl', 'dt', 'dd', 'blockquote', 'pre', 'hr',
|
|
33
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
34
|
+
]);
|
|
35
|
+
// Depth bound shared with the OOXML/ODF extractors (`ooxml-text.ts`): mail
|
|
36
|
+
// nests a few dozen levels even at its most table-happy; a crafted body can
|
|
37
|
+
// nest as deep as it has bytes. What was read before the bound is kept.
|
|
38
|
+
export function htmlToEmailText(html) {
|
|
39
|
+
let s = '';
|
|
40
|
+
let depth = 0;
|
|
41
|
+
let skipping = 0; // inside a container whose content is not body text
|
|
42
|
+
const parser = new Parser({
|
|
43
|
+
onopentag(name) {
|
|
44
|
+
depth++;
|
|
45
|
+
if (CONTAINER_TAGS.has(name))
|
|
46
|
+
skipping++;
|
|
47
|
+
else if (name === 'br' || BLOCK_TAGS.has(name))
|
|
48
|
+
s += '\n';
|
|
49
|
+
if (depth > MAX_ELEMENT_DEPTH)
|
|
50
|
+
throw TOO_DEEP;
|
|
51
|
+
},
|
|
52
|
+
ontext(text) {
|
|
53
|
+
if (skipping === 0)
|
|
54
|
+
s += text;
|
|
55
|
+
},
|
|
56
|
+
onclosetag(name) {
|
|
57
|
+
if (CONTAINER_TAGS.has(name)) {
|
|
58
|
+
if (skipping > 0)
|
|
59
|
+
skipping--;
|
|
60
|
+
}
|
|
61
|
+
else if (BLOCK_TAGS.has(name))
|
|
62
|
+
s += '\n';
|
|
63
|
+
depth--;
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
// HTML mode, not XML: an email body is HTML, with its void elements, its
|
|
67
|
+
// implied closes, its raw-text `<script>`, and its named entities.
|
|
68
|
+
{ decodeEntities: true });
|
|
69
|
+
try {
|
|
70
|
+
parser.write(html);
|
|
71
|
+
parser.end();
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
if (err !== TOO_DEEP)
|
|
75
|
+
throw err;
|
|
76
|
+
parser.reset();
|
|
77
|
+
}
|
|
78
|
+
// ` ` decodes to U+00A0, which LOOKS like a space and is not one: an
|
|
79
|
+
// agent grepping the extraction for "Para one" would miss a line that reads
|
|
80
|
+
// exactly that. Extracted text is for reading and searching, so the
|
|
81
|
+
// non-breaking space becomes an ordinary one.
|
|
82
|
+
const out = [];
|
|
83
|
+
for (const raw of s.replace(/ /g, ' ').split('\n')) {
|
|
84
|
+
const line = raw.trim();
|
|
85
|
+
if (line !== '')
|
|
86
|
+
out.push(line);
|
|
87
|
+
else if (out.length > 0 && out[out.length - 1] !== '')
|
|
88
|
+
out.push('');
|
|
89
|
+
}
|
|
90
|
+
while (out.length > 0 && out[out.length - 1] === '')
|
|
91
|
+
out.pop();
|
|
92
|
+
return out.join('\n');
|
|
93
|
+
}
|
|
94
|
+
/** `name (mimeType, N bytes)` with the parenthesis dropped when nothing is known.
|
|
95
|
+
* CR/LF in the metadata are shown as escapes rather than obeyed, so each
|
|
96
|
+
* attachment stays on ONE line and grep line numbers hold. */
|
|
97
|
+
function attachmentLine(a) {
|
|
98
|
+
// The extractors default a MISSING name, but an EMPTY (or whitespace-only)
|
|
99
|
+
// one reaches here as ''; without the same fallback the section printed a
|
|
100
|
+
// blank line — or one starting with the parenthesis — for that attachment.
|
|
101
|
+
const name = a.name.trim() === '' ? 'unnamed attachment' : a.name;
|
|
102
|
+
const details = [a.mimeType, a.sizeBytes !== undefined ? `${a.sizeBytes} bytes` : undefined]
|
|
103
|
+
.filter((d) => d !== undefined && d !== '')
|
|
104
|
+
.join(', ');
|
|
105
|
+
const line = details === '' ? name : `${name} (${details})`;
|
|
106
|
+
return line.replace(/[\r\n]/g, (c) => (c === '\r' ? '\\r' : '\\n'));
|
|
107
|
+
}
|
|
108
|
+
/** A header value with its line breaks made visible instead of obeyed. */
|
|
109
|
+
function oneLine(value) {
|
|
110
|
+
return value.replace(/[\r\n]/g, (c) => (c === '\r' ? '\\r' : '\\n'));
|
|
111
|
+
}
|
|
112
|
+
/** Render the model into the marker summary + extraction text (see module doc). */
|
|
113
|
+
export function emailExtraction(model) {
|
|
114
|
+
const header = [];
|
|
115
|
+
// Absent OR blank fields are omitted — a header marker is never printed empty.
|
|
116
|
+
const pushHeader = (label, value) => {
|
|
117
|
+
if (value === undefined || value.trim() === '')
|
|
118
|
+
return;
|
|
119
|
+
// A header marker is ONE line. A From or Subject carrying CR/LF would
|
|
120
|
+
// otherwise forge further lines into the extraction — including lines that
|
|
121
|
+
// read like other markers — so the breaks are shown as escapes.
|
|
122
|
+
header.push(`[${label}] ${oneLine(value)}`);
|
|
123
|
+
};
|
|
124
|
+
pushHeader('from', model.from);
|
|
125
|
+
pushHeader('to', model.to);
|
|
126
|
+
pushHeader('cc', model.cc);
|
|
127
|
+
pushHeader('bcc', model.bcc);
|
|
128
|
+
pushHeader('subject', model.subject);
|
|
129
|
+
pushHeader('date', model.date);
|
|
130
|
+
const sections = [];
|
|
131
|
+
if (header.length > 0)
|
|
132
|
+
sections.push(header.join('\n'));
|
|
133
|
+
if (model.bodySource === 'rtf-only')
|
|
134
|
+
sections.push(RTF_ONLY_BODY_LINE);
|
|
135
|
+
else if (model.body !== '')
|
|
136
|
+
sections.push(model.body);
|
|
137
|
+
if (model.attachments.length > 0) {
|
|
138
|
+
sections.push(`[attachments]\n${model.attachments.map(attachmentLine).join('\n')}`);
|
|
139
|
+
}
|
|
140
|
+
const n = model.attachments.length;
|
|
141
|
+
const parts = ['email message'];
|
|
142
|
+
if (n > 0)
|
|
143
|
+
parts.push(`${n} attachment${n === 1 ? '' : 's'} listed (names only; not extracted)`);
|
|
144
|
+
if (model.bodySource === 'html')
|
|
145
|
+
parts.push('HTML body rendered as plain text');
|
|
146
|
+
if (model.bodySource === 'rtf-only')
|
|
147
|
+
parts.push('body is RTF; no plain-text part');
|
|
148
|
+
parts.push('formatting and full headers omitted');
|
|
149
|
+
return { summary: parts.join('; '), text: sections.join('\n\n') };
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=email-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email-text.js","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/email-text.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AA2B9D,yEAAyE;AACzE,MAAM,CAAC,MAAM,kBAAkB,GAAG,mCAAmC,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,+EAA+E;AAC/E,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAErE,iFAAiF;AACjF,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC;IACzB,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM;IAC5D,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IACpD,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,E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nF,KAAK,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC;IAElD,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;AACpE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ExtractResult } from './doc-extract.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Extract the BODY text of a `.docx` (Word) document.
|
|
4
|
+
*
|
|
5
|
+
* A docx is a zip whose main part is `word/document.xml`. v1 extracts the body
|
|
6
|
+
* only — headers/footers are skipped, and the marker summary says so.
|
|
7
|
+
*
|
|
8
|
+
* - Paragraphs become lines. `<w:t>` runs are concatenated with NO separator
|
|
9
|
+
* (Word splits runs mid-word on formatting boundaries).
|
|
10
|
+
* - Tables become lines with cell text tab-separated, one line per row.
|
|
11
|
+
*/
|
|
12
|
+
export declare function extractDocx(bytes: Buffer): ExtractResult;
|
|
13
|
+
//# sourceMappingURL=extract-docx.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-docx.d.ts","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/extract-docx.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAG5D;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa,CAmDxD"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import AdmZip from 'adm-zip';
|
|
2
|
+
import { localBlocks, localElementBlocks, localName, paragraphRunText, zipEntryOversize } from './ooxml-text.js';
|
|
3
|
+
/**
|
|
4
|
+
* Extract the BODY text of a `.docx` (Word) document.
|
|
5
|
+
*
|
|
6
|
+
* A docx is a zip whose main part is `word/document.xml`. v1 extracts the body
|
|
7
|
+
* only — headers/footers are skipped, and the marker summary says so.
|
|
8
|
+
*
|
|
9
|
+
* - Paragraphs become lines. `<w:t>` runs are concatenated with NO separator
|
|
10
|
+
* (Word splits runs mid-word on formatting boundaries).
|
|
11
|
+
* - Tables become lines with cell text tab-separated, one line per row.
|
|
12
|
+
*/
|
|
13
|
+
export function extractDocx(bytes) {
|
|
14
|
+
let xml;
|
|
15
|
+
try {
|
|
16
|
+
const zip = new AdmZip(bytes);
|
|
17
|
+
const entry = zip.getEntry('word/document.xml');
|
|
18
|
+
if (!entry) {
|
|
19
|
+
return { ok: false, message: 'could not be parsed as a .docx (no word/document.xml inside the archive)' };
|
|
20
|
+
}
|
|
21
|
+
// Declared-uncompressed-size bound BEFORE inflation — see zipEntryOversize.
|
|
22
|
+
const oversize = zipEntryOversize(entry);
|
|
23
|
+
if (oversize)
|
|
24
|
+
return { ok: false, message: `could not be extracted as a .docx (${oversize})` };
|
|
25
|
+
xml = entry.getData().toString('utf8');
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
return { ok: false, message: `could not be parsed as a .docx (${err.message})` };
|
|
29
|
+
}
|
|
30
|
+
// The BODY element read by the parser rather than matched by a regex: a
|
|
31
|
+
// comment or CDATA section mentioning `<w:body>` could answer as the
|
|
32
|
+
// document body and hand back its text instead of the real one.
|
|
33
|
+
const body = localBlocks(xml, 'body')[0] ?? xml;
|
|
34
|
+
const lines = [];
|
|
35
|
+
let paragraphs = 0;
|
|
36
|
+
let tables = 0;
|
|
37
|
+
// Tables and paragraphs in DOCUMENT order, straight from the parser: a match
|
|
38
|
+
// is never descended into, so a table's own paragraphs stay inside it and are
|
|
39
|
+
// rendered once, as its rows. The hand-rolled splitter this replaces counted
|
|
40
|
+
// `<w:tbl>` opens and closes with a regex, which a comment or CDATA section
|
|
41
|
+
// mentioning either could throw off — splitting a paragraph in half and
|
|
42
|
+
// dropping its text.
|
|
43
|
+
for (const block of localElementBlocks(body, ['tbl', 'p'])) {
|
|
44
|
+
if (localName(block.name) === 'tbl') {
|
|
45
|
+
tables++;
|
|
46
|
+
for (const row of localBlocks(block.body ?? '', 'tr')) {
|
|
47
|
+
const cells = localBlocks(row, 'tc').map((cell) => paragraphRunText(cell, 't'));
|
|
48
|
+
lines.push(cells.join(' '));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
lines.push(paragraphRunText(block.body ?? '', 't'));
|
|
53
|
+
paragraphs++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
while (lines.length > 0 && lines[lines.length - 1].trim() === '')
|
|
57
|
+
lines.pop();
|
|
58
|
+
const parts = [`${paragraphs} paragraph${paragraphs === 1 ? '' : 's'}`];
|
|
59
|
+
if (tables > 0)
|
|
60
|
+
parts.push(`${tables} table${tables === 1 ? '' : 's'}`);
|
|
61
|
+
return {
|
|
62
|
+
ok: true,
|
|
63
|
+
summary: `${parts.join(' + ')}; body only (headers/footers skipped); layout, images and formatting omitted`,
|
|
64
|
+
text: lines.join('\n'),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=extract-docx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-docx.js","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/extract-docx.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,SAAS,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEjH;;;;;;;;;GASG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,IAAI,GAAW,CAAC;IAChB,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;QAC9B,MAAM,KAAK,GAAG,GAAG,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CAAC;QAChD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,E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AG,CAAC,CAAC,CAAC;gBAChF,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;YACpD,UAAU,EAAE,CAAC;QACf,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE;QAAE,KAAK,CAAC,GAAG,EAAE,CAAC;IAE9E,MAAM,KAAK,GAAG,CAAC,GAAG,UAAU,aAAa,UAAU,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IACxE,IAAI,MAAM,GAAG,CAAC;QAAE,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,SAAS,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IACxE,OAAO;QACL,EAAE,EAAE,IAAI;QACR,OAAO,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,8EAA8E;QAC3G,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;KACvB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ExtractResult } from './doc-extract.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Extract a `.eml` (RFC 822 / MIME) email into the shared email text shape
|
|
4
|
+
* (see `email-text.ts`).
|
|
5
|
+
*
|
|
6
|
+
* Parsing is `postal-mime` (postalsys, MIT-0): small, dependency-free, ESM,
|
|
7
|
+
* and the same code runs in the browser — which keeps the frontend viewer's
|
|
8
|
+
* story identical to the agent's. It normalizes encoded-word headers and
|
|
9
|
+
* multipart bodies, and hands the `Date:` header over as ISO when it parses.
|
|
10
|
+
*
|
|
11
|
+
* postal-mime is LENIENT — random bytes "parse" to an empty message rather
|
|
12
|
+
* than throwing — so recognizability is checked here: a file yielding NO
|
|
13
|
+
* email headers at all (no From/To/Cc/Bcc/Subject/Date/Message-ID) and no
|
|
14
|
+
* attachments is not an email, and gets the typed could-not-be-parsed
|
|
15
|
+
* failure instead of an empty extraction.
|
|
16
|
+
*/
|
|
17
|
+
export declare function extractEml(bytes: Buffer): Promise<ExtractResult>;
|
|
18
|
+
//# sourceMappingURL=extract-eml.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-eml.d.ts","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/extract-eml.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAI5D;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CA4BtE"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import PostalMime from 'postal-mime';
|
|
2
|
+
import { emailExtraction, htmlToEmailText } from './email-text.js';
|
|
3
|
+
import { MAX_DOC_PART_BYTES } from './ooxml-text.js';
|
|
4
|
+
/**
|
|
5
|
+
* Extract a `.eml` (RFC 822 / MIME) email into the shared email text shape
|
|
6
|
+
* (see `email-text.ts`).
|
|
7
|
+
*
|
|
8
|
+
* Parsing is `postal-mime` (postalsys, MIT-0): small, dependency-free, ESM,
|
|
9
|
+
* and the same code runs in the browser — which keeps the frontend viewer's
|
|
10
|
+
* story identical to the agent's. It normalizes encoded-word headers and
|
|
11
|
+
* multipart bodies, and hands the `Date:` header over as ISO when it parses.
|
|
12
|
+
*
|
|
13
|
+
* postal-mime is LENIENT — random bytes "parse" to an empty message rather
|
|
14
|
+
* than throwing — so recognizability is checked here: a file yielding NO
|
|
15
|
+
* email headers at all (no From/To/Cc/Bcc/Subject/Date/Message-ID) and no
|
|
16
|
+
* attachments is not an email, and gets the typed could-not-be-parsed
|
|
17
|
+
* failure instead of an empty extraction.
|
|
18
|
+
*/
|
|
19
|
+
export async function extractEml(bytes) {
|
|
20
|
+
// The same raw-size bound the PDF extractor applies: no container to
|
|
21
|
+
// pre-scan, so the bound is the file's size, checked before parsing.
|
|
22
|
+
if (bytes.length > MAX_DOC_PART_BYTES) {
|
|
23
|
+
return {
|
|
24
|
+
ok: false,
|
|
25
|
+
message: `could not be extracted as a .eml (the file is ${bytes.length} bytes — over the ${MAX_DOC_PART_BYTES}-byte (50 MB) extraction limit)`,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
let email;
|
|
29
|
+
try {
|
|
30
|
+
email = await PostalMime.parse(bytes);
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
return { ok: false, message: `could not be parsed as a .eml (${err.message})` };
|
|
34
|
+
}
|
|
35
|
+
if (email.from === undefined &&
|
|
36
|
+
email.to === undefined &&
|
|
37
|
+
email.cc === undefined &&
|
|
38
|
+
email.bcc === undefined &&
|
|
39
|
+
email.subject === undefined &&
|
|
40
|
+
email.date === undefined &&
|
|
41
|
+
email.messageId === undefined &&
|
|
42
|
+
email.attachments.length === 0) {
|
|
43
|
+
return { ok: false, message: 'could not be parsed as a .eml (no email headers found)' };
|
|
44
|
+
}
|
|
45
|
+
return { ok: true, ...emailExtraction(emlModel(email)) };
|
|
46
|
+
}
|
|
47
|
+
/** postal-mime's parse, shaped into the format-independent email model. */
|
|
48
|
+
function emlModel(email) {
|
|
49
|
+
const text = email.text !== undefined && email.text.trim() !== '' ? email.text : undefined;
|
|
50
|
+
const html = email.html !== undefined && email.html.trim() !== '' ? email.html : undefined;
|
|
51
|
+
const body = text ?? (html !== undefined ? htmlToEmailText(html) : '');
|
|
52
|
+
return {
|
|
53
|
+
from: email.from && addressListText([email.from]),
|
|
54
|
+
to: email.to && addressListText(email.to),
|
|
55
|
+
cc: email.cc && addressListText(email.cc),
|
|
56
|
+
bcc: email.bcc && addressListText(email.bcc),
|
|
57
|
+
subject: email.subject,
|
|
58
|
+
date: email.date !== undefined ? isoDate(email.date) : undefined,
|
|
59
|
+
body: body.replace(/\s+$/, ''),
|
|
60
|
+
bodySource: text !== undefined ? 'text' : html !== undefined ? 'html' : 'none',
|
|
61
|
+
attachments: email.attachments.map((a) => ({
|
|
62
|
+
name: a.filename ?? 'unnamed attachment',
|
|
63
|
+
mimeType: a.mimeType,
|
|
64
|
+
sizeBytes: typeof a.content === 'string' ? Buffer.byteLength(a.content) : a.content.byteLength,
|
|
65
|
+
})),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** `Name <addr>, addr2, Group: member, member` — groups flattened inline. */
|
|
69
|
+
function addressListText(list) {
|
|
70
|
+
const s = list
|
|
71
|
+
.map(function one(a) {
|
|
72
|
+
if (a.group !== undefined)
|
|
73
|
+
return `${a.name}: ${a.group.map(one).join(', ')}`;
|
|
74
|
+
if (a.address === undefined || a.address === '')
|
|
75
|
+
return a.name;
|
|
76
|
+
return a.name !== '' && a.name !== a.address ? `${a.name} <${a.address}>` : a.address;
|
|
77
|
+
})
|
|
78
|
+
.filter((t) => t !== '')
|
|
79
|
+
.join(', ');
|
|
80
|
+
return s === '' ? undefined : s;
|
|
81
|
+
}
|
|
82
|
+
/** postal-mime already normalizes parseable dates to ISO; keep the raw value when it could not. */
|
|
83
|
+
function isoDate(value) {
|
|
84
|
+
const d = new Date(value);
|
|
85
|
+
return Number.isNaN(d.getTime()) ? value : d.toISOString();
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=extract-eml.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-eml.js","sourceRoot":"","sources":["../../../../src/modules/workspace/file-readers/extract-eml.ts"],"names":[],"mappings":"AAAA,OAAO,UAAwC,MAAM,aAAa,CAAC;AAEnE,OAAO,EA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B,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AAC7D,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ExtractResult } from './doc-extract.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Extract a `.msg` (Outlook item, CFB container) email into the shared email
|
|
4
|
+
* text shape (see `email-text.ts`).
|
|
5
|
+
*
|
|
6
|
+
* Parsing is `@kenjiuno/msgreader` (HiraokaHyperTools, Apache-2.0) — the
|
|
7
|
+
* maintained MAPI/CFB reader. It never throws for bad content of its own
|
|
8
|
+
* accord: unparseable bytes come back as `{ dataType: null, error }`, which
|
|
9
|
+
* maps onto the typed could-not-be-parsed failure here.
|
|
10
|
+
*
|
|
11
|
+
* Body preference mirrors `.eml`: the plain-text `PidTagBody` first, an HTML
|
|
12
|
+
* body stripped to text second. An Outlook item whose body exists ONLY as
|
|
13
|
+
* compressed RTF is degraded honestly — the extraction says
|
|
14
|
+
* "[body is RTF; no plain-text part]" instead of pretending to decode RTF.
|
|
15
|
+
*/
|
|
16
|
+
export declare function extractMsg(bytes: Buffer): ExtractResult;
|
|
17
|
+
//# sourceMappingURL=extract-msg.d.ts.map
|