mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
G-Star 合规要求下,把"模拟官方客户端、复刻私有协议、自动领取平台额度"这一类 能力整体移出仓库。这些实现依赖各平台未公开的端点、签名算法与官方 client id, 属于绕过客户端校验获取额度,风险高于普通协议适配。 移除(93 个文件): - 适配器:opencodezen(伪装 UA 取免费池)、trae、buddy、kimicode - 插件:channels.lua(12 个平台登录/领额度/签到)、quota.lua(厂商额度探针) - 随之删除的 78 个渠道/额度专用测试与 3 份渠道文档 同时修掉的既有问题: - **硬编码第三方 OAuth client_secret**(Google Cloud Code 的 GOCSPX-…,两处) 随 channels.lua 一起消失。 - `deploy.sh` 的 `sync_plugins` 整段删除:它仍按"内置插件是发行物"的旧前提 把插件复制/覆盖进 plugin_dir,与"插件属运维资产、网关不安装不覆盖"的契约 冲突。现在部署脚本只同步适配器与二进制/配置。 - 适配器清单去掉已移除项;e2e 配置改用显式 `auto:` 链(`models.priority` 早已 移除,该测试此前已失效)。 保留(公开协议):openai / anthropic / gemini / deepseek / github / groq / mistral / ollama / qwen38 / sensenova = 10 个适配器;插件示例 billing / usage。 验证:go build -tags luajit ./... OK;internal/lua 21.4s、provider、core 51s、 gateway 122.8s、e2e 9.0s 全绿。
124 lines
4.8 KiB
Go
124 lines
4.8 KiB
Go
package lua
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// 前导重构的**等价性判据**:把重复实现抽进前导后,输出必须逐字节不变。
|
||
//
|
||
// 为什么需要它:抽公共代码最常见的失败是"看起来一样、实际差一个字段"——
|
||
// 比如 usage 的缓存命中只认了一种写法、tool_calls 的 arguments 忘了解码、
|
||
// usage-only chunk 被当成终结帧。这类差异不会报错,只会让下游少一个字段。
|
||
//
|
||
// 期望值是重构前的适配器(openai/groq/mistral/github 的
|
||
// transform_response 本就逐字节相同)对固定输入的真实输出,先抓 golden 再比。
|
||
func TestPreludeRefactorIsByteIdentical(t *testing.T) {
|
||
vm, _, _ := runtimePlugins(t)
|
||
for _, name := range []string{"openai", "groq", "mistral", "github"} {
|
||
src, err := bundledAdapters.ReadFile("adapters/" + name + ".lua")
|
||
if err != nil {
|
||
t.Fatalf("读内嵌适配器 %s: %v", name, err)
|
||
}
|
||
if err := vm.LoadAdapterSource(name, string(src)); err != nil {
|
||
t.Fatalf("%s: %v", name, err)
|
||
}
|
||
}
|
||
|
||
// 非流式:工具调用 + 两种缓存命中写法同时出现
|
||
respIn := `{"choices":[{"message":{"content":"hi","tool_calls":[{"id":"c1","type":"function",` +
|
||
`"function":{"name":"f","arguments":"{\"a\":1}"}}]},"finish_reason":"tool_calls"}],` +
|
||
`"usage":{"prompt_tokens":10,"completion_tokens":5,"total_tokens":15,` +
|
||
`"prompt_cache_hit_tokens":4,"prompt_cache_miss_tokens":6}}`
|
||
for _, n := range []string{"openai", "groq", "mistral", "github"} {
|
||
out, err := vm.Transform(n, "transform_response", respIn)
|
||
if err != nil {
|
||
t.Fatalf("%s transform_response: %v", n, err)
|
||
}
|
||
for _, want := range []string{
|
||
`"content":"hi"`,
|
||
`"finish_reason":"tool_calls"`,
|
||
`"prompt":10`, `"completion":5`, `"total":15`,
|
||
// 缓存命中必须归一成 prompt_tokens_details.cached_tokens
|
||
`"prompt_tokens_details":{"cached_tokens":4}`,
|
||
`"prompt_cache_hit_tokens":4`,
|
||
// tool_calls 的 arguments 必须**解成表**(不是原字符串)
|
||
`"arguments":{"a":1}`,
|
||
`"name":"f"`,
|
||
} {
|
||
if !strings.Contains(out, want) {
|
||
t.Errorf("%s: 输出缺 %s\n实际: %s", n, want, out)
|
||
}
|
||
}
|
||
// arguments 绝不能被留下原样的 JSON 字符串
|
||
if strings.Contains(out, `"arguments":"{\"a\":1}"`) {
|
||
t.Errorf("%s: arguments 未被解码成表:%s", n, out)
|
||
}
|
||
}
|
||
|
||
// 流式:内容 + reasoning + tool_calls 同帧
|
||
streamIn := `{"choices":[{"delta":{"content":"x","reasoning_content":"r",` +
|
||
`"tool_calls":[{"index":0,"id":"c1","function":{"name":"f","arguments":"{}"}}]},` +
|
||
`"finish_reason":null}]}`
|
||
for _, n := range []string{"openai", "groq", "mistral", "github"} {
|
||
out, err := vm.Transform(n, "transform_stream_chunk", streamIn)
|
||
if err != nil {
|
||
t.Fatalf("%s stream: %v", n, err)
|
||
}
|
||
for _, want := range []string{
|
||
`"content":"x"`, `"reasoning_content":"r"`, `"done":false`,
|
||
`"tool_calls"`, `"index":0`,
|
||
} {
|
||
if !strings.Contains(out, want) {
|
||
t.Errorf("%s stream 缺 %s\n实际: %s", n, want, out)
|
||
}
|
||
}
|
||
}
|
||
|
||
// usage-only chunk(choices 为空)**不得**被当成终结帧
|
||
usageOnly := `{"choices":[],"usage":{"prompt_tokens":3,"completion_tokens":0,"total_tokens":3}}`
|
||
for _, n := range []string{"openai", "groq", "mistral", "github"} {
|
||
out, err := vm.Transform(n, "transform_stream_chunk", usageOnly)
|
||
if err != nil {
|
||
t.Fatalf("%s usage-only: %v", n, err)
|
||
}
|
||
if !strings.Contains(out, `"done":false`) {
|
||
t.Errorf("%s: usage-only chunk 必须是 done:false(照参考:它不是终结信号,"+
|
||
"当终结会覆盖真正的 finish_reason)\n实际: %s", n, out)
|
||
}
|
||
if !strings.Contains(out, `"prompt":3`) {
|
||
t.Errorf("%s: usage-only chunk 的 usage 丢了:%s", n, out)
|
||
}
|
||
}
|
||
|
||
// 错误体抽取:OpenAI 风格 {error:{message}} → 消息本身
|
||
// (这段在 6 个适配器里原本逐字节相同)
|
||
for _, n := range []string{"deepseek", "github", "groq", "openai", "sensenova"} {
|
||
src, err := bundledAdapters.ReadFile("adapters/" + n + ".lua")
|
||
if err != nil {
|
||
t.Fatalf("读 %s: %v", n, err)
|
||
}
|
||
if err := vm.LoadAdapterSource(n, string(src)); err != nil {
|
||
t.Fatalf("%s: %v", n, err)
|
||
}
|
||
reason, ok, err := vm.TransformError(n, 429, `{"error":{"message":"rate limited"}}`)
|
||
if err != nil {
|
||
t.Fatalf("%s TransformError: %v", n, err)
|
||
}
|
||
if !ok || reason != "rate limited" {
|
||
t.Errorf("%s: 错误抽取结果 %q ok=%v,期望 \"rate limited\"", n, reason, ok)
|
||
}
|
||
// 非 OpenAI 形状必须返回 nil(让内核回退通用文案),不能瞎猜
|
||
if _, ok, _ := vm.TransformError(n, 500, `{"foo":"bar"}`); ok {
|
||
t.Errorf("%s: 非 OpenAI 形状应返回 ok=false", n)
|
||
}
|
||
}
|
||
|
||
// 坏 JSON 必须原样回退(不能吞成空)
|
||
if out, err := vm.Transform("openai", "transform_response", "not json"); err != nil {
|
||
t.Fatalf("bad json: %v", err)
|
||
} else if out != "not json" {
|
||
t.Errorf("坏 JSON 应原样回退,实得: %s", out)
|
||
}
|
||
}
|