按 DevDocs/design/01 §2.1/§2.2 与 design/04 §3.2 实现(规格含联网核过的官方依据:思考内容刻意不角色化、不写分步思考指令、思考控制只走参数级)。
- 人格 schema 新增 thinking:budgetTokens / effort / showToUser(*bool,省略=nil=保持现行为,显式 false=只落库) / mode(default|on|off→Enable 三态);非法 mode/负预算 → ErrInvalidThinking(不静默)
- 5 个死字段接上注入:personality→values→speechStyle→catchphrase→background 拼为独立段落(带【】小标题,空值不拼);**全空时输出与旧实现逐字节一致**(有专门测试)
- model.Request 新增 Thinking{ BudgetTokens, Enable } 与 ReasoningEffort;新增 model/reasoning.go:厂商识别(模型名→baseURL 兜底)、取值白名单(OpenAI 系 none…max / DeepSeek low|high|max)、非法值降级+WARN、**不支持一律 WARN 而非静默**;Anthropic 映射 thinking{budget_tokens} 并**保证 budget<max_tokens**(不足时抬高 max_tokens+WARN,正是历史上思考吃光预算→空回复那个坑)
- loop:新增 Options.ThinkingShowToUser(*bool) 仅门控 thinking_* 帧(正文/工具帧不受影响);场景级 mute 语义不变
- 补一处任务清单外但必需的接线:persona→model.Request 的 thinking 映射(否则 budget/effort/mode 会变成新的死字段);showToUser 刻意不产生 Thinking(纯呈现开关)
- 管理端:personaUpsertRequest 补 thinking DTO + 校验(400);PUT 省略时**保留已存块**(防旧客户端抹配置);写侧 yaml round-trip 已验证
- Docs/api-client v0.40→v0.41(可选请求字段)
- 测试:26 个新用例(loader 解析/拼接逐字节/映射、anthropic 非流式+流式预算约束、openai 透传/降级/丢弃、Qwen 诚实 WARN、loop 门控、DTO round-trip);3 次变异验证(省略与 false 混同 / 去掉预算约束 / 短路门控 各自 FAIL)
- 验收:go build/vet/test 全绿(含 57 包)+ linux/darwin 交叉编译 + 前端 build;唯一 FAIL 是既有 flake TestInterruptCancelsTurnAndClearsPending(3/20 新代码 vs 3/20 纯 HEAD,已归因非本次引入)
365 lines
14 KiB
Go
365 lines
14 KiB
Go
package model
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
|
)
|
|
|
|
// captureLog returns a logger writing text records into a buffer, plus the
|
|
// buffer, so a test can assert the WARN an adapter emits.
|
|
func captureLog() (*slog.Logger, *bytes.Buffer) {
|
|
buf := &bytes.Buffer{}
|
|
return slog.New(slog.NewTextHandler(buf, &slog.HandlerOptions{Level: slog.LevelDebug})), buf
|
|
}
|
|
|
|
// boolPtr is a convenience for the tri-state Thinking.Enable.
|
|
func boolPtr(v bool) *bool { return &v }
|
|
|
|
// TestAnthropicChat_ThinkingBudgetLeavesRoomForTheAnswer is the design/04 §3.2
|
|
// red line: a budget large enough to eat max_tokens must not be sent as-is —
|
|
// the adapter raises max_tokens so thinking and the visible answer both fit, and
|
|
// it logs the adjustment (never a silent rewrite).
|
|
func TestAnthropicChat_ThinkingBudgetLeavesRoomForTheAnswer(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan anthropicCaptured, 1)
|
|
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
|
log, buf := captureLog()
|
|
ad := NewAnthropicChat(
|
|
config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"},
|
|
WithAnthropicLogger(log))
|
|
|
|
const budget = 4096
|
|
// max_tokens equals the budget: without the guard the whole budget would be
|
|
// consumed by thinking and the answer would come back empty.
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "claude-test",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
MaxTokens: budget,
|
|
Thinking: &Thinking{BudgetTokens: budget},
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
body := (<-ch).body
|
|
think, ok := body["thinking"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("body.thinking = %v, want an enabled directive", body["thinking"])
|
|
}
|
|
if think["type"] != anthropicThinkingTypeEnabled {
|
|
t.Errorf("thinking.type = %v, want %q", think["type"], anthropicThinkingTypeEnabled)
|
|
}
|
|
gotBudget, _ := think["budget_tokens"].(float64)
|
|
maxTokens, _ := body["max_tokens"].(float64)
|
|
if int(gotBudget) != budget {
|
|
t.Errorf("thinking.budget_tokens = %v, want %d", gotBudget, budget)
|
|
}
|
|
if maxTokens <= gotBudget {
|
|
t.Fatalf("max_tokens = %v, want > budget_tokens %v (thinking would eat the answer)", maxTokens, gotBudget)
|
|
}
|
|
if maxTokens < gotBudget+anthropicThinkingBodyReserve {
|
|
t.Errorf("max_tokens = %v, want >= budget %v + body reserve %d", maxTokens, gotBudget, anthropicThinkingBodyReserve)
|
|
}
|
|
if !strings.Contains(buf.String(), "max_tokens raised") {
|
|
t.Errorf("log = %q, want a WARN explaining the max_tokens raise", buf.String())
|
|
}
|
|
}
|
|
|
|
// TestAnthropicChat_ThinkingBudgetBelowMaxTokensUnchanged pins the non-saturated
|
|
// case: when the caller's max_tokens already leaves room, it is sent untouched
|
|
// and the budget stays strictly below it.
|
|
func TestAnthropicChat_ThinkingBudgetBelowMaxTokensUnchanged(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan anthropicCaptured, 1)
|
|
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
|
log, buf := captureLog()
|
|
ad := NewAnthropicChat(
|
|
config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"},
|
|
WithAnthropicLogger(log))
|
|
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "claude-test",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
MaxTokens: anthropicDefaultMaxTokens,
|
|
Thinking: &Thinking{BudgetTokens: 4096},
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
body := (<-ch).body
|
|
think := body["thinking"].(map[string]any)
|
|
gotBudget, _ := think["budget_tokens"].(float64)
|
|
maxTokens, _ := body["max_tokens"].(float64)
|
|
if maxTokens != float64(anthropicDefaultMaxTokens) {
|
|
t.Errorf("max_tokens = %v, want the requested %d (unchanged)", maxTokens, anthropicDefaultMaxTokens)
|
|
}
|
|
if gotBudget >= maxTokens {
|
|
t.Errorf("budget_tokens %v >= max_tokens %v", gotBudget, maxTokens)
|
|
}
|
|
if strings.Contains(buf.String(), "max_tokens raised") {
|
|
t.Errorf("unexpected adjustment log: %q", buf.String())
|
|
}
|
|
}
|
|
|
|
// TestAnthropicChat_ThinkingDisabledOmitsField asserts the explicit off switch
|
|
// sends no thinking directive at all (Anthropic treats a missing field as off).
|
|
func TestAnthropicChat_ThinkingDisabledOmitsField(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan anthropicCaptured, 1)
|
|
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
|
ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"})
|
|
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "claude-test",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
Thinking: &Thinking{BudgetTokens: 4096, Enable: boolPtr(false)},
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
if body := (<-ch).body; body["thinking"] != nil {
|
|
t.Fatalf("body.thinking = %v, want the field omitted", body["thinking"])
|
|
}
|
|
}
|
|
|
|
// TestAnthropicChat_ThinkingBudgetRaisedToProtocolMinimum asserts a budget below
|
|
// the API minimum is raised (sending it as-is would 400) and reported.
|
|
func TestAnthropicChat_ThinkingBudgetRaisedToProtocolMinimum(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan anthropicCaptured, 1)
|
|
srv := newAnthropicServer(t, http.StatusOK, "application/json", anthropicTextResponse, ch)
|
|
log, buf := captureLog()
|
|
ad := NewAnthropicChat(
|
|
config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"},
|
|
WithAnthropicLogger(log))
|
|
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "claude-test",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
Thinking: &Thinking{BudgetTokens: 512},
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
think := (<-ch).body["thinking"].(map[string]any)
|
|
if got, _ := think["budget_tokens"].(float64); int(got) != anthropicMinThinkingBudget {
|
|
t.Errorf("thinking.budget_tokens = %v, want the minimum %d", got, anthropicMinThinkingBudget)
|
|
}
|
|
if !strings.Contains(buf.String(), "protocol minimum") {
|
|
t.Errorf("log = %q, want a WARN about the minimum budget", buf.String())
|
|
}
|
|
}
|
|
|
|
// TestOpenAIChat_ReasoningEffortPassthrough asserts a supported tier on a known
|
|
// provider is forwarded verbatim, with no warning.
|
|
func TestOpenAIChat_ReasoningEffortPassthrough(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan capturedRequest, 1)
|
|
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
|
log, buf := captureLog()
|
|
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "gpt-test"}, WithOpenAILogger(log))
|
|
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "gpt-4o",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
ReasoningEffort: "high",
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
if got := (<-ch).body["reasoning_effort"]; got != "high" {
|
|
t.Fatalf("body.reasoning_effort = %v, want high", got)
|
|
}
|
|
if buf.Len() != 0 {
|
|
t.Errorf("unexpected log for a supported value: %q", buf.String())
|
|
}
|
|
}
|
|
|
|
// TestOpenAIChat_ReasoningEffortDowngradedForDeepSeek asserts a tier the vendor
|
|
// does not accept (DeepSeek allows only low/high/max) is rounded down to the
|
|
// nearest allowed tier and reported, never forwarded as-is.
|
|
func TestOpenAIChat_ReasoningEffortDowngradedForDeepSeek(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan capturedRequest, 1)
|
|
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
|
log, buf := captureLog()
|
|
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "deepseek-chat"}, WithOpenAILogger(log))
|
|
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "deepseek-chat",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
ReasoningEffort: "medium",
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
if got := (<-ch).body["reasoning_effort"]; got != "low" {
|
|
t.Fatalf("body.reasoning_effort = %v, want the downgraded low", got)
|
|
}
|
|
rec := buf.String()
|
|
for _, want := range []string{"level=WARN", "reasoning_effort", "deepseek", "medium", "low"} {
|
|
if !strings.Contains(rec, want) {
|
|
t.Errorf("log %q missing %q", rec, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestOpenAIChat_ReasoningEffortDroppedForUnknownProvider asserts an
|
|
// unrecognized provider/model leaves the request untouched and reports the
|
|
// ignored field (design/04 §3.2: never silently ineffective).
|
|
func TestOpenAIChat_ReasoningEffortDroppedForUnknownProvider(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan capturedRequest, 1)
|
|
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
|
log, buf := captureLog()
|
|
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "mystery-1"}, WithOpenAILogger(log))
|
|
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "mystery-1",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
ReasoningEffort: "high",
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
body := (<-ch).body
|
|
if _, present := body["reasoning_effort"]; present {
|
|
t.Fatalf("body.reasoning_effort = %v, want the field dropped", body["reasoning_effort"])
|
|
}
|
|
rec := buf.String()
|
|
for _, want := range []string{"level=WARN", "reasoning_effort", "unknown", "high"} {
|
|
if !strings.Contains(rec, want) {
|
|
t.Errorf("log %q missing %q", rec, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestOpenAIChat_QwenEnableThinkingReportedNotForwarded documents the deliberate
|
|
// choice for the Qwen3 hard switch: the OpenAI-compatible protocol has no
|
|
// portable wire shape for enable_thinking, so this version reports it and leaves
|
|
// the request byte-identical instead of guessing a shape that could 400.
|
|
func TestOpenAIChat_QwenEnableThinkingReportedNotForwarded(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan capturedRequest, 1)
|
|
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
|
log, buf := captureLog()
|
|
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "qwen3-max"}, WithOpenAILogger(log))
|
|
|
|
plain := make(chan capturedRequest, 1)
|
|
plainSrv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, plain)
|
|
plainAd := NewOpenAIChat(config.LLMConfig{BaseURL: plainSrv.URL, Model: "qwen3-max"})
|
|
if _, err := plainAd.Chat(context.Background(), Request{
|
|
Model: "qwen3-max",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "qwen3-max",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
Thinking: &Thinking{Enable: boolPtr(false)},
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
body := (<-ch).body
|
|
for _, key := range []string{"enable_thinking", "chat_template_kwargs", "extra_body", "thinking"} {
|
|
if _, present := body[key]; present {
|
|
t.Errorf("body.%s = %v, want the request unchanged", key, body[key])
|
|
}
|
|
}
|
|
if len(body) != len((<-plain).body) {
|
|
t.Errorf("request shape changed with a thinking directive: %v", body)
|
|
}
|
|
rec := buf.String()
|
|
for _, want := range []string{"level=WARN", "enable_thinking", "qwen"} {
|
|
if !strings.Contains(rec, want) {
|
|
t.Errorf("log %q missing %q", rec, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestOpenAIChat_ThinkingBudgetReportedIgnored asserts a token budget (an
|
|
// Anthropic-style directive) is reported as unmappable on an OpenAI-compatible
|
|
// endpoint rather than silently dropped.
|
|
func TestOpenAIChat_ThinkingBudgetReportedIgnored(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan capturedRequest, 1)
|
|
srv := newCompletionServer(t, http.StatusOK, "application/json", okCompletion, ch)
|
|
log, buf := captureLog()
|
|
ad := NewOpenAIChat(config.LLMConfig{BaseURL: srv.URL, Model: "gpt-4o"}, WithOpenAILogger(log))
|
|
|
|
if _, err := ad.Chat(context.Background(), Request{
|
|
Model: "gpt-4o",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
Thinking: &Thinking{BudgetTokens: 8192},
|
|
}, Options{}); err != nil {
|
|
t.Fatalf("Chat error: %v", err)
|
|
}
|
|
if _, present := (<-ch).body["thinking"]; present {
|
|
t.Fatal("body.thinking was sent to an OpenAI-compatible endpoint")
|
|
}
|
|
rec := buf.String()
|
|
for _, want := range []string{"level=WARN", "thinking.budget_tokens", "gpt"} {
|
|
if !strings.Contains(rec, want) {
|
|
t.Errorf("log %q missing %q", rec, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestAnthropicChatStream_ThinkingBudgetLeavesRoomForTheAnswer runs the same
|
|
// red-line check on the streaming path, which builds its own request body.
|
|
func TestAnthropicChatStream_ThinkingBudgetLeavesRoomForTheAnswer(t *testing.T) {
|
|
t.Parallel()
|
|
ch := make(chan anthropicCaptured, 1)
|
|
const sse = "event: message_start\ndata: {\"type\":\"message_start\"}\n\n" +
|
|
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n" +
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n" +
|
|
"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
|
|
srv := newAnthropicServer(t, http.StatusOK, "text/event-stream", sse, ch)
|
|
ad := NewAnthropicChat(config.LLMConfig{AnthropicBaseURL: srv.URL, AnthropicModel: "claude-test"})
|
|
|
|
if _, err := ad.ChatStream(context.Background(), Request{
|
|
Model: "claude-test",
|
|
Messages: []Message{TextMessage(RoleUser, "hi")},
|
|
MaxTokens: 2048,
|
|
Thinking: &Thinking{BudgetTokens: 2048},
|
|
}, func(StreamEvent) {}); err != nil {
|
|
t.Fatalf("ChatStream error: %v", err)
|
|
}
|
|
body := (<-ch).body
|
|
think, ok := body["thinking"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("stream body.thinking = %v, want an enabled directive", body["thinking"])
|
|
}
|
|
gotBudget, _ := think["budget_tokens"].(float64)
|
|
maxTokens, _ := body["max_tokens"].(float64)
|
|
if gotBudget >= maxTokens {
|
|
t.Fatalf("stream max_tokens = %v, want > budget_tokens %v", maxTokens, gotBudget)
|
|
}
|
|
}
|
|
|
|
// TestReasoningVendorFor_Classification locks the vendor detection the whitelist
|
|
// depends on, including the fallback to the base URL.
|
|
func TestReasoningVendorFor_Classification(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
model string
|
|
baseURL string
|
|
want reasoningVendor
|
|
}{
|
|
{model: "deepseek-reasoner", want: vendorDeepSeek},
|
|
{model: "Qwen3-Max", want: vendorQwen},
|
|
{model: "gpt-4o", want: vendorOpenAI},
|
|
{model: "o3-mini", want: vendorOpenAI},
|
|
{model: "chatgpt-4o-latest", want: vendorOpenAI},
|
|
{model: "claude-sonnet-4", baseURL: "https://api.openai.com/v1", want: vendorOpenAI},
|
|
{model: "qwen-plus", baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1", want: vendorQwen},
|
|
{model: "some-model", baseURL: "https://api.deepseek.com/v1", want: vendorDeepSeek},
|
|
{model: "some-model", baseURL: "http://localhost:8000/v1", want: vendorUnknown},
|
|
}
|
|
for _, tt := range cases {
|
|
if got := reasoningVendorFor(tt.model, tt.baseURL); got != tt.want {
|
|
t.Errorf("reasoningVendorFor(%q, %q) = %q, want %q", tt.model, tt.baseURL, got, tt.want)
|
|
}
|
|
}
|
|
}
|