@sqlrooms/vega 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -107,6 +107,94 @@ The chart can be sized in multiple ways:
107
107
  />
108
108
  ```
109
109
 
110
+ ### VegaChartTool
111
+
112
+ A tool for creating Vega-Lite charts in AI-assisted workflows, designed to work with the [SQLRooms AI assistant framework](/api/ai/).
113
+
114
+ #### Features
115
+
116
+ - **AI Integration**: Allows AI assistants to create data visualizations
117
+ - **SQL-Powered**: Uses SQL queries to fetch data for visualizations
118
+ - **Declarative Specs**: Uses Vega-Lite's declarative JSON syntax for chart creation
119
+ - **Responsive Design**: Charts automatically adapt to container size
120
+
121
+ #### Usage
122
+
123
+ ```tsx
124
+ import {createVegaChartTool} from '@sqlrooms/vega';
125
+ import {createAiSlice} from '@sqlrooms/ai';
126
+ import {createProjectStore} from '@sqlrooms/project-builder';
127
+
128
+ // Create a project store with the VegaChartTool configured
129
+ export const {projectStore, useProjectStore} = createProjectStore(
130
+ (set, get, store) => ({
131
+ // Other slices and state...
132
+
133
+ // AI slice with custom tools
134
+ ...createAiSlice({
135
+ getApiKey: (modelProvider: string) => {
136
+ return get()?.apiKeys[modelProvider] || '';
137
+ },
138
+ // Configure custom tools at store creation time
139
+ customTools: {
140
+ // Add the VegaChart tool
141
+ chart: createVegaChartTool({
142
+ // Optional custom description
143
+ description:
144
+ 'Create data visualizations using Vega-Lite and SQL queries',
145
+ }),
146
+
147
+ // Other custom tools...
148
+ },
149
+ })(set, get, store),
150
+
151
+ // Other state and methods...
152
+ }),
153
+ );
154
+ ```
155
+
156
+ #### Tool Parameters
157
+
158
+ The VegaChartTool accepts the following parameters:
159
+
160
+ | Parameter | Type | Description |
161
+ | -------------- | -------- | ----------------------------------------------------------- |
162
+ | `sqlQuery` | `string` | The SQL query to fetch data for the chart |
163
+ | `vegaLiteSpec` | `string` | The Vega-Lite specification as a JSON string |
164
+ | `reasoning` | `string` | Optional explanation of the chart's purpose or significance |
165
+
166
+ #### Example AI Assistant Usage
167
+
168
+ When the AI assistant uses the VegaChartTool, it will generate a response like this:
169
+
170
+ ```json
171
+ {
172
+ "sqlQuery": "SELECT product_category, SUM(sales) as total_sales FROM sales GROUP BY product_category ORDER BY total_sales DESC LIMIT 10",
173
+ "vegaLiteSpec": "{\"mark\": \"bar\", \"encoding\": {\"x\": {\"field\": \"product_category\", \"type\": \"nominal\", \"sort\": \"-y\"}, \"y\": {\"field\": \"total_sales\", \"type\": \"quantitative\", \"title\": \"Total Sales\"}, \"color\": {\"field\": \"product_category\", \"type\": \"nominal\", \"legend\": null}}}",
174
+ "reasoning": "This chart visualizes the top 10 product categories by total sales to identify the best-performing categories."
175
+ }
176
+ ```
177
+
178
+ In a conversation with the AI assistant, it might look like this:
179
+
180
+ ```
181
+ User: Can you show me a chart of our top 10 product categories by sales?
182
+
183
+ AI: I'll create a visualization of your top 10 product categories by sales.
184
+
185
+ [Chart: Bar chart showing product categories on the x-axis and total sales on the y-axis]
186
+
187
+ This chart displays your top 10 product categories ranked by total sales. As you can see,
188
+ Electronics leads with the highest sales, followed by Furniture and Clothing. This visualization
189
+ helps identify which product categories are driving the most revenue for your business.
190
+ ```
191
+
192
+ The tool will:
193
+
194
+ 1. Execute the SQL query to fetch the data
195
+ 2. Apply the Vega-Lite specification to create the visualization
196
+ 3. Render the chart in the UI with responsive sizing
197
+
110
198
  ## Dependencies
111
199
 
112
200
  This package depends on:
@@ -114,4 +202,5 @@ This package depends on:
114
202
  - `@sqlrooms/duckdb` - For SQL query execution
115
203
  - `@sqlrooms/ui` - For UI utilities
116
204
  - `@sqlrooms/utils` - For utility functions
205
+ - `@sqlrooms/ai` - For AI assistant integration
117
206
  - `react-vega` - For rendering Vega-Lite visualizations
@@ -0,0 +1,33 @@
1
+ import { z } from 'zod';
2
+ import { AiSliceTool } from '@sqlrooms/ai';
3
+ /**
4
+ * Zod schema for the VegaChart tool parameters
5
+ */
6
+ export declare const VegaChartToolParameters: z.ZodObject<{
7
+ sqlQuery: z.ZodString;
8
+ vegaLiteSpec: z.ZodString;
9
+ reasoning: z.ZodString;
10
+ }, "strip", z.ZodTypeAny, {
11
+ sqlQuery: string;
12
+ vegaLiteSpec: string;
13
+ reasoning: string;
14
+ }, {
15
+ sqlQuery: string;
16
+ vegaLiteSpec: string;
17
+ reasoning: string;
18
+ }>;
19
+ export type VegaChartToolParameters = z.infer<typeof VegaChartToolParameters>;
20
+ /**
21
+ * Default description for the VegaChart tool
22
+ */
23
+ export declare const DEFAULT_VEGA_CHART_DESCRIPTION = "A tool for creating VegaLite charts based on the schema of the SQL query result from the \"query\" tool.\nIn the response:\n- omit the data from the vegaLiteSpec\n- provide an sql query in sqlQuery instead.";
24
+ /**
25
+ * Creates a VegaLite chart visualization tool for AI assistants
26
+ * @param options - Configuration options for the VegaChart tool
27
+ * @param options.description - Custom description for the tool (defaults to a standard description)
28
+ * @returns A tool that can be used with the AI assistant
29
+ */
30
+ export declare function createVegaChartTool({ description, }?: {
31
+ description?: string;
32
+ }): AiSliceTool;
33
+ //# sourceMappingURL=VegaChartTool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VegaChartTool.d.ts","sourceRoot":"","sources":["../src/VegaChartTool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAC,WAAW,EAAC,MAAM,cAAc,CAAC;AAEzC;;GAEG;AACH,eAAO,MAAM,uBAAuB;;;;;;;;;;;;EAIlC,CAAC;AAEH,MAAM,MAAM,uBAAuB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE9E;;GAEG;AACH,eAAO,MAAM,8BAA8B,mNAGC,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,WAA4C,GAC7C,GAAE;IACD,WAAW,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,WAAW,CA8BnB"}
@@ -0,0 +1,56 @@
1
+ import { z } from 'zod';
2
+ import { VegaChartToolResult } from './VegaChartToolResult';
3
+ /**
4
+ * Zod schema for the VegaChart tool parameters
5
+ */
6
+ export const VegaChartToolParameters = z.object({
7
+ sqlQuery: z.string(),
8
+ vegaLiteSpec: z.string(),
9
+ reasoning: z.string(),
10
+ });
11
+ /**
12
+ * Default description for the VegaChart tool
13
+ */
14
+ export const DEFAULT_VEGA_CHART_DESCRIPTION = `A tool for creating VegaLite charts based on the schema of the SQL query result from the "query" tool.
15
+ In the response:
16
+ - omit the data from the vegaLiteSpec
17
+ - provide an sql query in sqlQuery instead.`;
18
+ /**
19
+ * Creates a VegaLite chart visualization tool for AI assistants
20
+ * @param options - Configuration options for the VegaChart tool
21
+ * @param options.description - Custom description for the tool (defaults to a standard description)
22
+ * @returns A tool that can be used with the AI assistant
23
+ */
24
+ export function createVegaChartTool({ description = DEFAULT_VEGA_CHART_DESCRIPTION, } = {}) {
25
+ return {
26
+ description,
27
+ parameters: VegaChartToolParameters,
28
+ execute: async ({ sqlQuery, vegaLiteSpec }) => {
29
+ try {
30
+ const parsedVegaLiteSpec = JSON.parse(vegaLiteSpec);
31
+ // data object of the vegaLiteSpec and sqlQuery
32
+ // it is not used yet, but we can use it to create a JSON editor for user to edit the vegaLiteSpec so that chart can be updated
33
+ return {
34
+ llmResult: {
35
+ success: true,
36
+ details: 'Chart created successfully.',
37
+ },
38
+ additionalData: {
39
+ sqlQuery,
40
+ vegaLiteSpec: parsedVegaLiteSpec,
41
+ },
42
+ };
43
+ }
44
+ catch (error) {
45
+ return {
46
+ llmResult: {
47
+ success: false,
48
+ details: `Not a valid JSON object: ${error}`,
49
+ },
50
+ };
51
+ }
52
+ },
53
+ component: VegaChartToolResult,
54
+ };
55
+ }
56
+ //# sourceMappingURL=VegaChartTool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VegaChartTool.js","sourceRoot":"","sources":["../src/VegaChartTool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AACtB,OAAO,EAAC,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAG1D;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC9C,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE;IACxB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC;AAIH;;GAEG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG;;;4CAGF,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,WAAW,GAAG,8BAA8B,MAG1C,EAAE;IACJ,OAAO;QACL,WAAW;QACX,UAAU,EAAE,uBAAuB;QACnC,OAAO,EAAE,KAAK,EAAE,EAAC,QAAQ,EAAE,YAAY,EAA0B,EAAE,EAAE;YACnE,IAAI,CAAC;gBACH,MAAM,kBAAkB,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;gBACpD,+CAA+C;gBAC/C,+HAA+H;gBAC/H,OAAO;oBACL,SAAS,EAAE;wBACT,OAAO,EAAE,IAAI;wBACb,OAAO,EAAE,6BAA6B;qBACvC;oBACD,cAAc,EAAE;wBACd,QAAQ;wBACR,YAAY,EAAE,kBAAkB;qBACjC;iBACF,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO;oBACL,SAAS,EAAE;wBACT,OAAO,EAAE,KAAK;wBACd,OAAO,EAAE,4BAA4B,KAAK,EAAE;qBAC7C;iBACF,CAAC;YACJ,CAAC;QACH,CAAC;QACD,SAAS,EAAE,mBAAmB;KAC/B,CAAC;AACJ,CAAC","sourcesContent":["import {z} from 'zod';\nimport {VegaChartToolResult} from './VegaChartToolResult';\nimport {AiSliceTool} from '@sqlrooms/ai';\n\n/**\n * Zod schema for the VegaChart tool parameters\n */\nexport const VegaChartToolParameters = z.object({\n sqlQuery: z.string(),\n vegaLiteSpec: z.string(),\n reasoning: z.string(),\n});\n\nexport type VegaChartToolParameters = z.infer<typeof VegaChartToolParameters>;\n\n/**\n * Default description for the VegaChart tool\n */\nexport const DEFAULT_VEGA_CHART_DESCRIPTION = `A tool for creating VegaLite charts based on the schema of the SQL query result from the \"query\" tool.\nIn the response:\n- omit the data from the vegaLiteSpec\n- provide an sql query in sqlQuery instead.`;\n\n/**\n * Creates a VegaLite chart visualization tool for AI assistants\n * @param options - Configuration options for the VegaChart tool\n * @param options.description - Custom description for the tool (defaults to a standard description)\n * @returns A tool that can be used with the AI assistant\n */\nexport function createVegaChartTool({\n description = DEFAULT_VEGA_CHART_DESCRIPTION,\n}: {\n description?: string;\n} = {}): AiSliceTool {\n return {\n description,\n parameters: VegaChartToolParameters,\n execute: async ({sqlQuery, vegaLiteSpec}: VegaChartToolParameters) => {\n try {\n const parsedVegaLiteSpec = JSON.parse(vegaLiteSpec);\n // data object of the vegaLiteSpec and sqlQuery\n // it is not used yet, but we can use it to create a JSON editor for user to edit the vegaLiteSpec so that chart can be updated\n return {\n llmResult: {\n success: true,\n details: 'Chart created successfully.',\n },\n additionalData: {\n sqlQuery,\n vegaLiteSpec: parsedVegaLiteSpec,\n },\n };\n } catch (error) {\n return {\n llmResult: {\n success: false,\n details: `Not a valid JSON object: ${error}`,\n },\n };\n }\n },\n component: VegaChartToolResult,\n };\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import { VisualizationSpec } from 'react-vega';
2
+ type VegaChartToolResultProps = {
3
+ reasoning: string;
4
+ sqlQuery: string;
5
+ vegaLiteSpec: VisualizationSpec;
6
+ };
7
+ /**
8
+ * Renders a chart tool call with visualization using Vega-Lite
9
+ * @param {VegaChartToolResultProps} props - The component props
10
+ * @returns {JSX.Element} The rendered chart tool call
11
+ */
12
+ export declare function VegaChartToolResult({ sqlQuery, vegaLiteSpec, }: VegaChartToolResultProps): import("react/jsx-runtime").JSX.Element;
13
+ export {};
14
+ //# sourceMappingURL=VegaChartToolResult.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VegaChartToolResult.d.ts","sourceRoot":"","sources":["../src/VegaChartToolResult.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAE7C,KAAK,wBAAwB,GAAG;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,iBAAiB,CAAC;CACjC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,QAAQ,EACR,YAAY,GACb,EAAE,wBAAwB,2CAwB1B"}
@@ -0,0 +1,13 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { Suspense } from 'react';
3
+ import { VegaLiteChart } from './VegaLiteChart';
4
+ import { QueryToolResult } from '@sqlrooms/ai';
5
+ /**
6
+ * Renders a chart tool call with visualization using Vega-Lite
7
+ * @param {VegaChartToolResultProps} props - The component props
8
+ * @returns {JSX.Element} The rendered chart tool call
9
+ */
10
+ export function VegaChartToolResult({ sqlQuery, vegaLiteSpec, }) {
11
+ return (_jsx(_Fragment, { children: vegaLiteSpec && (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(QueryToolResult, { title: "Query", sqlQuery: sqlQuery }), _jsx(Suspense, { fallback: _jsx("div", { className: "flex h-full w-full items-center justify-center", children: _jsx("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-blue-600" }) }), children: _jsx(VegaLiteChart, { className: "max-w-[600px]", aspectRatio: 16 / 9, sqlQuery: sqlQuery, spec: vegaLiteSpec }) })] })) }));
12
+ }
13
+ //# sourceMappingURL=VegaChartToolResult.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VegaChartToolResult.js","sourceRoot":"","sources":["../src/VegaChartToolResult.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,eAAe,EAAC,MAAM,cAAc,CAAC;AAS7C;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,QAAQ,EACR,YAAY,GACa;IACzB,OAAO,CACL,4BACG,YAAY,IAAI,CACf,eAAK,SAAS,EAAC,qBAAqB,aAClC,KAAC,eAAe,IAAC,KAAK,EAAC,OAAO,EAAC,QAAQ,EAAE,QAAQ,GAAI,EACrD,KAAC,QAAQ,IACP,QAAQ,EACN,cAAK,SAAS,EAAC,gDAAgD,YAC7D,cAAK,SAAS,EAAC,8EAA8E,GAAO,GAChG,YAGR,KAAC,aAAa,IACZ,SAAS,EAAC,eAAe,EACzB,WAAW,EAAE,EAAE,GAAG,CAAC,EACnB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,YAAY,GAClB,GACO,IACP,CACP,GACA,CACJ,CAAC;AACJ,CAAC","sourcesContent":["import {Suspense} from 'react';\nimport {VegaLiteChart} from './VegaLiteChart';\nimport {QueryToolResult} from '@sqlrooms/ai';\nimport {VisualizationSpec} from 'react-vega';\n\ntype VegaChartToolResultProps = {\n reasoning: string;\n sqlQuery: string;\n vegaLiteSpec: VisualizationSpec;\n};\n\n/**\n * Renders a chart tool call with visualization using Vega-Lite\n * @param {VegaChartToolResultProps} props - The component props\n * @returns {JSX.Element} The rendered chart tool call\n */\nexport function VegaChartToolResult({\n sqlQuery,\n vegaLiteSpec,\n}: VegaChartToolResultProps) {\n return (\n <>\n {vegaLiteSpec && (\n <div className=\"flex flex-col gap-2\">\n <QueryToolResult title=\"Query\" sqlQuery={sqlQuery} />\n <Suspense\n fallback={\n <div className=\"flex h-full w-full items-center justify-center\">\n <div className=\"h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-blue-600\"></div>\n </div>\n }\n >\n <VegaLiteChart\n className=\"max-w-[600px]\"\n aspectRatio={16 / 9}\n sqlQuery={sqlQuery}\n spec={vegaLiteSpec}\n />\n </Suspense>\n </div>\n )}\n </>\n );\n}\n"]}
@@ -87,6 +87,6 @@ export const VegaLiteChart = ({ className, width = 'auto', height = 'auto', aspe
87
87
  };
88
88
  fetchData();
89
89
  }, [sqlQuery, conn]);
