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

8.3 KB
Raw Blame
/**
 * db/providers/nodes.ts — Provider nodes CRUD.
 */

import { v4 as uuidv4 } from "uuid";
import { getDbInstance, rowToCamel } from "../core";
import { selectProviderNodeForConnection } from "../providerNodeSelect";
import { backupDbFile } from "../backup";
import { invalidateDbCache } from "../readCache";
import { toRecord, type JsonRecord } from "./columns";
import { resetProviderNodePrefixCache } from "./nodePrefix";

function normalizeDailyQuotaResetHour(value: unknown): number | null {
  return value === 0 || value ? Number(value) : null;
}

function withParsedCustomHeaders(node: JsonRecord, storedJson: string | null): JsonRecord {
  const result: JsonRecord = { ...node };
  if (storedJson) {
    try {
      result.customHeaders = JSON.parse(storedJson);
    } catch {
      result.customHeaders = null;
    }
  } else {
    result.customHeaders = null;
  }
  delete result.customHeadersJson;
  return result;
}

interface StatementLike<TRow = unknown> {
  all: (...params: unknown[]) => TRow[];
  get: (...params: unknown[]) => TRow | undefined;
  run: (...params: unknown[]) => { changes?: number };
}

interface DbLike {
  prepare: <TRow = unknown>(sql: string) => StatementLike<TRow>;
}

export async function getProviderNodes(filter: JsonRecord = {}, limit?: number, offset?: number) {
  const db = getDbInstance() as unknown as DbLike;
  let sql = "SELECT * FROM provider_nodes";
  const params: Record<string, unknown> = {};

  if (filter.type) {
    sql += " WHERE type = @type";
    params.type = filter.type;
  }
  sql += " ORDER BY id ASC";
  if (limit !== undefined) {
    sql += " LIMIT @limit OFFSET @offset";
    params.limit = limit;
    params.offset = offset ?? 0;
  }

  return db.prepare(sql).all(params).map(rowToCamel);
}

export function getProviderNodesCount(filter: JsonRecord = {}): number {
  const db = getDbInstance() as unknown as DbLike;
  let sql = "SELECT count(*) as cnt FROM provider_nodes";
  const params: Record<string, unknown> = {};

  if (filter.type) {
    sql += " WHERE type = @type";
    params.type = filter.type;
  }

  const row = db.prepare(sql).get(params) as { cnt: number };
  return row.cnt;
}

export async function getProviderNodeById(id: string) {
  const db = getDbInstance() as unknown as DbLike;
  const row = db.prepare("SELECT * FROM provider_nodes WHERE id = ?").get(id);
  return row ? rowToCamel(row) : null;
}

// #4421: resolve the provider node for a new connection from either its concrete id
// (what the dashboard sends, "<type>-<uuid>") OR the bare derived type (what callers
// using the /api/providers API directly often pass, e.g. "openai-compatible-responses").
// Falls back to the sole node of that type only when unambiguous; otherwise null (so the
// caller still surfaces the existing 404).
export async function resolveProviderNodeForConnection(idOrType: string) {
  const exact = await getProviderNodeById(idOrType);
  if (exact) return exact;
  const all = (await getProviderNodes()) as JsonRecord[];
  return selectProviderNodeForConnection(idOrType, all);
}

export const PROVIDER_NODE_ID_CONFLICT = "PROVIDER_NODE_ID_CONFLICT";

/** Thrown by createProviderNode when a node with the same id already exists. */
export class ProviderNodeIdConflictError extends Error {
  readonly code = PROVIDER_NODE_ID_CONFLICT;
  readonly nodeId: string;

  constructor(nodeId: string) {
    super(`Provider node ID "${nodeId}" already exists`);
    this.name = "ProviderNodeIdConflictError";
    this.nodeId = nodeId;
  }
}

// Matched by `code` (not only `instanceof`) so the guard survives duplicated module
// instances across bundles/realms.
export function isProviderNodeIdConflictError(
  error: unknown
): error is ProviderNodeIdConflictError {
  return (
    error instanceof ProviderNodeIdConflictError ||
    (typeof error === "object" &&
      error !== null &&
      (error as { code?: unknown }).code === PROVIDER_NODE_ID_CONFLICT)
  );
}

