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.

✨ feat: add GLM-5 model support to LobeHub provider (#13189)

YuTengjingfadaeef8d3de
diff --git a/packages/model-bank/src/aiModels/lobehub/chat/index.ts b/packages/model-bank/src/aiModels/lobehub/chat/index.ts
index edff8c17a5c..45e013ac699 100644
--- a/packages/model-bank/src/aiModels/lobehub/chat/index.ts
+++ b/packages/model-bank/src/aiModels/lobehub/chat/index.ts
@@ -7,6 +7,7 @@ import { moonshotChatModels } from './moonshot';
import { openaiChatModels } from './openai';
import { xaiChatModels } from './xai';
import { xiaomimimoChatModels } from './xiaomimimo';
+import { zhipuChatModels } from './zhipu';
export const lobehubChatModels: AIChatModelCard[] = [
...anthropicChatModels,
@@ -14,8 +15,9 @@ export const lobehubChatModels: AIChatModelCard[] = [
...openaiChatModels,
...deepseekChatModels,
...xaiChatModels,
- ...minimaxChatModels,
...moonshotChatModels,
+ ...minimaxChatModels,
+ ...zhipuChatModels,
...xiaomimimoChatModels,
];
@@ -27,3 +29,4 @@ export { moonshotChatModels } from './moonshot';
export { openaiChatModels } from './openai';
export { xaiChatModels } from './xai';
export { xiaomimimoChatModels } from './xiaomimimo';
+export { zhipuChatModels } from './zhipu';
diff --git a/packages/model-bank/src/aiModels/lobehub/chat/zhipu.ts b/packages/model-bank/src/aiModels/lobehub/chat/zhipu.ts
new file mode 100644
index 00000000000..e5fb3cb58bb
--- /dev/null
+++ b/packages/model-bank/src/aiModels/lobehub/chat/zhipu.ts
@@ -0,0 +1,31 @@
+import type { AIChatModelCard } from '../../../types/aiModel';
+
+export const zhipuChatModels: AIChatModelCard[] = [
+ {
+ abilities: {
+ functionCall: true,
+ reasoning: true,
+ search: true,
+ },
+ contextWindowTokens: 200_000,
+ description:
+ "Zai's new-generation flagship foundation model, designed for Agentic Engineering, capable of providing reliable productivity in complex system engineering and long-range Agent tasks.",
+ displayName: 'GLM-5',
+ enabled: true,
+ id: 'glm-5',
+ maxOutput: 131_072,
+ pricing: {
+ units: [
+ { name: 'textInput', rate: 1, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'textInput_cacheRead', rate: 0.2, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'textOutput', rate: 3.2, strategy: 'fixed', unit: 'millionTokens' },
+ ],
+ },
+ releasedAt: '2026-02-12',
+ settings: {
+ extendParams: ['enableReasoning'],
+ searchImpl: 'params',
+ },
+ type: 'chat',
+ },
+];
diff --git a/packages/model-runtime/src/core/RouterRuntime/apiTypes.ts b/packages/model-runtime/src/core/RouterRuntime/apiTypes.ts
index 9a2f6b29228..ee20838939e 100644
--- a/packages/model-runtime/src/core/RouterRuntime/apiTypes.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/apiTypes.ts
@@ -15,6 +15,7 @@ export type ApiType =
| 'vertexai'
| 'volcengine'
| 'xai'
- | 'xiaomimimo';
+ | 'xiaomimimo'
+ | 'zhipu';
export type RuntimeClass = new (options?: any) => LobeRuntimeAI;
diff --git a/packages/model-runtime/src/core/RouterRuntime/baseRuntimeMap.ts b/packages/model-runtime/src/core/RouterRuntime/baseRuntimeMap.ts
index da0a20de5c6..9062769a7d0 100644
--- a/packages/model-runtime/src/core/RouterRuntime/baseRuntimeMap.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/baseRuntimeMap.ts
@@ -13,6 +13,7 @@ import { LobeVertexAI } from '../../providers/vertexai';
import { LobeVolcengineAI } from '../../providers/volcengine';
import { LobeXAI } from '../../providers/xai';
import { LobeXiaomiMiMoAI } from '../../providers/xiaomimimo';
+import { LobeZhipuAI } from '../../providers/zhipu';
import type { ApiType, RuntimeClass } from './apiTypes';
export const baseRuntimeMap = {
@@ -31,4 +32,5 @@ export const baseRuntimeMap = {
volcengine: LobeVolcengineAI,
xai: LobeXAI,
xiaomimimo: LobeXiaomiMiMoAI,
+ zhipu: LobeZhipuAI,
} satisfies Record<ApiType, RuntimeClass>;
diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts
index 32364580ea4..2e9abd361c9 100644
--- a/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts
+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts
@@ -949,7 +949,7 @@ describe('LobeOpenAICompatibleFactory', () => {
it('should use custom stream handler when provided', async () => {
// Create a custom stream handler that handles both ReadableStream and OpenAI Stream
const customStreamHandler = vi.fn(
- (stream: ReadableStream | Stream<OpenAI.ChatCompletionChunk>) => {
+ (stream: ReadableStream | Stream<OpenAI.ChatCompletionChunk>, _options?: any) => {
const readableStream =
stream instanceof ReadableStream ? stream : stream.toReadableStream();
return new ReadableStream({
@@ -1009,6 +1009,13 @@ describe('LobeOpenAICompatibleFactory', () => {
await instance.chat(payload);
expect(customStreamHandler).toHaveBeenCalled();
+
+ // Verify payload is passed to custom stream handler
+ const handlerOptions = customStreamHandler.mock.calls[0][1];
+ expect(handlerOptions.payload).toMatchObject({
+ model: 'test-model',
+ provider: ModelProvider.OpenAI,
+ });
});
it('should use custom transform handler for non-streaming response', async () => {
diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts
index b1eb49e0a46..1693198f95d 100644
--- a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts
+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts
@@ -104,7 +104,11 @@ export interface OpenAICompatibleFactoryOptions<T extends Record<string, any> =
) => OpenAI.ChatCompletionCreateParamsStreaming;
handleStream?: (
stream: Stream<OpenAI.ChatCompletionChunk> | ReadableStream,
- { callbacks, inputStartAt }: { callbacks?: ChatStreamCallbacks; inputStartAt?: number },
+ options: {
+ callbacks?: ChatStreamCallbacks;
+ inputStartAt?: number;
+ payload?: ChatPayloadForTransformStream;
+ },
) => ReadableStream;
handleStreamBizErrorType?: (error: {
message: string;
@@ -507,6 +511,7 @@ export const createOpenAICompatibleRuntime = <T extends Record<string, any> = an
? chatCompletion.handleStream(prod, {
callbacks: streamOptions.callbacks,
inputStartAt,
+ payload: streamOptions.payload,
})
: OpenAIStream(prod, {
...streamOptions,
@@ -537,6 +542,7 @@ export const createOpenAICompatibleRuntime = <T extends Record<string, any> = an
? chatCompletion.handleStream(stream, {
callbacks: streamOptions.callbacks,
inputStartAt,
+ payload: streamOptions.payload,
})
: OpenAIStream(stream, { ...streamOptions, enableStreaming: false, inputStartAt }),
{
diff --git a/packages/model-runtime/src/providers/zhipu/index.test.ts b/packages/model-runtime/src/providers/zhipu/index.test.ts
index 7ef068cbfc4..42a8510e5a1 100644
--- a/packages/model-runtime/src/providers/zhipu/index.test.ts
+++ b/packages/model-runtime/src/providers/zhipu/index.test.ts
@@ -648,6 +648,76 @@ describe('LobeZhipuAI - custom features', () => {
expect(result).toBeDefined();
});
+ it('should filter out incomplete placeholder tool_call chunks from proxies', async () => {
+ // Some proxies (e.g., aihubmix) send empty placeholder chunks without
+ // id/function.name when tool_stream is enabled. These must be filtered
+ // out to prevent ZodError in parseToolCalls.
+ const mockStream = new ReadableStream({
+ start(controller) {
+ // Placeholder chunks (no id, no name, empty arguments)
+ controller.enqueue({
+ choices: [
+ {
+ delta: {
+ tool_calls: [{ type: 'function', function: { arguments: '' }, index: 0 }],
+ },
+ finish_reason: null,
+ index: 0,
+ },
+ ],
+ created: 1234567890,
+ id: 'chatcmpl-123',
+ model: 'glm-5',
+ object: 'chat.completion.chunk',
+ });
+ // Real chunk with id and name
+ controller.enqueue({
+ choices: [
+ {
+ delta: {
+ tool_calls: [
+ {
+ id: 'tool-abc123',
+ type: 'function',
+ function: { name: 'calculator', arguments: '{"expression":"1+1"}' },
+ index: 0,
+ },
+ ],
+ },
+ finish_reason: null,
+ index: 0,
+ },
+ ],
+ created: 1234567890,
+ id: 'chatcmpl-123',
+ model: 'glm-5',
+ object: 'chat.completion.chunk',
+ });
+ controller.close();
+ },
+ });
+
+ (instance['client'].chat.completions.create as any).mockResolvedValue(mockStream);
+
+ // Should not throw ZodError from incomplete placeholder chunks
+ const result = await instance.chat({
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'glm-5',
+ temperature: 0.5,
+ });
+
+ const reader = result.body?.getReader();
+ if (reader) {
+ let done = false;
+ while (!done) {
+ const { value, done: isDone } = await reader.read();
+ done = isDone;
+ }
+ }
+
+ expect(result).toBeDefined();
+ });
+
it('should handle multiple chunks with tool_calls', async () => {
const mockStream = new ReadableStream({
start(controller) {
diff --git a/packages/model-runtime/src/providers/zhipu/index.ts b/packages/model-runtime/src/providers/zhipu/index.ts
index 6233e479174..81d4b23771f 100644
--- a/packages/model-runtime/src/providers/zhipu/index.ts
+++ b/packages/model-runtime/src/providers/zhipu/index.ts
@@ -83,7 +83,7 @@ export const params = {
tools: zhipuTools,
} as any;
},
- handleStream: (stream, { callbacks, inputStartAt }) => {
+ handleStream: (stream, { callbacks, inputStartAt, payload }) => {
const readableStream =
stream instanceof ReadableStream ? stream : convertIterableToStream(stream);
@@ -97,28 +97,33 @@ export const params = {
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
- const fixedToolCalls = choice.delta.tool_calls.map(
- (toolCall: any, globalIndex: number) => ({
+ // 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,
- }),
- );
+ }));
- // Create fixed chunk
- const fixedChunk = {
- ...chunk,
- choices: [
- {
- ...choice,
- delta: {
- ...choice.delta,
- tool_calls: fixedToolCalls,
- },
- },
- ],
- };
-
- controller.enqueue(fixedChunk);
+ 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);
}
@@ -132,9 +137,7 @@ export const params = {
return OpenAIStream(preprocessedStream, {
callbacks,
inputStartAt,
- payload: {
- provider: 'zhipu',
- },
+ payload,
});
},
},