-
Notifications
You must be signed in to change notification settings - Fork 2.7k
refactor: unify model picker components #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26a315b
refactor: unify model picker components
samhvw8 793b13e
remove verbose
samhvw8 e9d9e03
fix: improve model picker refresh handling and API config updates
samhvw8 fabaf07
pr comment
samhvw8 bbc9555
feat: add custom model input support to ModelPicker
samhvw8 1653263
pr comment
samhvw8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { VSCodeLink } from "@vscode/webview-ui-toolkit/react" | ||
| import debounce from "debounce" | ||
| import { useMemo, useState, useCallback, useEffect } from "react" | ||
| import { useMemo, useState, useCallback, useEffect, useRef } from "react" | ||
| import { useMount } from "react-use" | ||
| import { CaretSortIcon, CheckIcon } from "@radix-ui/react-icons" | ||
|
|
||
|
|
@@ -23,15 +23,24 @@ import { vscode } from "../../utils/vscode" | |
| import { normalizeApiConfiguration } from "./ApiOptions" | ||
| import { ModelInfoView } from "./ModelInfoView" | ||
|
|
||
| interface ModelPickerProps { | ||
| type ModelProvider = "glama" | "openRouter" | "unbound" | "requesty" | "openAi" | ||
|
|
||
| type ModelKeys<T extends ModelProvider> = `${T}Models` | ||
| type ConfigKeys<T extends ModelProvider> = `${T}ModelId` | ||
| type InfoKeys<T extends ModelProvider> = `${T}ModelInfo` | ||
| type RefreshMessageType<T extends ModelProvider> = `refresh${Capitalize<T>}Models` | ||
|
|
||
| interface ModelPickerProps<T extends ModelProvider = ModelProvider> { | ||
| defaultModelId: string | ||
| modelsKey: "glamaModels" | "openRouterModels" | "unboundModels" | "requestyModels" | ||
| configKey: "glamaModelId" | "openRouterModelId" | "unboundModelId" | "requestyModelId" | ||
| infoKey: "glamaModelInfo" | "openRouterModelInfo" | "unboundModelInfo" | "requestyModelInfo" | ||
| refreshMessageType: "refreshGlamaModels" | "refreshOpenRouterModels" | "refreshUnboundModels" | "refreshRequestyModels" | ||
| modelsKey: ModelKeys<T> | ||
| configKey: ConfigKeys<T> | ||
| infoKey: InfoKeys<T> | ||
| refreshMessageType: RefreshMessageType<T> | ||
| refreshValues?: Record<string, any> | ||
| serviceName: string | ||
| serviceUrl: string | ||
| recommendedModel: string | ||
| allowCustomModel?: boolean | ||
| } | ||
|
|
||
| export const ModelPicker = ({ | ||
|
|
@@ -40,25 +49,51 @@ export const ModelPicker = ({ | |
| configKey, | ||
| infoKey, | ||
| refreshMessageType, | ||
| refreshValues, | ||
| serviceName, | ||
| serviceUrl, | ||
| recommendedModel, | ||
| allowCustomModel = false, | ||
| }: ModelPickerProps) => { | ||
| const [customModelId, setCustomModelId] = useState("") | ||
| const [isCustomModel, setIsCustomModel] = useState(false) | ||
| const [open, setOpen] = useState(false) | ||
| const [value, setValue] = useState(defaultModelId) | ||
| const [isDescriptionExpanded, setIsDescriptionExpanded] = useState(false) | ||
| const prevRefreshValuesRef = useRef<Record<string, any> | undefined>() | ||
|
|
||
| const { apiConfiguration, setApiConfiguration, [modelsKey]: models, onUpdateApiConfig } = useExtensionState() | ||
| const modelIds = useMemo(() => Object.keys(models).sort((a, b) => a.localeCompare(b)), [models]) | ||
| const { apiConfiguration, [modelsKey]: models, onUpdateApiConfig, setApiConfiguration } = useExtensionState() | ||
|
|
||
| const modelIds = useMemo( | ||
| () => (Array.isArray(models) ? models : Object.keys(models)).sort((a, b) => a.localeCompare(b)), | ||
| [models], | ||
| ) | ||
|
|
||
| const { selectedModelId, selectedModelInfo } = useMemo( | ||
| () => normalizeApiConfiguration(apiConfiguration), | ||
| [apiConfiguration], | ||
| ) | ||
|
|
||
| const onSelectCustomModel = useCallback( | ||
| (modelId: string) => { | ||
| setCustomModelId(modelId) | ||
| const modelInfo = { id: modelId } | ||
| const apiConfig = { ...apiConfiguration, [configKey]: modelId, [infoKey]: modelInfo } | ||
| setApiConfiguration(apiConfig) | ||
| onUpdateApiConfig(apiConfig) | ||
| setValue(modelId) | ||
| setOpen(false) | ||
| setIsCustomModel(false) | ||
| }, | ||
| [apiConfiguration, configKey, infoKey, onUpdateApiConfig, setApiConfiguration], | ||
| ) | ||
|
|
||
| const onSelect = useCallback( | ||
| (modelId: string) => { | ||
| const apiConfig = { ...apiConfiguration, [configKey]: modelId, [infoKey]: models[modelId] } | ||
| const modelInfo = Array.isArray(models) | ||
| ? { id: modelId } // For OpenAI models which are just strings | ||
| : models[modelId] // For other models that have full info objects | ||
| const apiConfig = { ...apiConfiguration, [configKey]: modelId, [infoKey]: modelInfo } | ||
| setApiConfiguration(apiConfig) | ||
| onUpdateApiConfig(apiConfig) | ||
| setValue(modelId) | ||
|
|
@@ -67,16 +102,42 @@ export const ModelPicker = ({ | |
| [apiConfiguration, configKey, infoKey, models, onUpdateApiConfig, setApiConfiguration], | ||
| ) | ||
|
|
||
| const debouncedRefreshModels = useMemo( | ||
| () => debounce(() => vscode.postMessage({ type: refreshMessageType }), 50), | ||
| [refreshMessageType], | ||
| ) | ||
| const debouncedRefreshModels = useMemo(() => { | ||
| return debounce(() => { | ||
| const message = refreshValues | ||
| ? { type: refreshMessageType, values: refreshValues } | ||
| : { type: refreshMessageType } | ||
| vscode.postMessage(message) | ||
| }, 100) | ||
| }, [refreshMessageType, refreshValues]) | ||
|
|
||
| useMount(() => { | ||
| debouncedRefreshModels() | ||
| return () => debouncedRefreshModels.clear() | ||
| }) | ||
|
|
||
| useEffect(() => { | ||
| if (!refreshValues) { | ||
| prevRefreshValuesRef.current = undefined | ||
| return | ||
| } | ||
|
|
||
| // Check if all values in refreshValues are truthy | ||
| if (Object.values(refreshValues).some((value) => !value)) { | ||
| prevRefreshValuesRef.current = undefined | ||
| return | ||
| } | ||
|
|
||
| // Compare with previous values | ||
| const prevValues = prevRefreshValuesRef.current | ||
| if (prevValues && JSON.stringify(prevValues) === JSON.stringify(refreshValues)) { | ||
| return | ||
| } | ||
|
|
||
| prevRefreshValuesRef.current = refreshValues | ||
| debouncedRefreshModels() | ||
| }, [debouncedRefreshModels, refreshValues]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick - adding useEffect(() => {
if (!refreshValues) return;
debouncedRefreshModels();
}, [debouncedRefreshModels, JSON.stringify(refreshValues)]);I'm not 100% sure the ref is actually needed, but I'll defer to you. |
||
|
|
||
| useEffect(() => setValue(selectedModelId), [selectedModelId]) | ||
|
|
||
| return ( | ||
|
|
@@ -104,6 +165,17 @@ export const ModelPicker = ({ | |
| </CommandItem> | ||
| ))} | ||
| </CommandGroup> | ||
| {allowCustomModel && ( | ||
| <CommandGroup heading="Custom"> | ||
| <CommandItem | ||
| onSelect={() => { | ||
| setIsCustomModel(true) | ||
| setOpen(false) | ||
| }}> | ||
| + Add custom model | ||
| </CommandItem> | ||
| </CommandGroup> | ||
| )} | ||
| </CommandList> | ||
| </Command> | ||
| </PopoverContent> | ||
|
|
@@ -125,6 +197,28 @@ export const ModelPicker = ({ | |
| <VSCodeLink onClick={() => onSelect(recommendedModel)}>{recommendedModel}.</VSCodeLink> | ||
| You can also try searching "free" for no-cost options currently available. | ||
| </p> | ||
| {allowCustomModel && isCustomModel && ( | ||
| <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"> | ||
| <div className="bg-[var(--vscode-editor-background)] p-6 rounded-lg w-96"> | ||
| <h3 className="text-lg font-semibold mb-4">Add Custom Model</h3> | ||
| <input | ||
| type="text" | ||
| className="w-full p-2 mb-4 bg-[var(--vscode-input-background)] text-[var(--vscode-input-foreground)] border border-[var(--vscode-input-border)] rounded" | ||
| placeholder="Enter model ID" | ||
| value={customModelId} | ||
| onChange={(e) => setCustomModelId(e.target.value)} | ||
| /> | ||
| <div className="flex justify-end gap-2"> | ||
| <Button variant="secondary" onClick={() => setIsCustomModel(false)}> | ||
| Cancel | ||
| </Button> | ||
| <Button onClick={() => onSelectCustomModel(customModelId)} disabled={!customModelId.trim()}> | ||
| Add | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.