@copilotkit/aimock 1.14.9 → 1.15.1

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 (119) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/dist/bedrock-converse.cjs +129 -21
  3. package/dist/bedrock-converse.cjs.map +1 -1
  4. package/dist/bedrock-converse.d.cts.map +1 -1
  5. package/dist/bedrock-converse.d.ts.map +1 -1
  6. package/dist/bedrock-converse.js +131 -23
  7. package/dist/bedrock-converse.js.map +1 -1
  8. package/dist/bedrock.cjs +258 -44
  9. package/dist/bedrock.cjs.map +1 -1
  10. package/dist/bedrock.d.cts.map +1 -1
  11. package/dist/bedrock.d.ts.map +1 -1
  12. package/dist/bedrock.js +259 -46
  13. package/dist/bedrock.js.map +1 -1
  14. package/dist/cohere.cjs +287 -31
  15. package/dist/cohere.cjs.map +1 -1
  16. package/dist/cohere.d.cts +9 -0
  17. package/dist/cohere.d.cts.map +1 -1
  18. package/dist/cohere.d.ts +9 -0
  19. package/dist/cohere.d.ts.map +1 -1
  20. package/dist/cohere.js +288 -32
  21. package/dist/cohere.js.map +1 -1
  22. package/dist/config-loader.d.ts.map +1 -1
  23. package/dist/embeddings.cjs +21 -3
  24. package/dist/embeddings.cjs.map +1 -1
  25. package/dist/embeddings.d.cts +2 -2
  26. package/dist/embeddings.d.cts.map +1 -1
  27. package/dist/embeddings.d.ts +2 -2
  28. package/dist/embeddings.d.ts.map +1 -1
  29. package/dist/embeddings.js +21 -3
  30. package/dist/embeddings.js.map +1 -1
  31. package/dist/gemini.cjs +46 -43
  32. package/dist/gemini.cjs.map +1 -1
  33. package/dist/gemini.d.cts.map +1 -1
  34. package/dist/gemini.d.ts.map +1 -1
  35. package/dist/gemini.js +46 -43
  36. package/dist/gemini.js.map +1 -1
  37. package/dist/helpers.cjs +9 -0
  38. package/dist/helpers.cjs.map +1 -1
  39. package/dist/helpers.d.cts.map +1 -1
  40. package/dist/helpers.d.ts.map +1 -1
  41. package/dist/helpers.js +9 -0
  42. package/dist/helpers.js.map +1 -1
  43. package/dist/images.cjs +19 -1
  44. package/dist/images.cjs.map +1 -1
  45. package/dist/images.js +19 -1
  46. package/dist/images.js.map +1 -1
  47. package/dist/index.cjs +2 -0
  48. package/dist/index.d.cts +2 -2
  49. package/dist/index.d.ts +2 -2
  50. package/dist/index.js +3 -3
  51. package/dist/jest.cjs +10 -3
  52. package/dist/jest.cjs.map +1 -1
  53. package/dist/jest.js +10 -3
  54. package/dist/jest.js.map +1 -1
  55. package/dist/journal.cjs +1 -1
  56. package/dist/journal.cjs.map +1 -1
  57. package/dist/journal.d.cts.map +1 -1
  58. package/dist/journal.d.ts.map +1 -1
  59. package/dist/journal.js +1 -1
  60. package/dist/journal.js.map +1 -1
  61. package/dist/messages.cjs +3 -2
  62. package/dist/messages.cjs.map +1 -1
  63. package/dist/messages.js +3 -2
  64. package/dist/messages.js.map +1 -1
  65. package/dist/ollama.cjs +125 -4
  66. package/dist/ollama.cjs.map +1 -1
  67. package/dist/ollama.d.cts.map +1 -1
  68. package/dist/ollama.d.ts.map +1 -1
  69. package/dist/ollama.js +126 -5
  70. package/dist/ollama.js.map +1 -1
  71. package/dist/recorder.cjs +224 -53
  72. package/dist/recorder.cjs.map +1 -1
  73. package/dist/recorder.js +224 -53
  74. package/dist/recorder.js.map +1 -1
  75. package/dist/responses.cjs +10 -1
  76. package/dist/responses.cjs.map +1 -1
  77. package/dist/responses.d.cts +2 -1
  78. package/dist/responses.d.cts.map +1 -1
  79. package/dist/responses.d.ts +2 -1
  80. package/dist/responses.d.ts.map +1 -1
  81. package/dist/responses.js +10 -2
  82. package/dist/responses.js.map +1 -1
  83. package/dist/router.cjs +13 -6
  84. package/dist/router.cjs.map +1 -1
  85. package/dist/router.js +13 -6
  86. package/dist/router.js.map +1 -1
  87. package/dist/server.cjs +144 -62
  88. package/dist/server.cjs.map +1 -1
  89. package/dist/server.d.cts.map +1 -1
  90. package/dist/server.d.ts.map +1 -1
  91. package/dist/server.js +145 -63
  92. package/dist/server.js.map +1 -1
  93. package/dist/speech.cjs +19 -1
  94. package/dist/speech.cjs.map +1 -1
  95. package/dist/speech.js +19 -1
  96. package/dist/speech.js.map +1 -1
  97. package/dist/transcription.cjs +8 -4
  98. package/dist/transcription.cjs.map +1 -1
  99. package/dist/transcription.d.cts.map +1 -1
  100. package/dist/transcription.d.ts.map +1 -1
  101. package/dist/transcription.js +8 -4
  102. package/dist/transcription.js.map +1 -1
  103. package/dist/types.d.cts +1 -0
  104. package/dist/types.d.cts.map +1 -1
  105. package/dist/types.d.ts +1 -0
  106. package/dist/types.d.ts.map +1 -1
  107. package/dist/video.cjs +63 -1
  108. package/dist/video.cjs.map +1 -1
  109. package/dist/video.d.cts +15 -2
  110. package/dist/video.d.cts.map +1 -1
  111. package/dist/video.d.ts +15 -2
  112. package/dist/video.d.ts.map +1 -1
  113. package/dist/video.js +63 -2
  114. package/dist/video.js.map +1 -1
  115. package/dist/vitest.cjs +10 -3
  116. package/dist/vitest.cjs.map +1 -1
  117. package/dist/vitest.js +10 -3
  118. package/dist/vitest.js.map +1 -1
  119. package/package.json +1 -1
