@copilotkit/aimock 1.15.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/CHANGELOG.md +33 -15
  3. package/README.md +1 -1
  4. package/dist/bedrock-converse.cjs +133 -25
  5. package/dist/bedrock-converse.cjs.map +1 -1
  6. package/dist/bedrock-converse.d.cts.map +1 -1
  7. package/dist/bedrock-converse.d.ts.map +1 -1
  8. package/dist/bedrock-converse.js +135 -27
  9. package/dist/bedrock-converse.js.map +1 -1
  10. package/dist/bedrock.cjs +262 -48
  11. package/dist/bedrock.cjs.map +1 -1
  12. package/dist/bedrock.d.cts.map +1 -1
  13. package/dist/bedrock.d.ts.map +1 -1
  14. package/dist/bedrock.js +263 -50
  15. package/dist/bedrock.js.map +1 -1
  16. package/dist/chaos.cjs +9 -35
  17. package/dist/chaos.cjs.map +1 -1
  18. package/dist/chaos.d.cts +2 -17
  19. package/dist/chaos.d.cts.map +1 -1
  20. package/dist/chaos.d.ts +2 -17
  21. package/dist/chaos.d.ts.map +1 -1
  22. package/dist/chaos.js +10 -35
  23. package/dist/chaos.js.map +1 -1
  24. package/dist/cohere.cjs +289 -33
  25. package/dist/cohere.cjs.map +1 -1
  26. package/dist/cohere.d.cts +9 -0
  27. package/dist/cohere.d.cts.map +1 -1
  28. package/dist/cohere.d.ts +9 -0
  29. package/dist/cohere.d.ts.map +1 -1
  30. package/dist/cohere.js +290 -34
  31. package/dist/cohere.js.map +1 -1
  32. package/dist/config-loader.d.cts.map +1 -1
  33. package/dist/embeddings.cjs +22 -4
  34. package/dist/embeddings.cjs.map +1 -1
  35. package/dist/embeddings.d.cts +2 -2
  36. package/dist/embeddings.d.cts.map +1 -1
  37. package/dist/embeddings.d.ts +2 -2
  38. package/dist/embeddings.d.ts.map +1 -1
  39. package/dist/embeddings.js +22 -4
  40. package/dist/embeddings.js.map +1 -1
  41. package/dist/fixture-loader.cjs +19 -4
  42. package/dist/fixture-loader.cjs.map +1 -1
  43. package/dist/fixture-loader.d.cts.map +1 -1
  44. package/dist/fixture-loader.d.ts.map +1 -1
  45. package/dist/fixture-loader.js +19 -4
  46. package/dist/fixture-loader.js.map +1 -1
  47. package/dist/gemini.cjs +48 -45
  48. package/dist/gemini.cjs.map +1 -1
  49. package/dist/gemini.d.cts.map +1 -1
  50. package/dist/gemini.d.ts.map +1 -1
  51. package/dist/gemini.js +48 -45
  52. package/dist/gemini.js.map +1 -1
  53. package/dist/helpers.cjs +9 -0
  54. package/dist/helpers.cjs.map +1 -1
  55. package/dist/helpers.d.cts.map +1 -1
  56. package/dist/helpers.d.ts.map +1 -1
  57. package/dist/helpers.js +9 -0
  58. package/dist/helpers.js.map +1 -1
  59. package/dist/images.cjs +21 -3
  60. package/dist/images.cjs.map +1 -1
  61. package/dist/images.js +21 -3
  62. package/dist/images.js.map +1 -1
  63. package/dist/index.cjs +2 -0
  64. package/dist/index.d.cts +2 -2
  65. package/dist/index.d.ts +2 -2
  66. package/dist/index.js +3 -3
  67. package/dist/jest.cjs +10 -3
  68. package/dist/jest.cjs.map +1 -1
  69. package/dist/jest.js +10 -3
  70. package/dist/jest.js.map +1 -1
  71. package/dist/journal.cjs +1 -1
  72. package/dist/journal.cjs.map +1 -1
  73. package/dist/journal.d.cts.map +1 -1
  74. package/dist/journal.d.ts.map +1 -1
  75. package/dist/journal.js +1 -1
  76. package/dist/journal.js.map +1 -1
  77. package/dist/llmock.cjs +6 -0
  78. package/dist/llmock.cjs.map +1 -1
  79. package/dist/llmock.d.cts +1 -0
  80. package/dist/llmock.d.cts.map +1 -1
  81. package/dist/llmock.d.ts +1 -0
  82. package/dist/llmock.d.ts.map +1 -1
  83. package/dist/llmock.js +6 -0
  84. package/dist/llmock.js.map +1 -1
  85. package/dist/messages.cjs +5 -4
  86. package/dist/messages.cjs.map +1 -1
  87. package/dist/messages.js +5 -4
  88. package/dist/messages.js.map +1 -1
  89. package/dist/ollama.cjs +129 -8
  90. package/dist/ollama.cjs.map +1 -1
  91. package/dist/ollama.d.cts.map +1 -1
  92. package/dist/ollama.d.ts.map +1 -1
  93. package/dist/ollama.js +130 -9
  94. package/dist/ollama.js.map +1 -1
  95. package/dist/recorder.cjs +234 -69
  96. package/dist/recorder.cjs.map +1 -1
  97. package/dist/recorder.d.cts +5 -50
  98. package/dist/recorder.d.cts.map +1 -1
  99. package/dist/recorder.d.ts +5 -50
  100. package/dist/recorder.d.ts.map +1 -1
  101. package/dist/recorder.js +234 -69
  102. package/dist/recorder.js.map +1 -1
  103. package/dist/responses.cjs +12 -3
  104. package/dist/responses.cjs.map +1 -1
  105. package/dist/responses.d.cts +2 -1
  106. package/dist/responses.d.cts.map +1 -1
  107. package/dist/responses.d.ts +2 -1
  108. package/dist/responses.d.ts.map +1 -1
  109. package/dist/responses.js +12 -4
  110. package/dist/responses.js.map +1 -1
  111. package/dist/router.cjs +19 -6
  112. package/dist/router.cjs.map +1 -1
  113. package/dist/router.js +19 -6
  114. package/dist/router.js.map +1 -1
  115. package/dist/server.cjs +150 -94
  116. package/dist/server.cjs.map +1 -1
  117. package/dist/server.d.cts.map +1 -1
  118. package/dist/server.d.ts.map +1 -1
  119. package/dist/server.js +152 -96
  120. package/dist/server.js.map +1 -1
  121. package/dist/speech.cjs +21 -3
  122. package/dist/speech.cjs.map +1 -1
  123. package/dist/speech.js +21 -3
  124. package/dist/speech.js.map +1 -1
  125. package/dist/transcription.cjs +10 -6
  126. package/dist/transcription.cjs.map +1 -1
  127. package/dist/transcription.d.cts.map +1 -1
  128. package/dist/transcription.d.ts.map +1 -1
  129. package/dist/transcription.js +10 -6
  130. package/dist/transcription.js.map +1 -1
  131. package/dist/types.d.cts +5 -16
  132. package/dist/types.d.cts.map +1 -1
  133. package/dist/types.d.ts +5 -16
  134. package/dist/types.d.ts.map +1 -1
  135. package/dist/video.cjs +66 -10
  136. package/dist/video.cjs.map +1 -1
  137. package/dist/video.d.cts +16 -3
  138. package/dist/video.d.cts.map +1 -1
  139. package/dist/video.d.ts +16 -3
  140. package/dist/video.d.ts.map +1 -1
  141. package/dist/video.js +66 -11
  142. package/dist/video.js.map +1 -1
  143. package/dist/vitest.cjs +10 -3
  144. package/dist/vitest.cjs.map +1 -1
  145. package/dist/vitest.js +10 -3
  146. package/dist/vitest.js.map +1 -1
  147. package/package.json +1 -1
  148. package/skills/write-fixtures/SKILL.md +75 -49
