Skip to content

Commit c011ac3

Browse files
author
Alyar
committed
Implement data display from AI service
1 parent 5b66b24 commit c011ac3

File tree

8 files changed

+71
-26
lines changed

8 files changed

+71
-26
lines changed

packages/devextreme/js/__internal/grids/data_grid/m_widget_base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const DATAGRID_DEPRECATED_TEMPLATE_WARNING = 'Specifying grid templates with the
2626
gridCore.registerModulesOrder([
2727
'stateStoring',
2828
'columns',
29-
'aiColumn',
3029
'selection',
3130
'editorFactory',
3231
'columnChooser',
@@ -40,6 +39,7 @@ gridCore.registerModulesOrder([
4039
'adaptivity',
4140
'data',
4241
'virtualScrolling',
42+
'aiColumn',
4343
'columnHeaders',
4444
'filterRow',
4545
'headerPanel',

packages/devextreme/js/__internal/grids/grid_core/ai_column/m_ai_column_controller.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { GenerateGridColumnCommandResult, RequestCallbacks } from '@js/comm
33
import type { Callback } from '@js/core/utils/callbacks';
44

55
import type { Column, ColumnsController } from '../columns_controller/m_columns_controller';
6-
import type { DataController } from '../data_controller/m_data_controller';
6+
import type { DataController, HandleDataChangedArguments } from '../data_controller/m_data_controller';
77
import { Controller } from '../m_modules';
88
import { AIColumnIntegrationController } from './m_ai_column_integration_controller';
99
import { getAICommandColumnDefaultOptions, isAIColumnAutoMode } from './utils';
@@ -15,14 +15,29 @@ export class AIColumnController extends Controller {
1515

1616
private aiColumnIntegrationController!: AIColumnIntegrationController;
1717

18-
private dataChangedHandler!: (e) => any;
18+
private dataSourceChangedHandler!: (e?: HandleDataChangedArguments) => void;
1919

2020
public aiRequestCompleted!: Callback;
2121

2222
public aiRequestRejected!: Callback;
2323

2424
private addAICommandColumn(): void {
25-
this.columnsController.addCommandColumn(getAICommandColumnDefaultOptions());
25+
const { dataController, aiColumnIntegrationController } = this;
26+
27+
this.columnsController.addCommandColumn({
28+
...getAICommandColumnDefaultOptions(),
29+
calculateCellValue(data) {
30+
const key = dataController.keyOf(data);
31+
const response = aiColumnIntegrationController.getColumnResponseData(this.name);
32+
33+
return response?.[key] ?? null;
34+
},
35+
});
36+
}
37+
38+
private subscribeToDataSourceChanged(): void {
39+
this.dataSourceChangedHandler = this.handleDataSourceChanged.bind(this);
40+
this.dataController.dataSource()?.changed.add(this.dataSourceChangedHandler);
2641
}
2742

2843
protected callbackNames(): string[] {
@@ -33,13 +48,11 @@ export class AIColumnController extends Controller {
3348
this.columnsController = this.getController('columns');
3449
this.dataController = this.getController('data');
3550

36-
this.addAICommandColumn();
37-
3851
this.aiColumnIntegrationController = new AIColumnIntegrationController(this.component);
3952
this.aiColumnIntegrationController.init();
4053

41-
this.dataChangedHandler = this.handleDataChanged.bind(this);
42-
this.dataController.changed.add(this.dataChangedHandler);
54+
this.subscribeToDataSourceChanged();
55+
this.addAICommandColumn();
4356
}
4457

4558
private showResults(
@@ -54,9 +67,13 @@ export class AIColumnController extends Controller {
5467
return this.columnsController.getColumns().filter((col) => col.type === 'ai') as Column[];
5568
}
5669

57-
private handleDataChanged(e) {
70+
private handleDataSourceChanged(args?: HandleDataChangedArguments): void {
5871
const aiColumns = this.getAIColumns();
5972

73+
if (args?.changeType === 'loadError') {
74+
return;
75+
}
76+
6077
for (const col of aiColumns) {
6178
if (isAIColumnAutoMode(col)) {
6279
this.refreshAIColumn(col.name as string);
@@ -96,6 +113,7 @@ export class AIColumnController extends Controller {
96113
return {
97114
onComplete: (data): void => {
98115
this.aiRequestCompleted.fire(data);
116+
this.dataController.updateItems();
99117
},
100118
onError: (error: Error): void => {
101119
this.aiRequestRejected.fire(error);
@@ -105,14 +123,16 @@ export class AIColumnController extends Controller {
105123

106124
public clearAIColumn(columnName: string): void {
107125
this.aiColumnIntegrationController.abortRequest(columnName);
126+
this.aiColumnIntegrationController.resetColumnResponseData();
108127
this.columnsController.columnOption(columnName, 'ai.prompt', '');
128+
this.dataController.updateItems();
109129
}
110130

111131
public getAIColumnText(columnName: string, key: any): void {
112132

113133
}
114134

115135
public dispose(): void {
116-
this.dataController.changed.remove(this.dataChangedHandler);
136+
this.dataController.dataSource()?.changed.remove(this.dataSourceChangedHandler);
117137
}
118138
}

packages/devextreme/js/__internal/grids/grid_core/ai_column/m_ai_column_integration_controller.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { getDataFromRowItems, reduceDataCachedKeys } from './utils';
1515
export class AIColumnIntegrationController extends Controller {
1616
private aborts: Record<string, (() => void) | undefined> = { };
1717

18+
private responseData: Record<string, Record<PropertyKey, unknown>> = {};
19+
1820
private columnsController!: ColumnsController;
1921

2022
private dataController!: DataController;
@@ -79,8 +81,8 @@ export class AIColumnIntegrationController extends Controller {
7981
const keyField = this.dataController.key();
8082
const reducedData = reduceDataCachedKeys(data, cachedResponse, keyField);
8183
const areAllDataCached = Object.keys(reducedData).length === 0;
84+
8285
if (areAllDataCached) {
83-
this.showResult(columnName, {}, cachedResponse);
8486
return;
8587
}
8688

@@ -103,14 +105,12 @@ export class AIColumnIntegrationController extends Controller {
103105
this.abortRequest(columnName);
104106
}
105107

106-
/* eslint-disable @typescript-eslint/no-unused-vars */
107-
private showResult(
108+
private saveResult(
108109
columnName: string,
109110
response: Record<PropertyKey, unknown>,
110-
cachedData: Record<PropertyKey, string>,
111+
// cachedData: Record<PropertyKey, string>,
111112
): void {
112-
// TODO: Implement result display logic
113-
const mergedData = { ...cachedData, ...response };
113+
this.responseData[columnName] = response;
114114
}
115115

116116
private getAICommandCallbacks(
@@ -129,10 +129,10 @@ export class AIColumnIntegrationController extends Controller {
129129
};
130130

131131
this.executeAction('onAIColumnResponseReceived', args);
132-
this.showResult(
132+
this.saveResult(
133133
columnName,
134134
finalResponse.data,
135-
cachedResponse,
135+
// cachedResponse,
136136
);
137137
this.processCommandCompletion(columnName);
138138
callBacks?.onComplete?.(finalResponse);
@@ -189,4 +189,12 @@ export class AIColumnIntegrationController extends Controller {
189189
super.dispose();
190190
Object.keys(this.aborts).forEach((columnName) => this.abortRequest(columnName));
191191
}
192+
193+
public getColumnResponseData(columnName: string): Record<PropertyKey, unknown> | null {
194+
return this.responseData[columnName] ?? null;
195+
}
196+
197+
public resetColumnResponseData(): void {
198+
this.responseData = {};
199+
}
192200
}

packages/devextreme/js/__internal/grids/grid_core/ai_column/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Column } from '../columns_controller/m_columns_controller';
44
import type { Item, UserData } from '../data_controller/m_data_controller';
55
import { AI_COLUMN_NAME, CLASSES } from './const';
66

7-
export const getAICommandColumnDefaultOptions = (): unknown => ({
7+
export const getAICommandColumnDefaultOptions = (): object => ({
88
type: AI_COLUMN_NAME,
99
command: AI_COLUMN_NAME,
1010
cssClass: CLASSES.aiColumn,

packages/devextreme/js/__internal/grids/grid_core/columns_controller/m_columns_controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export interface Column extends ColumnBase {
9292
visibleWidth?: string | number;
9393
hidingPriority?: number;
9494
ai?: ColumnAIOptions;
95+
command?: string;
9596
}
9697

9798
export class ColumnsController extends modules.Controller {
@@ -1740,10 +1741,13 @@ export class ColumnsController extends modules.Controller {
17401741
extend(true, calculatedColumnOptions, {
17411742
allowSorting: false,
17421743
allowGrouping: false,
1743-
calculateCellValue() {
1744-
return null;
1745-
},
17461744
});
1745+
1746+
if (columnOptions?.type !== AI_COLUMN_NAME) {
1747+
calculatedColumnOptions.calculateCellValue = function () {
1748+
return null;
1749+
};
1750+
}
17471751
}
17481752

17491753
if (bandColumn) {

packages/devextreme/js/__internal/grids/grid_core/data_controller/m_data_controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { SelectionController } from '@ts/grids/grid_core/selection/m_select
2020
import type { StateStoringController } from '@ts/grids/grid_core/state_storing/m_state_storing_core';
2121
import type { ValidatingController } from '@ts/grids/grid_core/validating/m_validating';
2222

23+
import { AI_COLUMN_NAME } from '../ai_column/const';
2324
import modules from '../m_modules';
2425
import type {
2526
Controllers, Module,
@@ -63,7 +64,7 @@ const changePaging = function (that, optionName, value) {
6364
return 0;
6465
};
6566

66-
interface HandleDataChangedArguments {
67+
export interface HandleDataChangedArguments {
6768
changeType?: 'refresh' | 'update' | 'loadError';
6869
isDelayed?: boolean;
6970
isLiveUpdate?: boolean;
@@ -803,7 +804,7 @@ export class DataController extends DataHelperMixin(modules.Controller) {
803804
for (let i = 0; i < columns.length; i++) {
804805
const column = columns[i];
805806
value = isModified ? undefined : null;
806-
if (!column.command) {
807+
if (!column.command || column.type === AI_COLUMN_NAME) {
807808
if (column.calculateCellValue) {
808809
value = column.calculateCellValue(data);
809810
} else if (column.dataField) {

packages/devextreme/js/__internal/grids/grid_core/views/m_rows_view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import type { ResizingController } from '@ts/grids/grid_core/views/m_grid_view';
2929

3030
import { CLASSES as REORDERING_CLASSES } from '../columns_resizing_reordering/const';
3131
import type { EditingController } from '../editing/m_editing';
32-
import type { EditorFactory } from '../editor_factory/m_editor_factory';
3332
import gridCoreUtils from '../m_utils';
3433
import { CLASSES } from '../sticky_columns/const';
3534
import { ColumnsView } from './m_columns_view';
35+
import { getCellText } from './utils';
3636

3737
const ROWS_VIEW_CLASS = 'rowsview';
3838
const CONTENT_CLASS = 'content';
@@ -911,7 +911,7 @@ export class RowsView extends ColumnsView {
911911
parameters.data = data;
912912
parameters.rowType = row.rowType;
913913
parameters.values = row.values;
914-
parameters.text = !column.command ? gridCoreUtils.formatValue(displayValue, column) : '';
914+
parameters.text = getCellText(column, displayValue);
915915
parameters.rowIndex = row.rowIndex;
916916
parameters.summaryItems = summaryCells && summaryCells[options.columnIndex];
917917
parameters.resized = column.resizedCallbacks;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { AI_COLUMN_NAME } from '../ai_column/const';
2+
import type { Column } from '../columns_controller/m_columns_controller';
3+
import gridCoreUtils from '../m_utils';
4+
5+
export const getCellText = (
6+
column: Column,
7+
displayValue: unknown,
8+
): string => (
9+
!column.command || column.type === AI_COLUMN_NAME
10+
? gridCoreUtils.formatValue(displayValue, column) as string
11+
: ''
12+
);

0 commit comments

Comments
 (0)