M4 收口:神识用户面与计量闭环——BYOK/配额三段式/usage 落库/Anthropic+Gemini 适配器/神识阁
- 神识 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 标记完成、能力矩阵神识行升已验证
This commit is contained in:
1 parent
462d570751
commit
144415247e
51 files changed
+3533
-90
No files matched your search
@@ -0,0 +1,146 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
// Anthropic 出站适配器:IR → Anthropic Messages API(SSE 事件流)。
|
||||
package transform
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hunjing/shenshi/internal/ir"
|
||||
)
|
||||
|
||||
// AnthropicAdaptor Anthropic Messages 端点适配器。
|
||||
type AnthropicAdaptor struct {
|
||||
EndpointName string
|
||||
BaseURL string
|
||||
APIKey string
|
||||
UpstreamModel string
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
// Name 实现 Adaptor。
|
||||
func (a *AnthropicAdaptor) Name() string { return a.EndpointName }
|
||||
|
||||
type antMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// 可选标量一律指针 + omitempty(shenshi-gateway.md §5 DTO 纪律)。
|
||||
type antRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []antMessage `json:"messages"`
|
||||
MaxTokens *int `json:"max_tokens,omitempty"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
Stream bool `json:"stream"`
|
||||
System *string `json:"system,omitempty"`
|
||||
}
|
||||
|
||||
// antEvent Anthropic SSE 事件:message_start 的用量在 message.usage,
|
||||
// message_delta 的用量在顶层 usage(两处形态不同,指针区分可选标量)。
|
||||
type antEvent struct {
|
||||
Type string `json:"type"`
|
||||
Delta struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
} `json:"delta"`
|
||||
Message *struct {
|
||||
Usage *struct {
|
||||
InputTokens int `json:"input_tokens"`
|
||||
OutputTokens int `json:"output_tokens"`
|
||||
} `json:"usage"`
|
||||
} `json:"message"`
|
||||
Usage *struct {
|
||||
OutputTokens int `json:"output_tokens"`
|
||||
} `json:"usage"`
|
||||
}
|
||||
|
||||
// Stream 实现 Adaptor:Anthropic SSE(message_delta / content_block_delta 等)转 IR Delta。
|
||||
func (a *AnthropicAdaptor) Stream(ctx context.Context, req *ir.ChatRequest, out chan<- ir.Delta) error {
|
||||
body := antRequest{
|
||||
Model: a.UpstreamModel,
|
||||
Messages: make([]antMessage, 0, len(req.Messages)),
|
||||
MaxTokens: req.MaxTokens,
|
||||
Temperature: req.Temperature,
|
||||
Stream: true,
|
||||
}
|
||||
// Anthropic 的 system 是顶层字段而非消息序列首条
|
||||
var system strings.Builder
|
||||
for _, m := range req.Messages {
|
||||
if m.Role == "system" {
|
||||
if system.Len() > 0 {
|
||||
system.WriteString("\n\n")
|
||||
}
|
||||
system.WriteString(m.Content)
|
||||
continue
|
||||
}
|
||||
role := m.Role
|
||||
if role == "tool" {
|
||||
role = "user" // 最小映射:tool 结果按用户侧内容回传(工具循环随百工 M5 完整化)
|
||||
}
|
||||
body.Messages = append(body.Messages, antMessage{Role: role, Content: m.Content})
|
||||
}
|
||||
if system.Len() > 0 {
|
||||
s := system.String()
|
||||
body.System = &s
|
||||
}
|
||||
if body.MaxTokens == nil {
|
||||
def := 4096 // Anthropic 要求 max_tokens 必填
|
||||
body.MaxTokens = &def
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
||||
strings.TrimRight(a.BaseURL, "/")+"/v1/messages", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
httpReq.Header.Set("content-type", "application/json")
|
||||
httpReq.Header.Set("x-api-key", a.APIKey)
|
||||
httpReq.Header.Set("anthropic-version", "2023-06-01")
|
||||
|
||||
client := a.Client
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 120 * time.Second}
|
||||
}
|
||||
resp, err := client.Do(httpReq)
|
||||
if err != nil {
|
||||
return &UpstreamError{StatusCode: 0, Message: err.Error()}
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if resp.StatusCode >= 400 {
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(resp.Body)
|
||||
return &UpstreamError{StatusCode: resp.StatusCode, Message: buf.String()}
|
||||
}
|
||||
|
||||
var finish string
|
||||
var usagePrompt, usageCompletion int
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if !strings.HasPrefix(line, "data:") {
|
||||
continue
|
||||
}
|
||||
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
||||
if data == "" {
|
||||
continue
|
||||
}
|
||||
var ev antEvent
|
||||
if err := json.Unmarshal([]byte(data), &ev); err != nil {
|
||||
continue
|
||||
}
|
||||
switch ev.Type {
|
||||
case "content_block_delta":
|
||||
if ev.Delta.Type == "text_delta" && ev.Delta.Text != "" {
|
||||
out <- ir.Delta{Delta: ev.Delta.Text}
|
||||
}
|
||||
case "message_delta":
|
||||
// 结束与用量在 message_delta 顶层 usage 汇总(output_tokens)
|
||||
if ev.Usage != nil {
|
||||
usageCompletion = ev.Usage.OutputTokens
|
||||
}
|
||||
finish = "stop"
|
||||
case "message_start":
|
||||
if ev.Message != nil && ev.Message.Usage != nil {
|
||||
usagePrompt = ev.Message.Usage.InputTokens
|
||||
}
|
||||
case "error":
|
||||
return fmt.Errorf("上游流内错误: %s", data)
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if finish != "" {
|
||||
out <- ir.Delta{FinishReason: finish, UsagePromptTokens: usagePrompt, UsageCompletionTokens: usageCompletion}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// Gemini 出站适配器:IR → Google Generative Language streamGenerateContent(alt=sse)。
|
||||
package transform
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hunjing/shenshi/internal/ir"
|
||||
)
|
||||
|
||||
// GeminiAdaptor Gemini generateContent 端点适配器。
|
||||
type GeminiAdaptor struct {
|
||||
EndpointName string
|
||||
BaseURL string
|
||||
APIKey string
|
||||
UpstreamModel string
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
// Name 实现 Adaptor。
|
||||
func (g *GeminiAdaptor) Name() string { return g.EndpointName }
|
||||
|
||||
type gemPart struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type gemContent struct {
|
||||
Role string `json:"role,omitempty"` // user / model(Gemini 无 system 角色)
|
||||
Parts []gemPart `json:"parts"`
|
||||
}
|
||||
|
||||
type gemGeneration struct {
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
MaxTokens *int `json:"maxOutputTokens,omitempty"`
|
||||
}
|
||||
|
||||
type gemRequest struct {
|
||||
Contents []gemContent `json:"contents"`
|
||||
Generation *gemGeneration `json:"generationConfig,omitempty"`
|
||||
SystemInstruction *gemContent `json:"systemInstruction,omitempty"`
|
||||
}
|
||||
|
||||
type gemChunk struct {
|
||||
Candidates []struct {
|
||||
Content struct {
|
||||
Parts []gemPart `json:"parts"`
|
||||
} `json:"content"`
|
||||
FinishReason *string `json:"finishReason"`
|
||||
} `json:"candidates"`
|
||||
UsageMetadata *struct {
|
||||
PromptTokenCount int `json:"promptTokenCount"`
|
||||
CandidatesTokenCount int `json:"candidatesTokenCount"`
|
||||
} `json:"usageMetadata"`
|
||||
}
|
||||
|
||||
// Stream 实现 Adaptor:Gemini SSE 数据帧转 IR Delta。
|
||||
func (g *GeminiAdaptor) Stream(ctx context.Context, req *ir.ChatRequest, out chan<- ir.Delta) error {
|
||||
body := gemRequest{}
|
||||
for _, m := range req.Messages {
|
||||
switch m.Role {
|
||||
case "system":
|
||||
if body.SystemInstruction == nil {
|
||||
body.SystemInstruction = &gemContent{}
|
||||
}
|
||||
body.SystemInstruction.Parts = append(body.SystemInstruction.Parts, gemPart{Text: m.Content})
|
||||
case "assistant":
|
||||
body.Contents = append(body.Contents, gemContent{Role: "model", Parts: []gemPart{{Text: m.Content}}})
|
||||
default: // user / tool 结果按 user 侧回传(工具循环随百工 M5 完整化)
|
||||
body.Contents = append(body.Contents, gemContent{Role: "user", Parts: []gemPart{{Text: m.Content}}})
|
||||
}
|
||||
}
|
||||
if req.Temperature != nil || req.MaxTokens != nil {
|
||||
body.Generation = &gemGeneration{Temperature: req.Temperature, MaxTokens: req.MaxTokens}
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url := strings.TrimRight(g.BaseURL, "/") +
|
||||
"/v1beta/models/" + g.UpstreamModel + ":streamGenerateContent?alt=sse"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
httpReq.Header.Set("content-type", "application/json")
|
||||
httpReq.Header.Set("x-goog-api-key", g.APIKey)
|
||||
|
||||
client := g.Client
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 120 * time.Second}
|
||||
}
|
||||
resp, err := client.Do(httpReq)
|
||||
if err != nil {
|
||||
return &UpstreamError{StatusCode: 0, Message: err.Error()}
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if resp.StatusCode >= 400 {
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(resp.Body)
|
||||
return &UpstreamError{StatusCode: resp.StatusCode, Message: buf.String()}
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if !strings.HasPrefix(line, "data:") {
|
||||
continue
|
||||
}
|
||||
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
||||
if data == "" {
|
||||
continue
|
||||
}
|
||||
var chunk gemChunk
|
||||
if err := json.Unmarshal([]byte(data), &chunk); err != nil {
|
||||
continue
|
||||
}
|
||||
if len(chunk.Candidates) > 0 {
|
||||
c := chunk.Candidates[0]
|
||||
for _, part := range c.Content.Parts {
|
||||
if part.Text != "" {
|
||||
out <- ir.Delta{Delta: part.Text}
|
||||
}
|
||||
}
|
||||
if c.FinishReason != nil {
|
||||
finish := "stop"
|
||||
if strings.EqualFold(*c.FinishReason, "SAFETY") || strings.EqualFold(*c.FinishReason, "RECITATION") {
|
||||
finish = "content_filter"
|
||||
}
|
||||
var up, uc int
|
||||
if chunk.UsageMetadata != nil {
|
||||
up, uc = chunk.UsageMetadata.PromptTokenCount, chunk.UsageMetadata.CandidatesTokenCount
|
||||
}
|
||||
out <- ir.Delta{FinishReason: finish, UsagePromptTokens: up, UsageCompletionTokens: uc}
|
||||
}
|
||||
}
|
||||
}
|
||||
return scanner.Err()
|
||||
}
|
||||
Reference in new issue
Block a user