Skip to content

Commit 104d34c

Browse files
authored
fix(copilot): context filtering (#1160)
* Add filter * Scope kb and chats * Lint * Remove comments * Lint
1 parent 06e9a6b commit 104d34c

File tree

1 file changed

+22
-3
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/user-input

1 file changed

+22
-3
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/user-input/user-input.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
X,
3232
Zap,
3333
} from 'lucide-react'
34+
import { useParams } from 'next/navigation'
3435
import {
3536
Button,
3637
DropdownMenu,
@@ -153,6 +154,8 @@ const UserInput = forwardRef<UserInputRef, UserInputProps>(
153154

154155
const { data: session } = useSession()
155156
const { currentChat, workflowId } = useCopilotStore()
157+
const params = useParams()
158+
const workspaceId = params.workspaceId as string
156159

157160
// Determine placeholder based on mode
158161
const effectivePlaceholder =
@@ -219,8 +222,19 @@ const UserInput = forwardRef<UserInputRef, UserInputProps>(
219222
if (!resp.ok) throw new Error(`Failed to load chats: ${resp.status}`)
220223
const data = await resp.json()
221224
const items = Array.isArray(data?.chats) ? data.chats : []
225+
226+
if (workflows.length === 0) {
227+
await ensureWorkflowsLoaded()
228+
}
229+
230+
const workspaceWorkflowIds = new Set(workflows.map((w) => w.id))
231+
232+
const workspaceChats = items.filter(
233+
(c: any) => !c.workflowId || workspaceWorkflowIds.has(c.workflowId)
234+
)
235+
222236
setPastChats(
223-
items.map((c: any) => ({
237+
workspaceChats.map((c: any) => ({
224238
id: c.id,
225239
title: c.title ?? null,
226240
workflowId: c.workflowId ?? null,
@@ -241,8 +255,12 @@ const UserInput = forwardRef<UserInputRef, UserInputProps>(
241255
if (!resp.ok) throw new Error(`Failed to load workflows: ${resp.status}`)
242256
const data = await resp.json()
243257
const items = Array.isArray(data?.data) ? data.data : []
258+
// Filter workflows by workspace (same as sidebar)
259+
const workspaceFiltered = items.filter(
260+
(w: any) => w.workspaceId === workspaceId || !w.workspaceId
261+
)
244262
// Sort by last modified/updated (newest first), matching sidebar behavior
245-
const sorted = [...items].sort((a: any, b: any) => {
263+
const sorted = [...workspaceFiltered].sort((a: any, b: any) => {
246264
const ta = new Date(a.lastModified || a.updatedAt || a.createdAt || 0).getTime()
247265
const tb = new Date(b.lastModified || b.updatedAt || b.createdAt || 0).getTime()
248266
return tb - ta
@@ -264,7 +282,8 @@ const UserInput = forwardRef<UserInputRef, UserInputProps>(
264282
if (isLoadingKnowledge || knowledgeBases.length > 0) return
265283
try {
266284
setIsLoadingKnowledge(true)
267-
const resp = await fetch('/api/knowledge')
285+
// Filter by workspace like the Knowledge page does
286+
const resp = await fetch(`/api/knowledge?workspaceId=${workspaceId}`)
268287
if (!resp.ok) throw new Error(`Failed to load knowledge bases: ${resp.status}`)
269288
const data = await resp.json()
270289
const items = Array.isArray(data?.data) ? data.data : []

0 commit comments

Comments
 (0)