Skip to content

Commit

Permalink
🔧 refactor(ai): 优化 AI 提供程序和参数配置
Browse files Browse the repository at this point in the history
- 修改 AI 提供商类型常量,将 VSCODE 更改为 VS_CODE_PROVIDED
- 简化 AIProvider 配置获取方式,使用统一的 PROVIDERS 配置项
- 调整 AIRequestParams 接口,将选项分为代码分析和提交格式两个子类别
- 重构生成系统提示的逻辑,增加防循环调用保护
- 增加配置管理器集成,使用完整配置构建提示信息
  • Loading branch information
littleCareless committed Dec 12, 2024
1 parent 9e97e1c commit 238d03a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/ai/AIProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ export class AIProviderFactory {
this.cleanStaleProviders();
const providerType =
type ||
ConfigurationManager.getInstance().getConfig<string>("PROVIDER") ||
ConfigurationManager.getInstance().getConfig("PROVIDERS") ||
AIProvider.OPENAI;

let provider = this.providers.get(providerType);

console.log("AIProvider", AIProvider);
console.log("providerType", providerType.toLowerCase());
console.log("AIProvider.VSCODE", AIProvider.VSCODE);
if (!provider) {
switch (providerType.toLowerCase()) {
case AIProvider.OPENAI:
Expand All @@ -41,7 +43,7 @@ export class AIProviderFactory {
case AIProvider.OLLAMA:
provider = new OllamaProvider();
break;
case AIProvider.VSCODE:
case AIProvider.VS_CODE_PROVIDED:
provider = new VSCodeProvider();
break;
case AIProvider.ZHIPU:
Expand Down
12 changes: 9 additions & 3 deletions src/ai/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ export interface AIRequestParams {
model: AIModel;
language?: string;
scm?: "git" | "svn"; // 新增SCM类型
allowMergeCommits?: boolean;
splitChangesInSingleFile?: boolean;
additionalContext: string;
useEmoji: boolean;

// 代码分析相关选项 - 从 features.codeAnalysis
simplifyDiff?: boolean;
maxLineLength?: number;
contextLines?: number;

// 提交格式相关选项 - 从 features.commitFormat
enableMergeCommit?: boolean;
enableEmoji?: boolean;
}

// 添加通用错误处理接口
Expand Down
37 changes: 25 additions & 12 deletions src/ai/utils/generateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NotificationHandler } from "../../utils/NotificationHandler";
import { LocalizationManager } from "../../utils/LocalizationManager";
import { generateCommitMessageSystemPrompt } from "../../prompt/prompt";
import { AIRequestParams } from "../types";
import { ConfigurationManager } from "../../config/ConfigurationManager";

// 添加错误类型枚举
export enum AIGenerationErrorType {
Expand Down Expand Up @@ -45,9 +46,7 @@ export async function generateWithRetry<T>(

if (params.diff.length > maxInputLength) {
NotificationHandler.warn(
LocalizationManager.getInstance().getMessage(
`${provider}.input.truncated`
)
LocalizationManager.getInstance().getMessage(`input.truncated`)
);
}

Expand Down Expand Up @@ -75,14 +74,28 @@ export async function generateWithRetry<T>(
}
}

let isGeneratingPrompt = false;

export function getSystemPrompt(params: AIRequestParams): string {
return (
params.systemPrompt ||
generateCommitMessageSystemPrompt(
params.language || "",
params.allowMergeCommits || false,
params.splitChangesInSingleFile || false,
params.scm || "git"
)
);
if (isGeneratingPrompt) {
return ""; // 防止循环调用时返回空字符串
}

try {
isGeneratingPrompt = true;
if (params.systemPrompt) {
return params.systemPrompt;
}

// 从 ConfigurationManager 获取完整配置
const config = ConfigurationManager.getInstance().getConfiguration();

// 使用配置和运行时参数构建提示
return generateCommitMessageSystemPrompt({
config,
vcsType: params.scm || "git",
});
} finally {
isGeneratingPrompt = false;
}
}

0 comments on commit 238d03a

Please sign in to comment.