Fix find tool filters by mapping many-to-one relations to fieldId#15716
Fix find tool filters by mapping many-to-one relations to fieldId#15716FelixMalfait merged 5 commits intomainfrom
Conversation
…ndoffFromDefaultAgent` property. Update transformation utility to streamline agent data extraction.
…le to streamline codebase.
…t keyword to streamline code structure.
- Added imports for FieldMetadataType and RelationType. - Updated filter handling to correctly process many-to-one relation fields by appending 'Id' to the field name when applicable. - Improved code readability and structure by returning early if no filter schema is found.
…x/find-tool-many-to-one-filter
| @@ -1,14 +0,0 @@ | |||
| export const hasStructuredStreamData = (data: string): boolean => { | |||
There was a problem hiding this comment.
not related to this PR
| @@ -20,7 +20,7 @@ import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.ent | |||
| import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator'; | |||
| import { WorkspaceAuthGuard } from 'src/engine/guards/workspace-auth.guard'; | |||
|
|
|||
| export interface ChatRequest { | |||
There was a problem hiding this comment.
not related to this PR
There was a problem hiding this comment.
Greptile Overview
Greptile Summary
This PR fixes a critical bug where many-to-one relation filters were exposed under the relation name (e.g., company) instead of the foreign-key field name (e.g., companyId), causing agent-tool queries to never match records.
Key Changes:
- Modified
generateFindToolInputSchemainfind-tool.zod-schema.tsto detect many-to-one relations and remap filter keys from<fieldName>to<fieldName>Id - The fix aligns with existing logic in
generateFieldFilterZodSchemawhich already generates UUID filters for many-to-one relations - Additional cleanup: removed unused
createHandoffFromDefaultAgentproperty from standard agent definitions and related files - Minor refactor: changed
ChatRequestinterface from exported to internal scope inai.controller.ts
Implementation Details:
The fix uses isFieldMetadataEntityOfType to check if a field is a RELATION type, then validates field.settings?.relationType === RelationType.MANY_TO_ONE. When true, it appends Id to the field name before adding it to the filter shape. This ensures consistency between the schema definition and actual database queries.
Confidence Score: 5/5
- This PR is safe to merge with minimal risk
- The core fix is straightforward, well-contained, and consistent with existing patterns in the codebase. The logic correctly detects many-to-one relations and remaps filter keys appropriately. All cleanup changes remove unused code without affecting functionality.
- No files require special attention
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| packages/twenty-server/src/engine/core-modules/record-crud/zod-schemas/find-tool.zod-schema.ts | 5/5 | Maps many-to-one relation filters to fieldId format; logic is correct and consistent with field-filters.zod-schema.ts |
| packages/twenty-server/src/engine/core-modules/record-crud/zod-schemas/field-filters.zod-schema.ts | 5/5 | Already correctly generates filter schema with fieldId description for many-to-one relations; no changes needed |
Sequence Diagram
sequenceDiagram
participant Agent as AI Agent Tool
participant Schema as generateFindToolInputSchema
participant Filter as generateFieldFilterZodSchema
participant Meta as Field Metadata
Agent->>Schema: Request input schema for object
Schema->>Meta: Iterate over fields
loop For each field
Meta->>Schema: Return field metadata
Schema->>Schema: Check if excluded or restricted
Schema->>Filter: generateFieldFilterZodSchema(field)
Filter->>Filter: Check field type
alt RELATION field with MANY_TO_ONE
Filter-->>Schema: Return UUID filter schema (with fieldId description)
else Other field types
Filter-->>Schema: Return appropriate filter schema
end
Schema->>Schema: Check if MANY_TO_ONE relation
alt isManyToOneRelationField = true
Schema->>Schema: Add filter as `${field.name}Id`
else Regular field
Schema->>Schema: Add filter as `${field.name}`
end
end
Schema-->>Agent: Return complete Zod schema with remapped filters
9 files reviewed, no comments
|
🚀 Preview Environment Ready! Your preview environment is available at: http://bore.pub:44218 This environment will automatically shut down when the PR is closed or after 5 hours. |
|
Thanks @abdulrahmancodes for your contribution! |

Root Cause
Many-to-one relation filters were being exposed under the relation name (e.g.,
company) instead of the corresponding foreign-key attribute (e.g.,companyId).Change
<fieldName>Id.