package/dist/server.cjs CHANGED
@@ -97,14 +97,24 @@ const DEFAULT_MODELS = [
97
97
  const CORS_HEADERS = {
98
98
  "Access-Control-Allow-Origin": "*",
99
99
  "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
100
- "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Aimock-Chaos-Drop, X-Aimock-Chaos-Malformed, X-Aimock-Chaos-Disconnect, X-Test-Id"
100
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
101
101
  };
102
102
  function setCorsHeaders(res) {
103
103
  for (const [key, value] of Object.entries(CORS_HEADERS)) res.setHeader(key, value);
104
104
  }
105
- async function readBody(req) {
105
+ const DEFAULT_MAX_BODY_BYTES = 10 * 1024 * 1024;
106
+ async function readBody(req, maxBytes = DEFAULT_MAX_BODY_BYTES) {
106
107
  const buffers = [];
107
- for await (const chunk of req) buffers.push(chunk);
108
+ let totalBytes = 0;
109
+ for await (const chunk of req) {
110
+ const buf = chunk;
111
+ totalBytes += buf.length;
112
+ if (totalBytes > maxBytes) {
113
+ req.destroy();
114
+ throw new Error(`Request body exceeded size limit of ${maxBytes} bytes`);
115
+ }
116
+ buffers.push(buf);
117
+ }
108
118
  return Buffer.concat(buffers).toString();
109
119
  }
110
120
  function handleOptions(res) {
@@ -124,7 +134,7 @@ const CONTROL_PREFIX = "/__aimock";
124
134
  * Handle requests under `/__aimock/`. Returns `true` if the request was
125
135
  * handled, `false` if the path doesn't match the control prefix.
126
136
  */
127
- async function handleControlAPI(req, res, pathname, fixtures, journal, videoStates) {
137
+ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStates, defaults) {
128
138
  if (!pathname.startsWith(CONTROL_PREFIX)) return false;
129
139
  const subPath = pathname.slice(9);
130
140
  setCorsHeaders(res);
@@ -142,17 +152,21 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
142
152
  let raw;
143
153
  try {
144
154
  raw = await readBody(req);
145
- } catch {
155
+ } catch (err) {
156
+ const msg = err instanceof Error ? err.message : String(err);
157
+ defaults.logger.error(`POST /__aimock/fixtures: failed to read body: ${msg}`);
146
158
  res.writeHead(400, { "Content-Type": "application/json" });
147
- res.end(JSON.stringify({ error: "Failed to read request body" }));
159
+ res.end(JSON.stringify({ error: `Failed to read request body: ${msg}` }));
148
160
  return true;
149
161
  }
150
162
  let parsed;
151
163
  try {
152
164
  parsed = JSON.parse(raw);
153
- } catch {
165
+ } catch (err) {
166
+ const msg = err instanceof Error ? err.message : String(err);
167
+ defaults.logger.error(`POST /__aimock/fixtures: invalid JSON: ${msg}`);
154
168
  res.writeHead(400, { "Content-Type": "application/json" });
155
- res.end(JSON.stringify({ error: "Invalid JSON" }));
169
+ res.end(JSON.stringify({ error: `Invalid JSON: ${msg}` }));
156
170
  return true;
157
171
  }
158
172
  if (!Array.isArray(parsed.fixtures)) {
@@ -171,12 +185,14 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
171
185
  return true;
172
186
  }
173
187
  fixtures.push(...converted);
188
+ if (defaults.registry) defaults.registry.setGauge("aimock_fixtures_loaded", {}, fixtures.length);
174
189
  res.writeHead(200, { "Content-Type": "application/json" });
175
190
  res.end(JSON.stringify({ added: converted.length }));
176
191
  return true;
177
192
  }
178
193
  if (subPath === "/fixtures" && req.method === "DELETE") {
179
194
  fixtures.length = 0;
195
+ if (defaults.registry) defaults.registry.setGauge("aimock_fixtures_loaded", {}, fixtures.length);
180
196
  res.writeHead(200, { "Content-Type": "application/json" });
181
197
  res.end(JSON.stringify({ cleared: true }));
182
198
  return true;
@@ -185,6 +201,7 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
185
201
  fixtures.length = 0;
186
202
  journal.clear();
187
203
  videoStates.clear();
204
+ if (defaults.registry) defaults.registry.setGauge("aimock_fixtures_loaded", {}, fixtures.length);
188
205
  res.writeHead(200, { "Content-Type": "application/json" });
189
206
  res.end(JSON.stringify({ reset: true }));
190
207
  return true;
@@ -193,17 +210,21 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
193
210
  let raw;
194
211
  try {
195
212
  raw = await readBody(req);
196
- } catch {
213
+ } catch (err) {
214
+ const msg = err instanceof Error ? err.message : String(err);
215
+ defaults.logger.error(`POST /__aimock/error: failed to read body: ${msg}`);
197
216
  res.writeHead(400, { "Content-Type": "application/json" });
198
- res.end(JSON.stringify({ error: "Failed to read request body" }));
217
+ res.end(JSON.stringify({ error: `Failed to read request body: ${msg}` }));
199
218
  return true;
200
219
  }
201
220
  let parsed;
202
221
  try {
203
222
  parsed = JSON.parse(raw);
204
- } catch {
223
+ } catch (err) {
224
+ const msg = err instanceof Error ? err.message : String(err);
225
+ defaults.logger.error(`POST /__aimock/error: invalid JSON: ${msg}`);
205
226
  res.writeHead(400, { "Content-Type": "application/json" });
206
- res.end(JSON.stringify({ error: "Invalid JSON" }));
227
+ res.end(JSON.stringify({ error: `Invalid JSON: ${msg}` }));
207
228
  return true;
208
229
  }
209
230
  const status = parsed.status ?? 500;
@@ -223,10 +244,10 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
223
244
  const original = errorFixture.match.predicate;
224
245
  errorFixture.match.predicate = (req) => {
225
246
  const result = original(req);
226
- if (result) queueMicrotask(() => {
247
+ if (result) {
227
248
  const idx = fixtures.indexOf(errorFixture);
228
249
  if (idx !== -1) fixtures.splice(idx, 1);
229
- });
250
+ }
230
251
  return result;
231
252
  };
232
253
  res.writeHead(200, { "Content-Type": "application/json" });
@@ -281,47 +302,39 @@ async function handleCompletions(req, res, fixtures, journal, defaults, modelFal
281
302
  } }));
282
303
  return;
283
304
  }
