47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import "server-only";
|
||
|
||
import { getEnabledAiProviders } from "@/lib/db/queries";
|
||
import type { TripoProviderConfig } from "./types";
|
||
|
||
const DEFAULT_TRIPO_BASE_URL = "https://api.tripo3d.ai/v2/openapi";
|
||
|
||
function sanitizeTripoApiKey(value: string): string {
|
||
return String(value || "")
|
||
.replace(/[\u2028\u2029]/g, "")
|
||
.replace(/[\u0000-\u001F\u007F]/g, "")
|
||
.replace(/\s+/g, "")
|
||
.trim();
|
||
}
|
||
|
||
function sanitizeTripoBaseUrl(value: string): string {
|
||
return String(value || "")
|
||
.replace(/[\u2028\u2029]/g, "")
|
||
.replace(/[\u0000-\u001F\u007F]/g, "")
|
||
.trim();
|
||
}
|
||
|
||
export async function getTripoProviderConfig(): Promise<TripoProviderConfig> {
|
||
const providers = await getEnabledAiProviders();
|
||
const tripoProvider = providers.find((item) => item.type === "tripo-3d");
|
||
|
||
const providerApiKey = sanitizeTripoApiKey(String(tripoProvider?.apiKey || ""));
|
||
if (tripoProvider && providerApiKey) {
|
||
return {
|
||
apiKey: providerApiKey,
|
||
baseUrl: sanitizeTripoBaseUrl(String(tripoProvider?.baseUrl || "")) || DEFAULT_TRIPO_BASE_URL,
|
||
providerId: tripoProvider.id,
|
||
providerName: tripoProvider.name,
|
||
};
|
||
}
|
||
|
||
const envApiKey = sanitizeTripoApiKey(String(process.env.TRIPO_API_KEY || ""));
|
||
if (!envApiKey) {
|
||
throw new Error("未配置 Tripo3D API,请在系统设置-本地AI新增 Tripo3D 提供商");
|
||
}
|
||
|
||
return {
|
||
apiKey: envApiKey,
|
||
baseUrl: sanitizeTripoBaseUrl(String(process.env.TRIPO_BASE_URL || "")) || DEFAULT_TRIPO_BASE_URL,
|
||
};
|
||
}
|