Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(prices): only read own keys from the operator price list

lhy-dcf9b7b4d23f778
diff --git a/server/prices.test.ts b/server/prices.test.ts
new file mode 100644
index 0000000000..20bf403a31
--- /dev/null
+++ b/server/prices.test.ts
@@ -0,0 +1,27 @@
+import { describe, expect, it } from "vitest";
+
+import { billableFor, priceFor } from "./prices.ts";
+
+const driverPrice = { inputPerMillion: 1, outputPerMillion: 2 };
+const modelPrice = { inputPerMillion: 3, outputPerMillion: 4 };
+const defaultPrice = { inputPerMillion: 5, outputPerMillion: 6 };
+
+describe("priceFor", () => {
+ it("keeps the driver/model -> model -> default precedence", () => {
+ const prices = { "openai/gpt": driverPrice, gpt: modelPrice, default: defaultPrice };
+ expect(priceFor({ driverKind: "openai", model: "gpt" }, prices)).toEqual(driverPrice);
+ expect(priceFor({ driverKind: "else", model: "gpt" }, prices)).toEqual(modelPrice);
+ expect(priceFor({ driverKind: "else", model: "unknown" }, prices)).toEqual(defaultPrice);
+ expect(priceFor({ driverKind: "else", model: "unknown" }, {})).toBeNull();
+ });
+
+ it("never returns an Object.prototype member as a price", () => {
+ for (const key of ["toString", "constructor", "valueOf", "hasOwnProperty", "__proto__"]) {
+ expect(priceFor({ driverKind: "openai", model: key }, {})).toBeNull();
+ }
+ });
+
+ it("billing stays null instead of producing NaN for a prototype key", () => {
+ expect(billableFor({ driverKind: "openai", model: "toString", input: 100, output: 10 }, {})).toBeNull();
+ });
+});
diff --git a/server/prices.ts b/server/prices.ts
index 4a7cfe95bc..c16285a957 100644
--- a/server/prices.ts
+++ b/server/prices.ts
@@ -17,7 +17,11 @@ export interface PricedTurn {
}
export function priceFor(turn: Pick<PricedTurn, "driverKind" | "model">, prices: PriceList): ModelPrice | null {
- return prices[`${turn.driverKind}/${turn.model}`] ?? prices[turn.model] ?? prices.default ?? null;
+ const driverKey = `${turn.driverKind}/${turn.model}`;
+ const forDriver = Object.hasOwn(prices, driverKey) ? prices[driverKey] : undefined;
+ const forModel = Object.hasOwn(prices, turn.model) ? prices[turn.model] : undefined;
+ const fallback = Object.hasOwn(prices, "default") ? prices.default : undefined;
+ return forDriver ?? forModel ?? fallback ?? null;
}
const clean = (value: number | undefined): number => (typeof value === "number" && Number.isFinite(value) ? Math.max(0, value) : 0);