| 1 | import { exec } from "child_process"; |
| 2 | import * as path from "path"; |
| 3 | |
| 4 | import { env } from "../../env.js"; |
| 5 | import { services } from "../../services/index.js"; |
| 6 | import { useNavigation } from "../context/NavigationContext.js"; |
| 7 | |
| 8 | interface ConfigOption { |
| 9 | id: string; |
| 10 | name: string; |
| 11 | type: "local" | "assistant" | "create"; |
| 12 | slug?: string; |
| 13 | organizationId?: string | null; |
| 14 | } |
| 15 | |
| 16 | interface UseConfigSelectorProps { |
| 17 | configPath?: string; |
| 18 | onMessage: (message: { |
| 19 | role: string; |
| 20 | content: string; |
| 21 | messageType: "system"; |
| 22 | }) => void; |
| 23 | handleClear: () => void; |
| 24 | } |
| 25 | |
| 26 | const CONFIG_PATH = path.join(env.continueHome, "config.yaml"); |
| 27 | |
| 28 | export function useConfigSelector({ |
| 29 | onMessage, |
| 30 | handleClear, |
| 31 | }: UseConfigSelectorProps) { |
| 32 | const { closeCurrentScreen } = useNavigation(); |
| 33 | |
| 34 | const handleConfigSelect = async (config: ConfigOption) => { |
| 35 | closeCurrentScreen(); |
| 36 | |
| 37 | if (config.type === "create") { |
| 38 | // Open the web browser to create new assistant |
| 39 | const url = new URL("https://hub.continue.dev/new"); |
| 40 | url.searchParams.set("type", "assistant"); |
| 41 | |
| 42 | try { |
| 43 | let command: string; |
| 44 | if (process.platform === "darwin") { |
| 45 | command = `open "${url}"`; |
| 46 | } else if (process.platform === "win32") { |
| 47 | command = `start "${url}"`; |
| 48 | } else { |
| 49 | command = `xdg-open "${url}"`; |
| 50 | } |
| 51 | |
| 52 | exec(command, (error) => { |
| 53 | if (error) { |
| 54 | console.error("Failed to open browser:", error); |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | onMessage({ |
| 59 | role: "system", |
| 60 | content: `Opening ${url} in your browser to create a new assistant...`, |
| 61 | messageType: "system" as const, |
| 62 | }); |
| 63 | } catch { |
| 64 | onMessage({ |
| 65 | role: "system", |
| 66 | content: `Please visit ${url} to create a new assistant`, |
| 67 | messageType: "system" as const, |
| 68 | }); |
| 69 | } |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | try { |
| 74 | // Show loading message |
| 75 | onMessage({ |
| 76 | role: "system", |
| 77 | content: `Switching to configuration: ${config.name}...`, |
| 78 | messageType: "system" as const, |
| 79 | }); |
| 80 | |
| 81 | // First, check if we need to switch organizations |
| 82 | const currentAuthState = services.auth.getState(); |
| 83 | const currentOrgId = currentAuthState.authConfig |
| 84 | ? (currentAuthState.authConfig.organizationId ?? null) |
| 85 | : null; |
| 86 | |
| 87 | let targetConfigPath: string | undefined; |
| 88 | |
| 89 | if (config.type === "local") { |
| 90 | targetConfigPath = CONFIG_PATH; |
| 91 | } else if (config.type === "assistant" && config.slug) { |
| 92 | targetConfigPath = config.slug; |
| 93 | } |
| 94 | |
| 95 | // If we need to switch organizations, we'll handle both org + config switching |
| 96 | // using a different approach to avoid duplicate reloads |
| 97 | if (config.organizationId === currentOrgId) { |
| 98 | // Only config path is changing, no organization switch needed |
| 99 | await services.config.updateConfigPath(targetConfigPath); |
| 100 | } else { |
| 101 | // Switch organization first |
| 102 | await services.auth.switchOrganization(config.organizationId ?? null); |
| 103 | |
| 104 | // Update config path THEN reload services once |
| 105 | // This avoids the double reload issue |
| 106 | await services.config.updateConfigPath(targetConfigPath); |
| 107 | } |
| 108 | |
| 109 | handleClear(); |
| 110 | |
| 111 | // Show success message |
| 112 | onMessage({ |
| 113 | role: "system", |
| 114 | content: `Successfully switched to configuration: ${config.name}`, |
| 115 | messageType: "system" as const, |
| 116 | }); |
| 117 | } catch (error: any) { |
| 118 | // Show error message |
| 119 | onMessage({ |
| 120 | role: "system", |
| 121 | content: `Failed to switch configuration: ${error.message}`, |
| 122 | messageType: "system" as const, |
| 123 | }); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | return { |
| 128 | handleConfigSelect, |
| 129 | }; |
| 130 | } |