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.

9.0 KB
Raw Blame
import { ModelProvider, zhipu as zhipuChatModels } from 'model-bank';

import {
  createOpenAICompatibleRuntime,
  type OpenAICompatibleFactoryOptions,
} from '../../core/openaiCompatible';
import { resolveParameters } from '../../core/parameterResolver';
import { OpenAIStream } from '../../core/protocols/openaiChatCompletions/stream';
import { convertIterableToStream } from '../../core/streams/protocol';
import { getModelMaxOutputs } from '../../utils/getModelMaxOutputs';
import { MODEL_LIST_CONFIGS, processModelList } from '../../utils/modelParse';
import { createZhipuImage } from './createImage';
import { createZhipuVideo } from './createVideo';
import { isAlwaysOnThinkingGLMModel, isToolStreamSupportedGLMModel } from './modelId';

export interface ZhipuModelCard {
  description: string;
  modelCode: string;
  modelName: string;
}

interface ZhipuRuntimeOptions {
  [key: string]: unknown;
  disableToolStream?: boolean;
}

const isFireworksRuntime = (options: ZhipuRuntimeOptions) =>
  typeof options.baseURL === 'string' && options.baseURL.includes('fireworks.ai');

export const params = {
  baseURL: 'https://open.bigmodel.cn/api/paas/v4',
  chatCompletion: {
    handlePayload: (payload, options: ZhipuRuntimeOptions = {}) => {
      const {
        enabledSearch,
        max_tokens,
        model,
        preserveThinking,
        reasoning_effort,
        stream,
        temperature,
        thinking,
        tools,
        top_p,
        ...rest
      } = payload;

      const messages = (rest.messages || []).map((message: any) => {
        const { reasoning, ...messageRest } = message;

        const reasoningContent =
          typeof messageRest.reasoning_content === 'string'
            ? messageRest.reasoning_content
            : typeof reasoning?.content === 'string'
              ? reasoning.content
              : undefined;

        if (reasoningContent !== undefined) {
          return {
            ...messageRest,
            reasoning_content: reasoningContent,
          };
        }

        return messageRest;
      });

      const shouldSetClearThinking = typeof preserveThinking === 'boolean';
      const thinkingPayload = thinking ? { type: thinking.type } : undefined;
      const resolvedThinking = shouldSetClearThinking
        ? {
            ...thinkingPayload,
            clear_thinking: !preserveThinking,
          }
        : thinkingPayload;
      // GLM-5.3+ rejects thinking.type=disabled; keep effort-only control.
      const enforcedThinking = isAlwaysOnThinkingGLMModel(model)
        ? { ...resolvedThinking, type: 'enabled' as const }
        : resolvedThinking;

      const zhipuTools = enabledSearch
        ? [
            ...(tools || []),
            {
              type: 'web_search',
              web_search: {
                enable: true,
                result_sequence: 'before', // Change search result return sequence to 'before' to minimize OpenAIStream modifications
                search_engine: process.env.ZHIPU_SEARCH_ENGINE || 'search_std', // search_std, search_pro
                search_result: true,
              },
            },
          ]
        : tools;

      // Resolve parameters based on model-specific constraints
      const resolvedParams = resolveParameters(
        {
          max_tokens:
            max_tokens !== undefined
              ? max_tokens
              : getModelMaxOutputs(payload.model, zhipuChatModels),
          temperature,
          top_p,
        },
        {
          // max_tokens constraints
          maxTokensRange: model.includes('glm-4v')
            ? { max: 1024 }
            : model === 'glm-zero-preview'
              ? { max: 15_300 }
              : undefined,
          normalizeTemperature: true,
          // glm-4-alltools has stricter temperature and top_p constraints
          ...(model === 'glm-4-alltools' && {
            temperatureRange: { max: 0.99, min: 0.01 },
            topPRange: { max: 0.99, min: 0.01 },
          }),
        },
      );

      // Example: Fireworks serves GLM-5.2 but rejects the Z.ai-only `tool_stream` field.
      const isFireworks = isFireworksRuntime(options);
      const shouldEnableToolStream =
        !isFireworks &&
        !options.disableToolStream &&
        stream &&
        isToolStreamSupportedGLMModel(model);
      const shouldDropReasoningEffort = Boolean(
        isFireworks && reasoning_effort && enforcedThinking?.type === 'disabled',
      );
      // Example rejected by Fireworks GLM-5.2:
      // { thinking: { type: 'enabled' }, reasoning_effort: 'max' }.
      const shouldDropThinking = Boolean(
        isFireworks && reasoning_effort && enforcedThinking?.type !== 'disabled',
      );

      return {
        ...rest,
        ...resolvedParams,
        messages,
        model,
        ...(reasoning_effort && !shouldDropReasoningEffort ? { reasoning_effort } : {}),
        ...(isFireworks && preserveThinking ? { reasoning_history: 'preserved' } : {}),
        stream,
        thinking: shouldDropThinking ? undefined : enforcedThinking,
        tool_stream: shouldEnableToolStream ? true : undefined,
        tools: zhipuTools,
      } as any;
    },
    handleStream: (stream, { callbacks, inputStartAt, payload }) => {
      const readableStream =
        stream instanceof ReadableStream ? stream : convertIterableToStream(stream);

      // GLM-4.5 series models return index -1 in tool_calls, needs to be fixed before entering OpenAIStream
      // because OpenAIStream internally filters out tool_calls with index < 0 (openai.ts:58-60)
      const preprocessedStream = readableStream.pipeThrough(
        new TransformStream({
          transform(chunk, controller) {
            // Handle raw OpenAI ChatCompletionChunk format
            if (chunk.choices && chunk.choices[0]) {
              const choice = chunk.choices[0];
              if (choice.delta?.tool_calls && Array.isArray(choice.delta.tool_calls)) {
                // Fix negative index, convert -1 to positive index based on array position
                // With tool_stream enabled, some proxies (e.g., aihubmix) send
                // incomplete tool_call chunks without id/function.name before the
                // real chunk arrives. Filter them out to prevent ZodError in parseToolCalls.
                const fixedToolCalls = choice.delta.tool_calls
                  .filter(
                    (toolCall: any) =>
                      // Keep chunks that have id/name (first real chunk) or
                      // non-empty arguments (subsequent incremental chunks)
                      toolCall.id || toolCall.function?.name || toolCall.function?.arguments,
                  )
                  .map((toolCall: any, globalIndex: number) => ({
                    ...toolCall,
                    // Fix negative index (-1 → array position)
                    index: toolCall.index < 0 ? globalIndex : toolCall.index,
                  }));

                if (fixedToolCalls.length === 0) {
                  // All tool_calls were incomplete placeholders, skip this chunk
                  controller.enqueue({ ...chunk, choices: [{ ...choice, delta: {} }] });
                } else {
                  controller.enqueue({
                    ...chunk,
                    choices: [
                      { ...choice, delta: { ...choice.delta, tool_calls: fixedToolCalls } },
                    ],
                  });
                }
              } else {
                controller.enqueue(chunk);
              }
            } else {
              controller.enqueue(chunk);
            }
          },
        }),
      );

      return OpenAIStream(preprocessedStream, {
        callbacks,
        inputStartAt,
        payload,
      });
    },
  },
  createImage: createZhipuImage,
  createVideo: createZhipuVideo,
  handlePollVideoStatus: async (inferenceId, options) => {
    const { pollZhipuVideoStatus } = await import('./createVideo');
    return pollZhipuVideoStatus(inferenceId, {
      apiKey: options.apiKey,
      baseURL: options.baseURL || '',
    });
  },
  debug: {
    chatCompletion: () => process.env.DEBUG_ZHIPU_CHAT_COMPLETION === '1',
  },
  models: async ({ client }) => {
    // ref: https://open.bigmodel.cn/console/modelcenter/square
    const url = 'https://open.bigmodel.cn/api/fine-tuning/model_center/list?pageSize=100&pageNum=1';
    const response = await fetch(url, {
      headers: {
        'Authorization': `Bearer ${client.apiKey}`,
        'Bigmodel-Organization': 'lobehub',
        'Bigmodel-Project': 'lobechat',
      },
      method: 'GET',
    });
    const json = await response.json();

    const modelList: ZhipuModelCard[] = json.rows;

    const standardModelList = modelList.map((model) => ({
      description: model.description,
      displayName: model.modelName,
      id: model.modelCode,
    }));
    return processModelList(standardModelList, MODEL_LIST_CONFIGS.zhipu, 'zhipu');
  },
  provider: ModelProvider.ZhiPu,
} satisfies OpenAICompatibleFactoryOptions<ZhipuRuntimeOptions>;

export const LobeZhipuAI = createOpenAICompatibleRuntime<ZhipuRuntimeOptions>(params);