File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed
src/services/image/adapters Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change 1- import { GoogleGenAI , setDefaultBaseUrls } from '@google/genai'
1+ import { GoogleGenAI } from '@google/genai'
22import { AbstractImageProviderAdapter } from './abstract-adapter'
33import type {
44 ImageProvider ,
@@ -19,8 +19,11 @@ export class GeminiImageAdapter extends AbstractImageProviderAdapter {
1919 supportsDynamicModels : false ,
2020 connectionSchema : {
2121 required : [ 'apiKey' ] ,
22- optional : [ ] ,
23- fieldTypes : { apiKey : 'string' }
22+ optional : [ 'baseURL' ] ,
23+ fieldTypes : {
24+ apiKey : 'string' ,
25+ baseURL : 'string'
26+ }
2427 }
2528 }
2629 }
@@ -79,14 +82,17 @@ export class GeminiImageAdapter extends AbstractImageProviderAdapter {
7982 }
8083
8184 protected async doGenerate ( request : ImageRequest , config : ImageModelConfig ) : Promise < ImageResult > {
82- // 使用官方 GoogleGenAI SDK,按需设置代理后的基础地址(geminiUrl)
83- const finalBase = this . resolveBaseUrl ( config , /*isStream*/ false )
84- if ( finalBase ) {
85- // 仅设置 geminiUrl,不设置 vertexUrl
86- setDefaultBaseUrls ( { geminiUrl : finalBase } )
87- }
88-
89- const genAI = new GoogleGenAI ( { apiKey : config . connectionConfig ?. apiKey } )
85+ const rawBaseUrl = config . connectionConfig ?. baseURL ?. trim ( ) || ''
86+ const normalizedBaseUrl = rawBaseUrl ? this . normalizeBaseUrl ( rawBaseUrl ) : ''
87+
88+ const genAI = normalizedBaseUrl
89+ ? new GoogleGenAI ( {
90+ apiKey : config . connectionConfig ?. apiKey ,
91+ httpOptions : {
92+ baseUrl : normalizedBaseUrl
93+ }
94+ } )
95+ : new GoogleGenAI ( { apiKey : config . connectionConfig ?. apiKey } )
9096
9197 // 构建请求内容
9298 let contents : any
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ describe('GeminiImageAdapter', () => {
1919 expect ( provider . defaultBaseURL ) . toBe ( 'https://generativelanguage.googleapis.com' )
2020 expect ( provider . supportsDynamicModels ) . toBe ( false )
2121 expect ( provider . connectionSchema ?. required ) . toContain ( 'apiKey' )
22+ expect ( provider . connectionSchema ?. optional ) . toContain ( 'baseURL' )
2223 } )
2324 } )
2425
@@ -129,6 +130,8 @@ describe('GeminiImageAdapter', () => {
129130 const provider = adapter . getProvider ( )
130131 expect ( provider . connectionSchema ?. required ) . toContain ( 'apiKey' )
131132 expect ( provider . connectionSchema ?. fieldTypes . apiKey ) . toBe ( 'string' )
133+ expect ( provider . connectionSchema ?. optional ) . toContain ( 'baseURL' )
134+ expect ( provider . connectionSchema ?. fieldTypes . baseURL ) . toBe ( 'string' )
132135 } )
133136 } )
134137
You can’t perform that action at this time.
0 commit comments