Skip to content

Commit

Permalink
Merge pull request #11 from forrany/feat/add-model-list
Browse files Browse the repository at this point in the history
✨ feat(user-interface): add command to show available models
  • Loading branch information
Sitoi authored Nov 12, 2024
2 parents be39b1b + 949ed77 commit 0071958
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"light": "images/icon.svg"
},
"title": "AI Commit"
},
{
"command": "ai-commit.showAvailableModels",
"title": "Show Available OpenAI Models"
}
],
"configuration": {
Expand Down Expand Up @@ -106,7 +110,7 @@
},
"ai-commit.OPENAI_MODEL": {
"default": "gpt-4o",
"description": "OpenAI MODEL",
"description": "OpenAI MODEL, you can select a model from the list by running the 'Show Available OpenAI Models' command",
"type": "string"
},
"ai-commit.AI_COMMIT_SYSTEM_PROMPT": {
Expand Down
15 changes: 15 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { generateCommitMsg } from './generate-commit-msg';
import { ConfigurationManager } from './config';

/**
* Manages the registration and disposal of commands.
Expand All @@ -14,6 +15,20 @@ export class CommandManager {
this.registerCommand('extension.configure-ai-commit', () =>
vscode.commands.executeCommand('workbench.action.openSettings', 'ai-commit')
);

// Show available OpenAI models
this.registerCommand('ai-commit.showAvailableModels', async () => {
const configManager = ConfigurationManager.getInstance();
const models = await configManager.getAvailableModels();
const selected = await vscode.window.showQuickPick(models, {
placeHolder: 'Please select a model'
});

if (selected) {
const config = vscode.workspace.getConfiguration('ai-commit');
await config.update('OPENAI_MODEL', selected, vscode.ConfigurationTarget.Global);
}
});
}

private registerCommand(command: string, handler: (...args: any[]) => any) {
Expand Down
52 changes: 48 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from 'vscode';
import { createOpenAIApi } from './openai-utils';

/**
* Configuration keys used in the AI commit extension.
Expand Down Expand Up @@ -26,18 +27,25 @@ export class ConfigurationManager {
private static instance: ConfigurationManager;
private configCache: Map<string, any> = new Map();
private disposable: vscode.Disposable;
private context: vscode.ExtensionContext;

private constructor() {
private constructor(context: vscode.ExtensionContext) {
this.context = context;
this.disposable = vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('ai-commit')) {
this.configCache.clear();

if (event.affectsConfiguration('ai-commit.OPENAI_BASE_URL') ||
event.affectsConfiguration('ai-commit.OPENAI_API_KEY')) {
this.updateModelList();
}
}
});
}

static getInstance(): ConfigurationManager {
if (!this.instance) {
this.instance = new ConfigurationManager();
static getInstance(context?: vscode.ExtensionContext): ConfigurationManager {
if (!this.instance && context) {
this.instance = new ConfigurationManager(context);
}
return this.instance;
}
Expand All @@ -53,4 +61,40 @@ export class ConfigurationManager {
dispose() {
this.disposable.dispose();
}

/**
* Updates the list of available OpenAI models.
*/
private async updateModelList() {
try {
const openai = createOpenAIApi();
const models = await openai.models.list();

// Save available models to extension state
await this.context.globalState.update('availableModels', models.data.map(model => model.id));

// Get the current selected model
const config = vscode.workspace.getConfiguration('ai-commit');
const currentModel = config.get<string>('OPENAI_MODEL');

// If the current selected model is not in the available list, set it to the default value
const availableModels = models.data.map(model => model.id);
if (!availableModels.includes(currentModel)) {
await config.update('OPENAI_MODEL', 'gpt-4', vscode.ConfigurationTarget.Global);
}
} catch (error) {
console.error('Failed to fetch OpenAI models:', error);
}
}

/**
* Retrieves the list of available OpenAI models.
* @returns {Promise<string[]>} The list of available models.
*/
public async getAvailableModels(): Promise<string[]> {
if (!this.context.globalState.get<string[]>('availableModels')) {
await this.updateModelList();
}
return this.context.globalState.get<string[]>('availableModels', []);
}
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ConfigurationManager } from './config';
*/
export async function activate(context: vscode.ExtensionContext) {
try {
const configManager = ConfigurationManager.getInstance();
const configManager = ConfigurationManager.getInstance(context);

const commandManager = new CommandManager(context);
commandManager.registerCommands();
Expand Down

0 comments on commit 0071958

Please sign in to comment.