Skip to content

fix: implement regex operation in Condition node#5711

Open
thecaptain789 wants to merge 2 commits intoFlowiseAI:mainfrom
thecaptain789:fix/condition-regex-operation
Open

fix: implement regex operation in Condition node#5711
thecaptain789 wants to merge 2 commits intoFlowiseAI:mainfrom
thecaptain789:fix/condition-regex-operation

Conversation

@thecaptain789
Copy link

Description

The 'Regex' operation was available in the frontend dropdown for Condition nodes but was not implemented in the backend's compareOperationFunctions map. This caused a runtime error: compareOperationFunctions[operation] is not a function.

Root Cause

  • Line 101-102: The regex option is defined in the frontend options
  • Lines 265-280: The compareOperationFunctions object did NOT include a regex handler

Solution

Added a regex handler to the compareOperationFunctions map that:

  • Creates a RegExp from the value2 pattern
  • Tests value1 against the pattern using pattern.test()
  • Returns false gracefully if the regex pattern is invalid (try/catch)

Changes

regex: (value1: CommonType, value2: CommonType) => {
    try {
        const pattern = new RegExp(value2 as string)
        return pattern.test((value1 || '').toString())
    } catch {
        return false
    }
}

Testing

  1. Create an Agentflow with a Condition node
  2. Add a condition with Type: String
  3. Select Operation: "Regex"
  4. Set Value 1 to a test string (e.g., "hello123")
  5. Set Value 2 to a regex pattern (e.g., "[0-9]+")
  6. Run the flow
  7. The condition should now evaluate correctly

Related Issue

Fixes #5650

The 'Regex' operation was available in the frontend dropdown but not
implemented in the backend's compareOperationFunctions map, causing
a runtime error when used.

Added regex handler that:
- Creates a RegExp from the value2 pattern
- Tests value1 against the pattern
- Returns false if the regex pattern is invalid (graceful error handling)

Fixes FlowiseAI#5650
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @thecaptain789, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical issue where the 'Regex' operation, despite being available in the frontend for Condition nodes, was not implemented in the backend's compareOperationFunctions map. The change introduces the necessary backend logic to correctly evaluate regular expression conditions, preventing runtime errors and enabling the full functionality of regex-based comparisons within Agentflow Condition nodes.

Highlights

  • Backend Regex Implementation: Implemented the missing 'Regex' operation handler in the compareOperationFunctions map for Condition nodes, resolving a runtime error where the frontend option was not supported in the backend.
  • Robust Pattern Handling: The new regex handler includes a try-catch block to gracefully handle invalid regular expression patterns, ensuring the condition returns false instead of throwing an error.
Changelog
  • packages/components/nodes/agentflow/Condition/Condition.ts
    • Added a regex function to the compareOperationFunctions object. This function creates a RegExp object from value2, tests value1 against it, and includes error handling for invalid regex patterns.
Activity
  • No specific activity (comments, reviews, approvals) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly implements the missing regex operation for the Condition node, resolving a runtime error. The implementation gracefully handles invalid regex patterns by returning false. I've added a suggestion to further improve the robustness of the type handling and to enhance debuggability by logging errors from invalid patterns.

Comment on lines 278 to 285
regex: (value1: CommonType, value2: CommonType) => {
try {
const pattern = new RegExp(value2 as string)
return pattern.test((value1 || '').toString())
} catch {
return false
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This is a good implementation. I have a couple of suggestions to make it even more robust and easier to debug:

  1. Safer Type Handling: value2 is of type CommonType and can be null. new RegExp(null) creates a regex for the literal string "null", which is likely not intended. Converting value2 to a string explicitly (with null or undefined becoming an empty string) is safer and more predictable.
  2. Error Logging: The empty catch block hides errors from invalid regex patterns. Logging the error provides a hook for easier debugging in the future if users report issues with their regex patterns.
Suggested change
regex: (value1: CommonType, value2: CommonType) => {
try {
const pattern = new RegExp(value2 as string)
return pattern.test((value1 || '').toString())
} catch {
return false
}
},
regex: (value1: CommonType, value2: CommonType) => {
try {
const pattern = new RegExp(String(value2 ?? ''))
return pattern.test((value1 || '').toString())
} catch (e) {
// For debugging, consider logging the error.
// console.error(`[Condition Node] Invalid regex pattern: "${value2}"`, e);
return false
}
},

@HenryHengZJ
Copy link
Contributor

we have a similar PR

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.

Bug: "Regex" operation in Condition node is not implemented in backend

2 participants