@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.js CHANGED
@@ -4,7 +4,7 @@ import { matchFixture } from "./router.js";
4
4
  import { entryToFixture, validateFixtures } from "./fixture-loader.js";
5
5
  import { writeErrorResponse, writeSSEStream } from "./sse-writer.js";
6
6
  import { createInterruptionSignal } from "./interruption.js";
7
- import { applyChaosAction, evaluateChaos } from "./chaos.js";
7
+ import { applyChaos } from "./chaos.js";
8
8
  import { proxyAndRecord } from "./recorder.js";
9
9
  import { handleResponses } from "./responses.js";
10
10
  import { handleMessages } from "./messages.js";
@@ -15,7 +15,7 @@ import { handleEmbeddings } from "./embeddings.js";
15
15
  import { handleImages } from "./images.js";
16
16
  import { handleSpeech } from "./speech.js";
17
17
  import { handleTranscription } from "./transcription.js";
18
- import { handleVideoCreate, handleVideoStatus } from "./video.js";
18
+ import { VideoStateMap, handleVideoCreate, handleVideoStatus } from "./video.js";
19
19
  import { handleOllama, handleOllamaGenerate } from "./ollama.js";
20
20
  import { handleCohere } from "./cohere.js";
21
21
  import { handleSearch } from "./search.js";
