fix: preserve reasoning_content for thinking models in multi-turn conversations

GLM-Z1 and other thinking models require reasoning_content to be preserved
across tool call iterations. Without this, the API returns:
  'thinking is enabled but reasoning_content is missing in assistant tool call message'

Changes:
- Add ReasoningContent field to LLMResponse and Message types
- Parse reasoning_content from API response
- Preserve ReasoningContent when building assistant messages with tool calls
This commit is contained in:
mxrain 2026-02-14 14:14:31 +08:00
parent 5872e0f55e
commit 39998a6618
3 changed files with 20 additions and 15 deletions

View file

@ -482,6 +482,7 @@ func (al *AgentLoop) runLLMIteration(ctx context.Context, messages []providers.M
assistantMsg := providers.Message{
Role: "assistant",
Content: response.Content,
ReasoningContent: response.ReasoningContent, // Preserve for thinking models (e.g., GLM-Z1)
}
for _, tc := range response.ToolCalls {
argumentsJSON, _ := json.Marshal(tc.Arguments)

View file

@ -127,6 +127,7 @@ func (p *HTTPProvider) parseResponse(body []byte) (*LLMResponse, error) {
Choices []struct {
Message struct {
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content"` // For thinking models (e.g., GLM-Z1)
ToolCalls []struct {
ID string `json:"id"`
Type string `json:"type"`
@ -186,6 +187,7 @@ func (p *HTTPProvider) parseResponse(body []byte) (*LLMResponse, error) {
return &LLMResponse{
Content: choice.Message.Content,
ReasoningContent: choice.Message.ReasoningContent,
ToolCalls: toolCalls,
FinishReason: choice.FinishReason,
Usage: apiResponse.Usage,

View file

@ -17,6 +17,7 @@ type FunctionCall struct {
type LLMResponse struct {
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content,omitempty"` // For thinking models (e.g., GLM-Z1)
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
FinishReason string `json:"finish_reason"`
Usage *UsageInfo `json:"usage,omitempty"`
@ -31,6 +32,7 @@ type UsageInfo struct {
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content,omitempty"` // For thinking models (e.g., GLM-Z1)
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}