|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { prisma } from "@/lib/prisma"; |
| 3 | + |
| 4 | +interface ModelInfo { |
| 5 | + id: string; |
| 6 | + name: string; |
| 7 | + meta: { |
| 8 | + profile_image_url: string; |
| 9 | + }; |
| 10 | +} |
| 11 | + |
| 12 | +interface ModelResponse { |
| 13 | + data: { |
| 14 | + info: ModelInfo; |
| 15 | + }[]; |
| 16 | +} |
| 17 | + |
| 18 | +function normalizeUrl(domain: string): string { |
| 19 | + if (!domain) return ""; |
| 20 | + |
| 21 | + // 移除首尾空格 |
| 22 | + let url = domain.trim(); |
| 23 | + |
| 24 | + // 如果没有协议前缀,添加 https:// |
| 25 | + if (!url.startsWith("http://") && !url.startsWith("https://")) { |
| 26 | + url = "https://" + url; |
| 27 | + } |
| 28 | + |
| 29 | + // 移除末尾的斜杠 |
| 30 | + url = url.replace(/\/+$/, ""); |
| 31 | + |
| 32 | + return url; |
| 33 | +} |
| 34 | + |
| 35 | +export async function GET() { |
| 36 | + try { |
| 37 | + const domain = process.env.OPENWEBUI_DOMAIN; |
| 38 | + if (!domain) { |
| 39 | + throw new Error("OPENWEBUI_DOMAIN 环境变量未设置"); |
| 40 | + } |
| 41 | + |
| 42 | + const normalizedDomain = normalizeUrl(domain); |
| 43 | + if (!normalizedDomain) { |
| 44 | + throw new Error("无效的域名格式"); |
| 45 | + } |
| 46 | + |
| 47 | + const apiUrl = `${normalizedDomain}/api/models`; |
| 48 | + console.log("Requesting URL:", apiUrl); // 调试日志 |
| 49 | + |
| 50 | + const response = await fetch(apiUrl, { |
| 51 | + headers: { |
| 52 | + Authorization: `Bearer ${process.env.JWT_TOKEN || ""}`, |
| 53 | + }, |
| 54 | + // 添加超时设置 |
| 55 | + signal: AbortSignal.timeout(10000), // 10 秒超时 |
| 56 | + }); |
| 57 | + |
| 58 | + if (!response.ok) { |
| 59 | + throw new Error(`请求失败: ${response.status} ${response.statusText}`); |
| 60 | + } |
| 61 | + |
| 62 | + const data = (await response.json()) as ModelResponse; |
| 63 | + if (!data || !Array.isArray(data.data)) { |
| 64 | + throw new Error("返回数据格式错误"); |
| 65 | + } |
| 66 | + |
| 67 | + // 获取所有模型的价格信息 |
| 68 | + const modelPrices = await prisma.modelPrice.findMany(); |
| 69 | + const priceMap = new Map(modelPrices.map((mp) => [mp.id, mp])); |
| 70 | + |
| 71 | + // 合并API返回的模型信息和价格信息 |
| 72 | + const models = await Promise.all( |
| 73 | + data.data.map(async (item) => { |
| 74 | + if (!item.info || !item.info.id) { |
| 75 | + console.warn("Invalid model data:", item); |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + let priceInfo = priceMap.get(item.info.id); |
| 80 | + |
| 81 | + // 如果是新模型,创建默认价格记录 |
| 82 | + if (!priceInfo) { |
| 83 | + try { |
| 84 | + priceInfo = await prisma.modelPrice.create({ |
| 85 | + data: { |
| 86 | + id: item.info.id, |
| 87 | + name: item.info.name || "Unknown Model", |
| 88 | + inputPrice: 60, |
| 89 | + outputPrice: 60, |
| 90 | + }, |
| 91 | + }); |
| 92 | + } catch (err) { |
| 93 | + console.error("Error creating price record:", err); |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + id: item.info.id, |
| 100 | + name: item.info.name || "Unknown Model", |
| 101 | + imageUrl: item.info.meta?.profile_image_url || "", |
| 102 | + inputPrice: priceInfo.inputPrice, |
| 103 | + outputPrice: priceInfo.outputPrice, |
| 104 | + }; |
| 105 | + }) |
| 106 | + ); |
| 107 | + |
| 108 | + // 过滤掉无效的模型数据 |
| 109 | + const validModels = models.filter( |
| 110 | + (model): model is NonNullable<typeof model> => model !== null |
| 111 | + ); |
| 112 | + |
| 113 | + return NextResponse.json(validModels); |
| 114 | + } catch (error) { |
| 115 | + console.error( |
| 116 | + "Error fetching models:", |
| 117 | + error instanceof Error ? error.message : "Unknown error" |
| 118 | + ); |
| 119 | + return NextResponse.json( |
| 120 | + { error: error instanceof Error ? error.message : "获取模型失败" }, |
| 121 | + { status: 500 } |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// 添加更新价格的端点 |
| 127 | +export async function PUT(request: Request) { |
| 128 | + try { |
| 129 | + const body = await request.json(); |
| 130 | + const { id, inputPrice, outputPrice } = body; |
| 131 | + |
| 132 | + const updatedPrice = await prisma.modelPrice.update({ |
| 133 | + where: { id }, |
| 134 | + data: { |
| 135 | + inputPrice: parseFloat(inputPrice), |
| 136 | + outputPrice: parseFloat(outputPrice), |
| 137 | + }, |
| 138 | + }); |
| 139 | + |
| 140 | + return NextResponse.json(updatedPrice); |
| 141 | + } catch (error) { |
| 142 | + console.error("Error updating model price:", error); |
| 143 | + return NextResponse.json( |
| 144 | + { error: "Failed to update model price" }, |
| 145 | + { status: 500 } |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
0 commit comments