Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion backend/src/entities/ai/ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { WidgetTypeEnum } from '../../enums/widget-type.enum.js';
import { QueryOrderingEnum } from '../../enums/query-ordering.enum.js';
import { checkFieldAutoincrement } from '../../helpers/check-field-autoincrement.js';
import { TableWidgetEntity } from '../widget/table-widget.entity.js';
import { TableInformation } from './ai-data-entities/types/ai-module-types.js';
Expand All @@ -12,6 +13,8 @@ interface AIGeneratedTableSettings {
search_fields: string[];
readonly_fields: string[];
columns_view: string[];
ordering: string;
ordering_field: string;
widgets: Array<{
field_name: string;
widget_type: string;
Expand Down Expand Up @@ -72,7 +75,9 @@ For each table, provide:
2. search_fields: Columns that should be searchable (text fields like name, email, title)
3. readonly_fields: Columns that should not be editable (like auto_increment, timestamps)
4. columns_view: All columns in preferred display order
5. widgets: For each column, suggest the best widget type from: ${widgetTypes}
5. ordering: Default sort order - either "ASC" or "DESC" (use "DESC" for tables with timestamps to show newest first)
6. ordering_field: Column name to sort by default (prefer created_at, updated_at, or primary key)
7. widgets: For each column, suggest the best widget type from: ${widgetTypes}
Available widget types and when to use them:
- Password: for password fields
Expand Down Expand Up @@ -111,6 +116,8 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
"search_fields": ["name", "email"],
"readonly_fields": ["id", "created_at"],
"columns_view": ["id", "name", "email", "created_at"],
"ordering": "DESC",
"ordering_field": "created_at",
"widgets": [
{
"field_name": "column_name",
Expand Down Expand Up @@ -148,6 +155,8 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
settings.search_fields = this.filterValidColumns(tableSettings.search_fields, validColumnNames);
settings.readonly_fields = this.filterValidColumns(tableSettings.readonly_fields, validColumnNames);
settings.columns_view = this.filterValidColumns(tableSettings.columns_view, validColumnNames);
settings.ordering = this.mapOrdering(tableSettings.ordering);
settings.ordering_field = validColumnNames.includes(tableSettings.ordering_field) ? tableSettings.ordering_field : null;
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation logic doesn't handle the case when tableSettings.ordering_field is undefined or null. If the AI response is missing this field or it's null, validColumnNames.includes(undefined) or validColumnNames.includes(null) will be called, which will always return false but is not semantically clear. Consider adding an explicit check for undefined/null before the includes check.

Suggested change
settings.ordering_field = validColumnNames.includes(tableSettings.ordering_field) ? tableSettings.ordering_field : null;
const orderingField = tableSettings.ordering_field;
settings.ordering_field =
orderingField != null && validColumnNames.includes(orderingField) ? orderingField : null;

Copilot uses AI. Check for mistakes.
Comment on lines +158 to +159
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no validation to ensure that ordering and ordering_field are set together. If the AI provides an ordering direction but an invalid ordering_field (or vice versa), the table settings will have incomplete sorting configuration. Consider validating that if one is set, the other should also be set, or set both to null if either is invalid to maintain consistency.

Suggested change
settings.ordering = this.mapOrdering(tableSettings.ordering);
settings.ordering_field = validColumnNames.includes(tableSettings.ordering_field) ? tableSettings.ordering_field : null;
const mappedOrdering = this.mapOrdering(tableSettings.ordering);
const validOrderingField = validColumnNames.includes(tableSettings.ordering_field)
? tableSettings.ordering_field
: null;
if (mappedOrdering && validOrderingField) {
settings.ordering = mappedOrdering;
settings.ordering_field = validOrderingField;
} else {
settings.ordering = null;
settings.ordering_field = null;
}

Copilot uses AI. Check for mistakes.
settings.table_widgets = tableSettings.widgets
.filter((w) => validColumnNames.includes(w.field_name))
.map((widgetData) => {
Expand All @@ -167,6 +176,12 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
return columns?.filter((col) => validColumnNames.includes(col)) || [];
}

private mapOrdering(ordering: string): QueryOrderingEnum | null {
if (ordering === 'ASC') return QueryOrderingEnum.ASC;
if (ordering === 'DESC') return QueryOrderingEnum.DESC;
return null;
}

private mapWidgetType(widgetType: string): WidgetTypeEnum | undefined {
const widgetTypeMap = new Map<string, WidgetTypeEnum>([
['Password', WidgetTypeEnum.Password],
Expand Down
Loading