File size: 4,263 Bytes
212c959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useMemo, useState } from 'react'
import { Check } from 'lucide-react'
import Modal from './Modal'
import Button from './Button'

export default function ModelDialog({
  open,
  onOpenChange,
  registry,
  currentProvider,
  currentModel,
  onSelect,
}) {
  const providerIds = useMemo(() => Object.keys(registry || {}), [registry])
  const [activeProvider, setActiveProvider] = useState(currentProvider || providerIds[0] || 'groq')
  const provider = registry?.[activeProvider]
  const models = provider?.models || []
  const providerEnabled = provider?.enabled !== false

  useEffect(() => {
    if (!open || !providerIds.length) return
    setActiveProvider(currentProvider || providerIds[0])
  }, [open, currentProvider, providerIds])

  return (
    <Modal
      open={open}
      onOpenChange={onOpenChange}
      size="lg"
      title="Models"
    >
      <div className="grid gap-4 lg:grid-cols-[200px_minmax(0,1fr)]">
        <div className="space-y-2">
          {providerIds.map((providerId) => (
            <button
              key={providerId}
              type="button"
              onClick={() => setActiveProvider(providerId)}
              className={`w-full rounded-lg border px-3 py-2.5 text-left text-sm transition ${
                activeProvider === providerId
                  ? 'border-border bg-secondary text-foreground'
                  : 'border-border bg-panel text-muted-foreground hover:bg-secondary hover:text-foreground'
              }`}
            >
              <div className="flex items-center justify-between gap-2">
                <p className="truncate font-medium">{registry[providerId].label}</p>
                <span
                  className={`h-2 w-2 rounded-full ${
                    registry[providerId].enabled !== false
                      ? 'bg-accent'
                      : 'bg-danger'
                  }`}
                  aria-label={registry[providerId].enabled !== false ? 'Connected' : 'Setup needed'}
                />
              </div>
            </button>
          ))}
        </div>

        <div className="space-y-3">
          {providerEnabled ? null : (
            <div className="rounded-lg border border-danger/25 bg-danger/5 px-4 py-3 text-sm text-muted-foreground">
              {provider?.reason || 'This provider is not configured for the current deployment.'}
            </div>
          )}
          {models.map((model) => {
            const active = currentProvider === activeProvider && currentModel === model.id
            return (
              <button
                key={model.id}
                type="button"
                disabled={!providerEnabled}
                onClick={() => {
                  if (!providerEnabled) return
                  onSelect(activeProvider, model.id)
                  onOpenChange(false)
                }}
                className={`w-full rounded-lg border px-4 py-3 text-left transition disabled:cursor-not-allowed disabled:opacity-60 ${
                  active
                    ? 'border-border bg-secondary'
                    : 'border-border bg-panel hover:bg-secondary'
                }`}
              >
                <div className="flex items-center justify-between gap-3">
                  <div className="min-w-0">
                    <p className="truncate font-medium text-foreground">{model.name}</p>
                    <p className="truncate text-sm text-muted-foreground">{model.id}</p>
                  </div>
                  {active ? (
                    <div className="rounded-lg bg-accent p-2 text-accent-foreground">
                      <Check className="h-4 w-4" />
                    </div>
                  ) : null}
                </div>
              </button>
            )
          })}
          {!models.length ? (
            <div className="rounded-lg border border-border bg-panel px-4 py-10 text-center text-sm text-muted-foreground">
              No models available for this provider.
            </div>
          ) : null}
        </div>
      </div>
      <div className="mt-5 flex justify-end">
        <Button variant="ghost" onClick={() => onOpenChange(false)}>
          Close
        </Button>
      </div>
    </Modal>
  )
}