package/dist/server.js CHANGED
@@ -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";
@@ -100,9 +100,19 @@ const CORS_HEADERS = {
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,6 +300,23 @@ async function handleCompletions(req, res, fixtures, journal, defaults, modelFal
279
300
  } }));
280
301
  return;
281
302
  }
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
+ }
282
320
  body._endpointType = "chat";
283
321
  const testId = getTestId(req);
284
322
  const fixture = matchFixture(fixtures, body, journal.getFixtureMatchCountsForTest(testId), defaults.requestTransform);
@@ -302,7 +340,8 @@ async function handleCompletions(req, res, fixtures, journal, defaults, modelFal
302
340
  body,
303
341
  response: {
304
342
  status: res.statusCode ?? 200,
305
- fixture: null
343
+ fixture: null,
344
+ source: "proxy"
306
345
  }
307
346
  });
308
347
  return;
@@ -507,7 +546,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
507
546
  maxEntries: options?.journalMaxEntries ?? 1e3,
508
547
  fixtureCountsMaxTestIds: options?.fixtureCountsMaxTestIds ?? 500
509
548
  });
510
- const videoStates = /* @__PURE__ */ new Map();
549
+ const videoStates = new VideoStateMap();
511
550
  if (mounts) for (const { handler } of mounts) {
512
551
  if (handler.setJournal) handler.setJournal(journal);
513
552
  if (registry && handler.setRegistry) handler.setRegistry(registry);
@@ -554,7 +593,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
554
593
  }
555
594
  });
556
595
  if (pathname.startsWith(CONTROL_PREFIX)) {
557
- await handleControlAPI(req, res, pathname, fixtures, journal, videoStates);
596
+ await handleControlAPI(req, res, pathname, fixtures, journal, videoStates, defaults);
558
597
  return;
559
598
  }
560
599
  if (mounts) {
@@ -656,7 +695,9 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
656
695
  return;
657
696
  }
658
697
  if (pathname === RESPONSES_PATH && req.method === "POST") {
659
- 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) {
660
701
  const msg = err instanceof Error ? err.message : "Internal error";
661
702
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
662
703
  message: msg,
@@ -670,11 +711,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
670
711
  }
671
712
  res.end();
672
713
  }
673
- });
714
+ }
674
715
  return;
675
716
  }
676
717
  if (pathname === MESSAGES_PATH && req.method === "POST") {
677
- 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) {
678
721
  const msg = err instanceof Error ? err.message : "Internal error";
679
722
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
680
723
  message: msg,
@@ -688,11 +731,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
688
731
  }
689
732
  res.end();
690
733
  }
691
- });
734
+ }
692
735
  return;
693
736
  }
694
737
  if (pathname === COHERE_CHAT_PATH && req.method === "POST") {
695
- 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) {
696
741
  const msg = err instanceof Error ? err.message : "Internal error";
697
742
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
698
743
  message: msg,
@@ -706,72 +751,82 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
706
751
  }
707
752
  res.end();
708
753
  }
709
- });
754
+ }
710
755
  return;
711
756
  }
