@@ -11,24 +11,33 @@ import (
1111
1212const fileAISearchMaxPathRunes = 240
1313
14- func buildFileAISearchSystemPrompt () string {
15- return strings . Join ( []string {
14+ func buildFileAISearchSystemPrompt (responseLanguage string ) string {
15+ lines := []string {
1616 "You are a file browser assistant for a server panel." ,
1717 "Answer using Markdown (headings, bullet lists)." ,
1818 "Only mention files and directories that appear in the provided inventory. Do not invent paths." ,
1919 "If a \" Content line matches\" section is present, you may reference those lines and numbers only; never invent line numbers or snippets that are not listed there." ,
2020 "If the inventory was truncated or incomplete, say so and suggest narrowing the directory or increasing limits." ,
2121 "Group or rank results by relevance to the user's question when helpful." ,
22- "Prefer responding in the same language as the user's query (e.g. Chinese if the query is in Chinese)." ,
23- }, "\n " )
22+ }
23+ if lang := normalizeFileAISearchLanguage (responseLanguage ); lang != "" {
24+ lines = append (lines , "Respond in " + lang + "." )
25+ } else {
26+ lines = append (lines , "Prefer responding in the same language as the user's query (e.g. Chinese if the query is in Chinese)." )
27+ }
28+ return strings .Join (lines , "\n " )
2429}
2530
26- func buildFileAISearchUserPrompt (root , query string , items []AISearchInventoryItem , truncated , preFiltered bool , contentHits []FileAIContentHit , contentScannedFiles int , contentHitsTruncated bool , matchDesc string , promptHitMaxBytes int ) string {
31+ func buildFileAISearchUserPrompt (root , query , responseLanguage string , items []AISearchInventoryItem , truncated , preFiltered bool , contentHits []FileAIContentHit , contentScannedFiles int , contentHitsTruncated bool , matchDesc string , promptHitMaxBytes int ) string {
2732 var b strings.Builder
2833 b .WriteString ("User question:\n " )
2934 b .WriteString (strings .TrimSpace (query ))
3035 b .WriteString ("\n \n Root directory:\n " )
3136 b .WriteString (root )
37+ if lang := normalizeFileAISearchLanguage (responseLanguage ); lang != "" {
38+ b .WriteString ("\n \n Panel reply language:\n " )
39+ b .WriteString (lang )
40+ }
3241 b .WriteString ("\n \n Inventory notes:\n " )
3342 if truncated {
3443 b .WriteString ("- Listing was truncated; not all files under the root were included.\n " )
@@ -80,7 +89,7 @@ func buildFileAISearchUserPrompt(root, query string, items []AISearchInventoryIt
8089 return b .String ()
8190}
8291
83- func RunFileAISearchLLM (ctx context.Context , cfg terminalai.GeneratorConfig , clientTimeout time.Duration , root , query string , items []AISearchInventoryItem , truncated , preFiltered bool , contentHits []FileAIContentHit , contentScannedFiles int , contentHitsTruncated bool , matchDesc string , promptHitMaxBytes , llmMaxOutputTokens int ) (string , terminalai.ResponseUsage , error ) {
92+ func RunFileAISearchLLM (ctx context.Context , cfg terminalai.GeneratorConfig , clientTimeout time.Duration , root , query , responseLanguage string , items []AISearchInventoryItem , truncated , preFiltered bool , contentHits []FileAIContentHit , contentScannedFiles int , contentHitsTruncated bool , matchDesc string , promptHitMaxBytes , llmMaxOutputTokens int ) (string , terminalai.ResponseUsage , error ) {
8493 timeout := clientTimeout
8594 if timeout <= 0 {
8695 timeout = 2 * time .Minute
@@ -109,8 +118,8 @@ func RunFileAISearchLLM(ctx context.Context, cfg terminalai.GeneratorConfig, cli
109118 }
110119 resp , err := client .ChatCompletion (ctx , terminalai.ChatCompletionRequest {
111120 Messages : []terminalai.ChatMessage {
112- {Role : "system" , Content : buildFileAISearchSystemPrompt ()},
113- {Role : "user" , Content : buildFileAISearchUserPrompt (root , query , items , truncated , preFiltered , contentHits , contentScannedFiles , contentHitsTruncated , matchDesc , promptHitMaxBytes )},
121+ {Role : "system" , Content : buildFileAISearchSystemPrompt (responseLanguage )},
122+ {Role : "user" , Content : buildFileAISearchUserPrompt (root , query , responseLanguage , items , truncated , preFiltered , contentHits , contentScannedFiles , contentHitsTruncated , matchDesc , promptHitMaxBytes )},
114123 },
115124 MaxTokens : outTokens ,
116125 })
@@ -123,3 +132,33 @@ func RunFileAISearchLLM(ctx context.Context, cfg terminalai.GeneratorConfig, cli
123132 }
124133 return summary , resp .Usage , nil
125134}
135+
136+ func normalizeFileAISearchLanguage (lang string ) string {
137+ lang = strings .TrimSpace (strings .ToLower (lang ))
138+ switch {
139+ case lang == "" , lang == "*" :
140+ return "English"
141+ case strings .HasPrefix (lang , "zh-hant" ), strings .HasPrefix (lang , "zh-tw" ), strings .HasPrefix (lang , "zh-hk" ):
142+ return "Traditional Chinese"
143+ case strings .HasPrefix (lang , "zh" ):
144+ return "Simplified Chinese"
145+ case strings .HasPrefix (lang , "en" ):
146+ return "English"
147+ case strings .HasPrefix (lang , "ja" ):
148+ return "Japanese"
149+ case strings .HasPrefix (lang , "ko" ):
150+ return "Korean"
151+ case strings .HasPrefix (lang , "ru" ):
152+ return "Russian"
153+ case strings .HasPrefix (lang , "ms" ):
154+ return "Malay"
155+ case strings .HasPrefix (lang , "tr" ):
156+ return "Turkish"
157+ case strings .HasPrefix (lang , "pt-br" ):
158+ return "Brazilian Portuguese"
159+ case strings .HasPrefix (lang , "es" ):
160+ return "Spanish"
161+ default :
162+ return lang
163+ }
164+ }
0 commit comments