Skip to content

Commit

Permalink
🔧 chore(providers): 优化 AI 服务商配置和错误处理
Browse files Browse the repository at this point in the history
- 修复 Gemini AI 枚举值命名
- 统一 Gemini 服务商显示名称
- 优化 OpenAI 模型获取逻辑【性能】
- 移除基础命令中的配置校验【重构】
- 修复文件末尾换行符问题【格式】
  • Loading branch information
littleCareless committed Dec 13, 2024
1 parent 911ed64 commit 4dde116
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,4 @@
]
}
}
}
}
2 changes: 1 addition & 1 deletion src/ai/AIProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class AIProviderFactory {
case AIProvider.DOUBAO:
provider = new DoubaoProvider();
break;
case AIProvider.Gemini:
case AIProvider.GEMINI:
provider = new GeminiAIProvider();
break;
default:
Expand Down
27 changes: 24 additions & 3 deletions src/ai/providers/BaseOpenAIProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,30 @@ export abstract class BaseOpenAIProvider implements AIProvider {
}
}

async getModels(): Promise<AIModel[]> {
return Promise.resolve(this.config.models);
async getModels(): Promise<AIModel[] | any[]> {
try {
const response = await this.openai.models.list();
return response.data.map((model: any) => {
console.log("model", model);
return {
id: model.id,
name: model.id,
maxTokens: {
input: model.context_window || 4096,
output: Math.floor((model.context_window || 4096) / 2),
},
provider: this.provider,
};
});
} catch (error) {
console.warn("Failed to fetch models:", error);
return this.config.models;
}
}

async refreshModels(): Promise<string[]> {
const models = await this.getModels();
return models.map((m) => m.id);
}

getName(): string {
Expand All @@ -135,5 +157,4 @@ export abstract class BaseOpenAIProvider implements AIProvider {
}

abstract isAvailable(): Promise<boolean>;
abstract refreshModels(): Promise<string[]>;
}
2 changes: 1 addition & 1 deletion src/ai/providers/GeminiAIProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class GeminiAIProvider extends BaseOpenAIProvider {
apiKey: configManager.getConfig("PROVIDERS_GEMINI_APIKEY"),
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
providerId: "gemini",
providerName: "Gemini AI",
providerName: "Gemini",
models: geminiModels,
defaultModel: "gemini-1",
});
Expand Down
8 changes: 4 additions & 4 deletions src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export abstract class BaseCommand {
}

protected async validateConfig(): Promise<boolean> {
if (!(await ConfigurationManager.getInstance().validateConfiguration())) {
await NotificationHandler.error("command.execution.failed");
return false;
}
// if (!(await ConfigurationManager.getInstance().validateConfiguration())) {
// await NotificationHandler.error("command.execution.failed");
// return false;
// }
return true;
}

Expand Down

0 comments on commit 4dde116

Please sign in to comment.