Skip to content

Commit 34c254e

Browse files
authored
Merge branch 'main' into main
2 parents 69f6832 + 6d11f7a commit 34c254e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+847
-837
lines changed

app/api/config/route.ts

-23
This file was deleted.

app/api/init/route.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { NextResponse } from "next/server";
2+
import { initDatabase } from "@/lib/db/client";
3+
4+
let initialized = false;
5+
6+
export async function GET() {
7+
if (!initialized) {
8+
try {
9+
await initDatabase();
10+
initialized = true;
11+
return NextResponse.json({ success: true, message: "数据库初始化成功" });
12+
} catch (error) {
13+
console.error("数据库初始化失败:", error);
14+
return NextResponse.json(
15+
{ success: false, error: "数据库初始化失败" },
16+
{ status: 500 }
17+
);
18+
}
19+
} else {
20+
return NextResponse.json({ success: true, message: "数据库已初始化" });
21+
}
22+
}

app/api/config/key/route.ts renamed to app/api/v1/config/key/route.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { NextResponse } from "next/server";
2-
import { cookies } from "next/headers";
2+
import { verifyApiToken } from "@/lib/auth";
3+
4+
export async function GET(req: Request) {
5+
const authError = verifyApiToken(req);
6+
if (authError) {
7+
return authError;
8+
}
39

4-
export async function GET() {
510
const apiKey = process.env.API_KEY;
611

712
if (!apiKey) {

app/api/v1/config/route.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { NextResponse } from "next/server";
2+
import { verifyApiToken } from "@/lib/auth";
3+
4+
export async function GET(req: Request) {
5+
const authError = verifyApiToken(req);
6+
if (authError) {
7+
return authError;
8+
}
9+
10+
return NextResponse.json({
11+
apiKey: process.env.API_KEY || "Unconfigured",
12+
status: 200,
13+
});
14+
}

app/api/v1/inlet/route.ts

-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export async function POST(req: Request) {
99
const user = await getOrCreateUser(data.user);
1010
const modelId = data.body?.model;
1111

12-
// 如果用户被拉黑,返回余额为 -1
1312
if (user.deleted) {
1413
return NextResponse.json({
1514
success: true,
@@ -18,10 +17,8 @@ export async function POST(req: Request) {
1817
});
1918
}
2019

21-
// 获取预扣费金额
2220
const inletCost = getModelInletCost(modelId);
2321

24-
// 预扣费
2522
if (inletCost > 0) {
2623
const userResult = await query(
2724
`UPDATE users

app/api/v1/models/price/route.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { updateModelPrice } from "@/lib/db";
2+
import { updateModelPrice } from "@/lib/db/client";
3+
import { verifyApiToken } from "@/lib/auth";
34

45
interface PriceUpdate {
56
id: string;
@@ -9,11 +10,15 @@ interface PriceUpdate {
910
}
1011

1112
export async function POST(request: NextRequest) {
13+
const authError = verifyApiToken(request);
14+
if (authError) {
15+
return authError;
16+
}
17+
1218
try {
1319
const data = await request.json();
1420
console.log("Raw data received:", data);
1521

16-
// 从对象中提取模型数组
1722
const updates = data.updates || data;
1823
if (!Array.isArray(updates)) {
1924
console.error("Invalid data format - expected array:", updates);
@@ -23,7 +28,6 @@ export async function POST(request: NextRequest) {
2328
);
2429
}
2530

26-
// 验证并转换数据格式
2731
const validUpdates = updates
2832
.map((update: any) => ({
2933
id: update.id,
@@ -52,7 +56,6 @@ export async function POST(request: NextRequest) {
5256
`Successfully verified price updating requests of ${validUpdates.length} models`
5357
);
5458

55-
// 执行批量更新并收集结果
5659
const results = await Promise.all(
5760
validUpdates.map(async (update: PriceUpdate) => {
5861
try {

app/api/v1/models/route.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextResponse } from "next/server";
2-
import { ensureTablesExist, getOrCreateModelPrices } from "@/lib/db";
2+
import { ensureTablesExist, getOrCreateModelPrices } from "@/lib/db/client";
3+
import { verifyApiToken } from "@/lib/auth";
34

45
interface ModelInfo {
56
id: string;
@@ -21,17 +22,20 @@ interface ModelResponse {
2122
}[];
2223
}
2324

24-
export async function GET() {
25+
export async function GET(req: Request) {
26+
const authError = verifyApiToken(req);
27+
if (authError) {
28+
return authError;
29+
}
30+
2531
try {
26-
// Ensure database is initialized
2732
await ensureTablesExist();
2833

2934
const domain = process.env.OPENWEBUI_DOMAIN;
3035
if (!domain) {
3136
throw new Error("OPENWEBUI_DOMAIN environment variable is not set.");
3237
}
3338

34-
// Normalize API URL
3539
const apiUrl = domain.replace(/\/+$/, "") + "/api/models";
3640

3741
const response = await fetch(apiUrl, {
@@ -47,9 +51,7 @@ export async function GET() {
4751
throw new Error(`Failed to fetch models: ${response.status}`);
4852
}
4953

50-
// Get response text for debugging
5154
const responseText = await response.text();
52-
// console.log("API response:", responseText);
5355

5456
let data: ModelResponse;
5557
try {
@@ -66,13 +68,10 @@ export async function GET() {
6668
throw new Error("Unexpected API response structure");
6769
}
6870

69-
// Get price information for all models
7071
const modelsWithPrices = await getOrCreateModelPrices(
7172
data.data.map((item) => {
72-
// 处理形如 gemini_search.gemini-2.0-flash 的派生模型ID
7373
let baseModelId = item.info?.base_model_id;
7474

75-
// 如果没有明确的base_model_id,尝试从ID中提取
7675
if (!baseModelId && item.id) {
7776
const idParts = String(item.id).split(".");
7877
if (idParts.length > 1) {
@@ -89,10 +88,8 @@ export async function GET() {
8988
);
9089

9190
const validModels = data.data.map((item, index) => {
92-
// 处理形如 gemini_search.gemini-2.0-flash 的派生模型ID
9391
let baseModelId = item.info?.base_model_id || "";
9492

95-
// 如果没有明确的base_model_id,尝试从ID中提取
9693
if (!baseModelId && item.id) {
9794
const idParts = String(item.id).split(".");
9895
if (idParts.length > 1) {
@@ -126,19 +123,26 @@ export async function GET() {
126123
}
127124
}
128125

129-
// Add inlet endpoint
130126
export async function POST(req: Request) {
127+
const authError = verifyApiToken(req);
128+
if (authError) {
129+
return authError;
130+
}
131+
131132
const data = await req.json();
132133

133134
return new Response("Inlet placeholder response", {
134135
headers: { "Content-Type": "application/json" },
135136
});
136137
}
137138

138-
// Add outlet endpoint
139139
export async function PUT(req: Request) {
140+
const authError = verifyApiToken(req);
141+
if (authError) {
142+
return authError;
143+
}
144+
140145
const data = await req.json();
141-
// console.log("Outlet received:", JSON.stringify(data, null, 2));
142146

143147
return new Response("Outlet placeholder response", {
144148
headers: { "Content-Type": "application/json" },

app/api/v1/models/sync-all-prices/route.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { pool } from "@/lib/db";
2+
import { pool } from "@/lib/db/client";
3+
import { verifyApiToken } from "@/lib/auth";
34

45
export async function POST(request: NextRequest) {
6+
const authError = verifyApiToken(request);
7+
if (authError) {
8+
return authError;
9+
}
10+
511
try {
612
const client = await pool.connect();
713
try {
8-
// 1. 获取所有有效的派生模型(base_model_id 存在且在数据库中有对应记录)
914
const derivedModelsResult = await client.query(`
1015
SELECT d.id, d.name, d.base_model_id
1116
FROM model_prices d
@@ -24,10 +29,8 @@ export async function POST(request: NextRequest) {
2429
const derivedModels = derivedModelsResult.rows;
2530
const syncResults = [];
2631

27-
// 2. 为每个派生模型同步价格
2832
for (const derivedModel of derivedModels) {
2933
try {
30-
// 获取上游模型价格
3134
const baseModelResult = await client.query(
3235
`SELECT input_price, output_price, per_msg_price FROM model_prices WHERE id = $1`,
3336
[derivedModel.base_model_id]
@@ -45,7 +48,6 @@ export async function POST(request: NextRequest) {
4548

4649
const baseModel = baseModelResult.rows[0];
4750

48-
// 更新派生模型价格
4951
const updateResult = await client.query(
5052
`UPDATE model_prices
5153
SET

app/api/v1/models/sync-price/route.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { pool } from "@/lib/db";
2+
import { pool } from "@/lib/db/client";
3+
import { verifyApiToken } from "@/lib/auth";
34

45
export async function POST(request: NextRequest) {
6+
const authError = verifyApiToken(request);
7+
if (authError) {
8+
return authError;
9+
}
10+
511
try {
612
const data = await request.json();
713
const { modelId } = data;
@@ -15,7 +21,6 @@ export async function POST(request: NextRequest) {
1521

1622
const client = await pool.connect();
1723
try {
18-
// 1. 获取派生模型信息
1924
const derivedModelResult = await client.query(
2025
`SELECT id, name, base_model_id FROM model_prices WHERE id = $1`,
2126
[modelId]
@@ -28,13 +33,11 @@ export async function POST(request: NextRequest) {
2833
const derivedModel = derivedModelResult.rows[0];
2934
let baseModelId = derivedModel.base_model_id;
3035

31-
// 如果数据库中没有base_model_id,尝试从ID中提取
3236
if (!baseModelId) {
3337
const idParts = modelId.split(".");
3438
if (idParts.length > 1) {
3539
baseModelId = idParts[idParts.length - 1];
3640

37-
// 更新数据库中的base_model_id
3841
await client.query(
3942
`UPDATE model_prices SET base_model_id = $2 WHERE id = $1`,
4043
[modelId, baseModelId]
@@ -49,7 +52,6 @@ export async function POST(request: NextRequest) {
4952
);
5053
}
5154

52-
// 2. 获取上游模型价格
5355
const baseModelResult = await client.query(
5456
`SELECT input_price, output_price, per_msg_price FROM model_prices WHERE id = $1`,
5557
[baseModelId]
@@ -64,7 +66,6 @@ export async function POST(request: NextRequest) {
6466

6567
const baseModel = baseModelResult.rows[0];
6668

67-
// 3. 更新派生模型价格
6869
const updateResult = await client.query(
6970
`UPDATE model_prices
7071
SET

app/api/v1/models/test/route.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { NextResponse } from "next/server";
2+
import { verifyApiToken } from "@/lib/auth";
23

34
export async function POST(req: Request) {
5+
const authError = verifyApiToken(req);
6+
if (authError) {
7+
return authError;
8+
}
9+
410
try {
511
const { modelId } = await req.json();
612

0 commit comments

Comments
 (0)