@echofiles/echo-pdf 0.4.3 → 0.6.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 (55) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +85 -562
  3. package/bin/echo-pdf.js +130 -525
  4. package/dist/file-utils.d.ts +0 -3
  5. package/dist/file-utils.js +0 -18
  6. package/dist/local/document.d.ts +10 -0
  7. package/dist/local/document.js +133 -0
  8. package/dist/local/index.d.ts +3 -135
  9. package/dist/local/index.js +2 -555
  10. package/dist/local/semantic.d.ts +2 -0
  11. package/dist/local/semantic.js +231 -0
  12. package/dist/local/shared.d.ts +50 -0
  13. package/dist/local/shared.js +173 -0
  14. package/dist/local/types.d.ts +183 -0
  15. package/dist/local/types.js +2 -0
  16. package/dist/node/pdfium-local.js +30 -6
  17. package/dist/pdf-config.js +2 -65
  18. package/dist/pdf-types.d.ts +1 -58
  19. package/dist/types.d.ts +1 -87
  20. package/echo-pdf.config.json +1 -21
  21. package/package.json +25 -22
  22. package/bin/lib/http.js +0 -97
  23. package/bin/lib/mcp-stdio.js +0 -99
  24. package/dist/auth.d.ts +0 -18
  25. package/dist/auth.js +0 -36
  26. package/dist/core/index.d.ts +0 -50
  27. package/dist/core/index.js +0 -7
  28. package/dist/file-ops.d.ts +0 -11
  29. package/dist/file-ops.js +0 -36
  30. package/dist/file-store-do.d.ts +0 -36
  31. package/dist/file-store-do.js +0 -298
  32. package/dist/http-error.d.ts +0 -9
  33. package/dist/http-error.js +0 -14
  34. package/dist/index.d.ts +0 -1
  35. package/dist/index.js +0 -1
  36. package/dist/mcp-server.d.ts +0 -3
  37. package/dist/mcp-server.js +0 -124
  38. package/dist/node/semantic-local.d.ts +0 -16
  39. package/dist/node/semantic-local.js +0 -113
  40. package/dist/pdf-agent.d.ts +0 -18
  41. package/dist/pdf-agent.js +0 -217
  42. package/dist/pdf-storage.d.ts +0 -8
  43. package/dist/pdf-storage.js +0 -86
  44. package/dist/pdfium-engine.d.ts +0 -9
  45. package/dist/pdfium-engine.js +0 -180
  46. package/dist/r2-file-store.d.ts +0 -20
  47. package/dist/r2-file-store.js +0 -176
  48. package/dist/response-schema.d.ts +0 -15
  49. package/dist/response-schema.js +0 -159
  50. package/dist/tool-registry.d.ts +0 -16
  51. package/dist/tool-registry.js +0 -175
  52. package/dist/worker.d.ts +0 -7
  53. package/dist/worker.js +0 -386
  54. package/scripts/export-fixtures.sh +0 -204
  55. package/wrangler.toml +0 -19
