import { Combobox } from "@kobalte/core/combobox" import { createEffect, createMemo, createSignal } from "solid-js" import { providers, fetchProviders } from "../stores/sessions" import { ChevronDown } from "lucide-solid" import type { Model } from "../types/session" import Kbd from "./kbd" interface ModelSelectorProps { instanceId: string sessionId: string currentModel: { providerId: string; modelId: string } onModelChange: (model: { providerId: string; modelId: string }) => Promise } interface FlatModel extends Model { providerName: string key: string searchText: string } export default function ModelSelector(props: ModelSelectorProps) { const instanceProviders = () => providers().get(props.instanceId) || [] const [isOpen, setIsOpen] = createSignal(false) let triggerRef!: HTMLButtonElement let searchInputRef!: HTMLInputElement createEffect(() => { if (instanceProviders().length === 0) { fetchProviders(props.instanceId).catch(console.error) } }) const allModels = createMemo(() => instanceProviders().flatMap((p) => p.models.map((m) => ({ ...m, providerName: p.name, key: `${m.providerId}/${m.id}`, searchText: `${m.name} ${p.name} ${m.providerId} ${m.id} ${m.providerId}/${m.id}`, })), ), ) const currentModelValue = createMemo(() => allModels().find((m) => m.providerId === props.currentModel.providerId && m.id === props.currentModel.modelId), ) const handleChange = async (value: FlatModel | null) => { if (!value) return await props.onModelChange({ providerId: value.providerId, modelId: value.id }) } const customFilter = (option: FlatModel, inputValue: string) => { return option.searchText.toLowerCase().includes(inputValue.toLowerCase()) } createEffect(() => { if (isOpen()) { setTimeout(() => { searchInputRef?.focus() }, 100) } }) return ( ) }