284
- const method = req.method ?? "POST";
285
- const path = req.url ?? COMPLETIONS_PATH;
286
- const flatHeaders = require_helpers.flattenHeaders(req.headers);
305
+ if (!Array.isArray(body.messages)) {
306
+ journal.add({
307
+ method: req.method ?? "POST",
308
+ path: req.url ?? COMPLETIONS_PATH,
309
+ headers: require_helpers.flattenHeaders(req.headers),
310
+ body: null,
311
+ response: {
312
+ status: 400,
313
+ fixture: null
314
+ }
315
+ });
316
+ require_sse_writer.writeErrorResponse(res, 400, JSON.stringify({ error: {
317
+ message: "Missing required parameter: 'messages'",
318
+ type: "invalid_request_error"
319
+ } }));
320
+ return;
321
+ }
287
322
  body._endpointType = "chat";
288
323
  const testId = require_helpers.getTestId(req);
289
324
  const fixture = require_router.matchFixture(fixtures, body, journal.getFixtureMatchCountsForTest(testId), defaults.requestTransform);
290
325
  if (fixture) journal.incrementFixtureMatchCount(fixture, fixtures, testId);
291
- const chaosAction = require_chaos.evaluateChaos(fixture, defaults.chaos, req.headers, defaults.logger);
292
- const chaosContext = {
326
+ const method = req.method ?? "POST";
327
+ const path = req.url ?? COMPLETIONS_PATH;
328
+ const flatHeaders = require_helpers.flattenHeaders(req.headers);
329
+ if (require_chaos.applyChaos(res, fixture, defaults.chaos, req.headers, journal, {
293
330
  method,
294
331
  path,
295
332
  headers: flatHeaders,
296
333
  body
297
- };
298
- if (chaosAction === "drop" || chaosAction === "disconnect") {
299
- require_chaos.applyChaosAction(chaosAction, res, fixture, journal, chaosContext, fixture ? "fixture" : "proxy", defaults.registry);
300
- return;
301
- }
302
- if (fixture && chaosAction === "malformed") {
303
- require_chaos.applyChaosAction(chaosAction, res, fixture, journal, chaosContext, "fixture", defaults.registry);
304
- return;
305
- }
334
+ }, defaults.registry, defaults.logger)) return;
306
335
  if (!fixture) {
307
336
  if (defaults.record && providerKey) {
308
- const hookOptions = chaosAction === "malformed" ? {
309
- beforeWriteResponse: () => {
310
- require_chaos.applyChaosAction(chaosAction, res, null, journal, chaosContext, "proxy", defaults.registry);
311
- return true;
312
- },
313
- onHookBypassed: (reason) => {
314
- defaults.logger.warn(`[chaos] malformed bypassed on proxy: upstream returned SSE (${reason})`);
315
- defaults.registry?.incrementCounter("aimock_chaos_bypassed_total", {
316
- action: "malformed",
317
- source: "proxy",
318
- reason
319
- });
320
- }
321
- } : void 0;
322
- const outcome = await require_recorder.proxyAndRecord(req, res, body, providerKey, req.url ?? COMPLETIONS_PATH, fixtures, defaults, raw, hookOptions);
323
- if (outcome === "handled_by_hook") return;
324
- if (outcome === "relayed") {
337
+ if (await require_recorder.proxyAndRecord(req, res, body, providerKey, req.url ?? COMPLETIONS_PATH, fixtures, defaults, raw)) {
325
338
  journal.add({
326
339
  method: req.method ?? "POST",
327
340
  path: req.url ?? COMPLETIONS_PATH,
@@ -535,7 +548,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
535
548
  maxEntries: options?.journalMaxEntries ?? 1e3,
536
549
  fixtureCountsMaxTestIds: options?.fixtureCountsMaxTestIds ?? 500
537
550
  });
538
- const videoStates = /* @__PURE__ */ new Map();
551
+ const videoStates = new require_video.VideoStateMap();
539
552
  if (mounts) for (const { handler } of mounts) {
540
553
  if (handler.setJournal) handler.setJournal(journal);
541
554
  if (registry && handler.setRegistry) handler.setRegistry(registry);
@@ -582,7 +595,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
582
595
  }
583
596
  });
584
597
  if (pathname.startsWith(CONTROL_PREFIX)) {
585
- await handleControlAPI(req, res, pathname, fixtures, journal, videoStates);
598
+ await handleControlAPI(req, res, pathname, fixtures, journal, videoStates, defaults);
586
599
  return;
587
600
  }
588
601
  if (mounts) {
@@ -684,7 +697,9 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
684
697
  return;
685
698
  }
686
699
  if (pathname === RESPONSES_PATH && req.method === "POST") {
687
- readBody(req).then((raw) => require_responses.handleResponses(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
700
+ try {
701
+ await require_responses.handleResponses(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
702
+ } catch (err) {
688
703
  const msg = err instanceof Error ? err.message : "Internal error";
689
704
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
690
705
  message: msg,
@@ -698,11 +713,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
698
713
  }
699
714
  res.end();
700
715
  }
701
- });
716
+ }
702
717
  return;
703
718
  }
704
719
  if (pathname === MESSAGES_PATH && req.method === "POST") {
705
- readBody(req).then((raw) => require_messages.handleMessages(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
720
+ try {
721
+ await require_messages.handleMessages(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
722
+ } catch (err) {
706
723
  const msg = err instanceof Error ? err.message : "Internal error";
707
724
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
708
725
  message: msg,
@@ -716,11 +733,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
716
733
  }
717
734
  res.end();
718
735
  }
719
- });
736
+ }
720
737
  return;
721
738
  }
