M0+M1: 基建 + 用户/密钥/核心代理
后端 (Go/Gin/GORM): - 配置(viper+env)、SQLite/Postgres 迁移、argon2id、AES-GCM 渠道密钥、JWT+refresh cookie - 用户注册/登录/刷新/登出、API Key CRUD(仅存哈希、明文一次展示) - 代理网关: /v1/chat/completions、/v1/responses、/v1/models 直通 OpenAI 渠道 非流式+流式(SSE 零缓冲转发), 用量捕获(chat 末块/responses completed 嵌套), OpenAI 错误格式(401/402/404/502), 余额检查 - 异步批量记账 + 余额流水 + 日聚合, admin 用户/余额/配置 API - 单测: crypto/jwt/apikey/流式 usage 提取 前端 (Vue3+TS+Vite+Tailwind v4): - taste-skill 设计 tokens: 深色仪表盘, 石墨+信号铜色, Outfit+JetBrains Mono - Landing/登录/注册, 控制台(仪表盘图表/密钥管理/用量明细) - 基础组件 Button/Input/Badge/Modal, ECharts 用量图 部署: docker-compose(nginx+api+postgres), 双 Dockerfile, nginx SSE 反代 联调: scripts/mockupstream 本地 mock 上游, 端到端验证通过
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
module mockupstream
|
||||
|
||||
go 1.26.5
|
||||
@@ -0,0 +1,140 @@
|
||||
// mockupstream 本地 mock OpenAI 上游服务(联调代理链路,无需真实 key)。
|
||||
// 支持 /v1/chat/completions 与 /v1/responses,含流式与非流式。
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := flag.String("addr", ":9000", "listen address")
|
||||
flag.Parse()
|
||||
|
||||
http.HandleFunc("/v1/chat/completions", func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var req struct {
|
||||
Model string `json:"model"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
_ = json.Unmarshal(body, &req)
|
||||
if req.Stream {
|
||||
streamChat(w, req.Model)
|
||||
return
|
||||
}
|
||||
replyChat(w, req.Model)
|
||||
})
|
||||
|
||||
http.HandleFunc("/v1/responses", func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var req struct {
|
||||
Model string `json:"model"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
_ = json.Unmarshal(body, &req)
|
||||
if req.Stream {
|
||||
streamResponses(w, req.Model)
|
||||
return
|
||||
}
|
||||
replyResponses(w, req.Model)
|
||||
})
|
||||
|
||||
http.HandleFunc("/v1/models", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"object":"list","data":[{"id":"gpt-4o-mini","object":"model"},{"id":"gpt-4o","object":"model"}]}`)
|
||||
})
|
||||
|
||||
log.Printf("mock upstream listening on %s", *addr)
|
||||
log.Fatal(http.ListenAndServe(*addr, nil))
|
||||
}
|
||||
|
||||
func replyChat(w http.ResponseWriter, model string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
resp := map[string]any{
|
||||
"id": "chatcmpl-mock123",
|
||||
"object": "chat.completion",
|
||||
"model": model,
|
||||
"choices": []any{map[string]any{
|
||||
"index": 0,
|
||||
"message": map[string]any{"role": "assistant", "content": "你好,这是 mock 上游的回复。"},
|
||||
"finish_reason": "stop",
|
||||
}},
|
||||
"usage": map[string]any{"prompt_tokens": 12, "completion_tokens": 9, "total_tokens": 21},
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
func streamChat(w http.ResponseWriter, model string) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
fl, _ := w.(http.Flusher)
|
||||
chunks := []string{"你好", ",这是", " mock ", "流式回复。"}
|
||||
for i, c := range chunks {
|
||||
chunk := map[string]any{
|
||||
"id": "chatcmpl-mock123", "object": "chat.completion.chunk", "model": model,
|
||||
"choices": []any{map[string]any{
|
||||
"index": 0,
|
||||
"delta": map[string]any{"content": c},
|
||||
"finish_reason": nil,
|
||||
}},
|
||||
}
|
||||
if i == len(chunks)-1 {
|
||||
chunk["choices"] = []any{map[string]any{
|
||||
"index": 0, "delta": map[string]any{}, "finish_reason": "stop",
|
||||
}}
|
||||
}
|
||||
b, _ := json.Marshal(chunk)
|
||||
fmt.Fprintf(w, "data: %s\n\n", b)
|
||||
fl.Flush()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
// usage 末块(include_usage 时返回)
|
||||
usage := map[string]any{
|
||||
"id": "chatcmpl-mock123", "object": "chat.completion.chunk", "model": model,
|
||||
"choices": []any{}, "usage": map[string]any{"prompt_tokens": 12, "completion_tokens": 9, "total_tokens": 21},
|
||||
}
|
||||
b, _ := json.Marshal(usage)
|
||||
fmt.Fprintf(w, "data: %s\n\n", b)
|
||||
fmt.Fprint(w, "data: [DONE]\n\n")
|
||||
fl.Flush()
|
||||
}
|
||||
|
||||
func replyResponses(w http.ResponseWriter, model string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
resp := map[string]any{
|
||||
"id": "resp_mock456",
|
||||
"object": "response",
|
||||
"model": model,
|
||||
"status": "completed",
|
||||
"output": []any{map[string]any{
|
||||
"type": "message", "role": "assistant",
|
||||
"content": []any{map[string]any{"type": "output_text", "text": "这是 Responses API 的 mock 回复。"}},
|
||||
}},
|
||||
"usage": map[string]any{"input_tokens": 15, "output_tokens": 11, "total_tokens": 26},
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
func streamResponses(w http.ResponseWriter, model string) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fl, _ := w.(http.Flusher)
|
||||
events := []map[string]any{
|
||||
{"type": "response.created", "response": map[string]any{"id": "resp_mock456", "model": model, "object": "response", "status": "in_progress"}},
|
||||
{"type": "response.output_text.delta", "delta": "流式 Responses 内容", "item_id": "msg_1", "output_index": 0, "content_index": 0},
|
||||
{"type": "response.completed", "response": map[string]any{
|
||||
"id": "resp_mock456", "object": "response", "model": model, "status": "completed",
|
||||
"usage": map[string]any{"input_tokens": 15, "output_tokens": 11, "total_tokens": 26},
|
||||
}},
|
||||
}
|
||||
for _, e := range events {
|
||||
b, _ := json.Marshal(e)
|
||||
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", e["type"], b)
|
||||
fl.Flush()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user