Skip to content

Commit 4ac7e50

Browse files
committed
fix: 从数据库获取最新的 route 值作为 API Key
1 parent e36071a commit 4ac7e50

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

app/api/sync-model-prices/route.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { NextResponse } from "next/server";
22
import { cookies } from "next/headers";
3-
import { inArray } from "drizzle-orm";
3+
import { inArray, desc } from "drizzle-orm";
44
import { config } from "@/lib/config";
55
import { db } from "@/lib/db/client";
6-
import { modelPrices } from "@/lib/db/schema";
6+
import { modelPrices, usageRecords } from "@/lib/db/schema";
77

88
export const runtime = "nodejs";
99

@@ -83,12 +83,6 @@ export async function POST(request: Request) {
8383
syncInFlight = true;
8484
syncStartedAt = now;
8585

86-
// 使用服务端配置的 API Key,而不是客户端传入
87-
const apiKey = config.cliproxy.apiKey;
88-
if (!apiKey) {
89-
return NextResponse.json({ error: "服务端未配置 CLIPROXY_SECRET_KEY" }, { status: 500 });
90-
}
91-
9286
if (!config.cliproxy.baseUrl) {
9387
return NextResponse.json({ error: "服务端未配置 CLIPROXY_API_BASE_URL" }, { status: 500 });
9488
}
@@ -97,6 +91,19 @@ export async function POST(request: Request) {
9791
return NextResponse.json({ error: "服务端未配置 DATABASE_URL" }, { status: 500 });
9892
}
9993

94+
// 从数据库获取最新的 route 值作为 API Key
95+
const latestRecord = await db
96+
.select({ route: usageRecords.route })
97+
.from(usageRecords)
98+
.orderBy(desc(usageRecords.id))
99+
.limit(1);
100+
101+
if (!latestRecord.length || !latestRecord[0].route) {
102+
return NextResponse.json({ error: "数据库中没有可用的 API Key 记录" }, { status: 500 });
103+
}
104+
105+
const apiKey = latestRecord[0].route;
106+
100107
// 1. 从 models.dev 获取价格数据
101108
const modelsDevHeaders: Record<string, string> = { "Accept": "application/json" };
102109
if (modelsDevETag) modelsDevHeaders["If-None-Match"] = modelsDevETag;
@@ -149,7 +156,9 @@ export async function POST(request: Request) {
149156
}
150157

151158
// 3. 从 CLIProxyAPI 获取当前模型列表
152-
const modelsUrl = `${config.cliproxy.baseUrl}/models`;
159+
// 使用 OpenAI 兼容的 /v1/models 端点而非管理 API
160+
const baseUrlWithoutManagement = config.cliproxy.baseUrl.replace(/\/v0\/management\/?$/, "");
161+
const modelsUrl = `${baseUrlWithoutManagement}/v1/models`;
153162
const cliproxyRes = await fetch(modelsUrl, {
154163
headers: { "Authorization": `Bearer ${apiKey}`, "Accept": "application/json" },
155164
cache: "no-store"

app/components/Modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function Modal({
4949
setIsClosing(false);
5050
setCachedChildren(null);
5151
setCachedTitle(undefined);
52-
}, 200); // Match animation duration
52+
}, 150); // Match animation duration
5353
return () => clearTimeout(timer);
5454
}
5555
}, [isClosing]);

0 commit comments

Comments
 (0)