UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

68 lines 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createImageGenAndPinTool = createImageGenAndPinTool; const client_js_1 = require("../client.js"); const zod_1 = require("zod"); /** * Uses Fireworks AI Stable Diffusion XL, * then pins the result to IPFS via Pinata. */ function createImageGenAndPinTool({ fireworksApiKey, // <-- NEW pinataJWT, model = "accounts/fireworks/models/stable-diffusion-xl-1024-v1-0", // default SDXL }) { return (0, client_js_1.createTool)({ name: "generateAndPinImage", description: `Generate an image from text with Fireworks AI (${model}), then upload ` + "it to IPFS via Pinata. Returns the IPFS CID and link.", parameters: zod_1.z.object({ prompt: zod_1.z.string().describe("A detailed prompt for image generation"), negativePrompt: zod_1.z.string().optional(), steps: zod_1.z.number().int().min(1).max(50).default(30).optional(), }), execute: async (_client, args) => { const { prompt, negativePrompt, steps } = args; /* 1 ▶ Generate the image (binary) */ const fwEndpoint = `https://api.fireworks.ai/inference/v1/image_generation/${model}`; const fwRes = await fetch(fwEndpoint, { method: "POST", headers: { Authorization: `Bearer ${fireworksApiKey}`, "Content-Type": "application/json", Accept: "image/png", // binary response → easier piping }, body: JSON.stringify({ prompt, negative_prompt: negativePrompt, width: 1024, height: 1024, steps, // default 30 safety_check: true, }), }); if (!fwRes.ok) { const detail = await fwRes.text().catch(() => fwRes.statusText); throw new Error(`Fireworks error ${fwRes.status}: ${detail}`); } const imageBuffer = await fwRes.arrayBuffer(); /* 2 ▶ Pin to IPFS via Pinata */ const formData = new FormData(); formData.append("file", new Blob([imageBuffer]), "image.png"); formData.append("pinataMetadata", JSON.stringify({ name: "AI Generated Image", keyvalues: { prompt } })); const pinRes = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", { method: "POST", headers: { Authorization: `Bearer ${pinataJWT}` }, body: formData, }); const pinJson = await pinRes.json(); if (!pinJson?.IpfsHash) { throw new Error(`Failed to pin image to IPFS: ${pinJson?.error || ""}`); } return { prompt, ipfsCid: pinJson.IpfsHash, ipfsUrl: `https://content.wrappr.wtf/ipfs/${pinJson.IpfsHash}`, }; }, }); } //# sourceMappingURL=tools.js.map