@retiform/paperless-mcp 2.3.5 → 2.4.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.
Files changed (50) hide show
  1. package/README.md +301 -4
  2. package/build/api/PaperlessAPI.js +1 -6
  3. package/build/search/build-outcome.d.ts +28 -0
  4. package/build/search/build-outcome.js +31 -0
  5. package/build/search/chunk.d.ts +16 -0
  6. package/build/search/chunk.js +64 -0
  7. package/build/search/config.d.ts +120 -0
  8. package/build/search/config.js +264 -0
  9. package/build/search/embedder.d.ts +346 -0
  10. package/build/search/embedder.js +734 -0
  11. package/build/search/rank.d.ts +11 -0
  12. package/build/search/rank.js +87 -0
  13. package/build/search/store.d.ts +406 -0
  14. package/build/search/store.js +847 -0
  15. package/build/search/sync.d.ts +204 -0
  16. package/build/search/sync.js +819 -0
  17. package/build/search/webhook.d.ts +193 -0
  18. package/build/search/webhook.js +445 -0
  19. package/build/server.d.ts +29 -0
  20. package/build/server.js +77 -0
  21. package/build/tools/documents.d.ts +13 -3
  22. package/build/tools/documents.js +23 -6
  23. package/build/tools/search.d.ts +518 -0
  24. package/build/tools/search.js +1199 -0
  25. package/build/tools/utils/middlewares.d.ts +5 -0
  26. package/build/tools/utils/middlewares.js +282 -9
  27. package/package.json +10 -3
  28. package/paperless-mcp.dxt +0 -0
  29. package/build/api/documentEnhancer.test.d.ts +0 -1
  30. package/build/api/documentEnhancer.test.js +0 -80
  31. package/build/resources/documents.test.d.ts +0 -1
  32. package/build/resources/documents.test.js +0 -124
  33. package/build/server.test.d.ts +0 -1
  34. package/build/server.test.js +0 -54
  35. package/build/tools/boundedContent.test.d.ts +0 -1
  36. package/build/tools/boundedContent.test.js +0 -565
  37. package/build/tools/documents.test.d.ts +0 -1
  38. package/build/tools/documents.test.js +0 -658
  39. package/build/tools/mail.test.d.ts +0 -1
  40. package/build/tools/mail.test.js +0 -212
  41. package/build/tools/notes.test.d.ts +0 -1
  42. package/build/tools/notes.test.js +0 -177
  43. package/build/tools/schemaCompat.test.d.ts +0 -1
  44. package/build/tools/schemaCompat.test.js +0 -59
  45. package/build/tools/utils/monetary.test.d.ts +0 -1
  46. package/build/tools/utils/monetary.test.js +0 -25
  47. package/build/tools/utils/resourceUri.test.d.ts +0 -1
  48. package/build/tools/utils/resourceUri.test.js +0 -50
  49. package/build/tools/utils/selectFields.test.d.ts +0 -1
  50. package/build/tools/utils/selectFields.test.js +0 -164
