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 32bfa7a commit d3a81bc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 47 deletions.
17 changes: 10 additions & 7 deletions ui/desktop/src/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ProviderOption,
} from "./utils/providerUtils";
import Keys from "./components/settings/Keys";
import { initializeSystem } from './utils/systemInitializer';

declare global {
interface Window {
Expand Down Expand Up @@ -426,14 +427,14 @@ export default function ChatWindow() {
return response;
};

const addAgent = async (provider: string) => {
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 }),
body: JSON.stringify({ provider: provider.id }),
});

if (!response.ok) {
Expand All @@ -445,15 +446,17 @@ export default function ChatWindow() {

const addSystemConfig = async (system: string) => {
await addMCP("goosed", ["mcp", system]);
}
};

const initializeSystem = async (provider: string) => {
const initializeSystem = async (provider: ProviderOption) => {
try {
await addAgent(provider);
await addSystemConfig("developer2");
// add system from deep link up front
if (window.appConfig.get('DEEP_LINK')) {
await addMCPSystem(window.appConfig.get('DEEP_LINK'));

// Handle deep link if present
const deepLink = window.appConfig.get('DEEP_LINK');
if (deepLink) {
await addMCPSystem(deepLink);
}
} catch (error) {
console.error("Failed to initialize system:", error);
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 d3a81bc

Please sign in to comment.