From dcb95a41ae4ebac10d026858ca9de492e1516d29 Mon Sep 17 00:00:00 2001 From: Karl Clement Date: Tue, 12 Mar 2024 09:42:49 -0700 Subject: [PATCH 1/2] Changes for demo --- codex-vscode/src/services/oauth.service.ts | 22 ++++++++++++------- .../services/undefinedTextEditor.service.ts | 9 ++++---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/codex-vscode/src/services/oauth.service.ts b/codex-vscode/src/services/oauth.service.ts index 08f6a362..a1e1e68a 100644 --- a/codex-vscode/src/services/oauth.service.ts +++ b/codex-vscode/src/services/oauth.service.ts @@ -2,7 +2,8 @@ import vscode, { EventEmitter } from "vscode"; import { OAuthUrlHandler } from "../helpers/handlers/OAuthUrl.handler"; import { AuthService } from "./auth.service"; import { PromiseAdapter, promiseFromEvent } from "../helpers/eventEmitter.helper"; -import { ErrorResponse, IOAuthService, OAuthPath, OAuthPlatform, UserInfo } from "../models/shared/IOAuthService"; + +import {ErrorResponse, IOAuthService, OAuthPath, OAuthPlatform, UserInfo} from "../models/shared/IOAuthService"; type AuthorizeResponse = { code: string; @@ -16,7 +17,7 @@ export class OAuthService implements IOAuthService { */ public async authenticate(oAuthPath: OAuthPath): Promise { const oAuthPlatform: OAuthPlatform = oAuthPath === OAuthPath.google ? OAuthPlatform.google : OAuthPlatform.github; - const eventEmitter = OAuthUrlHandler.getInstance().getEventEmitter(); + const ee = OAuthUrlHandler.getInstance().getEventEmitter(); const uri = vscode.Uri.parse(`${process.env.API_URL}${oAuthPath}`); await vscode.env.openExternal(uri); @@ -24,13 +25,17 @@ export class OAuthService implements IOAuthService { // Register a single listener for the URI callback, in case the user starts the login process multiple times // before completing it. if (!OAuthService.codeExchangePromise) { - OAuthService.codeExchangePromise = promiseFromEvent(eventEmitter.event, this.postAuthorization()); + OAuthService.codeExchangePromise = promiseFromEvent( + ee.event, + this.postAuthorization() + ); } return Promise.race([ OAuthService.codeExchangePromise.promise, new Promise((_, reject) => setTimeout(() => { + // TODO we should make this alot simpler this.clearCodeExchangePromise(); reject({ errorType: "cancelled", @@ -46,23 +51,24 @@ export class OAuthService implements IOAuthService { () => async (uri: vscode.Uri, resolve: (userInfo: UserInfo) => void, reject: (err: any) => void): Promise => { this.clearCodeExchangePromise(); - const parsedQuery = this.parseQuery(uri) as AuthorizeResponse | ErrorResponse; - if ((parsedQuery as AuthorizeResponse).code) { + const parsed_query = this.parseQuery(uri) as AuthorizeResponse | ErrorResponse; + if ((parsed_query as AuthorizeResponse).code) { // Get code from URI, pass to our server to exchange for a JWT & user info. - const loginResponse = await AuthService.getAuthService().login((parsedQuery as AuthorizeResponse).code); + const loginResponse = await AuthService.getAuthService().login((parsed_query as AuthorizeResponse).code); const userInfo = loginResponse.data; resolve(userInfo); } else { reject({ - errorType: (parsedQuery as ErrorResponse).errorType, - errorMessage: (parsedQuery as ErrorResponse).errorMessage, + errorType: (parsed_query as ErrorResponse).errorType, + errorMessage: (parsed_query as ErrorResponse).errorMessage, }); } }; private parseQuery(uri: vscode.Uri) { return uri.query.split("&").reduce((prev: any, current) => { + // important to split const queryString = current.split("="); prev[queryString[0]] = queryString[1]; return prev; diff --git a/codex-vscode/src/services/undefinedTextEditor.service.ts b/codex-vscode/src/services/undefinedTextEditor.service.ts index 1cdc8fd9..9ab52ed3 100644 --- a/codex-vscode/src/services/undefinedTextEditor.service.ts +++ b/codex-vscode/src/services/undefinedTextEditor.service.ts @@ -24,6 +24,7 @@ export class UndefinedTextEditorService implements ITextEditorService { public async loadData(): Promise { await this.getContexts(true); + } public async getDefaultCodices(): Promise { @@ -41,6 +42,7 @@ export class UndefinedTextEditorService implements ITextEditorService { const defaultCodices = await this.getDefaultCodices(); if (defaultCodices.length > 0) { const defaultCodex = defaultCodices[0]; + return new CodexService(defaultCodex.id, UserService.getInstance()); } else { throw new Error("No active codices at this time."); @@ -51,7 +53,7 @@ export class UndefinedTextEditorService implements ITextEditorService { return new CodexService(codexId, UserService.getInstance()); } - public async getDefaultCodexServiceForRepo(repositoryService: RepositoryService): Promise { + public async getDefaultCodexServiceForRepo(repositoryService: any): Promise { const codex: Codex = await UserService.getInstance() .getCCLCodexServiceWrapper() .getDefaultCodex(repositoryService.origin); @@ -65,6 +67,7 @@ export class UndefinedTextEditorService implements ITextEditorService { if (force) { this.contexts = ( await Promise.all( + this.repositoryServices.map(async (rs) => { const codexService = await this.getDefaultCodexServiceForRepo(rs); if (codexService) { @@ -84,9 +87,7 @@ export class UndefinedTextEditorService implements ITextEditorService { } public async updateContextDecorations(force: boolean): Promise { - if (force) { - await this.getContexts(force); - } + if (force) await this.getContexts(force); await updateDecorations(this.contexts, LINE_DECORATION, GUTTER_DECORATION, this.textEditor); } From be573a73b552a3541916290355109c73cbb031ea Mon Sep 17 00:00:00 2001 From: Karl Clement Date: Tue, 12 Mar 2024 09:46:28 -0700 Subject: [PATCH 2/2] Added change to user.service --- codex-vscode/src/services/user.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/codex-vscode/src/services/user.service.ts b/codex-vscode/src/services/user.service.ts index 9da98489..41a99a74 100644 --- a/codex-vscode/src/services/user.service.ts +++ b/codex-vscode/src/services/user.service.ts @@ -96,6 +96,7 @@ export class UserService implements Singleton, IUserService { return this.ccl.getContextsService(); } + // TODO clean up comments public getCCLCodexServiceWrapper(): ICCLCodexServiceWrapper { if (!this.cclCodexServiceWrapper) { this.cclCodexServiceWrapper = new CCLCodexServiceWrapper(this.ccl.getCodexService(), this);