@@ -95,14 +95,24 @@ const DEFAULT_MODELS = [
95
95
  const CORS_HEADERS = {
96
96
  "Access-Control-Allow-Origin": "*",
97
97
  "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
98
- "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Aimock-Chaos-Drop, X-Aimock-Chaos-Malformed, X-Aimock-Chaos-Disconnect, X-Test-Id"
98
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
99
99
  };
100
100
  function setCorsHeaders(res) {
101
101
  for (const [key, value] of Object.entries(CORS_HEADERS)) res.setHeader(key, value);
102
102
  }
103
- async function readBody(req) {
103
+ const DEFAULT_MAX_BODY_BYTES = 10 * 1024 * 1024;
104
+ async function readBody(req, maxBytes = DEFAULT_MAX_BODY_BYTES) {
104
105
  const buffers = [];
105
- for await (const chunk of req) buffers.push(chunk);
106
+ let totalBytes = 0;
107
+ for await (const chunk of req) {
108
+ const buf = chunk;
109
+ totalBytes += buf.length;
110
+ if (totalBytes > maxBytes) {
111
+ req.destroy();
112
+ throw new Error(`Request body exceeded size limit of ${maxBytes} bytes`);
113
+ }
114
+ buffers.push(buf);
115
+ }
106
116
  return Buffer.concat(buffers).toString();
107
117
  }
108
118
  function handleOptions(res) {
@@ -122,7 +132,7 @@ const CONTROL_PREFIX = "/__aimock";
122
132
  * Handle requests under `/__aimock/`. Returns `true` if the request was
123
133
  * handled, `false` if the path doesn't match the control prefix.
124
134
  */
125
- async function handleControlAPI(req, res, pathname, fixtures, journal, videoStates) {
135
+ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStates, defaults) {
126
136
  if (!pathname.startsWith(CONTROL_PREFIX)) return false;
127
137
  const subPath = pathname.slice(9);
128
138
  setCorsHeaders(res);
@@ -140,17 +150,21 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
140
150
  let raw;
141
151
  try {
142
152
  raw = await readBody(req);
143
- } catch {
153
+ } catch (err) {
154
+ const msg = err instanceof Error ? err.message : String(err);
155
+ defaults.logger.error(`POST /__aimock/fixtures: failed to read body: ${msg}`);
144
156
  res.writeHead(400, { "Content-Type": "application/json" });
145
- res.end(JSON.stringify({ error: "Failed to read request body" }));
157
+ res.end(JSON.stringify({ error: `Failed to read request body: ${msg}` }));
146
158
  return true;
147
159
  }
148
160
  let parsed;
149
161
  try {
150
162
  parsed = JSON.parse(raw);
151
- } catch {
163
+ } catch (err) {
164
+ const msg = err instanceof Error ? err.message : String(err);
165
+ defaults.logger.error(`POST /__aimock/fixtures: invalid JSON: ${msg}`);
152
166
  res.writeHead(400, { "Content-Type": "application/json" });
153
- res.end(JSON.stringify({ error: "Invalid JSON" }));
167
+ res.end(JSON.stringify({ error: `Invalid JSON: ${msg}` }));
154
168
  return true;
155
169
  }
156
170
  if (!Array.isArray(parsed.fixtures)) {
@@ -169,12 +183,14 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
169
183
  return true;
170
184
  }
171
185
  fixtures.push(...converted);
186
+ if (defaults.registry) defaults.registry.setGauge("aimock_fixtures_loaded", {}, fixtures.length);
172
187
  res.writeHead(200, { "Content-Type": "application/json" });
173
188
  res.end(JSON.stringify({ added: converted.length }));
174
189
  return true;
175
190
  }
176
191
  if (subPath === "/fixtures" && req.method === "DELETE") {
177
192
  fixtures.length = 0;
193
+ if (defaults.registry) defaults.registry.setGauge("aimock_fixtures_loaded", {}, fixtures.length);
178
194
  res.writeHead(200, { "Content-Type": "application/json" });
179
195
  res.end(JSON.stringify({ cleared: true }));
180
196
  return true;
@@ -183,6 +199,7 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
183
199
  fixtures.length = 0;
184
200
  journal.clear();
185
201
  videoStates.clear();
202
+ if (defaults.registry) defaults.registry.setGauge("aimock_fixtures_loaded", {}, fixtures.length);
186
203
  res.writeHead(200, { "Content-Type": "application/json" });
187
204
  res.end(JSON.stringify({ reset: true }));
188
205
  return true;
@@ -191,17 +208,21 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
191
208
  let raw;
192
209
  try {
193
210
  raw = await readBody(req);
194
- } catch {
211
+ } catch (err) {
212
+ const msg = err instanceof Error ? err.message : String(err);
213
+ defaults.logger.error(`POST /__aimock/error: failed to read body: ${msg}`);
195
214
  res.writeHead(400, { "Content-Type": "application/json" });
196
- res.end(JSON.stringify({ error: "Failed to read request body" }));
215
+ res.end(JSON.stringify({ error: `Failed to read request body: ${msg}` }));
197
216
  return true;
198
217
  }
199
218
  let parsed;
200
219
  try {
201
220
  parsed = JSON.parse(raw);
202
- } catch {
221
+ } catch (err) {
222
+ const msg = err instanceof Error ? err.message : String(err);
223
+ defaults.logger.error(`POST /__aimock/error: invalid JSON: ${msg}`);
203
224
  res.writeHead(400, { "Content-Type": "application/json" });
204
- res.end(JSON.stringify({ error: "Invalid JSON" }));
225
+ res.end(JSON.stringify({ error: `Invalid JSON: ${msg}` }));
205
226
  return true;
206
227
  }
207
228
  const status = parsed.status ?? 500;
@@ -221,10 +242,10 @@ async function handleControlAPI(req, res, pathname, fixtures, journal, videoStat
221
242
  const original = errorFixture.match.predicate;
222
243
  errorFixture.match.predicate = (req) => {
223
244
  const result = original(req);
224
- if (result) queueMicrotask(() => {
245
+ if (result) {
225
246
  const idx = fixtures.indexOf(errorFixture);
226
247
  if (idx !== -1) fixtures.splice(idx, 1);
227
- });
248
+ }
228
249
  return result;
229
250
  };
230
251
  res.writeHead(200, { "Content-Type": "application/json" });
@@ -279,47 +300,39 @@ async function handleCompletions(req, res, fixtures, journal, defaults, modelFal
279
300
  } }));
280
301
  return;
281
302
  }
282
- const method = req.method ?? "POST";
283
- const path = req.url ?? COMPLETIONS_PATH;
284
- const flatHeaders = flattenHeaders(req.headers);
303
+ if (!Array.isArray(body.messages)) {
304
+ journal.add({
305
+ method: req.method ?? "POST",
306
+ path: req.url ?? COMPLETIONS_PATH,
307
+ headers: flattenHeaders(req.headers),
308
+ body: null,
309
+ response: {
310
+ status: 400,
311
+ fixture: null
312
+ }
313
+ });
314
+ writeErrorResponse(res, 400, JSON.stringify({ error: {
315
+ message: "Missing required parameter: 'messages'",
316
+ type: "invalid_request_error"
317
+ } }));
318
+ return;
319
+ }
285
320
  body._endpointType = "chat";
286
321
  const testId = getTestId(req);
287
322
  const fixture = matchFixture(fixtures, body, journal.getFixtureMatchCountsForTest(testId), defaults.requestTransform);
288
323
  if (fixture) journal.incrementFixtureMatchCount(fixture, fixtures, testId);
289
- const chaosAction = evaluateChaos(fixture, defaults.chaos, req.headers, defaults.logger);
290
- const chaosContext = {
324
+ const method = req.method ?? "POST";
325
+ const path = req.url ?? COMPLETIONS_PATH;
326
+ const flatHeaders = flattenHeaders(req.headers);
327
+ if (applyChaos(res, fixture, defaults.chaos, req.headers, journal, {
291
328
  method,
292
329
  path,
293
330
  headers: flatHeaders,
294
331
  body
295
- };
296
- if (chaosAction === "drop" || chaosAction === "disconnect") {
297
- applyChaosAction(chaosAction, res, fixture, journal, chaosContext, fixture ? "fixture" : "proxy", defaults.registry);
298
- return;
299
- }
300
- if (fixture && chaosAction === "malformed") {
301
- applyChaosAction(chaosAction, res, fixture, journal, chaosContext, "fixture", defaults.registry);
302
- return;
303
- }
332
+ }, defaults.registry, defaults.logger)) return;
304
333
  if (!fixture) {
305
334
  if (defaults.record && providerKey) {
306
- const hookOptions = chaosAction === "malformed" ? {
307
- beforeWriteResponse: () => {
308
- applyChaosAction(chaosAction, res, null, journal, chaosContext, "proxy", defaults.registry);
309
- return true;
310
- },
311
- onHookBypassed: (reason) => {
312
- defaults.logger.warn(`[chaos] malformed bypassed on proxy: upstream returned SSE (${reason})`);
313
- defaults.registry?.incrementCounter("aimock_chaos_bypassed_total", {
314
- action: "malformed",
315
- source: "proxy",
316
- reason
317
- });
318
- }
319
- } : void 0;
320
- const outcome = await proxyAndRecord(req, res, body, providerKey, req.url ?? COMPLETIONS_PATH, fixtures, defaults, raw, hookOptions);
321
- if (outcome === "handled_by_hook") return;
322
- if (outcome === "relayed") {
335
+ if (await proxyAndRecord(req, res, body, providerKey, req.url ?? COMPLETIONS_PATH, fixtures, defaults, raw)) {
323
336
  journal.add({
324
337
  method: req.method ?? "POST",
325
338
  path: req.url ?? COMPLETIONS_PATH,
@@ -533,7 +546,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
533
546
  maxEntries: options?.journalMaxEntries ?? 1e3,
534
547
  fixtureCountsMaxTestIds: options?.fixtureCountsMaxTestIds ?? 500
535
548
  });
536
- const videoStates = /* @__PURE__ */ new Map();
549
+ const videoStates = new VideoStateMap();
537
550
  if (mounts) for (const { handler } of mounts) {
538
551
  if (handler.setJournal) handler.setJournal(journal);
539
552
  if (registry && handler.setRegistry) handler.setRegistry(registry);
@@ -580,7 +593,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
580
593
  }
581
594
  });
582
595
  if (pathname.startsWith(CONTROL_PREFIX)) {
583
- await handleControlAPI(req, res, pathname, fixtures, journal, videoStates);
596
+ await handleControlAPI(req, res, pathname, fixtures, journal, videoStates, defaults);
584
597
  return;
585
598
  }
586
599
  if (mounts) {
@@ -682,7 +695,9 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
682
695
  return;
683
696
  }
684
697
  if (pathname === RESPONSES_PATH && req.method === "POST") {
685
- readBody(req).then((raw) => handleResponses(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
698
+ try {
699
+ await handleResponses(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
700
+ } catch (err) {
686
701
  const msg = err instanceof Error ? err.message : "Internal error";
687
702
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
688
703
  message: msg,
@@ -696,11 +711,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
696
711
  }
697
712
  res.end();
698
713
  }
699
- });
714
+ }
700
715
  return;
701
716
  }
702
717
  if (pathname === MESSAGES_PATH && req.method === "POST") {
703
- readBody(req).then((raw) => handleMessages(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
718
+ try {
719
+ await handleMessages(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
720
+ } catch (err) {
704
721
  const msg = err instanceof Error ? err.message : "Internal error";
705
722
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
706
723
  message: msg,
@@ -714,11 +731,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
714
731
  }
715
732
  res.end();
716
733
  }
717
- });
734
+ }
718
735
  return;
719
736
  }
720
737
  if (pathname === COHERE_CHAT_PATH && req.method === "POST") {
721
- readBody(req).then((raw) => handleCohere(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
738
+ try {
739
+ await handleCohere(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
740
+ } catch (err) {
722
741
  const msg = err instanceof Error ? err.message : "Internal error";
723
742
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
724
743
  message: msg,
@@ -732,98 +751,112 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
732
751
  }
733
752
  res.end();
734
753
  }
735
- });
754
+ }
736
755
  return;
737
756
  }
