| 1 | import type { |
| 2 | ArtifactType, |
| 3 | EmbeddingsCacheResponse, |
| 4 | IContinueServerClient, |
| 5 | } from "../interface.js"; |
| 6 | |
| 7 | export class ContinueServerClient implements IContinueServerClient { |
| 8 | url: URL | undefined; |
| 9 | |
| 10 | constructor( |
| 11 | serverUrl: string | undefined, |
| 12 | private readonly userToken: string | undefined, |
| 13 | ) { |
| 14 | try { |
| 15 | this.url = |
| 16 | typeof serverUrl !== "string" || serverUrl === "" |
| 17 | ? undefined |
| 18 | : new URL(serverUrl.endsWith("/") ? serverUrl : `${serverUrl}/`); |
| 19 | } catch (e) { |
| 20 | console.warn("Invalid Continue server url", e); |
| 21 | this.url = undefined; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | getUserToken(): string | undefined { |
| 26 | return this.userToken; |
| 27 | } |
| 28 | |
| 29 | get connected(): boolean { |
| 30 | return this.url !== undefined && this.userToken !== undefined; |
| 31 | } |
| 32 | |
| 33 | public async getConfig(): Promise<{ configJson: string }> { |
| 34 | const userToken = await this.userToken; |
| 35 | const response = await fetch(new URL("sync", this.url).href, { |
| 36 | method: "GET", |
| 37 | headers: { |
| 38 | Authorization: `Bearer ${userToken}`, |
| 39 | }, |
| 40 | }); |
| 41 | if (!response.ok) { |
| 42 | throw new Error( |
| 43 | `Failed to sync remote config (HTTP ${response.status}): ${response.statusText}`, |
| 44 | ); |
| 45 | } |
| 46 | const data = await response.json(); |
| 47 | return data; |
| 48 | } |
| 49 | |
| 50 | public async getFromIndexCache<T extends ArtifactType>( |
| 51 | keys: string[], |
| 52 | artifactId: T, |
| 53 | repoName: string | undefined, |
| 54 | ): Promise<EmbeddingsCacheResponse<T>> { |
| 55 | if (repoName === undefined) { |
| 56 | console.warn( |
| 57 | "No repo name provided to getFromIndexCache, this may cause no results to be returned.", |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | if (keys.length === 0) { |
| 62 | return { |
| 63 | files: {}, |
| 64 | }; |
| 65 | } |
| 66 | const url = new URL("indexing/cache", this.url); |
| 67 | |
| 68 | try { |
| 69 | const response = await fetch(url, { |
| 70 | method: "POST", |
| 71 | headers: { |
| 72 | Authorization: `Bearer ${await this.userToken}`, |
| 73 | }, |
| 74 | body: JSON.stringify({ |
| 75 | keys, |
| 76 | artifactId, |
| 77 | repo: repoName ?? "NONE", |
| 78 | }), |
| 79 | }); |
| 80 | |
| 81 | if (!response.ok) { |
| 82 | const text = await response.text(); |
| 83 | console.warn( |
| 84 | `Failed to retrieve from remote cache (HTTP ${response.status}): ${text}`, |
| 85 | ); |
| 86 | return { |
| 87 | files: {}, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | const data = await response.json(); |
| 92 | return data; |
| 93 | } catch (e) { |
| 94 | console.warn("Failed to retrieve from remote cache", e); |
| 95 | return { |
| 96 | files: {}, |
| 97 | }; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public async sendFeedback(feedback: string, data: string): Promise<void> { |
| 102 | if (!this.url) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | const url = new URL("feedback", this.url); |
| 107 | |
| 108 | const response = await fetch(url, { |
| 109 | method: "POST", |
| 110 | headers: { |
| 111 | Authorization: `Bearer ${await this.userToken}`, |
| 112 | }, |
| 113 | body: JSON.stringify({ |
| 114 | feedback, |
| 115 | data, |
| 116 | }), |
| 117 | }); |
| 118 | } |
| 119 | } |