|
| 1 | +import { AbstractImageProviderAdapter } from './abstract-adapter' |
| 2 | +import type { |
| 3 | + ImageProvider, |
| 4 | + ImageModel, |
| 5 | + ImageRequest, |
| 6 | + ImageResult, |
| 7 | + ImageModelConfig, |
| 8 | + ImageParameterDefinition |
| 9 | +} from '../types' |
| 10 | + |
| 11 | +/** |
| 12 | + * ModelScope (魔搭) 图像生成适配器 |
| 13 | + * |
| 14 | + * API 端点: https://api-inference.modelscope.cn/v1/images/generations |
| 15 | + * 免费额度: 每天 2000 次调用 |
| 16 | + * 文档: https://modelscope.cn/docs/model-service/API-Inference/intro |
| 17 | + * |
| 18 | + * 支持的模型: |
| 19 | + * - Tongyi-MAI/Z-Image-Turbo: 6B 参数高效图像生成模型(已验证可用) |
| 20 | + * - 其他模型请访问 ModelScope 文档查看当前支持列表 |
| 21 | + * - 可以通过 buildDefaultModel() 创建任意模型 ID 的配置进行测试 |
| 22 | + * |
| 23 | + * 环境变量支持: |
| 24 | + * - MODELSCOPE_API_KEY: SDK Token (Docker 环境,无 VITE_ 前缀) |
| 25 | + * - VITE_MODELSCOPE_API_KEY: SDK Token (开发环境,Vite 构建) |
| 26 | + */ |
| 27 | +export class ModelScopeImageAdapter extends AbstractImageProviderAdapter { |
| 28 | + protected normalizeBaseUrl(base: string): string { |
| 29 | + const trimmed = base.replace(/\/$/, '') |
| 30 | + // 确保 URL 以 /v1 结尾 |
| 31 | + return /\/v1$/.test(trimmed) ? trimmed : `${trimmed}/v1` |
| 32 | + } |
| 33 | + |
| 34 | + getProvider(): ImageProvider { |
| 35 | + return { |
| 36 | + id: 'modelscope', |
| 37 | + name: 'ModelScope', |
| 38 | + description: 'ModelScope 魔搭社区图像生成服务,每天免费 2000 次调用', |
| 39 | + requiresApiKey: true, |
| 40 | + defaultBaseURL: 'https://api-inference.modelscope.cn/v1', |
| 41 | + supportsDynamicModels: false, |
| 42 | + connectionSchema: { |
| 43 | + required: ['apiKey'], |
| 44 | + optional: ['baseURL'], |
| 45 | + fieldTypes: { |
| 46 | + apiKey: 'string', |
| 47 | + baseURL: 'string' |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + getModels(): ImageModel[] { |
| 54 | + return [ |
| 55 | + { |
| 56 | + id: 'Tongyi-MAI/Z-Image-Turbo', |
| 57 | + name: 'Z-Image-Turbo', |
| 58 | + description: 'Z-Image-Turbo 6B 参数高效图像生成模型,擅长人像生成和快速出图(10步以内)', |
| 59 | + providerId: 'modelscope', |
| 60 | + capabilities: { |
| 61 | + text2image: true, |
| 62 | + image2image: false, |
| 63 | + multiImage: false |
| 64 | + }, |
| 65 | + parameterDefinitions: this.getDefaultParameterDefinitions(), |
| 66 | + defaultParameterValues: { |
| 67 | + size: '1024x1024', |
| 68 | + n: 1 |
| 69 | + } |
| 70 | + } |
| 71 | + ] |
| 72 | + } |
| 73 | + |
| 74 | + private getDefaultParameterDefinitions(): ImageParameterDefinition[] { |
| 75 | + return [ |
| 76 | + { |
| 77 | + name: 'size', |
| 78 | + labelKey: 'image.params.size.label', |
| 79 | + descriptionKey: 'image.params.size.description', |
| 80 | + type: 'string', |
| 81 | + defaultValue: '1024x1024', |
| 82 | + allowedValues: ['1024x1024', '1536x1024', '1024x1536'] |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'n', |
| 86 | + labelKey: 'image.params.count.label', |
| 87 | + descriptionKey: 'image.params.count.description', |
| 88 | + type: 'integer', |
| 89 | + defaultValue: 1, |
| 90 | + minValue: 1, |
| 91 | + maxValue: 4 |
| 92 | + } |
| 93 | + ] |
| 94 | + } |
| 95 | + |
| 96 | + protected getTestImageRequest(testType: 'text2image' | 'image2image'): Omit<ImageRequest, 'configId'> { |
| 97 | + if (testType === 'text2image') { |
| 98 | + return { |
| 99 | + prompt: '一朵简单的红色花朵', |
| 100 | + count: 1 |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + throw new Error(`Test type ${testType} not supported by ModelScope image adapter`) |
| 105 | + } |
| 106 | + |
| 107 | + protected getParameterDefinitions(_modelId: string): readonly ImageParameterDefinition[] { |
| 108 | + return this.getDefaultParameterDefinitions() |
| 109 | + } |
| 110 | + |
| 111 | + protected getDefaultParameterValues(_modelId: string): Record<string, unknown> { |
| 112 | + return { |
| 113 | + size: '1024x1024', |
| 114 | + n: 1 |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + protected async doGenerate(request: ImageRequest, config: ImageModelConfig): Promise<ImageResult> { |
| 119 | + // ModelScope 适配器仅支持文生图 |
| 120 | + if (request.inputImage) { |
| 121 | + throw new Error('ModelScope adapter only supports text-to-image generation. For image editing, please use DashScope adapter.') |
| 122 | + } |
| 123 | + |
| 124 | + return await this.generateImage(request, config) |
| 125 | + } |
| 126 | + |
| 127 | + private async generateImage(request: ImageRequest, config: ImageModelConfig): Promise<ImageResult> { |
| 128 | + const url = this.resolveEndpointUrl(config, '/images/generations') |
| 129 | + |
| 130 | + const merged: Record<string, any> = { |
| 131 | + ...config.paramOverrides, |
| 132 | + ...request.paramOverrides |
| 133 | + } |
| 134 | + |
| 135 | + const payload = { |
| 136 | + model: config.modelId, |
| 137 | + prompt: request.prompt, |
| 138 | + size: merged.size || '1024x1024', |
| 139 | + n: merged.n || request.count || 1 |
| 140 | + } |
| 141 | + |
| 142 | + // 提交异步任务 |
| 143 | + const response = await fetch(url, { |
| 144 | + method: 'POST', |
| 145 | + headers: { |
| 146 | + 'Authorization': `Bearer ${config.connectionConfig?.apiKey}`, |
| 147 | + 'Content-Type': 'application/json', |
| 148 | + 'X-ModelScope-Async-Mode': 'true' // 异步模式 |
| 149 | + }, |
| 150 | + body: JSON.stringify(payload) |
| 151 | + }) |
| 152 | + |
| 153 | + if (!response.ok) { |
| 154 | + let errorMessage = `ModelScope API error: ${response.status} ${response.statusText}` |
| 155 | + try { |
| 156 | + const errorData = await response.json() |
| 157 | + if (errorData.message || errorData.error?.message) { |
| 158 | + errorMessage = errorData.message || errorData.error.message |
| 159 | + } |
| 160 | + } catch { |
| 161 | + // 忽略 JSON 解析错误 |
| 162 | + } |
| 163 | + throw new Error(errorMessage) |
| 164 | + } |
| 165 | + |
| 166 | + const submitData = await response.json() |
| 167 | + const taskId = submitData.task_id |
| 168 | + |
| 169 | + if (!taskId) { |
| 170 | + throw new Error('No task_id received from ModelScope API') |
| 171 | + } |
| 172 | + |
| 173 | + // 轮询任务状态 |
| 174 | + return await this.pollTaskResult(taskId, config, 120, 3000) |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * 轮询任务结果 |
| 179 | + */ |
| 180 | + private async pollTaskResult( |
| 181 | + taskId: string, |
| 182 | + config: ImageModelConfig, |
| 183 | + maxAttempts: number = 60, |
| 184 | + intervalMs: number = 2000 |
| 185 | + ): Promise<ImageResult> { |
| 186 | + const taskUrl = this.resolveEndpointUrl(config, `/tasks/${taskId}`) |
| 187 | + |
| 188 | + for (let attempt = 0; attempt < maxAttempts; attempt++) { |
| 189 | + await new Promise(resolve => setTimeout(resolve, intervalMs)) |
| 190 | + |
| 191 | + const response = await fetch(taskUrl, { |
| 192 | + method: 'GET', |
| 193 | + headers: { |
| 194 | + 'Authorization': `Bearer ${config.connectionConfig?.apiKey}`, |
| 195 | + 'X-ModelScope-Task-Type': 'image_generation' |
| 196 | + } |
| 197 | + }) |
| 198 | + |
| 199 | + if (!response.ok) { |
| 200 | + // 尝试解析错误响应体以提供更详细的错误信息 |
| 201 | + let errorMessage = `${response.status} ${response.statusText}` |
| 202 | + try { |
| 203 | + const errorData = await response.json() |
| 204 | + if (errorData.error || errorData.message) { |
| 205 | + errorMessage = errorData.error || errorData.message |
| 206 | + } |
| 207 | + } catch { |
| 208 | + // 如果无法解析 JSON,使用默认错误信息 |
| 209 | + } |
| 210 | + throw new Error(`Failed to poll task status: ${errorMessage}`) |
| 211 | + } |
| 212 | + |
| 213 | + const data = await response.json() |
| 214 | + const status = data.task_status |
| 215 | + |
| 216 | + if (status === 'SUCCEED') { |
| 217 | + // 任务成功,解析结果 |
| 218 | + const outputImages = data.output_images || [] |
| 219 | + if (outputImages.length === 0) { |
| 220 | + throw new Error('No output images in task result') |
| 221 | + } |
| 222 | + |
| 223 | + const images = outputImages.map((imageUrl: string) => ({ |
| 224 | + url: imageUrl, |
| 225 | + mimeType: 'image/png' |
| 226 | + })) |
| 227 | + |
| 228 | + return { |
| 229 | + images, |
| 230 | + metadata: { |
| 231 | + providerId: 'modelscope', |
| 232 | + modelId: config.modelId, |
| 233 | + configId: config.id, |
| 234 | + taskId |
| 235 | + } |
| 236 | + } |
| 237 | + } else if (status === 'FAILED' || status === 'ERROR' || status === 'CANCELLED' || status === 'CANCELED') { |
| 238 | + // 任务失败或被取消,提取错误信息 |
| 239 | + const errorMessage = data.error?.message || data.error || data.message || 'Unknown error' |
| 240 | + throw new Error(`Task ${status.toLowerCase()}: ${errorMessage}`) |
| 241 | + } else if (status !== 'PENDING' && status !== 'RUNNING') { |
| 242 | + // 未知的终态,视为失败 |
| 243 | + throw new Error(`Unknown task status: ${status}`) |
| 244 | + } |
| 245 | + // task_status 为 PENDING 或 RUNNING,继续轮询 |
| 246 | + } |
| 247 | + |
| 248 | + throw new Error(`Task timeout after ${maxAttempts} attempts`) |
| 249 | + } |
| 250 | + |
| 251 | +} |
0 commit comments