Skip to content

Commit 231e0a0

Browse files
committed
fix(chat): compare selection ids as sets, not sequences
Cursor Bugbot: sameIds compared rowIds/columnIds by index, but a table selection's ids iterate in click order (they come from a Set), so the same rows picked in a different order — or reached via a cell range rather than the gutter — compared unequal. prepareContextForInsert then added a second ordinalized chip pointing at rows already referenced instead of no-opping. More reachable since the previous commit started sourcing rowIds from rowSel.ids directly, where insertion order tracks the user's clicks.
1 parent 9a1da09 commit 231e0a0

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ describe('prepareContextForInsert', () => {
6767
expect(prepareContextForInsert(other, [fileSelection()])).not.toBeNull()
6868
})
6969

70+
it('treats the same rows picked in a different order as one selection', () => {
71+
// Row ids iterate in click order, so re-picking the same rows differently
72+
// must no-op rather than add a second chip over rows already referenced.
73+
const reordered = tableSelection({ rowIds: ['r3', 'r1', 'r2'] })
74+
75+
expect(prepareContextForInsert(reordered, [tableSelection()])).toBeNull()
76+
})
77+
7078
it('distinguishes a cell range from the whole rows it spans', () => {
7179
const range = tableSelection({ columnIds: ['c_name'] })
7280

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,20 @@ type McpContext = Extract<ChatContext, { kind: 'mcp' }>
193193
type FileSelectionContext = Extract<ChatContext, { kind: 'file_selection' }>
194194
type TableSelectionContext = Extract<ChatContext, { kind: 'table_selection' }>
195195

196-
/** Order-sensitive equality for two optional id lists. */
196+
/**
197+
* Set equality for two optional id lists.
198+
*
199+
* Deliberately order-insensitive: a table selection's row ids come from a Set
200+
* whose iteration order follows click order, and the same rows picked in a
201+
* different order — or via a cell range rather than the gutter — are the same
202+
* selection. Comparing by index would call those distinct and add a duplicate
203+
* ordinalized chip pointing at rows already referenced.
204+
*/
197205
function sameIds(a: string[] | undefined, b: string[] | undefined): boolean {
198206
if (a === b) return true
199207
if (!a || !b || a.length !== b.length) return false
200-
return a.every((id, i) => id === b[i])
208+
const inA = new Set(a)
209+
return b.every((id) => inA.has(id))
201210
}
202211

203212
/**

0 commit comments

Comments
 (0)