Elffuss BraidHub

kikocis/continue public

main · 3.6 KiBRawPermalink
1import * as fs from "fs";
2
3import { ContinueServerClient } from "core/continueServer/stubs/client";
4import { EXTENSION_NAME } from "core/control-plane/env";
5import { getConfigJsonPathForRemote } from "core/util/paths";
6import { canParseUrl } from "core/util/url";
7import * as vscode from "vscode";
8
9import { CONTINUE_WORKSPACE_KEY } from "../util/workspaceConfig";
10
11export class RemoteConfigSync {
12 private userToken: string | null;
13 private remoteConfigServerUrl: string | null;
14 private remoteConfigSyncPeriod: number;
15
16 private syncInterval: NodeJS.Timer | undefined = undefined;
17
18 constructor(
19 private triggerReloadConfig: () => void,
20 userToken: string | null,
21 ) {
22 let {
23 userToken: settingsUserToken,
24 remoteConfigServerUrl,
25 remoteConfigSyncPeriod,
26 } = this.loadVsCodeSettings();
27 this.userToken = settingsUserToken || userToken;
28 this.remoteConfigServerUrl = remoteConfigServerUrl;
29 this.remoteConfigSyncPeriod = remoteConfigSyncPeriod;
30
31 // Listen for changes to VS Code settings, then trigger a refresh
32 vscode.workspace.onDidChangeConfiguration(async (event) => {
33 if (event.affectsConfiguration(CONTINUE_WORKSPACE_KEY)) {
34 const { userToken, remoteConfigServerUrl, remoteConfigSyncPeriod } =
35 await this.loadVsCodeSettings();
36 if (
37 userToken !== this.userToken ||
38 remoteConfigServerUrl !== this.remoteConfigServerUrl ||
39 remoteConfigSyncPeriod !== this.remoteConfigSyncPeriod
40 ) {
41 this.userToken = userToken;
42 this.remoteConfigServerUrl = remoteConfigServerUrl;
43 this.remoteConfigSyncPeriod = remoteConfigSyncPeriod;
44
45 this.setInterval();
46 }
47 }
48 });
49 }
50
51 private loadVsCodeSettings() {
52 const settings = vscode.workspace.getConfiguration(EXTENSION_NAME);
53 const userToken = settings.get<string | null>("userToken", null);
54 const remoteConfigServerUrl = settings.get<string | null>(
55 "remoteConfigServerUrl",
56 null,
57 );
58 const remoteConfigSyncPeriod = settings.get<number>(
59 "remoteConfigSyncPeriod",
60 60,
61 );
62
63 return {
64 userToken,
65 remoteConfigServerUrl,
66 remoteConfigSyncPeriod,
67 };
68 }
69
70 async setup() {
71 if (
72 this.userToken === null ||
73 this.remoteConfigServerUrl === null ||
74 this.remoteConfigServerUrl.trim() === ""
75 ) {
76 return;
77 }
78 if (!canParseUrl(this.remoteConfigServerUrl)) {
79 vscode.window.showErrorMessage(
80 "The value set for 'remoteConfigServerUrl' is not valid: ",
81 this.remoteConfigServerUrl,
82 );
83 return;
84 }
85
86 // Sync once
87 await this.sync(this.userToken, this.remoteConfigServerUrl);
88
89 // Set timer to sync at user-defined interval
90 this.setInterval();
91 }
92
93 private setInterval() {
94 if (this.syncInterval !== undefined) {
95 // @ts-ignore
96 clearInterval(this.syncInterval);
97 }
98 this.syncInterval = setInterval(
99 () => {
100 if (!this.userToken || !this.remoteConfigServerUrl) {
101 return;
102 }
103 this.sync(this.userToken, this.remoteConfigServerUrl);
104 },
105 this.remoteConfigSyncPeriod * 1000 * 60,
106 );
107 }
108
109 async sync(userToken: string, remoteConfigServerUrl: string) {
110 try {
111 const client = new ContinueServerClient(
112 remoteConfigServerUrl.toString(),
113 userToken,
114 );
115 const { configJson } = await client.getConfig();
116
117 fs.writeFileSync(
118 getConfigJsonPathForRemote(remoteConfigServerUrl),
119 configJson,
120 );
121 this.triggerReloadConfig();
122 } catch (e) {
123 vscode.window.showWarningMessage(`Failed to sync remote config: ${e}`);
124 }
125 }
126}