| 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 { IdeSettings, ModelDescription } from "../index.js"; |
| 14 | |
| 15 | import { getControlPlaneEnv } from "./env.js"; |
| 16 | |
| 17 | export interface ControlPlaneSessionInfo { |
| 18 | accessToken: string; |
| 19 | account: { |
| 20 | label: string; |
| 21 | id: string; |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | export interface ControlPlaneWorkspace { |
| 26 | id: string; |
| 27 | name: string; |
| 28 | settings: ConfigJson; |
| 29 | } |
| 30 | |
| 31 | export interface ControlPlaneModelDescription extends ModelDescription {} |
| 32 | |
| 33 | export const TRIAL_PROXY_URL = |
| 34 | "https://proxy-server-blue-l6vsfbzhba-uw.a.run.app"; |
| 35 | |
| 36 | export class ControlPlaneClient { |
| 37 | constructor( |
| 38 | private readonly sessionInfoPromise: Promise< |
| 39 | ControlPlaneSessionInfo | undefined |
| 40 | >, |
| 41 | private readonly ideSettingsPromise: Promise<IdeSettings>, |
| 42 | ) {} |
| 43 | |
| 44 | async resolveFQSNs( |
| 45 | fqsns: FQSN[], |
| 46 | orgScopeId: string | null, |
| 47 | ): Promise<(SecretResult | undefined)[]> { |
| 48 | const userId = await this.userId; |
| 49 | if (!userId) { |
| 50 | return fqsns.map((fqsn) => ({ |
| 51 | found: false, |
| 52 | fqsn, |
| 53 | secretLocation: { |
| 54 | secretName: fqsn.secretName, |
| 55 | secretType: SecretType.NotFound, |
| 56 | }, |
| 57 | })); |
| 58 | } |
| 59 | |
| 60 | const resp = await this.request("ide/sync-secrets", { |
| 61 | method: "POST", |
| 62 | body: JSON.stringify({ fqsns, orgScopeId }), |
| 63 | }); |
| 64 | return (await resp.json()) as any; |
| 65 | } |
| 66 | |
| 67 | get userId(): Promise<string | undefined> { |
| 68 | return this.sessionInfoPromise.then( |
| 69 | (sessionInfo) => sessionInfo?.account.id, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | async getAccessToken(): Promise<string | undefined> { |
| 74 | return (await this.sessionInfoPromise)?.accessToken; |
| 75 | } |
| 76 | |
| 77 | private async request(path: string, init: RequestInit): Promise<Response> { |
| 78 | const accessToken = await this.getAccessToken(); |
| 79 | if (!accessToken) { |
| 80 | throw new Error("No access token"); |
| 81 | } |
| 82 | |
| 83 | const env = await getControlPlaneEnv(this.ideSettingsPromise); |
| 84 | const url = new URL(path, env.CONTROL_PLANE_URL).toString(); |
| 85 | const resp = await fetch(url, { |
| 86 | ...init, |
| 87 | headers: { |
| 88 | ...init.headers, |
| 89 | Authorization: `Bearer ${accessToken}`, |
| 90 | }, |
| 91 | }); |
| 92 | |
| 93 | if (!resp.ok) { |
| 94 | throw new Error( |
| 95 | `Control plane request failed: ${resp.status} ${await resp.text()}`, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | return resp; |
| 100 | } |
| 101 | |
| 102 | public async listWorkspaces(): Promise<ControlPlaneWorkspace[]> { |
| 103 | const userId = await this.userId; |
| 104 | if (!userId) { |
| 105 | return []; |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | const resp = await this.request("workspaces", { |
| 110 | method: "GET", |
| 111 | }); |
| 112 | return (await resp.json()) as any; |
| 113 | } catch (e) { |
| 114 | return []; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public async listAssistants(organizationId: string | null): Promise< |
| 119 | { |
| 120 | configResult: ConfigResult<AssistantUnrolled>; |
| 121 | ownerSlug: string; |
| 122 | packageSlug: string; |
| 123 | iconUrl: string; |
| 124 | rawYaml: string; |
| 125 | }[] |
| 126 | > { |
| 127 | const userId = await this.userId; |
| 128 | if (!userId) { |
| 129 | return []; |
| 130 | } |
| 131 | |
| 132 | try { |
| 133 | const url = organizationId |
| 134 | ? `ide/list-assistants?organizationId=${organizationId}` |
| 135 | : "ide/list-assistants"; |
| 136 | |
| 137 | const resp = await this.request(url, { |
| 138 | method: "GET", |
| 139 | }); |
| 140 | return (await resp.json()) as any; |
| 141 | } catch (e) { |
| 142 | return []; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public async listOrganizations(): Promise<Array<OrganizationDescription>> { |
| 147 | const userId = await this.userId; |
| 148 | |
| 149 | if (!userId) { |
| 150 | return []; |
| 151 | } |
| 152 | |
| 153 | try { |
| 154 | const resp = await this.request("ide/list-organizations", { |
| 155 | method: "GET", |
| 156 | }); |
| 157 | const { organizations } = (await resp.json()) as any; |
| 158 | return organizations; |
| 159 | } catch (e) { |
| 160 | return []; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public async listAssistantFullSlugs( |
| 165 | organizationId: string | null, |
| 166 | ): Promise<FullSlug[] | null> { |
| 167 | const userId = await this.userId; |
| 168 | if (!userId) { |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | const url = organizationId |
| 173 | ? `ide/list-assistant-full-slugs?organizationId=${organizationId}` |
| 174 | : "ide/list-assistant-full-slugs"; |
| 175 | |
| 176 | try { |
| 177 | const resp = await this.request(url, { |
| 178 | method: "GET", |
| 179 | }); |
| 180 | const { fullSlugs } = (await resp.json()) as any; |
| 181 | return fullSlugs; |
| 182 | } catch (e) { |
| 183 | return null; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | async getSettingsForWorkspace(workspaceId: string): Promise<ConfigJson> { |
| 188 | const userId = await this.userId; |
| 189 | if (!userId) { |
| 190 | throw new Error("No user id"); |
| 191 | } |
| 192 | |
| 193 | const resp = await this.request(`workspaces/${workspaceId}`, { |
| 194 | method: "GET", |
| 195 | }); |
| 196 | return ((await resp.json()) as any).settings; |
| 197 | } |
| 198 | } |