export async function createProviderNode(data: JsonRecord) {
  const db = getDbInstance() as unknown as DbLike;
  const now = new Date().toISOString();

  const customHeadersJson = data.customHeaders ? JSON.stringify(data.customHeaders) : null;

  const node = {
    id: data.id || uuidv4(),
    type: data.type,
    name: data.name,
    prefix: data.prefix || null,
    apiType: data.apiType || null,
    baseUrl: data.baseUrl || null,
    chatPath: data.chatPath || null,
    modelsPath: data.modelsPath || null,
    // Optional operator-supplied remote icon URL (#2166) — plain TEXT, no JSON parsing needed.
    iconUrl: data.iconUrl || null,
    customHeadersJson,
    dailyQuotaResetTimezone: data.dailyQuotaResetTimezone || null,
    dailyQuotaResetHour: normalizeDailyQuotaResetHour(data.dailyQuotaResetHour),
    createdAt: now,
    updatedAt: now,
  };

  const result = db
    .prepare(
      `
    INSERT INTO provider_nodes (id, type, name, prefix, api_type, base_url, chat_path, models_path, icon_url, custom_headers_json, daily_quota_reset_timezone, daily_quota_reset_hour, created_at, updated_at)
    VALUES (@id, @type, @name, @prefix, @apiType, @baseUrl, @chatPath, @modelsPath, @iconUrl, @customHeadersJson, @dailyQuotaResetTimezone, @dailyQuotaResetHour, @createdAt, @updatedAt)
    ON CONFLICT(id) DO NOTHING
  `
    )
    .run(node);
  // Ids can be operator-chosen (custom suffix), so a collision is a caller error,
  // never a silent overwrite nor an unhandled constraint failure.
  if ((result?.changes ?? 0) === 0) throw new ProviderNodeIdConflictError(String(node.id));

  backupDbFile("pre-write");
  invalidateDbCache("nodes");
  resetProviderNodePrefixCache();

  return withParsedCustomHeaders({ ...node }, customHeadersJson);
}

export async function updateProviderNode(id: string, data: JsonRecord) {
  const db = getDbInstance() as unknown as DbLike;
  const existing = db.prepare("SELECT * FROM provider_nodes WHERE id = ?").get(id);
  if (!existing) return null;

  const merged: JsonRecord = {
    ...toRecord(rowToCamel(existing)),
    ...data,
    updatedAt: new Date().toISOString(),
  };

  if (data.customHeaders !== undefined) {
    merged["customHeadersJson"] = data.customHeaders ? JSON.stringify(data.customHeaders) : null;
  } else {
    // Partial update that omits customHeaders must PRESERVE the stored value.
    // rowToCamel surfaces the column under `customHeaders` (suffix stripped),
    // never `customHeadersJson`, so read the raw stored JSON from `existing`
    // directly instead of relying on the (absent) merged key — otherwise the
    // UPDATE would bind null and silently wipe the saved headers.
    const existingJson = (existing as JsonRecord).custom_headers_json;
    merged["customHeadersJson"] = typeof existingJson === "string" ? existingJson : null;
  }

  db.prepare(
    `
    UPDATE provider_nodes SET type = @type, name = @name, prefix = @prefix,
    api_type = @apiType, base_url = @baseUrl, chat_path = @chatPath,
    models_path = @modelsPath, icon_url = @iconUrl,
    custom_headers_json = @customHeadersJson,
    daily_quota_reset_timezone = @dailyQuotaResetTimezone,
    daily_quota_reset_hour = @dailyQuotaResetHour,
    updated_at = @updatedAt
    WHERE id = @id
  `
  ).run({
    id,
    type: merged["type"],
    name: merged["name"],
    prefix: merged["prefix"] || null,
    apiType: merged["apiType"] || null,
    baseUrl: merged["baseUrl"] || null,
    chatPath: merged["chatPath"] || null,
    modelsPath: merged["modelsPath"] || null,
    // #2166: iconUrl is nullable — explicit `null` (not omission) clears a previously
    // stored custom icon when the caller submits an empty value.
    iconUrl: merged["iconUrl"] || null,
    customHeadersJson: merged["customHeadersJson"] || null,
    dailyQuotaResetTimezone: merged["dailyQuotaResetTimezone"] || null,
    dailyQuotaResetHour: normalizeDailyQuotaResetHour(merged["dailyQuotaResetHour"]),
    updatedAt: merged["updatedAt"],
  });

  backupDbFile("pre-write");
  invalidateDbCache("nodes");
  resetProviderNodePrefixCache();

  return withParsedCustomHeaders(merged, (merged["customHeadersJson"] as string | null) ?? null);
}

export async function deleteProviderNode(id: string) {
  const db = getDbInstance() as unknown as DbLike;
  const existing = db.prepare("SELECT * FROM provider_nodes WHERE id = ?").get(id);
  if (!existing) return null;

  db.prepare("DELETE FROM provider_nodes WHERE id = ?").run(id);
  backupDbFile("pre-write");
  invalidateDbCache("nodes");
  resetProviderNodePrefixCache();
  return rowToCamel(existing);
}