Spaces:
Configuration error
Configuration error
File size: 7,747 Bytes
bcce530 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | "use client"
import { useOllama } from "@/contexts/ollama-context"
import { AI_MODELS, OLLAMA_MODELS, isOllamaModel } from "@/types/prompt"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
Cloud,
Server,
ChevronDown,
Check
} from "lucide-react"
import { useState, useRef, useEffect } from "react"
import { cn } from "@/lib/utils"
interface ModelSelectorProps {
value: string
onChange: (model: string) => void
disabled?: boolean
showOllama?: boolean
compact?: boolean
}
export function ModelSelector({
value,
onChange,
disabled = false,
showOllama = true,
compact = false
}: ModelSelectorProps) {
const { settings } = useOllama()
const [open, setOpen] = useState(false)
const dropdownRef = useRef<HTMLDivElement>(null)
// Close on click outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setOpen(false)
}
}
document.addEventListener("mousedown", handleClickOutside)
return () => document.removeEventListener("mousedown", handleClickOutside)
}, [])
// Get current model info
const isOllama = isOllamaModel(value)
const currentModel = isOllama
? (OLLAMA_MODELS as Record<string, any>)[value] || { name: value }
: (AI_MODELS as Record<string, any>)[value] || { name: value }
// Build cloud models list
const cloudModels = Object.entries(AI_MODELS).map(([id, config]) => ({
id,
...config,
isOllama: false,
}))
// Build Ollama models list (from detected + predefined)
const ollamaModels = settings.enabled
? settings.availableModels.map(name => ({
id: name,
name: name,
description: "Local Ollama model",
isOllama: true,
}))
: []
return (
<div className="relative" ref={dropdownRef}>
<Button
variant="outline"
size={compact ? "sm" : "default"}
onClick={() => !disabled && setOpen(!open)}
disabled={disabled}
className={cn(
"justify-between gap-2",
compact ? "w-auto" : "w-full"
)}
>
<div className="flex items-center gap-2">
{isOllama ? (
<Server className="h-4 w-4 text-green-500" />
) : (
<Cloud className="h-4 w-4 text-blue-500" />
)}
<span className="truncate">{currentModel.name || value}</span>
</div>
<ChevronDown className={cn("h-4 w-4 transition-transform", open && "rotate-180")} />
</Button>
{open && (
<div className="absolute z-50 mt-1 w-72 max-h-80 overflow-auto rounded-lg border bg-popover shadow-lg">
{/* Cloud Models */}
<div className="p-2 border-b">
<p className="text-xs font-medium text-muted-foreground px-2 py-1 flex items-center gap-1">
<Cloud className="h-3 w-3" />
Cloud Models
</p>
{cloudModels.map((model) => (
<button
key={model.id}
onClick={() => {
onChange(model.id)
setOpen(false)
}}
className={cn(
"w-full px-2 py-2 text-left rounded-md hover:bg-muted flex items-center justify-between",
value === model.id && "bg-muted"
)}
>
<div>
<p className="text-sm font-medium">{model.name}</p>
<p className="text-xs text-muted-foreground">{model.description}</p>
</div>
{value === model.id && <Check className="h-4 w-4 text-primary" />}
</button>
))}
</div>
{/* Ollama Models */}
{showOllama && (
<div className="p-2">
<p className="text-xs font-medium text-muted-foreground px-2 py-1 flex items-center gap-1">
<Server className="h-3 w-3" />
Ollama Models
{!settings.enabled && (
<Badge variant="outline" className="ml-1 text-xs">
Not connected
</Badge>
)}
</p>
{ollamaModels.length > 0 ? (
ollamaModels.map((model) => (
<button
key={model.id}
onClick={() => {
onChange(model.id)
setOpen(false)
}}
className={cn(
"w-full px-2 py-2 text-left rounded-md hover:bg-muted flex items-center justify-between",
value === model.id && "bg-muted"
)}
>
<div>
<p className="text-sm font-medium">{model.name}</p>
<p className="text-xs text-muted-foreground">{model.description}</p>
</div>
{value === model.id && <Check className="h-4 w-4 text-green-500" />}
</button>
))
) : (
<p className="px-2 py-2 text-xs text-muted-foreground">
{settings.enabled
? "No models found. Run: ollama pull llama3.2"
: "Click 'Ollama' in header to connect"
}
</p>
)}
</div>
)}
</div>
)}
</div>
)
}
// Simple inline selector for compact spaces
export function ModelBadge({ model }: { model: string }) {
const isOllama = isOllamaModel(model)
const modelInfo = isOllama
? (OLLAMA_MODELS as Record<string, any>)[model]
: (AI_MODELS as Record<string, any>)[model]
return (
<Badge
variant="outline"
className={cn(
"gap-1",
isOllama ? "border-green-500/50" : "border-blue-500/50"
)}
>
{isOllama ? (
<Server className="h-3 w-3 text-green-500" />
) : (
<Cloud className="h-3 w-3 text-blue-500" />
)}
{modelInfo?.name || model}
</Badge>
)
}
|