From 69f86dbc0d6a6c1ca0d190298b364d33a109ca7b Mon Sep 17 00:00:00 2001 From: Roe-xin Date: Fri, 20 Mar 2026 15:19:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=90=AF=E7=94=A8/?= =?UTF-8?q?=E7=A6=81=E7=94=A8=E4=B8=AA=E4=BA=BA=E8=A7=84=E5=88=99=E5=A4=B1?= =?UTF-8?q?=E6=95=88=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/panels/helpers/messageRouter.ts | 47 +++++++++---- src/utils/personalRulesManager.ts | 103 ++++++++++++++++++++-------- src/views/rulesSettingsComponent.ts | 6 ++ 3 files changed, 116 insertions(+), 40 deletions(-) diff --git a/src/panels/helpers/messageRouter.ts b/src/panels/helpers/messageRouter.ts index d5f646c..98f8071 100644 --- a/src/panels/helpers/messageRouter.ts +++ b/src/panels/helpers/messageRouter.ts @@ -27,11 +27,15 @@ import { savePersonalRule, updatePersonalRule, deletePersonalRule, + updatePersonalRulesEnabled, } from "../../utils/personalRulesManager"; import { compactDialog } from "../../services/apiClient"; import { ChatHistoryManager } from "../../utils/chatHistoryManager"; import { getCachedUserInfo } from "../../services/userService"; -import { loadConversationHistory, selectConversation } from "./conversationHelper"; +import { + loadConversationHistory, + selectConversation, +} from "./conversationHelper"; import { getVCDFileInfo } from "./vcdHelper"; import { handleAddContextFile, @@ -138,16 +142,16 @@ export async function handleWebviewMessage( break; case "loadConversationHistory": - loadConversationHistory( - panel, - message.offset || 0, - message.limit || 10, - ); + loadConversationHistory(panel, message.offset || 0, message.limit || 10); break; case "selectConversation": if (message.conversationId) { - selectConversation(panel, message.conversationId, context.extensionPath); + selectConversation( + panel, + message.conversationId, + context.extensionPath, + ); } break; @@ -261,7 +265,9 @@ export async function handleWebviewMessage( verified: true, }); } else { - const { InvitationService } = require("../../services/invitationService"); + const { + InvitationService, + } = require("../../services/invitationService"); const isVerified = await InvitationService.isVerified(context); panel.webview.postMessage({ command: "invitationCodeStatus", @@ -292,7 +298,9 @@ export async function handleWebviewMessage( case "checkTrialExpiration": { - const { TrialExpirationService } = require("../../services/trialExpirationService"); + const { + TrialExpirationService, + } = require("../../services/trialExpirationService"); const trialService = new TrialExpirationService(context, panel); await trialService.checkExpiration(); } @@ -300,7 +308,9 @@ export async function handleWebviewMessage( case "verifyInvitationCode": { - const { InvitationService } = require("../../services/invitationService"); + const { + InvitationService, + } = require("../../services/invitationService"); const result = await InvitationService.verifyCode(message.code); if (result.success) { @@ -390,7 +400,7 @@ export async function handleWebviewMessage( case "openContextSettings": panel.webview.postMessage({ command: "openSettingsTab", - tab: "context" + tab: "context", }); break; @@ -401,7 +411,7 @@ export async function handleWebviewMessage( canSelectFolders: false, openLabel: "选择文件", filters: { - "支持的文件": ["md", "txt", "v", "sv", "pdf"], + 支持的文件: ["md", "txt", "v", "sv", "pdf"], }, }); @@ -557,6 +567,19 @@ export async function handleWebviewMessage( } break; + case "updatePersonalRulesEnabled": + { + const success = await updatePersonalRulesEnabled(message.enabled); + if (success) { + const data = loadPersonalRules(); + panel.webview.postMessage({ + command: "personalRulesLoaded", + data: data, + }); + } + } + break; + case "loadDocumentSets": const { getDocumentSets } = await import("./contextHelper"); panel.webview.postMessage({ diff --git a/src/utils/personalRulesManager.ts b/src/utils/personalRulesManager.ts index c1d29a8..deda980 100644 --- a/src/utils/personalRulesManager.ts +++ b/src/utils/personalRulesManager.ts @@ -5,16 +5,16 @@ * 使用场景:保存和加载用户的个人规则 */ -import * as vscode from 'vscode'; -import * as fs from 'fs'; -import * as path from 'path'; -import * as os from 'os'; +import * as vscode from "vscode"; +import * as fs from "fs"; +import * as path from "path"; +import * as os from "os"; /** * 获取规则目录路径 */ function getRulesDir(): string { - return path.join(os.homedir(), '.iccoder', 'rules'); + return path.join(os.homedir(), ".iccoder", "rules"); } /** @@ -31,18 +31,22 @@ function ensureRulesDir(): void { * 从文件内容中提取规则名称 */ function extractRuleName(content: string): string { - const lines = content.split('\n'); + const lines = content.split("\n"); const firstLine = lines[0]?.trim(); - if (firstLine && firstLine.startsWith('# ')) { + if (firstLine && firstLine.startsWith("# ")) { return firstLine.substring(2).trim(); } - return content.substring(0, 30) + (content.length > 30 ? '...' : ''); + return content.substring(0, 30) + (content.length > 30 ? "..." : ""); } /** * 保存新规则 */ -export async function savePersonalRule(name: string, content: string, enabled: boolean): Promise { +export async function savePersonalRule( + name: string, + content: string, + enabled: boolean, +): Promise { try { ensureRulesDir(); @@ -51,11 +55,17 @@ export async function savePersonalRule(name: string, content: string, enabled: b const filePath = path.join(getRulesDir(), filename); const fileContent = `# ${name}\n\n${content}`; - fs.writeFileSync(filePath, fileContent, 'utf-8'); + fs.writeFileSync(filePath, fileContent, "utf-8"); - await vscode.workspace.getConfiguration('ic-coder').update('personalRulesEnabled', enabled, vscode.ConfigurationTarget.Global); + await vscode.workspace + .getConfiguration("ic-coder") + .update( + "personalRulesEnabled", + enabled, + vscode.ConfigurationTarget.Global, + ); - vscode.window.showInformationMessage('规则已保存'); + vscode.window.showInformationMessage("规则已保存"); return true; } catch (error) { vscode.window.showErrorMessage(`保存规则失败: ${error}`); @@ -66,15 +76,26 @@ export async function savePersonalRule(name: string, content: string, enabled: b /** * 更新规则 */ -export async function updatePersonalRule(filename: string, name: string, content: string, enabled: boolean): Promise { +export async function updatePersonalRule( + filename: string, + name: string, + content: string, + enabled: boolean, +): Promise { try { const filePath = path.join(getRulesDir(), filename); const fileContent = `# ${name}\n\n${content}`; - fs.writeFileSync(filePath, fileContent, 'utf-8'); + fs.writeFileSync(filePath, fileContent, "utf-8"); - await vscode.workspace.getConfiguration('ic-coder').update('personalRulesEnabled', enabled, vscode.ConfigurationTarget.Global); + await vscode.workspace + .getConfiguration("ic-coder") + .update( + "personalRulesEnabled", + enabled, + vscode.ConfigurationTarget.Global, + ); - vscode.window.showInformationMessage('规则已更新'); + vscode.window.showInformationMessage("规则已更新"); return true; } catch (error) { vscode.window.showErrorMessage(`更新规则失败: ${error}`); @@ -90,7 +111,7 @@ export async function deletePersonalRule(filename: string): Promise { const filePath = path.join(getRulesDir(), filename); if (fs.existsSync(filePath)) { fs.unlinkSync(filePath); - vscode.window.showInformationMessage('规则已删除'); + vscode.window.showInformationMessage("规则已删除"); return true; } return false; @@ -103,8 +124,13 @@ export async function deletePersonalRule(filename: string): Promise { /** * 加载所有规则 */ -export function loadPersonalRules(): { rules: Array<{ filename: string; name: string; content: string }>; enabled: boolean } { - const enabled = vscode.workspace.getConfiguration('ic-coder').get('personalRulesEnabled', true); +export function loadPersonalRules(): { + rules: Array<{ filename: string; name: string; content: string }>; + enabled: boolean; +} { + const enabled = vscode.workspace + .getConfiguration("ic-coder") + .get("personalRulesEnabled", true); const dir = getRulesDir(); if (!fs.existsSync(dir)) { @@ -112,16 +138,16 @@ export function loadPersonalRules(): { rules: Array<{ filename: string; name: st } try { - const files = fs.readdirSync(dir).filter(f => f.endsWith('.md')); - const rules = files.map(filename => { - const content = fs.readFileSync(path.join(dir, filename), 'utf-8'); - const lines = content.split('\n'); - let name = ''; + const files = fs.readdirSync(dir).filter((f) => f.endsWith(".md")); + const rules = files.map((filename) => { + const content = fs.readFileSync(path.join(dir, filename), "utf-8"); + const lines = content.split("\n"); + let name = ""; let actualContent = content; - if (lines[0]?.trim().startsWith('# ')) { + if (lines[0]?.trim().startsWith("# ")) { name = lines[0].substring(2).trim(); - actualContent = lines.slice(2).join('\n').trim(); + actualContent = lines.slice(2).join("\n").trim(); } else { name = extractRuleName(content); } @@ -130,11 +156,32 @@ export function loadPersonalRules(): { rules: Array<{ filename: string; name: st }); return { rules, enabled }; } catch (error) { - console.error('读取规则失败:', error); + console.error("读取规则失败:", error); return { rules: [], enabled }; } } +/** + * 更新个人规则启用状态 + */ +export async function updatePersonalRulesEnabled( + enabled: boolean, +): Promise { + try { + await vscode.workspace + .getConfiguration("ic-coder") + .update( + "personalRulesEnabled", + enabled, + vscode.ConfigurationTarget.Global, + ); + return true; + } catch (error) { + console.error("更新规则启用状态失败:", error); + return false; + } +} + /** * 获取当前生效的所有规则内容 */ @@ -143,5 +190,5 @@ export function getActiveRules(): string | null { if (!enabled || rules.length === 0) { return null; } - return rules.map(r => r.content).join('\n\n'); + return rules.map((r) => r.content).join("\n\n"); } diff --git a/src/views/rulesSettingsComponent.ts b/src/views/rulesSettingsComponent.ts index 564ae90..cfb1a20 100644 --- a/src/views/rulesSettingsComponent.ts +++ b/src/views/rulesSettingsComponent.ts @@ -400,6 +400,12 @@ export function getRulesSettingsComponentScript(): string { } } + //监听启用个人规则开关变化 + document.getElementById('enablePersonalRulesCheckbox').addEventListener('change', function() { + const enabled = this.checked; + vscode.postMessage({ command: 'updatePersonalRulesEnabled', enabled: enabled }); + }); + // 页面加载时请求规则数据 vscode.postMessage({ command: 'loadPersonalRules' }); `;