Files
AskaEth 3dc1232bc2
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
docs(comments): 注释中文化 · internal/runtime/model + internal/services/backthink(54 文件,纯替换)
- 只翻注释/文档字符串:`go/scanner` 剥注释后按 token 序列+literal 逐 token 比对,**54/54 文件 SAME**;
  改动文件总行数与 HEAD 漂移 = 0(严格 1:1,无合并/拆行)
- 裁决编号与 design 节号逐字保留(03.4.1/03.4.2/03.4.3/14.4.1/14.4.5/07.4.2-4、design/04 §3.1/§3.2 等);
  `chat_template_kwargs` / `enable_thinking` / `reasoning_effort` / `thinking:{type}` 字段名与形状说明未动
- 存疑译法(已登记):`persona` 在叙事语境用「角色」、标识符语境保留 `persona`(仓库既有模块本身混用);
  `rung`→「级」;`provider` 存在「提供商/提供者」两种落笔(可按需一键统一)
- 复核:build/vet 绿 + 两包测试绿;gofmt 干净;残留英文注释 0 条
- 既有存量(未修,只报告):`model/anthropic_test.go` 612 行 >500;`applyAnthropicEvent` 115 行 >80
2026-10-04 16:27:54 +08:00

120 lines
4.8 KiB
Go

package model
import (
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config/runtimeconfig"
)
// TestRouter_RouteRequestForPersona 钉住裁决 03.4.1:按角色路由里的
// 「角色」是本回合作答所依据的 persona,而未绑定的 persona 保持
// 消息角色行为。
func TestRouter_RouteRequestForPersona(t *testing.T) {
t.Parallel()
r := NewRouter("default",
WithRoleModel(RoleUser, "user-model"),
WithPersonaModel("cyrene", "persona-model"),
WithPersonaProvider("cyrene", ProviderAnthropic),
WithDefaultProvider(ProviderOpenAI),
)
req := Request{Messages: []Message{TextMessage(RoleUser, "hi")}}
if got, _ := r.RouteRequestForPersona("cyrene", req); got != "persona-model" {
t.Errorf("RouteRequestForPersona(cyrene) = %q, want persona-model", got)
}
if got, _ := r.RouteRequestForPersona("other", req); got != "user-model" {
t.Errorf("RouteRequestForPersona(other) = %q, want user-model (role fallback)", got)
}
if got, _ := r.RouteRequestForPersona("", req); got != "user-model" {
t.Errorf("RouteRequestForPersona(\"\") = %q, want user-model", got)
}
if got := r.RouteForPersona("cyrene"); got != "persona-model" {
t.Errorf("RouteForPersona(cyrene) = %q, want persona-model", got)
}
if got := r.RouteProviderForPersona("cyrene"); got != ProviderAnthropic {
t.Errorf("RouteProviderForPersona(cyrene) = %q, want anthropic", got)
}
if got := r.RouteProviderForPersona("other"); got != ProviderOpenAI {
t.Errorf("RouteProviderForPersona(other) = %q, want the default provider openai", got)
}
}
// TestRouter_PersonaBindingIgnoresBlankAndImageDoesNotSwitch 钉住两个边界:空
// persona/模型无法创建绑定,而按 persona 路由的图像请求
// 不会被静默改道到视觉模型(那个决策属于
// vision.Router,裁决 14.4.1/14.4.5)。
func TestRouter_PersonaBindingIgnoresBlankAndImageDoesNotSwitch(t *testing.T) {
t.Parallel()
r := NewRouter("default",
WithPersonaModel(" ", "x"),
WithPersonaModel("cyrene", ""),
WithVisionModel("vision-model"),
)
if got := r.RouteForPersona("cyrene"); got != "default" {
t.Errorf("RouteForPersona(cyrene) = %q, want default (no binding stored)", got)
}
img := Request{Messages: []Message{ImageMessage(RoleUser, "", "data:image/png;base64,abc")}}
got, needsVision := r.RouteRequestForPersona("cyrene", img)
if !needsVision {
t.Error("needsVision = false, want true for an image-bearing request")
}
if got == "vision-model" {
t.Errorf("RouteRequestForPersona = %q, want the persona/role model, not the vision model", got)
}
}
// TestRuntimeLLM_PersonaRoutesFromModelsJSON 钉住载体:写在
// models.json 组上的按 persona 绑定经热
// 快照到达路由器,且一次重载会拾取改过的绑定(裁决 03.4.1 载体 = models.json)。
func TestRuntimeLLM_PersonaRoutesFromModelsJSON(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{{
ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://x", Model: "group-model",
PersonaModels: map[string]string{"cyrene": "persona-model"},
PersonaProviders: map[string]string{"cyrene": ProviderAnthropic},
}})
m := NewRuntimeLLM(path, rtEnv(), nil)
req := Request{Messages: []Message{TextMessage(RoleUser, "hi")}}
got, _ := m.RouteRequestForPersona("cyrene", req)
if got != "persona-model" {
t.Fatalf("RouteRequestForPersona(cyrene) = %q, want persona-model", got)
}
if got, _ := m.RouteRequestForPersona("other", req); got != "group-model" {
t.Fatalf("RouteRequestForPersona(other) = %q, want group-model", got)
}
if err := runtimeconfig.SaveModels(path, []runtimeconfig.Group{{
ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://x", Model: "group-model",
}}); err != nil {
t.Fatalf("SaveModels: %v", err)
}
if err := m.Reload(); err != nil {
t.Fatalf("Reload: %v", err)
}
if got, _ := m.RouteRequestForPersona("cyrene", req); got != "group-model" {
t.Fatalf("after reload RouteRequestForPersona(cyrene) = %q, want group-model (binding removed)", got)
}
}
// TestRuntimeLLM_NoVisionPreemptionOnImage 钉住运行时快照不再
// 携带 env 视觉模型:已配置的 LLM_VISION_MODEL 不得把
// 携带图片的请求切到另一个模型,因为图像路由归
// vision.Router 所有(裁决 14.4.1/14.4.5)。
func TestRuntimeLLM_NoVisionPreemptionOnImage(t *testing.T) {
path := writeRTFile(t, []runtimeconfig.Group{{
ID: "openai", Provider: ProviderOpenAI, BaseURL: "https://x", Model: "group-model",
}})
env := rtEnv()
env.VisionModel = "vision-model"
m := NewRuntimeLLM(path, env, nil)
req := Request{Messages: []Message{ImageMessage(RoleUser, "", "data:image/png;base64,abc")}}
got, needsVision := m.RouteRequest(req)
if !needsVision {
t.Error("needsVision = false, want true for an image-bearing request")
}
if got != "group-model" {
t.Errorf("RouteRequest = %q, want group-model (no vision pre-emption)", got)
}
}