140 lines
4.0 KiB
Go
140 lines
4.0 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"inbox/internal/base/httpx"
|
|
"inbox/internal/domain/role"
|
|
)
|
|
|
|
func (h *Handler) listRoles(w http.ResponseWriter, r *http.Request) {
|
|
items, err := h.Roles.List(r.Context())
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"roles": items})
|
|
}
|
|
|
|
func (h *Handler) getRoleDetail(w http.ResponseWriter, r *http.Request) {
|
|
item, err := h.Roles.GetDetail(r.Context(), r.PathValue("roleName"), r.URL.Query().Get("workspace_id"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
func (h *Handler) putRole(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
Title string `json:"title"`
|
|
ExecutorKind role.ExecutorKind `json:"executor_kind"`
|
|
Description string `json:"description"`
|
|
IsEnabled bool `json:"is_enabled"`
|
|
IsBuiltin bool `json:"is_builtin"`
|
|
SortOrder int `json:"sort_order"`
|
|
UpdatedBy string `json:"updated_by"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Roles.Upsert(r.Context(), role.Definition{
|
|
Name: r.PathValue("roleName"),
|
|
Title: req.Title,
|
|
ExecutorKind: req.ExecutorKind,
|
|
Description: req.Description,
|
|
IsEnabled: req.IsEnabled,
|
|
IsBuiltin: req.IsBuiltin,
|
|
SortOrder: req.SortOrder,
|
|
}, req.UpdatedBy)
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
func (h *Handler) putRolePrompt(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
WorkspaceID string `json:"workspace_id"`
|
|
ContentMarkdown string `json:"content_markdown"`
|
|
UpdatedBy string `json:"updated_by"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Roles.UpsertPrompt(r.Context(), role.Prompt{
|
|
RoleName: r.PathValue("roleName"),
|
|
WorkspaceID: req.WorkspaceID,
|
|
PromptKind: role.PromptKind(r.PathValue("promptKind")),
|
|
ContentMarkdown: req.ContentMarkdown,
|
|
}, req.UpdatedBy)
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
func (h *Handler) putRoleConfig(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
ConfigTOML string `json:"config_toml"`
|
|
AuthJSON string `json:"auth_json"`
|
|
UpdatedBy string `json:"updated_by"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Roles.UpsertConfig(r.Context(), role.Config{
|
|
RoleName: r.PathValue("roleName"),
|
|
ConfigTOML: req.ConfigTOML,
|
|
AuthJSON: req.AuthJSON,
|
|
}, req.UpdatedBy)
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
func (h *Handler) putRoleSkillBinding(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
WorkspaceID string `json:"workspace_id"`
|
|
IsEnabled bool `json:"is_enabled"`
|
|
SortOrder int `json:"sort_order"`
|
|
Config map[string]any `json:"config"`
|
|
UpdatedBy string `json:"updated_by"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Roles.UpsertSkillBinding(r.Context(), r.PathValue("roleName"), r.PathValue("skillKey"), role.SkillBinding{
|
|
WorkspaceID: req.WorkspaceID,
|
|
IsEnabled: req.IsEnabled,
|
|
SortOrder: req.SortOrder,
|
|
Config: req.Config,
|
|
}, req.UpdatedBy)
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
func (h *Handler) getResolvedRole(w http.ResponseWriter, r *http.Request) {
|
|
item, err := h.RuntimeConfig.ResolveRole(r.Context(), r.URL.Query().Get("workspace_id"), r.PathValue("roleName"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, item)
|
|
}
|