| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import type { |
| ImageGenerationConfig, |
| ImageGenerationOptions, |
| ImageGenerationResult, |
| } from '../types'; |
|
|
| const DEFAULT_MODEL = 'doubao-seedream-5-0-260128'; |
| const DEFAULT_BASE_URL = 'https://ark.cn-beijing.volces.com'; |
|
|
| |
| |
| |
| |
| |
| function resolveSeedreamSize(options: ImageGenerationOptions): string { |
| if (options.width && options.height) { |
| |
| const pixels = options.width * options.height; |
| if (pixels < 3_686_400) { |
| |
| const scale = Math.ceil(Math.sqrt(3_686_400 / pixels)); |
| return `${options.width * scale}x${options.height * scale}`; |
| } |
| return `${options.width}x${options.height}`; |
| } |
| |
| return '2K'; |
| } |
|
|
| |
| |
| |
| |
| export async function testSeedreamConnectivity( |
| config: ImageGenerationConfig, |
| ): Promise<{ success: boolean; message: string }> { |
| const baseUrl = config.baseUrl || DEFAULT_BASE_URL; |
| try { |
| |
| |
| const response = await fetch(`${baseUrl}/api/v3/images/generations`, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| Authorization: `Bearer ${config.apiKey}`, |
| }, |
| body: JSON.stringify({ |
| model: config.model || DEFAULT_MODEL, |
| prompt: '', |
| size: '1x1', |
| }), |
| }); |
| if (response.status === 401 || response.status === 403) { |
| const text = await response.text(); |
| return { |
| success: false, |
| message: `Seedream auth failed (${response.status}): ${text}`, |
| }; |
| } |
| return { success: true, message: 'Connected to Seedream' }; |
| } catch (err) { |
| return { success: false, message: `Seedream connectivity error: ${err}` }; |
| } |
| } |
|
|
| export async function generateWithSeedream( |
| config: ImageGenerationConfig, |
| options: ImageGenerationOptions, |
| ): Promise<ImageGenerationResult> { |
| const baseUrl = config.baseUrl || DEFAULT_BASE_URL; |
|
|
| const response = await fetch(`${baseUrl}/api/v3/images/generations`, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| Authorization: `Bearer ${config.apiKey}`, |
| }, |
| body: JSON.stringify({ |
| model: config.model || DEFAULT_MODEL, |
| prompt: options.prompt, |
| size: resolveSeedreamSize(options), |
| watermark: false, |
| }), |
| }); |
|
|
| if (!response.ok) { |
| const text = await response.text(); |
| throw new Error(`Seedream generation failed (${response.status}): ${text}`); |
| } |
|
|
| const data = await response.json(); |
|
|
| |
| const imageData = data.data?.[0]; |
| if (!imageData) { |
| throw new Error('Seedream returned empty response'); |
| } |
|
|
| return { |
| url: imageData.url, |
| base64: imageData.b64_json, |
| width: options.width || 1024, |
| height: options.height || 1024, |
| }; |
| } |
|
|