Skip to content

Commit

Permalink
Merge pull request #13 from jeanibarz/refactor/major_code_restructuring
Browse files Browse the repository at this point in the history
Major Code Overhaul: Refactor, Docstrings, and GCP Vertex AI Support
  • Loading branch information
jeanibarz authored Sep 6, 2024
2 parents e93df0b + 545b5e6 commit b49d972
Show file tree
Hide file tree
Showing 38 changed files with 1,715 additions and 800 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package-lock.json
.vscode-test/
*.vsix
.DS_Store
!node_modules/chatgpt/build/index.js
!node_modules/chatgpt/build/index.js
.secrets/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"displayName": "ChatGPT Copilot",
"icon": "images/ai-logo.png",
"description": "A forked version of the original ChatGPT Copilot Extension by jeanibarz (https://github.com/jeanibarz/chatgpt-copilot), providing additional features for VS Code integration.",
"version": "5.0.1",
"version": "5.1.0",
"aiKey": "",
"repository": {
"url": "https://github.com/jeanibarz/chatgpt-copilot"
Expand Down
19 changes: 19 additions & 0 deletions src/base/baseErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// src/base/baseErrorHandler.ts
import { ILogger } from "../interfaces/ILogger";

/**
* The `BaseErrorHandler` class serves as an abstract base class for error handlers.
* It provides a common interface and shared functionality for handling errors
* in the application. Subclasses must implement specific error handling logic
* as needed.
*/
export abstract class BaseErrorHandler {
protected logger: ILogger;

/**
* Constructor for the `BaseErrorHandler` class.
* Initializes the error handler with a logger instance for logging error events.
*
* @param logger - An instance of ILogger for logging purposes.
*/
constructor(logger: ILogger) {
this.logger = logger;
}

/**
* Handles an error by logging it using the logger instance.
* This method should be called whenever an error occurs in the application.
*
* @param error - The error object to log.
* @param context - A string providing context about where the error occurred.
*/
public handleError(error: any, context: string): void {
this.logger.logError(error, context);
}
Expand Down
19 changes: 19 additions & 0 deletions src/base/baseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
import { IHandler } from "../interfaces/IHandler";
import { ILogger } from "../interfaces/ILogger";

/**
* The `BaseHandler` class serves as an abstract base class for handlers that execute operations.
* It implements the `IHandler<T>` interface and provides a common structure for logging errors.
*
* Subclasses must implement the `execute` method to define their specific handling logic.
*/
export abstract class BaseHandler<T> implements IHandler<T> {
protected logger: ILogger;

/**
* Constructor for the `BaseHandler` class.
* Initializes the handler with a logger instance for logging events.
*
* @param logger - An instance of ILogger for logging purposes.
*/
constructor(logger: ILogger) {
this.logger = logger;
}

/**
* Executes the handler's logic with the provided data.
* This method must be implemented by subclasses to define specific behavior.
*
* @param data - The data to process.
* @returns A Promise that resolves when the execution is complete.
*/
public abstract execute(data: T): Promise<void>;

/**
Expand Down
21 changes: 20 additions & 1 deletion src/base/baseModelNormalizer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// src/base/baseModelNormalizer.ts
import { ILogger } from "../interfaces/ILogger";

/**
* The `BaseModelNormalizer` class serves as an abstract base class for model normalizers.
* It provides a common interface and shared functionality for normalizing model types.
*
* Subclasses must implement the `normalize` method to define their own normalization logic.
* This class also provides logging capabilities to track the normalization process.
*/
export abstract class BaseModelNormalizer {
protected logger: ILogger;

/**
* Constructor for the `BaseModelNormalizer` class.
* Initializes the normalizer with a logger instance for logging normalization events.
*
* @param logger - An instance of ILogger for logging purposes.
*/
constructor(logger: ILogger) {
this.logger = logger;
}

/**
* Normalizes the given model type to a standardized format.
*
* @param modelType - The original model type to normalize.
* @returns The normalized model type as a string, or null if no normalization was found.
*/
public abstract normalize(modelType: string): string | null;

/**
Expand All @@ -20,7 +39,7 @@ export abstract class BaseModelNormalizer {
if (normalizedType) {
this.logger.info(`Normalized model type: ${modelType} to ${normalizedType}`);
} else {
this.logger.warning(`No normalization found for model type: ${modelType}`);
this.logger.warn(`No normalization found for model type: ${modelType}`);
}
}
}
37 changes: 36 additions & 1 deletion src/chatHistoryManager.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
// File: src/chatHistoryManager.ts

/**
* This module manages the chat history for the ChatGPT VS Code extension.
* It provides methods to add messages from users and assistants, clear the history,
* and retrieve the current chat history or the last message.
*
* Key Features:
* - Stores chat messages in an internal array.
* - Allows adding messages with specified roles.
* - Provides functionality to clear and retrieve chat history.
*/

import { CoreMessage } from "ai";

/**
* The `ChatHistoryManager` class manages the chat history of user interactions
* with the assistant. It allows for adding messages, clearing history, and
* retrieving past messages.
*/
export class ChatHistoryManager {
private chatHistory: CoreMessage[] = [];
private chatHistory: CoreMessage[] = []; // Array to store chat messages

/**
* Adds a message to the chat history.
*
* @param role - The role of the message sender ('user' or 'assistant').
* @param content - The content of the message.
*/
public addMessage(role: 'user' | 'assistant', content: string) {
this.chatHistory.push({ role, content });
}

/**
* Clears the entire chat history.
*/
public clearHistory() {
this.chatHistory = [];
}

/**
* Retrieves the current chat history.
*
* @returns An array of `CoreMessage` objects representing the chat history.
*/
public getHistory(): CoreMessage[] {
return this.chatHistory;
}

/**
* Retrieves the last message in the chat history.
*
* @returns The last `CoreMessage` object or undefined if the history is empty.
*/
public getLastMessage(): CoreMessage | undefined {
return this.chatHistory[this.chatHistory.length - 1];
}
Expand Down
Loading

0 comments on commit b49d972

Please sign in to comment.