@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.cjs CHANGED
@@ -102,9 +102,19 @@ const CORS_HEADERS = {
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,6 +302,23 @@ async function handleCompletions(req, res, fixtures, journal, defaults, modelFal
281
302
  } }));
282
303
  return;
283
304
  }
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
+ }
284
322
  body._endpointType = "chat";
285
323
  const testId = require_helpers.getTestId(req);
286
324
  const fixture = require_router.matchFixture(fixtures, body, journal.getFixtureMatchCountsForTest(testId), defaults.requestTransform);
@@ -304,7 +342,8 @@ async function handleCompletions(req, res, fixtures, journal, defaults, modelFal
304
342
  body,
305
343
  response: {
306
344
  status: res.statusCode ?? 200,
307
- fixture: null
345
+ fixture: null,
346
+ source: "proxy"
308
347
  }
309
348
  });
310
349
  return;
@@ -509,7 +548,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
509
548
  maxEntries: options?.journalMaxEntries ?? 1e3,
510
549
  fixtureCountsMaxTestIds: options?.fixtureCountsMaxTestIds ?? 500
511
550
  });
512
- const videoStates = /* @__PURE__ */ new Map();
551
+ const videoStates = new require_video.VideoStateMap();
513
552
  if (mounts) for (const { handler } of mounts) {
514
553
  if (handler.setJournal) handler.setJournal(journal);
515
554
  if (registry && handler.setRegistry) handler.setRegistry(registry);
@@ -556,7 +595,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
556
595
  }
557
596
  });
558
597
  if (pathname.startsWith(CONTROL_PREFIX)) {
559
- await handleControlAPI(req, res, pathname, fixtures, journal, videoStates);
598
+ await handleControlAPI(req, res, pathname, fixtures, journal, videoStates, defaults);
560
599
  return;
561
600
  }
562
601
  if (mounts) {
@@ -658,7 +697,9 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
658
697
  return;
659
698
  }
660
699
  if (pathname === RESPONSES_PATH && req.method === "POST") {
661
- 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) {
662
703
  const msg = err instanceof Error ? err.message : "Internal error";
663
704
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
664
705
  message: msg,
@@ -672,11 +713,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
672
713
  }
673
714
  res.end();
674
715
  }
675
- });
716
+ }
676
717
  return;
677
718
  }
678
719
  if (pathname === MESSAGES_PATH && req.method === "POST") {
679
- 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) {
680
723
  const msg = err instanceof Error ? err.message : "Internal error";
681
724
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
682
725
  message: msg,
@@ -690,11 +733,13 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
690
733
  }
691
734
  res.end();
692
735
  }
693
- });
736
+ }
694
737
  return;
695
738
  }
696
739
  if (pathname === COHERE_CHAT_PATH && req.method === "POST") {
697
- 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) {
698
743
  const msg = err instanceof Error ? err.message : "Internal error";
699
744
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
700
745
  message: msg,
@@ -708,72 +753,82 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
708
753
  }
709
754
  res.end();
710
755
  }
711
- });
756
+ }
712
757
  return;
713
758
  }
714
759
  if (pathname === EMBEDDINGS_PATH && req.method === "POST") {
715
- const deploymentId = azureDeploymentId;
716
- readBody(req).then((raw) => {
760
+ try {
761
+ const deploymentId = azureDeploymentId;
762
+ const embeddingsProvider = azureDeploymentId ? "azure" : "openai";
763
+ let raw = await readBody(req);
717
764
  if (deploymentId) try {
718
765
  const parsed = JSON.parse(raw);
719
766
  if (!parsed.model) {
720
767
  parsed.model = deploymentId;
721
- return require_embeddings.handleEmbeddings(req, res, JSON.stringify(parsed), fixtures, journal, defaults, setCorsHeaders);
768
+ raw = JSON.stringify(parsed);
722
769
  }
723
770
  } catch {}
724
- return require_embeddings.handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders);
725
- }).catch((err) => {
771
+ await require_embeddings.handleEmbeddings(req, res, raw, fixtures, journal, defaults, setCorsHeaders, embeddingsProvider);
772
+ } catch (err) {
726
773
  const msg = err instanceof Error ? err.message : "Internal error";
727
774
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
728
775
  message: msg,
729
776
  type: "server_error"
730
777
  } }));
731
778
  else if (!res.writableEnded) res.destroy();
