Skip to content

Fix find tool filters by mapping many-to-one relations to fieldId#15716

Merged
FelixMalfait merged 5 commits intomainfrom
fix/find-tool-many-to-one-filter
Nov 8, 2025
Merged

Fix find tool filters by mapping many-to-one relations to fieldId#15716
FelixMalfait merged 5 commits intomainfrom
fix/find-tool-many-to-one-filter

Conversation

@abdulrahmancodes
Copy link
Copy Markdown
Contributor

@abdulrahmancodes abdulrahmancodes commented Nov 7, 2025

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

  • Detect many-to-one relation metadata and remap those filter keys to <fieldName>Id.
  • Removed some unused code unrelated to this fix.

…ndoffFromDefaultAgent` property. Update transformation utility to streamline agent data extraction.
- 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.
@@ -1,14 +0,0 @@
export const hasStructuredStreamData = (data: string): boolean => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not related to this PR

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

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 generateFindToolInputSchema in find-tool.zod-schema.ts to detect many-to-one relations and remap filter keys from <fieldName> to <fieldName>Id
  • The fix aligns with existing logic in generateFieldFilterZodSchema which already generates UUID filters for many-to-one relations
  • Additional cleanup: removed unused createHandoffFromDefaultAgent property from standard agent definitions and related files
  • Minor refactor: changed ChatRequest interface from exported to internal scope in ai.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
Loading

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 7, 2025

🚀 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.

@FelixMalfait FelixMalfait merged commit 06d8c8c into main Nov 8, 2025
51 of 52 checks passed
@FelixMalfait FelixMalfait deleted the fix/find-tool-many-to-one-filter branch November 8, 2025 13:32
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 8, 2025

Thanks @abdulrahmancodes for your contribution!
This marks your 71st PR on the repo. You're top 1% of all our contributors 🎉
See contributor page - Share on LinkedIn - Share on Twitter

Contributions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants