Changeset 0.29.0 (#195)
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/database"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/store"
|
||||
)
|
||||
|
||||
@@ -66,34 +65,10 @@ func (t *conversationSearchTool) Execute(ctx context.Context, execCtx ExecutionC
|
||||
args.MaxResults = 5
|
||||
}
|
||||
|
||||
// Build query — full-text search scoped to this channel
|
||||
// Uses on-the-fly to_tsvector (no index needed — channel-scoped is bounded)
|
||||
roleClause := ""
|
||||
queryArgs := []interface{}{execCtx.ChannelID, args.Query, args.MaxResults}
|
||||
if args.RoleFilter == "user" || args.RoleFilter == "assistant" {
|
||||
roleClause = "AND m.role = $4"
|
||||
queryArgs = append(queryArgs, args.RoleFilter)
|
||||
}
|
||||
|
||||
rows, err := database.DB.QueryContext(ctx, fmt.Sprintf(`
|
||||
SELECT m.id, m.role,
|
||||
ts_headline('english', m.content, plainto_tsquery('english', $2),
|
||||
'MaxWords=60, MinWords=20, StartSel=**, StopSel=**') AS headline,
|
||||
ts_rank(to_tsvector('english', m.content), plainto_tsquery('english', $2)) AS rank,
|
||||
m.created_at
|
||||
FROM messages m
|
||||
WHERE m.channel_id = $1
|
||||
AND m.deleted_at IS NULL
|
||||
AND m.role IN ('user', 'assistant')
|
||||
AND to_tsvector('english', m.content) @@ plainto_tsquery('english', $2)
|
||||
%s
|
||||
ORDER BY rank DESC, m.created_at DESC
|
||||
LIMIT $3
|
||||
`, roleClause), queryArgs...)
|
||||
storeResults, err := t.stores.Messages.SearchInChannel(ctx, execCtx.ChannelID, args.Query, args.RoleFilter, args.MaxResults)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("search failed: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
type searchResult struct {
|
||||
MessageID string `json:"message_id"`
|
||||
@@ -103,17 +78,15 @@ func (t *conversationSearchTool) Execute(ctx context.Context, execCtx ExecutionC
|
||||
Timestamp string `json:"timestamp"`
|
||||
}
|
||||
|
||||
results := make([]searchResult, 0)
|
||||
for rows.Next() {
|
||||
var r searchResult
|
||||
var rank float64
|
||||
var createdAt time.Time
|
||||
if err := rows.Scan(&r.MessageID, &r.Role, &r.Excerpt, &rank, &createdAt); err != nil {
|
||||
continue
|
||||
}
|
||||
r.Rank = rank
|
||||
r.Timestamp = createdAt.Format(time.RFC3339)
|
||||
results = append(results, r)
|
||||
results := make([]searchResult, 0, len(storeResults))
|
||||
for _, sr := range storeResults {
|
||||
results = append(results, searchResult{
|
||||
MessageID: sr.MessageID,
|
||||
Role: sr.Role,
|
||||
Excerpt: sr.Excerpt,
|
||||
Rank: sr.Rank,
|
||||
Timestamp: sr.Timestamp.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
log.Printf("🔍 conversation_search: %q → %d results in channel %s",
|
||||
|
||||
Reference in New Issue
Block a user