712
757
  if (pathname === EMBEDDINGS_PATH && req.method === "POST") {
713
- const deploymentId = azureDeploymentId;
714
- readBody(req).then((raw) => {
758
+ try {
759
+ const deploymentId = azureDeploymentId;
760
+ const embeddingsProvider = azureDeploymentId ? "azure" : "openai";
761
+ let raw = await readBody(req);
715
762
  if (deploymentId) try {
716
763
  const parsed = JSON.parse(raw);
717
764
  if (!parsed.model) {
718
765
  parsed.model = deploymentId;
719
- return handleEmbeddings(req, res, JSON.stringify(parsed), fixtures, journal, defaults, setCorsHeaders);
766
+ raw = JSON.stringify(parsed);
720
767
  }
721
768
  } catch {}
722
- return handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders);
723
- }).catch((err) => {
769
+ await handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders, embeddingsProvider);
770
+ } catch (err) {
724
771
  const msg = err instanceof Error ? err.message : "Internal error";
725
772
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
726
773
  message: msg,
727
774
  type: "server_error"
728
775
  } }));
729
776
  else if (!res.writableEnded) res.destroy();
730
- });
777
+ }
731
778
  return;
732
779
  }
733
780
  if (pathname === IMAGES_PATH && req.method === "POST") {
734
- 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) {
735
784
  const msg = err instanceof Error ? err.message : "Internal error";
736
785
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
737
786
  message: msg,
738
787
  type: "server_error"
739
788
  } }));
740
789
  else if (!res.writableEnded) res.destroy();
741
- });
790
+ }
742
791
  return;
743
792
  }
744
793
  if (pathname === SPEECH_PATH && req.method === "POST") {
745
- 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) {
746
797
  const msg = err instanceof Error ? err.message : "Internal error";
747
798
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
748
799
  message: msg,
749
800
  type: "server_error"
750
801
  } }));
751
802
  else if (!res.writableEnded) res.destroy();
752
- });
803
+ }
753
804
  return;
754
805
  }
755
806
  if (pathname === TRANSCRIPTIONS_PATH && req.method === "POST") {
756
- 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) {
757
810
  const msg = err instanceof Error ? err.message : "Internal error";
758
811
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
759
812
  message: msg,
760
813
  type: "server_error"
761
814
  } }));
762
815
  else if (!res.writableEnded) res.destroy();
763
- });
816
+ }
764
817
  return;
765
818
  }
766
819
  if (pathname === VIDEOS_PATH && req.method === "POST") {
767
- 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) {
768
823
  const msg = err instanceof Error ? err.message : "Internal error";
769
824
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
770
825
  message: msg,
771
826
  type: "server_error"
772
827
  } }));
773
828
  else if (!res.writableEnded) res.destroy();
774
- });
829
+ }
775
830
  return;
776
831
  }
777
832
  const videoStatusMatch = pathname.match(VIDEOS_STATUS_RE);
@@ -783,21 +838,25 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
783
838
  const geminiPredictMatch = pathname.match(GEMINI_PREDICT_RE);
784
839
  if (geminiPredictMatch && req.method === "POST") {
785
840
  const predictModel = geminiPredictMatch[1];
786
- 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) {
787
844
  const msg = err instanceof Error ? err.message : "Internal error";
788
845
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
789
846
  message: msg,
790
847
  type: "server_error"
791
848
  } }));
792
849
  else if (!res.writableEnded) res.destroy();
793
- });
850
+ }
794
851
  return;
795
852
  }
796
853
  const geminiMatch = pathname.match(GEMINI_PATH_RE);
797
854
  if (geminiMatch && req.method === "POST") {
798
855
  const geminiModel = geminiMatch[1];
799
856
  const streaming = geminiMatch[2] === "streamGenerateContent";
800
- 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) {
801
860
  const msg = err instanceof Error ? err.message : "Internal error";
802
861
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
803
862
  message: msg,
@@ -811,14 +870,16 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
811
870
  }
812
871
  res.end();
813
872
  }
814
- });
873
+ }
815
874
  return;
816
875
  }
817
876
  const vertexMatch = pathname.match(VERTEX_AI_RE);
818
877
  if (vertexMatch && req.method === "POST") {
819
878
  const vertexModel = vertexMatch[1];
820
879
  const streaming = vertexMatch[2] === "streamGenerateContent";
821
- 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) {
822
883
  const msg = err instanceof Error ? err.message : "Internal error";
823
884
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
824
885
  message: msg,
@@ -832,81 +893,93 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
832
893
  }
833
894
  res.end();
834
895
  }
835
- });
896
+ }
836
897
  return;
837
898
  }
838
899
  const bedrockMatch = pathname.match(BEDROCK_INVOKE_RE);
839
900
  if (bedrockMatch && req.method === "POST") {
840
901
  const bedrockModelId = bedrockMatch[1];
841
- 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) {
842
905
  const msg = err instanceof Error ? err.message : "Internal error";
843
906
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
844
907
  message: msg,
845
908
  type: "server_error"
846
909
  } }));
