mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Merge pull request #23 from chriswritescode-dev/fix/agent-model-config-format
Fix agent model config to use string format instead of object
This commit is contained in:
3 files changed
+312
-27
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { z } from 'zod'
|
||||
import { useMemo } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@@ -8,6 +10,8 @@ import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, For
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { Combobox, type ComboboxOption } from '@/components/ui/combobox'
|
||||
import { getProvidersWithModels } from '@/api/providers'
|
||||
|
||||
const agentFormSchema = z.object({
|
||||
name: z.string().min(1, 'Agent name is required').regex(/^[a-z0-9-]+$/, 'Must be lowercase letters, numbers, and hyphens only'),
|
||||
@@ -36,10 +40,8 @@ interface Agent {
|
||||
mode?: 'subagent' | 'primary' | 'all'
|
||||
temperature?: number
|
||||
topP?: number
|
||||
model?: {
|
||||
modelID: string
|
||||
providerID: string
|
||||
}
|
||||
top_p?: number
|
||||
model?: string
|
||||
tools?: Record<string, boolean>
|
||||
permission?: {
|
||||
edit?: 'ask' | 'allow' | 'deny'
|
||||
@@ -50,6 +52,12 @@ interface Agent {
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
function parseModelString(model?: string): { providerId: string; modelId: string } {
|
||||
if (!model) return { providerId: '', modelId: '' }
|
||||
const [providerId, ...rest] = model.split('/')
|
||||
return { providerId: providerId || '', modelId: rest.join('/') || '' }
|
||||
}
|
||||
|
||||
interface AgentDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
@@ -58,6 +66,29 @@ interface AgentDialogProps {
|
||||
}
|
||||
|
||||
export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: AgentDialogProps) {
|
||||
const parsedModel = parseModelString(editingAgent?.agent.model)
|
||||
|
||||
const { data: providers = [] } = useQuery({
|
||||
queryKey: ['providers-with-models'],
|
||||
queryFn: () => getProvidersWithModels(),
|
||||
enabled: open,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
})
|
||||
|
||||
const providerOptions: ComboboxOption[] = useMemo(() => {
|
||||
const sourceLabels: Record<string, string> = {
|
||||
configured: 'Custom',
|
||||
local: 'Local',
|
||||
builtin: 'Built-in',
|
||||
}
|
||||
return providers.map(p => ({
|
||||
value: p.id,
|
||||
label: p.name || p.id,
|
||||
description: p.models.length > 0 ? `${p.models.length} models` : undefined,
|
||||
group: sourceLabels[p.source] || 'Other',
|
||||
}))
|
||||
}, [providers])
|
||||
|
||||
const form = useForm<AgentFormValues>({
|
||||
resolver: zodResolver(agentFormSchema),
|
||||
defaultValues: {
|
||||
@@ -66,9 +97,9 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
|
||||
prompt: editingAgent?.agent.prompt || '',
|
||||
mode: editingAgent?.agent.mode || 'subagent',
|
||||
temperature: editingAgent?.agent.temperature ?? 0.7,
|
||||
topP: editingAgent?.agent.topP ?? 1,
|
||||
modelId: editingAgent?.agent.model?.modelID || '',
|
||||
providerId: editingAgent?.agent.model?.providerID || '',
|
||||
topP: editingAgent?.agent.topP ?? editingAgent?.agent.top_p ?? 1,
|
||||
modelId: parsedModel.modelId,
|
||||
providerId: parsedModel.providerId,
|
||||
write: editingAgent?.agent.tools?.write ?? true,
|
||||
edit: editingAgent?.agent.tools?.edit ?? true,
|
||||
bash: editingAgent?.agent.tools?.bash ?? true,
|
||||
@@ -80,6 +111,23 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
|
||||
}
|
||||
})
|
||||
|
||||
const selectedProviderId = form.watch('providerId')
|
||||
|
||||
const modelOptions: ComboboxOption[] = useMemo(() => {
|
||||
const selectedProvider = providers.find(p => p.id === selectedProviderId)
|
||||
if (selectedProvider && selectedProvider.models.length > 0) {
|
||||
return selectedProvider.models.map(m => ({
|
||||
value: m.id,
|
||||
label: m.name || m.id,
|
||||
}))
|
||||
}
|
||||
return providers.flatMap(p => p.models.map(m => ({
|
||||
value: m.id,
|
||||
label: m.name || m.id,
|
||||
group: p.name || p.id,
|
||||
})))
|
||||
}, [providers, selectedProviderId])
|
||||
|
||||
const handleSubmit = (values: AgentFormValues) => {
|
||||
const agent: Agent = {
|
||||
prompt: values.prompt,
|
||||
@@ -101,11 +149,8 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
|
||||
}
|
||||
}
|
||||
|
||||
if (values.modelId || values.providerId) {
|
||||
agent.model = {
|
||||
modelID: values.modelId || '',
|
||||
providerID: values.providerId || ''
|
||||
}
|
||||
if (values.modelId && values.providerId) {
|
||||
agent.model = `${values.providerId}/${values.modelId}`
|
||||
}
|
||||
|
||||
onSubmit(values.name, agent)
|
||||
@@ -259,14 +304,17 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
|
||||
<div className="flex flex-col sm:grid grid-cols-2 gap-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="modelId"
|
||||
name="providerId"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Model ID</FormLabel>
|
||||
<FormLabel>Provider ID</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
{...field}
|
||||
placeholder="claude-3-5-sonnet-20241022"
|
||||
<Combobox
|
||||
value={field.value || ''}
|
||||
onChange={field.onChange}
|
||||
options={providerOptions}
|
||||
placeholder="Select or type provider..."
|
||||
allowCustomValue
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
@@ -276,14 +324,17 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="providerId"
|
||||
name="modelId"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Provider ID</FormLabel>
|
||||
<FormLabel>Model ID</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
{...field}
|
||||
placeholder="anthropic"
|
||||
<Combobox
|
||||
value={field.value || ''}
|
||||
onChange={field.onChange}
|
||||
options={modelOptions}
|
||||
placeholder="Select or type model..."
|
||||
allowCustomValue
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
|
||||
@@ -11,10 +11,8 @@ interface Agent {
|
||||
mode?: 'subagent' | 'primary' | 'all'
|
||||
temperature?: number
|
||||
topP?: number
|
||||
model?: {
|
||||
modelID: string
|
||||
providerID: string
|
||||
}
|
||||
top_p?: number
|
||||
model?: string
|
||||
tools?: Record<string, boolean>
|
||||
permission?: {
|
||||
edit?: 'ask' | 'allow' | 'deny'
|
||||
@@ -120,7 +118,7 @@ export function AgentsEditor({ agents, onChange }: AgentsEditorProps) {
|
||||
<p>Mode: {agent.mode}</p>
|
||||
{agent.temperature !== undefined && <p>Temperature: {agent.temperature}</p>}
|
||||
{agent.topP !== undefined && <p>Top P: {agent.topP}</p>}
|
||||
{agent.model?.modelID && <p>Model: {agent.model.providerID}/{agent.model.modelID}</p>}
|
||||
{agent.model && <p>Model: {agent.model}</p>}
|
||||
{agent.disable && <p>Status: Disabled</p>}
|
||||
</div>
|
||||
{agent.prompt && (
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
import { useState, useRef, useEffect, useCallback } from 'react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { ChevronDown } from 'lucide-react'
|
||||
|
||||
export interface ComboboxOption {
|
||||
value: string
|
||||
label: string
|
||||
description?: string
|
||||
group?: string
|
||||
}
|
||||
|
||||
interface ComboboxProps {
|
||||
value: string
|
||||
onChange: (value: string) => void
|
||||
options: ComboboxOption[]
|
||||
placeholder?: string
|
||||
disabled?: boolean
|
||||
className?: string
|
||||
allowCustomValue?: boolean
|
||||
}
|
||||
|
||||
export function Combobox({
|
||||
value,
|
||||
onChange,
|
||||
options,
|
||||
placeholder = 'Select or type...',
|
||||
disabled = false,
|
||||
className,
|
||||
allowCustomValue = true,
|
||||
}: ComboboxProps) {
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
const [inputValue, setInputValue] = useState(value)
|
||||
const [selectedIndex, setSelectedIndex] = useState(0)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
const listRef = useRef<HTMLDivElement>(null)
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
setInputValue(value)
|
||||
}, [value])
|
||||
|
||||
const filteredOptions = options.filter(option =>
|
||||
option.value.toLowerCase().includes(inputValue.toLowerCase()) ||
|
||||
option.label.toLowerCase().includes(inputValue.toLowerCase()) ||
|
||||
(option.description?.toLowerCase().includes(inputValue.toLowerCase()))
|
||||
)
|
||||
|
||||
const groupedOptions = filteredOptions.reduce((acc, option) => {
|
||||
const group = option.group || ''
|
||||
if (!acc[group]) acc[group] = []
|
||||
acc[group].push(option)
|
||||
return acc
|
||||
}, {} as Record<string, ComboboxOption[]>)
|
||||
|
||||
const flatFilteredOptions = Object.values(groupedOptions).flat()
|
||||
|
||||
useEffect(() => {
|
||||
setSelectedIndex(0)
|
||||
}, [inputValue])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen) return
|
||||
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
|
||||
setIsOpen(false)
|
||||
if (allowCustomValue) {
|
||||
onChange(inputValue)
|
||||
} else if (!options.some(o => o.value === inputValue)) {
|
||||
setInputValue(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside)
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside)
|
||||
}, [isOpen, inputValue, value, onChange, options, allowCustomValue])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen || !listRef.current) return
|
||||
|
||||
const items = listRef.current.querySelectorAll('[data-option]')
|
||||
const selectedItem = items[selectedIndex] as HTMLElement
|
||||
if (selectedItem) {
|
||||
selectedItem.scrollIntoView({ block: 'nearest' })
|
||||
}
|
||||
}, [selectedIndex, isOpen])
|
||||
|
||||
const handleSelect = useCallback((optionValue: string) => {
|
||||
setInputValue(optionValue)
|
||||
onChange(optionValue)
|
||||
setIsOpen(false)
|
||||
inputRef.current?.blur()
|
||||
}, [onChange])
|
||||
|
||||
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
||||
if (!isOpen) {
|
||||
if (e.key === 'ArrowDown' || e.key === 'Enter') {
|
||||
setIsOpen(true)
|
||||
e.preventDefault()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
switch (e.key) {
|
||||
case 'ArrowDown':
|
||||
e.preventDefault()
|
||||
setSelectedIndex(prev => Math.min(prev + 1, flatFilteredOptions.length - 1))
|
||||
break
|
||||
case 'ArrowUp':
|
||||
e.preventDefault()
|
||||
setSelectedIndex(prev => Math.max(prev - 1, 0))
|
||||
break
|
||||
case 'Enter':
|
||||
e.preventDefault()
|
||||
if (flatFilteredOptions[selectedIndex]) {
|
||||
handleSelect(flatFilteredOptions[selectedIndex].value)
|
||||
} else if (allowCustomValue && inputValue) {
|
||||
handleSelect(inputValue)
|
||||
}
|
||||
break
|
||||
case 'Escape':
|
||||
e.preventDefault()
|
||||
setIsOpen(false)
|
||||
setInputValue(value)
|
||||
break
|
||||
case 'Tab':
|
||||
setIsOpen(false)
|
||||
if (allowCustomValue) {
|
||||
onChange(inputValue)
|
||||
}
|
||||
break
|
||||
}
|
||||
}, [isOpen, selectedIndex, flatFilteredOptions, handleSelect, allowCustomValue, inputValue, value, onChange])
|
||||
|
||||
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const newValue = e.target.value
|
||||
setInputValue(newValue)
|
||||
setIsOpen(true)
|
||||
if (allowCustomValue) {
|
||||
onChange(newValue)
|
||||
}
|
||||
}, [allowCustomValue, onChange])
|
||||
|
||||
const handleFocus = useCallback(() => {
|
||||
setIsOpen(true)
|
||||
}, [])
|
||||
|
||||
let optionIndex = -1
|
||||
|
||||
return (
|
||||
<div ref={containerRef} className={cn('relative', className)}>
|
||||
<div className="relative">
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
value={inputValue}
|
||||
onChange={handleInputChange}
|
||||
onFocus={handleFocus}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={placeholder}
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors',
|
||||
'file:border-0 file:bg-transparent file:text-sm file:font-medium',
|
||||
'placeholder:text-muted-foreground',
|
||||
'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
'pr-8'
|
||||
)}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
if (!disabled) {
|
||||
setIsOpen(!isOpen)
|
||||
inputRef.current?.focus()
|
||||
}
|
||||
}}
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
|
||||
tabIndex={-1}
|
||||
>
|
||||
<ChevronDown className={cn('h-4 w-4 transition-transform', isOpen && 'rotate-180')} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{isOpen && flatFilteredOptions.length > 0 && (
|
||||
<div
|
||||
ref={listRef}
|
||||
className="absolute z-50 mt-1 w-full bg-popover border border-border rounded-md shadow-lg max-h-60 overflow-y-auto"
|
||||
>
|
||||
{Object.entries(groupedOptions).map(([group, groupOptions]) => (
|
||||
<div key={group || 'default'}>
|
||||
{group && (
|
||||
<div className="px-3 py-1.5 text-xs font-semibold text-muted-foreground bg-muted/50 sticky top-0">
|
||||
{group}
|
||||
</div>
|
||||
)}
|
||||
{groupOptions.map((option) => {
|
||||
optionIndex++
|
||||
const currentIndex = optionIndex
|
||||
const isSelected = currentIndex === selectedIndex
|
||||
|
||||
return (
|
||||
<button
|
||||
key={option.value}
|
||||
data-option
|
||||
type="button"
|
||||
onClick={() => handleSelect(option.value)}
|
||||
className={cn(
|
||||
'w-full px-3 py-2 text-left text-sm transition-colors',
|
||||
isSelected ? 'bg-accent text-accent-foreground' : 'hover:bg-muted'
|
||||
)}
|
||||
>
|
||||
<div className="font-medium">{option.label}</div>
|
||||
{option.description && (
|
||||
<div className="text-xs text-muted-foreground truncate">{option.description}</div>
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isOpen && flatFilteredOptions.length === 0 && inputValue && allowCustomValue && (
|
||||
<div className="absolute z-50 mt-1 w-full bg-popover border border-border rounded-md shadow-lg p-3">
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Press Enter to use "<span className="font-medium text-foreground">{inputValue}</span>"
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user