Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/sim/app/api/knowledge/[id]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ describe('Knowledge Base By ID API Route', () => {
{
name: validUpdateData.name,
description: validUpdateData.description,
workspaceId: undefined,
chunkingConfig: undefined,
},
expect.any(String)
Expand Down
1 change: 1 addition & 0 deletions apps/sim/app/api/knowledge/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export async function PUT(req: NextRequest, { params }: { params: Promise<{ id:
{
name: validatedData.name,
description: validatedData.description,
workspaceId: validatedData.workspaceId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Missing permission validation when changing workspaceId. Should verify user has access to target workspace before allowing the update.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/sim/app/api/knowledge/[id]/route.ts
Line: 106:106

Comment:
**logic:** Missing permission validation when changing workspaceId. Should verify user has access to target workspace before allowing the update.

How can I resolve this? If you propose a fix, please make it concise.

chunkingConfig: validatedData.chunkingConfig,
},
requestId
Expand Down
4 changes: 0 additions & 4 deletions apps/sim/app/workspace/[workspaceId]/knowledge/[id]/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,6 @@ export function KnowledgeBase({
options={{
knowledgeBaseId: id,
currentWorkspaceId: knowledgeBase?.workspaceId || null,
onWorkspaceChange: () => {
// Refresh the page to reflect the workspace change
window.location.reload()
},
onDeleteKnowledgeBase: () => setShowDeleteDialog(true),
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@/components/ui/dropdown-menu'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { createLogger } from '@/lib/logs/console/logger'
import { useKnowledgeStore } from '@/stores/knowledge/store'

const logger = createLogger('WorkspaceSelector')

Expand All @@ -33,6 +34,7 @@ export function WorkspaceSelector({
onWorkspaceChange,
disabled = false,
}: WorkspaceSelectorProps) {
const { updateKnowledgeBase } = useKnowledgeStore()
const [workspaces, setWorkspaces] = useState<Workspace[]>([])
const [isLoading, setIsLoading] = useState(false)
const [isUpdating, setIsUpdating] = useState(false)
Expand Down Expand Up @@ -95,6 +97,11 @@ export function WorkspaceSelector({

if (result.success) {
logger.info(`Knowledge base workspace updated: ${knowledgeBaseId} -> ${workspaceId}`)

// Update the store immediately to reflect the change without page reload
updateKnowledgeBase(knowledgeBaseId, { workspaceId: workspaceId || undefined })

// Notify parent component of the change
onWorkspaceChange?.(workspaceId)
} else {
throw new Error(result.error || 'Failed to update workspace')
Expand Down
3 changes: 3 additions & 0 deletions apps/sim/lib/knowledge/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export async function updateKnowledgeBase(
updates: {
name?: string
description?: string
workspaceId?: string | null
chunkingConfig?: {
maxSize: number
minSize: number
Expand All @@ -148,6 +149,7 @@ export async function updateKnowledgeBase(
updatedAt: Date
name?: string
description?: string | null
workspaceId?: string | null
chunkingConfig?: {
maxSize: number
minSize: number
Expand All @@ -161,6 +163,7 @@ export async function updateKnowledgeBase(

if (updates.name !== undefined) updateData.name = updates.name
if (updates.description !== undefined) updateData.description = updates.description
if (updates.workspaceId !== undefined) updateData.workspaceId = updates.workspaceId
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Missing permission validation when changing workspaceId. Users could move knowledge bases to unauthorized workspaces. Should verify user has permissions for target workspace before allowing the change.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/sim/lib/knowledge/service.ts
Line: 166:166

Comment:
**logic:** Missing permission validation when changing workspaceId. Users could move knowledge bases to unauthorized workspaces. Should verify user has permissions for target workspace before allowing the change.

How can I resolve this? If you propose a fix, please make it concise.

if (updates.chunkingConfig !== undefined) {
updateData.chunkingConfig = updates.chunkingConfig
updateData.embeddingModel = 'text-embedding-3-small'
Expand Down
19 changes: 19 additions & 0 deletions apps/sim/stores/knowledge/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ interface KnowledgeStore {
updateChunk: (documentId: string, chunkId: string, updates: Partial<ChunkData>) => void
addPendingDocuments: (knowledgeBaseId: string, documents: DocumentData[]) => void
addKnowledgeBase: (knowledgeBase: KnowledgeBaseData) => void
updateKnowledgeBase: (id: string, updates: Partial<KnowledgeBaseData>) => void
removeKnowledgeBase: (id: string) => void
removeDocument: (knowledgeBaseId: string, documentId: string) => void
clearDocuments: (knowledgeBaseId: string) => void
Expand Down Expand Up @@ -796,6 +797,24 @@ export const useKnowledgeStore = create<KnowledgeStore>((set, get) => ({
logger.info(`Knowledge base added: ${knowledgeBase.id}`)
},

updateKnowledgeBase: (id: string, updates: Partial<KnowledgeBaseData>) => {
set((state) => {
const existingKb = state.knowledgeBases[id]
if (!existingKb) return state

const updatedKb = { ...existingKb, ...updates }

return {
knowledgeBases: {
...state.knowledgeBases,
[id]: updatedKb,
},
knowledgeBasesList: state.knowledgeBasesList.map((kb) => (kb.id === id ? updatedKb : kb)),
}
})
logger.info(`Knowledge base updated: ${id}`)
},

removeKnowledgeBase: (id: string) => {
set((state) => {
const newKnowledgeBases = { ...state.knowledgeBases }
Expand Down