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.

5.2 KB
Raw Blame
import { ModelProvider } from 'model-bank';

import type { OpenAICompatibleFactoryOptions } from '../../core/openaiCompatible';
import { createOpenAICompatibleRuntime } from '../../core/openaiCompatible';
import type { ChatCompletionErrorPayload } from '../../types';
import { AgentRuntimeErrorType } from '../../types/error';
import { processMultiProviderModelList } from '../../utils/modelParse';
import { createSiliconCloudImage } from './createImage';
import { createSiliconCloudVideo } from './createVideo';

export interface SiliconCloudModelCard {
  id: string;
}

const getByteLength = (value: string): number => {
  return new TextEncoder().encode(value).length;
};

const defaultFetch = globalThis.fetch?.bind(globalThis);

const siliconFetch: typeof fetch = async (input, init) => {
  if (!defaultFetch) return fetch(input, init);

  const response = await defaultFetch(input, init);

  if (!response || response.status < 400) return response;

  const contentType = response.headers.get('content-type') || '';
  if (!contentType.includes('application/json')) return response;

  try {
    const cloned = response.clone();
    const data = await cloned.json();

    if (data && typeof data === 'object' && !('error' in data)) {
      const headers = new Headers(response.headers);
      headers.delete('content-length');

      const body = JSON.stringify({ error: data });
      headers.set('content-length', getByteLength(body).toString());

      return new Response(body, {
        headers,
        status: response.status,
        statusText: response.statusText,
      });
    }
  } catch {
    // ignore JSON parse errors and fall back to original response
  }

  return response;
};

export const params = {
  baseURL: 'https://api.siliconflow.cn/v1',
  chatCompletion: {
    handleError: (error: any): Omit<ChatCompletionErrorPayload, 'provider'> | undefined => {
      const status = error?.status || (error instanceof Response && error.status);

      if (status === 401) {
        return {
          error: status,
          errorType: AgentRuntimeErrorType.InvalidProviderAPIKey,
        };
      }

      if (status === 403) {
        return {
          error: status,
          errorType: AgentRuntimeErrorType.ProviderBizError,
          message:
            'Please check if your API Key balance is sufficient, or if you are using an unverified API Key to access models that require verification.',
        };
      }

      if (error?.error || error?.code || error?.message) {
        // Prioritize nested error structure, then fall back to top-level fields
        const errorData = error?.error?.error || error?.error || error;
        const { code, message, data } = errorData;

        if (code || message || data) {
          return {
            error: {
              ...(code !== undefined ? { code } : {}),
              ...(typeof data !== 'undefined' ? { data } : {}),
              ...(message !== undefined ? { message } : {}),
            },
            errorType: AgentRuntimeErrorType.ProviderBizError,
          };
        }
      }

      return { error };
    },
    handlePayload: (payload) => {
      const { max_tokens, model, thinking, reasoning_effort, ...rest } = payload;
      const thinkingBudget =
        thinking?.budget_tokens === 0 ? 1 : thinking?.budget_tokens || undefined;

      const result: any = {
        ...rest,
        max_tokens:
          max_tokens === undefined ? undefined : Math.min(Math.max(max_tokens, 1), 16_384),
        model,
      };

      const thinkingExplicitlyDisabled = thinking?.type === 'disabled';

      if (thinking) {
        // Only set enable_thinking if type is explicitly provided
        if (typeof thinking.type !== 'undefined') {
          result.enable_thinking = thinking.type === 'enabled';
        }
        if (typeof thinkingBudget !== 'undefined') {
          result.thinking_budget = Math.min(Math.max(thinkingBudget, 128), 32_768);
        }
      }

      // SiliconCloud supports reasoning_effort for deepseek-ai/DeepSeek-V4-Flash.
      // It should not be sent when thinking is explicitly disabled.
      if (!thinkingExplicitlyDisabled && reasoning_effort) {
        result.reasoning_effort = reasoning_effort;
      }

      return result;
    },
  },
  constructorOptions: {
    fetch: siliconFetch,
  },
  createImage: createSiliconCloudImage,
  createVideo: createSiliconCloudVideo,
  handlePollVideoStatus: async (inferenceId, options) => {
    const { pollSiliconCloudVideoStatus } = await import('./createVideo');
    return pollSiliconCloudVideoStatus(inferenceId, {
      apiKey: options.apiKey,
      baseURL: options.baseURL || '',
    });
  },
  debug: {
    chatCompletion: () => process.env.DEBUG_SILICONCLOUD_CHAT_COMPLETION === '1',
  },
  errorType: {
    bizError: AgentRuntimeErrorType.ProviderBizError,
    invalidAPIKey: AgentRuntimeErrorType.InvalidProviderAPIKey,
  },
  models: async ({ client }) => {
    const modelsPage = (await client.models.list()) as any;
    const modelList: SiliconCloudModelCard[] = modelsPage.data;

    return processMultiProviderModelList(modelList, 'siliconcloud');
  },
  provider: ModelProvider.SiliconCloud,
} satisfies OpenAICompatibleFactoryOptions;

export const LobeSiliconCloudAI = createOpenAICompatibleRuntime(params);