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.

add run stale cells tool, refactor out notebook actions (#6842)

Shahmir Varqhaa208c5d0488b
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index e92fc193de..bb6fc65fcd 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -5,7 +5,7 @@ import { useChat } from "@ai-sdk/react";
import { storePrompt } from "@marimo-team/codemirror-ai";
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { DefaultChatTransport, type ToolUIPart } from "ai";
-import { useAtom, useAtomValue, useSetAtom } from "jotai";
+import { useAtom, useAtomValue, useSetAtom, useStore } from "jotai";
import {
AtSignIcon,
BotMessageSquareIcon,
@@ -30,16 +30,19 @@ import {
import { replaceMessagesInChat } from "@/core/ai/chat-utils";
import { useModelChange } from "@/core/ai/config";
import { AiModelId, type ProviderId } from "@/core/ai/ids/ids";
+import { useStagedAICellsActions } from "@/core/ai/staged-cells";
import {
activeChatAtom,
type Chat,
type ChatId,
chatStateAtom,
} from "@/core/ai/state";
+import type { ToolNotebookContext } from "@/core/ai/tools/base";
import {
type CopilotMode,
FRONTEND_TOOL_REGISTRY,
} from "@/core/ai/tools/registry";
+import { useCellActions } from "@/core/cells/cells";
import { aiAtom, aiEnabledAtom } from "@/core/config/config";
import { DEFAULT_AI_MODEL } from "@/core/config/config-schema";
import { FeatureFlagged } from "@/core/config/feature-flag";
@@ -48,7 +51,6 @@ import { useRuntimeManager } from "@/core/runtime/config";
import { ErrorBanner } from "@/plugins/impl/common/error-banner";
import { cn } from "@/utils/cn";
import { Logger } from "@/utils/Logger";
-
import { AIModelDropdown } from "../ai/ai-model-dropdown";
import { useOpenSettingsToTab } from "../app-config/state";
import { PromptInput } from "../editor/ai/add-cell-with-ai";
@@ -523,9 +525,20 @@ const ChatPanelBody = () => {
const fileInputRef = useRef<HTMLInputElement>(null);
const messagesEndRef = useRef<HTMLDivElement>(null);
const runtimeManager = useRuntimeManager();
- const { invokeAiTool } = useRequestClient();
+ const { invokeAiTool, sendRun } = useRequestClient();
const activeChatId = activeChat?.id;
+ const store = useStore();
+
+ const { addStagedCell } = useStagedAICellsActions();
+ const { createNewCell, prepareForRun } = useCellActions();
+ const toolContext: ToolNotebookContext = {
+ addStagedCell,
+ createNewCell,
+ prepareForRun,
+ sendRun,
+ store,
+ };
const {
messages,
@@ -567,6 +580,13 @@ const ChatPanelBody = () => {
});
},
onToolCall: async ({ toolCall }) => {
+ // Dynamic tool calls will throw an error for toolName
+ // https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-tool-usage#client-side-page
+ if (toolCall.dynamic) {
+ Logger.debug("Skipping dynamic tool call", toolCall);
+ return;
+ }
+
await handleToolCall({
invokeAiTool,
addToolResult,
@@ -575,6 +595,7 @@ const ChatPanelBody = () => {
toolCallId: toolCall.toolCallId,
input: toolCall.input as Record<string, never>,
},
+ toolContext,
});
},
onError: (error) => {
diff --git a/frontend/src/components/chat/chat-utils.ts b/frontend/src/components/chat/chat-utils.ts
index b758a663cf..6214308edf 100644
--- a/frontend/src/components/chat/chat-utils.ts
+++ b/frontend/src/components/chat/chat-utils.ts
@@ -2,6 +2,7 @@
import type { components } from "@marimo-team/marimo-api";
import type { FileUIPart, ToolUIPart, UIMessage } from "ai";
+import type { ToolNotebookContext } from "@/core/ai/tools/base";
import { FRONTEND_TOOL_REGISTRY } from "@/core/ai/tools/registry";
import type {
InvokeAiToolRequest,
@@ -106,8 +107,9 @@ interface AddToolResult {
export async function handleToolCall({
invokeAiTool,
- addToolResult,
+ addToolResult, // Important that we don't await addToolResult to prevent potential deadlocks
toolCall,
+ toolContext,
}: {
invokeAiTool: (request: InvokeAiToolRequest) => Promise<InvokeAiToolResponse>;
addToolResult: (result: AddToolResult) => Promise<void>;
@@ -116,6 +118,7 @@ export async function handleToolCall({
toolCallId: string;
input: Record<string, never>;
};
+ toolContext: ToolNotebookContext;
}) {
try {
if (FRONTEND_TOOL_REGISTRY.has(toolCall.toolName)) {
@@ -123,6 +126,7 @@ export async function handleToolCall({
const response = await FRONTEND_TOOL_REGISTRY.invoke(
toolCall.toolName,
toolCall.input,
+ toolContext,
);
addToolResult({
tool: toolCall.toolName,
diff --git a/frontend/src/components/chat/tool-call-accordion.tsx b/frontend/src/components/chat/tool-call-accordion.tsx
index dfb53a8f04..38fa8d53c0 100644
--- a/frontend/src/components/chat/tool-call-accordion.tsx
+++ b/frontend/src/components/chat/tool-call-accordion.tsx
@@ -119,6 +119,8 @@ const ToolArgsRenderer: React.FC<{ input: unknown }> = ({ input }) => {
return null;
}
+ const isEmptyInput = isEmpty(input);
+
const isObject =
typeof input === "object" &&
Object.keys(input as Record<string, unknown>).length > 0;
@@ -129,7 +131,11 @@ const ToolArgsRenderer: React.FC<{ input: unknown }> = ({ input }) => {
Tool Request
</h3>
<pre className="bg-[var(--slate-2)] p-2 text-muted-foreground border border-[var(--slate-4)] rounded text-xs overflow-auto scrollbar-thin max-h-64">
- {isObject ? JSON.stringify(input, null, 2) : String(input)}
+ {isEmptyInput
+ ? "{}"
+ : isObject
+ ? JSON.stringify(input, null, 2)
+ : String(input)}
</pre>
</div>
);
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 6d11fd6461..5337bead48 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -44,6 +44,8 @@ import {
} from "@/components/ui/dropdown-menu";
import { toast } from "@/components/ui/use-toast";
import { stagedAICellsAtom, useStagedCells } from "@/core/ai/staged-cells";
+import type { ToolNotebookContext } from "@/core/ai/tools/base";
+import { useCellActions } from "@/core/cells/cells";
import { resourceExtension } from "@/core/codemirror/ai/resources";
import { useRequestClient } from "@/core/network/requests";
import type { AiCompletionRequest } from "@/core/network/types";
@@ -79,15 +81,24 @@ export const AddCellWithAI: React.FC<{
const store = useStore();
const [input, setInput] = useState("");
- const { deleteAllStagedCells, clearStagedCells, onStream } =
+ const { deleteAllStagedCells, clearStagedCells, onStream, addStagedCell } =
useStagedCells(store);
const [language, setLanguage] = useAtom(languageAtom);
const runtimeManager = useRuntimeManager();
- const { invokeAiTool } = useRequestClient();
+ const { invokeAiTool, sendRun } = useRequestClient();
const stagedAICells = useAtomValue(stagedAICellsAtom);
const inputRef = useRef<ReactCodeMirrorRef>(null);
+ const { createNewCell, prepareForRun } = useCellActions();
+ const toolContext: ToolNotebookContext = {
+ store,
+ addStagedCell,
+ createNewCell,
+ prepareForRun,
+ sendRun,
+ };
+
const { sendMessage, stop, status, addToolResult } = useChat({
// Throttle the messages and data updates to 100ms
experimental_throttle: 100,
@@ -125,6 +136,7 @@ export const AddCellWithAI: React.FC<{
toolCallId: toolCall.toolCallId,
input: toolCall.input as Record<string, never>,
},
+ toolContext,
});
},
onError: (error) => {
diff --git a/frontend/src/core/ai/context/providers/__tests__/__snapshots__/cell-output.test.ts.snap b/frontend/src/core/ai/context/providers/__tests__/__snapshots__/cell-output.test.ts.snap
new file mode 100644
index 0000000000..ff80a67c24
--- /dev/null
+++ b/frontend/src/core/ai/context/providers/__tests__/__snapshots__/cell-output.test.ts.snap
@@ -0,0 +1,17 @@
+// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
+
+exports[`CellOutputContextProvider > formatContext > should format media output context correctly > media-output-context 1`] = `
+"<cell-output name="cell-1" cellId="cell2" outputType="media" mimetype="image/png">Cell Code:
+import matplotlib.pyplot as plt
+plt.plot([1,2,3])
+
+Media Output: Contains image/png content</cell-output>"
+`;
+
+exports[`CellOutputContextProvider > formatContext > should format text output context correctly > text-output-context 1`] = `
+"<cell-output name="My Cell" cellId="cell1" outputType="text" mimetype="text/plain">Cell Code:
+print('hello world')
+
+Output:
+hello world</cell-output>"
+`;
diff --git a/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts b/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts
index 958b758f8d..f02e6b7017 100644
--- a/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts
+++ b/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts
@@ -16,7 +16,7 @@ import type { NotebookState } from "@/core/cells/cells";
import type { CellId } from "@/core/cells/ids";
import type { OutputMessage } from "@/core/kernel/messages";
import type { JotaiStore } from "@/core/state/jotai";
-import { CellOutputContextProvider } from "../cell-output";
+import { CellOutputContextProvider, getCellContextData } from "../cell-output";
// Test helper to create mock store
function createMockStore(notebook: NotebookState): JotaiStore {
@@ -104,23 +104,23 @@ describe("CellOutputContextProvider", () => {
const textItem = items.find((item) => item.data.cellId === "cell1");
const mediaItem = items.find((item) => item.data.cellId === "cell2");
- expect(textItem?.data.outputType).toBe("text");
- expect(mediaItem?.data.outputType).toBe("media");
+ expect(textItem?.data.cellOutput.outputType).toBe("text");
+ expect(mediaItem?.data.cellOutput.outputType).toBe("media");
});
it("should process text content correctly", () => {
const items = provider.getItems();
const textItem = items.find((item) => item.data.cellId === "cell1");
- expect(textItem?.data.processedContent).toBe("hello world");
+ expect(textItem?.data.cellOutput.processedContent).toBe("hello world");
});
it("should mark media items for download when no direct URL available", () => {
const items = provider.getItems();
const mediaItem = items.find((item) => item.data.cellId === "cell2");
- expect(mediaItem?.data.shouldDownloadImage).toBe(true);
- expect(mediaItem?.data.imageUrl).toBeUndefined();
+ expect(mediaItem?.data.cellOutput.shouldDownloadImage).toBe(true);
+ expect(mediaItem?.data.cellOutput.imageUrl).toBeUndefined();
});
it("should include cell code and names", () => {
@@ -156,6 +156,8 @@ describe("CellOutputContextProvider", () => {
expect(context).toContain("print('hello world')");
expect(context).toContain("Output:");
expect(context).toContain("hello world");
+
+ expect(context).toMatchSnapshot("text-output-context");
});
it("should format media output context correctly", () => {
@@ -173,6 +175,8 @@ describe("CellOutputContextProvider", () => {
expect(context).toContain("Cell Code:");
expect(context).toContain("import matplotlib.pyplot as plt");
expect(context).toContain("Media Output: Contains image/png content");
+
+ expect(context).toMatchSnapshot("media-output-context");
});
});
@@ -239,7 +243,7 @@ describe("Cell output utility functions", () => {
provider = new CellOutputContextProvider(mockStore);
const items = provider.getItems();
- expect(items[0]?.data.outputType).toBe(testCase.expected);
+ expect(items[0]?.data.cellOutput.outputType).toBe(testCase.expected);
}
});
@@ -274,7 +278,7 @@ describe("Cell output utility functions", () => {
provider = new CellOutputContextProvider(mockStore);
const items = provider.getItems();
- expect(items[0]?.data.outputType).toBe(testCase.expected);
+ expect(items[0]?.data.cellOutput.outputType).toBe(testCase.expected);
}
});
@@ -308,7 +312,7 @@ describe("Cell output utility functions", () => {
provider = new CellOutputContextProvider(mockStore);
const items = provider.getItems();
- expect(items[0]?.data.outputType).toBe(testCase.expected);
+ expect(items[0]?.data.cellOutput.outputType).toBe(testCase.expected);
}
});
});
@@ -371,7 +375,187 @@ describe("Cell output utility functions", () => {
provider = new CellOutputContextProvider(mockStore);
const items = provider.getItems();
- expect(items[0]?.data.processedContent).toBe("Hello world!");
+ expect(items[0]?.data.cellOutput.processedContent).toBe("Hello world!");
+ });
+ });
+
+ describe("getCellContextData", () => {
+ let mockNotebook: NotebookState;
+
+ beforeEach(() => {
+ mockNotebook = {
+ cellIds: {
+ inOrderIds: ["cell1" as CellId, "cell2" as CellId],
+ },
+ cellData: {
+ cell1: {
+ id: "cell1" as CellId,
+ name: "My Named Cell",
+ code: "x = 42",
+ },
+ cell2: {
+ id: "cell2" as CellId,
+ name: "",
+ code: "y = x * 2",
+ },
+ },
+ cellRuntime: {
+ cell1: {
+ output: {
+ mimetype: "text/plain",
+ data: "42",
+ } as OutputMessage,
+ consoleOutputs: [],
+ },
+ cell2: {
+ output: null,
+ consoleOutputs: [
+ {
+ mimetype: "text/plain",
+ data: "console log message",
+ } as OutputMessage,
+ ],
+ },
+ },
+ } as unknown as NotebookState;
+ });
+
+ it("should extract basic cell data", () => {
+ const result = getCellContextData("cell1" as CellId, mockNotebook);
+
+ expect(result.cellId).toBe("cell1");
+ expect(result.cellName).toBe("My Named Cell");
+ expect(result.cellCode).toBe("x = 42");
+ });
+
+ it("should generate cell name for unnamed cells", () => {
+ const result = getCellContextData("cell2" as CellId, mockNotebook);
+
+ expect(result.cellName).toBe("cell-1"); // 0-indexed, so cell2 is at index 1
+ });
+
+ it("should include cell output when present", () => {
+ const result = getCellContextData("cell1" as CellId, mockNotebook);
+
+ expect(result.cellOutput).toBeDefined();
+ expect(result.cellOutput?.outputType).toBe("text");
+ expect(result.cellOutput?.processedContent).toBe("42");
+ });
+
+ it("should not include cell output when not present", () => {
+ const result = getCellContextData("cell2" as CellId, mockNotebook);
+
+ expect(result.cellOutput).toBeUndefined();
+ });
+
+ it("should not include console outputs by default", () => {
+ const result = getCellContextData("cell2" as CellId, mockNotebook);
+
+ expect(result.consoleOutputs).toBeUndefined();
+ });
+
+ it("should include console outputs when opted in", () => {
+ const result = getCellContextData("cell2" as CellId, mockNotebook, {
+ includeConsoleOutput: true,
+ });
+
+ expect(result.consoleOutputs).toBeDefined();
+ expect(result.consoleOutputs).toHaveLength(1);
+ expect(result.consoleOutputs?.[0]?.processedContent).toBe(
+ "console log message",
+ );
+ });
+
+ it("should filter out empty console outputs", () => {
+ const cell2Runtime = mockNotebook.cellRuntime["cell2" as CellId];
+ if (cell2Runtime) {
+ (cell2Runtime as { consoleOutputs: OutputMessage[] }).consoleOutputs = [
+ {
+ mimetype: "text/plain",
+ data: "valid output",
+ } as OutputMessage,
+ {
+ mimetype: "text/plain",
+ data: "",
+ } as OutputMessage,
+ ];
+ }
+
+ const result = getCellContextData("cell2" as CellId, mockNotebook, {
+ includeConsoleOutput: true,
+ });
+
+ expect(result.consoleOutputs).toHaveLength(1);
+ expect(result.consoleOutputs?.[0]?.processedContent).toBe("valid output");
+ });
+
+ it("should handle cells with both cell output and console outputs", () => {
+ const cell1Runtime = mockNotebook.cellRuntime["cell1" as CellId];
+ if (cell1Runtime) {
+ (cell1Runtime as { consoleOutputs: OutputMessage[] }).consoleOutputs = [
+ {
+ mimetype: "text/plain",
+ data: "debug message",
+ } as OutputMessage,
+ ];
+ }
+
+ const result = getCellContextData("cell1" as CellId, mockNotebook, {
+ includeConsoleOutput: true,
+ });
+
+ expect(result.cellOutput).toBeDefined();
+ expect(result.cellOutput?.processedContent).toBe("42");
+ expect(result.consoleOutputs).toHaveLength(1);
+ expect(result.consoleOutputs?.[0]?.processedContent).toBe(
+ "debug message",
+ );
+ });
+
+ it("should handle empty console outputs array", () => {
+ const result = getCellContextData("cell1" as CellId, mockNotebook, {
+ includeConsoleOutput: true,
+ });
+
+ expect(result.consoleOutputs).toEqual([]);
+ });
+
+ it("should handle media outputs in cell output", () => {
+ const cell1Runtime = mockNotebook.cellRuntime["cell1" as CellId];
+ if (cell1Runtime) {
+ (cell1Runtime as { output: OutputMessage | null }).output = {
+ mimetype: "image/png",
+ data: "data:image/png;base64,test",
+ } as OutputMessage;
+ }
+
+ const result = getCellContextData("cell1" as CellId, mockNotebook);
+
+ expect(result.cellOutput).toBeDefined();
+ expect(result.cellOutput?.outputType).toBe("media");
+ expect(result.cellOutput?.imageUrl).toBe("data:image/png;base64,test");
+ });
+
+ it("should handle media outputs in console outputs", () => {
+ const cell2Runtime = mockNotebook.cellRuntime["cell2" as CellId];
+ if (cell2Runtime) {
+ (cell2Runtime as { consoleOutputs: OutputMessage[] }).consoleOutputs = [
+ {
+ mimetype: "image/png",
+ data: "https://example.com/image.png",
+ } as OutputMessage,
+ ];
+ }
+
+ const result = getCellContextData("cell2" as CellId, mockNotebook, {
+ includeConsoleOutput: true,
+ });
+
+ expect(result.consoleOutputs).toHaveLength(1);
+ expect(result.consoleOutputs?.[0]?.outputType).toBe("media");
+ expect(result.consoleOutputs?.[0]?.imageUrl).toBe(
+ "https://example.com/image.png",
+ );
});
});
});
diff --git a/frontend/src/core/ai/context/providers/cell-output.ts b/frontend/src/core/ai/context/providers/cell-output.ts
index 2bfa7afaae..c22aad4c20 100644
--- a/frontend/src/core/ai/context/providers/cell-output.ts
+++ b/frontend/src/core/ai/context/providers/cell-output.ts
@@ -3,7 +3,7 @@
import type { Completion } from "@codemirror/autocomplete";
import type { FileUIPart } from "ai";
import { toPng } from "html-to-image";
-import { notebookAtom } from "@/core/cells/cells";
+import { type NotebookState, notebookAtom } from "@/core/cells/cells";
import { type CellId, CellOutputId } from "@/core/cells/ids";
import { displayCellName } from "@/core/cells/names";
import { isOutputEmpty } from "@/core/cells/outputs";
@@ -15,18 +15,33 @@ import { type AIContextItem, AIContextProvider } from "../registry";
import { contextToXml } from "../utils";
import { Boosts, Sections } from "./common";
+export interface BaseOutput {
+ processedContent?: string;
+ imageUrl?: string;
+ shouldDownloadImage?: boolean;
+ output: OutputMessage;
+ outputType: "text" | "media";
+}
+
+export interface CellOutputData {
+ cellId: CellId;
+ cellName: string;
+ cellCode: string;
+ cellOutput?: BaseOutput;
+ consoleOutputs?: BaseOutput[];
+}
+
+// For the context provider
+// Currently, we enforce that cellOutput is present.
+interface CellOutputContextData
+ extends CellOutputData,
+ Record<string, unknown> {
+ cellOutput: BaseOutput;
+}
+
export interface CellOutputContextItem extends AIContextItem {
type: "cell-output";
- data: {
- cellId: CellId;
- cellName: string;
- cellCode: string;
- output: OutputMessage;
- outputType: "text" | "media";
- processedContent?: string;
- imageUrl?: string;
- shouldDownloadImage?: boolean;
- };
+ data: CellOutputContextData;
}
function isMediaMimetype(
@@ -79,69 +94,23 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
const notebook = this.store.get(notebookAtom);
const items: CellOutputContextItem[] = [];
- for (const [cellIndex, cellId] of notebook.cellIds.inOrderIds.entries()) {
- const cellRuntime = notebook.cellRuntime[cellId];
+ for (const cellId of notebook.cellIds.inOrderIds) {
+ const cellContextData = getCellContextData(cellId, notebook);
+ const cellOutput = cellContextData.cellOutput;
- // Filter to only cells with output
- if (!cellRuntime?.output || isOutputEmpty(cellRuntime.output)) {
+ // Skip cells with no output
+ if (!cellOutput) {
continue;
}
- const cellData = notebook.cellData[cellId];
- const output = cellRuntime.output;
- const mimetype = output.mimetype;
-
- // Determine output type
- const isMedia = isMediaMimetype(mimetype, String(output.data));
- const outputType = isMedia ? "media" : "text";
-
- const cellName = displayCellName(cellData.name, cellIndex);
-
- let processedContent: string | undefined;
- let imageUrl: string | undefined;
- let shouldDownloadImage = false;
-
- // Process text content
- if (outputType === "text" && typeof output.data === "string") {
- processedContent =
- mimetype === "text/html"
- ? parseHtmlContent(output.data)
- : output.data;
- }
-
- // Process media content - for now, we'll just note that it's media
- if (outputType === "media") {
- if (
- typeof output.data === "string" &&
- output.data.startsWith("data:")
- ) {
- imageUrl = output.data; // Data URL
- } else if (
- typeof output.data === "string" &&
- output.data.startsWith("http")
- ) {
- imageUrl = output.data; // External URL
- } else {
- // Download the image from the DOM element
- shouldDownloadImage = true;
- }
- }
+ const mimetype = cellOutput.output.mimetype || "unknown";
items.push({
uri: this.asURI(cellId),
- name: cellName,
+ name: cellContextData.cellName,
type: this.contextType,
- description: `Cell output (${mimetype || "unknown"})`,
- data: {
- cellId,
- cellName,
- cellCode: cellData?.code || "",
- output,
- outputType,
- processedContent,
- imageUrl,
- shouldDownloadImage,
- },
+ description: `Cell output (${mimetype})`,
+ data: { ...cellContextData, cellOutput },
});
}
@@ -149,16 +118,16 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
}
formatCompletion(item: CellOutputContextItem): Completion {
- const { data } = item;
+ const { cellOutput: data, cellName, cellCode } = item.data;
return {
- label: `@${data.cellName}`,
- displayLabel: data.cellName,
+ label: `@${cellName}`,
+ displayLabel: cellName,
detail: `${data.outputType} output`,
boost: data.outputType === "media" ? Boosts.HIGH : Boosts.MEDIUM,
type: this.contextType,
section: Sections.CELL_OUTPUT,
- apply: `@${data.cellName}`,
+ apply: `@${cellName}`,
info: () => {
const infoContainer = document.createElement("div");
infoContainer.classList.add(
@@ -176,7 +145,7 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
const nameDiv = document.createElement("div");
nameDiv.classList.add("font-bold", "text-base");
- nameDiv.textContent = data.cellName;
+ nameDiv.textContent = cellName;
headerDiv.append(nameDiv);
const descriptionDiv = document.createElement("div");
@@ -186,7 +155,7 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
infoContainer.append(headerDiv);
// Show cell code preview
- if (data.cellCode) {
+ if (cellCode) {
const codeHeaderDiv = document.createElement("div");
codeHeaderDiv.classList.add(
"text-xs",
@@ -207,8 +176,7 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
"overflow-y-auto",
);
codeDiv.textContent =
- data.cellCode.slice(0, 200) +
- (data.cellCode.length > 200 ? "..." : "");
+ cellCode.slice(0, 200) + (cellCode.length > 200 ? "..." : "");
infoContainer.append(codeDiv);
}
@@ -258,16 +226,16 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
}
formatContext(item: CellOutputContextItem): string {
- const { data } = item;
+ const { cellOutput: data, cellName, cellId, cellCode } = item.data;
const contextData = {
- name: data.cellName,
- cellId: data.cellId,
+ name: cellName,
+ cellId: cellId,
outputType: data.outputType,
mimetype: data.output.mimetype,
} as const;
- let details = `Cell Code:\n${data.cellCode}\n\n`;
+ let details = `Cell Code:\n${cellCode}\n\n`;
if (data.outputType === "text" && data.processedContent) {
details += `Output:\n${data.processedContent}`;
@@ -289,45 +257,135 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
override async getAttachments(
items: CellOutputContextItem[],
): Promise<FileUIPart[]> {
- // Filter items that need image downloading
- const itemsNeedingDownload = items.filter(
- (item) =>
- item.data.shouldDownloadImage && item.data.outputType === "media",
- );
+ const cellId = items[0].data.cellId;
+ const cellName = items[0].data.cellName;
+ const cellOutputs = items.map((item) => item.data.cellOutput);
- if (itemsNeedingDownload.length === 0) {
+ return getAttachmentsForOutputs(cellOutputs, cellId, cellName);
+ }
+}
+
+export async function getAttachmentsForOutputs(
+ data: BaseOutput[],
+ cellId: CellId,
+ cellName: string,
+): Promise<FileUIPart[]> {
+ // Filter items that need image downloading
+ const itemsNeedingDownload = data.filter(
+ (item) => item.shouldDownloadImage && item.outputType === "media",
+ );
+
+ if (itemsNeedingDownload.length === 0) {
+ return [];
+ }
+
+ // Prepare download requests
+ const downloadRequests = itemsNeedingDownload.flatMap((item) => {
+ const outputElement = document.getElementById(CellOutputId.create(cellId));
+ if (!outputElement) {
+ Logger.warn(`Output element not found for cell ${cellId}`);
return [];
}
+ return {
+ cellId: cellId,
+ cellName: cellName,
+ mimetype: item.output.mimetype,
+ element: outputElement,
+ };
+ });
+
+ try {
+ return await Promise.all(
+ downloadRequests.map(async (item) => ({
+ type: "file",
+ filename: `${cellName}-output-screenshot`,
+ mediaType: "image/png",
+ url: await toPng(item.element),
+ })),
+ );
+ } catch (error) {
+ Logger.error("Error downloading cell output images:", error);
+ return [];
+ }
+}
- // Prepare download requests
- const downloadRequests = itemsNeedingDownload.flatMap((item) => {
- const outputElement = document.getElementById(
- CellOutputId.create(item.data.cellId),
- );
- if (!outputElement) {
- Logger.warn(`Output element not found for cell ${item.data.cellId}`);
- return [];
- }
- return {
- cellId: item.data.cellId,
- cellName: item.data.cellName,
- mimetype: item.data.output.mimetype,
- element: outputElement,
- };
- });
+function getBaseOutput(output: OutputMessage): BaseOutput | null {
+ if (isOutputEmpty(output)) {
+ return null;
+ }
- try {
- return await Promise.all(
- downloadRequests.map(async (item) => ({
- type: "file",
- filename: `${item.cellName}-output-screenshot`,
- mediaType: "image/png",
- url: await toPng(item.element),
- })),
- );
- } catch (error) {
- Logger.error("Error downloading cell output images:", error);
- return [];
+ const mimetype = output.mimetype;
+
+ // Determine output type
+ const isMedia = isMediaMimetype(mimetype, String(output.data));
+ const outputType = isMedia ? "media" : "text";
+
+ let processedContent: string | undefined;
+ let imageUrl: string | undefined;
+ let shouldDownloadImage = false;
+
+ // Process text content
+ if (outputType === "text" && typeof output.data === "string") {
+ processedContent =
+ mimetype === "text/html" ? parseHtmlContent(output.data) : output.data;
+ }
+
+ // Process media content - for now, we'll just note that it's media
+ if (outputType === "media") {
+ if (typeof output.data === "string" && output.data.startsWith("data:")) {
+ imageUrl = output.data; // Data URL
+ } else if (
+ typeof output.data === "string" &&
+ output.data.startsWith("http")
+ ) {
+ imageUrl = output.data; // External URL
+ } else {
+ // Download the image from the DOM element
+ shouldDownloadImage = true;
}
}
+
+ return {
+ processedContent,
+ imageUrl,
+ shouldDownloadImage,
+ outputType,
+ output,
+ };
+}
+
+export function getCellContextData(
+ cellId: CellId,
+ notebook: NotebookState,
+ opts?: {
+ includeConsoleOutput: boolean;
+ },
+): CellOutputData {
+ const { includeConsoleOutput = false } = opts || {};
+ const cellRuntime = notebook.cellRuntime[cellId];
+
+ const cellData = notebook.cellData[cellId];
+ const cellIndex = notebook.cellIds.inOrderIds.indexOf(cellId);
+ const cellName = displayCellName(cellData.name, cellIndex);
+
+ let consoleOutputs: BaseOutput[] | undefined;
+ if (includeConsoleOutput) {
+ consoleOutputs = cellRuntime.consoleOutputs
+ .map((output) => getBaseOutput(output))
+ .filter((output) => output !== null);
+ }
+
+ let cellOutput: BaseOutput | undefined;
+ const rawCellOutput = cellRuntime.output;
+ if (rawCellOutput && !isOutputEmpty(rawCellOutput)) {
+ cellOutput = getBaseOutput(rawCellOutput) ?? undefined;
+ }
+
+ return {
+ cellId,
+ cellName,
+ cellCode: cellData.code,
+ cellOutput,
+ consoleOutputs,
+ };
}
diff --git a/frontend/src/core/ai/staged-cells.ts b/frontend/src/core/ai/staged-cells.ts
index f25ac18a65..a76290b79d 100644
--- a/frontend/src/core/ai/staged-cells.ts
+++ b/frontend/src/core/ai/staged-cells.ts
@@ -60,6 +60,7 @@ const {
});
export {
+ useStagedAICellsActions,
createActions as createStagedAICellsActions,
reducer as stagedAICellsReducer,
};
diff --git a/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts b/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts
index 333df23e20..24574d379c 100644
--- a/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts
+++ b/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts
@@ -7,6 +7,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { MockNotebook } from "@/__mocks__/notebook";
import { notebookAtom } from "@/core/cells/cells";
import type { CellId } from "@/core/cells/ids";
+import { updateEditorCodeFromPython } from "@/core/codemirror/language/utils";
import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";
import type { CellColumnId } from "@/utils/id-tree";
import { MultiColumn } from "@/utils/id-tree";
@@ -26,8 +27,6 @@ vi.mock("@/core/codemirror/language/utils", () => ({
updateEditorCodeFromPython: vi.fn(),
}));
-import { updateEditorCodeFromPython } from "@/core/codemirror/language/utils";
-
function createMockEditorView(code: string): EditorView {
return new EditorView({
state: EditorState.create({
@@ -66,10 +65,31 @@ describe("EditNotebookTool", () => {
let cellId1: CellId;
let cellId2: CellId;
let cellId3: CellId;
+ let toolContext: {
+ addStagedCell: ReturnType<typeof vi.fn>;
+ createNewCell: ReturnType<typeof vi.fn>;
+ prepareForRun: ReturnType<typeof vi.fn>;
+ sendRun: ReturnType<typeof vi.fn>;
+ store: ReturnType<typeof getDefaultStore>;
+ };
beforeEach(() => {
store = getDefaultStore();
- tool = new EditNotebookTool(store);
+
+ // Reset atom states first
+ store.set(stagedAICellsAtom, new Map());
+
+ toolContext = {
+ addStagedCell: vi.fn(({ cellId, edit }) => {
+ const current = store.get(stagedAICellsAtom);
+ store.set(stagedAICellsAtom, new Map(current).set(cellId, edit));
+ }),
+ createNewCell: vi.fn(),
+ prepareForRun: vi.fn(),
+ sendRun: vi.fn().mockResolvedValue(null),
+ store,
+ };
+ tool = new EditNotebookTool();
cellId1 = "cell-1" as CellId;
cellId2 = "cell-2" as CellId;
@@ -77,9 +97,6 @@ describe("EditNotebookTool", () => {
// Reset mocks
vi.clearAllMocks();
-
- // Reset atom states
- store.set(stagedAICellsAtom, new Map());
});
describe("tool metadata", () => {
@@ -108,13 +125,16 @@ describe("EditNotebookTool", () => {
notebook.cellHandles[cellId1] = { current: { editorView } } as never;
store.set(notebookAtom, notebook);
- const result = await tool.handler({
- edit: {
- type: "update_cell",
- cellId: cellId1,
- code: newCode,
+ const result = await tool.handler(
+ {
+ edit: {
+ type: "update_cell",
+ cellId: cellId1,
+ code: newCode,
+ },
},
- });
+ toolContext as never,
+ );
expect(result.status).toBe("success");
expect(vi.mocked(updateEditorCodeFromPython)).toHaveBeenCalledWith(
@@ -140,23 +160,29 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
await expect(
- tool.handler({
- edit: {
- type: "update_cell",
- cellId: "nonexistent" as CellId,
- code: "x = 2",
+ tool.handler(
+ {
+ edit: {
+ type: "update_cell",
+ cellId: "nonexistent" as CellId,
+ code: "x = 2",
+ },
},
- }),
+ toolContext as never,
+ ),
).rejects.toThrow(ToolExecutionError);
await expect(
- tool.handler({
- edit: {
- type: "update_cell",
- cellId: "nonexistent" as CellId,
- code: "x = 2",
+ tool.handler(
+ {
+ edit: {
+ type: "update_cell",
+ cellId: "nonexistent" as CellId,
+ code: "x = 2",
+ },
},
- }),
+ toolContext as never,
+ ),
).rejects.toThrow("Cell not found");
});
@@ -171,13 +197,16 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
await expect(
- tool.handler({
- edit: {
- type: "update_cell",
- cellId: cellId1,
- code: "x = 2",
+ tool.handler(
+ {
+ edit: {
+ type: "update_cell",
+ cellId: cellId1,
+ code: "x = 2",
+ },
},
- }),
+ toolContext as never,
+ ),
).rejects.toThrow("Cell editor not found");
});
});
@@ -192,13 +221,16 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
const newCode = "y = 2";
- const result = await tool.handler({
- edit: {
- type: "add_cell",
- position: "__end__",
- code: newCode,
+ const result = await tool.handler(
+ {
+ edit: {
+ type: "add_cell",
+ position: "__end__",
+ code: newCode,
+ },
},
- });
+ toolContext as never,
+ );
expect(result.status).toBe("success");
@@ -220,13 +252,16 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
const newCode = "y = 2";
- const result = await tool.handler({
- edit: {
- type: "add_cell",
- position: { cellId: cellId2, before: true },
- code: newCode,
+ const result = await tool.handler(
+ {
+ edit: {
+ type: "add_cell",
+ position: { cellId: cellId2, before: true },
+ code: newCode,
+ },
},
- });
+ toolContext as never,
+ );
expect(result.status).toBe("success");
@@ -247,13 +282,16 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
const newCode = "y = 2";
- const result = await tool.handler({
- edit: {
- type: "add_cell",
- position: { cellId: cellId2, before: false },
- code: newCode,
+ const result = await tool.handler(
+ {
+ edit: {
+ type: "add_cell",
+ position: { cellId: cellId2, before: false },
+ code: newCode,
+ },
},
- });
+ toolContext as never,
+ );
expect(result.status).toBe("success");
@@ -275,13 +313,16 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
const newCode = "y = 2";
- const result = await tool.handler({
- edit: {
- type: "add_cell",
- position: { type: "__end__", columnId },
- code: newCode,
+ const result = await tool.handler(
+ {
+ edit: {
+ type: "add_cell",
+ position: { type: "__end__", columnId },
+ code: newCode,
+ },
},
- });
+ toolContext as never,
+ );
expect(result.status).toBe("success");
@@ -299,13 +340,16 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
await expect(
- tool.handler({
- edit: {
- type: "add_cell",
- position: { cellId: "nonexistent" as CellId, before: true },
- code: "y = 2",
+ tool.handler(
+ {
+ edit: {
+ type: "add_cell",
+ position: { cellId: "nonexistent" as CellId, before: true },
+ code: "y = 2",
+ },
},
- }),
+ toolContext as never,
+ ),
).rejects.toThrow("Cell not found");
});
@@ -318,16 +362,19 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
await expect(
- tool.handler({
- edit: {
- type: "add_cell",
- position: {
- type: "__end__",
- columnId: "nonexistent" as CellColumnId,
+ tool.handler(
+ {
+ edit: {
+ type: "add_cell",
+ position: {
+ type: "__end__",
+ columnId: "nonexistent" as CellColumnId,
+ },
+ code: "y = 2",
},
- code: "y = 2",
},
- }),
+ toolContext as never,
+ ),
).rejects.toThrow("Column not found");
});
});
@@ -345,12 +392,15 @@ describe("EditNotebookTool", () => {
notebook.cellHandles[cellId1] = { current: { editorView } } as never;
store.set(notebookAtom, notebook);
- const result = await tool.handler({
- edit: {
- type: "delete_cell",
- cellId: cellId1,
+ const result = await tool.handler(
+ {
+ edit: {
+ type: "delete_cell",
+ cellId: cellId1,
+ },
},
- });
+ toolContext as never,
+ );
expect(result.status).toBe("success");
@@ -372,12 +422,15 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
await expect(
- tool.handler({
- edit: {
- type: "delete_cell",
- cellId: "nonexistent" as CellId,
+ tool.handler(
+ {
+ edit: {
+ type: "delete_cell",
+ cellId: "nonexistent" as CellId,
+ },
},
- }),
+ toolContext as never,
+ ),
).rejects.toThrow("Cell not found");
});
@@ -392,12 +445,15 @@ describe("EditNotebookTool", () => {
store.set(notebookAtom, notebook);
await expect(
- tool.handler({
- edit: {
- type: "delete_cell",
- cellId: cellId1,
+ tool.handler(
+ {
+ edit: {
+ type: "delete_cell",
+ cellId: cellId1,
+ },
},
- }),
+ toolContext as never,
+ ),
).rejects.toThrow("Cell editor not found");
});
});
@@ -421,23 +477,29 @@ describe("EditNotebookTool", () => {
notebook.cellHandles[cellId3] = { current: { editorView } } as never;
await expect(
- tool.handler({
- edit: {
- type: "update_cell",
- cellId: cellId1,
- code: "y = 1",
+ tool.handler(
+ {
+ edit: {
+ type: "update_cell",
+ cellId: cellId1,
+ code: "y = 1",
+ },
},
- }),
+ toolContext as never,
+ ),
).resolves.toBeDefined();
await expect(
- tool.handler({
- edit: {
- type: "update_cell",
- cellId: cellId3,
- code: "y = 3",
+ tool.handler(
+ {
+ edit: {
+ type: "update_cell",
+ cellId: cellId3,
+ code: "y = 3",
+ },
},
- }),
+ toolContext as never,
+ ),
).resolves.toBeDefined();
});
});
@@ -451,13 +513,16 @@ describe("EditNotebookTool", () => {
});
store.set(notebookAtom, notebook);
- const result = await tool.handler({
- edit: {
- type: "add_cell",
- position: "__end__",
- code: "y = 2",
+ const result = await tool.handler(
+ {
+ edit: {
+ type: "add_cell",
+ position: "__end__",
+ code: "y = 2",
+ },
},
- });
+ toolContext as never,
+ );
expect(result.status).toBe("success");
expect(result.next_steps).toBeDefined();
diff --git a/frontend/src/core/ai/tools/__tests__/registry.test.ts b/frontend/src/core/ai/tools/__tests__/registry.test.ts
index e5fa2af4d2..32c74ed735 100644
--- a/frontend/src/core/ai/tools/__tests__/registry.test.ts
+++ b/frontend/src/core/ai/tools/__tests__/registry.test.ts
@@ -13,9 +13,13 @@ describe("FrontendToolRegistry", () => {
it("invokes a tool with valid args and validates input/output", async () => {
const registry = new FrontendToolRegistry([new TestFrontendTool()]);
- const response = await registry.invoke("test_frontend_tool", {
- name: "Alice",
- });
+ const response = await registry.invoke(
+ "test_frontend_tool",
+ {
+ name: "Alice",
+ },
+ {} as never,
+ );
// Check InvokeResult wrapper
expect(response.tool_name).toBe("test_frontend_tool");
@@ -38,7 +42,11 @@ describe("FrontendToolRegistry", () => {
it("returns a structured error on invalid args", async () => {
const registry = new FrontendToolRegistry([new TestFrontendTool()]);
- const response = await registry.invoke("test_frontend_tool", {});
+ const response = await registry.invoke(
+ "test_frontend_tool",
+ {},
+ {} as never,
+ );
// Check InvokeResult wrapper
expect(response.tool_name).toBe("test_frontend_tool");
diff --git a/frontend/src/core/ai/tools/__tests__/run-cells-tool.test.ts b/frontend/src/core/ai/tools/__tests__/run-cells-tool.test.ts
new file mode 100644
index 0000000000..c11307576c
--- /dev/null
+++ b/frontend/src/core/ai/tools/__tests__/run-cells-tool.test.ts
@@ -0,0 +1,411 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { getDefaultStore } from "jotai";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { MockNotebook } from "@/__mocks__/notebook";
+import { notebookAtom } from "@/core/cells/cells";
+import type { CellId } from "@/core/cells/ids";
+import { RunStaleCellsTool } from "../run-cells-tool";
+
+// Mock runCells
+vi.mock("@/components/editor/cell/useRunCells", () => ({
+ runCells: vi.fn(),
+}));
+
+// Mock getCellContextData
+vi.mock("../../context/providers/cell-output", () => ({
+ getCellContextData: vi.fn(),
+}));
+
+import { runCells } from "@/components/editor/cell/useRunCells";
+import { getCellContextData } from "../../context/providers/cell-output";
+
+describe("RunStaleCellsTool", () => {
+ let store: ReturnType<typeof getDefaultStore>;
+ let tool: RunStaleCellsTool;
+ let cellId1: CellId;
+ let cellId2: CellId;
+ let cellId3: CellId;
+ let toolContext: {
+ addStagedCell: ReturnType<typeof vi.fn>;
+ createNewCell: ReturnType<typeof vi.fn>;
+ prepareForRun: ReturnType<typeof vi.fn>;
+ sendRun: ReturnType<typeof vi.fn>;
+ store: ReturnType<typeof getDefaultStore>;
+ };
+
+ beforeEach(() => {
+ store = getDefaultStore();
+ toolContext = {
+ addStagedCell: vi.fn(),
+ createNewCell: vi.fn(),
+ prepareForRun: vi.fn(),
+ sendRun: vi.fn().mockResolvedValue(null),
+ store,
+ };
+
+ tool = new RunStaleCellsTool();
+
+ cellId1 = "cell-1" as CellId;
+ cellId2 = "cell-2" as CellId;
+ cellId3 = "cell-3" as CellId;
+
+ // Reset mocks
+ vi.clearAllMocks();
+ });
+
+ describe("no stale cells", () => {
+ it("should return success message when no stale cells exist", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: {
+ code: "x = 1",
+ edited: false,
+ lastCodeRun: "x = 1",
+ lastExecutionTime: 100,
+ },
+ },
+ });
+ // Make sure cell is not stale by setting runtime state
+ notebook.cellRuntime[cellId1] = {
+ ...notebook.cellRuntime[cellId1],
+ runElapsedTimeMs: 100 as never,
+ status: "idle",
+ };
+ store.set(notebookAtom, notebook);
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.message).toBe("No stale cells found.");
+ expect(result.cellsToOutput).toBeUndefined();
+ expect(vi.mocked(runCells)).not.toHaveBeenCalled();
+ });
+ });
+
+ describe("running stale cells", () => {
+ it("should run stale cells and return their outputs", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: {
+ code: "x = 1",
+ edited: true,
+ lastCodeRun: null,
+ },
+ [cellId2]: {
+ code: "y = 2",
+ edited: false,
+ lastCodeRun: "y = 2",
+ lastExecutionTime: 100,
+ },
+ [cellId3]: {
+ code: "z = 3",
+ edited: true,
+ lastCodeRun: "z = 2",
+ },
+ },
+ });
+ // Make cellId2 not stale
+ notebook.cellRuntime[cellId2] = {
+ ...notebook.cellRuntime[cellId2],
+ runElapsedTimeMs: 100 as never,
+ status: "idle",
+ };
+ store.set(notebookAtom, notebook);
+
+ // Mock runCells to update cell runtime status
+ vi.mocked(runCells).mockImplementation(async () => {
+ const updatedNotebook = store.get(notebookAtom);
+ updatedNotebook.cellRuntime[cellId1] = {
+ ...updatedNotebook.cellRuntime[cellId1],
+ status: "idle",
+ };
+ updatedNotebook.cellRuntime[cellId3] = {
+ ...updatedNotebook.cellRuntime[cellId3],
+ status: "idle",
+ };
+ store.set(notebookAtom, updatedNotebook);
+ });
+
+ // Mock getCellContextData to return outputs
+ vi.mocked(getCellContextData).mockImplementation((cellId) => {
+ if (cellId === cellId1) {
+ return {
+ cellOutput: {
+ outputType: "text",
+ processedContent: "1",
+ imageUrl: null,
+ output: {},
+ },
+ consoleOutputs: null,
+ cellName: "cell1",
+ } as never;
+ }
+ if (cellId === cellId3) {
+ return {
+ cellOutput: {
+ outputType: "text",
+ processedContent: "3",
+ imageUrl: null,
+ output: {},
+ },
+ consoleOutputs: null,
+ cellName: "cell3",
+ } as never;
+ }
+ return {} as never;
+ });
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.cellsToOutput).toBeDefined();
+ expect(result.cellsToOutput).toHaveProperty(cellId1);
+ expect(result.cellsToOutput).toHaveProperty(cellId3);
+ expect(result.cellsToOutput).not.toHaveProperty(cellId2);
+
+ // Verify runCells was called with correct parameters
+ expect(vi.mocked(runCells)).toHaveBeenCalledWith({
+ cellIds: [cellId1, cellId3],
+ sendRun: toolContext.sendRun,
+ prepareForRun: toolContext.prepareForRun,
+ notebook,
+ });
+ });
+
+ it("should handle cells with no output", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: { code: "x = 1", edited: true },
+ },
+ });
+ store.set(notebookAtom, notebook);
+
+ // Mock runCells
+ vi.mocked(runCells).mockImplementation(async () => {
+ const updatedNotebook = store.get(notebookAtom);
+ updatedNotebook.cellRuntime[cellId1] = {
+ ...updatedNotebook.cellRuntime[cellId1],
+ status: "idle",
+ };
+ store.set(notebookAtom, updatedNotebook);
+ });
+
+ // Mock getCellContextData to return no outputs
+ vi.mocked(getCellContextData).mockReturnValue({
+ cellOutput: null,
+ consoleOutputs: null,
+ cellName: "cell1",
+ } as never);
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.cellsToOutput).toBeDefined();
+ expect(result.cellsToOutput?.[cellId1]).toBeNull();
+ });
+
+ it("should handle cells with console output", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: { code: 'print("hello")', edited: true },
+ },
+ });
+ store.set(notebookAtom, notebook);
+
+ // Mock runCells
+ vi.mocked(runCells).mockImplementation(async () => {
+ const updatedNotebook = store.get(notebookAtom);
+ updatedNotebook.cellRuntime[cellId1] = {
+ ...updatedNotebook.cellRuntime[cellId1],
+ status: "idle",
+ };
+ store.set(notebookAtom, updatedNotebook);
+ });
+
+ // Mock getCellContextData to return console output
+ vi.mocked(getCellContextData).mockReturnValue({
+ cellOutput: null,
+ consoleOutputs: [
+ {
+ outputType: "text",
+ processedContent: "hello",
+ imageUrl: null,
+ output: {},
+ },
+ ],
+ cellName: "cell1",
+ } as never);
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.cellsToOutput).toBeDefined();
+ expect(result.cellsToOutput?.[cellId1]?.consoleOutput).toContain("hello");
+ expect(result.message).toContain("Console output");
+ });
+
+ it("should handle cells with media output", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: { code: "plt.plot()", edited: true },
+ },
+ });
+ store.set(notebookAtom, notebook);
+
+ // Mock runCells
+ vi.mocked(runCells).mockImplementation(async () => {
+ const updatedNotebook = store.get(notebookAtom);
+ updatedNotebook.cellRuntime[cellId1] = {
+ ...updatedNotebook.cellRuntime[cellId1],
+ status: "idle",
+ };
+ store.set(notebookAtom, updatedNotebook);
+ });
+
+ // Mock getCellContextData to return media output
+ vi.mocked(getCellContextData).mockReturnValue({
+ cellOutput: {
+ outputType: "media",
+ processedContent: null,
+ imageUrl: "https://example.com/image.png",
+ output: { mimetype: "image/png" },
+ },
+ consoleOutputs: null,
+ cellName: "cell1",
+ } as never);
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.cellsToOutput).toBeDefined();
+ expect(result.cellsToOutput?.[cellId1]?.cellOutput).toContain(
+ "image/png",
+ );
+ expect(result.cellsToOutput?.[cellId1]?.cellOutput).toContain(
+ "https://example.com/image.png",
+ );
+ });
+
+ it("should handle both cell output and console output", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: { code: 'x = 1; print("debug")', edited: true },
+ },
+ });
+ store.set(notebookAtom, notebook);
+
+ // Mock runCells
+ vi.mocked(runCells).mockImplementation(async () => {
+ const updatedNotebook = store.get(notebookAtom);
+ updatedNotebook.cellRuntime[cellId1] = {
+ ...updatedNotebook.cellRuntime[cellId1],
+ status: "idle",
+ };
+ store.set(notebookAtom, updatedNotebook);
+ });
+
+ // Mock getCellContextData to return both outputs
+ vi.mocked(getCellContextData).mockReturnValue({
+ cellOutput: {
+ outputType: "text",
+ processedContent: "1",
+ imageUrl: null,
+ output: {},
+ },
+ consoleOutputs: [
+ {
+ outputType: "text",
+ processedContent: "debug",
+ imageUrl: null,
+ output: {},
+ },
+ ],
+ cellName: "cell1",
+ } as never);
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.cellsToOutput).toBeDefined();
+ expect(result.cellsToOutput?.[cellId1]?.cellOutput).toContain("1");
+ expect(result.cellsToOutput?.[cellId1]?.consoleOutput).toContain("debug");
+ });
+
+ it("should return success when all stale cells have no output", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: { code: "x = 1", edited: true },
+ [cellId2]: { code: "y = 2", edited: true },
+ },
+ });
+ store.set(notebookAtom, notebook);
+
+ // Mock runCells
+ vi.mocked(runCells).mockImplementation(async () => {
+ const updatedNotebook = store.get(notebookAtom);
+ updatedNotebook.cellRuntime[cellId1] = {
+ ...updatedNotebook.cellRuntime[cellId1],
+ status: "idle",
+ };
+ updatedNotebook.cellRuntime[cellId2] = {
+ ...updatedNotebook.cellRuntime[cellId2],
+ status: "idle",
+ };
+ store.set(notebookAtom, updatedNotebook);
+ });
+
+ // Mock getCellContextData to return no outputs for both cells
+ vi.mocked(getCellContextData).mockReturnValue({
+ cellOutput: null,
+ consoleOutputs: null,
+ cellName: "cell1",
+ } as never);
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.cellsToOutput).toBeDefined();
+ // Both cells should be in the output as null
+ expect(result.cellsToOutput?.[cellId1]).toBeNull();
+ expect(result.cellsToOutput?.[cellId2]).toBeNull();
+ });
+ });
+
+ describe("cell execution completion", () => {
+ it("should complete immediately if cells are already idle", async () => {
+ const notebook = MockNotebook.notebookState({
+ cellData: {
+ [cellId1]: { code: "x = 1", edited: true },
+ },
+ });
+
+ // Set cell runtime to already be idle
+ notebook.cellRuntime[cellId1] = {
+ ...notebook.cellRuntime[cellId1],
+ status: "idle",
+ };
+ store.set(notebookAtom, notebook);
+
+ // Mock runCells to do nothing (cells already idle)
+ vi.mocked(runCells).mockResolvedValue();
+
+ // Mock getCellContextData
+ vi.mocked(getCellContextData).mockReturnValue({
+ cellOutput: {
+ outputType: "text",
+ processedContent: "1",
+ imageUrl: null,
+ output: {},
+ },
+ consoleOutputs: null,
+ cellName: "cell1",
+ } as never);
+
+ const result = await tool.handler({}, toolContext as never);
+
+ expect(result.status).toBe("success");
+ expect(result.cellsToOutput).toBeDefined();
+ });
+ });
+});
diff --git a/frontend/src/core/ai/tools/base.ts b/frontend/src/core/ai/tools/base.ts
index 845943e2ef..7de066dbd1 100644
--- a/frontend/src/core/ai/tools/base.ts
+++ b/frontend/src/core/ai/tools/base.ts
@@ -1,6 +1,11 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { z } from "zod";
+import type { CreateNewCellAction } from "@/core/cells/cells";
+import type { CellId } from "@/core/cells/ids";
+import type { RunRequest } from "@/core/network/types";
+import type { JotaiStore } from "@/core/state/jotai";
+import type { Edit } from "../staged-cells";
import type { CopilotMode } from "./registry";
/**
@@ -53,6 +58,9 @@ export class ToolExecutionError extends Error {
}
}
+// Can be any input
+export type EmptyToolInput = unknown;
+
/**
* Base interface for tool output responses.
* Mirrors the SuccessResult dataclass from marimo/_ai/_tools/types.py
@@ -92,11 +100,23 @@ export interface ToolDescription {
additionalInfo?: string;
}
+/** Utility functions for tools to interact with the notebook */
+export interface ToolNotebookContext {
+ store: JotaiStore;
+ addStagedCell: (payload: { cellId: CellId; edit: Edit }) => void;
+ createNewCell: (payload: CreateNewCellAction) => void;
+ prepareForRun: (payload: { cellId: CellId }) => void;
+ sendRun: (request: RunRequest) => Promise<null>;
+}
+
export interface AiTool<TIn, TOut> {
name: string;
description: ToolDescription;
schema: z.ZodType<TIn>;
outputSchema: z.ZodType<TOut>;
mode: CopilotMode[];
- handler: (args: TIn) => TOut | Promise<TOut>;
+ handler: (
+ args: TIn,
+ toolContext: ToolNotebookContext,
+ ) => TOut | Promise<TOut>;
}
diff --git a/frontend/src/core/ai/tools/edit-notebook-tool.ts b/frontend/src/core/ai/tools/edit-notebook-tool.ts
index f9739e15d5..bccfb477e8 100644
--- a/frontend/src/core/ai/tools/edit-notebook-tool.ts
+++ b/frontend/src/core/ai/tools/edit-notebook-tool.ts
@@ -4,25 +4,18 @@ import type { EditorView } from "@codemirror/view";
import { z } from "zod";
import { scrollAndHighlightCell } from "@/components/editor/links/cell-link";
import {
- createNotebookActions,
type CellPosition as NotebookCellPosition,
type NotebookState,
notebookAtom,
- notebookReducer,
} from "@/core/cells/cells";
import { CellId } from "@/core/cells/ids";
import { updateEditorCodeFromPython } from "@/core/codemirror/language/utils";
-import type { JotaiStore } from "@/core/state/jotai";
import type { CellColumnId } from "@/utils/id-tree";
-import {
- createStagedAICellsActions,
- stagedAICellsAtom,
- stagedAICellsReducer,
-} from "../staged-cells";
import {
type AiTool,
type ToolDescription,
ToolExecutionError,
+ type ToolNotebookContext,
type ToolOutputBase,
toolOutputBaseSchema,
} from "./base";
@@ -93,42 +86,30 @@ export type EditType = EditOperation["type"];
export class EditNotebookTool
implements AiTool<EditNotebookInput, ToolOutputBase>
{
- private readonly store: JotaiStore;
- private readonly notebookActions: ReturnType<typeof createNotebookActions>;
- private readonly stagedAICellsActions: ReturnType<
- typeof createStagedAICellsActions
- >;
readonly name = "edit_notebook_tool";
readonly description = description;
readonly schema = editNotebookSchema;
readonly outputSchema = toolOutputBaseSchema;
readonly mode: CopilotMode[] = ["agent"];
- constructor(store: JotaiStore) {
- this.store = store;
- this.notebookActions = createNotebookActions((action) => {
- this.store.set(notebookAtom, (state) => notebookReducer(state, action));
- });
- this.stagedAICellsActions = createStagedAICellsActions((action) => {
- this.store.set(stagedAICellsAtom, (state) =>
- stagedAICellsReducer(state, action),
- );
- });
- }
+ handler = async (
+ { edit }: EditNotebookInput,
+ toolContext: ToolNotebookContext,
+ ): Promise<ToolOutputBase> => {
+ const { addStagedCell, createNewCell, store } = toolContext;
- handler = async ({ edit }: EditNotebookInput): Promise<ToolOutputBase> => {
switch (edit.type) {
case "update_cell": {
const { cellId, code } = edit;
- const notebook = this.store.get(notebookAtom);
+ const notebook = store.get(notebookAtom);
this.validateCellIdExists(cellId, notebook);
const editorView = this.getCellEditorView(cellId, notebook);
scrollAndHighlightCell(cellId);
const currentCellCode = editorView.state.doc.toString();
- this.stagedAICellsActions.addStagedCell({
+ addStagedCell({
cellId,
edit: { type: "update_cell", previousCode: currentCellCode },
});
@@ -146,7 +127,7 @@ export class EditNotebookTool
const newCellId = CellId.create();
if (typeof position === "object") {
- const notebook = this.store.get(notebookAtom);
+ const notebook = store.get(notebookAtom);
if ("cellId" in position) {
this.validateCellIdExists(position.cellId, notebook);
notebookPosition = position.cellId;
@@ -157,15 +138,14 @@ export class EditNotebookTool
}
}
- this.notebookActions.createNewCell({
+ createNewCell({
cellId: notebookPosition,
before,
code,
newCellId,
});
- // Add to staged AICells
- this.stagedAICellsActions.addStagedCell({
+ addStagedCell({
cellId: newCellId,
edit: { type: "add_cell" },
});
@@ -178,14 +158,14 @@ export class EditNotebookTool
case "delete_cell": {
const { cellId } = edit;
- const notebook = this.store.get(notebookAtom);
+ const notebook = store.get(notebookAtom);
this.validateCellIdExists(cellId, notebook);
const editorView = this.getCellEditorView(cellId, notebook);
const currentCellCode = editorView.state.doc.toString();
// Add to staged AICells - don't actually delete the cell yet
- this.stagedAICellsActions.addStagedCell({
+ addStagedCell({
cellId,
edit: { type: "delete_cell", previousCode: currentCellCode },
});
diff --git a/frontend/src/core/ai/tools/registry.ts b/frontend/src/core/ai/tools/registry.ts
index 72b4b756d7..a3df19345b 100644
--- a/frontend/src/core/ai/tools/registry.ts
+++ b/frontend/src/core/ai/tools/registry.ts
@@ -3,9 +3,13 @@
import type { components } from "@marimo-team/marimo-api";
import { Memoize } from "typescript-memoize";
import { type ZodObject, z } from "zod";
-import { store } from "@/core/state/jotai";
-import { type AiTool, ToolExecutionError } from "./base";
+import {
+ type AiTool,
+ ToolExecutionError,
+ type ToolNotebookContext,
+} from "./base";
import { EditNotebookTool } from "./edit-notebook-tool";
+import { RunStaleCellsTool } from "./run-cells-tool";
import { formatToolDescription } from "./utils";
export type AnyZodObject = ZodObject<z.ZodRawShape>;
@@ -51,6 +55,7 @@ export class FrontendToolRegistry {
async invoke<TName extends string>(
toolName: TName,
rawArgs: unknown,
+ toolContext: ToolNotebookContext,
): Promise<InvokeResult<TName>> {
const tool = this.getToolOrThrow(toolName);
const handler = tool.handler;
@@ -67,7 +72,7 @@ export class FrontendToolRegistry {
const args = inputResponse.data;
// Call the handler
- const rawOutput = await handler(args);
+ const rawOutput = await handler(args, toolContext);
// Parse output
const response = await outputSchema.safeParseAsync(rawOutput);
@@ -125,6 +130,6 @@ export class FrontendToolRegistry {
}
export const FRONTEND_TOOL_REGISTRY = new FrontendToolRegistry([
- new EditNotebookTool(store),
- // ADD MORE TOOLS HERE
+ new EditNotebookTool(),
+ new RunStaleCellsTool(),
]);
diff --git a/frontend/src/core/ai/tools/run-cells-tool.ts b/frontend/src/core/ai/tools/run-cells-tool.ts
new file mode 100644
index 0000000000..2c162292ae
--- /dev/null
+++ b/frontend/src/core/ai/tools/run-cells-tool.ts
@@ -0,0 +1,223 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import type { FileUIPart } from "ai";
+import { z } from "zod";
+import { runCells } from "@/components/editor/cell/useRunCells";
+import { notebookAtom } from "@/core/cells/cells";
+import type { CellId } from "@/core/cells/ids";
+import { staleCellIds } from "@/core/cells/utils";
+import { type JotaiStore, waitFor } from "@/core/state/jotai";
+import {
+ type BaseOutput,
+ getCellContextData,
+} from "../context/providers/cell-output";
+import {
+ type AiTool,
+ type EmptyToolInput,
+ type ToolDescription,
+ type ToolNotebookContext,
+ type ToolOutputBase,
+ toolOutputBaseSchema,
+} from "./base";
+import type { CopilotMode } from "./registry";
+
+interface CellOutput {
+ consoleOutput?: string;
+ // consoleAttachments?: FileUIPart[];
+ cellOutput?: string;
+ // cellAttachments?: FileUIPart[];
+}
+
+// Must use Record instead of Map because Map serializes to JSON as {}
+interface RunStaleCellsOutput extends ToolOutputBase {
+ cellsToOutput?: Record<CellId, CellOutput | null>;
+}
+
+const description: ToolDescription = {
+ baseDescription:
+ "Run cells in the current notebook that are stale. Stale cells are cells that have been edited or newly added. You can run this tool after editing the notebook or when requested. The output of the ran cells will be returned alongside metadata.",
+};
+
+const filePartSchema = z.object({
+ type: z.literal("file"),
+ url: z.string(),
+ mediaType: z.string(),
+}) satisfies z.ZodType<FileUIPart>;
+
+export class RunStaleCellsTool
+ implements AiTool<EmptyToolInput, RunStaleCellsOutput>
+{
+ readonly name = "run_stale_cells_tool";
+ readonly description = description;
+ readonly schema = z.object({});
+ readonly outputSchema = toolOutputBaseSchema.extend({
+ cellsToOutput: z
+ .record(
+ z.string(),
+ z
+ .object({
+ consoleOutput: z.string().optional(),
+ cellOutput: z.string().optional(),
+ consoleAttachments: z.array(filePartSchema).optional(),
+ cellAttachments: z.array(filePartSchema).optional(),
+ })
+ .nullable(),
+ )
+ .optional(),
+ }) satisfies z.ZodType<RunStaleCellsOutput>;
+ readonly mode: CopilotMode[] = ["agent"];
+
+ handler = async (
+ _args: EmptyToolInput,
+ toolContext: ToolNotebookContext,
+ ): Promise<RunStaleCellsOutput> => {
+ const { prepareForRun, sendRun, store } = toolContext;
+
+ const notebook = store.get(notebookAtom);
+ const staleCells = staleCellIds(notebook);
+
+ if (staleCells.length === 0) {
+ return {
+ status: "success",
+ message: "No stale cells found.",
+ };
+ }
+
+ await runCells({
+ cellIds: staleCells,
+ sendRun: sendRun,
+ prepareForRun,
+ notebook,
+ });
+
+ // Wait for all cells to finish executing
+ const allCellsFinished = await this.waitForCellsToFinish(store, staleCells);
+ if (!allCellsFinished) {
+ return {
+ status: "success",
+ message:
+ "No output was returned because some cells have not finished executing",
+ };
+ }
+
+ // Get notebook state after cells have finished
+ const updatedNotebook = store.get(notebookAtom);
+
+ const cellsToOutput = new Map<CellId, CellOutput | null>();
+ let resultMessage = "";
+
+ for (const cellId of staleCells) {
+ const cellContextData = getCellContextData(cellId, updatedNotebook, {
+ includeConsoleOutput: true,
+ });
+
+ let cellOutputString: string | undefined;
+ // let cellAttachments: FileUIPart[] | undefined;
+ let consoleOutputString: string | undefined;
+ // let consoleAttachments: FileUIPart[] | undefined;
+
+ const cellOutput = cellContextData.cellOutput;
+ const consoleOutputs = cellContextData.consoleOutputs;
+ if (!cellOutput && !consoleOutputs) {
+ // Set null to show no output
+ cellsToOutput.set(cellId, null);
+ continue;
+ }
+
+ if (cellOutput) {
+ cellOutputString = this.formatOutputString(cellOutput);
+ // cellAttachments = await getAttachmentsForOutputs(
+ // [cellOutput],
+ // cellId,
+ // cellContextData.cellName,
+ // );
+ }
+
+ if (consoleOutputs) {
+ // consoleAttachments = await getAttachmentsForOutputs(
+ // consoleOutputs,
+ // cellId,
+ // cellContextData.cellName,
+ // );
+ consoleOutputString = consoleOutputs
+ .map((output) => this.formatOutputString(output))
+ .join("\n");
+ resultMessage +=
+ "Console output represents the stdout or stderr of the cell (eg. print statements).";
+ }
+
+ cellsToOutput.set(cellId, {
+ cellOutput: cellOutputString,
+ consoleOutput: consoleOutputString,
+ // cellAttachments: cellAttachments,
+ // consoleAttachments: consoleAttachments,
+ });
+ }
+
+ if (cellsToOutput.size === 0) {
+ return {
+ status: "success",
+ message: "Stale cells have been run. No output was returned.",
+ };
+ }
+
+ return {
+ status: "success",
+ cellsToOutput: Object.fromEntries(cellsToOutput),
+ message: resultMessage === "" ? undefined : resultMessage,
+ };
+ };
+
+ private formatOutputString(cellOutput: BaseOutput): string {
+ let outputString = "";
+ const { outputType, processedContent, imageUrl, output } = cellOutput;
+ if (outputType === "text" && processedContent) {
+ outputString += `Output:\n${processedContent}`;
+ } else if (outputType === "media") {
+ outputString += `Media Output: Contains ${output.mimetype} content`;
+ if (imageUrl) {
+ outputString += `\nImage URL: ${imageUrl}`;
+ }
+ }
+ return outputString;
+ }
+
+ /**
+ * Wait for cells to finish executing (status becomes "idle")
+ * Returns true if all cells finished executing, false if the timeout was reached
+ */
+ private async waitForCellsToFinish(
+ store: JotaiStore,
+ cellIds: CellId[],
+ timeout = 30_000,
+ ): Promise<boolean> {
+ const checkAllFinished = (
+ notebook: ReturnType<typeof notebookAtom.read>,
+ ) => {
+ return cellIds.every((cellId) => {
+ const cellRuntime = notebook.cellRuntime[cellId];
+ return (
+ cellRuntime?.status !== "running" && cellRuntime?.status !== "queued"
+ );
+ });
+ };
+
+ // If already finished, return immediately
+ if (checkAllFinished(store.get(notebookAtom))) {
+ return true;
+ }
+
+ // Wait for notebook state changes with timeout
+ try {
+ await Promise.race([
+ waitFor(notebookAtom, checkAllFinished),
+ new Promise<never>((_, reject) =>
+ setTimeout(() => reject(new Error("timeout")), timeout),
+ ),
+ ]);
+ return true;
+ } catch {
+ return false;
+ }
+ }
+}