mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
DeepSeek upstreams (zen5-pro→deepseek-v4-pro, zen5-flash→deepseek-4-flash)
inline chain-of-thought into `content` as `…reasoning…</think><answer>` — the
opening <think> is consumed by the chat template, so only the closing </think>
survives. GLM upstreams (zen5, zen5-coder) instead use the separate
reasoning_content field. The relay forwarded DeepSeek content verbatim, so its
</think> template token leaked into the visible answer and broke the agent's
character in agent and the console.
One normalizer, one place: model.{InlinesReasoning,StripLeadingReasoning,
ReasoningStripper} is the single source of truth (package model, so model/openai.go
can use it without a cycle). Every relay surface strips only when the upstream
inlines reasoning; every other stream is byte-for-byte unchanged.
- model/reasoning.go — core: gate + whole-string strip + streaming stripper
(64 KiB cap so a missing </think> can never buffer unboundedly).
- controllers/reasoning.go — OpenAI-SSE wrappers (applyReasoningStrip /
stripReasoningBody), fail-safe passthrough on any parse error.
- controllers/anthropic_translate.go — the agent path (Anthropic in →
OpenAI upstream → Anthropic out): strip in both the streaming translator and
openAIResponseToAnthropic. Renamed the shadowing `model` param → `modelID` to
remove the package-shadow footgun (values, not places).
- controllers/openai_api.go, billing_reserve.go, model/openai.go (QueryText) —
wire the stripper on every remaining forward path. Billing always tokenizes
the ORIGINAL (reasoning still counted); only the forwarded text is cleaned.
Tests: streaming strip across a chunk-split </think>, non-stream strip, GLM
reasoning_content→thinking preserved, non-DeepSeek content untouched (gate),
billing unchanged. TDD green.
107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/hanzoai/ai/model"
|
|
)
|
|
|
|
// Relay-side wrappers over the shared reasoning normalizer (package model). They
|
|
// rewrite the wire JSON — an OpenAI SSE chunk (streaming) or a full completion
|
|
// body (non-streaming) — stripping a reasoning-inlining upstream's leading
|
|
// <think></think> block from content so it never reaches the client. Both are
|
|
// fail-safe: any parse error returns the input unchanged, so a malformed chunk
|
|
// can never corrupt the stream or drop a response.
|
|
|
|
// applyReasoningStrip rewrites the content deltas of one OpenAI SSE data chunk
|
|
// (the JSON after "data: ") through the stripper. Billing is unaffected — the
|
|
// caller counts the original content before this runs.
|
|
func applyReasoningStrip(raw string, s *model.ReasoningStripper) string {
|
|
var chunk map[string]any
|
|
if json.Unmarshal([]byte(raw), &chunk) != nil {
|
|
return raw
|
|
}
|
|
choices, ok := chunk["choices"].([]any)
|
|
if !ok {
|
|
return raw
|
|
}
|
|
changed := false
|
|
for _, c := range choices {
|
|
choice, ok := c.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
delta, ok := choice["delta"].(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
content, ok := delta["content"].(string)
|
|
if !ok || content == "" {
|
|
continue
|
|
}
|
|
delta["content"] = s.Feed(content)
|
|
changed = true
|
|
}
|
|
if !changed {
|
|
return raw
|
|
}
|
|
out, err := json.Marshal(chunk)
|
|
if err != nil {
|
|
return raw
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// stripReasoningBody rewrites message.content in a non-streaming OpenAI chat
|
|
// completion response, removing each choice's leading reasoning block.
|
|
func stripReasoningBody(body []byte) []byte {
|
|
var resp map[string]any
|
|
if json.Unmarshal(body, &resp) != nil {
|
|
return body
|
|
}
|
|
choices, ok := resp["choices"].([]any)
|
|
if !ok {
|
|
return body
|
|
}
|
|
changed := false
|
|
for _, c := range choices {
|
|
choice, ok := c.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
msg, ok := choice["message"].(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
content, ok := msg["content"].(string)
|
|
if !ok || content == "" {
|
|
continue
|
|
}
|
|
if clean := model.StripLeadingReasoning(content); clean != content {
|
|
msg["content"] = clean
|
|
changed = true
|
|
}
|
|
}
|
|
if !changed {
|
|
return body
|
|
}
|
|
if out, err := json.Marshal(resp); err == nil {
|
|
return out
|
|
}
|
|
return body
|
|
}
|