|
1 | 1 | (ns llm-mock.openai |
2 | 2 | (:require |
3 | 3 | [cheshire.core :as json] |
| 4 | + [clojure.string :as string] |
4 | 5 | [integration.helper :as h] |
5 | 6 | [llm-mock.mocks :as llm.mocks] |
6 | 7 | [org.httpkit.server :as hk])) |
|
179 | 180 | :status "completed"}}) |
180 | 181 | (hk/close ch))))) |
181 | 182 |
|
| 183 | +(defn ^:private chat-title-text-0 [ch] |
| 184 | + (sse-send! ch "response.output_text.delta" |
| 185 | + {:type "response.output_text.delta" :delta "Some Cool"}) |
| 186 | + (sse-send! ch "response.output_text.delta" |
| 187 | + {:type "response.output_text.delta" :delta " Title"}) |
| 188 | + (sse-send! ch "response.completed" |
| 189 | + {:type "response.completed" |
| 190 | + :response {:output [] |
| 191 | + :usage {:input_tokens 5 |
| 192 | + :output_tokens 10} |
| 193 | + :status "completed"}}) |
| 194 | + (hk/close ch)) |
| 195 | + |
182 | 196 | (defn handle-openai-responses [req] |
183 | | - (llm.mocks/set-last-req-body! (some-> (slurp (:body req)) |
184 | | - (json/parse-string true))) |
185 | | - (hk/as-channel |
186 | | - req |
187 | | - {:on-open (fn [ch] |
188 | | - ;; initial SSE handshake |
189 | | - (hk/send! ch {:status 200 |
190 | | - :headers {"Content-Type" "text/event-stream; charset=utf-8" |
191 | | - "Cache-Control" "no-cache" |
192 | | - "Connection" "keep-alive"}} |
193 | | - false) |
194 | | - (case llm.mocks/*case* |
195 | | - :simple-text-0 (simple-text-0 ch) |
196 | | - :simple-text-1 (simple-text-1 ch) |
197 | | - :simple-text-2 (simple-text-2 ch) |
198 | | - :reasoning-0 (reasoning-0 ch) |
199 | | - :reasoning-1 (reasoning-1 ch) |
200 | | - :tool-calling-0 (tool-calling-0 ch)))})) |
| 197 | + (let [body (some-> (slurp (:body req)) |
| 198 | + (json/parse-string true))] |
| 199 | + (llm.mocks/set-last-req-body! body) |
| 200 | + (hk/as-channel |
| 201 | + req |
| 202 | + {:on-open (fn [ch] |
| 203 | + ;; initial SSE handshake |
| 204 | + (hk/send! ch {:status 200 |
| 205 | + :headers {"Content-Type" "text/event-stream; charset=utf-8" |
| 206 | + "Cache-Control" "no-cache" |
| 207 | + "Connection" "keep-alive"}} |
| 208 | + false) |
| 209 | + (if (string/includes? (:instructions body) llm.mocks/chat-title-generator-str) |
| 210 | + (do |
| 211 | + (Thread/sleep 2000) ;; avoid tests failing with mismatch order of contents |
| 212 | + (chat-title-text-0 ch)) |
| 213 | + (case llm.mocks/*case* |
| 214 | + :simple-text-0 (simple-text-0 ch) |
| 215 | + :simple-text-1 (simple-text-1 ch) |
| 216 | + :simple-text-2 (simple-text-2 ch) |
| 217 | + :reasoning-0 (reasoning-0 ch) |
| 218 | + :reasoning-1 (reasoning-1 ch) |
| 219 | + :tool-calling-0 (tool-calling-0 ch))))}))) |
0 commit comments