@echofiles/echo-pdf 0.4.0 → 0.4.2
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/README.md +80 -0
- package/bin/echo-pdf.js +9 -164
- package/bin/lib/http.js +72 -0
- package/bin/lib/mcp-stdio.js +99 -0
- package/dist/agent-defaults.d.ts +3 -0
- package/dist/agent-defaults.js +18 -0
- package/dist/auth.d.ts +18 -0
- package/dist/auth.js +24 -0
- package/dist/core/index.d.ts +50 -0
- package/dist/core/index.js +7 -0
- package/dist/file-ops.d.ts +11 -0
- package/dist/file-ops.js +36 -0
- package/dist/file-store-do.d.ts +36 -0
- package/dist/file-store-do.js +298 -0
- package/dist/file-utils.d.ts +6 -0
- package/dist/file-utils.js +36 -0
- package/dist/http-error.d.ts +9 -0
- package/dist/http-error.js +14 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mcp-server.d.ts +3 -0
- package/dist/mcp-server.js +127 -0
- package/dist/pdf-agent.d.ts +18 -0
- package/dist/pdf-agent.js +217 -0
- package/dist/pdf-config.d.ts +4 -0
- package/dist/pdf-config.js +130 -0
- package/dist/pdf-storage.d.ts +8 -0
- package/dist/pdf-storage.js +86 -0
- package/dist/pdf-types.d.ts +79 -0
- package/dist/pdf-types.js +1 -0
- package/dist/pdfium-engine.d.ts +9 -0
- package/dist/pdfium-engine.js +180 -0
- package/dist/provider-client.d.ts +12 -0
- package/dist/provider-client.js +134 -0
- package/dist/provider-keys.d.ts +10 -0
- package/dist/provider-keys.js +27 -0
- package/dist/r2-file-store.d.ts +20 -0
- package/dist/r2-file-store.js +176 -0
- package/dist/response-schema.d.ts +15 -0
- package/dist/response-schema.js +159 -0
- package/dist/tool-registry.d.ts +16 -0
- package/dist/tool-registry.js +175 -0
- package/dist/types.d.ts +91 -0
- package/dist/types.js +1 -0
- package/dist/worker.d.ts +7 -0
- package/dist/worker.js +366 -0
- package/package.json +22 -4
- package/wrangler.toml +1 -1
- package/src/agent-defaults.ts +0 -25
- package/src/file-ops.ts +0 -50
- package/src/file-store-do.ts +0 -349
- package/src/file-utils.ts +0 -43
- package/src/http-error.ts +0 -21
- package/src/index.ts +0 -400
- package/src/mcp-server.ts +0 -158
- package/src/pdf-agent.ts +0 -252
- package/src/pdf-config.ts +0 -143
- package/src/pdf-storage.ts +0 -109
- package/src/pdf-types.ts +0 -85
- package/src/pdfium-engine.ts +0 -207
- package/src/provider-client.ts +0 -176
- package/src/provider-keys.ts +0 -44
- package/src/r2-file-store.ts +0 -195
- package/src/response-schema.ts +0 -182
- package/src/tool-registry.ts +0 -203
- package/src/types.ts +0 -40
- package/src/wasm.d.ts +0 -4
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { init } from "@embedpdf/pdfium";
|
|
2
|
+
import { encode as encodePng } from "@cf-wasm/png";
|
|
3
|
+
let moduleInstance = null;
|
|
4
|
+
let libraryInitialized = false;
|
|
5
|
+
const toUint8 = (value) => new Uint8Array(value);
|
|
6
|
+
const textDecoder = new TextDecoder();
|
|
7
|
+
const isWorkerdRuntime = () => typeof globalThis.WebSocketPair === "function";
|
|
8
|
+
const ensureWasmFunctionShim = () => {
|
|
9
|
+
const wasmApi = WebAssembly;
|
|
10
|
+
if (typeof wasmApi.Function === "function")
|
|
11
|
+
return;
|
|
12
|
+
wasmApi.Function = (_sig, fn) => fn;
|
|
13
|
+
};
|
|
14
|
+
const ensurePdfium = async (config) => {
|
|
15
|
+
ensureWasmFunctionShim();
|
|
16
|
+
if (!moduleInstance) {
|
|
17
|
+
if (isWorkerdRuntime()) {
|
|
18
|
+
const wasmModuleImport = await import("@embedpdf/pdfium/pdfium.wasm");
|
|
19
|
+
const maybeModule = wasmModuleImport.default ?? wasmModuleImport;
|
|
20
|
+
if (maybeModule instanceof WebAssembly.Module) {
|
|
21
|
+
moduleInstance = await init({
|
|
22
|
+
instantiateWasm: (imports, successCallback) => {
|
|
23
|
+
const instance = new WebAssembly.Instance(maybeModule, imports);
|
|
24
|
+
successCallback(instance, maybeModule);
|
|
25
|
+
return instance.exports;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (!moduleInstance) {
|
|
31
|
+
const wasmBinary = await fetch(config.pdfium.wasmUrl).then((res) => res.arrayBuffer());
|
|
32
|
+
moduleInstance = await init({ wasmBinary });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (!libraryInitialized) {
|
|
36
|
+
moduleInstance.FPDF_InitLibrary();
|
|
37
|
+
libraryInitialized = true;
|
|
38
|
+
}
|
|
39
|
+
return moduleInstance;
|
|
40
|
+
};
|
|
41
|
+
const makeDoc = (pdfium, bytes) => {
|
|
42
|
+
const memPtr = pdfium.pdfium.wasmExports.malloc(bytes.length);
|
|
43
|
+
pdfium.pdfium.HEAPU8.set(bytes, memPtr);
|
|
44
|
+
const doc = pdfium.FPDF_LoadMemDocument(memPtr, bytes.length, "");
|
|
45
|
+
if (!doc) {
|
|
46
|
+
pdfium.pdfium.wasmExports.free(memPtr);
|
|
47
|
+
throw new Error("Failed to load PDF document");
|
|
48
|
+
}
|
|
49
|
+
return { doc, memPtr };
|
|
50
|
+
};
|
|
51
|
+
const closeDoc = (pdfium, doc, memPtr) => {
|
|
52
|
+
pdfium.FPDF_CloseDocument(doc);
|
|
53
|
+
pdfium.pdfium.wasmExports.free(memPtr);
|
|
54
|
+
};
|
|
55
|
+
const bgraToRgba = (bgra) => {
|
|
56
|
+
const rgba = new Uint8Array(bgra.length);
|
|
57
|
+
for (let i = 0; i < bgra.length; i += 4) {
|
|
58
|
+
rgba[i] = bgra[i + 2] ?? 0;
|
|
59
|
+
rgba[i + 1] = bgra[i + 1] ?? 0;
|
|
60
|
+
rgba[i + 2] = bgra[i] ?? 0;
|
|
61
|
+
rgba[i + 3] = bgra[i + 3] ?? 255;
|
|
62
|
+
}
|
|
63
|
+
return rgba;
|
|
64
|
+
};
|
|
65
|
+
const decodeUtf16Le = (buf) => {
|
|
66
|
+
const view = new Uint16Array(buf.buffer, buf.byteOffset, Math.floor(buf.byteLength / 2));
|
|
67
|
+
const chars = [];
|
|
68
|
+
for (const code of view) {
|
|
69
|
+
if (code === 0)
|
|
70
|
+
break;
|
|
71
|
+
chars.push(code);
|
|
72
|
+
}
|
|
73
|
+
return String.fromCharCode(...chars);
|
|
74
|
+
};
|
|
75
|
+
export const getPdfPageCount = async (config, bytes) => {
|
|
76
|
+
const pdfium = await ensurePdfium(config);
|
|
77
|
+
const { doc, memPtr } = makeDoc(pdfium, bytes);
|
|
78
|
+
try {
|
|
79
|
+
return pdfium.FPDF_GetPageCount(doc);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
closeDoc(pdfium, doc, memPtr);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
export const renderPdfPageToPng = async (config, bytes, pageIndex, scale = config.service.defaultRenderScale) => {
|
|
86
|
+
const pdfium = await ensurePdfium(config);
|
|
87
|
+
const { doc, memPtr } = makeDoc(pdfium, bytes);
|
|
88
|
+
let page = 0;
|
|
89
|
+
let bitmap = 0;
|
|
90
|
+
try {
|
|
91
|
+
page = pdfium.FPDF_LoadPage(doc, pageIndex);
|
|
92
|
+
if (!page) {
|
|
93
|
+
throw new Error(`Failed to load page ${pageIndex}`);
|
|
94
|
+
}
|
|
95
|
+
const width = Math.max(1, Math.round(pdfium.FPDF_GetPageWidthF(page) * scale));
|
|
96
|
+
const height = Math.max(1, Math.round(pdfium.FPDF_GetPageHeightF(page) * scale));
|
|
97
|
+
bitmap = pdfium.FPDFBitmap_Create(width, height, 1);
|
|
98
|
+
if (!bitmap) {
|
|
99
|
+
throw new Error("Failed to create bitmap");
|
|
100
|
+
}
|
|
101
|
+
pdfium.FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xffffffff);
|
|
102
|
+
pdfium.FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
|
|
103
|
+
const stride = pdfium.FPDFBitmap_GetStride(bitmap);
|
|
104
|
+
const bufferPtr = pdfium.FPDFBitmap_GetBuffer(bitmap);
|
|
105
|
+
const heap = pdfium.pdfium.HEAPU8;
|
|
106
|
+
const bgra = heap.slice(bufferPtr, bufferPtr + stride * height);
|
|
107
|
+
const rgba = bgraToRgba(bgra);
|
|
108
|
+
const png = encodePng(rgba, width, height);
|
|
109
|
+
return { width, height, png };
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
if (bitmap)
|
|
113
|
+
pdfium.FPDFBitmap_Destroy(bitmap);
|
|
114
|
+
if (page)
|
|
115
|
+
pdfium.FPDF_ClosePage(page);
|
|
116
|
+
closeDoc(pdfium, doc, memPtr);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
export const extractPdfPageText = async (config, bytes, pageIndex) => {
|
|
120
|
+
const pdfium = await ensurePdfium(config);
|
|
121
|
+
const { doc, memPtr } = makeDoc(pdfium, bytes);
|
|
122
|
+
let page = 0;
|
|
123
|
+
let textPage = 0;
|
|
124
|
+
let outPtr = 0;
|
|
125
|
+
try {
|
|
126
|
+
page = pdfium.FPDF_LoadPage(doc, pageIndex);
|
|
127
|
+
if (!page) {
|
|
128
|
+
throw new Error(`Failed to load page ${pageIndex}`);
|
|
129
|
+
}
|
|
130
|
+
textPage = pdfium.FPDFText_LoadPage(page);
|
|
131
|
+
if (!textPage)
|
|
132
|
+
return "";
|
|
133
|
+
const chars = pdfium.FPDFText_CountChars(textPage);
|
|
134
|
+
if (chars <= 0)
|
|
135
|
+
return "";
|
|
136
|
+
const bytesLen = (chars + 1) * 2;
|
|
137
|
+
outPtr = pdfium.pdfium.wasmExports.malloc(bytesLen);
|
|
138
|
+
pdfium.FPDFText_GetText(textPage, 0, chars, outPtr);
|
|
139
|
+
const heap = pdfium.pdfium.HEAPU8;
|
|
140
|
+
const raw = heap.slice(outPtr, outPtr + bytesLen);
|
|
141
|
+
return decodeUtf16Le(raw).trim();
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
if (outPtr)
|
|
145
|
+
pdfium.pdfium.wasmExports.free(outPtr);
|
|
146
|
+
if (textPage)
|
|
147
|
+
pdfium.FPDFText_ClosePage(textPage);
|
|
148
|
+
if (page)
|
|
149
|
+
pdfium.FPDF_ClosePage(page);
|
|
150
|
+
closeDoc(pdfium, doc, memPtr);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
export const toBytes = async (value) => {
|
|
154
|
+
const response = await fetch(value);
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
throw new Error(`Failed to fetch source: HTTP ${response.status}`);
|
|
157
|
+
}
|
|
158
|
+
const contentType = (response.headers.get("content-type") ?? "").toLowerCase();
|
|
159
|
+
const bytes = toUint8(await response.arrayBuffer());
|
|
160
|
+
const signature = textDecoder.decode(bytes.subarray(0, Math.min(8, bytes.length)));
|
|
161
|
+
if (contentType.includes("application/pdf") || signature.startsWith("%PDF-")) {
|
|
162
|
+
return bytes;
|
|
163
|
+
}
|
|
164
|
+
const html = textDecoder.decode(bytes);
|
|
165
|
+
const pdfMatch = html.match(/https?:\/\/[^"' )]+\.pdf[^"' )]*/i);
|
|
166
|
+
if (!pdfMatch || pdfMatch.length === 0) {
|
|
167
|
+
throw new Error("URL does not point to a PDF and no PDF link was found in the page");
|
|
168
|
+
}
|
|
169
|
+
const resolvedUrl = pdfMatch[0].replace(/&/g, "&");
|
|
170
|
+
const pdfResponse = await fetch(resolvedUrl);
|
|
171
|
+
if (!pdfResponse.ok) {
|
|
172
|
+
throw new Error(`Failed to fetch resolved PDF url: HTTP ${pdfResponse.status}`);
|
|
173
|
+
}
|
|
174
|
+
const pdfBytes = toUint8(await pdfResponse.arrayBuffer());
|
|
175
|
+
const pdfSignature = textDecoder.decode(pdfBytes.subarray(0, Math.min(8, pdfBytes.length)));
|
|
176
|
+
if (!pdfSignature.startsWith("%PDF-")) {
|
|
177
|
+
throw new Error("Resolved file is not a valid PDF");
|
|
178
|
+
}
|
|
179
|
+
return pdfBytes;
|
|
180
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Env } from "./types.js";
|
|
2
|
+
import type { EchoPdfConfig } from "./pdf-types.js";
|
|
3
|
+
export declare const listProviderModels: (config: EchoPdfConfig, env: Env, alias: string, runtimeApiKeys?: Record<string, string>) => Promise<ReadonlyArray<string>>;
|
|
4
|
+
export declare const visionRecognize: (input: {
|
|
5
|
+
config: EchoPdfConfig;
|
|
6
|
+
env: Env;
|
|
7
|
+
providerAlias: string;
|
|
8
|
+
model: string;
|
|
9
|
+
prompt: string;
|
|
10
|
+
imageDataUrl: string;
|
|
11
|
+
runtimeApiKeys?: Record<string, string>;
|
|
12
|
+
}) => Promise<string>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { resolveProviderApiKey } from "./provider-keys.js";
|
|
2
|
+
const defaultBaseUrl = (provider) => {
|
|
3
|
+
if (provider.baseUrl)
|
|
4
|
+
return provider.baseUrl;
|
|
5
|
+
switch (provider.type) {
|
|
6
|
+
case "openrouter":
|
|
7
|
+
return "https://openrouter.ai/api/v1";
|
|
8
|
+
case "vercel-ai-gateway":
|
|
9
|
+
return "https://ai-gateway.vercel.sh/v1";
|
|
10
|
+
case "openai":
|
|
11
|
+
default:
|
|
12
|
+
return "https://api.openai.com/v1";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const noTrailingSlash = (url) => url.replace(/\/+$/, "");
|
|
16
|
+
const resolveEndpoint = (provider, kind) => {
|
|
17
|
+
const configured = provider.endpoints?.[kind];
|
|
18
|
+
if (configured?.startsWith("http://") || configured?.startsWith("https://")) {
|
|
19
|
+
return configured;
|
|
20
|
+
}
|
|
21
|
+
const fallback = kind === "chatCompletionsPath" ? "/chat/completions" : "/models";
|
|
22
|
+
const path = configured && configured.length > 0 ? configured : fallback;
|
|
23
|
+
return `${noTrailingSlash(defaultBaseUrl(provider))}${path.startsWith("/") ? path : `/${path}`}`;
|
|
24
|
+
};
|
|
25
|
+
const toAuthHeader = (config, providerAlias, provider, env, runtimeApiKeys) => {
|
|
26
|
+
const token = resolveProviderApiKey({
|
|
27
|
+
config,
|
|
28
|
+
env,
|
|
29
|
+
providerAlias,
|
|
30
|
+
provider,
|
|
31
|
+
runtimeApiKeys,
|
|
32
|
+
});
|
|
33
|
+
return { Authorization: `Bearer ${token}` };
|
|
34
|
+
};
|
|
35
|
+
const withTimeout = async (url, init, timeoutMs) => {
|
|
36
|
+
const ctrl = new AbortController();
|
|
37
|
+
const timer = setTimeout(() => ctrl.abort("timeout"), timeoutMs);
|
|
38
|
+
try {
|
|
39
|
+
return await fetch(url, { ...init, signal: ctrl.signal });
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
43
|
+
throw new Error(`Request timeout after ${timeoutMs}ms for ${url}`);
|
|
44
|
+
}
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const responseDetail = async (response) => {
|
|
52
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
53
|
+
try {
|
|
54
|
+
if (contentType.includes("application/json")) {
|
|
55
|
+
return JSON.stringify(await response.json()).slice(0, 800);
|
|
56
|
+
}
|
|
57
|
+
return (await response.text()).slice(0, 800);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return "<unable to parse response payload>";
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const getProvider = (config, alias) => {
|
|
64
|
+
const provider = config.providers[alias];
|
|
65
|
+
if (!provider) {
|
|
66
|
+
throw new Error(`Provider "${alias}" not configured`);
|
|
67
|
+
}
|
|
68
|
+
return provider;
|
|
69
|
+
};
|
|
70
|
+
export const listProviderModels = async (config, env, alias, runtimeApiKeys) => {
|
|
71
|
+
const provider = getProvider(config, alias);
|
|
72
|
+
const url = resolveEndpoint(provider, "modelsPath");
|
|
73
|
+
const response = await withTimeout(url, {
|
|
74
|
+
method: "GET",
|
|
75
|
+
headers: {
|
|
76
|
+
Accept: "application/json",
|
|
77
|
+
...toAuthHeader(config, alias, provider, env, runtimeApiKeys),
|
|
78
|
+
...(provider.headers ?? {}),
|
|
79
|
+
},
|
|
80
|
+
}, provider.timeoutMs ?? 30000);
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
throw new Error(`Model list request failed: HTTP ${response.status} url=${url} detail=${await responseDetail(response)}`);
|
|
83
|
+
}
|
|
84
|
+
const payload = await response.json();
|
|
85
|
+
const data = payload.data;
|
|
86
|
+
if (!Array.isArray(data))
|
|
87
|
+
return [];
|
|
88
|
+
return data
|
|
89
|
+
.map((item) => item)
|
|
90
|
+
.map((item) => (typeof item.id === "string" ? item.id : ""))
|
|
91
|
+
.filter((id) => id.length > 0);
|
|
92
|
+
};
|
|
93
|
+
export const visionRecognize = async (input) => {
|
|
94
|
+
const provider = getProvider(input.config, input.providerAlias);
|
|
95
|
+
const url = resolveEndpoint(provider, "chatCompletionsPath");
|
|
96
|
+
const response = await withTimeout(url, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
headers: {
|
|
99
|
+
"Content-Type": "application/json",
|
|
100
|
+
...toAuthHeader(input.config, input.providerAlias, provider, input.env, input.runtimeApiKeys),
|
|
101
|
+
...(provider.headers ?? {}),
|
|
102
|
+
},
|
|
103
|
+
body: JSON.stringify({
|
|
104
|
+
model: input.model,
|
|
105
|
+
messages: [
|
|
106
|
+
{
|
|
107
|
+
role: "user",
|
|
108
|
+
content: [
|
|
109
|
+
{ type: "text", text: input.prompt },
|
|
110
|
+
{ type: "image_url", image_url: { url: input.imageDataUrl } },
|
|
111
|
+
],
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
}),
|
|
115
|
+
}, provider.timeoutMs ?? 30000);
|
|
116
|
+
if (!response.ok) {
|
|
117
|
+
throw new Error(`Vision request failed: HTTP ${response.status} url=${url} detail=${await responseDetail(response)}`);
|
|
118
|
+
}
|
|
119
|
+
const payload = await response.json();
|
|
120
|
+
const message = payload.choices?.[0]?.message;
|
|
121
|
+
if (!message)
|
|
122
|
+
return "";
|
|
123
|
+
const content = message.content;
|
|
124
|
+
if (typeof content === "string")
|
|
125
|
+
return content;
|
|
126
|
+
if (Array.isArray(content)) {
|
|
127
|
+
return content
|
|
128
|
+
.map((part) => part)
|
|
129
|
+
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
130
|
+
.map((part) => part.text ?? "")
|
|
131
|
+
.join("");
|
|
132
|
+
}
|
|
133
|
+
return "";
|
|
134
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { EchoPdfConfig, EchoPdfProviderConfig } from "./pdf-types.js";
|
|
2
|
+
import type { Env } from "./types.js";
|
|
3
|
+
export declare const runtimeProviderKeyCandidates: (_config: EchoPdfConfig, providerAlias: string, provider: EchoPdfProviderConfig) => string[];
|
|
4
|
+
export declare const resolveProviderApiKey: (input: {
|
|
5
|
+
config: EchoPdfConfig;
|
|
6
|
+
env: Env;
|
|
7
|
+
providerAlias: string;
|
|
8
|
+
provider: EchoPdfProviderConfig;
|
|
9
|
+
runtimeApiKeys?: Record<string, string>;
|
|
10
|
+
}) => string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { readRequiredEnv } from "./pdf-config.js";
|
|
2
|
+
const normalizeKey = (value) => value.trim();
|
|
3
|
+
const keyVariants = (value) => {
|
|
4
|
+
const raw = normalizeKey(value);
|
|
5
|
+
if (raw.length === 0)
|
|
6
|
+
return [];
|
|
7
|
+
return Array.from(new Set([
|
|
8
|
+
raw,
|
|
9
|
+
raw.replace(/-/g, "_"),
|
|
10
|
+
raw.replace(/_/g, "-"),
|
|
11
|
+
]));
|
|
12
|
+
};
|
|
13
|
+
export const runtimeProviderKeyCandidates = (_config, providerAlias, provider) => {
|
|
14
|
+
const aliases = keyVariants(providerAlias);
|
|
15
|
+
const types = keyVariants(provider.type);
|
|
16
|
+
return Array.from(new Set([...aliases, ...types]));
|
|
17
|
+
};
|
|
18
|
+
export const resolveProviderApiKey = (input) => {
|
|
19
|
+
const candidates = runtimeProviderKeyCandidates(input.config, input.providerAlias, input.provider);
|
|
20
|
+
for (const candidate of candidates) {
|
|
21
|
+
const value = input.runtimeApiKeys?.[candidate];
|
|
22
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
23
|
+
return value.trim();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return readRequiredEnv(input.env, input.provider.apiKeyEnv);
|
|
27
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { StoragePolicy } from "./pdf-types.js";
|
|
2
|
+
import type { FileStore, R2Bucket, StoredFileMeta, StoredFileRecord } from "./types.js";
|
|
3
|
+
export declare class R2FileStore implements FileStore {
|
|
4
|
+
private readonly bucket;
|
|
5
|
+
private readonly policy;
|
|
6
|
+
constructor(bucket: R2Bucket, policy: StoragePolicy);
|
|
7
|
+
put(input: {
|
|
8
|
+
readonly filename: string;
|
|
9
|
+
readonly mimeType: string;
|
|
10
|
+
readonly bytes: Uint8Array;
|
|
11
|
+
}): Promise<StoredFileMeta>;
|
|
12
|
+
get(fileId: string): Promise<StoredFileRecord | null>;
|
|
13
|
+
list(): Promise<ReadonlyArray<StoredFileMeta>>;
|
|
14
|
+
delete(fileId: string): Promise<boolean>;
|
|
15
|
+
stats(): Promise<unknown>;
|
|
16
|
+
cleanup(): Promise<unknown>;
|
|
17
|
+
private cleanupInternal;
|
|
18
|
+
private pickEvictions;
|
|
19
|
+
private listAllFiles;
|
|
20
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
const PREFIX = "file/";
|
|
2
|
+
const toId = (key) => key.startsWith(PREFIX) ? key.slice(PREFIX.length) : key;
|
|
3
|
+
const toKey = (id) => `${PREFIX}${id}`;
|
|
4
|
+
const parseCreatedAt = (value, fallback) => {
|
|
5
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
6
|
+
const ms = Date.parse(value);
|
|
7
|
+
if (Number.isFinite(ms))
|
|
8
|
+
return new Date(ms).toISOString();
|
|
9
|
+
}
|
|
10
|
+
return fallback.toISOString();
|
|
11
|
+
};
|
|
12
|
+
const isExpired = (createdAtIso, ttlHours) => {
|
|
13
|
+
const ms = Date.parse(createdAtIso);
|
|
14
|
+
if (!Number.isFinite(ms))
|
|
15
|
+
return false;
|
|
16
|
+
return Date.now() - ms > ttlHours * 60 * 60 * 1000;
|
|
17
|
+
};
|
|
18
|
+
export class R2FileStore {
|
|
19
|
+
bucket;
|
|
20
|
+
policy;
|
|
21
|
+
constructor(bucket, policy) {
|
|
22
|
+
this.bucket = bucket;
|
|
23
|
+
this.policy = policy;
|
|
24
|
+
}
|
|
25
|
+
async put(input) {
|
|
26
|
+
const sizeBytes = input.bytes.byteLength;
|
|
27
|
+
if (sizeBytes > this.policy.maxFileBytes) {
|
|
28
|
+
const err = new Error(`file too large: ${sizeBytes} bytes exceeds maxFileBytes ${this.policy.maxFileBytes}`);
|
|
29
|
+
err.status = 413;
|
|
30
|
+
err.code = "FILE_TOO_LARGE";
|
|
31
|
+
err.details = { policy: this.policy, sizeBytes };
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
await this.cleanupInternal(sizeBytes);
|
|
35
|
+
const id = crypto.randomUUID();
|
|
36
|
+
const createdAt = new Date().toISOString();
|
|
37
|
+
await this.bucket.put(toKey(id), input.bytes, {
|
|
38
|
+
httpMetadata: {
|
|
39
|
+
contentType: input.mimeType,
|
|
40
|
+
},
|
|
41
|
+
customMetadata: {
|
|
42
|
+
filename: input.filename,
|
|
43
|
+
mimeType: input.mimeType,
|
|
44
|
+
createdAt,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
return { id, filename: input.filename, mimeType: input.mimeType, sizeBytes, createdAt };
|
|
48
|
+
}
|
|
49
|
+
async get(fileId) {
|
|
50
|
+
const obj = await this.bucket.get(toKey(fileId));
|
|
51
|
+
if (!obj)
|
|
52
|
+
return null;
|
|
53
|
+
const meta = (obj.customMetadata ?? {});
|
|
54
|
+
const createdAt = parseCreatedAt(meta.createdAt, obj.uploaded);
|
|
55
|
+
const filename = meta.filename ?? fileId;
|
|
56
|
+
const mimeType = meta.mimeType ?? obj.httpMetadata?.contentType ?? "application/octet-stream";
|
|
57
|
+
const bytes = new Uint8Array(await obj.arrayBuffer());
|
|
58
|
+
return {
|
|
59
|
+
id: fileId,
|
|
60
|
+
filename,
|
|
61
|
+
mimeType,
|
|
62
|
+
sizeBytes: bytes.byteLength,
|
|
63
|
+
createdAt,
|
|
64
|
+
bytes,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async list() {
|
|
68
|
+
return await this.listAllFiles();
|
|
69
|
+
}
|
|
70
|
+
async delete(fileId) {
|
|
71
|
+
await this.bucket.delete(toKey(fileId));
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
async stats() {
|
|
75
|
+
const files = await this.listAllFiles();
|
|
76
|
+
const totalBytes = files.reduce((sum, file) => sum + file.sizeBytes, 0);
|
|
77
|
+
return {
|
|
78
|
+
backend: "r2",
|
|
79
|
+
policy: this.policy,
|
|
80
|
+
stats: {
|
|
81
|
+
fileCount: files.length,
|
|
82
|
+
totalBytes,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async cleanup() {
|
|
87
|
+
const files = await this.listAllFiles();
|
|
88
|
+
const expired = files.filter((f) => isExpired(f.createdAt, this.policy.ttlHours));
|
|
89
|
+
const active = files.filter((f) => !isExpired(f.createdAt, this.policy.ttlHours));
|
|
90
|
+
if (expired.length > 0) {
|
|
91
|
+
await this.bucket.delete(expired.map((f) => toKey(f.id)));
|
|
92
|
+
}
|
|
93
|
+
const evict = this.pickEvictions(active, 0);
|
|
94
|
+
if (evict.length > 0) {
|
|
95
|
+
await this.bucket.delete(evict.map((f) => toKey(f.id)));
|
|
96
|
+
}
|
|
97
|
+
const evictIds = new Set(evict.map((f) => f.id));
|
|
98
|
+
const after = active.filter((f) => !evictIds.has(f.id));
|
|
99
|
+
const totalBytes = after.reduce((sum, file) => sum + file.sizeBytes, 0);
|
|
100
|
+
return {
|
|
101
|
+
backend: "r2",
|
|
102
|
+
policy: this.policy,
|
|
103
|
+
deletedExpired: expired.length,
|
|
104
|
+
deletedEvicted: evict.length,
|
|
105
|
+
stats: {
|
|
106
|
+
fileCount: after.length,
|
|
107
|
+
totalBytes,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async cleanupInternal(incomingBytes) {
|
|
112
|
+
const files = await this.listAllFiles();
|
|
113
|
+
const expired = files.filter((f) => isExpired(f.createdAt, this.policy.ttlHours));
|
|
114
|
+
const active = files.filter((f) => !isExpired(f.createdAt, this.policy.ttlHours));
|
|
115
|
+
if (expired.length > 0) {
|
|
116
|
+
await this.bucket.delete(expired.map((f) => toKey(f.id)));
|
|
117
|
+
}
|
|
118
|
+
const evict = this.pickEvictions(active, incomingBytes);
|
|
119
|
+
if (evict.length > 0) {
|
|
120
|
+
await this.bucket.delete(evict.map((f) => toKey(f.id)));
|
|
121
|
+
}
|
|
122
|
+
const evictIds = new Set(evict.map((f) => f.id));
|
|
123
|
+
const remaining = active.filter((f) => !evictIds.has(f.id));
|
|
124
|
+
const finalTotal = remaining.reduce((sum, file) => sum + file.sizeBytes, 0);
|
|
125
|
+
if (finalTotal + incomingBytes > this.policy.maxTotalBytes) {
|
|
126
|
+
const err = new Error(`storage quota exceeded: total ${finalTotal} + incoming ${incomingBytes} > maxTotalBytes ${this.policy.maxTotalBytes}`);
|
|
127
|
+
err.status = 507;
|
|
128
|
+
err.code = "STORAGE_QUOTA_EXCEEDED";
|
|
129
|
+
err.details = { policy: this.policy, totalBytes: finalTotal, incomingBytes };
|
|
130
|
+
throw err;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
pickEvictions(files, incomingBytes) {
|
|
134
|
+
const totalBytes = files.reduce((sum, f) => sum + f.sizeBytes, 0);
|
|
135
|
+
const projected = totalBytes + incomingBytes;
|
|
136
|
+
if (projected <= this.policy.maxTotalBytes)
|
|
137
|
+
return [];
|
|
138
|
+
const needFree = projected - this.policy.maxTotalBytes;
|
|
139
|
+
const candidates = [...files].sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt));
|
|
140
|
+
const evict = [];
|
|
141
|
+
let freed = 0;
|
|
142
|
+
for (const file of candidates) {
|
|
143
|
+
evict.push(file);
|
|
144
|
+
freed += file.sizeBytes;
|
|
145
|
+
if (freed >= needFree)
|
|
146
|
+
break;
|
|
147
|
+
if (evict.length >= this.policy.cleanupBatchSize)
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
return evict;
|
|
151
|
+
}
|
|
152
|
+
async listAllFiles() {
|
|
153
|
+
const files = [];
|
|
154
|
+
let cursor;
|
|
155
|
+
while (true) {
|
|
156
|
+
const listed = await this.bucket.list({ prefix: PREFIX, limit: 1000, cursor });
|
|
157
|
+
for (const obj of listed.objects) {
|
|
158
|
+
const meta = (obj.customMetadata ?? {});
|
|
159
|
+
const createdAt = parseCreatedAt(meta.createdAt, obj.uploaded);
|
|
160
|
+
const filename = meta.filename ?? toId(obj.key);
|
|
161
|
+
const mimeType = meta.mimeType ?? obj.httpMetadata?.contentType ?? "application/octet-stream";
|
|
162
|
+
files.push({
|
|
163
|
+
id: toId(obj.key),
|
|
164
|
+
filename,
|
|
165
|
+
mimeType,
|
|
166
|
+
sizeBytes: obj.size,
|
|
167
|
+
createdAt,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (listed.truncated !== true || !listed.cursor)
|
|
171
|
+
break;
|
|
172
|
+
cursor = listed.cursor;
|
|
173
|
+
}
|
|
174
|
+
return files;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ToolArtifact {
|
|
2
|
+
readonly id?: string;
|
|
3
|
+
readonly kind: "image" | "pdf" | "file" | "json" | "text";
|
|
4
|
+
readonly mimeType?: string;
|
|
5
|
+
readonly filename?: string;
|
|
6
|
+
readonly sizeBytes?: number;
|
|
7
|
+
readonly url?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ToolOutputEnvelope {
|
|
10
|
+
readonly ok: true;
|
|
11
|
+
readonly data: unknown;
|
|
12
|
+
readonly artifacts: ToolArtifact[];
|
|
13
|
+
}
|
|
14
|
+
export declare const buildToolOutputEnvelope: (result: unknown, baseUrl: string) => ToolOutputEnvelope;
|
|
15
|
+
export declare const buildMcpContent: (envelope: ToolOutputEnvelope) => Array<Record<string, unknown>>;
|