Files
easyai-ai-gateway/apps/api/internal/httpapi/model_list_handlers.go
T
wangbo 7376d6fab6 refactor(access): 统一分层白名单权限语义
取消跨主体专属占用,按租户、用户组、用户、当前 API Key 和 scope 分层求交,并在任务落库前统一校验候选。\n\n增加旧 allow 规则归档清理迁移、脱敏审计工具和回滚运行手册,补齐主体隔离、deny 优先及列表与运行时一致性测试。
2026-08-03 15:43:49 +08:00

64 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package httpapi
import (
"net/http"
"sort"
"github.com/easyai/easyai-ai-gateway/apps/api/internal/auth"
)
// listLegacyPlayableModels godoc
// @Summary 列出可调用平台模型(已弃用)
// @Description 兼容期 rich 平台来源明细;新客户端应改用 /api/v1/platform-modelsOpenAI 客户端使用 /v1/models。
// @Tags playground
// @Produce json
// @Security BearerAuth
// @Deprecated
// @Success 200 {object} PlatformModelListResponse
// @Failure 401 {object} ErrorEnvelope
// @Failure 500 {object} ErrorEnvelope
// @Router /api/v1/models [get]
func (s *Server) listLegacyPlayableModels(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Deprecation", "true")
w.Header().Set("Link", "</api/v1/platform-models>; rel=\"successor-version\"")
s.listPlayableModels(w, r)
}
// listOpenAIModels godoc
// @Summary 列出 OpenAI 兼容模型
// @Description 按全局启用、租户、用户组、用户、当前 API Key 分层白名单及 scope 的交集返回去重后的逻辑模型列表;其他主体规则不参与求值。
// @Tags openai-compatible
// @Produce json
// @Security BearerAuth
// @Success 200 {object} OpenAIModelListResponse
// @Failure 401 {object} ErrorEnvelope
// @Failure 500 {object} ErrorEnvelope
// @Router /v1/models [get]
func (s *Server) listOpenAIModels(w http.ResponseWriter, r *http.Request) {
user, _ := auth.UserFromContext(r.Context())
models, err := s.store.ListAccessiblePlatformModels(r.Context(), user)
if err != nil {
s.logger.Error("list openai models failed", "error", err)
writeError(w, http.StatusInternalServerError, "list models failed")
return
}
byID := map[string]OpenAIModel{}
for _, model := range models {
id := model.ModelName
if id == "" {
continue
}
created := model.CreatedAt.Unix()
current, exists := byID[id]
if !exists || created < current.Created {
byID[id] = OpenAIModel{ID: id, Object: "model", Created: created, OwnedBy: "easyai"}
}
}
data := make([]OpenAIModel, 0, len(byID))
for _, model := range byID {
data = append(data, model)
}
sort.Slice(data, func(i, j int) bool { return data[i].ID < data[j].ID })
writeJSON(w, http.StatusOK, OpenAIModelListResponse{Object: "list", Data: data})
}