@shuji-bonji/pdf-reader-mcp 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +144 -4
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/services/page-renderer.d.ts +25 -0
- package/dist/services/page-renderer.d.ts.map +1 -1
- package/dist/services/page-renderer.js +160 -62
- package/dist/services/page-renderer.js.map +1 -1
- package/dist/services/page-renderer.worker.mjs +104 -0
- package/dist/services/reading-scope.d.ts +115 -0
- package/dist/services/reading-scope.d.ts.map +1 -0
- package/dist/services/reading-scope.js +123 -0
- package/dist/services/reading-scope.js.map +1 -0
- package/dist/services/validation-service.d.ts.map +1 -1
- package/dist/services/validation-service.js +78 -15
- package/dist/services/validation-service.js.map +1 -1
- package/dist/tools/tier1/read-text.d.ts.map +1 -1
- package/dist/tools/tier1/read-text.js +49 -34
- package/dist/tools/tier1/read-text.js.map +1 -1
- package/dist/tools/tier1/read-url.d.ts.map +1 -1
- package/dist/tools/tier1/read-url.js +62 -38
- package/dist/tools/tier1/read-url.js.map +1 -1
- package/dist/tools/tier1/render-page.d.ts.map +1 -1
- package/dist/tools/tier1/render-page.js +2 -0
- package/dist/tools/tier1/render-page.js.map +1 -1
- package/dist/tools/tier1/search-text.d.ts.map +1 -1
- package/dist/tools/tier1/search-text.js +66 -55
- package/dist/tools/tier1/search-text.js.map +1 -1
- package/dist/tools/tier1/summarize.d.ts.map +1 -1
- package/dist/tools/tier1/summarize.js +71 -37
- package/dist/tools/tier1/summarize.js.map +1 -1
- package/dist/tools/tier2/extract-structured-text.d.ts.map +1 -1
- package/dist/tools/tier2/extract-structured-text.js +38 -28
- package/dist/tools/tier2/extract-structured-text.js.map +1 -1
- package/dist/tools/tier3/compare-structure.d.ts.map +1 -1
- package/dist/tools/tier3/compare-structure.js +2 -0
- package/dist/tools/tier3/compare-structure.js.map +1 -1
- package/dist/tools/tier3/validate-tagged.d.ts.map +1 -1
- package/dist/tools/tier3/validate-tagged.js +2 -0
- package/dist/tools/tier3/validate-tagged.js.map +1 -1
- package/dist/types.d.ts +56 -13
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/error-handler.d.ts.map +1 -1
- package/dist/utils/error-handler.js +15 -2
- package/dist/utils/error-handler.js.map +1 -1
- package/dist/utils/formatter.d.ts.map +1 -1
- package/dist/utils/formatter.js +41 -17
- package/dist/utils/formatter.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ResponseFormat } from '../../constants.js';
|
|
2
2
|
import { ReadUrlSchema } from '../../schemas/tier1.js';
|
|
3
3
|
import { extractTextFromDoc, loadDocumentFromData } from '../../services/pdfjs-service.js';
|
|
4
|
+
import { bothFailedError, bothHalves, formatReadingScope } from '../../services/reading-scope.js';
|
|
4
5
|
import { byPage, observeExtractabilityFromData, summarizeExtractability, } from '../../services/text-extractability-service.js';
|
|
5
6
|
import { fetchPdfFromUrl } from '../../services/url-fetcher.js';
|
|
6
7
|
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
@@ -24,7 +25,7 @@ Args:
|
|
|
24
25
|
- compact_whitespace (boolean, optional): Collapse whitespace runs (incl. U+3000) to one ASCII space. Default false.
|
|
25
26
|
|
|
26
27
|
Returns:
|
|
27
|
-
|
|
28
|
+
\`{ scope, pages }\`, the same shape as read_text: \`pages\` is the extracted text by page number, and \`scope\` says which of the two readings behind it were done — taking the characters off the page, and observing whether those characters have a route to Unicode (ISO 32000-2 §9.10.1). Either can fail on its own; only when neither could be done is this an error, and it then names both reasons.
|
|
28
29
|
|
|
29
30
|
Examples:
|
|
30
31
|
- Read remote PDF: { url: "https://example.com/document.pdf" }
|
|
@@ -38,52 +39,75 @@ Examples:
|
|
|
38
39
|
openWorldHint: true,
|
|
39
40
|
},
|
|
40
41
|
}, async (params) => {
|
|
42
|
+
let data;
|
|
41
43
|
try {
|
|
42
|
-
|
|
43
|
-
const doc = await loadDocumentFromData(data);
|
|
44
|
-
try {
|
|
45
|
-
const [extracted, observations] = await Promise.all([
|
|
46
|
-
extractTextFromDoc(doc, params.pages, {
|
|
47
|
-
splitColumns: params.split_columns,
|
|
48
|
-
compactWhitespace: params.compact_whitespace,
|
|
49
|
-
}),
|
|
50
|
-
// #21: the same three-state answer as read_text — the fetched bytes
|
|
51
|
-
// are already here, so there is no reason for this path to be the
|
|
52
|
-
// one that still returns a bare empty string.
|
|
53
|
-
observeExtractabilityFromData(data, params.pages),
|
|
54
|
-
]);
|
|
55
|
-
const observed = byPage(observations);
|
|
56
|
-
const results = extracted.map((page) => ({
|
|
57
|
-
...page,
|
|
58
|
-
...(observed.has(page.page) ? { extractability: observed.get(page.page) } : {}),
|
|
59
|
-
}));
|
|
60
|
-
let text;
|
|
61
|
-
if (params.response_format === ResponseFormat.JSON) {
|
|
62
|
-
text = JSON.stringify(results, null, 2);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
text = [
|
|
66
|
-
...summarizeExtractability(observations),
|
|
67
|
-
'',
|
|
68
|
-
formatPageTextsMarkdown(results),
|
|
69
|
-
].join('\n');
|
|
70
|
-
}
|
|
71
|
-
const { text: finalText } = truncateIfNeeded(text);
|
|
72
|
-
return {
|
|
73
|
-
content: [{ type: 'text', text: finalText }],
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
finally {
|
|
77
|
-
await doc.destroy();
|
|
78
|
-
}
|
|
44
|
+
data = await fetchPdfFromUrl(params.url);
|
|
79
45
|
}
|
|
80
46
|
catch (error) {
|
|
47
|
+
// 取ってこられなければ、どちらの読み方も始まらない。ここだけは 1 つの理由。
|
|
81
48
|
const err = handleStructuredError(error);
|
|
82
49
|
return {
|
|
83
50
|
content: [{ type: 'text', text: JSON.stringify(err, null, 2) }],
|
|
84
51
|
isError: true,
|
|
85
52
|
};
|
|
86
53
|
}
|
|
54
|
+
// 🔴 pdfjs は渡された配列の中身を worker へ移す。移したあと元の配列は空になり、
|
|
55
|
+
// 同じ配列を読む 2 人目には 0 バイトに見える(実測: loadDocumentFromData の
|
|
56
|
+
// あと data.buffer.byteLength === 0、data.slice が detached ArrayBuffer で落ちる)。
|
|
57
|
+
// 0.13.0 ではそれが原因で read_url が全検体で「No PDF header found」を返していた。
|
|
58
|
+
// 読み手が 2 人いるので、それぞれに自分の分を渡す。
|
|
59
|
+
const forObservation = new Uint8Array(data);
|
|
60
|
+
let doc;
|
|
61
|
+
const { text: extraction, observation, scope, } = await bothHalves(async () => {
|
|
62
|
+
doc = await loadDocumentFromData(data);
|
|
63
|
+
return extractTextFromDoc(doc, params.pages, {
|
|
64
|
+
splitColumns: params.split_columns,
|
|
65
|
+
compactWhitespace: params.compact_whitespace,
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
// #21: the same four-state answer as read_text — the fetched bytes are
|
|
69
|
+
// already here, so there is no reason for this path to be the one that
|
|
70
|
+
// still returns a bare empty string.
|
|
71
|
+
() => observeExtractabilityFromData(forObservation, params.pages));
|
|
72
|
+
if (doc)
|
|
73
|
+
await doc.destroy();
|
|
74
|
+
if (!extraction.ok && !observation.ok) {
|
|
75
|
+
return {
|
|
76
|
+
content: [
|
|
77
|
+
{
|
|
78
|
+
type: 'text',
|
|
79
|
+
text: JSON.stringify(bothFailedError(extraction.error, observation.error), null, 2),
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
isError: true,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const observations = observation.ok ? observation.value : [];
|
|
86
|
+
const observed = byPage(observations);
|
|
87
|
+
const results = extraction.ok
|
|
88
|
+
? extraction.value.map((page) => ({
|
|
89
|
+
...page,
|
|
90
|
+
...(observed.has(page.page) ? { extractability: observed.get(page.page) } : {}),
|
|
91
|
+
}))
|
|
92
|
+
: observations.map((o) => ({ page: o.page, text: null, extractability: o }));
|
|
93
|
+
let text;
|
|
94
|
+
if (params.response_format === ResponseFormat.JSON) {
|
|
95
|
+
text = JSON.stringify({ scope, pages: results }, null, 2);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
const banner = observation.ok ? summarizeExtractability(observations) : [];
|
|
99
|
+
text = [
|
|
100
|
+
...formatReadingScope(scope),
|
|
101
|
+
'',
|
|
102
|
+
...banner,
|
|
103
|
+
'',
|
|
104
|
+
formatPageTextsMarkdown(results),
|
|
105
|
+
].join('\n');
|
|
106
|
+
}
|
|
107
|
+
const { text: finalText } = truncateIfNeeded(text);
|
|
108
|
+
return {
|
|
109
|
+
content: [{ type: 'text', text: finalText }],
|
|
110
|
+
};
|
|
87
111
|
});
|
|
88
112
|
}
|
|
89
113
|
//# sourceMappingURL=read-url.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-url.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-url.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAqB,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC3F,OAAO,EACL,MAAM,EACN,6BAA6B,EAC7B,uBAAuB,GACxB,MAAM,+CAA+C,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"read-url.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-url.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAqB,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC3F,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAClG,OAAO,EACL,MAAM,EACN,6BAA6B,EAC7B,uBAAuB,GACxB,MAAM,+CAA+C,CAAC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F,CAAC,CAAC;YACL,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAE/E,IAAI,IAAY,CAAC;QACjB,IAAI,MAAM,CAAC,eAAe,KAAK,cAAc,CAAC,IAAI,EAAE,CAAC;YACnD,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QAC5D,CAAC;aAAM,CAAC;YACN,MAAM,MAAM,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,uBAAuB,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC3E,IAAI,GAAG;gBACL,GAAG,kBAAkB,CAAC,KAAK,CAAC;gBAC5B,EAAE;gBACF,GAAG,MAAM;gBACT,EAAE;gBACF,uBAAuB,CAAC,OAAO,CAAC;aACjC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACf,CAAC;QAED,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;QACnD,OAAO;YACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;SACtD,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render-page.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/render-page.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAW9D,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"render-page.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/render-page.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAW9D,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAkG1D"}
|
|
@@ -24,6 +24,8 @@ Args:
|
|
|
24
24
|
Returns:
|
|
25
25
|
A text block with per-page metadata (point size, pixel size, effective dpi, bytes) and any omissions, then one image content block per rendered page.
|
|
26
26
|
|
|
27
|
+
Rasterising a page can take unbounded time — a tiling pattern (ISO 32000-2 §8.7.3.1) whose \`/XStep\` or \`/YStep\` is a near-zero magnitude asks for an astronomical number of tiles, and the clause forbids only zero. Each page therefore gets 20 seconds (\`PDF_READER_RENDER_TIMEOUT_MS\` overrides it); the rendering runs off the main thread, so a page that does not finish is stopped and named in the omissions rather than taking the server down with it. The pages rendered before it are still returned, and the pages after it are reported separately as not attempted — "could not be rendered" and "never started" are different answers.
|
|
28
|
+
|
|
27
29
|
Examples:
|
|
28
30
|
- A scanned page: { file_path: "/path/to/scan.pdf", pages: "1", format: "jpeg" }
|
|
29
31
|
- A diagram at high detail: { file_path: "/path/to/doc.pdf", pages: "3", dpi: 300 }`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render-page.js","sourceRoot":"","sources":["../../../src/tools/tier1/render-page.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EACL,kBAAkB,EAClB,wBAAwB,EACxB,iBAAiB,EACjB,WAAW,GACZ,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAErE,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IAClD,MAAM,CAAC,YAAY,CACjB,aAAa,EACb;QACE,KAAK,EAA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,CAAC,CAAC;gBAC1D,CAAC;YACH,CAAC;YAED,OAAO;gBACL,OAAO,EAAE;oBACP,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;oBACjD,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;wBAC7B,IAAI,EAAE,OAAgB;wBACtB,IAAI,EAAE,IAAI,CAAC,UAAU;wBACrB,QAAQ,EAAE,IAAI,CAAC,QAAQ;qBACxB,CAAC,CAAC;iBACJ;aACF,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,GAAG,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;YACzC,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;gBACxE,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAS9D,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAS9D,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAwH1D"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ResponseFormat } from '../../constants.js';
|
|
2
2
|
import { SearchTextSchema } from '../../schemas/tier1.js';
|
|
3
3
|
import { searchText } from '../../services/pdfjs-service.js';
|
|
4
|
+
import { bothFailedError, bothHalves, formatReadingScope } from '../../services/reading-scope.js';
|
|
4
5
|
import { observeExtractability } from '../../services/text-extractability-service.js';
|
|
5
|
-
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
6
6
|
import { formatSearchResultMarkdown } from '../../utils/formatter.js';
|
|
7
7
|
export function registerSearchText(server) {
|
|
8
8
|
server.registerTool('search_text', {
|
|
@@ -22,7 +22,7 @@ Args:
|
|
|
22
22
|
- response_format ('markdown' | 'json'): Output format (default: 'markdown')
|
|
23
23
|
|
|
24
24
|
Returns:
|
|
25
|
-
Search matches with page number, matched text, and surrounding context.
|
|
25
|
+
Search matches with page number, matched text, and surrounding context, plus \`scope\` — which of the two readings behind the answer were done: searching the characters on the page, and observing whether those characters have a route to Unicode (ISO 32000-2 §9.10.1). When the search itself could not run, \`totalMatches\` and \`matches\` are \`null\` rather than \`0\` and \`[]\`: "could not search" and "searched and found nothing" are different answers.
|
|
26
26
|
|
|
27
27
|
Examples:
|
|
28
28
|
- Search entire PDF: { file_path: "/path/to/doc.pdf", query: "digital signature" }
|
|
@@ -35,63 +35,74 @@ Examples:
|
|
|
35
35
|
openWorldHint: false,
|
|
36
36
|
},
|
|
37
37
|
}, async (params) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const unsearchable = observations.filter((page) => page.state !== 'extracted');
|
|
46
|
-
const truncated = allMatches.length > params.max_results;
|
|
47
|
-
const matches = allMatches.slice(0, params.max_results);
|
|
48
|
-
const result = {
|
|
49
|
-
query: params.query,
|
|
50
|
-
totalMatches: allMatches.length,
|
|
51
|
-
matches,
|
|
52
|
-
truncated,
|
|
53
|
-
...(unsearchable.length > 0 ? { unsearchablePages: unsearchable } : {}),
|
|
54
|
-
};
|
|
55
|
-
// #18 resolves /ActualText, so the blanket #15 warning no longer
|
|
56
|
-
// applies — but the resolution can still be skipped. Keep saying so,
|
|
57
|
-
// now only where it is actually true, and say which of the two reasons
|
|
58
|
-
// it is: they call for different next steps, and pointing an encrypted
|
|
59
|
-
// document at extract_structured_text would waste the caller's time
|
|
60
|
-
// (that tool reads its replacement text through pdf-lib as well).
|
|
61
|
-
if (allMatches.length === 0 && unresolvedPages.length === 0 && unsearchable.length > 0) {
|
|
62
|
-
result.note =
|
|
63
|
-
`No matches. ${unsearchable.length} of the ${observations.length} page(s) searched ` +
|
|
64
|
-
'do not yield text that can be converted to Unicode (ISO 32000-2 §9.10.1), so this ' +
|
|
65
|
-
'result says the query was not found in what could be read — not that it is absent ' +
|
|
66
|
-
'from the document. See the per-page states above.';
|
|
67
|
-
}
|
|
68
|
-
else if (allMatches.length === 0 && unresolvedPages.length > 0) {
|
|
69
|
-
result.note =
|
|
70
|
-
unresolvedReason === 'encrypted'
|
|
71
|
-
? 'No matches. This document is encrypted, so replacement text (/ActualText, ' +
|
|
72
|
-
'ISO 32000-2 §14.9.4) could not be read: §7.6.2 encrypts strings and streams, ' +
|
|
73
|
-
'and this server does not decrypt them. What was searched is the glyphs as ' +
|
|
74
|
-
'drawn. No other tool here will do better — decrypt the file first if you ' +
|
|
75
|
-
'need the replacement text.'
|
|
76
|
-
: 'No matches. Replacement text (/ActualText, ISO 32000-2 §14.9.4) was resolved, ' +
|
|
77
|
-
`except on page(s) ${unresolvedPages.join(', ')}, whose content stream could not ` +
|
|
78
|
-
'be aligned with the extracted text. A Span-level replacement there would be ' +
|
|
79
|
-
'invisible to this search — try extract_structured_text if the document is tagged.';
|
|
80
|
-
}
|
|
81
|
-
const text = params.response_format === ResponseFormat.JSON
|
|
82
|
-
? JSON.stringify(result, null, 2)
|
|
83
|
-
: formatSearchResultMarkdown(result);
|
|
84
|
-
return {
|
|
85
|
-
content: [{ type: 'text', text }],
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
const err = handleStructuredError(error);
|
|
38
|
+
// 🔴 検索と観測は別々に失敗しうる。Promise.all に入れると、先に失敗した
|
|
39
|
+
// ほうの理由だけが残り、もう片方の答えが捨てられる。
|
|
40
|
+
const { text: search, observation, scope, } = await bothHalves(() => searchText(params.file_path, params.query, params.context_chars, params.pages),
|
|
41
|
+
// #21: zero hits on a page whose characters do not convert to Unicode
|
|
42
|
+
// is "not found in what could be read", not "not in the document".
|
|
43
|
+
() => observeExtractability(params.file_path, params.pages));
|
|
44
|
+
if (!search.ok && !observation.ok) {
|
|
90
45
|
return {
|
|
91
|
-
content: [
|
|
46
|
+
content: [
|
|
47
|
+
{
|
|
48
|
+
type: 'text',
|
|
49
|
+
text: JSON.stringify(bothFailedError(search.error, observation.error), null, 2),
|
|
50
|
+
},
|
|
51
|
+
],
|
|
92
52
|
isError: true,
|
|
93
53
|
};
|
|
94
54
|
}
|
|
55
|
+
const observations = observation.ok ? observation.value : [];
|
|
56
|
+
const unsearchable = observations.filter((page) => page.state !== 'extracted');
|
|
57
|
+
const allMatches = search.ok ? search.value.matches : null;
|
|
58
|
+
const unresolvedPages = search.ok ? search.value.unresolvedPages : [];
|
|
59
|
+
const unresolvedReason = search.ok ? search.value.unresolvedReason : undefined;
|
|
60
|
+
const truncated = allMatches === null ? null : allMatches.length > params.max_results;
|
|
61
|
+
const matches = allMatches === null ? null : allMatches.slice(0, params.max_results);
|
|
62
|
+
const result = {
|
|
63
|
+
scope,
|
|
64
|
+
query: params.query,
|
|
65
|
+
// 🔴 探せなかったときは null。0 と書くと「探して見つからなかった」になる。
|
|
66
|
+
totalMatches: allMatches === null ? null : allMatches.length,
|
|
67
|
+
matches,
|
|
68
|
+
truncated,
|
|
69
|
+
...(unsearchable.length > 0 ? { unsearchablePages: unsearchable } : {}),
|
|
70
|
+
};
|
|
71
|
+
// #18 resolves /ActualText, so the blanket #15 warning no longer
|
|
72
|
+
// applies — but the resolution can still be skipped. Keep saying so,
|
|
73
|
+
// now only where it is actually true, and say which of the two reasons
|
|
74
|
+
// it is: they call for different next steps, and pointing an encrypted
|
|
75
|
+
// document at extract_structured_text would waste the caller's time
|
|
76
|
+
// (that tool reads its replacement text through pdf-lib as well).
|
|
77
|
+
if (allMatches !== null &&
|
|
78
|
+
allMatches.length === 0 &&
|
|
79
|
+
unresolvedPages.length === 0 &&
|
|
80
|
+
unsearchable.length > 0) {
|
|
81
|
+
result.note =
|
|
82
|
+
`No matches. ${unsearchable.length} of the ${observations.length} page(s) searched ` +
|
|
83
|
+
'do not yield text that can be converted to Unicode (ISO 32000-2 §9.10.1), so this ' +
|
|
84
|
+
'result says the query was not found in what could be read — not that it is absent ' +
|
|
85
|
+
'from the document. See the per-page states above.';
|
|
86
|
+
}
|
|
87
|
+
else if (allMatches !== null && allMatches.length === 0 && unresolvedPages.length > 0) {
|
|
88
|
+
result.note =
|
|
89
|
+
unresolvedReason === 'encrypted'
|
|
90
|
+
? 'No matches. This document is encrypted, so replacement text (/ActualText, ' +
|
|
91
|
+
'ISO 32000-2 §14.9.4) could not be read: §7.6.2 encrypts strings and streams, ' +
|
|
92
|
+
'and this server does not decrypt them. What was searched is the glyphs as ' +
|
|
93
|
+
'drawn. No other tool here will do better — decrypt the file first if you ' +
|
|
94
|
+
'need the replacement text.'
|
|
95
|
+
: 'No matches. Replacement text (/ActualText, ISO 32000-2 §14.9.4) was resolved, ' +
|
|
96
|
+
`except on page(s) ${unresolvedPages.join(', ')}, whose content stream could not ` +
|
|
97
|
+
'be aligned with the extracted text. A Span-level replacement there would be ' +
|
|
98
|
+
'invisible to this search — try extract_structured_text if the document is tagged.';
|
|
99
|
+
}
|
|
100
|
+
const text = params.response_format === ResponseFormat.JSON
|
|
101
|
+
? JSON.stringify(result, null, 2)
|
|
102
|
+
: [...formatReadingScope(scope), '', formatSearchResultMarkdown(result)].join('\n');
|
|
103
|
+
return {
|
|
104
|
+
content: [{ type: 'text', text }],
|
|
105
|
+
};
|
|
95
106
|
});
|
|
96
107
|
}
|
|
97
108
|
//# sourceMappingURL=search-text.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"search-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAClG,OAAO,EAAE,qBAAqB,EAAE,MAAM,+CAA+C,CAAC;AAEtF,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAEtE,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IAClD,MAAM,CAAC,YAAY,CACjB,aAAa,EACb;QACE,KAAK,EAAE,iBAAiB;QACxB,WAAW,EAAE;;;;;;;;;;;;;;;;;;;4FAmByE;QACtF,WAAW,EAAE,gBAAgB;QAC7B,WAAW,EAAE;YACX,YAAY,EAAE,IAAI;YAClB,eAAe,EAAE,KAAK;YACtB,cAAc,EAAE,IAAI;YACpB,aAAa,EAAE,KAAK;SACrB;KACF,EACD,KAAK,EAAE,MAAuB,EAAE,EAAE;QAChC,6CAA6C;QAC7C,4BAA4B;QAC5B,MAAM,EACJ,IAAI,EAAE,MAAM,EACZ,WAAW,EACX,KAAK,GACN,GAAG,MAAM,UAAU,CAClB,GAAG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C9B,CAAC,CAAC,gFAAgF;wBAChF,qBAAqB,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,mCAAmC;wBAClF,8EAA8E;wBAC9E,mFAAmF,CAAC;QAC5F,CAAC;QAED,MAAM,IAAI,GACR,MAAM,CAAC,eAAe,KAAK,cAAc,CAAC,IAAI;YAC5C,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YACjC,CAAC,CAAC,CAAC,GAAG,kBAAkB,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,0BAA0B,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAExF,OAAO;YACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,CAAC;SAC3C,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summarize.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/summarize.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"summarize.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/summarize.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AA4G9D,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAsHzD"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ResponseFormat } from '../../constants.js';
|
|
2
2
|
import { SummarizeSchema } from '../../schemas/tier1.js';
|
|
3
3
|
import { countImagesFromDoc, extractTextFromDoc, getMetadataFromDoc, loadDocument, } from '../../services/pdfjs-service.js';
|
|
4
|
+
import { bothFailedError, formatSummaryScope, half, } from '../../services/reading-scope.js';
|
|
4
5
|
import { foldExtractability, observeExtractability, } from '../../services/text-extractability-service.js';
|
|
5
|
-
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
6
6
|
import { formatSummaryMarkdown } from '../../utils/formatter.js';
|
|
7
7
|
/**
|
|
8
8
|
* Derive "what to call next" from the summary's own observations (#24).
|
|
@@ -14,13 +14,17 @@ import { formatSummaryMarkdown } from '../../utils/formatter.js';
|
|
|
14
14
|
*/
|
|
15
15
|
function suggestNext(metadata, textExtractability, unreadablePages) {
|
|
16
16
|
const next = [];
|
|
17
|
+
// 🔴 観測できなかった前提からは何も勧めない。読まなかったことを
|
|
18
|
+
// 「そうではなかった」として扱うと、助言が観測の顔をする。
|
|
19
|
+
if (!metadata)
|
|
20
|
+
return next;
|
|
17
21
|
if (metadata.isEncrypted) {
|
|
18
22
|
next.push('isEncrypted is true: content streams and strings are ciphertext to this server ' +
|
|
19
23
|
'(ISO 32000-2 §7.6.2), so text and structure tools will under-report. Decrypt the ' +
|
|
20
24
|
'file first if its content is needed.');
|
|
21
25
|
return next;
|
|
22
26
|
}
|
|
23
|
-
const unreadable = unreadablePages.filter((page) => page.state === 'no_text_layer' || page.state === 'not_extractable');
|
|
27
|
+
const unreadable = (unreadablePages ?? []).filter((page) => page.state === 'no_text_layer' || page.state === 'not_extractable');
|
|
24
28
|
if (textExtractability === 'no_text_layer' || textExtractability === 'not_extractable') {
|
|
25
29
|
const pageList = formatPageList(unreadable.map((page) => page.page));
|
|
26
30
|
next.push(`textExtractability is ${textExtractability} (page ${pageList}): read those pages as ` +
|
|
@@ -78,6 +82,8 @@ The summary ends with a \`next\` list: tool suggestions derived from the observa
|
|
|
78
82
|
Returns:
|
|
79
83
|
Summary including: page count, PDF version, file size, tagged/encrypted/signature flags, text presence, per-document text extractability, the pages that are not fully extractable, image count, a text preview from page 1, and the \`next\` suggestions.
|
|
80
84
|
|
|
85
|
+
Four separate readings produce that summary — the document information, the text of page 1, the image count, and the extractability observation — and any of them can fail on its own. \`scope\` says which were done. A field whose reading did not happen is \`null\`, never \`0\`, \`false\` or \`""\`: "not read" and "read and found nothing" are different answers, and \`next\` stays silent about any premise that was not observed.
|
|
86
|
+
|
|
81
87
|
Examples:
|
|
82
88
|
- Quick overview: { file_path: "/path/to/doc.pdf" }
|
|
83
89
|
- Machine-readable: { file_path: "/path/to/doc.pdf", response_format: "json" }`,
|
|
@@ -89,52 +95,80 @@ Examples:
|
|
|
89
95
|
openWorldHint: false,
|
|
90
96
|
},
|
|
91
97
|
}, async (params) => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
98
|
+
// 🔴 4 つの読みは別々に失敗しうる。Promise.all に入れると、先に失敗した
|
|
99
|
+
// ものの理由だけが残り、ほかの 3 つの答えが捨てられる。
|
|
100
|
+
// 3 つは同じ pdfjs の文書から取るので、文書が開けなければ 3 つとも同じ理由になる。
|
|
101
|
+
const docHalf = await half(() => loadDocument(params.file_path));
|
|
102
|
+
let metadataOutcome = docHalf.outcome;
|
|
103
|
+
let previewOutcome = docHalf.outcome;
|
|
104
|
+
let imagesOutcome = docHalf.outcome;
|
|
105
|
+
let metadata = null;
|
|
106
|
+
let textPreview = null;
|
|
107
|
+
let imageCount = null;
|
|
108
|
+
if (docHalf.ok) {
|
|
109
|
+
const doc = docHalf.value;
|
|
95
110
|
try {
|
|
96
|
-
const [
|
|
97
|
-
getMetadataFromDoc(doc, params.file_path),
|
|
98
|
-
extractTextFromDoc(doc, '1'),
|
|
99
|
-
countImagesFromDoc(doc),
|
|
100
|
-
// #21: over ALL pages, not just page 1. `hasText` is decided from
|
|
101
|
-
// the first page's preview, so a document whose page 1 happens to
|
|
102
|
-
// carry a title would otherwise report "has text" for 400 scanned
|
|
103
|
-
// pages behind it.
|
|
104
|
-
observeExtractability(params.file_path),
|
|
111
|
+
const [m, t, i] = await Promise.all([
|
|
112
|
+
half(() => getMetadataFromDoc(doc, params.file_path)),
|
|
113
|
+
half(() => extractTextFromDoc(doc, '1')),
|
|
114
|
+
half(() => countImagesFromDoc(doc)),
|
|
105
115
|
]);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
metadata,
|
|
113
|
-
textPreview,
|
|
114
|
-
imageCount,
|
|
115
|
-
hasText,
|
|
116
|
-
textExtractability,
|
|
117
|
-
unreadablePages,
|
|
118
|
-
next: suggestNext(metadata, textExtractability, unreadablePages),
|
|
119
|
-
};
|
|
120
|
-
const text = params.response_format === ResponseFormat.JSON
|
|
121
|
-
? JSON.stringify(summary, null, 2)
|
|
122
|
-
: formatSummaryMarkdown(summary);
|
|
123
|
-
return {
|
|
124
|
-
content: [{ type: 'text', text }],
|
|
125
|
-
};
|
|
116
|
+
metadataOutcome = m.outcome;
|
|
117
|
+
previewOutcome = t.outcome;
|
|
118
|
+
imagesOutcome = i.outcome;
|
|
119
|
+
metadata = m.ok ? m.value : null;
|
|
120
|
+
textPreview = t.ok ? (t.value[0]?.text?.slice(0, 500) ?? '') : null;
|
|
121
|
+
imageCount = i.ok ? i.value : null;
|
|
126
122
|
}
|
|
127
123
|
finally {
|
|
128
124
|
await doc.destroy();
|
|
129
125
|
}
|
|
130
126
|
}
|
|
131
|
-
|
|
132
|
-
|
|
127
|
+
// #21: over ALL pages, not just page 1. `hasText` is decided from the
|
|
128
|
+
// first page's preview, so a document whose page 1 happens to carry a
|
|
129
|
+
// title would otherwise report "has text" for 400 scanned pages behind it.
|
|
130
|
+
const obs = await half(() => observeExtractability(params.file_path));
|
|
131
|
+
const scope = {
|
|
132
|
+
metadata: metadataOutcome,
|
|
133
|
+
textPreview: previewOutcome,
|
|
134
|
+
imageCount: imagesOutcome,
|
|
135
|
+
extractabilityObservation: obs.outcome,
|
|
136
|
+
};
|
|
137
|
+
// どれ 1 つ読めなかったときだけ、エラーにする。
|
|
138
|
+
if (!docHalf.ok && !obs.ok) {
|
|
133
139
|
return {
|
|
134
|
-
content: [
|
|
140
|
+
content: [
|
|
141
|
+
{
|
|
142
|
+
type: 'text',
|
|
143
|
+
text: JSON.stringify(bothFailedError(docHalf.error, obs.error), null, 2),
|
|
144
|
+
},
|
|
145
|
+
],
|
|
135
146
|
isError: true,
|
|
136
147
|
};
|
|
137
148
|
}
|
|
149
|
+
const observations = obs.ok ? obs.value : null;
|
|
150
|
+
const textExtractability = observations ? foldExtractability(observations) : null;
|
|
151
|
+
const unreadablePages = observations
|
|
152
|
+
? observations.filter((page) => page.state !== 'extracted')
|
|
153
|
+
: null;
|
|
154
|
+
const summary = {
|
|
155
|
+
filePath: params.file_path,
|
|
156
|
+
scope,
|
|
157
|
+
metadata,
|
|
158
|
+
textPreview,
|
|
159
|
+
imageCount,
|
|
160
|
+
// 🔴 1 ページ目の文字を読めていないなら、文字があるともないとも言えない。
|
|
161
|
+
hasText: textPreview === null ? null : textPreview.trim().length > 0,
|
|
162
|
+
textExtractability,
|
|
163
|
+
unreadablePages,
|
|
164
|
+
next: suggestNext(metadata, textExtractability, unreadablePages),
|
|
165
|
+
};
|
|
166
|
+
const text = params.response_format === ResponseFormat.JSON
|
|
167
|
+
? JSON.stringify(summary, null, 2)
|
|
168
|
+
: [...formatSummaryScope(scope), '', formatSummaryMarkdown(summary)].join('\n');
|
|
169
|
+
return {
|
|
170
|
+
content: [{ type: 'text', text }],
|
|
171
|
+
};
|
|
138
172
|
});
|
|
139
173
|
}
|
|
140
174
|
//# sourceMappingURL=summarize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summarize.js","sourceRoot":"","sources":["../../../src/tools/tier1/summarize.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAuB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,YAAY,GACb,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,kBAAkB,EAClB,
|
|
1
|
+
{"version":3,"file":"summarize.js","sourceRoot":"","sources":["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EpF,OAAO;YACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,CAAC;SAC3C,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract-structured-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier2/extract-structured-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"extract-structured-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier2/extract-structured-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAe9D,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CA0IrE"}
|