847
910
  else if (!res.writableEnded) res.destroy();
848
- });
911
+ }
849
912
  return;
850
913
  }
851
914
  const bedrockStreamMatch = pathname.match(BEDROCK_STREAM_RE);
852
915
  if (bedrockStreamMatch && req.method === "POST") {
853
916
  const bedrockModelId = bedrockStreamMatch[1];
854
- 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) {
855
920
  const msg = err instanceof Error ? err.message : "Internal error";
856
921
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
857
922
  message: msg,
858
923
  type: "server_error"
859
924
  } }));
860
925
  else if (!res.writableEnded) res.destroy();
861
- });
926
+ }
862
927
  return;
863
928
  }
864
929
  const converseMatch = pathname.match(BEDROCK_CONVERSE_RE);
865
930
  if (converseMatch && req.method === "POST") {
866
931
  const converseModelId = converseMatch[1];
867
- 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) {
868
935
  const msg = err instanceof Error ? err.message : "Internal error";
869
936
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
870
937
  message: msg,
871
938
  type: "server_error"
872
939
  } }));
873
940
  else if (!res.writableEnded) res.destroy();
874
- });
941
+ }
875
942
  return;
876
943
  }
877
944
  const converseStreamMatch = pathname.match(BEDROCK_CONVERSE_STREAM_RE);
878
945
  if (converseStreamMatch && req.method === "POST") {
879
946
  const converseStreamModelId = converseStreamMatch[1];
880
- 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) {
881
950
  const msg = err instanceof Error ? err.message : "Internal error";
882
951
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
883
952
  message: msg,
884
953
  type: "server_error"
885
954
  } }));
886
955
  else if (!res.writableEnded) res.destroy();
887
- });
956
+ }
888
957
  return;
889
958
  }
890
959
  if (pathname === OLLAMA_CHAT_PATH && req.method === "POST") {
891
- 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) {
892
963
  const msg = err instanceof Error ? err.message : "Internal error";
893
964
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
894
965
  message: msg,
895
966
  type: "server_error"
896
967
  } }));
897
968
  else if (!res.writableEnded) res.destroy();
898
- });
969
+ }
899
970
  return;
900
971
  }
901
972
  if (pathname === OLLAMA_GENERATE_PATH && req.method === "POST") {
902
- 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) {
903
976
  const msg = err instanceof Error ? err.message : "Internal error";
904
977
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
905
978
  message: msg,
906
979
  type: "server_error"
907
980
  } }));
908
981
  else if (!res.writableEnded) res.destroy();
909
- });
982
+ }
910
983
  return;
911
984
  }
912
985
  if (pathname === OLLAMA_TAGS_PATH && req.method === "GET") {
@@ -926,36 +999,42 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
926
999
  return;
927
1000
  }
928
1001
  if (pathname === SEARCH_PATH && req.method === "POST") {
929
- 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) {
930
1005
  const msg = err instanceof Error ? err.message : "Internal error";
931
1006
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
932
1007
  message: msg,
933
1008
  type: "server_error"
934
1009
  } }));
935
1010
  else if (!res.writableEnded) res.destroy();
936
- });
1011
+ }
937
1012
  return;
938
1013
  }
939
1014
  if (pathname === RERANK_PATH && req.method === "POST") {
940
- 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) {
941
1018
  const msg = err instanceof Error ? err.message : "Internal error";
942
1019
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
943
1020
  message: msg,
944
1021
  type: "server_error"
945
1022
  } }));
946
1023
  else if (!res.writableEnded) res.destroy();
947
- });
1024
+ }
948
1025
  return;
949
1026
  }
950
1027
  if (pathname === MODERATIONS_PATH && req.method === "POST") {
951
- 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) {
952
1031
  const msg = err instanceof Error ? err.message : "Internal error";
953
1032
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
954
1033
  message: msg,
955
1034
  type: "server_error"
956
1035
  } }));
957
1036
  else if (!res.writableEnded) res.destroy();
958
- });
1037
+ }
959
1038
  return;
960
1039
  }
961
1040
  if (pathname !== COMPLETIONS_PATH) {
@@ -966,7 +1045,10 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
966
1045
  handleNotFound(res, "Not found");
967
1046
  return;
968
1047
  }
969
- 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) {
970
1052
  const msg = err instanceof Error ? err.message : "Internal error";
971
1053
  if (!res.headersSent) writeErrorResponse(res, 500, JSON.stringify({ error: {
972
1054
  message: msg,
@@ -983,7 +1065,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
983
1065
  }
984
1066
  res.end();
985
1067
  }
986
- });
1068
+ }
987
1069
  }
988
1070
  const activeConnections = /* @__PURE__ */ new Set();
989
1071
  server.on("upgrade", (req, socket, head) => {