Skip to content

Commit

Permalink
new window spawns with new provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lily-de committed Jan 17, 2025
1 parent 746641c commit e6fe753
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 98 deletions.
59 changes: 1 addition & 58 deletions ui/desktop/src/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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();
Expand Down
42 changes: 2 additions & 40 deletions ui/desktop/src/components/settings/Keys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ModalHeader,
ModalTitle
} from '../ui/modal';
import { initializeSystem } from '../../utils/systemInitializer';

const PROVIDER_ORDER = ['openai', 'anthropic', 'databricks'];

Expand Down Expand Up @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions ui/desktop/src/utils/systemInitializer.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit e6fe753

Please sign in to comment.