738
757
  if (pathname === EMBEDDINGS_PATH && req.method === "POST") {
739
- const deploymentId = azureDeploymentId;
740
- readBody(req).then((raw) => {
758
+ try {
759
+ const deploymentId = azureDeploymentId;
760
+ const embeddingsProvider = azureDeploymentId ? "azure" : "openai";
761
+ let raw = await readBody(req);
741
762
  if (deploymentId) try {
742
763
  const parsed = JSON.parse(raw);
743
764
  if (!parsed.model) {
744
765
  parsed.model = deploymentId;
745
- return handleEmbeddings(req, res, JSON.stringify(parsed), fixtures, journal, defaults, setCorsHeaders);
766
+ raw = JSON.stringify(parsed);
746
767
  }
747
768
  } catch {}
748
- return handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders);
749
- }).catch((err) => {
769
+ await handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders, embeddingsProvider);
770
+ } catch (err) {
750
771
  const msg = err instanceof Error ? err.message : "Internal error";
751
772
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
752
773
  message: msg,
753
774
  type: "server_error"
754
775
  } }));
755
776
  else if (!res.writableEnded) res.destroy();
756
- });
777
+ }
757
778
  return;
758
779
  }
759
780
  if (pathname === IMAGES_PATH && req.method === "POST") {
760
- readBody(req).then((raw) => handleImages(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
781
+ try {
782
+ await handleImages(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
783
+ } catch (err) {
761
784
  const msg = err instanceof Error ? err.message : "Internal error";
762
785
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
763
786
  message: msg,
764
787
  type: "server_error"
765
788
  } }));
766
789
  else if (!res.writableEnded) res.destroy();
767
- });
790
+ }
768
791
  return;
