refactor: rename Tool.alpha to Tool.experimental

This commit is contained in:
Siddharth Kumar Sah
2026-03-26 01:10:51 +08:00
parent ab370a74fe
commit 585d66f0c9
178 changed files with 5637 additions and 4082 deletions
+116 -121
View File
@@ -1,10 +1,10 @@
import { z } from "zod";
import sharp from "sharp";
import PDFDocument from "pdfkit";
import type { FastifyInstance } from "fastify";
import { randomUUID } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { join, basename } from "node:path";
import { basename, join } from "node:path";
import type { FastifyInstance } from "fastify";
import PDFDocument from "pdfkit";
import sharp from "sharp";
import { z } from "zod";
import { createWorkspace } from "../../lib/workspace.js";
const settingsSchema = z.object({
@@ -21,128 +21,123 @@ const PAGE_SIZES: Record<string, [number, number]> = {
};
export function registerImageToPdf(app: FastifyInstance) {
app.post(
"/api/v1/tools/image-to-pdf",
async (request, reply) => {
const files: Array<{ buffer: Buffer; filename: string }> = [];
let settingsRaw: string | null = null;
app.post("/api/v1/tools/image-to-pdf", async (request, reply) => {
const files: Array<{ buffer: Buffer; filename: string }> = [];
let settingsRaw: string | null = null;
try {
const parts = request.parts();
for await (const part of parts) {
if (part.type === "file") {
const chunks: Buffer[] = [];
for await (const chunk of part.file) {
chunks.push(chunk);
}
const buf = Buffer.concat(chunks);
if (buf.length > 0) {
files.push({
buffer: buf,
filename: basename(part.filename ?? `image-${files.length}`),
});
}
} else if (part.fieldname === "settings") {
settingsRaw = part.value as string;
try {
const parts = request.parts();
for await (const part of parts) {
if (part.type === "file") {
const chunks: Buffer[] = [];
for await (const chunk of part.file) {
chunks.push(chunk);
}
const buf = Buffer.concat(chunks);
if (buf.length > 0) {
files.push({
buffer: buf,
filename: basename(part.filename ?? `image-${files.length}`),
});
}
} else if (part.fieldname === "settings") {
settingsRaw = part.value as string;
}
} catch (err) {
return reply.status(400).send({
error: "Failed to parse multipart request",
details: err instanceof Error ? err.message : String(err),
}
} catch (err) {
return reply.status(400).send({
error: "Failed to parse multipart request",
details: err instanceof Error ? err.message : String(err),
});
}
if (files.length === 0) {
return reply.status(400).send({ error: "No image files provided" });
}
let settings: z.infer<typeof settingsSchema>;
try {
const parsed = settingsRaw ? JSON.parse(settingsRaw) : {};
const result = settingsSchema.safeParse(parsed);
if (!result.success) {
return reply.status(400).send({ error: "Invalid settings", details: result.error.issues });
}
settings = result.data;
} catch {
return reply.status(400).send({ error: "Settings must be valid JSON" });
}
try {
let [pageW, pageH] = PAGE_SIZES[settings.pageSize] ?? PAGE_SIZES.A4;
if (settings.orientation === "landscape") {
[pageW, pageH] = [pageH, pageW];
}
const margin = settings.margin;
const contentW = pageW - margin * 2;
const contentH = pageH - margin * 2;
// Create PDF
const doc = new PDFDocument({
size: [pageW, pageH],
margin,
autoFirstPage: false,
});
const pdfChunks: Buffer[] = [];
doc.on("data", (chunk: Buffer) => pdfChunks.push(chunk));
const pdfDone = new Promise<Buffer>((resolve) => {
doc.on("end", () => resolve(Buffer.concat(pdfChunks)));
});
for (const file of files) {
doc.addPage({ size: [pageW, pageH], margin });
// Convert to PNG for PDFKit compatibility
const pngBuffer = await sharp(file.buffer).png().toBuffer();
const meta = await sharp(pngBuffer).metadata();
const imgW = meta.width ?? 100;
const imgH = meta.height ?? 100;
// Scale to fit within content area
const scale = Math.min(contentW / imgW, contentH / imgH, 1);
const scaledW = imgW * scale;
const scaledH = imgH * scale;
// Center on page
const x = margin + (contentW - scaledW) / 2;
const y = margin + (contentH - scaledH) / 2;
doc.image(pngBuffer, x, y, {
width: scaledW,
height: scaledH,
});
}
if (files.length === 0) {
return reply.status(400).send({ error: "No image files provided" });
}
doc.end();
const pdfBuffer = await pdfDone;
let settings: z.infer<typeof settingsSchema>;
try {
const parsed = settingsRaw ? JSON.parse(settingsRaw) : {};
const result = settingsSchema.safeParse(parsed);
if (!result.success) {
return reply.status(400).send({ error: "Invalid settings", details: result.error.issues });
}
settings = result.data;
} catch {
return reply.status(400).send({ error: "Settings must be valid JSON" });
}
const jobId = randomUUID();
const workspacePath = await createWorkspace(jobId);
const filename = "images.pdf";
const outputPath = join(workspacePath, "output", filename);
await writeFile(outputPath, pdfBuffer);
try {
let [pageW, pageH] = PAGE_SIZES[settings.pageSize] ?? PAGE_SIZES.A4;
if (settings.orientation === "landscape") {
[pageW, pageH] = [pageH, pageW];
}
const margin = settings.margin;
const contentW = pageW - margin * 2;
const contentH = pageH - margin * 2;
// Create PDF
const doc = new PDFDocument({
size: [pageW, pageH],
margin,
autoFirstPage: false,
});
const pdfChunks: Buffer[] = [];
doc.on("data", (chunk: Buffer) => pdfChunks.push(chunk));
const pdfDone = new Promise<Buffer>((resolve) => {
doc.on("end", () => resolve(Buffer.concat(pdfChunks)));
});
for (const file of files) {
doc.addPage({ size: [pageW, pageH], margin });
// Convert to PNG for PDFKit compatibility
const pngBuffer = await sharp(file.buffer)
.png()
.toBuffer();
const meta = await sharp(pngBuffer).metadata();
const imgW = meta.width ?? 100;
const imgH = meta.height ?? 100;
// Scale to fit within content area
const scale = Math.min(contentW / imgW, contentH / imgH, 1);
const scaledW = imgW * scale;
const scaledH = imgH * scale;
// Center on page
const x = margin + (contentW - scaledW) / 2;
const y = margin + (contentH - scaledH) / 2;
doc.image(pngBuffer, x, y, {
width: scaledW,
height: scaledH,
});
}
doc.end();
const pdfBuffer = await pdfDone;
const jobId = randomUUID();
const workspacePath = await createWorkspace(jobId);
const filename = "images.pdf";
const outputPath = join(workspacePath, "output", filename);
await writeFile(outputPath, pdfBuffer);
return reply.send({
jobId,
downloadUrl: `/api/v1/download/${jobId}/${filename}`,
originalSize: files.reduce((s, f) => s + f.buffer.length, 0),
processedSize: pdfBuffer.length,
pages: files.length,
});
} catch (err) {
return reply.status(422).send({
error: "PDF creation failed",
details: err instanceof Error ? err.message : "Unknown error",
});
}
},
);
return reply.send({
jobId,
downloadUrl: `/api/v1/download/${jobId}/${filename}`,
originalSize: files.reduce((s, f) => s + f.buffer.length, 0),
processedSize: pdfBuffer.length,
pages: files.length,
});
} catch (err) {
return reply.status(422).send({
error: "PDF creation failed",
details: err instanceof Error ? err.message : "Unknown error",
});
}
});
}