Changeset 0.29.0 (#195)

This commit is contained in:
2026-03-17 16:28:47 +00:00
parent 128cbb8174
commit 5d637d3a90
129 changed files with 9418 additions and 3016 deletions

View File

@@ -8,7 +8,6 @@ import (
"strings"
"time"
"git.gobha.me/xcaliber/chat-switchboard/database"
"git.gobha.me/xcaliber/chat-switchboard/models"
"git.gobha.me/xcaliber/chat-switchboard/providers"
"git.gobha.me/xcaliber/chat-switchboard/roles"
@@ -173,16 +172,22 @@ Write the summary in a way that would allow the conversation to continue natural
metaJSON, _ := json.Marshal(metadata)
siblingIdx := treepath.NextSiblingIndex(req.ChannelID, &lastMessageID)
var summaryMsgID string
err = database.DB.QueryRow(`
INSERT INTO messages (channel_id, parent_id, role, content, model, metadata, sibling_index, participant_type)
VALUES ($1, $2, 'assistant', $3, $4, $5, $6, 'system')
RETURNING id
`, req.ChannelID, lastMessageID, result.Content, result.Model, string(metaJSON), siblingIdx).Scan(&summaryMsgID)
if err != nil {
summaryMsg := &models.Message{
ChannelID: req.ChannelID,
ParentID: &lastMessageID,
Role: "assistant",
Content: result.Content,
Model: result.Model,
Metadata: models.JSONMap{},
SiblingIndex: siblingIdx,
ParticipantType: "system",
}
_ = json.Unmarshal(metaJSON, &summaryMsg.Metadata)
if err := s.stores.Messages.Create(ctx, summaryMsg); err != nil {
log.Printf("⚠ Failed to persist summary for channel %s: %v", req.ChannelID, err)
return nil, fmt.Errorf("failed to save summary: %w", err)
}
summaryMsgID := summaryMsg.ID
// Update cursor to point to the summary node
if err := treepath.UpdateCursor(req.ChannelID, req.UserID, summaryMsgID); err != nil {
@@ -268,11 +273,8 @@ func (s *Service) CheckRateLimit(ctx context.Context, userID string) error {
// GetUserTeamID returns the user's first team ID (for role resolution).
func (s *Service) GetUserTeamID(ctx context.Context, userID string) *string {
var teamID string
err := database.DB.QueryRow(`
SELECT team_id FROM team_members WHERE user_id = $1 LIMIT 1
`, userID).Scan(&teamID)
if err != nil || teamID == "" {
teamID, _ := s.stores.Teams.GetFirstTeamIDForUser(ctx, userID)
if teamID == "" {
return nil
}
return &teamID

View File

@@ -2,12 +2,10 @@ package compaction
import (
"context"
"encoding/json"
"log"
"sync"
"time"
"git.gobha.me/xcaliber/chat-switchboard/database"
"git.gobha.me/xcaliber/chat-switchboard/models"
"git.gobha.me/xcaliber/chat-switchboard/roles"
"git.gobha.me/xcaliber/chat-switchboard/store"
@@ -236,47 +234,12 @@ func (sc *Scanner) findCandidates(ctx context.Context) []models.Channel {
activityGap := time.Now().Add(-candidateActivityGap)
maxAge := time.Now().Add(-candidateMaxAge)
rows, err := database.DB.QueryContext(ctx, `
SELECT c.id, c.user_id, COALESCE(c.model, ''), COALESCE(c.settings::text, '{}'),
COUNT(m.id) AS msg_count,
COALESCE(SUM(LENGTH(m.content)), 0) AS total_chars
FROM channels c
JOIN messages m ON m.channel_id = c.id AND m.deleted_at IS NULL
WHERE c.type = 'direct'
AND c.is_archived = false
AND c.updated_at < $1
AND c.updated_at > $2
GROUP BY c.id
HAVING COUNT(m.id) >= $3
AND COALESCE(SUM(LENGTH(m.content)), 0) > $4
ORDER BY c.updated_at DESC
LIMIT $5
`, activityGap,
maxAge,
candidateMinMessages,
candidateMinChars,
candidateBatchSize,
)
candidates, err := sc.stores.Channels.FindCompactionCandidates(ctx,
activityGap, maxAge, candidateMinMessages, candidateMinChars, candidateBatchSize)
if err != nil {
log.Printf("⚠ compaction: candidate query failed: %v", err)
return nil
}
defer rows.Close()
var candidates []models.Channel
for rows.Next() {
var ch models.Channel
var settingsRaw string
var msgCount, totalChars int
if err := rows.Scan(&ch.ID, &ch.UserID, &ch.Model, &settingsRaw, &msgCount, &totalChars); err != nil {
log.Printf("⚠ compaction: candidate scan: %v", err)
continue
}
if settingsRaw != "" {
_ = json.Unmarshal([]byte(settingsRaw), &ch.Settings)
}
candidates = append(candidates, ch)
}
return candidates
}