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.

fix: ai edit cell when there is existing text (#4429)

Myles Scolnick297476956446
diff --git a/marimo/_messaging/ops.py b/marimo/_messaging/ops.py
index 28982266e2..6d3cf579e7 100644
--- a/marimo/_messaging/ops.py
+++ b/marimo/_messaging/ops.py
@@ -323,7 +323,7 @@ class CellOp(Op):
stream: Stream | None = None,
) -> None:
status: Optional[TopLevelHints] = serialization.hint
- CellOp(cell_id=cell_id, serialization=status).broadcast(stream)
+ CellOp(cell_id=cell_id, serialization=str(status)).broadcast(stream)
@dataclass
diff --git a/marimo/_server/ai/prompts.py b/marimo/_server/ai/prompts.py
index c884a1e72a..cc629ac732 100644
--- a/marimo/_server/ai/prompts.py
+++ b/marimo/_server/ai/prompts.py
@@ -103,6 +103,11 @@ def get_refactor_or_insert_notebook_cell_system_prompt(
assert "<insert_here>" in cell_code
system_prompt += "The point you'll need to insert at is marked with <insert_here></insert_here>.\n"
else:
+ # If no selected text and <rewrite_this> is not present, we need to
+ # wrap the whole code in <rewrite_this> tags
+ if "<rewrite_this>" not in cell_code and not selected_text:
+ cell_code = f"<rewrite_this>\n{cell_code}\n</rewrite_this>"
+
assert "<rewrite_this>" in cell_code
system_prompt += "The section you'll need to rewrite is marked with <rewrite_this></rewrite_this> tags.\n"
diff --git a/tests/_messaging/test_ops.py b/tests/_messaging/test_ops.py
index f97fad3216..8d26a435e9 100644
--- a/tests/_messaging/test_ops.py
+++ b/tests/_messaging/test_ops.py
@@ -1,9 +1,14 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from marimo._messaging.ops import VariableValue
+from unittest.mock import MagicMock
+
+from marimo._ast.toplevel import HINT_UNPARSABLE, TopLevelStatus
+from marimo._messaging.ops import CellOp, VariableValue
from marimo._output.hypertext import Html
from marimo._plugins.ui._impl.input import slider
+from marimo._types.ids import CellId_t
+from marimo._utils.parse_dataclass import parse_raw
def test_value_ui_element() -> None:
@@ -28,3 +33,22 @@ def test_variable_value_broken_str() -> None:
assert variable_value.datatype == "Broken"
assert variable_value.value is not None
assert variable_value.value.startswith("<Broken object at")
+
+
+def test_broadcast_serialization() -> None:
+ cell_id = CellId_t("test_cell_id")
+
+ stream = MagicMock()
+ stream.write = MagicMock()
+ status = MagicMock(TopLevelStatus)
+ status.hint = HINT_UNPARSABLE
+
+ CellOp.broadcast_serialization(
+ cell_id=cell_id, serialization=status, stream=stream
+ )
+
+ stream.write.assert_called_once()
+ cell_op = stream.write.call_args.kwargs["data"]
+ assert cell_op["serialization"] == str(HINT_UNPARSABLE)
+
+ assert isinstance(parse_raw(cell_op, CellOp), CellOp)