Files
llm-proxy-api/scripts/seed.ts
T
2026-02-27 21:18:44 +01:00

284 lines
8.6 KiB
TypeScript

// @ts-nocheck
import "dotenv/config";
import { hash } from "bcryptjs";
import { MongoClient } from "mongodb";
function getDatabaseUrl() {
const url = process.env.DATABASE_URL;
if (!url) throw new Error("Missing DATABASE_URL.");
return url;
}
function getDatabaseNameFromUrl(urlString: string) {
const parsed = new URL(urlString);
const dbName = parsed.pathname.replace("/", "");
return dbName || "test";
}
async function main() {
console.log("Seeding database...");
const databaseUrl = getDatabaseUrl();
const client = new MongoClient(databaseUrl);
await client.connect();
const db = client.db(getDatabaseNameFromUrl(databaseUrl));
try {
const now = new Date();
const plans = [
{
name: "free",
displayName: "Free",
description: "Get started with basic access",
monthlyPriceCents: 0,
dailyRequestLimit: 100,
dailyTokenLimit: 100000,
requestLimitPerMonth: 3000,
tokenLimitPerMonth: 3000000,
maxApiKeys: 2,
sortOrder: 0,
isPublic: true,
},
{
name: "pro",
displayName: "Pro",
description: "For developers and small teams",
monthlyPriceCents: 2900,
dailyRequestLimit: 5000,
dailyTokenLimit: 2000000,
requestLimitPerMonth: 150000,
tokenLimitPerMonth: 60000000,
maxApiKeys: 10,
sortOrder: 1,
isPublic: true,
},
{
name: "enterprise",
displayName: "Enterprise",
description: "Unlimited access for large teams",
monthlyPriceCents: 9900,
dailyRequestLimit: null,
dailyTokenLimit: null,
requestLimitPerMonth: null,
tokenLimitPerMonth: null,
maxApiKeys: 50,
sortOrder: 2,
isPublic: true,
},
];
for (const plan of plans) {
await db.collection("subscription_plans").updateOne(
{ name: plan.name },
{
$setOnInsert: {
_id: crypto.randomUUID(),
createdAt: now,
},
$set: {
updatedAt: now,
...plan,
},
},
{ upsert: true },
);
}
console.log(" Plans created");
const proPlan = await db.collection<{ _id: string }>("subscription_plans").findOne({ name: "pro" });
if (!proPlan) throw new Error("Pro plan not found after upsert.");
const adminEmail = "[email protected]";
const adminPassword = await hash("admin123", 12);
const existingAdmin = await db.collection<{ _id: string }>("users").findOne({ email: adminEmail });
const adminId = existingAdmin?._id ?? crypto.randomUUID();
await db.collection("users").updateOne(
{ email: adminEmail },
{
$setOnInsert: {
_id: adminId,
email: adminEmail,
passwordHash: adminPassword,
name: "Admin",
role: "ADMIN",
status: "ACTIVE",
createdAt: now,
},
$set: {
updatedAt: now,
},
},
{ upsert: true },
);
await db.collection("subscriptions").updateOne(
{ userId: adminId },
{
$setOnInsert: {
_id: crypto.randomUUID(),
userId: adminId,
planId: proPlan._id,
status: "ACTIVE",
currentPeriodStart: now,
currentPeriodEnd: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000),
createdAt: now,
},
$set: {
updatedAt: now,
},
},
{ upsert: true },
);
console.log(` Admin user created: ${adminEmail} / admin123`);
const providers = [
{ id: "openai", displayName: "OpenAI", baseUrl: "https://api.openai.com" },
{ id: "anthropic", displayName: "Anthropic", baseUrl: "https://api.anthropic.com" },
{ id: "grok", displayName: "Grok (xAI)", baseUrl: "https://api.x.ai" },
{ id: "deepseek", displayName: "DeepSeek", baseUrl: "https://api.deepseek.com" },
{ id: "perplexity", displayName: "Perplexity", baseUrl: "https://api.perplexity.ai" },
];
for (const provider of providers) {
await db.collection("providers").updateOne(
{ _id: provider.id },
{
$setOnInsert: { createdAt: now },
$set: {
displayName: provider.displayName,
baseUrl: provider.baseUrl,
enabled: true,
updatedAt: now,
},
},
{ upsert: true },
);
}
console.log(" Providers created");
const mappings = [
{ logicalModelId: "gpt-4o", providerId: "openai", providerModelId: "gpt-4o", priority: 0 },
{ logicalModelId: "gpt-4o-mini", providerId: "openai", providerModelId: "gpt-4o-mini", priority: 0 },
{ logicalModelId: "gpt-4.1", providerId: "openai", providerModelId: "gpt-4.1", priority: 0 },
{ logicalModelId: "gpt-4.1-mini", providerId: "openai", providerModelId: "gpt-4.1-mini", priority: 0 },
{ logicalModelId: "gpt-4.1-nano", providerId: "openai", providerModelId: "gpt-4.1-nano", priority: 0 },
{ logicalModelId: "o3", providerId: "openai", providerModelId: "o3", priority: 0 },
{ logicalModelId: "o3-mini", providerId: "openai", providerModelId: "o3-mini", priority: 0 },
{ logicalModelId: "o4-mini", providerId: "openai", providerModelId: "o4-mini", priority: 0 },
{
logicalModelId: "claude-sonnet-4-20250514",
providerId: "anthropic",
providerModelId: "claude-sonnet-4-20250514",
priority: 0,
},
{
logicalModelId: "claude-haiku-4-20250414",
providerId: "anthropic",
providerModelId: "claude-haiku-4-20250414",
priority: 0,
},
{
logicalModelId: "claude-opus-4-20250514",
providerId: "anthropic",
providerModelId: "claude-opus-4-20250514",
priority: 0,
},
{ logicalModelId: "deepseek-chat", providerId: "deepseek", providerModelId: "deepseek-chat", priority: 0 },
{
logicalModelId: "deepseek-reasoner",
providerId: "deepseek",
providerModelId: "deepseek-reasoner",
priority: 0,
},
{ logicalModelId: "grok-3", providerId: "grok", providerModelId: "grok-3", priority: 0 },
{ logicalModelId: "grok-3-mini", providerId: "grok", providerModelId: "grok-3-mini", priority: 0 },
{ logicalModelId: "sonar", providerId: "perplexity", providerModelId: "sonar", priority: 0 },
{ logicalModelId: "sonar-pro", providerId: "perplexity", providerModelId: "sonar-pro", priority: 0 },
{
logicalModelId: "sonar-reasoning",
providerId: "perplexity",
providerModelId: "sonar-reasoning",
priority: 0,
},
{
logicalModelId: "sonar-reasoning-pro",
providerId: "perplexity",
providerModelId: "sonar-reasoning-pro",
priority: 0,
},
// Image generation models
{
logicalModelId: "gpt-image-1",
providerId: "openai",
providerModelId: "gpt-image-1",
priority: 0,
modelType: "image",
},
{
logicalModelId: "dall-e-3",
providerId: "openai",
providerModelId: "dall-e-3",
priority: 0,
modelType: "image",
},
{
logicalModelId: "dall-e-2",
providerId: "openai",
providerModelId: "dall-e-2",
priority: 0,
modelType: "image",
},
// Audio TTS models
{
logicalModelId: "tts-1",
providerId: "openai",
providerModelId: "tts-1",
priority: 0,
modelType: "audio",
},
{
logicalModelId: "tts-1-hd",
providerId: "openai",
providerModelId: "tts-1-hd",
priority: 0,
modelType: "audio",
},
// Grok image model
{
logicalModelId: "grok-2-image",
providerId: "grok",
providerModelId: "grok-2-image",
priority: 0,
modelType: "image",
},
];
for (const mapping of mappings) {
await db.collection("model_mappings").updateOne(
{
logicalModelId: mapping.logicalModelId,
providerId: mapping.providerId,
providerModelId: mapping.providerModelId,
},
{
$setOnInsert: { _id: crypto.randomUUID(), createdAt: now },
$set: { ...mapping, enabled: true, modelType: mapping.modelType ?? "chat", updatedAt: now },
},
{ upsert: true },
);
}
console.log(` ${mappings.length} model mappings created`);
console.log("\nSeed complete!");
console.log(" Admin login: [email protected] / admin123");
} finally {
await client.close();
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});