-
-
Notifications
You must be signed in to change notification settings - Fork 548
feat(web): show a fork preview dialog before forking a conversation #1673
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
Open
junmo-kim
wants to merge
6
commits into
tiann:main
Choose a base branch
from
junmo-kim:feat/fork-preview-dialog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3311f96
test(web): add fork preview confirm dialog e2e spec
junmo-kim 1349259
feat(web): show fork preview dialog before forking a conversation
junmo-kim 7bbaa2e
fix(web): make fork preview replace the generic confirm and match hub…
junmo-kim daef4f6
fix(web): keep fork preview open on failure and scope the boundary to…
junmo-kim 85390e4
fix(web): distinguish unloaded prefix from an empty one in fork preview
junmo-kim 1eb1265
fix(web): use theme-aware text color on fork preview badge and CTA
junmo-kim 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * End-to-end coverage for the fork preview confirm dialog. Drives the | ||
| * production ForkPreviewDialog via the standalone fixture page (no hub): | ||
| * the stub harness on `window.__forkPreviewE2E` counts cancel/confirm | ||
| * callbacks, standing in for the real fork API call. | ||
| */ | ||
|
|
||
| import { expect, test, type Page } from '@playwright/test' | ||
|
|
||
| async function openFixture(page: Page): Promise<void> { | ||
| await page.goto('/e2e-fixtures/fork-preview-fixture.html') | ||
| const dialog = page.getByRole('dialog') | ||
| await expect(dialog).toBeVisible() | ||
| } | ||
|
|
||
| test('shows the kept turns above the fork boundary and the new-session start below', async ({ page }) => { | ||
| await openFixture(page) | ||
| const dialog = page.getByRole('dialog') | ||
| await expect(dialog).toBeVisible() | ||
| await expect(dialog.getByText('first question about pagination')).toBeVisible() | ||
| await expect(dialog.getByText('second question about forking')).toBeVisible() | ||
| await expect(dialog.getByTestId('fork-preview-boundary')).toBeVisible() | ||
| await expect(dialog.getByTestId('fork-preview-boundary-message')).toContainText('third question') | ||
| }) | ||
|
|
||
| test('cancel closes the dialog without confirming the fork', async ({ page }) => { | ||
| await openFixture(page) | ||
| const dialog = page.getByRole('dialog') | ||
| await dialog.getByRole('button', { name: 'Cancel' }).click() | ||
| await expect(page.getByRole('dialog')).toHaveCount(0) | ||
| const harness = await page.evaluate(() => window.__forkPreviewE2E) | ||
| expect(harness?.cancelled).toBe(1) | ||
| expect(harness?.confirmed).toBe(0) | ||
| }) | ||
|
|
||
| test('confirm runs the fork and closes the dialog', async ({ page }) => { | ||
| await openFixture(page) | ||
| const dialog = page.getByRole('dialog') | ||
| await dialog.getByTestId('fork-preview-confirm').click() | ||
| await expect(page.getByRole('dialog')).toHaveCount(0) | ||
| const harness = await page.evaluate(() => window.__forkPreviewE2E) | ||
| expect(harness?.confirmed).toBe(1) | ||
| expect(harness?.cancelled).toBe(0) | ||
| }) | ||
|
|
||
| test('localizes the dialog in Chinese', async ({ page }) => { | ||
| await page.addInitScript(() => localStorage.setItem('hapi-lang', 'zh-CN')) | ||
| await openFixture(page) | ||
| const dialog = page.getByRole('dialog') | ||
| await expect(dialog.getByRole('heading', { name: '从这里开始一个新会话' })).toBeVisible() | ||
| await expect(dialog.getByText('↑ 会复制到新会话中')).toBeVisible() | ||
| await expect(dialog.getByRole('button', { name: '在此分叉' })).toBeVisible() | ||
| }) | ||
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,16 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>HAPI fork preview e2e fixture</title> | ||
| <style> | ||
| html { background: #fff } | ||
| body { margin: 0; padding: 16px; font-family: system-ui, sans-serif } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="./fork-preview-fixture.tsx"></script> | ||
| </body> | ||
| </html> |
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,57 @@ | ||
| /* | ||
| * Standalone Vite-served fixture for the fork preview Playwright spec | ||
| * (scratchlist pattern). Mounts the production ForkPreviewDialog inside | ||
| * an I18nProvider with a stub confirm callback on `window.__forkPreviewE2E` | ||
| * so the spec can assert dialog rendering, the boundary marker, and that | ||
| * cancel/confirm route to the right callback without the hub stack. | ||
| */ | ||
|
|
||
| import React, { useState } from 'react' | ||
| import ReactDOM from 'react-dom/client' | ||
| import '../src/index.css' | ||
| import { I18nProvider } from '../src/lib/i18n-context' | ||
| import { ForkPreviewDialog } from '../src/components/AssistantChat/ForkPreviewDialog' | ||
| import type { ForkPreviewTurn } from '../src/lib/forkPreview' | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| __forkPreviewE2E?: { | ||
| confirmed: number | ||
| cancelled: number | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const KEPT_TURNS: ForkPreviewTurn[] = [ | ||
| { role: 'user', text: 'first question about pagination' }, | ||
| { role: 'assistant', text: 'first answer explaining the boundary' }, | ||
| { role: 'user', text: 'second question about forking' }, | ||
| ] | ||
|
|
||
| function Fixture() { | ||
| const [open, setOpen] = useState(true) | ||
| return ( | ||
| <ForkPreviewDialog | ||
| isOpen={open} | ||
| kind="historical" | ||
| keptTurns={KEPT_TURNS} | ||
| boundaryText="third question — not copied into the new session" | ||
| pending={false} | ||
| onCancel={() => { | ||
| window.__forkPreviewE2E!.cancelled += 1 | ||
| setOpen(false) | ||
| }} | ||
| onConfirm={() => { | ||
| window.__forkPreviewE2E!.confirmed += 1 | ||
| setOpen(false) | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| window.__forkPreviewE2E = { confirmed: 0, cancelled: 0 } | ||
| ReactDOM.createRoot(document.getElementById('root')!).render( | ||
| <I18nProvider> | ||
| <Fixture /> | ||
| </I18nProvider> | ||
| ) |
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,111 @@ | ||
| import { useState } from 'react' | ||
| import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' | ||
| import { useTranslation } from '@/lib/use-translation' | ||
| import type { ForkPreviewKind, ForkPreviewTurn } from '@/lib/forkPreview' | ||
|
|
||
| type ForkPreviewDialogProps = { | ||
| isOpen: boolean | ||
| kind: ForkPreviewKind | ||
| keptTurns: ForkPreviewTurn[] | ||
| boundaryText: string | null | ||
| /** True when older messages exist beyond the loaded window, so an empty | ||
| * prefix does not mean the child starts empty. */ | ||
| prefixMayHaveMore?: boolean | ||
| onCancel: () => void | ||
| onConfirm: () => Promise<void> | ||
| } | ||
|
|
||
| export function ForkPreviewDialog({ isOpen, kind, keptTurns, boundaryText, prefixMayHaveMore = false, onCancel, onConfirm }: ForkPreviewDialogProps) { | ||
| const { t } = useTranslation() | ||
| const [pending, setPending] = useState(false) | ||
| const [error, setError] = useState<string | null>(null) | ||
|
|
||
| const handleConfirm = async () => { | ||
| setError(null) | ||
| setPending(true) | ||
| try { | ||
| await onConfirm() | ||
| } catch (err) { | ||
| setError(err instanceof Error && err.message ? err.message : t('dialog.error.default')) | ||
| } finally { | ||
| setPending(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Dialog open={isOpen} onOpenChange={(open) => { if (!open && !pending) onCancel() }}> | ||
| <DialogContent aria-describedby={undefined} className="flex max-h-[85vh] flex-col gap-3"> | ||
| <DialogHeader> | ||
| <DialogTitle>{t('forkPreview.title')}</DialogTitle> | ||
| </DialogHeader> | ||
| <div className="min-h-0 flex-1 overflow-y-auto rounded-lg border border-[var(--app-subtle-bg)] p-3" data-testid="fork-preview-thread"> | ||
| {keptTurns.length > 0 ? ( | ||
| <div className="flex flex-col gap-2"> | ||
| {keptTurns.map((turn, index) => ( | ||
| <div key={index} className={turn.role === 'user' ? 'self-end rounded-xl bg-[var(--app-subtle-bg)] px-3 py-2 text-sm' : 'self-start text-sm'}> | ||
| <span className="mb-0.5 block text-[10px] uppercase tracking-wide text-[var(--app-hint)]"> | ||
| {t(turn.role === 'user' ? 'forkPreview.roleUser' : 'forkPreview.roleAssistant')} | ||
| </span> | ||
| <span className="line-clamp-3">{turn.text}</span> | ||
| </div> | ||
| ))} | ||
| <div className="text-center text-[10px] text-[var(--app-hint)]"> | ||
| {t('forkPreview.keptAbove')} | ||
| </div> | ||
| </div> | ||
| ) : ( | ||
| <div className="text-center text-xs text-[var(--app-hint)]"> | ||
| {t(prefixMayHaveMore ? 'forkPreview.noTextPreview' : 'forkPreview.emptyPrefix')} | ||
| </div> | ||
| )} | ||
| {kind === 'historical' ? ( | ||
| <> | ||
| <div className="my-3 flex items-center gap-2" data-testid="fork-preview-boundary"> | ||
| <span className="h-px flex-1 bg-[var(--app-link)]" /> | ||
| <span className="rounded-full bg-[var(--app-link)] px-2 py-0.5 text-[10px] font-medium text-white"> | ||
| {t('forkPreview.boundaryBadge')} | ||
| </span> | ||
| <span className="h-px flex-1 bg-[var(--app-link)]" /> | ||
| </div> | ||
| {boundaryText ? ( | ||
| <div className="rounded-xl border-2 border-dashed border-[var(--app-link)] px-3 py-2 text-sm" data-testid="fork-preview-boundary-message"> | ||
| <span className="mb-0.5 block text-[10px] uppercase tracking-wide text-[var(--app-link)]"> | ||
| {t('forkPreview.newSessionStart')} | ||
| </span> | ||
| <span className="line-clamp-2">{boundaryText}</span> | ||
| </div> | ||
| ) : null} | ||
| </> | ||
| ) : null} | ||
| </div> | ||
| <p className="text-xs text-[var(--app-hint)]"> | ||
| {t(kind === 'historical' ? 'forkPreview.below' : 'forkPreview.currentTail')} | ||
| </p> | ||
| {error ? ( | ||
| <div className="rounded-md bg-red-50 p-3 text-sm text-red-600 dark:bg-red-900/20 dark:text-red-400" data-testid="fork-preview-error"> | ||
| {error} | ||
| </div> | ||
| ) : null} | ||
| <div className={`flex gap-2 ${pending ? 'opacity-60' : ''}`}> | ||
| <button | ||
| type="button" | ||
| onClick={onCancel} | ||
| disabled={pending} | ||
| className="flex-1 rounded-lg border border-[var(--app-subtle-bg)] px-3 py-2 text-sm hover:bg-[var(--app-subtle-bg)]" | ||
| > | ||
| {t('forkPreview.cancel')} | ||
| </button> | ||
| <button | ||
| type="button" | ||
| onClick={() => { void handleConfirm() }} | ||
| disabled={pending} | ||
| data-testid="fork-preview-confirm" | ||
| className="flex-1 rounded-lg bg-[var(--app-link)] px-3 py-2 text-sm font-medium text-white hover:opacity-90" | ||
| > | ||
| {t('forkPreview.confirm')} | ||
| </button> | ||
| </div> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ) | ||
| } |
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
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.