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.

generate multiple cells with generate with ai (#6312)

Shahmir Varqha7c1f9493885a
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index e112e9d359..da7de0a636 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -48,7 +48,6 @@ import { DEFAULT_AI_MODEL } from "@/core/config/config-schema";
import { FeatureFlagged } from "@/core/config/feature-flag";
import { useRequestClient } from "@/core/network/requests";
import { useRuntimeManager } from "@/core/runtime/config";
-import type { ChatMessage } from "@/plugins/impl/chat/types";
import { ErrorBanner } from "@/plugins/impl/common/error-banner";
import { cn } from "@/utils/cn";
import { timeAgo } from "@/utils/dates";
@@ -59,7 +58,6 @@ import { PromptInput } from "../editor/ai/add-cell-with-ai";
import {
addContextCompletion,
CONTEXT_TRIGGER,
- getAICompletionBodyWithAttachments,
} from "../editor/ai/completion-utils";
import { PanelEmptyState } from "../editor/chrome/panels/empty-state";
import { CopyClipboardIcon } from "../icons/copy-icon";
@@ -68,8 +66,11 @@ import { Tooltip, TooltipProvider } from "../ui/tooltip";
import { toast } from "../ui/use-toast";
import { AttachmentRenderer, FileAttachmentPill } from "./chat-components";
import {
+ buildCompletionRequestBody,
convertToFileUIPart,
generateChatTitle,
+ handleToolCall,
+ hasPendingToolCalls,
isLastMessageReasoning,
} from "./chat-utils";
import { MarkdownRenderer } from "./markdown-renderer";
@@ -565,80 +566,20 @@ const ChatPanelBody = () => {
id: chatId,
} = useChat({
id: activeChatId,
- // Only automatically submit if we have tool calls but no text response yet
- sendAutomaticallyWhen: ({ messages }) => {
- if (messages.length === 0) {
- return false;
- }
-
- const lastMessage = messages[messages.length - 1];
- const parts = lastMessage.parts;
-
- if (parts.length === 0) {
- return false;
- }
-
- // Only auto-send if the last message is an assistant message
- // Because assistant messages are the ones that can have tool calls
- if (lastMessage.role !== "assistant") {
- return false;
- }
-
- const toolParts = parts.filter((part) =>
- part.type.startsWith("tool-"),
- ) as ToolUIPart[];
-
- const hasCompletedToolCalls = toolParts.some(
- (part) => part.state === "output-available",
- );
-
- // Check if the last part has any text content
- const lastPart = parts[parts.length - 1];
- const hasTextContent =
- lastPart.type === "text" && lastPart.text?.trim().length > 0;
-
- // Only auto-send if we have completed tool calls and there is no reply yet
- return hasCompletedToolCalls && !hasTextContent;
- },
+ sendAutomaticallyWhen: ({ messages }) => hasPendingToolCalls(messages),
messages: activeChat?.messages || [], // initial messages
transport: new DefaultChatTransport({
api: runtimeManager.getAiURL("chat").toString(),
headers: runtimeManager.headers(),
prepareSendMessagesRequest: async (options) => {
- // Map from parts to a single string
- function toContent(parts: UIMessage["parts"]): string {
- return parts
- .map((part) => (part.type === "text" ? part.text : ""))
- .join("\n");
- }
-
- const input = toContent(options.messages.flatMap((m) => m.parts));
- const completionBody = await getAICompletionBodyWithAttachments({
- input,
- });
-
- // Map from UIMessage to our ChatMessage type
- // If it's the last message, add the attachments from the completion body
- function mapMessage(m: UIMessage, isLastMessage: boolean): ChatMessage {
- const parts = m.parts;
- if (isLastMessage) {
- parts.push(...completionBody.attachments);
- }
- return {
- role: m.role,
- content: toContent(m.parts),
- parts: parts,
- };
- }
+ const completionBody = await buildCompletionRequestBody(
+ options.messages,
+ );
return {
body: {
...options,
- ...completionBody.body,
- messages: options.messages.map((m, idx) => ({
- ...m,
- ...mapMessage(m, idx === options.messages.length - 1),
- })),
+ ...completionBody,
},
};
},
@@ -653,24 +594,15 @@ const ChatPanelBody = () => {
});
},
onToolCall: async ({ toolCall }) => {
- try {
- const response = await invokeAiTool({
+ await handleToolCall({
+ invokeAiTool,
+ addToolResult,
+ toolCall: {
toolName: toolCall.toolName,
- arguments: toolCall.input as Record<string, never>,
- });
- addToolResult({
- tool: toolCall.toolName,
toolCallId: toolCall.toolCallId,
- output: response.result || response.error,
- });
- } catch (error) {
- Logger.error("Tool call failed:", error);
- addToolResult({
- tool: toolCall.toolName,
- toolCallId: toolCall.toolCallId,
- output: `Error: ${error instanceof Error ? error.message : String(error)}`,
- });
- }
+ input: toolCall.input as Record<string, never>,
+ },
+ });
},
onError: (error) => {
Logger.error("An error occurred:", error);
diff --git a/frontend/src/components/chat/chat-utils.ts b/frontend/src/components/chat/chat-utils.ts
index 30ac6a5769..2f343b324d 100644
--- a/frontend/src/components/chat/chat-utils.ts
+++ b/frontend/src/components/chat/chat-utils.ts
@@ -1,7 +1,15 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import type { FileUIPart, UIMessage } from "ai";
+import type { components } from "@marimo-team/marimo-api";
+import type { FileUIPart, ToolUIPart, UIMessage } from "ai";
+import type {
+ InvokeAiToolRequest,
+ InvokeAiToolResponse,
+} from "@/core/network/types";
+import type { ChatMessage } from "@/plugins/impl/chat/types";
import { blobToString } from "@/utils/fileToBase64";
+import { Logger } from "@/utils/Logger";
+import { getAICompletionBodyWithAttachments } from "../editor/ai/completion-utils";
export function generateChatTitle(message: string): string {
return message.length > 50 ? `${message.slice(0, 50)}...` : message;
@@ -48,3 +56,121 @@ export function isLastMessageReasoning(messages: UIMessage[]): boolean {
const lastPart = parts[parts.length - 1];
return lastPart.type === "reasoning";
}
+
+function stringifyTextParts(parts: UIMessage["parts"]): string {
+ return parts
+ .map((part) => (part.type === "text" ? part.text : ""))
+ .join("\n");
+}
+
+export async function buildCompletionRequestBody(
+ messages: UIMessage[],
+): Promise<{
+ messages: ChatMessage[];
+ context?: (null | components["schemas"]["AiCompletionContext"]) | undefined;
+ includeOtherCode: string;
+ selectedText?: string | null | undefined;
+}> {
+ const input = stringifyTextParts(messages.flatMap((m) => m.parts));
+ const completionBody = await getAICompletionBodyWithAttachments({ input });
+
+ // Map from UIMessage to our ChatMessage type
+ // If it's the last message, add the attachments from the completion body
+ function toChatMessage(message: UIMessage, isLast: boolean): ChatMessage {
+ // Clone parts to avoid mutating the original message
+ const parts = [...message.parts];
+ if (isLast) {
+ parts.push(...completionBody.attachments);
+ }
+ return {
+ role: message.role,
+ content: stringifyTextParts(message.parts), // This is no longer used in the backend
+ parts,
+ };
+ }
+
+ return {
+ ...completionBody.body,
+ messages: messages.map((m, idx) =>
+ toChatMessage(m, idx === messages.length - 1),
+ ),
+ };
+}
+
+interface AddToolResult {
+ tool: string;
+ toolCallId: string;
+ output: unknown;
+}
+
+export async function handleToolCall({
+ invokeAiTool,
+ addToolResult,
+ toolCall,
+}: {
+ invokeAiTool: (request: InvokeAiToolRequest) => Promise<InvokeAiToolResponse>;
+ addToolResult: (result: AddToolResult) => Promise<void>;
+ toolCall: {
+ toolName: string;
+ toolCallId: string;
+ input: Record<string, never>;
+ };
+}) {
+ try {
+ const response = await invokeAiTool({
+ toolName: toolCall.toolName,
+ arguments: toolCall.input,
+ });
+ addToolResult({
+ tool: toolCall.toolName,
+ toolCallId: toolCall.toolCallId,
+ output: response.result || response.error,
+ });
+ } catch (error) {
+ Logger.error("Tool call failed:", error);
+ addToolResult({
+ tool: toolCall.toolName,
+ toolCallId: toolCall.toolCallId,
+ output: `Error: ${error instanceof Error ? error.message : String(error)}`,
+ });
+ }
+}
+
+/**
+ * Checks if we should send a message automatically based on the messages.
+ * We only want to send a message if we have completed tool calls and there is no reply yet.
+ */
+export function hasPendingToolCalls(messages: UIMessage[]): boolean {
+ if (messages.length === 0) {
+ return false;
+ }
+
+ const lastMessage = messages[messages.length - 1];
+ const parts = lastMessage.parts;
+
+ if (parts.length === 0) {
+ return false;
+ }
+
+ // Only auto-send if the last message is an assistant message
+ // Because assistant messages are the ones that can have tool calls
+ if (lastMessage.role !== "assistant") {
+ return false;
+ }
+
+ const toolParts = parts.filter((part) =>
+ part.type.startsWith("tool-"),
+ ) as ToolUIPart[];
+
+ const hasCompletedToolCalls = toolParts.some(
+ (part) => part.state === "output-available",
+ );
+
+ // Check if the last part has any text content
+ const lastPart = parts[parts.length - 1];
+ const hasTextContent =
+ lastPart.type === "text" && lastPart.text?.trim().length > 0;
+
+ // Only auto-send if we have completed tool calls and there is no reply yet
+ return hasCompletedToolCalls && !hasTextContent;
+}
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 79e3fb05ff..0fbdf17827 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -72,6 +72,10 @@ import { CollapsedCellBanner, CollapseToggle } from "./cell/collapse";
import { DeleteButton } from "./cell/DeleteButton";
import { PendingDeleteConfirmation } from "./cell/PendingDeleteConfirmation";
import { RunButton } from "./cell/RunButton";
+import {
+ StagedAICellBackground,
+ StagedAICellFooter,
+} from "./cell/StagedAICell";
import { useDeleteCellCallback } from "./cell/useDeleteCell";
import { useRunCell } from "./cell/useRunCells";
import { HideCodeButton } from "./code/readonly-python-code";
@@ -569,6 +573,7 @@ const EditableCellComponent = ({
ref={cellContainerRef}
{...cellDomProps(cellId, cellData.name)}
>
+ <StagedAICellBackground cellId={cellId} className="z-5" />
{cellOutput === "above" && outputArea}
<div className={cn("tray")} data-hidden={isMarkdownCodeHidden}>
<div className="absolute right-2 -top-4 z-10">
@@ -729,6 +734,7 @@ const EditableCellComponent = ({
/>
<PendingDeleteConfirmation cellId={cellId} />
</div>
+ <StagedAICellFooter cellId={cellId} />
{isCollapsed && (
<CollapsedCellBanner
onClick={() => actions.expandCell({ cellId })}
diff --git a/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts b/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
index da81d13bc9..be69444d6b 100644
--- a/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
+++ b/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
@@ -8,7 +8,7 @@ import type { DatasetsState } from "@/core/datasets/types";
import { store } from "@/core/state/jotai";
import { variablesAtom } from "@/core/variables/state";
import type { Variable, VariableName } from "@/core/variables/types";
-import { getAICompletionBody } from "../completion-utils";
+import { codeToCells, getAICompletionBody } from "../completion-utils";
// Mock getCodes function
vi.mock("@/core/codemirror/copilot/getCodes", () => ({
@@ -357,3 +357,180 @@ describe("getAICompletionBody", () => {
`);
});
});
+
+describe("codeToCells", () => {
+ it("should return empty array for empty string", () => {
+ const code = "";
+ const result = codeToCells(code);
+ expect(result).toEqual([]);
+ });
+
+ it("should return empty array for whitespace only", () => {
+ const code = " \n\t ";
+ const result = codeToCells(code);
+ expect(result).toEqual([]);
+ });
+
+ it("should convert code without backticks to single python cell", () => {
+ const code = "print('Hello, world!')";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should convert code with single closed backticks to cells", () => {
+ const code = "```python\nprint('Hello, world!')\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should convert code with unclosed backticks to cells", () => {
+ const code = "```python\nprint('Hello, world!')\n";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should convert code with multiple closed cells", () => {
+ const code =
+ "```python\nprint('Hello, world!')\n```\n```sql\nSELECT * FROM users\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ { language: "sql", code: "SELECT * FROM users" },
+ ]);
+ });
+
+ it("should handle code with no language identifier", () => {
+ const code = "```\nprint('Hello, world!')\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should handle unclosed code with no language identifier", () => {
+ const code = "```\nprint('Hello, world!')\n";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should handle markdown language", () => {
+ const code = "```markdown\n# Hello, world!\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([{ language: "markdown", code: "# Hello, world!" }]);
+ });
+
+ it("should handle sql language", () => {
+ const code = "```sql\nSELECT * FROM users\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([{ language: "sql", code: "SELECT * FROM users" }]);
+ });
+
+ it("should handle unclosed markdown cell", () => {
+ const code = "```markdown\n# Hello, world!\n";
+ const result = codeToCells(code);
+ expect(result).toEqual([{ language: "markdown", code: "# Hello, world!" }]);
+ });
+
+ it("should handle unclosed sql cell", () => {
+ const code = "```sql\nSELECT * FROM users\n";
+ const result = codeToCells(code);
+ expect(result).toEqual([{ language: "sql", code: "SELECT * FROM users" }]);
+ });
+
+ it("should handle empty cells and skip them", () => {
+ const code = "```python\n\n```\n```sql\nSELECT * FROM users\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([{ language: "sql", code: "SELECT * FROM users" }]);
+ });
+
+ it("should handle cells with only whitespace and skip them", () => {
+ const code = "```python\n \n```\n```sql\nSELECT * FROM users\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([{ language: "sql", code: "SELECT * FROM users" }]);
+ });
+
+ it("should handle code with trailing newlines", () => {
+ const code = "```python\nprint('Hello, world!')\n\n\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should handle unclosed code with trailing newlines", () => {
+ const code = "```python\nprint('Hello, world!')\n\n\n";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should handle multiple cells with different languages", () => {
+ const code =
+ "```python\nprint('Hello, world!')\n```\n```sql\nSELECT * FROM users\n```\n```markdown\n# Title\nThis is markdown\n```";
+
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ { language: "sql", code: "SELECT * FROM users" },
+ { language: "markdown", code: "# Title\nThis is markdown" },
+ ]);
+ });
+
+ it("should handle complex multiline code", () => {
+ const code =
+ '```python\ndef hello():\n print("Hello, world!")\n return "success"\n\nhello()\n```\n```sql\nSELECT \n id,\n name,\n email\nFROM users\nWHERE active = true\nORDER BY name;\n```';
+
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ {
+ language: "python",
+ code: 'def hello():\n print("Hello, world!")\n return "success"\n\nhello()',
+ },
+ {
+ language: "sql",
+ code: "SELECT \n id,\n name,\n email\nFROM users\nWHERE active = true\nORDER BY name;",
+ },
+ ]);
+ });
+
+ it("should handle code with backticks in the content", () => {
+ const code = "```python\nprint('```')\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([{ language: "python", code: "print('" }]);
+ });
+
+ it("should handle code with no backticks in the last cell", () => {
+ const code =
+ "```python\nprint('Hello, world!')\n```\n```python\nprint('Hello, world!')";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should handle case insensitive language detection", () => {
+ const code = "```PYTHON\nprint('Hello, world!')\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "python", code: "print('Hello, world!')" },
+ ]);
+ });
+
+ it("should handle unknown language", { fails: true }, () => {
+ const code = "```javascript\nconsole.log('Hello, world!')\n```";
+ const result = codeToCells(code);
+ expect(result).toEqual([
+ { language: "javascript", code: "console.log('Hello, world!')" },
+ ]);
+ });
+});
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 91e2f63e90..6d11fd6461 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -1,6 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { useCompletion } from "@ai-sdk/react";
+import { useChat } from "@ai-sdk/react";
import {
autocompletion,
type Completion,
@@ -8,7 +8,6 @@ import {
type CompletionSource,
} from "@codemirror/autocomplete";
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, {
@@ -17,7 +16,7 @@ import ReactCodeMirror, {
minimalSetup,
type ReactCodeMirrorRef,
} from "@uiw/react-codemirror";
-import { useAtom, useStore } from "jotai";
+import { useAtom, useAtomValue, useStore } from "jotai";
import { atomWithStorage } from "jotai/utils";
import {
ChevronsUpDown,
@@ -31,6 +30,10 @@ 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 {
+ buildCompletionRequestBody,
+ handleToolCall,
+} from "@/components/chat/chat-utils";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
@@ -40,31 +43,22 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { toast } from "@/components/ui/use-toast";
+import { stagedAICellsAtom, useStagedCells } from "@/core/ai/staged-cells";
import { resourceExtension } from "@/core/codemirror/ai/resources";
-import { customPythonLanguageSupport } from "@/core/codemirror/language/languages/python";
-import { SQLLanguageAdapter } from "@/core/codemirror/language/languages/sql/sql";
+import { useRequestClient } from "@/core/network/requests";
+import type { AiCompletionRequest } from "@/core/network/types";
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 {
CompletionActions,
createAiCompletionOnKeydown,
} from "./completion-handlers";
-import {
- CONTEXT_TRIGGER,
- getAICompletionBody,
- mentionsCompletionSource,
-} from "./completion-utils";
-
-const pythonExtensions = [
- customPythonLanguageSupport(),
- EditorView.lineWrapping,
-];
-const sqlExtensions = [sql(), EditorView.lineWrapping];
+import { CONTEXT_TRIGGER, mentionsCompletionSource } from "./completion-utils";
+import { StreamingChunkTransport } from "./transport/chat-transport";
// Persist across sessions
const languageAtom = atomWithStorage<"python" | "sql">(
@@ -82,32 +76,56 @@ const promptHistoryStorage = new ZodLocalStorage(z.array(z.string()), () => []);
export const AddCellWithAI: React.FC<{
onClose: () => void;
}> = ({ onClose }) => {
- const { createNewCell } = useCellActions();
- const [completionBody, setCompletionBody] = useState<object>({});
+ const store = useStore();
+ const [input, setInput] = useState("");
+
+ const { deleteAllStagedCells, clearStagedCells, onStream } =
+ useStagedCells(store);
const [language, setLanguage] = useAtom(languageAtom);
- const { theme } = useTheme();
const runtimeManager = useRuntimeManager();
+ const { invokeAiTool } = useRequestClient();
+ const stagedAICells = useAtomValue(stagedAICellsAtom);
const inputRef = useRef<ReactCodeMirrorRef>(null);
- const {
- completion,
- input,
- stop,
- isLoading,
- setCompletion,
- setInput,
- handleSubmit,
- } = useCompletion({
- api: runtimeManager.getAiURL("completion").toString(),
- headers: runtimeManager.headers(),
- streamProtocol: "text",
+ const { sendMessage, stop, status, addToolResult } = useChat({
// Throttle the messages and data updates to 100ms
experimental_throttle: 100,
- body: {
- ...completionBody,
- language: language,
- code: "",
+ transport: new StreamingChunkTransport(
+ {
+ api: runtimeManager.getAiURL("completion").toString(),
+ headers: runtimeManager.headers(),
+ prepareSendMessagesRequest: async (options) => {
+ const completionBody = await buildCompletionRequestBody(
+ options.messages,
+ );
+ const body: AiCompletionRequest = {
+ ...options,
+ ...completionBody,
+ code: "",
+ prompt: "", // Don't need prompt since we are using messages
+ language: language,
+ };
+
+ return {
+ body: body,
+ };
+ },
+ },
+ (chunk) => {
+ onStream(chunk);
+ },
+ ),
+ onToolCall: async ({ toolCall }) => {
+ await handleToolCall({
+ invokeAiTool,
+ addToolResult,
+ toolCall: {
+ toolName: toolCall.toolName,
+ toolCallId: toolCall.toolCallId,
+ input: toolCall.input as Record<string, never>,
+ },
+ });
},
onError: (error) => {
toast({
@@ -115,18 +133,20 @@ export const AddCellWithAI: React.FC<{
description: prettyError(error),
});
},
- onFinish: (_prompt, completion) => {
- // Remove trailing new lines
- setCompletion(completion.trimEnd());
- },
});
+ const isLoading = status === "streaming" || status === "submitted";
+ const hasCompletion = stagedAICells.size > 0;
+ const multipleCompletions = stagedAICells.size > 1;
+
const submit = () => {
if (!isLoading) {
if (inputRef.current?.view) {
storePrompt(inputRef.current.view);
}
- handleSubmit();
+ // TODO: When we have conversations, don't delete existing cells
+ deleteAllStagedCells();
+ sendMessage({ text: input });
}
};
@@ -171,20 +191,12 @@ export const AddCellWithAI: React.FC<{
);
const handleAcceptCompletion = () => {
- createNewCell({
- cellId: "__end__",
- before: false,
- code:
- language === "python"
- ? completion
- : SQLLanguageAdapter.fromQuery(completion),
- });
- setCompletion("");
+ clearStagedCells();
onClose();
};
const handleDeclineCompletion = () => {
- setCompletion("");
+ deleteAllStagedCells();
};
const inputComponent = (
@@ -193,20 +205,19 @@ export const AddCellWithAI: React.FC<{
<PromptInput
inputRef={inputRef}
onClose={() => {
- setCompletion("");
+ deleteAllStagedCells();
onClose();
}}
value={input}
onChange={(newValue) => {
setInput(newValue);
- setCompletionBody(getAICompletionBody({ input: newValue }));
}}
onSubmit={submit}
onKeyDown={createAiCompletionOnKeydown({
handleAcceptCompletion,
handleDeclineCompletion,
isLoading,
- completion,
+ hasCompletion,
})}
/>
{isLoading && (
@@ -234,7 +245,7 @@ export const AddCellWithAI: React.FC<{
<div className={cn("flex flex-col w-full gap-2 py-2")}>
{inputComponent}
<div className="flex flex-row justify-between -mt-1 ml-1 mr-3">
- {!completion && (
+ {!hasCompletion && (
<span className="text-xs text-muted-foreground px-3 flex flex-col gap-1">
<span>
You can mention{" "}
@@ -245,12 +256,13 @@ export const AddCellWithAI: React.FC<{
<span>Code from other cells is automatically included.</span>
</span>
)}
- {completion && (
+ {hasCompletion && (
<CompletionActions
isLoading={isLoading}
onAccept={handleAcceptCompletion}
onDecline={handleDeclineCompletion}
size="sm"
+ multipleCompletions={multipleCompletions}
/>
)}
<div className="ml-auto flex items-center gap-1">
@@ -262,16 +274,6 @@ export const AddCellWithAI: React.FC<{
/>
</div>
</div>
-
- {completion && (
- <ReactCodeMirror
- value={completion}
- className="cm border-t"
- onChange={setCompletion}
- theme={theme === "dark" ? "dark" : "light"}
- extensions={language === "python" ? pythonExtensions : sqlExtensions}
- />
- )}
</div>
);
};
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index 0b8d7d7476..35569b597f 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -165,7 +165,7 @@ export const AiCompletionEditor: React.FC<Props> = ({
<SparklesIcon className="text-(--blue-10) shrink-0" size={16} />
<PromptInput
inputRef={inputRef}
- className="h-full my-0 py-2"
+ className="h-full my-0 py-2 flex items-center"
onClose={() => {
declineChange();
setCompletion("");
@@ -187,7 +187,7 @@ export const AiCompletionEditor: React.FC<Props> = ({
handleAcceptCompletion,
handleDeclineCompletion,
isLoading,
- completion,
+ hasCompletion: completion.trim().length > 0,
})}
/>
{isLoading && (
@@ -202,30 +202,33 @@ export const AiCompletionEditor: React.FC<Props> = ({
Stop
</Button>
)}
- {completion && (
- <CompletionActions
- isLoading={isLoading}
- onAccept={handleAcceptCompletion}
- onDecline={handleDeclineCompletion}
- size="xs"
- />
- )}
- <div className="flex flex-row items-center gap-0.5 -ml-1.5 -mr-2">
- <Tooltip content="Add context">
- <Button
- variant="text"
- size="icon"
- onClick={() => addContextCompletion(inputRef)}
- >
- <AtSignIcon className="h-3 w-3" />
- </Button>
- </Tooltip>
- <AIModelDropdown
- triggerClassName="h-7"
- iconSize="small"
- displayIconOnly={true}
- forRole="edit"
- />
+ <div className="-mr-1.5 py-1.5">
+ <div className="flex flex-row items-center justify-end gap-0.5">
+ <Tooltip content="Add context">
+ <Button
+ variant="text"
+ size="icon"
+ onClick={() => addContextCompletion(inputRef)}
+ >
+ <AtSignIcon className="h-3 w-3" />
+ </Button>
+ </Tooltip>
+ <AIModelDropdown
+ triggerClassName="h-7 text-xs w-24"
+ iconSize="small"
+ forRole="edit"
+ />
+ </div>
+ {completion && (
+ <div className="-mb-1.5">
+ <CompletionActions
+ isLoading={isLoading}
+ onAccept={handleAcceptCompletion}
+ onDecline={handleDeclineCompletion}
+ size="xs"
+ />
+ </div>
+ )}
</div>
<div className="h-full w-px bg-border mx-2" />
diff --git a/frontend/src/components/editor/ai/completion-handlers.tsx b/frontend/src/components/editor/ai/completion-handlers.tsx
index bd73fcdea3..f7c62cc5be 100644
--- a/frontend/src/components/editor/ai/completion-handlers.tsx
+++ b/frontend/src/components/editor/ai/completion-handlers.tsx
@@ -12,20 +12,20 @@ export const createAiCompletionOnKeydown = (opts: {
handleAcceptCompletion: () => void;
handleDeclineCompletion: () => void;
isLoading: boolean;
- completion: string;
+ hasCompletion: boolean;
}) => {
const {
handleAcceptCompletion,
handleDeclineCompletion,
isLoading,
- completion,
+ hasCompletion,
} = opts;
return (e: React.KeyboardEvent<HTMLDivElement>) => {
const metaKey = isPlatformMac() ? e.metaKey : e.ctrlKey;
// Mod+Enter should accept the completion, if there is one
- if (metaKey && e.key === "Enter" && !isLoading && completion) {
+ if (metaKey && e.key === "Enter" && !isLoading && hasCompletion) {
handleAcceptCompletion();
}
@@ -49,6 +49,7 @@ export const CompletionActions: React.FC<{
size?: "xs" | "sm";
acceptShortcut?: string;
declineShortcut?: string;
+ multipleCompletions?: boolean;
}> = ({
isLoading,
onAccept,
@@ -56,6 +57,7 @@ export const CompletionActions: React.FC<{
size = "sm",
acceptShortcut = "Mod-↵",
declineShortcut = "Shift-Mod-Delete",
+ multipleCompletions = false,
}) => {
return (
<>
@@ -68,7 +70,7 @@ export const CompletionActions: React.FC<{
onClick={onAccept}
>
<span className="text-(--grass-11) opacity-100">
- Accept{" "}
+ Accept{multipleCompletions && " all"}{" "}
<MinimalHotkeys className="ml-1 inline" shortcut={acceptShortcut} />
</span>
</Button>
@@ -76,14 +78,46 @@ export const CompletionActions: React.FC<{
data-testid="decline-completion-button"
variant="text"
size={size}
- className="mb-0 pl-1"
+ className="mb-0 pl-1 pr-0"
onClick={onDecline}
>
<span className="text-(--red-10)">
- Reject{" "}
+ Reject{multipleCompletions && " all"}{" "}
<MinimalHotkeys className="ml-1 inline" shortcut={declineShortcut} />
</span>
</Button>
</>
);
};
+
+export const CompletionActionsCellFooter: React.FC<{
+ isLoading: boolean;
+ onAccept: () => void;
+ onDecline: () => void;
+ size?: "xs" | "sm";
+ multipleCompletions?: boolean;
+}> = ({ isLoading, onAccept, onDecline }) => {
+ return (
+ <>
+ <Button
+ variant="text"
+ size="xs"
+ disabled={isLoading}
+ onClick={onAccept}
+ className="h-6 text-(--grass-11) border-(--grass-7) bg-(--grass-3)/60
+ hover:bg-(--grass-4) dark:bg-(--grass-4)/80 dark:hover:bg-(--grass-3) rounded px-3 font-semibold"
+ >
+ Accept
+ </Button>
+ <Button
+ variant="text"
+ size="xs"
+ onClick={onDecline}
+ className="h-6 text-(--red-10) border-(--red-7) bg-(--red-3)/60 hover:bg-(--red-4)
+ dark:bg-(--red-4)/80 dark:hover:bg-(--red-3) rounded px-3 font-semibold"
+ >
+ Reject
+ </Button>
+ </>
+ );
+};
diff --git a/frontend/src/components/editor/ai/completion-utils.ts b/frontend/src/components/editor/ai/completion-utils.ts
index 8be0a6a1bd..9290557175 100644
--- a/frontend/src/components/editor/ai/completion-utils.ts
+++ b/frontend/src/components/editor/ai/completion-utils.ts
@@ -10,6 +10,7 @@ import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import type { FileUIPart } from "ai";
import { getAIContextRegistry } from "@/core/ai/context/context";
import { getCodes } from "@/core/codemirror/copilot/getCodes";
+import type { LanguageAdapterType } from "@/core/codemirror/language/types";
import type { AiCompletionRequest } from "@/core/network/types";
import { store } from "@/core/state/jotai";
import { Logger } from "@/utils/Logger";
@@ -145,3 +146,94 @@ export function addContextCompletion(
startCompletion(inputRef.current.view);
}
}
+
+export interface AiCompletion {
+ language: LanguageAdapterType;
+ code: string;
+}
+
+/**
+ * Extracts code blocks (delimited by triple backticks) and their language ("python", "sql", "markdown").
+ * Defaults to "python" if no language is specified or no code blocks are found.
+ * Returns an array of AiCompletion objects.
+ */
+export function codeToCells(code: string): AiCompletion[] {
+ if (code.trim().length === 0) {
+ return [];
+ }
+
+ // If there are no backticks, assume code is in 1 cell and python
+ if (!code.includes("```")) {
+ return [{ language: "python", code: code }];
+ }
+
+ // If code has opening backticks, get the code after it
+ const cells: AiCompletion[] = [];
+ let start = 0;
+
+ let openIndex = code.indexOf("```", start);
+ while (openIndex !== -1) {
+ const newlineIndex = code.indexOf("\n", openIndex);
+ if (newlineIndex === -1) {
+ // If there's no newline after opening backticks, treat everything after as code
+ const remaining = code.slice(openIndex + 3);
+ const firstSpace = remaining.indexOf(" ");
+ const language =
+ firstSpace === -1 ? remaining : remaining.slice(0, firstSpace);
+ const finalLanguage =
+ language === "markdown"
+ ? "markdown"
+ : language === "sql"
+ ? "sql"
+ : "python";
+ // Extract code after the language identifier
+ const codeContent =
+ firstSpace === -1 ? "" : remaining.slice(firstSpace + 1);
+ if (codeContent.trim()) {
+ cells.push({ language: finalLanguage, code: codeContent.trim() });
+ }
+ break;
+ }
+
+ let language = code.slice(openIndex + 3, newlineIndex).trim() || "";
+ language =
+ language === "markdown"
+ ? "markdown"
+ : language === "sql"
+ ? "sql"
+ : "python";
+ const codeStart = newlineIndex + 1;
+
+ const closeIndex = code.indexOf("```", codeStart);
+ if (closeIndex === -1) {
+ // If there's no closing backticks, treat everything after the opening as code
+ const codeContent = code.slice(codeStart).replace(/\n+$/, "");
+ if (codeContent.trim()) {
+ cells.push({
+ language: language as LanguageAdapterType,
+ code: codeContent,
+ });
+ }
+ break;
+ }
+
+ // Remove trailing newlines
+ const codeContent = code.slice(codeStart, closeIndex).replace(/\n+$/, "");
+ if (codeContent.trim()) {
+ cells.push({
+ language: language as LanguageAdapterType,
+ code: codeContent,
+ });
+ }
+
+ start = closeIndex + 3;
+ openIndex = code.indexOf("```", start);
+ }
+
+ // If no cells found, assume code is in 1 cell and python
+ if (cells.length === 0) {
+ cells.push({ language: "python", code: code });
+ }
+
+ return cells;
+}
diff --git a/frontend/src/components/editor/ai/transport/chat-transport.tsx b/frontend/src/components/editor/ai/transport/chat-transport.tsx
new file mode 100644
index 0000000000..9bd22f9e34
--- /dev/null
+++ b/frontend/src/components/editor/ai/transport/chat-transport.tsx
@@ -0,0 +1,36 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import {
+ DefaultChatTransport,
+ type HttpChatTransportInitOptions,
+ type UIMessage,
+ type UIMessageChunk,
+} from "ai";
+
+/**
+ * Thin wrapper around the DefaultChatTransport that calls a callback when a chunk is received.
+ */
+export class StreamingChunkTransport<
+ UI_MESSAGE extends UIMessage,
+> extends DefaultChatTransport<UI_MESSAGE> {
+ constructor(
+ options: HttpChatTransportInitOptions<UI_MESSAGE> = {},
+ private onChunkReceived: (chunk: UIMessageChunk) => void,
+ ) {
+ super(options);
+ }
+
+ protected override processResponseStream(
+ stream: ReadableStream<Uint8Array>,
+ ): ReadableStream<UIMessageChunk> {
+ const onChunkReceived = this.onChunkReceived;
+ return super.processResponseStream(stream).pipeThrough(
+ new TransformStream<UIMessageChunk, UIMessageChunk>({
+ async transform(chunk, controller) {
+ onChunkReceived(chunk);
+ controller.enqueue(chunk);
+ },
+ }),
+ );
+ }
+}
diff --git a/frontend/src/components/editor/cell/StagedAICell.tsx b/frontend/src/components/editor/cell/StagedAICell.tsx
new file mode 100644
index 0000000000..7ce5aa0679
--- /dev/null
+++ b/frontend/src/components/editor/cell/StagedAICell.tsx
@@ -0,0 +1,60 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { useAtomValue, useStore } from "jotai";
+import { stagedAICellsAtom, useStagedCells } from "@/core/ai/staged-cells";
+import type { CellId } from "@/core/cells/ids";
+import { cn } from "@/utils/cn";
+import { CompletionActionsCellFooter } from "../ai/completion-handlers";
+
+export const StagedAICellBackground: React.FC<{
+ cellId: CellId;
+ className?: string;
+}> = ({ cellId, className }) => {
+ const stagedAICells = useAtomValue(stagedAICellsAtom);
+
+ if (!stagedAICells.has(cellId)) {
+ return null;
+ }
+
+ return (
+ <div
+ className={cn(
+ "absolute h-full w-full pointer-events-none rounded-lg",
+ "shadow-[inset_0_0_0_1px_rgba(59,130,246,0.15),inset_0_0_12px_rgba(59,130,246,0.2),inset_0_0_24px_rgba(59,130,246,0.1)]",
+ "dark:shadow-[inset_0_0_0_1px_rgba(59,130,246,0.15),inset_0_0_12px_rgba(59,130,246,0.2),inset_0_0_24px_rgba(59,130,246,0.1)]",
+ className,
+ )}
+ />
+ );
+};
+
+export const StagedAICellFooter: React.FC<{ cellId: CellId }> = ({
+ cellId,
+}) => {
+ const store = useStore();
+ const stagedAICells = useAtomValue(stagedAICellsAtom);
+ const { deleteStagedCell, removeStagedCell } = useStagedCells(store);
+
+ if (!stagedAICells.has(cellId)) {
+ return null;
+ }
+
+ const handleAcceptCompletion = () => {
+ removeStagedCell(cellId);
+ };
+
+ const handleDeclineCompletion = () => {
+ deleteStagedCell(cellId);
+ };
+
+ return (
+ <div className="flex items-center justify-end gap-1.5 w-full pb-1 pt-2">
+ <CompletionActionsCellFooter
+ isLoading={false}
+ onAccept={handleAcceptCompletion}
+ onDecline={handleDeclineCompletion}
+ size="xs"
+ />
+ </div>
+ );
+};
diff --git a/frontend/src/core/ai/__tests__/staged-cells.test.ts b/frontend/src/core/ai/__tests__/staged-cells.test.ts
new file mode 100644
index 0000000000..54a047c41a
--- /dev/null
+++ b/frontend/src/core/ai/__tests__/staged-cells.test.ts
@@ -0,0 +1,356 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { renderHook } from "@testing-library/react";
+import { getDefaultStore } from "jotai";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { CellId } from "@/core/cells/ids";
+import { updateEditorCodeFromPython } from "../../codemirror/language/utils";
+import {
+ stagedAICellsAtom,
+ useStagedCells,
+ visibleForTesting,
+} from "../staged-cells";
+
+const { createActions, reducer, initialState } = visibleForTesting;
+
+// Mock the dependencies
+const mockCreateNewCell = vi.fn();
+const mockUpdateCellEditor = vi.fn();
+const mockDeleteCellCallback = vi.fn();
+
+// Mock cell handle with editor view
+const mockCellHandle = {
+ current: {
+ editorViewOrNull: {
+ dispatch: vi.fn(),
+ },
+ },
+};
+
+vi.mock("../../cells/cells", () => ({
+ useCellActions: () => ({
+ createNewCell: mockCreateNewCell,
+ updateCellEditor: mockUpdateCellEditor,
+ }),
+ cellHandleAtom: vi.fn(() => ({
+ read: vi.fn(() => mockCellHandle),
+ })),
+}));
+
+vi.mock("@/components/editor/cell/useDeleteCell", () => ({
+ useDeleteCellCallback: () => mockDeleteCellCallback,
+}));
+
+vi.mock("../../codemirror/language/utils", () => ({
+ updateEditorCodeFromPython: vi.fn(),
+}));
+
+// Mock CellId.create
+vi.mock("@/core/cells/ids", () => ({
+ CellId: {
+ create: vi.fn(),
+ },
+}));
+
+describe("staged-cells", () => {
+ let store: ReturnType<typeof getDefaultStore>;
+ let cellId1: CellId;
+ let cellId2: CellId;
+
+ beforeEach(() => {
+ store = getDefaultStore();
+ cellId1 = "cell-1" as CellId;
+ cellId2 = "cell-2" as CellId;
+
+ // Reset mocks
+ vi.clearAllMocks();
+
+ // Reset the atom state
+ store.set(stagedAICellsAtom, new Set<CellId>());
+ });
+
+ describe("reducer and actions", () => {
+ it("should initialize with empty map", () => {
+ const state = initialState();
+ expect(state).toEqual(new Set());
+ });
+
+ it("should add cell IDs", () => {
+ let state = initialState();
+ state = reducer(state, {
+ type: "addStagedCell",
+ payload: { cellId: cellId1 },
+ });
+ state = reducer(state, {
+ type: "addStagedCell",
+ payload: { cellId: cellId2 },
+ });
+
+ expect(state.has(cellId1)).toBe(true);
+ expect(state.has(cellId2)).toBe(true);
+ });
+
+ it("should remove cell IDs", () => {
+ const state = new Set([cellId1, cellId2]);
+ const newState = reducer(state, {
+ type: "removeStagedCell",
+ payload: cellId1,
+ });
+
+ expect(newState.has(cellId1)).toBe(false);
+ expect(newState.has(cellId2)).toBe(true);
+ });
+
+ it("should clear all cell IDs", () => {
+ const state = new Set([cellId1, cellId2]);
+ const newState = reducer(state, {
+ type: "clearStagedCells",
+ payload: undefined,
+ });
+
+ expect(newState).toEqual(new Set());
+ });
+
+ it("should not mutate original state", () => {
+ const state = new Set([cellId1]);
+ const originalSize = state.size;
+
+ reducer(state, {
+ type: "addStagedCell",
+ payload: { cellId: cellId2 },
+ });
+
+ expect(state.size).toBe(originalSize);
+ expect(state.has(cellId1)).toBe(true);
+ expect(state.has(cellId2)).toBe(false);
+ });
+
+ it("should create action functions", () => {
+ const mockDispatch = vi.fn();
+ const actions = createActions(mockDispatch);
+
+ expect(typeof actions.addStagedCell).toBe("function");
+ expect(typeof actions.removeStagedCell).toBe("function");
+ expect(typeof actions.clearStagedCells).toBe("function");
+ });
+
+ it("should initialize atom with empty map", () => {
+ const state = store.get(stagedAICellsAtom);
+ expect(state).toEqual(new Set());
+ });
+ });
+
+ describe("useStagedCells hook", () => {
+ it("should create a staged cell with code", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+ const testCode = "print('hello world')";
+
+ // Mock CellId.create to return a predictable ID
+ const mockCellId = "mock-cell-id" as CellId;
+ vi.mocked(CellId.create).mockReturnValue(mockCellId);
+
+ const returnedCellId = result.current.createStagedCell(testCode);
+
+ expect(returnedCellId).toBe(mockCellId);
+ expect(mockCreateNewCell).toHaveBeenCalledWith({
+ cellId: "__end__",
+ code: testCode,
+ before: false,
+ newCellId: mockCellId,
+ });
+ });
+
+ it("should delete a staged cell", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+ const testCellId = "test-cell-id" as CellId;
+
+ result.current.deleteStagedCell(testCellId);
+
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({
+ cellId: testCellId,
+ });
+ });
+
+ it("should delete all staged cells when none exist", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+
+ // Should not throw when no cells exist
+ expect(() => result.current.deleteAllStagedCells()).not.toThrow();
+ expect(mockDeleteCellCallback).not.toHaveBeenCalled();
+ });
+
+ it("should delete all staged cells when cells exist", () => {
+ // First set the atom state before rendering the hook
+ store.set(stagedAICellsAtom, new Set([cellId1, cellId2]));
+
+ const { result } = renderHook(() => useStagedCells(store));
+ result.current.deleteAllStagedCells();
+
+ expect(mockDeleteCellCallback).toHaveBeenCalledTimes(2);
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({ cellId: cellId1 });
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({ cellId: cellId2 });
+
+ // Verify cells were cleared from the atom
+ const state = store.get(stagedAICellsAtom);
+ expect(state).toEqual(new Set());
+ });
+ });
+
+ it("should add staged cell", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+
+ result.current.addStagedCell({ cellId: cellId1 });
+
+ // Check that the cell was added to the atom
+ const state = store.get(stagedAICellsAtom);
+ expect(state.has(cellId1)).toBe(true);
+ });
+
+ it("should remove staged cell", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+
+ // First add cells
+ result.current.addStagedCell({ cellId: cellId1 });
+ result.current.addStagedCell({ cellId: cellId2 });
+
+ // Then remove one
+ result.current.removeStagedCell(cellId1);
+
+ // Check that only the remaining cell is in the map
+ const state = store.get(stagedAICellsAtom);
+ expect(state.has(cellId1)).toBe(false);
+ expect(state.has(cellId2)).toBe(true);
+ });
+
+ it("should clear all staged cells", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+
+ // First add some cells
+ result.current.addStagedCell({ cellId: cellId1 });
+ result.current.addStagedCell({ cellId: cellId2 });
+
+ // Then clear all
+ result.current.clearStagedCells();
+
+ // Check that no cells remain
+ const state = store.get(stagedAICellsAtom);
+ expect(state).toEqual(new Set());
+ });
+
+ it("should handle multiple operations correctly", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+
+ // Create a staged cell
+ const mockCellId = "mock-cell-id" as CellId;
+ vi.mocked(CellId.create).mockReturnValue(mockCellId);
+
+ const createdCellId = result.current.createStagedCell("test code");
+
+ // Verify it was created and added
+ expect(createdCellId).toBe(mockCellId);
+ expect(mockCreateNewCell).toHaveBeenCalled();
+
+ let state = store.get(stagedAICellsAtom);
+ expect(state.has(mockCellId)).toBe(true);
+
+ // Delete the staged cell
+ result.current.deleteStagedCell(mockCellId);
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({
+ cellId: mockCellId,
+ });
+
+ // Verify it was removed from staged cells
+ state = store.get(stagedAICellsAtom);
+ expect(state.has(mockCellId)).toBe(false);
+ });
+});
+
+describe("onStream", () => {
+ let store: ReturnType<typeof getDefaultStore>;
+ beforeEach(() => {
+ store = getDefaultStore();
+ });
+
+ it("should create a cell creation stream", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+ result.current.onStream({ type: "text-start", id: "test-id" });
+
+ // No cell or cell update should have been called
+ expect(mockCreateNewCell).not.toHaveBeenCalled();
+ expect(mockUpdateCellEditor).not.toHaveBeenCalled();
+ });
+
+ it("should not create cells when text-delta is received and no stream has been created", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+ result.current.onStream({
+ type: "text-delta",
+ id: "test-id",
+ delta: "test-delta",
+ });
+
+ // No cell or cell update should have been called
+ expect(mockCreateNewCell).not.toHaveBeenCalled();
+ expect(mockUpdateCellEditor).not.toHaveBeenCalled();
+ });
+
+ it("should create cells when text-delta is received and a stream has been created", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+ result.current.onStream({ type: "text-start", id: "test-id" });
+
+ // Mock CellId.create to return a predictable ID
+ const mockCellId = "mock-cell-id" as CellId;
+ vi.mocked(CellId.create).mockReturnValue(mockCellId);
+
+ result.current.onStream({
+ type: "text-delta",
+ id: "test-id",
+ delta: "some code",
+ });
+
+ expect(mockCreateNewCell).toHaveBeenCalledWith({
+ cellId: "__end__",
+ code: "some code",
+ before: false,
+ newCellId: "mock-cell-id",
+ });
+ });
+
+ it("should handle delta chunks", () => {
+ const { result } = renderHook(() => useStagedCells(store));
+ result.current.onStream({ type: "text-start", id: "test-id" });
+
+ const mockCellId = "mock-cell-id" as CellId;
+ vi.mocked(CellId.create).mockReturnValue(mockCellId);
+
+ result.current.onStream({
+ type: "text-delta",
+ id: "test-id",
+ delta: "``",
+ });
+
+ expect(mockCreateNewCell).toHaveBeenCalledWith({
+ cellId: "__end__",
+ code: "``",
+ before: false,
+ newCellId: "mock-cell-id",
+ });
+
+ result.current.onStream({
+ type: "text-delta",
+ id: "test-id",
+ delta: "```python\nsome code",
+ });
+
+ // Now the cell is recognized and only some code is seen
+ expect(vi.mocked(updateEditorCodeFromPython)).toHaveBeenCalledWith(
+ mockCellHandle.current.editorViewOrNull,
+ "some code",
+ );
+
+ result.current.onStream({
+ type: "text-delta",
+ id: "test-id",
+ delta: "\n```",
+ });
+ });
+});
diff --git a/frontend/src/core/ai/staged-cells.ts b/frontend/src/core/ai/staged-cells.ts
new file mode 100644
index 0000000000..70b48c2684
--- /dev/null
+++ b/frontend/src/core/ai/staged-cells.ts
@@ -0,0 +1,208 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import type { UIMessageChunk } from "ai";
+import { useRef } from "react";
+import {
+ type AiCompletion,
+ codeToCells,
+} from "@/components/editor/ai/completion-utils";
+import { useDeleteCellCallback } from "@/components/editor/cell/useDeleteCell";
+import { CellId } from "@/core/cells/ids";
+import { createReducerAndAtoms } from "@/utils/createReducer";
+import { Logger } from "@/utils/Logger";
+import { cellHandleAtom, useCellActions } from "../cells/cells";
+import type { LanguageAdapterType } from "../codemirror/language/types";
+import { updateEditorCodeFromPython } from "../codemirror/language/utils";
+import type { JotaiStore } from "../state/jotai";
+
+/**
+ * Cells that are staged for AI completion
+ * They function similarly to cells in the notebook, but they can be deleted or accepted by the user.
+ * We only track one set of staged cells at a time.
+ */
+
+const initialState = (): Set<CellId> => {
+ return new Set();
+};
+
+const {
+ valueAtom: stagedAICellsAtom,
+ useActions: useStagedAICellsActions,
+ createActions,
+ reducer,
+} = createReducerAndAtoms(initialState, {
+ addStagedCell: (state, action: { cellId: CellId }) => {
+ const { cellId } = action;
+ return new Set([...state, cellId]);
+ },
+ removeStagedCell: (state, cellId: CellId) => {
+ return new Set([...state].filter((id) => id !== cellId));
+ },
+ clearStagedCells: () => {
+ return initialState();
+ },
+});
+
+interface UpdateStagedCellAction {
+ cellId: CellId;
+ code: string;
+ language?: LanguageAdapterType;
+}
+
+/**
+ * Helper functions to create and delete staged cells.
+ */
+export function useStagedCells(store: JotaiStore) {
+ const { addStagedCell, removeStagedCell, clearStagedCells } =
+ useStagedAICellsActions();
+ const { createNewCell } = useCellActions();
+ const deleteCellCallback = useDeleteCellCallback();
+
+ const cellCreationStream = useRef<CellCreationStream | null>(null);
+
+ const createStagedCell = (code: string): CellId => {
+ const newCellId = CellId.create();
+ addStagedCell({ cellId: newCellId });
+ createNewCell({
+ cellId: "__end__",
+ code,
+ before: false,
+ newCellId: newCellId,
+ });
+ return newCellId;
+ };
+
+ const updateStagedCell = (opts: UpdateStagedCellAction) => {
+ const { cellId, code } = opts;
+ const stagedAICells = store.get(stagedAICellsAtom);
+
+ if (!stagedAICells.has(cellId)) {
+ Logger.error("Staged cell not found", { cellId });
+ return;
+ }
+
+ const cellHandle = store.get(cellHandleAtom(cellId));
+ const editorView = cellHandle?.current?.editorViewOrNull;
+ if (!editorView) {
+ Logger.error("Editor for this cell not found", { cellId });
+ return;
+ }
+ // TODO: Update the language
+ updateEditorCodeFromPython(editorView, code);
+ };
+
+ // Delete a staged cell and the corresponding cell in the notebook.
+ const deleteStagedCell = (cellId: CellId) => {
+ removeStagedCell(cellId);
+ deleteCellCallback({ cellId });
+ };
+
+ // Delete all staged cells and the corresponding cells in the notebook.
+ const deleteAllStagedCells = () => {
+ const stagedAICells = store.get(stagedAICellsAtom);
+ for (const cellId of stagedAICells) {
+ deleteCellCallback({ cellId });
+ }
+ clearStagedCells();
+ };
+
+ const onStream = (chunk: UIMessageChunk) => {
+ switch (chunk.type) {
+ case "text-start":
+ // Create stream
+ cellCreationStream.current = new CellCreationStream(
+ createStagedCell,
+ updateStagedCell,
+ );
+ break;
+ case "text-delta":
+ if (!cellCreationStream.current) {
+ Logger.error("Cell creation stream not found");
+ return;
+ }
+ cellCreationStream.current.stream(chunk);
+ break;
+ case "text-end":
+ case "finish":
+ if (!cellCreationStream.current) {
+ Logger.error("Cell creation stream not found");
+ return;
+ }
+ cellCreationStream.current.stop();
+ break;
+ default:
+ Logger.error("Unknown chunk type", { chunk });
+ }
+ };
+
+ return {
+ createStagedCell,
+ updateStagedCell,
+ addStagedCell,
+ removeStagedCell,
+ clearStagedCells,
+ deleteStagedCell,
+ deleteAllStagedCells,
+ onStream,
+ };
+}
+
+export { stagedAICellsAtom };
+export const visibleForTesting = {
+ createActions,
+ reducer,
+ initialState,
+ useStagedAICellsActions,
+};
+
+type TextDeltaChunk = Extract<UIMessageChunk, { type: "text-delta" }>;
+
+interface CreatedCell {
+ cellId: CellId;
+ cell: AiCompletion;
+}
+
+class CellCreationStream {
+ private createdCells: CreatedCell[] = [];
+ private buffer = "";
+
+ private onCreateCell: (code: string) => CellId;
+ private onUpdateCell: (opts: UpdateStagedCellAction) => void;
+
+ constructor(
+ onCreateCell: (code: string) => CellId,
+ onUpdateCell: (opts: UpdateStagedCellAction) => void,
+ ) {
+ this.onCreateCell = onCreateCell;
+ this.onUpdateCell = onUpdateCell;
+ }
+
+ stream(chunk: TextDeltaChunk) {
+ const delta = chunk.delta;
+ this.buffer += delta;
+ const completionCells = codeToCells(this.buffer);
+
+ // As incoming chunks are appended to the buffer,
+ // we parse the buffer into cells and determine which parts correspond to which cell.
+ // For each parsed cell, we either update an existing staged cell or create a new one.
+ for (const [idx, cell] of completionCells.entries()) {
+ if (idx < this.createdCells.length) {
+ const existingCell = this.createdCells[idx];
+ this.createdCells[idx] = { ...existingCell, cell };
+ this.onUpdateCell({
+ cellId: existingCell.cellId,
+ code: cell.code,
+ language: cell.language,
+ });
+ } else {
+ const newCellId = this.onCreateCell(cell.code);
+ this.createdCells.push({ cellId: newCellId, cell });
+ }
+ }
+ }
+
+ stop() {
+ // Clear all state
+ this.buffer = "";
+ }
+}
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 904acc93a4..6293972f09 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -1623,7 +1623,7 @@ const cellDataAtom = atomFamily((cellId: CellId) =>
const cellRuntimeAtom = atomFamily((cellId: CellId) =>
atom((get) => get(notebookAtom).cellRuntime[cellId]),
);
-const cellHandleAtom = atomFamily((cellId: CellId) =>
+export const cellHandleAtom = atomFamily((cellId: CellId) =>
atom((get) => get(notebookAtom).cellHandles[cellId]),
);
/**
diff --git a/marimo/_server/ai/prompts.py b/marimo/_server/ai/prompts.py
index 33342a4d40..44cab42b6f 100644
--- a/marimo/_server/ai/prompts.py
+++ b/marimo/_server/ai/prompts.py
@@ -86,6 +86,7 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
*,
language: Language,
is_insert: bool,
+ support_multiple_cells: bool,
custom_rules: Optional[str],
cell_code: Optional[str],
selected_text: Optional[str],
@@ -94,6 +95,21 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
) -> str:
if cell_code:
system_prompt = f"Here's a {language} document from a Python notebook that I'm going to ask you to make an edit to.\n\n"
+ elif support_multiple_cells:
+ system_prompt = (
+ "You are an AI assistant integrated into the marimo notebook code editor.\n"
+ "Your goal is to create new cells in the notebook.\n"
+ "You can create multiple cells with different languages. Each cell should be wrapped in backticks.\n"
+ "The user may reference additional context in the form @kind://name. You can use this context to help you with the current task.\n"
+ "You can reference variables from other cells, but you cannot redefine a variable if it already exists.\n"
+ "Immediately start with the following format. Do NOT comment on the code, just output the code itself: \n\n"
+ "```python\n{PYTHON_CODE}\n```\n\n"
+ '```sql\ndf_name = mo.sql(f"""{SQL_QUERY}""")\n```\n\n'
+ '```markdown\nmo.md(f"""{MARKDOWN_CONTENT}""")\n```\n\n'
+ "You can have multiple cells of any type. Each cell is wrapped in backticks with the appropriate language identifier.\n"
+ "Create clear variable names if they will be used in other cells. Do not prefix with underscore.\n"
+ "Separate logic into multiple cells to keep the code organized and readable."
+ )
else:
system_prompt = (
"You are an AI assistant integrated into the marimo notebook code editor.\n"
@@ -147,7 +163,14 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
system_prompt += "\n\nAnd here's the section to rewrite based on that prompt again for reference:\n\n"
system_prompt += _tag("rewrite_this", selected_text)
- if language in language_rules and language_rules[language]:
+ if support_multiple_cells:
+ # Add all language rules for multi-cell scenarios
+ for lang in language_rules:
+ if len(language_rules[lang]) > 0:
+ system_prompt += (
+ f"\n\n## Rules for {lang}:\n{_rules(language_rules[lang])}"
+ )
+ elif language in language_rules and language_rules[language]:
system_prompt += (
f"\n\n## Rules for {language}\n{_rules(language_rules[language])}"
)
@@ -165,7 +188,10 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
"code_from_other_cells", other_cell_codes
)
- system_prompt += "\n\nAgain, just output the code itself."
+ if support_multiple_cells:
+ system_prompt += "\n\nAgain, just output code wrapped in cells. Each cell is wrapped in backticks with the appropriate language identifier (python, sql, markdown)."
+ else:
+ system_prompt += "\n\nAgain, just output the code itself."
return system_prompt
diff --git a/marimo/_server/api/endpoints/ai.py b/marimo/_server/api/endpoints/ai.py
index 41195c7189..75ba6ee0cf 100644
--- a/marimo/_server/api/endpoints/ai.py
+++ b/marimo/_server/api/endpoints/ai.py
@@ -55,6 +55,7 @@ if TYPE_CHECKING:
from collections.abc import AsyncGenerator
from starlette.requests import Request
+ from starlette.responses import ContentStream
LOGGER = _loggers.marimo_logger()
@@ -133,10 +134,12 @@ async def ai_completion(
ai_config = get_ai_config(config)
custom_rules = ai_config.get("rules", None)
+ use_messages = len(body.messages) >= 1
system_prompt = get_refactor_or_insert_notebook_cell_system_prompt(
language=body.language,
is_insert=False,
+ support_multiple_cells=use_messages,
custom_rules=custom_rules,
cell_code=body.code,
selected_text=body.selected_text,
@@ -150,21 +153,40 @@ async def ai_completion(
AnyProviderConfig.for_model(model, ai_config),
model=model,
)
+
+ messages = (
+ body.messages
+ if use_messages
+ else [ChatMessage(role="user", content=prompt)]
+ )
+
response = await provider.stream_completion(
- messages=[ChatMessage(role="user", content=prompt)],
+ messages=messages,
system_prompt=system_prompt,
max_tokens=get_max_tokens(config),
)
- return StreamingResponse(
- content=safe_stream_wrapper(
+ # Pass back the entire SDK message if the frontend can handle it
+ content: ContentStream
+ if use_messages:
+ content = safe_stream_wrapper(
+ provider.as_stream_response(
+ response, StreamOptions(format_stream=True, text_only=False)
+ ),
+ text_only=False,
+ )
+ else:
+ content = safe_stream_wrapper(
without_wrapping_backticks(
provider.as_stream_response(
response, StreamOptions(text_only=True)
)
),
text_only=True,
- ),
+ )
+
+ return StreamingResponse(
+ content=content,
media_type="application/json",
headers={"x-vercel-ai-data-stream": "v1"},
)
diff --git a/marimo/_server/models/completion.py b/marimo/_server/models/completion.py
index 990f0f6047..1a945172dc 100644
--- a/marimo/_server/models/completion.py
+++ b/marimo/_server/models/completion.py
@@ -40,6 +40,7 @@ class AiCompletionRequest(msgspec.Struct, rename="camel"):
prompt: str
include_other_code: str
code: str
+ messages: list[ChatMessage] = []
selected_text: Optional[str] = None
context: Optional[AiCompletionContext] = None
language: Language = "python"
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 49faff4684..c2de0b612d 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -67,6 +67,11 @@ components:
- markdown
- python
- sql
+ messages:
+ default: []
+ items:
+ $ref: '#/components/schemas/ChatMessage'
+ type: array
prompt:
type: string
selectedText:
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 37edb9d992..e44fe7567c 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -2624,6 +2624,8 @@ export interface components {
* @enum {unknown}
*/
language?: "markdown" | "python" | "sql";
+ /** @default [] */
+ messages?: components["schemas"]["ChatMessage"][];
prompt: string;
/** @default null */
selectedText?: string | null;
diff --git a/tests/_server/ai/snapshots/system_prompts.txt b/tests/_server/ai/snapshots/system_prompts.txt
index 4d6851ebec..fff3e16548 100644
--- a/tests/_server/ai/snapshots/system_prompts.txt
+++ b/tests/_server/ai/snapshots/system_prompts.txt
@@ -310,4 +310,52 @@ Immediately start with the following format. Do NOT comment on the code, just ou
- value_preview: <Model object>
-Again, just output the code itself.
\ No newline at end of file
+Again, just output the code itself.
+
+==================== with support_multiple_cells=True ====================
+
+You are an AI assistant integrated into the marimo notebook code editor.
+Your goal is to create new cells in the notebook.
+You can create multiple cells with different languages. Each cell should be wrapped in backticks.
+The user may reference additional context in the form @kind://name. You can use this context to help you with the current task.
+You can reference variables from other cells, but you cannot redefine a variable if it already exists.
+Immediately start with the following format. Do NOT comment on the code, just output the code itself:
+
+```python
+{PYTHON_CODE}
+```
+
+```sql
+df_name = mo.sql(f"""{SQL_QUERY}""")
+```
+
+```markdown
+mo.md(f"""{MARKDOWN_CONTENT}""")
+```
+
+You can have multiple cells of any type. Each cell is wrapped in backticks with the appropriate language identifier.
+Create clear variable names if they will be used in other cells. Do not prefix with underscore.
+Separate logic into multiple cells to keep the code organized and readable.
+
+## Rules for python:
+1. For matplotlib: use plt.gca() as the last expression instead of plt.show().
+2. For plotly: return the figure object directly.
+3. For altair: return the chart object directly. Add tooltips where appropriate.
+4. Include proper labels, titles, and color schemes.
+5. Make visualizations interactive where appropriate.
+6. If an import already exists, do not import it again.
+7. If a variable is already defined, use another name, or make it private by adding an underscore at the beginning.
+
+## Rules for sql:
+1. The SQL must use duckdb syntax.
+
+## Available variables from other cells:
+- variable: `df`
+ - value_type: DataFrame
+ - value_preview: <DataFrame with 100 rows and 5 columns>
+- variable: `model`
+ - value_type: Model
+ - value_preview: <Model object>
+
+
+Again, just output code wrapped in cells. Each cell is wrapped in backticks with the appropriate language identifier (python, sql, markdown).
\ No newline at end of file
diff --git a/tests/_server/ai/test_prompts.py b/tests/_server/ai/test_prompts.py
index 18525c0c37..2b5eb135f2 100644
--- a/tests/_server/ai/test_prompts.py
+++ b/tests/_server/ai/test_prompts.py
@@ -34,6 +34,7 @@ def test_system_prompts():
result += get_refactor_or_insert_notebook_cell_system_prompt(
language=cast(Language, language),
is_insert=False,
+ support_multiple_cells=False,
custom_rules=None,
cell_code=None,
selected_text=None,
@@ -45,6 +46,7 @@ def test_system_prompts():
result += get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=False,
+ support_multiple_cells=False,
custom_rules="Always use type hints.",
cell_code=None,
selected_text=None,
@@ -55,6 +57,7 @@ def test_system_prompts():
result += _header("with context")
result += get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
+ support_multiple_cells=False,
context=AiCompletionContext(
schema=[
SchemaTable(
@@ -84,6 +87,7 @@ def test_system_prompts():
result += get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=True,
+ support_multiple_cells=False,
custom_rules=None,
cell_code="def fib(n):\n <insert_here></insert_here>",
selected_text=None,
@@ -95,6 +99,7 @@ def test_system_prompts():
result += get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=False,
+ support_multiple_cells=False,
custom_rules=None,
cell_code="def hello():\n <rewrite_this>print('Hello, world!')</rewrite_this>",
selected_text="print('Hello, world!')",
@@ -106,6 +111,7 @@ def test_system_prompts():
result += get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=False,
+ support_multiple_cells=False,
custom_rules=None,
cell_code="def hello():\n <rewrite_this>print('Hello, world!')</rewrite_this>",
selected_text="print('Hello, world!')",
@@ -117,6 +123,7 @@ def test_system_prompts():
result += get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=False,
+ support_multiple_cells=False,
custom_rules=None,
cell_code="<rewrite_this>pl.DataFrame()</rewrite_this>",
selected_text="pl.DataFrame()",
@@ -128,6 +135,32 @@ def test_system_prompts():
result += get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=False,
+ support_multiple_cells=False,
+ custom_rules=None,
+ cell_code=None,
+ selected_text=None,
+ other_cell_codes=None,
+ context=AiCompletionContext(
+ variables=[
+ VariableContext(
+ name="df",
+ value_type="DataFrame",
+ preview_value="<DataFrame with 100 rows and 5 columns>",
+ ),
+ VariableContext(
+ name="model",
+ value_type="Model",
+ preview_value="<Model object>",
+ ),
+ ]
+ ),
+ )
+
+ result += _header("with support_multiple_cells=True")
+ result += get_refactor_or_insert_notebook_cell_system_prompt(
+ language="python",
+ is_insert=False,
+ support_multiple_cells=True,
custom_rules=None,
cell_code=None,
selected_text=None,
@@ -155,6 +188,7 @@ def test_empty_rules():
assert get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=False,
+ support_multiple_cells=False,
custom_rules=None,
cell_code=None,
selected_text=None,
@@ -163,6 +197,7 @@ def test_empty_rules():
) == get_refactor_or_insert_notebook_cell_system_prompt(
language="python",
is_insert=False,
+ support_multiple_cells=False,
custom_rules=" ",
cell_code=None,
selected_text=None,