Skip to content

Commit 748010e

Browse files
committed
API cleanup
1 parent 86c2737 commit 748010e

File tree

14 files changed

+67
-82
lines changed

14 files changed

+67
-82
lines changed

docs/data/data-grid/prompt/PromptWithDataSampling.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ function ToolbarWithPromptInput() {
2222
const [prompt, setPrompt] = React.useState('');
2323

2424
const context = React.useMemo(
25-
() =>
26-
apiRef.current.unstable_getPromptContext(
27-
apiRef.current.unstable_collectSampleData(),
28-
),
25+
() => apiRef.current.unstable_getPromptContext(true),
2926
[apiRef],
3027
);
3128

@@ -35,7 +32,7 @@ function ToolbarWithPromptInput() {
3532
setPrompt(selectedPrompt);
3633
if (selectedPrompt) {
3734
apiRef.current.setLoading(true);
38-
const result = await mockPromptResolver(context, selectedPrompt);
35+
const result = await mockPromptResolver(selectedPrompt, context);
3936
apiRef.current.unstable_applyPromptResult(result);
4037
apiRef.current.setLoading(false);
4138
}

docs/data/data-grid/prompt/PromptWithDataSampling.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ function ToolbarWithPromptInput() {
2222
const [prompt, setPrompt] = React.useState('');
2323

2424
const context = React.useMemo(
25-
() =>
26-
apiRef.current.unstable_getPromptContext(
27-
apiRef.current.unstable_collectSampleData(),
28-
),
25+
() => apiRef.current.unstable_getPromptContext(true),
2926
[apiRef],
3027
);
3128

@@ -35,7 +32,7 @@ function ToolbarWithPromptInput() {
3532
setPrompt(selectedPrompt);
3633
if (selectedPrompt) {
3734
apiRef.current.setLoading(true);
38-
const result = await mockPromptResolver(context, selectedPrompt);
35+
const result = await mockPromptResolver(selectedPrompt, context);
3936
apiRef.current.unstable_applyPromptResult(result);
4037
apiRef.current.setLoading(false);
4138
}

docs/data/data-grid/prompt/PromptWithDataSource.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ function ToolbarWithPromptInput() {
4444
const [prompt, setPrompt] = React.useState('');
4545

4646
const context = React.useMemo(
47-
() =>
48-
apiRef.current.unstable_getPromptContext(
49-
apiRef.current.unstable_collectSampleData(),
50-
),
47+
() => apiRef.current.unstable_getPromptContext(true),
5148
[apiRef],
5249
);
5350

@@ -57,7 +54,7 @@ function ToolbarWithPromptInput() {
5754
setPrompt(selectedPrompt);
5855
if (selectedPrompt) {
5956
apiRef.current.setLoading(true);
60-
const result = await mockPromptResolver(context, selectedPrompt);
57+
const result = await mockPromptResolver(selectedPrompt, context);
6158
apiRef.current.unstable_applyPromptResult(result);
6259
}
6360
},

docs/data/data-grid/prompt/PromptWithDataSource.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ function ToolbarWithPromptInput() {
4646
const [prompt, setPrompt] = React.useState('');
4747

4848
const context = React.useMemo(
49-
() =>
50-
apiRef.current.unstable_getPromptContext(
51-
apiRef.current.unstable_collectSampleData(),
52-
),
49+
() => apiRef.current.unstable_getPromptContext(true),
5350
[apiRef],
5451
);
5552

@@ -59,7 +56,7 @@ function ToolbarWithPromptInput() {
5956
setPrompt(selectedPrompt);
6057
if (selectedPrompt) {
6158
apiRef.current.setLoading(true);
62-
const result = await mockPromptResolver(context, selectedPrompt);
59+
const result = await mockPromptResolver(selectedPrompt, context);
6360
apiRef.current.unstable_applyPromptResult(result);
6461
}
6562
},

docs/data/data-grid/prompt/PromptWithExamples.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function ToolbarWithPromptInput() {
4444
setPrompt(selectedPrompt);
4545
if (selectedPrompt) {
4646
apiRef.current.setLoading(true);
47-
const result = await mockPromptResolver(context, selectedPrompt);
47+
const result = await mockPromptResolver(selectedPrompt, context);
4848
apiRef.current.unstable_applyPromptResult(result);
4949
apiRef.current.setLoading(false);
5050
}

docs/data/data-grid/prompt/PromptWithExamples.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function ToolbarWithPromptInput() {
4444
setPrompt(selectedPrompt);
4545
if (selectedPrompt) {
4646
apiRef.current.setLoading(true);
47-
const result = await mockPromptResolver(context, selectedPrompt);
47+
const result = await mockPromptResolver(selectedPrompt, context);
4848
apiRef.current.unstable_applyPromptResult(result);
4949
apiRef.current.setLoading(false);
5050
}

docs/data/data-grid/prompt/prompt.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ const PROMPT_RESOLVER_PROXY_BASE_URL =
8686
? 'http://localhost:3000'
8787
: 'https://api.my-proxy.com';
8888

89-
function processPrompt(context: string, query: string) {
90-
const extendedContext = `The rows represent: List of employees with their company, position and start date\n\n${context}`;
89+
function processPrompt(query: string, context: string) {
90+
const additionalContext = `The rows represent: List of employees with their company, position and start date`;
9191

9292
return unstable_gridDefaultPromptResolver(
9393
`${PROMPT_RESOLVER_PROXY_BASE_URL}/api/datagrid/prompt`,
94-
extendedContext,
9594
query,
95+
context,
96+
additionalContext,
9697
);
9798
}
9899
```

docs/pages/x/api/data-grid/grid-api.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,8 @@
471471
"required": true,
472472
"isPremiumPlan": true
473473
},
474-
"unstable_collectSampleData": {
475-
"type": { "description": "(sampleCount?: number) => Record<string, any[]>" },
476-
"required": true,
477-
"isPremiumPlan": true
478-
},
479474
"unstable_getPromptContext": {
480-
"type": { "description": "(examples?: Record<string, any[]>) => string" },
475+
"type": { "description": "(allowDataSampling?: boolean) => string" },
481476
"required": true,
482477
"isPremiumPlan": true
483478
},

docs/translations/api-docs/data-grid/grid-api.json

-3
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@
242242
"unstable_applyPromptResult": {
243243
"description": "Use the prompt processing result to update all relevant parts of the grid state."
244244
},
245-
"unstable_collectSampleData": {
246-
"description": "Collects sample data from the grid that can be used to improve the prompt context.<br />It takes random values for all columns but from different rows to make the samples more random."
247-
},
248245
"unstable_getPromptContext": { "description": "Get the context for the prompt." },
249246
"unstable_replaceRows": { "description": "Replace a set of rows with new rows." },
250247
"unstable_setColumnVirtualization": { "description": "Enable/disable column virtualization." },

packages/x-data-grid-generator/src/services/prompt-resolver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { unstable_PromptResponse as PromptResponse } from '@mui/x-data-grid-premium';
22
import { mockPrompts } from '../constants/prompts';
33

4-
export const mockPromptResolver = (_: string, query: string) => {
4+
export const mockPromptResolver = (query: string, _: string) => {
55
const resolved = mockPrompts.get(query.toLowerCase().trim());
66

77
return new Promise<PromptResponse>((resolve, reject) => {

packages/x-data-grid-premium/src/components/promptControl/GridToolbarPromptControl.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const GridToolbarPromptControlRoot = styled('div', {
4242
flexDirection: 'row',
4343
});
4444

45-
type GridToolbarPromptControlProps = {
45+
export type GridToolbarPromptControlProps = {
4646
/**
4747
* Allow taking couple of random cell values from each column to improve the prompt context.
4848
* If allowed, samples are taken from different rows.
@@ -58,11 +58,11 @@ type GridToolbarPromptControlProps = {
5858
/**
5959
* Called when the new prompt is ready to be processed.
6060
* Provides the prompt and the data context and expects the grid state updates to be returned.
61-
* @param {string} context The context of the prompt
6261
* @param {string} query The query to process
62+
* @param {string} context The context of the prompt
6363
* @returns {Promise<PromptResponse>} The grid state updates
6464
*/
65-
onPrompt: (context: string, query: string) => Promise<PromptResponse>;
65+
onPrompt: (query: string, context: string) => Promise<PromptResponse>;
6666
/**
6767
* Called when an error occurs.
6868
* @param {string} error The error message
@@ -82,17 +82,17 @@ function GridToolbarPromptControl(props: GridToolbarPromptControlProps) {
8282
const ownerState = { classes: rootProps.classes, recording: isRecording };
8383
const classes = useUtilityClasses(ownerState);
8484

85-
const context = React.useMemo(() => {
86-
const examples = allowDataSampling ? apiRef.current.unstable_collectSampleData() : undefined;
87-
return apiRef.current.unstable_getPromptContext(examples);
88-
}, [apiRef, allowDataSampling]);
85+
const context = React.useMemo(
86+
() => apiRef.current.unstable_getPromptContext(allowDataSampling),
87+
[apiRef, allowDataSampling],
88+
);
8989

9090
const processPrompt = React.useCallback(() => {
9191
setLoading(true);
9292
setError(null);
9393
apiRef.current.setLoading(true);
9494

95-
onPrompt(context, query)
95+
onPrompt(query, context)
9696
.then(apiRef.current.unstable_applyPromptResult)
9797
.catch((promptError) => {
9898
setError(apiRef.current.getLocaleText('toolbarPromptControlErrorMessage'));

packages/x-data-grid-premium/src/hooks/features/prompt/api.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@ import { PromptResponse } from './gridPromptInterfaces';
22

33
type Result<T> = { ok: false; message: string } | { ok: true; data: T };
44

5-
export function gridDefaultPromptResolver(url: string, context: string, query: string) {
5+
/**
6+
* Prompt resolver for the <DataGridPremium /> component.
7+
* @param {string} url The URL to send the request to.
8+
* @param {string} query The query to be processed.
9+
* @param {string} context The prompt context containing necessary information about the current columns definition.
10+
* Either use the `context` parameter of the `onPrompt` callback or the return value of the `unstable_getPromptContext()` API method.
11+
* @param {string} additionalContext Optional, additional context to make the processing results more accurate.
12+
* @returns {Promise<PromptResponse>} The grid state updates to be applied.
13+
*/
14+
export function gridDefaultPromptResolver(
15+
url: string,
16+
query: string,
17+
context: string,
18+
additionalContext = '',
19+
) {
620
return fetch(url, {
721
mode: 'cors',
822
method: 'POST',
@@ -13,16 +27,14 @@ export function gridDefaultPromptResolver(url: string, context: string, query: s
1327
body: JSON.stringify({
1428
context,
1529
query,
30+
additionalContext,
1631
}),
1732
})
1833
.then((result) => result.json())
1934
.then((result: Result<PromptResponse>) => {
2035
if (result.ok === false) {
2136
return Promise.reject(new Error(result.message));
2237
}
23-
if (result.data.error) {
24-
return Promise.reject(new Error(result.data.error));
25-
}
2638
return result.data;
2739
});
2840
}

packages/x-data-grid-premium/src/hooks/features/prompt/gridPromptInterfaces.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ export type PromptResponse = {
3232
* The prompt API interface that is available in the grid [[apiRef]].
3333
*/
3434
export interface GridPromptApi {
35-
/**
36-
* Collects sample data from the grid that can be used to improve the prompt context.
37-
* It takes random values for all columns but from different rows to make the samples more random.
38-
* @param {number} sampleCount The number of samples (randomized rows) to collect.
39-
* Default is 5. Maximum is 20.
40-
* If the number of rows in the grid is less than the sample count, all rows are used.
41-
* @returns {Record<string, any[]>} The sample data.
42-
*/
43-
unstable_collectSampleData: (sampleCount?: number) => Record<string, any[]>;
4435
/**
4536
* Get the context for the prompt.
46-
* @param {Record<string, any[]>} examples Column examples to be used to make the context more detailed and the response more accurate.
37+
* @param {boolean} allowDataSampling Whether to use grid data as examples in the context.
38+
* If true, random cell values from each column are used to make the context more detailed and the response more accurate.
39+
* If false, the samples are only generated from the unstable_examples property of the column(s).
40+
* Default is false.
4741
* @returns {string} The context for the prompt.
4842
*/
49-
unstable_getPromptContext: (examples?: Record<string, any[]>) => string;
43+
unstable_getPromptContext: (allowDataSampling?: boolean) => string;
5044
/**
5145
* Use the prompt processing result to update all relevant parts of the grid state.
5246
* @param {PromptResponse} result The result of the prompt.

packages/x-data-grid-premium/src/hooks/features/prompt/useGridPrompt.tsx

+22-24
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,46 @@ import { getValueOptions, getVisibleRows } from '@mui/x-data-grid-pro/internals'
1313
import { GridPrivateApiPremium } from '../../../models/gridApiPremium';
1414
import { PromptResponse } from './gridPromptInterfaces';
1515

16-
const MAX_SAMPLE_COUNT = 20;
16+
const DEFAULT_SAMPLE_COUNT = 5;
1717

1818
export const useGridPrompt = (apiRef: RefObject<GridPrivateApiPremium>) => {
1919
const columnsLookup = gridColumnLookupSelector(apiRef);
2020
const columns = Object.values(columnsLookup);
2121
const rows = Object.values(gridRowsLookupSelector(apiRef));
2222

23-
const collectSampleData = React.useCallback(
24-
(sampleCount = 5) => {
25-
const columnExamples: Record<string, any[]> = {};
26-
27-
columns.forEach((column) => {
28-
columnExamples[column.field] = Array.from({
29-
length: Math.min(sampleCount, rows.length, MAX_SAMPLE_COUNT),
30-
}).map(() => {
31-
const row = rows[Math.floor(Math.random() * rows.length)];
32-
if (column.valueGetter) {
33-
return column.valueGetter(row[column.field] as never, row, column, apiRef);
34-
}
35-
return row[column.field];
36-
});
23+
const collectSampleData = React.useCallback(() => {
24+
const columnExamples: Record<string, any[]> = {};
25+
26+
columns.forEach((column) => {
27+
columnExamples[column.field] = Array.from({
28+
length: Math.min(DEFAULT_SAMPLE_COUNT, rows.length),
29+
}).map(() => {
30+
const row = rows[Math.floor(Math.random() * rows.length)];
31+
if (column.valueGetter) {
32+
return column.valueGetter(row[column.field] as never, row, column, apiRef);
33+
}
34+
return row[column.field];
3735
});
36+
});
3837

39-
return columnExamples;
40-
},
41-
[apiRef, columns, rows],
42-
);
38+
return columnExamples;
39+
}, [apiRef, columns, rows]);
4340

4441
const getPromptContext = React.useCallback(
45-
(examples?: Record<string, any[]>) => {
42+
(allowDataSampling = false) => {
43+
const examples = allowDataSampling ? collectSampleData() : {};
44+
4645
const columnsContext = columns.map((column) => ({
4746
field: column.field,
4847
description: column.description ?? null,
49-
examples: examples?.[column.field] ?? column.unstable_examples ?? [],
48+
examples: examples[column.field] ?? column.unstable_examples ?? [],
5049
type: column.type ?? 'string',
5150
allowedOperators: column.filterOperators?.map((operator) => operator.value) ?? [],
5251
}));
5352

54-
return `The columns are described by the following JSON:\n${JSON.stringify(columnsContext)}`;
53+
return JSON.stringify(columnsContext);
5554
},
56-
[columns],
55+
[columns, collectSampleData],
5756
);
5857

5958
const applyPromptResult = React.useCallback(
@@ -119,7 +118,6 @@ export const useGridPrompt = (apiRef: RefObject<GridPrivateApiPremium>) => {
119118
useGridApiMethod(
120119
apiRef,
121120
{
122-
unstable_collectSampleData: collectSampleData,
123121
unstable_getPromptContext: getPromptContext,
124122
unstable_applyPromptResult: applyPromptResult,
125123
},

0 commit comments

Comments
 (0)