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.

improvement: store previous prompts in local storage. cycle through with up/down arrow (#6157)

Myles Scolnick3e37c2be44fa
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index efbf83e52c..51693ba1c3 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { useChat } from "@ai-sdk/react";
+import { storePrompt } from "@marimo-team/codemirror-ai";
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import type { Message } from "ai/react";
import { useAtom, useAtomValue } from "jotai";
@@ -347,6 +348,7 @@ const ChatInput: React.FC<ChatInputProps> = memo(
<div className="border-t relative shrink-0 min-h-[80px] flex flex-col">
<div className="px-2 py-3 flex-1">
<PromptInput
+ inputRef={inputRef}
value={input}
onChange={setInput}
onSubmit={onSubmit}
@@ -600,15 +602,17 @@ const ChatPanelBody = () => {
});
});
- const handleChatInputSubmit = (
- e: KeyboardEvent | undefined,
- newValue: string,
- ): void => {
- if (!newValue.trim()) {
- return;
- }
- handleSubmit(e);
- };
+ const handleChatInputSubmit = useEvent(
+ (e: KeyboardEvent | undefined, newValue: string): void => {
+ if (!newValue.trim()) {
+ return;
+ }
+ if (newMessageInputRef.current?.view) {
+ storePrompt(newMessageInputRef.current.view);
+ }
+ handleSubmit(e);
+ },
+ );
const handleReload = () => {
reload();
@@ -618,6 +622,9 @@ const ChatPanelBody = () => {
if (!newThreadInput.trim()) {
return;
}
+ if (newThreadInputRef.current?.view) {
+ storePrompt(newThreadInputRef.current.view);
+ }
createNewThread(newThreadInput.trim());
});
@@ -648,6 +655,7 @@ const ChatPanelBody = () => {
<div className="flex flex-col rounded-md border bg-background">
<div className="px-1">
<PromptInput
+ inputRef={newThreadInputRef}
key="new-thread-input"
value={newThreadInput}
placeholder="Ask anything, @ to include context about tables or dataframes"
diff --git a/frontend/src/components/editor/ai/add-cell-with-ai.tsx b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
index dbcb0f2ad7..1711722b2f 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -9,6 +9,7 @@ import {
import { markdown } from "@codemirror/lang-markdown";
import { sql } from "@codemirror/lang-sql";
import { Prec } from "@codemirror/state";
+import { promptHistory, storePrompt } from "@marimo-team/codemirror-ai";
import ReactCodeMirror, {
EditorView,
keymap,
@@ -26,8 +27,9 @@ import {
SparklesIcon,
XIcon,
} from "lucide-react";
-import { useMemo, useState } from "react";
+import { useMemo, useRef, useState } from "react";
import useEvent from "react-use-event-hook";
+import { z } from "zod";
import { AIModelDropdown } from "@/components/ai/ai-model-dropdown";
import { Button } from "@/components/ui/button";
import {
@@ -48,6 +50,7 @@ import { useRuntimeManager } from "@/core/runtime/config";
import { useTheme } from "@/theme/useTheme";
import { cn } from "@/utils/cn";
import { prettyError } from "@/utils/errors";
+import { ZodLocalStorage } from "@/utils/localStorage";
import { useCellActions } from "../../../core/cells/cells";
import { PythonIcon } from "../cell/code/icons";
import {
@@ -71,6 +74,10 @@ const languageAtom = atomWithStorage<"python" | "sql">(
"python",
);
+const KEY = "marimo:ai-prompt-history";
+// Store the prompt history in local storage
+const promptHistoryStorage = new ZodLocalStorage(z.array(z.string()), () => []);
+
/**
* Add a cell with AI.
*/
@@ -86,6 +93,7 @@ export const AddCellWithAI: React.FC<{
const ai = useAtomValue(aiAtom);
const editModel = ai?.models?.edit_model || DEFAULT_AI_MODEL;
const { saveModelChange } = useModelChange();
+ const inputRef = useRef<ReactCodeMirrorRef>(null);
const {
completion,
@@ -120,6 +128,9 @@ export const AddCellWithAI: React.FC<{
const submit = () => {
if (!isLoading) {
+ if (inputRef.current?.view) {
+ storePrompt(inputRef.current.view);
+ }
handleSubmit();
}
};
@@ -185,6 +196,7 @@ export const AddCellWithAI: React.FC<{
<div className="flex items-center px-3">
<SparklesIcon className="size-4 text-(--blue-11) mr-2" />
<PromptInput
+ inputRef={inputRef}
onClose={() => {
setCompletion("");
onClose();
@@ -338,6 +350,12 @@ export const PromptInput = ({
markdownLanguage.language.data.of({
autocomplete: additionalCompletionsSource,
}),
+ promptHistory({
+ storage: {
+ load: () => promptHistoryStorage.get(KEY),
+ save: (prompts) => promptHistoryStorage.set(KEY, prompts),
+ },
+ }),
EditorView.lineWrapping,
minimalSetup(),
Prec.highest(
@@ -392,21 +410,6 @@ export const PromptInput = ({
},
},
]),
- // Trap arrow up/down to prevent them from being used to navigate the editor
- keymap.of([
- {
- key: "ArrowUp",
- preventDefault: true,
- stopPropagation: true,
- },
- ]),
- keymap.of([
- {
- key: "ArrowDown",
- preventDefault: true,
- stopPropagation: true,
- },
- ]),
];
}, [store, additionalCompletionsSource, handleSubmit, handleEscape]);
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index b7c92515fd..d61c8df8b6 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -9,6 +9,7 @@ import { Button } from "@/components/ui/button";
import { customPythonLanguageSupport } from "@/core/codemirror/language/languages/python";
import "./merge-editor.css";
+import { storePrompt } from "@marimo-team/codemirror-ai";
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { useAtom } from "jotai";
import { Checkbox } from "@/components/ui/checkbox";
@@ -175,6 +176,9 @@ export const AiCompletionEditor: React.FC<Props> = ({
}}
onSubmit={() => {
if (!isLoading) {
+ if (inputRef.current?.view) {
+ storePrompt(inputRef.current.view);
+ }
handleSubmit();
}
}}