-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathProviderModelPicker.tsx
More file actions
215 lines (209 loc) · 8.01 KB
/
ProviderModelPicker.tsx
File metadata and controls
215 lines (209 loc) · 8.01 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
215
import { type ModelSlug, type ProviderKind } from "@okcode/contracts";
import { resolveSelectableModel } from "@okcode/shared/model";
import { memo, useState } from "react";
import { type ProviderPickerKind, PROVIDER_OPTIONS } from "../../session-logic";
import { ChevronDownIcon } from "lucide-react";
import { Button } from "../ui/button";
import {
Menu,
MenuGroup,
MenuGroupLabel,
MenuItem,
MenuPopup,
MenuRadioGroup,
MenuRadioItem,
MenuSeparator as MenuDivider,
MenuTrigger,
} from "../ui/menu";
import { ClaudeAI, CursorIcon, Gemini, Icon, OpenAI, OpenClawIcon, OpenCodeIcon } from "../Icons";
import { cn } from "~/lib/utils";
import { getThreadProviderLabel } from "~/lib/providerAvailability";
const PROVIDER_ICON_BY_PROVIDER: Record<ProviderPickerKind, Icon> = {
codex: OpenAI,
claudeAgent: ClaudeAI,
openclaw: OpenClawIcon,
cursor: CursorIcon,
};
const UNAVAILABLE_PROVIDER_OPTIONS = PROVIDER_OPTIONS.filter((option) => !option.available);
const COMING_SOON_PROVIDER_OPTIONS = [
{ id: "opencode", label: "OpenCode", icon: OpenCodeIcon },
{ id: "gemini", label: "Gemini", icon: Gemini },
] as const;
function providerIconClassName(
provider: ProviderKind | ProviderPickerKind,
fallbackClassName: string,
): string {
if (provider === "claudeAgent") return "text-[#d97757]";
if (provider === "openclaw") return "text-[#6cb4ee]";
return fallbackClassName;
}
function getProviderLabel(provider: ProviderKind): string {
return getThreadProviderLabel(provider);
}
export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
provider: ProviderKind;
model: ModelSlug;
lockedProvider: ProviderKind | null;
availableProviders: ReadonlyArray<ProviderKind>;
modelOptionsByProvider: Record<ProviderKind, ReadonlyArray<{ slug: string; name: string }>>;
activeProviderIconClassName?: string;
compact?: boolean;
disabled?: boolean;
onProviderModelChange: (provider: ProviderKind, model: ModelSlug) => void;
}) {
const [isMenuOpen, setIsMenuOpen] = useState(false);
const activeProvider = props.lockedProvider ?? props.provider;
const visibleProviders =
props.lockedProvider !== null ? [props.lockedProvider] : props.availableProviders;
const selectedProviderOptions = props.modelOptionsByProvider[activeProvider];
const selectedModelLabel =
selectedProviderOptions.find((option) => option.slug === props.model)?.name ?? props.model;
const ProviderIcon = PROVIDER_ICON_BY_PROVIDER[activeProvider];
const handleModelChange = (provider: ProviderKind, value: string) => {
if (props.disabled) return;
if (!value) return;
const resolvedModel = resolveSelectableModel(
provider,
value,
props.modelOptionsByProvider[provider],
);
if (!resolvedModel) return;
props.onProviderModelChange(provider, resolvedModel);
setIsMenuOpen(false);
};
return (
<Menu
open={isMenuOpen}
onOpenChange={(open) => {
if (props.disabled) {
setIsMenuOpen(false);
return;
}
setIsMenuOpen(open);
}}
>
<MenuTrigger
render={
<Button
size="sm"
variant="ghost"
className={cn(
"min-w-0 justify-start overflow-hidden whitespace-nowrap px-2 text-muted-foreground/70 hover:text-foreground/80 [&_svg]:mx-0",
props.compact ? "max-w-42 shrink-0" : "max-w-48 shrink sm:max-w-56 sm:px-3",
)}
disabled={props.disabled}
/>
}
>
<span
className={cn(
"flex min-w-0 w-full items-center gap-2 overflow-hidden",
props.compact ? "max-w-36" : undefined,
)}
>
<ProviderIcon
aria-hidden="true"
className={cn(
"size-4 shrink-0",
providerIconClassName(activeProvider, "text-muted-foreground/70"),
props.activeProviderIconClassName,
)}
/>
<span className="min-w-0 flex-1 truncate">{selectedModelLabel}</span>
<ChevronDownIcon aria-hidden="true" className="size-3 shrink-0 opacity-60" />
</span>
</MenuTrigger>
<MenuPopup align="start">
{props.lockedProvider !== null ? (
<MenuGroup>
<MenuGroupLabel className="px-2 pb-1 pt-2 text-[11px] uppercase tracking-[0.08em]">
{getProviderLabel(props.lockedProvider)} · locked for this thread
</MenuGroupLabel>
<MenuRadioGroup
value={props.model}
onValueChange={(value) => handleModelChange(props.lockedProvider!, value)}
>
{props.modelOptionsByProvider[props.lockedProvider].map((modelOption) => (
<MenuRadioItem
key={`${props.lockedProvider}:${modelOption.slug}`}
value={modelOption.slug}
onClick={() => setIsMenuOpen(false)}
>
{modelOption.name}
</MenuRadioItem>
))}
</MenuRadioGroup>
</MenuGroup>
) : (
<>
{visibleProviders.map((provider, index) => {
const option = {
value: provider,
label: getThreadProviderLabel(provider),
};
const OptionIcon = PROVIDER_ICON_BY_PROVIDER[option.value];
return (
<MenuGroup key={option.value}>
{index > 0 ? <MenuDivider /> : null}
<MenuGroupLabel className="flex items-center gap-2 px-2 pb-1 pt-2 text-[11px] uppercase tracking-[0.08em]">
<OptionIcon
aria-hidden="true"
className={cn(
"size-4 shrink-0",
providerIconClassName(option.value, "text-muted-foreground/85"),
)}
/>
<span>{option.label}</span>
</MenuGroupLabel>
<MenuRadioGroup
value={props.provider === option.value ? props.model : ""}
onValueChange={(value) => handleModelChange(option.value, value)}
>
{props.modelOptionsByProvider[option.value].map((modelOption) => (
<MenuRadioItem
key={`${option.value}:${modelOption.slug}`}
value={modelOption.slug}
onClick={() => setIsMenuOpen(false)}
>
{modelOption.name}
</MenuRadioItem>
))}
</MenuRadioGroup>
</MenuGroup>
);
})}
{UNAVAILABLE_PROVIDER_OPTIONS.length > 0 && <MenuDivider />}
{UNAVAILABLE_PROVIDER_OPTIONS.map((option) => {
const OptionIcon = PROVIDER_ICON_BY_PROVIDER[option.value];
return (
<MenuItem key={option.value} disabled>
<OptionIcon
aria-hidden="true"
className="size-4 shrink-0 text-muted-foreground/85 opacity-80"
/>
<span>{option.label}</span>
<span className="ms-auto text-[11px] text-muted-foreground/80 uppercase tracking-[0.08em]">
Coming soon
</span>
</MenuItem>
);
})}
{UNAVAILABLE_PROVIDER_OPTIONS.length === 0 && <MenuDivider />}
{COMING_SOON_PROVIDER_OPTIONS.map((option) => {
const OptionIcon = option.icon;
return (
<MenuItem key={option.id} disabled>
<OptionIcon aria-hidden="true" className="size-4 shrink-0 opacity-80" />
<span>{option.label}</span>
<span className="ms-auto text-[11px] text-muted-foreground/80 uppercase tracking-[0.08em]">
Coming soon
</span>
</MenuItem>
);
})}
</>
)}
</MenuPopup>
</Menu>
);
});