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 {
AIConfig, AIInnerConfig,
AIConfig,
AIInnerConfig,
Config,
ConfigType,
GoogleConfig,

View File

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

View File

@ -65,6 +65,23 @@ function scimValidation() {
}).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() {
// prettier-ignore
return auth.joiValidator.body(Joi.object({
@ -82,7 +99,8 @@ function buildConfigSaveValidation() {
{ is: ConfigType.ACCOUNT, then: Joi.object().unknown(true) },
{ is: ConfigType.GOOGLE, then: googleValidation() },
{ is: ConfigType.OIDC, then: oidcValidation() },
{ is: ConfigType.SCIM, then: scimValidation() }
{ is: ConfigType.SCIM, then: scimValidation() },
{ is: ConfigType.AI, then: aiValidation() }
],
}),
}).required().unknown(true),