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.

feat: editor placeholder for AI prompt (#1057)

Myles Scolnick938d429c454d
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsx
index 8155c02838..4fda36f888 100644
--- a/frontend/src/components/editor/actions/useCellActionButton.tsx
+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx
@@ -34,7 +34,7 @@ import { saveCellConfig } from "@/core/network/requests";
import { EditorView } from "@codemirror/view";
import { useRunCell } from "../cell/useRunCells";
import { NameCellInput } from "./name-cell-input";
-import { useSetAtom } from "jotai";
+import { useAtomValue, useSetAtom } from "jotai";
import { aiCompletionCellAtom } from "@/core/ai/state";
import { useImperativeModal } from "@/components/modal/ImperativeModal";
import {
@@ -44,7 +44,7 @@ import {
} from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { MarkdownIcon, PythonIcon } from "../cell/code/icons";
-import { useUserConfig } from "@/core/config/config";
+import { aiEnabledAtom } from "@/core/config/config";
export interface CellActionButtonProps
extends Pick<CellData, "name" | "config"> {
@@ -72,7 +72,7 @@ export function useCellActionButtons({ cell }: Props) {
const runCell = useRunCell(cell?.cellId);
const { openModal } = useImperativeModal();
const setAiCompletionCell = useSetAtom(aiCompletionCellAtom);
- const [userConfig] = useUserConfig();
+ const aiEnabled = useAtomValue(aiEnabledAtom);
if (!cell) {
return [];
}
@@ -160,7 +160,7 @@ export function useCellActionButtons({ cell }: Props) {
{
icon: <SparklesIcon size={13} strokeWidth={1.5} />,
label: "AI completion",
- hidden: !userConfig.ai.open_ai?.api_key,
+ hidden: !aiEnabled,
handle: () => {
setAiCompletionCell((current) =>
current === cellId ? null : cellId,
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 1a9296b25d..9dc5f11c99 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -139,6 +139,7 @@ const CellEditorInternal = ({
const extensions = setupCodeMirror({
cellId,
showPlaceholder,
+ enableAI: Boolean(userConfig.ai.open_ai?.api_key),
cellCodeCallbacks: {
updateCellCode,
},
@@ -201,6 +202,7 @@ const CellEditorInternal = ({
cellId,
userConfig.keymap,
userConfig.completion,
+ userConfig.ai.open_ai?.api_key,
theme,
showPlaceholder,
createAbove,
diff --git a/frontend/src/core/codemirror/__tests__/setup.test.ts b/frontend/src/core/codemirror/__tests__/setup.test.ts
index 3e30aa7204..e4047fa955 100644
--- a/frontend/src/core/codemirror/__tests__/setup.test.ts
+++ b/frontend/src/core/codemirror/__tests__/setup.test.ts
@@ -21,6 +21,7 @@ function setup(config: Partial<CodeMirrorSetupOpts> = {}): Extension[] {
return setupCodeMirror({
cellId: "0" as CellId,
showPlaceholder: false,
+ enableAI: false,
cellMovementCallbacks: {
onRun: namedFunction("onRun"),
aiCellCompletion: namedFunction("aiCellCompletion"),
@@ -113,3 +114,9 @@ test("placeholder adds another extension", () => {
const withoutPlaceholder = setup({ showPlaceholder: false }).flat();
expect(withPlaceholder.length - 1).toBe(withoutPlaceholder.length);
});
+
+test("ai adds more extensions", () => {
+ const withAI = setup({ enableAI: true }).flat();
+ const withoutAI = setup({ enableAI: false }).flat();
+ expect(withAI.length - 2).toBe(withoutAI.length);
+});
diff --git a/frontend/src/core/codemirror/cm.ts b/frontend/src/core/codemirror/cm.ts
index 82361a9cc1..594244d606 100644
--- a/frontend/src/core/codemirror/cm.ts
+++ b/frontend/src/core/codemirror/cm.ts
@@ -48,18 +48,20 @@ import {
} from "./cells/extensions";
import { CellId } from "../cells/ids";
import { keymapBundle } from "./keymaps/keymaps";
-import {
- scrollActiveLineIntoView,
- smartPlaceholderExtension,
-} from "./extensions";
+import { scrollActiveLineIntoView } from "./extensions";
import { copilotBundle } from "./copilot/extension";
import { hintTooltip } from "./completion/hints";
import { adaptiveLanguageConfiguration } from "./language/extension";
import { historyCompartment } from "./editing/extensions";
+import {
+ clickablePlaceholderExtension,
+ smartPlaceholderExtension,
+} from "./placeholder/extensions";
export interface CodeMirrorSetupOpts {
cellId: CellId;
showPlaceholder: boolean;
+ enableAI: boolean;
cellMovementCallbacks: MovementCallbacks;
cellCodeCallbacks: CodeCallbacks;
completionConfig: CompletionConfig;
@@ -73,6 +75,7 @@ export interface CodeMirrorSetupOpts {
export const setupCodeMirror = ({
cellId,
showPlaceholder,
+ enableAI,
cellMovementCallbacks,
cellCodeCallbacks,
completionConfig,
@@ -89,7 +92,14 @@ export const setupCodeMirror = ({
basicBundle(completionConfig, theme),
showPlaceholder
? Prec.highest(smartPlaceholderExtension("import marimo as mo"))
- : [],
+ : enableAI
+ ? clickablePlaceholderExtension({
+ beforeText: "Start coding or ",
+ linkText: "generate",
+ afterText: " with AI.",
+ onClick: cellMovementCallbacks.aiCellCompletion,
+ })
+ : [],
];
};
diff --git a/frontend/src/core/codemirror/extensions.ts b/frontend/src/core/codemirror/extensions.ts
index b7a5ec2c17..a0b43d65db 100644
--- a/frontend/src/core/codemirror/extensions.ts
+++ b/frontend/src/core/codemirror/extensions.ts
@@ -1,5 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { EditorView, keymap, placeholder } from "@codemirror/view";
+import { EditorView, keymap } from "@codemirror/view";
import { CellId } from "../cells/ids";
import { formatEditorViews, toggleMarkdown } from "./format";
import { smartScrollIntoView } from "../../utils/scroll";
@@ -7,50 +7,6 @@ import { HOTKEYS } from "@/core/hotkeys/hotkeys";
import { CellActions } from "../cells/cells";
import { invariant } from "@/utils/invariant";
-function acceptPlaceholder(cm: EditorView, text: string) {
- // if empty, insert the placeholder
- if (cm.state.doc.length === 0) {
- cm.dispatch({
- changes: {
- from: 0,
- to: cm.state.doc.length,
- insert: text,
- },
- });
- // move cursor to end of placeholder
- cm.dispatch({
- selection: {
- anchor: cm.state.doc.length,
- head: cm.state.doc.length,
- },
- });
- return true;
- }
- return false;
-}
-
-/**
- * A placeholder that will be shown when the editor is empty and support
- * auto-complete on right arrow or Tab.
- */
-export function smartPlaceholderExtension(text: string) {
- return [
- placeholder(text),
- keymap.of([
- {
- key: "ArrowRight",
- preventDefault: true,
- run: (cm) => acceptPlaceholder(cm, text),
- },
- {
- key: "Tab",
- preventDefault: true,
- run: (cm) => acceptPlaceholder(cm, text),
- },
- ]),
- ];
-}
-
/**
* Add a keymap to format the code in the editor.
*/
diff --git a/frontend/src/core/codemirror/placeholder/extensions.ts b/frontend/src/core/codemirror/placeholder/extensions.ts
new file mode 100644
index 0000000000..dbc8f24dd7
--- /dev/null
+++ b/frontend/src/core/codemirror/placeholder/extensions.ts
@@ -0,0 +1,89 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { Extension } from "@codemirror/state";
+import { EditorView, keymap, placeholder } from "@codemirror/view";
+
+/**
+ * A placeholder that will be shown when the editor is empty and support
+ * auto-complete on right arrow or Tab.
+ */
+export function smartPlaceholderExtension(text: string): Extension[] {
+ return [
+ placeholder(text),
+ keymap.of([
+ {
+ key: "ArrowRight",
+ preventDefault: true,
+ run: (cm) => acceptPlaceholder(cm, text),
+ },
+ {
+ key: "Tab",
+ preventDefault: true,
+ run: (cm) => acceptPlaceholder(cm, text),
+ },
+ ]),
+ ];
+}
+
+function acceptPlaceholder(cm: EditorView, text: string) {
+ // if empty, insert the placeholder
+ if (cm.state.doc.length === 0) {
+ cm.dispatch({
+ changes: {
+ from: 0,
+ to: cm.state.doc.length,
+ insert: text,
+ },
+ });
+ // move cursor to end of placeholder
+ cm.dispatch({
+ selection: {
+ anchor: cm.state.doc.length,
+ head: cm.state.doc.length,
+ },
+ });
+ return true;
+ }
+ return false;
+}
+
+/**
+ * Create a placeholder with a clickable link.
+ */
+export function clickablePlaceholderExtension(opts: {
+ beforeText: string;
+ linkText: string;
+ afterText: string;
+ onClick: () => void;
+}): Extension[] {
+ const { beforeText, linkText, afterText, onClick } = opts;
+
+ // Create a placeholder
+ const placeholderText = document.createElement("span");
+ placeholderText.append(document.createTextNode(beforeText));
+ const link = document.createElement("span");
+ link.textContent = linkText;
+ link.classList.add("cm-clickable-placeholder");
+ link.onclick = (evt) => {
+ evt.stopPropagation();
+ onClick();
+ };
+ placeholderText.append(link);
+ placeholderText.append(document.createTextNode(afterText));
+
+ return [
+ placeholder(placeholderText),
+ EditorView.theme({
+ ".cm-clickable-placeholder": {
+ cursor: "pointer !important",
+ pointerEvents: "auto",
+ color: "var(--slate-10)",
+ textDecoration: "underline",
+ textDecorationThickness: "0.1em",
+ textUnderlineOffset: "0.2em",
+ },
+ ".cm-clickable-placeholder:hover": {
+ color: "var(--sky-11)",
+ },
+ }),
+ ];
+}
diff --git a/frontend/src/core/config/config.ts b/frontend/src/core/config/config.ts
index c55da4c53b..4a3e3b0583 100644
--- a/frontend/src/core/config/config.ts
+++ b/frontend/src/core/config/config.ts
@@ -24,6 +24,10 @@ export function getUserConfig() {
return store.get(userConfigAtom);
}
+export const aiEnabledAtom = atom<boolean>((get) => {
+ return Boolean(get(userConfigAtom).ai.open_ai?.api_key);
+});
+
/**
* Atom for storing the app config.
*/