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.

3.7 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import type { UIMessage } from "@ai-sdk/react";
import type { FileUIPart } from "ai";
import { atom } from "jotai";
import { atomWithStorage } from "jotai/utils";
import { toast } from "@/components/ui/use-toast";
import { uniqueBy } from "@/utils/arrays";
import { Logger } from "@/utils/Logger";
import { adaptForLocalStorage, jotaiJsonStorage } from "@/utils/storage/jotai";
import type { TypedString } from "@/utils/typed";
import type { CellId } from "../cells/ids";
import type { ChatOptions } from "../network/types";

const KEY = "marimo:ai:chatState:v5";

export type ChatId = TypedString<"ChatId">;

export interface AiCompletionCell {
  cellId: CellId;
  initialPrompt?: string;
  triggerImmediately?: boolean;
}

export const aiCompletionCellAtom = atom<AiCompletionCell | null>(null);

/**
 * A prompt queued to be delivered to the AI assistant panel.
 * The active panel consumes this on mount/when ready, then clears it.
 */
export interface PendingAiPrompt {
  prompt: string;
  submit: boolean;
}

export const pendingAiPromptAtom = atom<PendingAiPrompt | null>(null);

const CHAT_OPTIONS_KEY = "marimo:ai:chatOptions";
export const chatOptionsAtom = atomWithStorage<Required<ChatOptions>>(
  CHAT_OPTIONS_KEY,
  {
    webSearch: false,
  },
  jotaiJsonStorage,
);

const INCLUDE_OTHER_CELLS_KEY = "marimo:ai:includeOtherCells";
export const includeOtherCellsAtom = atomWithStorage<boolean>(
  INCLUDE_OTHER_CELLS_KEY,
  true,
  jotaiJsonStorage,
);

export interface Message {
  id: string;
  role: "user" | "assistant" | "data" | "system";
  content: string;
  timestamp: number;
  parts?: UIMessage["parts"];
  attachments?: FileUIPart[];
}

export interface Chat {
  id: ChatId;
  title: string;
  messages: UIMessage[];
  createdAt: number;
  updatedAt: number;
}

export interface ChatState {
  chats: Map<ChatId, Chat>;
  activeChatId: ChatId | null;
}

function removeEmptyChats(chatState: Map<ChatId, Chat>): Map<ChatId, Chat> {
  const result = new Map<ChatId, Chat>();

  // Dedupe messages with the same id
  for (const [chatId, chat] of chatState.entries()) {
    if (chat.messages.length === 0) {
      continue;
    }
    const dedupedMessages = uniqueBy(chat.messages, (message) => message.id);
    result.set(chatId, { ...chat, messages: dedupedMessages });
  }
  return result;
}

const chatStorage = adaptForLocalStorage({
  toSerializable: (value: ChatState) => ({
    chats: [...removeEmptyChats(value.chats).entries()],
    activeChatId: value.activeChatId,
  }),
  fromSerializable: (value) => ({
    chats: new Map(value.chats),
    activeChatId: value.activeChatId,
  }),
});

export const chatStateAtom = atomWithStorage<ChatState>(
  KEY,
  {
    chats: new Map(),
    activeChatId: null,
  },
  {
    ...chatStorage,
    setItem: (key, value) => {
      try {
        // Removing the key frees space even when no further writes can fit.
        if (removeEmptyChats(value.chats).size === 0) {
          chatStorage.removeItem(key);
        } else {
          chatStorage.setItem(key, value);
        }
      } catch (error) {
        Logger.warn("Could not save chat history", error);
        toast({
          id: "chat-storage-error",
          once: true,
          title: "Chat history couldn't be saved",
          description:
            "Try deleting old chats. New messages may be lost when you reload.",
          variant: "danger",
        });
      }
    },
  },
);

export const activeChatAtom = atom(
  (get) => {
    const state = get(chatStateAtom);
    if (!state.activeChatId) {
      return null;
    }
    return state.chats.get(state.activeChatId);
  },
  (_get, set, chatId: ChatId | null) => {
    set(chatStateAtom, (prev) => ({
      ...prev,
      activeChatId: chatId,
    }));
  },
);