Changeset 0.19.0.1 (#82)

This commit is contained in:
2026-02-28 23:46:23 +00:00
parent 091ce2af6a
commit 748f49bedd
30 changed files with 3873 additions and 151 deletions

View File

@@ -53,6 +53,7 @@ type channelResponse struct {
IsArchived bool `json:"is_archived"`
IsPinned bool `json:"is_pinned"`
Folder *string `json:"folder"`
ProjectID *string `json:"project_id,omitempty"`
Tags []string `json:"tags"`
Settings json.RawMessage `json:"settings,omitempty"`
MessageCount int `json:"message_count"`
@@ -138,6 +139,7 @@ func (h *ChannelHandler) ListChannels(c *gin.Context) {
folder := c.Query("folder")
channelType := c.DefaultQuery("type", "") // empty = all types
search := strings.TrimSpace(c.Query("search"))
projectFilter := c.Query("project_id") // "uuid" or "none"
// Count total
countQuery := `SELECT COUNT(*) FROM channels WHERE user_id = $1 AND is_archived = $2`
@@ -159,6 +161,13 @@ func (h *ChannelHandler) ListChannels(c *gin.Context) {
countArgs = append(countArgs, "%"+search+"%")
argN++
}
if projectFilter == "none" {
countQuery += ` AND project_id IS NULL`
} else if projectFilter != "" {
countQuery += ` AND project_id = $` + strconv.Itoa(argN)
countArgs = append(countArgs, projectFilter)
argN++
}
var total int
if err := database.DB.QueryRow(database.Q(countQuery), countArgs...).Scan(&total); err != nil {
@@ -169,7 +178,8 @@ func (h *ChannelHandler) ListChannels(c *gin.Context) {
// Fetch channels with message count
query := `
SELECT c.id, c.user_id, c.title, c.type, c.description, c.model, c.provider_config_id,
c.system_prompt, c.is_archived, c.is_pinned, c.folder, c.tags, c.settings,
c.system_prompt, c.is_archived, c.is_pinned, c.folder, c.project_id,
c.tags, c.settings,
COALESCE(mc.cnt, 0) AS message_count,
c.created_at, c.updated_at
FROM channels c
@@ -196,6 +206,13 @@ func (h *ChannelHandler) ListChannels(c *gin.Context) {
args = append(args, "%"+search+"%")
argN++
}
if projectFilter == "none" {
query += ` AND c.project_id IS NULL`
} else if projectFilter != "" {
query += ` AND c.project_id = $` + strconv.Itoa(argN)
args = append(args, projectFilter)
argN++
}
query += ` ORDER BY c.is_pinned DESC, c.updated_at DESC LIMIT $` + strconv.Itoa(argN) + ` OFFSET $` + strconv.Itoa(argN+1)
args = append(args, perPage, offset)
@@ -213,7 +230,7 @@ func (h *ChannelHandler) ListChannels(c *gin.Context) {
var tags []string
err := rows.Scan(
&ch.ID, &ch.UserID, &ch.Title, &ch.Type, &ch.Description, &ch.Model, &ch.APIConfigID,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder, &ch.ProjectID,
scanTags(&tags), scanJSON(&ch.Settings),
&ch.MessageCount, &ch.CreatedAt, &ch.UpdatedAt,
)
@@ -278,11 +295,12 @@ func (h *ChannelHandler) CreateChannel(c *gin.Context) {
// Read back the row
err = database.DB.QueryRow(`
SELECT id, user_id, title, type, description, model, provider_config_id,
system_prompt, is_archived, is_pinned, folder, tags, settings,
system_prompt, is_archived, is_pinned, folder, project_id,
tags, settings,
created_at, updated_at
FROM channels WHERE id = ?`, id).Scan(
&ch.ID, &ch.UserID, &ch.Title, &ch.Type, &ch.Description, &ch.Model, &ch.APIConfigID,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder, &ch.ProjectID,
scanTags(&tags), scanJSON(&ch.Settings), &ch.CreatedAt, &ch.UpdatedAt,
)
if err != nil {
@@ -294,12 +312,12 @@ func (h *ChannelHandler) CreateChannel(c *gin.Context) {
INSERT INTO channels (user_id, title, type, description, model, system_prompt, provider_config_id, folder, tags)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, user_id, title, type, description, model, provider_config_id, system_prompt,
is_archived, is_pinned, folder, tags, settings, created_at, updated_at
is_archived, is_pinned, folder, project_id, tags, settings, created_at, updated_at
`, userID, req.Title, channelType, req.Description, req.Model, req.SystemPrompt, req.APIConfigID,
req.Folder, pq.Array(req.Tags),
).Scan(
&ch.ID, &ch.UserID, &ch.Title, &ch.Type, &ch.Description, &ch.Model, &ch.APIConfigID,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder, &ch.ProjectID,
pq.Array(&tags), &ch.Settings, &ch.CreatedAt, &ch.UpdatedAt,
)
if err != nil {
@@ -359,7 +377,8 @@ func (h *ChannelHandler) GetChannel(c *gin.Context) {
var tags []string
err := database.DB.QueryRow(database.Q(`
SELECT c.id, c.user_id, c.title, c.type, c.description, c.model, c.provider_config_id,
c.system_prompt, c.is_archived, c.is_pinned, c.folder, c.tags, c.settings,
c.system_prompt, c.is_archived, c.is_pinned, c.folder, c.project_id,
c.tags, c.settings,
COALESCE(mc.cnt, 0) AS message_count,
c.created_at, c.updated_at
FROM channels c
@@ -369,7 +388,7 @@ func (h *ChannelHandler) GetChannel(c *gin.Context) {
WHERE c.id = $1 AND c.user_id = $2
`), channelID, userID).Scan(
&ch.ID, &ch.UserID, &ch.Title, &ch.Type, &ch.Description, &ch.Model, &ch.APIConfigID,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder,
&ch.SystemPrompt, &ch.IsArchived, &ch.IsPinned, &ch.Folder, &ch.ProjectID,
scanTags(&tags), scanJSON(&ch.Settings),
&ch.MessageCount, &ch.CreatedAt, &ch.UpdatedAt,
)

View File

@@ -851,6 +851,25 @@ func BuildKBHint(ctx context.Context, stores store.Stores, channelID, userID, pe
}
}
// Project-bound KBs (v0.19.0)
if stores.Projects != nil {
projID, _ := stores.Projects.GetProjectIDForChannel(ctx, channelID)
if projID != "" {
projKBIDs, projErr := stores.Projects.GetKBIDs(ctx, projID)
if projErr == nil {
for _, kbID := range projKBIDs {
if !seen[kbID] {
kb, kbErr := stores.KnowledgeBases.GetByID(ctx, kbID)
if kbErr == nil && kb.ChunkCount > 0 {
kbs = append(kbs, kbInfo{Name: kb.Name, DocCount: kb.DocumentCount})
seen[kbID] = true
}
}
}
}
}
}
// Channel-linked KBs
channelKBs, err := stores.KnowledgeBases.GetChannelKBs(ctx, channelID)
if err == nil {

View File

@@ -164,6 +164,15 @@ func (h *NoteHandler) Create(c *gin.Context) {
h.stores.NoteLinks.ResolveByTitle(c.Request.Context(), userID, note.ID, note.Title)
}
// Auto-associate note with source channel's project (v0.19.0)
if req.SourceChannelID != "" && h.stores.Projects != nil {
projID, _ := h.stores.Projects.GetProjectIDForChannel(
c.Request.Context(), req.SourceChannelID)
if projID != "" {
_ = h.stores.Projects.AddNote(c.Request.Context(), projID, note.ID)
}
}
c.JSON(http.StatusCreated, toNoteResponse(note))
}

471
server/handlers/projects.go Normal file
View File

@@ -0,0 +1,471 @@
package handlers
import (
"database/sql"
"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"
)
// ── Request / Response Types ────────────────
type createProjectRequest struct {
Name string `json:"name" binding:"required,max=200"`
Description string `json:"description,omitempty"`
Color *string `json:"color,omitempty"`
Icon *string `json:"icon,omitempty"`
}
type updateProjectRequest = models.ProjectPatch
type addChannelRequest struct {
ChannelID string `json:"channel_id" binding:"required"`
Position int `json:"position"`
}
type reorderChannelsRequest struct {
ChannelIDs []string `json:"channel_ids" binding:"required"`
}
type addKBRequest struct {
KBID string `json:"kb_id" binding:"required"`
AutoSearch bool `json:"auto_search"`
}
type addNoteRequest struct {
NoteID string `json:"note_id" binding:"required"`
}
// ProjectHandler handles project CRUD and associations.
type ProjectHandler struct {
stores store.Stores
}
// NewProjectHandler creates a new project handler.
func NewProjectHandler(s store.Stores) *ProjectHandler {
return &ProjectHandler{stores: s}
}
// ── CRUD ────────────────────────────────────
func (h *ProjectHandler) List(c *gin.Context) {
userID := getUserID(c)
teamIDs, _ := h.stores.Teams.GetUserTeamIDs(c.Request.Context(), userID)
includeArchived := c.Query("include_archived") == "true"
projects, err := h.stores.Projects.ListForUser(c.Request.Context(), userID, teamIDs, includeArchived)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list projects"})
return
}
if projects == nil {
projects = []models.Project{}
}
c.JSON(http.StatusOK, gin.H{"data": projects})
}
func (h *ProjectHandler) Create(c *gin.Context) {
userID := getUserID(c)
var req createProjectRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
p := &models.Project{
Name: req.Name,
Description: req.Description,
Color: req.Color,
Icon: req.Icon,
Scope: models.ScopePersonal,
OwnerID: userID,
}
if err := h.stores.Projects.Create(c.Request.Context(), p); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create project"})
return
}
c.JSON(http.StatusCreated, p)
}
func (h *ProjectHandler) Get(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
c.JSON(http.StatusOK, project)
}
func (h *ProjectHandler) Update(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
var req updateProjectRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.stores.Projects.Update(c.Request.Context(), project.ID, req); err != nil {
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "project not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update project"})
return
}
// Return refreshed project
updated, err := h.stores.Projects.GetByID(c.Request.Context(), project.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to reload project"})
return
}
c.JSON(http.StatusOK, updated)
}
func (h *ProjectHandler) Delete(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
// Only owner or admin can delete
userID := getUserID(c)
if project.OwnerID != userID {
role, _ := c.Get("role")
if role != "admin" {
c.JSON(http.StatusForbidden, gin.H{"error": "only the project owner can delete it"})
return
}
}
if err := h.stores.Projects.Delete(c.Request.Context(), project.ID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete project"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "project deleted"})
}
// ── Channel Association ─────────────────────
func (h *ProjectHandler) AddChannel(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
var req addChannelRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Verify the user owns the channel
userID := getUserID(c)
owns, err := h.stores.Channels.UserOwns(c.Request.Context(), req.ChannelID, userID)
if err != nil || !owns {
c.JSON(http.StatusForbidden, gin.H{"error": "channel not found or not owned"})
return
}
if err := h.stores.Projects.AddChannel(c.Request.Context(), project.ID, req.ChannelID, req.Position); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to add channel"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "channel added"})
}
func (h *ProjectHandler) RemoveChannel(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
channelID := c.Param("channelId")
if channelID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "channel_id required"})
return
}
if err := h.stores.Projects.RemoveChannel(c.Request.Context(), project.ID, channelID); err != nil {
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "channel not in project"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to remove channel"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "channel removed"})
}
func (h *ProjectHandler) ListChannels(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
channels, err := h.stores.Projects.ListChannels(c.Request.Context(), project.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list channels"})
return
}
if channels == nil {
channels = []models.ProjectChannel{}
}
c.JSON(http.StatusOK, gin.H{"data": channels})
}
func (h *ProjectHandler) ReorderChannels(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
var req reorderChannelsRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.stores.Projects.ReorderChannels(c.Request.Context(), project.ID, req.ChannelIDs); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to reorder channels"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "channels reordered"})
}
// ── KB Association ──────────────────────────
func (h *ProjectHandler) AddKB(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
var req addKBRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.stores.Projects.AddKB(c.Request.Context(), project.ID, req.KBID, req.AutoSearch); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to add KB"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "KB added"})
}
func (h *ProjectHandler) RemoveKB(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
kbID := c.Param("kbId")
if kbID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "kb_id required"})
return
}
if err := h.stores.Projects.RemoveKB(c.Request.Context(), project.ID, kbID); err != nil {
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "KB not in project"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to remove KB"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "KB removed"})
}
func (h *ProjectHandler) ListKBs(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
kbs, err := h.stores.Projects.ListKBs(c.Request.Context(), project.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list KBs"})
return
}
if kbs == nil {
kbs = []models.ProjectKB{}
}
c.JSON(http.StatusOK, gin.H{"data": kbs})
}
// ── Note Association ────────────────────────
func (h *ProjectHandler) AddNote(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
var req addNoteRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.stores.Projects.AddNote(c.Request.Context(), project.ID, req.NoteID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to add note"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "note added"})
}
func (h *ProjectHandler) RemoveNote(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
noteID := c.Param("noteId")
if noteID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "note_id required"})
return
}
if err := h.stores.Projects.RemoveNote(c.Request.Context(), project.ID, noteID); err != nil {
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "note not in project"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to remove note"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "note removed"})
}
func (h *ProjectHandler) ListNotes(c *gin.Context) {
project, ok := h.loadAndAuthorize(c)
if !ok {
return
}
notes, err := h.stores.Projects.ListNotes(c.Request.Context(), project.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list notes"})
return
}
if notes == nil {
notes = []models.ProjectNote{}
}
c.JSON(http.StatusOK, gin.H{"data": notes})
}
// ── Auth Helper ─────────────────────────────
// ── Admin ────────────────────────────────────
// AdminList returns all projects (no scope filtering). Admin only.
func (h *ProjectHandler) AdminList(c *gin.Context) {
ctx := c.Request.Context()
includeArchived := c.Query("include_archived") == "true"
// Admin sees everything — query with no user scope
rows, err := database.DB.QueryContext(ctx, database.Q(`
SELECT p.id, p.name, p.description, p.scope,
p.owner_id, p.team_id, p.is_archived,
p.created_at, p.updated_at,
(SELECT COUNT(*) FROM project_channels WHERE project_id = p.id),
(SELECT COUNT(*) FROM project_knowledge_bases WHERE project_id = p.id),
(SELECT COUNT(*) FROM project_notes WHERE project_id = p.id),
u.username
FROM projects p
LEFT JOIN users u ON u.id = p.owner_id
WHERE ($1 OR p.is_archived = false)
ORDER BY p.updated_at DESC`), includeArchived)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list projects"})
return
}
defer rows.Close()
type adminProject struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Scope string `json:"scope"`
OwnerID string `json:"owner_id"`
TeamID *string `json:"team_id,omitempty"`
IsArchived bool `json:"is_archived"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ChannelCount int `json:"channel_count"`
KBCount int `json:"kb_count"`
NoteCount int `json:"note_count"`
OwnerName string `json:"owner_name"`
}
var projects []adminProject
for rows.Next() {
var p adminProject
if err := rows.Scan(
&p.ID, &p.Name, &p.Description, &p.Scope,
&p.OwnerID, &p.TeamID, &p.IsArchived,
&p.CreatedAt, &p.UpdatedAt,
&p.ChannelCount, &p.KBCount, &p.NoteCount,
&p.OwnerName,
); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "scan error"})
return
}
projects = append(projects, p)
}
if projects == nil {
projects = []adminProject{}
}
c.JSON(http.StatusOK, gin.H{"data": projects})
}
// ── Auth Helper ─────────────────────────────
// loadAndAuthorize loads the project by :id param and checks user access.
// Returns (project, true) on success, or writes an error response and returns (nil, false).
func (h *ProjectHandler) loadAndAuthorize(c *gin.Context) (*models.Project, bool) {
projectID := c.Param("id")
if projectID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "project id required"})
return nil, false
}
userID := getUserID(c)
teamIDs, _ := h.stores.Teams.GetUserTeamIDs(c.Request.Context(), userID)
ok, err := h.stores.Projects.UserCanAccess(c.Request.Context(), userID, projectID, teamIDs)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "access check failed"})
return nil, false
}
if !ok {
c.JSON(http.StatusNotFound, gin.H{"error": "project not found"})
return nil, false
}
project, err := h.stores.Projects.GetByID(c.Request.Context(), projectID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load project"})
return nil, false
}
return project, true
}