feat: use thunj

This commit is contained in:
Patrick Erichsen 2025-04-08 18:47:29 -07:00
parent ea753e98d4
commit eae1dd20bb
9 changed files with 129 additions and 69 deletions

View file

@ -157,7 +157,7 @@ async function configYamlToContinueConfig(
summarize: [],
},
selectedModelByRole: {
chat: null, // not currently used - defaultModel on GUI is used
chat: null,
edit: null, // not currently used
apply: null,
embed: null,

View file

@ -1,11 +1,12 @@
import { CubeIcon } from "@heroicons/react/24/outline";
import { FormEventHandler, useContext, useState } from "react";
import { useDispatch } from "react-redux";
import { Button, Input, InputSubtext, lightGray } from "../..";
import { useAuth } from "../../../context/Auth";
import { IdeMessengerContext } from "../../../context/IdeMessenger";
import { models } from "../../../pages/AddNewModel/configs/models";
import { providers } from "../../../pages/AddNewModel/configs/providers";
import { setSelectedChatModel } from "../../../redux/slices/configSlice";
import { useAppDispatch } from "../../../redux/hooks";
import { updateSelectedModelByRole } from "../../../redux/thunks";
import AddModelButtonSubtext from "../../AddModelButtonSubtext";
const { anthropic, mistral } = providers;
@ -25,7 +26,8 @@ interface BestExperienceConfigFormProps {
function BestExperienceConfigForm({
onComplete,
}: BestExperienceConfigFormProps) {
const dispatch = useDispatch();
const dispatch = useAppDispatch();
const { selectedProfile } = useAuth();
const ideMessenger = useContext(IdeMessengerContext);
@ -61,8 +63,13 @@ function BestExperienceConfigForm({
model: repoMapConfig,
role: "repoMapFileSelection",
});
dispatch(
setSelectedChatModel({ title: chatModelConfig.title, force: true }),
updateSelectedModelByRole({
selectedProfile,
role: "chat",
modelTitle: chatModelConfig.title,
}),
);
if (!!autocompleteApiKey) {

View file

@ -6,11 +6,12 @@ import {
LOCAL_ONBOARDING_PROVIDER_TITLE,
} from "core/config/onboarding";
import { useContext, useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { Button } from "../..";
import { useAuth } from "../../../context/Auth";
import { IdeMessengerContext } from "../../../context/IdeMessenger";
import { setSelectedChatModel } from "../../../redux/slices/configSlice";
import { useAppDispatch } from "../../../redux/hooks";
import { setDialogMessage, setShowDialog } from "../../../redux/slices/uiSlice";
import { updateSelectedModelByRole } from "../../../redux/thunks";
import AddModelButtonSubtext from "../../AddModelButtonSubtext";
import OllamaModelDownload from "../components/OllamaModelDownload";
import { OllamaStatus } from "../components/OllamaStatus";
@ -23,13 +24,14 @@ interface OnboardingLocalTabProps {
}
function OnboardingLocalTab({ isDialog }: OnboardingLocalTabProps) {
const dispatch = useDispatch();
const dispatch = useAppDispatch();
const ideMessenger = useContext(IdeMessengerContext);
const { submitOnboarding } = useSubmitOnboarding("Local", isDialog);
const [hasLoadedChatModel, setHasLoadedChatModel] = useState(false);
const [downloadedOllamaModels, setDownloadedOllamaModels] = useState<
string[]
>([]);
const { selectedProfile } = useAuth();
const [isOllamaConnected, setIsOllamaConnected] = useState(false);
@ -135,11 +137,11 @@ function OnboardingLocalTab({ isDialog }: OnboardingLocalTabProps) {
dispatch(setShowDialog(false));
}
// Set the selected model to the local chat model
dispatch(
setSelectedChatModel({
title: LOCAL_ONBOARDING_CHAT_TITLE,
force: true, // Because it doesn't exist in the webview's config object yet
updateSelectedModelByRole({
selectedProfile,
role: "chat",
modelTitle: LOCAL_ONBOARDING_CHAT_TITLE,
}),
);
}}

View file

@ -1,41 +1,31 @@
import { ModelRole } from "@continuedev/config-yaml";
import { ModelDescription } from "core";
import { useContext } from "react";
import { useAuth } from "../../../../context/Auth";
import { IdeMessengerContext } from "../../../../context/IdeMessenger";
import ModelRoleSelector from "../../../../pages/config/ModelRoleSelector";
import { useAppDispatch, useAppSelector } from "../../../../redux/hooks";
import { updateConfig } from "../../../../redux/slices/configSlice";
import { updateSelectedModelByRole } from "../../../../redux/thunks";
import { isJetBrains } from "../../../../util";
import { ExploreBlocksButton } from "./ExploreBlocksButton";
export function ModelsSection() {
const { selectedProfile } = useAuth();
const dispatch = useAppDispatch();
const ideMessenger = useContext(IdeMessengerContext);
const config = useAppSelector((state) => state.config.config);
const jetbrains = isJetBrains();
function handleRoleUpdate(role: ModelRole, model: ModelDescription | null) {
if (!selectedProfile) {
if (!model) {
return;
}
// Optimistic update
dispatch(
updateConfig({
...config,
selectedModelByRole: {
...config.selectedModelByRole,
[role]: model,
},
updateSelectedModelByRole({
role,
selectedProfile,
modelTitle: model.title,
}),
);
ideMessenger.post("config/updateSelectedModel", {
profileId: selectedProfile.id,
role,
title: model?.title ?? null,
});
}
return (

View file

@ -6,18 +6,15 @@ import {
PlusIcon,
} from "@heroicons/react/24/outline";
import { useContext, useEffect, useRef, useState } from "react";
import { useDispatch } from "react-redux";
import styled from "styled-components";
import { defaultBorderRadius, lightGray } from "..";
import { useAuth } from "../../context/Auth";
import { IdeMessengerContext } from "../../context/IdeMessenger";
import AddModelForm from "../../forms/AddModelForm";
import { useAppSelector } from "../../redux/hooks";
import {
selectSelectedChatModel,
setSelectedChatModel,
} from "../../redux/slices/configSlice";
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
import { selectSelectedChatModel } from "../../redux/slices/configSlice";
import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice";
import { updateSelectedModelByRole } from "../../redux/thunks";
import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util";
import { Listbox, ListboxButton, ListboxOption, ListboxOptions } from "../ui";
@ -122,8 +119,8 @@ function ModelOption({
}
function ModelSelect() {
const dispatch = useDispatch();
const defaultModel = useAppSelector(selectSelectedChatModel);
const dispatch = useAppDispatch();
const selectedChatModel = useAppSelector(selectSelectedChatModel);
const allModels = useAppSelector((state) => state.config.config.models);
const buttonRef = useRef<HTMLButtonElement>(null);
const [options, setOptions] = useState<Option[]>([]);
@ -157,14 +154,25 @@ function ModelSelect() {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "'" && isMetaEquivalentKeyPressed(event as any)) {
if (!selectedProfile) {
return;
}
const direction = event.shiftKey ? -1 : 1;
const currentIndex = options.findIndex(
(option) => option.value === defaultModel?.title,
(option) => option.value === selectedChatModel?.title,
);
let nextIndex = (currentIndex + 1 * direction) % options.length;
if (nextIndex < 0) nextIndex = options.length - 1;
const newModelTitle = options[nextIndex].value;
dispatch(setSelectedChatModel({ title: newModelTitle }));
dispatch(
updateSelectedModelByRole({
selectedProfile,
role: "chat",
modelTitle: newModelTitle,
}),
);
}
};
@ -172,7 +180,7 @@ function ModelSelect() {
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [options, defaultModel]);
}, [options, selectedChatModel]);
function onClickAddModel(e: MouseEvent) {
e.stopPropagation();
@ -195,11 +203,16 @@ function ModelSelect() {
}
return (
// <span className="line-clamp-1">Hiad sfasdfasdf asdfasdf</span>
<Listbox
onChange={async (val: string) => {
if (val === defaultModel?.title) return;
dispatch(setSelectedChatModel({ title: val }));
if (val === selectedChatModel?.title) return;
dispatch(
updateSelectedModelByRole({
selectedProfile,
role: "chat",
modelTitle: val,
}),
);
}}
>
<div className="relative flex">
@ -209,7 +222,7 @@ function ModelSelect() {
className="h-[18px] gap-1 border-none text-gray-400"
>
<span className="line-clamp-1 hover:brightness-110">
{modelSelectTitle(defaultModel) || "Select model"}
{modelSelectTitle(selectedChatModel) || "Select model"}
</span>
<ChevronDownIcon
className="h-2 w-2 flex-shrink-0 hover:brightness-110"
@ -224,7 +237,7 @@ function ModelSelect() {
idx={idx}
key={idx}
showMissingApiKeyMsg={option.apiKey === ""}
isSelected={option.value === defaultModel?.title}
isSelected={option.value === selectedChatModel?.title}
/>
))}
</div>

View file

@ -1,11 +1,11 @@
import { ArrowTopRightOnSquareIcon } from "@heroicons/react/24/outline";
import { useContext, useEffect, useState } from "react";
import { FormProvider, useForm } from "react-hook-form";
import { useDispatch } from "react-redux";
import { Button, Input, InputSubtext, StyledActionButton } from "../components";
import AddModelButtonSubtext from "../components/AddModelButtonSubtext";
import Alert from "../components/gui/Alert";
import ModelSelectionListbox from "../components/modelSelection/ModelSelectionListbox";
import { useAuth } from "../context/Auth";
import { IdeMessengerContext } from "../context/IdeMessenger";
import { completionParamsInputs } from "../pages/AddNewModel/configs/completionParamsInputs";
import { DisplayInfo } from "../pages/AddNewModel/configs/models";
@ -13,7 +13,8 @@ import {
ProviderInfo,
providers,
} from "../pages/AddNewModel/configs/providers";
import { setSelectedChatModel } from "../redux/slices/configSlice";
import { useAppDispatch } from "../redux/hooks";
import { updateSelectedModelByRole } from "../redux/thunks";
import { FREE_TRIAL_LIMIT_REQUESTS, hasPassedFTL } from "../util/freeTrial";
interface QuickModelSetupProps {
@ -33,13 +34,14 @@ function AddModelForm({
const [selectedProvider, setSelectedProvider] = useState<ProviderInfo>(
providers["openai"]!,
);
const dispatch = useAppDispatch();
const { selectedProfile } = useAuth();
const [selectedModel, setSelectedModel] = useState(
selectedProvider.packages[0],
);
const formMethods = useForm();
const dispatch = useDispatch();
const ideMessenger = useContext(IdeMessengerContext);
const popularProviderTitles = [
@ -115,7 +117,13 @@ function AddModelForm({
profileId: "local",
});
dispatch(setSelectedChatModel({ title: model.title, force: true }));
dispatch(
updateSelectedModelByRole({
selectedProfile,
role: "chat",
modelTitle: model.title,
}),
);
onDone();
}

View file

@ -78,24 +78,6 @@ export const configSlice = createSlice({
) => {
state.config = config;
},
setSelectedChatModel: (
state,
{ payload }: PayloadAction<{ title: string; force?: boolean }>,
) => {
const model = state.config.modelsByRole["chat"].find(
(model) => model.title === payload.title,
);
if (!model && !payload.force) return;
state.config = {
...state.config,
selectedModelByRole: {
...state.config.selectedModelByRole,
chat: model ?? null,
},
};
},
},
selectors: {
selectSelectedChatModelContextLength: (state): number => {
@ -113,8 +95,7 @@ export const configSlice = createSlice({
},
});
export const { updateConfig, setConfigResult, setSelectedChatModel } =
configSlice.actions;
export const { updateConfig, setConfigResult } = configSlice.actions;
export const {
selectSelectedChatModelContextLength,

View file

@ -5,3 +5,4 @@ export { streamNormalInput } from "./streamNormalInput";
export { streamResponseThunk } from "./streamResponse";
export { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
export { streamThunkWrapper } from "./streamThunkWrapper";
export { updateSelectedModelByRole } from "./updateSelectedModelByRole";

View file

@ -0,0 +1,58 @@
import { ModelRole } from "@continuedev/config-yaml";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { ProfileDescription } from "core/config/ProfileLifecycleManager";
import { updateConfig } from "../slices/configSlice";
import { ThunkApiType } from "../store";
export const updateSelectedModelByRole = createAsyncThunk<
void,
{
role: ModelRole;
modelTitle: string;
selectedProfile: ProfileDescription | null;
},
ThunkApiType
>(
"config/updateSelectedModel",
async (
{ role, modelTitle, selectedProfile },
{ dispatch, extra, getState },
) => {
if (!selectedProfile) {
return;
}
const state = getState();
const {
config: { config },
} = state;
const model = state.config.config.modelsByRole[role]?.find(
(m) => m.title === modelTitle,
);
if (!model) {
console.error(
`Model with title "${modelTitle}" not found for role "${role}"`,
);
return;
}
dispatch(
updateConfig({
...config,
selectedModelByRole: {
...config.selectedModelByRole,
[role]: model,
},
}),
);
extra.ideMessenger.post("config/updateSelectedModel", {
role,
profileId: selectedProfile.id,
title: modelTitle,
});
},
);