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.

improvement: better UX for AI context items (#6649)

Myles Scolnick192f61d814d9
diff --git a/frontend/src/components/chat/acp/__tests__/context-utils.test.ts b/frontend/src/components/chat/acp/__tests__/context-utils.test.ts
index 9618303f42..1cf52d8579 100644
--- a/frontend/src/components/chat/acp/__tests__/context-utils.test.ts
+++ b/frontend/src/components/chat/acp/__tests__/context-utils.test.ts
@@ -133,7 +133,7 @@ describe("parseContextFromPrompt", () => {
type: "resource",
resource: {
uri: "context.md",
- mimeType: "text/markdown",
+ mimeType: "text/plain",
text: "formatted context",
},
});
diff --git a/frontend/src/components/chat/acp/agent-panel.tsx b/frontend/src/components/chat/acp/agent-panel.tsx
index 32a4029b36..468f3fe0ac 100644
--- a/frontend/src/components/chat/acp/agent-panel.tsx
+++ b/frontend/src/components/chat/acp/agent-panel.tsx
@@ -867,7 +867,7 @@ const AgentPanel: React.FC = () => {
type: "resource",
resource: {
uri: "marimo_rules.md",
- mimeType: "text/markdown",
+ mimeType: "text/plain",
text: getAgentPrompt(filename),
},
},
diff --git a/frontend/src/components/chat/acp/blocks.tsx b/frontend/src/components/chat/acp/blocks.tsx
index 6c40491501..72ad9df2a9 100644
--- a/frontend/src/components/chat/acp/blocks.tsx
+++ b/frontend/src/components/chat/acp/blocks.tsx
@@ -30,7 +30,6 @@ import {
} from "@/components/ui/popover";
import { logNever } from "@/utils/assertNever";
import { cn } from "@/utils/cn";
-import { type Base64String, base64ToDataURL } from "@/utils/json/base64";
import { Strings } from "@/utils/strings";
import { MarkdownRenderer } from "../markdown-renderer";
import { SimpleAccordion } from "./common";
@@ -289,7 +288,7 @@ export const PlansBlock = (props: { data: PlanNotificationEvent[] }) => {
export const UserMessagesBlock = (props: { data: UserNotificationEvent[] }) => {
return (
- <div className="flex flex-col gap-2 text-muted-foreground border p-2 bg-background rounded break-words">
+ <div className="flex flex-col gap-2 text-muted-foreground border p-2 bg-background rounded break-words overflow-x-hidden">
<ContentBlocks data={props.data.map((item) => item.content)} />
</div>
);
@@ -364,58 +363,28 @@ export const ResourceBlock = (props: { data: ContentBlockOf<"resource"> }) => {
return (
<Popover>
<PopoverTrigger>
- <span className="flex items-center gap-1">
+ <span className="flex items-center gap-1 hover:bg-muted rounded-md px-1">
{props.data.resource.mimeType && (
<MimeIcon mimeType={props.data.resource.mimeType} />
)}
{props.data.resource.uri}
</span>
</PopoverTrigger>
- <PopoverContent className="max-h-96 overflow-y-auto scrollbar-thin">
- <MarkdownRenderer content={props.data.resource.text} />
+ <PopoverContent className="max-h-96 overflow-y-auto scrollbar-thin whitespace-pre-wrap w-full max-w-[500px]">
+ <span className="text-muted-foreground text-xs mb-1 italic">
+ Formatted for agents, not humans.
+ </span>
+ {props.data.resource.mimeType === "text/plain" ? (
+ <pre className="text-xs whitespace-pre-wrap p-2 bg-muted rounded-md break-words">
+ {props.data.resource.text}
+ </pre>
+ ) : (
+ <MarkdownRenderer content={props.data.resource.text} />
+ )}
</PopoverContent>
</Popover>
);
}
-
- if ("blob" in props.data.resource) {
- if (props.data.resource.mimeType?.startsWith("image/")) {
- return (
- <ImageBlock
- data={{
- type: "image",
- mimeType: props.data.resource.mimeType,
- data: props.data.resource.blob,
- }}
- />
- );
- }
- if (props.data.resource.mimeType?.startsWith("audio/")) {
- return (
- <AudioBlock
- data={{
- type: "audio",
- mimeType: props.data.resource.mimeType,
- data: props.data.resource.blob,
- }}
- />
- );
- }
- const dataURL = base64ToDataURL(
- props.data.resource.blob as Base64String,
- props.data.resource.mimeType ?? "",
- );
- return (
- <a href={dataURL} className="flex items-center gap-1" download={true}>
- {props.data.resource.mimeType && (
- <MimeIcon mimeType={props.data.resource.mimeType} />
- )}
- {props.data.resource.uri}
- </a>
- );
- }
- logNever(props.data.resource);
- return null;
};
export const ResourceLinkBlock = (props: {
@@ -427,15 +396,38 @@ export const ResourceLinkBlock = (props: {
href={props.data.uri}
target="_blank"
rel="noopener noreferrer"
- className="text-link hover:underline"
+ className="text-link hover:underline px-1"
>
{props.data.name}
</a>
);
}
+ // Show image in popover for image mime types
+ if (props.data.mimeType?.startsWith("image/")) {
+ return (
+ <div>
+ <Popover>
+ <PopoverTrigger>
+ <span className="flex items-center gap-1 hover:bg-muted rounded-md px-1 cursor-pointer">
+ <MimeIcon mimeType={props.data.mimeType} />
+ {props.data.name || props.data.title || props.data.uri}
+ </span>
+ </PopoverTrigger>
+ <PopoverContent className="w-auto max-w-[500px] p-2">
+ <img
+ src={props.data.uri}
+ alt={props.data.name || props.data.title || "Image"}
+ className="max-w-full max-h-96 object-contain"
+ />
+ </PopoverContent>
+ </Popover>
+ </div>
+ );
+ }
+
return (
- <span className="flex items-center gap-1">
+ <span className="flex items-center gap-1 px-1">
{props.data.mimeType && <MimeIcon mimeType={props.data.mimeType} />}
{props.data.name || props.data.title || props.data.uri}
</span>
@@ -443,22 +435,23 @@ export const ResourceLinkBlock = (props: {
};
export const MimeIcon = (props: { mimeType: string }) => {
+ const classNames = "h-2 w-2 flex-shrink-0";
if (props.mimeType.startsWith("image/")) {
- return <FileImageIcon className="h-2 w-2" />;
+ return <FileImageIcon className={classNames} />;
}
if (props.mimeType.startsWith("audio/")) {
- return <FileAudio2Icon className="h-2 w-2" />;
+ return <FileAudio2Icon className={classNames} />;
}
if (props.mimeType.startsWith("video/")) {
- return <FileVideoCameraIcon className="h-2 w-2" />;
+ return <FileVideoCameraIcon className={classNames} />;
}
if (props.mimeType.startsWith("text/")) {
- return <FileTextIcon className="h-2 w-2" />;
+ return <FileTextIcon className={classNames} />;
}
if (props.mimeType.startsWith("application/")) {
- return <FileJsonIcon className="h-2 w-2" />;
+ return <FileJsonIcon className={classNames} />;
}
- return <FileIcon className="h-2 w-2" />;
+ return <FileIcon className={classNames} />;
};
export const SessionNotificationsBlock = <
@@ -533,7 +526,7 @@ export const ToolNotificationsBlock = (props: {
const toolCalls = mergeToolCalls(props.data);
return (
- <div className="flex flex-col text-muted-foreground">
+ <div className="flex flex-col text-muted-foreground overflow-x-hidden">
{toolCalls.map((item) => (
<SimpleAccordion
key={item.toolCallId}
diff --git a/frontend/src/components/chat/acp/common.tsx b/frontend/src/components/chat/acp/common.tsx
index 11bee411c9..fa7a3859b0 100644
--- a/frontend/src/components/chat/acp/common.tsx
+++ b/frontend/src/components/chat/acp/common.tsx
@@ -68,7 +68,7 @@ export const SimpleAccordion: React.FC<SimpleAccordionProps> = ({
>
<span className="flex items-center gap-1">
{getStatusIcon()}
- <code className="font-mono text-xs">{title}</code>
+ <code className="font-mono text-xs truncate">{title}</code>
</span>
</AccordionTrigger>
<AccordionContent className="p-2">
diff --git a/frontend/src/components/chat/acp/context-utils.ts b/frontend/src/components/chat/acp/context-utils.ts
index 9b9a6d2283..601a109fc2 100644
--- a/frontend/src/components/chat/acp/context-utils.ts
+++ b/frontend/src/components/chat/acp/context-utils.ts
@@ -93,7 +93,7 @@ export async function parseContextFromPrompt(
type: "resource",
resource: {
uri: "context.md",
- mimeType: "text/markdown",
+ mimeType: "text/plain",
text: contextString,
},
});
diff --git a/frontend/src/components/chat/acp/session-tabs.tsx b/frontend/src/components/chat/acp/session-tabs.tsx
index bdd993830c..4f800e07e3 100644
--- a/frontend/src/components/chat/acp/session-tabs.tsx
+++ b/frontend/src/components/chat/acp/session-tabs.tsx
@@ -30,7 +30,7 @@ const SessionTab: React.FC<SessionTabProps> = memo(
<div
className={cn(
"flex items-center gap-1 px-2 py-1 text-xs border-r border-border bg-muted/30 hover:bg-muted/50 cursor-pointer min-w-0",
- isActive && "bg-background border-b-0 relative z-10",
+ isActive && "bg-background border-b-0 relative z-1",
)}
onClick={() => onSelect(session.tabId)}
>
diff --git a/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts b/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
index be69444d6b..8a0b2557e8 100644
--- a/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
+++ b/frontend/src/components/editor/ai/__tests__/completion-utils.test.ts
@@ -55,15 +55,13 @@ describe("getAICompletionBody", () => {
expect(result).toMatchInlineSnapshot(`
{
"context": {
- "plainText": "<data name="dataset1" source="unknown">
- Columns:
- - col1: number
- - col2: string</data>
-
- <data name="dataset2" source="unknown">
- Columns:
- - col3: boolean
- - col4: date</data>",
+ "plainText": "<data name="dataset1" source="unknown">Columns:
+ col1 (number)
+ col2 (string)</data>
+
+ <data name="dataset2" source="unknown">Columns:
+ col3 (boolean)
+ col4 (date)</data>",
"schema": [],
"variables": [],
},
@@ -108,10 +106,9 @@ describe("getAICompletionBody", () => {
expect(result).toMatchInlineSnapshot(`
{
"context": {
- "plainText": "<data name="existingDataset" source="unknown">
- Columns:
- - col1: number
- - col2: string</data>",
+ "plainText": "<data name="existingDataset" source="unknown">Columns:
+ col1 (number)
+ col2 (string)</data>",
"schema": [],
"variables": [],
},
@@ -144,14 +141,12 @@ describe("getAICompletionBody", () => {
expect(result).toMatchInlineSnapshot(`
{
"context": {
- "plainText": "<data name="dataset.with.dots" source="unknown">
- Columns:
- - col1: number
- - col2: string</data>
-
- <data name="regular_dataset" source="unknown">
- Columns:
- - col3: boolean</data>",
+ "plainText": "<data name="dataset.with.dots" source="unknown">Columns:
+ col1 (number)
+ col2 (string)</data>
+
+ <data name="regular_dataset" source="unknown">Columns:
+ col3 (boolean)</data>",
"schema": [],
"variables": [],
},
@@ -198,9 +193,8 @@ describe("getAICompletionBody", () => {
expect(result).toMatchInlineSnapshot(`
{
"context": {
- "plainText": "<data name="table1" source="unknown">
- Columns:
- - col1: number</data>",
+ "plainText": "<data name="table1" source="unknown">Columns:
+ col1 (number)</data>",
"schema": [],
"variables": [],
},
@@ -276,10 +270,9 @@ describe("getAICompletionBody", () => {
expect(result).toMatchInlineSnapshot(`
{
"context": {
- "plainText": "<data name="dataset1" source="unknown">
- Columns:
- - col1: number
- - col2: string</data>
+ "plainText": "<data name="dataset1" source="unknown">Columns:
+ col1 (number)
+ col2 (string)</data>
<variable name="var1" dataType="string">"string value"</variable>",
"schema": [],
@@ -346,9 +339,8 @@ describe("getAICompletionBody", () => {
expect(result).toMatchInlineSnapshot(`
{
"context": {
- "plainText": "<data name="conflict" source="unknown">
- Columns:
- - col1: number</data>",
+ "plainText": "<data name="conflict" source="unknown">Columns:
+ col1 (number)</data>",
"schema": [],
"variables": [],
},
diff --git a/frontend/src/core/ai/context/providers/__tests__/__snapshots__/tables.test.ts.snap b/frontend/src/core/ai/context/providers/__tests__/__snapshots__/tables.test.ts.snap
index 6fe0704353..ca378d7242 100644
--- a/frontend/src/core/ai/context/providers/__tests__/__snapshots__/tables.test.ts.snap
+++ b/frontend/src/core/ai/context/providers/__tests__/__snapshots__/tables.test.ts.snap
@@ -1,34 +1,28 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`TableContextProvider > formatContext > should format context for basic table > basic-table-context 1`] = `
-"<data name="products" source="memory">
-Shape: 100 rows, 3 columns
+"<data name="products" source="memory">Shape: 100 rows, 3 columns
Columns:
- - id: integer
- - name: string
- - active: boolean</data>"
+ id (integer) - samples: [sample_id_1, sample_id_2]
+ name (string) - samples: [sample_name_1, sample_name_2]
+ active (boolean) - samples: [sample_active_1, sample_active_2]</data>"
`;
exports[`TableContextProvider > formatContext > should format context for remote database table > remote-table-context 1`] = `
-"<data name="remote_table" source="postgresql://localhost:5432/mydb">
-Shape: 100 rows, 3 columns
+"<data name="remote_table" source="postgresql://localhost:5432/mydb">Shape: 100 rows, 3 columns
Columns:
- - uuid: string
- - created_at: string
- - metadata: string</data>"
+ uuid (string) - samples: [sample_uuid_1, sample_uuid_2]
+ created_at (string) - samples: [sample_created_at_1, sample_created_at_2]
+ metadata (string) - samples: [sample_metadata_1, sample_metadata_2]</data>"
`;
-exports[`TableContextProvider > formatContext > should format context for table without columns > no-columns-table-context 1`] = `
-"<data name="no_columns" source="memory">
-Shape: 100 rows, 3 columns</data>"
-`;
+exports[`TableContextProvider > formatContext > should format context for table without columns > no-columns-table-context 1`] = `"<data name="no_columns" source="memory">Shape: 100 rows, 3 columns</data>"`;
exports[`TableContextProvider > formatContext > should format context for table without shape info > no-shape-table-context 1`] = `
-"<data name="no_shape" source="memory">
-Columns:
- - id: integer
- - name: string
- - active: boolean</data>"
+"<data name="no_shape" source="memory">Columns:
+ id (integer) - samples: [sample_id_1, sample_id_2]
+ name (string) - samples: [sample_name_1, sample_name_2]
+ active (boolean) - samples: [sample_active_1, sample_active_2]</data>"
`;
exports[`TableContextProvider > getItems > should handle dataframe tables with variable names > dataframe-table 1`] = `
diff --git a/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts b/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts
index fa87c4e5af..958b758f8d 100644
--- a/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts
+++ b/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts
@@ -187,7 +187,6 @@ describe("CellOutputContextProvider", () => {
expect(completion.displayLabel).toBe(item.data.cellName);
expect(completion.detail).toContain("output");
expect(completion.type).toBe("cell-output");
- expect(completion.section).toBe("Cell Output");
expect(typeof completion.info).toBe("function");
});
});
diff --git a/frontend/src/core/ai/context/providers/__tests__/datasource.test.ts b/frontend/src/core/ai/context/providers/__tests__/datasource.test.ts
index 40cfb0f950..1521922438 100644
--- a/frontend/src/core/ai/context/providers/__tests__/datasource.test.ts
+++ b/frontend/src/core/ai/context/providers/__tests__/datasource.test.ts
@@ -8,7 +8,7 @@ import type {
} from "@/core/datasets/data-source-connections";
import { DUCKDB_ENGINE } from "@/core/datasets/engines";
import type { DataSourceConnection, DataTable } from "@/core/kernel/messages";
-import { Boosts } from "../common";
+import { Boosts, Sections } from "../common";
import { DatasourceContextProvider } from "../datasource";
// Mock data for testing
@@ -268,8 +268,7 @@ describe("DatasourceContextProvider", () => {
);
const items = providerWithEmpty.getItems();
- expect(items).toHaveLength(1);
- expect(items[0].data.connection.databases).toEqual([]);
+ expect(items).toHaveLength(0);
});
it("should handle connections with databases but no schemas", () => {
@@ -311,7 +310,7 @@ describe("DatasourceContextProvider", () => {
detail: "DuckDB",
boost: Boosts.MEDIUM,
type: "datasource",
- section: "Data Sources",
+ section: Sections.DATA_SOURCES,
});
expect(completion.info).toBeDefined();
@@ -355,7 +354,7 @@ describe("DatasourceContextProvider", () => {
detail: "PostgreSQL",
boost: Boosts.MEDIUM,
type: "datasource",
- section: "Data Sources",
+ section: Sections.DATA_SOURCES,
});
});
@@ -380,7 +379,7 @@ describe("DatasourceContextProvider", () => {
detail: "DuckDB",
boost: Boosts.MEDIUM,
type: "datasource",
- section: "Data Sources",
+ section: Sections.DATA_SOURCES,
});
});
});
diff --git a/frontend/src/core/ai/context/providers/__tests__/error.test.ts b/frontend/src/core/ai/context/providers/__tests__/error.test.ts
index 85cb71145d..e416e3f42b 100644
--- a/frontend/src/core/ai/context/providers/__tests__/error.test.ts
+++ b/frontend/src/core/ai/context/providers/__tests__/error.test.ts
@@ -6,7 +6,6 @@ import { beforeEach, describe, expect, it } from "vitest";
import { MockNotebook } from "@/__mocks__/notebook";
import { notebookAtom } from "@/core/cells/cells";
import { type CellId, CellId as CellIdClass } from "@/core/cells/ids";
-import { Boosts } from "../common";
import { ErrorContextProvider } from "../error";
describe("ErrorContextProvider", () => {
@@ -118,15 +117,20 @@ describe("ErrorContextProvider", () => {
const items = provider.getItems();
const completion = provider.formatCompletion(items[0]);
- expect(completion).toMatchObject({
- label: "@Errors",
- displayLabel: "Errors",
- detail: "2 errors",
- boost: Boosts.ERROR,
- type: "error",
- apply: "@Errors",
- section: "Errors",
- });
+ expect(completion).toMatchInlineSnapshot(`
+ {
+ "apply": "@Errors",
+ "detail": "2 errors",
+ "displayLabel": "Errors",
+ "info": [Function],
+ "label": "@Errors",
+ "section": {
+ "name": "Error",
+ "rank": 1,
+ },
+ "type": "error",
+ }
+ `);
// Test the info function
expect(completion.info).toBeDefined();
@@ -179,11 +183,16 @@ describe("ErrorContextProvider", () => {
};
const completion = provider.formatCompletion(item);
- expect(completion).toMatchObject({
- label: "Error",
- displayLabel: "Error",
- boost: Boosts.ERROR,
- });
+ expect(completion).toMatchInlineSnapshot(`
+ {
+ "displayLabel": "Error",
+ "label": "Error",
+ "section": {
+ "name": "Error",
+ "rank": 1,
+ },
+ }
+ `);
});
});
diff --git a/frontend/src/core/ai/context/providers/cell-output.ts b/frontend/src/core/ai/context/providers/cell-output.ts
index 89956cfa30..2bfa7afaae 100644
--- a/frontend/src/core/ai/context/providers/cell-output.ts
+++ b/frontend/src/core/ai/context/providers/cell-output.ts
@@ -13,7 +13,7 @@ import { parseHtmlContent } from "@/utils/dom";
import { Logger } from "@/utils/Logger";
import { type AIContextItem, AIContextProvider } from "../registry";
import { contextToXml } from "../utils";
-import { Boosts } from "./common";
+import { Boosts, Sections } from "./common";
export interface CellOutputContextItem extends AIContextItem {
type: "cell-output";
@@ -150,13 +150,14 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
formatCompletion(item: CellOutputContextItem): Completion {
const { data } = item;
+
return {
label: `@${data.cellName}`,
displayLabel: data.cellName,
detail: `${data.outputType} output`,
- boost: Boosts.CELL_OUTPUT,
+ boost: data.outputType === "media" ? Boosts.HIGH : Boosts.MEDIUM,
type: this.contextType,
- section: "Cell Output",
+ section: Sections.CELL_OUTPUT,
apply: `@${data.cellName}`,
info: () => {
const infoContainer = document.createElement("div");
@@ -247,8 +248,7 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConte
"italic",
"mb-2",
);
- mediaDiv.textContent =
- "Contains media content (image, SVG, or canvas)";
+ mediaDiv.textContent = "A screenshot of the output will be attached";
infoContainer.append(mediaDiv);
}
diff --git a/frontend/src/core/ai/context/providers/common.ts b/frontend/src/core/ai/context/providers/common.ts
index d109a9b107..e61097110d 100644
--- a/frontend/src/core/ai/context/providers/common.ts
+++ b/frontend/src/core/ai/context/providers/common.ts
@@ -1,13 +1,22 @@
/* Copyright 2024 Marimo. All rights reserved. */
+import type { CompletionSection } from "@codemirror/autocomplete";
+
/** Number from -99 to 99. Higher numbers are prioritized when surfacing completions. */
export const Boosts = {
- LOCAL_TABLE: 5,
- REMOTE_TABLE: 4,
+ LOCAL_TABLE: 7,
+ REMOTE_TABLE: 5,
HIGH: 4,
- VARIABLE: 3,
MEDIUM: 3,
CELL_OUTPUT: 2,
LOW: 2,
- ERROR: 1,
} as const;
+
+export const Sections = {
+ ERROR: { name: "Error", rank: 1 },
+ TABLE: { name: "Table", rank: 2 },
+ DATA_SOURCES: { name: "Data Sources", rank: 3 },
+ VARIABLE: { name: "Variable", rank: 4 },
+ CELL_OUTPUT: { name: "Cell Output", rank: 5 },
+ FILE: { name: "File", rank: 6 },
+} satisfies Record<string, CompletionSection>;
diff --git a/frontend/src/core/ai/context/providers/datasource.ts b/frontend/src/core/ai/context/providers/datasource.ts
index e3701f7ad8..e37ce32328 100644
--- a/frontend/src/core/ai/context/providers/datasource.ts
+++ b/frontend/src/core/ai/context/providers/datasource.ts
@@ -17,7 +17,7 @@ import type { DataSourceConnection, DataTable } from "@/core/kernel/messages";
import type { AIContextItem } from "../registry";
import { AIContextProvider } from "../registry";
import { contextToXml } from "../utils";
-import { Boosts } from "./common";
+import { Boosts, Sections } from "./common";
type NamedDatasource = Omit<
DataSourceConnection,
@@ -53,24 +53,35 @@ export class DatasourceContextProvider extends AIContextProvider<DatasourceConte
}
getItems(): DatasourceContextItem[] {
- return [...this.connectionsMap.values()].map((connection) => {
- let description = "Database schema.";
- const data: DatasourceContextItem["data"] = {
- connection: connection,
- };
- if (INTERNAL_SQL_ENGINES.has(connection.name)) {
- data.tables = this.dataframes;
- description = "Database schema and the dataframes that can be queried";
- }
-
- return {
- uri: this.asURI(connection.name),
- name: connection.name,
- description: description,
- type: this.contextType,
- data: data,
- };
- });
+ return [...this.connectionsMap.values()]
+ .map((connection): DatasourceContextItem | null => {
+ let description = "Database schema.";
+ const data: DatasourceContextItem["data"] = {
+ connection: connection,
+ };
+
+ if (INTERNAL_SQL_ENGINES.has(connection.name)) {
+ data.tables = this.dataframes;
+ description =
+ "Database schema and the dataframes that can be queried";
+ }
+
+ // Hide empty datasources
+ const hasNoTables =
+ connection.databases.length === 0 && (data.tables?.length ?? 0) === 0;
+ if (hasNoTables) {
+ return null;
+ }
+
+ return {
+ uri: this.asURI(connection.name),
+ name: connection.name,
+ description: description,
+ type: this.contextType,
+ data: data,
+ };
+ })
+ .filter(Boolean);
}
formatContext(item: DatasourceContextItem): string {
@@ -115,7 +126,7 @@ export class DatasourceContextProvider extends AIContextProvider<DatasourceConte
detail: dbDisplayName(dataConnection.dialect),
boost: Boosts.MEDIUM,
type: this.contextType,
- section: "Data Sources",
+ section: Sections.DATA_SOURCES,
info: () => {
const infoContainer = document.createElement("div");
infoContainer.classList.add("mo-cm-tooltip", "docs-documentation");
diff --git a/frontend/src/core/ai/context/providers/error.ts b/frontend/src/core/ai/context/providers/error.ts
index 66f35dc0c8..2d207643b9 100644
--- a/frontend/src/core/ai/context/providers/error.ts
+++ b/frontend/src/core/ai/context/providers/error.ts
@@ -9,7 +9,7 @@ import { logNever } from "@/utils/assertNever";
import { PluralWord } from "@/utils/pluralize";
import { type AIContextItem, AIContextProvider } from "../registry";
import { contextToXml } from "../utils";
-import { Boosts } from "./common";
+import { Sections } from "./common";
export interface ErrorContextItem extends AIContextItem {
type: "error";
@@ -111,10 +111,9 @@ export class ErrorContextProvider extends AIContextProvider<ErrorContextItem> {
label: "@Errors",
displayLabel: "Errors",
detail: `${item.data.errors.length} ${errorsTxt.pluralize(item.data.errors.length)}`,
- boost: Boosts.ERROR,
type: "error",
apply: "@Errors",
- section: "Errors",
+ section: Sections.ERROR,
info: () => {
const infoContainer = document.createElement("div");
infoContainer.classList.add(
@@ -150,7 +149,7 @@ export class ErrorContextProvider extends AIContextProvider<ErrorContextItem> {
return {
label: "Error",
displayLabel: "Error",
- boost: Boosts.ERROR,
+ section: Sections.ERROR,
};
}
diff --git a/frontend/src/core/ai/context/providers/file.ts b/frontend/src/core/ai/context/providers/file.ts
index e8cf20e4c7..7128e296d8 100644
--- a/frontend/src/core/ai/context/providers/file.ts
+++ b/frontend/src/core/ai/context/providers/file.ts
@@ -15,7 +15,7 @@ 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";
+import { Boosts, Sections } from "./common";
export interface FileContextItem extends AIContextItem {
type: "file";
data: {
@@ -190,7 +190,7 @@ export class FileContextProvider extends AIContextProvider<FileContextItem> {
return {
...this.createBasicCompletion(item),
type: "file",
- section: "File",
+ section: Sections.FILE,
boost: data.isDirectory ? Boosts.MEDIUM : Boosts.LOW,
detail: data.path,
displayLabel: `${icon} ${name}`,
diff --git a/frontend/src/core/ai/context/providers/tables.ts b/frontend/src/core/ai/context/providers/tables.ts
index 9378380284..f0e3d2f0f9 100644
--- a/frontend/src/core/ai/context/providers/tables.ts
+++ b/frontend/src/core/ai/context/providers/tables.ts
@@ -9,7 +9,7 @@ import type { DataTable } from "@/core/kernel/messages";
import type { AIContextItem } from "../registry";
import { AIContextProvider } from "../registry";
import { contextToXml } from "../utils";
-import { Boosts } from "./common";
+import { Boosts, Sections } from "./common";
export interface TableContextItem extends AIContextItem {
type: "data";
@@ -38,21 +38,46 @@ export class TableContextProvider extends AIContextProvider<TableContextItem> {
formatContext(item: TableContextItem): string {
const { data } = item;
- const { columns, source, num_rows, num_columns, name } = data;
+ const { columns, source, num_rows, num_columns, name, variable_name } =
+ data;
+
+ // Build shape information
const shape = [
- num_rows == null ? undefined : `${num_rows} rows`,
- num_columns == null ? undefined : `${num_columns} columns`,
+ num_rows != null ? `${num_rows} rows` : undefined,
+ num_columns != null ? `${num_columns} columns` : undefined,
]
.filter(Boolean)
.join(", ");
let details = "";
+
+ // Add shape information
if (shape) {
- details += `\nShape: ${shape}`;
+ details += `Shape: ${shape}\n`;
+ }
+
+ // Add variable name if available
+ if (variable_name) {
+ details += `Variable: ${variable_name}\n`;
}
+ // Add column information with sample values
if (columns && columns.length > 0) {
- details += `\nColumns:\n${columns.map((col) => ` - ${col.name}: ${col.type}`).join("\n")}`;
+ details += "Columns:\n";
+ for (const col of columns) {
+ let columnInfo = ` ${col.name} (${col.type})`;
+
+ // Add sample values if available
+ if (col.sample_values && col.sample_values.length > 0) {
+ const samples = col.sample_values
+ .slice(0, 3) // Limit to first 3 samples
+ .map((val) => (val === null ? "null" : String(val)))
+ .join(", ");
+ columnInfo += ` - samples: [${samples}]`;
+ }
+
+ details += `${columnInfo}\n`;
+ }
}
return contextToXml({
@@ -61,7 +86,7 @@ export class TableContextProvider extends AIContextProvider<TableContextItem> {
name: name,
source: source ?? "unknown",
},
- details: details,
+ details: details.trim(),
});
}
@@ -78,7 +103,10 @@ export class TableContextProvider extends AIContextProvider<TableContextItem> {
: Boosts.REMOTE_TABLE,
type: getTableType(table),
apply: `@${tableName}`,
- section: getTableType(table) === "dataframe" ? "Dataframe" : "Table",
+ section: {
+ name: getTableType(table) === "dataframe" ? "Dataframe" : "Table",
+ rank: Sections.TABLE.rank,
+ },
info: () => this.createTableInfoElement(tableName, table),
};
}
diff --git a/frontend/src/core/ai/context/providers/variable.ts b/frontend/src/core/ai/context/providers/variable.ts
index da7de1c1f7..ff0f4c1c02 100644
--- a/frontend/src/core/ai/context/providers/variable.ts
+++ b/frontend/src/core/ai/context/providers/variable.ts
@@ -6,7 +6,7 @@ import type { DatasetTablesMap } from "@/core/datasets/data-source-connections";
import type { Variable, Variables } from "@/core/variables/types";
import { type AIContextItem, AIContextProvider } from "../registry";
import { contextToXml } from "../utils";
-import { Boosts } from "./common";
+import { Sections } from "./common";
export interface VariableContextItem extends AIContextItem {
type: "variable";
@@ -58,9 +58,8 @@ export class VariableContextProvider extends AIContextProvider<VariableContextIt
label: `@${variable.name}`,
displayLabel: variable.name,
detail: variable.dataType ?? "",
- boost: Boosts.VARIABLE,
type: this.contextType,
- section: "Variable",
+ section: Sections.VARIABLE,
info: () => {
return createVariableInfoElement(variable);
},