3cea19baf4
## Summary Adds a new **VIEW** tool category for the AI chat, enabling it to work with views: - **get-views**: List views in the workspace, optionally filtered by object metadata ID - **get-view-query-parameters**: Convert a view's filters and sorts into GraphQL query parameters that can be passed to existing `find_*` data tools - **create-view**, **update-view**, **delete-view**: CRUD operations for view management ### Key design decisions 1. **No pagination duplication**: Instead of creating a `find-records-from-view` tool that would duplicate pagination logic, `get-view-query-parameters` returns filter/sort parameters that the AI can pass to existing record-fetching tools. 2. **Permission model**: - Read tools (get-views, get-view-query-parameters) are available to all users - Write tools require the `VIEW` permission - UNLISTED views can only be modified by their creator 3. **Leverages existing utilities**: Uses `computeRecordGqlOperationFilter` from `twenty-shared` for filter conversion. ### Files changed - Added `ViewToolProvider`, `ViewToolsFactory`, and `ViewQueryParamsService` - Added `VIEW` to `ToolCategory` enum and tool registry - Updated `chat-execution.service.ts` to include view tools in the catalog and pass viewId in browsing context - Extracted shared `formatValidationErrors` utility to reduce duplication ## Test plan - [x] Unit tests for `ViewToolsFactory` - [x] Unit tests for `ViewQueryParamsService` - [x] Lint and typecheck pass
145 lines
4.6 KiB
TypeScript
145 lines
4.6 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import {
|
|
OrderByDirection,
|
|
RecordFilterGroupLogicalOperator,
|
|
type RecordGqlOperationFilter,
|
|
} from 'twenty-shared/types';
|
|
import {
|
|
computeRecordGqlOperationFilter,
|
|
isDefined,
|
|
type RecordFilter,
|
|
type RecordFilterGroup,
|
|
} from 'twenty-shared/utils';
|
|
|
|
import { type ObjectRecordOrderBy } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
|
|
|
|
import { WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.service';
|
|
import { findFlatEntityByIdInFlatEntityMapsOrThrow } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps-or-throw.util';
|
|
import { ViewFilterGroupLogicalOperator } from 'src/engine/metadata-modules/view-filter-group/enums/view-filter-group-logical-operator';
|
|
import { ViewSortDirection } from 'src/engine/metadata-modules/view-sort/enums/view-sort-direction';
|
|
import { ViewType } from 'src/engine/metadata-modules/view/enums/view-type.enum';
|
|
import { ViewService } from 'src/engine/metadata-modules/view/services/view.service';
|
|
|
|
export type ViewQueryParams = {
|
|
objectNameSingular: string;
|
|
filter: RecordGqlOperationFilter;
|
|
orderBy: ObjectRecordOrderBy;
|
|
viewName: string;
|
|
viewType: ViewType;
|
|
};
|
|
|
|
@Injectable()
|
|
export class ViewQueryParamsService {
|
|
constructor(
|
|
private readonly viewService: ViewService,
|
|
private readonly flatEntityMapsCacheService: WorkspaceManyOrAllFlatEntityMapsCacheService,
|
|
) {}
|
|
|
|
async resolveViewToQueryParams(
|
|
viewId: string,
|
|
workspaceId: string,
|
|
currentWorkspaceMemberId?: string,
|
|
): Promise<ViewQueryParams> {
|
|
const view = await this.viewService.findById(viewId, workspaceId);
|
|
|
|
if (!view) {
|
|
throw new Error(`View with id ${viewId} not found`);
|
|
}
|
|
|
|
const { flatObjectMetadataMaps, flatFieldMetadataMaps } =
|
|
await this.flatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
|
{
|
|
workspaceId,
|
|
flatMapsKeys: ['flatObjectMetadataMaps', 'flatFieldMetadataMaps'],
|
|
},
|
|
);
|
|
|
|
const objectMetadata = findFlatEntityByIdInFlatEntityMapsOrThrow({
|
|
flatEntityId: view.objectMetadataId,
|
|
flatEntityMaps: flatObjectMetadataMaps,
|
|
});
|
|
|
|
const recordFilters: RecordFilter[] = (view.viewFilters ?? [])
|
|
.map((viewFilter) => {
|
|
const field = flatFieldMetadataMaps.byId[viewFilter.fieldMetadataId];
|
|
|
|
if (!field) return null;
|
|
|
|
return {
|
|
id: viewFilter.id,
|
|
fieldMetadataId: viewFilter.fieldMetadataId,
|
|
value: viewFilter.value ?? '',
|
|
type: field.type,
|
|
recordFilterGroupId: viewFilter.viewFilterGroupId,
|
|
operand: viewFilter.operand,
|
|
subFieldName: viewFilter.subFieldName,
|
|
} as RecordFilter;
|
|
})
|
|
.filter(isDefined);
|
|
|
|
const recordFilterGroups: RecordFilterGroup[] = (
|
|
view.viewFilterGroups ?? []
|
|
).map((group) => ({
|
|
id: group.id,
|
|
parentRecordFilterGroupId: group.parentViewFilterGroupId,
|
|
logicalOperator:
|
|
group.logicalOperator === ViewFilterGroupLogicalOperator.OR
|
|
? RecordFilterGroupLogicalOperator.OR
|
|
: RecordFilterGroupLogicalOperator.AND,
|
|
}));
|
|
|
|
const fields = recordFilters
|
|
.map((filter) => {
|
|
const field = flatFieldMetadataMaps.byId[filter.fieldMetadataId];
|
|
|
|
if (!field) return null;
|
|
|
|
return {
|
|
id: field.id,
|
|
name: field.name,
|
|
type: field.type,
|
|
label: field.label,
|
|
options: field.options?.map((opt) => ({
|
|
id: opt.id ?? '',
|
|
label: opt.label,
|
|
value: opt.value,
|
|
color: 'color' in opt ? opt.color : undefined,
|
|
position: opt.position,
|
|
})),
|
|
};
|
|
})
|
|
.filter(isDefined);
|
|
|
|
const filter = computeRecordGqlOperationFilter({
|
|
fields,
|
|
recordFilters,
|
|
recordFilterGroups,
|
|
filterValueDependencies: { currentWorkspaceMemberId },
|
|
});
|
|
|
|
const orderBy: ObjectRecordOrderBy = (view.viewSorts ?? [])
|
|
.map((sort) => {
|
|
const field = flatFieldMetadataMaps.byId[sort.fieldMetadataId];
|
|
|
|
if (!field) return null;
|
|
|
|
return {
|
|
[field.name]:
|
|
sort.direction === ViewSortDirection.DESC
|
|
? OrderByDirection.DescNullsLast
|
|
: OrderByDirection.AscNullsFirst,
|
|
};
|
|
})
|
|
.filter(isDefined);
|
|
|
|
return {
|
|
objectNameSingular: objectMetadata.nameSingular,
|
|
filter,
|
|
orderBy,
|
|
viewName: view.name,
|
|
viewType: view.type,
|
|
};
|
|
}
|
|
}
|