/* Copyright 2026 Marimo. All rights reserved. */ import { useAtomValue, useSetAtom, useStore } from "jotai"; import { CheckIcon, ChevronDownIcon, HatGlasses, type LucideIcon, SparklesIcon, WrenchIcon, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Tooltip } from "@/components/ui/tooltip"; import { aiCompletionCellAtom } from "@/core/ai/state"; import { notebookAtom, useCellActions } from "@/core/cells/cells"; import type { CellId } from "@/core/cells/ids"; import { aiFeaturesEnabledAtom } from "@/core/config/config"; import { getAutoFixes } from "@/core/errors/errors"; import type { MarimoError } from "@/core/kernel/messages"; import { cn } from "@/utils/cn"; import { useOpenAiAssistant } from "../chrome/wrapper/useOpenAiAssistant"; import { type FixMode, useFixMode } from "./fix-mode"; export function buildFixInChatPrompt( cellId: CellId | undefined, fallbackErrorText?: string, ): string { if (cellId != null) { return `@error://${cellId}\n\nPlease fix this error.`; } const errorText = fallbackErrorText?.trim(); if (errorText) { return `My code gives the following error. Please fix it:\n\n${errorText}`; } return "Please fix this error."; } export const AutoFixButton = ({ errors, cellId, className, }: { errors: MarimoError[]; cellId: CellId; className?: string; }) => { const store = useStore(); const { createNewCell } = useCellActions(); const aiFeaturesEnabled = useAtomValue(aiFeaturesEnabledAtom); const autoFixes = errors.flatMap((error) => getAutoFixes(error, { aiEnabled: aiFeaturesEnabled }), ); const setAiCompletionCell = useSetAtom(aiCompletionCellAtom); const openAiAssistant = useOpenAiAssistant(); if (autoFixes.length === 0) { return null; } // TODO: Add a dropdown menu with the auto-fixes, when we need to support // multiple fixes. const firstFix = autoFixes[0]; const handleFix = (triggerFix: boolean) => { const editorView = store.get(notebookAtom).cellHandles[cellId].current?.editorView; firstFix.onFix({ addCodeBelow: (code: string) => { createNewCell({ cellId: cellId, autoFocus: false, before: false, code: code, }); }, editor: editorView, cellId: cellId, aiFix: { setAiCompletionCell, triggerFix, }, }); // Focus the editor editorView?.focus(); }; const openAISidebar = () => { openAiAssistant({ prompt: buildFixInChatPrompt(cellId), }); }; return (