diff --git a/packages/agent/src/activities/agent.ts b/packages/agent/src/activities/agent.ts index 58f55956..90d7d3dd 100644 --- a/packages/agent/src/activities/agent.ts +++ b/packages/agent/src/activities/agent.ts @@ -824,7 +824,7 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams): }) }, async () => prisma.asset.count({ where }), - { first: limit, after, includeCount: true }, + { first: limit, after }, ) // return asset id, asset name, asset type, asset size (sizeByte or size) diff --git a/packages/core/src/collection/collection.test.ts b/packages/core/src/collection/collection.test.ts index cc61942e..55cdd525 100644 --- a/packages/core/src/collection/collection.test.ts +++ b/packages/core/src/collection/collection.test.ts @@ -99,7 +99,6 @@ describe('CollectionService', () => { // List first page const list1 = await collectionService.listCollections(project.id, { first: 2, - includeCount: true, }) expect(list1.data.length).toBe(2) expect(list1.pageInfo.total).toBe(5) diff --git a/packages/core/src/notification/notification.ts b/packages/core/src/notification/notification.ts index e339565d..f2be2293 100644 --- a/packages/core/src/notification/notification.ts +++ b/packages/core/src/notification/notification.ts @@ -283,7 +283,6 @@ export class NotificationService { { after: params.after, first: params.pageSize, - includeCount: false, }, ) } diff --git a/packages/core/src/pagination.ts b/packages/core/src/pagination.ts index 2da1b758..011b1d95 100644 --- a/packages/core/src/pagination.ts +++ b/packages/core/src/pagination.ts @@ -8,7 +8,6 @@ const key = new Uint8Array([ export interface PaginationParams { first?: number after?: string - includeCount?: boolean } export interface PageInfo { @@ -41,7 +40,7 @@ export async function paginateQuery( ): Promise> { const info: PageInfo = {} - if (params.includeCount && countFn) { + if (countFn) { info.total = await countFn() } diff --git a/packages/core/src/search/search.ts b/packages/core/src/search/search.ts index c7294404..f0c66241 100644 --- a/packages/core/src/search/search.ts +++ b/packages/core/src/search/search.ts @@ -154,43 +154,41 @@ export class SearchService { } const pageInfo: PageInfo = {} - if (req.includeCount) { - const countBuilder = new SqlQueryBuilder() - .select(Prisma.sql`COUNT(*)`) - .from(Prisma.sql`assets a JOIN asset_embeddings ae ON a.id = ae.asset_id`) - .addWhere(Prisma.sql`a.is_deleted = false`) + const countBuilder = new SqlQueryBuilder() + .select(Prisma.sql`COUNT(*)`) + .from(Prisma.sql`assets a JOIN asset_embeddings ae ON a.id = ae.asset_id`) + .addWhere(Prisma.sql`a.is_deleted = false`) - if (targetFolderIds.length > 0) { - countBuilder.addWhere(Prisma.sql`a.parent_id = ANY(${targetFolderIds})`) - } - if (req.showSymlink) { - countBuilder.addWhere(Prisma.sql` - (a.type = ANY(${targetTypes}::"AssetType"[]) OR (a.type = 'symlink' AND a.target_id IN (SELECT id FROM assets WHERE type = ANY(${targetTypes}::"AssetType"[])))) - `) - } else { - countBuilder.addWhere(Prisma.sql`a.type = ANY(${targetTypes}::"AssetType"[])`) - } - - if (req.conditions && req.conditions.length > 0) { - countBuilder.addSearchConditions(req.operator, req.conditions, { skipNameContains: true }) - } - - if (nameCond) { - const valStr = String(nameCond.value) - const ngrams = generateSearchNgrams(valStr) - if (ngrams.length > 0) { - countBuilder.addWhere(Prisma.sql`a.name_ngram @> ${ngrams}::text[]`) - countBuilder.addWhere(Prisma.sql`a.name ILIKE ${'%' + valStr + '%'}`) - } else { - countBuilder.addWhere(Prisma.sql`a.name ILIKE ${'%' + valStr + '%'}`) - } - } - - const countRes = await this.prismaClient.$queryRaw<{ count: bigint }[]>( - countBuilder.build(), - ) - pageInfo.total = Number(countRes[0]?.count || 0) + if (targetFolderIds.length > 0) { + countBuilder.addWhere(Prisma.sql`a.parent_id = ANY(${targetFolderIds})`) } + if (req.showSymlink) { + countBuilder.addWhere(Prisma.sql` + (a.type = ANY(${targetTypes}::"AssetType"[]) OR (a.type = 'symlink' AND a.target_id IN (SELECT id FROM assets WHERE type = ANY(${targetTypes}::"AssetType"[])))) + `) + } else { + countBuilder.addWhere(Prisma.sql`a.type = ANY(${targetTypes}::"AssetType"[])`) + } + + if (req.conditions && req.conditions.length > 0) { + countBuilder.addSearchConditions(req.operator, req.conditions, { skipNameContains: true }) + } + + if (nameCond) { + const valStr = String(nameCond.value) + const ngrams = generateSearchNgrams(valStr) + if (ngrams.length > 0) { + countBuilder.addWhere(Prisma.sql`a.name_ngram @> ${ngrams}::text[]`) + countBuilder.addWhere(Prisma.sql`a.name ILIKE ${'%' + valStr + '%'}`) + } else { + countBuilder.addWhere(Prisma.sql`a.name ILIKE ${'%' + valStr + '%'}`) + } + } + + const countRes = await this.prismaClient.$queryRaw<{ count: bigint }[]>( + countBuilder.build(), + ) + pageInfo.total = Number(countRes[0]?.count || 0) if (hasNextPage) { pageInfo.cursor = encodeCursor(offset + limit) @@ -342,40 +340,38 @@ export class SearchService { } const pageInfo: PageInfo = {} - if (req.includeCount) { - if (countOverride !== undefined) { - pageInfo.total = countOverride - } else { - const countBuilder = new SqlQueryBuilder() - .select(Prisma.sql`COUNT(*)`) - .from(Prisma.sql`assets a`) - .addWhere(Prisma.sql`a.is_deleted = false`) + if (countOverride !== undefined) { + pageInfo.total = countOverride + } else { + const countBuilder = new SqlQueryBuilder() + .select(Prisma.sql`COUNT(*)`) + .from(Prisma.sql`assets a`) + .addWhere(Prisma.sql`a.is_deleted = false`) - if (targetFolderIds.length > 0) { - countBuilder.addWhere(Prisma.sql`a.parent_id = ANY(${targetFolderIds})`) - } - - if (req.showSymlink) { - countBuilder.addWhere(Prisma.sql` - (a.type = ANY(${targetTypes}::"AssetType"[]) OR (a.type = 'symlink' AND a.target_id IN (SELECT id FROM assets WHERE type = ANY(${targetTypes}::"AssetType"[])))) - `) - } else { - countBuilder.addWhere(Prisma.sql`a.type = ANY(${targetTypes}::"AssetType"[])`) - } - - if (req.conditions && req.conditions.length > 0) { - countBuilder.addSearchConditions(req.operator, req.conditions, { skipNameContains: true }) - } - - if (nameCond) { - countBuilder.addWhere(Prisma.sql`a.name ILIKE ${'%' + valStr + '%'}`) - } - - const countRes = await this.prismaClient.$queryRaw<{ count: bigint }[]>( - countBuilder.build(), - ) - pageInfo.total = Number(countRes[0]?.count || 0) + if (targetFolderIds.length > 0) { + countBuilder.addWhere(Prisma.sql`a.parent_id = ANY(${targetFolderIds})`) } + + if (req.showSymlink) { + countBuilder.addWhere(Prisma.sql` + (a.type = ANY(${targetTypes}::"AssetType"[]) OR (a.type = 'symlink' AND a.target_id IN (SELECT id FROM assets WHERE type = ANY(${targetTypes}::"AssetType"[])))) + `) + } else { + countBuilder.addWhere(Prisma.sql`a.type = ANY(${targetTypes}::"AssetType"[])`) + } + + if (req.conditions && req.conditions.length > 0) { + countBuilder.addSearchConditions(req.operator, req.conditions, { skipNameContains: true }) + } + + if (nameCond) { + countBuilder.addWhere(Prisma.sql`a.name ILIKE ${'%' + valStr + '%'}`) + } + + const countRes = await this.prismaClient.$queryRaw<{ count: bigint }[]>( + countBuilder.build(), + ) + pageInfo.total = Number(countRes[0]?.count || 0) } if (hasNextPage) { diff --git a/packages/dtos/src/pagination.ts b/packages/dtos/src/pagination.ts index 331ea1fd..51867f5a 100644 --- a/packages/dtos/src/pagination.ts +++ b/packages/dtos/src/pagination.ts @@ -10,10 +10,6 @@ export type PaginationPageInfo = z.infer export const paginationParamsSchema = z.object({ first: z.coerce.number().optional(), after: z.string().optional(), - includeCount: z - .union([z.boolean(), z.string()]) - .transform((v) => v === true || v === 'true') - .optional(), }) export type PaginationParams = z.infer diff --git a/packages/webui/components/search/search-filter-dialog.tsx b/packages/webui/components/search/search-filter-dialog.tsx index fced5fb5..d8fdebd7 100644 --- a/packages/webui/components/search/search-filter-dialog.tsx +++ b/packages/webui/components/search/search-filter-dialog.tsx @@ -9,7 +9,7 @@ import { Label } from '@/ui/components/ui/label' import { Switch } from '@/ui/components/ui/switch' import { formatSize } from '@/ui/lib/format' import { cn } from '@/ui/lib/utils' -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' +import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { useNavigate } from '@tanstack/react-router' import { AlertCircle, @@ -22,6 +22,7 @@ import { X, } from 'lucide-react' import { useEffect, useMemo, useState } from 'react' +import { useInView } from 'react-intersection-observer' import { toast } from 'sonner' import { FilterPanel } from './filter-panel' @@ -155,13 +156,18 @@ export function SearchFilterDialog({ }, }) + const { ref, inView } = useInView() + const { data: searchResults, isLoading, + isFetchingNextPage, + fetchNextPage, + hasNextPage, error: searchError, - } = useQuery({ + } = useInfiniteQuery({ queryKey: ['search-preview', teamId, assetId, combinedConditions, isSemantic, triggerQuery], - queryFn: async () => { + queryFn: async ({ pageParam }) => { const activeText = triggerQuery const res = await client.api.folders[':folderId'].search.$post({ param: { folderId: assetId }, @@ -172,19 +178,39 @@ export function SearchFilterDialog({ query: activeText.trim(), isSemantic: isSemantic, first: 20, + after: pageParam as string | undefined, }, }) if (!res.ok) { const errorData = (await res.json().catch(() => ({}))) as Record throw new Error((errorData.message as string) || 'Search failed') } - return (await res.json()) as { data: AssetInfo[] } + return (await res.json()) as { data: AssetInfo[]; pageInfo?: { total?: number; cursor?: string } } }, enabled: open && hasActiveCriteria, staleTime: 500, retry: false, + initialPageParam: '', + getNextPageParam: (lastPage) => lastPage.pageInfo?.cursor || undefined, }) + useEffect(() => { + if (inView && hasNextPage && !isFetchingNextPage) { + fetchNextPage() + } + }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]) + + const flattenedResults = useMemo(() => { + return searchResults?.pages.flatMap((page) => page.data) || [] + }, [searchResults]) + + const totalCount = searchResults?.pages[0]?.pageInfo?.total || 0 + + const formatMatchedCount = (count: number) => { + if (count === 10001) return '10000+' + return count.toString() + } + const handleApply = () => { onApply(combinedConditions) onOpenChange(false) @@ -395,7 +421,7 @@ export function SearchFilterDialog({ )) - ) : searchResults?.data?.length === 0 ? ( + ) : flattenedResults.length === 0 ? (

