Adding a model selector
This commit is contained in:
@@ -13,16 +13,20 @@ import {
|
||||
checkHealth,
|
||||
loadSavedComputers,
|
||||
loadSavedProjects,
|
||||
loadLastModel,
|
||||
removeSavedComputer,
|
||||
saveHostConfig,
|
||||
saveSavedProjects,
|
||||
saveLastModel,
|
||||
upsertSavedComputer,
|
||||
} from "@/lib/api";
|
||||
import { createSocket } from "@/lib/socket";
|
||||
import { createSocket, listModels as requestModels, setRuntimeModel as requestSetRuntimeModel } from "@/lib/socket";
|
||||
import type {
|
||||
ChatEvent,
|
||||
ChatHistoryPayload,
|
||||
LiveRuntimeInfo,
|
||||
ModelListPayload,
|
||||
ModelRef,
|
||||
PersistedSessionInfo,
|
||||
RuntimeCreatedPayload,
|
||||
SavedComputer,
|
||||
@@ -51,7 +55,10 @@ type SocketContextValue = {
|
||||
removeSavedComputerEntry: (id: string) => Promise<void>;
|
||||
disconnect: () => void;
|
||||
refreshSessions: () => void;
|
||||
createRuntime: (cwd: string, name?: string) => void;
|
||||
createRuntime: (cwd: string, name?: string, model?: ModelRef) => void;
|
||||
lastModel: ModelRef | null;
|
||||
listModels: (payload: { runtimeId?: string; cwd?: string }) => Promise<ModelListPayload>;
|
||||
setRuntimeModel: (runtimeId: string, model: ModelRef) => Promise<void>;
|
||||
openRuntime: (sessionFile: string) => void;
|
||||
attachRuntime: (runtimeId: string) => void;
|
||||
sendPrompt: (runtimeId: string, message: string) => void;
|
||||
@@ -78,6 +85,7 @@ export function SocketProvider({ children }: { children: ReactNode }) {
|
||||
const [persistedSessions, setPersistedSessions] = useState<PersistedSessionInfo[]>([]);
|
||||
const [liveRuntimes, setLiveRuntimes] = useState<LiveRuntimeInfo[]>([]);
|
||||
const [savedProjects, setSavedProjects] = useState<string[]>([]);
|
||||
const [lastModel, setLastModel] = useState<ModelRef | null>(null);
|
||||
|
||||
const socketRef = useRef<Socket | null>(null);
|
||||
const chatListenersRef = useRef(new Set<(event: ChatEvent) => void>());
|
||||
@@ -88,6 +96,7 @@ export function SocketProvider({ children }: { children: ReactNode }) {
|
||||
}>({ created: [], opened: [] });
|
||||
|
||||
useEffect(() => {
|
||||
void loadLastModel().then(setLastModel);
|
||||
void loadSavedComputers().then((computers) => {
|
||||
setSavedComputers(computers);
|
||||
const recent = computers[0];
|
||||
@@ -240,8 +249,44 @@ export function SocketProvider({ children }: { children: ReactNode }) {
|
||||
socketRef.current?.emit("sessions:list");
|
||||
}, []);
|
||||
|
||||
const createRuntime = useCallback((cwd: string, name?: string) => {
|
||||
socketRef.current?.emit("runtime:create", { cwd, name });
|
||||
const createRuntime = useCallback((cwd: string, name?: string, model?: ModelRef) => {
|
||||
socketRef.current?.emit("runtime:create", { cwd, name, model });
|
||||
}, []);
|
||||
|
||||
const listModels = useCallback(
|
||||
async (payload: { runtimeId?: string; cwd?: string }) => {
|
||||
const socket = socketRef.current;
|
||||
if (!socket) {
|
||||
throw new Error("Not connected");
|
||||
}
|
||||
try {
|
||||
return await requestModels(socket, payload);
|
||||
} catch (requestError) {
|
||||
const message = requestError instanceof Error ? requestError.message : "Failed to load models";
|
||||
setError(message);
|
||||
throw requestError;
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const setRuntimeModel = useCallback(async (runtimeId: string, model: ModelRef) => {
|
||||
const socket = socketRef.current;
|
||||
if (!socket) {
|
||||
throw new Error("Not connected");
|
||||
}
|
||||
setLastModel(model);
|
||||
try {
|
||||
const runtime = await requestSetRuntimeModel(socket, runtimeId, model);
|
||||
await saveLastModel(model);
|
||||
setLiveRuntimes((current) =>
|
||||
current.map((entry) => (entry.runtimeId === runtime.runtimeId ? runtime : entry)),
|
||||
);
|
||||
} catch (requestError) {
|
||||
const message = requestError instanceof Error ? requestError.message : "Failed to set model";
|
||||
setError(message);
|
||||
throw requestError;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const openRuntime = useCallback((sessionFile: string) => {
|
||||
@@ -307,6 +352,7 @@ export function SocketProvider({ children }: { children: ReactNode }) {
|
||||
persistedSessions,
|
||||
liveRuntimes,
|
||||
savedProjects,
|
||||
lastModel,
|
||||
addProject,
|
||||
removeProject,
|
||||
setHost,
|
||||
@@ -318,6 +364,8 @@ export function SocketProvider({ children }: { children: ReactNode }) {
|
||||
disconnect,
|
||||
refreshSessions,
|
||||
createRuntime,
|
||||
listModels,
|
||||
setRuntimeModel,
|
||||
openRuntime,
|
||||
attachRuntime,
|
||||
sendPrompt,
|
||||
@@ -338,6 +386,7 @@ export function SocketProvider({ children }: { children: ReactNode }) {
|
||||
persistedSessions,
|
||||
liveRuntimes,
|
||||
savedProjects,
|
||||
lastModel,
|
||||
addProject,
|
||||
removeProject,
|
||||
connect,
|
||||
@@ -346,6 +395,8 @@ export function SocketProvider({ children }: { children: ReactNode }) {
|
||||
disconnect,
|
||||
refreshSessions,
|
||||
createRuntime,
|
||||
listModels,
|
||||
setRuntimeModel,
|
||||
openRuntime,
|
||||
attachRuntime,
|
||||
sendPrompt,
|
||||
|
||||
Reference in New Issue
Block a user