Unverified 提交 2a7f5a89 authored 作者: Mohamed Aziz Mejri's avatar Mohamed Aziz Mejri 提交者: GitHub

feat: multi-component-selector (#1728)

<!-- This is an auto-generated description by cubic. --> ## Summary by cubic Adds multi-component selection in the preview and sends all selected components to chat for targeted edits. Updates overlays, UI, and IPC to support arrays, smarter context focusing, and cross-platform path normalization. - **New Features** - Select multiple components in the iframe; selection mode stays active until you deactivate it. - Show a scrollable list of selections with remove buttons and a Clear all; remove from the list or click an overlay in the preview to deselect. Sending clears all overlays. - Separate hover vs selected overlays with labels on hover; overlays persist after deactivation and re-position on layout changes/resizes. - Chat input and streaming now send selectedComponents; server builds per-component snippets and focuses their files in smart context. - **Migration** - Replace selectedComponentPreviewAtom with selectedComponentsPreviewAtom (ComponentSelection[]). - ChatStreamParams now uses selectedComponents; migrate any single-selection usages. - previewIframeRefAtom added for clearing overlays from the parent. <sup>Written for commit da0d64cc9e9f83fbf4b975278f6c869f0d3a8c7d. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
上级 c4591996
...@@ -522,8 +522,15 @@ export class PageObject { ...@@ -522,8 +522,15 @@ export class PageObject {
.click({ timeout: Timeout.EXTRA_LONG }); .click({ timeout: Timeout.EXTRA_LONG });
} }
async clickDeselectComponent() { async clickDeselectComponent(options?: { index?: number }) {
await this.page.getByRole("button", { name: "Deselect component" }).click(); const buttons = this.page.getByRole("button", {
name: "Deselect component",
});
if (options?.index !== undefined) {
await buttons.nth(options.index).click();
} else {
await buttons.first().click();
}
} }
async clickPreviewMoreOptions() { async clickPreviewMoreOptions() {
...@@ -582,12 +589,12 @@ export class PageObject { ...@@ -582,12 +589,12 @@ export class PageObject {
await expect(this.getChatInputContainer()).toMatchAriaSnapshot(); await expect(this.getChatInputContainer()).toMatchAriaSnapshot();
} }
getSelectedComponentDisplay() { getSelectedComponentsDisplay() {
return this.page.getByTestId("selected-component-display"); return this.page.getByTestId("selected-component-display");
} }
async snapshotSelectedComponentDisplay() { async snapshotSelectedComponentsDisplay() {
await expect(this.getSelectedComponentDisplay()).toMatchAriaSnapshot(); await expect(this.getSelectedComponentsDisplay()).toMatchAriaSnapshot();
} }
async snapshotPreview({ name }: { name?: string } = {}) { async snapshotPreview({ name }: { name?: string } = {}) {
......
...@@ -14,11 +14,11 @@ testSkipIfWindows("select component", async ({ po }) => { ...@@ -14,11 +14,11 @@ testSkipIfWindows("select component", async ({ po }) => {
.click(); .click();
await po.snapshotPreview(); await po.snapshotPreview();
await po.snapshotSelectedComponentDisplay(); await po.snapshotSelectedComponentsDisplay();
await po.sendPrompt("[dump] make it smaller"); await po.sendPrompt("[dump] make it smaller");
await po.snapshotPreview(); await po.snapshotPreview();
await expect(po.getSelectedComponentDisplay()).not.toBeVisible(); await expect(po.getSelectedComponentsDisplay()).not.toBeVisible();
await po.snapshotServerDump("all-messages"); await po.snapshotServerDump("all-messages");
...@@ -27,6 +27,34 @@ testSkipIfWindows("select component", async ({ po }) => { ...@@ -27,6 +27,34 @@ testSkipIfWindows("select component", async ({ po }) => {
await po.snapshotServerDump("last-message"); await po.snapshotServerDump("last-message");
}); });
testSkipIfWindows("select multiple components", async ({ po }) => {
await po.setUp();
await po.sendPrompt("tc=basic");
await po.clickTogglePreviewPanel();
await po.clickPreviewPickElement();
await po
.getPreviewIframeElement()
.contentFrame()
.getByRole("heading", { name: "Welcome to Your Blank App" })
.click();
await po
.getPreviewIframeElement()
.contentFrame()
.getByText("Made with Dyad")
.click();
await po.snapshotPreview();
await po.snapshotSelectedComponentsDisplay();
await po.sendPrompt("[dump] make both smaller");
await po.snapshotPreview();
await expect(po.getSelectedComponentsDisplay()).not.toBeVisible();
await po.snapshotServerDump("last-message");
});
testSkipIfWindows("deselect component", async ({ po }) => { testSkipIfWindows("deselect component", async ({ po }) => {
await po.setUp(); await po.setUp();
await po.sendPrompt("tc=basic"); await po.sendPrompt("tc=basic");
...@@ -40,19 +68,50 @@ testSkipIfWindows("deselect component", async ({ po }) => { ...@@ -40,19 +68,50 @@ testSkipIfWindows("deselect component", async ({ po }) => {
.click(); .click();
await po.snapshotPreview(); await po.snapshotPreview();
await po.snapshotSelectedComponentDisplay(); await po.snapshotSelectedComponentsDisplay();
// Deselect the component and make sure the state has reverted // Deselect the component and make sure the state has reverted
await po.clickDeselectComponent(); await po.clickDeselectComponent();
await po.snapshotPreview(); await po.snapshotPreview();
await expect(po.getSelectedComponentDisplay()).not.toBeVisible(); await expect(po.getSelectedComponentsDisplay()).not.toBeVisible();
// Send one more prompt to make sure it's a normal message. // Send one more prompt to make sure it's a normal message.
await po.sendPrompt("[dump] tc=basic"); await po.sendPrompt("[dump] tc=basic");
await po.snapshotServerDump("last-message"); await po.snapshotServerDump("last-message");
}); });
testSkipIfWindows(
"deselect individual component from multiple",
async ({ po }) => {
await po.setUp();
await po.sendPrompt("tc=basic");
await po.clickTogglePreviewPanel();
await po.clickPreviewPickElement();
await po
.getPreviewIframeElement()
.contentFrame()
.getByRole("heading", { name: "Welcome to Your Blank App" })
.click();
await po
.getPreviewIframeElement()
.contentFrame()
.getByText("Made with Dyad")
.click();
await po.snapshotSelectedComponentsDisplay();
await po.clickDeselectComponent({ index: 0 });
await po.snapshotPreview();
await po.snapshotSelectedComponentsDisplay();
await expect(po.getSelectedComponentsDisplay()).toBeVisible();
},
);
testSkipIfWindows("upgrade app to select component", async ({ po }) => { testSkipIfWindows("upgrade app to select component", async ({ po }) => {
await po.setUp(); await po.setUp();
await po.importApp("select-component"); await po.importApp("select-component");
...@@ -94,7 +153,7 @@ testSkipIfWindows("select component next.js", async ({ po }) => { ...@@ -94,7 +153,7 @@ testSkipIfWindows("select component next.js", async ({ po }) => {
.click(); .click();
await po.snapshotPreview(); await po.snapshotPreview();
await po.snapshotSelectedComponentDisplay(); await po.snapshotSelectedComponentsDisplay();
await po.sendPrompt("[dump] make it smaller"); await po.sendPrompt("[dump] make it smaller");
await po.snapshotPreview(); await po.snapshotPreview();
......
...@@ -5,5 +5,4 @@ ...@@ -5,5 +5,4 @@
- paragraph: Start building your amazing project here! - paragraph: Start building your amazing project here!
- link "Made with Dyad": - link "Made with Dyad":
- /url: https://www.dyad.sh/ - /url: https://www.dyad.sh/
- img - text: h1 src/pages/Index.tsx
- text: Edit with AI h1 src/pages/Index.tsx \ No newline at end of file
\ No newline at end of file
- text: Selected Components (1)
- button "Clear all"
- img - img
- text: h1 src/pages/Index.tsx:9 - text: h1 src/pages/Index.tsx:9
- button "Deselect component": - button "Deselect component":
......
- text: Selected Components (2)
- button "Clear all"
- img
- text: h1 src/pages/Index.tsx:9
- button "Deselect component":
- img
- img
- text: a src/components/made-with-dyad.tsx:4
- button "Deselect component":
- img
\ No newline at end of file
- region "Notifications (F8)":
- list
- region "Notifications alt+T"
- heading "Welcome to Your Blank App" [level=1]
- paragraph: Start building your amazing project here!
- link "Made with Dyad":
- /url: https://www.dyad.sh/
- text: a src/components/made-with-dyad.tsx
\ No newline at end of file
- text: Selected Components (1)
- button "Clear all"
- img
- text: a src/components/made-with-dyad.tsx:4
- button "Deselect component":
- img
\ No newline at end of file
...@@ -5,5 +5,4 @@ ...@@ -5,5 +5,4 @@
- paragraph: Start building your amazing project here! - paragraph: Start building your amazing project here!
- link "Made with Dyad": - link "Made with Dyad":
- /url: https://www.dyad.sh/ - /url: https://www.dyad.sh/
- img - text: h1 src/pages/Index.tsx
- text: Edit with AI h1 src/pages/Index.tsx \ No newline at end of file
\ No newline at end of file
...@@ -104,7 +104,9 @@ message: This is a simple basic response ...@@ -104,7 +104,9 @@ message: This is a simple basic response
role: user role: user
message: [dump] make it smaller message: [dump] make it smaller
Selected component: h1 (file: src/pages/Index.tsx) Selected components:
Component: h1 (file: src/pages/Index.tsx)
Snippet: Snippet:
``` ```
......
- text: Selected Components (1)
- button "Clear all"
- img - img
- text: h1 src/pages/Index.tsx:9 - text: h1 src/pages/Index.tsx:9
- button "Deselect component": - button "Deselect component":
......
- text: Edit with AI h1 src/app/page.tsx - main:
\ No newline at end of file - heading "Blank page" [level=1]
- link "Made with Dyad":
- /url: https://www.dyad.sh/
- text: h1 src/app/page.tsx
- alert
- button "Open Next.js Dev Tools":
- img
\ No newline at end of file
...@@ -151,7 +151,9 @@ message: This is a simple basic response ...@@ -151,7 +151,9 @@ message: This is a simple basic response
role: user role: user
message: [dump] make it smaller message: [dump] make it smaller
Selected component: h1 (file: src/app/page.tsx) Selected components:
Component: h1 (file: src/app/page.tsx)
Snippet: Snippet:
``` ```
......
- text: Selected Components (1)
- button "Clear all"
- img - img
- text: h1 src/app/page.tsx:7 - text: h1 src/app/page.tsx:7
- button "Deselect component": - button "Deselect component":
......
...@@ -2,3 +2,6 @@ ...@@ -2,3 +2,6 @@
- heading "Blank page" [level=1] - heading "Blank page" [level=1]
- link "Made with Dyad": - link "Made with Dyad":
- /url: https://www.dyad.sh/ - /url: https://www.dyad.sh/
- alert
- button "Open Next.js Dev Tools":
- img
\ No newline at end of file
- region "Notifications (F8)":
- list
- region "Notifications alt+T"
- heading "Welcome to Your Blank App" [level=1]
- paragraph: Start building your amazing project here!
- link "Made with Dyad":
- /url: https://www.dyad.sh/
- text: a src/components/made-with-dyad.tsx
\ No newline at end of file
===
role: user
message: [dump] make both smaller
Selected components:
1. Component: h1 (file: src/pages/Index.tsx)
Snippet:
```
<div className="text-center">
<h1 className="text-4xl font-bold mb-4">Welcome to Your Blank App</h1> // <-- EDIT HERE
<p className="text-xl text-gray-600">
Start building your amazing project here!
</p>
```
2. Component: a (file: src/components/made-with-dyad.tsx)
Snippet:
```
<div className="p-4 text-center">
<a // <-- EDIT HERE
href="https://www.dyad.sh/"
target="_blank"
rel="noopener noreferrer"
```
\ No newline at end of file
- text: Selected Components (2)
- button "Clear all"
- img
- text: h1 src/pages/Index.tsx:9
- button "Deselect component":
- img
- img
- text: a src/components/made-with-dyad.tsx:4
- button "Deselect component":
- img
\ No newline at end of file
- region "Notifications (F8)":
- list
- region "Notifications alt+T"
- heading "Welcome to Your Blank App" [level=1]
- paragraph: Start building your amazing project here!
- link "Made with Dyad":
- /url: https://www.dyad.sh/
\ No newline at end of file
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
role: user role: user
message: [dump] make it smaller message: [dump] make it smaller
Selected component: h1 (file: src/pages/Index.tsx) Selected components:
Component: h1 (file: src/pages/Index.tsx)
Snippet: Snippet:
``` ```
......
import { ComponentSelection } from "@/ipc/ipc_types"; import { ComponentSelection } from "@/ipc/ipc_types";
import { atom } from "jotai"; import { atom } from "jotai";
export const selectedComponentPreviewAtom = atom<ComponentSelection | null>( export const selectedComponentsPreviewAtom = atom<ComponentSelection[]>([]);
null,
); export const previewIframeRefAtom = atom<HTMLIFrameElement | null>(null);
...@@ -61,8 +61,11 @@ import { FileAttachmentDropdown } from "./FileAttachmentDropdown"; ...@@ -61,8 +61,11 @@ import { FileAttachmentDropdown } from "./FileAttachmentDropdown";
import { showError, showExtraFilesToast } from "@/lib/toast"; import { showError, showExtraFilesToast } from "@/lib/toast";
import { ChatInputControls } from "../ChatInputControls"; import { ChatInputControls } from "../ChatInputControls";
import { ChatErrorBox } from "./ChatErrorBox"; import { ChatErrorBox } from "./ChatErrorBox";
import { selectedComponentPreviewAtom } from "@/atoms/previewAtoms"; import {
import { SelectedComponentDisplay } from "./SelectedComponentDisplay"; selectedComponentsPreviewAtom,
previewIframeRefAtom,
} from "@/atoms/previewAtoms";
import { SelectedComponentsDisplay } from "./SelectedComponentDisplay";
import { useCheckProblems } from "@/hooks/useCheckProblems"; import { useCheckProblems } from "@/hooks/useCheckProblems";
import { LexicalChatInput } from "./LexicalChatInput"; import { LexicalChatInput } from "./LexicalChatInput";
import { useChatModeToggle } from "@/hooks/useChatModeToggle"; import { useChatModeToggle } from "@/hooks/useChatModeToggle";
...@@ -84,9 +87,10 @@ export function ChatInput({ chatId }: { chatId?: number }) { ...@@ -84,9 +87,10 @@ export function ChatInput({ chatId }: { chatId?: number }) {
const setMessagesById = useSetAtom(chatMessagesByIdAtom); const setMessagesById = useSetAtom(chatMessagesByIdAtom);
const setIsPreviewOpen = useSetAtom(isPreviewOpenAtom); const setIsPreviewOpen = useSetAtom(isPreviewOpenAtom);
const [showTokenBar, setShowTokenBar] = useAtom(showTokenBarAtom); const [showTokenBar, setShowTokenBar] = useAtom(showTokenBarAtom);
const [selectedComponent, setSelectedComponent] = useAtom( const [selectedComponents, setSelectedComponents] = useAtom(
selectedComponentPreviewAtom, selectedComponentsPreviewAtom,
); );
const previewIframeRef = useAtomValue(previewIframeRefAtom);
const { checkProblems } = useCheckProblems(appId); const { checkProblems } = useCheckProblems(appId);
// Use the attachments hook // Use the attachments hook
const { const {
...@@ -148,7 +152,21 @@ export function ChatInput({ chatId }: { chatId?: number }) { ...@@ -148,7 +152,21 @@ export function ChatInput({ chatId }: { chatId?: number }) {
const currentInput = inputValue; const currentInput = inputValue;
setInputValue(""); setInputValue("");
setSelectedComponent(null);
// Use all selected components for multi-component editing
const componentsToSend =
selectedComponents && selectedComponents.length > 0
? selectedComponents
: [];
setSelectedComponents([]);
// Clear overlays in the preview iframe
if (previewIframeRef?.contentWindow) {
previewIframeRef.contentWindow.postMessage(
{ type: "clear-dyad-component-overlays" },
"*",
);
}
// Send message with attachments and clear them after sending // Send message with attachments and clear them after sending
await streamMessage({ await streamMessage({
...@@ -156,7 +174,7 @@ export function ChatInput({ chatId }: { chatId?: number }) { ...@@ -156,7 +174,7 @@ export function ChatInput({ chatId }: { chatId?: number }) {
chatId, chatId,
attachments, attachments,
redo: false, redo: false,
selectedComponent, selectedComponents: componentsToSend,
}); });
clearAttachments(); clearAttachments();
posthog.capture("chat:submit"); posthog.capture("chat:submit");
...@@ -288,7 +306,7 @@ export function ChatInput({ chatId }: { chatId?: number }) { ...@@ -288,7 +306,7 @@ export function ChatInput({ chatId }: { chatId?: number }) {
/> />
)} )}
<SelectedComponentDisplay /> <SelectedComponentsDisplay />
{/* Use the AttachmentsList component */} {/* Use the AttachmentsList component */}
<AttachmentsList <AttachmentsList
......
import { selectedComponentPreviewAtom } from "@/atoms/previewAtoms"; import {
import { useAtom } from "jotai"; selectedComponentsPreviewAtom,
previewIframeRefAtom,
} from "@/atoms/previewAtoms";
import { useAtom, useAtomValue } from "jotai";
import { Code2, X } from "lucide-react"; import { Code2, X } from "lucide-react";
export function SelectedComponentDisplay() { export function SelectedComponentsDisplay() {
const [selectedComponent, setSelectedComponent] = useAtom( const [selectedComponents, setSelectedComponents] = useAtom(
selectedComponentPreviewAtom, selectedComponentsPreviewAtom,
); );
const previewIframeRef = useAtomValue(previewIframeRefAtom);
if (!selectedComponent) { const handleRemoveComponent = (index: number) => {
const componentToRemove = selectedComponents[index];
const newComponents = selectedComponents.filter((_, i) => i !== index);
setSelectedComponents(newComponents);
// Remove the specific overlay from the iframe
if (previewIframeRef?.contentWindow) {
previewIframeRef.contentWindow.postMessage(
{
type: "remove-dyad-component-overlay",
componentId: componentToRemove.id,
},
"*",
);
}
};
const handleClearAll = () => {
setSelectedComponents([]);
if (previewIframeRef?.contentWindow) {
previewIframeRef.contentWindow.postMessage(
{ type: "clear-dyad-component-overlays" },
"*",
);
}
};
if (!selectedComponents || selectedComponents.length === 0) {
return null; return null;
} }
return ( return (
<div className="p-2 pb-1" data-testid="selected-component-display"> <div
className="p-2 pb-1 max-h-[180px] overflow-y-auto"
data-testid="selected-component-display"
>
<div className="flex items-center justify-between mb-2 px-1">
<span className="text-xs font-medium text-muted-foreground">
Selected Components ({selectedComponents.length})
</span>
<button
onClick={handleClearAll}
className="text-xs text-muted-foreground hover:text-foreground transition-colors"
title="Clear all selected components"
>
Clear all
</button>
</div>
{selectedComponents.map((selectedComponent, index) => (
<div key={selectedComponent.id} className="mb-1 last:mb-0">
<div className="flex items-center justify-between rounded-md bg-indigo-600/10 px-2 py-1 text-sm"> <div className="flex items-center justify-between rounded-md bg-indigo-600/10 px-2 py-1 text-sm">
<div className="flex items-center gap-2 overflow-hidden"> <div className="flex items-center gap-2 overflow-hidden">
<Code2 <Code2
...@@ -30,12 +79,13 @@ export function SelectedComponentDisplay() { ...@@ -30,12 +79,13 @@ export function SelectedComponentDisplay() {
className="truncate text-xs text-indigo-600/80 dark:text-indigo-400/80" className="truncate text-xs text-indigo-600/80 dark:text-indigo-400/80"
title={`${selectedComponent.relativePath}:${selectedComponent.lineNumber}`} title={`${selectedComponent.relativePath}:${selectedComponent.lineNumber}`}
> >
{selectedComponent.relativePath}:{selectedComponent.lineNumber} {selectedComponent.relativePath}:
{selectedComponent.lineNumber}
</span> </span>
</div> </div>
</div> </div>
<button <button
onClick={() => setSelectedComponent(null)} onClick={() => handleRemoveComponent(index)}
className="ml-2 flex-shrink-0 rounded-full p-0.5 hover:bg-indigo-600/20" className="ml-2 flex-shrink-0 rounded-full p-0.5 hover:bg-indigo-600/20"
title="Deselect component" title="Deselect component"
> >
...@@ -43,5 +93,7 @@ export function SelectedComponentDisplay() { ...@@ -43,5 +93,7 @@ export function SelectedComponentDisplay() {
</button> </button>
</div> </div>
</div> </div>
))}
</div>
); );
} }
...@@ -35,7 +35,10 @@ import { ...@@ -35,7 +35,10 @@ import {
DropdownMenuTrigger, DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"; } from "@/components/ui/dropdown-menu";
import { useStreamChat } from "@/hooks/useStreamChat"; import { useStreamChat } from "@/hooks/useStreamChat";
import { selectedComponentPreviewAtom } from "@/atoms/previewAtoms"; import {
selectedComponentsPreviewAtom,
previewIframeRefAtom,
} from "@/atoms/previewAtoms";
import { ComponentSelection } from "@/ipc/ipc_types"; import { ComponentSelection } from "@/ipc/ipc_types";
import { import {
Tooltip, Tooltip,
...@@ -52,6 +55,7 @@ import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; ...@@ -52,6 +55,7 @@ import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { useRunApp } from "@/hooks/useRunApp"; import { useRunApp } from "@/hooks/useRunApp";
import { useShortcut } from "@/hooks/useShortcut"; import { useShortcut } from "@/hooks/useShortcut";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { normalizePath } from "../../../shared/normalizePath";
interface ErrorBannerProps { interface ErrorBannerProps {
error: { message: string; source: "preview-app" | "dyad-app" } | undefined; error: { message: string; source: "preview-app" | "dyad-app" } | undefined;
...@@ -169,9 +173,10 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => { ...@@ -169,9 +173,10 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => {
const [canGoForward, setCanGoForward] = useState(false); const [canGoForward, setCanGoForward] = useState(false);
const [navigationHistory, setNavigationHistory] = useState<string[]>([]); const [navigationHistory, setNavigationHistory] = useState<string[]>([]);
const [currentHistoryPosition, setCurrentHistoryPosition] = useState(0); const [currentHistoryPosition, setCurrentHistoryPosition] = useState(0);
const [selectedComponentPreview, setSelectedComponentPreview] = useAtom( const [selectedComponentsPreview, setSelectedComponentsPreview] = useAtom(
selectedComponentPreviewAtom, selectedComponentsPreviewAtom,
); );
const setPreviewIframeRef = useSetAtom(previewIframeRefAtom);
const iframeRef = useRef<HTMLIFrameElement>(null); const iframeRef = useRef<HTMLIFrameElement>(null);
const [isPicking, setIsPicking] = useState(false); const [isPicking, setIsPicking] = useState(false);
...@@ -189,9 +194,14 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => { ...@@ -189,9 +194,14 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => {
//detect if the user is using Mac //detect if the user is using Mac
const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0; const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
// Update iframe ref atom
useEffect(() => {
setPreviewIframeRef(iframeRef.current);
}, [iframeRef.current, setPreviewIframeRef]);
// Deactivate component selector when selection is cleared // Deactivate component selector when selection is cleared
useEffect(() => { useEffect(() => {
if (!selectedComponentPreview) { if (!selectedComponentsPreview || selectedComponentsPreview.length === 0) {
if (iframeRef.current?.contentWindow) { if (iframeRef.current?.contentWindow) {
iframeRef.current.contentWindow.postMessage( iframeRef.current.contentWindow.postMessage(
{ type: "deactivate-dyad-component-selector" }, { type: "deactivate-dyad-component-selector" },
...@@ -200,7 +210,7 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => { ...@@ -200,7 +210,7 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => {
} }
setIsPicking(false); setIsPicking(false);
} }
}, [selectedComponentPreview]); }, [selectedComponentsPreview]);
// Add message listener for iframe errors and navigation events // Add message listener for iframe errors and navigation events
useEffect(() => { useEffect(() => {
...@@ -217,8 +227,37 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => { ...@@ -217,8 +227,37 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => {
if (event.data?.type === "dyad-component-selected") { if (event.data?.type === "dyad-component-selected") {
console.log("Component picked:", event.data); console.log("Component picked:", event.data);
setSelectedComponentPreview(parseComponentSelection(event.data));
setIsPicking(false); // Parse the single selected component
const component = event.data.component
? parseComponentSelection({
type: "dyad-component-selected",
id: event.data.component.id,
name: event.data.component.name,
})
: null;
if (!component) return;
// Add to existing components, avoiding duplicates by id
setSelectedComponentsPreview((prev) => {
// Check if this component is already selected
if (prev.some((c) => c.id === component.id)) {
return prev;
}
return [...prev, component];
});
return;
}
if (event.data?.type === "dyad-component-deselected") {
const componentId = event.data.componentId;
if (componentId) {
setSelectedComponentsPreview((prev) =>
prev.filter((c) => c.id !== componentId),
);
}
return; return;
} }
...@@ -306,7 +345,7 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => { ...@@ -306,7 +345,7 @@ export const PreviewIframe = ({ loading }: { loading: boolean }) => {
errorMessage, errorMessage,
setErrorMessage, setErrorMessage,
setIsComponentSelectorInitialized, setIsComponentSelectorInitialized,
setSelectedComponentPreview, setSelectedComponentsPreview,
]); ]);
useEffect(() => { useEffect(() => {
...@@ -742,7 +781,7 @@ function parseComponentSelection(data: any): ComponentSelection | null { ...@@ -742,7 +781,7 @@ function parseComponentSelection(data: any): ComponentSelection | null {
return { return {
id, id,
name, name,
relativePath, relativePath: normalizePath(relativePath),
lineNumber, lineNumber,
columnNumber, columnNumber,
}; };
......
...@@ -69,14 +69,14 @@ export function useStreamChat({ ...@@ -69,14 +69,14 @@ export function useStreamChat({
chatId, chatId,
redo, redo,
attachments, attachments,
selectedComponent, selectedComponents,
onSettled, onSettled,
}: { }: {
prompt: string; prompt: string;
chatId: number; chatId: number;
redo?: boolean; redo?: boolean;
attachments?: FileAttachment[]; attachments?: FileAttachment[];
selectedComponent?: ComponentSelection | null; selectedComponents?: ComponentSelection[];
onSettled?: () => void; onSettled?: () => void;
}) => { }) => {
if ( if (
...@@ -106,7 +106,7 @@ export function useStreamChat({ ...@@ -106,7 +106,7 @@ export function useStreamChat({
let hasIncrementedStreamCount = false; let hasIncrementedStreamCount = false;
try { try {
IpcClient.getInstance().streamMessage(prompt, { IpcClient.getInstance().streamMessage(prompt, {
selectedComponent: selectedComponent ?? null, selectedComponents: selectedComponents ?? [],
chatId, chatId,
redo, redo,
attachments, attachments,
......
...@@ -351,18 +351,21 @@ export function registerChatStreamHandlers() { ...@@ -351,18 +351,21 @@ export function registerChatStreamHandlers() {
} catch (e) { } catch (e) {
logger.error("Failed to inline referenced prompts:", e); logger.error("Failed to inline referenced prompts:", e);
} }
if (req.selectedComponent) {
const componentsToProcess = req.selectedComponents || [];
if (componentsToProcess.length > 0) {
userPrompt += "\n\nSelected components:\n";
for (const component of componentsToProcess) {
let componentSnippet = "[component snippet not available]"; let componentSnippet = "[component snippet not available]";
try { try {
const componentFileContent = await readFile( const componentFileContent = await readFile(
path.join( path.join(getDyadAppPath(chat.app.path), component.relativePath),
getDyadAppPath(chat.app.path),
req.selectedComponent.relativePath,
),
"utf8", "utf8",
); );
const lines = componentFileContent.split("\n"); const lines = componentFileContent.split(/\r?\n/);
const selectedIndex = req.selectedComponent.lineNumber - 1; const selectedIndex = component.lineNumber - 1;
// Let's get one line before and three after for context. // Let's get one line before and three after for context.
const startIndex = Math.max(0, selectedIndex - 1); const startIndex = Math.max(0, selectedIndex - 1);
...@@ -378,10 +381,12 @@ export function registerChatStreamHandlers() { ...@@ -378,10 +381,12 @@ export function registerChatStreamHandlers() {
componentSnippet = snippetLines.join("\n"); componentSnippet = snippetLines.join("\n");
} catch (err) { } catch (err) {
logger.error(`Error reading selected component file content: ${err}`); logger.error(
`Error reading selected component file content: ${err}`,
);
} }
userPrompt += `\n\nSelected component: ${req.selectedComponent.name} (file: ${req.selectedComponent.relativePath}) userPrompt += `\n${componentsToProcess.length > 1 ? `${componentsToProcess.indexOf(component) + 1}. ` : ""}Component: ${component.name} (file: ${component.relativePath})
Snippet: Snippet:
\`\`\` \`\`\`
...@@ -389,6 +394,8 @@ ${componentSnippet} ...@@ -389,6 +394,8 @@ ${componentSnippet}
\`\`\` \`\`\`
`; `;
} }
}
await db await db
.insert(messages) .insert(messages)
.values({ .values({
...@@ -460,18 +467,18 @@ ${componentSnippet} ...@@ -460,18 +467,18 @@ ${componentSnippet}
const appPath = getDyadAppPath(updatedChat.app.path); const appPath = getDyadAppPath(updatedChat.app.path);
// When we don't have smart context enabled, we // When we don't have smart context enabled, we
// only include the selected component's file for codebase context. // only include the selected components' files for codebase context.
// //
// If we have selected component and smart context is enabled, // If we have selected components and smart context is enabled,
// we handle this specially below. // we handle this specially below.
const chatContext = const chatContext =
req.selectedComponent && !isSmartContextEnabled req.selectedComponents &&
req.selectedComponents.length > 0 &&
!isSmartContextEnabled
? { ? {
contextPaths: [ contextPaths: req.selectedComponents.map((component) => ({
{ globPath: component.relativePath,
globPath: req.selectedComponent.relativePath, })),
},
],
smartContextAutoIncludes: [], smartContextAutoIncludes: [],
} }
: validateChatContext(updatedChat.app.chatContext); : validateChatContext(updatedChat.app.chatContext);
...@@ -482,12 +489,19 @@ ${componentSnippet} ...@@ -482,12 +489,19 @@ ${componentSnippet}
chatContext, chatContext,
}); });
// For smart context and selected component, we will mark the selected component's file as focused. // For smart context and selected components, we will mark the selected components' files as focused.
// This means that we don't do the regular smart context handling, but we'll allow fetching // This means that we don't do the regular smart context handling, but we'll allow fetching
// additional files through <dyad-read> as needed. // additional files through <dyad-read> as needed.
if (isSmartContextEnabled && req.selectedComponent) { if (
isSmartContextEnabled &&
req.selectedComponents &&
req.selectedComponents.length > 0
) {
const selectedPaths = new Set(
req.selectedComponents.map((component) => component.relativePath),
);
for (const file of files) { for (const file of files) {
if (file.path === req.selectedComponent.relativePath) { if (selectedPaths.has(file.path)) {
file.focused = true; file.focused = true;
} }
} }
......
...@@ -387,7 +387,7 @@ export class IpcClient { ...@@ -387,7 +387,7 @@ export class IpcClient {
public streamMessage( public streamMessage(
prompt: string, prompt: string,
options: { options: {
selectedComponent: ComponentSelection | null; selectedComponents?: ComponentSelection[];
chatId: number; chatId: number;
redo?: boolean; redo?: boolean;
attachments?: FileAttachment[]; attachments?: FileAttachment[];
...@@ -401,7 +401,7 @@ export class IpcClient { ...@@ -401,7 +401,7 @@ export class IpcClient {
chatId, chatId,
redo, redo,
attachments, attachments,
selectedComponent, selectedComponents,
onUpdate, onUpdate,
onEnd, onEnd,
onError, onError,
...@@ -441,7 +441,7 @@ export class IpcClient { ...@@ -441,7 +441,7 @@ export class IpcClient {
prompt, prompt,
chatId, chatId,
redo, redo,
selectedComponent, selectedComponents,
attachments: fileDataArray, attachments: fileDataArray,
}) })
.catch((err) => { .catch((err) => {
...@@ -464,7 +464,7 @@ export class IpcClient { ...@@ -464,7 +464,7 @@ export class IpcClient {
prompt, prompt,
chatId, chatId,
redo, redo,
selectedComponent, selectedComponents,
}) })
.catch((err) => { .catch((err) => {
console.error("Error streaming message:", err); console.error("Error streaming message:", err);
......
...@@ -41,7 +41,7 @@ export interface ChatStreamParams { ...@@ -41,7 +41,7 @@ export interface ChatStreamParams {
data: string; // Base64 encoded file data data: string; // Base64 encoded file data
attachmentType: "upload-to-codebase" | "chat-context"; // FileAttachment type attachmentType: "upload-to-codebase" | "chat-context"; // FileAttachment type
}>; }>;
selectedComponent: ComponentSelection | null; selectedComponents?: ComponentSelection[];
} }
export interface ChatResponseEnd { export interface ChatResponseEnd {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论