File size: 19,934 Bytes
eeb9404 | 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | 'use client';
import React, { useState, useEffect } from 'react';
import { configManager } from '@/lib/config/storage';
import { validateApiKey as checkApiKey } from '@/lib/llm/llm-client';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { Eye, EyeOff, Check, X, ExternalLink, Loader2 } from 'lucide-react';
import { toast } from 'sonner';
import { ModelSelector } from '@/components/model-selector';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { ProviderId } from '@/lib/llm/providers/types';
import { getOfferableProviders, getProvider } from '@/lib/llm/providers/registry';
import { CodexAuthPanel } from '@/components/settings/codex-auth-panel';
import { HFAuthPanel } from '@/components/settings/hf-auth-panel';
import { ConnectionBadge } from '@/components/settings/connection-badge';
import { checkHFCapabilities } from '@/lib/auth/hf-auth';
import { track } from '@/lib/telemetry';
interface ModelSettingsPanelProps {
onClose?: () => void;
onModelChange?: (modelId: string) => void;
showJudgeModel?: boolean;
onJudgeModelChange?: (modelId: string) => void;
}
export function ModelSettingsPanel({ onClose, onModelChange, showJudgeModel, onJudgeModelChange }: ModelSettingsPanelProps) {
const [selectedProvider, setSelectedProvider] = useState<ProviderId>(() =>
configManager.getSelectedProvider()
);
const [showApiKey, setShowApiKey] = useState(false);
const [validatingKey, setValidatingKey] = useState(false);
const [keyValid, setKeyValid] = useState<boolean | null>(null);
const [currentApiKey, setCurrentApiKey] = useState('');
const [apiKeyStored, setApiKeyStored] = useState(() => {
const p = configManager.getSelectedProvider();
return getProvider(p).apiKeyRequired ? !!configManager.getProviderApiKey(p) : false;
});
const [codexAvailable, setCodexAvailable] = useState(true);
const [useSeparateChatModel, setUseSeparateChatModel] = useState<boolean>(() => {
if (typeof window !== 'undefined') {
const stored = localStorage.getItem(`osw-studio-use-separate-chat-model-${configManager.getSelectedProvider()}`);
return stored === 'true';
}
return false;
});
const [compactionEnabled, setCompactionEnabled] = useState<boolean>(() => {
return configManager.isCompactionEnabled(configManager.getSelectedProvider());
});
const [compactionLimit, setCompactionLimit] = useState<string>(() => {
const savedLimit = configManager.getCompactionLimit(configManager.getSelectedProvider());
return savedLimit ? String(savedLimit) : '';
});
// Check if Codex is available (blocked on HF Spaces — HttpOnly cookies don't work)
useEffect(() => {
checkHFCapabilities().then(caps => {
setCodexAvailable(caps.codexAvailable);
}).catch(() => {});
}, []);
useEffect(() => {
// Update API key when provider changes
const key = configManager.getProviderApiKey(selectedProvider) || '';
setCurrentApiKey(key);
setKeyValid(null); // Reset validation
const providerCfg = getProvider(selectedProvider);
setApiKeyStored(providerCfg.apiKeyRequired ? !!key : false);
// Load separate chat model setting for this provider
if (typeof window !== 'undefined') {
const stored = localStorage.getItem(`osw-studio-use-separate-chat-model-${selectedProvider}`);
setUseSeparateChatModel(stored === 'true');
}
// Load compaction settings for this provider
setCompactionEnabled(configManager.isCompactionEnabled(selectedProvider));
const savedLimit = configManager.getCompactionLimit(selectedProvider);
setCompactionLimit(savedLimit ? String(savedLimit) : '');
}, [selectedProvider]);
// Persist separate chat model setting
useEffect(() => {
if (typeof window !== 'undefined') {
localStorage.setItem(`osw-studio-use-separate-chat-model-${selectedProvider}`, String(useSeparateChatModel));
}
}, [useSeparateChatModel, selectedProvider]);
const handleProviderChange = (provider: ProviderId) => {
setSelectedProvider(provider);
configManager.setSelectedProvider(provider);
track('provider_selected', { provider, has_api_key: !!configManager.getProviderApiKey(provider) });
};
const handleApiKeyChange = (key: string) => {
setCurrentApiKey(key);
configManager.setProviderApiKey(selectedProvider, key);
setKeyValid(null);
configManager.clearModelCache(selectedProvider);
window.dispatchEvent(new CustomEvent('apiKeyUpdated', {
detail: { provider: selectedProvider, hasKey: !!key }
}));
};
const validateApiKey = async () => {
if (!currentApiKey) {
toast.error('Please enter an API key');
return;
}
setValidatingKey(true);
try {
const isValid = await checkApiKey(currentApiKey, selectedProvider);
setKeyValid(isValid);
if (isValid) {
toast.success('API key is valid!');
} else {
toast.error('Invalid API key');
}
} catch {
setKeyValid(false);
toast.error('Failed to validate API key');
} finally {
setValidatingKey(false);
}
};
const handleConnect = async () => {
const key = currentApiKey.trim();
if (!key) {
toast.error('Please enter an API key');
return;
}
setValidatingKey(true);
try {
const isValid = await checkApiKey(key, selectedProvider);
if (isValid) {
configManager.setProviderApiKey(selectedProvider, key);
configManager.clearModelCache(selectedProvider);
setApiKeyStored(true);
setCurrentApiKey('');
setKeyValid(null);
toast.success('API key connected!');
window.dispatchEvent(new CustomEvent('apiKeyUpdated', {
detail: { provider: selectedProvider, hasKey: true }
}));
} else {
toast.error('Invalid API key. Please check and try again.');
}
} catch {
toast.error('Failed to validate API key');
} finally {
setValidatingKey(false);
}
};
const handleApiKeyDisconnect = () => {
configManager.setProviderApiKey(selectedProvider, '');
configManager.clearModelCache(selectedProvider);
setApiKeyStored(false);
setCurrentApiKey('');
setKeyValid(null);
toast.success(`Disconnected from ${getProvider(selectedProvider).name}`);
window.dispatchEvent(new CustomEvent('apiKeyUpdated', {
detail: { provider: selectedProvider, hasKey: false }
}));
};
const providerConfig = getProvider(selectedProvider);
return (
<div className="flex-1 min-h-0 flex flex-col overflow-hidden">
{/* Header */}
<div className="shrink-0">
<h3 className="font-semibold text-base tracking-tight">Model Settings</h3>
<p className="text-muted-foreground text-xs mt-1">
Configure your AI model and API connection
</p>
</div>
{/* Scrollable content */}
<div className="flex-1 min-h-0 overflow-y-auto mt-5 space-y-5">
{/* Provider Selection */}
<div>
<Label htmlFor="provider" className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Provider</Label>
<Select value={selectedProvider} onValueChange={handleProviderChange}>
<SelectTrigger id="provider" className="mt-2 !h-fit w-full">
<SelectValue placeholder="Select a provider">
{selectedProvider && (
<div className="flex flex-col text-left">
<span className="font-medium">{providerConfig.name}</span>
<span className="text-xs text-muted-foreground">
{providerConfig.description}
</span>
</div>
)}
</SelectValue>
</SelectTrigger>
<SelectContent className="max-h-[400px]">
{getOfferableProviders()
.filter(p => codexAvailable || p.id !== 'openai-codex')
.map(provider => (
<SelectItem key={provider.id} value={provider.id} className="py-2.5">
<div className="flex flex-col">
<span className="font-medium">{provider.name}</span>
<span className="text-xs text-muted-foreground">
{provider.description}
</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Auth: OAuth panel for Codex/HF, API key for others */}
{providerConfig.usesOAuth ? (
selectedProvider === 'huggingface' ? (
<HFAuthPanel onAuthChange={() => {
window.dispatchEvent(new CustomEvent('apiKeyUpdated', {
detail: { provider: selectedProvider, hasKey: !!configManager.getProviderApiKey(selectedProvider) }
}));
}} />
) : (
<CodexAuthPanel onAuthChange={() => {
window.dispatchEvent(new CustomEvent('apiKeyUpdated', {
detail: { provider: selectedProvider, hasKey: !!configManager.getProviderApiKey(selectedProvider) }
}));
}} />
)
) : providerConfig.apiKeyRequired ? (
apiKeyStored ? (
<ConnectionBadge
method="API Key"
extra={(() => { const k = configManager.getProviderApiKey(selectedProvider); return k ? `···${k.slice(-4)}` : undefined; })()}
info={providerConfig.apiKeyHelpUrl && (
<a
href={providerConfig.apiKeyHelpUrl}
target="_blank"
rel="noopener noreferrer"
className="text-primary hover:underline inline-flex items-center gap-0.5"
>
Manage on {providerConfig.name} <ExternalLink className="h-2.5 w-2.5" />
</a>
)}
onDisconnect={handleApiKeyDisconnect}
/>
) : (
<div>
<Label htmlFor="api-key">{providerConfig.name} API Key</Label>
<div className="flex gap-2 mt-2">
<div className="relative flex-1">
<Input
id="api-key"
type={showApiKey ? "text" : "password"}
value={currentApiKey}
onChange={(e) => { setCurrentApiKey(e.target.value); setKeyValid(null); }}
onKeyDown={(e) => { if (e.key === 'Enter' && currentApiKey.trim()) handleConnect(); }}
placeholder={providerConfig.apiKeyPlaceholder || 'API Key'}
className="pr-10"
data-tour-id="provider-key-input"
disabled={validatingKey}
/>
<Button
size="icon"
variant="ghost"
className="absolute right-1 top-1 h-7 w-7"
onClick={() => setShowApiKey(!showApiKey)}
>
{showApiKey ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</Button>
</div>
<Button
onClick={handleConnect}
disabled={validatingKey || !currentApiKey.trim()}
size="sm"
>
{validatingKey ? (
<>
<Loader2 className="h-3 w-3 animate-spin mr-1" />
Connecting...
</>
) : (
'Connect'
)}
</Button>
</div>
{providerConfig.apiKeyHelpUrl && (
<p className="text-sm text-muted-foreground mt-2">
Get your API key from{' '}
<a
href={providerConfig.apiKeyHelpUrl}
target="_blank"
rel="noopener noreferrer"
className="text-primary hover:underline inline-flex items-center gap-1"
>
{providerConfig.name} <ExternalLink className="h-3 w-3" />
</a>
</p>
)}
</div>
)
) : providerConfig.isLocal ? (
<div>
<Label htmlFor="api-key">
{providerConfig.name} API Key
<span className="text-muted-foreground text-xs ml-1">(optional)</span>
</Label>
<div className="flex gap-2 mt-2">
<div className="relative flex-1">
<Input
id="api-key"
type={showApiKey ? "text" : "password"}
value={currentApiKey}
onChange={(e) => handleApiKeyChange(e.target.value)}
placeholder={providerConfig.apiKeyPlaceholder || 'API Key'}
className="pr-10"
data-tour-id="provider-key-input"
/>
<Button
size="icon"
variant="ghost"
className="absolute right-1 top-1 h-7 w-7"
onClick={() => setShowApiKey(!showApiKey)}
>
{showApiKey ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</Button>
</div>
<Button
onClick={validateApiKey}
disabled={validatingKey || !currentApiKey}
size="sm"
>
{validatingKey ? 'Validating...' : 'Validate'}
</Button>
{keyValid !== null && (
<div className="flex items-center">
{keyValid ? (
<Check className="h-5 w-5 text-green-500" />
) : (
<X className="h-5 w-5 text-red-500" />
)}
</div>
)}
</div>
<p className="text-sm text-muted-foreground mt-2">
API key is optional for {providerConfig.name}. Only needed if you've configured authentication on your local server.
</p>
</div>
) : null}
{!providerConfig.apiKeyRequired && providerConfig.isLocal && (
<div className="text-sm text-muted-foreground p-3 border rounded-md bg-muted/50">
<p className="font-medium mb-1">Local Provider</p>
<p>Make sure {providerConfig.name} is running on your machine.</p>
<p>Default endpoint: <code className="text-xs">{providerConfig.baseUrl}</code></p>
{selectedProvider === 'lmstudio' && (
<div className="mt-2 text-xs">
<p className="font-medium">For tool use support:</p>
<p>• Load a model like qwen/qwen3-4b-thinking-2507</p>
<p>• Start the local server in LM Studio</p>
<p>• Models will be automatically discovered</p>
</div>
)}
</div>
)}
{/* Divider */}
<hr className="border-border" />
{/* Code Model */}
<div>
<Label className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Code Model</Label>
<div className="mt-2">
<ModelSelector
provider={selectedProvider}
mode="inline"
autoFocus={apiKeyStored || (!providerConfig.apiKeyRequired && !providerConfig.usesOAuth)}
onChange={(modelId) => {
if (typeof window !== 'undefined') {
localStorage.setItem(`osw-studio-code-model-${selectedProvider}`, modelId);
}
if (!useSeparateChatModel) {
onModelChange?.(modelId);
}
}}
className="space-y-2"
/>
</div>
</div>
{/* Separate Chat Model Toggle — hidden in judge mode */}
{!showJudgeModel && (
<div className="flex items-center justify-between gap-3">
<div>
<div className="text-sm font-medium">Use different model for chat</div>
<p className="text-xs text-muted-foreground mt-0.5">
Select a separate (usually cheaper) model for chat/planning
</p>
</div>
<Switch
id="separate-chat-model"
checked={useSeparateChatModel}
onCheckedChange={(checked) => setUseSeparateChatModel(checked)}
/>
</div>
)}
{/* Chat Model (conditional) — hidden in judge mode */}
{!showJudgeModel && useSeparateChatModel && (
<div>
<Label className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Chat Model</Label>
<div className="mt-2">
<ModelSelector
provider={selectedProvider}
mode="inline"
onChange={(modelId) => {
if (typeof window !== 'undefined') {
localStorage.setItem(`osw-studio-chat-model-${selectedProvider}`, modelId);
}
onModelChange?.(modelId);
}}
className="space-y-2"
/>
</div>
</div>
)}
{/* Judge Model — shown only in benchmark mode */}
{showJudgeModel && (
<>
<hr className="border-border" />
<div>
<Label className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">
Judge Model <span className="normal-case font-normal">(optional)</span>
</Label>
<p className="text-xs text-muted-foreground mt-1 mb-2">
Separate model for evaluating subjective test criteria
</p>
<ModelSelector
provider={selectedProvider}
mode="inline"
skipGlobalSync
onChange={(modelId) => onJudgeModelChange?.(modelId)}
className="space-y-2"
/>
</div>
</>
)}
{/* Auto-Compaction */}
<div className="flex items-center justify-between gap-3">
<div>
<div className="text-sm font-medium">Auto-compact</div>
<p className="text-xs text-muted-foreground mt-0.5">
Summarize conversation when reaching 60% of the model's context limit
</p>
</div>
<Switch
id="compaction-enabled"
checked={compactionEnabled}
onCheckedChange={(checked) => {
setCompactionEnabled(checked);
configManager.setCompactionEnabled(selectedProvider, checked);
}}
/>
</div>
{compactionEnabled && (
<div className="space-y-2">
<label className="text-sm font-medium text-foreground">
Compaction limit (tokens)
</label>
<p className="text-xs text-muted-foreground">
Leave empty to auto-detect (60% of model context). Manual values are used as-is.
</p>
<input
type="number"
value={compactionLimit}
onChange={(e) => {
const val = e.target.value;
setCompactionLimit(val);
const num = parseInt(val, 10);
configManager.setCompactionLimit(
selectedProvider,
isNaN(num) || num <= 0 ? undefined : num
);
}}
placeholder="e.g. 128000"
className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
min={1}
/>
</div>
)}
</div>{/* end scrollable content */}
{/* Footer */}
{onClose && (
<div className="shrink-0 flex justify-end pt-4 border-t mt-4">
<Button onClick={onClose} size="sm">
Done
</Button>
</div>
)}
</div>
);
}
|