11package cc.unitmesh.llm
22
33import cc.unitmesh.agent.logging.getLogger
4- import cc.unitmesh.agent.subagent.DomainDictAgent
5- import cc.unitmesh.agent.subagent.DomainDictContext
64import cc.unitmesh.devins.filesystem.ProjectFileSystem
75import cc.unitmesh.devins.parser.CodeFence
86import cc.unitmesh.indexer.DomainDictService
97import cc.unitmesh.indexer.template.TemplateEngine
108
11- /* *
12- * Enhancement mode for PromptEnhancer
13- */
14- enum class EnhancementMode {
15- /* * Simple enhancement using existing domain.csv */
16- SIMPLE ,
17- /* * DeepResearch mode: iteratively improve domain.csv using DomainDictAgent */
18- DEEP_RESEARCH
19- }
20-
219/* *
2210 * Prompt Enhancer for improving user prompts using domain knowledge and context
23- *
11+ *
2412 * This is a multiplatform version of the IDEA plugin's PromptEnhancer,
2513 * designed to work across JVM, JS, and other Kotlin targets.
26- *
27- * Supports two enhancement modes:
28- * - SIMPLE: Uses existing domain.csv for quick enhancement
29- * - DEEP_RESEARCH: Uses DomainDictAgent to iteratively improve domain.csv before enhancement
3014 */
3115class PromptEnhancer (
3216 private val llmService : KoogLLMService ,
3317 private val fileSystem : ProjectFileSystem ,
34- private val domainDictService : DomainDictService ? = null ,
35- private val defaultMode : EnhancementMode = EnhancementMode .SIMPLE
18+ private val domainDictService : DomainDictService ? = null
3619) {
3720 private val logger = getLogger(" PromptEnhancer" )
3821 private val templateEngine = TemplateEngine ()
39-
22+
4023 /* *
4124 * Enhance a user prompt with domain knowledge and context
42- *
25+ *
4326 * @param userInput The original user input to enhance
4427 * @param language Language preference ("zh" or "en")
45- * @param mode Enhancement mode (SIMPLE or DEEP_RESEARCH)
46- * @param onProgress Progress callback for DEEP_RESEARCH mode
4728 * @return Enhanced prompt text
4829 */
49- suspend fun enhance (
50- userInput : String ,
51- language : String = "zh",
52- mode : EnhancementMode = defaultMode,
53- onProgress : ((String ) -> Unit )? = null
54- ): String {
30+ suspend fun enhance (userInput : String , language : String = "zh"): String {
5531 if (userInput.isBlank()) {
5632 logger.warn(" Empty user input provided for enhancement" )
5733 return userInput
5834 }
59-
60- return when (mode) {
61- EnhancementMode .SIMPLE -> enhanceSimple(userInput, language)
62- EnhancementMode .DEEP_RESEARCH -> enhanceWithDeepResearch(userInput, language, onProgress ? : {})
63- }
64- }
65-
66- /* *
67- * Simple enhancement using existing domain.csv
68- */
69- private suspend fun enhanceSimple (userInput : String , language : String ): String {
35+
7036 try {
7137 // Build context with available information
7238 val context = buildContext(userInput, language)
73-
39+
7440 // Get enhancement template
7541 val template = getEnhancementTemplate(language)
76-
42+
7743 // Render the enhancement prompt
7844 val enhancementPrompt = templateEngine.render(template, mapOf (" context" to context))
79-
45+
8046 logger.info(" Sending enhancement request to LLM" )
81-
47+
8248 // Call LLM for enhancement
8349 val result = StringBuilder ()
8450 llmService.streamPrompt(
@@ -88,119 +54,35 @@ class PromptEnhancer(
8854 ).collect { chunk ->
8955 result.append(chunk)
9056 }
91-
57+
9258 // Extract enhanced content from LLM response
9359 val enhancedContent = extractEnhancedContent(result.toString())
94-
60+
9561 logger.info(" Successfully enhanced prompt: ${userInput.take(50 )} ... -> ${enhancedContent.take(50 )} ..." )
96-
62+
9763 return enhancedContent.ifEmpty { userInput }
98-
64+
9965 } catch (e: Exception ) {
10066 logger.error(" Failed to enhance prompt: ${e.message} " , e)
10167 return userInput // Return original input on error
10268 }
10369 }
104-
105- /* *
106- * Deep research enhancement using DomainDictAgent
107- *
108- * This mode:
109- * 1. Uses DomainDictAgent to review and iteratively improve domain.csv
110- * 2. Queries codebase for relevant domain terms
111- * 3. Applies the optimized dictionary for final enhancement
112- */
113- private suspend fun enhanceWithDeepResearch (
114- userInput : String ,
115- language : String ,
116- onProgress : (String ) -> Unit
117- ): String {
118- if (domainDictService == null ) {
119- logger.warn(" DomainDictService not available, falling back to simple enhancement" )
120- return enhanceSimple(userInput, language)
121- }
122-
123- try {
124- onProgress(" 🔍 Starting DeepResearch enhancement..." )
125-
126- // Create and execute DomainDictAgent
127- val dictAgent = DomainDictAgent (
128- llmService = llmService,
129- fileSystem = fileSystem,
130- domainDictService = domainDictService,
131- maxDefaultIterations = 3
132- )
133-
134- val dictContext = DomainDictContext (
135- userQuery = userInput,
136- maxIterations = 3 ,
137- focusArea = extractFocusArea(userInput)
138- )
139-
140- onProgress(" 📚 Optimizing domain dictionary for: ${userInput.take(50 )} ..." )
141-
142- // Execute dictionary optimization
143- val dictResult = dictAgent.execute(dictContext, onProgress)
144-
145- if (dictResult.success) {
146- onProgress(" ✅ Dictionary optimization complete" )
147- logger.info(" Dictionary optimization completed: ${dictResult.metadata} " )
148- } else {
149- onProgress(" ⚠️ Dictionary optimization failed, using existing dictionary" )
150- logger.warn(" Dictionary optimization failed: ${dictResult.content} " )
151- }
152-
153- // Now perform simple enhancement with the optimized dictionary
154- onProgress(" 🔄 Applying enhanced dictionary..." )
155- val result = enhanceSimple(userInput, language)
156-
157- onProgress(" ✅ Enhancement complete" )
158- return result
159-
160- } catch (e: Exception ) {
161- logger.error(" DeepResearch enhancement failed: ${e.message} " , e)
162- onProgress(" ❌ DeepResearch failed, falling back to simple enhancement" )
163- return enhanceSimple(userInput, language)
164- }
165- }
166-
167- /* *
168- * Extract focus area from user input for targeted dictionary optimization
169- */
170- private fun extractFocusArea (userInput : String ): String? {
171- val keywords = listOf (
172- " authentication" to " auth" ,
173- " authorization" to " auth" ,
174- " payment" to " payment" ,
175- " agent" to " agent" ,
176- " document" to " document" ,
177- " database" to " database" ,
178- " api" to " api" ,
179- " 认证" to " auth" ,
180- " 支付" to " payment" ,
181- " 代理" to " agent" ,
182- " 文档" to " document"
183- )
184-
185- val lowerInput = userInput.lowercase()
186- return keywords.find { lowerInput.contains(it.first) }?.second
187- }
188-
70+
18971 /* *
19072 * Build enhancement context with available information
19173 */
19274 private suspend fun buildContext (userInput : String , language : String ): PromptEnhancerContext {
19375 val dict = loadDomainDict()
19476 val readme = loadReadmeContent()
195-
77+
19678 return PromptEnhancerContext .full(
19779 userInput = userInput,
19880 dict = dict,
19981 readme = readme,
20082 language = language
20183 )
20284 }
203-
85+
20486 /* *
20587 * Load domain dictionary content
20688 */
@@ -212,7 +94,7 @@ class PromptEnhancer(
21294 " "
21395 }
21496 }
215-
97+
21698 /* *
21799 * Load README file content
218100 */
@@ -224,17 +106,17 @@ class PromptEnhancer(
224106 " "
225107 }
226108 }
227-
109+
228110 /* *
229111 * Find and read README file from project root
230112 */
231113 private suspend fun findAndReadReadme (): String {
232114 val readmeVariations = listOf (
233115 " README.md" , " Readme.md" , " readme.md" ,
234- " README.txt" , " Readme.txt" , " readme.txt" ,
116+ " README.txt" , " Readme.txt" , " readme.txt" ,
235117 " README" , " Readme" , " readme"
236118 )
237-
119+
238120 for (variation in readmeVariations) {
239121 try {
240122 if (fileSystem.exists(variation)) {
@@ -248,10 +130,10 @@ class PromptEnhancer(
248130 continue
249131 }
250132 }
251-
133+
252134 return " "
253135 }
254-
136+
255137 /* *
256138 * Get enhancement template based on language
257139 */
0 commit comments