722
739
  if (pathname === COHERE_CHAT_PATH && req.method === "POST") {
723
- readBody(req).then((raw) => require_cohere.handleCohere(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
740
+ try {
741
+ await require_cohere.handleCohere(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
742
+ } catch (err) {
724
743
  const msg = err instanceof Error ? err.message : "Internal error";
725
744
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
726
745
  message: msg,
@@ -734,98 +753,112 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
734
753
  }
735
754
  res.end();
736
755
  }
737
- });
756
+ }
738
757
  return;
739
758
  }
740
759
  if (pathname === EMBEDDINGS_PATH && req.method === "POST") {
741
- const deploymentId = azureDeploymentId;
742
- readBody(req).then((raw) => {
760
+ try {
761
+ const deploymentId = azureDeploymentId;
762
+ const embeddingsProvider = azureDeploymentId ? "azure" : "openai";
763
+ let raw = await readBody(req);
743
764
  if (deploymentId) try {
744
765
  const parsed = JSON.parse(raw);
745
766
  if (!parsed.model) {
746
767
  parsed.model = deploymentId;
747
- return require_embeddings.handleEmbeddings(req, res, JSON.stringify(parsed), fixtures, journal, defaults, setCorsHeaders);
768
+ raw = JSON.stringify(parsed);
748
769
  }
749
770
  } catch {}
750
- return require_embeddings.handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders);
751
- }).catch((err) => {
771
+ await require_embeddings.handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders, embeddingsProvider);
772
+ } catch (err) {
752
773
  const msg = err instanceof Error ? err.message : "Internal error";
753
774
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
754
775
  message: msg,
755
776
  type: "server_error"
756
777
  } }));
