Download internal/panel/extensions.go from a3216/gcli2api: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/panel/extensions.go
- Command line
-
hf download hf://spaces/a3216/gcli2api/internal/panel/extensions.go
-
curl -L -o extensions.go https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/panel/extensions.go
10.8 kB
| // extensions.go 面板「扩展模型」页接口:免登录免费模型、自定义模型映射、访问密钥。 | |
| package panel | |
| import ( | |
| "context" | |
| "encoding/json" | |
| "io" | |
| "net/http" | |
| "os" | |
| "strings" | |
| "github.com/misakano7545/workbuddy2api-panel/internal/extstore" | |
| "github.com/misakano7545/workbuddy2api-panel/internal/freemodel" | |
| ) | |
| type extensionView struct { | |
| OK bool `json:"ok"` | |
| Path string `json:"path"` | |
| Free extstore.FreeSettings `json:"free"` | |
| CustomModels []extstore.CustomModel `json:"custom_models"` | |
| AccessKeys []extstore.AccessKey `json:"access_keys"` | |
| PanelKeyEnv bool `json:"panel_key_env_managed"` | |
| ProviderTypes []string `json:"provider_types"` | |
| } | |
| func (p *Panel) extensionView() extensionView { | |
| view := extensionView{ | |
| OK: true, | |
| ProviderTypes: []string{extstore.TypeOpenCode, extstore.TypeKilo, extstore.TypeOpenAI}, | |
| PanelKeyEnv: strings.TrimSpace(os.Getenv("WB2A_PANEL_KEY")) != "", | |
| } | |
| if p.cfg.Extensions == nil { | |
| return view | |
| } | |
| snap := p.cfg.Extensions.Snapshot() | |
| view.Path = p.cfg.Extensions.Path() | |
| view.Free = snap.Free | |
| view.CustomModels = snap.CustomModels | |
| view.AccessKeys = snap.AccessKeys | |
| if view.CustomModels == nil { | |
| view.CustomModels = []extstore.CustomModel{} | |
| } | |
| if view.AccessKeys == nil { | |
| view.AccessKeys = []extstore.AccessKey{} | |
| } | |
| return view | |
| } | |
| func (p *Panel) getExtensions(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| // extensionPanelModels 面板「模型与档位」页里的扩展模型行(免费渠道 + 自定义映射)。 | |
| // 字段与 WorkBuddy 模型行保持同一套键,额外带 channel / channel_label 供前端打标签。 | |
| func (p *Panel) extensionPanelModels() []map[string]any { | |
| if p.cfg.Extensions == nil { | |
| return nil | |
| } | |
| out := make([]map[string]any, 0) | |
| if p.cfg.FreeModels != nil { | |
| for _, m := range p.cfg.FreeModels.Models(context.Background()) { | |
| out = append(out, map[string]any{ | |
| "id": freemodel.FreeModelID(m.Provider, m.ID), | |
| "name": m.Name, | |
| "realm": "free", | |
| "channel": "free", | |
| "channel_label": m.ProviderLabel, | |
| "provider": m.Provider, | |
| "free": true, | |
| "hidden": false, | |
| "context_length": 131072, | |
| "max_output_tokens": 32768, | |
| "supports_tool_call": true, | |
| "supports_reasoning": true, | |
| "gateway_policy_enabled": false, | |
| }) | |
| } | |
| } | |
| snap := p.cfg.Extensions.Snapshot() | |
| for _, cm := range snap.CustomModels { | |
| if !cm.Enabled { | |
| continue | |
| } | |
| row := map[string]any{ | |
| "id": cm.Name, | |
| "name": cm.Name, | |
| "realm": "custom", | |
| "channel": "custom", | |
| "channel_label": "自定义映射", | |
| "base_model": cm.Target, | |
| "hidden": false, | |
| "context_length": 131072, | |
| "max_output_tokens": 32768, | |
| "gateway_policy_enabled": false, | |
| } | |
| if cm.Description != "" { | |
| row["description"] = cm.Description | |
| } | |
| out = append(out, row) | |
| } | |
| return out | |
| } | |
| type freeSettingsBody struct { | |
| Enabled *bool `json:"enabled"` | |
| Providers []extstore.FreeProvider `json:"providers"` | |
| } | |
| func (p *Panel) saveFreeSettings(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body freeSettingsBody | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| if body.Enabled != nil { | |
| if err := p.cfg.Extensions.SetFreeEnabled(*body.Enabled); err != nil { | |
| writeErr(w, http.StatusInternalServerError, err.Error()) | |
| return | |
| } | |
| } | |
| for _, provider := range body.Providers { | |
| if err := p.cfg.Extensions.UpsertFreeProvider(provider); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| func (p *Panel) deleteFreeProvider(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body struct { | |
| Name string `json:"name"` | |
| } | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| if err := p.cfg.Extensions.DeleteFreeProvider(body.Name); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| func (p *Panel) refreshFreeModels(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil || p.cfg.FreeModels == nil { | |
| writeErr(w, http.StatusNotImplemented, "free model layer not configured") | |
| return | |
| } | |
| var body struct { | |
| Provider string `json:"provider"` | |
| } | |
| _ = decodeJSON(r, &body) | |
| provider := strings.TrimSpace(body.Provider) | |
| if provider == "" { | |
| writeErr(w, http.StatusBadRequest, "provider is required") | |
| return | |
| } | |
| count, err := p.cfg.FreeModels.Refresh(r.Context(), provider) | |
| if err != nil { | |
| writeErr(w, http.StatusBadGateway, err.Error()) | |
| return | |
| } | |
| view := p.extensionView() | |
| writeJSON(w, http.StatusOK, map[string]any{ | |
| "ok": true, | |
| "provider": provider, | |
| "count": count, | |
| "view": view, | |
| }) | |
| } | |
| func (p *Panel) toggleFreeModel(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body struct { | |
| Provider string `json:"provider"` | |
| ID string `json:"id"` | |
| Enabled bool `json:"enabled"` | |
| } | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| if err := p.cfg.Extensions.SetModelEnabled(strings.TrimSpace(body.Provider), body.ID, body.Enabled); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| type customModelBody struct { | |
| Name string `json:"name"` | |
| Target string `json:"target"` | |
| Prompt string `json:"prompt"` | |
| Mode string `json:"mode"` | |
| Description string `json:"description"` | |
| Temperature *float64 `json:"temperature"` | |
| MaxTokens *int `json:"max_tokens"` | |
| Enabled *bool `json:"enabled"` | |
| } | |
| func (p *Panel) saveCustomModel(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body customModelBody | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| enabled := true | |
| if body.Enabled != nil { | |
| enabled = *body.Enabled | |
| } | |
| cm := extstore.CustomModel{ | |
| Name: body.Name, | |
| Target: body.Target, | |
| Prompt: body.Prompt, | |
| Mode: body.Mode, | |
| Description: body.Description, | |
| Temperature: body.Temperature, | |
| MaxTokens: body.MaxTokens, | |
| Enabled: enabled, | |
| } | |
| if err := p.cfg.Extensions.UpsertCustom(cm); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| func (p *Panel) deleteCustomModel(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body struct { | |
| Name string `json:"name"` | |
| } | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| if err := p.cfg.Extensions.DeleteCustom(body.Name); err != nil { | |
| writeErr(w, http.StatusInternalServerError, err.Error()) | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| type accessKeyBody struct { | |
| ID string `json:"id"` | |
| Name string `json:"name"` | |
| Note string `json:"note"` | |
| Enabled *bool `json:"enabled"` | |
| AllowModels []string `json:"allow_models"` | |
| DenyModels []string `json:"deny_models"` | |
| } | |
| func (p *Panel) createAccessKey(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body accessKeyBody | |
| _ = decodeJSON(r, &body) | |
| key, err := p.cfg.Extensions.AddAccessKey(body.Name, body.Note, body.AllowModels, body.DenyModels) | |
| if err != nil { | |
| writeErr(w, http.StatusInternalServerError, err.Error()) | |
| return | |
| } | |
| view := p.extensionView() | |
| writeJSON(w, http.StatusOK, map[string]any{ | |
| "ok": true, | |
| "key": key.Key, | |
| "id": key.ID, | |
| "view": view, | |
| }) | |
| } | |
| func (p *Panel) updateAccessKey(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body accessKeyBody | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| if strings.TrimSpace(body.ID) == "" { | |
| writeErr(w, http.StatusBadRequest, "id is required") | |
| return | |
| } | |
| enabled := true | |
| if body.Enabled != nil { | |
| enabled = *body.Enabled | |
| } | |
| if err := p.cfg.Extensions.UpdateAccessKey(body.ID, body.Name, body.Note, enabled, body.AllowModels, body.DenyModels); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| func (p *Panel) deleteAccessKey(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body struct { | |
| ID string `json:"id"` | |
| } | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| if err := p.cfg.Extensions.DeleteAccessKey(body.ID); err != nil { | |
| writeErr(w, http.StatusInternalServerError, err.Error()) | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| func (p *Panel) toggleAccessKey(w http.ResponseWriter, r *http.Request) { | |
| if p.cfg.Extensions == nil { | |
| writeErr(w, http.StatusNotImplemented, "extensions store not configured") | |
| return | |
| } | |
| var body struct { | |
| ID string `json:"id"` | |
| Enabled bool `json:"enabled"` | |
| } | |
| if err := decodeJSON(r, &body); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| if err := p.cfg.Extensions.SetAccessKeyEnabled(body.ID, body.Enabled); err != nil { | |
| writeErr(w, http.StatusBadRequest, err.Error()) | |
| return | |
| } | |
| writeJSON(w, http.StatusOK, p.extensionView()) | |
| } | |
| func decodeJSON(r *http.Request, dst any) error { | |
| raw, err := io.ReadAll(io.LimitReader(r.Body, 1<<20)) | |
| if err != nil { | |
| return err | |
| } | |
| if len(strings.TrimSpace(string(raw))) == 0 { | |
| return nil | |
| } | |
| return json.Unmarshal(raw, dst) | |
| } | |