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,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