UNPKG

cloudflare-image-mcp

Version:
83 lines 3.16 kB
import fetch from 'node-fetch'; export class CloudflareClient { config; constructor(config) { this.config = config; } async generateImage(modelName, payload, timeoutMs = 60000) { const url = `https://api.cloudflare.com/client/v4/accounts/${this.config.cloudflareAccountId}/ai/run/${modelName}`; const headers = { 'Authorization': `Bearer ${this.config.cloudflareApiToken}`, 'Content-Type': 'application/json', }; try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeoutMs); const response = await fetch(url, { method: 'POST', headers, body: JSON.stringify(payload), signal: controller.signal, }); clearTimeout(timeoutId); if (!response.ok) { const errorText = await response.text(); return { success: false, error: `API Error ${response.status}: ${errorText}` }; } const contentType = response.headers.get('content-type') || ''; if (contentType.includes('application/json')) { // JSON response (FLUX and Lucid Origin) const jsonResponse = await response.json(); if (!jsonResponse.success) { return { success: false, error: jsonResponse.errors?.[0]?.message || 'Unknown API error' }; } const imageData = jsonResponse.result?.image || jsonResponse.image; if (!imageData) { return { success: false, error: 'No image data found in response' }; } return { success: true, data: imageData, contentType: 'application/json' }; } else { // Binary response (SDXL models) const buffer = await response.buffer(); if (buffer.length < 100) { return { success: false, error: 'Invalid binary image data received' }; } return { success: true, data: buffer, contentType: contentType }; } } catch (error) { if (error instanceof Error && error.name === 'AbortError') { return { success: false, error: 'Request timed out. The image generation is taking longer than expected.' }; } return { success: false, error: `Unexpected error: ${error instanceof Error ? error.message : String(error)}` }; } } } //# sourceMappingURL=cloudflare-client.js.map