769
792
  }
770
793
  if (pathname === SPEECH_PATH && req.method === "POST") {
771
- readBody(req).then((raw) => handleSpeech(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
794
+ try {
795
+ await handleSpeech(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
796
+ } catch (err) {
772
797
  const msg = err instanceof Error ? err.message : "Internal error";
773
798
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
774
799
  message: msg,
775
800
  type: "server_error"
776
801
  } }));
777
802
  else if (!res.writableEnded) res.destroy();
778
- });
803
+ }
779
804
  return;
780
805
  }
781
806
  if (pathname === TRANSCRIPTIONS_PATH && req.method === "POST") {
782
- readBody(req).then((raw) => handleTranscription(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
807
+ try {
808
+ await handleTranscription(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
809
+ } catch (err) {
783
810
  const msg = err instanceof Error ? err.message : "Internal error";
784
811
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
785
812
  message: msg,
786
813
  type: "server_error"
787
814
  } }));
788
815
  else if (!res.writableEnded) res.destroy();
789
- });
816
+ }
790
817
  return;
791
818
  }
792
819
  if (pathname === VIDEOS_PATH && req.method === "POST") {
793
- readBody(req).then((raw) => handleVideoCreate(req, res, raw, fixtures, journal, defaults, setCorsHeaders, videoStates)).catch((err) => {
820
+ try {
821
+ await handleVideoCreate(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders, videoStates);
822
+ } catch (err) {
794
823
  const msg = err instanceof Error ? err.message : "Internal error";
795
824
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
796
825
  message: msg,
797
826
  type: "server_error"
798
827
  } }));