@@ -406,62 +432,69 @@ export function SearchFilterDialog({

) : ( - searchResults?.data?.map((record) => ( -
handleResultClick(record)} - className="p-3.5 flex items-center gap-3 hover:bg-muted/40 transition-colors group/record cursor-pointer" - > + <> + {flattenedResults.map((record) => (
handleResultClick(record)} + className="p-3.5 flex items-center gap-3 hover:bg-muted/40 transition-colors group/record cursor-pointer" > - {record.preview?.thumbnailUrl ? ( - - ) : record.type === 'folder' ? ( - - ) : ( - - )} -
+
+ {record.preview?.thumbnailUrl ? ( + + ) : record.type === 'folder' ? ( + + ) : ( + + )} +
-
-
-
- - {record.name} +
+
+
+ + {record.name} + + + {record.type === 'folder' + ? 'Folder' + : `Size: ${formatSize(record.sizeByte || 0)}`} + + {record.startTime !== undefined && + record.startTime !== null && + record.endTime !== undefined && + record.endTime !== null && ( + + Match found at {formatTimestamp(record.startTime)} -{' '} + {formatTimestamp(record.endTime)} + + )} +
+ + {new Date(record.createdAt).toLocaleDateString(undefined, { + year: 'numeric', + month: 'short', + })} - - {record.type === 'folder' - ? 'Folder' - : `Size: ${formatSize(record.sizeByte || 0)}`} - - {record.startTime !== undefined && - record.startTime !== null && - record.endTime !== undefined && - record.endTime !== null && ( - - Match found at {formatTimestamp(record.startTime)} -{' '} - {formatTimestamp(record.endTime)} - - )}
- - {new Date(record.createdAt).toLocaleDateString(undefined, { - year: 'numeric', - month: 'short', - })} -
-
- )) + ))} + {(hasNextPage || isFetchingNextPage) && ( +
+ +
+ )} + )}
@@ -472,7 +505,7 @@ export function SearchFilterDialog({ Matched{' '} - {searchResults?.data?.length || 0} + {formatMatchedCount(totalCount)} {' '} items