90
- return (_jsx("div", { ref: containerRef, className: cn('w-full h-full flex flex-col gap-2 overflow-hidden', className), children: refinedSpec && data && _jsx(VegaLite, { spec: refinedSpec, data: data }) }));
90
+ return (_jsx("div", { ref: containerRef, className: cn('flex h-full w-full flex-col gap-2 overflow-hidden', className), children: refinedSpec && data && _jsx(VegaLite, { spec: refinedSpec, data: data }) }));
91
91
  };
92
92
  //# sourceMappingURL=VegaLiteChart.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"VegaLiteChart.js","sourceRoot":"","sources":["../src/VegaLiteChart.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,gBAAgB,EAAE,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAC,EAAE,EAAE,wBAAwB,EAAC,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC3D,OAAO,EAAC,QAAQ,EAAoB,MAAM,YAAY,CAAC;AAEvD,MAAM,SAAS,GAAG,aAAa,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,MAAM,CAAC,MAAM,aAAa,GAOrB,CAAC,EACJ,SAAS,EACT,KAAK,GAAG,MAAM,EACd,MAAM,GAAG,MAAM,EACf,WAAW,GAAG,CAAC,GAAG,CAAC,EACnB,QAAQ,EACR,IAAI,GACL,EAAE,EAAE;IACH,MAAM,YAAY,GAAG,MAAM,CAAiB,IAAI,CAA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AC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,GAAI,GAC/D,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {arrowTableToJson, useDuckDb} from '@sqlrooms/duckdb';\nimport {cn, useAspectRatioDimensions} from '@sqlrooms/ui';\nimport {safeJsonParse} from '@sqlrooms/utils';\nimport {useEffect, useMemo, useRef, useState} from 'react';\nimport {VegaLite, VisualizationSpec} from 'react-vega';\n\nconst DATA_NAME = 'queryResult';\n\n/**\n * A component that renders a Vega-Lite chart with SQL data and responsive sizing.\n *\n * The chart can be sized in multiple ways:\n * - Fixed dimensions: Provide both width and height as numbers\n * - Fixed width, proportional height: Provide width as number, height as 'auto'\n * - Fixed height, proportional width: Provide height as number, width as 'auto'\n * - Fully responsive: Leave both as 'auto' (default), chart will fill container while maintaining aspect ratio\n *\n * @param props - The component props\n * @param {number | 'auto'} [props.width='auto'] - The chart width in pixels, or 'auto' to use container width\n * @param {number | 'auto'} [props.height='auto'] - The chart height in pixels, or 'auto' to calculate from aspect ratio\n * @param {number} [props.aspectRatio=3/2] - The desired width-to-height ratio when dimensions are auto-calculated\n * @param {string} props.sqlQuery - The SQL query to fetch data for the chart\n * @param {string | VisualizationSpec} props.spec - The Vega-Lite specification for the chart.\n * Can be either a JSON string or a VisualizationSpec object.\n * The data and size properties will be overridden by the component.\n *\n * @returns The rendered chart component\n *\n * @example\n * // Fixed size chart\n * <VegaLiteChart\n * width={600}\n * height={400}\n * sqlQuery=\"SELECT category, count(*) as count FROM sales GROUP BY category\"\n * spec={{\n * mark: 'bar',\n * encoding: {\n * x: {field: 'category', type: 'nominal'},\n * y: {field: 'count', type: 'quantitative'}\n * }\n * }}\n * />\n *\n * @example\n * // Responsive chart with 16:9 aspect ratio\n * <VegaLiteChart\n * className=\"max-w-[600px]\"\n * aspectRatio={16/9}\n * sqlQuery=\"SELECT date, value FROM metrics\"\n * spec={{\n * mark: 'line',\n * encoding: {\n * x: {field: 'date', type: 'temporal'},\n * y: {field: 'value', type: 'quantitative'}\n * }\n * }}\n * />\n */\nexport const VegaLiteChart: React.FC<{\n className?: string;\n width?: number | 'auto';\n height?: number | 'auto';\n aspectRatio?: number;\n sqlQuery: string;\n spec: string | VisualizationSpec;\n}> = ({\n className,\n width = 'auto',\n height = 'auto',\n aspectRatio = 3 / 2,\n sqlQuery,\n spec,\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const dimensions = useAspectRatioDimensions({\n containerRef,\n width,\n height,\n aspectRatio,\n });\n\n const {conn} = useDuckDb();\n const [data, setData] = useState<Record<string, unknown>>();\n\n const refinedSpec = useMemo(() => {\n const parsed = typeof spec === 'string' ? safeJsonParse(spec) : spec;\n if (!parsed) return null;\n return {\n ...parsed,\n data: {name: DATA_NAME},\n width: dimensions.width,\n height: dimensions.height,\n autosize: {\n type: 'fit',\n contains: 'padding',\n },\n } as VisualizationSpec;\n }, [spec, dimensions]);\n\n useEffect(() => {\n const fetchData = async () => {\n const result = await conn.query(sqlQuery);\n setData({[DATA_NAME]: arrowTableToJson(result)});\n };\n fetchData();\n }, [sqlQuery, conn]);\n\n return (\n <div\n ref={containerRef}\n className={cn(\n 'w-full h-full flex flex-col gap-2 overflow-hidden',\n className,\n )}\n >\n {refinedSpec && data && <VegaLite spec={refinedSpec} data={data} />}\n </div>\n );\n};\n"]}
