Skip to content

Commit 5dfaed2

Browse files
waleedlatif1claude
andcommitted
fix(knowledge): resolve type errors and unhandled rejection in search routes
- Use accessCheck.knowledgeBase.embeddingModel directly in chunks response - Narrow access-check predicate to KnowledgeBaseAccessResult in v1 search - Move inaccessible-KB 404 check before query embedding promise creation Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent cb1cab7 commit 5dfaed2

3 files changed

Lines changed: 10 additions & 11 deletions

File tree

apps/sim/app/api/knowledge/[id]/documents/[documentId]/chunks/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export const POST = withRouteHandler(
245245
completion: 0,
246246
total: newChunk.tokenCount,
247247
},
248-
model: chunkEmbeddingModel,
248+
model: accessCheck.knowledgeBase.embeddingModel,
249249
pricing: cost.pricing,
250250
},
251251
}

apps/sim/app/api/knowledge/search/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,6 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
264264
}
265265
const queryEmbeddingModel = embeddingModels[0]
266266

267-
const queryEmbeddingPromise = hasQuery
268-
? generateSearchEmbedding(validatedData.query!, queryEmbeddingModel, workspaceId)
269-
: Promise.resolve(null)
270-
271267
// Check if any requested knowledge bases were not accessible
272268
const inaccessibleKbIds = knowledgeBaseIds.filter((id) => !accessibleKbIds.includes(id))
273269

@@ -278,6 +274,10 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
278274
)
279275
}
280276

277+
const queryEmbeddingPromise = hasQuery
278+
? generateSearchEmbedding(validatedData.query!, queryEmbeddingModel, workspaceId)
279+
: Promise.resolve(null)
280+
281281
if (workflowId) {
282282
const authorization = await authorizeWorkflowByWorkspacePermission({
283283
workflowId,

apps/sim/app/api/v1/knowledge/search/route.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
handleVectorOnlySearch,
1515
type SearchResult,
1616
} from '@/app/api/knowledge/search/utils'
17-
import { checkKnowledgeBaseAccess } from '@/app/api/knowledge/utils'
17+
import { checkKnowledgeBaseAccess, type KnowledgeBaseAccessResult } from '@/app/api/knowledge/utils'
1818
import {
1919
authenticateRequest,
2020
handleError,
@@ -84,13 +84,12 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
8484
const accessChecks = await Promise.all(
8585
knowledgeBaseIds.map((kbId) => checkKnowledgeBaseAccess(kbId, userId))
8686
)
87-
const accessibleKbs = knowledgeBaseIds
88-
.map((_, idx) => accessChecks[idx])
87+
const accessibleKbs = accessChecks
8988
.filter(
90-
(ac): ac is NonNullable<typeof ac> =>
91-
Boolean(ac?.hasAccess) && ac?.knowledgeBase?.workspaceId === workspaceId
89+
(ac): ac is KnowledgeBaseAccessResult =>
90+
ac.hasAccess === true && ac.knowledgeBase.workspaceId === workspaceId
9291
)
93-
.map((ac) => ac.knowledgeBase!)
92+
.map((ac) => ac.knowledgeBase)
9493
const accessibleKbIds = accessibleKbs.map((kb) => kb.id)
9594

9695
if (accessibleKbIds.length === 0) {

0 commit comments

Comments
 (0)