mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-03 05:45:04 +03:00
* feat: support Bun bundled SQLite runtime * fix: harden Bun SQLite backups and params * docs: keep Node as the only supported runtime; document bun:sqlite as best-effort compatibility path Co-authored-by: Arul Kumaran <arul@luracast.com> --------- Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import os from "os";
|
|
import path from "path";
|
|
import { createRequire } from "node:module";
|
|
|
|
const _require = createRequire(import.meta.url);
|
|
const Database = process.versions.bun
|
|
? (_require("bun:sqlite").Database as typeof import("better-sqlite3"))
|
|
: (_require("better-sqlite3") as typeof import("better-sqlite3"));
|
|
|
|
function databaseOptions(readonly = false) {
|
|
return readonly ? { readonly: true } : { readwrite: true, create: true };
|
|
}
|
|
|
|
const getOmpDir = () => path.join(os.homedir(), ".omp", "agent");
|
|
const getOmpDbPath = () => path.join(getOmpDir(), "agent.db");
|
|
|
|
export function getOmpCredentials(providerId: string) {
|
|
const dbPath = getOmpDbPath();
|
|
try {
|
|
const db = new Database(dbPath, databaseOptions(true));
|
|
const row = db
|
|
.prepare(
|
|
"SELECT data FROM auth_credentials WHERE provider = ? AND credential_type = 'api_key'"
|
|
)
|
|
.get(providerId) as { data: string } | undefined;
|
|
db.close();
|
|
|
|
if (row?.data) {
|
|
const parsed = JSON.parse(row.data);
|
|
return { hasOmniRoute: true, baseUrl: parsed.baseUrl || null, apiKey: parsed.apiKey || null };
|
|
}
|
|
return { hasOmniRoute: false, baseUrl: null, apiKey: null };
|
|
} catch {
|
|
return { hasOmniRoute: false, baseUrl: null, apiKey: null };
|
|
}
|
|
}
|
|
|
|
export function saveOmpCredentials(providerId: string, apiKey: string, baseUrl: string) {
|
|
const dbPath = getOmpDbPath();
|
|
const db = new Database(dbPath, databaseOptions());
|
|
|
|
db.prepare("DELETE FROM auth_credentials WHERE provider = ?").run(providerId);
|
|
db.prepare(
|
|
"INSERT INTO auth_credentials (provider, credential_type, data, disabled_cause, identity_key, created_at, updated_at) VALUES (?, ?, ?, NULL, NULL, ?, ?)"
|
|
).run(
|
|
providerId,
|
|
"api_key",
|
|
JSON.stringify({ apiKey, baseUrl }),
|
|
Math.floor(Date.now() / 1000),
|
|
Math.floor(Date.now() / 1000)
|
|
);
|
|
|
|
db.close();
|
|
}
|
|
|
|
export function deleteOmpCredentials(providerId: string) {
|
|
const dbPath = getOmpDbPath();
|
|
const db = new Database(dbPath, databaseOptions());
|
|
db.prepare("DELETE FROM auth_credentials WHERE provider = ?").run(providerId);
|
|
db.close();
|
|
}
|