| 1 | import type { ImageModelConfig, ImageModelProvider, ModelConfig } from '../../lib/ipc' |
| 2 | import type { ImageModelForm, ModelForm } from './types' |
| 3 | import { DEFAULT_THINKING_PARAMETER_MODE } from '@shared/model-config.js' |
| 4 | |
| 5 | export const IMAGE_PROVIDER_OPTIONS: Array<{ value: ImageModelProvider; label: string }> = [ |
| 6 | { value: 'agnes', label: 'Agnes AI' }, |
| 7 | { value: 'jimeng', label: '即梦3.0' }, |
| 8 | { value: 'jimeng4', label: '即梦4.0' }, |
| 9 | { value: 'seedream', label: 'Seedream' }, |
| 10 | { value: 'siliconflow', label: '硅基流动' }, |
| 11 | { value: 'openaiCompatible', label: 'OpenAI 兼容' }, |
| 12 | { value: 'gemini', label: 'Gemini' } |
| 13 | ] |
| 14 | |
| 15 | const JIMENG_DEFAULT_REQ_KEY = 'jimeng_t2i_v30' |
| 16 | const JIMENG_V4_DEFAULT_REQ_KEY = 'jimeng_t2i_v40' |
| 17 | |
| 18 | export const readJsonObject = (value: string): Record<string, unknown> | null => { |
| 19 | const text = value.trim() |
| 20 | if (!text) return null |
| 21 | try { |
| 22 | const parsed = JSON.parse(text) |
| 23 | return parsed && typeof parsed === 'object' && !Array.isArray(parsed) |
| 24 | ? (parsed as Record<string, unknown>) |
| 25 | : null |
| 26 | } catch { |
| 27 | return null |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export const stringifyJsonObject = (record: Record<string, unknown>): string => { |
| 32 | return JSON.stringify(record, null, 2) |
| 33 | } |
| 34 | |
| 35 | export const summarizeImageModelConfig = (value: string): string => { |
| 36 | const config = readJsonObject(value) |
| 37 | if (!config) return 'model_config' |
| 38 | const model = typeof config.model === 'string' ? config.model.trim() : '' |
| 39 | const reqKey = typeof config.reqKey === 'string' ? config.reqKey.trim() : '' |
| 40 | const endpoint = |
| 41 | typeof config.endpoint === 'string' |
| 42 | ? config.endpoint.trim() |
| 43 | : typeof config.baseUrl === 'string' |
| 44 | ? config.baseUrl.trim() |
| 45 | : '' |
| 46 | return [model || reqKey || 'model_config', endpoint].filter(Boolean).join(' · ') |
| 47 | } |
| 48 | |
| 49 | export const createDefaultImageModelConfig = (provider: ImageModelProvider): string => { |
| 50 | if (provider === 'jimeng') { |
| 51 | return stringifyJsonObject({ |
| 52 | reqKey: JIMENG_DEFAULT_REQ_KEY, |
| 53 | accessKeyId: '', |
| 54 | secretKey: '' |
| 55 | }) |
| 56 | } |
| 57 | if (provider === 'jimeng4') { |
| 58 | return stringifyJsonObject({ |
| 59 | reqKey: JIMENG_V4_DEFAULT_REQ_KEY, |
| 60 | accessKeyId: '', |
| 61 | secretKey: '', |
| 62 | force_single: true |
| 63 | }) |
| 64 | } |
| 65 | if (provider === 'siliconflow') { |
| 66 | return stringifyJsonObject({ |
| 67 | model: 'Tongyi-MAI/Z-Image-Turbo', |
| 68 | apiKey: '' |
| 69 | }) |
| 70 | } |
| 71 | if (provider === 'openaiCompatible') { |
| 72 | return stringifyJsonObject({ |
| 73 | baseUrl: 'https://api.openai.com', |
| 74 | apiKey: '', |
| 75 | model: 'gpt-image-1' |
| 76 | }) |
| 77 | } |
| 78 | if (provider === 'gemini') { |
| 79 | return stringifyJsonObject({ |
| 80 | model: 'gemini-3.1-flash-image', |
| 81 | apiKey: '' |
| 82 | }) |
| 83 | } |
| 84 | if (provider === 'seedream') { |
| 85 | return stringifyJsonObject({ |
| 86 | baseUrl: 'https://ark.cn-beijing.volces.com', |
| 87 | model: 'doubao-seedream-5-0-260128', |
| 88 | apiKey: '', |
| 89 | response_format: 'url', |
| 90 | sizes: ['2K'], |
| 91 | sequential_image_generation: 'disabled', |
| 92 | stream: false |
| 93 | }) |
| 94 | } |
| 95 | return stringifyJsonObject({ |
| 96 | model: 'agnes-image-2.0-flash', |
| 97 | apiKey: '', |
| 98 | responseFormat: 'url' |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | export const createEmptyModelForm = (active = false): ModelForm => ({ |
| 103 | name: '', |
| 104 | provider: 'openai', |
| 105 | model: '', |
| 106 | apiKey: '', |
| 107 | baseUrl: '', |
| 108 | maxTokens: '4096', |
| 109 | disableTemperature: false, |
| 110 | thinkingParameterMode: DEFAULT_THINKING_PARAMETER_MODE, |
| 111 | active |
| 112 | }) |
| 113 | |
| 114 | export const createModelForm = (config: ModelConfig): ModelForm => ({ |
| 115 | id: config.id, |
| 116 | name: config.name, |
| 117 | provider: config.provider, |
| 118 | model: config.model, |
| 119 | apiKey: config.apiKey, |
| 120 | baseUrl: config.baseUrl, |
| 121 | maxTokens: String(config.maxTokens || 4096), |
| 122 | disableTemperature: config.disableTemperature, |
| 123 | thinkingParameterMode: config.thinkingParameterMode || DEFAULT_THINKING_PARAMETER_MODE, |
| 124 | active: config.active |
| 125 | }) |
| 126 | |
| 127 | export const createEmptyImageModelForm = (active = false): ImageModelForm => ({ |
| 128 | name: '', |
| 129 | provider: 'agnes', |
| 130 | modelConfig: createDefaultImageModelConfig('agnes'), |
| 131 | active |
| 132 | }) |
| 133 | |
| 134 | export const createImageModelForm = (config: ImageModelConfig): ImageModelForm => { |
| 135 | return { |
| 136 | id: config.id, |
| 137 | name: config.name, |
| 138 | provider: config.provider, |
| 139 | modelConfig: config.modelConfig || createDefaultImageModelConfig(config.provider), |
| 140 | active: config.active |
| 141 | } |
| 142 | } |
| 143 |