Download internal/freemodel/client.go from a3216/gcli2api: direct link, hf CLI and curl.
- Browser
- Download file 17.1 kB
-
https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/freemodel/client.go
- Command line
-
hf download hf://spaces/a3216/gcli2api/internal/freemodel/client.go
-
curl -L -o client.go https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/freemodel/client.go
17.1 kB
| // Package freemodel 免登录免费模型层:把 OpenCode Zen / Kilo 公共网关的免费模型 | |
| // 接进本网关的 OpenAI 兼容接口。 | |
| // | |
| // 这一层不碰 WorkBuddy 账号池:请求命中 free:<provider>/<model> 时直接转发到上游, | |
| // 不选号、不扣积分、不写 pool 状态。请求整形移植自 dsh-our-free-model 的 o2a 层 | |
| // (opencode2api 的 Go 实现的 JS 移植),只保留 chat 线所需的部分。 | |
| package freemodel | |
| import ( | |
| "bufio" | |
| "bytes" | |
| "context" | |
| "encoding/json" | |
| "fmt" | |
| "io" | |
| "net/http" | |
| "regexp" | |
| "strings" | |
| "sync" | |
| "time" | |
| "github.com/misakano7545/workbuddy2api-panel/internal/extstore" | |
| ) | |
| // Prefix 免费模型在 /v1/models 与请求里的前缀。 | |
| const Prefix = "free:" | |
| const ( | |
| listTimeout = 15 * time.Second | |
| headerTimeout = 120 * time.Second | |
| turnTimeout = 300 * time.Second | |
| maxErrorBytes = 64 << 10 | |
| maxBodyRewrite = 64 << 20 | |
| ) | |
| // Client 免费模型层。 | |
| type Client struct { | |
| store *extstore.Store | |
| http *http.Client | |
| refreshMu sync.Mutex | |
| } | |
| // New 构建客户端。store 为空时只读默认值(不会保存)。 | |
| func New(store *extstore.Store) *Client { | |
| return &Client{ | |
| store: store, | |
| http: newFreeHTTPClient(), | |
| } | |
| } | |
| // Store 返回底层存储。 | |
| func (c *Client) Store() *extstore.Store { return c.store } | |
| // FreeModelID 拼出对外模型名。 | |
| func FreeModelID(provider, id string) string { return Prefix + provider + "/" + id } | |
| // ParseModel 拆出 provider 与上游 model id。 | |
| func ParseModel(model string) (provider, id string, ok bool) { | |
| model = strings.TrimSpace(model) | |
| if !strings.HasPrefix(model, Prefix) { | |
| return "", "", false | |
| } | |
| rest := strings.TrimPrefix(model, Prefix) | |
| provider, id, ok = strings.Cut(rest, "/") | |
| if !ok || provider == "" || id == "" { | |
| return "", "", false | |
| } | |
| if provider != extstore.ProviderOpenCode && provider != extstore.ProviderKilo { | |
| return "", "", false | |
| } | |
| return provider, id, true | |
| } | |
| // Enabled 免费模型层是否可用。 | |
| func (c *Client) Enabled() bool { | |
| if c == nil || c.store == nil { | |
| return false | |
| } | |
| return c.store.Snapshot().Free.Enabled | |
| } | |
| // Handles 报告 model 是否属于本层且已启用。 | |
| func (c *Client) Handles(model string) bool { | |
| if c == nil || c.store == nil { | |
| return false | |
| } | |
| provider, id, ok := ParseModel(model) | |
| if !ok { | |
| return false | |
| } | |
| return c.store.Snapshot().FreeModelEnabled(provider, id) | |
| } | |
| // Model 一个对外可用的免费模型(带渠道信息)。 | |
| type Model struct { | |
| Provider string | |
| ProviderLabel string | |
| ID string | |
| Name string | |
| } | |
| // Models 返回当前已启用(并已发现)的免费模型,供 /v1/models 汇总。 | |
| // 只读缓存:清单由启动后的后台刷新与面板「刷新模型清单」维护,避免模型列表 | |
| // 接口被上游超时拖住(首次启动 3 秒后就会有清单)。 | |
| func (c *Client) Models(ctx context.Context) []Model { | |
| if c == nil || c.store == nil { | |
| return nil | |
| } | |
| snap := c.store.Snapshot() | |
| if !snap.Free.Enabled { | |
| return nil | |
| } | |
| var out []Model | |
| for _, p := range snap.Free.Providers { | |
| if !p.Enabled { | |
| continue | |
| } | |
| for _, m := range p.Models { | |
| if m.Enabled { | |
| out = append(out, Model{Provider: p.Name, ProviderLabel: ProviderLabel(p), ID: m.ID, Name: m.Name}) | |
| } | |
| } | |
| } | |
| return out | |
| } | |
| // ProviderLabel 返回渠道显示名(Store 已带 Label,这里只做兜底)。 | |
| func ProviderLabel(p extstore.FreeProvider) string { | |
| if strings.TrimSpace(p.Label) != "" { | |
| return p.Label | |
| } | |
| switch p.Type { | |
| case extstore.TypeOpenCode: | |
| return "OpenCode Zen" | |
| case extstore.TypeKilo: | |
| return "Kilo" | |
| } | |
| return p.Name | |
| } | |
| // Refresh 拉取一次上游模型清单并合并进存储。 | |
| func (c *Client) Refresh(ctx context.Context, provider string) (int, error) { | |
| if c == nil || c.store == nil { | |
| return 0, fmt.Errorf("free model store not configured") | |
| } | |
| snap := c.store.Snapshot() | |
| p, ok := snap.Provider(provider) | |
| if !ok { | |
| return 0, fmt.Errorf("unknown provider %q", provider) | |
| } | |
| if !p.Enabled { | |
| return 0, fmt.Errorf("provider %s is disabled", provider) | |
| } | |
| c.refreshMu.Lock() | |
| defer c.refreshMu.Unlock() | |
| models, err := c.listModels(ctx, p) | |
| if err != nil { | |
| return 0, err | |
| } | |
| if err := c.store.MergeModels(p.Name, models); err != nil { | |
| return 0, err | |
| } | |
| return len(models), nil | |
| } | |
| // RefreshAll 刷新所有已启用渠道。 | |
| func (c *Client) RefreshAll(ctx context.Context) { | |
| if c == nil || c.store == nil { | |
| return | |
| } | |
| snap := c.store.Snapshot() | |
| if !snap.Free.Enabled { | |
| return | |
| } | |
| for _, p := range snap.Free.Providers { | |
| if p.Enabled { | |
| _, _ = c.Refresh(ctx, p.Name) | |
| } | |
| } | |
| } | |
| // RunRefresh 启动后台清单刷新:启动 3 秒后一次,之后每 6 小时一次。 | |
| // 上游清单会随改版增删模型;没有它,进程重启后要么空清单、要么永远停在旧快照。 | |
| func (c *Client) RunRefresh(ctx context.Context) { | |
| if c == nil || c.store == nil { | |
| return | |
| } | |
| timer := time.NewTimer(3 * time.Second) | |
| defer timer.Stop() | |
| select { | |
| case <-ctx.Done(): | |
| return | |
| case <-timer.C: | |
| } | |
| c.RefreshAll(ctx) | |
| ticker := time.NewTicker(6 * time.Hour) | |
| defer ticker.Stop() | |
| for { | |
| select { | |
| case <-ctx.Done(): | |
| return | |
| case <-ticker.C: | |
| c.RefreshAll(ctx) | |
| } | |
| } | |
| } | |
| // listModels 拉取一个渠道的模型清单,并按渠道的 FreeFilter 过滤。 | |
| func (c *Client) listModels(ctx context.Context, p extstore.FreeProvider) ([]extstore.FreeModel, error) { | |
| ctx, cancel := context.WithTimeout(ctx, listTimeout) | |
| defer cancel() | |
| url := strings.TrimRight(p.BaseURL, "/") + p.ModelsPath | |
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | |
| if err != nil { | |
| return nil, err | |
| } | |
| req.Header.Set("accept", "application/json") | |
| c.applyProviderHeaders(req, p) | |
| resp, err := c.http.Do(req) | |
| if err != nil { | |
| return nil, err | |
| } | |
| defer resp.Body.Close() | |
| raw, err := io.ReadAll(io.LimitReader(resp.Body, 16<<20)) | |
| if err != nil { | |
| return nil, err | |
| } | |
| if resp.StatusCode != http.StatusOK { | |
| return nil, fmt.Errorf("%s listing: HTTP %d: %s", p.Name, resp.StatusCode, truncate(string(raw), 300)) | |
| } | |
| var doc struct { | |
| Data []struct { | |
| ID string `json:"id"` | |
| Name string `json:"name"` | |
| IsFree bool `json:"isFree"` | |
| } `json:"data"` | |
| } | |
| if err := json.Unmarshal(raw, &doc); err != nil { | |
| return nil, fmt.Errorf("%s listing: %w", p.Name, err) | |
| } | |
| out := make([]extstore.FreeModel, 0, len(doc.Data)) | |
| for _, row := range doc.Data { | |
| if row.ID == "" { | |
| continue | |
| } | |
| if !providerFreeFilter(p, row.ID, row.IsFree) { | |
| continue | |
| } | |
| out = append(out, extstore.FreeModel{ID: row.ID, Name: row.Name, Enabled: true}) | |
| } | |
| return out, nil | |
| } | |
| func providerFreeFilter(p extstore.FreeProvider, id string, isFree bool) bool { | |
| switch p.FreeFilter { | |
| case "all": | |
| return true | |
| case "free-suffix": | |
| return isOpenCodeFree(id) | |
| case "is-free": | |
| return isFree | |
| default: | |
| return true | |
| } | |
| } | |
| // applyProviderHeaders 统一设置渠道请求头(清单与对话共用)。 | |
| func (c *Client) applyProviderHeaders(req *http.Request, p extstore.FreeProvider) { | |
| switch p.Type { | |
| case extstore.TypeOpenCode: | |
| req.Header.Set("authorization", "Bearer public") | |
| req.Header.Set("user-agent", clientUA) | |
| case extstore.TypeKilo: | |
| req.Header.Set("user-agent", "dsh-our-free-model") | |
| req.Header.Set("http-referer", "https://kilo.ai/") | |
| req.Header.Set("x-title", "Our Free Model") | |
| default: | |
| req.Header.Set("user-agent", "wb2api-freemodel") | |
| } | |
| if p.AuthHeader != "" { | |
| req.Header.Set(p.AuthHeader, p.AuthValue) | |
| } | |
| for k, v := range p.Headers { | |
| if strings.TrimSpace(k) != "" { | |
| req.Header.Set(k, v) | |
| } | |
| } | |
| } | |
| // ── 免费 id 判定 ──────────────────────────────────────────────────────────── | |
| var openCodeAlwaysFree = map[string]bool{ | |
| "big-pickle": true, | |
| // 这两条在 2026-10-31 的上游改版里下线过;保留判定,上游复活即可用。 | |
| "union-alpha": true, | |
| "space-bunny-free": true, | |
| } | |
| // freeIDSegment 与 dsh 的 isFreeLane 同口径:free 必须是独立分段 | |
| // (-free / _free / .free / :free,也允许出现在中间),不能是 freedom 这类子串。 | |
| var freeIDSegment = regexp.MustCompile(`(?:^|[-_.:])free(?:$|[-_.:])`) | |
| func isOpenCodeFree(id string) bool { | |
| id = strings.TrimSpace(id) | |
| if id == "" { | |
| return false | |
| } | |
| if openCodeAlwaysFree[id] { | |
| return true | |
| } | |
| return freeIDSegment.MatchString(strings.ToLower(id)) | |
| } | |
| // ── 请求转发 ──────────────────────────────────────────────────────────────── | |
| // ServeChat 处理一条命中免费模型层的 /v1/chat/completions 请求。 | |
| // body 是已经读出的原始请求体;exposed 是客户端请求的对外模型名。 | |
| func (c *Client) ServeChat(w http.ResponseWriter, r *http.Request, body []byte, exposed string) { | |
| c.ServeChatEcho(w, r, body, exposed, exposed) | |
| } | |
| // ServeChatEcho 同 ServeChat,但响应里的 model 用 echo 覆写(自定义模型映射时 | |
| // 客户端请求名与免费模型名不同,响应必须回显客户端自己那个名字)。 | |
| func (c *Client) ServeChatEcho(w http.ResponseWriter, r *http.Request, body []byte, exposed, echo string) { | |
| provider, model, ok := ParseModel(exposed) | |
| if !ok { | |
| writeOpenAIError(w, http.StatusBadRequest, "model_not_found", "unknown free model: "+exposed) | |
| return | |
| } | |
| if !c.Handles(exposed) { | |
| writeOpenAIError(w, http.StatusBadRequest, "model_not_found", | |
| "free model is disabled in the panel: "+exposed) | |
| return | |
| } | |
| if strings.TrimSpace(echo) == "" { | |
| echo = exposed | |
| } | |
| snap := c.store.Snapshot() | |
| p, ok := snap.Provider(provider) | |
| if !ok { | |
| writeOpenAIError(w, http.StatusBadRequest, "model_not_found", "unknown free provider: "+provider) | |
| return | |
| } | |
| switch p.Type { | |
| case extstore.TypeOpenCode: | |
| c.serveOpenCodeChat(w, r, p, model, exposed, echo, body) | |
| default: | |
| c.serveOpenAIChat(w, r, p, model, exposed, echo, body) | |
| } | |
| } | |
| // serveOpenAIChat 任意 OpenAI 兼容渠道(Kilo / 自定义公共端点)的透明转发。 | |
| func (c *Client) serveOpenAIChat(w http.ResponseWriter, r *http.Request, p extstore.FreeProvider, model, exposed, echo string, body []byte) { | |
| var obj map[string]any | |
| if err := json.Unmarshal(body, &obj); err != nil { | |
| writeOpenAIError(w, http.StatusBadRequest, "invalid_request_error", "invalid JSON body: "+err.Error()) | |
| return | |
| } | |
| obj["model"] = model | |
| out, err := json.Marshal(obj) | |
| if err != nil { | |
| writeOpenAIError(w, http.StatusBadRequest, "invalid_request_error", err.Error()) | |
| return | |
| } | |
| ctx, cancel := context.WithTimeout(r.Context(), turnTimeout) | |
| defer cancel() | |
| url := strings.TrimRight(p.BaseURL, "/") + p.ChatPath | |
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(out)) | |
| if err != nil { | |
| writeOpenAIError(w, http.StatusBadGateway, "upstream_error", err.Error()) | |
| return | |
| } | |
| req.Header.Set("content-type", "application/json") | |
| req.Header.Set("accept", "text/event-stream") | |
| c.applyProviderHeaders(req, p) | |
| resp, err := c.http.Do(req) | |
| if err != nil { | |
| writeOpenAIError(w, http.StatusBadGateway, "upstream_error", "free upstream: "+err.Error()) | |
| return | |
| } | |
| defer resp.Body.Close() | |
| // 原样透传内容,只把上游真实模型名改回客户端请求的 free: 名。 | |
| copyUpstreamResponseRewritten(w, resp, nil, echo) | |
| } | |
| func (c *Client) serveOpenCodeChat(w http.ResponseWriter, r *http.Request, p extstore.FreeProvider, model, exposed, echo string, body []byte) { | |
| var obj map[string]any | |
| if err := json.Unmarshal(body, &obj); err != nil { | |
| writeOpenAIError(w, http.StatusBadRequest, "invalid_request_error", "invalid JSON body: "+err.Error()) | |
| return | |
| } | |
| obj["model"] = model | |
| session := sessionFor(r, obj) | |
| project := projectID(r, session) | |
| renames := applyFingerprint(obj, model, session) | |
| out, err := json.Marshal(obj) | |
| if err != nil { | |
| writeOpenAIError(w, http.StatusBadRequest, "invalid_request_error", err.Error()) | |
| return | |
| } | |
| ctx, cancel := context.WithTimeout(r.Context(), turnTimeout) | |
| defer cancel() | |
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(p.BaseURL, "/")+p.ChatPath, bytes.NewReader(out)) | |
| if err != nil { | |
| writeOpenAIError(w, http.StatusBadGateway, "upstream_error", err.Error()) | |
| return | |
| } | |
| for k, v := range gatewayHeaders(session, project) { | |
| req.Header.Set(k, v) | |
| } | |
| // 官方指纹头之后允许渠道额外覆盖(自定义 Headers / AuthHeader)。 | |
| c.applyProviderHeaders(req, p) | |
| resp, err := c.http.Do(req) | |
| if err != nil { | |
| writeOpenAIError(w, http.StatusBadGateway, "upstream_error", "free upstream: "+err.Error()) | |
| return | |
| } | |
| defer resp.Body.Close() | |
| copyUpstreamResponseRewritten(w, resp, renames, echo) | |
| } | |
| // copyUpstreamResponseRewritten 透传 OpenCode 响应,并把工具名/模型名还原。 | |
| func copyUpstreamResponseRewritten(w http.ResponseWriter, resp *http.Response, renames *renameMap, exposed string) { | |
| contentType := resp.Header.Get("content-type") | |
| isSSE := strings.Contains(strings.ToLower(contentType), "text/event-stream") | |
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | |
| raw, _ := io.ReadAll(io.LimitReader(resp.Body, maxErrorBytes)) | |
| w.Header().Set("content-type", "application/json") | |
| w.WriteHeader(resp.StatusCode) | |
| _, _ = w.Write(raw) | |
| return | |
| } | |
| if !isSSE { | |
| raw, err := io.ReadAll(io.LimitReader(resp.Body, maxBodyRewrite)) | |
| if err != nil { | |
| writeOpenAIError(w, http.StatusBadGateway, "upstream_error", "read upstream: "+err.Error()) | |
| return | |
| } | |
| rewritten := rewriteJSONPayload(raw, renames, exposed) | |
| w.Header().Set("content-type", "application/json") | |
| w.WriteHeader(resp.StatusCode) | |
| _, _ = w.Write(rewritten) | |
| return | |
| } | |
| copyHeaders(w, resp) | |
| w.Header().Set("content-type", "text/event-stream; charset=utf-8") | |
| w.Header().Set("cache-control", "no-cache") | |
| w.Header().Set("x-accel-buffering", "no") | |
| w.WriteHeader(resp.StatusCode) | |
| flusher, _ := w.(http.Flusher) | |
| reader := bufio.NewReaderSize(resp.Body, 64<<10) | |
| for { | |
| line, err := reader.ReadBytes('\n') | |
| if len(line) > 0 { | |
| trimmed := bytes.TrimRight(line, "\r\n") | |
| if bytes.HasPrefix(trimmed, []byte("data:")) { | |
| payload := bytes.TrimSpace(trimmed[len("data:"):]) | |
| if len(payload) > 0 && !bytes.Equal(payload, []byte("[DONE]")) { | |
| payload = rewriteJSONPayload(payload, renames, exposed) | |
| } | |
| _, _ = w.Write([]byte("data: ")) | |
| _, _ = w.Write(payload) | |
| _, _ = w.Write([]byte("\n\n")) | |
| } else { | |
| _, _ = w.Write(line) | |
| if !bytes.HasSuffix(line, []byte("\n")) { | |
| _, _ = w.Write([]byte("\n")) | |
| } | |
| } | |
| if flusher != nil { | |
| flusher.Flush() | |
| } | |
| } | |
| if err != nil { | |
| if err != io.EOF { | |
| // 流中断:写一条协议内错误帧,客户端不会把它当成正常结束。 | |
| _, _ = w.Write([]byte("data: {\"error\":{\"message\":\"free upstream stream interrupted: " + | |
| jsonEscape(err.Error()) + "\",\"type\":\"upstream_error\"}}\n\n")) | |
| if flusher != nil { | |
| flusher.Flush() | |
| } | |
| } | |
| return | |
| } | |
| } | |
| } | |
| func rewriteJSONPayload(raw []byte, renames *renameMap, exposed string) []byte { | |
| var obj map[string]any | |
| if err := json.Unmarshal(raw, &obj); err != nil { | |
| return raw | |
| } | |
| restoreResponse(obj, renames, exposed) | |
| out, err := json.Marshal(obj) | |
| if err != nil { | |
| return raw | |
| } | |
| return out | |
| } | |
| func restoreResponse(obj map[string]any, renames *renameMap, exposed string) { | |
| if exposed != "" { | |
| obj["model"] = exposed | |
| } | |
| if renames == nil { | |
| return | |
| } | |
| choices, _ := obj["choices"].([]any) | |
| for _, rawChoice := range choices { | |
| choice, _ := rawChoice.(map[string]any) | |
| if choice == nil { | |
| continue | |
| } | |
| for _, key := range []string{"delta", "message"} { | |
| holder, _ := choice[key].(map[string]any) | |
| if holder == nil { | |
| continue | |
| } | |
| restoreToolCalls(holder, renames) | |
| if fc, ok := holder["function_call"].(map[string]any); ok { | |
| if name, ok := fc["name"].(string); ok { | |
| fc["name"] = renames.resolve(name) | |
| } | |
| } | |
| } | |
| } | |
| // 非流式的 responses 形状兜底(output 里可能带 function_call)。 | |
| restoreToolCalls(obj, renames) | |
| } | |
| func restoreToolCalls(holder map[string]any, renames *renameMap) { | |
| calls, _ := holder["tool_calls"].([]any) | |
| for _, rawCall := range calls { | |
| call, _ := rawCall.(map[string]any) | |
| fn, _ := call["function"].(map[string]any) | |
| if fn == nil { | |
| continue | |
| } | |
| if name, ok := fn["name"].(string); ok { | |
| fn["name"] = renames.resolve(name) | |
| } | |
| } | |
| } | |
| func copyHeaders(w http.ResponseWriter, resp *http.Response) { | |
| for _, key := range []string{"retry-after", "x-request-id"} { | |
| if v := resp.Header.Get(key); v != "" { | |
| w.Header().Set(key, v) | |
| } | |
| } | |
| } | |
| func writeOpenAIError(w http.ResponseWriter, status int, code, msg string) { | |
| w.Header().Set("content-type", "application/json") | |
| w.WriteHeader(status) | |
| _ = json.NewEncoder(w).Encode(map[string]any{ | |
| "error": map[string]any{"message": msg, "type": code, "code": code}, | |
| }) | |
| } | |
| func truncate(s string, n int) string { | |
| if len(s) <= n { | |
| return s | |
| } | |
| return s[:n] | |
| } | |
| func jsonEscape(s string) string { | |
| b, _ := json.Marshal(s) | |
| if len(b) >= 2 { | |
| return string(b[1 : len(b)-1]) | |
| } | |
| return s | |
| } | |