MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / createStreamWithThinking

Method createStreamWithThinking

src/api/providers/zai.ts:75–132  ·  view source on GitHub ↗

* Creates a stream with explicit thinking control for GLM thinking-capable models.

(
		systemPrompt: string,
		messages: Anthropic.Messages.MessageParam[],
		metadata?: ApiHandlerCreateMessageMetadata,
	)

Source from the content-addressed store, hash-verified

73 * Creates a stream with explicit thinking control for GLM thinking-capable models.
74 */
75 private createStreamWithThinking(
76 systemPrompt: string,
77 messages: Anthropic.Messages.MessageParam[],
78 metadata?: ApiHandlerCreateMessageMetadata,
79 ) {
80 const { id: model, info } = this.getModel()
81
82 // Fall back to the model default when the resolved effort isn't supported by the model.
83 const supported = info.supportsReasoningEffort
84 const raw =
85 this.options.enableReasoningEffort === false
86 ? undefined
87 : (this.options.reasoningEffort ?? info.reasoningEffort)
88 const effort =
89 raw && raw !== "disable" && Array.isArray(supported) && !supported.includes(raw)
90 ? info.reasoningEffort
91 : raw
92 const reasoningEffort = effort && effort !== "disable" ? effort : undefined
93 const useReasoning = reasoningEffort !== undefined
94
95 const max_tokens =
96 this.options.modelMaxTokens ||
97 (getModelMaxOutputTokens({
98 modelId: model,
99 model: info,
100 settings: this.options,
101 format: "openai",
102 }) ??
103 undefined)
104
105 const temperature = this.options.modelTemperature ?? this.defaultTemperature
106
107 // Use Z.ai format to preserve reasoning_content and merge post-tool text into tool messages
108 const convertedMessages = convertToZAiFormat(messages, { mergeToolResultText: true })
109
110 const params: ZAiChatCompletionParams = {
111 model,
112 max_tokens,
113 temperature,
114 messages: [{ role: "system", content: systemPrompt }, ...convertedMessages],
115 stream: true,
116 stream_options: { include_usage: true },
117 // Thinking is ON by default for these models, so explicitly disable it when needed.
118 thinking: useReasoning ? { type: "enabled" } : { type: "disabled" },
119 reasoning_effort: reasoningEffort,
120 tools: this.convertToolsForOpenAI(metadata?.tools),
121 tool_choice: metadata?.tool_choice,
122 parallel_tool_calls: metadata?.parallelToolCalls ?? true,
123 }
124
125 try {
126 return this.client.chat.completions.create(
127 params as OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming,
128 )
129 } catch (error) {
130 throw handleOpenAIError(error, this.providerName)
131 }
132 }

Callers 1

createStreamMethod · 0.95

Calls 5

getModelMaxOutputTokensFunction · 0.90
convertToZAiFormatFunction · 0.90
handleOpenAIErrorFunction · 0.90
getModelMethod · 0.65
createMethod · 0.45

Tested by

no test coverage detected