@gridsay/mui-x-data-grid 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +121 -2
- package/dist/index.d.ts +66 -0
- package/dist/index.js +296 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bluvio Software Studio LLP
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,122 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @gridsay/mui-x-data-grid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Gridsay prompt resolver for the MUI X Data Grid Premium AI Assistant. It replaces MUI's
|
|
4
|
+
`unstable_gridDefaultPromptResolver` in one line, sends your grid's switches, column limits and
|
|
5
|
+
current view with each question, and checks every answer before the grid applies it.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @gridsay/mui-x-data-grid
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependency: `@mui/x-data-grid-premium` 9.18 or a later 9.x release (`^9.18.0`); other major
|
|
12
|
+
versions are not tested. Data Grid Premium is a commercial MUI package: you need your own MUI X licence, and the grid shows MUI's watermark without one.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { createPromptResolver } from '@gridsay/mui-x-data-grid';
|
|
18
|
+
import { DataGridPremium, GridAiAssistantPanel, useGridApiRef } from '@mui/x-data-grid-premium';
|
|
19
|
+
import { useMemo } from 'react';
|
|
20
|
+
|
|
21
|
+
const apiRef = useGridApiRef();
|
|
22
|
+
const onPrompt = useMemo(
|
|
23
|
+
() => createPromptResolver({ url: '/api/ai-assistant', apiRef }),
|
|
24
|
+
[apiRef],
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
<DataGridPremium
|
|
28
|
+
apiRef={apiRef}
|
|
29
|
+
rows={rows}
|
|
30
|
+
columns={columns}
|
|
31
|
+
aiAssistant
|
|
32
|
+
onPrompt={onPrompt}
|
|
33
|
+
slots={{ aiAssistantPanel: GridAiAssistantPanel }}
|
|
34
|
+
showToolbar
|
|
35
|
+
/>;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`/api/ai-assistant` is your server route that adds your secret key and forwards to
|
|
39
|
+
`https://api.gridsay.com/api/v1/datagrid/prompt`; see
|
|
40
|
+
[the server-route quickstart](https://gridsay.com/docs/quickstart). To call the gateway straight
|
|
41
|
+
from the browser, pass that URL with a `publishableKey` and `getToken`; see
|
|
42
|
+
[browser keys](https://gridsay.com/docs/browser-keys).
|
|
43
|
+
|
|
44
|
+
| Option | Meaning |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `url` | Required. Where the prompt is posted: your own route, or the Gridsay gateway. |
|
|
47
|
+
| `apiRef` | The grid's `apiRef`. With it, the helper describes the grid to the server and adapts answers to it. Without it, you still get answer checking and readable errors. |
|
|
48
|
+
| `headers` | An object, or a function returning one (it may be async), added to each request. |
|
|
49
|
+
| `publishableKey` | A publishable key, sent as `x-api-key`. For calling the gateway from the browser. |
|
|
50
|
+
| `getToken` | Returns your end user's token, called for every request and sent as `Authorization: Bearer …`. If it throws or returns nothing (`null`, `undefined` or `''`, at once or from a promise), the prompt fails with "Sign in to use the assistant." (`unauthenticated_user`) and nothing is sent. |
|
|
51
|
+
| `additionalContext`, `privateMode`, `referenceId` | Sent in the request's `options`, as the stock resolver sends them. |
|
|
52
|
+
|
|
53
|
+
## Errors
|
|
54
|
+
|
|
55
|
+
The resolver rejects with a `GridsayError`; the grid shows its `message` to your user as it is.
|
|
56
|
+
To log why, wrap the resolver and read the error's `code`, `status`, `retryAfter` and `detail`.
|
|
57
|
+
`code` is the gateway's (such as `quota_exceeded` or `rate_limited`) or one of the helper's own,
|
|
58
|
+
below. Compare codes, never messages: the messages may change.
|
|
59
|
+
|
|
60
|
+
The helper's own failures, each code exported as `<NAME>_CODE` beside its sentence:
|
|
61
|
+
|
|
62
|
+
| `code` | Sentence | When |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| `unauthenticated_user` | `UNAUTHENTICATED_USER`: "Sign in to use the assistant." (or your route's own sentence) | `getToken` threw or returned nothing (nothing was sent), or the response was a 401 whose body names no other code. The same code the gateway sends. |
|
|
65
|
+
| `unexpected_error` | `INVALID_ANSWER`: "The assistant couldn't complete your request. Try again." | Your `headers` function threw. The user sees this sentence, never the text of what it threw; the original is in `cause`. |
|
|
66
|
+
| `connection_failed` | `CONNECTION_FAILED`: "The connection was interrupted. Try again." | The request got no answer, or the answer was cut off. |
|
|
67
|
+
| `not_json` | `NOT_JSON`: "The assistant couldn't respond. Try again." | The response was not JSON (a 401 is `unauthenticated_user` instead). `status` 404 usually means `url` is wrong. |
|
|
68
|
+
| `invalid_answer` | `INVALID_ANSWER` | The response was not an answer the helper can read, or an error without a sentence of its own. |
|
|
69
|
+
|
|
70
|
+
An error from the gateway keeps the gateway's own code, `status`, `retryAfter` and `detail`.
|
|
71
|
+
|
|
72
|
+
## What changes compared with the stock resolver
|
|
73
|
+
|
|
74
|
+
- **The server sees your grid.** Each request carries a snapshot: features you switched off
|
|
75
|
+
(`disableColumnSorting`, `rowSelection={false}`, no active chart…), per-column limits
|
|
76
|
+
(`filterable`, `sortable`, `groupable`, `pivotable`, `aggregable: false`,
|
|
77
|
+
`availableAggregationFunctions`), singleSelect values and labels, and the filters, sorting,
|
|
78
|
+
grouping, aggregation and pivot the user has now, including changes made by hand.
|
|
79
|
+
- **Answers are checked before the grid touches them.** A malformed answer leaves the grid as it
|
|
80
|
+
was. End users see short messages instead of browser errors
|
|
81
|
+
(see Errors). Messages from the server are shown as they are.
|
|
82
|
+
- **Top N works.** Asking for more rows than exist, or for rows spread over several pages, selects
|
|
83
|
+
the rows that exist. With a data source, selection waits for the new rows.
|
|
84
|
+
- **Sorting a grouped column reorders the groups.** The sort is moved to the grouping column the
|
|
85
|
+
grid generates (single or multiple grouping-column mode); other sorts are kept.
|
|
86
|
+
- **Leaving pivot mode** no longer brings back the grouping and aggregation from before the pivot.
|
|
87
|
+
- **A chart follow-up** keeps the aggregation it asked for.
|
|
88
|
+
- **One prompt at a time.** A prompt sent while another is running waits for it, so a follow-up
|
|
89
|
+
always builds on the previous answer.
|
|
90
|
+
|
|
91
|
+
## Limits
|
|
92
|
+
|
|
93
|
+
- Grid-wide switches (`disable…`, `rowSelection`, `rowGroupingColumnMode`, `dataSource`) are not
|
|
94
|
+
in the grid's public API. The helper reads them from the grid's internal props, tested with
|
|
95
|
+
`@mui/x-data-grid-premium` 9.18. If a later version moves them, the helper treats every feature
|
|
96
|
+
as switched on.
|
|
97
|
+
- With a data source, top N selects at most the rows of the loaded page.
|
|
98
|
+
- In single grouping-column mode, all group levels share one sort direction: the first grouped
|
|
99
|
+
column in the sort decides it.
|
|
100
|
+
- While leaving pivot mode, the grid shows its pre-pivot state for a moment before the answer
|
|
101
|
+
lands.
|
|
102
|
+
- The snapshot leaves out quick-filter text, filter values that are not text, numbers, booleans
|
|
103
|
+
or dates, aggregation functions other than `avg`, `sum`, `min`, `max` and `size`, and
|
|
104
|
+
singleSelect options computed from a row.
|
|
105
|
+
- The assistant cannot create, remove or change the type of a chart; it only fills the active one.
|
|
106
|
+
- Requests use the browser's default credentials mode (cookies only for the same origin). Put a
|
|
107
|
+
cross-origin route's credentials in `headers` or `getToken`.
|
|
108
|
+
|
|
109
|
+
## Status
|
|
110
|
+
|
|
111
|
+
0.1.0. Where this stands:
|
|
112
|
+
[gridsay.com/features#status](https://gridsay.com/features#status). The API may change before 1.0.
|
|
113
|
+
ESM only, no runtime dependencies besides `@gridsay/core`.
|
|
114
|
+
|
|
115
|
+
Example: [`examples/mui-x-data-grid`](https://github.com/gridsay/gridsay/tree/main/examples/mui-x-data-grid).
|
|
116
|
+
|
|
117
|
+
Gridsay is an independent product, not affiliated with, sponsored or endorsed by MUI. MUI is a
|
|
118
|
+
trademark of its owner.
|
|
119
|
+
|
|
120
|
+
## Licence
|
|
121
|
+
|
|
122
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { CONNECTION_FAILED, CONNECTION_FAILED_CODE, GridsayError, INVALID_ANSWER, INVALID_ANSWER_CODE, NOT_JSON, NOT_JSON_CODE, RequestHeaders, RequestOptions, UNAUTHENTICATED_USER, UNAUTHENTICATED_USER_CODE, UNEXPECTED_ERROR_CODE } from "@gridsay/core";
|
|
2
|
+
import { DataGridPremiumProps, GridApiPremium, GridRowId, ValueOptions } from "@mui/x-data-grid-premium";
|
|
3
|
+
type State = GridApiPremium['state'];
|
|
4
|
+
/** The column properties the helper reads. Kept structural, so a second copy of the grid's types still fits. */
|
|
5
|
+
type Column = {
|
|
6
|
+
field: string;
|
|
7
|
+
type?: string;
|
|
8
|
+
filterable?: boolean;
|
|
9
|
+
sortable?: boolean;
|
|
10
|
+
groupable?: boolean;
|
|
11
|
+
pivotable?: boolean;
|
|
12
|
+
aggregable?: boolean;
|
|
13
|
+
availableAggregationFunctions?: string[];
|
|
14
|
+
filterOperators?: readonly {
|
|
15
|
+
value: string;
|
|
16
|
+
requiresFilterValue?: boolean;
|
|
17
|
+
}[];
|
|
18
|
+
valueOptions?: ValueOptions[] | ((params: {
|
|
19
|
+
field: string;
|
|
20
|
+
}) => ValueOptions[]);
|
|
21
|
+
getOptionValue?: (option: ValueOptions) => unknown;
|
|
22
|
+
getOptionLabel?: (option: ValueOptions) => string;
|
|
23
|
+
};
|
|
24
|
+
/** The part of the grid API the helper reads and drives. */
|
|
25
|
+
type GridApi = Pick<GridApiPremium, 'getSortedRowIds' | 'setRowSelectionModel' | 'setAggregationModel' | 'setPivotActive'> & {
|
|
26
|
+
getAllColumns: () => Column[];
|
|
27
|
+
getRowNode: (id: GridRowId) => {
|
|
28
|
+
type: string;
|
|
29
|
+
} | null;
|
|
30
|
+
subscribeEvent: (event: 'rowsSet' | 'aggregationModelChange', handler: () => void) => () => void;
|
|
31
|
+
state: {
|
|
32
|
+
filter: Pick<State['filter'], 'filteredRowsLookup'> & {
|
|
33
|
+
filterModel: Pick<State['filter']['filterModel'], 'items'> & {
|
|
34
|
+
logicOperator?: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
sorting: Pick<State['sorting'], 'sortModel'>;
|
|
38
|
+
rowGrouping: Pick<State['rowGrouping'], 'model'>;
|
|
39
|
+
aggregation: Pick<State['aggregation'], 'model'>;
|
|
40
|
+
pivoting: Pick<State['pivoting'], 'active' | 'model'>;
|
|
41
|
+
chartsIntegration: Pick<State['chartsIntegration'], 'activeChartId'>;
|
|
42
|
+
rows: Pick<State['rows'], 'loading'>;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
type PromptResolver = NonNullable<DataGridPremiumProps['onPrompt']>;
|
|
46
|
+
export type PromptResolverOptions = {
|
|
47
|
+
/** Where the prompt is posted: your own proxy route, or the Gridsay gateway. */
|
|
48
|
+
url: string;
|
|
49
|
+
/** With it, the helper describes the grid to the server and adapts answers to the grid. */
|
|
50
|
+
apiRef?: {
|
|
51
|
+
readonly current: GridApi | null;
|
|
52
|
+
};
|
|
53
|
+
/** Added to each request. */
|
|
54
|
+
headers?: RequestHeaders | (() => RequestHeaders | Promise<RequestHeaders>);
|
|
55
|
+
/** A publishable key, sent as `x-api-key`. */
|
|
56
|
+
publishableKey?: string;
|
|
57
|
+
/** The end user's token, fetched for every request and sent as a bearer token. */
|
|
58
|
+
getToken?: RequestOptions['getToken'];
|
|
59
|
+
additionalContext?: string;
|
|
60
|
+
privateMode?: boolean;
|
|
61
|
+
/** Your id for the end user. */
|
|
62
|
+
referenceId?: string;
|
|
63
|
+
};
|
|
64
|
+
/** Replaces `unstable_gridDefaultPromptResolver` as the grid's `onPrompt`. */
|
|
65
|
+
export declare function createPromptResolver(options: PromptResolverOptions): PromptResolver;
|
|
66
|
+
export { CONNECTION_FAILED, CONNECTION_FAILED_CODE, GridsayError, INVALID_ANSWER, INVALID_ANSWER_CODE, NOT_JSON, NOT_JSON_CODE, type PromptResolver, UNAUTHENTICATED_USER, UNAUTHENTICATED_USER_CODE, UNEXPECTED_ERROR_CODE };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { CONNECTION_FAILED, CONNECTION_FAILED as CONNECTION_FAILED$1, CONNECTION_FAILED_CODE, CONNECTION_FAILED_CODE as CONNECTION_FAILED_CODE$1, GridsayError, GridsayError as GridsayError$1, INVALID_ANSWER, INVALID_ANSWER_CODE, NOT_JSON, NOT_JSON_CODE, UNAUTHENTICATED_USER, UNAUTHENTICATED_USER_CODE, UNEXPECTED_ERROR_CODE } from "@gridsay/core";
|
|
2
|
+
import { isRecord, promptOptionsOf, readResponse, requestHeaders } from "@gridsay/core/internal";
|
|
3
|
+
// src/grid.ts
|
|
4
|
+
/** Generated grouping columns, as exported by the grid (`GRID_ROW_GROUPING_SINGLE_GROUPING_FIELD`). */
|
|
5
|
+
const SINGLE_GROUPING_FIELD = "__row_group_by_columns_group__";
|
|
6
|
+
const groupingField = (column) => `__row_group_by_columns_group_${column}__`;
|
|
7
|
+
/**
|
|
8
|
+
* Grid-wide props. The public API does not expose them, so they are read from the private API
|
|
9
|
+
* the grid keeps on `apiRef.current`; when that is missing, every feature counts as on.
|
|
10
|
+
*/
|
|
11
|
+
function readSwitches(api) {
|
|
12
|
+
const symbol = Object.getOwnPropertySymbols(api).find((s) => s.description === "mui.api_private");
|
|
13
|
+
const privateApi = symbol && Reflect.get(api, symbol);
|
|
14
|
+
const props = isRecord(privateApi) && isRecord(privateApi.rootProps) ? privateApi.rootProps : {};
|
|
15
|
+
return {
|
|
16
|
+
filtering: props.disableColumnFilter !== true,
|
|
17
|
+
sorting: props.disableColumnSorting !== true,
|
|
18
|
+
grouping: props.disableRowGrouping !== true,
|
|
19
|
+
aggregation: props.disableAggregation !== true,
|
|
20
|
+
pivoting: props.disablePivoting !== true,
|
|
21
|
+
selection: props.rowSelection !== false,
|
|
22
|
+
multipleGroupingColumns: props.rowGroupingColumnMode === "multiple",
|
|
23
|
+
dataSource: props.dataSource != null
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const tick = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
27
|
+
/** Sorting a grouped column must target the grouping column, or group headers keep their order. */
|
|
28
|
+
function sortGroups(data, multiple) {
|
|
29
|
+
const grouped = new Set(data.grouping.map((g) => g.column));
|
|
30
|
+
const seen = /* @__PURE__ */ new Set();
|
|
31
|
+
return data.sorting.flatMap((sort) => {
|
|
32
|
+
if (!grouped.has(sort.column)) return [sort];
|
|
33
|
+
const column = multiple ? groupingField(sort.column) : SINGLE_GROUPING_FIELD;
|
|
34
|
+
if (seen.has(column)) return [];
|
|
35
|
+
seen.add(column);
|
|
36
|
+
return [{
|
|
37
|
+
...sort,
|
|
38
|
+
column
|
|
39
|
+
}];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function selectTop(api, count) {
|
|
43
|
+
const ids = api.getSortedRowIds().filter((id) => api.state.filter.filteredRowsLookup[id] !== false && api.getRowNode(id)?.type === "leaf").slice(0, count);
|
|
44
|
+
api.setRowSelectionModel({
|
|
45
|
+
type: "include",
|
|
46
|
+
ids: new Set(ids)
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function rowsSet(api, timeout) {
|
|
50
|
+
return new Promise((resolve) => {
|
|
51
|
+
const done = () => {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
unsubscribe();
|
|
54
|
+
resolve();
|
|
55
|
+
};
|
|
56
|
+
const timer = setTimeout(done, timeout);
|
|
57
|
+
const unsubscribe = api.subscribeEvent("rowsSet", done);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const sameAggregation = (a, b) => Object.keys(a).length === Object.keys(b).length && Object.keys(a).every((k) => a[k] === b[k]);
|
|
61
|
+
/** The charts integration writes back a stale aggregation model a render after a chart update. */
|
|
62
|
+
function keepAggregation(api, aggregation) {
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
const unsubscribe = api.subscribeEvent("aggregationModelChange", () => {
|
|
65
|
+
if (!sameAggregation(api.state.aggregation.model, aggregation)) api.setAggregationModel(aggregation);
|
|
66
|
+
});
|
|
67
|
+
setTimeout(() => {
|
|
68
|
+
unsubscribe();
|
|
69
|
+
resolve();
|
|
70
|
+
}, 500);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Prepares a checked answer for the grid. `response` goes back to the grid, which applies it
|
|
75
|
+
* synchronously; `applied` settles once the helper has finished its own changes after that.
|
|
76
|
+
*/
|
|
77
|
+
async function adapt(api, data) {
|
|
78
|
+
const on = readSwitches(api);
|
|
79
|
+
const pivots = on.pivoting && "columns" in data.pivoting;
|
|
80
|
+
const response = {
|
|
81
|
+
...data,
|
|
82
|
+
select: -1
|
|
83
|
+
};
|
|
84
|
+
if (on.grouping && !pivots && !on.dataSource && data.grouping.length > 0) response.sorting = sortGroups(data, on.multipleGroupingColumns);
|
|
85
|
+
if (!pivots && api.state.pivoting.active) {
|
|
86
|
+
const restored = rowsSet(api, 1e3);
|
|
87
|
+
api.setPivotActive(false);
|
|
88
|
+
await restored;
|
|
89
|
+
await tick();
|
|
90
|
+
}
|
|
91
|
+
const top = on.selection && data.select > 0 ? data.select : 0;
|
|
92
|
+
const charted = data.chart && !pivots && on.aggregation && api.state.chartsIntegration.activeChartId;
|
|
93
|
+
return {
|
|
94
|
+
response,
|
|
95
|
+
applied: (async () => {
|
|
96
|
+
const kept = charted ? keepAggregation(api, data.aggregation) : void 0;
|
|
97
|
+
await tick();
|
|
98
|
+
if (top > 0) {
|
|
99
|
+
if (on.dataSource) {
|
|
100
|
+
await tick();
|
|
101
|
+
if (api.state.rows.loading) {
|
|
102
|
+
await rowsSet(api, 15e3);
|
|
103
|
+
await tick();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
selectTop(api, top);
|
|
107
|
+
}
|
|
108
|
+
await kept;
|
|
109
|
+
})()
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
// src/queue.ts
|
|
113
|
+
const finish = (api, data) => api ? adapt(api, data) : {
|
|
114
|
+
response: data,
|
|
115
|
+
applied: Promise.resolve()
|
|
116
|
+
};
|
|
117
|
+
/** One prompt at a time, so a follow-up is sent after the previous answer is on the grid. */
|
|
118
|
+
function oneAtATime(send) {
|
|
119
|
+
let queue = Promise.resolve();
|
|
120
|
+
let pending;
|
|
121
|
+
return (query, context, conversationId) => {
|
|
122
|
+
const previous = {
|
|
123
|
+
queue,
|
|
124
|
+
call: pending
|
|
125
|
+
};
|
|
126
|
+
const call = { asked: conversationId };
|
|
127
|
+
pending = call;
|
|
128
|
+
const result = previous.queue.then(async () => {
|
|
129
|
+
const prev = previous.call;
|
|
130
|
+
const sent = await send(query, context, prev && prev.asked === conversationId ? prev.received ?? conversationId : conversationId);
|
|
131
|
+
call.received = sent.response.conversationId;
|
|
132
|
+
return sent;
|
|
133
|
+
});
|
|
134
|
+
const settle = () => {
|
|
135
|
+
if (pending === call) pending = void 0;
|
|
136
|
+
};
|
|
137
|
+
result.then(settle, settle);
|
|
138
|
+
queue = result.then((sent) => sent.applied).catch(() => void 0);
|
|
139
|
+
return result.then((sent) => sent.response);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
// src/snapshot.ts
|
|
143
|
+
const FUNCTIONS = [
|
|
144
|
+
"avg",
|
|
145
|
+
"sum",
|
|
146
|
+
"min",
|
|
147
|
+
"max",
|
|
148
|
+
"size"
|
|
149
|
+
];
|
|
150
|
+
const isFunction = (fn) => FUNCTIONS.includes(fn);
|
|
151
|
+
const isGenerated = (field) => field.startsWith("__") && field.endsWith("__");
|
|
152
|
+
function readOptions(column) {
|
|
153
|
+
if (!column.valueOptions) return [];
|
|
154
|
+
let raw;
|
|
155
|
+
try {
|
|
156
|
+
raw = typeof column.valueOptions === "function" ? column.valueOptions({ field: column.field }) : column.valueOptions;
|
|
157
|
+
} catch {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
return raw.flatMap((option) => {
|
|
161
|
+
const value = column.getOptionValue ? column.getOptionValue(option) : isRecord(option) ? option.value : option;
|
|
162
|
+
const label = column.getOptionLabel ? column.getOptionLabel(option) : isRecord(option) ? option.label : option;
|
|
163
|
+
const text = typeof label === "number" ? String(label) : label;
|
|
164
|
+
if (typeof text !== "string" || text.length > 255) return [];
|
|
165
|
+
if (typeof value === "number" || typeof value === "string" && value.length <= 255) return [{
|
|
166
|
+
value,
|
|
167
|
+
label: text
|
|
168
|
+
}];
|
|
169
|
+
return [];
|
|
170
|
+
}).slice(0, 500);
|
|
171
|
+
}
|
|
172
|
+
function readLimits(column) {
|
|
173
|
+
const limits = {};
|
|
174
|
+
for (const flag of [
|
|
175
|
+
"filterable",
|
|
176
|
+
"sortable",
|
|
177
|
+
"groupable",
|
|
178
|
+
"pivotable",
|
|
179
|
+
"aggregable"
|
|
180
|
+
]) if (column[flag] === false) limits[flag] = false;
|
|
181
|
+
const functions = column.availableAggregationFunctions?.filter((fn) => fn.length <= 64);
|
|
182
|
+
if (functions) limits.aggregationFunctions = functions.slice(0, 32);
|
|
183
|
+
const options = readOptions(column);
|
|
184
|
+
if (options.length > 0) limits.options = options;
|
|
185
|
+
return limits;
|
|
186
|
+
}
|
|
187
|
+
function toFilterValue(value) {
|
|
188
|
+
if (typeof value === "string" || typeof value === "boolean") return value;
|
|
189
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
190
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? void 0 : value.toISOString();
|
|
191
|
+
if (!Array.isArray(value)) return void 0;
|
|
192
|
+
if (value.every((item) => typeof item === "number")) return value;
|
|
193
|
+
return value.map(String);
|
|
194
|
+
}
|
|
195
|
+
function readFilters(api, columns) {
|
|
196
|
+
return api.state.filter.filterModel.items.flatMap((item) => {
|
|
197
|
+
const valueless = columns.get(item.field)?.filterOperators?.find((o) => o.value === item.operator)?.requiresFilterValue === false;
|
|
198
|
+
const value = valueless ? "" : toFilterValue(item.value);
|
|
199
|
+
if (value === void 0 || !valueless && (value === "" || Array.isArray(value) && !value.length)) return [];
|
|
200
|
+
return [{
|
|
201
|
+
column: item.field,
|
|
202
|
+
operator: item.operator,
|
|
203
|
+
value
|
|
204
|
+
}];
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
function readState(api, columns) {
|
|
208
|
+
const { filter, sorting, rowGrouping, aggregation, pivoting } = api.state;
|
|
209
|
+
const state = {};
|
|
210
|
+
const filters = readFilters(api, columns);
|
|
211
|
+
if (filters.length > 0) state.filters = filters;
|
|
212
|
+
if (filters.length > 1 && filter.filterModel.logicOperator === "or") state.filterOperator = "or";
|
|
213
|
+
const grouping = pivoting.active ? [] : rowGrouping.model;
|
|
214
|
+
const sorts = sorting.sortModel.flatMap((item) => {
|
|
215
|
+
const column = item.field === "__row_group_by_columns_group__" ? grouping[0] : item.field.replace(/^__row_group_by_columns_group_(.+)__$/, "$1");
|
|
216
|
+
return item.sort && column && !isGenerated(column) ? [{
|
|
217
|
+
column,
|
|
218
|
+
direction: item.sort
|
|
219
|
+
}] : [];
|
|
220
|
+
});
|
|
221
|
+
if (sorts.length > 0) state.sorting = sorts;
|
|
222
|
+
if (pivoting.active) {
|
|
223
|
+
const model = pivoting.model;
|
|
224
|
+
state.pivoting = {
|
|
225
|
+
columns: model.columns.filter((c) => !c.hidden).map((c) => ({
|
|
226
|
+
column: c.field,
|
|
227
|
+
direction: c.sort ?? "asc"
|
|
228
|
+
})),
|
|
229
|
+
rows: model.rows.filter((r) => !r.hidden).map((r) => r.field),
|
|
230
|
+
values: model.values.flatMap((v) => !v.hidden && isFunction(v.aggFunc) ? [{ [v.field]: v.aggFunc }] : [])
|
|
231
|
+
};
|
|
232
|
+
return state;
|
|
233
|
+
}
|
|
234
|
+
if (grouping.length > 0) state.grouping = grouping.map((column) => ({ column }));
|
|
235
|
+
const functions = Object.entries(aggregation.model).flatMap(([field, fn]) => isFunction(fn) ? [[field, fn]] : []);
|
|
236
|
+
if (functions.length > 0) state.aggregation = Object.fromEntries(functions);
|
|
237
|
+
return state;
|
|
238
|
+
}
|
|
239
|
+
/** What the grid allows and shows, leaving out everything at its default. */
|
|
240
|
+
function readSnapshot(api) {
|
|
241
|
+
const on = readSwitches(api);
|
|
242
|
+
const disabled = [];
|
|
243
|
+
if (!on.filtering) disabled.push("filtering");
|
|
244
|
+
if (!on.sorting) disabled.push("sorting");
|
|
245
|
+
if (!on.grouping) disabled.push("grouping");
|
|
246
|
+
if (!on.aggregation) disabled.push("aggregation");
|
|
247
|
+
if (!on.pivoting) disabled.push("pivoting");
|
|
248
|
+
if (!on.selection) disabled.push("selection");
|
|
249
|
+
if (!api.state.chartsIntegration.activeChartId) disabled.push("charts");
|
|
250
|
+
const all = api.getAllColumns().filter((c) => !isGenerated(c.field) && c.field.length <= 255);
|
|
251
|
+
const columns = {};
|
|
252
|
+
for (const column of all) {
|
|
253
|
+
const limits = readLimits(column);
|
|
254
|
+
if (Object.keys(limits).length > 0) columns[column.field] = limits;
|
|
255
|
+
}
|
|
256
|
+
const state = readState(api, new Map(all.map((c) => [c.field, c])));
|
|
257
|
+
const snapshot = {};
|
|
258
|
+
if (disabled.length > 0) snapshot.disabled = disabled;
|
|
259
|
+
if (Object.keys(columns).length > 0) snapshot.columns = columns;
|
|
260
|
+
snapshot.state = state;
|
|
261
|
+
return snapshot;
|
|
262
|
+
}
|
|
263
|
+
// src/index.ts
|
|
264
|
+
/** Replaces `unstable_gridDefaultPromptResolver` as the grid's `onPrompt`. */
|
|
265
|
+
function createPromptResolver(options) {
|
|
266
|
+
const { url, apiRef } = options;
|
|
267
|
+
const promptOptions = promptOptionsOf(options);
|
|
268
|
+
async function send(query, context, conversationId) {
|
|
269
|
+
const api = apiRef?.current ?? null;
|
|
270
|
+
const body = {
|
|
271
|
+
context,
|
|
272
|
+
query,
|
|
273
|
+
conversationId,
|
|
274
|
+
options: promptOptions
|
|
275
|
+
};
|
|
276
|
+
if (api) body.grid = readSnapshot(api);
|
|
277
|
+
const init = {
|
|
278
|
+
method: "POST",
|
|
279
|
+
headers: await requestHeaders(options),
|
|
280
|
+
body: JSON.stringify(body)
|
|
281
|
+
};
|
|
282
|
+
let response;
|
|
283
|
+
try {
|
|
284
|
+
response = await fetch(url, init);
|
|
285
|
+
} catch (cause) {
|
|
286
|
+
throw new GridsayError$1(CONNECTION_FAILED$1, {
|
|
287
|
+
code: CONNECTION_FAILED_CODE$1,
|
|
288
|
+
cause
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
const data = await readResponse(response);
|
|
292
|
+
return finish(api, data);
|
|
293
|
+
}
|
|
294
|
+
return oneAtATime(send);
|
|
295
|
+
}
|
|
296
|
+
export { CONNECTION_FAILED, CONNECTION_FAILED_CODE, GridsayError, INVALID_ANSWER, INVALID_ANSWER_CODE, NOT_JSON, NOT_JSON_CODE, UNAUTHENTICATED_USER, UNAUTHENTICATED_USER_CODE, UNEXPECTED_ERROR_CODE, createPromptResolver };
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gridsay/mui-x-data-grid",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Gridsay prompt resolver for the MUI X Data Grid Premium AI Assistant.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"gridsay",
|
|
7
|
+
"ai",
|
|
8
|
+
"mui",
|
|
9
|
+
"mui-x",
|
|
10
|
+
"data-grid",
|
|
11
|
+
"ai-assistant"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://gridsay.com/docs/browser-keys",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/gridsay/gridsay/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/gridsay/gridsay.git",
|
|
20
|
+
"directory": "packages/mui-x-data-grid"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@gridsay/core": "0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@mui/x-data-grid-premium": "^9.18.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"provenance": true
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@emotion/react": "11.14.0",
|
|
47
|
+
"@emotion/styled": "11.14.1",
|
|
48
|
+
"@mui/material": "9.6.0",
|
|
49
|
+
"@mui/x-data-grid-premium": "9.18.0",
|
|
50
|
+
"@types/react": "19.3.3",
|
|
51
|
+
"@types/react-dom": "19.3.0",
|
|
52
|
+
"react": "19.3.1",
|
|
53
|
+
"react-dom": "19.3.1",
|
|
54
|
+
"zod": "4.6.7"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsdown",
|
|
58
|
+
"typecheck": "tsc",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"check:package": "publint --strict && attw --pack . --profile esm-only"
|
|
61
|
+
}
|
|
6
62
|
}
|