Changeset 0.17.0 (#75)
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/database"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/store"
|
||||
)
|
||||
@@ -256,31 +256,52 @@ func (r *personaRequest) toPersona() *models.Persona {
|
||||
// ResolvePreset loads a persona by ID and returns it if the user has access.
|
||||
// Returns nil if not found, inactive, or not accessible.
|
||||
// Used by completion.go and messages.go for preset unwrapping.
|
||||
func ResolvePreset(presetID, userID string) *models.Persona {
|
||||
var p models.Persona
|
||||
var providerConfigID *string
|
||||
err := database.DB.QueryRow(`
|
||||
SELECT id, name, base_model_id, provider_config_id, system_prompt,
|
||||
temperature, max_tokens, thinking_budget, top_p,
|
||||
scope, owner_id, created_by, is_active
|
||||
FROM personas
|
||||
WHERE id = $1 AND is_active = true
|
||||
AND (
|
||||
scope = 'global'
|
||||
OR (scope = 'personal' AND created_by = $2)
|
||||
OR (scope = 'personal' AND is_shared = true)
|
||||
OR (scope = 'team' AND owner_id IN (
|
||||
SELECT team_id FROM team_members WHERE user_id = $2
|
||||
))
|
||||
)
|
||||
`, presetID, userID).Scan(
|
||||
&p.ID, &p.Name, &p.BaseModelID, &providerConfigID, &p.SystemPrompt,
|
||||
&p.Temperature, &p.MaxTokens, &p.ThinkingBudget, &p.TopP,
|
||||
&p.Scope, &p.OwnerID, &p.CreatedBy, &p.IsActive,
|
||||
)
|
||||
if err != nil {
|
||||
func ResolvePreset(stores store.Stores, presetID, userID string) *models.Persona {
|
||||
ctx := context.Background()
|
||||
|
||||
p, err := stores.Personas.GetByID(ctx, presetID)
|
||||
if err != nil || !p.IsActive {
|
||||
return nil
|
||||
}
|
||||
p.ProviderConfigID = providerConfigID
|
||||
return &p
|
||||
|
||||
ok, err := stores.Personas.UserCanAccess(ctx, userID, presetID)
|
||||
if err != nil || !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// ── Persona-KB Binding Endpoints (v0.17.0) ──────
|
||||
|
||||
// GetPersonaKBs returns the knowledge bases bound to a persona.
|
||||
func (h *PersonaHandler) GetPersonaKBs(c *gin.Context) {
|
||||
personaID := c.Param("id")
|
||||
kbs, err := h.stores.Personas.GetKBs(c.Request.Context(), personaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load persona KBs"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": kbs})
|
||||
}
|
||||
|
||||
// SetPersonaKBs replaces the knowledge bases bound to a persona.
|
||||
func (h *PersonaHandler) SetPersonaKBs(c *gin.Context) {
|
||||
personaID := c.Param("id")
|
||||
|
||||
var req struct {
|
||||
KBIDs []string `json:"kb_ids"`
|
||||
AutoSearch map[string]bool `json:"auto_search"` // kb_id → true/false
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.stores.Personas.SetKBs(c.Request.Context(), personaID, req.KBIDs, req.AutoSearch); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update persona KBs"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user