File size: 11,583 Bytes
0bcd90f
51f2163
33e7e31
51f2163
0bcd90f
 
 
 
 
 
 
 
 
 
 
33e7e31
0bcd90f
 
 
33e7e31
 
 
0bcd90f
 
 
33e7e31
 
 
0bcd90f
 
 
33e7e31
 
 
0bcd90f
 
 
33e7e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51f2163
 
33e7e31
 
 
 
 
0bcd90f
 
33e7e31
0bcd90f
 
 
33e7e31
0bcd90f
 
 
 
33e7e31
 
 
0bcd90f
 
33e7e31
 
 
 
 
 
 
 
 
0bcd90f
 
 
 
51f2163
33e7e31
 
 
0bcd90f
 
33e7e31
0bcd90f
f6b47db
33e7e31
0bcd90f
51f2163
 
0bcd90f
33e7e31
 
 
 
 
 
 
 
 
 
 
f6b47db
51f2163
33e7e31
 
 
51f2163
33e7e31
51f2163
 
 
 
 
 
 
 
 
33e7e31
0bcd90f
33e7e31
 
51f2163
33e7e31
 
0bcd90f
51f2163
33e7e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51f2163
0bcd90f
33e7e31
 
 
 
 
 
 
 
 
0bcd90f
33e7e31
 
 
 
0bcd90f
33e7e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bcd90f
 
 
33e7e31
 
 
0bcd90f
 
51f2163
33e7e31
 
