11<script setup lang="ts">
2- import { ref , computed } from ' vue'
2+ import { ref , computed , onMounted } from ' vue'
33import { useI18n } from ' vue-i18n'
44import { apiFetch } from ' @/shared/api'
55import { store } from ' @/shared/store'
66import { useToast , useConfirm , SButton , SInput , SSelect , SModal , SFormItem , SPageToolbar , SPageContent , STable } from ' sbot-ui'
77import type { STableColumn } from ' sbot-ui'
88import { EmbeddingProvider } from ' @/shared/types'
99import type { EmbeddingConfig } from ' @/shared/types'
10+ import { isConfigFieldVisible , type ShowWhen } from ' @/utils/configField'
1011import ResourceRefs from ' @/components/ResourceRefs.vue'
1112import { useResourceRefs } from ' @/composables/useResourceRefs'
1213
@@ -35,19 +36,93 @@ const refs = makeResourceRefs({
3536})
3637const expandedIds = ref <string []>([])
3738
38- const providerDefaults: Record <string , { baseURL: string ; apiKey: string ; model: string }> = {
39- [EmbeddingProvider .OpenAI ]: { baseURL: ' https://api.openai.com/v1' , apiKey: ' sk-...' , model: ' text-embedding-ada-002' },
40- [EmbeddingProvider .Ollama ]: { baseURL: ' http://localhost:11434' , apiKey: ' ' , model: ' nomic-embed-text' },
41- [EmbeddingProvider .Gemini ]: { baseURL: ' https://generativelanguage.googleapis.com' , apiKey: ' AIza...' , model: ' text-embedding-004' },
42- [EmbeddingProvider .VoyageAI ]: { baseURL: ' https://api.voyageai.com/v1' , apiKey: ' pa-...' , model: ' voyage-3' },
43- [EmbeddingProvider .Cohere ]: { baseURL: ' https://api.cohere.com/v2' , apiKey: ' sk-...' , model: ' embed-v4.0' },
39+ interface ProviderField {
40+ label: string
41+ type: ' string' | ' textarea' | ' password' | ' boolean' | ' number' | ' select'
42+ required? : boolean
43+ description? : string
44+ default? : string | boolean | number
45+ options? : Array <{ label: string ; value: string }>
46+ showWhen? : ShowWhen
4447}
4548
46- const defaults = computed (() => providerDefaults [form .value .provider ] || providerDefaults [EmbeddingProvider .OpenAI ])
47- const isOllama = computed (() => form .value .provider === EmbeddingProvider .Ollama )
48- const isGemini = computed (() => form .value .provider === EmbeddingProvider .Gemini )
49+ interface EmbeddingProviderDefinition {
50+ type: string
51+ label: string
52+ configSchema: Record <string , ProviderField >
53+ defaults? : { baseURL? : string ; model? : string ; config? : Record <string , any > }
54+ baseURLEnabled? : boolean
55+ apiKeyEnabled? : boolean
56+ apiKeyRequired? : boolean
57+ supportsModelListing: boolean
58+ }
59+
60+ const providers = ref <EmbeddingProviderDefinition []>([])
61+ const showModal = ref (false )
62+ const editingName = ref <string | null >(null )
63+ const showApiKey = ref (false )
64+ const privateFieldVisible = ref <Record <string , boolean >>({})
65+ const form = ref <EmbeddingConfig >({
66+ name: ' ' , provider: EmbeddingProvider .OpenAI , baseURL: ' ' , apiKey: ' ' , model: ' ' , config: {},
67+ })
68+
69+ const currentProvider = computed (() => providers .value .find (provider => provider .type === form .value .provider ))
70+ const providerOptions = computed <EmbeddingProviderDefinition []>(() => {
71+ const options = providers .value .length > 0
72+ ? [... providers .value ]
73+ : Object .values (EmbeddingProvider ).map (type => ({ type , label: type , configSchema: {}, supportsModelListing: false }))
74+ if (form .value .provider && ! options .some (provider => provider .type === form .value .provider )) {
75+ options .push ({ type: form .value .provider , label: form .value .provider , configSchema: {}, supportsModelListing: false })
76+ }
77+ return options
78+ })
79+ const currentSchema = computed (() => currentProvider .value ?.configSchema ?? {})
80+ const providerConfig = computed <Record <string , any >>({
81+ get : () => form .value .config ?? {},
82+ set : value => { form .value .config = value },
83+ })
84+ const visibleSchemaEntries = computed (() =>
85+ Object .entries (currentSchema .value ).filter (([, field ]) => isConfigFieldVisible (field , form .value .config )),
86+ )
87+ const baseURLEnabled = computed (() => currentProvider .value ?.baseURLEnabled !== false )
88+ const apiKeyEnabled = computed (() => currentProvider .value ?.apiKeyEnabled !== false )
89+ const apiKeyRequired = computed (() => currentProvider .value ?.apiKeyRequired ?? form .value .provider !== EmbeddingProvider .Ollama )
90+ const providerBaseURL = computed (() => currentProvider .value ?.defaults ?.baseURL ?? ' ' )
91+ const providerModel = computed (() => currentProvider .value ?.defaults ?.model ?? ' ' )
92+ const canPick = computed (() => {
93+ if (! currentProvider .value ?.supportsModelListing ) return false
94+ if (! form .value .baseURL ) return false
95+ return ! apiKeyRequired .value || !! form .value .apiKey
96+ })
4997
50- const canPick = computed (() => isGemini .value || isOllama .value || !! form .value .baseURL )
98+ onMounted (() => { void loadProviders () })
99+
100+ async function loadProviders() {
101+ try {
102+ const res = await apiFetch (' /api/embedding-providers' )
103+ providers .value = res .data as EmbeddingProviderDefinition []
104+ } catch (e : any ) {
105+ show (e .message , ' error' )
106+ }
107+ }
108+
109+ function defaultPrivateConfig(provider = currentProvider .value ): Record <string , any > {
110+ const result = { ... (provider ?.defaults ?.config ?? {}) }
111+ for (const [key, field] of Object .entries (provider ?.configSchema ?? {})) {
112+ if (result [key ] === undefined && field .default !== undefined ) result [key ] = field .default
113+ }
114+ return result
115+ }
116+
117+ function onProviderChange() {
118+ const provider = currentProvider .value
119+ form .value .baseURL = provider ?.defaults ?.baseURL ?? ' '
120+ form .value .model = provider ?.defaults ?.model ?? ' '
121+ form .value .apiKey = ' '
122+ form .value .config = defaultPrivateConfig (provider )
123+ showApiKey .value = false
124+ privateFieldVisible .value = {}
125+ }
51126
52127const showPicker = ref (false )
53128const pickerLoading = ref (false )
@@ -63,11 +138,11 @@ async function openPicker() {
63138 pickerFilter .value = ' '
64139 showPicker .value = true
65140 try {
66- const provider = form .value .provider as string
67- const res = await apiFetch (' /api/models/available' , ' POST' , {
141+ const res = await apiFetch (' /api/embeddings/available' , ' POST' , {
68142 baseURL: form .value .baseURL ,
69143 apiKey: form .value .apiKey ,
70- provider ,
144+ provider: form .value .provider ,
145+ config: form .value .config ,
71146 })
72147 pickerModels .value = res .data as string []
73148 } catch (e : any ) {
@@ -83,22 +158,24 @@ function pickModel(m: string) {
83158 showPicker .value = false
84159}
85160
86- const showModal = ref (false )
87- const editingName = ref <string | null >(null )
88- const showApiKey = ref (false )
89- const form = ref <EmbeddingConfig >({
90- name: ' ' , provider: EmbeddingProvider .OpenAI , baseURL: ' ' , apiKey: ' ' , model: ' ' ,
91- })
92-
93161function openAdd() {
94162 editingName .value = null
95163 showApiKey .value = false
96- form .value = { name: ' ' , provider: EmbeddingProvider .OpenAI , baseURL: ' ' , apiKey: ' ' , model: ' ' }
164+ const provider = providers .value .find (item => item .type === EmbeddingProvider .OpenAI ) ?? providers .value [0 ]
165+ form .value = {
166+ name: ' ' ,
167+ provider: provider ?.type ?? EmbeddingProvider .OpenAI ,
168+ baseURL: provider ?.defaults ?.baseURL ?? ' ' ,
169+ apiKey: ' ' ,
170+ model: provider ?.defaults ?.model ?? ' ' ,
171+ config: defaultPrivateConfig (provider ),
172+ }
97173 showModal .value = true
98174}
99175
100176function openEdit(id : string ) {
101177 const e = embeddings .value [id ]
178+ const provider = providers .value .find (item => item .type === e .provider )
102179 editingName .value = id
103180 showApiKey .value = false
104181 form .value = {
@@ -107,16 +184,38 @@ function openEdit(id: string) {
107184 baseURL: e .baseURL ,
108185 apiKey: e .apiKey ,
109186 model: e .model ,
187+ config: { ... defaultPrivateConfig (provider ), ... (e .config ?? {}) },
110188 }
111189 showModal .value = true
112190}
113191
114192async function save() {
115193 if (! form .value .name .trim ()) { show (t (' common.name_required' ), ' error' ); return }
116- if (! isOllama .value && ! form .value .apiKey .trim ()) { show (t (' common.api_key_required' ), ' error' ); return }
194+ if (apiKeyRequired .value && ! form .value .apiKey .trim ()) { show (t (' common.api_key_required' ), ' error' ); return }
117195 if (! form .value .model .trim ()) { show (t (' common.model_required' ), ' error' ); return }
196+ for (const [key, field] of visibleSchemaEntries .value ) {
197+ const value = form .value .config ?.[key ]
198+ if (field .required && (value === undefined || value === null || value === ' ' )) {
199+ show (` ${field .label } is required ` , ' error' )
200+ return
201+ }
202+ }
118203 try {
119- const body = { ... form .value }
204+ const body: any = { ... form .value }
205+ if (! baseURLEnabled .value ) delete body .baseURL
206+ if (! apiKeyEnabled .value ) body .apiKey = ' '
207+ if (currentProvider .value ) {
208+ const providerConfig: Record <string , any > = {}
209+ for (const [key, field] of Object .entries (currentSchema .value )) {
210+ if (! isConfigFieldVisible (field , body .config )) continue
211+ const value = body .config ?.[key ]
212+ if (value !== undefined && value !== null && value !== ' ' ) providerConfig [key ] = value
213+ }
214+ if (Object .keys (providerConfig ).length > 0 ) body .config = providerConfig
215+ else delete body .config
216+ } else if (! body .config || Object .keys (body .config ).length === 0 ) {
217+ delete body .config
218+ }
120219 const id = editingName .value
121220 const res = id
122221 ? await apiFetch (` /api/settings/embeddings/${encodeURIComponent (id )} ` , ' PUT' , body )
@@ -188,27 +287,48 @@ async function refresh() {
188287 <SInput v-model =" form .name " :placeholder =" t (' embeddings.name_placeholder' )" />
189288 </SFormItem >
190289 <SFormItem :label =" t (' common.provider' ) + ' *' " >
191- <SSelect v-model =" form .provider " >
192- <option v-for =" p in Object.values(EmbeddingProvider) " :key =" p " :value =" p " >{{ p }}</option >
290+ <SSelect v-model =" form .provider " @ change = " onProviderChange " >
291+ <option v-for =" provider in providerOptions " :key =" provider.type " :value =" provider.type " >{{ provider.label }}</option >
193292 </SSelect >
194293 </SFormItem >
195- <SFormItem :label =" t (' common.base_url' )" >
196- <SInput v-model =" form .baseURL " :placeholder =" defaults . baseURL " />
294+ <SFormItem v-if = " baseURLEnabled " :label =" t (' common.base_url' )" >
295+ <SInput v-model =" form .baseURL " :placeholder =" providerBaseURL " />
197296 </SFormItem >
198- <SFormItem v-if =" ! isOllama " :label =" t (' common.api_key' ) + ' *' " >
297+ <SFormItem v-if =" apiKeyEnabled " :label =" t (' common.api_key' ) + ( apiKeyRequired ? ' *' : ' ' ) " >
199298 <div class =" apikey-field" >
200- <SInput v-model =" form .apiKey " :type =" showApiKey ? ' text' : ' password' " : placeholder =" defaults . apiKey " class="apikey-input" />
299+ <SInput v-model =" form .apiKey " :type =" showApiKey ? ' text' : ' password' " placeholder="API Key " class="apikey-input" />
201300 <SButton type="outline" size="sm" class="apikey-toggle" @click =" showApiKey = ! showApiKey " >
202301 {{ showApiKey ? t('common.hide') : t('common.show') }}
203302 </SButton >
204303 </div >
205304 </SFormItem >
206305 <SFormItem :label =" t (' common.model' ) + ' *' " >
207306 <div class =" model-field" >
208- <SInput v-model =" form .model " :placeholder =" defaults . model " class="model-input" />
307+ <SInput v-model =" form .model " :placeholder =" providerModel " class="model-input" />
209308 <SButton type="outline" size="sm" class="model-pick-btn" :disabled =" ! canPick " @click =" openPicker " >{{ t('models.pick') }}</SButton >
210309 </div >
211310 </SFormItem >
311+ <template v-for =" [key , field ] in visibleSchemaEntries " :key =" key " >
312+ <SFormItem :label =" field .label + (field .required ? ' *' : ' ' )" >
313+ <SSelect v-if =" field .type === ' select' " v-model =" providerConfig [key ]" >
314+ <option v-for =" option in field.options" :key =" option.value" :value =" option.value" >{{ option.label }}</option >
315+ </SSelect >
316+ <label v-else-if =" field.type === 'boolean'" class =" checkbox-label" >
317+ <input v-model =" providerConfig[key]" type =" checkbox" />
318+ {{ field.description || '' }}
319+ </label >
320+ <SInput v-else-if =" field .type === ' number' " v-model .number =" providerConfig [key ]" type="number" :placeholder =" field .description || ' ' " />
321+ <div v-else-if =" field.type === 'password'" class =" apikey-field" >
322+ <SInput v-model =" providerConfig [key ]" :type =" privateFieldVisible [key ] ? ' text' : ' password' " :placeholder =" field .description || ' ' " class="apikey-input" />
323+ <SButton type="outline" size="sm" @click =" privateFieldVisible [key ] = ! privateFieldVisible [key ]" >
324+ {{ privateFieldVisible[key] ? t('common.hide') : t('common.show') }}
325+ </SButton >
326+ </div >
327+ <SInput v-else-if =" field .type === ' textarea' " v-model =" providerConfig [key ]" multiline :placeholder =" field .description || ' ' " />
328+ <SInput v-else v-model =" providerConfig [key ]" :placeholder =" field .description || ' ' " />
329+ <template v-if =" field .type !== ' boolean' && field .description " #hint >{{ field.description }}</template >
330+ </SFormItem >
331+ </template >
212332 <template #footer >
213333 <SButton type="outline" @click =" showModal = false " >{{ t('common.cancel') }}</SButton >
214334 <SButton type="primary" @click =" save " >{{ t('common.save') }}</SButton >
@@ -248,6 +368,12 @@ async function refresh() {
248368.apikey-input , .model-input {
249369 flex : 1 ;
250370}
371+ .checkbox-label {
372+ display : inline-flex ;
373+ align-items : center ;
374+ gap : var (--sui-sp-2 );
375+ cursor : pointer ;
376+ }
251377.picker-filter-bar {
252378 display : flex ;
253379 align-items : center ;
0 commit comments