From 2544094f50648af88f16a877e9350f4e362ac3e1 Mon Sep 17 00:00:00 2001 From: Guodong Lu <1015455325@qq.com> Date: Wed, 18 Jun 2025 10:49:21 +0800 Subject: [PATCH 1/3] Updated .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 50ba24aaa..1abe0fe09 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ data.json # Test Environment .env.test +.env From 73b58b105702222f5845e6550e352cb76858b714 Mon Sep 17 00:00:00 2001 From: Guodong Lu <1015455325@qq.com> Date: Wed, 18 Jun 2025 10:50:19 +0800 Subject: [PATCH 2/3] Updated package-lock.json --- package-lock.json | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 066a9840e..fa3e1ef86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15483,9 +15483,10 @@ } }, "node_modules/lilconfig": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-3.1.2.tgz", - "integrity": "sha512-eop+wDAvpItUys0FWkHIKeC9ybYrTGbU41U5K7+bttZZeohvnY7M9dZ5kB21GNWiFT2q1OoPTvncPCgSOVO5ow==", + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-3.1.3.tgz", + "integrity": "sha512-/vlFKAoH5Cgt3Ie+JLhRbwOsCQePABiU3tJ1egGvyQ+33R/vcwM2Zl2QR/LzjsBeItPt3oSVXapn+m4nQDvpzw==", + "license": "MIT", "engines": { "node": ">=14" }, @@ -20167,9 +20168,10 @@ } }, "node_modules/tailwindcss": { - "version": "3.4.15", - "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.15.tgz", - "integrity": "sha512-r4MeXnfBmSOuKUWmXe6h2CcyfzJCEk4F0pptO5jlnYSIViUkVmsawj80N5h2lO3gwcmSb4n3PuN+e+GC1Guylw==", + "version": "3.4.17", + "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.17.tgz", + "integrity": "sha512-w33E2aCvSDP0tW9RZuNXadXlkHXqFzSkQew/aIa2i/Sj8fThxwovwlXHSPXTbAHwEIhBFXAedUhP2tueAKP8Og==", + "license": "MIT", "dependencies": { "@alloc/quick-lru": "^5.2.0", "arg": "^5.0.2", @@ -20180,7 +20182,7 @@ "glob-parent": "^6.0.2", "is-glob": "^4.0.3", "jiti": "^1.21.6", - "lilconfig": "^2.1.0", + "lilconfig": "^3.1.3", "micromatch": "^4.0.8", "normalize-path": "^3.0.0", "object-hash": "^3.0.0", @@ -20210,14 +20212,6 @@ "tailwindcss": ">=3.0.0 || insiders" } }, - "node_modules/tailwindcss/node_modules/lilconfig": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-2.1.0.tgz", - "integrity": "sha512-utWOt/GHzuUxnLKxB6dk81RoOeoNeHgbrXiuGk4yyF5qlRz+iIVWu56E2fqGHFrXz0QNUhLB/8nKqvRH66JKGQ==", - "engines": { - "node": ">=10" - } - }, "node_modules/tailwindcss/node_modules/postcss": { "version": "8.4.49", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.49.tgz", From 69e92072c5bf257da0c26a3b7b0f0c3c930c22cb Mon Sep 17 00:00:00 2001 From: lgd-matlab <69723265+lgd-matlab@users.noreply.github.com> Date: Sat, 12 Jul 2025 23:04:40 +0800 Subject: [PATCH 3/3] feat: Add prompt enhancement feature and English UI localization - Add custom prompt enhancement instruction functionality - Implement prompt optimization with template support - Convert Chinese UI text to English in Advanced Settings - Support placeholders for prompt, history, and context - Add promptEnhancer.ts for prompt processing logic - Fix lint errors: remove unused import and use const --- src/components/Chat.tsx | 2 + src/components/chat-components/ChatInput.tsx | 96 ++++ src/constants.ts | 43 ++ src/promptEnhancer.ts | 416 ++++++++++++++++++ src/settings/model.ts | 1 + .../v2/components/AdvancedSettings.tsx | 33 ++ 6 files changed, 591 insertions(+) create mode 100644 src/promptEnhancer.ts diff --git a/src/components/Chat.tsx b/src/components/Chat.tsx index 4a65b8104..d83ff42fc 100644 --- a/src/components/Chat.tsx +++ b/src/components/Chat.tsx @@ -700,6 +700,8 @@ ${chatContent}`; onAddImage={(files: File[]) => setSelectedImages((prev) => [...prev, ...files])} setSelectedImages={setSelectedImages} disableModelSwitch={selectedChain === ChainType.PROJECT_CHAIN} + chatHistory={chatHistory} + chainManager={chainManager} /> diff --git a/src/components/chat-components/ChatInput.tsx b/src/components/chat-components/ChatInput.tsx index 1757be278..34304fdac 100644 --- a/src/components/chat-components/ChatInput.tsx +++ b/src/components/chat-components/ChatInput.tsx @@ -23,8 +23,12 @@ import { CustomPromptProcessor } from "@/customPromptProcessor"; import { COPILOT_TOOL_NAMES } from "@/LLMProviders/intentAnalyzer"; import { Mention } from "@/mentions/Mention"; import { getModelKeyFromModel, useSettingsValue } from "@/settings/model"; +import { ChatMessage } from "@/sharedState"; import { getToolDescription } from "@/tools/toolManager"; import { checkModelApiKey, err2String, extractNoteFiles, isNoteTitleUnique } from "@/utils"; +import { enhancePrompt } from "@/promptEnhancer"; +import { getSettings } from "@/settings/model"; +import ChainManager from "@/LLMProviders/chainManager"; import { ArrowBigUp, ChevronDown, @@ -32,9 +36,11 @@ import { CornerDownLeft, Image, Loader2, + Sparkles, StopCircle, X, } from "lucide-react"; + import { App, Notice, Platform, TFile } from "obsidian"; import React, { forwardRef, @@ -68,6 +74,8 @@ interface ChatInputProps { onAddImage: (files: File[]) => void; setSelectedImages: React.Dispatch>; disableModelSwitch?: boolean; + chatHistory?: ChatMessage[]; + chainManager?: ChainManager; } const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>( @@ -88,6 +96,8 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>( onAddImage, setSelectedImages, disableModelSwitch, + chatHistory = [], + chainManager, }, ref ) => { @@ -107,6 +117,7 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>( const isCopilotPlus = currentChain === ChainType.COPILOT_PLUS_CHAIN || currentChain === ChainType.PROJECT_CHAIN; const [loadingMessageIndex, setLoadingMessageIndex] = useState(0); + const [isEnhancing, setIsEnhancing] = useState(false); const loadingMessages = [ "Loading the project context...", "Processing context files...", @@ -169,6 +180,76 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>( }); }; + const handleEnhancePrompt = async () => { + if (!inputMessage.trim()) { + new Notice("请先输入提示词"); + return; + } + + if (isEnhancing) { + return; + } + + setIsEnhancing(true); + + try { + // 获取设置 + const settings = getSettings(); + + // 收集添加的上下文 + let addedContext = ""; + + // 添加上下文笔记内容 + if (contextNotes.length > 0) { + const contextContents = await Promise.all( + contextNotes.map(async (note) => { + const content = await app.vault.read(note); + return `【${note.basename}】\n${content}`; + }) + ); + addedContext += contextContents.join("\n\n"); + } + + // 添加当前活动笔记内容(如果启用) + if (includeActiveNote && currentActiveNote) { + const activeNoteContent = await app.vault.read(currentActiveNote); + if (addedContext) addedContext += "\n\n"; + addedContext += `【当前笔记:${currentActiveNote.basename}】\n${activeNoteContent}`; + } + + // 添加 URL 上下文 + if (contextUrls.length > 0) { + if (addedContext) addedContext += "\n\n"; + addedContext += `【相关链接】\n${contextUrls.join("\n")}`; + } + + // 调用增强功能 + if (!chainManager) { + throw new Error("ChainManager 未初始化"); + } + + const result = await enhancePrompt({ + originalPrompt: inputMessage, + chatHistory: chatHistory, + addedContext: addedContext || "(无额外上下文)", + chainManager: chainManager, + customInstructionTemplate: settings.promptEnhancementTemplate, + }); + + if (result.success) { + setInputMessage(result.enhancedPrompt); + new Notice("提示词已成功优化!"); + } else { + throw new Error(result.error || "增强失败"); + } + } catch (error) { + console.error("Prompt enhancement failed:", error); + new Notice(`优化失败: ${error instanceof Error ? error.message : "未知错误"}`); + } finally { + setIsEnhancing(false); + } + }; + const handleInputChange = async (event: React.ChangeEvent) => { const inputValue = event.target.value; const cursorPos = event.target.selectionStart; @@ -590,6 +671,21 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>( ) : ( <> + {/* 增强按钮 */} + + {isCopilotPlus && (