- Channel 新增 homepage 字段(渠道主页)
- 后端 /admin/channels/:id/favicon 代理抓取 {homepage}/favicon.ico, 内存缓存 1h;
服务端抓取可解析 localhost/内网主页, 避免浏览器跨域
- 前端渠道表单加"渠道主页"输入; 表格新增主页列(favicon + 域名), 加载失败回退灰色地球图标
- mock 上游提供 /favicon.ico 演示
Co-Authored-By: Claude <noreply@anthropic.com>
196 lines
6.8 KiB
Go
196 lines
6.8 KiB
Go
// mockupstream 本地 mock OpenAI 上游服务(联调代理链路,无需真实 key)。
|
|
// 支持 /v1/chat/completions 与 /v1/responses,含流式与非流式;/v1/models 返回模型列表。
|
|
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"},{"id":"claude-sonnet-5","object":"model"}]}`)
|
|
})
|
|
|
|
// favicon(演示渠道主页图标)
|
|
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
fmt.Fprint(w, `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><rect width="16" height="16" rx="4" fill="#34d399"/><path d="M4 4.6h8v1.4H4zM4 7.3h8v1.4H4zM4 10h5v1.4H4z" fill="#09090b"/></svg>`)
|
|
})
|
|
|
|
// Anthropic Messages 端点(provider=anthropic 的渠道走这里)
|
|
http.HandleFunc("/v1/messages", 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 {
|
|
streamMessages(w, req.Model)
|
|
return
|
|
}
|
|
replyMessages(w, req.Model)
|
|
})
|
|
|
|
log.Printf("mock upstream listening on %s", *addr)
|
|
log.Fatal(http.ListenAndServe(*addr, nil))
|
|
}
|
|
|
|
func replyMessages(w http.ResponseWriter, model string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
resp := map[string]any{
|
|
"id": "msg_mock789",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"model": model,
|
|
"content": []any{map[string]any{"type": "text", "text": "这是 mock Anthropic 上游的回复。"}},
|
|
"stop_reason": "end_turn",
|
|
"usage": map[string]any{"input_tokens": 14, "output_tokens": 10},
|
|
}
|
|
_ = json.NewEncoder(w).Encode(resp)
|
|
}
|
|
|
|
func streamMessages(w http.ResponseWriter, model string) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
fl, _ := w.(http.Flusher)
|
|
events := []map[string]any{
|
|
{"type": "message_start", "message": map[string]any{"id": "msg_mock789", "type": "message", "role": "assistant", "model": model, "content": []any{}, "usage": map[string]any{"input_tokens": 14, "output_tokens": 0}}},
|
|
{"type": "content_block_start", "index": 0, "content_block": map[string]any{"type": "text", "text": ""}},
|
|
{"type": "content_block_delta", "index": 0, "delta": map[string]any{"type": "text_delta", "text": "这是"}},
|
|
{"type": "content_block_delta", "index": 0, "delta": map[string]any{"type": "text_delta", "text": " Anthropic"}},
|
|
{"type": "content_block_stop", "index": 0},
|
|
{"type": "message_delta", "delta": map[string]any{"stop_reason": "end_turn", "stop_sequence": nil}, "usage": map[string]any{"output_tokens": 10}},
|
|
{"type": "message_stop"},
|
|
}
|
|
for _, e := range events {
|
|
b, _ := json.Marshal(e)
|
|
typ, _ := e["type"].(string)
|
|
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", typ, b)
|
|
fl.Flush()
|
|
time.Sleep(40 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
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},
|
|
}},
|
|
}
|
|
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)
|
|
}
|
|
}
|