799
828
  else if (!res.writableEnded) res.destroy();
800
- });
829
+ }
801
830
  return;
802
831
  }
803
832
  const videoStatusMatch = pathname.match(VIDEOS_STATUS_RE);
804
833
  if (videoStatusMatch && req.method === "GET") {
805
834
  const videoId = videoStatusMatch[1];
806
- handleVideoStatus(req, res, videoId, journal, defaults, setCorsHeaders, videoStates);
835
+ handleVideoStatus(req, res, videoId, journal, setCorsHeaders, videoStates);
807
836
  return;
808
837
  }
809
838
  const geminiPredictMatch = pathname.match(GEMINI_PREDICT_RE);
810
839
  if (geminiPredictMatch && req.method === "POST") {
811
840
  const predictModel = geminiPredictMatch[1];
812
- readBody(req).then((raw) => handleImages(req, res, raw, fixtures, journal, defaults, setCorsHeaders, "gemini", predictModel)).catch((err) => {
841
+ try {
842
+ await handleImages(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders, "gemini", predictModel);
843
+ } catch (err) {
813
844
  const msg = err instanceof Error ? err.message : "Internal error";
814
845
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
815
846
  message: msg,
816
847
  type: "server_error"
817
848
  } }));
818
849
  else if (!res.writableEnded) res.destroy();
819
- });
850
+ }
820
851
  return;
821
852
  }
822
853
  const geminiMatch = pathname.match(GEMINI_PATH_RE);
823
854
  if (geminiMatch && req.method === "POST") {
824
855
  const geminiModel = geminiMatch[1];
825
856
  const streaming = geminiMatch[2] === "streamGenerateContent";
826
- readBody(req).then((raw) => handleGemini(req, res, raw, geminiModel, streaming, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
857
+ try {
858
+ await handleGemini(req, res, await readBody(req), geminiModel, streaming, fixtures, journal, defaults, setCorsHeaders);
859
+ } catch (err) {
827
860
  const msg = err instanceof Error ? err.message : "Internal error";
828
861
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
829
862
  message: msg,
@@ -837,14 +870,16 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
837
870
  }
838
871
  res.end();
839
872
  }
840
- });
873
+ }
841
874
  return;
842
875
  }
843
876
  const vertexMatch = pathname.match(VERTEX_AI_RE);
844
877
  if (vertexMatch && req.method === "POST") {
845
878
  const vertexModel = vertexMatch[1];
846
879
  const streaming = vertexMatch[2] === "streamGenerateContent";
847
- readBody(req).then((raw) => handleGemini(req, res, raw, vertexModel, streaming, fixtures, journal, defaults, setCorsHeaders, "vertexai")).catch((err) => {
880
+ try {
881
+ await handleGemini(req, res, await readBody(req), vertexModel, streaming, fixtures, journal, defaults, setCorsHeaders, "vertexai");
882
+ } catch (err) {
848
883
  const msg = err instanceof Error ? err.message : "Internal error";
849
884
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
850
885
  message: msg,
@@ -858,81 +893,93 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
858
893
  }
859
894
  res.end();
860
895
  }
861
- });
896
+ }
862
897
  return;
