Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: Optimized New API provider (#10452)

sxjeru7f40f15cbbf3
diff --git a/packages/model-runtime/src/providers/newapi/index.ts b/packages/model-runtime/src/providers/newapi/index.ts
index 18bd8ee0630..7f148504b96 100644
--- a/packages/model-runtime/src/providers/newapi/index.ts
+++ b/packages/model-runtime/src/providers/newapi/index.ts
@@ -25,6 +25,62 @@ export interface NewAPIPricing {
supported_endpoint_types?: string[];
}
+/**
+ * Detect if running in browser environment
+ */
+const isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined';
+
+/**
+ * Parse a pricing API HTTP response into a `NewAPIPricing[] | null`.
+ * Shared between browser and server branches to avoid duplicated logic.
+ */
+const parsePricingResponse = async (res: Response): Promise<NewAPIPricing[] | null> => {
+ if (!res.ok) {
+ return null;
+ }
+
+ try {
+ const body = await res.json();
+ return body?.success && body?.data ? (body.data as NewAPIPricing[]) : null;
+ } catch {
+ return null;
+ }
+};
+
+/**
+ * Fetch pricing information with CORS bypass for client-side requests
+ * In browser environment, use /webapi/proxy to avoid CORS errors
+ */
+const fetchPricing = async (
+ pricingUrl: string,
+ apiKey: string,
+): Promise<NewAPIPricing[] | null> => {
+ try {
+ if (isBrowser()) {
+ // In browser environment, use the proxy endpoint to avoid CORS
+ // The proxy endpoint expects the URL as the request body
+ const proxyResponse = await fetch('/webapi/proxy', {
+ body: pricingUrl,
+ method: 'POST',
+ });
+
+ return await parsePricingResponse(proxyResponse);
+ } else {
+ // In server environment, fetch directly
+ const pricingResponse = await fetch(pricingUrl, {
+ headers: {
+ Authorization: `Bearer ${apiKey}`,
+ },
+ });
+
+ return await parsePricingResponse(pricingResponse);
+ }
+ } catch (error) {
+ console.debug('Failed to fetch NewAPI pricing info:', error);
+ return null;
+ }
+};
+
export const params = {
debug: {
chatCompletion: () => process.env.DEBUG_NEWAPI_CHAT_COMPLETION === '1',
@@ -42,25 +98,12 @@ export const params = {
// Try to get pricing information to enrich model details
let pricingMap: Map<string, NewAPIPricing> = new Map();
- try {
- // Use saved baseURL
- const pricingResponse = await fetch(`${baseURL}/api/pricing`, {
- headers: {
- Authorization: `Bearer ${openAIClient.apiKey}`,
- },
- });
- if (pricingResponse.ok) {
- const pricingData = await pricingResponse.json();
- if (pricingData.success && pricingData.data) {
- (pricingData.data as NewAPIPricing[]).forEach((pricing) => {
- pricingMap.set(pricing.model_name, pricing);
- });
- }
- }
- } catch (error) {
- // If fetching pricing information fails, continue using the basic model information
- console.debug('Failed to fetch NewAPI pricing info:', error);
+ const pricingList = await fetchPricing(`${baseURL}/api/pricing`, openAIClient.apiKey || '');
+ if (pricingList) {
+ pricingList.forEach((pricing) => {
+ pricingMap.set(pricing.model_name, pricing);
+ });
}
// Process the model list: determine the provider for each model based on priority rules
diff --git a/packages/model-runtime/src/runtimeMap.ts b/packages/model-runtime/src/runtimeMap.ts
index ab3624b2ccb..9af69fc2f2f 100644
--- a/packages/model-runtime/src/runtimeMap.ts
+++ b/packages/model-runtime/src/runtimeMap.ts
@@ -112,6 +112,7 @@ export const providerRuntimeMap = {
ppio: LobePPIOAI,
qiniu: LobeQiniuAI,
qwen: LobeQwenAI,
+ router: LobeNewAPIAI,
sambanova: LobeSambaNovaAI,
search1api: LobeSearch1API,
sensenova: LobeSenseNovaAI,
diff --git a/src/app/[variants]/(main)/settings/provider/features/ProviderConfig/UpdateProviderInfo/SettingModal.tsx b/src/app/[variants]/(main)/settings/provider/features/ProviderConfig/UpdateProviderInfo/SettingModal.tsx
index 000ea36e121..2c9380a03b0 100644
--- a/src/app/[variants]/(main)/settings/provider/features/ProviderConfig/UpdateProviderInfo/SettingModal.tsx
+++ b/src/app/[variants]/(main)/settings/provider/features/ProviderConfig/UpdateProviderInfo/SettingModal.tsx
@@ -84,12 +84,16 @@ const CreateNewProvider = memo<CreateNewProviderProps>(({ onClose, open, initial
{
children: (
<Select
- optionRender={({ label, value }) => (
- <Flexbox align={'center'} gap={8} horizontal>
- <ProviderIcon provider={value as string} size={18} />
- {label}
- </Flexbox>
- )}
+ optionRender={({ label, value }) => {
+ // Map 'router' to 'newapi' for displaying the correct icon
+ const iconProvider = value === 'router' ? 'newapi' : (value as string);
+ return (
+ <Flexbox align={'center'} gap={8} horizontal>
+ <ProviderIcon provider={iconProvider} size={18} />
+ {label}
+ </Flexbox>
+ );
+ }}
options={CUSTOM_PROVIDER_SDK_OPTIONS}
placeholder={t('createNewAiProvider.sdkType.placeholder')}
variant={'filled'}