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: update value when clearing chat messages (#3729)

devin-ai-integration[bot]71fc68d0f4e9
diff --git a/frontend/src/plugins/impl/chat/ChatPlugin.tsx b/frontend/src/plugins/impl/chat/ChatPlugin.tsx
index 389ab4c6ea..7556b8f66d 100644
--- a/frontend/src/plugins/impl/chat/ChatPlugin.tsx
+++ b/frontend/src/plugins/impl/chat/ChatPlugin.tsx
@@ -13,7 +13,9 @@ type PluginFunctions = {
send_prompt: (req: SendMessageRequest) => Promise<string>;
};
-export const ChatPlugin = createPlugin<ChatMessage[]>("marimo-chatbot")
+export const ChatPlugin = createPlugin<{ messages: ChatMessage[] }>(
+ "marimo-chatbot",
+)
.withData(
z.object({
prompts: z.array(z.string()).default(Arrays.EMPTY),
@@ -80,8 +82,8 @@ export const ChatPlugin = createPlugin<ChatMessage[]>("marimo-chatbot")
allowAttachments={props.data.allowAttachments}
config={props.data.config}
sendPrompt={props.functions.send_prompt}
- value={props.value || Arrays.EMPTY}
- setValue={props.setValue}
+ value={props.value?.messages || Arrays.EMPTY}
+ setValue={(messages) => props.setValue({ messages })}
/>
</TooltipProvider>
));
diff --git a/frontend/src/plugins/impl/chat/chat-ui.tsx b/frontend/src/plugins/impl/chat/chat-ui.tsx
index 0322529be0..5a77990877 100644
--- a/frontend/src/plugins/impl/chat/chat-ui.tsx
+++ b/frontend/src/plugins/impl/chat/chat-ui.tsx
@@ -232,7 +232,14 @@ export const Chatbot: React.FC<Props> = (props) => {
style={{ maxHeight: props.maxHeight }}
>
<div className="absolute top-0 right-0 flex justify-end z-10 border border-[var(--slate-6)] bg-inherit rounded-bl-lg">
- <Button variant="text" size="icon" onClick={() => setMessages([])}>
+ <Button
+ variant="text"
+ size="icon"
+ onClick={() => {
+ setMessages([]);
+ props.setValue([]);
+ }}
+ >
<Trash2Icon className="h-3 w-3" />
</Button>
</div>
diff --git a/marimo/_smoke_tests/chat/chatbot.py b/marimo/_smoke_tests/chat/chatbot.py
index 092a13903e..4d1a9bdfc2 100644
--- a/marimo/_smoke_tests/chat/chatbot.py
+++ b/marimo/_smoke_tests/chat/chatbot.py
@@ -246,5 +246,18 @@ def _(cars, get_sample_prompts, mo, my_complex_model):
return (prompts,)
+def _(mo):
+ no_bot = mo.ui.chat(model=lambda x: "no")
+ no_bot
+ return (no_bot,)
+
+
+def _(no_bot):
+ no_bot.value
+ return
+
+
if __name__ == "__main__":
app.run()
diff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.py
index 737feaba6a..16a946ab9e 100644
--- a/tests/_plugins/ui/_impl/chat/test_chat.py
+++ b/tests/_plugins/ui/_impl/chat/test_chat.py
@@ -220,6 +220,22 @@ def test_chat_with_show_configuration_controls():
assert chat._component_args["show-configuration-controls"] is True
+async def test_chat_clear_messages():
+ def mock_model(
+ messages: List[ChatMessage], config: ChatModelConfig
+ ) -> str:
+ del messages, config
+ return "Mock response"
+
+ chat = ui.chat(mock_model)
+ chat._chat_history = [
+ ChatMessage(role="user", content="Hello"),
+ ChatMessage(role="assistant", content="Hi there!"),
+ ]
+
+ assert chat._convert_value({"messages": []}) == []
+
+
async def test_chat_send_message_enqueues_ui_element_request(
k: Kernel, exec_req: ExecReqProvider
) -> None: