1
1
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" ;
3
4
4
5
interface ModelInfo {
5
6
id : string ;
@@ -21,17 +22,20 @@ interface ModelResponse {
21
22
} [ ] ;
22
23
}
23
24
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
+
25
31
try {
26
- // Ensure database is initialized
27
32
await ensureTablesExist ( ) ;
28
33
29
34
const domain = process . env . OPENWEBUI_DOMAIN ;
30
35
if ( ! domain ) {
31
36
throw new Error ( "OPENWEBUI_DOMAIN environment variable is not set." ) ;
32
37
}
33
38
34
- // Normalize API URL
35
39
const apiUrl = domain . replace ( / \/ + $ / , "" ) + "/api/models" ;
36
40
37
41
const response = await fetch ( apiUrl , {
@@ -47,9 +51,7 @@ export async function GET() {
47
51
throw new Error ( `Failed to fetch models: ${ response . status } ` ) ;
48
52
}
49
53
50
- // Get response text for debugging
51
54
const responseText = await response . text ( ) ;
52
- // console.log("API response:", responseText);
53
55
54
56
let data : ModelResponse ;
55
57
try {
@@ -66,13 +68,10 @@ export async function GET() {
66
68
throw new Error ( "Unexpected API response structure" ) ;
67
69
}
68
70
69
- // Get price information for all models
70
71
const modelsWithPrices = await getOrCreateModelPrices (
71
72
data . data . map ( ( item ) => {
72
- // 处理形如 gemini_search.gemini-2.0-flash 的派生模型ID
73
73
let baseModelId = item . info ?. base_model_id ;
74
74
75
- // 如果没有明确的base_model_id,尝试从ID中提取
76
75
if ( ! baseModelId && item . id ) {
77
76
const idParts = String ( item . id ) . split ( "." ) ;
78
77
if ( idParts . length > 1 ) {
@@ -89,10 +88,8 @@ export async function GET() {
89
88
) ;
90
89
91
90
const validModels = data . data . map ( ( item , index ) => {
92
- // 处理形如 gemini_search.gemini-2.0-flash 的派生模型ID
93
91
let baseModelId = item . info ?. base_model_id || "" ;
94
92
95
- // 如果没有明确的base_model_id,尝试从ID中提取
96
93
if ( ! baseModelId && item . id ) {
97
94
const idParts = String ( item . id ) . split ( "." ) ;
98
95
if ( idParts . length > 1 ) {
@@ -126,19 +123,26 @@ export async function GET() {
126
123
}
127
124
}
128
125
129
- // Add inlet endpoint
130
126
export async function POST ( req : Request ) {
127
+ const authError = verifyApiToken ( req ) ;
128
+ if ( authError ) {
129
+ return authError ;
130
+ }
131
+
131
132
const data = await req . json ( ) ;
132
133
133
134
return new Response ( "Inlet placeholder response" , {
134
135
headers : { "Content-Type" : "application/json" } ,
135
136
} ) ;
136
137
}
137
138
138
- // Add outlet endpoint
139
139
export async function PUT ( req : Request ) {
140
+ const authError = verifyApiToken ( req ) ;
141
+ if ( authError ) {
142
+ return authError ;
143
+ }
144
+
140
145
const data = await req . json ( ) ;
141
- // console.log("Outlet received:", JSON.stringify(data, null, 2));
142
146
143
147
return new Response ( "Outlet placeholder response" , {
144
148
headers : { "Content-Type" : "application/json" } ,
0 commit comments