diff --git a/ui/desktop/src/ChatWindow.tsx b/ui/desktop/src/ChatWindow.tsx index 5783300a01..e4098e7b7f 100644 --- a/ui/desktop/src/ChatWindow.tsx +++ b/ui/desktop/src/ChatWindow.tsx @@ -26,6 +26,7 @@ import { ANTHROPIC_DEFAULT_MODEL, } from "./utils/providerUtils"; import Keys from "./components/settings/Keys"; +import { initializeSystem } from './utils/systemInitializer'; declare global { interface Window { @@ -333,37 +334,6 @@ function ChatContent({ ); } -// Function to send the system configuration to the server -const addSystemConfig = async (system: string) => { - console.log("calling add system"); - const systemConfig = { - type: "Stdio", - cmd: await window.electron.getBinaryPath("goosed"), - args: ["mcp", system], - }; - - try { - const response = await fetch(getApiUrl("/systems/add"), { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-Secret-Key": getSecretKey(), - }, - body: JSON.stringify(systemConfig), - }); - - if (!response.ok) { - throw new Error( - `Failed to add system config for ${system}: ${response.statusText}` - ); - } - - console.log(`Successfully added system config for ${system}`); - } catch (error) { - console.log(`Error adding system config for ${system}:`, error); - } -}; - export default function ChatWindow() { // Shared function to create a chat window const openNewChatWindow = () => { @@ -453,33 +423,6 @@ export default function ChatWindow() { return response; }; - const addAgent = async (provider: ProviderOption) => { - const response = await fetch(getApiUrl("/agent"), { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-Secret-Key": getSecretKey(), - }, - body: JSON.stringify({ provider: provider.id }), - }); - - if (!response.ok) { - throw new Error(`Failed to add agent: ${response.statusText}`); - } - - return response; - }; - - const initializeSystem = async (provider: ProviderOption) => { - try { - await addAgent(provider); - await addSystemConfig("developer2"); - } catch (error) { - console.error("Failed to initialize system:", error); - throw error; - } - }; - const handleModalSubmit = async (apiKey: string) => { try { const trimmedKey = apiKey.trim(); diff --git a/ui/desktop/src/components/settings/Keys.tsx b/ui/desktop/src/components/settings/Keys.tsx index 46e975eb34..5be1c4b61c 100644 --- a/ui/desktop/src/components/settings/Keys.tsx +++ b/ui/desktop/src/components/settings/Keys.tsx @@ -9,6 +9,7 @@ import { ModalHeader, ModalTitle } from '../ui/modal'; +import { initializeSystem } from '../../utils/systemInitializer'; const PROVIDER_ORDER = ['openai', 'anthropic', 'databricks']; @@ -293,46 +294,7 @@ export default function Keys() { const handleSelectProvider = async (providerId: string) => { setIsChangingProvider(true); try { - const url = getApiUrl("/agent"); - console.log("Attempting to fetch:", url); - - // Initialize agent with new provider - const agentResponse = await fetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-Secret-Key": getSecretKey(), - }, - body: JSON.stringify({ - provider: providerId, - }), - }).catch(error => { - console.error("Fetch failed:", error); - throw error; - }); - - if (agentResponse.status === 401) { - throw new Error("Unauthorized - invalid secret key"); - } - if (!agentResponse.ok) { - throw new Error(`Failed to set agent: ${agentResponse.statusText}`); - } - - // Initialize system config - const systemResponse = await fetch(getApiUrl("/system"), { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-Secret-Key": getSecretKey(), - }, - body: JSON.stringify({ system: "developer2" }), - }); - - if (!systemResponse.ok) { - throw new Error("Failed to set system config"); - } - - // Update localStorage + // Update localStorage const provider = providers.find(p => p.id === providerId); if (provider) { localStorage.setItem("GOOSE_PROVIDER", provider.name); diff --git a/ui/desktop/src/utils/systemInitializer.ts b/ui/desktop/src/utils/systemInitializer.ts new file mode 100644 index 0000000000..eddd7a9fb5 --- /dev/null +++ b/ui/desktop/src/utils/systemInitializer.ts @@ -0,0 +1,52 @@ +import { ProviderOption } from './providerUtils'; +import { getApiUrl, getSecretKey } from '../config'; + +export const addAgent = async (provider: ProviderOption) => { + const response = await fetch(getApiUrl("/agent"), { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-Secret-Key": getSecretKey(), + }, + body: JSON.stringify({ provider: provider.id }), + }); + + if (!response.ok) { + throw new Error(`Failed to add agent: ${response.statusText}`); + } + + return response; +}; + +export const addSystemConfig = async (system: string) => { + const systemConfig = { + type: "Stdio", + cmd: await window.electron.getBinaryPath("goosed"), + args: ["mcp", system], + }; + + const response = await fetch(getApiUrl("/systems/add"), { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-Secret-Key": getSecretKey(), + }, + body: JSON.stringify(systemConfig), + }); + + if (!response.ok) { + throw new Error(`Failed to add system config for ${system}: ${response.statusText}`); + } + + return response; +}; + +export const initializeSystem = async (provider: ProviderOption) => { + try { + await addAgent(provider); + await addSystemConfig("developer2"); + } catch (error) { + console.error("Failed to initialize system:", error); + throw error; + } +}; \ No newline at end of file