package/dist/auth.js DELETED
@@ -1,36 +0,0 @@
1
- export const checkHeaderAuth = (request, env, options) => {
2
- const authHeader = typeof options.authHeader === "string" ? options.authHeader.trim() : "";
3
- const authEnv = typeof options.authEnv === "string" ? options.authEnv.trim() : "";
4
- const hasHeader = authHeader.length > 0;
5
- const hasEnv = authEnv.length > 0;
6
- if (!hasHeader && !hasEnv)
7
- return { ok: true };
8
- if (!hasHeader || !hasEnv) {
9
- return {
10
- ok: false,
11
- status: 500,
12
- code: options.misconfiguredCode,
13
- message: `${options.contextName} auth must configure both authHeader and authEnv`,
14
- };
15
- }
16
- const required = env[authEnv];
17
- if (typeof required !== "string" || required.length === 0) {
18
- if (options.allowMissingSecret === true)
19
- return { ok: true };
20
- return {
21
- ok: false,
22
- status: 500,
23
- code: options.misconfiguredCode,
24
- message: `${options.contextName} auth is configured but env "${authEnv}" is missing`,
25
- };
26
- }
27
- if (request.headers.get(authHeader) !== required) {
28
- return {
29
- ok: false,
30
- status: 401,
31
- code: options.unauthorizedCode,
32
- message: "Unauthorized",
33
- };
34
- }
35
- return { ok: true };
36
- };
@@ -1,50 +0,0 @@
1
- export { callTool, listToolSchemas } from "../tool-registry.js";
2
- export type { ToolRuntimeContext } from "../tool-registry.js";
3
- export type { ToolSchema } from "../pdf-types.js";
4
- export type { Env, FileStore, JsonObject } from "../types.js";
5
- import type { ReturnMode } from "../types.js";
6
- export interface PdfExtractPagesArgs {
7
- readonly fileId?: string;
8
- readonly url?: string;
9
- readonly base64?: string;
10
- readonly filename?: string;
11
- readonly pages: ReadonlyArray<number>;
12
- readonly renderScale?: number;
13
- readonly returnMode?: ReturnMode;
14
- }
15
- export interface PdfOcrPagesArgs {
16
- readonly fileId?: string;
17
- readonly url?: string;
18
- readonly base64?: string;
19
- readonly filename?: string;
20
- readonly pages: ReadonlyArray<number>;
21
- readonly renderScale?: number;
22
- readonly provider?: string;
23
- readonly model?: string;
24
- readonly prompt?: string;
25
- }
26
- export interface PdfTablesToLatexArgs {
27
- readonly fileId?: string;
28
- readonly url?: string;
29
- readonly base64?: string;
30
- readonly filename?: string;
31
- readonly pages: ReadonlyArray<number>;
32
- readonly renderScale?: number;
33
- readonly provider?: string;
34
- readonly model?: string;
35
- readonly prompt?: string;
36
- }
37
- export interface FileOpsArgs {
38
- readonly op: "list" | "read" | "delete" | "put";
39
- readonly fileId?: string;
40
- readonly includeBase64?: boolean;
41
- readonly text?: string;
42
- readonly filename?: string;
43
- readonly mimeType?: string;
44
- readonly base64?: string;
45
- readonly returnMode?: ReturnMode;
46
- }
47
- export declare const pdf_extract_pages: (args: PdfExtractPagesArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
48
- export declare const pdf_ocr_pages: (args: PdfOcrPagesArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
49
- export declare const pdf_tables_to_latex: (args: PdfTablesToLatexArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
50
- export declare const file_ops: (args: FileOpsArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
@@ -1,7 +0,0 @@
1
- export { callTool, listToolSchemas } from "../tool-registry.js";
2
- import { callTool } from "../tool-registry.js";
3
- const asJsonObject = (value) => value;
4
- export const pdf_extract_pages = async (args, ctx) => callTool("pdf_extract_pages", asJsonObject(args), ctx);
5
- export const pdf_ocr_pages = async (args, ctx) => callTool("pdf_ocr_pages", asJsonObject(args), ctx);
6
- export const pdf_tables_to_latex = async (args, ctx) => callTool("pdf_tables_to_latex", asJsonObject(args), ctx);
7
- export const file_ops = async (args, ctx) => callTool("file_ops", asJsonObject(args), ctx);
@@ -1,11 +0,0 @@
1
- import type { FileStore, ReturnMode } from "./types.js";
2
- export declare const runFileOp: (fileStore: FileStore, input: {
3
- readonly op: "list" | "read" | "delete" | "put";
4
- readonly fileId?: string;
5
- readonly includeBase64?: boolean;
6
- readonly text?: string;
7
- readonly filename?: string;
8
- readonly mimeType?: string;
9
- readonly base64?: string;
10
- readonly returnMode?: ReturnMode;
11
- }) => Promise<unknown>;
package/dist/file-ops.js DELETED
@@ -1,36 +0,0 @@
1
- import { fromBase64, normalizeReturnMode, toInlineFilePayload } from "./file-utils.js";
2
- export const runFileOp = async (fileStore, input) => {
3
- if (input.op === "list") {
4
- return { files: await fileStore.list() };
5
- }
6
- if (input.op === "put") {
7
- const bytes = input.base64 ? fromBase64(input.base64) : new TextEncoder().encode(input.text ?? "");
8
- const meta = await fileStore.put({
9
- filename: input.filename ?? `file-${Date.now()}.txt`,
10
- mimeType: input.mimeType ?? "text/plain; charset=utf-8",
11
- bytes,
12
- });
13
- const returnMode = normalizeReturnMode(input.returnMode);
14
- if (returnMode === "file_id")
15
- return { returnMode, file: meta };
16
- if (returnMode === "url")
17
- return { returnMode, file: meta, url: `/api/files/get?fileId=${encodeURIComponent(meta.id)}` };
18
- const stored = await fileStore.get(meta.id);
19
- if (!stored)
20
- throw new Error(`File not found after put: ${meta.id}`);
21
- return {
22
- returnMode,
23
- ...toInlineFilePayload(stored, true),
24
- };
25
- }
26
- if (!input.fileId) {
27
- throw new Error("fileId is required");
28
- }
29
- if (input.op === "delete") {
30
- return { deleted: await fileStore.delete(input.fileId), fileId: input.fileId };
31
- }
32
- const file = await fileStore.get(input.fileId);
33
- if (!file)
34
- throw new Error(`File not found: ${input.fileId}`);
35
- return toInlineFilePayload(file, Boolean(input.includeBase64));
36
- };
@@ -1,36 +0,0 @@
1
- import type { StoragePolicy } from "./pdf-types.js";
2
- import type { DurableObjectNamespace, DurableObjectState, StoredFileMeta, StoredFileRecord } from "./types.js";
3
- interface StoreStats {
4
- readonly fileCount: number;
5
- readonly totalBytes: number;
6
- }
7
- export declare class FileStoreDO {
8
- private readonly state;
9
- constructor(state: DurableObjectState);
10
- fetch(request: Request): Promise<Response>;
11
- }
12
- export declare class DurableObjectFileStore {
13
- private readonly namespace;
14
- private readonly policy;
15
- constructor(namespace: DurableObjectNamespace, policy: StoragePolicy);
16
- private stub;
17
- put(input: {
18
- readonly filename: string;
19
- readonly mimeType: string;
20
- readonly bytes: Uint8Array;
21
- }): Promise<StoredFileMeta>;
22
- get(fileId: string): Promise<StoredFileRecord | null>;
23
- list(): Promise<ReadonlyArray<StoredFileMeta>>;
24
- delete(fileId: string): Promise<boolean>;
25
- stats(): Promise<{
26
- policy: StoragePolicy;
27
- stats: StoreStats;
28
- }>;
29
- cleanup(): Promise<{
30
- policy: StoragePolicy;
31
- deletedExpired: number;
32
- deletedEvicted: number;
33
- stats: StoreStats;
34
- }>;
35
- }
36
- export {};
@@ -1,298 +0,0 @@
1
- import { fromBase64, toBase64 } from "./file-utils.js";
2
- const json = (data, status = 200) => new Response(JSON.stringify(data), {
3
- status,
4
- headers: { "Content-Type": "application/json; charset=utf-8" },
5
- });
6
- const readJson = async (request) => {
7
- try {
8
- const body = await request.json();
9
- if (typeof body === "object" && body !== null && !Array.isArray(body)) {
10
- return body;
11
- }
12
- return {};
13
- }
14
- catch {
15
- return {};
16
- }
17
- };
18
- const defaultPolicy = () => ({
19
- maxFileBytes: 1_200_000,
20
- maxTotalBytes: 52_428_800,
21
- ttlHours: 24,
22
- cleanupBatchSize: 50,
23
- });
24
- const parsePolicy = (input) => {
25
- const raw = typeof input === "object" && input !== null && !Array.isArray(input)
26
- ? input
27
- : {};
28
- const fallback = defaultPolicy();
29
- const maxFileBytes = Number(raw.maxFileBytes ?? fallback.maxFileBytes);
30
- const maxTotalBytes = Number(raw.maxTotalBytes ?? fallback.maxTotalBytes);
31
- const ttlHours = Number(raw.ttlHours ?? fallback.ttlHours);
32
- const cleanupBatchSize = Number(raw.cleanupBatchSize ?? fallback.cleanupBatchSize);
33
- return {
34
- maxFileBytes: Number.isFinite(maxFileBytes) && maxFileBytes > 0 ? Math.floor(maxFileBytes) : fallback.maxFileBytes,
35
- maxTotalBytes: Number.isFinite(maxTotalBytes) && maxTotalBytes > 0 ? Math.floor(maxTotalBytes) : fallback.maxTotalBytes,
36
- ttlHours: Number.isFinite(ttlHours) && ttlHours > 0 ? ttlHours : fallback.ttlHours,
37
- cleanupBatchSize: Number.isFinite(cleanupBatchSize) && cleanupBatchSize > 0 ? Math.floor(cleanupBatchSize) : fallback.cleanupBatchSize,
38
- };
39
- };
40
- const toMeta = (value) => ({
41
- id: value.id,
42
- filename: value.filename,
43
- mimeType: value.mimeType,
44
- sizeBytes: value.sizeBytes,
45
- createdAt: value.createdAt,
46
- });
47
- const listStoredValues = async (state) => {
48
- const listed = await state.storage.list({ prefix: "file:" });
49
- return [...listed.values()];
50
- };
51
- const computeStats = (files) => ({
52
- fileCount: files.length,
53
- totalBytes: files.reduce((sum, file) => sum + file.sizeBytes, 0),
54
- });
55
- const isExpired = (createdAt, ttlHours) => {
56
- const createdMs = Date.parse(createdAt);
57
- if (!Number.isFinite(createdMs))
58
- return false;
59
- return Date.now() - createdMs > ttlHours * 60 * 60 * 1000;
60
- };
61
- const deleteFiles = async (state, files) => {
62
- let deleted = 0;
63
- for (const file of files) {
64
- const ok = await state.storage.delete(`file:${file.id}`);
65
- if (ok)
66
- deleted += 1;
67
- }
68
- return deleted;
69
- };
70
- export class FileStoreDO {
71
- state;
72
- constructor(state) {
73
- this.state = state;
74
- }
75
- async fetch(request) {
76
- const url = new URL(request.url);
77
- if (request.method === "POST" && url.pathname === "/put") {
78
- const body = await readJson(request);
79
- const policy = parsePolicy(body.policy);
80
- const filename = typeof body.filename === "string" ? body.filename : `file-${Date.now()}`;
81
- const mimeType = typeof body.mimeType === "string" ? body.mimeType : "application/octet-stream";
82
- const bytesBase64 = typeof body.bytesBase64 === "string" ? body.bytesBase64 : "";
83
- const bytes = fromBase64(bytesBase64);
84
- if (bytes.byteLength > policy.maxFileBytes) {
85
- return json({
86
- error: `file too large: ${bytes.byteLength} bytes exceeds maxFileBytes ${policy.maxFileBytes}`,
87
- code: "FILE_TOO_LARGE",
88
- policy,
89
- }, 413);
90
- }
91
- let files = await listStoredValues(this.state);
92
- const expired = files.filter((file) => isExpired(file.createdAt, policy.ttlHours));
93
- if (expired.length > 0) {
94
- await deleteFiles(this.state, expired);
95
- files = await listStoredValues(this.state);
96
- }
97
- let stats = computeStats(files);
98
- const projected = stats.totalBytes + bytes.byteLength;
99
- if (projected > policy.maxTotalBytes) {
100
- const needFree = projected - policy.maxTotalBytes;
101
- const candidates = [...files]
102
- .sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt))
103
- .slice(0, policy.cleanupBatchSize);
104
- let freed = 0;
105
- const evictList = [];
106
- for (const file of candidates) {
107
- evictList.push(file);
108
- freed += file.sizeBytes;
109
- if (freed >= needFree)
110
- break;
111
- }
112
- if (evictList.length > 0) {
113
- await deleteFiles(this.state, evictList);
114
- files = await listStoredValues(this.state);
115
- stats = computeStats(files);
116
- }
117
- }
118
- if (stats.totalBytes + bytes.byteLength > policy.maxTotalBytes) {
119
- return json({
120
- error: `storage quota exceeded: total ${stats.totalBytes} + incoming ${bytes.byteLength} > maxTotalBytes ${policy.maxTotalBytes}`,
121
- code: "STORAGE_QUOTA_EXCEEDED",
122
- policy,
123
- stats,
124
- }, 507);
125
- }
126
- const id = crypto.randomUUID();
127
- const value = {
128
- id,
129
- filename,
130
- mimeType,
131
- sizeBytes: bytes.byteLength,
132
- createdAt: new Date().toISOString(),
133
- bytesBase64,
134
- };
135
- await this.state.storage.put(`file:${id}`, value);
136
- return json({ file: toMeta(value), policy });
137
- }
138
- if (request.method === "GET" && url.pathname === "/get") {
139
- const fileId = url.searchParams.get("fileId");
140
- if (!fileId)
141
- return json({ error: "Missing fileId" }, 400);
142
- const value = await this.state.storage.get(`file:${fileId}`);
143
- if (!value)
144
- return json({ file: null });
145
- return json({ file: value });
146
- }
147
- if (request.method === "GET" && url.pathname === "/list") {
148
- const listed = await this.state.storage.list({ prefix: "file:" });
149
- const files = [...listed.values()].map(toMeta);
150
- return json({ files });
151
- }
152
- if (request.method === "POST" && url.pathname === "/delete") {
153
- const body = await readJson(request);
154
- const fileId = typeof body.fileId === "string" ? body.fileId : "";
155
- if (!fileId)
156
- return json({ error: "Missing fileId" }, 400);
157
- const key = `file:${fileId}`;
158
- const existing = await this.state.storage.get(key);
159
- if (!existing)
160
- return json({ deleted: false });
161
- await this.state.storage.delete(key);
162
- return json({ deleted: true });
163
- }
164
- if (request.method === "GET" && url.pathname === "/stats") {
165
- let policyInput;
166
- const encoded = url.searchParams.get("policy");
167
- if (encoded) {
168
- try {
169
- policyInput = JSON.parse(encoded);
170
- }
171
- catch {
172
- policyInput = undefined;
173
- }
174
- }
175
- const policy = parsePolicy(policyInput);
176
- const files = await listStoredValues(this.state);
177
- const stats = computeStats(files);
178
- return json({ policy, stats });
179
- }
180
- if (request.method === "POST" && url.pathname === "/cleanup") {
181
- const body = await readJson(request);
182
- const policy = parsePolicy(body.policy);
183
- const files = await listStoredValues(this.state);
184
- const expired = files.filter((file) => isExpired(file.createdAt, policy.ttlHours));
185
- const deletedExpired = await deleteFiles(this.state, expired);
186
- const afterExpired = await listStoredValues(this.state);
187
- let stats = computeStats(afterExpired);
188
- let deletedEvicted = 0;
189
- if (stats.totalBytes > policy.maxTotalBytes) {
190
- const sorted = [...afterExpired].sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt));
191
- const evictList = [];
192
- for (const file of sorted) {
193
- evictList.push(file);
194
- const projected = stats.totalBytes - evictList.reduce((sum, item) => sum + item.sizeBytes, 0);
195
- if (projected <= policy.maxTotalBytes)
196
- break;
197
- if (evictList.length >= policy.cleanupBatchSize)
198
- break;
199
- }
200
- deletedEvicted = await deleteFiles(this.state, evictList);
201
- stats = computeStats(await listStoredValues(this.state));
202
- }
203
- return json({
204
- policy,
205
- deletedExpired,
206
- deletedEvicted,
207
- stats,
208
- });
209
- }
210
- return json({ error: "Not found" }, 404);
211
- }
212
- }
213
- export class DurableObjectFileStore {
214
- namespace;
215
- policy;
216
- constructor(namespace, policy) {
217
- this.namespace = namespace;
218
- this.policy = policy;
219
- }
220
- stub() {
221
- return this.namespace.get(this.namespace.idFromName("echo-pdf-file-store"));
222
- }
223
- async put(input) {
224
- const response = await this.stub().fetch("https://do/put", {
225
- method: "POST",
226
- headers: { "Content-Type": "application/json" },
227
- body: JSON.stringify({
228
- filename: input.filename,
229
- mimeType: input.mimeType,
230
- bytesBase64: toBase64(input.bytes),
231
- policy: this.policy,
232
- }),
233
- });
234
- const payload = (await response.json());
235
- if (!response.ok || !payload.file) {
236
- const details = payload;
237
- const error = new Error(payload.error ?? "DO put failed");
238
- error.status = response.status;
239
- error.code = typeof details.code === "string" ? details.code : undefined;
240
- error.details = { policy: details.policy, stats: details.stats };
241
- throw error;
242
- }
243
- return payload.file;
244
- }
245
- async get(fileId) {
246
- const response = await this.stub().fetch(`https://do/get?fileId=${encodeURIComponent(fileId)}`);
247
- const payload = (await response.json());
248
- if (!response.ok)
249
- throw new Error("DO get failed");
250
- if (!payload.file)
251
- return null;
252
- return {
253
- id: payload.file.id,
254
- filename: payload.file.filename,
255
- mimeType: payload.file.mimeType,
256
- sizeBytes: payload.file.sizeBytes,
257
- createdAt: payload.file.createdAt,
258
- bytes: fromBase64(payload.file.bytesBase64),
259
- };
260
- }
261
- async list() {
262
- const response = await this.stub().fetch("https://do/list");
263
- const payload = (await response.json());
264
- if (!response.ok)
265
- throw new Error("DO list failed");
266
- return payload.files ?? [];
267
- }
268
- async delete(fileId) {
269
- const response = await this.stub().fetch("https://do/delete", {
270
- method: "POST",
271
- headers: { "Content-Type": "application/json" },
272
- body: JSON.stringify({ fileId }),
273
- });
274
- const payload = (await response.json());
275
- if (!response.ok)
276
- throw new Error("DO delete failed");
277
- return payload.deleted === true;
278
- }
279
- async stats() {
280
- const policyEncoded = encodeURIComponent(JSON.stringify(this.policy));
281
- const response = await this.stub().fetch(`https://do/stats?policy=${policyEncoded}`);
282
- const payload = (await response.json());
283
- if (!response.ok)
284
- throw new Error("DO stats failed");
285
- return payload;
286
- }
287
- async cleanup() {
288
- const response = await this.stub().fetch("https://do/cleanup", {
289
- method: "POST",
290
- headers: { "Content-Type": "application/json" },
291
- body: JSON.stringify({ policy: this.policy }),
292
- });
293
- const payload = (await response.json());
294
- if (!response.ok)
295
- throw new Error("DO cleanup failed");
296
- return payload;
297
- }
298
- }
@@ -1,9 +0,0 @@
1
- export declare class HttpError extends Error {
2
- readonly status: number;
3
- readonly code: string;
4
- readonly details?: unknown;
5
- constructor(status: number, code: string, message: string, details?: unknown);
6
- }
7
- export declare const badRequest: (code: string, message: string, details?: unknown) => HttpError;
8
- export declare const notFound: (code: string, message: string, details?: unknown) => HttpError;
9
- export declare const unprocessable: (code: string, message: string, details?: unknown) => HttpError;
@@ -1,14 +0,0 @@
1
- export class HttpError extends Error {
2
- status;
3
- code;
4
- details;
5
- constructor(status, code, message, details) {
6
- super(message);
7
- this.status = status;
8
- this.code = code;
9
- this.details = details;
10
- }
11
- }
12
- export const badRequest = (code, message, details) => new HttpError(400, code, message, details);
13
- export const notFound = (code, message, details) => new HttpError(404, code, message, details);
14
- export const unprocessable = (code, message, details) => new HttpError(422, code, message, details);
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./core/index.js";
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- export * from "./core/index.js";
@@ -1,3 +0,0 @@
1
- import type { Env, FileStore } from "./types.js";
2
- import type { EchoPdfConfig } from "./pdf-types.js";
3
- export declare const handleMcpRequest: (request: Request, env: Env, config: EchoPdfConfig, fileStore: FileStore) => Promise<Response>;
@@ -1,124 +0,0 @@
1
- import { checkHeaderAuth } from "./auth.js";
2
- import { buildMcpContent, buildToolOutputEnvelope } from "./response-schema.js";
3
- import { callTool, listToolSchemas } from "./tool-registry.js";
4
- const ok = (id, result) => new Response(JSON.stringify({
5
- jsonrpc: "2.0",
6
- id: id ?? null,
7
- result,
8
- }), { headers: { "Content-Type": "application/json" } });
9
- const err = (id, code, message, data, httpStatus = 400) => new Response(JSON.stringify({
10
- jsonrpc: "2.0",
11
- id: id ?? null,
12
- error: data ? { code, message, data } : { code, message },
13
- }), { status: httpStatus, headers: { "Content-Type": "application/json" } });
14
- const asObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v) ? v : {};
15
- const resolvePublicBaseUrl = (request, configured) => typeof configured === "string" && configured.length > 0 ? configured : request.url;
16
- const prepareMcpToolArgs = (toolName, args) => {
17
- if (toolName === "pdf_extract_pages") {
18
- const mode = typeof args.returnMode === "string" ? args.returnMode : "";
19
- if (!mode) {
20
- return { ...args, returnMode: "url" };
21
- }
22
- }
23
- return args;
24
- };
25
- export const handleMcpRequest = async (request, env, config, fileStore) => {
26
- const auth = checkHeaderAuth(request, env, {
27
- authHeader: config.mcp.authHeader,
28
- authEnv: config.mcp.authEnv,
29
- allowMissingSecret: env.ECHO_PDF_ALLOW_MISSING_AUTH_SECRET === "1",
30
- misconfiguredCode: "AUTH_MISCONFIGURED",
31
- unauthorizedCode: "UNAUTHORIZED",
32
- contextName: "MCP",
33
- });
34
- if (!auth.ok) {
35
- return err(null, -32001, auth.message, { status: auth.status, code: auth.code }, 200);
36
- }
37
- let body;
38
- try {
39
- body = (await request.json());
40
- }
41
- catch {
42
- return err(null, -32700, "Parse error");
43
- }
44
- if (typeof body !== "object" || body === null) {
45
- return err(null, -32600, "Invalid Request");
46
- }
47
- if (body.jsonrpc !== "2.0") {
48
- return err(body.id ?? null, -32600, "Invalid Request: jsonrpc must be '2.0'");
49
- }
50
- const method = body.method ?? "";
51
- const id = body.id ?? null;
52
- if (typeof method !== "string" || method.length === 0) {
53
- return err(id, -32600, "Invalid Request: method is required");
54
- }
55
- if (method.startsWith("notifications/")) {
56
- return new Response(null, { status: 204 });
57
- }
58
- const params = asObj(body.params);
59
- if (method === "initialize") {
60
- return ok(id, {
61
- protocolVersion: "2024-11-05",
62
- serverInfo: {
63
- name: config.mcp.serverName,
64
- version: config.mcp.version,
65
- },
66
- capabilities: {
67
- tools: {},
68
- },
69
- });
70
- }
71
- if (method === "tools/list") {
72
- return ok(id, { tools: listToolSchemas().map((tool) => ({
73
- name: tool.name,
74
- description: tool.description,
75
- inputSchema: tool.inputSchema,
76
- })) });
77
- }
78
- if (method !== "tools/call") {
79
- return err(id, -32601, `Unsupported method: ${method}`);
80
- }
81
- const toolName = typeof params.name === "string" ? params.name : "";
82
- const args = prepareMcpToolArgs(toolName, asObj(params.arguments));
83
- if (!toolName) {
84
- return err(id, -32602, "Invalid params: name is required", {
85
- code: "INVALID_PARAMS",
86
- status: 400,
87
- });
88
- }
89
- try {
90
- const result = await callTool(toolName, args, {
91
- config,
92
- env,
93
- fileStore,
94
- });
95
- const envelope = buildToolOutputEnvelope(result, resolvePublicBaseUrl(request, config.service.publicBaseUrl));
96
- return ok(id, { content: buildMcpContent(envelope) });
97
- }
98
- catch (error) {
99
- const message = error instanceof Error ? error.message : String(error);
100
- const status = error?.status;
101
- const stableStatus = typeof status === "number" && Number.isFinite(status) ? status : 500;
102
- const code = error?.code;
103
- const details = error?.details;
104
- if (message.startsWith("Unknown tool:")) {
105
- return err(id, -32601, message, {
106
- code: typeof code === "string" ? code : "TOOL_NOT_FOUND",
107
- status: 404,
108
- details,
109
- });
110
- }
111
- if (stableStatus >= 400 && stableStatus < 500) {
112
- return err(id, -32602, message, {
113
- code: typeof code === "string" ? code : "INVALID_PARAMS",
114
- status: stableStatus,
115
- details,
116
- });
117
- }
118
- return err(id, -32000, message, {
119
- code: typeof code === "string" ? code : "INTERNAL_ERROR",
120
- status: stableStatus,
121
- details,
122
- });
123
- }
124
- };
@@ -1,16 +0,0 @@
1
- export interface SemanticPageInput {
2
- readonly pageNumber: number;
3
- readonly text: string;
4
- readonly artifactPath: string;
5
- }
6
- export interface SemanticSectionNode {
7
- readonly id: string;
8
- readonly type: "section";
9
- readonly title: string;
10
- readonly level: number;
11
- readonly pageNumber: number;
12
- readonly pageArtifactPath: string;
13
- readonly excerpt: string;
14
- readonly children: ReadonlyArray<SemanticSectionNode>;
15
- }
16
- export declare const buildSemanticSectionTree: (pages: ReadonlyArray<SemanticPageInput>) => ReadonlyArray<SemanticSectionNode>;