Elffuss BraidHub

kikocis/continue public

627d7d0d1f85 · 3.0 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(
84 organizationId ? "ide/list-assistants" : "ide/list-assistants",
85 this.apiBase,
86 );
87 if (organizationId) {
88 urlObj.searchParams.set("organizationId", organizationId);
89 }
90 if (alwaysUseProxy) {
91 urlObj.searchParams.set("alwaysUseProxy", alwaysUseProxy.toString());
92 }
93 const url = urlObj.toString();
94
95 const resp = await this.request(url, {
96 method: "GET",
97 });
98 return (await resp.json()) as any;
99 } catch (e) {
100 return [];
101 }
102 }
103
104 async listAssistantFullSlugs(
105 organizationId: string | null,
106 ): Promise<FullSlug[] | null> {
107 const url = organizationId
108 ? `ide/list-assistant-full-slugs?organizationId=${organizationId}`
109 : "ide/list-assistant-full-slugs";
110
111 try {
112 const resp = await this.request(url, {
113 method: "GET",
114 });
115 const { fullSlugs } = (await resp.json()) as any;
116 return fullSlugs;
117 } catch (e) {
118 return null;
119 }
120 }
121}