- 神识 Go:store 双实现(PG 自动建表+60s 就绪重试 / 内存回退)承载 usage_logs·byok_keys·quota_balances;BYOK 密钥库(AES-GCM 加密、API 脱敏、撤回即失效、主密钥未配置 fail-closed);配额三段式(预扣 max_tokens 钳位 1M→结算实际 token→全败幂等退款,HJ-2004);计量归属(IR user_id→usage_logs,/me/usage 用户维度聚合+配额总览,/admin/usage/sample 对账抽样);Anthropic/Gemini 出站适配器(SSE 解析、system 提升、用量映射);stub 端点补名修复计量空 endpoint - IR 契约:user_id/byok_key_id 双端同步(Go types.go + Py types.py) - 灵台:runs.owner_id(迁移 005,server_default 兼容存量)经 kernel 全链透传为 IR user_id;shenshi_client 扩展用户面方法(X-Hunjing-User-Id 代理,Echo 桩带内存实现);/me/byok-keys 登记·列表·撤回与 /me/usage 端点(4 用例) - 前端神识阁(/models 重建):偏好(生成参数表单)/自带密钥(登记加密保存、脱敏列表、撤回二次确认)/用量(聚合表+配额进度条)三区;me API 库+mock+5 用例;侧栏恢复入口 - 配置与部署:.env.example 增 SHENSHI_DATABASE_URL/SHENSHI_BYOK_MASTER_KEY/SHENSHI_USER_BUDGET_TOKENS;compose 神识容器接自有库(init-shenshi.sql 预建 hunjing_shenshi,与灵台不共库共表) - 证据:Go 34 用例(BYOK 独占/配额拒绝/结算退款/归属聚合)、后端 532、前端 210、BFF 4 全绿;跨进程 E2E 四场景(灵台 HttpShenshiClient→真 Go 神识:登记脱敏/BYOK 独占不可达整体失败/用量归属聚合/撤回即失效)通过;rewrite-plan M4 标记完成、能力矩阵神识行升已验证
147 lines
4.7 KiB
Go
147 lines
4.7 KiB
Go
package transform
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"hunjing/shenshi/internal/ir"
|
|
)
|
|
|
|
func readJSON(r *http.Request, v any) error {
|
|
defer func() { _ = r.Body.Close() }()
|
|
return json.NewDecoder(r.Body).Decode(v)
|
|
}
|
|
|
|
// 收集全量增量(适配器测试通用)。
|
|
func collect(t *testing.T, ad Adaptor, req *ir.ChatRequest) []ir.Delta {
|
|
t.Helper()
|
|
out := make(chan ir.Delta, 64)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
defer close(out)
|
|
done <- ad.Stream(context.Background(), req, out)
|
|
}()
|
|
var deltas []ir.Delta
|
|
for d := range out {
|
|
deltas = append(deltas, d)
|
|
}
|
|
if err := <-done; err != nil {
|
|
t.Fatalf("Stream 出错: %v", err)
|
|
}
|
|
return deltas
|
|
}
|
|
|
|
func TestAnthropicAdaptorRequestAndStream(t *testing.T) {
|
|
var gotBody antRequest
|
|
var gotAuth, gotVersion bool
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/messages" {
|
|
t.Errorf("路径错误: %s", r.URL.Path)
|
|
}
|
|
gotAuth = r.Header.Get("x-api-key") == "sk-test"
|
|
gotVersion = r.Header.Get("anthropic-version") != ""
|
|
_ = readJSON(r, &gotBody)
|
|
w.Header().Set("content-type", "text/event-stream")
|
|
_, _ = w.Write([]byte("data: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":11,\"output_tokens\":1}}}\n\n"))
|
|
_, _ = w.Write([]byte("data: {\"type\":\"content_block_delta\",\"delta\":{\"type\":\"text_delta\",\"text\":\"你好\"}}\n\n"))
|
|
_, _ = w.Write([]byte("data: {\"type\":\"content_block_delta\",\"delta\":{\"type\":\"text_delta\",\"text\":\",镜中\"}}\n\n"))
|
|
_, _ = w.Write([]byte("data: {\"type\":\"message_delta\",\"usage\":{\"output_tokens\":7}}\n\n"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ad := &AnthropicAdaptor{EndpointName: "ant", BaseURL: srv.URL, APIKey: "sk-test", UpstreamModel: "claude-x"}
|
|
req := &ir.ChatRequest{
|
|
Messages: []ir.Message{
|
|
{Role: "system", Content: "系统契约"},
|
|
{Role: "user", Content: "照心"},
|
|
},
|
|
RunID: "run_ant", Stream: true,
|
|
}
|
|
deltas := collect(t, ad, req)
|
|
|
|
if !gotAuth || !gotVersion {
|
|
t.Fatal("缺少 x-api-key / anthropic-version 头")
|
|
}
|
|
if gotBody.System == nil || *gotBody.System != "系统契约" {
|
|
t.Fatalf("system 应提升为顶层字段: %+v", gotBody.System)
|
|
}
|
|
if gotBody.MaxTokens == nil {
|
|
t.Fatal("Anthropic 要求 max_tokens 必填,适配器应兜底")
|
|
}
|
|
var text string
|
|
var last ir.Delta
|
|
for _, d := range deltas {
|
|
text += d.Delta
|
|
if d.FinishReason != "" {
|
|
last = d
|
|
}
|
|
}
|
|
if text != "你好,镜中" {
|
|
t.Fatalf("增量拼接错误: %q", text)
|
|
}
|
|
if last.FinishReason != "stop" || last.UsagePromptTokens != 11 || last.UsageCompletionTokens != 7 {
|
|
t.Fatalf("末帧用量错误: %+v", last)
|
|
}
|
|
}
|
|
|
|
func TestAnthropicAdaptorSurfaces429(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
_, _ = w.Write([]byte(`{"error":"rate_limited"}`))
|
|
}))
|
|
defer srv.Close()
|
|
ad := &AnthropicAdaptor{BaseURL: srv.URL, APIKey: "k", UpstreamModel: "m"}
|
|
out := make(chan ir.Delta, 4)
|
|
err := ad.Stream(context.Background(), &ir.ChatRequest{RunID: "r", Stream: true, Messages: []ir.Message{{Role: "user", Content: "x"}}}, out)
|
|
up, ok := err.(*UpstreamError)
|
|
if !ok || up.StatusCode != 429 {
|
|
t.Fatalf("应携带 429 UpstreamError 供绑定级冷却,实际 %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGeminiAdaptorRequestAndStream(t *testing.T) {
|
|
var gotPath, gotKey string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath = r.URL.Path
|
|
gotKey = r.Header.Get("x-goog-api-key")
|
|
w.Header().Set("content-type", "text/event-stream")
|
|
_, _ = w.Write([]byte("data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"第一\"}]}}]}\n\n"))
|
|
_, _ = w.Write([]byte("data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"第二\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":5,\"candidatesTokenCount\":9}}\n\n"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ad := &GeminiAdaptor{EndpointName: "gem", BaseURL: srv.URL, APIKey: "AIza-test", UpstreamModel: "gemini-x"}
|
|
req := &ir.ChatRequest{
|
|
Messages: []ir.Message{
|
|
{Role: "system", Content: "系统契约"},
|
|
{Role: "user", Content: "问"},
|
|
},
|
|
RunID: "run_gem", Stream: true,
|
|
}
|
|
deltas := collect(t, ad, req)
|
|
|
|
if gotPath != "/v1beta/models/gemini-x:streamGenerateContent" {
|
|
t.Fatalf("Gemini 路径错误: %s", gotPath)
|
|
}
|
|
if gotKey != "AIza-test" {
|
|
t.Fatal("缺少 x-goog-api-key 头")
|
|
}
|
|
var text string
|
|
var last ir.Delta
|
|
for _, d := range deltas {
|
|
text += d.Delta
|
|
if d.FinishReason != "" {
|
|
last = d
|
|
}
|
|
}
|
|
if text != "第一第二" {
|
|
t.Fatalf("增量拼接错误: %q", text)
|
|
}
|
|
if last.UsagePromptTokens != 5 || last.UsageCompletionTokens != 9 {
|
|
t.Fatalf("用量解析错误: %+v", last)
|
|
}
|
|
}
|