757
778
  else if (!res.writableEnded) res.destroy();
758
- });
779
+ }
759
780
  return;
760
781
  }
761
782
  if (pathname === IMAGES_PATH && req.method === "POST") {
762
- readBody(req).then((raw) => require_images.handleImages(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
783
+ try {
784
+ await require_images.handleImages(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
785
+ } catch (err) {
763
786
  const msg = err instanceof Error ? err.message : "Internal error";
764
787
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
765
788
  message: msg,
766
789
  type: "server_error"
767
790
  } }));
768
791
  else if (!res.writableEnded) res.destroy();
769
- });
792
+ }
770
793
  return;
771
794
  }
772
795
  if (pathname === SPEECH_PATH && req.method === "POST") {
773
- readBody(req).then((raw) => require_speech.handleSpeech(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
796
+ try {
797
+ await require_speech.handleSpeech(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
798
+ } catch (err) {
774
799
  const msg = err instanceof Error ? err.message : "Internal error";
775
800
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
776
801
  message: msg,
777
802
  type: "server_error"
778
803
  } }));
779
804
  else if (!res.writableEnded) res.destroy();
780
- });
805
+ }
781
806
  return;
782
807
  }
783
808
  if (pathname === TRANSCRIPTIONS_PATH && req.method === "POST") {
784
- readBody(req).then((raw) => require_transcription.handleTranscription(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
809
+ try {
810
+ await require_transcription.handleTranscription(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
811
+ } catch (err) {
785
812
  const msg = err instanceof Error ? err.message : "Internal error";
786
813
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
787
814
  message: msg,
788
815
  type: "server_error"
789
816
  } }));
790
817
  else if (!res.writableEnded) res.destroy();
791
- });
818
+ }
792
819
  return;
