mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
产品名早已是 ModelRouter,但仓库标识符仍沿用旧名。这次统一,读写两侧都做兼容, 已部署的机器不会因为顺序问题立刻失效。 - go module `llmsproxy` → `modelrouter`;全仓 import 与 `cmd/modelrouter` 同步。 - `Makefile BINARY=modelrouter`;发行包名、systemd unit、/usr/share 资源、 `/usr/local/bin` 目标全部改为 modelrouter。 - 默认配置目录 `/etc/llmsproxy` → `/etc/modelrouter`。**读取侧回退**:插件与 `storeDir` 仍会检查旧路径,所以"先换二进制、后改 unit"不会立即失效。 - 环境变量 `LLMSPROXY_*`(PLUGIN_DIR / XDISPLAY_BASE / ARCH / LIVE_ZEN / BUILD_DIR)改为 `MODELROUTER_*`;`LLMS_PROXY_MASTER_KEY` → `MODELROUTER_MASTER_KEY`。旧名在读取侧继续接受。 - 桌面端内嵌核心 `bin/llmsproxy` → `bin/modelrouter`,并保留旧文件名回退, 避免升级到一半的 profile 被判成"core binary missing"。 - 新增 `packaging/migrate-to-modelrouter.sh`:**默认 dry-run**,复制而非移动、 旧目录永不删除、支持 `--rollback`;随 release tar 分发并由 core-dist 校验。 - `deploy.sh` 增加改名保护:发现旧 `/etc/llmsproxy` 而新配置不存在时**中止**, 否则会部署出一个"能启动但没有 key、没有源"的空实例。 - 临时目录前缀 `/tmp/llmsproxy-*` → `/tmp/modelrouter-*`;清理逻辑同时回收两代。 兼容别名(`__llmsproxy_*` 全局、`llmsproxy` SDK 别名、旧路径/旧 env)刻意保留, 它们是升级路径而非遗留;docs/plugins.md 已标注为废弃。 本提交未包含:`adapters/opencodezen.lua`、`adapter_opencodezen_test.go`、 `adapter_opencodezen_live_test.go`(OpenCode UA/免费池的既有改动,另批提交)。
134 lines
4.3 KiB
Go
134 lines
4.3 KiB
Go
package gateway
|
||
|
||
import (
|
||
"encoding/json"
|
||
"testing"
|
||
|
||
"modelrouter/internal/types"
|
||
)
|
||
|
||
// TestThinkingFromClient 把「客户端措辞 → 中立语义」这条翻译链钉死。
|
||
//
|
||
// v1 的问题:disable_thinking 是内核私有字段,被序列化进交给适配器的 body,
|
||
// 于是 10 个适配器各自写 req.disable_thinking = nil 来清理它。v2 把它换成
|
||
// 双方都认可的 thinking 字段,这条测试确保翻译只发生一次、且在适配器之前。
|
||
func TestThinkingFromClient(t *testing.T) {
|
||
cases := []struct {
|
||
name string
|
||
req chatRequest
|
||
want *types.ThinkingConfig
|
||
}{
|
||
{
|
||
name: "无任何字段 → 不带 thinking(上游用自己默认)",
|
||
req: chatRequest{},
|
||
want: nil,
|
||
},
|
||
{
|
||
name: "disable_thinking=true → enabled=false",
|
||
req: chatRequest{DisableThinking: true},
|
||
want: &types.ThinkingConfig{Enabled: boolPtr(false)},
|
||
},
|
||
{
|
||
name: "中立 thinking 显式给出 → 原样透传",
|
||
req: chatRequest{Thinking: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 4096}},
|
||
want: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 4096},
|
||
},
|
||
{
|
||
name: "两者冲突时,中立的 thinking 优先(它带预算,布尔表达不了)",
|
||
req: chatRequest{
|
||
DisableThinking: true,
|
||
Thinking: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 1024},
|
||
},
|
||
want: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 1024},
|
||
},
|
||
}
|
||
for _, c := range cases {
|
||
t.Run(c.name, func(t *testing.T) {
|
||
got := c.req.thinkingFromClient()
|
||
if c.want == nil {
|
||
if got != nil {
|
||
t.Fatalf("want nil, got %+v", got)
|
||
}
|
||
return
|
||
}
|
||
if got == nil {
|
||
t.Fatal("want non-nil thinking, got nil")
|
||
}
|
||
if (got.Enabled == nil) != (c.want.Enabled == nil) {
|
||
t.Fatalf("Enabled presence mismatch: got %v want %v", got.Enabled, c.want.Enabled)
|
||
}
|
||
if got.Enabled != nil && *got.Enabled != *c.want.Enabled {
|
||
t.Errorf("Enabled = %v, want %v", *got.Enabled, *c.want.Enabled)
|
||
}
|
||
if got.BudgetTokens != c.want.BudgetTokens {
|
||
t.Errorf("BudgetTokens = %d, want %d", got.BudgetTokens, c.want.BudgetTokens)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// TestDisableThinkingNeverReachesAdapterBody 是那条泄漏的回归测试:内核私有
|
||
// 字段不得出现在交给适配器的 body 里。
|
||
func TestDisableThinkingNeverReachesAdapterBody(t *testing.T) {
|
||
var r chatRequest
|
||
if err := json.Unmarshal([]byte(`{"model":"m","messages":[],"disable_thinking":true}`), &r); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
inner := &types.ChatRequest{
|
||
Model: "m",
|
||
Thinking: r.thinkingFromClient(),
|
||
Tools: r.rawTools(),
|
||
ToolChoice: r.rawToolChoice(),
|
||
ExtraBody: r.ExtraBody,
|
||
ClientSession: "",
|
||
}
|
||
body, err := json.Marshal(inner)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var m map[string]json.RawMessage
|
||
if err := json.Unmarshal(body, &m); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, leaked := m["disable_thinking"]; leaked {
|
||
t.Errorf("内核私有字段 disable_thinking 泄漏进适配器 body(v1 回归): %s", body)
|
||
}
|
||
if _, ok := m["thinking"]; !ok {
|
||
t.Errorf("中立 thinking 字段缺失: %s", body)
|
||
}
|
||
}
|
||
|
||
// TestToolsPassthroughRaw 确认 tools/tool_choice 以原始形状交给适配器:
|
||
// 内核不解释工具 schema,那是适配器的职责。
|
||
func TestToolsPassthroughRaw(t *testing.T) {
|
||
var r chatRequest
|
||
body := `{"model":"m","messages":[],"tools":[{"type":"function","function":{"name":"f","parameters":{"type":"object"}}}],"tool_choice":"auto"}`
|
||
if err := json.Unmarshal([]byte(body), &r); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
tools := r.rawTools()
|
||
if len(tools) == 0 {
|
||
t.Fatal("tools 未透传")
|
||
}
|
||
var decoded []map[string]interface{}
|
||
if err := json.Unmarshal(tools, &decoded); err != nil {
|
||
t.Fatalf("tools 不是合法 JSON: %v (%s)", err, tools)
|
||
}
|
||
if len(decoded) != 1 || decoded[0]["type"] != "function" {
|
||
t.Errorf("tools 形状被内核改写: %s", tools)
|
||
}
|
||
if tc := string(r.rawToolChoice()); tc != `"auto"` {
|
||
t.Errorf("tool_choice = %s, want \"auto\"", tc)
|
||
}
|
||
// 空值不应变成 "null" 出现在 body 里
|
||
var empty chatRequest
|
||
if got := empty.rawTools(); got != nil {
|
||
t.Errorf("无 tools 时应返回 nil, got %s", got)
|
||
}
|
||
if got := empty.rawToolChoice(); got != nil {
|
||
t.Errorf("无 tool_choice 时应返回 nil, got %s", got)
|
||
}
|
||
}
|
||
|
||
func boolPtr(v bool) *bool { return &v }
|