import { Tool } from 'fastmcp';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import satori from 'satori';
import z from 'zod';
import { useLogger } from '../utils/logger';
import { DemoTemplate } from '../template/demo';
import sharp from 'sharp';
import { uploadImage } from '../utils/uploadImage';
const _DIRNAME = __dirname ? __dirname : resolve();
const name = '生成 SVG';
const description = '生成 SVG';
const parameters = z.object({});
const generateSVGDemo: Tool<any, z.ZodType<typeof parameters._type>> = {
name,
description,
parameters,
execute: async (_args, context) => {
const { log } = context;
const logger = useLogger(log);
const svg = await satori(<DemoTemplate />, {
width: 800,
fonts: [
{
name: 'Inter',
data: readFileSync(resolve(_DIRNAME, '../fonts/Inter-Medium.woff')),
weight: 400,
style: 'normal',
},
{
name: 'Inter',
data: readFileSync(resolve(_DIRNAME, '../fonts/Inter-Bold.woff')),
weight: 700,
style: 'normal',
},
],
});
logger.info('Generated SVG length: ' + svg.length);
const sharpJPG = await sharp(Buffer.from(svg)).jpeg();
const uuid = crypto.randomUUID();
const file = new File([await sharpJPG.toBuffer()], `${uuid}.jpg`, {
type: 'image/jpeg',
});
const result = await uploadImage(file);
return JSON.stringify(result, null, 2);
},
};
export { generateSVGDemo as demo };