|
93 | 93 | :on-tools-called on-tools-called |
94 | 94 | :on-reason on-reason |
95 | 95 | :on-usage-updated on-usage-updated})] |
96 | | - ;; We spawn a new future to not block the lsp4clj thread |
97 | | - ;; in case a tool call approval is needed |
98 | | - (future |
99 | | - (try |
100 | | - (when-not api-url (throw (ex-info (format "API url not found.\nMake sure you have provider '%s' configured properly." provider) {}))) |
101 | | - (cond |
102 | | - (= "openai" provider) |
103 | | - (llm-providers.openai/create-response! |
104 | | - {:model real-model |
105 | | - :instructions instructions |
106 | | - :user-messages user-messages |
107 | | - :max-output-tokens max-output-tokens |
108 | | - :reason? reason? |
109 | | - :supports-image? supports-image? |
110 | | - :past-messages past-messages |
111 | | - :tools tools |
112 | | - :web-search web-search |
113 | | - :extra-payload (merge {:parallel_tool_calls true} |
114 | | - extra-payload) |
115 | | - :api-url api-url |
116 | | - :api-key api-key |
117 | | - :auth-type auth-type} |
118 | | - callbacks) |
| 96 | + (try |
| 97 | + (when-not api-url (throw (ex-info (format "API url not found.\nMake sure you have provider '%s' configured properly." provider) {}))) |
| 98 | + (cond |
| 99 | + (= "openai" provider) |
| 100 | + (llm-providers.openai/create-response! |
| 101 | + {:model real-model |
| 102 | + :instructions instructions |
| 103 | + :user-messages user-messages |
| 104 | + :max-output-tokens max-output-tokens |
| 105 | + :reason? reason? |
| 106 | + :supports-image? supports-image? |
| 107 | + :past-messages past-messages |
| 108 | + :tools tools |
| 109 | + :web-search web-search |
| 110 | + :extra-payload (merge {:parallel_tool_calls true} |
| 111 | + extra-payload) |
| 112 | + :api-url api-url |
| 113 | + :api-key api-key |
| 114 | + :auth-type auth-type} |
| 115 | + callbacks) |
119 | 116 |
|
120 | | - (= "anthropic" provider) |
121 | | - (llm-providers.anthropic/chat! |
122 | | - {:model real-model |
123 | | - :instructions instructions |
124 | | - :user-messages user-messages |
125 | | - :max-output-tokens max-output-tokens |
126 | | - :reason? reason? |
127 | | - :supports-image? supports-image? |
128 | | - :past-messages past-messages |
129 | | - :tools tools |
130 | | - :web-search web-search |
131 | | - :extra-payload extra-payload |
132 | | - :api-url api-url |
133 | | - :api-key api-key |
134 | | - :auth-type auth-type} |
135 | | - callbacks) |
| 117 | + (= "anthropic" provider) |
| 118 | + (llm-providers.anthropic/chat! |
| 119 | + {:model real-model |
| 120 | + :instructions instructions |
| 121 | + :user-messages user-messages |
| 122 | + :max-output-tokens max-output-tokens |
| 123 | + :reason? reason? |
| 124 | + :supports-image? supports-image? |
| 125 | + :past-messages past-messages |
| 126 | + :tools tools |
| 127 | + :web-search web-search |
| 128 | + :extra-payload extra-payload |
| 129 | + :api-url api-url |
| 130 | + :api-key api-key |
| 131 | + :auth-type auth-type} |
| 132 | + callbacks) |
136 | 133 |
|
137 | | - (= "github-copilot" provider) |
138 | | - (llm-providers.openai-chat/chat-completion! |
139 | | - {:model real-model |
140 | | - :instructions instructions |
141 | | - :user-messages user-messages |
142 | | - :max-output-tokens max-output-tokens |
143 | | - :reason? reason? |
144 | | - :supports-image? supports-image? |
145 | | - :past-messages past-messages |
146 | | - :tools tools |
147 | | - :extra-payload (merge {:parallel_tool_calls true} |
148 | | - extra-payload) |
149 | | - :api-url api-url |
150 | | - :api-key api-key |
151 | | - :extra-headers {"openai-intent" "conversation-panel" |
152 | | - "x-request-id" (str (random-uuid)) |
153 | | - "vscode-sessionid" "" |
154 | | - "vscode-machineid" "" |
155 | | - "Copilot-Vision-Request" "true" |
156 | | - "copilot-integration-id" "vscode-chat"}} |
157 | | - callbacks) |
| 134 | + (= "github-copilot" provider) |
| 135 | + (llm-providers.openai-chat/chat-completion! |
| 136 | + {:model real-model |
| 137 | + :instructions instructions |
| 138 | + :user-messages user-messages |
| 139 | + :max-output-tokens max-output-tokens |
| 140 | + :reason? reason? |
| 141 | + :supports-image? supports-image? |
| 142 | + :past-messages past-messages |
| 143 | + :tools tools |
| 144 | + :extra-payload (merge {:parallel_tool_calls true} |
| 145 | + extra-payload) |
| 146 | + :api-url api-url |
| 147 | + :api-key api-key |
| 148 | + :extra-headers {"openai-intent" "conversation-panel" |
| 149 | + "x-request-id" (str (random-uuid)) |
| 150 | + "vscode-sessionid" "" |
| 151 | + "vscode-machineid" "" |
| 152 | + "Copilot-Vision-Request" "true" |
| 153 | + "copilot-integration-id" "vscode-chat"}} |
| 154 | + callbacks) |
| 155 | + |
| 156 | + (= "google" provider) |
| 157 | + (llm-providers.openai-chat/chat-completion! |
| 158 | + {:model real-model |
| 159 | + :instructions instructions |
| 160 | + :user-messages user-messages |
| 161 | + :max-output-tokens max-output-tokens |
| 162 | + :reason? reason? |
| 163 | + :supports-image? supports-image? |
| 164 | + :past-messages past-messages |
| 165 | + :tools tools |
| 166 | + :thinking-tag "thought" |
| 167 | + :extra-payload (merge {:parallel_tool_calls false} |
| 168 | + (when reason? |
| 169 | + {:extra_body {:google {:thinking_config {:include_thoughts true}}}}) |
| 170 | + extra-payload) |
| 171 | + :api-url api-url |
| 172 | + :api-key api-key} |
| 173 | + callbacks) |
| 174 | + |
| 175 | + (= "ollama" provider) |
| 176 | + (llm-providers.ollama/chat! |
| 177 | + {:api-url api-url |
| 178 | + :reason? (:reason? model-capabilities) |
| 179 | + :supports-image? supports-image? |
| 180 | + :model real-model |
| 181 | + :instructions instructions |
| 182 | + :user-messages user-messages |
| 183 | + :past-messages past-messages |
| 184 | + :tools tools |
| 185 | + :extra-payload extra-payload} |
| 186 | + callbacks) |
158 | 187 |
|
159 | | - (= "google" provider) |
160 | | - (llm-providers.openai-chat/chat-completion! |
| 188 | + model-config |
| 189 | + (let [provider-fn (case (:api provider-config) |
| 190 | + ("openai-responses" |
| 191 | + "openai") llm-providers.openai/create-response! |
| 192 | + "anthropic" llm-providers.anthropic/chat! |
| 193 | + "openai-chat" llm-providers.openai-chat/chat-completion! |
| 194 | + (on-error {:message (format "Unknown model %s for provider %s" (:api provider-config) provider)})) |
| 195 | + url-relative-path (:completionUrlRelativePath provider-config)] |
| 196 | + (provider-fn |
161 | 197 | {:model real-model |
162 | 198 | :instructions instructions |
163 | 199 | :user-messages user-messages |
164 | 200 | :max-output-tokens max-output-tokens |
| 201 | + :web-search web-search |
165 | 202 | :reason? reason? |
166 | 203 | :supports-image? supports-image? |
167 | 204 | :past-messages past-messages |
168 | 205 | :tools tools |
169 | | - :thinking-tag "thought" |
170 | | - :extra-payload (merge {:parallel_tool_calls false} |
171 | | - (when reason? |
172 | | - {:extra_body {:google {:thinking_config {:include_thoughts true}}}}) |
173 | | - extra-payload) |
| 206 | + :extra-payload extra-payload |
| 207 | + :url-relative-path url-relative-path |
174 | 208 | :api-url api-url |
175 | 209 | :api-key api-key} |
176 | | - callbacks) |
| 210 | + callbacks)) |
177 | 211 |
|
178 | | - (= "ollama" provider) |
179 | | - (llm-providers.ollama/chat! |
180 | | - {:api-url api-url |
181 | | - :reason? (:reason? model-capabilities) |
182 | | - :supports-image? supports-image? |
183 | | - :model real-model |
184 | | - :instructions instructions |
185 | | - :user-messages user-messages |
186 | | - :past-messages past-messages |
187 | | - :tools tools |
188 | | - :extra-payload extra-payload} |
189 | | - callbacks) |
190 | | - |
191 | | - model-config |
192 | | - (let [provider-fn (case (:api provider-config) |
193 | | - ("openai-responses" |
194 | | - "openai") llm-providers.openai/create-response! |
195 | | - "anthropic" llm-providers.anthropic/chat! |
196 | | - "openai-chat" llm-providers.openai-chat/chat-completion! |
197 | | - (on-error {:message (format "Unknown model %s for provider %s" (:api provider-config) provider)})) |
198 | | - url-relative-path (:completionUrlRelativePath provider-config)] |
199 | | - (provider-fn |
200 | | - {:model real-model |
201 | | - :instructions instructions |
202 | | - :user-messages user-messages |
203 | | - :max-output-tokens max-output-tokens |
204 | | - :web-search web-search |
205 | | - :reason? reason? |
206 | | - :supports-image? supports-image? |
207 | | - :past-messages past-messages |
208 | | - :tools tools |
209 | | - :extra-payload extra-payload |
210 | | - :url-relative-path url-relative-path |
211 | | - :api-url api-url |
212 | | - :api-key api-key} |
213 | | - callbacks)) |
| 212 | + :else |
| 213 | + (on-error {:message (format "ECA Unsupported model %s for provider %s" real-model provider)})) |
| 214 | + (catch Exception e |
| 215 | + (on-error {:exception e}))))) |
214 | 216 |
|
215 | | - :else |
216 | | - (on-error {:message (format "ECA Unsupported model %s for provider %s" real-model provider)})) |
217 | | - (catch Exception e |
218 | | - (on-error {:exception e})))))) |
219 | | - |
220 | | -(defn async-prompt! [{:keys [provider model model-capabilities instructions user-messages config on-first-response-received |
221 | | - on-message-received on-error on-prepare-tool-call on-tools-called on-reason on-usage-updated |
222 | | - past-messages tools provider-auth] |
223 | | - :or {on-first-response-received identity |
224 | | - on-message-received identity |
225 | | - on-error identity |
226 | | - on-prepare-tool-call identity |
227 | | - on-tools-called identity |
228 | | - on-reason identity |
229 | | - on-usage-updated identity}}] |
| 217 | +(defn sync-or-async-prompt! |
| 218 | + [{:keys [provider model model-capabilities instructions user-messages config on-first-response-received |
| 219 | + on-message-received on-error on-prepare-tool-call on-tools-called on-reason on-usage-updated |
| 220 | + past-messages tools provider-auth] |
| 221 | + :or {on-first-response-received identity |
| 222 | + on-message-received identity |
| 223 | + on-error identity |
| 224 | + on-prepare-tool-call identity |
| 225 | + on-tools-called identity |
| 226 | + on-reason identity |
| 227 | + on-usage-updated identity}}] |
230 | 228 | (let [first-response-received* (atom false) |
231 | 229 | emit-first-message-fn (fn [& args] |
232 | 230 | (when-not @first-response-received* |
|
244 | 242 | on-error-wrapper (fn [{:keys [exception] :as args}] |
245 | 243 | (when-not (:silent? (ex-data exception)) |
246 | 244 | (logger/error args) |
247 | | - (on-error args)))] |
248 | | - (prompt! |
249 | | - {:sync? false |
250 | | - :provider provider |
251 | | - :model model |
252 | | - :model-capabilities model-capabilities |
253 | | - :instructions instructions |
254 | | - :tools tools |
255 | | - :provider-auth provider-auth |
256 | | - :past-messages past-messages |
257 | | - :user-messages user-messages |
258 | | - :on-message-received on-message-received-wrapper |
259 | | - :on-prepare-tool-call on-prepare-tool-call-wrapper |
260 | | - :on-tools-called on-tools-called |
261 | | - :on-usage-updated on-usage-updated |
262 | | - :on-reason on-reason-wrapper |
263 | | - :on-error on-error-wrapper |
264 | | - :config config}))) |
| 245 | + (on-error args))) |
| 246 | + provider-config (get-in config [:providers provider]) |
| 247 | + model-config (get-in provider-config [:models model]) |
| 248 | + extra-payload (:extraPayload model-config) |
| 249 | + stream? (if (not (nil? (:stream extra-payload))) |
| 250 | + (:stream extra-payload) |
| 251 | + true)] |
| 252 | + (if (not stream?) |
| 253 | + (loop [result (prompt! |
| 254 | + {:sync? true |
| 255 | + :provider provider |
| 256 | + :model model |
| 257 | + :model-capabilities model-capabilities |
| 258 | + :instructions instructions |
| 259 | + :tools tools |
| 260 | + :provider-auth provider-auth |
| 261 | + :past-messages past-messages |
| 262 | + :user-messages user-messages |
| 263 | + :on-error on-error-wrapper |
| 264 | + :config config})] |
| 265 | + (let [{:keys [error output-text reason-text tools-to-call call-tools-fn reason-id usage]} result] |
| 266 | + (if error |
| 267 | + (on-error-wrapper error) |
| 268 | + (do |
| 269 | + (when reason-text |
| 270 | + (on-reason-wrapper {:status :started :id reason-id}) |
| 271 | + (on-reason-wrapper {:status :thinking :id reason-id :text reason-text}) |
| 272 | + (on-reason-wrapper {:status :finished :id reason-id})) |
| 273 | + (on-message-received-wrapper {:type :text :text output-text}) |
| 274 | + (some-> usage (on-usage-updated)) |
| 275 | + (if-let [new-result (when (seq tools-to-call) |
| 276 | + (doseq [tool-to-call tools-to-call] |
| 277 | + (on-prepare-tool-call tool-to-call)) |
| 278 | + (call-tools-fn on-tools-called))] |
| 279 | + (recur new-result) |
| 280 | + (on-message-received-wrapper {:type :finish :finish-reason "stop"})))))) |
| 281 | + (prompt! |
| 282 | + {:sync? false |
| 283 | + :provider provider |
| 284 | + :model model |
| 285 | + :model-capabilities model-capabilities |
| 286 | + :instructions instructions |
| 287 | + :tools tools |
| 288 | + :provider-auth provider-auth |
| 289 | + :past-messages past-messages |
| 290 | + :user-messages user-messages |
| 291 | + :on-message-received on-message-received-wrapper |
| 292 | + :on-prepare-tool-call on-prepare-tool-call-wrapper |
| 293 | + :on-tools-called on-tools-called |
| 294 | + :on-usage-updated on-usage-updated |
| 295 | + :on-reason on-reason-wrapper |
| 296 | + :on-error on-error-wrapper |
| 297 | + :config config})))) |
265 | 298 |
|
266 | 299 | (defn sync-prompt! |
267 | 300 | [{:keys [provider model model-capabilities instructions |
268 | 301 | prompt past-messages user-messages config tools provider-auth]}] |
269 | | - @(prompt! |
270 | | - {:sync? true |
271 | | - :provider provider |
272 | | - :model model |
273 | | - :model-capabilities model-capabilities |
274 | | - :instructions instructions |
275 | | - :tools tools |
276 | | - :provider-auth provider-auth |
277 | | - :past-messages past-messages |
278 | | - :user-messages (or user-messages |
279 | | - [{:role "user" :content [{:type :text :text prompt}]}]) |
280 | | - :config config |
281 | | - :on-error (fn [error] {:error error})})) |
| 302 | + (prompt! |
| 303 | + {:sync? true |
| 304 | + :provider provider |
| 305 | + :model model |
| 306 | + :model-capabilities model-capabilities |
| 307 | + :instructions instructions |
| 308 | + :tools tools |
| 309 | + :provider-auth provider-auth |
| 310 | + :past-messages past-messages |
| 311 | + :user-messages (or user-messages |
| 312 | + [{:role "user" :content [{:type :text :text prompt}]}]) |
| 313 | + :config config |
| 314 | + :on-error (fn [error] {:error error})})) |
0 commit comments