0bcd90f
51f2163
33e7e31
 
 
 
 
 
 
51f2163
 
 
 
 
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { useEffect, useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { X, ChevronDown, ChevronUp, Eye, EyeOff, Check } from 'lucide-react';

type GroupKey = 'group1' | 'group2' | 'group3';
type GroupState = Record<GroupKey, { model: string; apiKey: string; baseUrl: string }>;

const DEFAULT_GROUPS: GroupState = {
  group1: { model: '', apiKey: '', baseUrl: '' },
  group2: { model: '', apiKey: '', baseUrl: '' },
  group3: { model: '', apiKey: '', baseUrl: '' },
};

const GROUP_META: Record<
  GroupKey,
  { title: string; roles: string; example: string; modelHint: string }
> = {
  group1: {
    title: 'Fix & Debug Agents',
    roles: 'Fixer · Debugger · Reasoner',
    example: 'openai/deepseek-ai/deepseek-v3.2',
    modelHint: 'Strong reasoning model',
  },
  group2: {
    title: 'Core Build Agents',
    roles: 'Architect · Designer · Testbench · Verifier · Manager · Physical',
    example: 'glm-4-plus',
    modelHint: 'Strong coding/planning model',
  },
  group3: {
    title: 'Documentation Agents',
    roles: 'Documenter · Reporter',
    example: 'groq/llama-3.3-70b-versatile',
    modelHint: 'Fast text generation model',
  },
};

const MaskedKey = ({ value }: { value: string }) => {
  const [visible, setVisible] = useState(false);
  if (!value) return null;
  const masked = value.length > 8 ? `${value.slice(0, 6)}${'•'.repeat(Math.min(20, value.length - 10))}${value.slice(-4)}` : '••••••••';
  return (
    <button
      className="byok-key-peek"
      onClick={() => setVisible(!visible)}
      type="button"
      title={visible ? 'Hide key' : 'Show key'}
    >
      {visible ? <EyeOff size={14} /> : <Eye size={14} />}
      <code>{visible ? value : masked}</code>
    </button>
  );
};

const ByokGroupCard = ({
  groupKey, index, meta, group, onUpdate,
}: {
  groupKey: GroupKey;
  index: number;
  meta: { title: string; roles: string; example: string; modelHint: string };
  group: { model: string; apiKey: string; baseUrl: string };
  onUpdate: (key: GroupKey, field: 'model' | 'apiKey' | 'baseUrl', value: string) => void;
}) => {
  const [open, setOpen] = useState(index === 0 || !!group.apiKey);

  return (
    <div className={`byok-group${open ? ' byok-group--open' : ''}`}>
      <button className="byok-group-header" onClick={() => setOpen(!open)} type="button">
        <div>
          <span className="byok-group-index">{index + 1}</span>
          <span className="byok-group-title">{meta.title}</span>
          <span className="byok-group-roles">{meta.roles}</span>
        </div>
        {open ? <ChevronUp size={16} /> : <ChevronDown size={16} />}
      </button>
      {open && (
        <div className="byok-group-body">
          <p className="byok-group-hint">Recommended: {meta.example}</p>
          <div className="byok-field-row">
            <div className="byok-field">
              <label className="byok-field-label">Model</label>
              <input
                className="byok-field-input"
                placeholder={meta.modelHint}
                value={group.model}
                onChange={(e) => onUpdate(groupKey, 'model', e.target.value)}
              />
            </div>
            <div className="byok-field">
              <label className="byok-field-label">Base URL <span className="byok-optional">(optional)</span></label>
              <input
                className="byok-field-input"
                placeholder="Leave blank for hosted"
                value={group.baseUrl}
                onChange={(e) => onUpdate(groupKey, 'baseUrl', e.target.value)}
              />
            </div>
          </div>
          <div className="byok-field">
            <label className="byok-field-label">API Key</label>
            <input
              className="byok-field-input"
              type="password"
              placeholder="API Key"
              value={group.apiKey}
              onChange={(e) => onUpdate(groupKey, 'apiKey', e.target.value)}
            />
            <MaskedKey value={group.apiKey} />
          </div>
        </div>
      )}
    </div>
  );
};

export const BillingModal = ({
  isOpen,
  onClose,
  onKeySaved,
}: {
  isOpen: boolean;
  onClose: () => void;
  onKeySaved: () => void;
}) => {
  const [groups, setGroups] = useState<GroupState>(DEFAULT_GROUPS);
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState('');
  const [quickMode, setQuickMode] = useState(true);
  const [quickKey, setQuickKey] = useState('');
  const [quickModel, setQuickModel] = useState('');
  const [quickBaseUrl, setQuickBaseUrl] = useState('');
  const [saved, setSaved] = useState(false);

  useEffect(() => {
    if (!isOpen) { setSaved(false); return; }
    setError('');
    try {
      const raw = localStorage.getItem('agentic_byok_key');
      if (!raw) { setGroups(DEFAULT_GROUPS); return; }
      const parsed = JSON.parse(raw);
      if (!parsed || typeof parsed !== 'object') return;

      const nextGroups: GroupState = {
        group1: { model: parsed.group1?.model || '', apiKey: parsed.group1?.api_key || '', baseUrl: parsed.group1?.base_url || '' },
        group2: { model: parsed.group2?.model || '', apiKey: parsed.group2?.api_key || '', baseUrl: parsed.group2?.base_url || '' },
        group3: { model: parsed.group3?.model || '', apiKey: parsed.group3?.api_key || '', baseUrl: parsed.group3?.base_url || '' },
      };
      setGroups(nextGroups);

      // If all keys are the same, show quick mode
      const k1 = nextGroups.group1.apiKey;
      if (k1 && k1 === nextGroups.group2.apiKey && k1 === nextGroups.group3.apiKey) {
        setQuickKey(k1);
        setQuickModel(nextGroups.group1.model);
        setQuickBaseUrl(nextGroups.group1.baseUrl);
        setQuickMode(true);
      }
    } catch {
      setGroups(DEFAULT_GROUPS);
    }
  }, [isOpen]);

  const hasAnyKey = quickMode
    ? quickKey.trim().length > 0
    : Object.values(groups).some((g) => g.apiKey.trim());

  const updateGroup = (key: GroupKey, field: 'model' | 'apiKey' | 'baseUrl', value: string) => {
    setGroups((prev) => ({ ...prev, [key]: { ...prev[key], [field]: value } }));
  };

  const handleSave = async () => {
    if (!hasAnyKey) return;
    setSaving(true);
    setError('');

    let payload: any;
    if (quickMode) {
      const common = { model: quickModel, api_key: quickKey, base_url: quickBaseUrl };
      payload = { group1: { ...common }, group2: { ...common }, group3: { ...common } };
    } else {
      payload = {
        group1: { model: groups.group1.model, api_key: groups.group1.apiKey, base_url: groups.group1.baseUrl },
        group2: { model: groups.group2.model, api_key: groups.group2.apiKey, base_url: groups.group2.baseUrl },
        group3: { model: groups.group3.model, api_key: groups.group3.apiKey, base_url: groups.group3.baseUrl },
      };
    }

    try {
      localStorage.setItem('agentic_byok_key', JSON.stringify(payload));
      setSaved(true);
      setTimeout(() => { onKeySaved(); onClose(); }, 600);
    } catch (err: any) {
      setError(err.message || 'Failed to save keys.');
    } finally {
      setSaving(false);
    }
  };

  if (!isOpen) return null;

  return (
    <AnimatePresence>
      <div className="byok-overlay" onClick={onClose}>
        <motion.div
          className="byok-dialog"
          initial={{ opacity: 0, scale: 0.97, y: 8 }}
          animate={{ opacity: 1, scale: 1, y: 0 }}
          exit={{ opacity: 0, scale: 0.97, y: 8 }}
          transition={{ duration: 0.18 }}
          onClick={(e) => e.stopPropagation()}
        >
          {/* Header */}
          <div className="byok-header">
            <div>
              <h2 className="byok-title">Configure API Keys</h2>
              <p className="byok-subtitle">
                Bring your own LLM keys. Works with any OpenAI-compatible provider.
              </p>
            </div>
            <button className="byok-close" onClick={onClose} aria-label="Close">
              <X size={18} />
            </button>
          </div>

          {/* Mode toggle */}
          <div className="byok-mode-toggle">
            <button
              className={`byok-mode-btn${quickMode ? ' byok-mode-btn--active' : ''}`}
              onClick={() => setQuickMode(true)}
            >
              Quick Setup
            </button>
            <button
              className={`byok-mode-btn${!quickMode ? ' byok-mode-btn--active' : ''}`}
              onClick={() => setQuickMode(false)}
            >
              Advanced
            </button>
          </div>

          {/* Quick mode */}
          {quickMode && (
            <div className="byok-quick">
              <p className="byok-quick-hint">
                One key for all agent groups. Perfect if you're using a single provider.
              </p>
              <div className="byok-field-row">
                <div className="byok-field">
                  <label className="byok-field-label">Model</label>
                  <input
                    className="byok-field-input"
                    placeholder="e.g. gpt-4o, glm-4-plus, groq/llama-3.3-70b"
                    value={quickModel}
                    onChange={(e) => setQuickModel(e.target.value)}
                  />
                </div>
                <div className="byok-field">
                  <label className="byok-field-label">Base URL <span className="byok-optional">(optional)</span></label>
                  <input
                    className="byok-field-input"
                    placeholder="Leave blank for hosted providers"
                    value={quickBaseUrl}
                    onChange={(e) => setQuickBaseUrl(e.target.value)}
                  />
                </div>
              </div>
              <div className="byok-field">
                <label className="byok-field-label">API Key</label>
                <input
                  className="byok-field-input"
                  type="password"
                  placeholder="sk-... or provider-specific key"
                  value={quickKey}
                  onChange={(e) => setQuickKey(e.target.value)}
                  autoFocus
                />
                <MaskedKey value={quickKey} />
              </div>
            </div>
          )}

          {/* Advanced mode */}
          {!quickMode && (
            <div className="byok-advanced">
              {(['group1', 'group2', 'group3'] as GroupKey[]).map((key, index) => (
                <ByokGroupCard
                  key={key}
                  groupKey={key}
                  index={index}
                  meta={GROUP_META[key]}
                  group={groups[key]}
                  onUpdate={updateGroup}
                />
              ))}
            </div>
          )}

          {error && <div className="byok-error">{error}</div>}

          {/* Actions */}
          <div className="byok-footer">
            <button className="byok-cancel-btn" onClick={onClose} disabled={saving}>
              Cancel
            </button>
            <button
              className={`byok-save-btn${saved ? ' byok-save-btn--done' : ''}`}
              onClick={handleSave}
              disabled={saving || !hasAnyKey}
            >
              {saved ? (
                <><Check size={16} /> Saved</>
              ) : saving ? (
                'Saving…'
              ) : (
                'Save Keys'
              )}
            </button>
          </div>
        </motion.div>
      </div>
    </AnimatePresence>
  );
};