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.

Visibility for @ in Generate with AI, also add model dropdown (#6300)

Shahmir Varqha6f0bd7431218
diff --git a/frontend/src/components/ai/ai-model-dropdown.tsx b/frontend/src/components/ai/ai-model-dropdown.tsx
index 830eff09c1..62e200a1a2 100644
--- a/frontend/src/components/ai/ai-model-dropdown.tsx
+++ b/frontend/src/components/ai/ai-model-dropdown.tsx
@@ -10,6 +10,7 @@ import {
CircleHelpIcon,
} from "lucide-react";
import React from "react";
+import { type SupportedRole, useModelChange } from "@/core/ai/config";
import {
AiModelId,
isKnownAIProvider,
@@ -36,12 +37,13 @@ import { getCurrentRoleTooltip, getTagColour } from "./display-helpers";
interface AIModelDropdownProps {
value?: string;
placeholder?: string;
- onSelect: (modelId: QualifiedModelId) => void;
+ onSelect?: (modelId: QualifiedModelId) => void;
triggerClassName?: string;
customDropdownContent?: React.ReactNode;
iconSize?: "medium" | "small";
showAddCustomModelDocs?: boolean;
- forRole?: Role;
+ displayIconOnly?: boolean;
+ forRole: SupportedRole;
}
export const AIModelDropdown = ({
@@ -53,12 +55,13 @@ export const AIModelDropdown = ({
iconSize = "medium",
showAddCustomModelDocs = false,
forRole,
+ displayIconOnly = false,
}: AIModelDropdownProps) => {
- const currentValue = value ? AiModelId.parse(value) : undefined;
const [isOpen, setIsOpen] = React.useState(false);
const ai = useAtomValue(aiAtom);
const completion = useAtomValue(completionAtom);
+ const { saveModelChange } = useModelChange();
// Only include autocompleteModel if copilot is set to "custom"
const autocompleteModel =
@@ -88,6 +91,13 @@ export const AIModelDropdown = ({
? ai?.models?.edit_model
: undefined;
+ // If value is provided, use it, otherwise use the active model
+ const currentValue = value
+ ? AiModelId.parse(value)
+ : activeModel
+ ? AiModelId.parse(activeModel)
+ : undefined;
+
const iconSizeClass = iconSize === "medium" ? "h-4 w-4" : "h-3 w-3";
const renderModelWithRole = (modelId: AiModelId, role: Role) => {
@@ -119,7 +129,11 @@ export const AIModelDropdown = ({
};
const handleSelect = (modelId: QualifiedModelId) => {
- onSelect(modelId);
+ if (onSelect) {
+ onSelect(modelId);
+ } else {
+ saveModelChange(modelId, forRole);
+ }
setIsOpen(false);
};
@@ -136,11 +150,13 @@ export const AIModelDropdown = ({
provider={currentValue.providerId}
className={iconSizeClass}
/>
- <span className="truncate">
- {isKnownAIProvider(currentValue.providerId)
- ? currentValue.shortModelId
- : currentValue.id}
- </span>
+ {displayIconOnly ? null : (
+ <span className="truncate">
+ {isKnownAIProvider(currentValue.providerId)
+ ? currentValue.shortModelId
+ : currentValue.id}
+ </span>
+ )}
</>
) : (
<span className="text-muted-foreground truncate">
diff --git a/frontend/src/components/app-config/ai-config.tsx b/frontend/src/components/app-config/ai-config.tsx
index 4e0cfdba0b..74de26c053 100644
--- a/frontend/src/components/app-config/ai-config.tsx
+++ b/frontend/src/components/app-config/ai-config.tsx
@@ -31,6 +31,7 @@ import { Input } from "@/components/ui/input";
import { Kbd } from "@/components/ui/kbd";
import { NativeSelect } from "@/components/ui/native-select";
import { Textarea } from "@/components/ui/textarea";
+import type { SupportedRole } from "@/core/ai/config";
import {
AiModelId,
PROVIDERS,
@@ -225,6 +226,7 @@ interface ModelSelectorProps {
description?: React.ReactNode;
disabled?: boolean;
label: string;
+ forRole: SupportedRole;
onSubmit: (values: UserConfig) => void;
}
@@ -237,6 +239,7 @@ export const ModelSelector: React.FC<ModelSelectorProps> = ({
description,
disabled = false,
label,
+ forRole,
onSubmit,
}) => {
return (
@@ -289,6 +292,7 @@ export const ModelSelector: React.FC<ModelSelectorProps> = ({
</div>
</>
}
+ forRole={forRole}
/>
</FormControl>
<FormMessage />
@@ -420,6 +424,7 @@ const renderCopilotProvider = ({
testId="custom-model-input"
description="Model to use for code completion when using a custom provider."
onSubmit={onSubmit}
+ forRole="autocomplete"
/>
);
}
@@ -905,6 +910,7 @@ export const AiAssistConfig: React.FC<AiConfigProps> = ({
description={
<span>Model to use for chat conversations in the Chat panel.</span>
}
+ forRole="chat"
onSubmit={onSubmit}
/>
<ModelSelector
@@ -921,6 +927,7 @@ export const AiAssistConfig: React.FC<AiConfigProps> = ({
<Kbd className="inline">Generate with AI</Kbd> button.
</span>
}
+ forRole="edit"
onSubmit={onSubmit}
/>
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index a678405601..e112e9d359 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -2,7 +2,6 @@
import type { UIMessage } from "@ai-sdk/react";
import { useChat } from "@ai-sdk/react";
-import { startCompletion } from "@codemirror/autocomplete";
import { storePrompt } from "@marimo-team/codemirror-ai";
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { DefaultChatTransport, type ToolUIPart } from "ai";
@@ -57,7 +56,11 @@ 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";
-import { getAICompletionBodyWithAttachments } from "../editor/ai/completion-utils";
+import {
+ addContextCompletion,
+ CONTEXT_TRIGGER,
+ getAICompletionBodyWithAttachments,
+} from "../editor/ai/completion-utils";
import { PanelEmptyState } from "../editor/chrome/panels/empty-state";
import { CopyClipboardIcon } from "../icons/copy-icon";
import { Input } from "../ui/input";
@@ -76,8 +79,6 @@ import { ToolCallAccordion } from "./tool-call-accordion";
// Default mode for the AI
const DEFAULT_MODE = "manual";
-const CONTEXT_TRIGGER = "@";
-
// We need to modify the backend to support attachments for other providers
// And other types
const PROVIDERS_THAT_SUPPORT_ATTACHMENTS = new Set<ProviderId>([
@@ -337,7 +338,7 @@ const ChatInputFooter: React.FC<ChatInputFooterProps> = memo(
const currentModel = ai?.models?.chat_model || DEFAULT_AI_MODEL;
const currentProvider = AiModelId.parse(currentModel).providerId;
- const { saveModeChange, saveModelChange } = useModelChange();
+ const { saveModeChange } = useModelChange();
const modeOptions = [
{
@@ -387,9 +388,7 @@ const ChatInputFooter: React.FC<ChatInputFooterProps> = memo(
</Select>
</FeatureFlagged>
<AIModelDropdown
- value={currentModel}
placeholder="Model"
- onSelect={(model) => saveModelChange(model, "chat")}
triggerClassName="h-6 text-xs shadow-none! ring-0! bg-muted hover:bg-muted/30 rounded-sm"
iconSize="small"
showAddCustomModelDocs={true}
@@ -503,28 +502,7 @@ const ChatInput: React.FC<ChatInputProps> = memo(
</div>
<ChatInputFooter
isEmpty={!input.trim()}
- onAddContext={() => {
- if (!inputRef.current?.view) {
- return;
- }
- const pos = inputRef.current.view.state.selection.main.from;
- // Insert @ at the cursor position
- inputRef.current.view.dispatch({
- changes: {
- from: pos,
- to: pos,
- insert: CONTEXT_TRIGGER,
- },
- selection: {
- anchor: pos + 1,
- head: pos + 1,
- },
- });
- // Focus the view
- inputRef.current.view.focus();
- // Start completion
- startCompletion(inputRef.current.view);
- }}
+ onAddContext={() => addContextCompletion(inputRef)}
onSendClick={handleSendClick}
isLoading={isLoading}
onStop={onStop}
@@ -848,7 +826,7 @@ const ChatPanelBody = () => {
const chatInput = isNewThread ? (
<ChatInput
key="new-thread-input"
- placeholder="Ask anything, @ to include context about tables or dataframes"
+ placeholder={`Ask anything, ${CONTEXT_TRIGGER} to include context about tables or dataframes`}
input={newThreadInput}
inputRef={newThreadInputRef}
inputClassName="px-1 py-0"
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 449419dd07..91e2f63e90 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -17,7 +17,7 @@ import ReactCodeMirror, {
minimalSetup,
type ReactCodeMirrorRef,
} from "@uiw/react-codemirror";
-import { useAtom, useAtomValue, useStore } from "jotai";
+import { useAtom, useStore } from "jotai";
import { atomWithStorage } from "jotai/utils";
import {
ChevronsUpDown,
@@ -40,12 +40,9 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { toast } from "@/components/ui/use-toast";
-import { useModelChange } from "@/core/ai/config";
import { resourceExtension } from "@/core/codemirror/ai/resources";
import { customPythonLanguageSupport } from "@/core/codemirror/language/languages/python";
import { SQLLanguageAdapter } from "@/core/codemirror/language/languages/sql/sql";
-import { aiAtom } from "@/core/config/config";
-import { DEFAULT_AI_MODEL } from "@/core/config/config-schema";
import { useRuntimeManager } from "@/core/runtime/config";
import { useTheme } from "@/theme/useTheme";
import { cn } from "@/utils/cn";
@@ -58,6 +55,7 @@ import {
createAiCompletionOnKeydown,
} from "./completion-handlers";
import {
+ CONTEXT_TRIGGER,
getAICompletionBody,
mentionsCompletionSource,
} from "./completion-utils";
@@ -90,9 +88,6 @@ export const AddCellWithAI: React.FC<{
const { theme } = useTheme();
const runtimeManager = useRuntimeManager();
- const ai = useAtomValue(aiAtom);
- const editModel = ai?.models?.edit_model || DEFAULT_AI_MODEL;
- const { saveModelChange } = useModelChange();
const inputRef = useRef<ReactCodeMirrorRef>(null);
const {
@@ -261,10 +256,6 @@ export const AddCellWithAI: React.FC<{
<div className="ml-auto flex items-center gap-1">
{languageDropdown}
<AIModelDropdown
- value={editModel}
- onSelect={(model) => {
- saveModelChange(model, "edit");
- }}
triggerClassName="h-7 text-xs max-w-64"
iconSize="small"
forRole="edit"
@@ -437,7 +428,9 @@ export const PromptInput = ({
onChange={onChange}
onKeyDown={onKeyDown}
theme={theme === "dark" ? "dark" : "light"}
- placeholder={placeholder || "Generate with AI"}
+ placeholder={
+ placeholder || `Generate with AI, ${CONTEXT_TRIGGER} to include context`
+ }
/>
);
};
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index d61c8df8b6..0b8d7d7476 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -2,7 +2,7 @@
import { useCompletion } from "@ai-sdk/react";
import { EditorView } from "@codemirror/view";
-import { Loader2Icon, SparklesIcon, XIcon } from "lucide-react";
+import { AtSignIcon, Loader2Icon, SparklesIcon, XIcon } from "lucide-react";
import React, { useEffect, useId, useState } from "react";
import CodeMirrorMerge from "react-codemirror-merge";
import { Button } from "@/components/ui/button";
@@ -12,6 +12,7 @@ import "./merge-editor.css";
import { storePrompt } from "@marimo-team/codemirror-ai";
import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { useAtom } from "jotai";
+import { AIModelDropdown } from "@/components/ai/ai-model-dropdown";
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";
import { Tooltip } from "@/components/ui/tooltip";
@@ -30,7 +31,7 @@ import {
CompletionActions,
createAiCompletionOnKeydown,
} from "./completion-handlers";
-import { getAICompletionBody } from "./completion-utils";
+import { addContextCompletion, getAICompletionBody } from "./completion-utils";
const Original = CodeMirrorMerge.Original;
const Modified = CodeMirrorMerge.Modified;
@@ -164,7 +165,7 @@ export const AiCompletionEditor: React.FC<Props> = ({
<SparklesIcon className="text-(--blue-10) shrink-0" size={16} />
<PromptInput
inputRef={inputRef}
- className="h-full my-0 py-1.5"
+ className="h-full my-0 py-2"
onClose={() => {
declineChange();
setCompletion("");
@@ -209,6 +210,24 @@ export const AiCompletionEditor: React.FC<Props> = ({
size="xs"
/>
)}
+ <div className="flex flex-row items-center gap-0.5 -ml-1.5 -mr-2">
+ <Tooltip content="Add context">
+ <Button
+ variant="text"
+ size="icon"
+ onClick={() => addContextCompletion(inputRef)}
+ >
+ <AtSignIcon className="h-3 w-3" />
+ </Button>
+ </Tooltip>
+ <AIModelDropdown
+ triggerClassName="h-7"
+ iconSize="small"
+ displayIconOnly={true}
+ forRole="edit"
+ />
+ </div>
+
<div className="h-full w-px bg-border mx-2" />
<Tooltip content="Include code from other cells">
<div className="flex flex-row items-start gap-1 overflow-hidden">
diff --git a/frontend/src/components/editor/ai/completion-utils.ts b/frontend/src/components/editor/ai/completion-utils.ts
index 8526b98731..8be0a6a1bd 100644
--- a/frontend/src/components/editor/ai/completion-utils.ts
+++ b/frontend/src/components/editor/ai/completion-utils.ts
@@ -1,10 +1,12 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import type {
- Completion,
- CompletionContext,
- CompletionSource,
+import {
+ type Completion,
+ type CompletionContext,
+ type CompletionSource,
+ startCompletion,
} from "@codemirror/autocomplete";
+import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import type { FileUIPart } from "ai";
import { getAIContextRegistry } from "@/core/ai/context/context";
import { getCodes } from "@/core/codemirror/copilot/getCodes";
@@ -12,6 +14,8 @@ import type { AiCompletionRequest } from "@/core/network/types";
import { store } from "@/core/state/jotai";
import { Logger } from "@/utils/Logger";
+export const CONTEXT_TRIGGER = "@";
+
interface Opts {
input: string;
}
@@ -118,3 +122,26 @@ export function mentionsCompletionSource(
};
};
}
+
+export function addContextCompletion(
+ inputRef: React.RefObject<ReactCodeMirrorRef | null>,
+) {
+ if (inputRef.current?.view) {
+ const pos = inputRef.current.view.state.selection.main.from;
+ // Insert @ at the cursor position
+ inputRef.current.view.dispatch({
+ changes: {
+ from: pos,
+ to: pos,
+ insert: CONTEXT_TRIGGER,
+ },
+ selection: {
+ anchor: pos + 1,
+ head: pos + 1,
+ },
+ });
+ inputRef.current.view.focus();
+ // Trigger completion
+ startCompletion(inputRef.current.view);
+ }
+}
diff --git a/frontend/src/core/ai/config.ts b/frontend/src/core/ai/config.ts
index ec69ad0d9e..d7f1abde91 100644
--- a/frontend/src/core/ai/config.ts
+++ b/frontend/src/core/ai/config.ts
@@ -8,7 +8,7 @@ import type { AIModelKey, UserConfig } from "@/core/config/config-schema";
import { useRequestClient } from "@/core/network/requests";
// Extract only the supported roles from the Role type
-type SupportedRole = Extract<Role, "chat" | "autocomplete" | "edit">;
+export type SupportedRole = Extract<Role, "chat" | "autocomplete" | "edit">;
const getModelKeyForRole = (forRole: SupportedRole): AIModelKey | null => {
switch (forRole) {