793
820
  }
794
821
  if (pathname === VIDEOS_PATH && req.method === "POST") {
795
- readBody(req).then((raw) => require_video.handleVideoCreate(req, res, raw, fixtures, journal, defaults, setCorsHeaders, videoStates)).catch((err) => {
822
+ try {
823
+ await require_video.handleVideoCreate(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders, videoStates);
824
+ } catch (err) {
796
825
  const msg = err instanceof Error ? err.message : "Internal error";
797
826
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
798
827
  message: msg,
799
828
  type: "server_error"
800
829
  } }));
801
830
  else if (!res.writableEnded) res.destroy();
802
- });
831
+ }
803
832
  return;
804
833
  }
805
834
  const videoStatusMatch = pathname.match(VIDEOS_STATUS_RE);
806
835
  if (videoStatusMatch && req.method === "GET") {
807
836
  const videoId = videoStatusMatch[1];
808
- require_video.handleVideoStatus(req, res, videoId, journal, defaults, setCorsHeaders, videoStates);
837
+ require_video.handleVideoStatus(req, res, videoId, journal, setCorsHeaders, videoStates);
809
838
  return;
810
839
  }
811
840
  const geminiPredictMatch = pathname.match(GEMINI_PREDICT_RE);
812
841
  if (geminiPredictMatch && req.method === "POST") {
813
842
  const predictModel = geminiPredictMatch[1];
814
- readBody(req).then((raw) => require_images.handleImages(req, res, raw, fixtures, journal, defaults, setCorsHeaders, "gemini", predictModel)).catch((err) => {
843
+ try {
844
+ await require_images.handleImages(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders, "gemini", predictModel);
845
+ } catch (err) {
815
846
  const msg = err instanceof Error ? err.message : "Internal error";
816
847
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
817
848
  message: msg,
818
849
  type: "server_error"
819
850
  } }));
820
851
  else if (!res.writableEnded) res.destroy();
821
- });
852
+ }
822
853
  return;
823
854
  }
824
855
  const geminiMatch = pathname.match(GEMINI_PATH_RE);
825
856
  if (geminiMatch && req.method === "POST") {
826
857
  const geminiModel = geminiMatch[1];
827
858
  const streaming = geminiMatch[2] === "streamGenerateContent";
828
- readBody(req).then((raw) => require_gemini.handleGemini(req, res, raw, geminiModel, streaming, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
859
+ try {
860
+ await require_gemini.handleGemini(req, res, await readBody(req), geminiModel, streaming, fixtures, journal, defaults, setCorsHeaders);
861
+ } catch (err) {
829
862
  const msg = err instanceof Error ? err.message : "Internal error";
830
863
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
831
864
  message: msg,
@@ -839,14 +872,16 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
839
872
  }
840
873
  res.end();
841
874
  }
842
- });
875
+ }
843
876
  return;
844
877
  }
845
878
  const vertexMatch = pathname.match(VERTEX_AI_RE);
