-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsightsModelSelector.tsx
More file actions
214 lines (197 loc) · 7.59 KB
/
Copy pathInsightsModelSelector.tsx
File metadata and controls
214 lines (197 loc) · 7.59 KB
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
import { useState, useMemo, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Brain, Scale, Zap, Sparkles, Sliders, Check } from 'lucide-react';
import { Button } from './ui/button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
DropdownMenuLabel
} from './ui/dropdown-menu';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from './ui/select';
import { DEFAULT_AGENT_PROFILES } from '../../shared/constants';
import { getInsightsProviderOptions, getModelLabelForProvider } from '../../shared/constants/insights-providers';
import type { InsightsModelConfig, InsightsProvider, ModelType } from '../../shared/types';
import { CustomModelModal } from './CustomModelModal';
interface InsightsModelSelectorProps {
currentConfig?: InsightsModelConfig;
onConfigChange: (config: InsightsModelConfig) => void;
disabled?: boolean;
}
const iconMap: Record<string, React.ElementType> = {
Brain,
Scale,
Zap,
Sparkles
};
export function InsightsModelSelector({
currentConfig,
onConfigChange,
disabled
}: InsightsModelSelectorProps) {
const { t } = useTranslation(['dialogs', 'common']);
const [showCustomModal, setShowCustomModal] = useState(false);
// Provider state - sync with currentConfig to avoid stale state
const [selectedProvider, setSelectedProvider] = useState<InsightsProvider>(
currentConfig?.provider || 'claude'
);
// Sync selectedProvider when currentConfig changes externally
useEffect(() => {
const configProvider = currentConfig?.provider || 'claude';
setSelectedProvider(configProvider);
}, [currentConfig?.provider]);
// Build provider options with i18n
const insightsProviders = useMemo(() => getInsightsProviderOptions(t), [t]);
// Default to 'balanced' if no config, or if 'auto' profile was selected (not applicable for insights)
const rawProfileId = currentConfig?.profileId || 'balanced';
const selectedProfileId = rawProfileId === 'auto' ? 'balanced' : rawProfileId;
const profile = DEFAULT_AGENT_PROFILES.find(p => p.id === selectedProfileId);
// Get the appropriate icon
const Icon = selectedProfileId === 'custom'
? Sliders
: (profile?.icon ? iconMap[profile.icon] : Scale);
// Get provider-specific model label for current profile
const providerModelLabel = useMemo(() => {
if (profile && profile.model) {
return getModelLabelForProvider(profile.model as ModelType, selectedProvider);
}
return null;
}, [profile, selectedProvider]);
const handleSelectProfile = (profileId: string) => {
if (profileId === 'custom') {
setShowCustomModal(true);
return;
}
const selected = DEFAULT_AGENT_PROFILES.find(p => p.id === profileId);
if (selected) {
onConfigChange({
profileId: selected.id,
model: selected.model,
thinkingLevel: selected.thinkingLevel,
provider: selectedProvider
});
}
};
const handleProviderChange = (providerId: InsightsProvider) => {
setSelectedProvider(providerId);
// When provider changes, update the current config with the new provider
if (currentConfig) {
onConfigChange({
...currentConfig,
provider: providerId
});
}
};
const handleCustomSave = (config: InsightsModelConfig) => {
// Ensure provider is included when saving custom config
onConfigChange({
...config,
provider: config.provider || selectedProvider
});
setShowCustomModal(false);
};
// Build display text for current selection
const getDisplayText = () => {
if (selectedProfileId === 'custom' && currentConfig) {
const modelLabel = getModelLabelForProvider(currentConfig.model, currentConfig.provider);
const providerLabel = insightsProviders.find(p => p.id === currentConfig.provider)?.label || currentConfig.provider;
return `${providerLabel}: ${modelLabel} + ${currentConfig.thinkingLevel}`;
}
const providerLabel = insightsProviders.find(p => p.id === selectedProvider)?.label || selectedProvider;
const modelLabel = providerModelLabel || profile?.name || 'Balanced';
return `${providerLabel} - ${profile?.name || modelLabel}`;
};
return (
<div className="flex items-center gap-2">
{/* Provider Selector */}
<Select value={selectedProvider} onValueChange={(value) => handleProviderChange(value as InsightsProvider)} disabled={disabled}>
<SelectTrigger className="h-8 w-[140px]">
<SelectValue placeholder={t('dialogs:customModel.provider')} />
</SelectTrigger>
<SelectContent align="end">
{insightsProviders.map((provider) => (
<SelectItem key={provider.id} value={provider.id}>
<div className="flex flex-col">
<span className="font-medium">{provider.label}</span>
<span className="text-xs text-muted-foreground">{provider.description}</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
{/* Profile Selector */}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="sm"
className="h-8 gap-2 px-2"
disabled={disabled}
title={`Model: ${getDisplayText()}`}
>
<Icon className="h-4 w-4" />
<span className="hidden text-xs text-muted-foreground sm:inline">
{getDisplayText()}
</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-64">
<DropdownMenuLabel>{t('dialogs:customModel.agentProfile')}</DropdownMenuLabel>
{DEFAULT_AGENT_PROFILES.filter(p => !p.isAutoProfile).map((p) => {
const ProfileIcon = iconMap[p.icon || 'Brain'];
const isSelected = selectedProfileId === p.id;
const modelLabel = getModelLabelForProvider(p.model as ModelType, selectedProvider);
return (
<DropdownMenuItem
key={p.id}
onClick={() => handleSelectProfile(p.id)}
className="flex cursor-pointer items-center gap-2"
>
<ProfileIcon className="h-4 w-4 shrink-0" />
<div className="min-w-0 flex-1">
<div className="font-medium">{p.name}</div>
<div className="truncate text-xs text-muted-foreground">
{modelLabel} + {p.thinkingLevel}
</div>
</div>
{isSelected && (
<Check className="h-4 w-4 shrink-0 text-primary" />
)}
</DropdownMenuItem>
);
})}
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => handleSelectProfile('custom')}
className="flex cursor-pointer items-center gap-2"
>
<Sliders className="h-4 w-4 shrink-0" />
<div className="flex-1">
<div className="font-medium">{t('dialogs:customModel.custom')}</div>
<div className="text-xs text-muted-foreground">
{t('dialogs:customModel.customDesc')}
</div>
</div>
{selectedProfileId === 'custom' && (
<Check className="h-4 w-4 shrink-0 text-primary" />
)}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<CustomModelModal
open={showCustomModal}
currentConfig={currentConfig}
onSave={handleCustomSave}
onClose={() => setShowCustomModal(false)}
/>
</div>
);
}