732
- });
779
+ }
733
780
  return;
734
781
  }
735
782
  if (pathname === IMAGES_PATH && req.method === "POST") {
736
- 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) {
737
786
  const msg = err instanceof Error ? err.message : "Internal error";
738
787
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
739
788
  message: msg,
740
789
  type: "server_error"
741
790
  } }));
742
791
  else if (!res.writableEnded) res.destroy();
743
- });
792
+ }
744
793
  return;
745
794
  }
746
795
  if (pathname === SPEECH_PATH && req.method === "POST") {
747
- 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) {
748
799
  const msg = err instanceof Error ? err.message : "Internal error";
749
800
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
750
801
  message: msg,
751
802
  type: "server_error"
752
803
  } }));
753
804
  else if (!res.writableEnded) res.destroy();
754
- });
805
+ }
755
806
  return;
756
807
  }
757
808
  if (pathname === TRANSCRIPTIONS_PATH && req.method === "POST") {
758
- 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) {
759
812
  const msg = err instanceof Error ? err.message : "Internal error";
760
813
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
761
814
  message: msg,
762
815
  type: "server_error"
763
816
  } }));
764
817
  else if (!res.writableEnded) res.destroy();
765
- });
818
+ }
766
819
  return;
767
820
  }
768
821
  if (pathname === VIDEOS_PATH && req.method === "POST") {
769
- 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) {
770
825
  const msg = err instanceof Error ? err.message : "Internal error";
771
826
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
772
827
  message: msg,
773
828
  type: "server_error"
774
829
  } }));
775
830
  else if (!res.writableEnded) res.destroy();
776
- });
831
+ }
777
832
  return;
778
833
  }
779
834
  const videoStatusMatch = pathname.match(VIDEOS_STATUS_RE);
@@ -785,21 +840,25 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
785
840
  const geminiPredictMatch = pathname.match(GEMINI_PREDICT_RE);
786
841
  if (geminiPredictMatch && req.method === "POST") {
787
842
  const predictModel = geminiPredictMatch[1];
788
- 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) {
789
846
  const msg = err instanceof Error ? err.message : "Internal error";
790
847
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
791
848
  message: msg,
792
849
  type: "server_error"
793
850
  } }));
794
851
  else if (!res.writableEnded) res.destroy();
795
- });
852
+ }
796
853
  return;
797
854
  }
798
855
  const geminiMatch = pathname.match(GEMINI_PATH_RE);
799
856
  if (geminiMatch && req.method === "POST") {
800
857
  const geminiModel = geminiMatch[1];
801
858
  const streaming = geminiMatch[2] === "streamGenerateContent";
802
- 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) {
803
862
  const msg = err instanceof Error ? err.message : "Internal error";
804
863
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
805
864
  message: msg,
@@ -813,14 +872,16 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
813
872
  }
814
873
  res.end();
815
874
  }
816
- });
875
+ }
817
876
  return;
818
877
  }
819
878
  const vertexMatch = pathname.match(VERTEX_AI_RE);
820
879
  if (vertexMatch && req.method === "POST") {
821
880
  const vertexModel = vertexMatch[1];
822
881
  const streaming = vertexMatch[2] === "streamGenerateContent";
823
- 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) {
824
885
  const msg = err instanceof Error ? err.message : "Internal error";
825
886
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
826
887
  message: msg,
@@ -834,81 +895,93 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
834
895
  }
835
896
  res.end();
836
897
  }
837
- });
898
+ }
838
899
  return;
839
900
  }
840
901
  const bedrockMatch = pathname.match(BEDROCK_INVOKE_RE);
841
902
  if (bedrockMatch && req.method === "POST") {
842
903
  const bedrockModelId = bedrockMatch[1];
843
- 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) {
844
907
  const msg = err instanceof Error ? err.message : "Internal error";
845
908
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
846
909
  message: msg,
847
910
  type: "server_error"
848
911
  } }));
849
912
  else if (!res.writableEnded) res.destroy();
850
- });
913
+ }
851
914
  return;
852
915
  }
853
916
  const bedrockStreamMatch = pathname.match(BEDROCK_STREAM_RE);
854
917
  if (bedrockStreamMatch && req.method === "POST") {
855
918
  const bedrockModelId = bedrockStreamMatch[1];
856
- 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) {
857
922
  const msg = err instanceof Error ? err.message : "Internal error";
858
923
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
859
924
  message: msg,
860
925
  type: "server_error"
861
926
  } }));