846
879
  if (vertexMatch && req.method === "POST") {
847
880
  const vertexModel = vertexMatch[1];
848
881
  const streaming = vertexMatch[2] === "streamGenerateContent";
849
- readBody(req).then((raw) => require_gemini.handleGemini(req, res, raw, vertexModel, streaming, fixtures, journal, defaults, setCorsHeaders, "vertexai")).catch((err) => {
882
+ try {
883
+ await require_gemini.handleGemini(req, res, await readBody(req), vertexModel, streaming, fixtures, journal, defaults, setCorsHeaders, "vertexai");
884
+ } catch (err) {
850
885
  const msg = err instanceof Error ? err.message : "Internal error";
851
886
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
852
887
  message: msg,
@@ -860,81 +895,93 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
860
895
  }
861
896
  res.end();
862
897
  }
863
- });
898
+ }
864
899
  return;
865
900
  }
866
901
  const bedrockMatch = pathname.match(BEDROCK_INVOKE_RE);
867
902
  if (bedrockMatch && req.method === "POST") {
868
903
  const bedrockModelId = bedrockMatch[1];
869
- readBody(req).then((raw) => require_bedrock.handleBedrock(req, res, raw, bedrockModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
904
+ try {
905
+ await require_bedrock.handleBedrock(req, res, await readBody(req), bedrockModelId, fixtures, journal, defaults, setCorsHeaders);
906
+ } catch (err) {
870
907
  const msg = err instanceof Error ? err.message : "Internal error";
871
908
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
872
909
  message: msg,
873
910
  type: "server_error"
874
911
  } }));
875
912
  else if (!res.writableEnded) res.destroy();
876
- });
913
+ }
877
914
  return;
878
915
  }
879
916
  const bedrockStreamMatch = pathname.match(BEDROCK_STREAM_RE);
880
917
  if (bedrockStreamMatch && req.method === "POST") {
881
918
  const bedrockModelId = bedrockStreamMatch[1];
882
- readBody(req).then((raw) => require_bedrock.handleBedrockStream(req, res, raw, bedrockModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
919
+ try {
920
+ await require_bedrock.handleBedrockStream(req, res, await readBody(req), bedrockModelId, fixtures, journal, defaults, setCorsHeaders);
921
+ } catch (err) {
883
922
  const msg = err instanceof Error ? err.message : "Internal error";
884
923
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
885
924
  message: msg,
886
925
  type: "server_error"
887
926
  } }));
888
927
  else if (!res.writableEnded) res.destroy();
889
- });
928
+ }
890
929
  return;
891
930
  }
892
931
  const converseMatch = pathname.match(BEDROCK_CONVERSE_RE);
893
932
  if (converseMatch && req.method === "POST") {
894
933
  const converseModelId = converseMatch[1];
895
- readBody(req).then((raw) => require_bedrock_converse.handleConverse(req, res, raw, converseModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
934
+ try {
935
+ await require_bedrock_converse.handleConverse(req, res, await readBody(req), converseModelId, fixtures, journal, defaults, setCorsHeaders);
936
+ } catch (err) {
896
937
  const msg = err instanceof Error ? err.message : "Internal error";
897
938
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
898
939
  message: msg,
899
940
  type: "server_error"
900
941
  } }));
901
942
  else if (!res.writableEnded) res.destroy();
902
- });
943
+ }
903
944
  return;
904
945
  }
905
946
  const converseStreamMatch = pathname.match(BEDROCK_CONVERSE_STREAM_RE);
906
947
  if (converseStreamMatch && req.method === "POST") {
907
948
  const converseStreamModelId = converseStreamMatch[1];
908
- readBody(req).then((raw) => require_bedrock_converse.handleConverseStream(req, res, raw, converseStreamModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
949
+ try {
950
+ await require_bedrock_converse.handleConverseStream(req, res, await readBody(req), converseStreamModelId, fixtures, journal, defaults, setCorsHeaders);
951
+ } catch (err) {
909
952
  const msg = err instanceof Error ? err.message : "Internal error";
910
953
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
911
954
  message: msg,
912
955
  type: "server_error"
913
956
  } }));
914
957
  else if (!res.writableEnded) res.destroy();
915
- });
958
+ }
916
959
  return;
917
960
  }
918
961
  if (pathname === OLLAMA_CHAT_PATH && req.method === "POST") {
919
- readBody(req).then((raw) => require_ollama.handleOllama(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
962
+ try {
963
+ await require_ollama.handleOllama(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
964
+ } catch (err) {
920
965
  const msg = err instanceof Error ? err.message : "Internal error";
921
966
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
922
967
  message: msg,
923
968
  type: "server_error"
924
969
  } }));
925
970
  else if (!res.writableEnded) res.destroy();
926
- });
971
+ }
927
972
  return;
