Elffuss BraidHub

kikocis/continue public

main · 1.7 KiBRawPermalink
1import { updateModelName } from "../../auth/workos.js";
2import {
3 SERVICE_NAMES,
4 serviceContainer,
5 services,
6} from "../../services/index.js";
7import { ModelServiceState } from "../../services/types.js";
8import { useNavigation } from "../context/NavigationContext.js";
9
10interface ModelOption {
11 id: string;
12 name: string;
13 index: number;
14 provider: string;
15}
16
17interface UseModelSelectorProps {
18 onMessage: (message: {
19 role: string;
20 content: string;
21 messageType: "system";
22 }) => void;
23 onModelSwitch?: () => void;
24}
25
26export function useModelSelector({
27 onMessage,
28 onModelSwitch,
29}: UseModelSelectorProps) {
30 const { closeCurrentScreen } = useNavigation();
31
32 const handleModelSelect = async (model: ModelOption) => {
33 closeCurrentScreen();
34
35 try {
36 await services.model.switchModel(model.index);
37 const modelInfo = services.model.getModelInfo();
38
39 // Update the service container to trigger re-renders
40 const currentState = services.model.getState();
41 serviceContainer.set<ModelServiceState>(
42 SERVICE_NAMES.MODEL,
43 currentState,
44 );
45
46 // Persist the model choice using the actual model name
47 if (modelInfo?.name) {
48 updateModelName(modelInfo.name);
49 }
50
51 onMessage({
52 role: "system",
53 content: `Switched to model: ${modelInfo?.name}`,
54 messageType: "system",
55 });
56
57 // Trigger any additional actions after model switch
58 if (onModelSwitch) {
59 onModelSwitch();
60 }
61 } catch (error: any) {
62 onMessage({
63 role: "system",
64 content: `Failed to switch model: ${error.message}`,
65 messageType: "system",
66 });
67 }
68 };
69
70 return {
71 handleModelSelect,
72 };
73}