| 21 | * Query chunks for a document with filtering and pagination |
| 22 | */ |
| 23 | export async function queryChunks( |
| 24 | documentId: string, |
| 25 | filters: ChunkFilters, |
| 26 | requestId: string |
| 27 | ): Promise<ChunkQueryResult> { |
| 28 | const { |
| 29 | search, |
| 30 | enabled = 'all', |
| 31 | limit = 50, |
| 32 | offset = 0, |
| 33 | sortBy = 'chunkIndex', |
| 34 | sortOrder = 'asc', |
| 35 | } = filters |
| 36 | |
| 37 | const conditions = [eq(embedding.documentId, documentId)] |
| 38 | |
| 39 | if (enabled === 'true') { |
| 40 | conditions.push(eq(embedding.enabled, true)) |
| 41 | } else if (enabled === 'false') { |
| 42 | conditions.push(eq(embedding.enabled, false)) |
| 43 | } |
| 44 | |
| 45 | if (search) { |
| 46 | conditions.push(ilike(embedding.content, `%${search}%`)) |
| 47 | } |
| 48 | |
| 49 | const chunks = await db |
| 50 | .select({ |
| 51 | id: embedding.id, |
| 52 | chunkIndex: embedding.chunkIndex, |
| 53 | content: embedding.content, |
| 54 | contentLength: embedding.contentLength, |
| 55 | tokenCount: embedding.tokenCount, |
| 56 | enabled: embedding.enabled, |
| 57 | startOffset: embedding.startOffset, |
| 58 | endOffset: embedding.endOffset, |
| 59 | tag1: embedding.tag1, |
| 60 | tag2: embedding.tag2, |
| 61 | tag3: embedding.tag3, |
| 62 | tag4: embedding.tag4, |
| 63 | tag5: embedding.tag5, |
| 64 | tag6: embedding.tag6, |
| 65 | tag7: embedding.tag7, |
| 66 | createdAt: embedding.createdAt, |
| 67 | updatedAt: embedding.updatedAt, |
| 68 | }) |
| 69 | .from(embedding) |
| 70 | .where(and(...conditions)) |
| 71 | .orderBy( |
| 72 | (() => { |
| 73 | const col = |
| 74 | sortBy === 'tokenCount' |
| 75 | ? embedding.tokenCount |
| 76 | : sortBy === 'enabled' |
| 77 | ? embedding.enabled |
| 78 | : embedding.chunkIndex |
| 79 | return sortOrder === 'desc' ? desc(col) : asc(col) |
| 80 | })() |