| 1 | import { ConfigJson } from "@continuedev/config-types"; |
| 2 | import { |
| 3 | AssistantUnrolled, |
| 4 | ConfigResult, |
| 5 | FQSN, |
| 6 | FullSlug, |
| 7 | SecretResult, |
| 8 | SecretType, |
| 9 | } from "@continuedev/config-yaml"; |
| 10 | import fetch, { RequestInit, Response } from "node-fetch"; |
| 11 | |
| 12 | import { OrganizationDescription } from "../config/ProfileLifecycleManager.js"; |
| 13 | import { IdeInfo, IdeSettings, ModelDescription } from "../index.js"; |
| 14 | |
| 15 | import { ControlPlaneSessionInfo, isOnPremSession } from "./AuthTypes.js"; |
| 16 | import { getControlPlaneEnv } from "./env.js"; |
| 17 | |
| 18 | export interface ControlPlaneWorkspace { |
| 19 | id: string; |
| 20 | name: string; |
| 21 | settings: ConfigJson; |
| 22 | } |
| 23 | |
| 24 | export interface ControlPlaneModelDescription extends ModelDescription {} |
| 25 | |
| 26 | export interface FreeTrialStatus { |
| 27 | optedInToFreeTrial: boolean; |
| 28 | chatCount?: number; |
| 29 | autocompleteCount?: number; |
| 30 | chatLimit: number; |
| 31 | autocompleteLimit: number; |
| 32 | } |
| 33 | |
| 34 | export const TRIAL_PROXY_URL = |
| 35 | "https://proxy-server-blue-l6vsfbzhba-uw.a.run.app"; |
| 36 | |
| 37 | export class ControlPlaneClient { |
| 38 | constructor( |
| 39 | private readonly sessionInfoPromise: Promise< |
| 40 | ControlPlaneSessionInfo | undefined |
| 41 | >, |
| 42 | private readonly ideSettingsPromise: Promise<IdeSettings>, |
| 43 | private readonly ideInfoPromise: Promise<IdeInfo>, |
| 44 | ) {} |
| 45 | |
| 46 | async resolveFQSNs( |
| 47 | fqsns: FQSN[], |
| 48 | orgScopeId: string | null, |
| 49 | ): Promise<(SecretResult | undefined)[]> { |
| 50 | if (!(await this.isSignedIn())) { |
| 51 | return fqsns.map((fqsn) => ({ |
| 52 | found: false, |
| 53 | fqsn, |
| 54 | secretLocation: { |
| 55 | secretName: fqsn.secretName, |
| 56 | secretType: SecretType.NotFound, |
| 57 | }, |
| 58 | })); |
| 59 | } |
| 60 | |
| 61 | const resp = await this.request("ide/sync-secrets", { |
| 62 | method: "POST", |
| 63 | body: JSON.stringify({ fqsns, orgScopeId }), |
| 64 | }); |
| 65 | return (await resp.json()) as any; |
| 66 | } |
| 67 | |
| 68 | async isSignedIn(): Promise<boolean> { |
| 69 | const sessionInfo = await this.sessionInfoPromise; |
| 70 | return !!sessionInfo; |
| 71 | } |
| 72 | |
| 73 | async getAccessToken(): Promise<string | undefined> { |
| 74 | const sessionInfo = await this.sessionInfoPromise; |
| 75 | return isOnPremSession(sessionInfo) ? undefined : sessionInfo?.accessToken; |
| 76 | } |
| 77 | |
| 78 | private async request(path: string, init: RequestInit): Promise<Response> { |
| 79 | const sessionInfo = await this.sessionInfoPromise; |
| 80 | const onPremSession = isOnPremSession(sessionInfo); |
| 81 | const accessToken = await this.getAccessToken(); |
| 82 | |
| 83 | // Bearer token not necessary for on-prem auth type |
| 84 | if (!accessToken && !onPremSession) { |
| 85 | throw new Error("No access token"); |
| 86 | } |
| 87 | |
| 88 | const env = await getControlPlaneEnv(this.ideSettingsPromise); |
| 89 | const url = new URL(path, env.CONTROL_PLANE_URL).toString(); |
| 90 | const ideInfo = await this.ideInfoPromise; |
| 91 | |
| 92 | const resp = await fetch(url, { |
| 93 | ...init, |
| 94 | headers: { |
| 95 | ...init.headers, |
| 96 | Authorization: `Bearer ${accessToken}`, |
| 97 | ...{ |
| 98 | "x-extension-version": ideInfo.extensionVersion, |
| 99 | "x-is-prerelease": String(ideInfo.isPrerelease), |
| 100 | }, |
| 101 | }, |
| 102 | }); |
| 103 | |
| 104 | if (!resp.ok) { |
| 105 | throw new Error( |
| 106 | `Control plane request failed: ${resp.status} ${await resp.text()}`, |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return resp; |
| 111 | } |
| 112 | |
| 113 | public async listAssistants(organizationId: string | null): Promise< |
| 114 | { |
| 115 | configResult: ConfigResult<AssistantUnrolled>; |
| 116 | ownerSlug: string; |
| 117 | packageSlug: string; |
| 118 | iconUrl: string; |
| 119 | rawYaml: string; |
| 120 | }[] |
| 121 | > { |
| 122 | if (!(await this.isSignedIn())) { |
| 123 | return []; |
| 124 | } |
| 125 | |
| 126 | try { |
| 127 | const url = organizationId |
| 128 | ? `ide/list-assistants?organizationId=${organizationId}` |
| 129 | : "ide/list-assistants"; |
| 130 | |
| 131 | const resp = await this.request(url, { |
| 132 | method: "GET", |
| 133 | }); |
| 134 | return (await resp.json()) as any; |
| 135 | } catch (e) { |
| 136 | return []; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public async listOrganizations(): Promise<Array<OrganizationDescription>> { |
| 141 | if (!(await this.isSignedIn())) { |
| 142 | return []; |
| 143 | } |
| 144 | |
| 145 | try { |
| 146 | const resp = await this.request("ide/list-organizations", { |
| 147 | method: "GET", |
| 148 | }); |
| 149 | const { organizations } = (await resp.json()) as any; |
| 150 | return organizations; |
| 151 | } catch (e) { |
| 152 | return []; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public async listAssistantFullSlugs( |
| 157 | organizationId: string | null, |
| 158 | ): Promise<FullSlug[] | null> { |
| 159 | if (!(await this.isSignedIn())) { |
| 160 | return null; |
| 161 | } |
| 162 | |
| 163 | const url = organizationId |
| 164 | ? `ide/list-assistant-full-slugs?organizationId=${organizationId}` |
| 165 | : "ide/list-assistant-full-slugs"; |
| 166 | |
| 167 | try { |
| 168 | const resp = await this.request(url, { |
| 169 | method: "GET", |
| 170 | }); |
| 171 | const { fullSlugs } = (await resp.json()) as any; |
| 172 | return fullSlugs; |
| 173 | } catch (e) { |
| 174 | return null; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | public async getFreeTrialStatus(): Promise<FreeTrialStatus | null> { |
| 179 | if (!(await this.isSignedIn())) { |
| 180 | return null; |
| 181 | } |
| 182 | |
| 183 | try { |
| 184 | const resp = await this.request("ide/free-trial-status", { |
| 185 | method: "GET", |
| 186 | }); |
| 187 | return (await resp.json()) as FreeTrialStatus; |
| 188 | } catch (e) { |
| 189 | return null; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * JetBrains does not support deep links, so we only check for `vsCodeUriScheme` |
| 195 | * @param vsCodeUriScheme |
| 196 | * @returns |
| 197 | */ |
| 198 | public async getModelsAddOnCheckoutUrl( |
| 199 | vsCodeUriScheme?: string, |
| 200 | ): Promise<{ url: string } | null> { |
| 201 | if (!(await this.isSignedIn())) { |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | try { |
| 206 | const params = new URLSearchParams({ |
| 207 | // LocalProfileLoader ID |
| 208 | profile_id: "local", |
| 209 | }); |
| 210 | |
| 211 | if (vsCodeUriScheme) { |
| 212 | params.set("vscode_uri_scheme", vsCodeUriScheme); |
| 213 | } |
| 214 | |
| 215 | const resp = await this.request( |
| 216 | `ide/get-models-add-on-checkout-url?${params.toString()}`, |
| 217 | { |
| 218 | method: "GET", |
| 219 | }, |
| 220 | ); |
| 221 | return (await resp.json()) as { url: string }; |
| 222 | } catch (e) { |
| 223 | return null; |
| 224 | } |
| 225 | } |
| 226 | } |