863
898
  }
864
899
  const bedrockMatch = pathname.match(BEDROCK_INVOKE_RE);
865
900
  if (bedrockMatch && req.method === "POST") {
866
901
  const bedrockModelId = bedrockMatch[1];
867
- readBody(req).then((raw) => handleBedrock(req, res, raw, bedrockModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
902
+ try {
903
+ await handleBedrock(req, res, await readBody(req), bedrockModelId, fixtures, journal, defaults, setCorsHeaders);
904
+ } catch (err) {
868
905
  const msg = err instanceof Error ? err.message : "Internal error";
869
906
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
870
907
  message: msg,
871
908
  type: "server_error"
872
909
  } }));
873
910
  else if (!res.writableEnded) res.destroy();
874
- });
911
+ }
875
912
  return;
876
913
  }
877
914
  const bedrockStreamMatch = pathname.match(BEDROCK_STREAM_RE);
878
915
  if (bedrockStreamMatch && req.method === "POST") {
879
916
  const bedrockModelId = bedrockStreamMatch[1];
880
- readBody(req).then((raw) => handleBedrockStream(req, res, raw, bedrockModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
917
+ try {
918
+ await handleBedrockStream(req, res, await readBody(req), bedrockModelId, fixtures, journal, defaults, setCorsHeaders);
919
+ } catch (err) {
881
920
  const msg = err instanceof Error ? err.message : "Internal error";
882
921
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
883
922
  message: msg,
884
923
  type: "server_error"
885
924
  } }));
886
925
  else if (!res.writableEnded) res.destroy();
887
- });
926
+ }
888
927
  return;
889
928
  }
890
929
  const converseMatch = pathname.match(BEDROCK_CONVERSE_RE);
891
930
  if (converseMatch && req.method === "POST") {
892
931
  const converseModelId = converseMatch[1];
893
- readBody(req).then((raw) => handleConverse(req, res, raw, converseModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
932
+ try {
933
+ await handleConverse(req, res, await readBody(req), converseModelId, fixtures, journal, defaults, setCorsHeaders);
934
+ } catch (err) {
894
935
  const msg = err instanceof Error ? err.message : "Internal error";
895
936
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
896
937
  message: msg,
897
938
  type: "server_error"
898
939
  } }));
899
940
  else if (!res.writableEnded) res.destroy();
900
- });
941
+ }
901
942
  return;
902
943
  }
903
944
  const converseStreamMatch = pathname.match(BEDROCK_CONVERSE_STREAM_RE);
904
945
  if (converseStreamMatch && req.method === "POST") {
905
946
  const converseStreamModelId = converseStreamMatch[1];
906
- readBody(req).then((raw) => handleConverseStream(req, res, raw, converseStreamModelId, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
947
+ try {
948
+ await handleConverseStream(req, res, await readBody(req), converseStreamModelId, fixtures, journal, defaults, setCorsHeaders);
949
+ } catch (err) {
907
950
  const msg = err instanceof Error ? err.message : "Internal error";
908
951
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
909
952
  message: msg,
910
953
  type: "server_error"
911
954
  } }));
912
955
  else if (!res.writableEnded) res.destroy();
913
- });
956
+ }
914
957
  return;
915
958
  }
916
959
  if (pathname === OLLAMA_CHAT_PATH && req.method === "POST") {
917
- readBody(req).then((raw) => handleOllama(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
960
+ try {
961
+ await handleOllama(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
962
+ } catch (err) {
918
963
  const msg = err instanceof Error ? err.message : "Internal error";
919
964
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
920
965
  message: msg,
921
966
  type: "server_error"
922
967
  } }));
923
968
  else if (!res.writableEnded) res.destroy();
924
- });
969
+ }
925
970
  return;
926
971
  }
