fix(search): harden authorized results and ordering
This commit is contained in:
@@ -125,6 +125,19 @@ test('FormData body leaves Content-Type unset so fetch provides the boundary', a
|
|||||||
assert.equal(requestInit.headers['Content-Type'], undefined)
|
assert.equal(requestInit.headers['Content-Type'], undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('request forwards its AbortSignal to fetch', async () => {
|
||||||
|
let requestInit
|
||||||
|
const controller = new AbortController()
|
||||||
|
globalThis.fetch = async (_url, init) => {
|
||||||
|
requestInit = init
|
||||||
|
return Response.json({ ok: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
await apiRequest('/search', { signal: controller.signal })
|
||||||
|
|
||||||
|
assert.equal(requestInit.signal, controller.signal)
|
||||||
|
})
|
||||||
|
|
||||||
test('base URL normalizer trims whitespace and all trailing slashes', () => {
|
test('base URL normalizer trims whitespace and all trailing slashes', () => {
|
||||||
assert.equal(normalizeBaseUrl(' https://host.example/// '), 'https://host.example')
|
assert.equal(normalizeBaseUrl(' https://host.example/// '), 'https://host.example')
|
||||||
})
|
})
|
||||||
|
|||||||
41
apps/web_v1/scripts/search-request-gate.test.mjs
Normal file
41
apps/web_v1/scripts/search-request-gate.test.mjs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import test from 'node:test'
|
||||||
|
import { createSearchRequestGate } from '../src/app/search-request-gate.ts'
|
||||||
|
|
||||||
|
test('starting a new request aborts and invalidates the previous request', () => {
|
||||||
|
const gate = createSearchRequestGate()
|
||||||
|
const first = gate.begin()
|
||||||
|
const second = gate.begin()
|
||||||
|
|
||||||
|
assert.equal(first.signal.aborted, true)
|
||||||
|
assert.equal(first.isCurrent(), false)
|
||||||
|
assert.equal(second.signal.aborted, false)
|
||||||
|
assert.equal(second.isCurrent(), true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('session cleanup aborts the request and suppresses its stale error', async () => {
|
||||||
|
const gate = createSearchRequestGate()
|
||||||
|
const request = gate.begin()
|
||||||
|
const surfacedErrors = []
|
||||||
|
const oldSessionFailure = Promise.reject(new Error('旧会话搜索失败')).catch((error) => {
|
||||||
|
if (request.isCurrent()) surfacedErrors.push(error.message)
|
||||||
|
})
|
||||||
|
|
||||||
|
gate.invalidate()
|
||||||
|
await oldSessionFailure
|
||||||
|
|
||||||
|
assert.equal(request.signal.aborted, true)
|
||||||
|
assert.equal(request.isCurrent(), false)
|
||||||
|
assert.deepEqual(surfacedErrors, [])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('a request after invalidation belongs to the new session generation', () => {
|
||||||
|
const gate = createSearchRequestGate()
|
||||||
|
const oldSessionRequest = gate.begin()
|
||||||
|
gate.invalidate()
|
||||||
|
const newSessionRequest = gate.begin()
|
||||||
|
|
||||||
|
assert.equal(oldSessionRequest.isCurrent(), false)
|
||||||
|
assert.equal(newSessionRequest.isCurrent(), true)
|
||||||
|
assert.equal(newSessionRequest.signal.aborted, false)
|
||||||
|
})
|
||||||
@@ -8,6 +8,7 @@ type RequestOptions = {
|
|||||||
body?: unknown
|
body?: unknown
|
||||||
token?: string
|
token?: string
|
||||||
responseType?: 'json' | 'void'
|
responseType?: 'json' | 'void'
|
||||||
|
signal?: AbortSignal
|
||||||
}
|
}
|
||||||
|
|
||||||
type ErrorEnvelope = {
|
type ErrorEnvelope = {
|
||||||
@@ -59,6 +60,7 @@ export async function apiRequest<T>(path: string, options: RequestOptions = {}):
|
|||||||
...(options.token ? { Authorization: `Bearer ${options.token}` } : {}),
|
...(options.token ? { Authorization: `Bearer ${options.token}` } : {}),
|
||||||
},
|
},
|
||||||
body: requestBody as BodyInit | undefined,
|
body: requestBody as BodyInit | undefined,
|
||||||
|
signal: options.signal,
|
||||||
})
|
})
|
||||||
} catch {
|
} catch {
|
||||||
throw new ApiError(0, 'network_error', '无法连接服务器,请检查地址和网络后重试')
|
throw new ApiError(0, 'network_error', '无法连接服务器,请检查地址和网络后重试')
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export type SearchResponseDTO = {
|
|||||||
|
|
||||||
const searchPath = '/api/v1/search'
|
const searchPath = '/api/v1/search'
|
||||||
|
|
||||||
export async function searchWorkspace(session: ApiSession, query: string) {
|
export async function searchWorkspace(session: ApiSession, query: string, signal?: AbortSignal) {
|
||||||
const search = new URLSearchParams({ q: query })
|
const search = new URLSearchParams({ q: query })
|
||||||
return apiRequest<SearchResponseDTO>(`${searchPath}?${search.toString()}`, { token: session.token })
|
return apiRequest<SearchResponseDTO>(`${searchPath}?${search.toString()}`, { token: session.token, signal })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
import type { SearchResultDTO } from '../api/search'
|
import type { SearchResultDTO } from '../api/search'
|
||||||
import { LoginPage } from '../pages/login'
|
import { LoginPage } from '../pages/login'
|
||||||
import { ProjectActionModals, type CronDraft, type ProjectActionModal, type ProjectDraft, type SourceDraft, type TaskDraft } from '../pages/projects/project-action-modals'
|
import { ProjectActionModals, type CronDraft, type ProjectActionModal, type ProjectDraft, type SourceDraft, type TaskDraft } from '../pages/projects/project-action-modals'
|
||||||
|
import { SearchResultPreview } from '../pages/projects/search-result-preview'
|
||||||
import type { ProjectSettingsUpdate } from '../pages/projects/project-sidebar'
|
import type { ProjectSettingsUpdate } from '../pages/projects/project-sidebar'
|
||||||
import { ProjectPage } from '../pages/workspace-home'
|
import { ProjectPage } from '../pages/workspace-home'
|
||||||
import type { WorkspaceTaskUpdate } from '../pages/workspace-body'
|
import type { WorkspaceTaskUpdate } from '../pages/workspace-body'
|
||||||
@@ -37,6 +38,7 @@ function App() {
|
|||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [actionLoading, setActionLoading] = useState(false)
|
const [actionLoading, setActionLoading] = useState(false)
|
||||||
const [activeModal, setActiveModal] = useState<ProjectActionModal>(null)
|
const [activeModal, setActiveModal] = useState<ProjectActionModal>(null)
|
||||||
|
const [searchResultPreview, setSearchResultPreview] = useState<SearchResultDTO | null>(null)
|
||||||
const workspaceSearch = useWorkbenchSearch(session)
|
const workspaceSearch = useWorkbenchSearch(session)
|
||||||
|
|
||||||
const dark = theme === 'dark'
|
const dark = theme === 'dark'
|
||||||
@@ -228,16 +230,20 @@ function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleSelectSearchResult(result: SearchResultDTO) {
|
function handleSelectSearchResult(result: SearchResultDTO) {
|
||||||
const owningWorkspace = workspaces.find((workspace) => workspace.project.id === result.projectId)
|
|
||||||
if (!owningWorkspace) {
|
|
||||||
Message.warning('该搜索结果所在项目当前不可访问')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const target = searchResultTarget(result.type)
|
const target = searchResultTarget(result.type)
|
||||||
if (!target) {
|
if (!target) {
|
||||||
Message.warning('该搜索结果暂不支持导航')
|
Message.warning('该搜索结果暂不支持导航')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const owningWorkspace = workspaces.find((workspace) => workspace.project.id === result.projectId)
|
||||||
|
if (!owningWorkspace) {
|
||||||
|
if (isAuthorizedExternalPreview(result)) {
|
||||||
|
setSearchResultPreview(result)
|
||||||
|
} else {
|
||||||
|
Message.warning('该搜索结果所在项目当前不可访问')
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
setActiveProjectID(owningWorkspace.project.id)
|
setActiveProjectID(owningWorkspace.project.id)
|
||||||
setActiveView('project')
|
setActiveView('project')
|
||||||
setActiveChannel(target.channel)
|
setActiveChannel(target.channel)
|
||||||
@@ -304,6 +310,7 @@ function App() {
|
|||||||
onCreateCronPlan={handleCreateCronPlan}
|
onCreateCronPlan={handleCreateCronPlan}
|
||||||
tagOptions={activeTagOptions}
|
tagOptions={activeTagOptions}
|
||||||
/>
|
/>
|
||||||
|
<SearchResultPreview result={searchResultPreview} onClose={() => setSearchResultPreview(null)} />
|
||||||
</main>
|
</main>
|
||||||
</ConfigProvider>
|
</ConfigProvider>
|
||||||
)
|
)
|
||||||
@@ -316,4 +323,15 @@ function searchResultTarget(type: string): { channel: ChannelKey; openTask: bool
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isAuthorizedExternalPreview(result: SearchResultDTO) {
|
||||||
|
return (
|
||||||
|
(result.type === 'task' || result.type === 'note')
|
||||||
|
&& typeof result.projectId === 'string'
|
||||||
|
&& result.projectId.trim() !== ''
|
||||||
|
&& typeof result.title === 'string'
|
||||||
|
&& result.title.trim() !== ''
|
||||||
|
&& typeof result.snippet === 'string'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
|||||||
29
apps/web_v1/src/app/search-request-gate.ts
Normal file
29
apps/web_v1/src/app/search-request-gate.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
export type SearchRequestTicket = {
|
||||||
|
signal: AbortSignal
|
||||||
|
isCurrent: () => boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createSearchRequestGate() {
|
||||||
|
let generation = 0
|
||||||
|
let controller: AbortController | null = null
|
||||||
|
|
||||||
|
return {
|
||||||
|
begin(): SearchRequestTicket {
|
||||||
|
generation += 1
|
||||||
|
controller?.abort()
|
||||||
|
controller = new AbortController()
|
||||||
|
const requestGeneration = generation
|
||||||
|
return {
|
||||||
|
signal: controller.signal,
|
||||||
|
isCurrent: () => requestGeneration === generation,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
invalidate() {
|
||||||
|
generation += 1
|
||||||
|
controller?.abort()
|
||||||
|
controller = null
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SearchRequestGate = ReturnType<typeof createSearchRequestGate>
|
||||||
@@ -1,49 +1,63 @@
|
|||||||
import { useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { Message } from '@arco-design/web-react'
|
import { Message } from '@arco-design/web-react'
|
||||||
import type { ApiSession } from '../api/client'
|
import type { ApiSession } from '../api/client'
|
||||||
import { searchWorkspace, type SearchResultDTO } from '../api/search'
|
import { searchWorkspace, type SearchResultDTO } from '../api/search'
|
||||||
|
import { createSearchRequestGate, type SearchRequestGate } from './search-request-gate'
|
||||||
|
|
||||||
export function useWorkbenchSearch(session: ApiSession | null) {
|
export function useWorkbenchSearch(session: ApiSession | null) {
|
||||||
const [query, setQuery] = useState('')
|
const [query, setQuery] = useState('')
|
||||||
const [results, setResults] = useState<SearchResultDTO[]>([])
|
const [results, setResults] = useState<SearchResultDTO[]>([])
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [searched, setSearched] = useState(false)
|
const [searched, setSearched] = useState(false)
|
||||||
const requestSequence = useRef(0)
|
const requestGate = useRef<SearchRequestGate | null>(null)
|
||||||
|
if (!requestGate.current) requestGate.current = createSearchRequestGate()
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const gate = requestGate.current
|
||||||
|
gate?.invalidate()
|
||||||
|
setQuery('')
|
||||||
|
setResults([])
|
||||||
|
setLoading(false)
|
||||||
|
setSearched(false)
|
||||||
|
return () => gate?.invalidate()
|
||||||
|
}, [session])
|
||||||
|
|
||||||
function changeQuery(value: string) {
|
function changeQuery(value: string) {
|
||||||
requestSequence.current += 1
|
requestGate.current?.invalidate()
|
||||||
setQuery(value)
|
setQuery(value)
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
setSearched(false)
|
setSearched(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function submitSearch() {
|
async function submitSearch() {
|
||||||
const requestID = ++requestSequence.current
|
|
||||||
const trimmedQuery = query.trim()
|
const trimmedQuery = query.trim()
|
||||||
if (!trimmedQuery) {
|
if (!trimmedQuery) {
|
||||||
|
requestGate.current?.invalidate()
|
||||||
setResults([])
|
setResults([])
|
||||||
setSearched(false)
|
setSearched(false)
|
||||||
Message.warning('请输入搜索关键词')
|
Message.warning('请输入搜索关键词')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!session) {
|
if (!session) {
|
||||||
|
requestGate.current?.invalidate()
|
||||||
Message.error('未登录或登录已失效')
|
Message.error('未登录或登录已失效')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const request = requestGate.current!.begin()
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
setSearched(false)
|
setSearched(false)
|
||||||
try {
|
try {
|
||||||
const response = await searchWorkspace(session, trimmedQuery)
|
const response = await searchWorkspace(session, trimmedQuery, request.signal)
|
||||||
if (requestID !== requestSequence.current) return
|
if (!request.isCurrent()) return
|
||||||
setResults(response.items)
|
setResults(response.items)
|
||||||
setSearched(true)
|
setSearched(true)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (requestID !== requestSequence.current) return
|
if (!request.isCurrent()) return
|
||||||
setResults([])
|
setResults([])
|
||||||
Message.error(error instanceof Error ? error.message : '搜索失败,请稍后重试')
|
Message.error(error instanceof Error ? error.message : '搜索失败,请稍后重试')
|
||||||
} finally {
|
} finally {
|
||||||
if (requestID === requestSequence.current) setLoading(false)
|
if (request.isCurrent()) setLoading(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,10 +111,11 @@ export function ProjectTopbar({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function searchTypeLabel(type: SearchResultDTO['type']) {
|
function searchTypeLabel(type: string) {
|
||||||
if (type === 'project') return '项目'
|
if (type === 'project') return '项目'
|
||||||
if (type === 'task') return '任务'
|
if (type === 'task') return '任务'
|
||||||
return '笔记'
|
if (type === 'note') return '笔记'
|
||||||
|
return '未知'
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDesktopRuntime() {
|
function isDesktopRuntime() {
|
||||||
|
|||||||
37
apps/web_v1/src/pages/projects/search-result-preview.tsx
Normal file
37
apps/web_v1/src/pages/projects/search-result-preview.tsx
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { Alert, Button, Descriptions, Modal, Space, Tag, Typography } from '@arco-design/web-react'
|
||||||
|
import type { SearchResultDTO } from '../../api/search'
|
||||||
|
|
||||||
|
const { Paragraph, Text, Title } = Typography
|
||||||
|
|
||||||
|
export function SearchResultPreview({
|
||||||
|
result,
|
||||||
|
onClose,
|
||||||
|
}: {
|
||||||
|
result: SearchResultDTO | null
|
||||||
|
onClose: () => void
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
title="授权对象预览"
|
||||||
|
visible={result !== null}
|
||||||
|
onCancel={onClose}
|
||||||
|
footer={<Button type="primary" onClick={onClose}>关闭预览</Button>}
|
||||||
|
unmountOnExit
|
||||||
|
>
|
||||||
|
{result && (
|
||||||
|
<Space direction="vertical" size={16} style={{ width: '100%' }}>
|
||||||
|
<Alert type="info" content="仅显示被授权对象,不会加载完整项目工作区。" />
|
||||||
|
<Space direction="vertical" size={8} style={{ width: '100%' }}>
|
||||||
|
<Tag color="arcoblue">{result.type === 'task' ? '任务' : '笔记'}</Tag>
|
||||||
|
<Title heading={6}>{result.title}</Title>
|
||||||
|
{result.snippet && <Paragraph>{result.snippet}</Paragraph>}
|
||||||
|
</Space>
|
||||||
|
<Descriptions
|
||||||
|
column={1}
|
||||||
|
data={[{ label: '所属项目', value: <Text>{result.projectId}</Text> }]}
|
||||||
|
/>
|
||||||
|
</Space>
|
||||||
|
)}
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
package search
|
package search
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"senlinai-agent/backend/internal/models"
|
"senlinai-agent/backend/internal/models"
|
||||||
@@ -11,7 +14,15 @@ type Service struct {
|
|||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxSearchResults = 50
|
const (
|
||||||
|
maxSearchResults = 50
|
||||||
|
maxSearchSnippetRunes = 240
|
||||||
|
)
|
||||||
|
|
||||||
|
type rankedSearchResult struct {
|
||||||
|
result SearchResultDTO
|
||||||
|
updatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
// SearchResultDTO 是搜索接口的稳定结果,所有关联均使用公开 identity。
|
// SearchResultDTO 是搜索接口的稳定结果,所有关联均使用公开 identity。
|
||||||
type SearchResultDTO struct {
|
type SearchResultDTO struct {
|
||||||
@@ -42,84 +53,110 @@ func (s *Service) Search(userID uint, query string) ([]SearchResultDTO, error) {
|
|||||||
if query == "" {
|
if query == "" {
|
||||||
return []SearchResultDTO{}, nil
|
return []SearchResultDTO{}, nil
|
||||||
}
|
}
|
||||||
if s.database().Dialector.Name() == "postgres" {
|
|
||||||
return s.searchPostgres(userID, query)
|
|
||||||
}
|
|
||||||
|
|
||||||
like := containsPattern(query)
|
like := containsPattern(query)
|
||||||
results := []SearchResultDTO{}
|
operator := "LIKE"
|
||||||
|
if s.database().Dialector.Name() == "postgres" {
|
||||||
|
operator = "ILIKE"
|
||||||
|
}
|
||||||
|
|
||||||
var projects []models.SenlinAgentProject
|
var projects []models.SenlinAgentProject
|
||||||
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 {
|
projectFilter := fmt.Sprintf("owner_id = ? AND (name %s ? ESCAPE '!' OR description %s ? ESCAPE '!')", operator, operator)
|
||||||
|
if err := s.database().Where(projectFilter, userID, like, like).
|
||||||
|
Order("updated_at DESC").Order("identity ASC").
|
||||||
|
Limit(maxSearchResults).Find(&projects).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
projectResults := make([]rankedSearchResult, 0, len(projects))
|
||||||
for _, project := range projects {
|
for _, project := range projects {
|
||||||
results = append(results, SearchResultDTO{Type: "project", ID: project.Identity, ProjectID: project.Identity, Title: project.Name, Snippet: project.Description})
|
projectResults = append(projectResults, rankedSearchResult{
|
||||||
|
result: SearchResultDTO{Type: "project", ID: project.Identity, ProjectID: project.Identity, Title: project.Name, Snippet: project.Description},
|
||||||
|
updatedAt: project.UpdatedAt,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var tasks []models.SenlinAgentTask
|
var tasks []models.SenlinAgentTask
|
||||||
|
taskFilter := fmt.Sprintf("(senlin_agent_projects.owner_id = ? OR senlin_agent_tasks.assignee_id = ?) AND (senlin_agent_tasks.title %s ? ESCAPE '!' OR senlin_agent_tasks.description %s ? ESCAPE '!')", operator, operator)
|
||||||
if err := s.database().Select("senlin_agent_tasks.*").Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_tasks.project_id").
|
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).
|
Where(taskFilter, userID, userID, like, like).
|
||||||
|
Order("senlin_agent_tasks.updated_at DESC").Order("senlin_agent_tasks.identity ASC").
|
||||||
Limit(maxSearchResults).
|
Limit(maxSearchResults).
|
||||||
Find(&tasks).Error; err != nil {
|
Find(&tasks).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
taskResults := make([]rankedSearchResult, 0, len(tasks))
|
||||||
for _, task := range tasks {
|
for _, task := range tasks {
|
||||||
results = append(results, SearchResultDTO{Type: "task", ID: task.Identity, ProjectID: task.ProjectIdentity, Title: task.Title, Snippet: task.Description})
|
taskResults = append(taskResults, rankedSearchResult{
|
||||||
|
result: SearchResultDTO{Type: "task", ID: task.Identity, ProjectID: task.ProjectIdentity, Title: task.Title, Snippet: task.Description},
|
||||||
|
updatedAt: task.UpdatedAt,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var notes []models.SenlinAgentNote
|
var notes []models.SenlinAgentNote
|
||||||
if err := s.database().Select("senlin_agent_notes.*").Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_notes.project_id").
|
noteFilter := fmt.Sprintf(`(senlin_agent_projects.owner_id = ? OR EXISTS (
|
||||||
Where(`(senlin_agent_projects.owner_id = ? OR EXISTS (
|
|
||||||
SELECT 1 FROM senlin_agent_task_shares
|
SELECT 1 FROM senlin_agent_task_shares
|
||||||
JOIN senlin_agent_tasks ON senlin_agent_tasks.id = senlin_agent_task_shares.task_id
|
JOIN senlin_agent_tasks ON senlin_agent_tasks.id = senlin_agent_task_shares.task_id
|
||||||
WHERE senlin_agent_task_shares.object_type = 'note'
|
WHERE senlin_agent_task_shares.object_type = 'note'
|
||||||
AND senlin_agent_task_shares.object_id = senlin_agent_notes.id
|
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.project_id = senlin_agent_notes.project_id
|
||||||
AND senlin_agent_tasks.assignee_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).
|
)) AND (senlin_agent_notes.title %s ? ESCAPE '!' OR senlin_agent_notes.markdown %s ? ESCAPE '!')`, operator, operator)
|
||||||
|
if err := s.database().Select("senlin_agent_notes.*").Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_notes.project_id").
|
||||||
|
Where(noteFilter, userID, userID, like, like).
|
||||||
|
Order("senlin_agent_notes.updated_at DESC").Order("senlin_agent_notes.identity ASC").
|
||||||
Limit(maxSearchResults).
|
Limit(maxSearchResults).
|
||||||
Find(¬es).Error; err != nil {
|
Find(¬es).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
noteResults := make([]rankedSearchResult, 0, len(notes))
|
||||||
for _, note := range notes {
|
for _, note := range notes {
|
||||||
results = append(results, SearchResultDTO{Type: "note", ID: note.Identity, ProjectID: note.ProjectIdentity, Title: note.Title, Snippet: note.Markdown})
|
noteResults = append(noteResults, rankedSearchResult{
|
||||||
|
result: SearchResultDTO{Type: "note", ID: note.Identity, ProjectID: note.ProjectIdentity, Title: note.Title, Snippet: note.Markdown},
|
||||||
|
updatedAt: note.UpdatedAt,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
if len(results) > maxSearchResults {
|
|
||||||
results = results[:maxSearchResults]
|
return stableFairLimit(projectResults, taskResults, noteResults), nil
|
||||||
}
|
|
||||||
return results, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) searchPostgres(userID uint, query string) ([]SearchResultDTO, error) {
|
func stableFairLimit(buckets ...[]rankedSearchResult) []SearchResultDTO {
|
||||||
var results []SearchResultDTO
|
for _, bucket := range buckets {
|
||||||
like := containsPattern(query)
|
sort.Slice(bucket, func(left, right int) bool {
|
||||||
err := s.database().Raw(`
|
if bucket[left].updatedAt.Equal(bucket[right].updatedAt) {
|
||||||
SELECT 'project' AS type, p.identity AS id, p.identity AS project_id, p.name AS title, p.description AS snippet
|
return bucket[left].result.ID < bucket[right].result.ID
|
||||||
FROM senlin_agent_projects p
|
}
|
||||||
WHERE p.owner_id = ?
|
return bucket[left].updatedAt.After(bucket[right].updatedAt)
|
||||||
AND (coalesce(p.name, '') ILIKE ? ESCAPE '!' OR coalesce(p.description, '') ILIKE ? ESCAPE '!')
|
})
|
||||||
UNION ALL
|
}
|
||||||
SELECT 'task' AS type, t.identity AS id, p.identity AS project_id, t.title, t.description AS snippet
|
|
||||||
FROM senlin_agent_tasks t
|
results := make([]SearchResultDTO, 0, maxSearchResults)
|
||||||
JOIN senlin_agent_projects p ON p.id = t.project_id
|
for position := 0; len(results) < maxSearchResults; position++ {
|
||||||
WHERE (p.owner_id = ? OR t.assignee_id = ?)
|
added := false
|
||||||
AND (coalesce(t.title, '') ILIKE ? ESCAPE '!' OR coalesce(t.description, '') ILIKE ? ESCAPE '!')
|
for _, bucket := range buckets {
|
||||||
UNION ALL
|
if position >= len(bucket) {
|
||||||
SELECT 'note' AS type, n.identity AS id, p.identity AS project_id, n.title, n.markdown AS snippet
|
continue
|
||||||
FROM senlin_agent_notes n
|
}
|
||||||
JOIN senlin_agent_projects p ON p.id = n.project_id
|
result := bucket[position].result
|
||||||
WHERE (p.owner_id = ? OR EXISTS (
|
result.Snippet = boundedSnippet(result.Snippet)
|
||||||
SELECT 1 FROM senlin_agent_task_shares ts
|
results = append(results, result)
|
||||||
JOIN senlin_agent_tasks t ON t.id = ts.task_id
|
added = true
|
||||||
WHERE ts.object_type = 'note'
|
if len(results) == maxSearchResults {
|
||||||
AND ts.object_id = n.id
|
return results
|
||||||
AND t.project_id = n.project_id
|
}
|
||||||
AND t.assignee_id = ?
|
}
|
||||||
))
|
if !added {
|
||||||
AND (coalesce(n.title, '') ILIKE ? ESCAPE '!' OR coalesce(n.markdown, '') ILIKE ? ESCAPE '!')
|
return results
|
||||||
LIMIT ?
|
}
|
||||||
`, userID, like, like, userID, userID, like, like, userID, userID, like, like, maxSearchResults).Scan(&results).Error
|
}
|
||||||
return results, err
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
func boundedSnippet(value string) string {
|
||||||
|
runes := []rune(value)
|
||||||
|
if len(runes) <= maxSearchSnippetRunes {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return string(runes[:maxSearchSnippetRunes])
|
||||||
}
|
}
|
||||||
|
|
||||||
func containsPattern(query string) string {
|
func containsPattern(query string) string {
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package search
|
package search
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
@@ -45,6 +48,65 @@ func TestPostgresSearchKeepsAssignmentAndExplicitShareBoundaries(t *testing.T) {
|
|||||||
require.ElementsMatch(t, []string{assigned.Identity, sharedNote.Identity}, []string{results[0].ID, results[1].ID})
|
require.ElementsMatch(t, []string{assigned.Identity, sharedNote.Identity}, []string{results[0].ID, results[1].ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPostgresSearchUsesStableFairLimitAndRuneBoundedSnippets(t *testing.T) {
|
||||||
|
database := newPostgresSearchTestDB(t)
|
||||||
|
owner := createSearchUser(t, database, "postgres-search-order@example.com")
|
||||||
|
baseTime := time.Date(2026, time.July, 21, 12, 0, 0, 0, time.UTC)
|
||||||
|
for index := 0; index < 60; index++ {
|
||||||
|
project := models.SenlinAgentProject{
|
||||||
|
Identity: fmt.Sprintf("00000000-0000-7001-8000-%012d", index),
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
Name: fmt.Sprintf("稳定排序项目 %02d", index),
|
||||||
|
Identifier: fmt.Sprintf("PG-SORT-%02d", index),
|
||||||
|
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||||
|
}
|
||||||
|
require.NoError(t, database.Create(&project).Error)
|
||||||
|
require.NoError(t, database.Create(&models.SenlinAgentTask{
|
||||||
|
Identity: fmt.Sprintf("00000000-0000-7002-8000-%012d", index),
|
||||||
|
ProjectID: project.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Title: fmt.Sprintf("稳定排序任务 %02d", index),
|
||||||
|
Status: "open",
|
||||||
|
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||||
|
}).Error)
|
||||||
|
require.NoError(t, database.Create(&models.SenlinAgentNote{
|
||||||
|
Identity: fmt.Sprintf("00000000-0000-7003-8000-%012d", index),
|
||||||
|
ProjectID: project.ID,
|
||||||
|
CreatedBy: owner.ID,
|
||||||
|
Title: fmt.Sprintf("稳定排序笔记 %02d", index),
|
||||||
|
Markdown: strings.Repeat("森", 300) + "尾",
|
||||||
|
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||||
|
}).Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
service := NewService(database)
|
||||||
|
results, err := service.Search(owner.ID, "稳定排序")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, results, maxSearchResults)
|
||||||
|
require.Equal(t,
|
||||||
|
[]string{"project", "task", "note", "project", "task", "note", "project", "task", "note"},
|
||||||
|
resultTypes(results[:9]),
|
||||||
|
)
|
||||||
|
require.Equal(t, "00000000-0000-7001-8000-000000000059", results[0].ID)
|
||||||
|
require.Equal(t, "00000000-0000-7002-8000-000000000059", results[1].ID)
|
||||||
|
require.Equal(t, "00000000-0000-7003-8000-000000000059", results[2].ID)
|
||||||
|
counts := map[string]int{}
|
||||||
|
for _, result := range results {
|
||||||
|
counts[result.Type]++
|
||||||
|
}
|
||||||
|
require.Equal(t, map[string]int{"project": 17, "task": 17, "note": 16}, counts)
|
||||||
|
|
||||||
|
again, err := service.Search(owner.ID, "稳定排序")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, results, again)
|
||||||
|
|
||||||
|
noteResults, err := service.Search(owner.ID, "森")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEmpty(t, noteResults)
|
||||||
|
require.Len(t, []rune(noteResults[0].Snippet), 240)
|
||||||
|
require.NotContains(t, noteResults[0].Snippet, "尾")
|
||||||
|
}
|
||||||
|
|
||||||
func newPostgresSearchTestDB(t *testing.T) *gorm.DB {
|
func newPostgresSearchTestDB(t *testing.T) *gorm.DB {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
dsn := os.Getenv("DATABASE_URL")
|
dsn := os.Getenv("DATABASE_URL")
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package search
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/glebarez/sqlite"
|
"github.com/glebarez/sqlite"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -26,6 +28,91 @@ func TestSearchFindsNoteBody(t *testing.T) {
|
|||||||
require.Equal(t, "note", results[0].Type)
|
require.Equal(t, "note", results[0].Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSearchAppliesStableFairGlobalLimitAcrossResultTypes(t *testing.T) {
|
||||||
|
database, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, models.AutoMigrate(database))
|
||||||
|
models.DBService = database
|
||||||
|
|
||||||
|
baseTime := time.Date(2026, time.July, 21, 12, 0, 0, 0, time.UTC)
|
||||||
|
for index := 0; index < 60; index++ {
|
||||||
|
project := models.SenlinAgentProject{
|
||||||
|
Identity: fmt.Sprintf("00000000-0000-7001-8000-%012d", index),
|
||||||
|
OwnerID: 7,
|
||||||
|
Name: fmt.Sprintf("稳定排序项目 %02d", index),
|
||||||
|
Identifier: fmt.Sprintf("SORT-%02d", index),
|
||||||
|
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||||
|
}
|
||||||
|
require.NoError(t, database.Create(&project).Error)
|
||||||
|
require.NoError(t, database.Create(&models.SenlinAgentTask{
|
||||||
|
Identity: fmt.Sprintf("00000000-0000-7002-8000-%012d", index),
|
||||||
|
ProjectID: project.ID,
|
||||||
|
CreatedBy: 7,
|
||||||
|
Title: fmt.Sprintf("稳定排序任务 %02d", index),
|
||||||
|
Status: "open",
|
||||||
|
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||||
|
}).Error)
|
||||||
|
require.NoError(t, database.Create(&models.SenlinAgentNote{
|
||||||
|
Identity: fmt.Sprintf("00000000-0000-7003-8000-%012d", index),
|
||||||
|
ProjectID: project.ID,
|
||||||
|
CreatedBy: 7,
|
||||||
|
Title: fmt.Sprintf("稳定排序笔记 %02d", index),
|
||||||
|
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||||
|
}).Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
service := NewService(database)
|
||||||
|
results, err := service.Search(7, "稳定排序")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, results, maxSearchResults)
|
||||||
|
|
||||||
|
counts := map[string]int{}
|
||||||
|
for _, result := range results {
|
||||||
|
counts[result.Type]++
|
||||||
|
}
|
||||||
|
require.Equal(t, map[string]int{"project": 17, "task": 17, "note": 16}, counts)
|
||||||
|
require.Equal(t,
|
||||||
|
[]string{"project", "task", "note", "project", "task", "note", "project", "task", "note"},
|
||||||
|
resultTypes(results[:9]),
|
||||||
|
)
|
||||||
|
require.Equal(t, "00000000-0000-7001-8000-000000000059", results[0].ID)
|
||||||
|
require.Equal(t, "00000000-0000-7002-8000-000000000059", results[1].ID)
|
||||||
|
require.Equal(t, "00000000-0000-7003-8000-000000000059", results[2].ID)
|
||||||
|
|
||||||
|
again, err := service.Search(7, "稳定排序")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, results, again)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchBoundsChineseSnippetsByRune(t *testing.T) {
|
||||||
|
database, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, models.AutoMigrate(database))
|
||||||
|
models.DBService = database
|
||||||
|
project := models.SenlinAgentProject{OwnerID: 7, Name: "摘要项目", Identifier: "SNIPPET"}
|
||||||
|
require.NoError(t, database.Create(&project).Error)
|
||||||
|
require.NoError(t, database.Create(&models.SenlinAgentNote{
|
||||||
|
ProjectID: project.ID,
|
||||||
|
CreatedBy: 7,
|
||||||
|
Title: "中文摘要",
|
||||||
|
Markdown: strings.Repeat("森", 300) + "尾",
|
||||||
|
}).Error)
|
||||||
|
|
||||||
|
results, err := NewService(database).Search(7, "森")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, results, 1)
|
||||||
|
require.Len(t, []rune(results[0].Snippet), 240)
|
||||||
|
require.NotContains(t, results[0].Snippet, "尾")
|
||||||
|
}
|
||||||
|
|
||||||
|
func resultTypes(results []SearchResultDTO) []string {
|
||||||
|
types := make([]string, 0, len(results))
|
||||||
|
for _, result := range results {
|
||||||
|
types = append(types, result.Type)
|
||||||
|
}
|
||||||
|
return types
|
||||||
|
}
|
||||||
|
|
||||||
func TestSearchOnlyReturnsNavigableProjectTaskAndNoteResults(t *testing.T) {
|
func TestSearchOnlyReturnsNavigableProjectTaskAndNoteResults(t *testing.T) {
|
||||||
database, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
|
database, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user