-
Notifications
You must be signed in to change notification settings - Fork 3.2k
v0.3.11: fix force-dynamic routes, webhooks, kb search perms #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| import { and, eq, inArray, isNull, sql } from 'drizzle-orm' | ||
| import { and, eq, inArray, sql } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { retryWithExponentialBackoff } from '@/lib/documents/utils' | ||
| import { env } from '@/lib/env' | ||
| import { createLogger } from '@/lib/logs/console-logger' | ||
| import { estimateTokenCount } from '@/lib/tokenization/estimators' | ||
| import { getUserId } from '@/app/api/auth/oauth/utils' | ||
| import { checkKnowledgeBaseAccess } from '@/app/api/knowledge/utils' | ||
| import { db } from '@/db' | ||
| import { embedding, knowledgeBase } from '@/db/schema' | ||
| import { embedding } from '@/db/schema' | ||
| import { calculateCost } from '@/providers/utils' | ||
|
|
||
| const logger = createLogger('VectorSearchAPI') | ||
|
|
@@ -261,47 +262,45 @@ export async function POST(request: NextRequest) { | |
| ? validatedData.knowledgeBaseIds | ||
| : [validatedData.knowledgeBaseIds] | ||
|
|
||
| const [kb, queryEmbedding] = await Promise.all([ | ||
| db | ||
| .select() | ||
| .from(knowledgeBase) | ||
| .where( | ||
| and( | ||
| inArray(knowledgeBase.id, knowledgeBaseIds), | ||
| eq(knowledgeBase.userId, userId), | ||
| isNull(knowledgeBase.deletedAt) | ||
| ) | ||
| ), | ||
| generateSearchEmbedding(validatedData.query), | ||
| ]) | ||
|
|
||
| if (kb.length === 0) { | ||
| // Check access permissions for each knowledge base using proper workspace-based permissions | ||
| const accessibleKbIds: string[] = [] | ||
| for (const kbId of knowledgeBaseIds) { | ||
| const accessCheck = await checkKnowledgeBaseAccess(kbId, userId) | ||
| if (accessCheck.hasAccess) { | ||
| accessibleKbIds.push(kbId) | ||
| } | ||
| } | ||
|
|
||
| if (accessibleKbIds.length === 0) { | ||
| return NextResponse.json( | ||
| { error: 'Knowledge base not found or access denied' }, | ||
| { status: 404 } | ||
| ) | ||
| } | ||
|
|
||
| const foundKbIds = kb.map((k) => k.id) | ||
| const missingKbIds = knowledgeBaseIds.filter((id) => !foundKbIds.includes(id)) | ||
| // Generate query embedding in parallel with access checks | ||
| const queryEmbedding = await generateSearchEmbedding(validatedData.query) | ||
|
|
||
| // Check if any requested knowledge bases were not accessible | ||
| const inaccessibleKbIds = knowledgeBaseIds.filter((id) => !accessibleKbIds.includes(id)) | ||
|
|
||
| if (missingKbIds.length > 0) { | ||
| if (inaccessibleKbIds.length > 0) { | ||
| return NextResponse.json( | ||
| { error: `Knowledge bases not found: ${missingKbIds.join(', ')}` }, | ||
| { error: `Knowledge bases not found or access denied: ${inaccessibleKbIds.join(', ')}` }, | ||
| { status: 404 } | ||
| ) | ||
| } | ||
|
Comment on lines
+287
to
292
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: This check for inaccessible KBs creates a potential information leak - it reveals which specific knowledge base IDs exist but are inaccessible. Consider returning a generic error message instead. |
||
|
|
||
| // Adaptive query strategy based on KB count and parameters | ||
| const strategy = getQueryStrategy(foundKbIds.length, validatedData.topK) | ||
| // Adaptive query strategy based on accessible KB count and parameters | ||
| const strategy = getQueryStrategy(accessibleKbIds.length, validatedData.topK) | ||
| const queryVector = JSON.stringify(queryEmbedding) | ||
|
|
||
| let results: any[] | ||
|
|
||
| if (strategy.useParallel) { | ||
| // Execute parallel queries for better performance with many KBs | ||
| const parallelResults = await executeParallelQueries( | ||
| foundKbIds, | ||
| accessibleKbIds, | ||
| queryVector, | ||
| validatedData.topK, | ||
| strategy.distanceThreshold, | ||
|
|
@@ -311,7 +310,7 @@ export async function POST(request: NextRequest) { | |
| } else { | ||
| // Execute single optimized query for fewer KBs | ||
| results = await executeSingleQuery( | ||
| foundKbIds, | ||
| accessibleKbIds, | ||
| queryVector, | ||
| validatedData.topK, | ||
| strategy.distanceThreshold, | ||
|
|
@@ -350,8 +349,8 @@ export async function POST(request: NextRequest) { | |
| similarity: 1 - result.distance, | ||
| })), | ||
| query: validatedData.query, | ||
| knowledgeBaseIds: foundKbIds, | ||
| knowledgeBaseId: foundKbIds[0], | ||
| knowledgeBaseIds: accessibleKbIds, | ||
| knowledgeBaseId: accessibleKbIds[0], | ||
| topK: validatedData.topK, | ||
| totalResults: results.length, | ||
| ...(cost && tokenCount | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Moving embedding generation after access checks removes the optimization of parallel execution. Consider moving this back inside the access check loop or running it concurrently with the first access check.