862
927
  else if (!res.writableEnded) res.destroy();
863
- });
928
+ }
864
929
  return;
865
930
  }
866
931
  const converseMatch = pathname.match(BEDROCK_CONVERSE_RE);
867
932
  if (converseMatch && req.method === "POST") {
868
933
  const converseModelId = converseMatch[1];
869
- 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) {
870
937
  const msg = err instanceof Error ? err.message : "Internal error";
871
938
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
872
939
  message: msg,
873
940
  type: "server_error"
874
941
  } }));
875
942
  else if (!res.writableEnded) res.destroy();
876
- });
943
+ }
877
944
  return;
878
945
  }
879
946
  const converseStreamMatch = pathname.match(BEDROCK_CONVERSE_STREAM_RE);
880
947
  if (converseStreamMatch && req.method === "POST") {
881
948
  const converseStreamModelId = converseStreamMatch[1];
882
- 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) {
883
952
  const msg = err instanceof Error ? err.message : "Internal error";
884
953
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
885
954
  message: msg,
886
955
  type: "server_error"
887
956
  } }));
888
957
  else if (!res.writableEnded) res.destroy();
889
- });
958
+ }
890
959
  return;
891
960
  }
892
961
  if (pathname === OLLAMA_CHAT_PATH && req.method === "POST") {
893
- 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) {
894
965
  const msg = err instanceof Error ? err.message : "Internal error";
895
966
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
896
967
  message: msg,
897
968
  type: "server_error"
898
969
  } }));
899
970
  else if (!res.writableEnded) res.destroy();
900
- });
971
+ }
901
972
  return;
902
973
  }
903
974
  if (pathname === OLLAMA_GENERATE_PATH && req.method === "POST") {
904
- 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) {
905
978
  const msg = err instanceof Error ? err.message : "Internal error";
906
979
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
907
980
  message: msg,
908
981
  type: "server_error"
909
982
  } }));
910
983
  else if (!res.writableEnded) res.destroy();
911
- });
984
+ }
912
985
  return;
913
986
  }
914
987
  if (pathname === OLLAMA_TAGS_PATH && req.method === "GET") {
@@ -928,36 +1001,42 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
928
1001
  return;
929
1002
  }
930
1003
  if (pathname === SEARCH_PATH && req.method === "POST") {
931
- 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) {
932
1007
  const msg = err instanceof Error ? err.message : "Internal error";
933
1008
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
934
1009
  message: msg,
935
1010
  type: "server_error"
936
1011
  } }));
937
1012
  else if (!res.writableEnded) res.destroy();
938
- });
1013
+ }
939
1014
  return;
940
1015
  }
941
1016
  if (pathname === RERANK_PATH && req.method === "POST") {
942
- 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) {
943
1020
  const msg = err instanceof Error ? err.message : "Internal error";
944
1021
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
945
1022
  message: msg,
946
1023
  type: "server_error"
947
1024
  } }));
948
1025
  else if (!res.writableEnded) res.destroy();
949
- });
1026
+ }
950
1027
  return;
951
1028
  }
952
1029
  if (pathname === MODERATIONS_PATH && req.method === "POST") {
953
- 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) {
954
1033
  const msg = err instanceof Error ? err.message : "Internal error";
955
1034
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
956
1035
  message: msg,
957
1036
  type: "server_error"
958
1037
  } }));
959
1038
  else if (!res.writableEnded) res.destroy();
960
- });
1039
+ }
961
1040
  return;
962
1041
  }
963
1042
  if (pathname !== COMPLETIONS_PATH) {
@@ -968,7 +1047,10 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
968
1047
  handleNotFound(res, "Not found");
969
1048
  return;
970
1049
  }
971
- 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) {
972
1054
  const msg = err instanceof Error ? err.message : "Internal error";
973
1055
  if (!res.headersSent) require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
974
1056
  message: msg,
@@ -985,7 +1067,7 @@ async function createServer(fixtures, options, mounts, serviceFixtures) {
985
1067
  }
986
1068
  res.end();
987
1069
  }
988
- });
1070
+ }
989
1071
  }
990
1072
  const activeConnections = /* @__PURE__ */ new Set();
991
1073
  server.on("upgrade", (req, socket, head) => {