account for arrays

This commit is contained in:
Martin McKeaveney 2024-09-02 20:45:32 +01:00
parent 67d5219f75
commit f328ae4bf9
3 changed files with 51 additions and 21 deletions

View File

@ -1,5 +1,6 @@
import { import {
AIConfig, AIInnerConfig, AIConfig,
AIInnerConfig,
Config, Config,
ConfigType, ConfigType,
GoogleConfig, GoogleConfig,

View File

@ -23,37 +23,48 @@
const models = [ const models = [
"gpt4o-mini", "gpt4o-mini",
] ]
const defaultConfig = {
active: false,
isDefault: false,
}
let formValues = {} let aiConfig = defaultConfig
let validation let validation
$: { $: {
const { provider, model, name, apiKey } = formValues const { provider, defaultModel, name, apiKey } = aiConfig
validation = provider && model && name && apiKey validation = provider && defaultModel && name && apiKey
} }
function prefillConfig(evt) { function prefillConfig(evt) {
const provider = evt.detail const provider = evt.detail
// grab the preset config from the constants for that provider and fill it in // grab the preset config from the constants for that provider and fill it in
if (ConfigMap[provider]) { if (ConfigMap[provider]) {
formValues = { aiConfig = {
...formValues, ...aiConfig,
...ConfigMap[provider] ...ConfigMap[provider]
} }
} else { } else {
formValues = { aiConfig = {
...aiConfig,
provider provider
} }
} }
} }
async function saveConfig() { async function saveConfig() {
formValues.type = "ai" const config = {
type: "ai",
config: [
// TODO: include the ones that are already there, or just handle this in the backend
aiConfig,
]
}
try { try {
const savedConfig = await API.saveConfig(formValues) const savedConfig = await API.saveConfig(config)
formValues._rev = savedConfig._rev aiConfig._rev = savedConfig._rev
formValues._id = savedConfig._id aiConfig._id = savedConfig._id
notifications.success(`Configuration saved`) notifications.success(`Configuration saved`)
} catch (error) { } catch (error) {
notifications.error( notifications.error(
@ -92,33 +103,33 @@
<Label size="M">Provider</Label> <Label size="M">Provider</Label>
<Select <Select
placeholder={null} placeholder={null}
bind:value={formValues.provider} bind:value={aiConfig.provider}
options={providers} options={providers}
on:change={prefillConfig} on:change={prefillConfig}
/> />
</div> </div>
<div class="form-row"> <div class="form-row">
<Label size="M">Model</Label> <Label size="M">Default Model</Label>
<Select <Select
placeholder={null} placeholder={null}
bind:value={formValues.model} bind:value={aiConfig.defaultModel}
options={models} options={models}
/> />
</div> </div>
<div class="form-row"> <div class="form-row">
<Label size="M">Name</Label> <Label size="M">Name</Label>
<Input placeholder={"Test 1"} bind:value={formValues.name}/> <Input placeholder={"Test 1"} bind:value={aiConfig.name}/>
</div> </div>
<div class="form-row"> <div class="form-row">
<Label size="M">Base URL</Label> <Label size="M">Base URL</Label>
<Input placeholder={"www.google.com"} bind:value={formValues.baseUrl}/> <Input placeholder={"www.google.com"} bind:value={aiConfig.baseUrl}/>
</div> </div>
<div class="form-row"> <div class="form-row">
<Label size="M">API Key</Label> <Label size="M">API Key</Label>
<Input bind:value={formValues.apiKey}/> <Input bind:value={aiConfig.apiKey}/>
</div> </div>
<Toggle text="Active" bind:value={formValues.active}/> <Toggle text="Active" bind:value={aiConfig.active}/>
<Toggle text="Set as default" bind:value={formValues.isDefault}/> <Toggle text="Set as default" bind:value={aiConfig.isDefault}/>
</ModalContent> </ModalContent>
<style> <style>

View File

@ -65,6 +65,23 @@ function scimValidation() {
}).unknown(true) }).unknown(true)
} }
function aiValidation() {
// prettier-ignore
return Joi.array().items(
Joi.object({
provider: Joi.string().required(),
isDefault: Joi.boolean().required(),
name: Joi.string().required(),
active: Joi.boolean().required(),
baseUrl: Joi.string().optional(),
apiKey: Joi.string().required(),
// TODO: should be enum
defaultModel: Joi.string().optional(),
})
).required()
}
function buildConfigSaveValidation() { function buildConfigSaveValidation() {
// prettier-ignore // prettier-ignore
return auth.joiValidator.body(Joi.object({ return auth.joiValidator.body(Joi.object({
@ -82,7 +99,8 @@ function buildConfigSaveValidation() {
{ is: ConfigType.ACCOUNT, then: Joi.object().unknown(true) }, { is: ConfigType.ACCOUNT, then: Joi.object().unknown(true) },
{ is: ConfigType.GOOGLE, then: googleValidation() }, { is: ConfigType.GOOGLE, then: googleValidation() },
{ is: ConfigType.OIDC, then: oidcValidation() }, { is: ConfigType.OIDC, then: oidcValidation() },
{ is: ConfigType.SCIM, then: scimValidation() } { is: ConfigType.SCIM, then: scimValidation() },
{ is: ConfigType.AI, then: aiValidation() }
], ],
}), }),
}).required().unknown(true), }).required().unknown(true),