File size: 5,741 Bytes
4e039c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from 'react'
import { Plus, Trash2, RefreshCw, X, Server } from 'lucide-react'
import { useStore } from '../../store/useStore'
import { PROVIDER_PRESETS, fetchModels } from '../../lib/providers'
import { Btn, Field, Section, inputCls } from '../ui'

function ProviderCard({ p }) {
  const { updateProvider, removeProvider } = useStore.getState()
  const [newModel, setNewModel] = useState('')
  const [busy, setBusy] = useState(false)
  const [msg, setMsg] = useState('')
  const [filter, setFilter] = useState('')
  const up = (patch) => updateProvider(p.id, patch)

  const addModel = () => {
    const m = newModel.trim()
    if (m && !p.models.includes(m)) up({ models: [...p.models, m] })
    setNewModel('')
  }
  const doFetch = async () => {
    setBusy(true); setMsg('')
    try {
      const list = await fetchModels(p)
      up({ models: [...new Set([...p.models, ...list])] })
      setMsg(`抓到 ${list.length} 個模型`)
    } catch (e) {
      setMsg(`抓取失敗:${e.message}(可手動新增)`)
    } finally { setBusy(false) }
  }
  const shown = filter ? p.models.filter((m) => m.toLowerCase().includes(filter.toLowerCase())) : p.models

  return (
    <details className="bg-field rounded-lg">
      <summary className="cursor-pointer px-3 py-2.5 text-sm flex items-center gap-2">
        <Server className="w-3.5 h-3.5 text-muted" />
        <span className="flex-1 truncate font-medium">{p.name}</span>
        <span className={`text-[11px] ${p.noKey || p.apiKey ? 'text-emerald-500' : 'text-red-400'}`}>
          {p.noKey ? '免金鑰' : p.apiKey ? '已填金鑰' : '未填金鑰'}
        </span>
        <span className="text-[11px] text-muted">{p.models.length}</span>
      </summary>
      <div className="px-3 pb-3 space-y-3">
        <Field label="名稱"><input className={inputCls} value={p.name} onChange={(e) => up({ name: e.target.value })} /></Field>
        <Field label="API URL(OpenAI 相容,到 /v1 為止)">
          <input className={inputCls} value={p.baseUrl} onChange={(e) => up({ baseUrl: e.target.value })} placeholder="https://.../v1" />
        </Field>
        <Field label="API Key" hint="只存在瀏覽器 localStorage,直接從瀏覽器連到供應商。">
          <input type="password" className={inputCls} value={p.apiKey} onChange={(e) => up({ apiKey: e.target.value })} placeholder="sk-..." disabled={p.noKey} />
        </Field>
        <label className="flex items-center gap-2 text-xs text-muted">
          <input type="checkbox" checked={!!p.noKey} onChange={(e) => up({ noKey: e.target.checked })} /> 不需要金鑰(本機模型)
        </label>

        <div>
          <div className="text-xs text-muted mb-1 flex items-center justify-between">
            模型({p.models.length})
            <button type="button" onClick={doFetch} disabled={busy} className="inline-flex items-center gap-1 text-purple-400 hover:underline disabled:opacity-50">
              <RefreshCw className={`w-3 h-3 ${busy ? 'animate-spin' : ''}`} /> 抓取模型
            </button>
          </div>
          {msg && <p className="text-[11px] text-muted mb-1">{msg}</p>}
          {p.models.length > 8 && (
            <input className={`${inputCls} mb-2`} placeholder="篩選模型…" value={filter} onChange={(e) => setFilter(e.target.value)} />
          )}
          <div className="max-h-48 overflow-y-auto space-y-1">
            {shown.map((m) => (
              <div key={m} className="flex items-center gap-2 text-xs bg-app rounded px-2 py-1">
                <span className="flex-1 break-all">{m}</span>
                <button type="button" className="text-muted hover:text-red-500" onClick={() => up({ models: p.models.filter((x) => x !== m) })}><X className="w-3 h-3" /></button>
              </div>
            ))}
          </div>
          <div className="flex gap-2 mt-2">
            <input className={inputCls} value={newModel} onChange={(e) => setNewModel(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && addModel()} placeholder="手動新增模型 id" />
            <Btn onClick={addModel}><Plus className="w-4 h-4" /></Btn>
          </div>
          {p.models.length > 0 && (
            <button type="button" className="text-[11px] text-muted hover:text-red-400 mt-1" onClick={() => confirm('清空這個供應商的模型清單?') && up({ models: [] })}>
              清空清單
            </button>
          )}
        </div>

        <Btn variant="danger" className="w-full" onClick={() => confirm(`刪除供應商「${p.name}」?`) && removeProvider(p.id)}>
          <Trash2 className="w-4 h-4" /> 刪除供應商
        </Btn>
      </div>
    </details>
  )
}

export default function ModelsPanel() {
  const providers = useStore((s) => s.providers)
  const [preset, setPreset] = useState(0)
  return (
    <div>
      <Section title={`供應商(${providers.length})`}>
        <p className="text-[11px] text-muted">
          可同時設定多個供應商、每個供應商多個模型;在「對話」頁替每個對話選用,或在專案設定預設值。
        </p>
        <div className="space-y-2">
          {providers.map((p) => <ProviderCard key={p.id} p={p} />)}
        </div>
      </Section>
      <Section title="新增供應商">
        <select className={inputCls} value={preset} onChange={(e) => setPreset(Number(e.target.value))}>
          {PROVIDER_PRESETS.map((p, i) => <option key={p.name} value={i}>{p.name}</option>)}
        </select>
        <Btn variant="primary" className="w-full" onClick={() => useStore.getState().addProvider(PROVIDER_PRESETS[preset])}>
          <Plus className="w-4 h-4" /> 新增
        </Btn>
      </Section>
    </div>
  )
}