mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 15:48:20 +00:00
feat(search): add semantic search with embeddings and search mode toggle
This commit is contained in:
@@ -103,6 +103,7 @@ enum WorkflowTaskType {
|
||||
ai_metadata_autofill
|
||||
chat
|
||||
ai_embedding
|
||||
query_embedding_for_search
|
||||
}
|
||||
|
||||
enum WorkflowTaskStatus {
|
||||
|
||||
@@ -44,6 +44,9 @@ export type SearchOperator = z.infer<typeof searchOperatorSchema>
|
||||
export const searchAssetTypeSchema = z.enum(['file', 'folder'])
|
||||
export type SearchAssetType = z.infer<typeof searchAssetTypeSchema>
|
||||
|
||||
export const searchModeSchema = z.enum(['name', 'content', 'all']).optional().default('name')
|
||||
export type SearchMode = z.infer<typeof searchModeSchema>
|
||||
|
||||
export const searchFilterSchema = z.object({
|
||||
operator: searchOperatorSchema.optional().default('AND'),
|
||||
conditions: z.array(searchConditionSchema).optional().default([]),
|
||||
@@ -53,6 +56,7 @@ export const searchFilterSchema = z.object({
|
||||
showSymlink: z.boolean().optional(),
|
||||
recursively: z.boolean().optional().default(true),
|
||||
query: z.string().optional(),
|
||||
searchMode: searchModeSchema.default('name'),
|
||||
})
|
||||
export type SearchFilter = z.infer<typeof searchFilterSchema>
|
||||
|
||||
|
||||
Generated
+2
-1
@@ -128,7 +128,8 @@ export const WorkflowTaskType = {
|
||||
transcode: 'transcode',
|
||||
ai_metadata_autofill: 'ai_metadata_autofill',
|
||||
chat: 'chat',
|
||||
ai_embedding: 'ai_embedding'
|
||||
ai_embedding: 'ai_embedding',
|
||||
query_embedding_for_search: 'query_embedding_for_search'
|
||||
} as const
|
||||
|
||||
export type WorkflowTaskType = (typeof WorkflowTaskType)[keyof typeof WorkflowTaskType]
|
||||
|
||||
Generated
+1
-1
File diff suppressed because one or more lines are too long
@@ -244,6 +244,9 @@ declare global {
|
||||
projectId: string
|
||||
transcode?: TaskSpec
|
||||
agent?: AgentTaskPayload
|
||||
queryEmbeddingForSearch?: {
|
||||
text: string
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@@ -59,7 +59,7 @@ describe('AuthzService', () => {
|
||||
projectId: project.id,
|
||||
filter: {
|
||||
sourceFolderId: 'root',
|
||||
searchFilter: { conditions: [], operator: 'AND', recursively: true },
|
||||
searchFilter: { conditions: [], operator: 'AND', recursively: true, searchMode: 'name' },
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -263,7 +263,7 @@ describe('AuthzService', () => {
|
||||
projectId: project.id,
|
||||
filter: {
|
||||
sourceFolderId: 'root',
|
||||
searchFilter: { conditions: [], operator: 'AND', recursively: true },
|
||||
searchFilter: { conditions: [], operator: 'AND', recursively: true, searchMode: 'name' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -48,6 +48,7 @@ describe('CollectionService', () => {
|
||||
operator: 'AND',
|
||||
conditions: [{ field: 'name', operator: 'contains', value: 'test' }],
|
||||
recursively: true,
|
||||
searchMode: 'name',
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -68,6 +69,7 @@ describe('CollectionService', () => {
|
||||
operator: 'AND',
|
||||
conditions: [{ field: 'rating', operator: 'eq', value: 5 }],
|
||||
recursively: true,
|
||||
searchMode: 'name',
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -90,7 +92,7 @@ describe('CollectionService', () => {
|
||||
name: `Collection ${i}`,
|
||||
filter: {
|
||||
sourceFolderId: project.rootFolder!,
|
||||
searchFilter: { operator: 'AND', conditions: [], recursively: true },
|
||||
searchFilter: { operator: 'AND', conditions: [], recursively: true, searchMode: 'name' },
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -128,7 +130,7 @@ describe('CollectionService', () => {
|
||||
name: 'To be deleted',
|
||||
filter: {
|
||||
sourceFolderId: project.rootFolder!,
|
||||
searchFilter: { operator: 'AND', conditions: [], recursively: true },
|
||||
searchFilter: { operator: 'AND', conditions: [], recursively: true, searchMode: 'name' },
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ describe('N-gram Search Integration', () => {
|
||||
const result = await searchService.search(root.id, {
|
||||
operator: 'AND',
|
||||
recursively: true,
|
||||
searchMode: 'name',
|
||||
conditions: [
|
||||
{
|
||||
field: 'name',
|
||||
@@ -87,6 +88,7 @@ describe('N-gram Search Integration', () => {
|
||||
const result = await searchService.search(root.id, {
|
||||
operator: 'AND',
|
||||
recursively: true,
|
||||
searchMode: 'name',
|
||||
conditions: [
|
||||
{
|
||||
field: 'name',
|
||||
@@ -156,6 +158,7 @@ describe('N-gram Search Integration', () => {
|
||||
const result = await searchService.search(rootId, {
|
||||
operator: 'AND',
|
||||
recursively: true,
|
||||
searchMode: 'name',
|
||||
conditions: [
|
||||
{
|
||||
field: 'name',
|
||||
@@ -188,6 +191,7 @@ describe('N-gram Search Integration', () => {
|
||||
await searchService.search(rootId, {
|
||||
operator: 'AND',
|
||||
recursively: true,
|
||||
searchMode: 'name',
|
||||
conditions: [{ field: 'name', operator: 'contains', value: 'rareterm' }],
|
||||
})
|
||||
|
||||
@@ -209,6 +213,7 @@ describe('N-gram Search Integration', () => {
|
||||
await searchService.search(rootId, {
|
||||
operator: 'AND',
|
||||
recursively: true,
|
||||
searchMode: 'name',
|
||||
conditions: [{ field: 'name', operator: 'contains', value: 'commonterm' }],
|
||||
})
|
||||
|
||||
|
||||
@@ -376,6 +376,7 @@ describe('SearchService', () => {
|
||||
recursively: true,
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
searchMode: 'name',
|
||||
})
|
||||
const names = result.data.map((a) => a.name).sort()
|
||||
expect(names).toEqual(expected.sort())
|
||||
@@ -390,6 +391,7 @@ describe('SearchService', () => {
|
||||
recursively: true,
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
searchMode: 'name',
|
||||
})
|
||||
|
||||
expect(result.data).toHaveLength(1)
|
||||
@@ -404,6 +406,7 @@ describe('SearchService', () => {
|
||||
recursively: true,
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
searchMode: 'name',
|
||||
})
|
||||
|
||||
expect(result.data).toHaveLength(2)
|
||||
@@ -420,6 +423,7 @@ describe('SearchService', () => {
|
||||
recursively: true,
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
searchMode: 'name',
|
||||
})
|
||||
|
||||
expect(result.data).toHaveLength(1)
|
||||
@@ -434,6 +438,7 @@ describe('SearchService', () => {
|
||||
recursively: false,
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
searchMode: 'name',
|
||||
})
|
||||
|
||||
// Should only find file1, as file2 is in subfolder
|
||||
@@ -454,6 +459,7 @@ describe('SearchService', () => {
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
conditions: [],
|
||||
searchMode: 'name',
|
||||
})
|
||||
expect(resultAsc.data[0].name).toBe('file1')
|
||||
expect(resultAsc.data[1].name).toBe('file2')
|
||||
@@ -464,6 +470,7 @@ describe('SearchService', () => {
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
conditions: [],
|
||||
searchMode: 'name',
|
||||
})
|
||||
expect(resultDesc.data[0].name).toBe('file2')
|
||||
expect(resultDesc.data[1].name).toBe('file1')
|
||||
@@ -479,6 +486,7 @@ describe('SearchService', () => {
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
conditions: [],
|
||||
searchMode: 'name',
|
||||
})
|
||||
expect(resultAsc.data[0].sizeByte).toBe(100)
|
||||
expect(resultAsc.data[1].sizeByte).toBe(200)
|
||||
@@ -489,6 +497,7 @@ describe('SearchService', () => {
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
conditions: [],
|
||||
searchMode: 'name',
|
||||
})
|
||||
expect(resultDesc.data[0].sizeByte).toBe(200)
|
||||
expect(resultDesc.data[1].sizeByte).toBe(100)
|
||||
@@ -515,6 +524,7 @@ describe('SearchService', () => {
|
||||
assetType: 'file',
|
||||
operator: 'AND',
|
||||
conditions: [],
|
||||
searchMode: 'name',
|
||||
})
|
||||
expect(resultNormal.data.find((a) => a.id === symlink.id)).toBeUndefined()
|
||||
|
||||
@@ -525,6 +535,7 @@ describe('SearchService', () => {
|
||||
operator: 'AND',
|
||||
conditions: [],
|
||||
showSymlink: true,
|
||||
searchMode: 'name',
|
||||
})
|
||||
expect(resultWithSymlink.data.find((a) => a.id === symlink.id)).toBeDefined()
|
||||
})
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { prisma } from '@/db'
|
||||
import { Prisma, AssetType } from '@/generated/prisma/client.ts'
|
||||
import { Prisma, AssetType, WorkflowTaskType } from '@/generated/prisma/client.ts'
|
||||
import { AssetService, assetService } from '@/services/asset/asset'
|
||||
import { AssetInfo } from '@/dtos/asset'
|
||||
import { SearchRequest } from '@/dtos/search'
|
||||
import { paginateQuery, PaginatedData } from '@/services/pagination'
|
||||
import { generateSearchNgrams } from '@/utils/ngram'
|
||||
import { workflowService } from '@/workflow/workflow'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
|
||||
export class SearchService {
|
||||
constructor(
|
||||
@@ -57,8 +59,75 @@ export class SearchService {
|
||||
Object.assign(where, typeCondition)
|
||||
}
|
||||
|
||||
// AI Semantic search is skipped for now, but in the future we'll check if req.query exists
|
||||
// and if the team has embeddings enabled to do a vector search
|
||||
// ----------------------------------------------------------------------
|
||||
// AI Semantic search
|
||||
// ----------------------------------------------------------------------
|
||||
let semanticIds: string[] = []
|
||||
if (req.query && (req.searchMode === 'content' || req.searchMode === 'all')) {
|
||||
// 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,
|
||||
assetId: folderId, // Just using folderId as a placeholder
|
||||
payload: {
|
||||
projectId: '', // Not used for this task type
|
||||
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')
|
||||
}
|
||||
|
||||
// 3. Fetch top 1000 unique asset IDs from asset_embeddings via pgvector
|
||||
const semanticMatches = await this.prismaClient.$queryRaw<{ assetId: string }[]>`
|
||||
SELECT asset_id as "assetId" FROM (
|
||||
SELECT asset_id, MIN(embedding <=> ${JSON.stringify(queryVector)}::vector) as min_dist
|
||||
FROM asset_embeddings
|
||||
GROUP BY asset_id
|
||||
) as sub
|
||||
ORDER BY min_dist
|
||||
LIMIT 1000
|
||||
`
|
||||
semanticIds = semanticMatches.map((m) => m.assetId)
|
||||
|
||||
if (req.searchMode === 'content') {
|
||||
where.id = { in: semanticIds }
|
||||
} else if (req.searchMode === 'all') {
|
||||
const keywordCondition: Prisma.AssetWhereInput = {
|
||||
name: { contains: req.query, mode: 'insensitive' },
|
||||
}
|
||||
const semanticCondition: Prisma.AssetWhereInput = {
|
||||
id: { in: semanticIds },
|
||||
}
|
||||
where.OR = [keywordCondition, semanticCondition]
|
||||
}
|
||||
}
|
||||
|
||||
const orderBy: Prisma.AssetOrderByWithRelationInput = {}
|
||||
if (req.sort) {
|
||||
|
||||
@@ -185,7 +185,15 @@ export class TeamService {
|
||||
where: { id: teamId },
|
||||
})
|
||||
if (!team) throw new Error('team not found')
|
||||
return team.settings || {}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const settings = (team.settings || {}) as any
|
||||
|
||||
const embeddingAgent = await prisma.agent.findFirst({
|
||||
where: { teamId, type: 'embedding', enabled: true },
|
||||
})
|
||||
settings.semanticSearchEnabled = !!embeddingAgent
|
||||
|
||||
return settings
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
@@ -60,6 +60,21 @@ export function SearchFilterDialog({
|
||||
const [isFiltersExpanded, setIsFiltersExpanded] = useState(true)
|
||||
const [searchInput, setSearchInput] = useState(initialKeyword)
|
||||
const [debouncedSearchInput, setDebouncedSearchInput] = useState(initialKeyword)
|
||||
const [searchMode, setSearchMode] = useState<'name' | 'content' | 'all'>('name')
|
||||
|
||||
const { data: teamSettings } = useQuery({
|
||||
queryKey: ['team-settings', teamId],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.teams[':teamId'].settings.$get({
|
||||
param: { teamId },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to fetch team settings')
|
||||
return (await res.json()) as { semanticSearchEnabled: boolean }
|
||||
},
|
||||
enabled: open,
|
||||
})
|
||||
|
||||
const semanticSearchEnabled = teamSettings?.semanticSearchEnabled ?? false
|
||||
|
||||
// Sync back if changed from outside
|
||||
useEffect(() => {
|
||||
@@ -67,6 +82,7 @@ export function SearchFilterDialog({
|
||||
setSearchInput(initialKeyword)
|
||||
setDebouncedSearchInput(initialKeyword)
|
||||
setConditions(initialOtherConditions)
|
||||
setSearchMode('name')
|
||||
}
|
||||
}, [open, initialKeyword, initialOtherConditions])
|
||||
|
||||
@@ -80,7 +96,7 @@ export function SearchFilterDialog({
|
||||
|
||||
const combinedConditions = useMemo(() => {
|
||||
const result: SearchCondition[] = [...conditions]
|
||||
if (debouncedSearchInput.trim()) {
|
||||
if (debouncedSearchInput.trim() && searchMode === 'name') {
|
||||
result.push({
|
||||
field: 'name',
|
||||
operator: 'contains',
|
||||
@@ -88,7 +104,7 @@ export function SearchFilterDialog({
|
||||
})
|
||||
}
|
||||
return result
|
||||
}, [debouncedSearchInput, conditions])
|
||||
}, [debouncedSearchInput, conditions, searchMode])
|
||||
|
||||
const hasActiveCriteria = useMemo(() => {
|
||||
return (
|
||||
@@ -117,6 +133,8 @@ export function SearchFilterDialog({
|
||||
searchFilter: {
|
||||
conditions: combinedConditions,
|
||||
recursively: true,
|
||||
query: debouncedSearchInput.trim(),
|
||||
searchMode: searchMode,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -138,8 +156,19 @@ export function SearchFilterDialog({
|
||||
},
|
||||
})
|
||||
|
||||
const { data: searchResults, isLoading } = useQuery({
|
||||
queryKey: ['search-preview', teamId, assetId, combinedConditions],
|
||||
const {
|
||||
data: searchResults,
|
||||
isLoading,
|
||||
error: searchError,
|
||||
} = useQuery({
|
||||
queryKey: [
|
||||
'search-preview',
|
||||
teamId,
|
||||
assetId,
|
||||
combinedConditions,
|
||||
searchMode,
|
||||
debouncedSearchInput,
|
||||
],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.folders[':folderId'].search.$post({
|
||||
param: { folderId: assetId },
|
||||
@@ -147,32 +176,31 @@ export function SearchFilterDialog({
|
||||
assetType: undefined,
|
||||
conditions: combinedConditions,
|
||||
recursively: true,
|
||||
query: debouncedSearchInput.trim(),
|
||||
searchMode: searchMode,
|
||||
first: 20,
|
||||
},
|
||||
})
|
||||
if (!res.ok) throw new Error('Search failed')
|
||||
if (!res.ok) {
|
||||
const errorData = (await res.json().catch(() => ({}))) as Record<string, unknown>
|
||||
throw new Error((errorData.message as string) || 'Search failed')
|
||||
}
|
||||
return (await res.json()) as { data: AssetInfo[] }
|
||||
},
|
||||
enabled: open && hasActiveCriteria,
|
||||
staleTime: 500,
|
||||
retry: false,
|
||||
})
|
||||
|
||||
const handleApply = () => {
|
||||
const finalConditions = [...conditions]
|
||||
if (searchInput.trim()) {
|
||||
finalConditions.push({
|
||||
field: 'name',
|
||||
operator: 'contains',
|
||||
value: searchInput.trim(),
|
||||
})
|
||||
}
|
||||
onApply(finalConditions)
|
||||
onApply(combinedConditions)
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
const handleClearFilters = () => {
|
||||
setConditions([])
|
||||
setSearchInput('')
|
||||
setSearchMode('name')
|
||||
}
|
||||
|
||||
const handleResultClick = (item: AssetInfo) => {
|
||||
@@ -202,29 +230,85 @@ export function SearchFilterDialog({
|
||||
e.preventDefault()
|
||||
handleApply()
|
||||
}}
|
||||
className="relative mt-2"
|
||||
className="flex flex-col gap-3 mt-2"
|
||||
>
|
||||
<Input
|
||||
value={searchInput}
|
||||
onChange={(e) => setSearchInput(e.target.value)}
|
||||
placeholder="Search records by name..."
|
||||
className="w-full pl-4 pr-24 py-3 bg-background border-border rounded-xl text-sm h-12 focus-visible:ring-2 focus-visible:ring-primary/20 shadow-sm"
|
||||
/>
|
||||
{searchInput && (
|
||||
<div className="relative">
|
||||
<Input
|
||||
value={searchInput}
|
||||
onChange={(e) => setSearchInput(e.target.value)}
|
||||
placeholder={
|
||||
searchMode === 'name' ? 'Search records by name...' : 'Search by content...'
|
||||
}
|
||||
className="w-full pl-4 pr-24 py-3 bg-background border-border rounded-xl text-sm h-12 focus-visible:ring-2 focus-visible:ring-primary/20 shadow-sm"
|
||||
/>
|
||||
{searchInput && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearchInput('')}
|
||||
className="absolute right-20 top-1/2 -translate-y-1/2 p-1 rounded-md text-muted-foreground hover:text-foreground transition-colors"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearchInput('')}
|
||||
className="absolute right-20 top-1/2 -translate-y-1/2 p-1 rounded-md text-muted-foreground hover:text-foreground transition-colors"
|
||||
type="submit"
|
||||
className="absolute right-1.5 top-1.5 bottom-1.5 px-4 bg-primary hover:bg-primary/90 text-primary-foreground text-xs font-semibold rounded-lg flex items-center justify-center transition-colors shadow-sm"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
Search
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="submit"
|
||||
className="absolute right-1.5 top-1.5 bottom-1.5 px-4 bg-primary hover:bg-primary/90 text-primary-foreground text-xs font-semibold rounded-lg flex items-center justify-center transition-colors shadow-sm"
|
||||
>
|
||||
Search
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex bg-background border border-border p-1 rounded-lg">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearchMode('name')}
|
||||
className={cn(
|
||||
'px-3 py-1.5 text-xs font-medium rounded-md transition-colors',
|
||||
searchMode === 'name'
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'hover:bg-accent text-muted-foreground',
|
||||
)}
|
||||
>
|
||||
Name
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearchMode('content')}
|
||||
disabled={!semanticSearchEnabled}
|
||||
className={cn(
|
||||
'px-3 py-1.5 text-xs font-medium rounded-md transition-colors',
|
||||
searchMode === 'content'
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'hover:bg-accent text-muted-foreground',
|
||||
!semanticSearchEnabled && 'opacity-50 cursor-not-allowed',
|
||||
)}
|
||||
title={!semanticSearchEnabled ? 'Embedding agent is not enabled' : ''}
|
||||
>
|
||||
Content
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearchMode('all')}
|
||||
disabled={!semanticSearchEnabled}
|
||||
className={cn(
|
||||
'px-3 py-1.5 text-xs font-medium rounded-md transition-colors',
|
||||
searchMode === 'all'
|
||||
? 'bg-primary text-primary-foreground shadow-sm'
|
||||
: 'hover:bg-accent text-muted-foreground',
|
||||
!semanticSearchEnabled && 'opacity-50 cursor-not-allowed',
|
||||
)}
|
||||
title={!semanticSearchEnabled ? 'Embedding agent is not enabled' : ''}
|
||||
>
|
||||
All
|
||||
</button>
|
||||
</div>
|
||||
{!semanticSearchEnabled && (
|
||||
<span className="text-[10px] text-muted-foreground italic">
|
||||
* Semantic search requires an enabled embedding agent.
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div
|
||||
@@ -283,7 +367,9 @@ export function SearchFilterDialog({
|
||||
{isLoading && (
|
||||
<span className="inline-flex items-center gap-1.5 text-xs text-primary">
|
||||
<Loader2 className="h-3 w-3 animate-spin" />
|
||||
Querying database...
|
||||
{searchMode !== 'name'
|
||||
? 'Semantic search using media intelligence might be slow...'
|
||||
: 'Querying database...'}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
@@ -297,6 +383,16 @@ export function SearchFilterDialog({
|
||||
Enter a name or add a filter rule to start discovering assets.
|
||||
</p>
|
||||
</div>
|
||||
) : searchError ? (
|
||||
<div className="py-12 px-4 text-center flex flex-col items-center justify-center bg-muted/20">
|
||||
<AlertCircle className="w-10 h-10 text-destructive/50 mb-2" />
|
||||
<h3 className="text-sm font-semibold text-foreground/80">Search failed</h3>
|
||||
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
|
||||
{searchError.message.includes('Embedding agent not configured')
|
||||
? 'Please create and enable an embedding agent in the team settings to use semantic search.'
|
||||
: searchError.message}
|
||||
</p>
|
||||
</div>
|
||||
) : isLoading ? (
|
||||
Array.from({ length: 3 }).map((_, idx) => (
|
||||
<div key={idx} className="p-3.5 flex items-center gap-3 animate-pulse bg-muted/10">
|
||||
|
||||
@@ -159,6 +159,56 @@ export async function generateEmbeddingActivity(params: GenerateEmbeddingParams)
|
||||
}
|
||||
}
|
||||
|
||||
export interface GenerateTextEmbeddingParams {
|
||||
text: string
|
||||
teamId: string
|
||||
}
|
||||
|
||||
export async function generateTextEmbeddingActivity(params: GenerateTextEmbeddingParams): Promise<{
|
||||
embedding: number[]
|
||||
usage: Usage
|
||||
}> {
|
||||
const apiKey = process.env.GEMINI_API_KEY
|
||||
if (!apiKey) {
|
||||
throw ApplicationFailure.create({
|
||||
message: 'GEMINI_API_KEY environment variable is not configured',
|
||||
nonRetryable: true,
|
||||
})
|
||||
}
|
||||
|
||||
const resolvedApiKey = process.env[apiKey] || apiKey
|
||||
const ai = new GoogleGenAI({ apiKey: resolvedApiKey })
|
||||
|
||||
const usage: Usage = {
|
||||
model: 'gemini-embedding-2',
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
}
|
||||
|
||||
const response = await ai.models.embedContent({
|
||||
model: 'gemini-embedding-2',
|
||||
contents: [
|
||||
{
|
||||
parts: [{ text: params.text }],
|
||||
role: 'user',
|
||||
},
|
||||
],
|
||||
config: {
|
||||
outputDimensionality: 1536,
|
||||
},
|
||||
})
|
||||
|
||||
const values = response.embeddings?.[0]?.values
|
||||
if (!values) {
|
||||
throw new Error('No embedding values returned from gemini-embedding-2')
|
||||
}
|
||||
|
||||
return {
|
||||
embedding: values,
|
||||
usage,
|
||||
}
|
||||
}
|
||||
|
||||
export interface ExtractAiMetadataParams {
|
||||
assetKey: string
|
||||
filePath: string
|
||||
|
||||
@@ -2,6 +2,7 @@ import { prisma } from '@/db'
|
||||
import { WorkflowTask, WorkflowTaskStatus, WorkflowTaskType } from '@/generated/prisma/client'
|
||||
import { Executor } from './executor'
|
||||
import { agentEmbeddingMedia } from './workflows/agent-embedding'
|
||||
import { queryEmbeddingForSearch } from './workflows/query-embedding-for-search'
|
||||
import { agentAutofillMedia } from './workflows/agent-autofill'
|
||||
import { agentChat } from './workflows/agent-chat'
|
||||
import { transcodeMedia } from '@/transcode/workflows/transcode'
|
||||
@@ -85,6 +86,9 @@ export class LocalExecutor implements Executor {
|
||||
case WorkflowTaskType.ai_embedding:
|
||||
await agentEmbeddingMedia(task)
|
||||
break
|
||||
case WorkflowTaskType.query_embedding_for_search:
|
||||
await queryEmbeddingForSearch(task)
|
||||
break
|
||||
case WorkflowTaskType.ai_metadata_autofill:
|
||||
await agentAutofillMedia(task)
|
||||
break
|
||||
|
||||
@@ -19,6 +19,9 @@ export class TemporalExecutor implements Executor {
|
||||
case WorkflowTaskType.ai_embedding:
|
||||
workflowName = 'agentEmbeddingWorkflow'
|
||||
break
|
||||
case WorkflowTaskType.query_embedding_for_search:
|
||||
workflowName = 'queryEmbeddingForSearch'
|
||||
break
|
||||
case WorkflowTaskType.ai_metadata_autofill:
|
||||
workflowName = 'agentAutofillWorkflow'
|
||||
break
|
||||
|
||||
@@ -2,6 +2,7 @@ import { WorkflowTask } from '@/generated/prisma/client'
|
||||
import { Executor } from './executor'
|
||||
import { LocalExecutor } from './local-executor'
|
||||
import { TemporalExecutor } from './temporal-executor'
|
||||
import { prisma } from '@/db'
|
||||
|
||||
export class WorkflowService {
|
||||
private executor: Executor
|
||||
@@ -19,6 +20,35 @@ export class WorkflowService {
|
||||
return this.executor.submit(task)
|
||||
}
|
||||
|
||||
async executeWait(task: WorkflowTask, timeoutMs: number = 30000): Promise<WorkflowTask> {
|
||||
await this.submit(task)
|
||||
|
||||
const start = Date.now()
|
||||
while (Date.now() - start < timeoutMs) {
|
||||
const updatedTask = await prisma.workflowTask.findUnique({
|
||||
where: { id: task.id },
|
||||
})
|
||||
|
||||
if (!updatedTask) {
|
||||
throw new Error(`WorkflowTask ${task.id} not found`)
|
||||
}
|
||||
|
||||
if (updatedTask.status === 'completed') {
|
||||
return updatedTask
|
||||
}
|
||||
|
||||
if (updatedTask.status === 'failed') {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const error = (updatedTask.output as any)?.error || 'Workflow task failed'
|
||||
throw new Error(error)
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||
}
|
||||
|
||||
throw new Error('Workflow task timed out')
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.executor.start()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from './agent-embedding'
|
||||
export * from './agent-autofill'
|
||||
export * from './agent-chat'
|
||||
export * from './query-embedding-for-search'
|
||||
export * from '@/transcode/workflows/transcode'
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { WorkflowTask, WorkflowTaskStatus, Prisma } from '@/generated/prisma/client'
|
||||
import { getActivities } from '../workflow-utils'
|
||||
import { prisma } from '@/db'
|
||||
|
||||
export async function queryEmbeddingForSearch(task: WorkflowTask): Promise<void> {
|
||||
const { generateTextEmbeddingActivity } = getActivities()
|
||||
|
||||
const payload = task.payload as PrismaJson.WorkflowTaskPayload | null
|
||||
const text = payload?.queryEmbeddingForSearch?.text
|
||||
|
||||
if (!text) {
|
||||
throw new Error('Missing text in query_embedding_for_search task payload')
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Mark as processing
|
||||
await prisma.workflowTask.update({
|
||||
where: { id: task.id },
|
||||
data: { status: WorkflowTaskStatus.processing, heartbeat: new Date() },
|
||||
})
|
||||
|
||||
// 2. Generate embedding
|
||||
const result = await generateTextEmbeddingActivity({
|
||||
text,
|
||||
teamId: task.teamId!,
|
||||
})
|
||||
|
||||
// 3. Save output and complete
|
||||
await prisma.workflowTask.update({
|
||||
where: { id: task.id },
|
||||
data: {
|
||||
status: WorkflowTaskStatus.completed,
|
||||
output: { embedding: result.embedding } as Prisma.InputJsonValue,
|
||||
inputTokens: result.usage.inputTokens,
|
||||
outputTokens: result.usage.outputTokens,
|
||||
model: result.usage.model,
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(`queryEmbeddingForSearch failed for task ${task.id}:`, err)
|
||||
await prisma.workflowTask.update({
|
||||
where: { id: task.id },
|
||||
data: {
|
||||
status: WorkflowTaskStatus.failed,
|
||||
output: {
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
} as Prisma.InputJsonValue,
|
||||
},
|
||||
})
|
||||
throw err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user