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.

feat: Reference files as context with # (#6241)

Myles Scolnick543b3178261f
diff --git a/frontend/src/__mocks__/requests.ts b/frontend/src/__mocks__/requests.ts
index 6f65581354..aa9a50a289 100644
--- a/frontend/src/__mocks__/requests.ts
+++ b/frontend/src/__mocks__/requests.ts
@@ -44,6 +44,9 @@ export const MockRequestClient = {
getUsageStats: vi.fn().mockResolvedValue({}),
sendPdb: vi.fn().mockResolvedValue({}),
sendListFiles: vi.fn().mockResolvedValue({ files: [] }),
+ sendSearchFiles: vi
+ .fn()
+ .mockResolvedValue({ files: [], query: "", total_found: 0 }),
sendCreateFileOrFolder: vi.fn().mockResolvedValue({}),
sendDeleteFileOrFolder: vi.fn().mockResolvedValue({}),
sendRenameFileOrFolder: vi.fn().mockResolvedValue({}),
diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsx
index a9288d19c4..1e202f8416 100644
--- a/frontend/src/components/chat/chat-panel.tsx
+++ b/frontend/src/components/chat/chat-panel.tsx
@@ -2,6 +2,7 @@
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 {
@@ -11,6 +12,7 @@ import {
} from "ai";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import {
+ AtSignIcon,
BotMessageSquareIcon,
ClockIcon,
Loader2,
@@ -78,6 +80,8 @@ 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>([
@@ -313,8 +317,9 @@ interface ChatInputFooterProps {
onSendClick: () => void;
isLoading: boolean;
onStop: () => void;
- handleFileChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
+ onAddFiles: (files: File[]) => void;
fileInputRef: React.RefObject<HTMLInputElement | null>;
+ onAddContext: () => void;
}
const ChatInputFooter: React.FC<ChatInputFooterProps> = memo(
@@ -324,7 +329,8 @@ const ChatInputFooter: React.FC<ChatInputFooterProps> = memo(
isLoading,
onStop,
fileInputRef,
- handleFileChange,
+ onAddFiles,
+ onAddContext,
}) => {
const ai = useAtomValue(aiAtom);
const currentMode = ai?.mode || DEFAULT_MODE;
@@ -351,82 +357,97 @@ const ChatInputFooter: React.FC<ChatInputFooterProps> = memo(
PROVIDERS_THAT_SUPPORT_ATTACHMENTS.has(currentProvider);
return (
- <div className="px-3 py-2 border-t border-border/20 flex flex-row items-center justify-between">
- <div className="flex items-center gap-2">
- <FeatureFlagged feature="mcp_docs">
- <Select value={currentMode} onValueChange={saveModeChange}>
- <SelectTrigger className="h-6 text-xs border-border shadow-none! ring-0! bg-muted hover:bg-muted/30 py-0 px-2 gap-1 capitalize">
- {currentMode}
- </SelectTrigger>
- <SelectContent>
- <SelectGroup>
- <SelectLabel>AI Mode</SelectLabel>
- {modeOptions.map((option) => (
- <SelectItem
- key={option.value}
- value={option.value}
- className="text-xs"
- >
- <div className="flex flex-col">
- {option.label}
- <div className="text-muted-foreground text-xs pt-1 block">
- {option.subtitle}
+ <TooltipProvider>
+ <div className="px-3 py-2 border-t border-border/20 flex flex-row items-center justify-between">
+ <div className="flex items-center gap-2">
+ <FeatureFlagged feature="mcp_docs">
+ <Select value={currentMode} onValueChange={saveModeChange}>
+ <SelectTrigger className="h-6 text-xs border-border shadow-none! ring-0! bg-muted hover:bg-muted/30 py-0 px-2 gap-1 capitalize">
+ {currentMode}
+ </SelectTrigger>
+ <SelectContent>
+ <SelectGroup>
+ <SelectLabel>AI Mode</SelectLabel>
+ {modeOptions.map((option) => (
+ <SelectItem
+ key={option.value}
+ value={option.value}
+ className="text-xs"
+ >
+ <div className="flex flex-col">
+ {option.label}
+ <div className="text-muted-foreground text-xs pt-1 block">
+ {option.subtitle}
+ </div>
</div>
- </div>
- </SelectItem>
- ))}
- </SelectGroup>
- </SelectContent>
- </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}
- forRole="chat"
- />
- </div>
- <div className="flex flex-row">
- {isAttachmentSupported && (
- <>
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </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}
+ forRole="chat"
+ />
+ </div>
+ <div className="flex flex-row">
+ <Tooltip content="Add context">
+ <Button variant="text" size="icon" onClick={onAddContext}>
+ <AtSignIcon className="h-3.5 w-3.5" />
+ </Button>
+ </Tooltip>
+ {isAttachmentSupported && (
+ <>
+ <Tooltip content="Attach a file">
+ <Button
+ variant="text"
+ size="icon"
+ className="cursor-pointer"
+ onClick={() => fileInputRef.current?.click()}
+ title="Attach a file"
+ >
+ <PaperclipIcon className="h-3.5 w-3.5" />
+ </Button>
+ </Tooltip>
+ <Input
+ ref={fileInputRef}
+ type="file"
+ multiple={true}
+ hidden={true}
+ onChange={(event) => {
+ if (event.target.files) {
+ onAddFiles([...event.target.files]);
+ }
+ }}
+ accept={SUPPORTED_ATTACHMENT_TYPES.join(",")}
+ />
+ </>
+ )}
+
+ <Tooltip content="Submit">
<Button
variant="text"
- size="icon"
- className="cursor-pointer"
- onClick={() => fileInputRef.current?.click()}
- title="Attach a file"
+ size="sm"
+ className="h-6 w-6 p-0 hover:bg-muted/30 cursor-pointer"
+ onClick={isLoading ? onStop : onSendClick}
+ disabled={isLoading ? false : isEmpty}
>
- <PaperclipIcon className="h-3.5 w-3.5" />
+ {isLoading ? (
+ <SquareIcon className="h-3 w-3 fill-current" />
+ ) : (
+ <SendIcon className="h-3 w-3" />
+ )}
</Button>
- <Input
- ref={fileInputRef}
- type="file"
- multiple={true}
- hidden={true}
- onChange={handleFileChange}
- accept={SUPPORTED_ATTACHMENT_TYPES.join(",")}
- />
- </>
- )}
-
- <Button
- variant="text"
- size="sm"
- className="h-6 w-6 p-0 hover:bg-muted/30 cursor-pointer"
- onClick={isLoading ? onStop : onSendClick}
- disabled={isLoading ? false : isEmpty}
- >
- {isLoading ? (
- <SquareIcon className="h-3 w-3 fill-current" />
- ) : (
- <SendIcon className="h-3 w-3" />
- )}
- </Button>
+ </Tooltip>
+ </div>
</div>
- </div>
+ </TooltipProvider>
);
},
);
@@ -444,7 +465,7 @@ interface ChatInputProps {
onStop: () => void;
onClose: () => void;
fileInputRef: React.RefObject<HTMLInputElement | null>;
- handleFileChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
+ onAddFiles: (files: File[]) => void;
}
const ChatInput: React.FC<ChatInputProps> = memo(
@@ -458,7 +479,7 @@ const ChatInput: React.FC<ChatInputProps> = memo(
isLoading,
onStop,
fileInputRef,
- handleFileChange,
+ onAddFiles,
onClose,
}) => {
const handleSendClick = useEvent(() => {
@@ -476,16 +497,39 @@ const ChatInput: React.FC<ChatInputProps> = memo(
onChange={setInput}
onSubmit={onSubmit}
onClose={onClose}
+ onAddFiles={onAddFiles}
placeholder={placeholder || "Type your message..."}
/>
</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);
+ }}
onSendClick={handleSendClick}
isLoading={isLoading}
onStop={onStop}
fileInputRef={fileInputRef}
- handleFileChange={handleFileChange}
+ onAddFiles={onAddFiles}
/>
</div>
);
@@ -622,29 +666,26 @@ const ChatPanelBody = () => {
},
});
- const handleFileChange = useEvent(
- (event: React.ChangeEvent<HTMLInputElement>) => {
- const files = event.target.files;
- if (!files) {
- return;
- }
+ const onAddFiles = useEvent((files: File[]) => {
+ if (files.length === 0) {
+ return;
+ }
- let fileSize = 0;
- for (const file of files) {
- fileSize += file.size;
- }
+ let fileSize = 0;
+ for (const file of files) {
+ fileSize += file.size;
+ }
- if (fileSize > MAX_ATTACHMENT_SIZE) {
- toast({
- title: "File size exceeds 50MB limit",
- description: "Please remove some files and try again.",
- });
- return;
- }
+ if (fileSize > MAX_ATTACHMENT_SIZE) {
+ toast({
+ title: "File size exceeds 50MB limit",
+ description: "Please remove some files and try again.",
+ });
+ return;
+ }
- setFiles((prev) => [...(prev ?? []), ...files]);
- },
- );
+ setFiles((prev) => [...(prev ?? []), ...files]);
+ });
const removeFile = useEvent((file: File) => {
if (files) {
@@ -783,7 +824,7 @@ const ChatPanelBody = () => {
isLoading={isLoading}
onStop={stop}
fileInputRef={fileInputRef}
- handleFileChange={handleFileChange}
+ onAddFiles={onAddFiles}
onClose={handleOnCloseThread}
/>
) : (
@@ -796,7 +837,7 @@ const ChatPanelBody = () => {
onStop={stop}
onClose={() => newMessageInputRef.current?.editor?.blur()}
fileInputRef={fileInputRef}
- handleFileChange={handleFileChange}
+ onAddFiles={onAddFiles}
/>
);
diff --git a/frontend/src/components/chat/markdown-renderer.tsx b/frontend/src/components/chat/markdown-renderer.tsx
index 77af2ab04a..2a29573340 100644
--- a/frontend/src/components/chat/markdown-renderer.tsx
+++ b/frontend/src/components/chat/markdown-renderer.tsx
@@ -172,21 +172,17 @@ const CopyButton: React.FC<ButtonProps> = ({ onClick, ...props }) => {
};
const COMPONENTS: Components = {
- code: ({ children, className, key }) => {
+ code: ({ children, className }) => {
const language = className?.replace("language-", "");
if (language && typeof children === "string") {
return (
- <div key={key}>
+ <div>
<div className="text-xs text-muted-foreground pl-1">{language}</div>
<CodeBlock code={children.trim()} language={language} />
</div>
);
}
- return (
- <code key={key} className={className}>
- {children}
- </code>
- );
+ return <code className={className}>{children}</code>;
},
};
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 94550f3d37..449419dd07 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -301,6 +301,7 @@ interface PromptInputProps {
onSubmit: (e: KeyboardEvent | undefined, value: string) => void;
additionalCompletions?: AdditionalCompletions;
maxHeight?: string;
+ onAddFiles?: (files: File[]) => void;
}
/**
@@ -318,6 +319,7 @@ export const PromptInput = ({
onSubmit,
onKeyDown,
onClose,
+ onAddFiles,
additionalCompletions,
maxHeight,
}: PromptInputProps) => {
@@ -346,7 +348,11 @@ export const PromptInput = ({
return [
autocompletion({}),
markdownLanguage,
- resourceExtension(markdownLanguage.language, store),
+ resourceExtension({
+ language: markdownLanguage.language,
+ store,
+ onAddFiles,
+ }),
markdownLanguage.language.data.of({
autocomplete: additionalCompletionsSource,
}),
@@ -411,7 +417,13 @@ export const PromptInput = ({
},
]),
];
- }, [store, additionalCompletionsSource, handleSubmit, handleEscape]);
+ }, [
+ store,
+ onAddFiles,
+ additionalCompletionsSource,
+ handleSubmit,
+ handleEscape,
+ ]);
return (
<ReactCodeMirror
diff --git a/frontend/src/components/editor/ai/completion-handlers.tsx b/frontend/src/components/editor/ai/completion-handlers.tsx
index e993763f67..bd73fcdea3 100644
--- a/frontend/src/components/editor/ai/completion-handlers.tsx
+++ b/frontend/src/components/editor/ai/completion-handlers.tsx
@@ -25,10 +25,8 @@ export const createAiCompletionOnKeydown = (opts: {
const metaKey = isPlatformMac() ? e.metaKey : e.ctrlKey;
// Mod+Enter should accept the completion, if there is one
- if (metaKey && e.key === "Enter") {
- if (!isLoading && completion) {
- handleAcceptCompletion();
- }
+ if (metaKey && e.key === "Enter" && !isLoading && completion) {
+ handleAcceptCompletion();
}
// Mod+Shift+Delete should decline the completion
diff --git a/frontend/src/components/editor/alerts/startup-logs-alert.tsx b/frontend/src/components/editor/alerts/startup-logs-alert.tsx
index e6a703f6de..134a4e76cc 100644
--- a/frontend/src/components/editor/alerts/startup-logs-alert.tsx
+++ b/frontend/src/components/editor/alerts/startup-logs-alert.tsx
@@ -1,3 +1,4 @@
+/* Copyright 2024 Marimo. All rights reserved. */
import { BoxIcon, XIcon } from "lucide-react";
import { useEffect, useState } from "react";
import { useEvent } from "react-use-event-hook";
@@ -24,7 +25,7 @@ export const StartupLogsAlert: React.FC = () => {
if (isDone) {
// Dismiss after 5 seconds
- timeout = window.setTimeout(() => handleClear(), 5_000);
+ timeout = window.setTimeout(() => handleClear(), 5000);
}
return () => {
diff --git a/frontend/src/components/editor/controls/state.ts b/frontend/src/components/editor/controls/state.ts
index 36a6c7e29a..3c39d05d74 100644
--- a/frontend/src/components/editor/controls/state.ts
+++ b/frontend/src/components/editor/controls/state.ts
@@ -1,3 +1,4 @@
+/* Copyright 2024 Marimo. All rights reserved. */
import { atom } from "jotai";
export const commandPaletteAtom = atom(false);
diff --git a/frontend/src/components/editor/package-alert.tsx b/frontend/src/components/editor/package-alert.tsx
index a5aeb671fe..776b2d9c42 100644
--- a/frontend/src/components/editor/package-alert.tsx
+++ b/frontend/src/components/editor/package-alert.tsx
@@ -607,7 +607,9 @@ const StreamingLogsViewer: React.FC<StreamingLogsViewerProps> = ({
const [isExpanded, setIsExpanded] = useState(true);
const packageCount = Object.keys(packageLogs).length;
- if (packageCount === 0) return null;
+ if (packageCount === 0) {
+ return null;
+ }
return (
<div className="mt-4 border-t border-border pt-4 w-full">
diff --git a/frontend/src/core/ai/context/context.ts b/frontend/src/core/ai/context/context.ts
index c4b7e80f85..5228918430 100644
--- a/frontend/src/core/ai/context/context.ts
+++ b/frontend/src/core/ai/context/context.ts
@@ -1,10 +1,12 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { allTablesAtom } from "@/core/datasets/data-source-connections";
+import { getRequestClient } from "@/core/network/requests";
import type { JotaiStore } from "@/core/state/jotai";
import { variablesAtom } from "@/core/variables/state";
import { CellOutputContextProvider } from "./providers/cell-output";
import { ErrorContextProvider } from "./providers/error";
+import { FileContextProvider } from "./providers/file";
import { TableContextProvider } from "./providers/tables";
import { VariableContextProvider } from "./providers/variable";
import { AIContextRegistry } from "./registry";
@@ -12,9 +14,15 @@ import { AIContextRegistry } from "./registry";
export function getAIContextRegistry(store: JotaiStore) {
const tablesMap = store.get(allTablesAtom);
const variables = store.get(variablesAtom);
+
return new AIContextRegistry()
.register(new TableContextProvider(tablesMap))
.register(new VariableContextProvider(variables, tablesMap))
.register(new ErrorContextProvider(store))
.register(new CellOutputContextProvider(store));
}
+
+export function getFileContextProvider(): FileContextProvider {
+ const apiRequests = getRequestClient();
+ return new FileContextProvider(apiRequests);
+}
diff --git a/frontend/src/core/ai/context/providers/__tests__/file.test.ts b/frontend/src/core/ai/context/providers/__tests__/file.test.ts
new file mode 100644
index 0000000000..6669953c6b
--- /dev/null
+++ b/frontend/src/core/ai/context/providers/__tests__/file.test.ts
@@ -0,0 +1,119 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import type { CompletionContext } from "@codemirror/autocomplete";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { EditRequests, FileInfo, RunRequests } from "@/core/network/types";
+import { FileContextProvider } from "../file";
+
+describe("FileContextProvider", () => {
+ let provider: FileContextProvider;
+ let mockApiRequests: Partial<EditRequests & RunRequests>;
+
+ beforeEach(() => {
+ mockApiRequests = {
+ sendSearchFiles: vi.fn(),
+ };
+ provider = new FileContextProvider(
+ mockApiRequests as EditRequests & RunRequests,
+ );
+ });
+
+ it("should have correct provider properties", () => {
+ expect(provider.title).toBe("Files");
+ expect(provider.mentionPrefix).toBe("#");
+ expect(provider.contextType).toBe("file");
+ });
+
+ it("should return empty items array for static getItems", () => {
+ expect(provider.getItems()).toEqual([]);
+ });
+
+ it("should format context item correctly", () => {
+ const item = {
+ uri: provider.asURI("/path/to/file.py"),
+ name: "file.py",
+ type: "file" as const,
+ description: "Python file",
+ data: {
+ path: "/path/to/file.py",
+ isDirectory: false,
+ isMarimoFile: false,
+ },
+ };
+
+ const context = provider.formatContext(item);
+ expect(context).toContain("file");
+ expect(context).toContain("file.py");
+ expect(context).toContain("/path/to/file.py");
+ });
+
+ it("should create completion source that returns null for no match", async () => {
+ const completionSource = provider.createCompletionSource();
+ const mockContext = {
+ matchBefore: vi.fn().mockReturnValue(null),
+ } as unknown as CompletionContext;
+
+ const result = await completionSource(mockContext);
+ expect(result).toBeNull();
+ });
+
+ it("should create completion source that searches files for matches", async () => {
+ const mockFiles: FileInfo[] = [
+ {
+ id: "1",
+ path: "/src/app.py",
+ name: "app.py",
+ isDirectory: false,
+ isMarimoFile: false,
+ lastModified: Date.now(),
+ },
+ {
+ id: "2",
+ path: "/src/utils",
+ name: "utils",
+ isDirectory: true,
+ isMarimoFile: false,
+ lastModified: Date.now(),
+ },
+ ];
+
+ vi.mocked(mockApiRequests.sendSearchFiles!).mockResolvedValue({
+ files: mockFiles,
+ query: "app",
+ totalFound: 2,
+ });
+
+ const completionSource = provider.createCompletionSource();
+ const mockContext = {
+ matchBefore: vi.fn().mockReturnValue({
+ text: "#app",
+ from: 0,
+ }),
+ } as unknown as CompletionContext;
+
+ const result = await completionSource(mockContext);
+
+ expect(result).not.toBeNull();
+ expect(result!.from).toBe(0);
+ expect(result!.options).toHaveLength(2);
+ expect(result!.options[0].label).toContain("app.py");
+ expect(result!.options[0].displayLabel).toContain("app.py");
+ });
+
+ it("should handle search errors gracefully", async () => {
+ vi.mocked(mockApiRequests.sendSearchFiles!).mockRejectedValue(
+ new Error("Search failed"),
+ );
+
+ const completionSource = provider.createCompletionSource();
+ const mockContext = {
+ matchBefore: vi.fn().mockReturnValue({
+ text: "#test",
+ from: 0,
+ }),
+ } as unknown as CompletionContext;
+
+ const result = await completionSource(mockContext);
+ expect(result).toBeNull();
+ });
+});
diff --git a/frontend/src/core/ai/context/providers/common.ts b/frontend/src/core/ai/context/providers/common.ts
index 3c0869b75b..7e99f42133 100644
--- a/frontend/src/core/ai/context/providers/common.ts
+++ b/frontend/src/core/ai/context/providers/common.ts
@@ -3,7 +3,10 @@
export const Boosts = {
LOCAL_TABLE: 5,
REMOTE_TABLE: 4,
+ HIGH: 4,
VARIABLE: 3,
+ MEDIUM: 3,
CELL_OUTPUT: 2,
+ LOW: 2,
ERROR: 1,
} as const;
diff --git a/frontend/src/core/ai/context/providers/file.ts b/frontend/src/core/ai/context/providers/file.ts
new file mode 100644
index 0000000000..007c4c4d66
--- /dev/null
+++ b/frontend/src/core/ai/context/providers/file.ts
@@ -0,0 +1,287 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import {
+ type Completion,
+ type CompletionContext,
+ type CompletionResult,
+ type CompletionSource,
+ closeCompletion,
+} from "@codemirror/autocomplete";
+import { toast } from "@/components/ui/use-toast";
+import { contextCallbacks } from "@/core/codemirror/ai/state";
+import type { EditRequests, FileInfo, RunRequests } from "@/core/network/types";
+import { deserializeBlob } from "@/utils/blob";
+import { type Base64String, base64ToDataURL } from "@/utils/json/base64";
+import { Logger } from "@/utils/Logger";
+import { type AIContextItem, AIContextProvider } from "../registry";
+import { contextToXml } from "../utils";
+import { Boosts } from "./common";
+export interface FileContextItem extends AIContextItem {
+ type: "file";
+ data: {
+ path: string;
+ isDirectory: boolean;
+ };
+}
+
+export interface FileSearchConfig {
+ maxDepth: number;
+ maxResults: number;
+ defaultResultsLimit: number;
+ includeDirectories: boolean;
+}
+
+const DEFAULT_FILE_SEARCH_CONFIG: FileSearchConfig = {
+ maxDepth: 3,
+ maxResults: 20,
+ defaultResultsLimit: 5,
+ includeDirectories: false,
+};
+
+export class FileContextProvider extends AIContextProvider<FileContextItem> {
+ readonly title = "Files";
+ readonly mentionPrefix = "#";
+ readonly contextType = "file";
+
+ constructor(
+ private apiRequests: EditRequests & RunRequests,
+ private config: FileSearchConfig = DEFAULT_FILE_SEARCH_CONFIG,
+ ) {
+ super();
+ }
+
+ /**
+ * Create a dynamic completion source for file mentions
+ * This bypasses the standard registry system to enable dynamic searching
+ */
+ createCompletionSource(): CompletionSource {
+ return async (
+ context: CompletionContext,
+ ): Promise<CompletionResult | null> => {
+ // Look for # followed by any characters (including dots, slashes, etc.)
+ const match = context.matchBefore(/#[^\s#]*/);
+ if (!match) {
+ return null;
+ }
+
+ const matchText = match.text;
+ if (!matchText.startsWith("#")) {
+ return null;
+ }
+
+ const searchQuery = matchText.slice(1); // Remove the #
+ if (searchQuery.length === 0) {
+ // Show some popular files/directories even with no query
+ return this.getDefaultCompletions(match);
+ }
+
+ try {
+ const files = (await this.searchFiles(searchQuery)) || [];
+ const completions = files.map((file: FileInfo) => {
+ const item: FileContextItem = {
+ uri: this.asURI(file.path),
+ name: file.name,
+ type: this.contextType,
+ description: file.isDirectory ? "Directory" : "File",
+ data: {
+ path: file.path,
+ isDirectory: file.isDirectory,
+ },
+ };
+ return this.formatCompletion(item);
+ });
+
+ return {
+ from: match.from,
+ options: completions,
+ };
+ } catch (error) {
+ Logger.error("Failed to search files:", error);
+ return null;
+ }
+ };
+ }
+
+ private searchFiles = async (
+ query: string,
+ options: Partial<FileSearchConfig> = {},
+ ): Promise<FileInfo[]> => {
+ const { maxDepth, maxResults, includeDirectories } = {
+ ...this.config,
+ ...options,
+ };
+ const response = await this.apiRequests.sendSearchFiles({
+ query,
+ includeFiles: true,
+ includeDirectories: includeDirectories,
+ depth: maxDepth,
+ limit: maxResults,
+ });
+ return response.files;
+ };
+
+ private async getDefaultCompletions(match: {
+ from: number;
+ }): Promise<CompletionResult | null> {
+ try {
+ // Show common file types when no specific query is given
+ // Use broad searches for common file types
+ const searches = ["py", "md", "csv"];
+
+ // Try the first search that returns results
+ for (const search of searches) {
+ try {
+ const files = await this.searchFiles(search, {
+ maxDepth: 1,
+ maxResults: 5,
+ });
+
+ if (files && files.length > 0) {
+ const completions = files.map((file) => {
+ const item: FileContextItem = {
+ uri: this.asURI(file.path),
+ name: file.name,
+ type: this.contextType,
+ description: file.isDirectory ? "Directory" : "File",
+ data: {
+ path: file.path,
+ isDirectory: file.isDirectory,
+ },
+ };
+ return this.formatCompletion(item);
+ });
+
+ return {
+ from: match.from,
+ options: completions,
+ };
+ }
+ } catch (error) {
+ Logger.error("Failed to get default file completions:", error);
+ }
+ }
+
+ // If no searches return results, return empty
+ return {
+ from: match.from,
+ options: [],
+ };
+ } catch (error) {
+ Logger.error("Failed to get default file completions:", error);
+ return null;
+ }
+ }
+
+ getItems(): FileContextItem[] {
+ // Files are fetched dynamically, so return empty array
+ // This provider relies on dynamic fetching via createCompletionSource()
+ return [];
+ }
+
+ formatCompletion(item: FileContextItem): Completion {
+ const { data, name } = item;
+ const icon = data.isDirectory ? "📁" : "📄";
+
+ return {
+ ...this.createBasicCompletion(item),
+ type: "file",
+ section: "File",
+ boost: data.isDirectory ? Boosts.MEDIUM : Boosts.LOW,
+ detail: data.path,
+ displayLabel: `${icon} ${name}`,
+ apply: async (view, completion, from, to) => {
+ // First try to add the file as an attachment, if the callback is provided
+ // otherwise add it to the prompt
+ const addAttachment = view.state.facet(contextCallbacks)?.addAttachment;
+ if (!addAttachment) {
+ Logger.warn("No addAttachment callback provided");
+ return;
+ }
+
+ const fileDetails = await this.apiRequests
+ .sendFileDetails({ path: data.path })
+ .catch((error) => {
+ toast({
+ title: "Failed to get file details",
+ description: error.message,
+ });
+ return null;
+ });
+
+ if (!fileDetails) {
+ return;
+ }
+
+ const mimeType = fileDetails.mimeType || "text/plain";
+
+ // Handle binary vs text files
+ let blob: Blob;
+ if (
+ mimeType.startsWith("text/") ||
+ mimeType.includes("json") ||
+ mimeType.includes("xml")
+ ) {
+ // Text files - create blob directly from contents
+ blob = new Blob([fileDetails.contents || ""], { type: mimeType });
+ } else {
+ // Binary files - use blob utility to decode base64
+ if (fileDetails.contents) {
+ try {
+ // Create data URL using utility and deserialize blob
+ const dataURL = base64ToDataURL(
+ fileDetails.contents as Base64String,
+ mimeType,
+ );
+ blob = await deserializeBlob(dataURL);
+ } catch {
+ // Fallback to treating as text
+ blob = new Blob([fileDetails.contents], { type: mimeType });
+ }
+ } else {
+ blob = new Blob([""], { type: mimeType });
+ }
+ }
+
+ const file = new File([blob], name, { type: mimeType });
+ addAttachment(file);
+
+ // Close completion and delete the entire mention text (from # to cursor)
+ view.dispatch({
+ changes: { from, to, insert: "" },
+ });
+
+ closeCompletion(view);
+ },
+ info: () => {
+ const element = document.createElement("div");
+ element.classList.add("flex", "flex-col", "gap-1", "p-2");
+
+ const title = document.createElement("div");
+ title.classList.add("font-bold");
+ title.textContent = name;
+ element.append(title);
+
+ const path = document.createElement("div");
+ path.classList.add("text-xs", "text-muted-foreground");
+ path.textContent = data.path;
+ element.append(path);
+ return element;
+ },
+ };
+ }
+
+ formatContext(item: FileContextItem): string {
+ const { data, name } = item;
+ return contextToXml({
+ type: this.contextType,
+ data: {
+ name: name,
+ path: data.path,
+ isDirectory: data.isDirectory,
+ },
+ details: data.isDirectory
+ ? "Directory containing files and subdirectories"
+ : "File",
+ });
+ }
+}
diff --git a/frontend/src/core/ai/state.ts b/frontend/src/core/ai/state.ts
index a60f1b444f..008e2314a6 100644
--- a/frontend/src/core/ai/state.ts
+++ b/frontend/src/core/ai/state.ts
@@ -4,6 +4,7 @@ import type { UIMessage } from "@ai-sdk/react";
import type { FileUIPart } from "ai";
import { atom } from "jotai";
import { atomWithStorage } from "jotai/utils";
+import { uniqueBy } from "@/utils/arrays";
import { adaptForLocalStorage } from "@/utils/storage";
import type { TypedString } from "@/utils/typed";
import type { CellId } from "../cells/ids";
@@ -46,9 +47,17 @@ export interface ChatState {
}
function removeEmptyChats(chatState: Map<ChatId, Chat>): Map<ChatId, Chat> {
- return new Map(
- [...chatState.entries()].filter(([_, chat]) => chat.messages.length > 0),
- );
+ 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;
}
export const chatStateAtom = atomWithStorage<ChatState>(
diff --git a/frontend/src/core/alerts/state.ts b/frontend/src/core/alerts/state.ts
index 5e11814675..7a826a2900 100644
--- a/frontend/src/core/alerts/state.ts
+++ b/frontend/src/core/alerts/state.ts
@@ -67,17 +67,28 @@ const { valueAtom: alertAtom, useActions } = createReducerAndAtoms(
// Handle streaming logs for installing package alerts
if (isInstallingPackageAlert(alert) && alert.logs && alert.log_status) {
for (const [packageName, newContent] of Object.entries(alert.logs)) {
- if (alert.log_status === "start") {
- // Start new log for this package
- newPackageLogs[packageName] = newContent;
- } else if (alert.log_status === "append") {
- // Append to existing log
- const prevContent = newPackageLogs[packageName] || "";
- newPackageLogs[packageName] = prevContent + newContent;
- } else if (alert.log_status === "done") {
- // Append final content and mark as done
- const prevContent = newPackageLogs[packageName] || "";
- newPackageLogs[packageName] = prevContent + newContent;
+ switch (alert.log_status) {
+ case "start":
+ // Start new log for this package
+ newPackageLogs[packageName] = newContent;
+
+ break;
+
+ case "append": {
+ // Append to existing log
+ const prevContent = newPackageLogs[packageName] || "";
+ newPackageLogs[packageName] = prevContent + newContent;
+
+ break;
+ }
+ case "done": {
+ // Append final content and mark as done
+ const prevContent = newPackageLogs[packageName] || "";
+ newPackageLogs[packageName] = prevContent + newContent;
+
+ break;
+ }
+ // No default
}
}
}
diff --git a/frontend/src/core/codemirror/ai/resources.ts b/frontend/src/core/codemirror/ai/resources.ts
index 06efe57c2e..4f944e44cd 100644
--- a/frontend/src/core/codemirror/ai/resources.ts
+++ b/frontend/src/core/codemirror/ai/resources.ts
@@ -12,30 +12,72 @@ import {
resourcesField,
resourceTheme,
} from "@marimo-team/codemirror-mcp";
-import { getAIContextRegistry } from "@/core/ai/context/context";
+import {
+ getAIContextRegistry,
+ getFileContextProvider,
+} from "@/core/ai/context/context";
import type { AIContextItem } from "@/core/ai/context/registry";
import type { JotaiStore } from "@/core/state/jotai";
import { Logger } from "@/utils/Logger";
+import { contextCallbacks } from "./state";
+
+const NONE_RESOURCE_TYPE = "_none_";
+const NONE_RESOURCE = [
+ {
+ uri: "",
+ name: "No resources",
+ type: NONE_RESOURCE_TYPE,
+ data: {},
+ },
+];
+const NONE_RESOURCE_FORMAT_COMPLETION = {
+ info: "Variables, dataframes, and tables will appear here.",
+ apply: () => {
+ return;
+ },
+};
+
+export function resourceExtension(opts: {
+ language: Language;
+ store: JotaiStore;
+ onAddFiles?: (files: File[]) => void;
+}): Extension[] {
+ const { language, store, onAddFiles } = opts;
-export function resourceExtension(
- language: Language,
- store: JotaiStore,
-): Extension[] {
return [
language.data.of({
+ // Resource completion for static resources (variables, tables, etc.)
autocomplete: resourceCompletion(
async (): Promise<Resource[]> => {
const registry = getAIContextRegistry(store);
const resources = registry.getAllItems();
+ if (resources.length === 0) {
+ return NONE_RESOURCE;
+ }
return resources;
},
(resource) => {
+ if (resource.type === NONE_RESOURCE_TYPE) {
+ return NONE_RESOURCE_FORMAT_COMPLETION;
+ }
+
const registry = getAIContextRegistry(store);
const provider = registry.getProvider(resource.type);
return provider?.formatCompletion(resource as AIContextItem) || {};
},
),
}),
+ contextCallbacks.of({
+ addAttachment: (attachment) => onAddFiles?.([attachment]),
+ }),
+ // Dynamic file completion
+ ...(onAddFiles
+ ? [
+ language.data.of({
+ autocomplete: getFileContextProvider().createCompletionSource(),
+ }),
+ ]
+ : []),
resourceDecorations,
resourceInputFilter,
resourcesField.init(() => {
diff --git a/frontend/src/core/codemirror/ai/state.ts b/frontend/src/core/codemirror/ai/state.ts
new file mode 100644
index 0000000000..ff5e1ec3d2
--- /dev/null
+++ b/frontend/src/core/codemirror/ai/state.ts
@@ -0,0 +1,12 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { singleFacet } from "../facet";
+
+interface ContextCallbacks {
+ addAttachment?: (attachment: File) => void;
+}
+
+/**
+ * State for completion callbacks
+ */
+export const contextCallbacks = singleFacet<ContextCallbacks>();
diff --git a/frontend/src/core/islands/bridge.ts b/frontend/src/core/islands/bridge.ts
index e1f98172a1..91833d9d2e 100644
--- a/frontend/src/core/islands/bridge.ts
+++ b/frontend/src/core/islands/bridge.ts
@@ -155,6 +155,7 @@ export class IslandsPyodideBridge implements RunRequests, EditRequests {
previewDataSourceConnection = throwNotImplemented;
openFile = throwNotImplemented;
sendListFiles = throwNotImplemented;
+ sendSearchFiles = throwNotImplemented;
sendPdb = throwNotImplemented;
sendCreateFileOrFolder = throwNotImplemented;
sendDeleteFileOrFolder = throwNotImplemented;
diff --git a/frontend/src/core/network/requests-network.ts b/frontend/src/core/network/requests-network.ts
index 1c874e3172..b7c1e44a5d 100644
--- a/frontend/src/core/network/requests-network.ts
+++ b/frontend/src/core/network/requests-network.ts
@@ -235,6 +235,14 @@ export function createNetworkRequests(): EditRequests & RunRequests {
})
.then(handleResponse);
},
+ sendSearchFiles: async (request) => {
+ await waitForConnectionOpen();
+ return getClient()
+ .POST("/api/files/search", {
+ body: request,
+ })
+ .then(handleResponse);
+ },
sendCreateFileOrFolder: async (request) => {
await waitForConnectionOpen();
return getClient()
diff --git a/frontend/src/core/network/requests-static.ts b/frontend/src/core/network/requests-static.ts
index 205f817950..d32462ef61 100644
--- a/frontend/src/core/network/requests-static.ts
+++ b/frontend/src/core/network/requests-static.ts
@@ -60,6 +60,7 @@ export function createStaticRequests(): EditRequests & RunRequests {
openFile: throwNotInEditMode,
getUsageStats: throwNotInEditMode,
sendListFiles: throwNotInEditMode,
+ sendSearchFiles: throwNotInEditMode,
sendPdb: throwNotInEditMode,
sendCreateFileOrFolder: throwNotInEditMode,
sendDeleteFileOrFolder: throwNotInEditMode,
diff --git a/frontend/src/core/network/requests-toasting.ts b/frontend/src/core/network/requests-toasting.ts
index d83a97b185..701364e175 100644
--- a/frontend/src/core/network/requests-toasting.ts
+++ b/frontend/src/core/network/requests-toasting.ts
@@ -38,6 +38,7 @@ export function createErrorToastingRequests(
openFile: "Failed to open file",
getUsageStats: "", // No toast
sendListFiles: "Failed to list files",
+ sendSearchFiles: "Failed to search files",
sendPdb: "Failed to start debug session",
sendCreateFileOrFolder: "Failed to create file or folder",
sendDeleteFileOrFolder: "Failed to delete file or folder",
diff --git a/frontend/src/core/network/types.ts b/frontend/src/core/network/types.ts
index 2172735d84..e7846cc907 100644
--- a/frontend/src/core/network/types.ts
+++ b/frontend/src/core/network/types.ts
@@ -32,6 +32,8 @@ export type FileDetailsResponse = schemas["FileDetailsResponse"];
export type FileInfo = schemas["FileInfo"];
export type FileListRequest = schemas["FileListRequest"];
export type FileListResponse = schemas["FileListResponse"];
+export type FileSearchRequest = schemas["FileSearchRequest"];
+export type FileSearchResponse = schemas["FileSearchResponse"];
export type FileMoveRequest = schemas["FileMoveRequest"];
export type FileMoveResponse = schemas["FileMoveResponse"];
export type FileUpdateRequest = schemas["FileUpdateRequest"];
@@ -144,6 +146,7 @@ export interface EditRequests {
sendPdb: (request: PdbRequest) => Promise<null>;
// File explorer requests
sendListFiles: (request: FileListRequest) => Promise<FileListResponse>;
+ sendSearchFiles: (request: FileSearchRequest) => Promise<FileSearchResponse>;
sendCreateFileOrFolder: (
request: FileCreateRequest,
) => Promise<FileCreateResponse>;
diff --git a/frontend/src/core/wasm/bridge.ts b/frontend/src/core/wasm/bridge.ts
index 0be97b9b4a..5273acf0bd 100644
--- a/frontend/src/core/wasm/bridge.ts
+++ b/frontend/src/core/wasm/bridge.ts
@@ -21,6 +21,7 @@ import type {
FileDetailsResponse,
FileListResponse,
FileMoveResponse,
+ FileSearchResponse,
FileUpdateResponse,
FormatResponse,
RunRequests,
@@ -373,6 +374,14 @@ export class PyodideBridge implements RunRequests, EditRequests {
return response as FileListResponse;
};
+ sendSearchFiles: EditRequests["sendSearchFiles"] = async (request) => {
+ const response = await this.rpc.proxy.request.bridge({
+ functionName: "search_files",
+ payload: request,
+ });
+ return response as FileSearchResponse;
+ };
+
sendComponentValues: RunRequests["sendComponentValues"] = async (request) => {
await this.putControlRequest({
...request,
diff --git a/frontend/src/core/wasm/worker/types.ts b/frontend/src/core/wasm/worker/types.ts
index 9d0b456eca..01605d5c7b 100644
--- a/frontend/src/core/wasm/worker/types.ts
+++ b/frontend/src/core/wasm/worker/types.ts
@@ -16,6 +16,8 @@ import type {
FileListResponse,
FileMoveRequest,
FileMoveResponse,
+ FileSearchRequest,
+ FileSearchResponse,
FileUpdateRequest,
FileUpdateResponse,
FormatRequest,
@@ -72,6 +74,7 @@ export interface RawBridge {
save_user_config(request: SaveUserConfigurationRequest): Promise<null>;
rename_file(request: string): Promise<string>;
list_files(request: FileListRequest): Promise<FileListResponse>;
+ search_files(request: FileSearchRequest): Promise<FileSearchResponse>;
file_details(request: { path: string }): Promise<FileDetailsResponse>;
create_file_or_directory(
request: FileCreateRequest,
diff --git a/frontend/src/plugins/impl/code/any-language-editor.tsx b/frontend/src/plugins/impl/code/any-language-editor.tsx
index a406c413b3..7335a7d211 100644
--- a/frontend/src/plugins/impl/code/any-language-editor.tsx
+++ b/frontend/src/plugins/impl/code/any-language-editor.tsx
@@ -1,10 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import {
- type LanguageName,
- langs,
- loadLanguage,
-} from "@uiw/codemirror-extensions-langs";
+import { langs, loadLanguage } from "@uiw/codemirror-extensions-langs";
import ReactCodeMirror, {
type Extension,
type ReactCodeMirrorProps,
@@ -15,6 +11,12 @@ import type { ResolvedTheme } from "@/theme/useTheme";
import { Logger } from "@/utils/Logger";
import { ErrorBanner } from "../common/error-banner";
+const LANGUAGE_MAP: Record<string, string> = {
+ python: "py",
+ javascript: "js",
+ typescript: "ts",
+};
+
/**
* A code editor that supports any language.
*
@@ -28,6 +30,9 @@ const AnyLanguageCodeMirror: React.FC<
showCopyButton?: boolean;
}
> = ({ language, showCopyButton, extensions = [], ...props }) => {
+ // Maybe normalize the language to the extension
+ language = LANGUAGE_MAP[language || ""] || language;
+
const isNotSupported = language && !(language in langs);
if (isNotSupported) {
Logger.warn(`Language ${language} not found in CodeMirror.`);
@@ -37,9 +42,7 @@ const AnyLanguageCodeMirror: React.FC<
if (!language) {
return extensions;
}
- return [loadLanguage(language as LanguageName), ...extensions].filter(
- Boolean,
- );
+ return [loadLanguage(language), ...extensions].filter(Boolean);
}, [language, extensions]);
return (
diff --git a/frontend/src/theme/useTheme.ts b/frontend/src/theme/useTheme.ts
index cffd2e316a..63ece685ed 100644
--- a/frontend/src/theme/useTheme.ts
+++ b/frontend/src/theme/useTheme.ts
@@ -74,12 +74,17 @@ setupThemeListener();
function getVsCodeTheme(): "light" | "dark" | undefined {
const kind = document.body.dataset.vscodeThemeKind;
- if (kind === "vscode-dark") {
- return "dark";
- } else if (kind === "vscode-high-contrast") {
- return "dark";
- } else if (kind === "vscode-light") {
- return "light";
+ switch (kind) {
+ case "vscode-dark":
+ return "dark";
+
+ case "vscode-high-contrast":
+ return "dark";
+
+ case "vscode-light":
+ return "light";
+
+ // No default
}
return undefined;
}
diff --git a/frontend/src/utils/arrays.ts b/frontend/src/utils/arrays.ts
index fed6711d99..7c6c2d2248 100644
--- a/frontend/src/utils/arrays.ts
+++ b/frontend/src/utils/arrays.ts
@@ -73,3 +73,16 @@ export function arrayToggle<T>(arr: T[], item: T): T[] {
result.splice(index, 1);
return result;
}
+
+export function uniqueBy<T>(arr: T[], key: (item: T) => string): T[] {
+ const result = [];
+ const seen = new Set();
+ for (const item of arr) {
+ const k = key(item);
+ if (!seen.has(k)) {
+ seen.add(k);
+ result.push(item);
+ }
+ }
+ return result;
+}
diff --git a/frontend/src/utils/json/base64.ts b/frontend/src/utils/json/base64.ts
index 0db101a760..05e76e6b49 100644
--- a/frontend/src/utils/json/base64.ts
+++ b/frontend/src/utils/json/base64.ts
@@ -26,8 +26,11 @@ export function deserializeJson<T>(jsonString: JsonString<T>): T {
return JSON.parse(jsonString) as T;
}
-export function base64ToDataURL<T>(base64: Base64String<T>, mimeType: string) {
- return `data:${mimeType};base64,${base64}`;
+export function base64ToDataURL<T>(
+ base64: Base64String<T>,
+ mimeType: string,
+): DataURLString {
+ return `data:${mimeType};base64,${base64}` as DataURLString;
}
export function typedAtob<T>(base64: Base64String<T>): ByteString<T> {
diff --git a/marimo/_ai/_convert.py b/marimo/_ai/_convert.py
index 1de547d4e8..6a8497eee9 100644
--- a/marimo/_ai/_convert.py
+++ b/marimo/_ai/_convert.py
@@ -153,7 +153,7 @@ def get_anthropic_parts_from_chat_parts(
thinking_message: ThinkingBlockParam = {
"type": "thinking",
- "thinking": part.reasoning,
+ "thinking": part.text,
"signature": signature,
}
anthropic_parts.append(thinking_message)
@@ -289,7 +289,7 @@ def get_google_messages_from_parts(
# According to Google's thinking models documentation
reasoning_message: ContentDict = {
"role": "user" if role == "user" else "model",
- "parts": [{"text": part.reasoning, "thought": True}],
+ "parts": [{"text": part.text, "thought": True}],
}
messages.append(reasoning_message)
elif isinstance(part, FilePart):
diff --git a/marimo/_ai/_types.py b/marimo/_ai/_types.py
index 96bf00ca91..ee28a0eb1b 100644
--- a/marimo/_ai/_types.py
+++ b/marimo/_ai/_types.py
@@ -115,8 +115,8 @@ class ReasoningPart:
"""Represents a reasoning content part."""
type: Literal["reasoning"]
- reasoning: str
- details: list[ReasoningDetails]
+ text: str
+ details: Optional[list[ReasoningDetails]] = None
@dataclass
@@ -194,7 +194,7 @@ class ChatMessage(msgspec.Struct):
continue
raise msgspec.DecodeError(
- f"Could not decode part as any of {PART_TYPES}"
+ f"Could not decode part as any of {PART_TYPES}, for part {part}"
)
diff --git a/marimo/_cli/development/commands.py b/marimo/_cli/development/commands.py
index b1fe0b1dc9..73342d071e 100644
--- a/marimo/_cli/development/commands.py
+++ b/marimo/_cli/development/commands.py
@@ -170,6 +170,8 @@ def _generate_server_api_schema() -> dict[str, Any]:
files.FileDetailsResponse,
files.FileListRequest,
files.FileListResponse,
+ files.FileSearchRequest,
+ files.FileSearchResponse,
files.FileMoveRequest,
files.FileMoveResponse,
files.FileOpenRequest,
diff --git a/marimo/_plugins/ui/_impl/chat/utils.py b/marimo/_plugins/ui/_impl/chat/utils.py
index b889698d64..ef3ca1cfcb 100644
--- a/marimo/_plugins/ui/_impl/chat/utils.py
+++ b/marimo/_plugins/ui/_impl/chat/utils.py
@@ -70,7 +70,7 @@ def from_chat_message_dict(d: ChatMessageDict) -> ChatMessage:
parts.append(
ReasoningPart(
type="reasoning",
- reasoning=part_dict["reasoning"],
+ text=part_dict["reasoning"],
details=details,
)
)
diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.py
index 5b2ffb4d5f..df93f5188f 100644
--- a/marimo/_pyodide/pyodide_session.py
+++ b/marimo/_pyodide/pyodide_session.py
@@ -57,6 +57,8 @@ from marimo._server.models.files import (
FileListResponse,
FileMoveRequest,
FileMoveResponse,
+ FileSearchRequest,
+ FileSearchResponse,
FileUpdateRequest,
FileUpdateResponse,
)
@@ -260,7 +262,7 @@ class PyodideBridge:
self.session.app_manager.save_app_config(parsed.config)
def save_user_config(self, request: str) -> None:
- parsed = self._parse(request, requests.SetUserConfigRequest)
+ parsed = self._parse(request, SetUserConfigRequest)
config = merge_default_config(parsed.config)
self.session.put_control_request(SetUserConfigRequest(config=config))
@@ -271,17 +273,35 @@ class PyodideBridge:
self,
request: str,
) -> str:
- body = parse_raw(json.loads(request), FileListRequest)
+ body = self._parse(request, FileListRequest)
root = body.path or self.file_system.get_root()
files = self.file_system.list_files(root)
response = FileListResponse(files=files, root=root)
return self._dump(response)
+ def search_files(
+ self,
+ request: str,
+ ) -> str:
+ body = self._parse(request, FileSearchRequest)
+ files = self.file_system.search(
+ query=body.query,
+ path=body.path,
+ depth=body.depth,
+ include_directories=body.include_directories,
+ include_files=body.include_files,
+ limit=body.limit,
+ )
+ response = FileSearchResponse(
+ files=files, query=body.query, total_found=len(files)
+ )
+ return self._dump(response)
+
def file_details(
self,
request: str,
) -> str:
- body = parse_raw(json.loads(request), FileDetailsRequest)
+ body = self._parse(request, FileDetailsRequest)
response = self.file_system.get_details(body.path)
return self._dump(response)
@@ -289,7 +309,7 @@ class PyodideBridge:
self,
request: str,
) -> str:
- body = parse_raw(json.loads(request), FileCreateRequest)
+ body = self._parse(request, FileCreateRequest)
try:
# If we need to eliminate the overhead associated with
# base64-encoding/decoding the file contents, we could try pushing
@@ -311,7 +331,7 @@ class PyodideBridge:
self,
request: str,
) -> str:
- body = parse_raw(json.loads(request), FileDeleteRequest)
+ body = self._parse(request, FileDeleteRequest)
success = self.file_system.delete_file_or_directory(body.path)
response = FileDeleteResponse(success=success)
return self._dump(response)
@@ -320,7 +340,7 @@ class PyodideBridge:
self,
request: str,
) -> str:
- body = parse_raw(json.loads(request), FileMoveRequest)
+ body = self._parse(request, FileMoveRequest)
try:
info = self.file_system.move_file_or_directory(
body.path, body.new_path
@@ -334,7 +354,7 @@ class PyodideBridge:
self,
request: str,
) -> str:
- body = parse_raw(json.loads(request), FileUpdateRequest)
+ body = self._parse(request, FileUpdateRequest)
try:
Path(body.path).write_text(body.contents, encoding="utf-8")
response = FileUpdateResponse(success=True)
@@ -343,7 +363,7 @@ class PyodideBridge:
return self._dump(response)
def export_html(self, request: str) -> str:
- parsed = parse_raw(json.loads(request), ExportAsHTMLRequest)
+ parsed = self._parse(request, ExportAsHTMLRequest)
html, _filename = Exporter().export_as_html(
app=self.session.app_manager.app,
filename=self.session.app_manager.filename,
diff --git a/marimo/_server/api/endpoints/file_explorer.py b/marimo/_server/api/endpoints/file_explorer.py
index 7f7f9d5eee..208f91898c 100644
--- a/marimo/_server/api/endpoints/file_explorer.py
+++ b/marimo/_server/api/endpoints/file_explorer.py
@@ -21,6 +21,8 @@ from marimo._server.models.files import (
FileMoveRequest,
FileMoveResponse,
FileOpenRequest,
+ FileSearchRequest,
+ FileSearchResponse,
FileUpdateRequest,
FileUpdateResponse,
)
@@ -249,3 +251,37 @@ async def open_file(
except Exception as e:
LOGGER.error(f"Error opening file: {e}")
return ErrorResponse(success=False, message=str(e))
+
+
+@requires("edit")
+async def search_files(
+ *,
+ request: Request,
+) -> FileSearchResponse:
+ """
+ requestBody:
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/FileSearchRequest"
+ responses:
+ 200:
+ description: Search for files and directories matching a query
+ content:
+ application/json:
+ schema:
+ $ref: "#/components/schemas/FileSearchResponse"
+ """
+ body = await parse_request(request, cls=FileSearchRequest)
+ files = file_system.search(
+ query=body.query,
+ path=body.path,
+ include_directories=body.include_directories,
+ include_files=body.include_files,
+ depth=body.depth,
+ limit=body.limit,
+ )
+ return FileSearchResponse(
+ files=files, query=body.query, total_found=len(files)
+ )
diff --git a/marimo/_server/files/file_system.py b/marimo/_server/files/file_system.py
index 70dab10f31..fb6cda3771 100644
--- a/marimo/_server/files/file_system.py
+++ b/marimo/_server/files/file_system.py
@@ -62,3 +62,26 @@ class FileSystem(ABC):
def update_file(self, path: str, contents: str) -> FileInfo:
"""Update the contents of a file."""
pass
+
+ @abstractmethod
+ def search(
+ self,
+ query: str,
+ *,
+ path: Optional[str] = None,
+ include_directories: bool = True,
+ include_files: bool = True,
+ depth: int = 3,
+ limit: int = 100,
+ ) -> list[FileInfo]:
+ """Search for files and directories matching a query.
+
+ Args:
+ query: Search query string (matches file/directory names)
+ path: Root path to search from (defaults to root)
+ include_directories: Include directories
+ include_files: Include files
+ depth: Maximum depth to search (default: 3)
+ limit: Maximum number of results to return (default: 100)
+ """
+ pass
diff --git a/marimo/_server/files/os_file_system.py b/marimo/_server/files/os_file_system.py
index 98142bf640..e0c896c117 100644
--- a/marimo/_server/files/os_file_system.py
+++ b/marimo/_server/files/os_file_system.py
@@ -5,8 +5,10 @@ import base64
import mimetypes
import os
import platform
+import re
import shutil
import subprocess
+from collections import deque
from pathlib import Path
from typing import Literal, Optional, Union
@@ -23,6 +25,9 @@ IGNORE_LIST = [
".DS_Store",
"__pycache__",
"node_modules",
+ ".git",
+ ".venv",
+ "site-packages",
]
DISALLOWED_NAMES = [
@@ -184,6 +189,136 @@ class OSFileSystem(FileSystem):
file_path.write_text(contents, encoding="utf-8")
return self.get_details(path, contents=contents).file
+ def search(
+ self,
+ query: str,
+ *,
+ path: Optional[str] = None,
+ include_directories: bool = True,
+ include_files: bool = True,
+ depth: int = 3,
+ limit: int = 100,
+ ) -> list[FileInfo]:
+ """Search for files and directories matching a query with high performance."""
+ if not query.strip():
+ return []
+
+ search_path = path if path is not None else self.get_root()
+ if not os.path.exists(search_path):
+ return []
+
+ # Compile regex pattern for case-insensitive search
+ try:
+ pattern = re.compile(re.escape(query.lower()))
+ except re.error:
+ # If regex compilation fails, fall back to simple string matching
+ pattern = None
+
+ results: list[FileInfo] = []
+ seen_paths: set[str] = set()
+
+ # Use BFS with deque for better performance than recursive DFS
+ queue = deque([(search_path, 0)]) # (path, current_depth)
+
+ while queue and len(results) < limit:
+ current_path, current_depth = queue.popleft()
+
+ # Skip if we've exceeded depth limit
+ if current_depth > depth:
+ continue
+
+ # Skip if we've already processed this path (avoid symlink loops)
+ if current_path in seen_paths:
+ continue
+ seen_paths.add(current_path)
+
+ try:
+ # Use os.scandir for better performance than os.listdir
+ with os.scandir(current_path) as entries:
+ for entry in entries:
+ if len(results) >= limit:
+ break
+
+ # Skip ignored files/directories
+ if entry.name in IGNORE_LIST:
+ continue
+
+ # Check if name matches query
+ name_lower = entry.name.lower()
+ query_lower = query.lower()
+
+ matches = False
+ if pattern:
+ matches = pattern.search(name_lower) is not None
+ else:
+ matches = query_lower in name_lower
+
+ if not matches:
+ # If this is a directory and we haven't hit depth limit, add to queue
+ if current_depth < depth:
+ try:
+ if entry.is_dir():
+ queue.append(
+ (entry.path, current_depth + 1)
+ )
+ except OSError:
+ # Skip entries that can't be accessed
+ continue
+ continue
+
+ try:
+ is_directory = entry.is_dir()
+ entry_stat = entry.stat()
+
+ # Apply directory/file filtering
+ if not include_directories and is_directory:
+ # Skip directories if directory=False
+ continue
+ if not include_files and not is_directory:
+ # Skip files if file=False
+ continue
+
+ file_info = FileInfo(
+ id=entry.path,
+ path=entry.path,
+ name=entry.name,
+ is_directory=is_directory,
+ # This can be expensive, so we don't do it on search
+ is_marimo_file=False,
+ last_modified=entry_stat.st_mtime,
+ )
+ results.append(file_info)
+
+ # If this is a matching directory and we haven't hit depth limit, add to queue
+ if is_directory and current_depth < depth:
+ queue.append((entry.path, current_depth + 1))
+
+ except OSError:
+ # Skip files/directories that can't be accessed
+ continue
+
+ except OSError:
+ # Skip directories that can't be accessed
+ continue
+
+ # Sort results by relevance (exact matches first, then by name)
+ def sort_key(file_info: FileInfo) -> tuple[int, str]:
+ name_lower = file_info.name.lower()
+ query_lower = query.lower()
+
+ # Exact match gets highest priority
+ if name_lower == query_lower:
+ return (0, file_info.name)
+ # Starts with query gets second priority
+ elif name_lower.startswith(query_lower):
+ return (1, file_info.name)
+ # Contains query gets lowest priority
+ else:
+ return (2, file_info.name)
+
+ results.sort(key=sort_key)
+ return results[:limit]
+
def open_in_editor(self, path: str) -> bool:
try:
# First try to get editor from environment variable
diff --git a/marimo/_server/models/files.py b/marimo/_server/models/files.py
index 6f81a190fc..308ca735b0 100644
--- a/marimo/_server/models/files.py
+++ b/marimo/_server/models/files.py
@@ -50,6 +50,21 @@ class FileCreateRequest(msgspec.Struct, rename="camel"):
contents: Optional[str] = None
+class FileSearchRequest(msgspec.Struct, rename="camel"):
+ # The search query string
+ query: str
+ # The root directory path to search from (optional, defaults to root)
+ path: Optional[str] = None
+ # Include directories
+ include_directories: bool = True
+ # Include files
+ include_files: bool = True
+ # Maximum depth to search (default: 3)
+ depth: int = 3
+ # Maximum number of results to return (default: 100)
+ limit: int = 100
+
+
class FileDeleteRequest(msgspec.Struct, rename="camel"):
# The path of the file or directory to delete
path: str
@@ -101,3 +116,9 @@ class FileMoveResponse(BaseResponse):
# Additional information, e.g., error message
message: Optional[str] = None
info: Optional[FileInfo] = None
+
+
+class FileSearchResponse(msgspec.Struct, rename="camel"):
+ files: list[FileInfo]
+ query: str
+ total_found: int
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index bb8ded0058..1db9e450b7 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -1390,6 +1390,47 @@ components:
- path
title: FileOpenRequest
type: object
+ FileSearchRequest:
+ properties:
+ depth:
+ default: 3
+ type: integer
+ includeDirectories:
+ default: true
+ type: boolean
+ includeFiles:
+ default: true
+ type: boolean
+ limit:
+ default: 100
+ type: integer
+ path:
+ anyOf:
+ - type: string
+ - type: 'null'
+ default: null
+ query:
+ type: string
+ required:
+ - query
+ title: FileSearchRequest
+ type: object
+ FileSearchResponse:
+ properties:
+ files:
+ items:
+ $ref: '#/components/schemas/FileInfo'
+ type: array
+ query:
+ type: string
+ totalFound:
+ type: integer
+ required:
+ - files
+ - query
+ - totalFound
+ title: FileSearchResponse
+ type: object
FileUpdateRequest:
properties:
contents:
@@ -3673,6 +3714,20 @@ paths:
schema:
$ref: '#/components/schemas/BaseResponse'
description: Open a file in the system editor
+ /api/files/search:
+ post:
+ requestBody:
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/FileSearchRequest'
+ responses:
+ '200':
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/FileSearchResponse'
+ description: Search for files and directories matching a query
/api/files/update:
post:
requestBody:
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index c9a58e1a82..f4fe394ca4 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -902,6 +902,45 @@ export interface paths {
patch?: never;
trace?: never;
};
+ "/api/files/search": {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ get?: never;
+ put?: never;
+ post: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: {
+ content: {
+ "application/json": components["schemas"]["FileSearchRequest"];
+ };
+ };
+ responses: {
+ /** @description Search for files and directories matching a query */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": components["schemas"]["FileSearchResponse"];
+ };
+ };
+ };
+ };
+ delete?: never;
+ options?: never;
+ head?: never;
+ patch?: never;
+ trace?: never;
+ };
"/api/files/update": {
parameters: {
query?: never;
@@ -3308,6 +3347,26 @@ export interface components {
FileOpenRequest: {
path: string;
};
+ /** FileSearchRequest */
+ FileSearchRequest: {
+ /** @default 3 */
+ depth?: number;
+ /** @default true */
+ includeDirectories?: boolean;
+ /** @default true */
+ includeFiles?: boolean;
+ /** @default 100 */
+ limit?: number;
+ /** @default null */
+ path?: string | null;
+ query: string;
+ };
+ /** FileSearchResponse */
+ FileSearchResponse: {
+ files: components["schemas"]["FileInfo"][];
+ query: string;
+ totalFound: number;
+ };
/** FileUpdateRequest */
FileUpdateRequest: {
contents: string;
diff --git a/tests/_ai/test_chat_convert.py b/tests/_ai/test_chat_convert.py
index c5ceaeea0f..144bec7cd7 100644
--- a/tests/_ai/test_chat_convert.py
+++ b/tests/_ai/test_chat_convert.py
@@ -100,7 +100,7 @@ def test_convert_to_openai_messages_with_parts():
),
ReasoningPart(
type="reasoning",
- reasoning="Deep thinking process",
+ text="Deep thinking process",
details=[
ReasoningDetails(
type="text", text="Analysis", signature=None
@@ -131,7 +131,7 @@ def test_convert_to_openai_messages_with_parts():
{"type": "text", "text": "Message with parts and attachments"},
{
"type": "reasoning",
- "reasoning": "Deep thinking process",
+ "text": "Deep thinking process",
"details": [
{"type": "text", "text": "Analysis", "signature": None}
],
@@ -352,9 +352,7 @@ def test_from_chat_message_dict():
assert result_with_parts.parts[0].type == "text"
assert result_with_parts.parts[0].text == "This is a text part"
assert result_with_parts.parts[1].type == "reasoning"
- assert (
- result_with_parts.parts[1].reasoning == "This is my reasoning process"
- )
+ assert result_with_parts.parts[1].text == "This is my reasoning process"
# Test case 4: ChatMessage with tool invocation part (result state only)
message_dict_tool_result = {
@@ -658,7 +656,7 @@ def test_get_anthropic_parts_from_chat_parts_with_reasoning():
TextPart(type="text", text="Let me think about this"),
ReasoningPart(
type="reasoning",
- reasoning="My thinking process here",
+ text="My thinking process here",
details=reasoning_details,
),
]
@@ -683,7 +681,7 @@ def test_get_anthropic_parts_from_chat_parts_reasoning_no_signature():
parts = [
ReasoningPart(
type="reasoning",
- reasoning="Basic reasoning",
+ text="Basic reasoning",
details=reasoning_details,
),
]
@@ -703,7 +701,7 @@ def test_get_anthropic_parts_from_empty_reasoning_details_chat_part():
parts = [
ReasoningPart(
type="reasoning",
- reasoning="Some reasoning",
+ text="Some reasoning",
details=[], # Empty details
),
]
@@ -794,7 +792,7 @@ def test_get_google_messages_from_parts_with_reasoning():
TextPart(type="text", text="Regular text"),
ReasoningPart(
type="reasoning",
- reasoning="Deep thinking process",
+ text="Deep thinking process",
details=reasoning_details,
),
]
diff --git a/tests/_server/api/endpoints/test_file_explorer.py b/tests/_server/api/endpoints/test_file_explorer.py
index bf39c25ecd..fcc9586c2b 100644
--- a/tests/_server/api/endpoints/test_file_explorer.py
+++ b/tests/_server/api/endpoints/test_file_explorer.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import os
+from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
@@ -113,3 +114,130 @@ def test_open_file(client: TestClient) -> None:
assert response.status_code == 200, response.text
assert response.headers["content-type"] == "application/json"
assert "success" in response.json()
+
+
+def test_search_files_basic(client: TestClient, tmp_path: Path) -> None:
+ """Test basic file search functionality."""
+ response = client.post(
+ "/api/files/search",
+ headers=HEADERS,
+ json={"query": "test", "path": str(tmp_path)},
+ )
+ assert response.status_code == 200, response.text
+ assert response.headers["content-type"] == "application/json"
+ data = response.json()
+ assert "files" in data
+ assert "query" in data
+ assert "totalFound" in data
+ assert data["query"] == "test"
+ assert isinstance(data["files"], list)
+ assert isinstance(data["totalFound"], int)
+
+
+def test_search_files_with_matches(client: TestClient, tmp_path: Path) -> None:
+ """Test search returns expected matches."""
+ # Create test structure
+ search_dir = tmp_path
+
+ # Create test files
+ Path(search_dir, "hello.txt").write_text("content")
+ Path(search_dir, "world.txt").write_text("content")
+ Path(search_dir, "hello_world.py").write_text("print('hello')")
+
+ # Create subdirectory with files
+ sub_dir = Path(search_dir, "subdir")
+ sub_dir.mkdir()
+ Path(sub_dir, "hello.md").write_text("# Hello")
+
+ response = client.post(
+ "/api/files/search",
+ headers=HEADERS,
+ json={"query": "hello", "path": str(search_dir), "depth": 2},
+ )
+ assert response.status_code == 200, response.text
+ data = response.json()
+
+ # Should find hello.txt, hello_world.py, and hello.md
+ assert data["totalFound"] >= 2
+ file_names = [f["name"] for f in data["files"]]
+ assert "hello.txt" in file_names
+ assert "hello_world.py" in file_names
+
+
+def test_search_files_with_directory_and_file_filters(
+ client: TestClient, tmp_path: Path
+) -> None:
+ """Test directory and file filtering parameters in API."""
+ # Create test structure
+ (tmp_path / "test_file.txt").write_text("content")
+ (tmp_path / "test_file.py").write_text("# test content")
+ test_subdir = tmp_path / "test_dir"
+ test_subdir.mkdir()
+ (test_subdir / "nested_file.txt").write_text("content")
+
+ # Test searching for files only
+ response = client.post(
+ "/api/files/search",
+ headers=HEADERS,
+ json={
+ "query": "test",
+ "path": str(tmp_path),
+ "includeFiles": True,
+ "includeDirectories": False,
+ },
+ )
+ assert response.status_code == 200, response.text
+ data = response.json()
+
+ # Should only find files
+ for file_info in data["files"]:
+ assert not file_info["isDirectory"], (
+ f"Found directory {file_info['name']} when includeFiles=True, includeDirectories=False"
+ )
+ assert data["totalFound"] >= 2 # Should find test files
+
+ # Test searching for directories only
+ response = client.post(
+ "/api/files/search",
+ headers=HEADERS,
+ json={
+ "query": "test",
+ "path": str(tmp_path),
+ "includeFiles": False,
+ "includeDirectories": True,
+ },
+ )
+ assert response.status_code == 200, response.text
+ data = response.json()
+
+ # Should only find directories
+ for file_info in data["files"]:
+ assert file_info["isDirectory"], (
+ f"Found file {file_info['name']} when includeFiles=False, includeDirectories=True"
+ )
+ assert data["totalFound"] >= 1 # Should find test_dir
+
+ # Test with both file and directory filters as false
+ response = client.post(
+ "/api/files/search",
+ headers=HEADERS,
+ json={
+ "query": "test",
+ "path": str(tmp_path),
+ "includeFiles": False,
+ "includeDirectories": False,
+ },
+ )
+ assert response.status_code == 200, response.text
+ data_both_false = response.json()
+ assert data_both_false["totalFound"] == 0
+
+ # Compare with no filter
+ response = client.post(
+ "/api/files/search",
+ headers=HEADERS,
+ json={"query": "test", "path": str(tmp_path)},
+ )
+ assert response.status_code == 200, response.text
+ data_no_filter = response.json()
+ assert data_no_filter["totalFound"] == 3
diff --git a/tests/_server/files/test_os_file_system.py b/tests/_server/files/test_os_file_system.py
index 6bab7973a9..09099b02bb 100644
--- a/tests/_server/files/test_os_file_system.py
+++ b/tests/_server/files/test_os_file_system.py
@@ -286,3 +286,265 @@ def test_get_details_non_utf8_encoding_and_contents(
str(file_path), encoding="utf-8", contents="override"
)
assert result2.contents == "override"
+
+
+def test_search_basic(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test basic search functionality."""
+ # Create test files
+ (test_dir / "hello.txt").write_text("content")
+ (test_dir / "world.txt").write_text("content")
+ (test_dir / "hello_world.py").write_text("print('hello')")
+
+ # Search for "hello"
+ results = fs.search(query="hello", path=str(test_dir))
+
+ # Should find hello.txt and hello_world.py
+ assert len(results) >= 2
+ file_names = {f.name for f in results}
+ assert "hello.txt" in file_names
+ assert "hello_world.py" in file_names
+ assert "world.txt" not in file_names
+
+
+def test_search_empty_query(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test search with empty query returns no results."""
+ (test_dir / "test.txt").write_text("content")
+
+ results = fs.search(query="", path=str(test_dir))
+ assert results == []
+
+ results = fs.search(query=" ", path=str(test_dir))
+ assert results == []
+
+
+def test_search_case_insensitive(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test search is case insensitive."""
+ # Create files with mixed case
+ (test_dir / "CamelCase.txt").write_text("content")
+ (test_dir / "lowercase.txt").write_text("content")
+ (test_dir / "UPPERCASE.txt").write_text("content")
+
+ results = fs.search(query="case", path=str(test_dir))
+
+ file_names = {f.name for f in results}
+ assert "CamelCase.txt" in file_names
+ assert "lowercase.txt" in file_names
+ assert "UPPERCASE.txt" in file_names
+
+
+def test_search_with_depth_limit(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test search respects depth limit."""
+ # Create files at different depths
+ (test_dir / "root_match.txt").write_text("content")
+
+ # Level 1
+ level1 = test_dir / "level1"
+ level1.mkdir()
+ (level1 / "level1_match.txt").write_text("content")
+
+ # Level 2
+ level2 = level1 / "level2"
+ level2.mkdir()
+ (level2 / "level2_match.txt").write_text("content")
+
+ # Level 3
+ level3 = level2 / "level3"
+ level3.mkdir()
+ (level3 / "level3_match.txt").write_text("content")
+
+ # Search with depth=1 - should find root and level1 files only
+ results = fs.search(query="match", path=str(test_dir), depth=1)
+ file_names = {f.name for f in results}
+ assert "root_match.txt" in file_names
+ assert "level1_match.txt" in file_names
+ assert "level2_match.txt" not in file_names
+ assert "level3_match.txt" not in file_names
+
+ # Search with depth=2 - should find up to level2
+ results = fs.search(query="match", path=str(test_dir), depth=2)
+ file_names = {f.name for f in results}
+ assert "root_match.txt" in file_names
+ assert "level1_match.txt" in file_names
+ assert "level2_match.txt" in file_names
+ assert "level3_match.txt" not in file_names
+
+
+def test_search_with_limit(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test search respects result limit."""
+ # Create many matching files
+ for i in range(10):
+ (test_dir / f"test_{i}.txt").write_text("content")
+
+ # Search with limit=3
+ results = fs.search(query="test", path=str(test_dir), limit=3)
+ assert len(results) <= 3
+
+ # Search with limit=5
+ results = fs.search(query="test", path=str(test_dir), limit=5)
+ assert len(results) <= 5
+
+
+def test_search_result_ordering(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test search results are ordered by relevance."""
+ # Create files with different match types
+ (test_dir / "test").mkdir() # exact match (directory)
+ (test_dir / "test.txt").write_text("content") # exact match (file)
+ (test_dir / "test_file.py").write_text("content") # starts with
+ (test_dir / "my_test.txt").write_text("content") # contains
+ (test_dir / "another_test_file.py").write_text("content") # contains
+
+ results = fs.search(query="test", path=str(test_dir))
+
+ # Results should be ordered by relevance
+ file_names = [f.name for f in results]
+
+ # Exact matches should come first
+ exact_matches = [
+ name for name in file_names if name == "test" or name == "test.txt"
+ ]
+ assert len(exact_matches) >= 1
+
+ # "test.txt" should come before "my_test.txt"
+ test_txt_idx = file_names.index("test.txt")
+ my_test_idx = file_names.index("my_test.txt")
+ assert test_txt_idx < my_test_idx
+
+
+def test_search_include_directories(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test search includes both files and directories."""
+ # Create matching directory and file
+ (test_dir / "testdir").mkdir()
+ (test_dir / "testfile.txt").write_text("content")
+
+ results = fs.search(query="test", path=str(test_dir))
+
+ has_directory = any(f.is_directory for f in results)
+ has_file = any(not f.is_directory for f in results)
+ assert has_directory
+ assert has_file
+
+
+def test_search_respects_ignore_list(test_dir: Path, fs: OSFileSystem) -> None:
+ """Test search respects the ignore list."""
+ # Create files that should be ignored
+ (test_dir / ".DS_Store").write_text("content")
+ (test_dir / "__pycache__").mkdir()
+ (test_dir / "node_modules").mkdir()
+ (test_dir / "regular_file.txt").write_text("content")
+
+ # Search for anything - should not find ignored items
+ results = fs.search(
+ query="", path=str(test_dir)
+ ) # Empty query to test ignore functionality
+ # Since empty query returns empty list, let's search for a pattern that could match
+ results = fs.search(
+ query="_", path=str(test_dir)
+ ) # Should match __pycache__ if not ignored
+
+ file_names = {f.name for f in results}
+ assert ".DS_Store" not in file_names
+ assert "__pycache__" not in file_names
+ assert "node_modules" not in file_names
+
+
+def test_search_directory_and_file_filters(
+ test_dir: Path, fs: OSFileSystem
+) -> None:
+ """Test directory and file filtering parameters work correctly."""
+ # Create test files and directories
+ (test_dir / "test_file.txt").write_text("content")
+ (test_dir / "test_file.py").write_text("# test content")
+ test_subdir = test_dir / "test_dir"
+ test_subdir.mkdir()
+ (test_subdir / "nested_file.txt").write_text("content")
+
+ # Test searching for files only
+ results = fs.search(
+ query="test",
+ path=str(test_dir),
+ include_files=True,
+ include_directories=False,
+ )
+ file_results = [f for f in results if not f.is_directory]
+ dir_results = [f for f in results if f.is_directory]
+ assert len(file_results) > 0, "Should find files when file=True"
+ assert len(dir_results) == 0, "Should not find directories when file=True"
+
+ # Test searching for directories only
+ results = fs.search(
+ query="test",
+ path=str(test_dir),
+ include_directories=True,
+ include_files=False,
+ )
+ file_results = [f for f in results if not f.is_directory]
+ dir_results = [f for f in results if f.is_directory]
+ assert len(dir_results) > 0, "Should find directories when directory=True"
+ assert len(file_results) == 0, "Should not find files when directory=True"
+
+ # Test searching for both (default behavior)
+ results = fs.search(query="test", path=str(test_dir))
+ file_results = [f for f in results if not f.is_directory]
+ dir_results = [f for f in results if f.is_directory]
+ assert len(file_results) > 0, "Should find files with no filter"
+ assert len(dir_results) > 0, "Should find directories with no filter"
+
+ results_no_filter = fs.search(query="test", path=str(test_dir))
+ results_both_true = fs.search(
+ query="test",
+ path=str(test_dir),
+ include_files=True,
+ include_directories=True,
+ )
+ assert len(results_no_filter) == len(results_both_true), (
+ "Both true should be same as no filter"
+ )
+
+
+def test_search_handles_permission_errors(
+ test_dir: Path, fs: OSFileSystem
+) -> None:
+ """Test search gracefully handles permission errors."""
+ # Create a regular file that we can access
+ (test_dir / "accessible.txt").write_text("content")
+
+ # Search should not crash even if there are permission issues
+ results = fs.search(query="accessible", path=str(test_dir))
+ file_names = {f.name for f in results}
+ assert "accessible.txt" in file_names
+
+
+def test_search_nonexistent_path(fs: OSFileSystem) -> None:
+ """Test search with nonexistent path returns empty results."""
+ results = fs.search(query="test", path="/nonexistent/path")
+ assert results == []
+
+
+def test_search_default_path(fs: OSFileSystem, monkeypatch) -> None:
+ """Test search uses root path when path is None."""
+ # Mock get_root to return a known directory
+ test_root = "/tmp"
+ monkeypatch.setattr(fs, "get_root", lambda: test_root)
+
+ # This should not crash (though results may be empty)
+ results = fs.search(query="test", path=None)
+ assert isinstance(results, list)
+
+
+def test_search_includes_file_metadata(
+ test_dir: Path, fs: OSFileSystem
+) -> None:
+ """Test search results include proper file metadata."""
+ test_file = test_dir / "metadata_test.txt"
+ test_file.write_text("content")
+
+ results = fs.search(query="metadata", path=str(test_dir))
+
+ assert len(results) == 1
+ file_info = results[0]
+ assert file_info.name == "metadata_test.txt"
+ assert file_info.path == str(test_file)
+ assert file_info.id == str(test_file)
+ assert file_info.is_directory is False
+ assert file_info.is_marimo_file is False
+ assert file_info.last_modified is not None