Design Document - Part II API
Design Document - Part II: API
General Review of Software Architecture
As outlined in Design Document - Part I, the AI Collab Agent extension follows a client-server architecture within the VS Code environment:
- Backend (Extension Host): This is primarily handled by
extension.ts. It runs in the VS Code extension host process (Node.js environment).- Responsibilities: File system interaction (saving/loading data to
.aiCollabData.json), communication with VS Code API (showing messages, opening new editor tabs), generating AI prompts, and handling messages from the Webview. - Data Persistence: Manages the persistent storage of
TeamMemberandProjectdata.
- Responsibilities: File system interaction (saving/loading data to
- Frontend (Webview): This is handled by
webview.html(and its embedded JavaScript and CSS). It runs in a separate, sandboxed browser environment within VS Code.- Responsibilities: User Interface rendering (forms, lists, status messages), user input handling, local state management (though data persistence is delegated to the backend), and sending messages to the
extension.tsbackend via thevscodeAPI. - Communication: Interacts with
extension.tsvia message passing (vscode.postMessageandwindow.addEventListener('message')).
- Responsibilities: User Interface rendering (forms, lists, status messages), user input handling, local state management (though data persistence is delegated to the backend), and sending messages to the
Class Diagram Reference:
Below is a simplified class diagram to illustrate the relationships and responsibilities.
Module: extension.ts (Backend Logic)
This file contains the core logic that runs within the VS Code extension host.
Class: (Implicit) Extension Main Module
- Purpose: Manages the lifecycle of the VS Code extension, registers commands, initializes the webview, handles inter-process communication with the webview, and manages persistent data storage.
Data Fields: (Global/Module-level)
context: vscode.ExtensionContext- Purpose: Provides access to the VS Code API and resources relevant to the extension's lifecycle, such as its installation path (
extensionPath) and global state.
- Purpose: Provides access to the VS Code API and resources relevant to the extension's lifecycle, such as its installation path (
Methods:
-
getDataFilePath(): string | undefined- Purpose: Determines the absolute path where the extension's persistent data (
.aiCollabData.json) should be stored within the currently open VS Code workspace. - Pre-conditions: A VS Code workspace must be open.
- Post-conditions: Returns a valid file path string if a workspace is open, otherwise
undefined. - Parameters: None
- Return:
string | undefined- The full path to the data file. - Exceptions: None explicitly thrown, but returns
undefinedif conditions aren't met.
- Purpose: Determines the absolute path where the extension's persistent data (
-
loadInitialData(): Promise<any>- Purpose: Asynchronously reads the JSON data from the
.aiCollabData.jsonfile, parses it, and returns the application's current state (users, projects, prompt count). Provides default empty states if the file doesn't exist or is corrupted. - Pre-conditions: None. Relies on
getDataFilePath. - Post-conditions: Returns a data object containing
users,projects, andpromptCount.selectedMemberIdsin projects are guaranteed to be arrays. - Parameters: None
- Return:
Promise<any>- A promise that resolves to an object{ users: [], projects: [], promptCount: 0 }. - Exceptions: Handles file read/parse errors internally, falling back to default data.
- Purpose: Asynchronously reads the JSON data from the
-
saveInitialData(data: any): Promise<void>- Purpose: Asynchronously serializes the current application data (users, projects, prompt count) into a JSON string and writes it to the
.aiCollabData.jsonfile. - Pre-conditions:
datamust be a valid JavaScript object. A workspace must be open to determine the file path. - Post-conditions: The
dataobject is written to the.aiCollabData.jsonfile. - Parameters:
data: any: The application state object (containingusers,projects,promptCount) to be saved.
- Return:
Promise<void>- A promise that resolves when the data is successfully saved. - Exceptions: Catches and logs file write errors, shows an error message to the user.
- Purpose: Asynchronously serializes the current application data (users, projects, prompt count) into a JSON string and writes it to the
-
activate(context: vscode.ExtensionContext): void- Purpose: The entry point for the VS Code extension. Called when the extension is activated. It registers commands and initializes listeners.
- Pre-conditions: None (called by VS Code).
- Post-conditions: Commands (
aiCollab.debugHello,aiCollab.openPanel) are registered. - Parameters:
context: vscode.ExtensionContext: The context provided by VS Code for managing extension resources.
- Return:
void - Exceptions: None explicitly.
-
deactivate(): void- Purpose: Called when the extension is deactivated. Used for cleanup. (Currently empty, as VS Code handles most resource disposal for this simple extension).
- Pre-conditions: Extension is active.
- Post-conditions: Resources cleaned up (if any).
- Parameters: None
- Return:
void - Exceptions: None explicitly.
-
getHtml(webview: vscode.Webview, context: vscode.ExtensionContext): Promise<string>- Purpose: Reads the content of
webview.htmlfrom themediafolder, injects a Content Security Policy (CSP) meta tag with a uniquenonce, and ensures script execution. This prepares the HTML for display in the webview panel. - Pre-conditions:
media/webview.htmlmust exist. - Post-conditions: Returns the modified HTML string ready for the webview.
- Parameters:
webview: vscode.Webview: The webview instance, used to getcspSource.context: vscode.ExtensionContext: The extension context, used to getextensionPath.
- Return:
Promise<string>- A promise that resolves to the full HTML content with CSP injected. - Exceptions: Catches file read errors (though not explicitly handled, an error would propagate).
- Purpose: Reads the content of
-
getNonce(): string- Purpose: Generates a cryptographically random string (nonce) used for the Content Security Policy to allow specific scripts to run while disallowing others.
- Pre-conditions: None.
- Post-conditions: Returns a 32-character alphanumeric string.
- Parameters: None
- Return:
string- A random nonce string. - Exceptions: None.
Module: webview.html (Frontend Logic)
This file contains the HTML, CSS, and JavaScript that renders and manages the user interface within the VS Code webview panel.
Class: TeamMember
- Purpose: Represents a single team member with their profile details. Used to create, display, and manage individuals in the application.
Data Fields:
id: string | number- Purpose: Unique identifier for the team member. (Generated using
Date.now() + Math.random()).
- Purpose: Unique identifier for the team member. (Generated using
name: string- Purpose: The full name of the team member.
skills: string- Purpose: A comma-separated list of skills the member possesses.
programmingLanguages: string- Purpose: A comma-separated list of programming languages the member knows.
willingToWorkOn: string- Purpose: Description of the member's interests or preferred project types.
createdAt: string- Purpose: Timestamp (ISO string) of when the team member was added.
Methods:
constructor(name: string, skills: string, programmingLanguages: string, willingToWorkOn: string)- Purpose: Initializes a new
TeamMemberobject. - Pre-conditions:
name,skills,programmingLanguagesshould ideally be non-empty strings. - Post-conditions: A
TeamMemberinstance with generatedidandcreatedAtis created. - Parameters:
name: string: Full name.skills: string: Comma-separated skills.programmingLanguages: string: Comma-separated languages.willingToWorkOn: string: Interests/preferences.
- Return:
TeamMemberinstance. - Exceptions: None.
- Purpose: Initializes a new
Class: Project
- Purpose: Represents a single project within the application, encompassing its details and associated team members.
Data Fields:
id: string | number- Purpose: Unique identifier for the project.
name: string- Purpose: The name of the project.
description: string- Purpose: A detailed description of the project.
goals: string- Purpose: The objectives or aims of the project.
requirements: string- Purpose: Technical and functional requirements of the project.
selectedMemberIds: (string | number)[]- Purpose: An array of
ids of theTeamMembers assigned to this project.
- Purpose: An array of
createdAt: string- Purpose: Timestamp (ISO string) of when the project was created.
Methods:
constructor(name: string, description: string, goals: string, requirements: string, selectedMemberIds: (string | number)[])- Purpose: Initializes a new
Projectobject. - Pre-conditions:
name,descriptionshould ideally be non-empty strings.selectedMemberIdsshould be an array of validTeamMemberIDs. - Post-conditions: A
Projectinstance with generatedidandcreatedAtis created. - Parameters:
name: string: Project name.description: string: Project description.goals: string: Project goals.requirements: string: Project requirements.selectedMemberIds: (string | number)[]: Array of IDs of assigned team members.
- Return:
Projectinstance. - Exceptions: None.
- Purpose: Initializes a new
Class: TeamCollaborationApp
- Purpose: The central controller for the webview's UI and data management. It orchestrates user interactions, updates the display, and communicates with the VS Code extension backend for data persistence and AI services.
Data Fields:
users: TeamMember[]- Purpose: An array holding all
TeamMemberobjects currently loaded in the application.
- Purpose: An array holding all
projects: Project[]- Purpose: An array holding all
Projectobjects currently loaded in the application.
- Purpose: An array holding all
promptCount: number- Purpose: A counter for how many AI prompts have been generated.
Methods:
-
constructor()- Purpose: Initializes the application state and immediately requests initial data from the VS Code extension.
- Pre-conditions: None.
- Post-conditions:
users,projectsare initialized as empty arrays,promptCountto 0. A message is sent to the extension toloadData. - Parameters: None
- Return:
TeamCollaborationAppinstance. - Exceptions: None.
-
loadData(data: { users: any[], projects: any[], promptCount: number }): void- Purpose: Updates the internal state of the
TeamCollaborationAppwith data received from the VS Code extension. It reconstructsTeamMemberandProjectobjects from raw data and then triggers a UI update. - Pre-conditions:
datamust be an object containingusers,projects, andpromptCount. - Post-conditions:
this.users,this.projects,this.promptCountare updated. The UI is re-rendered. - Parameters:
data: object: The payload received from the extension, containing arrays of user and project data and the prompt count.
- Return:
void - Exceptions: None.
- Purpose: Updates the internal state of the
-
saveData(): void- Purpose: Sends the current internal state (
this.users,this.projects,this.promptCount) to the VS Code extension for persistent storage. - Pre-conditions: None.
- Post-conditions: A
saveDatamessage is posted to the VS Code extension. - Parameters: None
- Return:
void - Exceptions: None.
- Purpose: Sends the current internal state (
-
addUser(name: string, skills: string, programmingLanguages: string, willingToWorkOn: string): TeamMember- Purpose: Creates a new
TeamMemberobject, adds it to theusersarray, and triggers data saving and UI update. - Pre-conditions:
name,skills,programmingLanguagesmust be non-empty strings. - Post-conditions: A new
TeamMemberis added, data is saved, UI is updated. - Parameters:
name: string: Full name.skills: string: Comma-separated skills.programmingLanguages: string: Comma-separated languages.willingToWorkOn: string: Interests/preferences.
- Return:
TeamMember- The newly added user object. - Exceptions: Throws
Errorif required fields are empty.
- Purpose: Creates a new
-
createProject(name: string, description: string, goals: string, requirements: string, selectedUserIds: (string|number)[]): Project- Purpose: Creates a new
Projectobject with assigned team members, adds it to theprojectsarray, and triggers data saving and UI update. - Pre-conditions:
name,descriptionmust be non-empty strings.selectedUserIdsmust be a non-empty array of existing user IDs. - Post-conditions: A new
Projectis added, data is saved, UI is updated. - Parameters:
name: string: Project name.description: string: Project description.goals: string: Project goals.requirements: string: Project requirements.selectedUserIds: (string|number)[]: Array of IDs of selected team members.
- Return:
Project- The newly created project object. - Exceptions: Throws
Errorif required fields are empty or no members are selected.
- Purpose: Creates a new
-
generateAIPrompt(projectId: string | number): void- Purpose: Requests the VS Code extension backend to generate an AI prompt for a specified project.
- Pre-conditions:
projectIdmust be a valid ID of an existing project. - Post-conditions: A
generatePromptmessage is posted to the VS Code extension. The UI displays a "Generating..." status. - Parameters:
projectId: string | number: The ID of the project for which to generate the prompt.
- Return:
void - Exceptions: None (error handling for invalid project ID is on the extension side).
-
updateUI(): void- Purpose: A central method to refresh all parts of the webview UI (stats, user list, project list, dropdowns) based on the current internal data.
- Pre-conditions: None.
- Post-conditions: All relevant UI elements are updated.
- Parameters: None
- Return:
void - Exceptions: None.
-
updateStats(): void- Purpose: Updates the display of the total number of users, projects, and AI prompts.
- Pre-conditions: None.
- Post-conditions: The
userCount,projectCount, andpromptCountelements in the HTML are updated. - Parameters: None
- Return:
void - Exceptions: None.
-
updateUserList(): void- Purpose: Renders or re-renders the list of all team members in the "Team Members" tab.
- Pre-conditions: The
userListHTML element must exist. - Post-conditions: The
userListelement contains HTML representations of allthis.users. - Parameters: None
- Return:
void - Exceptions: None.
-
updateProjectList(): void- Purpose: Renders or re-renders the list of all projects in the "Projects" tab, showing assigned team members.
- Pre-conditions: The
projectListHTML element must exist. - Post-conditions: The
projectListelement contains HTML representations of allthis.projects. - Parameters: None
- Return:
void - Exceptions: None.
-
updateUserSelection(): void- Purpose: Populates the multi-select checkbox list for team member assignment when creating a project.
- Pre-conditions: The
userSelectionHTML element must exist. - Post-conditions: The
userSelectionelement contains checkbox inputs for allthis.users. - Parameters: None
- Return:
void - Exceptions: None.
-
updateProjectSelection(): void- Purpose: Populates the dropdown list of projects in the "AI Prompts" tab for prompt generation.
- Pre-conditions: The
promptProjectHTML select element must exist. - Post-conditions: The
promptProjectelement contains<option>tags for allthis.projects. - Parameters: None
- Return:
void - Exceptions: None.
-
removeUser(userId: string | number): void- Purpose: Removes a
TeamMemberfrom theusersarray by their ID. Also removes this user from any projects they were assigned to. Triggers data saving and UI update. - Pre-conditions:
userIdmust correspond to an existingTeamMember. - Post-conditions: The specified user is removed from
this.usersand fromselectedMemberIdsof any project. Data is saved, UI is updated. - Parameters:
userId: string | number: The ID of the user to remove.
- Return:
void - Exceptions: None.
- Purpose: Removes a
-
removeProject(projectId: string | number): void- Purpose: Removes a
Projectfrom theprojectsarray by its ID. Triggers data saving and UI update. - Pre-conditions:
projectIdmust correspond to an existingProject. - Post-conditions: The specified project is removed from
this.projects. Data is saved, UI is updated. - Parameters:
projectId: string | number: The ID of the project to remove.
- Return:
void - Exceptions: None.
- Purpose: Removes a
Additional Notes:
- Error Handling: Exceptions are primarily handled at the point of origin (e.g.,
addUserthrows anErrorfor missing fields, which is then caught by the event listener and displayed viashowStatus). Network/file errors are handled on the extension side. - Parameters & Data Types: TypeScript-like annotations (
: string,: number,: any[],: Promise<void>) are used to indicate data types. - Documentation Generation: Tools like JSDoc (for JavaScript/TypeScript) can parse comments structured like the ones described here (e.g., using
@param,@returns,@throws) to automatically generate HTML documentation. This document serves as the content that would typically go into those comments.
Module: ai-analyze-code.ts (AI Code Analyzer)
This file implements the "AI Analyze Code" feature of the VS Code extension. It is integrated with the Supabase backend to analyze the user highlighted code with the OpenAI API. It displays inline UI buttons via CodeLens, and AI feedbback in a side panel.
Class: AnalyzeCodeLensProvider
- Purpose: Provides and manages dynamic entries (inline buttons) in the editor when the user highlights a block of code eligible for AI analysis.