|
1 | | -import { useState } from 'react'; |
| 1 | +import { useState, useEffect } from 'react'; |
2 | 2 | import { createPortal } from 'react-dom'; |
3 | 3 | import { BookOpen, ChevronDown, ChevronRight, CheckCircle, X, Loader2 } from 'lucide-react'; |
4 | 4 | import { Input } from '../ui/Input'; |
5 | 5 | import { Button } from '../ui/Button'; |
6 | 6 | import { Switch } from '../ui/Switch'; |
7 | 7 | import { MetadataOverrideForm } from './MetadataOverrideForm'; |
8 | 8 | import { useMetadataEditor } from '../../hooks/useMetadataEditor'; |
| 9 | +import { api } from '../../lib/api'; |
9 | 10 | import type { |
10 | 11 | Alias, |
11 | 12 | AliasMetadata, |
@@ -45,6 +46,33 @@ export function ModelMetadataEditor({ editingAlias, setEditingAlias, isModalOpen |
45 | 46 | buildCustomDefaults, |
46 | 47 | } = useMetadataEditor(editingAlias, setEditingAlias, isModalOpen); |
47 | 48 |
|
| 49 | + // ── Pi model selector state ────────────────────────────────────────── |
| 50 | + const [piProviders, setPiProviders] = useState<string[]>([]); |
| 51 | + const [piModels, setPiModels] = useState<Array<{ id: string; name: string; api: string }>>([]); |
| 52 | + const [piModelsLoading, setPiModelsLoading] = useState(false); |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + if (!isOpen) return; |
| 56 | + api |
| 57 | + .getPiProviders() |
| 58 | + .then(setPiProviders) |
| 59 | + .catch(() => {}); |
| 60 | + }, [isOpen]); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + const provider = editingAlias.pi_model?.provider; |
| 64 | + if (!provider) { |
| 65 | + setPiModels([]); |
| 66 | + return; |
| 67 | + } |
| 68 | + setPiModelsLoading(true); |
| 69 | + api |
| 70 | + .getPiModels(provider) |
| 71 | + .then(setPiModels) |
| 72 | + .catch(() => setPiModels([])) |
| 73 | + .finally(() => setPiModelsLoading(false)); |
| 74 | + }, [editingAlias.pi_model?.provider]); |
| 75 | + |
48 | 76 | return ( |
49 | 77 | <> |
50 | 78 | <div className="border border-border-glass rounded-sm overflow-hidden"> |
@@ -272,6 +300,134 @@ export function ModelMetadataEditor({ editingAlias, setEditingAlias, isModalOpen |
272 | 300 | </div> |
273 | 301 | )} |
274 | 302 |
|
| 303 | + {/* Pi model */} |
| 304 | + <div> |
| 305 | + <label |
| 306 | + className="font-body text-[12px] font-medium text-text-secondary" |
| 307 | + style={{ display: 'block', marginBottom: '4px' }} |
| 308 | + > |
| 309 | + Pi model |
| 310 | + </label> |
| 311 | + <p className="font-body text-[11px] text-text-muted" style={{ marginBottom: '6px' }}> |
| 312 | + Link to a pi-ai model to include its compatibility options as{' '} |
| 313 | + <code className="text-primary">pi_options</code> in{' '} |
| 314 | + <code className="text-primary">GET /v1/models</code>. |
| 315 | + </p> |
| 316 | + <div style={{ display: 'flex', gap: '6px', alignItems: 'center' }}> |
| 317 | + {/* Provider dropdown */} |
| 318 | + <select |
| 319 | + className="font-body text-xs text-text bg-bg-glass border border-border-glass rounded-sm outline-none transition-all duration-200 backdrop-blur-md focus:border-primary" |
| 320 | + style={{ |
| 321 | + padding: '5px 8px', |
| 322 | + height: '30px', |
| 323 | + flex: '0 0 auto', |
| 324 | + maxWidth: '160px', |
| 325 | + }} |
| 326 | + value={editingAlias.pi_model?.provider ?? ''} |
| 327 | + onChange={(e) => { |
| 328 | + const provider = e.target.value; |
| 329 | + if (!provider) { |
| 330 | + const { pi_model: _removed, ...rest } = editingAlias; |
| 331 | + setEditingAlias(rest as Alias); |
| 332 | + } else { |
| 333 | + setEditingAlias({ ...editingAlias, pi_model: { provider, model_id: '' } }); |
| 334 | + } |
| 335 | + }} |
| 336 | + > |
| 337 | + <option value="">None</option> |
| 338 | + {piProviders.map((p) => ( |
| 339 | + <option key={p} value={p}> |
| 340 | + {p} |
| 341 | + </option> |
| 342 | + ))} |
| 343 | + </select> |
| 344 | + |
| 345 | + {/* Model dropdown */} |
| 346 | + {editingAlias.pi_model?.provider && ( |
| 347 | + <div style={{ position: 'relative', flex: 1 }}> |
| 348 | + <select |
| 349 | + className="w-full font-body text-xs text-text bg-bg-glass border border-border-glass rounded-sm outline-none transition-all duration-200 backdrop-blur-md focus:border-primary" |
| 350 | + style={{ |
| 351 | + padding: '5px 8px', |
| 352 | + height: '30px', |
| 353 | + paddingRight: piModelsLoading ? '28px' : undefined, |
| 354 | + }} |
| 355 | + value={editingAlias.pi_model?.model_id ?? ''} |
| 356 | + onChange={(e) => { |
| 357 | + const model_id = e.target.value; |
| 358 | + setEditingAlias({ |
| 359 | + ...editingAlias, |
| 360 | + pi_model: { provider: editingAlias.pi_model!.provider, model_id }, |
| 361 | + }); |
| 362 | + }} |
| 363 | + > |
| 364 | + <option value="">Select model...</option> |
| 365 | + {piModels.map((m) => ( |
| 366 | + <option key={m.id} value={m.id}> |
| 367 | + {m.name} ({m.id}) |
| 368 | + </option> |
| 369 | + ))} |
| 370 | + </select> |
| 371 | + {piModelsLoading && ( |
| 372 | + <Loader2 |
| 373 | + size={14} |
| 374 | + className="animate-spin text-text-muted" |
| 375 | + style={{ |
| 376 | + position: 'absolute', |
| 377 | + right: '8px', |
| 378 | + top: '50%', |
| 379 | + transform: 'translateY(-50%)', |
| 380 | + pointerEvents: 'none', |
| 381 | + }} |
| 382 | + /> |
| 383 | + )} |
| 384 | + </div> |
| 385 | + )} |
| 386 | + |
| 387 | + {/* Clear pi model */} |
| 388 | + {editingAlias.pi_model?.model_id && ( |
| 389 | + <Button |
| 390 | + variant="ghost" |
| 391 | + size="sm" |
| 392 | + onClick={() => { |
| 393 | + const { pi_model: _removed, ...rest } = editingAlias; |
| 394 | + setEditingAlias(rest as Alias); |
| 395 | + }} |
| 396 | + style={{ |
| 397 | + color: 'var(--color-danger)', |
| 398 | + padding: '4px', |
| 399 | + minHeight: 'auto', |
| 400 | + flex: '0 0 auto', |
| 401 | + }} |
| 402 | + title="Remove pi model" |
| 403 | + > |
| 404 | + <X size={14} /> |
| 405 | + </Button> |
| 406 | + )} |
| 407 | + </div> |
| 408 | + |
| 409 | + {/* Confirmation badge */} |
| 410 | + {editingAlias.pi_model?.model_id && ( |
| 411 | + <div |
| 412 | + className="rounded-sm border border-border-glass bg-bg-subtle px-3 py-2" |
| 413 | + style={{ |
| 414 | + fontSize: '11px', |
| 415 | + color: 'var(--color-text-secondary)', |
| 416 | + marginTop: '6px', |
| 417 | + }} |
| 418 | + > |
| 419 | + <div style={{ display: 'flex', alignItems: 'center', gap: '6px' }}> |
| 420 | + <CheckCircle size={12} className="text-success" /> |
| 421 | + <span> |
| 422 | + Pi model: <strong>{editingAlias.pi_model.provider}</strong> |
| 423 | + {' / '} |
| 424 | + <code className="text-primary">{editingAlias.pi_model.model_id}</code> |
| 425 | + </span> |
| 426 | + </div> |
| 427 | + </div> |
| 428 | + )} |
| 429 | + </div> |
| 430 | + |
275 | 431 | {/* Preferred API */} |
276 | 432 | <div> |
277 | 433 | <label |
|
0 commit comments