@@ -1,565 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const strict_1 = __importDefault(require("node:assert/strict"));
16
- const node_test_1 = require("node:test");
17
- const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
18
- const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
19
- const documents_1 = require("./documents");
20
- const paperlessApi_1 = require("../test/mocks/paperlessApi");
21
- function parseToolText(result) {
22
- var _a;
23
- const item = (_a = result.content) === null || _a === void 0 ? void 0 : _a[0];
24
- if (!item || item.type !== "text") {
25
- throw new Error("Expected text tool response");
26
- }
27
- return JSON.parse(item.text);
28
- }
29
- class TestTransport {
30
- start() {
31
- return __awaiter(this, void 0, void 0, function* () { });
32
- }
33
- send(message) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- var _a, _b;
36
- (_b = (_a = this.peer) === null || _a === void 0 ? void 0 : _a.onmessage) === null || _b === void 0 ? void 0 : _b.call(_a, message);
37
- });
38
- }
39
- close() {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- var _a;
42
- (_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
43
- });
44
- }
45
- }
46
- function createTransportPair() {
47
- const clientTransport = new TestTransport();
48
- const serverTransport = new TestTransport();
49
- clientTransport.peer = serverTransport;
50
- serverTransport.peer = clientTransport;
51
- return { clientTransport, serverTransport };
52
- }
53
- function createSearchApi(document, extraResults = []) {
54
- const results = [document, ...extraResults];
55
- const recording = {
56
- getDocumentCalls: [],
57
- getDocumentsQueries: [],
58
- document,
59
- documentsResponse: {
60
- count: results.length,
61
- next: null,
62
- previous: null,
63
- all: results.map((d) => d.id),
64
- results,
65
- },
66
- };
67
- const api = {
68
- getDocument: (id) => __awaiter(this, void 0, void 0, function* () {
69
- recording.getDocumentCalls.push(id);
70
- return document;
71
- }),
72
- getDocuments: (query) => __awaiter(this, void 0, void 0, function* () {
73
- recording.getDocumentsQueries.push(query);
74
- return recording.documentsResponse;
75
- }),
76
- getCorrespondents: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
77
- getDocumentTypes: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
78
- getTags: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
79
- getCustomFields: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
80
- };
81
- return Object.assign(Object.assign({}, recording), { api });
82
- }
83
- function withDocumentClient(api, run) {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- const server = new mcp_js_1.McpServer({ name: "paperless-bounded-test", version: "1.0.0" });
86
- (0, documents_1.registerDocumentTools)(server, api);
87
- const client = new index_js_1.Client({ name: "paperless-bounded-test-client", version: "1.0.0" });
88
- const { clientTransport, serverTransport } = createTransportPair();
89
- yield server.connect(serverTransport);
90
- yield client.connect(clientTransport);
91
- try {
92
- yield run(client);
93
- }
94
- finally {
95
- yield client.close();
96
- yield server.close();
97
- }
98
- });
99
- }
100
- (0, node_test_1.describe)("resolveBoundedRead", () => {
101
- (0, node_test_1.test)("defaults to the bounded head window", () => {
102
- const read = (0, documents_1.resolveBoundedRead)(undefined, undefined, 100000);
103
- strict_1.default.deepEqual(read, { offset: 0, maxChars: documents_1.DEFAULT_CONTENT_MAX_CHARS });
104
- });
105
- (0, node_test_1.test)("keeps caller-requested windows inside the budget", () => {
106
- const read = (0, documents_1.resolveBoundedRead)(500, 1200, 100000);
107
- strict_1.default.deepEqual(read, { offset: 500, maxChars: 1200 });
108
- });
109
- (0, node_test_1.test)("rejects offsets beyond the end of the document", () => {
110
- strict_1.default.throws(() => (0, documents_1.resolveBoundedRead)(50, 10, 40), /beyond the end of the document/);
111
- });
112
- (0, node_test_1.test)("allows an offset of zero on an empty document", () => {
113
- const read = (0, documents_1.resolveBoundedRead)(0, undefined, 0);
114
- strict_1.default.deepEqual(read, { offset: 0, maxChars: documents_1.DEFAULT_CONTENT_MAX_CHARS });
115
- });
116
- (0, node_test_1.test)("fails loudly when max_chars exceeds the inline budget", () => {
117
- strict_1.default.throws(() => (0, documents_1.resolveBoundedRead)(0, documents_1.DEFAULT_MAX_INLINE_CHARS + 1, 100000, documents_1.DEFAULT_MAX_INLINE_CHARS), /exceeds the inline budget/);
118
- });
119
- (0, node_test_1.test)("names the env var and the download resource in the budget error", () => {
120
- try {
121
- (0, documents_1.resolveBoundedRead)(0, documents_1.DEFAULT_MAX_INLINE_CHARS + 1, 100000, documents_1.DEFAULT_MAX_INLINE_CHARS);
122
- strict_1.default.fail("expected the budget error");
123
- }
124
- catch (error) {
125
- const message = error.message;
126
- strict_1.default.match(message, /PAPERLESS_MCP_MAX_INLINE_CHARS/);
127
- strict_1.default.match(message, /download_document/);
128
- strict_1.default.match(message, /offset/);
129
- }
130
- });
131
- });
132
- (0, node_test_1.describe)("getMaxInlineChars", () => {
133
- const KEY = "PAPERLESS_MCP_MAX_INLINE_CHARS";
134
- (0, node_test_1.before)(() => {
135
- delete process.env[KEY];
136
- });
137
- (0, node_test_1.after)(() => {
138
- delete process.env[KEY];
139
- });
140
- (0, node_test_1.test)("defaults to 50000 without configuration", () => {
141
- strict_1.default.equal((0, documents_1.getMaxInlineChars)(), documents_1.DEFAULT_MAX_INLINE_CHARS);
142
- });
143
- (0, node_test_1.test)("reads the environment at call time", () => {
144
- process.env[KEY] = "12345";
145
- strict_1.default.equal((0, documents_1.getMaxInlineChars)(), 12345);
146
- delete process.env[KEY];
147
- strict_1.default.equal((0, documents_1.getMaxInlineChars)(), documents_1.DEFAULT_MAX_INLINE_CHARS);
148
- });
149
- (0, node_test_1.test)("ignores invalid values and falls back to the default", () => {
150
- process.env[KEY] = "not-a-number";
151
- strict_1.default.equal((0, documents_1.getMaxInlineChars)(), documents_1.DEFAULT_MAX_INLINE_CHARS);
152
- process.env[KEY] = "0";
153
- strict_1.default.equal((0, documents_1.getMaxInlineChars)(), documents_1.DEFAULT_MAX_INLINE_CHARS);
154
- process.env[KEY] = "-5";
155
- strict_1.default.equal((0, documents_1.getMaxInlineChars)(), documents_1.DEFAULT_MAX_INLINE_CHARS);
156
- delete process.env[KEY];
157
- });
158
- });
159
- (0, node_test_1.describe)("sliceDocumentContent", () => {
160
- (0, node_test_1.test)("returns the requested window with honest pagination metadata", () => {
161
- const doc = (0, paperlessApi_1.createDocument)({ content: "x".repeat(300) });
162
- const sliced = (0, documents_1.sliceDocumentContent)(doc, 100, 50);
163
- strict_1.default.equal(sliced.content.length, 50);
164
- strict_1.default.equal(sliced.offset, 100);
165
- strict_1.default.equal(sliced.content_length, 300);
166
- strict_1.default.equal(sliced.truncated, true);
167
- strict_1.default.equal(sliced.has_more, true);
168
- strict_1.default.equal(sliced.returned_chars, 50);
169
- });
170
- (0, node_test_1.test)("marks the final window as complete", () => {
171
- const doc = (0, paperlessApi_1.createDocument)({ content: "y".repeat(100) });
172
- const sliced = (0, documents_1.sliceDocumentContent)(doc, 50, documents_1.DEFAULT_CONTENT_MAX_CHARS);
173
- strict_1.default.equal(sliced.content.length, 50);
174
- strict_1.default.equal(sliced.truncated, false);
175
- strict_1.default.equal(sliced.has_more, false);
176
- });
177
- (0, node_test_1.test)("handles documents with null content", () => {
178
- const doc = (0, paperlessApi_1.createDocument)({ content: null });
179
- const sliced = (0, documents_1.sliceDocumentContent)(doc, 0, documents_1.DEFAULT_CONTENT_MAX_CHARS);
180
- strict_1.default.equal(sliced.content, "");
181
- strict_1.default.equal(sliced.content_length, 0);
182
- strict_1.default.equal(sliced.truncated, false);
183
- });
184
- });
185
- (0, node_test_1.describe)("extractSearchTerms", () => {
186
- (0, node_test_1.test)("splits a plain query into lowercase terms", () => {
187
- strict_1.default.deepEqual((0, documents_1.extractSearchTerms)("Kuji IN"), ["kuji", "in"]);
188
- });
189
- (0, node_test_1.test)("strips query operators and punctuation", () => {
190
- strict_1.default.deepEqual((0, documents_1.extractSearchTerms)('"barber pole" AND worms'), [
191
- "barber",
192
- "pole",
193
- "and",
194
- "worms",
195
- ]);
196
- });
197
- (0, node_test_1.test)("keeps accented and non-Latin terms intact", () => {
198
- strict_1.default.deepEqual((0, documents_1.extractSearchTerms)("café"), ["café"]);
199
- strict_1.default.deepEqual((0, documents_1.extractSearchTerms)("kuji-in 九字"), ["kuji", "in", "九字"]);
200
- });
201
- (0, node_test_1.test)("drops empty fragments", () => {
202
- strict_1.default.deepEqual((0, documents_1.extractSearchTerms)(" -- ?? "), []);
203
- });
204
- });
205
- (0, node_test_1.describe)("buildSearchPassage", () => {
206
- (0, node_test_1.test)("centers the window on the first term occurrence", () => {
207
- const filler = "a".repeat(1000);
208
- const doc = (0, paperlessApi_1.createDocument)({ content: `${filler} NEEDLE ${filler}` });
209
- const passage = (0, documents_1.buildSearchPassage)(doc, ["needle"], 200);
210
- strict_1.default.equal(passage.id, doc.id);
211
- strict_1.default.ok(passage.passage.includes("NEEDLE"));
212
- // The window should be centered near the term, not starting at it.
213
- strict_1.default.ok(passage.offset < 1000);
214
- strict_1.default.ok(passage.offset > 900);
215
- strict_1.default.equal(passage.content_length, doc.content.length);
216
- });
217
- (0, node_test_1.test)("is case-insensitive when locating the term", () => {
218
- const filler = "b".repeat(500);
219
- const doc = (0, paperlessApi_1.createDocument)({ content: `${filler} mixedCaseToken ${filler}` });
220
- const passage = (0, documents_1.buildSearchPassage)(doc, ["mixedcasetoken"], 100);
221
- strict_1.default.ok(passage.passage.includes("mixedCaseToken"));
222
- });
223
- (0, node_test_1.test)("falls back to the document head when the term is absent verbatim", () => {
224
- const doc = (0, paperlessApi_1.createDocument)({ content: "z".repeat(2000) });
225
- const passage = (0, documents_1.buildSearchPassage)(doc, ["missing"], 300);
226
- strict_1.default.equal(passage.offset, 0);
227
- strict_1.default.equal(passage.passage.length, 300);
228
- });
229
- (0, node_test_1.test)("carries the search hit score and highlights when provided", () => {
230
- const doc = (0, paperlessApi_1.createDocument)({ content: "scored content here" });
231
- const passage = (0, documents_1.buildSearchPassage)(doc, ["scored"], 100, {
232
- score: 17.03,
233
- highlights: "<b>scored</b> content",
234
- });
235
- strict_1.default.equal(passage.score, 17.03);
236
- strict_1.default.equal(passage.highlights, "<b>scored</b> content");
237
- });
238
- (0, node_test_1.test)("omits score and highlights when no search hit is present", () => {
239
- const doc = (0, paperlessApi_1.createDocument)({ content: "plain content" });
240
- const passage = (0, documents_1.buildSearchPassage)(doc, ["plain"], 100);
241
- strict_1.default.ok(!("score" in passage));
242
- strict_1.default.ok(!("highlights" in passage));
243
- });
244
- });
245
- (0, node_test_1.describe)("buildSearchPassage highlight anchoring (live failure shapes)", () => {
246
- (0, node_test_1.test)("anchors on the highlighted phrase when the first query term occurs earlier in the doc", () => {
247
- // Live shape 1: common word ("best") appears at doc head; the real hit
248
- // ("minimize ejaculation") is far later, where the server highlighted it.
249
- const early = `the best documents are best read in the morning. `;
250
- const far = "z".repeat(2000);
251
- const hitText = `people concerned about it often think the best thing to do is minimize ejaculation of energy.`;
252
- const doc = (0, paperlessApi_1.createDocument)({ content: early + far + hitText + far });
253
- const passage = (0, documents_1.buildSearchPassage)(doc, ["best", "thing", "for", "them", "to", "do", "is", "minimize", "ejaculation", "of", "energy"], 800, { score: 18.5, highlights: "the <b>best</b> thing for them to do is <b>minimize</b> <b>ejaculation</b>" });
254
- // The window must contain the highlighted phrase, not merely the first
255
- // verbatim occurrence of the first term.
256
- strict_1.default.ok(passage.passage.includes("minimize ejaculation"), `window anchored at ${passage.offset} missed the highlight`);
257
- strict_1.default.ok(passage.offset >= early.length, "window should anchor at the highlight, not the first 'best'");
258
- });
259
- (0, node_test_1.test)("anchors on the highlighted phrase when stemming produced a hit absent from the raw text", () => {
260
- // Live shape 2: whoosh matched "minimal"; the doc contains "minimize" only.
261
- const doc = (0, paperlessApi_1.createDocument)({ content: `x`.repeat(1000) + `the best thing to do is minimize ejaculation today.` });
262
- const passage = (0, documents_1.buildSearchPassage)(doc, ["minimal", "ejaculation"], 400, { score: 12.0, highlights: "...the best thing to do is <b>minimize</b> <b>ejaculation</b> today..." });
263
- strict_1.default.ok(passage.passage.includes("minimize ejaculation"));
264
- // Centered window may start before the phrase; the assertion is that the
265
- // window sits at the hit (deep in the doc), not at the head.
266
- strict_1.default.ok(passage.offset >= 800, `offset ${passage.offset}`);
267
- });
268
- (0, node_test_1.test)("decodes entities and tolerates markup variants in highlights", () => {
269
- const doc = (0, paperlessApi_1.createDocument)({ content: `y`.repeat(900) + `fish and chips &amp; gravy nightly` });
270
- const passage = (0, documents_1.buildSearchPassage)(doc, ["fish"], 300, {
271
- score: 3,
272
- highlights: "<mark>fish</mark> and chips &amp; gravy",
273
- });
274
- strict_1.default.ok(passage.passage.includes("fish and chips"), `offset ${passage.offset}`);
275
- });
276
- (0, node_test_1.test)("decodes numeric character references so the longest anchor is not skipped", () => {
277
- // Live shape 3 (2026-11-05, doc 5 of the Kongokai series): the server
278
- // returns &#x27; for an apostrophe in its highlight HTML. The decoder
279
- // handled &#39; and &apos; but not the numeric/hex forms, so the LONGEST
280
- // candidate phrase carried a literal entity that never occurs in the
281
- // stored text. indexOf returned -1 and the search fell through to a SHORT
282
- // phrase that also occurs near the document head, so the reported offset
283
- // pointed at a region containing none of the hit.
284
- const early = "[SPEAKER_01] This is unrelated chatter near the head. ";
285
- const filler = "z".repeat(1500);
286
- const hit = "This is pre-living, pre-living, pre-manifesting, pre-living the goal. [SPEAKER_01] I" +
287
- String.fromCharCode(39) +
288
- "m getting familiar with it.";
289
- const doc = (0, paperlessApi_1.createDocument)({ content: early + filler + hit });
290
- const passage = (0, documents_1.buildSearchPassage)(doc, ["pre", "manifesting"], 600, {
291
- score: 9.71,
292
- highlights: "This is <b>pre</b>-living, <b>pre</b>-living, <b>pre</b>-<b>manifesting</b>, <b>pre</b>-living the goal. [SPEAKER_01] I&#x27;m getting familiar with it.",
293
- });
294
- strict_1.default.ok(passage.passage.includes("pre-manifesting"), "window anchored at " + passage.offset + " missed the hit");
295
- strict_1.default.ok(passage.offset >= early.length, "should anchor on the decoded long phrase, not a short phrase near the head");
296
- });
297
- (0, node_test_1.test)("skips very short highlight matches that cannot anchor", () => {
298
- const doc = (0, paperlessApi_1.createDocument)({ content: `head a b here ${"w".repeat(800)} a rare anchoring phrase tail` });
299
- const passage = (0, documents_1.buildSearchPassage)(doc, ["a", "rare"], 300, {
300
- score: 5,
301
- highlights: "a <b>rare</b> <b>a</b> phrase",
302
- });
303
- strict_1.default.ok(passage.passage.includes("rare anchoring phrase"), `offset ${passage.offset}`);
304
- });
305
- (0, node_test_1.test)("falls back to verbatim term anchoring when the highlight text is absent", () => {
306
- const doc = (0, paperlessApi_1.createDocument)({ content: `${"k".repeat(600)} findme soon ${"k".repeat(300)}` });
307
- const passage = (0, documents_1.buildSearchPassage)(doc, ["findme"], 200, {
308
- score: 2,
309
- highlights: "<b>completely different text</b>",
310
- });
311
- strict_1.default.ok(passage.passage.includes("findme"));
312
- });
313
- (0, node_test_1.test)("falls back to the document head when there is no hit and nothing matches", () => {
314
- const doc = (0, paperlessApi_1.createDocument)({ content: "z".repeat(2000) });
315
- const passage = (0, documents_1.buildSearchPassage)(doc, ["missing"], 300, {
316
- score: 1,
317
- highlights: "<b>unfindable</b>",
318
- });
319
- strict_1.default.equal(passage.offset, 0);
320
- });
321
- });
322
- (0, node_test_1.describe)("sanitizeFullTextQuery", () => {
323
- (0, node_test_1.test)("strips apostrophes (django-watson 400 source)", () => {
324
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)("God's favorite prayer"), "Gods favorite prayer");
325
- });
326
- (0, node_test_1.test)("drops all quotes when the count is unbalanced", () => {
327
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)('say "hello world'), "say hello world");
328
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)('odd "quote and "two" here'), "odd quote and two here");
329
- });
330
- (0, node_test_1.test)("leaves balanced quotes and clean queries untouched", () => {
331
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)('"barber pole" worms'), '"barber pole" worms');
332
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)("plain terms"), "plain terms");
333
- });
334
- (0, node_test_1.test)("keeps a searchable stem after apostrophe removal", () => {
335
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)("it's"), "its");
336
- });
337
- (0, node_test_1.test)("returns null when nothing searchable remains", () => {
338
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)(' '), null);
339
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)('"'), null);
340
- });
341
- (0, node_test_1.test)("collapses whitespace left by removals", () => {
342
- strict_1.default.equal((0, documents_1.sanitizeFullTextQuery)("what's new"), "whats new");
343
- });
344
- });
345
- (0, node_test_1.describe)("runDocumentSearch (400 resilience)", () => {
346
- function createFailingSearchApi(failOn, succeedWith, log) {
347
- return {
348
- getDocuments: (query) => __awaiter(this, void 0, void 0, function* () {
349
- log.queries.push(query);
350
- if (query.includes(failOn)) {
351
- const err = new Error("Request failed with status code 400");
352
- err.isAxiosError = true;
353
- err.response = { status: 400, data: { detail: "Error listing search results, check logs for more detail." } };
354
- throw err;
355
- }
356
- return succeedWith;
357
- }),
358
- getCorrespondents: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
359
- getDocumentTypes: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
360
- getTags: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
361
- getCustomFields: () => __awaiter(this, void 0, void 0, function* () { return ({ results: [] }); }),
362
- };
363
- }
364
- (0, node_test_1.test)("retries a sanitized query when the raw query 400s", () => __awaiter(void 0, void 0, void 0, function* () {
365
- const log = { queries: [] };
366
- const hit = (0, paperlessApi_1.createDocument)({ content: `answer here ${"f".repeat(400)}` });
367
- const api = createFailingSearchApi("%27", { count: 1, all: [hit.id], results: [hit] }, log);
368
- const out = yield (0, documents_1.runDocumentSearch)(api, "God's favorite prayer");
369
- strict_1.default.equal(out.querySanitized, true);
370
- strict_1.default.equal(out.response.results.length, 1);
371
- strict_1.default.ok(log.queries[0].includes("God%27s"), "first attempt must be the raw query");
372
- strict_1.default.ok(!log.queries[1].includes("'"), "retry must be sanitized");
373
- }));
374
- (0, node_test_1.test)("returns query_sanitized false when the raw query succeeds", () => __awaiter(void 0, void 0, void 0, function* () {
375
- const log = { queries: [] };
376
- const hit = (0, paperlessApi_1.createDocument)({ content: "plain content" });
377
- const api = createFailingSearchApi("NEVER-MATCHES", { count: 1, all: [hit.id], results: [hit] }, log);
378
- const out = yield (0, documents_1.runDocumentSearch)(api, "plain terms");
379
- strict_1.default.equal(out.querySanitized, false);
380
- strict_1.default.equal(log.queries.length, 1);
381
- }));
382
- (0, node_test_1.test)("surfaces a structured error when the sanitized retry also fails", () => __awaiter(void 0, void 0, void 0, function* () {
383
- const log = { queries: [] };
384
- const api = createFailingSearchApi("q", { count: 0, all: [], results: [] }, log);
385
- yield strict_1.default.rejects(() => (0, documents_1.runDocumentSearch)(api, "totally broken q'tery"), /400/);
386
- strict_1.default.equal(log.queries.length, 2, "expected raw attempt + one sanitized retry");
387
- }));
388
- (0, node_test_1.test)("does not retry when sanitization has nothing to fix", () => __awaiter(void 0, void 0, void 0, function* () {
389
- const log = { queries: [] };
390
- const api = createFailingSearchApi("q", { count: 0, all: [], results: [] }, log);
391
- yield strict_1.default.rejects(() => (0, documents_1.runDocumentSearch)(api, "clean query"), /400/);
392
- strict_1.default.equal(log.queries.length, 1, "no retry when the query was already clean");
393
- }));
394
- });
395
- (0, node_test_1.describe)("get_document_content bounded reads", () => {
396
- (0, node_test_1.test)("returns a bounded window instead of the whole document", () => __awaiter(void 0, void 0, void 0, function* () {
397
- const longContent = "c".repeat(100000);
398
- const { api, getDocumentCalls } = createSearchApi((0, paperlessApi_1.createDocument)({ id: 9, content: longContent }));
399
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
400
- var _a;
401
- const result = (yield client.callTool({
402
- name: "get_document_content",
403
- arguments: { id: 9 },
404
- }));
405
- strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
406
- const parsed = parseToolText(result);
407
- strict_1.default.equal(parsed.content.length, documents_1.DEFAULT_CONTENT_MAX_CHARS);
408
- strict_1.default.equal(parsed.content_length, 100000);
409
- strict_1.default.equal(parsed.truncated, true);
410
- strict_1.default.equal(parsed.has_more, true);
411
- strict_1.default.equal(parsed.offset, 0);
412
- }));
413
- strict_1.default.deepEqual(getDocumentCalls, [9]);
414
- }));
415
- (0, node_test_1.test)("honors offset and max_chars", () => __awaiter(void 0, void 0, void 0, function* () {
416
- const content = "0123456789".repeat(1000);
417
- const { api } = createSearchApi((0, paperlessApi_1.createDocument)({ id: 3, content }));
418
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
419
- const result = (yield client.callTool({
420
- name: "get_document_content",
421
- arguments: { id: 3, offset: 100, max_chars: 10 },
422
- }));
423
- const parsed = parseToolText(result);
424
- strict_1.default.equal(parsed.content, content.slice(100, 110));
425
- strict_1.default.equal(parsed.offset, 100);
426
- strict_1.default.equal(parsed.returned_chars, 10);
427
- }));
428
- }));
429
- (0, node_test_1.test)("rejects a max_chars above the budget with a size-aware error", () => __awaiter(void 0, void 0, void 0, function* () {
430
- const { api } = createSearchApi((0, paperlessApi_1.createDocument)({ id: 4, content: "d".repeat(100000) }));
431
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
432
- var _a;
433
- const result = (yield client.callTool({
434
- name: "get_document_content",
435
- arguments: { id: 4, max_chars: documents_1.DEFAULT_MAX_INLINE_CHARS + 1 },
436
- }));
437
- strict_1.default.ok(result.isError, "expected the budget error");
438
- const text = (_a = result.content) === null || _a === void 0 ? void 0 : _a[0];
439
- strict_1.default.ok(text && text.type === "text");
440
- const message = text.text;
441
- strict_1.default.match(message, /exceeds the inline budget/);
442
- strict_1.default.match(message, /PAPERLESS_MCP_MAX_INLINE_CHARS/);
443
- }));
444
- }));
445
- });
446
- (0, node_test_1.describe)("search_passages", () => {
447
- (0, node_test_1.test)("returns bounded passages per hit, never full content", () => __awaiter(void 0, void 0, void 0, function* () {
448
- const hit = (0, paperlessApi_1.createDocument)({
449
- id: 1,
450
- title: "Hayes",
451
- content: `${"q".repeat(2000)} kuji training ${"q".repeat(2000)}`,
452
- __search_hit__: { score: 9.5, highlights: "<b>kuji</b>", note_highlights: "", rank: 1 },
453
- });
454
- const other = (0, paperlessApi_1.createDocument)({
455
- id: 2,
456
- title: "Other",
457
- content: `${"w".repeat(2000)} kuji reference ${"w".repeat(2000)}`,
458
- __search_hit__: { score: 4.5, highlights: "<b>kuji</b> ref", note_highlights: "", rank: 2 },
459
- });
460
- const { api, getDocumentsQueries } = createSearchApi(hit, [other]);
461
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
462
- var _a;
463
- const result = (yield client.callTool({
464
- name: "search_passages",
465
- arguments: { query: "kuji" },
466
- }));
467
- strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
468
- const parsed = parseToolText(result);
469
- strict_1.default.equal(parsed.query, "kuji");
470
- strict_1.default.equal(parsed.total_hits, 2);
471
- strict_1.default.equal(parsed.passages.length, 2);
472
- for (const passage of parsed.passages) {
473
- strict_1.default.ok(passage.passage.length <= 800);
474
- strict_1.default.ok(typeof passage.content_length === "number");
475
- strict_1.default.ok(typeof passage.offset === "number");
476
- }
477
- const first = parsed.passages[0];
478
- strict_1.default.ok(first.passage.includes("kuji"));
479
- strict_1.default.equal(first.score, 9.5);
480
- strict_1.default.equal(first.highlights, "<b>kuji</b>");
481
- }));
482
- strict_1.default.equal(getDocumentsQueries.length, 1);
483
- strict_1.default.match(getDocumentsQueries[0], /query=kuji/);
484
- }));
485
- (0, node_test_1.test)("caps passages at max_passages and the total hit count", () => __awaiter(void 0, void 0, void 0, function* () {
486
- const hit = (0, paperlessApi_1.createDocument)({
487
- content: `${"e".repeat(500)} target text ${"e".repeat(500)}`,
488
- __search_hit__: { score: 1, highlights: "", note_highlights: "", rank: 1 },
489
- });
490
- const extras = [2, 3, 4, 5, 6, 7, 8].map((id) => (0, paperlessApi_1.createDocument)({
491
- id,
492
- content: `filler ${id}`,
493
- __search_hit__: { score: id, highlights: "", note_highlights: "", rank: id },
494
- }));
495
- const { api } = createSearchApi(hit, extras);
496
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
497
- const result = (yield client.callTool({
498
- name: "search_passages",
499
- arguments: { query: "target", max_passages: 3 },
500
- }));
501
- const parsed = parseToolText(result);
502
- strict_1.default.equal(parsed.total_hits, 8);
503
- strict_1.default.equal(parsed.passages.length, 3);
504
- }));
505
- }));
506
- (0, node_test_1.test)("caps the fetch at max_passages unless page_size is explicit", () => __awaiter(void 0, void 0, void 0, function* () {
507
- const hit = (0, paperlessApi_1.createDocument)({
508
- content: `${"f".repeat(300)} findme ${"f".repeat(300)}`,
509
- __search_hit__: { score: 1, highlights: "", note_highlights: "", rank: 1 },
510
- });
511
- const { api, getDocumentsQueries } = createSearchApi(hit);
512
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
513
- var _a;
514
- const result = (yield client.callTool({
515
- name: "search_passages",
516
- arguments: { query: "findme", max_passages: 2 },
517
- }));
518
- strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
519
- strict_1.default.match(getDocumentsQueries[0], /page_size=2/);
520
- }));
521
- }));
522
- (0, node_test_1.test)("an explicit page_size overrides the passage cap", () => __awaiter(void 0, void 0, void 0, function* () {
523
- const hit = (0, paperlessApi_1.createDocument)({
524
- content: `${"g".repeat(300)} findme ${"g".repeat(300)}`,
525
- __search_hit__: { score: 1, highlights: "", note_highlights: "", rank: 1 },
526
- });
527
- const { api, getDocumentsQueries } = createSearchApi(hit);
528
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
529
- var _a;
530
- const result = (yield client.callTool({
531
- name: "search_passages",
532
- arguments: { query: "findme", page_size: 10 },
533
- }));
534
- strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
535
- strict_1.default.match(getDocumentsQueries[0], /page_size=10/);
536
- }));
537
- }));
538
- (0, node_test_1.test)("rejects a window outside the supported range", () => __awaiter(void 0, void 0, void 0, function* () {
539
- const { api } = createSearchApi((0, paperlessApi_1.createDocument)({ content: "x" }));
540
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
541
- const result = (yield client.callTool({
542
- name: "search_passages",
543
- arguments: { query: "x", window: 50 },
544
- }));
545
- strict_1.default.ok(result.isError, "expected the window validation error");
546
- }));
547
- }));
548
- });
549
- (0, node_test_1.describe)("enhanced documents expose content_length", () => {
550
- (0, node_test_1.test)("list/query results carry content_length without content", () => __awaiter(void 0, void 0, void 0, function* () {
551
- const doc = (0, paperlessApi_1.createDocument)({ id: 11, content: "m".repeat(1234) });
552
- const { api } = createSearchApi(doc);
553
- yield withDocumentClient(api, (client) => __awaiter(void 0, void 0, void 0, function* () {
554
- var _a;
555
- const result = (yield client.callTool({
556
- name: "query_documents",
557
- arguments: { query: "anything" },
558
- }));
559
- strict_1.default.ok(!result.isError, (_a = parseToolText(result)) === null || _a === void 0 ? void 0 : _a.error);
560
- const parsed = parseToolText(result);
561
- strict_1.default.ok(!("content" in parsed.results[0]));
562
- strict_1.default.equal(parsed.results[0].content_length, 1234);
563
- }));
564
- }));
565
- });
@@ -1 +0,0 @@
1
- export {};