|
5 | 5 | [clojure.string :as string] |
6 | 6 | [eca.features.tools.mcp :as f.mcp] |
7 | 7 | [eca.logger :as logger] |
8 | | - [eca.shared :refer [multi-str] :as shared]) |
| 8 | + [eca.shared :refer [multi-str] :as shared] |
| 9 | + [selmer.parser :as selmer]) |
9 | 10 | (:import |
10 | 11 | [java.util Map])) |
11 | 12 |
|
|
32 | 33 |
|
33 | 34 | (def ^:private compact-prompt-template (memoize compact-prompt-template*)) |
34 | 35 |
|
35 | | -(defn ^:private replace-vars [s vars] |
36 | | - (reduce |
37 | | - (fn [p [k v]] |
38 | | - (string/replace p (str "{{" (name k) "}}") (str v))) |
39 | | - s |
40 | | - vars)) |
41 | | - |
42 | 36 | (defn ^:private eca-chat-prompt [behavior config] |
43 | 37 | (let [behavior-config (get-in config [:behavior behavior]) |
44 | 38 | prompt (:systemPrompt behavior-config) |
|
91 | 85 | (str "\n<additionalContext from=\"chatStart\">\n" startup-ctx "\n</additionalContext>\n\n")) |
92 | 86 | "</contexts>")) |
93 | 87 |
|
94 | | -(defn build-chat-instructions [refined-contexts rules repo-map* behavior config chat-id db] |
95 | | - (multi-str |
96 | | - (eca-chat-prompt behavior config) |
97 | | - (when (seq rules) |
98 | | - ["## Rules" |
99 | | - "" |
100 | | - "<rules description=\"Rules defined by user\">\n" |
101 | | - (reduce |
102 | | - (fn [rule-str {:keys [name content]}] |
103 | | - (str rule-str (format "<rule name=\"%s\">%s</rule>\n" name content))) |
104 | | - "" |
105 | | - rules) |
106 | | - "</rules>"]) |
107 | | - "" |
108 | | - (when (seq refined-contexts) |
109 | | - ["## Contexts" |
110 | | - "" |
111 | | - (contexts-str refined-contexts repo-map* (get-in db [:chats chat-id :startup-context]))]) |
112 | | - "" |
113 | | - (replace-vars |
114 | | - (load-builtin-prompt "additional_system_info.md") |
115 | | - {:workspaceRoots (shared/workspaces-as-str db)}))) |
116 | | - |
117 | | -(defn build-rewrite-instructions [text path full-text range config] |
| 88 | +(defn ^:private ->base-selmer-ctx [all-tools db] |
| 89 | + (merge |
| 90 | + {:workspaceRoots (shared/workspaces-as-str db)} |
| 91 | + (reduce |
| 92 | + (fn [m tool] |
| 93 | + (assoc m (keyword (str "toolEnabled_" (:full-name tool))) true)) |
| 94 | + {} |
| 95 | + all-tools))) |
| 96 | + |
| 97 | +(defn build-chat-instructions [refined-contexts rules repo-map* behavior config chat-id all-tools db] |
| 98 | + (let [selmer-ctx (->base-selmer-ctx all-tools db)] |
| 99 | + (multi-str |
| 100 | + (selmer/render (eca-chat-prompt behavior config) selmer-ctx) |
| 101 | + (when (seq rules) |
| 102 | + ["## Rules" |
| 103 | + "" |
| 104 | + "<rules description=\"Rules defined by user\">\n" |
| 105 | + (reduce |
| 106 | + (fn [rule-str {:keys [name content]}] |
| 107 | + (str rule-str (format "<rule name=\"%s\">%s</rule>\n" name content))) |
| 108 | + "" |
| 109 | + rules) |
| 110 | + "</rules>"]) |
| 111 | + "" |
| 112 | + (when (seq refined-contexts) |
| 113 | + ["## Contexts" |
| 114 | + "" |
| 115 | + (contexts-str refined-contexts repo-map* (get-in db [:chats chat-id :startup-context]))]) |
| 116 | + "" |
| 117 | + (selmer/render (load-builtin-prompt "additional_system_info.md") selmer-ctx)))) |
| 118 | + |
| 119 | +(defn build-rewrite-instructions [text path full-text range all-tools config db] |
118 | 120 | (let [legacy-prompt-file (-> config :rewrite :systemPromptFile) |
119 | 121 | prompt (-> config :rewrite :systemPrompt) |
120 | 122 | prompt-str (cond |
|
128 | 130 | ;; Resource path |
129 | 131 | :else |
130 | 132 | (load-builtin-prompt (some-> legacy-prompt-file (string/replace-first #"prompts/" ""))))] |
131 | | - (replace-vars |
132 | | - prompt-str |
133 | | - {:text text |
134 | | - :path (when path |
135 | | - (str "- File path: " path)) |
136 | | - :rangeText (multi-str |
137 | | - (str "- Start line: " (-> range :start :line)) |
138 | | - (str "- Start character: " (-> range :start :character)) |
139 | | - (str "- End line: " (-> range :end :line)) |
140 | | - (str "- End character: " (-> range :end :character))) |
141 | | - :fullText (when full-text |
142 | | - (multi-str |
143 | | - "- Full file content" |
144 | | - "```" |
145 | | - full-text |
146 | | - "```"))}))) |
147 | | - |
148 | | -(defn init-prompt [db] |
149 | | - (replace-vars |
| 133 | + (selmer/render prompt-str |
| 134 | + (merge |
| 135 | + (->base-selmer-ctx all-tools db) |
| 136 | + {:text text |
| 137 | + :path (when path |
| 138 | + (str "- File path: " path)) |
| 139 | + :rangeText (multi-str |
| 140 | + (str "- Start line: " (-> range :start :line)) |
| 141 | + (str "- Start character: " (-> range :start :character)) |
| 142 | + (str "- End line: " (-> range :end :line)) |
| 143 | + (str "- End character: " (-> range :end :character))) |
| 144 | + :fullText (when full-text |
| 145 | + (multi-str |
| 146 | + "- Full file content" |
| 147 | + "```" |
| 148 | + full-text |
| 149 | + "```"))})))) |
| 150 | + |
| 151 | +(defn init-prompt [all-tools db] |
| 152 | + (selmer/render |
150 | 153 | (init-prompt-template) |
151 | | - {:workspaceFolders (shared/workspaces-as-str db)})) |
| 154 | + (->base-selmer-ctx all-tools db))) |
152 | 155 |
|
153 | 156 | (defn title-prompt [] |
154 | 157 | (title-prompt-template)) |
155 | 158 |
|
156 | | -(defn compact-prompt [additional-input config] |
157 | | - (replace-vars |
158 | | - (or (:compactPrompt config) |
| 159 | +(defn compact-prompt [additional-input all-tools config db] |
| 160 | + (selmer/render |
| 161 | + (or (:compactPrompt config) |
159 | 162 | ;; legacy |
160 | | - (compact-prompt-template (:compactPromptFile config))) |
161 | | - {:additionalUserInput (if additional-input |
162 | | - (format "You MUST respect this user input in the summarization: %s." additional-input) |
163 | | - "")})) |
| 163 | + (compact-prompt-template (:compactPromptFile config))) |
| 164 | + (merge |
| 165 | + (->base-selmer-ctx all-tools db) |
| 166 | + {:additionalUserInput (if additional-input |
| 167 | + (format "You MUST respect this user input in the summarization: %s." additional-input) |
| 168 | + "")}))) |
164 | 169 |
|
165 | 170 | (defn inline-completion-prompt [config] |
166 | 171 | (let [legacy-prompt-file (get-in config [:completion :systemPromptFile]) |
|
0 commit comments