Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix with ai button for all cells (#6755)

Shahmir Varqha437f68161180
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index 615873324b..a39bdadb2a 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -49,13 +49,18 @@ const LazyVegaLite = React.lazy(() =>
type MimeBundle = Record<OutputMessage["mimetype"], { [key: string]: unknown }>;
type MimeBundleOrTuple = MimeBundle | [MimeBundle, { [key: string]: unknown }];
+export type OnRefactorWithAI = (opts: {
+ prompt: string;
+ triggerImmediately: boolean;
+}) => void;
+
/**
* Renders an output based on an OutputMessage.
*/
export const OutputRenderer: React.FC<{
message: Pick<OutputMessage, "channel" | "data" | "mimetype">;
cellId?: CellId;
- onRefactorWithAI?: (opts: { prompt: string }) => void;
+ onRefactorWithAI?: OnRefactorWithAI;
wrapText?: boolean;
}> = memo((props) => {
const { message, onRefactorWithAI, cellId, wrapText } = props;
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index 39656cfd20..f8eeb176ae 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -3,7 +3,7 @@
import { useCompletion } from "@ai-sdk/react";
import { EditorView } from "@codemirror/view";
import { AtSignIcon, Loader2Icon, SparklesIcon, XIcon } from "lucide-react";
-import React, { useEffect, useId, useState } from "react";
+import React, { useCallback, useEffect, useId, useState } from "react";
import CodeMirrorMerge from "react-codemirror-merge";
import { Button } from "@/components/ui/button";
import { customPythonLanguageSupport } from "@/core/codemirror/language/languages/python";
@@ -23,7 +23,6 @@ import { getCodes } from "@/core/codemirror/copilot/getCodes";
import type { LanguageAdapterType } from "@/core/codemirror/language/types";
import { selectAllText } from "@/core/codemirror/utils";
import { useRuntimeManager } from "@/core/runtime/config";
-import { useEvent } from "@/hooks/useEvent";
import { useTheme } from "@/theme/useTheme";
import { cn } from "@/utils/cn";
import { prettyError } from "@/utils/errors";
@@ -127,11 +126,12 @@ export const AiCompletionEditor: React.FC<Props> = ({
const inputRef = React.useRef<ReactCodeMirrorRef>(null);
const completion = untrimmedCompletion.trimEnd();
- const initialSubmit = useEvent(() => {
+ const initialSubmit = useCallback(() => {
if (triggerImmediately && !isLoading && initialPrompt) {
handleSubmit();
}
- });
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [triggerImmediately]);
// Focus the input
useEffect(() => {
diff --git a/frontend/src/components/editor/notebook-cell.tsx b/frontend/src/components/editor/notebook-cell.tsx
index fb09f9464a..6534578d8a 100644
--- a/frontend/src/components/editor/notebook-cell.tsx
+++ b/frontend/src/components/editor/notebook-cell.tsx
@@ -86,7 +86,7 @@ import {
useTemporarilyShownCode,
useTemporarilyShownCodeActions,
} from "./navigation/state";
-import { OutputArea } from "./Output";
+import { type OnRefactorWithAI, OutputArea } from "./Output";
import { ConsoleOutput } from "./output/ConsoleOutput";
import { CellDragHandle, SortableCell } from "./SortableCell";
@@ -532,9 +532,15 @@ const EditableCellComponent = ({
isMarkdownCodeHidden && hasOutput && !navigationProps["data-selected"],
});
- const handleRefactorWithAI = useEvent((opts: { prompt: string }) => {
- setAiCompletionCell({ cellId, initialPrompt: opts.prompt });
- });
+ const handleRefactorWithAI: OnRefactorWithAI = useEvent(
+ (opts: { prompt: string; triggerImmediately: boolean }) => {
+ setAiCompletionCell({
+ cellId,
+ initialPrompt: opts.prompt,
+ triggerImmediately: opts.triggerImmediately,
+ });
+ },
+ );
// TODO(akshayka): Move to our own Tooltip component once it's easier
// to get the tooltip to show next to the cursor ...
@@ -1027,9 +1033,15 @@ const SetupCellComponent = ({
},
);
- const handleRefactorWithAI = useEvent((opts: { prompt: string }) => {
- setAiCompletionCell({ cellId, initialPrompt: opts.prompt });
- });
+ const handleRefactorWithAI: OnRefactorWithAI = useEvent(
+ (opts: { prompt: string; triggerImmediately: boolean }) => {
+ setAiCompletionCell({
+ cellId,
+ initialPrompt: opts.prompt,
+ triggerImmediately: opts.triggerImmediately,
+ });
+ },
+ );
// TODO(akshayka): Move to our own Tooltip component once it's easier
// to get the tooltip to show next to the cursor ...
diff --git a/frontend/src/components/editor/output/ConsoleOutput.tsx b/frontend/src/components/editor/output/ConsoleOutput.tsx
index 01536eba56..ed8637d808 100644
--- a/frontend/src/components/editor/output/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/ConsoleOutput.tsx
@@ -19,7 +19,7 @@ import { invariant } from "@/utils/invariant";
import { Strings } from "@/utils/strings";
import { NameCellContentEditable } from "../actions/name-cell-input";
import { ErrorBoundary } from "../boundary/ErrorBoundary";
-import { OutputRenderer } from "../Output";
+import { type OnRefactorWithAI, OutputRenderer } from "../Output";
import { useWrapText } from "./useWrapText";
const ansiUp = new AnsiUp();
@@ -31,7 +31,7 @@ interface Props {
consoleOutputs: WithResponse<OutputMessage>[];
stale: boolean;
debuggerActive: boolean;
- onRefactorWithAI?: (opts: { prompt: string }) => void;
+ onRefactorWithAI?: OnRefactorWithAI;
onClear?: () => void;
onSubmitDebugger: (text: string, index: number) => void;
}
diff --git a/frontend/src/components/editor/output/MarimoTracebackOutput.tsx b/frontend/src/components/editor/output/MarimoTracebackOutput.tsx
index 0d65d2d9bd..6847517d35 100644
--- a/frontend/src/components/editor/output/MarimoTracebackOutput.tsx
+++ b/frontend/src/components/editor/output/MarimoTracebackOutput.tsx
@@ -3,7 +3,6 @@
import { type DOMNode, Element, Text } from "html-react-parser";
import { useAtomValue } from "jotai";
import {
- BugIcon,
BugPlayIcon,
ChevronDown,
CopyIcon,
@@ -40,12 +39,14 @@ import {
getTracebackInfo,
} from "@/utils/traceback";
import { cn } from "../../../utils/cn";
+import { AIFixButton } from "../errors/auto-fix";
import { CellLinkTraceback } from "../links/cell-link";
+import type { OnRefactorWithAI } from "../Output";
interface Props {
cellId: CellId | undefined;
traceback: string;
- onRefactorWithAI?: (opts: { prompt: string }) => void;
+ onRefactorWithAI?: OnRefactorWithAI;
}
const KEY = "item";
@@ -69,9 +70,10 @@ export const MarimoTracebackOutput = ({
// Get last traceback info
const tracebackInfo = extractAllTracebackInfo(traceback)?.at(0);
- const handleRefactorWithAI = () => {
+ const handleRefactorWithAI = (triggerImmediately: boolean) => {
onRefactorWithAI?.({
prompt: `My code gives the following error:\n\n${lastTracebackLine}`,
+ triggerImmediately,
});
};
@@ -103,10 +105,11 @@ export const MarimoTracebackOutput = ({
</Accordion>
<div className="flex gap-2">
{onRefactorWithAI && aiEnabled && (
- <Button size="xs" variant="outline" onClick={handleRefactorWithAI}>
- <BugIcon className="h-3 w-3 mr-2" />
- Fix with AI
- </Button>
+ <AIFixButton
+ tooltip="Fix with AI"
+ openPrompt={() => handleRefactorWithAI(false)}
+ applyAutofix={() => handleRefactorWithAI(true)}
+ />
)}
{tracebackInfo && tracebackInfo.kind === "cell" && !isWasm() && (
<Tooltip content={"Attach pdb to the exception point."}>