@dbx-tools/cli-model-proxy 0.3.28 → 0.3.29

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 (2) hide show
  1. package/package.json +4 -4
  2. package/src/server.ts +9 -7
package/package.json CHANGED
@@ -18,16 +18,16 @@
18
18
  "@databricks/sdk-experimental": "^0.17.0",
19
19
  "commander": "^15.0.0",
20
20
  "tsx": "^4.23.0",
21
- "@dbx-tools/model": "0.3.28",
22
- "@dbx-tools/shared-core": "0.3.28",
23
- "@dbx-tools/shared-model": "0.3.28"
21
+ "@dbx-tools/shared-core": "0.3.29",
22
+ "@dbx-tools/shared-model": "0.3.29",
23
+ "@dbx-tools/model": "0.3.29"
24
24
  },
25
25
  "main": "index.ts",
26
26
  "license": "UNLICENSED",
27
27
  "publishConfig": {
28
28
  "access": "public"
29
29
  },
30
- "version": "0.3.28",
30
+ "version": "0.3.29",
31
31
  "types": "index.ts",
32
32
  "type": "module",
33
33
  "exports": {
package/src/server.ts CHANGED
@@ -32,7 +32,7 @@
32
32
  */
33
33
 
34
34
  import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
35
- import { error, log } from "@dbx-tools/shared-core";
35
+ import { error, json, log } from "@dbx-tools/shared-core";
36
36
  import { classify, openaiChat, openaiResponses } from "@dbx-tools/shared-model";
37
37
 
38
38
  import type { DatabricksBackend } from "./backend";
@@ -222,7 +222,7 @@ async function handleProxy(
222
222
  req: IncomingMessage,
223
223
  res: ServerResponse,
224
224
  ): Promise<void> {
225
- const body = (await readJsonBody(req)) as Record<string, unknown>;
225
+ const body = await readJsonBody(req);
226
226
  const requested = typeof body.model === "string" ? body.model : undefined;
227
227
  if (!requested) {
228
228
  sendJson(res, 400, errorBody("missing 'model' in request body", "invalid_request_error"));
@@ -348,7 +348,7 @@ async function handleResponses(
348
348
  req: IncomingMessage,
349
349
  res: ServerResponse,
350
350
  ): Promise<void> {
351
- const body = (await readJsonBody(req)) as Record<string, unknown>;
351
+ const body = await readJsonBody(req);
352
352
  const requested = typeof body.model === "string" ? body.model : undefined;
353
353
  if (!requested) {
354
354
  sendJson(res, 400, errorBody("missing 'model' in request body", "invalid_request_error"));
@@ -424,14 +424,16 @@ function isAuthorized(req: IncomingMessage, apiKey: string): boolean {
424
424
  return header.replace(/^Bearer\s+/i, "").trim() === apiKey;
425
425
  }
426
426
 
427
- /** Drain a request body and parse it as JSON (empty body parses to `{}`). */
428
- async function readJsonBody(req: IncomingMessage): Promise<unknown> {
427
+ /**
428
+ * Drain a request body and parse it as a JSON object. An empty, malformed, or
429
+ * non-object body yields `{}`, which the callers reject as a missing `model`.
430
+ */
431
+ async function readJsonBody(req: IncomingMessage): Promise<Record<string, unknown>> {
429
432
  const chunks: Buffer[] = [];
430
433
  for await (const chunk of req) {
431
434
  chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as Uint8Array));
432
435
  }
433
- const raw = Buffer.concat(chunks).toString("utf8");
434
- return raw ? JSON.parse(raw) : {};
436
+ return json.parseRecord(Buffer.concat(chunks).toString("utf8")) ?? {};
435
437
  }
436
438
 
437
439
  /** Serialize and send a JSON response. */