Skip to content

Commit

Permalink
🔧 chore(config): 修改智谱 AI 相关配置命名
Browse files Browse the repository at this point in the history
- 更新配置文件中智谱 AI 相关命名 (zhipuai -> zhipu)
- 修复错误提示信息显示逻辑,增加超时参数
- 优化周报生成时的 UI 提示,添加作者和时间段信息
- 重构更新模型配置的逻辑,减少不必要的请求
- 改进 SVN 作者信息获取逻辑,支持带端口的 URL 匹配
- 删除不必要的调试日志
  • Loading branch information
littleCareless committed Dec 16, 2024
1 parent 7967df6 commit 3773623
Show file tree
Hide file tree
Showing 21 changed files with 212 additions and 130 deletions.
2 changes: 1 addition & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"ai.model.loading": "Loading AI model list...",
"weeklyReport.generating": "Generating weekly report...",
"weeklyReport.empty.response": "AI generated content is empty",
"weeklyReport.generation.success": "Weekly report generated successfully",
"weeklyReport.generation.success": "Weekly report generated successfully: : {0} {1}",
"weeklyReport.generation.failed": "Failed to generate weekly report: {0}",
"weeklyReport.copy.success": "Content copied to clipboard",
"weeklyReport.copy.failed": "Copy failed: {0}",
Expand Down
2 changes: 1 addition & 1 deletion i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"ai.model.loading": "正在加载 AI 模型列表...",
"weeklyReport.generating": "正在生成周报...",
"weeklyReport.empty.response": "AI 生成内容为空",
"weeklyReport.generation.success": "周报生成成功",
"weeklyReport.generation.success": "周报生成成功: {0} {1}",
"weeklyReport.generation.failed": "生成周报失败: {0}",
"weeklyReport.copy.success": "内容已复制到剪贴板",
"weeklyReport.copy.failed": "复制失败: {0}",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"OpenAI",
"Ollama",
"VS Code Provided",
"ZhipuAI",
"Zhipu",
"DashScope",
"Doubao",
"Gemini"
Expand All @@ -149,7 +149,7 @@
"default": "https://api.openai.com/v1",
"description": "OpenAI API 基础 URL"
},
"dish-ai-commit.providers.zhipuai.apiKey": {
"dish-ai-commit.providers.zhipu.apiKey": {
"type": "string",
"default": "",
"description": "智谱 AI API 密钥"
Expand Down 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 @@ -47,7 +47,7 @@ export class AIProviderFactory {
case AIProvider.VS_CODE_PROVIDED:
provider = new VSCodeProvider();
break;
case AIProvider.ZHIPUAI:
case AIProvider.ZHIPU:
provider = new ZhipuAIProvider();
break;
case AIProvider.DASHSCOPE:
Expand Down
1 change: 0 additions & 1 deletion src/ai/providers/VscodeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ export class VSCodeProvider implements AIProvider {
async getModels(): Promise<AIModel[]> {
try {
const models = await vscode.lm.selectChatModels();
console.log("model", models);
if (models.length > 0) {
return Promise.resolve(
models.map((model) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/ai/providers/ZhipuAIProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ZhipuAIProvider extends BaseOpenAIProvider {
constructor() {
const configManager = ConfigurationManager.getInstance();
super({
apiKey: configManager.getConfig("PROVIDERS_ZHIPUAI_APIKEY"),
apiKey: configManager.getConfig("PROVIDERS_ZHIPU_APIKEY"),
baseURL: "https://open.bigmodel.cn/api/paas/v4/",
providerId: "zhipu",
providerName: "zhipu",
Expand Down
4 changes: 4 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class CommandManager implements vscode.Disposable {
} catch (error) {
NotificationHandler.error(
"command.generate.failed",
3000,
error instanceof Error ? error.message : String(error)
);
}
Expand All @@ -38,6 +39,7 @@ export class CommandManager implements vscode.Disposable {
} catch (error) {
NotificationHandler.error(
"command.select.model.failed",
3000,
error instanceof Error ? error.message : String(error)
);
}
Expand All @@ -50,6 +52,7 @@ export class CommandManager implements vscode.Disposable {
} catch (error) {
NotificationHandler.error(
"command.weekly.report.failed",
3000,
error instanceof Error ? error.message : String(error)
);
}
Expand All @@ -59,6 +62,7 @@ export class CommandManager implements vscode.Disposable {
} catch (error) {
NotificationHandler.error(
"command.register.failed",
3000,
error instanceof Error ? error.message : String(error)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class BaseCommand {

protected async validateConfig(): Promise<boolean> {
// if (!(await ConfigurationManager.getInstance().validateConfiguration())) {
// await NotificationHandler.error("command.execution.failed");
// NotificationHandler.error("command.execution.failed");
// return false;
// }
return true;
Expand All @@ -24,7 +24,7 @@ export abstract class BaseCommand {
): Promise<void> {
console.error(errorMessage, error);
if (error instanceof Error) {
await NotificationHandler.error(
NotificationHandler.error(
LocalizationManager.getInstance().format(errorMessage, error.message)
);
}
Expand Down
18 changes: 8 additions & 10 deletions src/commands/GenerateCommitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ export class GenerateCommitCommand extends BaseCommand {
try {
const scmProvider = await SCMFactory.detectSCM();
if (!scmProvider) {
await NotificationHandler.error(
locManager.getMessage("scm.not.detected")
);
NotificationHandler.error(locManager.getMessage("scm.not.detected"));
return;
}

Expand Down Expand Up @@ -205,7 +203,7 @@ export class GenerateCommitCommand extends BaseCommand {

const diffContent = await scmProvider.getDiff(selectedFiles);
if (!diffContent) {
await NotificationHandler.info(locManager.getMessage("no.changes"));
NotificationHandler.info(locManager.getMessage("no.changes"));
throw new Error(locManager.getMessage("no.changes"));
}
const {
Expand All @@ -229,14 +227,14 @@ export class GenerateCommitCommand extends BaseCommand {
}
);
if (!response) {
return await NotificationHandler.info(
return NotificationHandler.info(
locManager.getMessage("no.commit.message.generated")
);
}
if (response?.content) {
try {
await scmProvider.setCommitInput(response.content);
await NotificationHandler.info(
NotificationHandler.info(
locManager.format(
"commit.message.generated",
scmProvider.type.toUpperCase(),
Expand All @@ -246,18 +244,18 @@ export class GenerateCommitCommand extends BaseCommand {
);
} catch (error) {
if (error instanceof Error) {
await NotificationHandler.error(
NotificationHandler.error(
locManager.format("commit.message.write.failed", error.message)
);
try {
// 如果写入失败,尝试复制到剪贴板
vscode.env.clipboard.writeText(response.content);
await NotificationHandler.info(
NotificationHandler.info(
locManager.getMessage("commit.message.copied")
);
} catch (error) {
if (error instanceof Error) {
await NotificationHandler.error(
NotificationHandler.error(
locManager.format("commit.message.copy.failed", error.message)
);
// 如果复制失败,提示用户手动复制 并将信息显示在消息框
Expand All @@ -273,7 +271,7 @@ export class GenerateCommitCommand extends BaseCommand {
} catch (error) {
console.log("error", error);
if (error instanceof Error) {
await NotificationHandler.error(
NotificationHandler.error(
locManager.format("generate.commit.failed", error.message)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/SelectModelCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export class SelectModelCommand extends BaseCommand {
modelSelection.provider,
modelSelection.model
);
await NotificationHandler.info(
NotificationHandler.info(
"model.update.success",
3000,
modelSelection.provider,
modelSelection.model
);
Expand Down
4 changes: 2 additions & 2 deletions src/config/ConfigSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const CONFIG_SCHEMA = {
"OpenAI",
"Ollama",
"VS Code Provided",
"ZhipuAI",
"Zhipu",
"DashScope",
"Doubao",
"Gemini",
Expand Down Expand Up @@ -90,7 +90,7 @@ export const CONFIG_SCHEMA = {
description: "OpenAI API 基础 URL",
},
},
zhipuai: {
zhipu: {
apiKey: {
type: "string",
default: "",
Expand Down
1 change: 0 additions & 1 deletion src/config/ConfigurationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export class ConfigurationManager {
): K extends keyof ConfigurationValueType
? ConfigurationValueType[K]
: string {
console.log("获取配置项:", key, ConfigKeys);
const configKey = ConfigKeys[key].replace("dish-ai-commit.", "");
const value = this.configuration.get<string>(configKey);
return value as K extends keyof ConfigurationValueType
Expand Down
4 changes: 2 additions & 2 deletions src/config/generated/configKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const CONFIG_KEYS = {
"PROVIDERS_OPENAI": "dish-ai-commit.providers.openai",
"PROVIDERS_OPENAI_APIKEY": "dish-ai-commit.providers.openai.apiKey",
"PROVIDERS_OPENAI_BASEURL": "dish-ai-commit.providers.openai.baseUrl",
"PROVIDERS_ZHIPUAI": "dish-ai-commit.providers.zhipuai",
"PROVIDERS_ZHIPUAI_APIKEY": "dish-ai-commit.providers.zhipuai.apiKey",
"PROVIDERS_ZHIPU": "dish-ai-commit.providers.zhipu",
"PROVIDERS_ZHIPU_APIKEY": "dish-ai-commit.providers.zhipu.apiKey",
"PROVIDERS_DASHSCOPE": "dish-ai-commit.providers.dashscope",
"PROVIDERS_DASHSCOPE_APIKEY": "dish-ai-commit.providers.dashscope.apiKey",
"PROVIDERS_DOUBAO": "dish-ai-commit.providers.doubao",
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function activate(context: vscode.ExtensionContext) {
// 添加用户可见的错误提示
NotificationHandler.error(
"extension.activation.failed",
3000,
e instanceof Error ? e.message : String(e)
);
throw e;
Expand Down
27 changes: 16 additions & 11 deletions src/scm/AuthorService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as vscode from 'vscode';
import { promisify } from 'util';
import { exec } from 'child_process';
import { SvnUtils } from './SvnUtils';
import { LocalizationManager } from '../utils/LocalizationManager';
import * as vscode from "vscode";
import { promisify } from "util";
import { exec } from "child_process";
import { SvnUtils } from "./SvnUtils";
import { LocalizationManager } from "../utils/LocalizationManager";

const execAsync = promisify(exec);

Expand All @@ -22,24 +22,29 @@ export class AuthorService {
}

private async getSvnAuthor(): Promise<string> {
const author = await SvnUtils.getSvnAuthorFromInfo(this.workspacePath)
|| await SvnUtils.getSvnAuthorFromAuth(this.workspacePath)
|| await this.promptForAuthor();

// await SvnUtils.getSvnAuthorFromInfo(this.workspacePath) ||
console.log(
"getSvnAuthorFromAuth",
await SvnUtils.getSvnAuthorFromAuth(this.workspacePath)
);
const author =
(await SvnUtils.getSvnAuthorFromAuth(this.workspacePath)) ||
(await this.promptForAuthor());

if (!author) {
throw new Error(
LocalizationManager.getInstance().getMessage("author.svn.not.found")
);
}

return author;
}

private async promptForAuthor(): Promise<string | undefined> {
const locManager = LocalizationManager.getInstance();
return vscode.window.showInputBox({
prompt: locManager.getMessage("author.manual.input.prompt"),
placeHolder: locManager.getMessage("author.manual.input.placeholder")
placeHolder: locManager.getMessage("author.manual.input.placeholder"),
});
}
}
4 changes: 4 additions & 0 deletions src/scm/CommitLogStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export class GitCommitStrategy implements CommitLogStrategy {
author: string
): Promise<string[]> {
const command = `git log --since="${period}" --pretty=format:"%h - %an, %ar : %s" --author="${author}"`;

console.log("command", command);
const { stdout } = await execAsync(command, { cwd: workspacePath });
return stdout.split("\n").filter((line) => line.trim());
}
Expand All @@ -33,6 +35,8 @@ export class SvnCommitStrategy implements CommitLogStrategy {
const { startDate, endDate } = DateUtils.getDateRangeFromPeriod(period);
const command = `svn log -r "{${startDate.toISOString()}}:{${endDate.toISOString()}}" --search="${author}" --xml`;

console.log("command", command);

const { stdout } = await execAsync(command, { cwd: workspacePath });
return this.parseXmlLogs(stdout);
}
Expand Down
35 changes: 29 additions & 6 deletions src/scm/SvnUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ export class SvnUtils {
urlOutput: string
): string | undefined {
const credentials = this.parseCredentials(authOutput);
console.log("credentials", credentials);
console.log("urlOutput", urlOutput);
const urlMatch = urlOutput.match(/URL: (.+)/);
console.log("urlMatch", urlMatch);

if (urlMatch) {
const repoUrl = urlMatch[1].trim();
const matchingCred = this.findMatchingCredential(credentials, repoUrl);
console.log("matchingCred", matchingCred);
if (matchingCred) {
return matchingCred;
}
Expand All @@ -101,25 +105,44 @@ export class SvnUtils {

private static parseCredentials(authOutput: string) {
return authOutput
.split(
/\n?------------------------------------------------------------------------\n/
)
.split(/\n?-+\n/)
.filter((block) => block.trim())
.map((block) => {
const usernameMatch = block.match(/Username: (.+)/);
const realmMatch = block.match(/: <([^>]+)>/);
const realmMatch = block.match(/: <([^>]+)>\s*([^\n]*)/);
return {
username: usernameMatch?.[1]?.trim() || null,
realm: realmMatch?.[1]?.trim() || null,
repoId: realmMatch?.[2]?.trim() || null,
};
})
.filter((cred) => cred.username && cred.realm);
}

private static findMatchingCredential(credentials: any[], repoUrl: string) {
const matchingCred = credentials.find(
(cred) => cred.realm && repoUrl.includes(cred.realm.split(":")[0])
// 首先尝试完全匹配
let matchingCred = credentials.find(
(cred) => cred.realm && repoUrl.startsWith(cred.realm)
);

// 如果没有完全匹配,尝试匹配主机名部分(忽略端口)
if (!matchingCred) {
const repoHost = this.extractHostWithoutPort(repoUrl);
matchingCred = credentials.find((cred) => {
const credHost = this.extractHostWithoutPort(cred.realm);
return cred.realm && credHost === repoHost;
});
}

return matchingCred?.username;
}

private static extractHostWithoutPort(url: string): string {
try {
const parsedUrl = new URL(url);
return parsedUrl.hostname;
} catch {
return '';
}
}
}
2 changes: 1 addition & 1 deletion src/services/ModelPickerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ModelPickerService {
return undefined;
} catch (error) {
console.error("获取模型列表失败:", error);
await NotificationHandler.error("model.list.failed");
NotificationHandler.error("model.list.failed");
return undefined;
}
}
Expand Down
Loading

0 comments on commit 3773623

Please sign in to comment.