Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

feat(quota): add quotaShared-* virtual model naming helpers (Phase B1)

diegosouzapw6214ea676835
diff --git a/src/lib/quota/quotaModelNaming.ts b/src/lib/quota/quotaModelNaming.ts
new file mode 100644
index 0000000000..95ecbfb623
--- /dev/null
+++ b/src/lib/quota/quotaModelNaming.ts
@@ -0,0 +1,75 @@
+/**
+ * Deterministic naming helpers for quota virtual models (Phase B1).
+ *
+ * FORMAT: `quotaShared-<poolSlug>-<provider>/<model>`
+ *
+ * The poolSlug is pure alphanumeric (no "-", no "/"), which makes the first
+ * "-" after the prefix and the first "/" unambiguous delimiters that allow
+ * round-trip parsing even when provider contains "-" or model contains "/".
+ */
+
+export const QUOTA_MODEL_PREFIX = "quotaShared-";
+
+/**
+ * Convert an arbitrary pool name into a safe, alphanumeric slug.
+ * Lowercases the name then strips every character that is not [a-z0-9].
+ * Falls back to "pool" when the result would be empty.
+ */
+export function quotaPoolSlug(poolName: string): string {
+ const slug = poolName.toLowerCase().replace(/[^a-z0-9]/g, "");
+ return slug.length > 0 ? slug : "pool";
+}
+
+/**
+ * Build the canonical virtual model name for a quota-shared target.
+ * Provider and model are kept verbatim (not slugged).
+ */
+export function quotaModelName(poolName: string, provider: string, model: string): string {
+ return `${QUOTA_MODEL_PREFIX}${quotaPoolSlug(poolName)}-${provider}/${model}`;
+}
+
+/**
+ * Parse a quota virtual model name back into its components.
+ * Returns null when the name is not a valid quota model name.
+ */
+export function parseQuotaModelName(
+ name: string,
+): { poolSlug: string; provider: string; model: string } | null {
+ if (!name.startsWith(QUOTA_MODEL_PREFIX)) {
+ return null;
+ }
+
+ // rest = "<poolSlug>-<provider>/<model>"
+ const rest = name.slice(QUOTA_MODEL_PREFIX.length);
+
+ // poolSlug has no "-" by construction → first "-" is the delimiter
+ const dashIdx = rest.indexOf("-");
+ if (dashIdx === -1) {
+ return null;
+ }
+
+ const poolSlug = rest.slice(0, dashIdx);
+ const tail = rest.slice(dashIdx + 1); // "<provider>/<model>"
+
+ // first "/" separates provider from model; model may contain additional "/"
+ const slashIdx = tail.indexOf("/");
+ if (slashIdx === -1) {
+ return null;
+ }
+
+ const provider = tail.slice(0, slashIdx);
+ const model = tail.slice(slashIdx + 1);
+
+ if (provider.length === 0 || model.length === 0) {
+ return null;
+ }
+
+ return { poolSlug, provider, model };
+}
+
+/**
+ * Fast prefix check — does not validate the full structure.
+ */
+export function isQuotaModelName(name: string): boolean {
+ return name.startsWith(QUOTA_MODEL_PREFIX);
+}
diff --git a/tests/unit/quota-model-naming.test.ts b/tests/unit/quota-model-naming.test.ts
new file mode 100644
index 0000000000..db5238f63c
--- /dev/null
+++ b/tests/unit/quota-model-naming.test.ts
@@ -0,0 +1,119 @@
+import { describe, it } from "node:test";
+import assert from "node:assert/strict";
+import {
+ QUOTA_MODEL_PREFIX,
+ quotaPoolSlug,
+ quotaModelName,
+ parseQuotaModelName,
+ isQuotaModelName,
+} from "../../src/lib/quota/quotaModelNaming.js";
+
+describe("quotaPoolSlug", () => {
+ it("strips non-alphanumeric characters and lowercases", () => {
+ assert.equal(quotaPoolSlug("Time XPT-2"), "timexpt2");
+ });
+
+ it("handles simple lowercase names unchanged", () => {
+ assert.equal(quotaPoolSlug("times"), "times");
+ });
+
+ it("strips dots and slashes", () => {
+ assert.equal(quotaPoolSlug("my.pool/v2"), "mypoolv2");
+ });
+
+ it("falls back to 'pool' for empty result (all-symbol name)", () => {
+ assert.equal(quotaPoolSlug("---"), "pool");
+ });
+
+ it("falls back to 'pool' for empty string", () => {
+ assert.equal(quotaPoolSlug(""), "pool");
+ });
+
+ it("falls back to 'pool' for symbol-only name", () => {
+ assert.equal(quotaPoolSlug("!@#$%"), "pool");
+ });
+});
+
+describe("quotaModelName", () => {
+ it("produces the canonical format", () => {
+ assert.equal(quotaModelName("Times", "cx", "gpt-5.5"), "quotaShared-times-cx/gpt-5.5");
+ });
+
+ it("uses the prefix constant", () => {
+ const name = quotaModelName("pool", "openai", "gpt-4");
+ assert.ok(name.startsWith(QUOTA_MODEL_PREFIX));
+ });
+
+ it("keeps provider and model verbatim (not slugged)", () => {
+ const name = quotaModelName("p", "openai", "org/model-x");
+ assert.equal(name, "quotaShared-p-openai/org/model-x");
+ });
+
+ it("provider with dashes is kept verbatim", () => {
+ const name = quotaModelName("pool", "some-prov", "m");
+ assert.equal(name, "quotaShared-pool-some-prov/m");
+ });
+});
+
+describe("parseQuotaModelName", () => {
+ it("round-trips a simple model name", () => {
+ const name = quotaModelName("Times", "cx", "gpt-5.5");
+ const parsed = parseQuotaModelName(name);
+ assert.deepEqual(parsed, { poolSlug: "times", provider: "cx", model: "gpt-5.5" });
+ });
+
+ it("round-trips a model name with a slash in model", () => {
+ const name = quotaModelName("p", "openai", "org/model-x");
+ const parsed = parseQuotaModelName(name);
+ assert.deepEqual(parsed, { poolSlug: "p", provider: "openai", model: "org/model-x" });
+ });
+
+ it("handles provider with a dash", () => {
+ const parsed = parseQuotaModelName("quotaShared-pool-some-prov/m");
+ assert.deepEqual(parsed, { poolSlug: "pool", provider: "some-prov", model: "m" });
+ });
+
+ it("returns null for a non-quota model name", () => {
+ assert.equal(parseQuotaModelName("gpt-4"), null);
+ });
+
+ it("returns null when there is no '-' after the prefix", () => {
+ assert.equal(parseQuotaModelName("quotaShared-noslash"), null);
+ });
+
+ it("returns null when there is no '/' in the tail", () => {
+ assert.equal(parseQuotaModelName("quotaShared-pool-noslash"), null);
+ });
+
+ it("returns null for empty provider", () => {
+ assert.equal(parseQuotaModelName("quotaShared-pool-/model"), null);
+ });
+
+ it("returns null for empty model", () => {
+ assert.equal(parseQuotaModelName("quotaShared-pool-cx/"), null);
+ });
+
+ it("handles model with multiple slashes", () => {
+ const parsed = parseQuotaModelName("quotaShared-mypoolv2-openai/a/b/c");
+ assert.deepEqual(parsed, { poolSlug: "mypoolv2", provider: "openai", model: "a/b/c" });
+ });
+});
+
+describe("isQuotaModelName", () => {
+ it("returns false for plain model names", () => {
+ assert.equal(isQuotaModelName("gpt-4"), false);
+ });
+
+ it("returns true for valid quota model names", () => {
+ assert.equal(isQuotaModelName("quotaShared-x-cx/m"), true);
+ });
+
+ it("returns true for any string starting with the prefix", () => {
+ assert.equal(isQuotaModelName("quotaShared-"), true);
+ });
+
+ it("is consistent with the QUOTA_MODEL_PREFIX constant", () => {
+ const name = quotaModelName("pool", "cx", "gpt-4");
+ assert.equal(isQuotaModelName(name), true);
+ });
+});