-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(chat): highlight-to-chat — reference file/table selections in Chat #6087
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
28 commits
Select commit
Hold shift + click to select a range
5c4454b
feat(chat): highlight-to-chat for file and table selections
mzxchandra 3dac5a8
fix(chat): widen selection code fence so embedded backticks can't tru…
mzxchandra 1551f96
fix(chat): carry the selection chip on a column-header copy
mzxchandra 27dbd17
Merge remote-tracking branch 'origin/staging' into worktree-add-to-ch…
waleedlatif1 6421994
refactor(chat): clean up highlight-to-chat selections
waleedlatif1 a80f2e2
refactor(chat): tighten table copy fallback and helper placement
waleedlatif1 8455da9
fix(chat): apply chip handoffs as one batch; widen table copy chip path
waleedlatif1 598791e
fix(chat): distinguish a line-less file-selection label from the whol…
waleedlatif1 7d3d58d
fix(chat): don't revive an aged-out chip handoff when accumulating
waleedlatif1 f82c44e
fix(chat): reference every selected row in a table chip, not just loa…
waleedlatif1 9a1da09
docs(chat): scope the table copy 'complete' comment to the text path
waleedlatif1 231e0a0
fix(chat): compare selection ids as sets, not sequences
waleedlatif1 b20cfcd
fix(chat): enforce the table selection budget over the whole rendered…
waleedlatif1 2e5f8f5
fix(chat): don't swallow a paste whose selection chip is already atta…
waleedlatif1 eff18e6
fix(chat): derive the budget reserve from the same clause it reserves…
waleedlatif1 d939aea
fix(chat): align MothershipChat's onContextRemove with the surface co…
waleedlatif1 d3b6878
refactor(chat): apply cleanup pass findings
waleedlatif1 1392a74
fix(chat): don't attach a selection chip to a copy from a nested input
waleedlatif1 94eab0d
fix(chat): persist the source names a selection chip renders from
waleedlatif1 14b9ba6
Merge remote-tracking branch 'origin/staging' into worktree-add-to-ch…
waleedlatif1 0ff8c7f
refactor(chat): remove a needless alias and correct an eslint-disable…
waleedlatif1 fafe018
fix(chat): bound the sync copy path by the text limit, not the chip cap
waleedlatif1 65ddf42
refactor(tables): extract selection-to-chip helpers into utils so the…
waleedlatif1 dac8313
fix(chat): scope selections to the columns actually picked, and stop …
waleedlatif1 db183b5
fix(tables): cap the Add-to-Chat label at the rows a chip can carry
waleedlatif1 3bb5d6c
fix(chat): stop a removed chip lingering when its label prefixes another
waleedlatif1 714e6d8
fix(chat): include the line range in file-selection equality
waleedlatif1 7644b8d
fix(tables): drain past the cap so exclusions can't shrink a select-a…
waleedlatif1 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
5 changes: 3 additions & 2 deletions
5
.../[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/toolbar-button.tsx
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
88 changes: 88 additions & 0 deletions
88
...p/workspace/[workspaceId]/files/components/file-viewer/use-selection-copy-bridge.test.tsx
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { act, createRef, type RefObject } from 'react' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { SIM_SELECTION_MIME } from '@/lib/copilot/chat/selection-clipboard' | ||
| import type { ChatContext } from '@/stores/panel' | ||
| import { useSelectionCopyBridge } from './use-selection-copy-bridge' | ||
|
|
||
| const selection: ChatContext = { | ||
| kind: 'file_selection', | ||
| fileId: 'f1', | ||
| fileName: 'notes.md', | ||
| label: 'notes.md:2-4', | ||
| text: 'the exact passage', | ||
| } | ||
|
|
||
| let container: HTMLDivElement | ||
| let root: Root | ||
| let containerRef: RefObject<HTMLDivElement | null> | ||
| let buildContext: ReturnType<typeof vi.fn> | ||
|
|
||
| /** | ||
| * Mirrors the editors this hook wraps: Monaco's editing surface is a hidden | ||
| * textarea, and its find widget is a real input nested in the same container. | ||
| */ | ||
| function Host() { | ||
| useSelectionCopyBridge(containerRef, buildContext as () => ChatContext | null) | ||
| return ( | ||
| <div ref={containerRef}> | ||
| <textarea id='editor-surface' /> | ||
| <input id='find-box' /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| /** Dispatches a bubbling copy from `id` and returns what was written. */ | ||
| function dispatchCopy(id: string): Record<string, string> { | ||
| const written: Record<string, string> = {} | ||
| const event = new Event('copy', { bubbles: true }) as ClipboardEvent | ||
| Object.defineProperty(event, 'clipboardData', { | ||
| value: { | ||
| setData: (type: string, value: string) => { | ||
| written[type] = value | ||
| }, | ||
| }, | ||
| }) | ||
| act(() => { | ||
| container.querySelector(`#${id}`)?.dispatchEvent(event) | ||
| }) | ||
| return written | ||
| } | ||
|
|
||
| describe('useSelectionCopyBridge', () => { | ||
| beforeEach(() => { | ||
| container = document.createElement('div') | ||
| document.body.appendChild(container) | ||
| containerRef = createRef<HTMLDivElement>() | ||
| buildContext = vi.fn(() => selection) | ||
| root = createRoot(container) | ||
| act(() => { | ||
| root.render(<Host />) | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| act(() => root.unmount()) | ||
| container.remove() | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('attaches the selection when copying from the editor surface', () => { | ||
| const written = dispatchCopy('editor-surface') | ||
|
|
||
| expect(buildContext).toHaveBeenCalled() | ||
| expect(written[SIM_SELECTION_MIME]).toContain('file_selection') | ||
| }) | ||
|
|
||
| it('ignores a copy from a nested input such as the find box', () => { | ||
| // The document still holds a highlight, so without the guard the chip would | ||
| // ride onto text the user never copied. | ||
| const written = dispatchCopy('find-box') | ||
|
|
||
| expect(buildContext).not.toHaveBeenCalled() | ||
| expect(written[SIM_SELECTION_MIME]).toBeUndefined() | ||
| }) | ||
| }) |
44 changes: 44 additions & 0 deletions
44
...sim/app/workspace/[workspaceId]/files/components/file-viewer/use-selection-copy-bridge.ts
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use client' | ||
|
|
||
| import { type RefObject, useEffect } from 'react' | ||
| import { attachSelectionContextToClipboard } from '@/lib/copilot/chat/selection-clipboard' | ||
| import type { ChatContext } from '@/stores/panel' | ||
|
|
||
| /** | ||
| * Rides a selection {@link ChatContext} onto the editor's native copy so a | ||
| * highlighted passage copied with Cmd+C pastes into Chat as a reference chip. | ||
| * | ||
| * Attached in the BUBBLE phase so it runs after the inner editor's own copy | ||
| * handler — Monaco and ProseMirror both `clearData()` before writing | ||
| * `text/plain`, so the custom type must be added last to survive. | ||
| * | ||
| * @param buildContext - Returns null when there is no non-empty selection. | ||
| * @param enabled - Re-runs the effect for a container that mounts late (behind a | ||
| * loading gate); a ref isn't reactive, so the effect would otherwise bail on the | ||
| * first render and never re-attach. | ||
| */ | ||
| export function useSelectionCopyBridge( | ||
| containerRef: RefObject<HTMLElement | null>, | ||
| buildContext: () => ChatContext | null, | ||
| enabled = true | ||
| ): void { | ||
| useEffect(() => { | ||
| const dom = containerRef.current | ||
| if (!dom || !enabled) return | ||
| const onCopy = (e: ClipboardEvent) => { | ||
| // A copy from a field nested in the editor — Monaco's find box being the | ||
| // common one — bubbles here while the document still holds a highlight, | ||
| // so the selection would be attached to text the user never copied. | ||
| // | ||
| // Only INPUT is skipped, deliberately: Monaco's own editing surface is a | ||
| // hidden TEXTAREA, so excluding textareas (as the table grid does, where | ||
| // the cell editors really are form fields) would suppress the chip on the | ||
| // main copy path this hook exists for. | ||
| if ((e.target as HTMLElement | null)?.tagName === 'INPUT') return | ||
| const context = buildContext() | ||
| if (context) attachSelectionContextToClipboard(e.clipboardData, context) | ||
| } | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| dom.addEventListener('copy', onCopy) | ||
| return () => dom.removeEventListener('copy', onCopy) | ||
| }, [containerRef, buildContext, enabled]) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.