Skip to content

Commit a65290b

Browse files
committed
fix: models unique identifier
1 parent 51b4846 commit a65290b

File tree

2 files changed

+58
-286
lines changed

2 files changed

+58
-286
lines changed

src/features/workspace/components/workspace-models-dropdown.tsx

+58-56
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ModelByProvider,
33
MuxRule,
44
V1ListAllModelsForAllProvidersResponse,
5-
} from "@/api/generated";
5+
} from '@/api/generated'
66
import {
77
DialogTrigger,
88
Button,
@@ -12,71 +12,73 @@ import {
1212
Input,
1313
OptionRenderer,
1414
OptionsSchema,
15-
} from "@stacklok/ui-kit";
16-
import { ChevronDown, SearchMd } from "@untitled-ui/icons-react";
17-
import { useState } from "react";
15+
} from '@stacklok/ui-kit'
16+
import { ChevronDown, SearchMd } from '@untitled-ui/icons-react'
17+
import { useState } from 'react'
1818

1919
type Props = {
20-
rule: MuxRule & { id: string };
21-
isArchived: boolean;
22-
models: V1ListAllModelsForAllProvidersResponse;
20+
rule: MuxRule & { id: string }
21+
isArchived: boolean
22+
models: V1ListAllModelsForAllProvidersResponse
2323
onChange: ({
2424
model,
2525
provider_id,
2626
}: {
27-
model: string;
28-
provider_id: string;
29-
}) => void;
30-
};
27+
model: string
28+
provider_id: string
29+
}) => void
30+
}
3131

3232
function groupModelsByProviderName(
33-
models: ModelByProvider[],
34-
): OptionsSchema<"listbox", string>[] {
35-
return models.reduce<OptionsSchema<"listbox", string>[]>(
33+
models: ModelByProvider[]
34+
): OptionsSchema<'listbox', string>[] {
35+
return models.reduce<OptionsSchema<'listbox', string>[]>(
3636
(groupedProviders, item) => {
3737
const providerData = groupedProviders.find(
38-
(group) => group.id === item.provider_name,
39-
);
38+
(group) => group.id === item.provider_name
39+
)
4040
if (!providerData) {
4141
groupedProviders.push({
4242
id: item.provider_name,
4343
items: [],
4444
textValue: item.provider_name,
45-
});
45+
})
4646
}
4747

48-
(providerData?.items ?? []).push({
49-
id: item.name,
48+
;(providerData?.items ?? []).push({
49+
id: `${item.provider_id}_${item.name}`,
5050
textValue: item.name,
51-
});
51+
})
5252

53-
return groupedProviders;
53+
return groupedProviders
5454
},
55-
[],
56-
);
55+
[]
56+
)
5757
}
5858

5959
function filterModels({
6060
groupedModels,
6161
searchItem,
6262
}: {
63-
searchItem: string;
64-
groupedModels: OptionsSchema<"listbox", string>[];
63+
searchItem: string
64+
groupedModels: OptionsSchema<'listbox', string>[]
6565
}) {
66-
return groupedModels
66+
const test = groupedModels
6767
.map((modelData) => {
68-
if (!searchItem) return modelData;
68+
if (!searchItem) return modelData
6969
const filteredModels = modelData.items?.filter((item) => {
70-
return item.textValue.includes(searchItem);
71-
});
70+
return item.textValue.includes(searchItem)
71+
})
7272

7373
const data = {
7474
...modelData,
7575
items: filteredModels,
76-
};
77-
return data;
76+
}
77+
return data
7878
})
79-
.filter((item) => (item.items ? item.items.length > 0 : false));
79+
.filter((item) => (item.items ? item.items.length > 0 : false))
80+
81+
return test
8082
}
8183

8284
export function WorkspaceModelsDropdown({
@@ -85,27 +87,28 @@ export function WorkspaceModelsDropdown({
8587
models = [],
8688
onChange,
8789
}: Props) {
88-
const [isOpen, setIsOpen] = useState(false);
89-
const [searchItem, setSearchItem] = useState("");
90-
const groupedModels = groupModelsByProviderName(models);
91-
const currentProvider = models.find(
92-
(p) => p.provider_id === rule.provider_id,
93-
);
90+
const [isOpen, setIsOpen] = useState(false)
91+
const [searchItem, setSearchItem] = useState('')
92+
const groupedModels = groupModelsByProviderName(models)
93+
console.log({ groupedModels, rule: rule })
94+
const currentProvider = models.find((p) => p.provider_id === rule.provider_id)
9495
const currentModel =
9596
currentProvider && rule.model
9697
? `${currentProvider?.provider_name}/${rule.model}`
97-
: "";
98+
: ''
99+
const selectedKey = `${rule.provider_id}_${rule.model}`
98100

99101
return (
100-
<div className="w-full flex">
102+
<div className="flex w-full">
101103
<DialogTrigger isOpen={isOpen} onOpenChange={(test) => setIsOpen(test)}>
102104
<Button
103105
variant="secondary"
104106
isDisabled={isArchived}
105107
data-testid="workspace-models-dropdown"
106-
className="flex justify-between cursor-pointer bg-gray-25 border-gray-400 shadow-none font-normal w-full"
108+
className="flex w-full cursor-pointer justify-between border-gray-400 bg-gray-25
109+
font-normal shadow-none"
107110
>
108-
<span>{currentModel || "Select a model"}</span>
111+
<span>{currentModel || 'Select a model'}</span>
109112
<ChevronDown className="shrink-0" />
110113
</Button>
111114

@@ -119,22 +122,21 @@ export function WorkspaceModelsDropdown({
119122
items={filterModels({ searchItem, groupedModels })}
120123
selectionMode="single"
121124
selectionBehavior="replace"
122-
selectedKeys={rule.model ? [rule.model] : []}
125+
selectedKeys={selectedKey ? [selectedKey] : []}
123126
onSelectionChange={(v) => {
124-
if (v === "all") {
125-
return;
127+
if (v === 'all') {
128+
return
126129
}
127-
const selectedValue = v.values().next().value;
128-
const providerId = models.find(
129-
(item) => item.name === selectedValue,
130-
)?.provider_id;
131-
if (typeof selectedValue === "string" && providerId) {
130+
const selectedValue = v.values().next().value
131+
if (!selectedValue && typeof selectedValue !== 'string') return
132+
if (typeof selectedValue === 'string') {
133+
const [provider_id, modelName] = selectedValue.split('_')
134+
if (!provider_id || !modelName) return
132135
onChange({
133-
model: selectedValue,
134-
provider_id: providerId,
135-
});
136-
137-
setIsOpen(false);
136+
model: modelName,
137+
provider_id,
138+
})
139+
setIsOpen(false)
138140
}
139141
}}
140142
className="-mx-1 mt-2 max-h-72 overflow-auto"
@@ -154,5 +156,5 @@ export function WorkspaceModelsDropdown({
154156
</Popover>
155157
</DialogTrigger>
156158
</div>
157-
);
159+
)
158160
}

0 commit comments

Comments
 (0)