π OpenHands: AI-Driven Development
Fix for setting LLM model, frontend settings refactor (#1169)
Robert Brennan9fd7068204ce
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsxindex db9ce8fb73..039a53929d 100644--- a/frontend/src/App.tsx+++ b/frontend/src/App.tsx@@ -10,9 +10,10 @@ import SettingModal from "./components/SettingModal";import Terminal from "./components/Terminal";import Workspace from "./components/Workspace";import { fetchMsgTotal } from "./services/session";-import { fetchConfigurations, saveSettings } from "./services/settingsService";+import { initializeAgent } from "./services/settingsService";import Socket from "./services/socket";import { ResConfigurations, ResFetchMsgTotal } from "./types/ResponseType";+import ActionType from "./types/ActionType";import { getCachedConfig } from "./utils/storage";interface Props {@@ -39,22 +40,6 @@ function App(): JSX.Element {const [settingOpen, setSettingOpen] = useState(false);const [loadMsgWarning, setLoadMsgWarning] = useState(false);- const getConfigurations = () => {- fetchConfigurations()- .then((data: ResConfigurations) => {- const settings = getCachedConfig();-- saveSettings(- Object.fromEntries(- Object.entries(data).map(([key, value]) => [key, String(value)]),- ),- settings,- true,- );- })- .catch();- };-const getMsgTotal = () => {fetchMsgTotal().then((data: ResFetchMsgTotal) => {@@ -69,9 +54,10 @@ function App(): JSX.Element {if (initOnce) return;initOnce = true;- Socket.registerCallback("open", [getConfigurations, getMsgTotal]);+ initializeAgent();++ Socket.registerCallback("open", [getMsgTotal]);- getConfigurations();getMsgTotal();}, []);diff --git a/frontend/src/components/SettingModal.tsx b/frontend/src/components/SettingModal.tsxindex 79df456508..c92421623a 100644--- a/frontend/src/components/SettingModal.tsx+++ b/frontend/src/components/SettingModal.tsx@@ -1,5 +1,4 @@import React, { useEffect, useState } from "react";-import { useSelector } from "react-redux";import {Autocomplete,AutocompleteItem,@@ -12,11 +11,10 @@ import { useTranslation } from "react-i18next";import {fetchAgents,fetchModels,- INITIAL_AGENTS,- INITIAL_MODELS,saveSettings,+ getCurrentSettings,+ Settings,} from "../services/settingsService";-import { RootState } from "../store";import { I18nKey } from "../i18n/declaration";import { AvailableLanguages } from "../i18n";import { ArgConfigType } from "../types/ConfigType";@@ -27,56 +25,39 @@ interface Props {onClose: () => void;}-const cachedModels = JSON.parse(- localStorage.getItem("supportedModels") || "[]",-);-const cachedAgents = JSON.parse(- localStorage.getItem("supportedAgents") || "[]",-);-function InnerSettingModal({ isOpen, onClose }: Props): JSX.Element {- const settings = useSelector((state: RootState) => state.settings);- const [model, setModel] = useState(settings[ArgConfigType.LLM_MODEL]);+ const currentSettings: Settings = getCurrentSettings();+ const [model, setModel] = useState(currentSettings[ArgConfigType.LLM_MODEL]);const [inputModel, setInputModel] = useState(- settings[ArgConfigType.LLM_MODEL],+ currentSettings[ArgConfigType.LLM_MODEL],+ );+ const [agent, setAgent] = useState(currentSettings[ArgConfigType.AGENT]);+ const [language, setLanguage] = useState(+ currentSettings[ArgConfigType.LANGUAGE],);- const [agent, setAgent] = useState(settings[ArgConfigType.AGENT]);- const [language, setLanguage] = useState(settings[ArgConfigType.LANGUAGE]);const { t } = useTranslation();- const [supportedModels, setSupportedModels] = useState(- cachedModels.length > 0 ? cachedModels : INITIAL_MODELS,- );- const [supportedAgents, setSupportedAgents] = useState(- cachedAgents.length > 0 ? cachedAgents : INITIAL_AGENTS,- );+ const [supportedModels, setSupportedModels] = useState([]);+ const [supportedAgents, setSupportedAgents] = useState([]);useEffect(() => {fetchModels().then((fetchedModels) => {const sortedModels = fetchedModels.sort(); // Sorting the models alphabeticallysetSupportedModels(sortedModels);- localStorage.setItem("supportedModels", JSON.stringify(sortedModels));});fetchAgents().then((fetchedAgents) => {setSupportedAgents(fetchedAgents);- localStorage.setItem("supportedAgents", JSON.stringify(fetchedAgents));});}, []);const handleSaveCfg = () => {- saveSettings(- {- [ArgConfigType.LLM_MODEL]: model ?? inputModel,- [ArgConfigType.AGENT]: agent,- [ArgConfigType.LANGUAGE]: language,- },- Object.fromEntries(- Object.entries(settings).map(([key, value]) => [key, value]),- ),- false,- );+ saveSettings({+ [ArgConfigType.LLM_MODEL]: model ?? inputModel,+ [ArgConfigType.AGENT]: agent,+ [ArgConfigType.LANGUAGE]: language,+ });onClose();};@@ -150,7 +131,7 @@ function InnerSettingModal({ isOpen, onClose }: Props): JSX.Element {<SelectselectionMode="single"onChange={(e) => setLanguage(e.target.value)}- selectedKeys={[language]}+ selectedKeys={[language || ""]}label={t(I18nKey.CONFIGURATION$LANGUAGE_SELECT_LABEL)}>{AvailableLanguages.map((lang) => (diff --git a/frontend/src/services/settingsService.test.ts b/frontend/src/services/settingsService.test.tsindex 4e588b9306..06928ad55f 100644--- a/frontend/src/services/settingsService.test.ts+++ b/frontend/src/services/settingsService.test.ts@@ -1,29 +1,22 @@-import { mergeAndUpdateSettings } from "./settingsService";+import { getUpdatedSettings } from "./settingsService";import { ArgConfigType } from "../types/ConfigType";describe("mergeAndUpdateSettings", () => {it("should return initial settings if newSettings is empty", () => {const oldSettings = { key1: "value1" };- const isInit = false;- const result = mergeAndUpdateSettings({}, oldSettings, isInit);+ const result = getUpdatedSettings({}, oldSettings);- expect(result.mergedSettings).toEqual(oldSettings);- expect(result.updatedSettings).toEqual({});+ expect(result).toEqual({});});- it("should add new keys to mergedSettings and updatedSettings", () => {+ it("should add new keys to updatedSettings", () => {const oldSettings = { key1: "value1" };const newSettings = { key2: "value2" };- const isInit = false;- const result = mergeAndUpdateSettings(newSettings, oldSettings, isInit);+ const result = getUpdatedSettings(newSettings, oldSettings);- expect(result.mergedSettings).toEqual({- key1: "value1",- key2: "value2",- });- expect(result.updatedSettings).toEqual({+ expect(result).toEqual({key2: "value2", // New key});});@@ -31,31 +24,13 @@ describe("mergeAndUpdateSettings", () => {it("should overwrite non-DISPLAY_MAP keys in mergedSettings", () => {const oldSettings = { key1: "value1" };const newSettings = { key1: "newvalue1" };- const isInit = false;-- const result = mergeAndUpdateSettings(newSettings, oldSettings, isInit);-- expect(result.mergedSettings).toEqual({ key1: "newvalue1" });- expect(result.updatedSettings).toEqual({});- });-- it("should keep old values in mergedSettings if they are equal", () => {- const oldSettings = {- [ArgConfigType.LLM_MODEL]: "gpt-4-0125-preview",- [ArgConfigType.AGENT]: "MonologueAgent",- };- const newSettings = {- [ArgConfigType.AGENT]: "MonologueAgent",- };- const isInit = false;- const result = mergeAndUpdateSettings(newSettings, oldSettings, isInit);+ const result = getUpdatedSettings(newSettings, oldSettings);- expect(result.mergedSettings).toEqual(oldSettings);- expect(result.updatedSettings).toEqual({});+ expect(result).toEqual({});});- it("should keep old values in mergedSettings if isInit is true and old value is not empty", () => {+ it("should show no values if they are equal", () => {const oldSettings = {[ArgConfigType.LLM_MODEL]: "gpt-4-0125-preview",[ArgConfigType.AGENT]: "MonologueAgent",@@ -63,15 +38,13 @@ describe("mergeAndUpdateSettings", () => {const newSettings = {[ArgConfigType.AGENT]: "MonologueAgent",};- const isInit = true;- const result = mergeAndUpdateSettings(newSettings, oldSettings, isInit);+ const result = getUpdatedSettings(newSettings, oldSettings);- expect(result.mergedSettings).toEqual(oldSettings);- expect(result.updatedSettings).toEqual({});+ expect(result).toEqual({});});- it("should update mergedSettings, updatedSettings and set needToSend to true for relevant changes", () => {+ it("should update all settings", () => {const oldSettings = {[ArgConfigType.LLM_MODEL]: "gpt-4-0125-preview",[ArgConfigType.AGENT]: "MonologueAgent",@@ -83,22 +56,13 @@ describe("mergeAndUpdateSettings", () => {key1: "newvalue1",key2: "value2",};- const isInit = false;- const result = mergeAndUpdateSettings(newSettings, oldSettings, isInit);+ const result = getUpdatedSettings(newSettings, oldSettings);- expect(result.mergedSettings).toEqual({- [ArgConfigType.LLM_MODEL]: "gpt-4-0125-preview",- [ArgConfigType.AGENT]: "CodeActAgent", // Updated value- [ArgConfigType.LANGUAGE]: "es", // New key added- key1: "newvalue1", // Updated value- key2: "value2", // New key added- });- expect(result.updatedSettings).toEqual({+ expect(result).toEqual({[ArgConfigType.AGENT]: "CodeActAgent",[ArgConfigType.LANGUAGE]: "es",key2: "value2",});- expect(result.needToSend).toBe(true);});});diff --git a/frontend/src/services/settingsService.ts b/frontend/src/services/settingsService.tsindex 6bad460787..c907d82995 100644--- a/frontend/src/services/settingsService.ts+++ b/frontend/src/services/settingsService.ts@@ -1,23 +1,12 @@import { setInitialized } from "../state/taskSlice";import store from "../store";import ActionType from "../types/ActionType";+import { SupportedSettings } from "../types/ConfigType";import Socket from "./socket";-import { setAllSettings, setByKey } from "../state/settingsSlice";-import { ResConfigurations } from "../types/ResponseType";-import { ArgConfigType } from "../types/ConfigType";+import { setByKey } from "../state/settingsSlice";import toast from "../utils/toast";-export async function fetchConfigurations(): Promise<ResConfigurations> {- const headers = new Headers({- "Content-Type": "application/json",- Authorization: `Bearer ${localStorage.getItem("token")}`,- });- const response = await fetch(`/api/configurations`, { headers });- if (response.status !== 200) {- throw new Error("Get configurations failed.");- }- return (await response.json()) as ResConfigurations;-}+export type Settings = { [key: string]: string };export async function fetchModels() {const response = await fetch(`/api/litellm-models`);@@ -25,123 +14,78 @@ export async function fetchModels() {}export async function fetchAgents() {- const response = await fetch(`/api/litellm-agents`);+ const response = await fetch(`/api/agents`);return response.json();}-export const INITIAL_MODELS = [- "gpt-3.5-turbo-1106",- "gpt-4-0125-preview",- "claude-3-haiku-20240307",- "claude-3-opus-20240229",- "claude-3-sonnet-20240229",-];--export type Model = (typeof INITIAL_MODELS)[number];--export const INITIAL_AGENTS = ["MonologueAgent", "CodeActAgent"];--export type Agent = (typeof INITIAL_AGENTS)[number];-// all available settings in the frontend// TODO: add the values to i18n to support multi languages-const DISPLAY_MAP = new Map<string, string>([- [ArgConfigType.LLM_MODEL, "model"],- [ArgConfigType.AGENT, "agent"],- [ArgConfigType.WORKSPACE_DIR, "directory"],- [ArgConfigType.LANGUAGE, "language"],-]);--type SettingsUpdateInfo = {- mergedSettings: Record<string, string>;- updatedSettings: Record<string, string>;- needToSend: boolean;+const DISPLAY_MAP: { [key: string]: string } = {+ LLM_MODEL: "model",+ AGENT: "agent",+ LANGUAGE: "language",};-// Function to merge and update settings-export const mergeAndUpdateSettings = (- newSettings: Record<string, string>,- oldSettings: Record<string, string>,- isInit: boolean,-) =>- Object.keys(newSettings).reduce(- (acc, key) => {- const newValue = String(newSettings[key]);- const oldValue = oldSettings[key];-- // key doesn't exist in frontend settings will be overwritten by backend settings- if (oldValue === undefined) {- acc.mergedSettings[key] = newValue;- acc.updatedSettings[key] = newValue;- return acc;- }-- if (!DISPLAY_MAP.has(key)) {- acc.mergedSettings[key] = newValue;- return acc;- }+const DEFAULT_SETTINGS: Settings = {+ LLM_MODEL: "gpt-3.5-turbo",+ AGENT: "MonologueAgent",+ LANGUAGE: "en",+};- if (oldValue === newValue || (isInit && oldValue !== "")) {- acc.mergedSettings[key] = oldValue;- return acc;- }+const getSettingOrDefault = (key: string): string => {+ const value = localStorage.getItem(key);+ return value || DEFAULT_SETTINGS[key];+};- acc.mergedSettings[key] = newValue;- acc.updatedSettings[key] = newValue;- acc.needToSend = true;+export const getCurrentSettings = (): Settings => ({+ LLM_MODEL: getSettingOrDefault("LLM_MODEL"),+ AGENT: getSettingOrDefault("AGENT"),+ LANGUAGE: getSettingOrDefault("LANGUAGE"),+});- return acc;- },- {- mergedSettings: { ...oldSettings },- updatedSettings: {},- needToSend: false,- } as SettingsUpdateInfo,- );+// Function to merge and update settings+export const getUpdatedSettings = (+ newSettings: Settings,+ currentSettings: Settings,+) => {+ const updatedSettings: Settings = {};+ SupportedSettings.forEach((setting) => {+ if (newSettings[setting] !== currentSettings[setting]) {+ updatedSettings[setting] = newSettings[setting];+ }+ });+ return updatedSettings;+};const dispatchSettings = (updatedSettings: Record<string, string>) => {let i = 0;for (const [key, value] of Object.entries(updatedSettings)) {- if (DISPLAY_MAP.has(key)) {- store.dispatch(setByKey({ key, value }));+ store.dispatch(setByKey({ key, value }));+ if (key in DISPLAY_MAP) {setTimeout(() => {- toast.settingsChanged(`Set ${DISPLAY_MAP.get(key)} to "${value}"`);+ toast.settingsChanged(`Set ${DISPLAY_MAP[key]} to "${value}"`);}, i * 500);i += 1;}}};-const sendSettings = (- mergedSettings: Record<string, string>,- needToSend: boolean,- isInit: boolean,-) => {- const settingsCopy = { ...mergedSettings };- delete settingsCopy.ALL_SETTINGS;-- if (needToSend || isInit) {- const event = { action: ActionType.INIT, args: settingsCopy };- const eventString = JSON.stringify(event);- store.dispatch(setInitialized(false));- Socket.send(eventString);- }+export const initializeAgent = () => {+ const event = { action: ActionType.INIT, args: getCurrentSettings() };+ const eventString = JSON.stringify(event);+ store.dispatch(setInitialized(false));+ Socket.send(eventString);};// Save and send settings to the server-export function saveSettings(- newSettings: { [key: string]: string },- oldSettings: { [key: string]: string },- isInit: boolean = false,-): void {- const { mergedSettings, updatedSettings, needToSend } =- mergeAndUpdateSettings(newSettings, oldSettings, isInit);+export function saveSettings(newSettings: Settings): void {+ const currentSettings = getCurrentSettings();+ const updatedSettings = getUpdatedSettings(newSettings, currentSettings);- dispatchSettings(updatedSettings);-- if (isInit) {- store.dispatch(setAllSettings(JSON.stringify(newSettings)));+ if (Object.keys(updatedSettings).length === 0) {+ return;}- sendSettings(mergedSettings, needToSend, isInit);+ dispatchSettings(updatedSettings);+ initializeAgent();}diff --git a/frontend/src/services/socket.ts b/frontend/src/services/socket.tsindex 1362bf87c2..da2f64a2b2 100644--- a/frontend/src/services/socket.ts+++ b/frontend/src/services/socket.ts@@ -19,10 +19,11 @@ class Socket {close: [],};- // prevent it failed in the first run, all related listen events never be called- private static isFirstRun = true;+ private static initializing = false;public static tryInitialize(): void {+ if (Socket.initializing) return;+ Socket.initializing = true;getToken().then((token) => {Socket._initialize(token);@@ -31,11 +32,9 @@ class Socket {const msg = `Connection failed. Retry...`;toast.stickyError("ws", msg);- if (this.isFirstRun) {- setTimeout(() => {- this.tryInitialize();- }, 3000);- }+ setTimeout(() => {+ this.tryInitialize();+ }, 1500);});}@@ -47,6 +46,7 @@ class Socket {Socket._socket.onopen = (e) => {toast.stickySuccess("ws", "Connected to server.");+ Socket.initializing = false;Socket.callbacks.open?.forEach((callback) => {callback(e);});@@ -67,8 +67,6 @@ class Socket {Socket.tryInitialize();}, 3000); // Reconnect after 3 seconds};-- this.isFirstRun = false;}static isConnected(): boolean {@@ -78,7 +76,13 @@ class Socket {}static send(message: string): void {- if (!Socket.isConnected()) Socket.tryInitialize();+ if (!Socket.isConnected()) {+ Socket.tryInitialize();+ }+ if (Socket.initializing) {+ setTimeout(() => Socket.send(message), 1000);+ return;+ }if (Socket.isConnected()) {Socket._socket?.send(message);diff --git a/frontend/src/types/ConfigType.tsx b/frontend/src/types/ConfigType.tsxindex 350ab21c80..d72bd7cf55 100644--- a/frontend/src/types/ConfigType.tsx+++ b/frontend/src/types/ConfigType.tsx@@ -1,35 +1,13 @@enum ArgConfigType {- LLM_API_KEY = "LLM_API_KEY",- LLM_BASE_URL = "LLM_BASE_URL",- WORKSPACE_DIR = "WORKSPACE_DIR",LLM_MODEL = "LLM_MODEL",- SANDBOX_CONTAINER_IMAGE = "SANDBOX_CONTAINER_IMAGE",- RUN_AS_DEVIN = "RUN_AS_DEVIN",- LLM_EMBEDDING_MODEL = "LLM_EMBEDDING_MODEL",- LLM_NUM_RETRIES = "LLM_NUM_RETRIES",- LLM_COOLDOWN_TIME = "LLM_COOLDOWN_TIME",- DIRECTORY_REWRITE = "DIRECTORY_REWRITE",- MAX_ITERATIONS = "MAX_ITERATIONS",- MAX_CHARS = "MAX_CHARS",AGENT = "AGENT",-LANGUAGE = "LANGUAGE",}-const SupportedList: string[] = [- // ArgConfigType.LLM_API_KEY,- // ArgConfigType.LLM_BASE_URL,- // ArgConfigType.WORKSPACE_DIR,+const SupportedSettings: string[] = [ArgConfigType.LLM_MODEL,- // ArgConfigType.SANDBOX_CONTAINER_IMAGE,- // ArgConfigType.RUN_AS_DEVIN,- // ArgConfigType.LLM_EMBEDDING_MODEL,- // ArgConfigType.LLM_NUM_RETRIES,- // ArgConfigType.LLM_COOLDOWN_TIME,- // ArgConfigType.DIRECTORY_REWRITE,- // ArgConfigType.MAX_ITERATIONS,ArgConfigType.AGENT,ArgConfigType.LANGUAGE,];-export { ArgConfigType, SupportedList };+export { ArgConfigType, SupportedSettings };diff --git a/opendevin/config.py b/opendevin/config.pyindex f9dd8656e8..4f8dccb77d 100644--- a/opendevin/config.py+++ b/opendevin/config.py@@ -1,4 +1,3 @@-import copyimport osimport argparse@@ -85,12 +84,3 @@ def get(key: str, required: bool = False):if not value and required:raise KeyError(f"Please set '{key}' in `config.toml` or `.env`.")return value---def get_fe_config() -> dict:- """- Get all the frontend configuration values by performing a deep copy.- """- fe_config = copy.deepcopy(config)- del fe_config['LLM_API_KEY']- return fe_configdiff --git a/opendevin/llm/llm.py b/opendevin/llm/llm.pyindex 0beaa2dab7..aab9f49312 100644--- a/opendevin/llm/llm.py+++ b/opendevin/llm/llm.py@@ -22,6 +22,7 @@ class LLM:num_retries=DEFAULT_LLM_NUM_RETRIES,cooldown_time=DEFAULT_LLM_COOLDOWN_TIME,):+ opendevin_logger.info(f'Initializing LLM with model: {model}')self.model_name = model if model else DEFAULT_MODEL_NAMEself.api_key = api_key if api_key else DEFAULT_API_KEYself.base_url = base_url if base_url else DEFAULT_BASE_URL@@ -33,7 +34,8 @@ class LLM:def my_wait(retry_state):seconds = (retry_state.attempt_number) * cooldown_time- opendevin_logger.info(f'Attempt #{retry_state.attempt_number} | Sleeping for {seconds}s for {retry_state.outcome.exception()}', )+ opendevin_logger.warning(f'LLM error: {retry_state.outcome.exception()}')+ opendevin_logger.info(f'Attempt #{retry_state.attempt_number} | Sleeping for {seconds}s')return seconds@retry(reraise=True,diff --git a/opendevin/mock/listen.py b/opendevin/mock/listen.pyindex 089e44372f..8fb72e68a0 100644--- a/opendevin/mock/listen.py+++ b/opendevin/mock/listen.py@@ -43,7 +43,7 @@ def read_llm_models():][email protected]('/litellm-agents')[email protected]('/agents')def read_llm_agents():return ['MonologueAgent',diff --git a/opendevin/server/agent/agent.py b/opendevin/server/agent/agent.pyindex 001cd2e016..fd87d6884f 100644--- a/opendevin/server/agent/agent.py+++ b/opendevin/server/agent/agent.py@@ -63,10 +63,6 @@ class AgentUnit:match action:case ActionType.INIT:- if self.controller is not None:- # Agent already started, no need to create a new one- await self.init_done()- returnawait self.create_controller(data)case ActionType.START:await self.start_task(data)diff --git a/opendevin/server/agent/manager.py b/opendevin/server/agent/manager.pyindex e842a46df4..5584035587 100644--- a/opendevin/server/agent/manager.py+++ b/opendevin/server/agent/manager.py@@ -27,6 +27,7 @@ class AgentManager:"""Dispatches actions to the agent from the client."""if sid not in self.sid_to_agent:# self.register_agent(sid) # auto-register agent, may be opened later+ logger.error(f'Agent not registered: {sid}')await session_manager.send_error(sid, 'Agent not registered')returndiff --git a/opendevin/server/listen.py b/opendevin/server/listen.pyindex 3b43dc6465..b8f3dadf04 100644--- a/opendevin/server/listen.py+++ b/opendevin/server/listen.py@@ -36,6 +36,7 @@ async def websocket_endpoint(websocket: WebSocket):await websocket.accept()sid = get_sid_from_token(websocket.query_params.get('token') or '')if sid == '':+ logger.error('Failed to decode token')returnsession_manager.add_session(sid, websocket)# TODO: actually the agent_manager is created for each websocket connection, even if the session id is the same,@@ -52,12 +53,13 @@ async def get_litellm_models():return list(set(litellm.model_list + list(litellm.model_cost.keys())))[email protected]('/api/litellm-agents')-async def get_litellm_agents():[email protected]('/api/agents')+async def get_agents():"""Get all agents supported by LiteLLM."""- return Agent.list_agents()+ agents = Agent.list_agents()+ return agents@app.get('/api/auth')@@ -101,11 +103,6 @@ async def del_messages(return {'ok': True}[email protected]('/api/configurations')-def read_default_model():- return config.get_fe_config()--@app.get('/api/refresh-files')def refresh_files():structure = files.get_folder_structure(Path(str(config.get('WORKSPACE_BASE'))))