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(ai): stream validated structured cell completions (#10652)

Shahmir Varqha16efdda6bdde
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 e5c4c8ac7d..166ba167f8 100644
--- a/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
+++ b/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
@@ -19,7 +19,6 @@ import { store } from "@/core/state/jotai";
import { variablesAtom } from "@/core/variables/state";
import type { FileUIPart, UIMessage } from "ai";
import {
- codeToCells,
getAICompletionBody,
getAICompletionBodyWithAttachments,
isContextAttachment,
@@ -485,180 +484,3 @@ describe("context attachment stamping", () => {
expect(isContextAttachment(attachments[0])).toBe(true);
});
});
-
-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/__tests__/staged-cell-submission.test.ts b/frontend/src/components/editor/ai/__tests__/staged-cell-submission.test.ts
new file mode 100644
index 0000000000..a948b38163
--- /dev/null
+++ b/frontend/src/components/editor/ai/__tests__/staged-cell-submission.test.ts
@@ -0,0 +1,106 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { describe, expect, it, vi } from "vitest";
+import { Deferred } from "@/utils/Deferred";
+import { StagedCellSubmissionController } from "../staged-cell-submission";
+
+describe("StagedCellSubmissionController", () => {
+ it("ignores duplicate submissions while preprocessing", async () => {
+ const preparation = new Deferred<string>();
+ const submit = vi.fn(async () => undefined);
+ const onError = vi.fn();
+ const controller = new StagedCellSubmissionController();
+
+ const first = controller.run({
+ prepare: () => preparation.promise,
+ submit,
+ onError,
+ });
+ await controller.run({
+ prepare: vi.fn(async () => "duplicate"),
+ submit,
+ onError,
+ });
+ preparation.resolve("prepared");
+ await first;
+
+ expect(submit).toHaveBeenCalledExactlyOnceWith("prepared");
+ expect(onError).not.toHaveBeenCalled();
+ });
+
+ it("does not submit after cancellation during preprocessing", async () => {
+ const preparation = new Deferred<string>();
+ const submit = vi.fn(async () => undefined);
+ const onError = vi.fn();
+ const controller = new StagedCellSubmissionController();
+
+ const run = controller.run({
+ prepare: () => preparation.promise,
+ submit,
+ onError,
+ });
+ controller.cancel();
+ preparation.resolve("prepared");
+ await run;
+
+ expect(submit).not.toHaveBeenCalled();
+ expect(onError).not.toHaveBeenCalled();
+ });
+
+ it("blocks another submission until a cancelled submission settles", async () => {
+ const submission = new Deferred<void>();
+ const firstSubmit = vi.fn(() => submission.promise);
+ const secondSubmit = vi.fn(async () => undefined);
+ const onError = vi.fn();
+ const controller = new StagedCellSubmissionController();
+
+ const firstRun = controller.run({
+ prepare: async () => "first",
+ submit: firstSubmit,
+ onError,
+ });
+ await vi.waitFor(() => {
+ expect(firstSubmit).toHaveBeenCalledExactlyOnceWith("first");
+ });
+
+ controller.cancel();
+ await controller.run({
+ prepare: async () => "second",
+ submit: secondSubmit,
+ onError,
+ });
+ expect(secondSubmit).not.toHaveBeenCalled();
+
+ submission.resolve();
+ await firstRun;
+ await controller.run({
+ prepare: async () => "second",
+ submit: secondSubmit,
+ onError,
+ });
+
+ expect(secondSubmit).toHaveBeenCalledExactlyOnceWith("second");
+ expect(onError).not.toHaveBeenCalled();
+ });
+
+ it("reports preprocessing and submission errors", async () => {
+ const preprocessingError = new Error("preprocessing failed");
+ const submissionError = new Error("submission failed");
+ const onError = vi.fn();
+ const controller = new StagedCellSubmissionController();
+
+ await controller.run({
+ prepare: async () => Promise.reject(preprocessingError),
+ submit: vi.fn(),
+ onError,
+ });
+ await controller.run({
+ prepare: async () => "prepared",
+ submit: async () => Promise.reject(submissionError),
+ onError,
+ });
+
+ expect(onError).toHaveBeenNthCalledWith(1, preprocessingError);
+ expect(onError).toHaveBeenNthCalledWith(2, submissionError);
+ });
+});
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 5e05f806d8..f7d3c731ef 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -24,8 +24,9 @@ import {
SparklesIcon,
XIcon,
} from "lucide-react";
-import { useMemo, useRef, useState } from "react";
+import { useEffect, useMemo, useRef, useState } from "react";
import useEvent from "react-use-event-hook";
+import { DefaultChatTransport } from "ai";
import { z } from "zod";
import { AIModelDropdown } from "@/components/ai/ai-model-dropdown";
import {
@@ -37,7 +38,6 @@ import {
import {
buildCompletionRequestBody,
convertToFileUIPart,
- handleToolCall,
PROVIDERS_THAT_SUPPORT_ATTACHMENTS,
useFileState,
} from "@/components/chat/chat-utils";
@@ -52,13 +52,11 @@ import {
import { toast } from "@/components/ui/use-toast";
import { AiModelId } from "@/core/ai/ids/ids";
import { AI_SDK_UI_THROTTLE_MS } from "@/core/ai/constants";
-import { stagedAICellsAtom, useStagedCells } from "@/core/ai/staged-cells";
-import type { ToolNotebookContext } from "@/core/ai/tools/base";
-import { useCellActions } from "@/core/cells/cells";
+import type { CompletionUIMessage } from "@/core/ai/completion-output";
+import { useStagedCellGeneration } from "@/core/ai/staged-cells";
import { resourceExtension } from "@/core/codemirror/ai/resources";
import { aiAtom } from "@/core/config/config";
import { DEFAULT_AI_MODEL } from "@/core/config/config-schema";
-import { useRequestClient } from "@/core/network/requests";
import type { AiCompletionRequest } from "@/core/network/types";
import { useRuntimeManager } from "@/core/runtime/config";
import { useTheme } from "@/theme/useTheme";
@@ -73,7 +71,7 @@ import {
CONTEXT_TRIGGER,
mentionsCompletionSource,
} from "./completion-utils";
-import { StreamingChunkTransport } from "./transport/chat-transport";
+import { StagedCellSubmissionController } from "./staged-cell-submission";
// Persist across sessions
const languageAtom = atomWithStorage<"python" | "sql">(
@@ -95,78 +93,95 @@ export const AddCellWithAI: React.FC<{
const store = useStore();
const [input, setInput] = useState("");
- const { deleteAllStagedCells, clearStagedCells, onStream, addStagedCell } =
- useStagedCells(store);
+ const {
+ acceptOwnedStagedCells,
+ beginStagedCellGeneration,
+ discardOwnedStagedCells,
+ finishStagedCellGeneration,
+ hasOwnedStagedCells,
+ onData,
+ } = useStagedCellGeneration(store);
const [language, setLanguage] = useAtom(languageAtom);
const runtimeManager = useRuntimeManager();
- const { invokeAiTool, sendRun } = useRequestClient();
- const stagedAICells = useAtomValue(stagedAICellsAtom);
const inputRef = useRef<ReactCodeMirrorRef>(null);
+ const submissionController = useRef(
+ new StagedCellSubmissionController(),
+ ).current;
const fileInputRef = useRef<HTMLInputElement>(null);
const { files, addFiles, removeFile } = useFileState();
const aiConfig = useAtomValue(aiAtom);
- const { createNewCell, prepareForRun } = useCellActions();
- const toolContext: ToolNotebookContext = {
- store,
- addStagedCell,
- createNewCell,
- prepareForRun,
- sendRun,
- };
-
- const { sendMessage, stop, status, addToolOutput } = useChat({
- throttle: AI_SDK_UI_THROTTLE_MS,
- transport: new StreamingChunkTransport(
- {
+ const transport = useMemo(
+ () =>
+ new DefaultChatTransport<CompletionUIMessage>({
api: runtimeManager.getAiURL("completion").toString(),
headers: () => runtimeManager.headers(),
prepareSendMessagesRequest: async (options) => {
const completionBody = await buildCompletionRequestBody(
options.messages,
);
- const body: AiCompletionRequest = {
- ...options,
+ const body = {
...completionBody,
code: "",
prompt: "", // Don't need prompt since we are using messages
language: language,
- };
+ } satisfies AiCompletionRequest;
return {
api: runtimeManager.getAiURL("completion").toString(),
body: body,
};
},
- },
- (chunk) => {
- onStream(chunk);
- },
- ),
- onToolCall: async ({ toolCall }) => {
- await handleToolCall({
- invokeAiTool,
- addToolOutput,
- toolCall: {
- toolName: toolCall.toolName,
- toolCallId: toolCall.toolCallId,
- input: toolCall.input as Record<string, never>,
- },
- toolContext,
- });
- },
- onError: (error) => {
- toast({
- title: "Generate with AI failed",
- description: prettyError(error),
- });
+ }),
+ [language, runtimeManager],
+ );
+
+ const handleGenerationError = useEvent((error: unknown) => {
+ finishStagedCellGeneration(false);
+ toast({
+ title: "Generate with AI failed",
+ description: prettyError(error),
+ });
+ });
+
+ const {
+ sendMessage,
+ stop: stopChat,
+ status,
+ } = useChat<CompletionUIMessage>({
+ throttle: AI_SDK_UI_THROTTLE_MS,
+ transport,
+ onData,
+ onError: handleGenerationError,
+ onFinish: ({ isAbort, isDisconnect, isError, finishReason }) => {
+ finishStagedCellGeneration(
+ !isAbort && !isDisconnect && !isError && finishReason === "stop",
+ );
},
});
const isLoading = status === "streaming" || status === "submitted";
- const hasCompletion = stagedAICells.size > 0;
+ const hasCompletion = hasOwnedStagedCells();
+ const stop = useEvent(() => {
+ submissionController.cancel();
+ finishStagedCellGeneration(false);
+ void stopChat();
+ });
+
+ const reject = useEvent(() => {
+ submissionController.cancel();
+ discardOwnedStagedCells();
+ void stopChat();
+ });
+
+ // Parent-driven unmounts bypass the close handlers, so cancel in-flight work here.
+ useEffect(() => {
+ return () => {
+ stop();
+ };
+ }, [stop]);
const currentModel = aiConfig?.models?.edit_model || DEFAULT_AI_MODEL;
const currentProvider = AiModelId.parse(currentModel).providerId;
@@ -175,14 +190,19 @@ export const AddCellWithAI: React.FC<{
const submit = async () => {
if (!isLoading) {
- if (inputRef.current?.view) {
- storePrompt(inputRef.current.view);
- }
- // TODO: When we have conversations, don't delete existing cells
- deleteAllStagedCells();
-
- const fileParts = files ? await convertToFileUIPart(files) : undefined;
- sendMessage({ text: input, files: fileParts });
+ await submissionController.run({
+ prepare: async () => {
+ if (inputRef.current?.view) {
+ storePrompt(inputRef.current.view);
+ }
+ return files ? await convertToFileUIPart(files) : undefined;
+ },
+ submit: async (fileParts) => {
+ beginStagedCellGeneration();
+ await sendMessage({ text: input, files: fileParts });
+ },
+ onError: handleGenerationError,
+ });
}
};
@@ -225,18 +245,19 @@ export const AddCellWithAI: React.FC<{
);
const handleAcceptCompletion = () => {
- clearStagedCells();
- onClose();
+ if (acceptOwnedStagedCells()) {
+ onClose();
+ }
};
const handleDeclineCompletion = () => {
- deleteAllStagedCells();
+ reject();
// Focus the input so the user can refine the prompt.
inputRef.current?.view?.focus();
};
const handleClose = () => {
- deleteAllStagedCells();
+ reject();
onClose();
};
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index 92a6545a48..0b2fa81081 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -1,6 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { useCompletion } from "@ai-sdk/react";
+import { useChat } from "@ai-sdk/react";
import { EditorView } from "@codemirror/view";
import {
CircleCheckIcon,
@@ -10,6 +10,7 @@ import {
} from "lucide-react";
import React, { useCallback, useEffect, useId, useState } from "react";
import CodeMirrorMerge from "react-codemirror-merge";
+import { DefaultChatTransport } from "ai";
import { Button } from "@/components/ui/button";
import { customPythonLanguageSupport } from "@/core/codemirror/language/languages/python";
@@ -27,14 +28,19 @@ import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { Tooltip } from "@/components/ui/tooltip";
import { toast } from "@/components/ui/use-toast";
+import {
+ CELL_COMPLETION_DATA_TYPE,
+ cellCompletionSchema,
+ type CompletionUIMessage,
+} from "@/core/ai/completion-output";
import { AI_SDK_UI_THROTTLE_MS } from "@/core/ai/constants";
-import { stripWrappingBackticks } from "@/core/ai/strip-wrapping-backticks";
import { type AiCompletionCell, includeOtherCellsAtom } from "@/core/ai/state";
import type { CellId } from "@/core/cells/ids";
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 type { AiCompletionRequest } from "@/core/network/types";
import { useTheme } from "@/theme/useTheme";
import { cn } from "@/utils/cn";
import { prettyError } from "@/utils/errors";
@@ -90,7 +96,6 @@ export const AiCompletionEditor: React.FC<Props> = ({
children,
}) => {
const [showInputPrompt, setShowInputPrompt] = useState(false);
- const [completionBody, setCompletionBody] = useState<object>({});
const [includeOtherCells, setIncludeOtherCells] = useAtom(
includeOtherCellsAtom,
@@ -112,50 +117,84 @@ export const AiCompletionEditor: React.FC<Props> = ({
previousCellCode = updatedCell.previousCode;
}
+ const [completion, setCompletion] = useState("");
+ const [input, setInput] = useState(initialPrompt ?? "");
+ const transport = React.useMemo(
+ () =>
+ new DefaultChatTransport<CompletionUIMessage>({
+ api: runtimeManager.getAiURL("completion").toString(),
+ headers: () => runtimeManager.headers(),
+ prepareSendMessagesRequest: ({ body }) => ({ body: body ?? {} }),
+ }),
+ [runtimeManager],
+ );
const {
- completion: untrimmedCompletion,
- input,
- stop,
- isLoading,
- setCompletion,
- setInput,
- handleSubmit,
- complete,
- } = useCompletion({
- api: runtimeManager.getAiURL("completion").toString(),
- headers: runtimeManager.headers(),
- initialInput: initialPrompt,
+ sendMessage,
+ stop: stopChat,
+ status,
+ } = useChat<CompletionUIMessage>({
throttle: AI_SDK_UI_THROTTLE_MS,
- body: {
- ...(Object.keys(completionBody).length > 0
- ? completionBody
- : initialPrompt
- ? getAICompletionBody({ input: initialPrompt })
- : {}),
- includeOtherCode: includeOtherCells ? getCodes(currentCode) : "",
- code: currentCode,
- language: currentLanguageAdapter,
+ transport,
+ onData: (part) => {
+ if (part.type === CELL_COMPLETION_DATA_TYPE) {
+ const completion = cellCompletionSchema.parse(part.data);
+ setCompletion(completion.code.trimEnd());
+ }
},
onError: (error) => {
+ setCompletion("");
toast({
title: "Completion failed",
description: prettyError(error),
});
},
- onFinish: (_prompt, completion) => {
- setCompletion(stripWrappingBackticks(completion).trimEnd());
+ onFinish: ({ isAbort, isDisconnect, isError, finishReason }) => {
+ if (isAbort || isDisconnect || isError || finishReason !== "stop") {
+ setCompletion("");
+ }
},
});
+ const isLoading = status === "submitted" || status === "streaming";
+
+ const stop = useCallback(() => {
+ setCompletion("");
+ void stopChat();
+ }, [stopChat]);
+
+ const complete = useCallback(
+ async (prompt: string) => {
+ await stopChat();
+ setCompletion("");
+
+ const body = {
+ ...getAICompletionBody({ input: prompt }),
+ prompt,
+ includeOtherCode: includeOtherCells ? getCodes(currentCode) : "",
+ code: currentCode,
+ language: currentLanguageAdapter ?? "python",
+ } satisfies AiCompletionRequest;
+
+ await sendMessage({ text: prompt }, { body });
+ },
+ [
+ currentCode,
+ currentLanguageAdapter,
+ includeOtherCells,
+ sendMessage,
+ stopChat,
+ ],
+ );
+
+ const handleSubmit = useCallback(() => {
+ void complete(input);
+ }, [complete, input]);
const inputRef = React.useRef<ReactCodeMirrorRef>(null);
- const completion = stripWrappingBackticks(untrimmedCompletion, {
- streaming: isLoading,
- }).trimEnd();
const initialSubmit = useCallback(() => {
if (triggerImmediately && !isLoading && initialPrompt) {
// Use complete to pass the prompt directly, else input might be empty
- complete(initialPrompt);
+ void complete(initialPrompt);
}
// oxlint-disable-next-line react-hooks/exhaustive-deps
}, [triggerImmediately]);
@@ -197,7 +236,6 @@ export const AiCompletionEditor: React.FC<Props> = ({
// Reject discards the suggestion but keeps the prompt open for refinement.
const handleDeclineCompletion = () => {
stop();
- setCompletion("");
setShowInputPrompt(true);
inputRef.current?.view?.focus();
};
@@ -299,14 +337,11 @@ export const AiCompletionEditor: React.FC<Props> = ({
inputRef={inputRef}
className="h-full my-0 py-2 flex items-center"
onClose={() => {
+ stop();
declineChange();
- setCompletion("");
}}
value={input}
- onChange={(newValue) => {
- setInput(newValue);
- setCompletionBody(getAICompletionBody({ input: newValue }));
- }}
+ onChange={setInput}
onSubmit={() => {
if (!isLoading) {
if (inputRef.current?.view) {
@@ -378,7 +413,6 @@ export const AiCompletionEditor: React.FC<Props> = ({
onClick={() => {
stop();
declineChange();
- setCompletion("");
}}
>
<XIcon className="text-(--red-10)" size={16} />
diff --git a/frontend/src/components/editor/ai/completion-handlers.tsx b/frontend/src/components/editor/ai/completion-handlers.tsx
index bc65915e3f..57657135dc 100644
--- a/frontend/src/components/editor/ai/completion-handlers.tsx
+++ b/frontend/src/components/editor/ai/completion-handlers.tsx
@@ -77,6 +77,7 @@ export const CompletionActionsCellFooter: React.FC<{
onDecline={onDecline}
size={size}
label={declineLabel}
+ disabled={isLoading}
/>
</>
);
@@ -169,6 +170,7 @@ export const AcceptCompletionButton: React.FC<{
export const RejectCompletionButton: React.FC<{
onDecline: () => void;
+ disabled?: boolean;
multipleCompletions?: boolean;
size?: "xs" | "sm";
className?: string;
@@ -177,6 +179,7 @@ export const RejectCompletionButton: React.FC<{
label?: string;
}> = ({
onDecline,
+ disabled = false,
multipleCompletions = false,
size = "sm",
className,
@@ -188,6 +191,7 @@ export const RejectCompletionButton: React.FC<{
<Button
variant="text"
size={size}
+ disabled={disabled}
onClick={onDecline}
className={`h-6 text-(--red-10) bg-(--red-3)/60 hover:bg-(--red-3)
dark:bg-(--red-4)/80 dark:hover:bg-(--red-3) rounded px-3 font-semibold
diff --git a/frontend/src/components/editor/ai/completion-utils.ts b/frontend/src/components/editor/ai/completion-utils.ts
index d41cb09589..547801e99a 100644
--- a/frontend/src/components/editor/ai/completion-utils.ts
+++ b/frontend/src/components/editor/ai/completion-utils.ts
@@ -11,7 +11,6 @@ import type { DataUIPart, FileUIPart, UIMessage } from "ai";
import { getAIContextRegistry } from "@/core/ai/context/context";
import type { ContextLocatorId } from "@/core/ai/context/registry";
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";
@@ -249,94 +248,3 @@ 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/staged-cell-submission.ts b/frontend/src/components/editor/ai/staged-cell-submission.ts
new file mode 100644
index 0000000000..381d42b14e
--- /dev/null
+++ b/frontend/src/components/editor/ai/staged-cell-submission.ts
@@ -0,0 +1,46 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+interface SubmissionCallbacks<T> {
+ prepare: () => Promise<T>;
+ submit: (prepared: T) => Promise<void>;
+ onError: (error: unknown) => void;
+}
+
+interface SubmissionAttempt {
+ cancelled: boolean;
+}
+
+/** Coordinates preprocessing and submission without resuming cancelled work. */
+export class StagedCellSubmissionController {
+ private activeAttempt: SubmissionAttempt | null = null;
+
+ cancel() {
+ if (this.activeAttempt) {
+ this.activeAttempt.cancelled = true;
+ }
+ }
+
+ async run<T>({ prepare, submit, onError }: SubmissionCallbacks<T>) {
+ if (this.activeAttempt !== null) {
+ return;
+ }
+
+ const attempt: SubmissionAttempt = { cancelled: false };
+ this.activeAttempt = attempt;
+
+ try {
+ const prepared = await prepare();
+ if (attempt.cancelled) {
+ return;
+ }
+
+ await submit(prepared);
+ } catch (error) {
+ if (!attempt.cancelled) {
+ onError(error);
+ }
+ } finally {
+ this.activeAttempt = null;
+ }
+ }
+}
diff --git a/frontend/src/components/editor/cell/StagedAICell.tsx b/frontend/src/components/editor/cell/StagedAICell.tsx
index 447f08a785..9d4dc546bb 100644
--- a/frontend/src/components/editor/cell/StagedAICell.tsx
+++ b/frontend/src/components/editor/cell/StagedAICell.tsx
@@ -1,10 +1,11 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { useStore } from "jotai";
+import { useAtomValue, useStore } from "jotai";
import { SparklesIcon } from "lucide-react";
import { Tooltip } from "@/components/ui/tooltip";
import {
type Edit,
+ stagedGenerationInProgressAtom,
useStagedAICell,
useStagedCells,
} from "@/core/ai/staged-cells";
@@ -48,6 +49,7 @@ export const StagedAICellFooter: React.FC<{ cellId: CellId }> = ({
cellId,
}) => {
const store = useStore();
+ const generationInProgress = useAtomValue(stagedGenerationInProgressAtom);
const stagedAiCell = useStagedAICell(cellId);
const runCell = useRunCell(cellId);
@@ -91,7 +93,7 @@ export const StagedAICellFooter: React.FC<{ cellId: CellId }> = ({
</div>
<div className="flex items-center gap-1.5">
<CompletionActionsCellFooter
- isLoading={false}
+ isLoading={generationInProgress}
onAccept={() => handleCompletion("accept")}
onDecline={() => handleCompletion("reject")}
size="xs"
diff --git a/frontend/src/components/editor/cell/__tests__/StagedAICell.test.tsx b/frontend/src/components/editor/cell/__tests__/StagedAICell.test.tsx
new file mode 100644
index 0000000000..debffa24fc
--- /dev/null
+++ b/frontend/src/components/editor/cell/__tests__/StagedAICell.test.tsx
@@ -0,0 +1,64 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { render, screen } from "@testing-library/react";
+import { createStore, Provider } from "jotai";
+import { describe, expect, it, vi } from "vitest";
+import { cellId } from "@/__tests__/branded";
+import { TooltipProvider } from "@/components/ui/tooltip";
+import { stagedAICellsAtom, visibleForTesting } from "@/core/ai/staged-cells";
+import { StagedAICellFooter } from "../StagedAICell";
+
+vi.mock("@/components/editor/cell/useDeleteCell", () => ({
+ useDeleteCellCallback: () => vi.fn(),
+}));
+
+vi.mock("@/core/cells/cells", () => ({
+ getCellEditorView: vi.fn(),
+ useCellActions: () => ({
+ createNewCell: vi.fn(),
+ updateCellCode: vi.fn(),
+ }),
+}));
+
+vi.mock("../useRunCells", () => ({
+ useRunCell: () => undefined,
+}));
+
+describe("StagedAICellFooter", () => {
+ it("disables cell actions until generation is complete", () => {
+ const store = createStore();
+ const generatedCellId = cellId("generated-cell");
+ store.set(
+ stagedAICellsAtom,
+ new Map([[generatedCellId, { type: "add_cell" }]]),
+ );
+ store.set(visibleForTesting.stagedGenerationAtom, {
+ id: Symbol("staged-cell-generation"),
+ status: "in_progress",
+ cellIds: [generatedCellId],
+ });
+
+ const { rerender } = render(
+ <Provider store={store}>
+ <TooltipProvider>
+ <StagedAICellFooter cellId={generatedCellId} />
+ </TooltipProvider>
+ </Provider>,
+ );
+
+ expect(screen.getByRole("button", { name: "Keep cell" })).toBeDisabled();
+ expect(screen.getByRole("button", { name: "Discard cell" })).toBeDisabled();
+
+ store.set(visibleForTesting.stagedGenerationAtom, null);
+ rerender(
+ <Provider store={store}>
+ <TooltipProvider>
+ <StagedAICellFooter cellId={generatedCellId} />
+ </TooltipProvider>
+ </Provider>,
+ );
+
+ expect(screen.getByRole("button", { name: "Keep cell" })).toBeEnabled();
+ expect(screen.getByRole("button", { name: "Discard cell" })).toBeEnabled();
+ });
+});
diff --git a/frontend/src/components/editor/chrome/wrapper/__tests__/pending-ai-cells.test.tsx b/frontend/src/components/editor/chrome/wrapper/__tests__/pending-ai-cells.test.tsx
new file mode 100644
index 0000000000..55ab71ee25
--- /dev/null
+++ b/frontend/src/components/editor/chrome/wrapper/__tests__/pending-ai-cells.test.tsx
@@ -0,0 +1,77 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { act, render, screen, within } from "@testing-library/react";
+import { createStore, Provider } from "jotai";
+import { describe, expect, it, vi } from "vitest";
+import { cellId } from "@/__tests__/branded";
+import { TooltipProvider } from "@/components/ui/tooltip";
+import { stagedAICellsAtom, visibleForTesting } from "@/core/ai/staged-cells";
+import { PendingAICells } from "../pending-ai-cells";
+
+vi.mock("@/components/editor/cell/useDeleteCell", () => ({
+ useDeleteCellCallback: () => vi.fn(),
+}));
+
+vi.mock("@/core/cells/cells", () => ({
+ getCellEditorView: vi.fn(),
+ useCellActions: () => ({
+ createNewCell: vi.fn(),
+ updateCellCode: vi.fn(),
+ }),
+}));
+
+vi.mock("@/components/editor/cell/useRunCells", () => ({
+ useRunCells: () => vi.fn(),
+}));
+
+vi.mock("@/components/editor/links/cell-link", () => ({
+ scrollAndHighlightCell: vi.fn(),
+}));
+
+describe("PendingAICells", () => {
+ it("disables bulk actions while cells are still generating", () => {
+ const store = createStore();
+ const generationId = Symbol("staged-cell-generation");
+ store.set(
+ stagedAICellsAtom,
+ new Map([[cellId("generated-cell"), { type: "add_cell" }]]),
+ );
+ store.set(visibleForTesting.stagedGenerationAtom, {
+ id: generationId,
+ status: "in_progress",
+ cellIds: [cellId("generated-cell")],
+ });
+
+ const view = (
+ <Provider store={store}>
+ <TooltipProvider>
+ <PendingAICells />
+ </TooltipProvider>
+ </Provider>
+ );
+ const { rerender } = render(view);
+
+ const acceptButton = screen.getByRole("button", { name: "Keep all" });
+ const acceptButtons = within(
+ acceptButton.parentElement as HTMLElement,
+ ).getAllByRole("button");
+ expect(acceptButtons).toHaveLength(2);
+ for (const button of acceptButtons) {
+ expect(button).toBeDisabled();
+ }
+ expect(screen.getByRole("button", { name: "Discard all" })).toBeDisabled();
+
+ act(() => {
+ store.set(visibleForTesting.stagedGenerationAtom, {
+ id: generationId,
+ status: "complete",
+ });
+ });
+ rerender(view);
+
+ for (const button of acceptButtons) {
+ expect(button).toBeEnabled();
+ }
+ expect(screen.getByRole("button", { name: "Discard all" })).toBeEnabled();
+ });
+});
diff --git a/frontend/src/components/editor/chrome/wrapper/pending-ai-cells.tsx b/frontend/src/components/editor/chrome/wrapper/pending-ai-cells.tsx
index f937dc5f5a..ecca98b97a 100644
--- a/frontend/src/components/editor/chrome/wrapper/pending-ai-cells.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/pending-ai-cells.tsx
@@ -4,7 +4,11 @@ import { useAtomValue, useStore } from "jotai";
import { ChevronDown, ChevronUp, SparklesIcon } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
-import { stagedAICellsAtom, useStagedCells } from "@/core/ai/staged-cells";
+import {
+ stagedAICellsAtom,
+ stagedGenerationInProgressAtom,
+ useStagedCells,
+} from "@/core/ai/staged-cells";
import type { CellId } from "@/core/cells/ids";
import { getNextIndex } from "@/utils/arrays";
import { cn } from "@/utils/cn";
@@ -20,6 +24,7 @@ export const PendingAICells: React.FC = () => {
const [currentIndex, setCurrentIndex] = useState<number | null>(null);
const stagedAiCells = useAtomValue(stagedAICellsAtom);
+ const generationInProgress = useAtomValue(stagedGenerationInProgressAtom);
const listStagedCells = [...stagedAiCells.keys()];
const store = useStore();
const { deleteStagedCell, removeStagedCell } = useStagedCells(store);
@@ -93,7 +98,7 @@ export const PendingAICells: React.FC = () => {
<AcceptCompletionButton
multipleCompletions={true}
onAccept={acceptAllCompletions}
- isLoading={false}
+ isLoading={generationInProgress}
label="Keep all"
size="xs"
buttonStyles="h-6.5"
@@ -103,6 +108,7 @@ export const PendingAICells: React.FC = () => {
<RejectCompletionButton
multipleCompletions={true}
onDecline={rejectAllCompletions}
+ disabled={generationInProgress}
size="xs"
label="Discard all"
className="h-6.5"
diff --git a/frontend/src/core/ai/__tests__/staged-cells.test.ts b/frontend/src/core/ai/__tests__/staged-cells.test.ts
index 41d06a04d7..623ac3d110 100644
--- a/frontend/src/core/ai/__tests__/staged-cells.test.ts
+++ b/frontend/src/core/ai/__tests__/staged-cells.test.ts
@@ -6,20 +6,23 @@ import { createElement, type ReactNode } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { cellId } from "@/__tests__/branded";
import { CellId } from "@/core/cells/ids";
+import { getCellEditorView } from "../../cells/cells";
import { updateEditorCodeFromPython } from "../../codemirror/language/utils";
import {
- type StagedAICells,
stagedAICellsAtom,
+ stagedGenerationInProgressAtom,
useStagedAICell,
+ useStagedCellGeneration,
useStagedCells,
visibleForTesting,
} from "../staged-cells";
-const { createActions, reducer, initialState } = visibleForTesting;
+const { stagedGenerationAtom, createActions, reducer, initialState } =
+ visibleForTesting;
// Mock the dependencies
const mockCreateNewCell = vi.fn();
-const mockUpdateCellEditor = vi.fn();
+const mockUpdateCellCode = vi.fn();
const mockDeleteCellCallback = vi.fn();
// Mock cell handle with editor view
@@ -31,21 +34,29 @@ const mockCellHandle = {
},
};
-vi.mock("../../cells/cells", () => ({
- useCellActions: () => ({
- createNewCell: mockCreateNewCell,
- updateCellEditor: mockUpdateCellEditor,
- }),
- cellHandleAtom: vi.fn(() => ({
- read: vi.fn(() => mockCellHandle),
- })),
- getCellEditorView: vi.fn(() => mockCellHandle.current.editorViewOrNull),
-}));
+vi.mock("../../cells/cells", async () => {
+ const { atom } = await import("jotai");
+ return {
+ notebookAtom: atom({ cellData: {}, cellIds: { inOrderIds: [] } }),
+ useCellActions: () => ({
+ createNewCell: mockCreateNewCell,
+ updateCellCode: mockUpdateCellCode,
+ }),
+ cellHandleAtom: vi.fn(() => ({
+ read: vi.fn(() => mockCellHandle),
+ })),
+ getCellEditorView: vi.fn(() => mockCellHandle.current.editorViewOrNull),
+ };
+});
vi.mock("@/components/editor/cell/useDeleteCell", () => ({
useDeleteCellCallback: () => mockDeleteCellCallback,
}));
+vi.mock("@/core/network/requests", () => ({
+ getRequestClient: () => ({ sendRun: vi.fn() }),
+}));
+
vi.mock("../../codemirror/language/utils", () => ({
updateEditorCodeFromPython: vi.fn(),
}));
@@ -69,9 +80,11 @@ describe("staged-cells", () => {
// Reset mocks
vi.clearAllMocks();
+ vi.mocked(CellId.create).mockReset();
// Reset the atom state
store.set(stagedAICellsAtom, new Map());
+ store.set(stagedGenerationAtom, null);
});
describe("reducer and actions", () => {
@@ -160,20 +173,6 @@ describe("staged-cells", () => {
expect(newState).toBe(state);
});
-
- it("should clear all cells", () => {
- const state = new Map([
- [cellId1, { type: "add_cell" as const }],
- [cellId2, { type: "add_cell" as const }],
- ]);
- const newState = reducer(state, {
- type: "clearStagedCells",
- payload: undefined,
- });
-
- expect(newState).toEqual(new Map());
- });
-
it("should not mutate original state when adding", () => {
const state = new Map([[cellId1, { type: "add_cell" as const }]]);
const originalSize = state.size;
@@ -211,7 +210,6 @@ describe("staged-cells", () => {
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", () => {
@@ -282,34 +280,6 @@ describe("staged-cells", () => {
});
});
- 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
- const initialState: StagedAICells = new Map([
- [cellId1, { type: "add_cell" }],
- [cellId2, { type: "add_cell" }],
- ]);
- store.set(stagedAICellsAtom, initialState);
-
- 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 Map());
- });
-
it("should add staged cell with edit info", () => {
const { result } = renderHook(() => useStagedCells(store));
@@ -349,27 +319,6 @@ describe("staged-cells", () => {
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,
- edit: { type: "add_cell" },
- });
- result.current.addStagedCell({
- cellId: cellId2,
- edit: { type: "add_cell" },
- });
-
- // Then clear all
- result.current.clearStagedCells();
-
- // Check that no cells remain
- const state = store.get(stagedAICellsAtom);
- expect(state).toEqual(new Map());
- });
-
it("should handle multiple operations correctly", () => {
const { result } = renderHook(() => useStagedCells(store));
@@ -434,92 +383,460 @@ describe("staged-cells", () => {
});
});
-describe("onStream", () => {
+describe("staged cell generation", () => {
let store: ReturnType<typeof getDefaultStore>;
+ const renderGenerationHook = () =>
+ renderHook(() => useStagedCellGeneration(store));
+
beforeEach(() => {
store = getDefaultStore();
+ vi.clearAllMocks();
+ vi.mocked(CellId.create).mockReset();
+ store.set(stagedAICellsAtom, new Map());
+ store.set(stagedGenerationAtom, null);
});
- it("should create a cell creation stream", () => {
- const { result } = renderHook(() => useStagedCells(store));
- result.current.onStream({ type: "text-start", id: "test-id" });
+ it("should begin generation", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
// No cell or cell update should have been called
expect(mockCreateNewCell).not.toHaveBeenCalled();
- expect(mockUpdateCellEditor).not.toHaveBeenCalled();
+ expect(updateEditorCodeFromPython).not.toHaveBeenCalled();
+ expect(store.get(stagedGenerationInProgressAtom)).toBe(true);
+ });
+
+ it("should mark generation complete after a successful finish", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+ result.current.finishStagedCellGeneration(true);
+
+ expect(store.get(stagedGenerationInProgressAtom)).toBe(false);
});
- 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",
+ it("should not couple unrelated staged-cell removal to generation state", () => {
+ const { result: stagedCells } = renderHook(() => useStagedCells(store));
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ const chatCellId = cellId("chat-edited-cell");
+ stagedCells.current.addStagedCell({
+ cellId: chatCellId,
+ edit: { type: "update_cell", previousCode: "before" },
});
+ stagedCells.current.removeStagedCell(chatCellId);
- // No cell or cell update should have been called
- expect(mockCreateNewCell).not.toHaveBeenCalled();
- expect(mockUpdateCellEditor).not.toHaveBeenCalled();
+ expect(store.get(stagedGenerationInProgressAtom)).toBe(true);
+ });
+
+ it("should only accept cells owned by the completed generation", () => {
+ const firstGeneration = renderGenerationHook();
+ firstGeneration.result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create).mockReturnValueOnce(cellId("superseded-cell"));
+ firstGeneration.result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "old" }],
+ },
+ });
+ firstGeneration.result.current.finishStagedCellGeneration(true);
+
+ const activeGeneration = renderGenerationHook();
+ activeGeneration.result.current.beginStagedCellGeneration();
+ vi.mocked(CellId.create).mockReturnValueOnce(cellId("active-cell"));
+ activeGeneration.result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "active" }],
+ },
+ });
+ activeGeneration.result.current.finishStagedCellGeneration(true);
+
+ expect(firstGeneration.result.current.hasOwnedStagedCells()).toBe(false);
+ expect(firstGeneration.result.current.acceptOwnedStagedCells()).toBe(false);
+ expect(store.get(stagedAICellsAtom)).toEqual(
+ new Map([
+ [cellId("superseded-cell"), { type: "add_cell" }],
+ [cellId("active-cell"), { type: "add_cell" }],
+ ]),
+ );
+
+ expect(activeGeneration.result.current.hasOwnedStagedCells()).toBe(true);
+ expect(activeGeneration.result.current.acceptOwnedStagedCells()).toBe(true);
+ expect(store.get(stagedAICellsAtom)).toEqual(
+ new Map([[cellId("superseded-cell"), { type: "add_cell" }]]),
+ );
+ });
+
+ it("should replace only the previous generation owned by this hook", () => {
+ const { result: stagedCells } = renderHook(() => useStagedCells(store));
+ const { result } = renderGenerationHook();
+ stagedCells.current.addStagedCell({
+ cellId: cellId("chat-edited-cell"),
+ edit: { type: "update_cell", previousCode: "before" },
+ });
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create).mockReturnValueOnce(cellId("generated-cell"));
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "generated" }],
+ },
+ });
+ result.current.finishStagedCellGeneration(true);
+ mockDeleteCellCallback.mockClear();
+
+ result.current.beginStagedCellGeneration();
+
+ expect(mockDeleteCellCallback).toHaveBeenCalledExactlyOnceWith({
+ cellId: cellId("generated-cell"),
+ });
+ expect(store.get(stagedAICellsAtom)).toEqual(
+ new Map([
+ [
+ cellId("chat-edited-cell"),
+ { type: "update_cell", previousCode: "before" },
+ ],
+ ]),
+ );
+ expect(store.get(stagedGenerationInProgressAtom)).toBe(true);
+ });
+
+ it("should leave staged cells from another workflow untouched when accepting", () => {
+ const { result: stagedCells } = renderHook(() => useStagedCells(store));
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create).mockReturnValueOnce(cellId("generated-cell"));
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "generated" }],
+ },
+ });
+ result.current.finishStagedCellGeneration(true);
+ stagedCells.current.addStagedCell({
+ cellId: cellId("chat-edited-cell"),
+ edit: { type: "update_cell", previousCode: "before" },
+ });
+
+ expect(result.current.acceptOwnedStagedCells()).toBe(true);
+ expect(store.get(stagedAICellsAtom)).toEqual(
+ new Map([
+ [
+ cellId("chat-edited-cell"),
+ { type: "update_cell", previousCode: "before" },
+ ],
+ ]),
+ );
+ });
+
+ it("should only discard the generation's remaining staged cells", () => {
+ const { result: stagedCells } = renderHook(() => useStagedCells(store));
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create)
+ .mockReturnValueOnce(cellId("accepted-cell"))
+ .mockReturnValueOnce(cellId("pending-cell"));
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [
+ { language: "python", code: "accepted" },
+ { language: "python", code: "pending" },
+ ],
+ },
+ });
+ result.current.finishStagedCellGeneration(true);
+ stagedCells.current.removeStagedCell(cellId("accepted-cell"));
+ stagedCells.current.addStagedCell({
+ cellId: cellId("chat-edited-cell"),
+ edit: { type: "update_cell", previousCode: "before" },
+ });
+
+ expect(result.current.discardOwnedStagedCells()).toBe(true);
+ expect(mockDeleteCellCallback).not.toHaveBeenCalledWith({
+ cellId: cellId("accepted-cell"),
+ });
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({
+ cellId: cellId("pending-cell"),
+ });
+ expect(mockDeleteCellCallback).not.toHaveBeenCalledWith({
+ cellId: cellId("chat-edited-cell"),
+ });
+ expect(store.get(stagedAICellsAtom)).toEqual(
+ new Map([
+ [
+ cellId("chat-edited-cell"),
+ { type: "update_cell", previousCode: "before" },
+ ],
+ ]),
+ );
+ });
+
+ it.each([
+ { name: "no cells", cells: [] },
+ {
+ name: "an empty cell",
+ cells: [{ language: "python" as const, code: "" }],
+ },
+ ])("should reject $name", ({ cells }) => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ expect(() =>
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: { cells },
+ }),
+ ).toThrow();
});
- 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" });
+ it("should create cells from a validated completion", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
// Mock CellId.create to return a predictable ID
const mockCellId = cellId("mock-cell-id");
vi.mocked(CellId.create).mockReturnValue(mockCellId);
- result.current.onStream({
- type: "text-delta",
- id: "test-id",
- delta: "some code",
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "print('```')" }],
+ },
});
expect(mockCreateNewCell).toHaveBeenCalledWith({
cellId: "__end__",
- code: "some code",
+ code: "print('```')",
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" });
+ it("should create multiple cells", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create)
+ .mockReturnValueOnce(cellId("first-cell"))
+ .mockReturnValueOnce(cellId("second-cell"));
+
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [
+ { language: "python", code: "value = 1" },
+ { language: "python", code: "value" },
+ ],
+ },
+ });
- const mockCellId = cellId("mock-cell-id");
- vi.mocked(CellId.create).mockReturnValue(mockCellId);
+ expect(mockCreateNewCell).toHaveBeenCalledWith(
+ expect.objectContaining({
+ code: "value = 1",
+ newCellId: "first-cell",
+ }),
+ );
+ expect(mockCreateNewCell).toHaveBeenCalledWith(
+ expect.objectContaining({
+ code: "value",
+ newCellId: "second-cell",
+ }),
+ );
+ });
+
+ it("should apply cumulative snapshots without recreating cells", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
- result.current.onStream({
- type: "text-delta",
- id: "test-id",
- delta: "``",
+ vi.mocked(CellId.create)
+ .mockReturnValueOnce(cellId("first-cell"))
+ .mockReturnValueOnce(cellId("second-cell"));
+
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "value" }],
+ },
+ });
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [
+ { language: "python", code: "value = 1" },
+ { language: "python", code: "value" },
+ ],
+ },
+ });
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [
+ { language: "python", code: "value = 1" },
+ { language: "python", code: "value + 1" },
+ ],
+ },
});
- expect(mockCreateNewCell).toHaveBeenCalledWith({
- cellId: "__end__",
- code: "``",
- before: false,
- newCellId: "mock-cell-id",
+ expect(mockCreateNewCell).toHaveBeenCalledTimes(2);
+ expect(store.get(stagedAICellsAtom).has(cellId("first-cell"))).toBe(true);
+ expect(store.get(stagedAICellsAtom).has(cellId("second-cell"))).toBe(true);
+ expect(updateEditorCodeFromPython).toHaveBeenCalledTimes(2);
+ });
+
+ it("should update notebook state when the cell editor is not mounted", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create).mockReturnValueOnce(cellId("unmounted-cell"));
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "value" }],
+ },
});
- result.current.onStream({
- type: "text-delta",
- id: "test-id",
- delta: "```python\nsome code",
+ vi.mocked(getCellEditorView).mockReturnValueOnce(null);
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "value = 1" }],
+ },
});
- // Now the cell is recognized and only some code is seen
- expect(vi.mocked(updateEditorCodeFromPython)).toHaveBeenCalledWith(
- mockCellHandle.current.editorViewOrNull,
- "some code",
+ expect(mockUpdateCellCode).toHaveBeenCalledWith({
+ cellId: cellId("unmounted-cell"),
+ code: "value = 1",
+ formattingChange: false,
+ });
+ });
+
+ it("should remove trailing cells when a retry produces a shorter snapshot", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create)
+ .mockReturnValueOnce(cellId("first-cell"))
+ .mockReturnValueOnce(cellId("second-cell"));
+
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [
+ { language: "python", code: "value = 1" },
+ { language: "python", code: "value" },
+ ],
+ },
+ });
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "replacement = 2" }],
+ },
+ });
+
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({
+ cellId: cellId("second-cell"),
+ });
+ expect(store.get(stagedAICellsAtom).has(cellId("first-cell"))).toBe(true);
+ expect(store.get(stagedAICellsAtom).has(cellId("second-cell"))).toBe(false);
+ });
+
+ it("should remove a provisional marimo import when a retry no longer needs it", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create)
+ .mockReturnValueOnce(cellId("generated-cell"))
+ .mockReturnValueOnce(cellId("marimo-import"));
+
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "sql", code: "result = mo.sql('SELECT 1')" }],
+ },
+ });
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "result = 1" }],
+ },
+ });
+
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({
+ cellId: cellId("marimo-import"),
+ });
+ expect(store.get(stagedAICellsAtom).has(cellId("marimo-import"))).toBe(
+ false,
);
+ });
+
+ it("should discard provisional cells when generation fails", () => {
+ const { result } = renderGenerationHook();
+ result.current.beginStagedCellGeneration();
+
+ vi.mocked(CellId.create).mockReturnValue(cellId("partial-cell"));
+ result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "partial" }],
+ },
+ });
+ result.current.finishStagedCellGeneration(false);
+
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({
+ cellId: cellId("partial-cell"),
+ });
+ expect(store.get(stagedAICellsAtom)).toEqual(new Map());
+ expect(store.get(stagedGenerationInProgressAtom)).toBe(false);
+ });
+
+ it("should not let a superseded generation mutate active cells", () => {
+ const firstGeneration = renderGenerationHook();
+ firstGeneration.result.current.beginStagedCellGeneration();
- result.current.onStream({
- type: "text-delta",
- id: "test-id",
- delta: "\n```",
+ vi.mocked(CellId.create).mockReturnValueOnce(cellId("superseded-cell"));
+ firstGeneration.result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "old" }],
+ },
});
+
+ const activeGeneration = renderGenerationHook();
+ activeGeneration.result.current.beginStagedCellGeneration();
+ expect(mockDeleteCellCallback).toHaveBeenCalledWith({
+ cellId: cellId("superseded-cell"),
+ });
+ mockDeleteCellCallback.mockClear();
+
+ vi.mocked(CellId.create).mockReturnValueOnce(cellId("active-cell"));
+ activeGeneration.result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "active" }],
+ },
+ });
+
+ firstGeneration.result.current.finishStagedCellGeneration(true);
+ firstGeneration.result.current.onData({
+ type: "data-notebook-cells-completion",
+ data: {
+ cells: [{ language: "python", code: "stale" }],
+ },
+ });
+
+ expect(mockDeleteCellCallback).not.toHaveBeenCalledWith({
+ cellId: cellId("active-cell"),
+ });
+ expect(store.get(stagedAICellsAtom)).toEqual(
+ new Map([[cellId("active-cell"), { type: "add_cell" }]]),
+ );
+ expect(store.get(stagedGenerationInProgressAtom)).toBe(true);
});
});
diff --git a/frontend/src/core/ai/__tests__/stream-completion-text.test.ts b/frontend/src/core/ai/__tests__/stream-completion-text.test.ts
new file mode 100644
index 0000000000..a2d64c4d09
--- /dev/null
+++ b/frontend/src/core/ai/__tests__/stream-completion-text.test.ts
@@ -0,0 +1,74 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import { streamCompletionText } from "../stream-completion-text";
+
+function streamResponse(...chunks: object[]): Response {
+ const body = [
+ ...chunks.map((chunk) => `data: ${JSON.stringify(chunk)}\n\n`),
+ "data: [DONE]\n\n",
+ ].join("");
+ return new Response(body, {
+ headers: { "Content-Type": "text/event-stream" },
+ });
+}
+
+describe("streamCompletionText", () => {
+ it("returns validated cell code", async () => {
+ const response = streamResponse(
+ { type: "start" },
+ {
+ type: "data-cell-completion",
+ data: { code: "print('```')" },
+ },
+ { type: "finish", finishReason: "stop" },
+ );
+
+ await expect(streamCompletionText(response)).resolves.toBe("print('```')");
+ });
+
+ it("rejects a missing structured completion", async () => {
+ const response = streamResponse(
+ { type: "start" },
+ { type: "finish", finishReason: "stop" },
+ );
+
+ await expect(streamCompletionText(response)).rejects.toThrow(
+ "AI completion returned no cell code",
+ );
+ });
+
+ it("rejects malformed completion data", async () => {
+ const response = streamResponse({
+ type: "data-cell-completion",
+ data: { code: 42 },
+ });
+
+ await expect(streamCompletionText(response)).rejects.toThrow();
+ });
+
+ it("rejects a partial completion without successful final validation", async () => {
+ const response = streamResponse({
+ type: "data-cell-completion",
+ data: { code: "partial" },
+ });
+
+ await expect(streamCompletionText(response)).rejects.toThrow(
+ "AI completion ended before final validation",
+ );
+ });
+
+ it("rejects a completion with an error finish reason", async () => {
+ const response = streamResponse(
+ {
+ type: "data-cell-completion",
+ data: { code: "partial" },
+ },
+ { type: "finish", finishReason: "error" },
+ );
+
+ await expect(streamCompletionText(response)).rejects.toThrow(
+ "AI completion ended before final validation",
+ );
+ });
+});
diff --git a/frontend/src/core/ai/__tests__/strip-wrapping-backticks.test.ts b/frontend/src/core/ai/__tests__/strip-wrapping-backticks.test.ts
deleted file mode 100644
index eedda36e6b..0000000000
--- a/frontend/src/core/ai/__tests__/strip-wrapping-backticks.test.ts
+++ /dev/null
@@ -1,133 +0,0 @@
-/* Copyright 2026 Marimo. All rights reserved. */
-
-import { describe, expect, it } from "vitest";
-import { stripWrappingBackticks } from "../strip-wrapping-backticks";
-
-// Cases aligned with `without_wrapping_backticks` on a complete string (single chunk).
-const CASES: [string[], string][] = [
- [["Hello", " world", "!"], "Hello world!"],
- [["```", "print('hello')", "print('world')"], "print('hello')print('world')"],
- [
- ["print('hello')", "print('world')", "```"],
- "print('hello')print('world')```",
- ],
- [["```", "print('hello')", "```"], "print('hello')"],
- [["Hello", " ``` ", "world"], "Hello ``` world"],
- [["``", "`print('hello')", "``", "`"], "print('hello')"],
- [["``", "`", "\n", "print('hello')", "\n", "``", "`"], "print('hello')"],
- [
- ["```\n", "print('hello')", "print('world')", "\n```"],
- "print('hello')print('world')",
- ],
- [
- ["```\nprint('hello')\n", "print('world')\n```"],
- "print('hello')\nprint('world')",
- ],
- [
- ["```\n", "def test():\n ", "return True\n```"],
- "def test():\n return True",
- ],
- [[], ""],
- [["```idk```"], "idk"],
- [["Hello world"], "Hello world"],
- [
- ["```python\n", "def hello():\n ", "print('world')\n```"],
- "def hello():\n print('world')",
- ],
- [
- ["```python", "\ndef hello():\n ", "print('world')\n```"],
- "def hello():\n print('world')",
- ],
- [
- ["```sql", "SELECT * FROM table", " WHERE id = 1", "```"],
- "SELECT * FROM table WHERE id = 1",
- ],
- [
- ["```sql\n", "SELECT * FROM table\n", "WHERE id = 1\n```"],
- "SELECT * FROM table\nWHERE id = 1",
- ],
- [["```", "print('hello')", "``` "], "print('hello') "],
- [["```python\n", "print('hello')", "\n``` "], "print('hello') "],
- [["```", "code", "```\t\n"], "code\t\n"],
- [[" ```", "code", "```"], "code"],
- [[" ```python\n", "code", "```"], "code"],
- [["\t```", "code", "```"], "code"],
- [["\n", "\n", "```\n", "code", "\n```\n"], "code\n"],
- [["\n", "\n", "```python\n", "code", "\n```\n"], "code\n"],
- [["\n``", "`python\n", "code", "\n```\n"], "code\n"],
- [["\n`", "`", "`python\n", "code", "\n```\n"], "code\n"],
- [["```python ", "code", "```"], " code"],
- [["```python\t", "code", "```"], "\tcode"],
- [["```\n", "```"], ""],
- [["```python\n", "```"], ""],
- [["```\n", "code"], "code"],
- [["```python\n", "code"], "code"],
- [["code", "\n```"], "code\n```"],
- [
- ["```\n", "x = 1\n", "```\n", "```\n", "y = 2\n", "```"],
- "x = 1\n```\n```\ny = 2",
- ],
- [["```python\n", "s = 'use `backticks`'\n", "```"], "s = 'use `backticks`'"],
- [["``", "`\n", "code\n", "``", "`"], "code"],
- [["```", "python\n", "code\n", "```"], "code"],
- [["```", "code", "```"], "code"],
- [["prefix ", "```\n", "code\n", "```"], "prefix ```\ncode\n```"],
- [["```\n", "code\n", "```", " suffix"], "code\n``` suffix"],
- [["```\n\n", "code\n\n", "```"], "\ncode\n"],
- [["```\n", " code \n", "```"], " code "],
- [["```\n", "\tcode\t\n", "```"], "\tcode\t"],
- [
- ["```\n", "x\n", "```\n", "```python\n", "y\n", "```"],
- "x\n```\n```python\ny",
- ],
- [["```javascript\n", "console.log()\n", "```"], "javascript\nconsole.log()"],
- [["```markdown\n", "# Title\n", "```"], "# Title"],
-];
-
-describe("stripWrappingBackticks", () => {
- it.each(CASES)("strips fences for %j", (chunks, expected) => {
- expect(stripWrappingBackticks(chunks.join(""))).toBe(expected);
- });
-});
-
-// In streaming mode, the opening fence is stripped as soon as it is
-// unambiguous, but the closing fence is left untouched (it may not have
-// arrived yet, or a trailing "```" may be content).
-const STREAMING_CASES: [string, string][] = [
- // No fence: passthrough.
- ["import pandas as pd", "import pandas as pd"],
- // Plain opening fence stripped once the first line is terminated.
- ["```\n", ""],
- ["```\ncode", "code"],
- ["```\ncode\nmore", "code\nmore"],
- // Opening fence stripped, closing fence intentionally kept while streaming.
- ["```\ncode\n```", "code\n```"],
- ["```python\ncode\n```", "code\n```"],
- // Language fences stripped as soon as the full identifier is present.
- ["```python", ""],
- ["```python\n", ""],
- ["```python\ncode", "code"],
- ["```sql\nSELECT 1", "SELECT 1"],
- ["```markdown\n# Title", "# Title"],
- // Leading whitespace before the fence.
- [" ```python\ncode", "code"],
- // Partial language identifiers are left intact until they resolve.
- ["```", "```"],
- ["```p", "```p"],
- ["```py", "```py"],
- ["```pyth", "```pyth"],
- ["```s", "```s"],
- ["```mark", "```mark"],
- // First-line content that is not a known language prefix is stripped as a
- // plain fence even before a newline arrives.
- ["```x", "x"],
- ["```x = 1", "x = 1"],
- // Unsupported language identifiers are kept as content (matches final mode).
- ["```json\ncode", "json\ncode"],
-];
-
-describe("stripWrappingBackticks (streaming)", () => {
- it.each(STREAMING_CASES)("strips opening fence for %j", (text, expected) => {
- expect(stripWrappingBackticks(text, { streaming: true })).toBe(expected);
- });
-});
diff --git a/frontend/src/core/ai/completion-output.ts b/frontend/src/core/ai/completion-output.ts
new file mode 100644
index 0000000000..7c4d28d014
--- /dev/null
+++ b/frontend/src/core/ai/completion-output.ts
@@ -0,0 +1,56 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import type { DataUIPart, UIMessage, UIMessageChunk } from "ai";
+import { z } from "zod";
+
+// Mirrored from marimo/_server/ai/completion_output.py; SSE data parts are not
+// represented in OpenAPI.
+const CELL_COMPLETION_DATA_PART = "cell-completion" as const;
+const NOTEBOOK_CELLS_COMPLETION_DATA_PART =
+ "notebook-cells-completion" as const;
+
+export const CELL_COMPLETION_DATA_TYPE =
+ `data-${CELL_COMPLETION_DATA_PART}` as const;
+export const NOTEBOOK_CELLS_COMPLETION_DATA_TYPE =
+ `data-${NOTEBOOK_CELLS_COMPLETION_DATA_PART}` as const;
+
+export const cellCompletionSchema = z.object({
+ code: z.string(),
+});
+
+export const generatedCellSchema = z.object({
+ language: z.enum(["python", "sql", "markdown"]),
+ code: z.string().min(1),
+});
+
+export const notebookCellsCompletionSchema = z.object({
+ cells: z.array(generatedCellSchema).min(1),
+});
+
+export type CellCompletion = z.infer<typeof cellCompletionSchema>;
+export type GeneratedCell = z.infer<typeof generatedCellSchema>;
+export type NotebookCellsCompletion = z.infer<
+ typeof notebookCellsCompletionSchema
+>;
+
+// Keep parsing data parts explicitly at the SSE boundary. AI SDK 7.0.37 types
+// schema keys without `data-`, but its runtime lookup uses the full wire type.
+const completionDataSchemas = {
+ [CELL_COMPLETION_DATA_PART]: cellCompletionSchema,
+ [NOTEBOOK_CELLS_COMPLETION_DATA_PART]: notebookCellsCompletionSchema,
+};
+
+export type CompletionDataParts = {
+ [Name in keyof typeof completionDataSchemas]: z.infer<
+ (typeof completionDataSchemas)[Name]
+ >;
+};
+
+export type CompletionUIMessage = UIMessage<unknown, CompletionDataParts>;
+export type CompletionDataPart = DataUIPart<CompletionDataParts>;
+
+type DataChunk = Extract<UIMessageChunk, { type: `data-${string}` }>;
+
+export function isDataChunk(chunk: UIMessageChunk): chunk is DataChunk {
+ return chunk.type.startsWith("data-");
+}
diff --git a/frontend/src/core/ai/staged-cells.ts b/frontend/src/core/ai/staged-cells.ts
index f50ee9d09d..abc8961bdc 100644
--- a/frontend/src/core/ai/staged-cells.ts
+++ b/frontend/src/core/ai/staged-cells.ts
@@ -1,34 +1,32 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import type { UIMessageChunk } from "ai";
-import { useAtomValue } from "jotai";
+import { atom, useAtomValue } from "jotai";
import { selectAtom } from "jotai/utils";
import { useMemo, 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 { logNever } from "@/utils/assertNever";
import { createReducerAndAtoms } from "@/utils/createReducer";
import { Logger } from "@/utils/Logger";
+import {
+ type CompletionDataPart,
+ NOTEBOOK_CELLS_COMPLETION_DATA_TYPE,
+ type GeneratedCell,
+ notebookCellsCompletionSchema,
+} from "./completion-output";
import { maybeAddMarimoImport } from "../cells/add-missing-import";
import {
type CreateNewCellAction,
getCellEditorView,
useCellActions,
} from "../cells/cells";
-import type { LanguageAdapterType } from "../codemirror/language/types";
import { updateEditorCodeFromPython } from "../codemirror/language/utils";
import type { JotaiStore } from "../state/jotai";
import type { EditType } from "./tools/edit-notebook-tool";
/**
- * Cells that are staged for AI completion
- * They function similarly to cells in the notebook, but they can be accepted or rejected by the user.
- * We track edited, new and deleted cells.
- * And we only track one set of staged cells at a time.
+ * Pending AI edits shared by completion, chat, and agent workflows.
+ * Generation ownership keeps one workflow from accepting or discarding another's
+ * staged cells.
*/
export type Edit =
@@ -38,6 +36,25 @@ export type Edit =
export type StagedAICells = Map<CellId, Edit>;
+type StagedGeneration =
+ | {
+ id: symbol;
+ status: "in_progress";
+ cellIds: readonly CellId[];
+ }
+ | { id: symbol; status: "complete" };
+
+interface OwnedStagedGeneration {
+ id: symbol;
+ status: "in_progress" | "complete";
+ reconciler: StagedCellReconciler;
+}
+
+const stagedGenerationAtom = atom<StagedGeneration | null>(null);
+export const stagedGenerationInProgressAtom = atom(
+ (get) => get(stagedGenerationAtom)?.status === "in_progress",
+);
+
const initialState = (): StagedAICells => {
return new Map();
};
@@ -60,16 +77,9 @@ const {
newState.delete(cellId);
return newState;
},
- clearStagedCells: () => {
- return initialState();
- },
});
-export {
- useStagedAICellsActions,
- createActions as createStagedAICellsActions,
- reducer as stagedAICellsReducer,
-};
+export { useStagedAICellsActions };
export function useStagedAICell(cellId: CellId): Edit | undefined {
const stagedCellAtom = useMemo(
@@ -83,20 +93,14 @@ export function useStagedAICell(cellId: CellId): Edit | undefined {
interface UpdateStagedCellAction {
cellId: CellId;
code: string;
- language?: LanguageAdapterType;
}
-/**
- * Helper functions to create and delete staged cells.
- */
+/** Manage staged cells without assuming which AI workflow owns them. */
export function useStagedCells(store: JotaiStore) {
- const { addStagedCell, removeStagedCell, clearStagedCells } =
- useStagedAICellsActions();
+ const { addStagedCell, removeStagedCell } = useStagedAICellsActions();
const { createNewCell, updateCellCode } = useCellActions();
const deleteCellCallback = useDeleteCellCallback();
- const cellCreationStream = useRef<CellCreationStream | null>(null);
-
const createStagedCell = (code: string): CellId => {
const newCellId = CellId.create();
addStagedCell({ cellId: newCellId, edit: { type: "add_cell" } });
@@ -118,8 +122,6 @@ export function useStagedCells(store: JotaiStore) {
return;
}
- // Update the editor code if the cell is mounted
- // Else, update the cell code in the notebook
const editorView = getCellEditorView(cellId);
if (editorView) {
updateEditorCodeFromPython(editorView, code);
@@ -128,190 +130,269 @@ export function useStagedCells(store: JotaiStore) {
}
};
- // 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 = () => {
+ return {
+ createStagedCell,
+ updateStagedCell,
+ addStagedCell,
+ removeStagedCell,
+ deleteStagedCell,
+ };
+}
+
+/** Owns the lifecycle of cells created by one structured completion flow. */
+export function useStagedCellGeneration(store: JotaiStore) {
+ const stagedCellActions = useStagedCells(store);
+ const { createNewCell } = useCellActions();
+ const ownedGeneration = useRef<OwnedStagedGeneration | null>(null);
+ // Ownership lives in a ref, so subscribe to its backing atoms to rerender
+ // consumers when completion or per-cell acceptance changes.
+ useAtomValue(stagedGenerationAtom, { store });
+ useAtomValue(stagedAICellsAtom, { store });
+
+ const discardStagedCellIds = (cellIds: readonly CellId[]) => {
const stagedAICells = store.get(stagedAICellsAtom);
- for (const cellId of stagedAICells.keys()) {
- deleteCellCallback({ cellId });
+ for (const cellId of cellIds.toReversed()) {
+ if (stagedAICells.has(cellId)) {
+ stagedCellActions.deleteStagedCell(cellId);
+ }
}
- clearStagedCells();
};
- const onStream = (chunk: UIMessageChunk) => {
- switch (chunk.type) {
- case "text-start":
- // Create stream
- cellCreationStream.current = new CellCreationStream(
- createStagedCell,
- updateStagedCell,
- addStagedCell,
- createNewCell,
- );
- 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;
- case "abort":
- case "error":
- case "tool-input-error":
- case "tool-output-error":
- Logger.error("Error", chunk.type, { chunk });
- break;
- case "tool-approval-request":
- Logger.log("Tool approval request", { chunk });
- break;
- case "tool-output-denied":
- Logger.error("Tool output denied", { chunk });
- break;
- // These logs are not useful for debugging
- case "start":
- case "start-step":
- case "finish-step":
- case "data-reasoning-signature":
- break;
- case "message-metadata":
- case "tool-input-available":
- case "tool-output-available":
- case "tool-approval-response":
- case "reasoning-start":
- case "reasoning-delta":
- case "reasoning-end":
- case "file":
- case "reasoning-file":
- case "source-document":
- case "source-url":
- case "tool-input-start":
- case "tool-input-delta":
- case "custom":
- Logger.debug(chunk.type, { chunk });
- break;
- default:
- if (isDataChunk(chunk)) {
- Logger.debug("Data chunk", { chunk });
- break;
- }
- logNever(chunk);
+ const beginStagedCellGeneration = () => {
+ const activeGeneration = store.get(stagedGenerationAtom);
+ if (activeGeneration?.status === "in_progress") {
+ discardStagedCellIds(activeGeneration.cellIds);
+ }
+ ownedGeneration.current?.reconciler.discard(store.get(stagedAICellsAtom));
+ const generationId = Symbol("staged-cell-generation");
+ ownedGeneration.current = {
+ id: generationId,
+ status: "in_progress",
+ reconciler: new StagedCellReconciler({
+ ...stagedCellActions,
+ createNewCell,
+ }),
+ };
+ store.set(stagedGenerationAtom, {
+ id: generationId,
+ status: "in_progress",
+ cellIds: [],
+ });
+ };
+
+ const finishStagedCellGeneration = (successful: boolean) => {
+ const generation = ownedGeneration.current;
+ if (generation?.status !== "in_progress") {
+ return;
+ }
+
+ const stagedGeneration = store.get(stagedGenerationAtom);
+ const ownsActiveGeneration =
+ stagedGeneration?.id === generation.id &&
+ stagedGeneration.status === "in_progress";
+ if (!ownsActiveGeneration || !successful) {
+ generation.reconciler.discard(store.get(stagedAICellsAtom));
+ if (ownsActiveGeneration) {
+ store.set(stagedGenerationAtom, null);
+ }
+ ownedGeneration.current = null;
+ return;
+ }
+
+ generation.status = "complete";
+ store.set(stagedGenerationAtom, {
+ id: generation.id,
+ status: "complete",
+ });
+ };
+
+ const hasOwnedStagedCells = () => {
+ const generation = ownedGeneration.current;
+ const stagedGeneration = store.get(stagedGenerationAtom);
+ return (
+ generation !== null &&
+ generation.status === "complete" &&
+ stagedGeneration?.status === "complete" &&
+ stagedGeneration.id === generation.id &&
+ generation.reconciler.hasStagedCells(store.get(stagedAICellsAtom))
+ );
+ };
+
+ const acceptOwnedStagedCells = () => {
+ if (!hasOwnedStagedCells()) {
+ return false;
+ }
+
+ ownedGeneration.current?.reconciler.accept();
+ store.set(stagedGenerationAtom, null);
+ ownedGeneration.current = null;
+ return true;
+ };
+
+ const discardOwnedStagedCells = () => {
+ const generation = ownedGeneration.current;
+ const stagedGeneration = store.get(stagedGenerationAtom);
+ if (generation === null || stagedGeneration?.id !== generation.id) {
+ return false;
}
+
+ generation.reconciler.discard(store.get(stagedAICellsAtom));
+ store.set(stagedGenerationAtom, null);
+ ownedGeneration.current = null;
+ return true;
+ };
+
+ const onData = (part: CompletionDataPart) => {
+ if (part.type !== NOTEBOOK_CELLS_COMPLETION_DATA_TYPE) {
+ return;
+ }
+ const generation = ownedGeneration.current;
+ const stagedGeneration = store.get(stagedGenerationAtom);
+ if (
+ generation?.status !== "in_progress" ||
+ stagedGeneration?.id !== generation.id ||
+ stagedGeneration.status !== "in_progress"
+ ) {
+ return;
+ }
+ const completion = notebookCellsCompletionSchema.parse(part.data);
+ generation.reconciler.reconcile(completion.cells);
+ store.set(stagedGenerationAtom, {
+ ...stagedGeneration,
+ cellIds: generation.reconciler.stagedCellIds(),
+ });
};
return {
- createStagedCell,
- updateStagedCell,
- addStagedCell,
- removeStagedCell,
- clearStagedCells,
- deleteStagedCell,
- deleteAllStagedCells,
- onStream,
+ beginStagedCellGeneration,
+ finishStagedCellGeneration,
+ hasOwnedStagedCells,
+ acceptOwnedStagedCells,
+ discardOwnedStagedCells,
+ onData,
};
}
export { stagedAICellsAtom };
export const visibleForTesting = {
+ stagedGenerationAtom,
createActions,
reducer,
initialState,
- useStagedAICellsActions,
};
-type TextDeltaChunk = Extract<UIMessageChunk, { type: "text-delta" }>;
-
interface CreatedCell {
cellId: CellId;
- cell: AiCompletion;
+ code: string;
}
-class CellCreationStream {
+interface StagedCellReconcilerActions {
+ createStagedCell: (code: string) => CellId;
+ updateStagedCell: (opts: UpdateStagedCellAction) => void;
+ deleteStagedCell: (cellId: CellId) => void;
+ removeStagedCell: (cellId: CellId) => void;
+ addStagedCell: (payload: { cellId: CellId; edit: Edit }) => void;
+ createNewCell: (opts: CreateNewCellAction) => void;
+}
+
+class StagedCellReconciler {
private createdCells: CreatedCell[] = [];
- private buffer = "";
-
- private onCreateCell: (code: string) => CellId;
- private onUpdateCell: (opts: UpdateStagedCellAction) => void;
- private addStagedCell: (payload: { cellId: CellId; edit: Edit }) => void;
- private createNewCell: (opts: CreateNewCellAction) => void;
- private hasMarimoImport = false;
-
- constructor(
- onCreateCell: (code: string) => CellId,
- onUpdateCell: (opts: UpdateStagedCellAction) => void,
- addStagedCell: (payload: { cellId: CellId; edit: Edit }) => void,
- createNewCell: (opts: CreateNewCellAction) => void,
- ) {
- this.onCreateCell = onCreateCell;
- this.onUpdateCell = onUpdateCell;
- this.addStagedCell = addStagedCell;
- this.createNewCell = createNewCell;
- }
+ private readonly actions: StagedCellReconcilerActions;
+ private marimoImportCellId: CellId | null = null;
- stream(chunk: TextDeltaChunk) {
- const delta = chunk.delta;
- this.buffer += delta;
- const completionCells = codeToCells(this.buffer);
+ constructor(actions: StagedCellReconcilerActions) {
+ this.actions = actions;
+ }
- // 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.
+ reconcile(completionCells: GeneratedCell[]) {
for (const [idx, cell] of completionCells.entries()) {
if (idx < this.createdCells.length) {
- this.addMarimoImport(cell.language);
const existingCell = this.createdCells[idx];
- this.createdCells[idx] = { ...existingCell, cell };
- this.onUpdateCell({
+ const codeChanged = existingCell.code !== cell.code;
+ this.createdCells[idx] = { ...existingCell, code: cell.code };
+ if (!codeChanged) {
+ continue;
+ }
+ this.actions.updateStagedCell({
cellId: existingCell.cellId,
code: cell.code,
- language: cell.language,
});
} else {
- const newCellId = this.onCreateCell(cell.code);
- this.createdCells.push({ cellId: newCellId, cell });
+ const newCellId = this.actions.createStagedCell(cell.code);
+ this.createdCells.push({ cellId: newCellId, code: cell.code });
}
}
+
+ const removedCells = this.createdCells.splice(completionCells.length);
+ for (const { cellId } of removedCells.toReversed()) {
+ this.actions.deleteStagedCell(cellId);
+ }
+
+ this.syncMarimoImport(completionCells);
}
- /** Add a marimo import if the cell is SQL or Markdown and we haven't added it yet. */
- private addMarimoImport(language: LanguageAdapterType) {
- if (this.hasMarimoImport || language === "python") {
+ discard(stagedCells: StagedAICells) {
+ for (const { cellId } of this.createdCells.toReversed()) {
+ if (stagedCells.has(cellId)) {
+ this.actions.deleteStagedCell(cellId);
+ }
+ }
+ this.createdCells = [];
+
+ if (this.marimoImportCellId && stagedCells.has(this.marimoImportCellId)) {
+ this.actions.deleteStagedCell(this.marimoImportCellId);
+ }
+ this.marimoImportCellId = null;
+ }
+
+ accept() {
+ for (const cellId of this.stagedCellIds()) {
+ this.actions.removeStagedCell(cellId);
+ }
+ }
+
+ hasStagedCells(stagedCells: StagedAICells) {
+ return this.stagedCellIds().some((cellId) => stagedCells.has(cellId));
+ }
+
+ stagedCellIds() {
+ const cellIds = this.createdCells.map(({ cellId }) => cellId);
+ if (this.marimoImportCellId) {
+ cellIds.push(this.marimoImportCellId);
+ }
+ return cellIds;
+ }
+
+ /** Keep the generated marimo import consistent with the latest snapshot. */
+ private syncMarimoImport(completionCells: GeneratedCell[]) {
+ const requiresMarimo = completionCells.some(
+ (cell) => cell.language !== "python",
+ );
+ if (!requiresMarimo) {
+ if (this.marimoImportCellId) {
+ this.actions.deleteStagedCell(this.marimoImportCellId);
+ this.marimoImportCellId = null;
+ }
+ return;
+ }
+ if (this.marimoImportCellId) {
return;
}
const cellId = maybeAddMarimoImport({
autoInstantiate: false,
- createNewCell: this.createNewCell,
+ createNewCell: this.actions.createNewCell,
fromCellId: this.createdCells[0]?.cellId,
before: true,
});
if (cellId) {
- this.addStagedCell({ cellId, edit: { type: "add_cell" } });
+ this.actions.addStagedCell({ cellId, edit: { type: "add_cell" } });
+ this.marimoImportCellId = cellId;
}
- this.hasMarimoImport = true;
}
-
- stop() {
- // Clear all state
- this.buffer = "";
- }
-}
-
-type DataChunk = Extract<UIMessageChunk, { type: `data-${string}` }>;
-
-function isDataChunk(chunk: UIMessageChunk): chunk is DataChunk {
- return chunk.type.startsWith("data-");
}
diff --git a/frontend/src/core/ai/stream-completion-text.ts b/frontend/src/core/ai/stream-completion-text.ts
index 0c5744b8cd..087d99485c 100644
--- a/frontend/src/core/ai/stream-completion-text.ts
+++ b/frontend/src/core/ai/stream-completion-text.ts
@@ -1,10 +1,14 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { consumeStream, parseJsonEventStream, uiMessageChunkSchema } from "ai";
-import { stripWrappingBackticks } from "./strip-wrapping-backticks";
+import {
+ CELL_COMPLETION_DATA_TYPE,
+ cellCompletionSchema,
+ isDataChunk,
+} from "./completion-output";
/**
- * Read an AI SDK UI message stream response and return the assistant text.
+ * Read an AI SDK UI message stream response and return validated cell code.
*/
export async function streamCompletionText(
response: Response,
@@ -17,7 +21,8 @@ export async function streamCompletionText(
throw new Error("Failed to get response body");
}
- let result = "";
+ let result: string | null = null;
+ let finishedSuccessfully = false;
await consumeStream({
stream: parseJsonEventStream({
@@ -31,10 +36,15 @@ export async function streamCompletionText(
}
const streamPart = part.value;
- if (streamPart.type === "text-delta") {
- result += streamPart.delta;
+ if (
+ isDataChunk(streamPart) &&
+ streamPart.type === CELL_COMPLETION_DATA_TYPE
+ ) {
+ result = cellCompletionSchema.parse(streamPart.data).code;
} else if (streamPart.type === "error") {
throw new Error(streamPart.errorText);
+ } else if (streamPart.type === "finish") {
+ finishedSuccessfully = streamPart.finishReason === "stop";
}
},
}),
@@ -44,5 +54,12 @@ export async function streamCompletionText(
},
});
- return stripWrappingBackticks(result);
+ if (!finishedSuccessfully) {
+ throw new Error("AI completion ended before final validation");
+ }
+ if (result === null) {
+ throw new Error("AI completion returned no cell code");
+ }
+
+ return result;
}
diff --git a/frontend/src/core/ai/strip-wrapping-backticks.ts b/frontend/src/core/ai/strip-wrapping-backticks.ts
deleted file mode 100644
index f05b8e5305..0000000000
--- a/frontend/src/core/ai/strip-wrapping-backticks.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-/* Copyright 2026 Marimo. All rights reserved. */
-
-const LANGS = ["python", "sql", "markdown"] as const;
-
-interface StripOptions {
- /**
- * When true, the text is treated as a partial stream:
- * - the opening fence is only stripped once we can be sure of its language
- * identifier (e.g. "```py" is left untouched since it may still become
- * "```python"), and
- * - the closing fence is left in place, since it may not have arrived yet (or
- * a trailing "```" may be part of the content).
- */
- streaming?: boolean;
-}
-
-/**
- * Removes wrapping markdown code fences from a completion string.
- */
-export function stripWrappingBackticks(
- text: string,
- opts: StripOptions = {},
-): string {
- const { streaming = false } = opts;
- const leadingWhitespace = text.match(/^\s*/)?.[0] ?? "";
- const rest = text.slice(leadingWhitespace.length);
-
- let body: string | null = null;
- let strippedOpening = false;
-
- for (const lang of LANGS) {
- if (rest.startsWith(`\`\`\`${lang}`)) {
- strippedOpening = true;
- body = rest.slice(3 + lang.length);
- if (body.startsWith("\n")) {
- body = body.slice(1);
- }
- break;
- }
- }
-
- if (!strippedOpening && rest.startsWith("```")) {
- const afterFence = rest.slice(3);
- if (streaming && isPartialLanguageFence(afterFence)) {
- return text;
- }
- strippedOpening = true;
- body = afterFence;
- if (body.startsWith("\n")) {
- body = body.slice(1);
- }
- }
-
- if (!strippedOpening || body === null) {
- return text;
- }
-
- // While streaming, the closing fence may not have arrived yet, so leave the
- // body as-is once the opening fence is removed.
- if (streaming) {
- return body;
- }
-
- const strippedEnd = body.trimEnd();
- const trailingSpace = body.slice(strippedEnd.length);
-
- if (strippedEnd.endsWith("\n```")) {
- return strippedEnd.slice(0, -4) + trailingSpace;
- }
- if (strippedEnd.endsWith("```")) {
- return strippedEnd.slice(0, -3) + trailingSpace;
- }
-
- return body;
-}
-
-/**
- * Returns true if the text after an opening "```" has no terminating newline
- * yet and could still become a known language identifier.
- */
-function isPartialLanguageFence(afterFence: string): boolean {
- // Once the first line is terminated, we already know it is not a known
- // language fence (those are handled above), so treat it as a plain fence.
- if (afterFence.includes("\n")) {
- return false;
- }
- return LANGS.some((lang) => lang.startsWith(afterFence));
-}
diff --git a/marimo/_server/ai/completion_output.py b/marimo/_server/ai/completion_output.py
new file mode 100644
index 0000000000..a351e9525e
--- /dev/null
+++ b/marimo/_server/ai/completion_output.py
@@ -0,0 +1,32 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from pydantic import BaseModel, Field
+
+from marimo._server.models.completion import Language
+
+# Mirrored by frontend/src/core/ai/completion-output.ts.
+CELL_COMPLETION_DATA_TYPE = "data-cell-completion"
+NOTEBOOK_CELLS_COMPLETION_DATA_TYPE = "data-notebook-cells-completion"
+
+
+class CellCompletion(BaseModel):
+ """A replacement for the code in one notebook cell."""
+
+ code: str = Field(description="Raw cell code without Markdown fences.")
+
+
+class GeneratedCell(BaseModel):
+ """A notebook cell generated from the user's request."""
+
+ language: Language
+ code: str = Field(
+ min_length=1,
+ description="Raw cell code without Markdown fences.",
+ )
+
+
+class NotebookCellsCompletion(BaseModel):
+ """The ordered notebook cells generated from the user's request."""
+
+ cells: list[GeneratedCell] = Field(min_length=1)
diff --git a/marimo/_server/ai/prompts.py b/marimo/_server/ai/prompts.py
index eb66472ad1..582858f9db 100644
--- a/marimo/_server/ai/prompts.py
+++ b/marimo/_server/ai/prompts.py
@@ -121,14 +121,11 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
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"
+ "You can create multiple cells with different languages.\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"
+ "Each cell's `code` value must contain only raw cell code, without Markdown fences or commentary.\n"
+ "For SQL cells, the code should be a complete mo.sql expression. For markdown cells, the code should be a complete mo.md expression.\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."
)
@@ -139,8 +136,7 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
f"Your output must be valid {language} code.\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"
- "```\n{CELL_CODE}\n```"
+ "The `code` value must contain only raw cell code, without Markdown fences or commentary."
)
# When we are modifying or inserting into an existing cell, we need to
@@ -167,8 +163,7 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
"<insert_here></insert_here> tags. Don't include the insert_here tags in your output.\n"
"Match the indentation in the original file in the inserted content, "
"don't include any indentation on blank lines.\n"
- "Immediately start with the following format. Do NOT comment on the code, just output the code itself:\n\n"
- "```\n{INSERTED_CODE}\n```"
+ "The `code` value must contain only the code to insert, without Markdown fences or commentary."
)
else:
system_prompt += (
@@ -177,8 +172,7 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
"Start at the indentation level in the original file in the rewritten content. "
"Don't stop until you've rewritten the entire section, even if you have no more changes to make, "
"always write out the whole section with no unnecessary elisions.\n"
- "Immediately start with the following format. Do NOT comment on the code, just output the code itself:\n\n"
- "```\n{REWRITTEN_CODE}\n```"
+ "The `code` value must contain the complete rewritten section, without Markdown fences, commentary, or elisions."
)
if selected_text:
@@ -211,11 +205,6 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
"code_from_other_cells", other_cell_codes
)
- 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 += f"\n\nAgain, just output the code itself and make sure to return the code as just {language}."
-
return system_prompt
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.py
index 8db39e40a4..ec7bff949a 100644
--- a/marimo/_server/ai/providers.py
+++ b/marimo/_server/ai/providers.py
@@ -41,15 +41,18 @@ from marimo._server.ai.tracing import (
trace_stream,
)
from marimo._server.models.completion import UIMessage as ServerUIMessage
+from marimo._utils.assert_never import log_never
from marimo._utils.http import HTTPStatus
from marimo._utils.typing import override
if TYPE_CHECKING:
- from collections.abc import Sequence
+ from collections.abc import AsyncIterator, Sequence
from openai import AsyncOpenAI
+ from pydantic import BaseModel
from pydantic_ai import Agent, DeferredToolRequests, FunctionToolset
from pydantic_ai.capabilities import AbstractCapability
+ from pydantic_ai.messages import FinishReason as PydanticFinishReason
from pydantic_ai.models import Model
from pydantic_ai.models.anthropic import AnthropicModelSettings
from pydantic_ai.models.bedrock import (
@@ -77,6 +80,9 @@ if TYPE_CHECKING:
from pydantic_ai.settings import ModelSettings, ThinkingLevel
from pydantic_ai.toolsets import AbstractToolset
from pydantic_ai.ui.vercel_ai.request_types import UIMessage, UIMessagePart
+ from pydantic_ai.ui.vercel_ai.response_types import (
+ FinishReason as VercelFinishReason,
+ )
from starlette.requests import Request
from starlette.responses import StreamingResponse
@@ -103,6 +109,25 @@ class BaseModelSettings:
ProviderT = TypeVar("ProviderT", bound="Provider", covariant=True)
+def _structured_completion_finish_reason(
+ finish_reason: PydanticFinishReason | None,
+) -> VercelFinishReason:
+ match finish_reason:
+ case "stop" | "length" | "error":
+ return finish_reason
+ case None:
+ return "stop"
+ case "content_filter":
+ return "content-filter"
+ case "tool_call":
+ # A structured-output tool call is the successful final result.
+ return "stop"
+ case _:
+ # Preserve the stream if Pydantic AI adds a new finish reason.
+ log_never(finish_reason)
+ return "other"
+
+
class PydanticProvider(ABC, Generic[ProviderT]):
def __init__(
self,
@@ -174,6 +199,116 @@ class PydanticProvider(ABC, Generic[ProviderT]):
deps_type=type(None),
)
+ def _create_structured_completion_agent(
+ self,
+ *,
+ name: str,
+ max_tokens: int | None,
+ system_prompt: str,
+ output_type: type[BaseModel],
+ ) -> Agent[None, BaseModel]:
+ """Create a completion agent with model-adaptive structured output."""
+ from pydantic_ai import Agent
+
+ model = self.create_model()
+
+ return Agent(
+ model,
+ name=name,
+ model_settings=self._build_model_settings(model, max_tokens),
+ instructions=system_prompt,
+ output_type=output_type,
+ deps_type=type(None),
+ )
+
+ async def stream_structured_completion(
+ self,
+ messages: list[ServerUIMessage],
+ system_prompt: str,
+ max_tokens: int | None,
+ output_type: type[BaseModel],
+ data_type: str,
+ stream_options: StreamOptions,
+ ) -> StreamingResponse:
+ """Stream partially validated completion snapshots as data events."""
+ from pydantic_ai.ui.vercel_ai import VercelAIAdapter
+ from pydantic_ai.ui.vercel_ai.request_types import SubmitMessage
+ from pydantic_ai.ui.vercel_ai.response_types import (
+ BaseChunk,
+ DataChunk,
+ DoneChunk,
+ ErrorChunk,
+ FinishChunk,
+ FinishStepChunk,
+ StartChunk,
+ StartStepChunk,
+ )
+
+ agent = self._create_structured_completion_agent(
+ name=stream_options.span_info.endpoint,
+ max_tokens=max_tokens,
+ system_prompt=system_prompt,
+ output_type=output_type,
+ )
+ # Completion is pure generation. Chat and agent endpoints own tool use.
+ stream_options.span_info.tool_count = 0
+
+ run_input = SubmitMessage(
+ id=generate_id("submit-message"),
+ trigger="submit-message",
+ messages=self.convert_messages(messages),
+ )
+ adapter = VercelAIAdapter(
+ agent=agent,
+ run_input=run_input,
+ accept=stream_options.accept,
+ sdk_version=AI_SDK_VERSION,
+ )
+
+ async def completion_events() -> AsyncIterator[BaseChunk]:
+ yield StartChunk()
+ yield StartStepChunk()
+
+ message_history = adapter.sanitize_messages(adapter.messages)
+ last_data: dict[str, object] | None = None
+ async with agent.run_stream(
+ message_history=message_history
+ ) as result:
+ async for output in result.stream_output():
+ data = output.model_dump(mode="json")
+ if data == last_data:
+ continue
+ last_data = data
+ yield DataChunk(
+ type=data_type,
+ data=data,
+ transient=True,
+ )
+
+ yield FinishStepChunk()
+ yield FinishChunk(
+ finish_reason=_structured_completion_finish_reason(
+ result.response.finish_reason
+ )
+ )
+ yield DoneChunk()
+
+ async def safe_completion_events() -> AsyncIterator[BaseChunk]:
+ try:
+ async for event in trace_stream(
+ completion_events(), stream_options.span_info
+ ):
+ yield event
+ except Exception as error:
+ LOGGER.exception("Structured completion failed")
+ yield ErrorChunk(error_text=str(error))
+ yield FinishStepChunk()
+ yield FinishChunk(finish_reason="error")
+ yield DoneChunk()
+
+ event_stream = safe_completion_events()
+ return adapter.streaming_response(event_stream)
+
def _build_model_settings(
self, model: Model, max_tokens: int | None
) -> ModelSettings | None:
diff --git a/marimo/_server/api/endpoints/ai.py b/marimo/_server/api/endpoints/ai.py
index 6636df929e..d3241936ed 100644
--- a/marimo/_server/api/endpoints/ai.py
+++ b/marimo/_server/api/endpoints/ai.py
@@ -158,12 +158,29 @@ async def ai_completion(
model=model,
)
- return await provider.stream_completion(
+ # These models require the optional Pydantic AI dependency checked above.
+ from marimo._server.ai.completion_output import (
+ CELL_COMPLETION_DATA_TYPE,
+ NOTEBOOK_CELLS_COMPLETION_DATA_TYPE,
+ CellCompletion,
+ NotebookCellsCompletion,
+ )
+
+ output_type = (
+ NotebookCellsCompletion if support_multiple_cells else CellCompletion
+ )
+ data_type = (
+ NOTEBOOK_CELLS_COMPLETION_DATA_TYPE
+ if support_multiple_cells
+ else CELL_COMPLETION_DATA_TYPE
+ )
+
+ return await provider.stream_structured_completion(
messages=messages,
system_prompt=system_prompt,
max_tokens=get_max_tokens(config),
- additional_tools=[],
- enable_capabilities=False,
+ output_type=output_type,
+ data_type=data_type,
stream_options=StreamOptions(
span_info=SpanInfo(
endpoint="completion",
diff --git a/tests/_server/ai/snapshots/system_prompts.txt b/tests/_server/ai/snapshots/system_prompts.txt
index 7bab6bbf63..1de0069f1e 100644
--- a/tests/_server/ai/snapshots/system_prompts.txt
+++ b/tests/_server/ai/snapshots/system_prompts.txt
@@ -7,11 +7,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
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:
-
-```
-{CELL_CODE}
-```
+The `code` value must contain only raw cell code, without Markdown fences or commentary.
## Rules for python
1. For matplotlib: use plt.gca() as the last expression instead of plt.show().
@@ -22,8 +18,6 @@ Immediately start with the following format. Do NOT comment on the code, just ou
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.
-Again, just output the code itself and make sure to return the code as just python.
-
==================== markdown ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -31,19 +25,13 @@ You goal is to create a new cell in the notebook.
Your output must be valid markdown code.
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:
-
-```
-{CELL_CODE}
-```
+The `code` value must contain only raw cell code, without Markdown fences or commentary.
## Rules for markdown
1. Use double dollar signs ($$) for ALL mathematical expressions (both inline and block) in markdown responses.
2. Example: $$E=mc^2$$
3. Do NOT use single dollar signs or square brackets for math.
-Again, just output the code itself and make sure to return the code as just markdown.
-
==================== sql ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -51,17 +39,11 @@ You goal is to create a new cell in the notebook.
Your output must be valid sql code.
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:
-
-```
-{CELL_CODE}
-```
+The `code` value must contain only raw cell code, without Markdown fences or commentary.
## Rules for sql
1. The SQL must use duckdb syntax.
-Again, just output the code itself and make sure to return the code as just sql.
-
==================== idk ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -69,13 +51,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid idk code.
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:
-
-```
-{CELL_CODE}
-```
-
-Again, just output the code itself and make sure to return the code as just idk.
+The `code` value must contain only raw cell code, without Markdown fences or commentary.
==================== with custom rules ====================
@@ -84,11 +60,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
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:
-
-```
-{CELL_CODE}
-```
+The `code` value must contain only raw cell code, without Markdown fences or commentary.
## Rules for python
1. For matplotlib: use plt.gca() as the last expression instead of plt.show().
@@ -102,8 +74,6 @@ Immediately start with the following format. Do NOT comment on the code, just ou
## Additional rules:
Always use type hints.
-Again, just output the code itself and make sure to return the code as just python.
-
==================== with context ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -111,11 +81,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
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:
-
-```
-{CELL_CODE}
-```
+The `code` value must contain only raw cell code, without Markdown fences or commentary.
## Rules for python
1. For matplotlib: use plt.gca() as the last expression instead of plt.show().
@@ -136,8 +102,6 @@ Immediately start with the following format. Do NOT comment on the code, just ou
- Sample values: Alice, Bob, Charlie
-Again, just output the code itself and make sure to return the code as just python.
-
==================== with is_insert=True ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -152,11 +116,7 @@ def fib(n):
You can't replace the content, your answer will be inserted in place of the <insert_here></insert_here> tags. Don't include the insert_here tags in your output.
Match the indentation in the original file in the inserted content, don't include any indentation on blank lines.
-Immediately start with the following format. Do NOT comment on the code, just output the code itself:
-
-```
-{INSERTED_CODE}
-```
+The `code` value must contain only the code to insert, without Markdown fences or commentary.
## Rules for python
1. For matplotlib: use plt.gca() as the last expression instead of plt.show().
@@ -167,8 +127,6 @@ Immediately start with the following format. Do NOT comment on the code, just ou
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.
-Again, just output the code itself and make sure to return the code as just python.
-
==================== with cell_code ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -183,11 +141,7 @@ def hello():
Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding content will be preserved.
Start at the indentation level in the original file in the rewritten content. Don't stop until you've rewritten the entire section, even if you have no more changes to make, always write out the whole section with no unnecessary elisions.
-Immediately start with the following format. Do NOT comment on the code, just output the code itself:
-
-```
-{REWRITTEN_CODE}
-```
+The `code` value must contain the complete rewritten section, without Markdown fences, commentary, or elisions.
And here's the section to rewrite based on that prompt again for reference:
@@ -204,8 +158,6 @@ print('Hello, world!')
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.
-Again, just output the code itself and make sure to return the code as just python.
-
==================== with selected_text ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -220,11 +172,7 @@ def hello():
Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding content will be preserved.
Start at the indentation level in the original file in the rewritten content. Don't stop until you've rewritten the entire section, even if you have no more changes to make, always write out the whole section with no unnecessary elisions.
-Immediately start with the following format. Do NOT comment on the code, just output the code itself:
-
-```
-{REWRITTEN_CODE}
-```
+The `code` value must contain the complete rewritten section, without Markdown fences, commentary, or elisions.
And here's the section to rewrite based on that prompt again for reference:
@@ -241,8 +189,6 @@ print('Hello, world!')
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.
-Again, just output the code itself and make sure to return the code as just python.
-
==================== with other_cell_codes ====================
Here's a python document from a Python notebook that I'm going to ask you to make an edit to.
@@ -256,11 +202,7 @@ The section you'll need to rewrite is marked with <rewrite_this></rewrite_this>
Only make changes that are necessary to fulfill the prompt, leave everything else as-is. All surrounding content will be preserved.
Start at the indentation level in the original file in the rewritten content. Don't stop until you've rewritten the entire section, even if you have no more changes to make, always write out the whole section with no unnecessary elisions.
-Immediately start with the following format. Do NOT comment on the code, just output the code itself:
-
-```
-{REWRITTEN_CODE}
-```
+The `code` value must contain the complete rewritten section, without Markdown fences, commentary, or elisions.
And here's the section to rewrite based on that prompt again for reference:
@@ -282,8 +224,6 @@ import pandas as pd
import numpy as np
</code_from_other_cells>
-Again, just output the code itself and make sure to return the code as just python.
-
==================== with VariableContext objects ====================
You are an AI assistant integrated into the marimo notebook code editor.
@@ -291,11 +231,7 @@ You goal is to create a new cell in the notebook.
Your output must be valid python code.
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:
-
-```
-{CELL_CODE}
-```
+The `code` value must contain only raw cell code, without Markdown fences or commentary.
## Rules for python
1. For matplotlib: use plt.gca() as the last expression instead of plt.show().
@@ -315,30 +251,15 @@ Immediately start with the following format. Do NOT comment on the code, just ou
- value_preview: <Model object>
-Again, just output the code itself and make sure to return the code as just python.
-
==================== 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.
+You can create multiple cells with different languages.
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.
+Each cell's `code` value must contain only raw cell code, without Markdown fences or commentary.
+For SQL cells, the code should be a complete mo.sql expression. For markdown cells, the code should be a complete mo.md expression.
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.
@@ -368,6 +289,3 @@ Separate logic into multiple cells to keep the code organized and readable.
- 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_providers.py b/tests/_server/ai/test_providers.py
index 2266a9906f..1cd0b029cc 100644
--- a/tests/_server/ai/test_providers.py
+++ b/tests/_server/ai/test_providers.py
@@ -1,14 +1,23 @@
"""Tests for the LLM providers in marimo._server.ai.providers."""
+import asyncio
import os
+from collections.abc import AsyncIterator
from pathlib import Path
from typing import Any, cast
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
+from pydantic import ValidationError
from marimo._config.config import AiConfig
from marimo._dependencies.dependencies import Dependency, DependencyManager
+from marimo._server.ai.completion_output import (
+ CELL_COMPLETION_DATA_TYPE,
+ NOTEBOOK_CELLS_COMPLETION_DATA_TYPE,
+ CellCompletion,
+ NotebookCellsCompletion,
+)
from marimo._server.ai.config import AnyProviderConfig
from marimo._server.ai.ids import AiModelId
from marimo._server.ai.providers import (
@@ -22,11 +31,278 @@ from marimo._server.ai.providers import (
StreamOptions,
_infer_provider_name_from_base_url,
_normalize_base_url,
+ _structured_completion_finish_reason,
get_completion_provider,
)
+from marimo._server.ai.tools.types import ToolDefinition
from marimo._server.ai.tracing import SpanInfo
+ ("pydantic_reason", "vercel_reason"),
+ [
+ ("stop", "stop"),
+ ("length", "length"),
+ ("content_filter", "content-filter"),
+ ("tool_call", "stop"),
+ ("error", "error"),
+ (None, "stop"),
+ ],
+)
+def test_structured_completion_finish_reason(
+ pydantic_reason: Any, vercel_reason: Any
+) -> None:
+ assert (
+ _structured_completion_finish_reason(pydantic_reason) == vercel_reason
+ )
+
+
+def test_structured_completion_unknown_finish_reason() -> None:
+ with patch("marimo._server.ai.providers.log_never") as log_never:
+ assert (
+ _structured_completion_finish_reason(cast(Any, "future_reason"))
+ == "other"
+ )
+
+ log_never.assert_called_once_with("future_reason")
+
+
+ "cells",
+ [
+ pytest.param([], id="no-cells"),
+ pytest.param([{"language": "python", "code": ""}], id="empty-code"),
+ ],
+)
+def test_notebook_cells_completion_requires_content(
+ cells: list[dict[str, str]],
+) -> None:
+ with pytest.raises(ValidationError):
+ NotebookCellsCompletion.model_validate({"cells": cells})
+
+
[email protected]("pydantic_ai")
+ "test_model_kwargs",
+ [
+ pytest.param(
+ {"custom_output_args": {"code": "print('```')"}},
+ id="tool-output",
+ ),
+ pytest.param(
+ {
+ "custom_output_text": '{"code":"print(\'```\')"}',
+ "profile": {
+ "default_structured_output_mode": "native",
+ "supports_json_schema_output": True,
+ },
+ },
+ id="native-output",
+ ),
+ pytest.param(
+ {
+ "custom_output_text": '{"code":"print(\'```\')"}',
+ "profile": {"default_structured_output_mode": "prompted"},
+ },
+ id="prompted-output",
+ ),
+ ],
+)
+async def test_stream_structured_completion_emits_validated_data(
+ test_model_kwargs: dict[str, Any],
+) -> None:
+ from pydantic_ai.models.test import TestModel
+ from pydantic_ai.profiles import ModelProfile
+
+ if profile := test_model_kwargs.get("profile"):
+ test_model_kwargs = {
+ **test_model_kwargs,
+ "profile": ModelProfile(**profile),
+ }
+
+ config = AnyProviderConfig(api_key="test-key", base_url="http://test-url")
+ provider = OpenAIProvider("gpt-4", config)
+ stream_options = StreamOptions(
+ span_info=SpanInfo(endpoint="completion", model="openai/gpt-4")
+ )
+
+ with patch.object(
+ provider,
+ "create_model",
+ return_value=TestModel(**test_model_kwargs),
+ ):
+ response = await provider.stream_structured_completion(
+ messages=[
+ {
+ "id": "user-message",
+ "role": "user",
+ "parts": [{"type": "text", "text": "write code"}],
+ }
+ ],
+ system_prompt="Return cell code.",
+ max_tokens=100,
+ output_type=CellCompletion,
+ data_type=CELL_COMPLETION_DATA_TYPE,
+ stream_options=stream_options,
+ )
+
+ chunks = [chunk async for chunk in response.body_iterator]
+ body = "".join(
+ chunk.decode() if isinstance(chunk, bytes) else chunk
+ for chunk in chunks
+ )
+ assert '"type":"data-cell-completion"' in body
+ assert "print('```')" in body
+ assert '"transient":true' in body
+
+
[email protected]("pydantic_ai")
+async def test_stream_structured_completion_emits_partial_snapshots() -> None:
+ from pydantic_ai.models.function import (
+ AgentInfo,
+ DeltaToolCall,
+ DeltaToolCalls,
+ FunctionModel,
+ )
+
+ async def respond(
+ messages: list[Any], info: AgentInfo
+ ) -> AsyncIterator[DeltaToolCalls]:
+ del messages
+ assert info.function_tools == []
+ yield {
+ 0: DeltaToolCall(
+ name="final_result",
+ json_args=('{"cells":[{"language":"python","code":"a'),
+ tool_call_id="completion",
+ )
+ }
+ await asyncio.sleep(0.11)
+ yield {
+ 0: DeltaToolCall(
+ json_args=(' = 1"},{"language":"python","code":"b')
+ )
+ }
+ await asyncio.sleep(0.11)
+ yield {0: DeltaToolCall(json_args=' = a + 1"}]}')}
+
+ from pydantic_ai.profiles import ModelProfile
+
+ model = FunctionModel(
+ stream_function=respond,
+ profile=ModelProfile(default_structured_output_mode="tool"),
+ )
+ config = AnyProviderConfig(
+ api_key="test-key",
+ base_url="http://test-url",
+ tools=[
+ ToolDefinition(
+ name="configured_tool",
+ description="Must not be exposed to completion generation.",
+ parameters={"type": "object"},
+ source="backend",
+ mode=["ask"],
+ )
+ ],
+ )
+ provider = OpenAIProvider("gpt-4", config)
+
+ with patch.object(provider, "create_model", return_value=model):
+ response = await provider.stream_structured_completion(
+ messages=[
+ {
+ "id": "user-message",
+ "role": "user",
+ "parts": [{"type": "text", "text": "write code"}],
+ }
+ ],
+ system_prompt="Return cells.",
+ max_tokens=100,
+ output_type=NotebookCellsCompletion,
+ data_type=NOTEBOOK_CELLS_COMPLETION_DATA_TYPE,
+ stream_options=StreamOptions(
+ span_info=SpanInfo(endpoint="completion", model="openai/gpt-4")
+ ),
+ )
+
+ chunks = [chunk async for chunk in response.body_iterator]
+ body = "".join(
+ chunk.decode() if isinstance(chunk, bytes) else chunk
+ for chunk in chunks
+ )
+ assert body.count('"type":"data-notebook-cells-completion"') == 3
+ assert body.index('"code":"a"') < body.index('"code":"a = 1"')
+ assert body.index('"code":"b"') < body.index('"code":"b = a + 1"')
+ assert '"finishReason":"stop"' in body
+
+
[email protected]("pydantic_ai")
+async def test_stream_structured_completion_reports_final_validation_error() -> (
+ None
+):
+ from pydantic_ai.models.function import (
+ AgentInfo,
+ DeltaToolCall,
+ DeltaToolCalls,
+ FunctionModel,
+ )
+
+ async def respond(
+ messages: list[Any], info: AgentInfo
+ ) -> AsyncIterator[DeltaToolCalls]:
+ del messages, info
+ yield {
+ 0: DeltaToolCall(
+ name="final_result",
+ json_args=(
+ '{"cells":[{"language":"python","code":"partial"}]}'
+ ),
+ tool_call_id="completion",
+ )
+ }
+ yield {0: DeltaToolCall(json_args=" invalid")}
+
+ from pydantic_ai.profiles import ModelProfile
+
+ model = FunctionModel(
+ stream_function=respond,
+ profile=ModelProfile(default_structured_output_mode="tool"),
+ )
+ config = AnyProviderConfig(api_key="test-key", base_url="http://test-url")
+ provider = OpenAIProvider("gpt-4", config)
+
+ with (
+ patch.object(provider, "create_model", return_value=model),
+ patch("marimo._server.ai.providers.LOGGER.exception") as log_exception,
+ ):
+ response = await provider.stream_structured_completion(
+ messages=[
+ {
+ "id": "user-message",
+ "role": "user",
+ "parts": [{"type": "text", "text": "write code"}],
+ }
+ ],
+ system_prompt="Return cells.",
+ max_tokens=100,
+ output_type=NotebookCellsCompletion,
+ data_type=NOTEBOOK_CELLS_COMPLETION_DATA_TYPE,
+ stream_options=StreamOptions(
+ span_info=SpanInfo(endpoint="completion", model="openai/gpt-4")
+ ),
+ )
+
+ chunks = [chunk async for chunk in response.body_iterator]
+ body = "".join(
+ chunk.decode() if isinstance(chunk, bytes) else chunk
+ for chunk in chunks
+ )
+ assert '"code":"partial"' in body
+ assert '"type":"error"' in body
+ assert '"finishReason":"error"' in body
+ log_exception.assert_called_once_with("Structured completion failed")
+
+
@pytest.mark.parametrize(
("model_name", "provider_name"),
[
diff --git a/tests/_server/api/endpoints/test_ai.py b/tests/_server/api/endpoints/test_ai.py
index e466dd5ad6..52ba389e4a 100644
--- a/tests/_server/api/endpoints/test_ai.py
+++ b/tests/_server/api/endpoints/test_ai.py
@@ -7,6 +7,12 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
+from marimo._server.ai.completion_output import (
+ CELL_COMPLETION_DATA_TYPE,
+ NOTEBOOK_CELLS_COMPLETION_DATA_TYPE,
+ CellCompletion,
+ NotebookCellsCompletion,
+)
from marimo._server.ai.prompts import (
FIM_MIDDLE_TAG,
FIM_PREFIX_TAG,
@@ -128,7 +134,9 @@ class TestOpenAiEndpoints:
@staticmethod
@with_session(SESSION_ID)
- @patch("marimo._server.ai.providers.OpenAIProvider.stream_completion")
+ @patch(
+ "marimo._server.ai.providers.OpenAIProvider.stream_structured_completion"
+ )
def test_completion_without_code(
client: TestClient, mock_stream_completion: Any
) -> None:
@@ -156,10 +164,49 @@ class TestOpenAiEndpoints:
_assert_completion_prompt_in_messages(
mock_stream_completion, "Help me create a dataframe"
)
+ kwargs = mock_stream_completion.call_args.kwargs
+ assert kwargs["output_type"] is CellCompletion
+ assert kwargs["data_type"] == CELL_COMPLETION_DATA_TYPE
@staticmethod
@with_session(SESSION_ID)
- @patch("marimo._server.ai.providers.OpenAIProvider.stream_completion")
+ @patch(
+ "marimo._server.ai.providers.OpenAIProvider.stream_structured_completion"
+ )
+ def test_completion_with_ui_messages(
+ client: TestClient, mock_stream_completion: Any
+ ) -> None:
+ user_config_manager = get_session_config_manager(client)
+ mock_stream_completion.return_value = (
+ _mock_stream_completion_response()
+ )
+
+ with patch.object(
+ user_config_manager,
+ "get_config",
+ return_value=_openai_config(),
+ ):
+ response = client.post(
+ "/api/ai/completion",
+ headers=HEADERS,
+ json={
+ "prompt": "",
+ "includeOtherCode": "",
+ "code": "",
+ "uiMessages": _create_messages("Create two cells"),
+ },
+ )
+
+ assert response.status_code == 200, response.text
+ kwargs = mock_stream_completion.call_args.kwargs
+ assert kwargs["output_type"] is NotebookCellsCompletion
+ assert kwargs["data_type"] == NOTEBOOK_CELLS_COMPLETION_DATA_TYPE
+
+ @staticmethod
+ @with_session(SESSION_ID)
+ @patch(
+ "marimo._server.ai.providers.OpenAIProvider.stream_structured_completion"
+ )
def test_completion_with_code(
client: TestClient, mock_stream_completion: Any
) -> None:
@@ -190,7 +237,9 @@ class TestOpenAiEndpoints:
@staticmethod
@with_session(SESSION_ID)
- @patch("marimo._server.ai.providers.OpenAIProvider.stream_completion")
+ @patch(
+ "marimo._server.ai.providers.OpenAIProvider.stream_structured_completion"
+ )
def test_completion_with_custom_model(
client: TestClient, mock_stream_completion: Any
) -> None:
@@ -219,7 +268,9 @@ class TestOpenAiEndpoints:
@staticmethod
@with_session(SESSION_ID)
- @patch("marimo._server.ai.providers.OpenAIProvider.stream_completion")
+ @patch(
+ "marimo._server.ai.providers.OpenAIProvider.stream_structured_completion"
+ )
def test_completion_with_custom_base_url(
client: TestClient, mock_stream_completion: Any
) -> None:
@@ -368,7 +419,9 @@ class TestAnthropicAiEndpoints:
@staticmethod
@with_session(SESSION_ID)
- @patch("marimo._server.ai.providers.AnthropicProvider.stream_completion")
+ @patch(
+ "marimo._server.ai.providers.AnthropicProvider.stream_structured_completion"
+ )
def test_anthropic_completion_with_code(
client: TestClient, stream_completion_mock: Any
) -> None:
@@ -394,10 +447,6 @@ class TestAnthropicAiEndpoints:
_assert_completion_prompt_in_messages(
stream_completion_mock, "Help me create a dataframe"
)
- assert (
- stream_completion_mock.call_args.kwargs["enable_capabilities"]
- is False
- )
@staticmethod
@with_session(SESSION_ID)
@@ -441,7 +490,9 @@ class TestAnthropicAiEndpoints:
class TestGoogleAiEndpoints:
@staticmethod
@with_session(SESSION_ID)
- @patch("marimo._server.ai.providers.GoogleProvider.stream_completion")
+ @patch(
+ "marimo._server.ai.providers.GoogleProvider.stream_structured_completion"
+ )
def test_google_ai_completion_with_code(
client: TestClient, stream_completion_mock: Any
) -> None:
@@ -488,7 +539,7 @@ class TestGoogleAiEndpoints:
user_config_manager = get_session_config_manager(client)
mock_provider = AsyncMock(spec=PydanticProvider)
- mock_provider.stream_completion.return_value = (
+ mock_provider.stream_structured_completion.return_value = (
_mock_stream_completion_response()
)
@@ -520,7 +571,8 @@ class TestGoogleAiEndpoints:
assert response.status_code == 200, response.text
_assert_completion_prompt_in_messages(
- mock_provider.stream_completion, "Help me create a dataframe"
+ mock_provider.stream_structured_completion,
+ "Help me create a dataframe",
)
@staticmethod