diff --git a/docs/guide/file-previews.md b/docs/guide/file-previews.md index fd7db0243c..abece5f428 100644 --- a/docs/guide/file-previews.md +++ b/docs/guide/file-previews.md @@ -15,6 +15,39 @@ without requiring sign-in. Deployments can add binary readers through the optional `artifactPreviews` browser contribution; see the [plugin contract](../full-stack-plugins.md#artifact-previews). +## Quote and rewrite a passage + +Select text in a Markdown artifact or its source editor to open the selection +toolbar. **Add to conversation** attaches the passage to the main composer; +**Ask in side chat** opens a follow-up alongside the conversation. Both carry +the selected text and file path, plus the source line range when available. You can +add several quotations before sending your question. Assistant replies already +offer the same two quotation actions. + +For a writable output file, **Rewrite selection** opens side chat with that +passage. Describe the change and choose **Rewrite selection** in the side-chat +composer. Use the regular send action to ask a question about the passage and +end the pending rewrite. Once the rewrite finishes, choose +**Review replacement**, inspect or adjust the proposed text, and choose +**Apply to draft**. This replaces exactly the selected source range and opens +the file's editor. **Save** publishes the draft using the existing revision +check. Everything before and after the selected range stays unchanged. + +The edit survives switching from the artifact panel to side chat. Changing +the draft, changing its saved revision, or starting a run prevents stale edits +from being applied or saved. Edits stay in memory for the current conversation. +Switching conversations, discarding the side chat, or clearing its selected +references cancels the pending rewrite. +Nothing writes to the file until you explicitly save it. + +Selections support up to 12,000 characters. Markdown may hide formatting markers, +so a rendered selection is rewritable only when the renderer can verify its +exact source range. For other selections, quote the passage or switch to +**View source** to choose an exact range. HTML and CSV/TSV use source selections; +binary previews do not expose text selections. Truncated previews can be quoted +but require **Load full file** before rewriting. Read-only artifacts, demos, +and files outside the output directory cannot be rewritten. + ## Office artifact preview extension The optional [Office preview package](../../examples/deerflow-extension-office-preview/README.md) diff --git a/frontend/src/components/workspace/AGENTS.md b/frontend/src/components/workspace/AGENTS.md index 808ecc3622..78299c9ec8 100644 --- a/frontend/src/components/workspace/AGENTS.md +++ b/frontend/src/components/workspace/AGENTS.md @@ -71,6 +71,15 @@ pnpm catalog:sync # after a change to the capability catalog `@`. Typed slash text is still submitted as a normal message, which the backend and the IM channels rely on. +## Artifact selection + +- **Artifact rewrite state outlives its panel.** Opening side chat unmounts the + file detail. Keep the source range and revision in the sidecar provider, apply + through the shared draft store, and preserve the revision-checked Save path. +- **Verify the source span before rewriting Markdown.** Use positions from the + complete static parse with unchanged preprocessing and literal rendered text. + For other rendered selections, offer quotation and direct rewrites to View source. + ## Delimited preview - **CSV parsing runs in the worker only**, with bounded input, cancellation of diff --git a/frontend/src/components/workspace/artifacts/artifact-file-detail.tsx b/frontend/src/components/workspace/artifacts/artifact-file-detail.tsx index ead26ac434..66bf9c73c1 100644 --- a/frontend/src/components/workspace/artifacts/artifact-file-detail.tsx +++ b/frontend/src/components/workspace/artifacts/artifact-file-detail.tsx @@ -80,6 +80,7 @@ import { } from "./artifact-file-preview"; import { useArtifacts } from "./context"; import { PluginArtifactPreview } from "./plugin-artifact-preview"; +import { useArtifactSelection } from "./use-artifact-selection"; const WRITE_FILE_PREVIEW_REFRESH_INTERVAL_MS = 3000; @@ -271,15 +272,38 @@ export function ArtifactFileDetail({ }, [content, filepath, isWriteFile, setDrafts, sha256, truncated]); const [viewMode, setViewMode] = useState<"code" | "preview">( - artifactViewState.initialViewMode, + isEditing ? "code" : artifactViewState.initialViewMode, ); const [isInstalling, setIsInstalling] = useState(false); const isLoadingFullContent = fullContentRequested && isLoading; const effectiveViewMode = truncated && language === "html" ? "code" : viewMode; + const selection = useArtifactSelection({ + filepath, + threadId, + ownerId: user?.id, + source: editorContent, + revision: activeDraft.baselineSha256, + enabled: + isCodeFile && + !isWriteFile && + !isSkillFile && + !isMock && + !isLoading && + !error && + !thread.isLoading && + !isSaving && + env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY !== "true", + editable: + canEdit && + !thread.isLoading && + !isSaving && + !activeDraft.conflict && + activeDraft.baselineSha256 === sha256, + }); useEffect(() => { - setViewMode(artifactViewState.initialViewMode); - }, [artifactViewState.initialViewMode, filepathFromProps]); + setViewMode(isEditing ? "code" : artifactViewState.initialViewMode); + }, [artifactViewState.initialViewMode, filepathFromProps, isEditing]); const confirmDiscard = useCallback(() => { return !isDirty || window.confirm(t.artifactEditing.discardChanges); @@ -669,6 +693,7 @@ export function ArtifactFileDetail({ + {selection.toolbar} {truncated && !(isTabular && effectiveViewMode === "preview") && (
@@ -712,6 +737,7 @@ export function ArtifactFileDetail({ url={url} truncated={truncated} active={effectiveViewMode === "preview" && !isLoading} + onTextSelection={selection.onTextSelection} /> )} {isCodeFile && @@ -735,6 +761,7 @@ export function ArtifactFileDetail({ })); }} onSave={() => void handleSave()} + onSelectionChange={selection.onSourceSelection} language={language} /> )} diff --git a/frontend/src/components/workspace/artifacts/artifact-file-preview.tsx b/frontend/src/components/workspace/artifacts/artifact-file-preview.tsx index 0efcfea5a4..8c0bac4188 100644 --- a/frontend/src/components/workspace/artifacts/artifact-file-preview.tsx +++ b/frontend/src/components/workspace/artifacts/artifact-file-preview.tsx @@ -14,6 +14,7 @@ import { createHtmlPreviewScrollKey, HTML_PREVIEW_SCROLL_MESSAGE_SOURCE, } from "@/core/artifacts/preview"; +import { rehypeArtifactSourceRanges } from "@/core/artifacts/selection-dom"; import { type ArtifactImageContext, resolveArtifactImageURL, @@ -126,6 +127,7 @@ export function ArtifactFilePreview({ artifact, truncated = false, active = true, + onTextSelection, }: { content: string; language: string; @@ -134,8 +136,17 @@ export function ArtifactFilePreview({ artifact?: ArtifactImageContext; truncated?: boolean; active?: boolean; + onTextSelection?: (container: HTMLElement) => void; }) { const { t } = useI18n(); + const selectionPlugins = useMemo( + () => + [ + ...(artifactMarkdownPlugins.rehypePlugins ?? []), + [rehypeArtifactSourceRanges, { source: content }], + ] as typeof artifactMarkdownPlugins.rehypePlugins, + [content], + ); const { filepath, threadId, isMock } = artifact ?? {}; const transformUrl = useCallback( (src, key, node) => @@ -265,12 +276,24 @@ export function ArtifactFilePreview({ } if (language === "markdown") { return ( -
+
onTextSelection?.(event.currentTarget)} + onKeyUp={(event) => onTextSelection?.(event.currentTarget)} + onTouchEnd={(event) => onTextSelection?.(event.currentTarget)} + data-testid="artifact-markdown-preview" + > diff --git a/frontend/src/components/workspace/artifacts/use-artifact-selection.tsx b/frontend/src/components/workspace/artifacts/use-artifact-selection.tsx new file mode 100644 index 0000000000..5940c90aa5 --- /dev/null +++ b/frontend/src/components/workspace/artifacts/use-artifact-selection.tsx @@ -0,0 +1,211 @@ +"use client"; + +import { + MessageSquareTextIcon, + PencilIcon, + QuoteIcon, + XIcon, +} from "lucide-react"; +import { useEffect, useState } from "react"; +import { createPortal } from "react-dom"; +import { toast } from "sonner"; + +import { Button } from "@/components/ui/button"; +import { + artifactSelectionContext, + MAX_SELECTION_LENGTH, + sourceSelectionOffsets, + type SourceSelection, +} from "@/core/artifacts/selection"; +import { readArtifactSelection } from "@/core/artifacts/selection-dom"; +import { useI18n } from "@/core/i18n/hooks"; +import { uuid } from "@/core/utils/uuid"; + +import { useMaybeSidecar } from "../sidecar/context"; + +type Selection = { + text: string; + range: SourceSelection | null; + exact: SourceSelection | null; + rect: { left: number; bottom: number }; +}; + +export function useArtifactSelection(options: { + filepath: string; + threadId: string; + ownerId?: string; + source: string; + revision: string | null; + enabled: boolean; + editable: boolean; +}) { + const { t } = useI18n(); + const sidecar = useMaybeSidecar(); + const [selection, setSelection] = useState(null); + useEffect(() => { + setSelection(null); + }, [ + options.filepath, + options.threadId, + options.ownerId, + options.source, + options.enabled, + ]); + useEffect(() => { + if (!selection) return; + const escape = (event: KeyboardEvent) => { + if (event.key === "Escape") setSelection(null); + }; + const dismiss = (event: Event) => { + if ( + !(event.target instanceof Element) || + !event.target.closest('[data-testid="artifact-selection-toolbar"]') + ) + setSelection(null); + }; + document.addEventListener("keydown", escape); + document.addEventListener("pointerdown", dismiss); + document.addEventListener("scroll", dismiss, true); + return () => { + document.removeEventListener("keydown", escape); + document.removeEventListener("pointerdown", dismiss); + document.removeEventListener("scroll", dismiss, true); + }; + }, [selection]); + + const accept = (next: Selection | null) => { + if (!options.enabled || !sidecar || !next?.text.trim()) { + setSelection(null); + return; + } + if (next.text.length > MAX_SELECTION_LENGTH) { + setSelection(null); + toast.error(t.artifactSelection.limit); + return; + } + setSelection(next); + }; + const dismiss = () => { + setSelection(null); + window.getSelection()?.removeAllRanges(); + }; + const context = selection + ? artifactSelectionContext( + options.filepath, + options.source, + selection.text, + selection.exact ?? selection.range, + ) + : null; + const rewrite = () => { + if ( + !selection?.exact || + !context || + !sidecar || + !options.editable || + !options.revision + ) + return; + sidecar.beginArtifactEdit({ + id: uuid(), + context, + filepath: options.filepath, + threadId: options.threadId, + ownerId: options.ownerId, + source: options.source, + revision: options.revision, + ...selection.exact, + }); + dismiss(); + }; + + return { + onTextSelection: + options.enabled && sidecar + ? (container: HTMLElement) => + accept(readArtifactSelection(container, options.source)) + : undefined, + onSourceSelection: ( + next: (SourceSelection & { rect: Selection["rect"] }) | null, + ) => { + if (!next) return accept(null); + const exact = sourceSelectionOffsets(options.source, next); + accept({ + text: options.source.slice(exact.from, exact.to), + range: exact, + exact, + rect: next.rect, + }); + }, + toolbar: + selection && context && sidecar && options.enabled + ? createPortal( +
event.preventDefault()} + > + + + {options.editable && ( + + )} + +
, + document.body, + ) + : null, + }; +} diff --git a/frontend/src/components/workspace/code-editor.tsx b/frontend/src/components/workspace/code-editor.tsx index ff5414f662..ab66f02cca 100644 --- a/frontend/src/components/workspace/code-editor.tsx +++ b/frontend/src/components/workspace/code-editor.tsx @@ -21,6 +21,7 @@ export function CodeEditor({ onSave, settings, language, + onSelectionChange, }: { className?: string; placeholder?: string; @@ -32,6 +33,13 @@ export function CodeEditor({ onSave?: () => void; settings?: unknown; language?: string; + onSelectionChange?: ( + selection: { + from: number; + to: number; + rect: { left: number; bottom: number }; + } | null, + ) => void; }) { const { thread: { isLoading }, @@ -83,7 +91,12 @@ export function CodeEditor({ /> ) : ( { + if ( + !onSelectionChange || + (!update.selectionSet && !update.docChanged) + ) + return; + const range = update.state.selection.main; + const rect = update.view.coordsAtPos(range.to); + onSelectionChange( + !update.docChanged && + !range.empty && + update.state.selection.ranges.length === 1 && + rect + ? { from: range.from, to: range.to, rect } + : null, + ); + }} value={value} /> )} diff --git a/frontend/src/components/workspace/sidecar/artifact-rewrite-review.tsx b/frontend/src/components/workspace/sidecar/artifact-rewrite-review.tsx new file mode 100644 index 0000000000..1e8164963f --- /dev/null +++ b/frontend/src/components/workspace/sidecar/artifact-rewrite-review.tsx @@ -0,0 +1,189 @@ +"use client"; + +import type { Message } from "@langchain/langgraph-sdk"; +import { useEffect, useState } from "react"; +import { toast } from "sonner"; + +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { Textarea } from "@/components/ui/textarea"; +import { useArtifactContent } from "@/core/artifacts/hooks"; +import { + artifactReplacement, + applyArtifactSelectionDraft, + MAX_REPLACEMENT_LENGTH, + type ArtifactSelectionEdit, +} from "@/core/artifacts/selection"; +import { useAuth } from "@/core/auth/AuthProvider"; +import { useI18n } from "@/core/i18n/hooks"; + +import { useArtifacts } from "../artifacts/context"; +import { useThread } from "../messages/context"; + +import { useSidecar } from "./context"; + +export function ArtifactRewriteReview({ + edit, + messages, + disabled, + onCancel, +}: { + edit: ArtifactSelectionEdit; + messages: Message[]; + disabled: boolean; + onCancel: () => void; +}) { + const { t } = useI18n(); + const { user } = useAuth(); + const { thread } = useThread(); + const sidecar = useSidecar(); + const clearArtifactEdit = sidecar.clearArtifactEdit; + const { + drafts, + setDrafts, + setEditingPath, + select, + setOpen: openArtifacts, + } = useArtifacts(); + const authorized = + edit.ownerId === user?.id && edit.threadId === sidecar.parentThreadId; + const artifact = useArtifactContent({ + filepath: edit.filepath, + threadId: edit.threadId, + enabled: authorized, + }); + const canApply = + authorized && + !disabled && + !thread.isLoading && + !artifact.isLoading && + !artifact.error && + artifact.sha256 === edit.revision; + const draft = drafts[edit.filepath]; + const changed = + !draft || + draft.conflict || + draft.draftContent !== edit.source || + draft.baselineSha256 !== edit.revision || + (artifact.sha256 !== undefined && artifact.sha256 !== edit.revision); + const [open, setOpen] = useState(false); + const [replacement, setReplacement] = useState(""); + const candidate = disabled ? null : artifactReplacement(messages, edit.id); + useEffect(() => { + if (!authorized) clearArtifactEdit(edit.id); + }, [authorized, edit.id, clearArtifactEdit]); + if (!authorized) return null; + return ( + <> +
+

+ {edit.context.label} +

+

+ {t.artifactSelection.instructions} +

+
+ + +
+
+ + + + {t.artifactSelection.review} + + {edit.context.label} + + +