| 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 createImageModelVerificationSignature = ( |
| 36 | provider: ImageModelProvider, |
| 37 | modelConfig: string |
| 38 | ): string => JSON.stringify({ provider, modelConfig }) |
| 39 | |
| 40 | export const isImageModelVerificationCurrent = ( |
| 41 | verifiedSignature: string | null, |
| 42 | provider: ImageModelProvider, |
| 43 | modelConfig: string | null |
| 44 | ): boolean => { |
| 45 | if (!modelConfig) return false |
| 46 | return verifiedSignature === createImageModelVerificationSignature(provider, modelConfig) |
| 47 | } |
| 48 | |
| 49 | export const summarizeImageModelConfig = (value: string): string => { |
| 50 | const config = readJsonObject(value) |
| 51 | if (!config) return 'model_config' |
| 52 | const model = typeof config.model === 'string' ? config.model.trim() : '' |
| 53 | const reqKey = typeof config.reqKey === 'string' ? config.reqKey.trim() : '' |
| 54 | const endpoint = |
| 55 | typeof config.endpoint === 'string' |
| 56 | ? config.endpoint.trim() |
| 57 | : typeof config.baseUrl === 'string' |
| 58 | ? config.baseUrl.trim() |
| 59 | : '' |
| 60 | return [model || reqKey || 'model_config', endpoint].filter(Boolean).join(' · ') |
| 61 | } |
| 62 | |
| 63 | export const createDefaultImageModelConfig = (provider: ImageModelProvider): string => { |
| 64 | if (provider === 'jimeng') { |
| 65 | return stringifyJsonObject({ |
| 66 | reqKey: JIMENG_DEFAULT_REQ_KEY, |
| 67 | accessKeyId: '', |
| 68 | secretKey: '' |
| 69 | }) |
| 70 | } |
| 71 | if (provider === 'jimeng4') { |
| 72 | return stringifyJsonObject({ |
| 73 | reqKey: JIMENG_V4_DEFAULT_REQ_KEY, |
| 74 | accessKeyId: '', |
| 75 | secretKey: '', |
| 76 | force_single: true |
| 77 | }) |
| 78 | } |
| 79 | if (provider === 'siliconflow') { |
| 80 | return stringifyJsonObject({ |
| 81 | model: 'Tongyi-MAI/Z-Image-Turbo', |
| 82 | apiKey: '' |
| 83 | }) |
| 84 | } |
| 85 | if (provider === 'openaiCompatible') { |
| 86 | return stringifyJsonObject({ |
| 87 | baseUrl: 'https://api.openai.com', |
| 88 | apiKey: '', |
| 89 | model: 'gpt-image-1' |
| 90 | }) |
| 91 | } |
| 92 | if (provider === 'gemini') { |
| 93 | return stringifyJsonObject({ |
| 94 | model: 'gemini-3.1-flash-image', |
| 95 | apiKey: '' |
| 96 | }) |
| 97 | } |
| 98 | if (provider === 'seedream') { |
| 99 | return stringifyJsonObject({ |
| 100 | baseUrl: 'https://ark.cn-beijing.volces.com', |
| 101 | model: 'doubao-seedream-5-0-260128', |
| 102 | apiKey: '', |
| 103 | response_format: 'url', |
| 104 | sizes: ['2K'], |
| 105 | sequential_image_generation: 'disabled', |
| 106 | stream: false |
| 107 | }) |
| 108 | } |
| 109 | return stringifyJsonObject({ |
| 110 | model: 'agnes-image-2.0-flash', |
| 111 | apiKey: '', |
| 112 | responseFormat: 'url' |
| 113 | }) |
| 114 | } |
| 115 | |
| 116 | export const createEmptyModelForm = (active = false): ModelForm => ({ |
| 117 | name: '', |
| 118 | provider: 'openai', |
| 119 | model: '', |
| 120 | apiKey: '', |
| 121 | baseUrl: '', |
| 122 | maxTokens: '4096', |
| 123 | disableTemperature: false, |
| 124 | thinkingParameterMode: DEFAULT_THINKING_PARAMETER_MODE, |
| 125 | active |
| 126 | }) |
| 127 | |
| 128 | export const createModelForm = (config: ModelConfig): ModelForm => ({ |
| 129 | id: config.id, |
| 130 | name: config.name, |
| 131 | provider: config.provider, |
| 132 | model: config.model, |
| 133 | apiKey: config.apiKey, |
| 134 | baseUrl: config.baseUrl, |
| 135 | maxTokens: String(config.maxTokens || 4096), |
| 136 | disableTemperature: config.disableTemperature, |
| 137 | thinkingParameterMode: config.thinkingParameterMode || DEFAULT_THINKING_PARAMETER_MODE, |
| 138 | active: config.active |
| 139 | }) |
| 140 | |
| 141 | export const createEmptyImageModelForm = (active = false): ImageModelForm => ({ |
| 142 | name: '', |
| 143 | provider: 'agnes', |
| 144 | modelConfig: createDefaultImageModelConfig('agnes'), |
| 145 | active |
| 146 | }) |
| 147 | |
| 148 | export const createImageModelForm = (config: ImageModelConfig): ImageModelForm => { |
| 149 | return { |
| 150 | id: config.id, |
| 151 | name: config.name, |
| 152 | provider: config.provider, |
| 153 | modelConfig: config.modelConfig || createDefaultImageModelConfig(config.provider), |
| 154 | active: config.active |
| 155 | } |
| 156 | } |
| 157 |