927
972
  if (pathname === OLLAMA_GENERATE_PATH && req.method === "POST") {
928
- readBody(req).then((raw) => handleOllamaGenerate(req, res, raw, fixtures, journal, defaults, setCorsHeaders)).catch((err) => {
973
+ try {
974
+ await handleOllamaGenerate(req, res, await readBody(req), fixtures, journal, defaults, setCorsHeaders);
975
+ } catch (err) {
929
976
  const msg = err instanceof Error ? err.message : "Internal error";
930
977
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
931
978
  message: msg,
932
979
  type: "server_error"
933
980
  } }));
934
981
  else if (!res.writableEnded) res.destroy();
935
- });
982
+ }
936
983
  return;
937
984
  }
938
985
  if (pathname === OLLAMA_TAGS_PATH && req.method === "GET") {
@@ -952,36 +999,42 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
952
999
  return;
953
1000
  }
954
1001
  if (pathname === SEARCH_PATH && req.method === "POST") {
955
- readBody(req).then((raw) => handleSearch(req, res, raw, serviceFixtures?.search ?? [], journal, defaults, setCorsHeaders)).catch((err) => {
1002
+ try {
1003
+ await handleSearch(req, res, await readBody(req), serviceFixtures?.search ?? [], journal, defaults, setCorsHeaders);
1004
+ } catch (err) {
956
1005
  const msg = err instanceof Error ? err.message : "Internal error";
957
1006
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
958
1007
  message: msg,
959
1008
  type: "server_error"
960
1009
  } }));
961
1010
  else if (!res.writableEnded) res.destroy();
962
- });
1011
+ }
963
1012
  return;
964
1013
  }
965
1014
  if (pathname === RERANK_PATH && req.method === "POST") {
966
- readBody(req).then((raw) => handleRerank(req, res, raw, serviceFixtures?.rerank ?? [], journal, defaults, setCorsHeaders)).catch((err) => {
1015
+ try {
1016
+ await handleRerank(req, res, await readBody(req), serviceFixtures?.rerank ?? [], journal, defaults, setCorsHeaders);
1017
+ } catch (err) {
967
1018
  const msg = err instanceof Error ? err.message : "Internal error";
968
1019
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
969
1020
  message: msg,
970
1021
  type: "server_error"
971
1022
  } }));
972
1023
  else if (!res.writableEnded) res.destroy();
973
- });
1024
+ }
974
1025
  return;
975
1026
  }
976
1027
  if (pathname === MODERATIONS_PATH && req.method === "POST") {
977
- readBody(req).then((raw) => handleModeration(req, res, raw, serviceFixtures?.moderation ?? [], journal, defaults, setCorsHeaders)).catch((err) => {
1028
+ try {
1029
+ await handleModeration(req, res, await readBody(req), serviceFixtures?.moderation ?? [], journal, defaults, setCorsHeaders);
1030
+ } catch (err) {
978
1031
  const msg = err instanceof Error ? err.message : "Internal error";
979
1032
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
980
1033
  message: msg,
981
1034
  type: "server_error"
982
1035
  } }));
983
1036
  else if (!res.writableEnded) res.destroy();
984
- });
1037
+ }
985
1038
  return;
986
1039
  }
987
1040
  if (pathname !== COMPLETIONS_PATH) {
@@ -992,7 +1045,10 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
992
1045
  handleNotFound(res, "Not found");
993
1046
  return;
994
1047
  }
995
- handleCompletions(req, res, fixtures, journal, defaults, azureDeploymentId, azureDeploymentId ? "azure" : "openai").catch((err) => {
1048
+ const completionsProvider = azureDeploymentId ? "azure" : "openai";
1049
+ try {
1050
+ await handleCompletions(req, res, fixtures, journal, defaults, azureDeploymentId, completionsProvider);
1051
+ } catch (err) {
996
1052
  const msg = err instanceof Error ? err.message : "Internal error";
997
1053
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
998
1054
  message: msg,
@@ -1009,7 +1065,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
1009
1065
  }
1010
1066
  res.end();
1011
1067
  }
1012
- });
1068
+ }
1013
1069
  }
1014
1070
  const activeConnections = /* @__PURE__ */ new Set();
1015
1071
  server.on("upgrade", (req, socket, head) => {