update type info

This commit is contained in:
Andrew Pareles 2024-12-09 19:01:08 -08:00
parent 8a95b13711
commit f7c9d732f5
3 changed files with 88 additions and 42 deletions

View file

@ -216,54 +216,93 @@ export type FeatureName = typeof featureNames[number]
export type VoidConfigState = {
[providerName in ProviderName]: ({
enabled: string, // 'true' | 'false'
models: string[] | null, // if null, user can type in any string as a model
model: string,
maxTokens: string,
} & {
[optionName in keyof typeof voidProviderDefaults[providerName]]: string
})
[providerName in ProviderName]: (
{
[optionName in keyof typeof voidProviderDefaults[providerName]]: string
}
&
{
enabled: string, // 'true' | 'false'
maxTokens: string,
models: string[] | null, // if null, user can type in any string as a model
model: string,
})
}
type UnionOfKeys<T> = T extends T ? keyof T : never;
export const descOfSettingName = (providerName: ProviderName, settingName: UnionOfKeys<VoidConfigState[ProviderName]>) => {
if (settingName === 'apiKey')
return 'API Key'
else if (settingName === 'endpoint') {
if (providerName === 'ollama') return 'The endpoint of your Ollama instance.'
if (providerName === 'openAICompatible') return 'The baseUrl (exluding /chat/completions).'
type SettingName = UnionOfKeys<VoidConfigState[ProviderName]>
type DisplayInfo = {
title: string,
type: string,
placeholder: string,
}
export const displayInfoOfSettingName = (providerName: ProviderName, settingName: SettingName): DisplayInfo => {
if (settingName === 'apiKey') {
return {
title: 'API Key',
type: 'string',
placeholder: providerName === 'anthropic' ? 'sk-ant-api03-abc123...' :
providerName === 'openAI' ? 'sk-proj-abc123...' :
providerName === 'openRouter' ? 'sk-or-v1-abc123...' :
providerName === 'gemini' ? 'abc123...' :
providerName === 'groq' ? 'gsk_abc123...' :
'(never)',
}
}
else if (settingName === 'endpoint') {
return {
title: providerName === 'ollama' ? 'The endpoint of your Ollama instance.' :
providerName === 'openAICompatible' ? 'The baseUrl (exluding /chat/completions).'
: '(never)',
type: 'string',
placeholder: providerName === 'ollama' || providerName === 'openAICompatible' ?
voidProviderDefaults[providerName].endpoint
: '(never)',
}
}
else if (settingName === 'maxTokens') {
return {
title: 'Max Tokens',
type: 'number',
placeholder: '1024',
}
}
else if (settingName === 'model') {
return {
title: 'Model',
type: '(never)',
placeholder: '(never)',
}
}
else if (settingName === 'enabled') {
return {
title: 'Enabled',
type: 'boolean',
placeholder: '(never)',
}
}
else if (settingName === 'models') {
return {
title: 'Available Models',
type: '(never)',
placeholder: '(never)',
}
}
else if (settingName === 'maxTokens')
return 'Max Tokens'
else if (settingName === 'model')
return 'Model'
else if (settingName === 'enabled')
return 'Enabled'
else if (settingName === 'models')
return 'Available Models'
throw new Error(`desc: Unknown setting name: "${settingName}"`)
throw new Error(`displayInfo: Unknown setting name: "${settingName}"`)
}
export const inputTypeOfSettingName = (settingName: UnionOfKeys<VoidConfigState[ProviderName]>) => {
if (settingName === 'apiKey')
return 'string' as const
else if (settingName === 'endpoint')
return 'string' as const
else if (settingName === 'maxTokens')
return 'number' as const
else if (settingName === 'model')
return 'string' as const
else if (settingName === 'enabled')
return 'boolean' as const
else if (settingName === 'models')
return 'string[]?' as const
throw new Error(`inputType: Unknown setting name: "${settingName}"`)
}
export const defaultVoidConfigState: VoidConfigState = {
@ -310,3 +349,6 @@ export const defaultVoidConfigState: VoidConfigState = {
maxTokens: '',
}
}

View file

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import React, { Fragment } from 'react'
import { descOfSettingName, inputTypeOfSettingName, ProviderName, providerNames } from '../../../../../../../platform/void/common/configTypes.js'
import { displayInfoOfSettingName, ProviderName, providerNames } from '../../../../../../../platform/void/common/configTypes.js'
import { VoidCheckBox, VoidInputBox, VoidSelectBox } from './inputs.js'
import { useConfigState, useService } from '../util/services.js'
@ -20,10 +20,12 @@ const SettingsForProvider = ({ providerName }: { providerName: ProviderName }) =
{Object.entries(others).map(([settingName, defaultVal], i) => {
const sName = settingName as keyof typeof others
const { title, type, placeholder } = displayInfoOfSettingName(providerName, sName)
return <Fragment key={i}>
<h2>{descOfSettingName(providerName, sName)}</h2>
<h2>{title}</h2>
{
inputTypeOfSettingName(sName) === 'boolean' ?
type === 'boolean' ?
<VoidCheckBox
initVal={defaultVal === 'true'}
onChangeChecked={(newVal) => { voidConfigService.setState(providerName, sName, newVal ? 'true' : 'false') }}
@ -33,8 +35,8 @@ const SettingsForProvider = ({ providerName }: { providerName: ProviderName }) =
:
<VoidInputBox
initVal={defaultVal}
placeholder={placeholder}
onChangeText={(newVal) => { () => { voidConfigService.setState(providerName, sName, newVal) } }}
placeholder={defaultVal}
multiline={false}
inputBoxRef={{ current: null }}
/>}

View file

@ -151,3 +151,5 @@ export const VoidCheckBox = ({ onChangeChecked, initVal, label, checkboxRef, }:
return <div ref={containerRef} className="w-full" />;
};