Skip to content

Migrate agent v2#16214

Merged
prastoin merged 54 commits intomainfrom
migrate-agent-v2
Dec 3, 2025
Merged

Migrate agent v2#16214
prastoin merged 54 commits intomainfrom
migrate-agent-v2

Conversation

@prastoin
Copy link
Copy Markdown
Contributor

@prastoin prastoin commented Dec 1, 2025

Introduction

Closes twentyhq/core-team-issues#1980

In this PR we migrate the agent from v1 to v2.

New FlatRoleTargetByAgentIdMaps

Derivated the flatRoleTargetMaps to be building a flatRoleTargetByAgentIdMaps to ease retrieving a roleId to associate to an agent

Coverage

Added strong coverage on both failing and successful CRU agents operations

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 1, 2025

🚀 Preview Environment Ready!

Your preview environment is available at: http://bore.pub:58196

This environment will automatically shut down when the PR is closed or after 5 hours.

@prastoin
Copy link
Copy Markdown
Contributor Author

prastoin commented Dec 1, 2025

I need to handle the pending migration

@prastoin prastoin marked this pull request as ready for review December 2, 2025 10:27
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Dec 2, 2025

Greptile Overview

Greptile Summary

This PR migrates the agent system from v1 to v2, introducing a new architecture built on FlatAgent maps and the workspace migration v2 system.

Key Changes:

  • Refactored AgentService to use FlatAgentMaps and WorkspaceMigrationValidateBuildAndRunService for all CRUD operations
  • Added WorkspaceFlatRoleTargetByAgentIdService to efficiently map agents to their assigned roles
  • Created comprehensive validators (FlatAgentValidatorService) for agent creation, update, and deletion with proper error handling
  • Added new action handlers for create/update/delete agent operations in the migration runner
  • Introduced AgentGraphqlApiExceptionInterceptor with exhaustive exception code handling
  • Extended AgentExceptionCode with INVALID_AGENT_INPUT, AGENT_ALREADY_EXISTS, and AGENT_IS_STANDARD
  • Fixed nullable types in AgentEntity for icon, description, and modelConfiguration
  • Added extensive integration test coverage for agent CRUD operations (both success and failure scenarios)

Issues Found:

  • Unit test for ROLE_NOT_FOUND exception handling has incorrect expectation (expects rethrow but handler converts to NotFoundError)

Confidence Score: 4/5

  • This PR is safe to merge with one minor test fix needed.
  • The migration is well-structured with comprehensive validation and test coverage. One unit test has an incorrect expectation but this doesn't affect production code.
  • packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/utils/tests/agent-graphql-api-exception-handler.util.spec.ts - test expects wrong error type for ROLE_NOT_FOUND

Important Files Changed

File Analysis

Filename Score Overview
packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/agent.service.ts 5/5 Migrated agent service to use v2 migration system with FlatAgent maps and role target handling. Well-structured refactoring.
packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/utils/tests/agent-graphql-api-exception-handler.util.spec.ts 2/5 Test file contains incorrect expectation: expects ROLE_NOT_FOUND to be rethrown but handler converts it to NotFoundError.
packages/twenty-server/src/engine/metadata-modules/flat-agent/services/workspace-flat-role-target-by-agent-id.service.ts 5/5 New service that computes and caches a map from agentId to FlatRoleTarget for efficient role lookups.
packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/utils/from-update-agent-input-to-flat-agent-to-update.util.ts 5/5 Handles agent update transformation including role target create/update/delete logic based on roleId changes.
packages/twenty-server/src/engine/workspace-manager/workspace-migration-v2/workspace-migration-builder-v2/validators/services/flat-agent-validator.service.ts 5/5 Comprehensive validation service for agent CRUD operations with proper error reporting and standard agent protection.

Sequence Diagram

sequenceDiagram
    participant Client
    participant AgentResolver
    participant AgentService
    participant FlatEntityMapsCacheService
    participant WorkspaceMigrationService
    participant FlatAgentValidator
    participant ActionHandler
    participant Database

    Note over Client,Database: Create Agent Flow
    Client->>AgentResolver: createOneAgent(input)
    AgentResolver->>AgentService: createOneAgent(input, workspaceId)
    AgentService->>FlatEntityMapsCacheService: getOrRecomputeManyOrAllFlatEntityMaps()
    FlatEntityMapsCacheService-->>AgentService: existingFlatAgentMaps, flatRoleTargetMaps, flatRoleMaps
    AgentService->>AgentService: fromCreateAgentInputToFlatAgent()
    AgentService->>WorkspaceMigrationService: validateBuildAndRunWorkspaceMigration()
    WorkspaceMigrationService->>FlatAgentValidator: validateFlatAgentCreation()
    FlatAgentValidator-->>WorkspaceMigrationService: validation result
    WorkspaceMigrationService->>ActionHandler: executeForMetadata()
    ActionHandler->>Database: save(agentEntity)
    Database-->>ActionHandler: created
    ActionHandler-->>WorkspaceMigrationService: success
    WorkspaceMigrationService-->>AgentService: null (success)
    AgentService->>FlatEntityMapsCacheService: getOrRecomputeManyOrAllFlatEntityMaps()
    FlatEntityMapsCacheService-->>AgentService: recomputedFlatAgentMaps
    AgentService-->>AgentResolver: FlatAgentWithRoleId
    AgentResolver->>AgentResolver: fromFlatAgentWithRoleIdToAgentDto()
    AgentResolver-->>Client: AgentDTO
Loading

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.

77 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

import { IsEnum } from 'class-validator';

export class AgentResponseFormatText {
@IsEnum(['text'])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Equals('text') seems less hacky?

Copy link
Copy Markdown
Member

@Weiko Weiko left a comment

Choose a reason for hiding this comment

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

LGTM

@prastoin prastoin merged commit 59672e3 into main Dec 3, 2025
74 checks passed
@prastoin prastoin deleted the migrate-agent-v2 branch December 3, 2025 10:51
@twenty-eng-sync
Copy link
Copy Markdown

Hey @prastoin! After you've done the QA of your Pull Request, you can mark it as done here. Thank you!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate agent to v2

3 participants