Elffuss BraidHub

kikocis/continue public

bdbad06b302d · 4.1 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, isHubSession } 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 const TRIAL_PROXY_URL =
27 "https://proxy-server-blue-l6vsfbzhba-uw.a.run.app";
28
29export class ControlPlaneClient {
30 constructor(
31 private readonly sessionInfoPromise: Promise<
32 ControlPlaneSessionInfo | undefined
33 >,
34 private readonly ideSettingsPromise: Promise<IdeSettings>,
35 ) {}
36
37 async resolveFQSNs(
38 fqsns: FQSN[],
39 orgScopeId: string | null,
40 ): Promise<(SecretResult | undefined)[]> {
41 if (!(await this.isSignedIn())) {
42 return fqsns.map((fqsn) => ({
43 found: false,
44 fqsn,
45 secretLocation: {
46 secretName: fqsn.secretName,
47 secretType: SecretType.NotFound,
48 },
49 }));
50 }
51
52 const resp = await this.request("ide/sync-secrets", {
53 method: "POST",
54 body: JSON.stringify({ fqsns, orgScopeId }),
55 });
56 return (await resp.json()) as any;
57 }
58
59 async isSignedIn(): Promise<boolean> {
60 const sessionInfo = await this.sessionInfoPromise;
61 return !!sessionInfo;
62 }
63
64 async getAccessToken(): Promise<string | undefined> {
65 const sessionInfo = await this.sessionInfoPromise;
66 return isHubSession(sessionInfo) ? sessionInfo.accessToken : undefined;
67 }
68
69 private async request(path: string, init: RequestInit): Promise<Response> {
70 const sessionInfo = await this.sessionInfoPromise;
71 const hubSession = isHubSession(sessionInfo);
72
73 const accessToken = hubSession ? sessionInfo.accessToken : undefined;
74
75 // Bearer token not necessary for on-prem auth type
76 if (!accessToken && hubSession) {
77 throw new Error("No access token");
78 }
79
80 const env = await getControlPlaneEnv(this.ideSettingsPromise);
81 const url = new URL(path, env.CONTROL_PLANE_URL).toString();
82 const resp = await fetch(url, {
83 ...init,
84 headers: {
85 ...init.headers,
86 Authorization: `Bearer ${accessToken}`,
87 },
88 });
89
90 if (!resp.ok) {
91 throw new Error(
92 `Control plane request failed: ${resp.status} ${await resp.text()}`,
93 );
94 }
95
96 return resp;
97 }
98
99 public async listAssistants(organizationId: string | null): Promise<
100 {
101 configResult: ConfigResult<AssistantUnrolled>;
102 ownerSlug: string;
103 packageSlug: string;
104 iconUrl: string;
105 rawYaml: string;
106 }[]
107 > {
108 if (!(await this.isSignedIn())) {
109 return [];
110 }
111
112 try {
113 const url = organizationId
114 ? `ide/list-assistants?organizationId=${organizationId}`
115 : "ide/list-assistants";
116
117 const resp = await this.request(url, {
118 method: "GET",
119 });
120 return (await resp.json()) as any;
121 } catch (e) {
122 return [];
123 }
124 }
125
126 public async listOrganizations(): Promise<Array<OrganizationDescription>> {
127 if (!(await this.isSignedIn())) {
128 return [];
129 }
130
131 try {
132 const resp = await this.request("ide/list-organizations", {
133 method: "GET",
134 });
135 const { organizations } = (await resp.json()) as any;
136 return organizations;
137 } catch (e) {
138 return [];
139 }
140 }
141
142 public async listAssistantFullSlugs(
143 organizationId: string | null,
144 ): Promise<FullSlug[] | null> {
145 if (!(await this.isSignedIn())) {
146 return null;
147 }
148
149 const url = organizationId
150 ? `ide/list-assistant-full-slugs?organizationId=${organizationId}`
151 : "ide/list-assistant-full-slugs";
152
153 try {
154 const resp = await this.request(url, {
155 method: "GET",
156 });
157 const { fullSlugs } = (await resp.json()) as any;
158 return fullSlugs;
159 } catch (e) {
160 return null;
161 }
162 }
163}