| 1 | import * as fs from "fs"; |
| 2 | import * as path from "path"; |
| 3 | |
| 4 | import chalk from "chalk"; |
| 5 | |
| 6 | import type { AuthConfig, AuthenticatedConfig } from "../auth/workos.js"; |
| 7 | import { saveAuthConfig } from "../auth/workos.js"; |
| 8 | import { getApiClient } from "../config.js"; |
| 9 | import { env } from "../env.js"; |
| 10 | |
| 11 | /** |
| 12 | * Creates an updated AuthenticatedConfig with a new organization ID and optional config URI |
| 13 | */ |
| 14 | export function createUpdatedAuthConfig( |
| 15 | config: AuthenticatedConfig, |
| 16 | organizationId: string | null | undefined, |
| 17 | configUri?: string | null, |
| 18 | ): AuthenticatedConfig { |
| 19 | return { |
| 20 | userId: config.userId, |
| 21 | userEmail: config.userEmail, |
| 22 | accessToken: config.accessToken, |
| 23 | refreshToken: config.refreshToken, |
| 24 | expiresAt: config.expiresAt, |
| 25 | organizationId, |
| 26 | configUri: configUri ?? config.configUri, |
| 27 | modelName: config.modelName, |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Check if an organization has available assistants |
| 33 | */ |
| 34 | async function hasOrgAssistants( |
| 35 | apiClient: ReturnType<typeof getApiClient>, |
| 36 | organizationId: string, |
| 37 | ): Promise<boolean> { |
| 38 | try { |
| 39 | const assistants = await apiClient.listAssistants({ organizationId }); |
| 40 | return assistants.length > 0; |
| 41 | } catch { |
| 42 | // If we can't check, assume no assistants |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the first available assistant from an organization |
| 49 | */ |
| 50 | async function getFirstAssistant( |
| 51 | apiClient: ReturnType<typeof getApiClient>, |
| 52 | organizationId: string, |
| 53 | ): Promise<string | null> { |
| 54 | try { |
| 55 | const assistants = await apiClient.listAssistants({ organizationId }); |
| 56 | if (assistants.length > 0) { |
| 57 | const first = assistants[0]; |
| 58 | return `${first.ownerSlug}/${first.packageSlug}`; |
| 59 | } |
| 60 | return null; |
| 61 | } catch { |
| 62 | return null; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Check if personal local config.yaml exists |
| 68 | */ |
| 69 | function hasPersonalLocalConfig(): boolean { |
| 70 | const defaultConfigPath = path.join(env.continueHome, "config.yaml"); |
| 71 | return fs.existsSync(defaultConfigPath); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Automatically select organization and configuration for authenticated config |
| 76 | * Priority: 1) Org with assistants (+ first assistant), 2) Personal with local config.yaml, 3) Personal fallback |
| 77 | */ |
| 78 | export async function autoSelectOrganizationAndConfig( |
| 79 | authenticatedConfig: AuthenticatedConfig, |
| 80 | ): Promise<AuthConfig> { |
| 81 | const apiClient = getApiClient(authenticatedConfig.accessToken); |
| 82 | |
| 83 | try { |
| 84 | const resp = await apiClient.listOrganizations(); |
| 85 | const organizations = resp.organizations; |
| 86 | |
| 87 | // Priority 1: Find any organization that has assistants - use first assistant |
| 88 | for (const org of organizations) { |
| 89 | const hasAssistants = await hasOrgAssistants(apiClient, org.id); |
| 90 | if (hasAssistants) { |
| 91 | // Get the first assistant from this org |
| 92 | const firstAssistantSlug = await getFirstAssistant(apiClient, org.id); |
| 93 | const configUri = firstAssistantSlug |
| 94 | ? `slug://${firstAssistantSlug}` |
| 95 | : undefined; |
| 96 | |
| 97 | const updatedConfig = createUpdatedAuthConfig( |
| 98 | authenticatedConfig, |
| 99 | org.id, |
| 100 | configUri, |
| 101 | ); |
| 102 | saveAuthConfig(updatedConfig); |
| 103 | return updatedConfig; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Priority 2: Check if personal organization has assistants |
| 108 | try { |
| 109 | const personalAssistants = await apiClient.listAssistants({ |
| 110 | organizationId: undefined, |
| 111 | }); |
| 112 | if (personalAssistants.length > 0) { |
| 113 | const firstAssistant = personalAssistants[0]; |
| 114 | const firstAssistantSlug = `${firstAssistant.ownerSlug}/${firstAssistant.packageSlug}`; |
| 115 | const configUri = `slug://${firstAssistantSlug}`; |
| 116 | |
| 117 | const updatedConfig = createUpdatedAuthConfig( |
| 118 | authenticatedConfig, |
| 119 | null, |
| 120 | configUri, |
| 121 | ); |
| 122 | saveAuthConfig(updatedConfig); |
| 123 | console.log(chalk.green("✓ Automatically selected personal assistant")); |
| 124 | return updatedConfig; |
| 125 | } |
| 126 | } catch { |
| 127 | // If personal assistants can't be fetched, continue to next priority |
| 128 | } |
| 129 | |
| 130 | // Priority 3: If no assistants anywhere, use local config.yaml |
| 131 | if (hasPersonalLocalConfig()) { |
| 132 | const updatedConfig = createUpdatedAuthConfig( |
| 133 | authenticatedConfig, |
| 134 | null, |
| 135 | `file://${path.join(env.continueHome, "config.yaml")}`, |
| 136 | ); |
| 137 | saveAuthConfig(updatedConfig); |
| 138 | console.log(chalk.green("✓ Using local config.yaml")); |
| 139 | return updatedConfig; |
| 140 | } |
| 141 | |
| 142 | // no org assistants or config.yaml |
| 143 | const updatedConfig = createUpdatedAuthConfig(authenticatedConfig, null); |
| 144 | saveAuthConfig(updatedConfig); |
| 145 | return updatedConfig; |
| 146 | } catch (error: any) { |
| 147 | console.error( |
| 148 | chalk.red("Error fetching organizations:"), |
| 149 | error.response?.data?.message || error.message || error, |
| 150 | ); |
| 151 | const updatedConfig = createUpdatedAuthConfig(authenticatedConfig, null); |
| 152 | saveAuthConfig(updatedConfig); |
| 153 | return updatedConfig; |
| 154 | } |
| 155 | } |