diff --git a/core/config/load.ts b/core/config/load.ts index f911b9932..d0df1aa06 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -51,10 +51,12 @@ function resolveSerializedConfig(filepath: string): SerializedContinueConfig { }; config.env.forEach((envVar) => { - content = content.replaceAll( - new RegExp(`"${envVar}"`, "g"), - `"${env[envVar]}"`, - ); + if (envVar in env) { + content = content.replaceAll( + new RegExp(`"${envVar}"`, "g"), + `"${env[envVar]}"`, + ); + } }); } diff --git a/extensions/vscode/src/activation/auth.ts b/extensions/vscode/src/activation/auth.ts new file mode 100644 index 000000000..f90985d96 --- /dev/null +++ b/extensions/vscode/src/activation/auth.ts @@ -0,0 +1,62 @@ +import { v4 as uuidv4 } from "uuid"; +import * as vscode from "vscode"; + +export async function setupAuth() { + const session = await vscode.authentication.getSession("github", [], { + createIfNone: true, + }); + return session; +} + +export class ManualTokenAuthProvider implements vscode.AuthenticationProvider { + constructor( + public onDidChangeSessions: vscode.Event, + private readonly context: vscode.ExtensionContext, + ) {} + + getSessions( + scopes?: readonly string[] | undefined, + ): Thenable { + const sessions = this.context.globalState.get< + vscode.AuthenticationSession[] + >("continue.manualTokenSessions"); + return Promise.resolve( + sessions?.filter((session) => { + if (!scopes) { + return true; + } + return !scopes.some((scope) => session.scopes.indexOf(scope) === -1); + }) ?? [], + ); + } + async createSession( + scopes: readonly string[], + ): Promise { + const token = await vscode.window.showInputBox({ + prompt: "Enter your Continue user token", + ignoreFocusOut: true, + }); + + if (!token) { + throw new Error("User canceled"); + } + + const session: vscode.AuthenticationSession = { + id: uuidv4(), + accessToken: token, + account: { + id: token, + label: token, + }, + scopes: [], + }; + return session; + } + removeSession(sessionId: string): Thenable { + return this.context.globalState.update( + "continue.manualTokenSessions", + (sessions: vscode.AuthenticationSession[] | undefined) => + sessions?.filter((session) => session.id !== sessionId), + ); + } +} diff --git a/extensions/vscode/src/extension/vscodeExtension.ts b/extensions/vscode/src/extension/vscodeExtension.ts index d06a3bc5a..36f8cafc4 100644 --- a/extensions/vscode/src/extension/vscodeExtension.ts +++ b/extensions/vscode/src/extension/vscodeExtension.ts @@ -11,8 +11,8 @@ import { DiffManager } from "../diff/horizontal"; import { VerticalPerLineDiffManager } from "../diff/verticalPerLine/manager"; import { VsCodeIde } from "../ideProtocol"; import { registerAllCodeLensProviders } from "../lang-server/codeLens"; +import { setupRemoteConfigSync } from "../stubs/activation"; import { TabAutocompleteModel } from "../util/loadAutocompleteModel"; -import { RemoteConfigSync } from "../util/remoteConfig"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; export class VsCodeExtension { @@ -78,14 +78,10 @@ export class VsCodeExtension { this.extensionContext, this.verticalDiffManager, ); - try { - const configSync = new RemoteConfigSync( - this.configHandler.reloadConfig.bind(this.configHandler), - ); - configSync.setup(); - } catch (e) { - console.warn(`Failed to sync remote config: ${e}`); - } + + setupRemoteConfigSync( + this.configHandler.reloadConfig.bind(this.configHandler), + ); // Sidebar context.subscriptions.push( diff --git a/extensions/vscode/src/stubs/activation.ts b/extensions/vscode/src/stubs/activation.ts new file mode 100644 index 000000000..3e4ae2635 --- /dev/null +++ b/extensions/vscode/src/stubs/activation.ts @@ -0,0 +1 @@ +export async function setupRemoteConfigSync(reloadConfig: () => void) {} diff --git a/extensions/vscode/src/stubs/auth.ts b/extensions/vscode/src/stubs/auth.ts new file mode 100644 index 000000000..70de9e24b --- /dev/null +++ b/extensions/vscode/src/stubs/auth.ts @@ -0,0 +1,3 @@ +export async function getUserToken(): Promise { + return Promise.resolve(""); +} diff --git a/extensions/vscode/src/util/remoteConfig.ts b/extensions/vscode/src/util/remoteConfig.ts deleted file mode 100644 index 294277484..000000000 --- a/extensions/vscode/src/util/remoteConfig.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { - getConfigJsPathForRemote, - getConfigJsonPathForRemote, -} from "core/util/paths"; -import * as fs from "fs"; -import * as vscode from "vscode"; - -export class RemoteConfigSync { - private userToken: string | null; - private remoteConfigServerUrl: string | null; - private remoteConfigSyncPeriod: number; - - private syncInterval: NodeJS.Timer | undefined = undefined; - - constructor(private triggerReloadConfig: () => void) { - const { userToken, remoteConfigServerUrl, remoteConfigSyncPeriod } = - this.loadVsCodeSettings(); - this.userToken = userToken; - this.remoteConfigServerUrl = remoteConfigServerUrl; - this.remoteConfigSyncPeriod = remoteConfigSyncPeriod; - - // Listen for changes to VS Code settings, then trigger a refresh - vscode.workspace.onDidChangeConfiguration((event) => { - if (event.affectsConfiguration("continue")) { - const { userToken, remoteConfigServerUrl, remoteConfigSyncPeriod } = - this.loadVsCodeSettings(); - if ( - userToken !== this.userToken || - remoteConfigServerUrl !== this.remoteConfigServerUrl || - remoteConfigSyncPeriod !== this.remoteConfigSyncPeriod - ) { - this.userToken = userToken; - this.remoteConfigServerUrl = remoteConfigServerUrl; - this.remoteConfigSyncPeriod = remoteConfigSyncPeriod; - - this.setInterval(); - } - } - }); - } - - private loadVsCodeSettings() { - const settings = vscode.workspace.getConfiguration("continue"); - const userToken = settings.get("userToken", null); - const remoteConfigServerUrl = settings.get( - "remoteConfigServerUrl", - null, - ); - const remoteConfigSyncPeriod = settings.get( - "remoteConfigSyncPeriod", - 60, - ); - - return { - userToken, - remoteConfigServerUrl, - remoteConfigSyncPeriod, - }; - } - - async setup() { - if ( - this.userToken === null || - this.remoteConfigServerUrl === null || - this.remoteConfigServerUrl.trim() === "" - ) { - return; - } - if (!URL.canParse(this.remoteConfigServerUrl)) { - vscode.window.showErrorMessage( - "The value set for 'remoteConfigServerUrl' is not valid: ", - this.remoteConfigServerUrl, - ); - return; - } - - // Sync once - await this.sync(this.userToken, new URL(this.remoteConfigServerUrl)); - - // Set timer to sync at user-defined interval - this.setInterval(); - } - - private setInterval() { - if (this.syncInterval !== undefined) { - clearInterval(this.syncInterval); - } - this.syncInterval = setInterval( - () => { - if (!this.userToken || !this.remoteConfigServerUrl) return; - this.sync(this.userToken, new URL(this.remoteConfigServerUrl)); - }, - this.remoteConfigSyncPeriod * 1000 * 60, - ); - } - - async sync(userToken: string, remoteConfigServerUrl: URL) { - try { - const response = await fetch( - new URL("sync", remoteConfigServerUrl).href, - { - method: "GET", - headers: { - Authorization: `Bearer ${userToken}`, - }, - }, - ); - - if (!response.ok) { - vscode.window.showErrorMessage( - `Failed to sync remote config (HTTP ${response.status}): ${response.statusText}`, - ); - return; - } - - const { configJson, configJs } = await response.json(); - - fs.writeFileSync( - getConfigJsonPathForRemote(remoteConfigServerUrl), - configJson, - ); - fs.writeFileSync( - getConfigJsPathForRemote(remoteConfigServerUrl), - configJs, - ); - this.triggerReloadConfig(); - } catch (e) { - vscode.window.showErrorMessage(`Failed to sync remote config: ${e}`); - } - } -}