Skip to content

Commit 0071958

Browse files
authored
Merge pull request #11 from forrany/feat/add-model-list
✨ feat(user-interface): add command to show available models
2 parents be39b1b + 949ed77 commit 0071958

4 files changed

Lines changed: 69 additions & 6 deletions

File tree

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
"light": "images/icon.svg"
3939
},
4040
"title": "AI Commit"
41+
},
42+
{
43+
"command": "ai-commit.showAvailableModels",
44+
"title": "Show Available OpenAI Models"
4145
}
4246
],
4347
"configuration": {
@@ -106,7 +110,7 @@
106110
},
107111
"ai-commit.OPENAI_MODEL": {
108112
"default": "gpt-4o",
109-
"description": "OpenAI MODEL",
113+
"description": "OpenAI MODEL, you can select a model from the list by running the 'Show Available OpenAI Models' command",
110114
"type": "string"
111115
},
112116
"ai-commit.AI_COMMIT_SYSTEM_PROMPT": {

src/commands.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import { generateCommitMsg } from './generate-commit-msg';
3+
import { ConfigurationManager } from './config';
34

45
/**
56
* Manages the registration and disposal of commands.
@@ -14,6 +15,20 @@ export class CommandManager {
1415
this.registerCommand('extension.configure-ai-commit', () =>
1516
vscode.commands.executeCommand('workbench.action.openSettings', 'ai-commit')
1617
);
18+
19+
// Show available OpenAI models
20+
this.registerCommand('ai-commit.showAvailableModels', async () => {
21+
const configManager = ConfigurationManager.getInstance();
22+
const models = await configManager.getAvailableModels();
23+
const selected = await vscode.window.showQuickPick(models, {
24+
placeHolder: 'Please select a model'
25+
});
26+
27+
if (selected) {
28+
const config = vscode.workspace.getConfiguration('ai-commit');
29+
await config.update('OPENAI_MODEL', selected, vscode.ConfigurationTarget.Global);
30+
}
31+
});
1732
}
1833

1934
private registerCommand(command: string, handler: (...args: any[]) => any) {

src/config.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode';
2+
import { createOpenAIApi } from './openai-utils';
23

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

30-
private constructor() {
32+
private constructor(context: vscode.ExtensionContext) {
33+
this.context = context;
3134
this.disposable = vscode.workspace.onDidChangeConfiguration((event) => {
3235
if (event.affectsConfiguration('ai-commit')) {
3336
this.configCache.clear();
37+
38+
if (event.affectsConfiguration('ai-commit.OPENAI_BASE_URL') ||
39+
event.affectsConfiguration('ai-commit.OPENAI_API_KEY')) {
40+
this.updateModelList();
41+
}
3442
}
3543
});
3644
}
3745

38-
static getInstance(): ConfigurationManager {
39-
if (!this.instance) {
40-
this.instance = new ConfigurationManager();
46+
static getInstance(context?: vscode.ExtensionContext): ConfigurationManager {
47+
if (!this.instance && context) {
48+
this.instance = new ConfigurationManager(context);
4149
}
4250
return this.instance;
4351
}
@@ -53,4 +61,40 @@ export class ConfigurationManager {
5361
dispose() {
5462
this.disposable.dispose();
5563
}
64+
65+
/**
66+
* Updates the list of available OpenAI models.
67+
*/
68+
private async updateModelList() {
69+
try {
70+
const openai = createOpenAIApi();
71+
const models = await openai.models.list();
72+
73+
// Save available models to extension state
74+
await this.context.globalState.update('availableModels', models.data.map(model => model.id));
75+
76+
// Get the current selected model
77+
const config = vscode.workspace.getConfiguration('ai-commit');
78+
const currentModel = config.get<string>('OPENAI_MODEL');
79+
80+
// If the current selected model is not in the available list, set it to the default value
81+
const availableModels = models.data.map(model => model.id);
82+
if (!availableModels.includes(currentModel)) {
83+
await config.update('OPENAI_MODEL', 'gpt-4', vscode.ConfigurationTarget.Global);
84+
}
85+
} catch (error) {
86+
console.error('Failed to fetch OpenAI models:', error);
87+
}
88+
}
89+
90+
/**
91+
* Retrieves the list of available OpenAI models.
92+
* @returns {Promise<string[]>} The list of available models.
93+
*/
94+
public async getAvailableModels(): Promise<string[]> {
95+
if (!this.context.globalState.get<string[]>('availableModels')) {
96+
await this.updateModelList();
97+
}
98+
return this.context.globalState.get<string[]>('availableModels', []);
99+
}
56100
}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ConfigurationManager } from './config';
99
*/
1010
export async function activate(context: vscode.ExtensionContext) {
1111
try {
12-
const configManager = ConfigurationManager.getInstance();
12+
const configManager = ConfigurationManager.getInstance(context);
1313

1414
const commandManager = new CommandManager(context);
1515
commandManager.registerCommands();

0 commit comments

Comments
 (0)