Skip to content

Commit 0347b00

Browse files
authored
feat(app): redesign model options (anomalyco#1746)
1 parent 4bdb402 commit 0347b00

12 files changed

Lines changed: 1237 additions & 107 deletions

File tree

‎apps/app/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"ai": "^6.0.146",
5656
"class-variance-authority": "^0.7.1",
5757
"clsx": "^2.1.1",
58+
"cmdk": "^1.1.1",
5859
"fuzzysort": "^3.1.0",
5960
"jsonc-parser": "^3.2.1",
6061
"lexical": "^0.35.0",

‎apps/app/src/app/lib/model-behavior.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ const WELL_KNOWN_VARIANT_ORDER = [
1414
"max",
1515
] as const;
1616

17+
const VARIANT_DEFAULT_TARGET = 3;
18+
const VARIANT_DEFAULT_SCORE: Record<string, number> = {
19+
none: 0,
20+
minimal: 1,
21+
low: 2,
22+
medium: VARIANT_DEFAULT_TARGET,
23+
high: 4,
24+
xhigh: 5,
25+
max: 6,
26+
};
27+
1728
function defaultBehaviorOption(): ModelBehaviorOption {
1829
return {
1930
value: null,
@@ -71,6 +82,30 @@ const sortVariantKeys = (keys: string[]) =>
7182
return a.localeCompare(b);
7283
});
7384

85+
const getDefaultVariantKey = (keys: string[]) => {
86+
let selected: string | null = null;
87+
let selectedScore: number | null = null;
88+
89+
for (const key of keys) {
90+
const score = VARIANT_DEFAULT_SCORE[key];
91+
if (score == null) continue;
92+
if (selectedScore == null) {
93+
selected = key;
94+
selectedScore = score;
95+
continue;
96+
}
97+
98+
const distance = Math.abs(score - VARIANT_DEFAULT_TARGET);
99+
const selectedDistance = Math.abs(selectedScore - VARIANT_DEFAULT_TARGET);
100+
if (distance < selectedDistance || (distance === selectedDistance && score > selectedScore)) {
101+
selected = key;
102+
selectedScore = score;
103+
}
104+
}
105+
106+
return selected ?? keys[0] ?? null;
107+
};
108+
74109
const providerFamily = (providerID: string, providerName?: string | null) => {
75110
const normalizedId = providerID.trim().toLowerCase();
76111
if (["anthropic", "openai", "google", "opencode"].includes(normalizedId)) {
@@ -167,6 +202,9 @@ export const getModelBehaviorOptions = (
167202
];
168203
};
169204

205+
const getDefaultModelBehaviorValue = (model: ProviderModel) =>
206+
getDefaultVariantKey(sortVariantKeys(getVariantKeys(model)));
207+
170208
export const sanitizeModelBehaviorValue = (
171209
providerID: string,
172210
model: ProviderModel,
@@ -188,14 +226,16 @@ export const getModelBehaviorSummary = (
188226
) => {
189227
const options = getModelBehaviorOptions(providerID, model, providerName);
190228
const sanitized = sanitizeModelBehaviorValue(providerID, model, value, providerName);
191-
const selected = options.find((option) => option.value === sanitized) ?? options[0] ?? null;
229+
const selectedValue = sanitized ?? getDefaultModelBehaviorValue(model);
230+
const selected = options.find((option) => option.value === selectedValue) ?? options[0] ?? null;
192231
const title = getBehaviorTitle(providerID, model, getVariantKeys(model), providerName);
193232

194233
if (options.length > 0) {
195234
return {
196235
title,
197236
label: selected?.label ?? defaultBehaviorOption().label,
198237
description: selected?.description ?? defaultBehaviorOption().description,
238+
value: selected?.value ?? null,
199239
options,
200240
};
201241
}
@@ -205,6 +245,7 @@ export const getModelBehaviorSummary = (
205245
title,
206246
label: t("model_behavior.label_builtin"),
207247
description: t("model_behavior.desc_builtin"),
248+
value: null,
208249
options,
209250
};
210251
}
@@ -213,6 +254,7 @@ export const getModelBehaviorSummary = (
213254
title,
214255
label: t("model_behavior.label_standard"),
215256
description: t("model_behavior.desc_standard"),
257+
value: null,
216258
options,
217259
};
218260
};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"use client";
2+
3+
import { t } from "@/i18n";
4+
import {
5+
Select,
6+
SelectContent,
7+
SelectGroup,
8+
SelectItem,
9+
SelectLabel,
10+
SelectTrigger,
11+
SelectValue,
12+
} from "@/components/ui/select";
13+
import {
14+
Tooltip,
15+
TooltipContent,
16+
TooltipTrigger,
17+
} from "@/components/ui/tooltip";
18+
19+
type ModelBehaviorOption = {
20+
value: string | null;
21+
label: string;
22+
};
23+
24+
type ModelBehaviorSelectProps = {
25+
value: string | null;
26+
label: string;
27+
options?: ModelBehaviorOption[];
28+
onChange: (value: string | null) => void;
29+
disabled?: boolean;
30+
};
31+
32+
export function ModelBehaviorSelect({
33+
value,
34+
label,
35+
options,
36+
onChange,
37+
disabled = false,
38+
}: ModelBehaviorSelectProps) {
39+
if (!options?.length) {
40+
return null;
41+
}
42+
43+
const items = options.flatMap((option) =>
44+
option.value ? [{ value: option.value, label: option.label }] : [],
45+
);
46+
const rawValue = value ?? null;
47+
const selectValue = items.some((option) => option.value === rawValue)
48+
? rawValue
49+
: items[0]?.value ?? null;
50+
51+
return (
52+
<Select
53+
value={selectValue}
54+
items={items}
55+
onValueChange={(nextValue) => {
56+
const option = options.find((item) => item.value === nextValue);
57+
58+
if (!option) {
59+
return;
60+
}
61+
62+
onChange(option.value ?? null);
63+
}}
64+
disabled={disabled}
65+
>
66+
<Tooltip>
67+
<TooltipTrigger
68+
render={
69+
<SelectTrigger
70+
size="sm"
71+
disabled={disabled}
72+
aria-label={t("composer.behavior_label")}
73+
className="h-10 border-0 bg-transparent px-2.5 py-1 text-sm rounded-md text-gray-10 shadow-none hover:bg-gray-3 hover:text-gray-12 data-[size=sm]:h-8"
74+
/>
75+
}
76+
>
77+
<SelectValue placeholder={label || t("settings.default_label")} />
78+
</TooltipTrigger>
79+
<TooltipContent>{t("composer.behavior_label")}</TooltipContent>
80+
</Tooltip>
81+
<SelectContent side="top" sideOffset={8} align="start" className="min-w-48">
82+
<SelectGroup>
83+
<SelectLabel>Thinking</SelectLabel>
84+
{items.map((option) => (
85+
<SelectItem key={option.value} value={option.value} className="text-xs">
86+
{option.label}
87+
</SelectItem>
88+
))}
89+
</SelectGroup>
90+
</SelectContent>
91+
</Select>
92+
);
93+
}

0 commit comments

Comments
 (0)