Changeset 0.8.1 (#44)

This commit is contained in:
2026-02-22 01:22:23 +00:00
parent 1adef94617
commit c0d95fd7f5
13 changed files with 473 additions and 55 deletions

View File

@@ -58,7 +58,7 @@ type presetResponse struct {
// These require user_providers_enabled for personal presets.
// ListUserPresets returns all presets visible to the user:
// their own personal presets + all global presets + shared presets.
// their own personal presets + all global presets + shared presets + team presets.
// GET /api/v1/presets
func (h *PresetHandler) ListUserPresets(c *gin.Context) {
userID := getUserID(c)
@@ -76,6 +76,9 @@ func (h *PresetHandler) ListUserPresets(c *gin.Context) {
mp.scope = 'global'
OR (mp.scope = 'personal' AND mp.created_by = $1)
OR (mp.scope = 'personal' AND mp.is_shared = true)
OR (mp.scope = 'team' AND mp.team_id IN (
SELECT team_id FROM team_members WHERE user_id = $1
))
)
ORDER BY mp.scope ASC, mp.name ASC
`, userID)
@@ -505,6 +508,9 @@ func ResolvePreset(presetID, userID string) *models.ModelPreset {
scope = 'global'
OR (scope = 'personal' AND created_by = $2)
OR (scope = 'personal' AND is_shared = true)
OR (scope = 'team' AND team_id IN (
SELECT team_id FROM team_members WHERE user_id = $2
))
)
`, presetID, userID).Scan(
&p.ID, &p.Name, &p.BaseModelID, &p.APIConfigID, &p.SystemPrompt,
@@ -515,3 +521,117 @@ func ResolvePreset(presetID, userID string) *models.ModelPreset {
}
return &p
}
// ── Team Preset Endpoints ─────────────────
// Team admins can create/manage presets scoped to their team.
type createTeamPresetRequest struct {
Name string `json:"name" binding:"required"`
Description string `json:"description"`
BaseModelID string `json:"base_model_id" binding:"required"`
APIConfigID *string `json:"api_config_id,omitempty"`
SystemPrompt string `json:"system_prompt"`
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
Icon string `json:"icon,omitempty"`
}
// ListTeamPresets returns presets scoped to a team.
// GET /api/v1/teams/:teamId/presets
func (h *PresetHandler) ListTeamPresets(c *gin.Context) {
teamID := getTeamID(c)
rows, err := database.DB.Query(`
SELECT mp.id, mp.name, mp.description, mp.base_model_id, mp.api_config_id,
mp.system_prompt, mp.temperature, mp.max_tokens, mp.tools_enabled,
mp.scope, mp.team_id, mp.created_by, mp.is_shared, mp.is_active,
mp.icon, mp.avatar, mp.created_at, mp.updated_at,
COALESCE(ac.name, '') as provider_name
FROM model_presets mp
LEFT JOIN api_configs ac ON mp.api_config_id = ac.id
WHERE mp.team_id = $1 AND mp.scope = 'team'
ORDER BY mp.name ASC
`, teamID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list team presets"})
return
}
defer rows.Close()
presets := make([]presetResponse, 0)
for rows.Next() {
var p presetResponse
if err := rows.Scan(
&p.ID, &p.Name, &p.Description, &p.BaseModelID, &p.APIConfigID,
&p.SystemPrompt, &p.Temperature, &p.MaxTokens, &p.ToolsEnabled,
&p.Scope, &p.TeamID, &p.CreatedBy, &p.IsShared, &p.IsActive,
&p.Icon, &p.Avatar, &p.CreatedAt, &p.UpdatedAt, &p.ProviderName,
); err != nil {
continue
}
presets = append(presets, p)
}
c.JSON(http.StatusOK, gin.H{"presets": presets})
}
// CreateTeamPreset creates a preset scoped to a team.
// POST /api/v1/teams/:teamId/presets
func (h *PresetHandler) CreateTeamPreset(c *gin.Context) {
teamID := getTeamID(c)
userID := getUserID(c)
var req createTeamPresetRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.Name = strings.TrimSpace(req.Name)
if req.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"})
return
}
toolsJSON := "[]"
var id string
err := database.DB.QueryRow(`
INSERT INTO model_presets (name, description, base_model_id, api_config_id,
system_prompt, temperature, max_tokens, tools_enabled,
scope, team_id, created_by, is_shared, icon)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8::jsonb, 'team', $9, $10, true, $11)
RETURNING id
`, req.Name, req.Description, req.BaseModelID, req.APIConfigID,
req.SystemPrompt, req.Temperature, req.MaxTokens, toolsJSON,
teamID, userID, req.Icon,
).Scan(&id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create team preset: " + err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"id": id})
}
// DeleteTeamPreset deletes a team-scoped preset.
// DELETE /api/v1/teams/:teamId/presets/:id
func (h *PresetHandler) DeleteTeamPreset(c *gin.Context) {
teamID := getTeamID(c)
presetID := c.Param("id")
res, err := database.DB.Exec(`
DELETE FROM model_presets WHERE id = $1 AND team_id = $2 AND scope = 'team'
`, presetID, teamID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "delete failed"})
return
}
if n, _ := res.RowsAffected(); n == 0 {
c.JSON(http.StatusNotFound, gin.H{"error": "preset not found"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}