2026-05-19 16:05:09 +08:00
import { prisma } from '@/db'
2026-05-28 20:14:11 +08:00
import { Prisma , AssetType , WorkflowTaskType } from '@/generated/prisma/client.ts'
2026-05-19 16:05:09 +08:00
import { AssetService , assetService } from '@/services/asset/asset'
import { AssetInfo } from '@/dtos/asset'
import { SearchRequest } from '@/dtos/search'
2026-05-29 11:50:03 +08:00
import { PaginatedData , decodeCursor , encodeCursor , PageInfo } from '@/services/pagination'
2026-05-19 16:05:09 +08:00
import { generateSearchNgrams } from '@/utils/ngram'
2026-05-28 20:14:11 +08:00
import { workflowService } from '@/workflow/workflow'
import { HTTPException } from 'hono/http-exception'
2026-05-29 11:00:23 +08:00
import { SqlQueryBuilder } from './sql-query-builder'
2026-05-19 16:05:09 +08:00
export class SearchService {
constructor (
private readonly prismaClient : typeof prisma = prisma ,
private readonly assetSvc : AssetService = assetService ,
) {}
async search ( folderId : string , req : SearchRequest ) : Promise < PaginatedData < AssetInfo [] >> {
let targetFolderIds : string [] = []
if ( req . recursively === false ) {
targetFolderIds = [ folderId ]
} else {
targetFolderIds = await this . assetSvc . getDescendantFolderIds ( folderId )
}
2026-05-22 15:39:46 +08:00
const targetTypes =
req . assetType === 'folder' ? [ AssetType . folder ] : [ AssetType . file , AssetType . version_stack ]
2026-05-19 16:05:09 +08:00
2026-05-28 20:14:11 +08:00
// ----------------------------------------------------------------------
// AI Semantic search
// ----------------------------------------------------------------------
2026-05-29 11:00:23 +08:00
if ( req . query && req . isSemantic ) {
2026-05-28 20:14:11 +08:00
// 1. Check if embedding agent exists and is enabled
const team = await this . prismaClient . asset . findUnique ({
where : { id : folderId },
select : { project : { select : { teamId : true } } },
})
const teamId = team ? . project ? . teamId
if ( ! teamId ) throw new Error ( 'Team ID not found for folder' )
const embeddingAgent = await this . prismaClient . agent . findFirst ({
where : { teamId , type : 'embedding' , enabled : true },
})
if ( ! embeddingAgent ) {
throw new HTTPException ( 422 , {
message : 'Embedding agent not configured or disabled for this team.' ,
})
}
// 2. Generate query embedding via workflow
const task = await this . prismaClient . workflowTask . create ({
data : {
type : WorkflowTaskType . query_embedding_for_search ,
teamId ,
2026-05-29 11:00:23 +08:00
assetId : folderId ,
2026-05-28 20:14:11 +08:00
payload : {
2026-05-29 11:00:23 +08:00
projectId : '' ,
2026-05-28 20:14:11 +08:00
queryEmbeddingForSearch : { text : req.query },
} as PrismaJson . WorkflowTaskPayload ,
status : 'pending' ,
},
})
const completedTask = await workflowService . executeWait ( task )
const output = completedTask . output as Record < string , unknown > | null
const queryVector = output ? . embedding as number [] | undefined
if ( ! queryVector ) {
throw new Error ( 'Failed to generate query embedding' )
}
2026-05-29 11:00:23 +08:00
// 3. Construct raw SQL using SqlQueryBuilder
2026-05-29 00:06:02 +08:00
const vectorJson = JSON . stringify ( queryVector )
2026-05-29 11:00:23 +08:00
const builder = new SqlQueryBuilder ()
. select (
Prisma . sql `a.id as "assetId", ae.start_time as "startTime", ae.end_time as "endTime", (ae.embedding <=> ${ vectorJson } ::vector) as "distance"` ,
)
. from ( Prisma . sql `assets a JOIN asset_embeddings ae ON a.id = ae.asset_id` )
. addWhere ( Prisma . sql `a.is_deleted = false` )
2026-05-28 20:14:11 +08:00
2026-05-29 11:00:23 +08:00
if ( targetFolderIds . length > 0 ) {
builder . addWhere ( Prisma . sql `a.parent_id = ANY( ${ targetFolderIds } )` )
2026-05-28 20:14:11 +08:00
}
2026-05-29 11:00:23 +08:00
if ( req . showSymlink ) {
builder . 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 {
builder . addWhere ( Prisma . sql `a.type = ANY( ${ targetTypes } ::"AssetType"[])` )
}
if ( req . conditions && req . conditions . length > 0 ) {
2026-05-29 12:11:05 +08:00
builder . addSearchConditions ( req . operator , req . conditions , { skipNameContains : true })
2026-05-29 11:00:23 +08:00
}
const nameCond = req . conditions ? . find (( c ) => c . field === 'name' && c . operator === 'contains' )
if ( nameCond ) {
const valStr = String ( nameCond . value )
const ngrams = generateSearchNgrams ( valStr )
if ( ngrams . length > 0 ) {
builder . addWhere ( Prisma . sql `a.name_ngram @> ${ ngrams } ::text[]` )
builder . addWhere ( Prisma . sql `a.name ILIKE ${ '%' + valStr + '%' } ` )
} else {
builder . addWhere ( Prisma . sql `a.name ILIKE ${ '%' + valStr + '%' } ` )
}
}
builder . orderBy ( Prisma . sql `distance ASC` )
// 4. Paginate in SQL using limit/offset
let limit = req . first || 20
if ( limit <= 0 || limit > 200 ) {
limit = 20
}
let offset = 0
if ( req . after ) {
offset = decodeCursor ( req . after )
}
builder . limit ( limit + 1 ). offset ( offset )
// 5. Execute raw SQL query
const query = builder . build ()
const semanticMatches = await this . prismaClient . $queryRaw <
{
assetId : string
startTime : number | null
endTime : number | null
distance : number
}[]
> ( query )
const hasNextPage = semanticMatches . length > limit
const finalMatches = hasNextPage ? semanticMatches . slice ( 0 , limit ) : semanticMatches
// 6. Map back to full rich metadata and return time-based duplicate segments
const uniqueIds = Array . from ( new Set ( finalMatches . map (( m ) => m . assetId )))
const fetchedInfos = await this . assetSvc . listAssetsByIds ( uniqueIds )
const assetInfosMap = new Map < string , AssetInfo >()
for ( const info of fetchedInfos ) {
assetInfosMap . set ( info . id , info )
}
const data : AssetInfo [] = []
for ( const match of finalMatches ) {
const baseInfo = assetInfosMap . get ( match . assetId )
if ( baseInfo ) {
data . push ({
... baseInfo ,
startTime : match.startTime ,
endTime : match.endTime ,
})
}
}
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` )
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 ) {
2026-05-29 12:11:05 +08:00
countBuilder . addSearchConditions ( req . operator , req . conditions , { skipNameContains : true })
2026-05-29 11:00:23 +08:00
}
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 )
}
return { data , pageInfo }
2026-05-28 20:14:11 +08:00
}
2026-05-19 16:05:09 +08:00
2026-05-29 11:50:03 +08:00
// ----------------------------------------------------------------------
// Non-semantic search (using SqlQueryBuilder)
// ----------------------------------------------------------------------
const builder = new SqlQueryBuilder ()
. select ( Prisma . sql `a.id as "assetId"` )
. from ( Prisma . sql `assets a` )
. addWhere ( Prisma . sql `a.is_deleted = false` )
if ( targetFolderIds . length > 0 ) {
builder . addWhere ( Prisma . sql `a.parent_id = ANY( ${ targetFolderIds } )` )
2026-05-19 16:05:09 +08:00
}
2026-05-29 11:50:03 +08:00
if ( req . showSymlink ) {
builder . 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 {
builder . addWhere ( Prisma . sql `a.type = ANY( ${ targetTypes } ::"AssetType"[])` )
}
if ( req . conditions && req . conditions . length > 0 ) {
2026-05-29 12:11:05 +08:00
builder . addSearchConditions ( req . operator , req . conditions , { skipNameContains : true })
2026-05-29 11:50:03 +08:00
}
// name contains n-grams / Switching Search Optimization
2026-05-19 16:05:09 +08:00
let countOverride : number | undefined
2026-05-29 11:50:03 +08:00
let useNgram = false
let valStr = ''
let ngrams : string [] = []
2026-05-19 16:05:09 +08:00
const nameCond = req . conditions ? . find (( c ) => c . field === 'name' && c . operator === 'contains' )
if ( nameCond ) {
2026-05-29 11:50:03 +08:00
valStr = String ( nameCond . value )
ngrams = generateSearchNgrams ( valStr )
2026-05-19 16:05:09 +08:00
if ( ngrams . length > 0 ) {
const PROBE_LIMIT = 10001
2026-05-29 11:50:03 +08:00
// Build SQL probe query to limit and fetch selective IDs
const probeBuilder = new SqlQueryBuilder ()
. select ( Prisma . sql `a.id` )
. from ( Prisma . sql `assets a` )
. addWhere ( Prisma . sql `a.is_deleted = false` )
if ( targetFolderIds . length > 0 ) {
probeBuilder . addWhere ( Prisma . sql `a.parent_id = ANY( ${ targetFolderIds } )` )
2026-05-19 16:05:09 +08:00
}
2026-05-29 11:50:03 +08:00
if ( req . showSymlink ) {
probeBuilder . 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 {
probeBuilder . addWhere ( Prisma . sql `a.type = ANY( ${ targetTypes } ::"AssetType"[])` )
}
if ( req . conditions && req . conditions . length > 0 ) {
2026-05-29 12:11:05 +08:00
probeBuilder . addSearchConditions ( req . operator , req . conditions , { skipNameContains : true })
2026-05-29 11:50:03 +08:00
}
probeBuilder . addWhere ( Prisma . sql `a.name_ngram @> ${ ngrams } ::text[]` )
probeBuilder . addWhere ( Prisma . sql `a.name ILIKE ${ '%' + valStr + '%' } ` )
probeBuilder . limit ( PROBE_LIMIT )
const probeMatches = await this . prismaClient . $queryRaw < { id : string }[] > (
probeBuilder . build (),
)
const probeCount = probeMatches . length
2026-05-19 16:05:09 +08:00
if ( probeCount < PROBE_LIMIT ) {
2026-05-29 11:50:03 +08:00
useNgram = true
2026-05-19 16:05:09 +08:00
countOverride = probeCount
} else {
countOverride = PROBE_LIMIT
}
2026-05-29 11:50:03 +08:00
}
}
if ( nameCond ) {
if ( useNgram && ngrams . length > 0 ) {
builder . addWhere ( Prisma . sql `a.name_ngram @> ${ ngrams } ::text[]` )
builder . addWhere ( Prisma . sql `a.name ILIKE ${ '%' + valStr + '%' } ` )
2026-05-19 16:05:09 +08:00
} else {
2026-05-29 11:50:03 +08:00
builder . addWhere ( Prisma . sql `a.name ILIKE ${ '%' + valStr + '%' } ` )
}
}
// Sorting
let orderSql = Prisma . sql `a.sort_index ASC`
if ( req . sort ) {
const direction = req . sort . order === 'desc' ? Prisma . raw ( 'DESC' ) : Prisma . raw ( 'ASC' )
if ( req . sort . field === 'custom' ) {
orderSql = Prisma . sql `a.sort_index ASC`
} else if ( req . sort . field === 'name' ) {
orderSql = Prisma . sql `a.name ${ direction } `
} else if ( req . sort . field === 'created_at' || req . sort . field === 'createdAt' ) {
orderSql = Prisma . sql `a.created_at ${ direction } `
} else if ( req . sort . field === 'size_byte' || req . sort . field === 'sizeByte' ) {
orderSql = Prisma . sql `a.size_byte ${ direction } `
} else {
orderSql = Prisma . sql `a.id DESC`
}
}
builder . orderBy ( orderSql )
// Paginate in SQL using limit/offset
let limit = req . first || 20
if ( limit <= 0 || limit > 200 ) {
limit = 20
}
let offset = 0
if ( req . after ) {
offset = decodeCursor ( req . after )
}
builder . limit ( limit + 1 ). offset ( offset )
// Execute raw SQL query
const query = builder . build ()
const matches = await this . prismaClient . $queryRaw < { assetId : string }[] > ( query )
const hasNextPage = matches . length > limit
const finalMatches = hasNextPage ? matches . slice ( 0 , limit ) : matches
// Map back to full rich metadata
const uniqueIds = Array . from ( new Set ( finalMatches . map (( m ) => m . assetId )))
const fetchedInfos = await this . assetSvc . listAssetsByIds ( uniqueIds )
const assetInfosMap = new Map < string , AssetInfo >()
for ( const info of fetchedInfos ) {
assetInfosMap . set ( info . id , info )
}
const data : AssetInfo [] = []
for ( const match of finalMatches ) {
const baseInfo = assetInfosMap . get ( match . assetId )
if ( baseInfo ) {
data . push ( baseInfo )
}
}
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 ( targetFolderIds . length > 0 ) {
countBuilder . addWhere ( Prisma . sql `a.parent_id = ANY( ${ targetFolderIds } )` )
2026-05-19 16:05:09 +08:00
}
2026-05-29 11:50:03 +08:00
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"[])` )
}
2026-05-19 16:05:09 +08:00
2026-05-29 11:50:03 +08:00
if ( req . conditions && req . conditions . length > 0 ) {
2026-05-29 12:11:05 +08:00
countBuilder . addSearchConditions ( req . operator , req . conditions , { skipNameContains : true })
2026-05-19 16:05:09 +08:00
}
2026-05-29 11:50:03 +08:00
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 )
2026-05-19 16:05:09 +08:00
}
}
2026-05-29 11:50:03 +08:00
if ( hasNextPage ) {
pageInfo . cursor = encodeCursor ( offset + limit )
2026-05-19 16:05:09 +08:00
}
2026-05-29 11:50:03 +08:00
return { data , pageInfo }
2026-05-19 16:05:09 +08:00
}
}
export const searchService = new SearchService ()