-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(chat): keep find stable across footnotes and pending chats, and match mention chips next to punctuation #8361
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
4 commits
Select commit
Hold shift + click to select a range
52a7a88
fix(chat): keep find stable across footnotes and pending chats, and m…
waleedlatif1 4a5d1e8
fix(chat): keep the longest overlapping mention chip and centralize t…
waleedlatif1 b7c2dc5
fix(chat): avoid Array.at in the mention range merge
waleedlatif1 f884d83
fix(chat): only treat a period as a mention boundary when no name con…
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
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
128 changes: 128 additions & 0 deletions
128
apps/sim/app/workspace/[workspaceId]/home/components/mothership-chat/use-chat-find.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,128 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { act, useRef } from 'react' | ||
| import type { Virtualizer } from '@tanstack/react-virtual' | ||
| import { createRoot } from 'react-dom/client' | ||
| import { renderToStaticMarkup } from 'react-dom/server' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { ChatContent } from '@/app/workspace/[workspaceId]/home/components/message-content/components/chat-content/chat-content' | ||
| import { useChatFind } from '@/app/workspace/[workspaceId]/home/components/mothership-chat/use-chat-find' | ||
| import type { ChatMessage } from '@/app/workspace/[workspaceId]/home/types' | ||
|
|
||
| type ChatFind = ReturnType<typeof useChatFind> | ||
|
|
||
| interface HarnessProps { | ||
| chatId?: string | ||
| messages: ChatMessage[] | ||
| onRender: (find: ChatFind) => void | ||
| } | ||
|
|
||
| // double-cast-allowed: the hook only calls scrollToIndex on the virtualizer | ||
| const virtualizer = { scrollToIndex: () => {} } as unknown as Virtualizer<HTMLDivElement, Element> | ||
|
|
||
| function Harness({ chatId, messages, onRender }: HarnessProps) { | ||
| const containerRef = useRef<HTMLDivElement>(null) | ||
| const scrollElementRef = useRef<HTMLDivElement>(null) | ||
| const find = useChatFind({ | ||
| chatId, | ||
| messages, | ||
| hiddenUserByIndex: [], | ||
| containerRef, | ||
| scrollElementRef, | ||
| virtualizer, | ||
| }) | ||
| onRender(find) | ||
| return ( | ||
| <div ref={containerRef} tabIndex={-1} onKeyDown={find.onKeyDown}> | ||
| <div ref={scrollElementRef}> | ||
| {messages.map((message, index) => ( | ||
| <div | ||
| key={message.id} | ||
| data-index={index} | ||
| dangerouslySetInnerHTML={{ | ||
| __html: renderToStaticMarkup(<ChatContent content={message.content} />), | ||
| }} | ||
| /> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const cleanups: (() => void)[] = [] | ||
|
|
||
| function renderFind(props: Omit<HarnessProps, 'onRender'>) { | ||
| let find: ChatFind | undefined | ||
| const host = document.createElement('div') | ||
| document.body.append(host) | ||
| const root = createRoot(host) | ||
| const render = (next: Omit<HarnessProps, 'onRender'>) => | ||
| act(() => root.render(<Harness {...next} onRender={(value) => (find = value)} />)) | ||
| cleanups.push(() => { | ||
| act(() => root.unmount()) | ||
| host.remove() | ||
| }) | ||
| render(props) | ||
| return { | ||
| get current() { | ||
| if (!find) throw new Error('Hook was not rendered') | ||
| return find | ||
| }, | ||
| rerender: render, | ||
| search(query: string) { | ||
| act(() => { | ||
| host.firstElementChild?.dispatchEvent( | ||
| new KeyboardEvent('keydown', { key: 'f', metaKey: true, bubbles: true }) | ||
| ) | ||
| }) | ||
| act(() => find?.onQueryChange(query)) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| function assistant(id: string, content: string): ChatMessage { | ||
| return { id, role: 'assistant', content } | ||
| } | ||
|
|
||
| class TestHighlight extends Set<Range> {} | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubGlobal('Highlight', TestHighlight) | ||
| vi.stubGlobal('CSS', { highlights: new Map<string, TestHighlight>() }) | ||
| Range.prototype.getBoundingClientRect = () => new DOMRect() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| for (const cleanup of cleanups.splice(0)) cleanup() | ||
| vi.unstubAllGlobals() | ||
| Reflect.deleteProperty(Range.prototype, 'getBoundingClientRect') | ||
| }) | ||
|
|
||
| describe('useChatFind', () => { | ||
| it('keeps the open search when a pending chat is persisted, and resets on a chat switch', () => { | ||
| const messages = [assistant('a', 'The answer is here.')] | ||
| const find = renderFind({ messages }) | ||
| find.search('answer') | ||
| expect(find.current).toMatchObject({ isOpen: true, query: 'answer', count: 1 }) | ||
|
|
||
| find.rerender({ chatId: 'chat-1', messages }) | ||
| expect(find.current).toMatchObject({ isOpen: true, query: 'answer', count: 1 }) | ||
|
|
||
| find.rerender({ chatId: 'chat-2', messages }) | ||
| expect(find.current).toMatchObject({ isOpen: false, query: '', count: 0 }) | ||
| }) | ||
|
|
||
| it('counts the same footnoted matches in the index as it highlights in the rendered message', async () => { | ||
| const find = renderFind({ | ||
| chatId: 'chat', | ||
| messages: [assistant('a', 'Chapter 1 cites this[^1].\n\n[^1]: Page 1.')], | ||
| }) | ||
| find.search('1') | ||
| await act(() => new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))) | ||
|
|
||
| const highlighted = CSS.highlights.get('chat-find') | ||
| expect(find.current.count).toBe(1) | ||
| expect(highlighted?.size).toBe(find.current.count) | ||
| }) | ||
| }) |
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
50 changes: 50 additions & 0 deletions
50
apps/sim/app/workspace/[workspaceId]/home/components/user-message-content/utils.test.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,50 @@ | ||
| import { integrationMatcherMock } from '@sim/testing/mocks/integration-matcher.mock' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { computeMentionRanges } from '@/app/workspace/[workspaceId]/home/components/user-message-content/utils' | ||
| import type { ChatMessageContext } from '@/app/workspace/[workspaceId]/home/types' | ||
|
|
||
| vi.mock('@/blocks/integration-matcher', () => integrationMatcherMock) | ||
|
|
||
| function spans(text: string, contexts: ChatMessageContext[]): string[] { | ||
| return computeMentionRanges(text, contexts).map((range) => text.slice(range.start, range.end)) | ||
| } | ||
|
|
||
| describe('computeMentionRanges', () => { | ||
| const workflow: ChatMessageContext = { kind: 'workflow', label: 'Workflow' } | ||
|
|
||
| it('matches a mention followed by punctuation', () => { | ||
| expect(spans('Run @Workflow, then stop.', [workflow])).toEqual(['@Workflow']) | ||
| expect(spans('(see @Workflow)', [workflow])).toEqual(['@Workflow']) | ||
| }) | ||
|
|
||
| it('treats a period as a boundary only when no name continues after it', () => { | ||
| expect(spans('see @Workflow.', [workflow])).toEqual(['@Workflow']) | ||
| expect(spans('open @report.pdf', [{ kind: 'file', label: 'report' }])).toEqual([]) | ||
| }) | ||
|
|
||
| it('matches every repeat of a mention separated by one space', () => { | ||
| expect(spans('@Workflow @Workflow', [workflow])).toEqual(['@Workflow', '@Workflow']) | ||
| }) | ||
|
|
||
| it('does not match a mention that is a prefix of a longer name', () => { | ||
| expect(spans('@Workflow-2', [workflow])).toEqual([]) | ||
| }) | ||
|
|
||
| it('keeps only the longest of overlapping mentions', () => { | ||
| const report: ChatMessageContext = { kind: 'file', label: 'report' } | ||
| const reportPdf: ChatMessageContext = { kind: 'file', label: 'report.pdf' } | ||
| expect(spans('compare @report.pdf with @report', [report, reportPdf])).toEqual([ | ||
| '@report.pdf', | ||
| '@report', | ||
| ]) | ||
| expect( | ||
| spans('@Workflow 2 and @Workflow', [workflow, { kind: 'workflow', label: 'Workflow 2' }]) | ||
| ).toEqual(['@Workflow 2', '@Workflow']) | ||
| }) | ||
|
|
||
| it('matches slash commands by their slash prefix', () => { | ||
| expect(spans('/research the topic', [{ kind: 'slash_command', label: 'research' }])).toEqual([ | ||
| '/research', | ||
| ]) | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { vi } from 'vitest' | ||
|
|
||
| /** | ||
| * Static mock module for `@/blocks/integration-matcher` that knows no integrations, so tests never | ||
| * load the block registry: the matcher has no regex and `mentionifyIntegrations` returns its input. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { integrationMatcherMock } from '@sim/testing/mocks/integration-matcher.mock' | ||
| * | ||
| * vi.mock('@/blocks/integration-matcher', () => integrationMatcherMock) | ||
| * ``` | ||
| */ | ||
| export const integrationMatcherMock = { | ||
| getIntegrationMatcher: vi.fn(() => ({ regex: null, byName: new Map() })), | ||
| mentionifyIntegrations: vi.fn((text: string) => text), | ||
| } |
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.