1
+ {"version":3,"file":"VegaLiteChart.js","sourceRoot":"","sources":["../src/VegaLiteChart.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,gBAAgB,EAAE,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAC,EAAE,EAAE,wBAAwB,EAAC,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC3D,OAAO,EAAC,QAAQ,EAAoB,MAAM,YAAY,CAAC;AAEvD,MAAM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AC,EAAC,CAAC,SAAS,CAAC,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAAC,CAAC,CAAC;QACnD,CAAC,CAAC;QACF,SAAS,EAAE,CAAC;IACd,CAAC,EAAE,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC;IAErB,OAAO,CACL,cACE,GAAG,EAAE,YAAY,EACjB,SAAS,EAAE,EAAE,CACX,mDAAmD,EACnD,SAAS,CACV,YAEA,WAAW,IAAI,IAAI,IAAI,KAAC,QAAQ,IAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,GAAI,GAC/D,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {arrowTableToJson, useDuckDb} from '@sqlrooms/duckdb';\nimport {cn, useAspectRatioDimensions} from '@sqlrooms/ui';\nimport {safeJsonParse} from '@sqlrooms/utils';\nimport {useEffect, useMemo, useRef, useState} from 'react';\nimport {VegaLite, VisualizationSpec} from 'react-vega';\n\nconst DATA_NAME = 'queryResult';\n\n/**\n * A component that renders a Vega-Lite chart with SQL data and responsive sizing.\n *\n * The chart can be sized in multiple ways:\n * - Fixed dimensions: Provide both width and height as numbers\n * - Fixed width, proportional height: Provide width as number, height as 'auto'\n * - Fixed height, proportional width: Provide height as number, width as 'auto'\n * - Fully responsive: Leave both as 'auto' (default), chart will fill container while maintaining aspect ratio\n *\n * @param props - The component props\n * @param {number | 'auto'} [props.width='auto'] - The chart width in pixels, or 'auto' to use container width\n * @param {number | 'auto'} [props.height='auto'] - The chart height in pixels, or 'auto' to calculate from aspect ratio\n * @param {number} [props.aspectRatio=3/2] - The desired width-to-height ratio when dimensions are auto-calculated\n * @param {string} props.sqlQuery - The SQL query to fetch data for the chart\n * @param {string | VisualizationSpec} props.spec - The Vega-Lite specification for the chart.\n * Can be either a JSON string or a VisualizationSpec object.\n * The data and size properties will be overridden by the component.\n *\n * @returns The rendered chart component\n *\n * @example\n * // Fixed size chart\n * <VegaLiteChart\n * width={600}\n * height={400}\n * sqlQuery=\"SELECT category, count(*) as count FROM sales GROUP BY category\"\n * spec={{\n * mark: 'bar',\n * encoding: {\n * x: {field: 'category', type: 'nominal'},\n * y: {field: 'count', type: 'quantitative'}\n * }\n * }}\n * />\n *\n * @example\n * // Responsive chart with 16:9 aspect ratio\n * <VegaLiteChart\n * className=\"max-w-[600px]\"\n * aspectRatio={16/9}\n * sqlQuery=\"SELECT date, value FROM metrics\"\n * spec={{\n * mark: 'line',\n * encoding: {\n * x: {field: 'date', type: 'temporal'},\n * y: {field: 'value', type: 'quantitative'}\n * }\n * }}\n * />\n */\nexport const VegaLiteChart: React.FC<{\n className?: string;\n width?: number | 'auto';\n height?: number | 'auto';\n aspectRatio?: number;\n sqlQuery: string;\n spec: string | VisualizationSpec;\n}> = ({\n className,\n width = 'auto',\n height = 'auto',\n aspectRatio = 3 / 2,\n sqlQuery,\n spec,\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const dimensions = useAspectRatioDimensions({\n containerRef,\n width,\n height,\n aspectRatio,\n });\n\n const {conn} = useDuckDb();\n const [data, setData] = useState<Record<string, unknown>>();\n\n const refinedSpec = useMemo(() => {\n const parsed = typeof spec === 'string' ? safeJsonParse(spec) : spec;\n if (!parsed) return null;\n return {\n ...parsed,\n data: {name: DATA_NAME},\n width: dimensions.width,\n height: dimensions.height,\n autosize: {\n type: 'fit',\n contains: 'padding',\n },\n } as VisualizationSpec;\n }, [spec, dimensions]);\n\n useEffect(() => {\n const fetchData = async () => {\n const result = await conn.query(sqlQuery);\n setData({[DATA_NAME]: arrowTableToJson(result)});\n };\n fetchData();\n }, [sqlQuery, conn]);\n\n return (\n <div\n ref={containerRef}\n className={cn(\n 'flex h-full w-full flex-col gap-2 overflow-hidden',\n className,\n )}\n >\n {refinedSpec && data && <VegaLite spec={refinedSpec} data={data} />}\n </div>\n );\n};\n"]}
package/dist/index.d.ts CHANGED
@@ -3,5 +3,8 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  export { VegaLiteChart } from './VegaLiteChart';
6
+ export { VegaChartToolResult as VegaChartToolResult } from './VegaChartToolResult';
6
7
  export type { VisualizationSpec } from 'react-vega';
