Elffuss BraidHub

kikocis/continue public

main · 2.9 KiBRawPermalink
1import {
2 AssistantUnrolled,
3 ConfigResult,
4 FQSN,
5 FullSlug,
6 SecretResult,
7} from "@continuedev/config-yaml";
8import { IContinueHubClient } from "./IContinueHubClient.js";
9
10interface ContinueHubClientOptions {
11 apiKey?: string;
12 apiBase?: string;
13 fetchOptions?: RequestInit;
14}
15
16export class ContinueHubClient implements IContinueHubClient {
17 private readonly apiKey?: string;
18 private readonly apiBase: string;
19 private readonly fetchOptions?: RequestInit;
20
21 constructor(options: ContinueHubClientOptions) {
22 this.apiKey = options.apiKey;
23 this.apiBase = options.apiBase ?? "https://api.continue.dev";
24 this.fetchOptions = options.fetchOptions;
25 }
26
27 private async request(path: string, init: RequestInit): Promise<Response> {
28 const url = new URL(path, this.apiBase).toString();
29
30 const finalInit: RequestInit = {
31 ...this.fetchOptions,
32 ...init,
33 headers: {
34 ...this.fetchOptions?.headers,
35 ...init.headers,
36 },
37 };
38
39 if (this.apiKey) {
40 finalInit.headers = {
41 ...finalInit.headers,
42 Authorization: `Bearer ${this.apiKey}`,
43 };
44 }
45
46 const resp = await fetch(url, finalInit);
47
48 if (!resp.ok) {
49 throw new Error(
50 `Control plane request failed: ${resp.status} ${await resp.text()}`,
51 );
52 }
53
54 return resp;
55 }
56
57 async resolveFQSNs(
58 fqsns: FQSN[],
59 orgScopeId: string | null,
60 ): Promise<(SecretResult | undefined)[]> {
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 async listAssistants(options: {
68 organizationId: string | null;
69 alwaysUseProxy?: boolean;
70 }): Promise<
71 {
72 configResult: ConfigResult<AssistantUnrolled>;
73 ownerSlug: string;
74 packageSlug: string;
75 iconUrl: string;
76 rawYaml: string;
77 }[]
78 > {
79 const organizationId = options.organizationId;
80 const alwaysUseProxy = options.alwaysUseProxy ?? false;
81
82 try {
83 const urlObj = new URL("ide/list-assistants", this.apiBase);
84 if (organizationId) {
85 urlObj.searchParams.set("organizationId", organizationId);
86 }
87 if (alwaysUseProxy) {
88 urlObj.searchParams.set("alwaysUseProxy", alwaysUseProxy.toString());
89 }
90 const url = urlObj.toString();
91
92 const resp = await this.request(url, {
93 method: "GET",
94 });
95 return (await resp.json()) as any;
96 } catch (e) {
97 return [];
98 }
99 }
100
101 async listAssistantFullSlugs(
102 organizationId: string | null,
103 ): Promise<FullSlug[] | null> {
104 const url = organizationId
105 ? `ide/list-assistant-full-slugs?organizationId=${organizationId}`
106 : "ide/list-assistant-full-slugs";
107
108 try {
109 const resp = await this.request(url, {
110 method: "GET",
111 });
112 const { fullSlugs } = (await resp.json()) as any;
113 return fullSlugs;
114 } catch (e) {
115 return null;
116 }
117 }
118}