feat: connect search and project settings

This commit is contained in:
2026-07-21 17:16:45 +08:00
parent 2275d388a7
commit 000de4bcdb
14 changed files with 698 additions and 141 deletions

View File

@@ -3,133 +3,126 @@ package search
import (
"strings"
"gorm.io/gorm"
"senlinai-agent/backend/internal/models"
)
type Service struct {
db *gorm.DB
}
type Result struct {
const maxSearchResults = 50
// SearchResultDTO 是搜索接口的稳定结果,所有关联均使用公开 identity。
type SearchResultDTO struct {
Type string `json:"type"`
ID uint `json:"id"`
ProjectID uint `json:"project_id"`
ID string `json:"id"`
ProjectID string `json:"projectId"`
Title string `json:"title"`
Snippet string `json:"snippet"`
}
func NewService() *Service {
return &Service{}
func NewService(databases ...*gorm.DB) *Service {
var database *gorm.DB
if len(databases) > 0 {
database = databases[0]
}
return &Service{db: database}
}
func (s *Service) Search(userID uint, query string) ([]Result, error) {
func (s *Service) database() *gorm.DB {
if s.db != nil {
return s.db
}
return models.DBService
}
func (s *Service) Search(userID uint, query string) ([]SearchResultDTO, error) {
query = strings.TrimSpace(query)
if query == "" {
return []Result{}, nil
return []SearchResultDTO{}, nil
}
if models.DBService.Dialector.Name() == "postgres" {
if s.database().Dialector.Name() == "postgres" {
return s.searchPostgres(userID, query)
}
like := "%" + query + "%"
results := []Result{}
like := containsPattern(query)
results := []SearchResultDTO{}
var projects []models.SenlinAgentProject
if err := models.DBService.Where("owner_id = ? AND (name LIKE ? OR description LIKE ?)", userID, like, like).Find(&projects).Error; err != nil {
if err := s.database().Where("owner_id = ? AND (name LIKE ? ESCAPE '!' OR description LIKE ? ESCAPE '!')", userID, like, like).Limit(maxSearchResults).Find(&projects).Error; err != nil {
return nil, err
}
for _, project := range projects {
results = append(results, Result{Type: "project", ID: project.ID, ProjectID: project.ID, Title: project.Name, Snippet: project.Description})
results = append(results, SearchResultDTO{Type: "project", ID: project.Identity, ProjectID: project.Identity, Title: project.Name, Snippet: project.Description})
}
var tasks []models.SenlinAgentTask
if err := models.DBService.Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_tasks.project_id").
Where("senlin_agent_projects.owner_id = ? AND (senlin_agent_tasks.title LIKE ? OR senlin_agent_tasks.description LIKE ?)", userID, like, like).
if err := s.database().Select("senlin_agent_tasks.*").Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_tasks.project_id").
Where("(senlin_agent_projects.owner_id = ? OR senlin_agent_tasks.assignee_id = ?) AND (senlin_agent_tasks.title LIKE ? ESCAPE '!' OR senlin_agent_tasks.description LIKE ? ESCAPE '!')", userID, userID, like, like).
Limit(maxSearchResults).
Find(&tasks).Error; err != nil {
return nil, err
}
for _, task := range tasks {
results = append(results, Result{Type: "task", ID: task.ID, ProjectID: task.ProjectID, Title: task.Title, Snippet: task.Description})
results = append(results, SearchResultDTO{Type: "task", ID: task.Identity, ProjectID: task.ProjectIdentity, Title: task.Title, Snippet: task.Description})
}
var notes []models.SenlinAgentNote
if err := models.DBService.Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_notes.project_id").
Where("senlin_agent_projects.owner_id = ? AND (senlin_agent_notes.title LIKE ? OR senlin_agent_notes.markdown LIKE ?)", userID, like, like).
if err := s.database().Select("senlin_agent_notes.*").Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_notes.project_id").
Where(`(senlin_agent_projects.owner_id = ? OR EXISTS (
SELECT 1 FROM senlin_agent_task_shares
JOIN senlin_agent_tasks ON senlin_agent_tasks.id = senlin_agent_task_shares.task_id
WHERE senlin_agent_task_shares.object_type = 'note'
AND senlin_agent_task_shares.object_id = senlin_agent_notes.id
AND senlin_agent_tasks.project_id = senlin_agent_notes.project_id
AND senlin_agent_tasks.assignee_id = ?
)) AND (senlin_agent_notes.title LIKE ? ESCAPE '!' OR senlin_agent_notes.markdown LIKE ? ESCAPE '!')`, userID, userID, like, like).
Limit(maxSearchResults).
Find(&notes).Error; err != nil {
return nil, err
}
for _, note := range notes {
results = append(results, Result{Type: "note", ID: note.ID, ProjectID: note.ProjectID, Title: note.Title, Snippet: note.Markdown})
results = append(results, SearchResultDTO{Type: "note", ID: note.Identity, ProjectID: note.ProjectIdentity, Title: note.Title, Snippet: note.Markdown})
}
var sources []models.SenlinAgentSource
if err := models.DBService.Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_sources.project_id").
Where("senlin_agent_projects.owner_id = ? AND (senlin_agent_sources.title LIKE ? OR senlin_agent_sources.url LIKE ? OR senlin_agent_sources.content_text LIKE ?)", userID, like, like, like).
Find(&sources).Error; err != nil {
return nil, err
}
for _, source := range sources {
results = append(results, Result{Type: "source", ID: source.ID, ProjectID: source.ProjectID, Title: source.Title, Snippet: source.ContentText})
}
var inboxItems []models.SenlinAgentInboxItem
if err := models.DBService.Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_inbox_items.project_id").
Where("senlin_agent_projects.owner_id = ? AND (senlin_agent_inbox_items.title LIKE ? OR senlin_agent_inbox_items.body LIKE ?)", userID, like, like).
Find(&inboxItems).Error; err != nil {
return nil, err
}
for _, item := range inboxItems {
results = append(results, Result{Type: "inbox", ID: item.ID, ProjectID: item.ProjectID, Title: item.Title, Snippet: item.Body})
}
var sessions []models.SenlinAgentAISession
if err := models.DBService.Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_ai_sessions.project_id").
Where("senlin_agent_projects.owner_id = ? AND (senlin_agent_ai_sessions.title LIKE ? OR senlin_agent_ai_sessions.context LIKE ?)", userID, like, like).
Find(&sessions).Error; err != nil {
return nil, err
}
for _, session := range sessions {
results = append(results, Result{Type: "ai_session", ID: session.ID, ProjectID: session.ProjectID, Title: session.Title, Snippet: session.Context})
if len(results) > maxSearchResults {
results = results[:maxSearchResults]
}
return results, nil
}
func (s *Service) searchPostgres(userID uint, query string) ([]Result, error) {
var results []Result
err := models.DBService.Raw(`
SELECT 'project' AS type, p.id, p.id AS project_id, p.name AS title, p.description AS snippet
func (s *Service) searchPostgres(userID uint, query string) ([]SearchResultDTO, error) {
var results []SearchResultDTO
like := containsPattern(query)
err := s.database().Raw(`
SELECT 'project' AS type, p.identity AS id, p.identity AS project_id, p.name AS title, p.description AS snippet
FROM senlin_agent_projects p
WHERE p.owner_id = ?
AND to_tsvector('simple', coalesce(p.name, '') || ' ' || coalesce(p.description, '')) @@ plainto_tsquery('simple', ?)
AND (coalesce(p.name, '') ILIKE ? ESCAPE '!' OR coalesce(p.description, '') ILIKE ? ESCAPE '!')
UNION ALL
SELECT 'task' AS type, t.id, t.project_id, t.title, t.description AS snippet
SELECT 'task' AS type, t.identity AS id, p.identity AS project_id, t.title, t.description AS snippet
FROM senlin_agent_tasks t
JOIN senlin_agent_projects p ON p.id = t.project_id
WHERE p.owner_id = ?
AND to_tsvector('simple', coalesce(t.title, '') || ' ' || coalesce(t.description, '')) @@ plainto_tsquery('simple', ?)
WHERE (p.owner_id = ? OR t.assignee_id = ?)
AND (coalesce(t.title, '') ILIKE ? ESCAPE '!' OR coalesce(t.description, '') ILIKE ? ESCAPE '!')
UNION ALL
SELECT 'note' AS type, n.id, n.project_id, n.title, n.markdown AS snippet
SELECT 'note' AS type, n.identity AS id, p.identity AS project_id, n.title, n.markdown AS snippet
FROM senlin_agent_notes n
JOIN senlin_agent_projects p ON p.id = n.project_id
WHERE p.owner_id = ?
AND to_tsvector('simple', coalesce(n.title, '') || ' ' || coalesce(n.markdown, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'source' AS type, s.id, s.project_id, s.title, s.content_text AS snippet
FROM senlin_agent_sources s
JOIN senlin_agent_projects p ON p.id = s.project_id
WHERE p.owner_id = ?
AND to_tsvector('simple', coalesce(s.title, '') || ' ' || coalesce(s.url, '') || ' ' || coalesce(s.content_text, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'inbox' AS type, i.id, i.project_id, i.title, i.body AS snippet
FROM senlin_agent_inbox_items i
JOIN senlin_agent_projects p ON p.id = i.project_id
WHERE p.owner_id = ?
AND to_tsvector('simple', coalesce(i.title, '') || ' ' || coalesce(i.body, '')) @@ plainto_tsquery('simple', ?)
UNION ALL
SELECT 'ai_session' AS type, a.id, a.project_id, a.title, a.context AS snippet
FROM senlin_agent_ai_sessions a
JOIN senlin_agent_projects p ON p.id = a.project_id
WHERE p.owner_id = ?
AND to_tsvector('simple', coalesce(a.title, '') || ' ' || coalesce(a.context, '')) @@ plainto_tsquery('simple', ?)
LIMIT 50
`, userID, query, userID, query, userID, query, userID, query, userID, query, userID, query).Scan(&results).Error
WHERE (p.owner_id = ? OR EXISTS (
SELECT 1 FROM senlin_agent_task_shares ts
JOIN senlin_agent_tasks t ON t.id = ts.task_id
WHERE ts.object_type = 'note'
AND ts.object_id = n.id
AND t.project_id = n.project_id
AND t.assignee_id = ?
))
AND (coalesce(n.title, '') ILIKE ? ESCAPE '!' OR coalesce(n.markdown, '') ILIKE ? ESCAPE '!')
LIMIT ?
`, userID, like, like, userID, userID, like, like, userID, userID, like, like, maxSearchResults).Scan(&results).Error
return results, err
}
func containsPattern(query string) string {
escaped := strings.NewReplacer("!", "!!", "%", "!%", "_", "!_").Replace(query)
return "%" + escaped + "%"
}