8
+ export { createVegaChartTool, VegaChartToolParameters, DEFAULT_VEGA_CHART_DESCRIPTION, } from './VegaChartTool';
9
+ export type { VegaChartToolParameters as VegaChartToolParametersType } from './VegaChartTool';
7
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,YAAY,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,mBAAmB,IAAI,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AACjF,YAAY,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAClD,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,8BAA8B,GAC/B,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAC,uBAAuB,IAAI,2BAA2B,EAAC,MAAM,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -3,4 +3,6 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  export { VegaLiteChart } from './VegaLiteChart';
6
+ export { VegaChartToolResult as VegaChartToolResult } from './VegaChartToolResult';
7
+ export { createVegaChartTool, VegaChartToolParameters, DEFAULT_VEGA_CHART_DESCRIPTION, } from './VegaChartTool';
6
8
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\nexport {VegaLiteChart} from './VegaLiteChart';\nexport type {VisualizationSpec} from 'react-vega';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,mBAAmB,IAAI,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAEjF,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,8BAA8B,GAC/B,MAAM,iBAAiB,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\nexport {VegaLiteChart} from './VegaLiteChart';\nexport {VegaChartToolResult as VegaChartToolResult} from './VegaChartToolResult';\nexport type {VisualizationSpec} from 'react-vega';\nexport {\n createVegaChartTool,\n VegaChartToolParameters,\n DEFAULT_VEGA_CHART_DESCRIPTION,\n} from './VegaChartTool';\nexport type {VegaChartToolParameters as VegaChartToolParametersType} from './VegaChartTool';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqlrooms/vega",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
@@ -19,9 +19,10 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@sqlrooms/duckdb": "0.7.0",
23
- "@sqlrooms/ui": "0.7.0",
24
- "@sqlrooms/utils": "0.7.0",
22
+ "@sqlrooms/ai": "0.8.1",
23
+ "@sqlrooms/duckdb": "0.8.1",
24
+ "@sqlrooms/ui": "0.8.1",
25
+ "@sqlrooms/utils": "0.8.1",
25
26
  "react-vega": "^7.6.0",
26
27
  "vega": "^5.31.0",
27
28
  "vega-lite": "^5.23.0"
@@ -36,5 +37,5 @@
36
37
  "lint": "eslint .",
37
38
  "typedoc": "typedoc"
38
39
  },
39
- "gitHead": "8be65f051c588d3a963f721322429657913b6c63"
40
+ "gitHead": "7b5e727b79d675c17b93412c109d1ba1b22699c8"
40
41
  }