928
973
  }
929
974
  if (pathname === OLLAMA_GENERATE_PATH && req.method === "POST") {
930
- readBody(req).then((raw) => require_ollama.handleOllamaGenerate(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
975
+ try {
976
+ await require_ollama.handleOllamaGenerate(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
977
+ } catch (err) {
931
978
  const msg = err instanceof Error ? err.message : "Internal error";
932
979
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
933
980
  message: msg,
934
981
  type: "server_error"
935
982
  } }));
936
983
  else if (!res.writableEnded) res.destroy();
937
- });
984
+ }
938
985
  return;
939
986
  }
940
987
  if (pathname === OLLAMA_TAGS_PATH && req.method === "GET") {
@@ -954,36 +1001,42 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
954
1001
  return;
955
1002
  }
956
1003
  if (pathname === SEARCH_PATH && req.method === "POST") {
957
- readBody(req).then((raw) => require_search.handleSearch(req, res, raw, serviceFixtures?.search ?? [], journal, defaults, setCorsHeaders)).catch((err) => {
1004
+ try {
1005
+ await require_search.handleSearch(req, res, await readBody(req), serviceFixtures?.search ?? [], journal, defaults, setCorsHeaders);
1006
+ } catch (err) {
958
1007
  const msg = err instanceof Error ? err.message : "Internal error";
959
1008
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
960
1009
  message: msg,
961
1010
  type: "server_error"
962
1011
  } }));
963
1012
  else if (!res.writableEnded) res.destroy();
964
- });
1013
+ }
965
1014
  return;
966
1015
  }
967
1016
  if (pathname === RERANK_PATH && req.method === "POST") {
968
- readBody(req).then((raw) => require_rerank.handleRerank(req, res, raw, serviceFixtures?.rerank ?? [], journal, defaults, setCorsHeaders)).catch((err) => {
1017
+ try {
1018
+ await require_rerank.handleRerank(req, res, await readBody(req), serviceFixtures?.rerank ?? [], journal, defaults, setCorsHeaders);
1019
+ } catch (err) {
969
1020
  const msg = err instanceof Error ? err.message : "Internal error";
970
1021
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
971
1022
  message: msg,
972
1023
  type: "server_error"
973
1024
  } }));
974
1025
  else if (!res.writableEnded) res.destroy();
975
- });
1026
+ }
976
1027
  return;
977
1028
  }
978
1029
  if (pathname === MODERATIONS_PATH && req.method === "POST") {
979
- readBody(req).then((raw) => require_moderation.handleModeration(req, res, raw, serviceFixtures?.moderation ?? [], journal, defaults, setCorsHeaders)).catch((err) => {
1030
+ try {
1031
+ await require_moderation.handleModeration(req, res, await readBody(req), serviceFixtures?.moderation ?? [], journal, defaults, setCorsHeaders);
1032
+ } catch (err) {
980
1033
  const msg = err instanceof Error ? err.message : "Internal error";
981
1034
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
982
1035
  message: msg,
983
1036
  type: "server_error"
984
1037
  } }));
985
1038
  else if (!res.writableEnded) res.destroy();
986
- });
1039
+ }
987
1040
  return;
988
1041
  }
989
1042
  if (pathname !== COMPLETIONS_PATH) {
@@ -994,7 +1047,10 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
994
1047
  handleNotFound(res, "Not found");
995
1048
  return;
996
1049
  }
997
- handleCompletions(req, res, fixtures, journal, defaults, azureDeploymentId, azureDeploymentId ? "azure" : "openai").catch((err) => {
1050
+ const completionsProvider = azureDeploymentId ? "azure" : "openai";
1051
+ try {
1052
+ await handleCompletions(req, res, fixtures, journal, defaults, azureDeploymentId, completionsProvider);
1053
+ } catch (err) {
998
1054
  const msg = err instanceof Error ? err.message : "Internal error";
999
1055
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
1000
1056
  message: msg,
@@ -1011,7 +1067,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
1011
1067
  }
1012
1068
  res.end();
1013
1069
  }
1014
- });
1070
+ }
1015
1071
  }
1016
1072
  const activeConnections = /* @__PURE__ */ new Set();
1017
1073
  server.on("upgrade", (req, socket, head) => {