mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 20:19:28 +00:00
feat(provider): optimize provider and model configuration management (#377)
* feat(provider): optimize provider and model configuration management * style(provider): use default tabs variant in provider settings dialog * style(provider): make tabs list full width * feat(provider): make model card row clickable and remove pencil edit icon * feat(settings): persist active settings tab in url hash * feat(agent): add search box and alphabetical sorting for provider and model selectors * refactor(providers): remove frontend pagination and display all models in scrollable list * fix(providers): address code review findings for model editing, i18n, and api protocol propagation
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
DialogTitle,
|
||||
} from '@/ui/components/ui/dialog'
|
||||
import { Field, FieldLabel, FieldError } from '@/ui/components/ui/field'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -22,8 +23,8 @@ import {
|
||||
import { Switch } from '@/ui/components/ui/switch'
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useForm, useStore } from '@tanstack/react-form'
|
||||
import { Loader2, Puzzle, Server, Terminal } from 'lucide-react'
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { Check, ChevronDown, Loader2, Puzzle, Search, Server, Terminal, Zap } from 'lucide-react'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { toast } from 'sonner'
|
||||
import { AgentInfo, AgentType, ThinkingLevel, thinkingLevelSchema } from '@shumai/dtos'
|
||||
@@ -218,6 +219,44 @@ export function AgentFormDialog({
|
||||
enabled: !!selectedProviderId && isOpen,
|
||||
})
|
||||
|
||||
const [providerSearchQuery, setProviderSearchQuery] = useState('')
|
||||
const [isProviderSelectOpen, setIsProviderSelectOpen] = useState(false)
|
||||
|
||||
// Sort providers alphabetically by name
|
||||
const sortedProviders = useMemo(() => {
|
||||
if (!providers) return []
|
||||
return [...providers].sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()))
|
||||
}, [providers])
|
||||
|
||||
// Filter providers based on search query
|
||||
const filteredProviders = useMemo(() => {
|
||||
if (!providerSearchQuery.trim()) return sortedProviders
|
||||
const q = providerSearchQuery.toLowerCase()
|
||||
return sortedProviders.filter((p) => p.name.toLowerCase().includes(q))
|
||||
}, [sortedProviders, providerSearchQuery])
|
||||
|
||||
const [modelSearchQuery, setModelSearchQuery] = useState('')
|
||||
const [isModelSelectOpen, setIsModelSelectOpen] = useState(false)
|
||||
|
||||
// Sort models alphabetically by display name / modelId
|
||||
const sortedModels = useMemo(() => {
|
||||
if (!models) return []
|
||||
return [...models].sort((a, b) => {
|
||||
const nameA = (a.name || a.modelId).toLowerCase()
|
||||
const nameB = (b.name || b.modelId).toLowerCase()
|
||||
return nameA.localeCompare(nameB)
|
||||
})
|
||||
}, [models])
|
||||
|
||||
// Filter models based on search query
|
||||
const filteredModels = useMemo(() => {
|
||||
if (!modelSearchQuery.trim()) return sortedModels
|
||||
const q = modelSearchQuery.toLowerCase()
|
||||
return sortedModels.filter(
|
||||
(m) => m.modelId.toLowerCase().includes(q) || (m.name && m.name.toLowerCase().includes(q)),
|
||||
)
|
||||
}, [sortedModels, modelSearchQuery])
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: async (values: AgentFormValues) => {
|
||||
const res = await client.api.teams[':teamId'].agents.$post({
|
||||
@@ -259,6 +298,10 @@ export function AgentFormDialog({
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
form.reset()
|
||||
setProviderSearchQuery('')
|
||||
setIsProviderSelectOpen(false)
|
||||
setModelSearchQuery('')
|
||||
setIsModelSelectOpen(false)
|
||||
}
|
||||
}, [isOpen, initialValues, type])
|
||||
|
||||
@@ -421,27 +464,109 @@ export function AgentFormDialog({
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
const selectedProvider = providers?.find((p) => p.id === field.state.value)
|
||||
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel>{m.provider()}</FieldLabel>
|
||||
<Select
|
||||
value={field.state.value}
|
||||
onValueChange={(val) => {
|
||||
field.handleChange(val)
|
||||
form.setFieldValue('modelId', '')
|
||||
<Popover
|
||||
open={isProviderSelectOpen}
|
||||
onOpenChange={(open) => {
|
||||
setIsProviderSelectOpen(open)
|
||||
if (!open) setProviderSearchQuery('')
|
||||
}}
|
||||
modal={true}
|
||||
>
|
||||
<SelectTrigger aria-invalid={isInvalid}>
|
||||
<SelectValue placeholder={m.select_provider()} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{providers?.map((p) => (
|
||||
<SelectItem key={p.id} value={p.id}>
|
||||
{p.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
role="combobox"
|
||||
aria-expanded={isProviderSelectOpen}
|
||||
aria-invalid={isInvalid}
|
||||
className={cn(
|
||||
'w-full justify-between font-normal h-10 px-3 bg-background border-border hover:bg-accent/50',
|
||||
isInvalid && 'border-destructive ring-destructive',
|
||||
)}
|
||||
>
|
||||
<span className="truncate">
|
||||
{selectedProvider ? (
|
||||
<span className="font-medium text-foreground truncate">
|
||||
{selectedProvider.name}
|
||||
</span>
|
||||
) : (
|
||||
<span className="text-muted-foreground">
|
||||
{m.select_provider()}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
<ChevronDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
className="w-[--radix-popover-trigger-width] min-w-[320px] max-h-[360px] p-0 flex flex-col overflow-hidden"
|
||||
align="start"
|
||||
side="bottom"
|
||||
sideOffset={4}
|
||||
onWheel={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="relative p-2 pb-1.5 border-b border-border shrink-0">
|
||||
<Search className="absolute left-4.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground" />
|
||||
<Input
|
||||
placeholder={m.search_providers_placeholder()}
|
||||
value={providerSearchQuery}
|
||||
onChange={(e) => setProviderSearchQuery(e.target.value)}
|
||||
className="pl-8 h-8 text-xs bg-muted/40"
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
<ScrollArea
|
||||
className="w-full p-1"
|
||||
style={{
|
||||
height:
|
||||
filteredProviders.length === 0
|
||||
? '96px'
|
||||
: `${Math.min(filteredProviders.length * 38 + 8, 256)}px`,
|
||||
}}
|
||||
onWheel={(e) => e.stopPropagation()}
|
||||
>
|
||||
{filteredProviders.length === 0 ? (
|
||||
<div className="flex items-center justify-center h-full py-6 text-xs text-muted-foreground">
|
||||
{m.no_providers_found()}
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-0.5 pr-2">
|
||||
{filteredProviders.map((p) => {
|
||||
const isSelected = field.state.value === p.id
|
||||
return (
|
||||
<div
|
||||
key={p.id}
|
||||
onClick={() => {
|
||||
field.handleChange(p.id)
|
||||
form.setFieldValue('modelId', '')
|
||||
setModelSearchQuery('')
|
||||
setIsProviderSelectOpen(false)
|
||||
setProviderSearchQuery('')
|
||||
}}
|
||||
className={cn(
|
||||
'flex items-center justify-between px-2.5 py-2 rounded-md text-xs cursor-pointer transition-colors',
|
||||
isSelected
|
||||
? 'bg-accent text-accent-foreground font-medium'
|
||||
: 'hover:bg-muted/60 text-foreground',
|
||||
)}
|
||||
>
|
||||
<span className="truncate flex-1 pr-2">{p.name}</span>
|
||||
{isSelected && (
|
||||
<Check className="w-4 h-4 text-primary shrink-0" />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</ScrollArea>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
@@ -455,28 +580,141 @@ export function AgentFormDialog({
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
const selectedModel = models?.find((m) => m.id === field.state.value)
|
||||
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel className="flex items-center gap-2">
|
||||
{m.model()}
|
||||
{isModelsLoading && <Loader2 className="w-3 h-3 animate-spin" />}
|
||||
</FieldLabel>
|
||||
<Select
|
||||
value={field.state.value}
|
||||
onValueChange={field.handleChange}
|
||||
disabled={!selectedProviderId || isModelsLoading}
|
||||
<Popover
|
||||
open={isModelSelectOpen}
|
||||
onOpenChange={(open) => {
|
||||
setIsModelSelectOpen(open)
|
||||
if (!open) setModelSearchQuery('')
|
||||
}}
|
||||
modal={true}
|
||||
>
|
||||
<SelectTrigger aria-invalid={isInvalid}>
|
||||
<SelectValue placeholder={m.select_model()} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{models?.map((m) => (
|
||||
<SelectItem key={m.id} value={m.id}>
|
||||
{m.name || m.modelId}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
role="combobox"
|
||||
aria-expanded={isModelSelectOpen}
|
||||
aria-invalid={isInvalid}
|
||||
disabled={!selectedProviderId || isModelsLoading}
|
||||
className={cn(
|
||||
'w-full justify-between font-normal h-10 px-3 bg-background border-border hover:bg-accent/50',
|
||||
isInvalid && 'border-destructive ring-destructive',
|
||||
)}
|
||||
>
|
||||
<span className="truncate flex items-center gap-2">
|
||||
{selectedModel ? (
|
||||
<>
|
||||
<span className="truncate font-medium text-foreground">
|
||||
{selectedModel.name || selectedModel.modelId}
|
||||
</span>
|
||||
{selectedModel.name &&
|
||||
selectedModel.name !== selectedModel.modelId && (
|
||||
<Badge
|
||||
variant="secondary"
|
||||
className="font-mono text-[10px] px-1.5 py-0 shrink-0"
|
||||
>
|
||||
{selectedModel.modelId}
|
||||
</Badge>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<span className="text-muted-foreground">
|
||||
{m.select_model()}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
<ChevronDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
className="w-[--radix-popover-trigger-width] min-w-[320px] max-h-[360px] p-0 flex flex-col overflow-hidden"
|
||||
align="start"
|
||||
side="bottom"
|
||||
sideOffset={4}
|
||||
onWheel={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="relative p-2 pb-1.5 border-b border-border shrink-0">
|
||||
<Search className="absolute left-4.5 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground" />
|
||||
<Input
|
||||
placeholder={m.search_models_placeholder()}
|
||||
value={modelSearchQuery}
|
||||
onChange={(e) => setModelSearchQuery(e.target.value)}
|
||||
className="pl-8 h-8 text-xs bg-muted/40"
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
<ScrollArea
|
||||
className="w-full p-1"
|
||||
style={{
|
||||
height:
|
||||
filteredModels.length === 0
|
||||
? '96px'
|
||||
: `${Math.min(filteredModels.length * 38 + 8, 256)}px`,
|
||||
}}
|
||||
onWheel={(e) => e.stopPropagation()}
|
||||
>
|
||||
{filteredModels.length === 0 ? (
|
||||
<div className="flex items-center justify-center h-full py-6 text-xs text-muted-foreground">
|
||||
{m.no_models_found()}
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-0.5 pr-2">
|
||||
{filteredModels.map((m) => {
|
||||
const isSelected = field.state.value === m.id
|
||||
return (
|
||||
<div
|
||||
key={m.id}
|
||||
onClick={() => {
|
||||
field.handleChange(m.id)
|
||||
setIsModelSelectOpen(false)
|
||||
setModelSearchQuery('')
|
||||
}}
|
||||
className={cn(
|
||||
'flex items-center justify-between px-2.5 py-2 rounded-md text-xs cursor-pointer transition-colors',
|
||||
isSelected
|
||||
? 'bg-accent text-accent-foreground font-medium'
|
||||
: 'hover:bg-muted/60 text-foreground',
|
||||
)}
|
||||
>
|
||||
<div className="min-w-0 flex-1 pr-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="truncate">
|
||||
{m.name || m.modelId}
|
||||
</span>
|
||||
{m.name && m.name !== m.modelId && (
|
||||
<span className="font-mono text-[10px] text-muted-foreground truncate">
|
||||
({m.modelId})
|
||||
</span>
|
||||
)}
|
||||
{m.config?.reasoning && (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="text-[9px] px-1 py-0 h-4 gap-0.5 bg-primary/10 text-primary border-primary/20 shrink-0"
|
||||
>
|
||||
<Zap className="w-2.5 h-2.5" />
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{isSelected && (
|
||||
<Check className="w-4 h-4 text-primary shrink-0" />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</ScrollArea>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { ModelFormDialog } from './ModelFormDialog'
|
||||
import type { ModelFormValues } from './ModelFormDialog'
|
||||
|
||||
describe('ModelFormDialog', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
const model1: ModelFormValues = {
|
||||
modelId: 'gpt-4o',
|
||||
name: 'GPT-4o Production',
|
||||
config: {
|
||||
api: 'openai-responses',
|
||||
reasoning: true,
|
||||
input: ['text', 'image'],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 4096,
|
||||
cost: { input: 2.5, output: 10, cacheRead: 1.25, cacheWrite: 2.5 },
|
||||
},
|
||||
}
|
||||
|
||||
const model2: ModelFormValues = {
|
||||
modelId: 'claude-3-5-sonnet',
|
||||
name: 'Claude 3.5 Sonnet',
|
||||
config: {
|
||||
api: 'anthropic-messages',
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
contextWindow: 200000,
|
||||
maxTokens: 8192,
|
||||
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
||||
},
|
||||
}
|
||||
|
||||
it('populates fields with initialValues when opened for editing', () => {
|
||||
render(
|
||||
<ModelFormDialog
|
||||
isOpen={true}
|
||||
onClose={vi.fn()}
|
||||
onSubmit={vi.fn()}
|
||||
title="Edit Model"
|
||||
initialValues={model1}
|
||||
defaultApi="openai-responses"
|
||||
/>,
|
||||
)
|
||||
|
||||
const modelIdInput = screen.getByLabelText(/model id/i) as HTMLInputElement
|
||||
const nameInput = screen.getByLabelText(/display name/i) as HTMLInputElement
|
||||
|
||||
expect(modelIdInput.value).toBe('gpt-4o')
|
||||
expect(nameInput.value).toBe('GPT-4o Production')
|
||||
})
|
||||
|
||||
it('updates fields when switching between different models', () => {
|
||||
const { rerender } = render(
|
||||
<ModelFormDialog
|
||||
key="model-1"
|
||||
isOpen={true}
|
||||
onClose={vi.fn()}
|
||||
onSubmit={vi.fn()}
|
||||
title="Edit Model"
|
||||
initialValues={model1}
|
||||
defaultApi="openai-responses"
|
||||
/>,
|
||||
)
|
||||
|
||||
let modelIdInput = screen.getByLabelText(/model id/i) as HTMLInputElement
|
||||
expect(modelIdInput.value).toBe('gpt-4o')
|
||||
|
||||
rerender(
|
||||
<ModelFormDialog
|
||||
key="model-2"
|
||||
isOpen={true}
|
||||
onClose={vi.fn()}
|
||||
onSubmit={vi.fn()}
|
||||
title="Edit Model"
|
||||
initialValues={model2}
|
||||
defaultApi="anthropic-messages"
|
||||
/>,
|
||||
)
|
||||
|
||||
modelIdInput = screen.getByLabelText(/model id/i) as HTMLInputElement
|
||||
const nameInput = screen.getByLabelText(/display name/i) as HTMLInputElement
|
||||
|
||||
expect(modelIdInput.value).toBe('claude-3-5-sonnet')
|
||||
expect(nameInput.value).toBe('Claude 3.5 Sonnet')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,398 @@
|
||||
import { ScrollArea } from '@/ui/components/ui/scroll-area'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/ui/components/ui/dialog'
|
||||
import { Field, FieldError, FieldLabel } from '@/ui/components/ui/field'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { Switch } from '@/ui/components/ui/switch'
|
||||
import { createModelRequestSchema } from '@shumai/dtos'
|
||||
import { useForm } from '@tanstack/react-form'
|
||||
import { DollarSign, Loader2 } from 'lucide-react'
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { z } from 'zod'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
|
||||
export type ModelFormValues = z.infer<typeof createModelRequestSchema>
|
||||
|
||||
export interface ModelFormDialogProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
onSubmit: (values: ModelFormValues) => void
|
||||
initialValues?: ModelFormValues | null
|
||||
title: string
|
||||
isLoading?: boolean
|
||||
defaultApi?: string
|
||||
existingModelIds?: string[]
|
||||
}
|
||||
|
||||
export function ModelFormDialog({
|
||||
isOpen,
|
||||
onClose,
|
||||
onSubmit,
|
||||
initialValues,
|
||||
title,
|
||||
isLoading = false,
|
||||
defaultApi,
|
||||
existingModelIds = [],
|
||||
}: ModelFormDialogProps) {
|
||||
const schema = useMemo(() => {
|
||||
return createModelRequestSchema.extend({
|
||||
modelId: z
|
||||
.string()
|
||||
.min(1, m.model_id())
|
||||
.refine(
|
||||
(id) => {
|
||||
if (initialValues?.modelId === id) return true
|
||||
return !existingModelIds.includes(id)
|
||||
},
|
||||
{ message: m.model_id_already_exists() },
|
||||
),
|
||||
})
|
||||
}, [existingModelIds, initialValues])
|
||||
|
||||
const getDefaultValues = () =>
|
||||
(initialValues || {
|
||||
modelId: '',
|
||||
name: '',
|
||||
config: {
|
||||
api: defaultApi,
|
||||
reasoning: false,
|
||||
input: ['text'],
|
||||
contextWindow: 128000,
|
||||
maxTokens: 4096,
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
},
|
||||
},
|
||||
}) as z.input<typeof schema>
|
||||
|
||||
const form = useForm({
|
||||
defaultValues: getDefaultValues(),
|
||||
validators: {
|
||||
onSubmit: schema,
|
||||
},
|
||||
onSubmit: async ({ value }) => {
|
||||
onSubmit(value as ModelFormValues)
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
form.reset(getDefaultValues())
|
||||
}
|
||||
}, [isOpen, initialValues, defaultApi, form])
|
||||
|
||||
const mapErrors = (errors: unknown[]) => {
|
||||
return errors.map((e) => {
|
||||
if (typeof e === 'string') return { message: e }
|
||||
if (e && typeof e === 'object' && 'message' in e) {
|
||||
return { message: String(e.message) }
|
||||
}
|
||||
return { message: String(e || '') }
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={onClose}>
|
||||
<DialogContent className="sm:max-w-xl h-[85vh] max-h-[700px] flex flex-col p-0 overflow-hidden">
|
||||
<DialogHeader className="p-6 pb-2 shrink-0">
|
||||
<DialogTitle className="text-xl">{title}</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
form.handleSubmit()
|
||||
}}
|
||||
className="flex-1 flex flex-col min-h-0 overflow-hidden"
|
||||
>
|
||||
<div className="flex-1 min-h-0">
|
||||
<ScrollArea className="h-full">
|
||||
<div className="p-6 pt-2 space-y-6 pr-6">
|
||||
{/* Basic Model Info */}
|
||||
<div className="space-y-4">
|
||||
<form.Field
|
||||
name="modelId"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name}>{m.model_id()}</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(e.target.value)}
|
||||
placeholder={m.model_id_placeholder()}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
|
||||
<form.Field
|
||||
name="name"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name}>{m.display_name_optional()}</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
value={field.state.value || ''}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(e.target.value)}
|
||||
placeholder={m.model_name_placeholder()}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Parameters & Reasoning */}
|
||||
<div className="space-y-4 pt-2 border-t border-border">
|
||||
<h4 className="text-sm font-semibold text-foreground">{m.parameters()}</h4>
|
||||
<form.Field
|
||||
name="config.reasoning"
|
||||
children={(field) => (
|
||||
<Field
|
||||
orientation="horizontal"
|
||||
className="rounded-lg border border-border p-3 shadow-sm bg-muted/40 justify-between flex items-center"
|
||||
>
|
||||
<FieldLabel htmlFor={field.name} className="cursor-pointer">
|
||||
{m.reasoning_support()}
|
||||
</FieldLabel>
|
||||
<Switch
|
||||
id={field.name}
|
||||
checked={field.state.value}
|
||||
onCheckedChange={field.handleChange}
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<form.Field
|
||||
name="config.contextWindow"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||
{m.context_window_tokens()}
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
type="number"
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(parseInt(e.target.value) || 0)}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
|
||||
<form.Field
|
||||
name="config.maxTokens"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||
{m.max_output_tokens()}
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
type="number"
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(parseInt(e.target.value) || 0)}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Pricing Section */}
|
||||
<div className="space-y-4 pt-2 border-t border-border">
|
||||
<h4 className="text-sm font-semibold text-foreground flex items-center gap-1.5">
|
||||
<DollarSign className="w-4 h-4 text-primary" />
|
||||
{m.pricing()}
|
||||
</h4>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<form.Field
|
||||
name="config.cost.input"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||
{m.input_cost_1m()}
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
type="number"
|
||||
step="0.0001"
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(parseFloat(e.target.value) || 0)}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
|
||||
<form.Field
|
||||
name="config.cost.output"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||
{m.output_cost_1m()}
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
type="number"
|
||||
step="0.0001"
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(parseFloat(e.target.value) || 0)}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
|
||||
<form.Field
|
||||
name="config.cost.cacheRead"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||
{m.cache_read_cost_1m()}
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
type="number"
|
||||
step="0.0001"
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(parseFloat(e.target.value) || 0)}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
|
||||
<form.Field
|
||||
name="config.cost.cacheWrite"
|
||||
children={(field) => {
|
||||
const isInvalid =
|
||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||
return (
|
||||
<Field data-invalid={isInvalid}>
|
||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||
{m.cache_write_cost_1m()}
|
||||
</FieldLabel>
|
||||
<Input
|
||||
id={field.name}
|
||||
name={field.name}
|
||||
type="number"
|
||||
step="0.0001"
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => field.handleChange(parseFloat(e.target.value) || 0)}
|
||||
aria-invalid={isInvalid}
|
||||
/>
|
||||
{isInvalid && (
|
||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||
)}
|
||||
</Field>
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
|
||||
<DialogFooter className="p-6 pt-4 border-t border-border gap-2 shrink-0">
|
||||
<Button type="button" variant="outline" onClick={onClose} disabled={isLoading}>
|
||||
{m.cancel()}
|
||||
</Button>
|
||||
<form.Subscribe
|
||||
selector={(state) => [state.canSubmit, state.isSubmitting]}
|
||||
children={([canSubmit, isSubmitting]) => (
|
||||
<Button
|
||||
type="submit"
|
||||
className="min-w-[100px]"
|
||||
disabled={!canSubmit || isSubmitting || isLoading}
|
||||
>
|
||||
{isSubmitting || isLoading ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
) : (
|
||||
m.save_changes()
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
/>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user