Elffuss BraidHub

kikocis/continue public

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