Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Implement wrapping of regular nodes as AI Tools #10641

Merged
merged 18 commits into from
Sep 4, 2024

Conversation

OlegIvaniv
Copy link
Contributor

@OlegIvaniv OlegIvaniv commented Sep 3, 2024

Summary

This PR introduces the ability to wrap regular n8n nodes as AI Tools for use with AI agents. Key changes include:

  1. Added usableAsTool property to node definitions to indicate eligibility for tool wrapping.

  2. Implemented createNodeAsTool function:

    • Analyzes node parameters to identify placeholders for dynamic inputs.
    • Generates a Zod schema based on these placeholders for input validation.
    • Creates a DynamicStructuredTool that wraps the node's functionality.
    • Handles nested parameters and maintains the original node's structure.
    • Provides a proxy for getNodeParameter to integrate LLM-provided values.
  3. Modified NodeExecuteFunctions:

    • Enhanced getInputConnectionData to handle tool nodes.
    • If a node doesn't have a supplyData method but is marked as usable as a tool, it creates a wrapper function that calls getNodeAsTool.
    • This allows tool nodes to be executed within the existing n8n framework.
  4. Updated NodeHelpers with convertNodeToAiTool function to modify node descriptions for tool use, including adding a custom description field for LLMs.

Right now, usableAsTool is not set for any of the nodes, we'll enable this in the next phase once we also have placeholder parameters implemented so that LLM could actually pass data to these tools.

Example with usableAsTool: true and HackerNews node:

CleanShot.2024-09-03.at.09.46.53.mp4

Related Linear tickets, Github issues, and Community forum posts

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team ui Enhancement in /editor-ui or /design-system labels Sep 3, 2024
Copy link
Contributor

@burivuhster burivuhster left a comment

Choose a reason for hiding this comment

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

Amazing work! 💪

Not sure it is relevant (feel free to discard), but lodash have some utility methods for working with object properties by path (e.g. get, set, toPath, property). Although lodash path format is slightly different for array elements, maybe it is possible to replace some custom code with methods from lodash.

I left some minor comments and questions.

@OlegIvaniv OlegIvaniv force-pushed the ai-284-implement-node-as-tool-wrapper branch from 134f288 to 576d601 Compare September 3, 2024 14:23
Copy link
Contributor Author

@OlegIvaniv OlegIvaniv left a comment

Choose a reason for hiding this comment

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

@burivuhster Thank you for the review! Could you have one more look, please?

packages/core/test/CreateNodeAsTool.test.ts Show resolved Hide resolved
packages/workflow/src/NodeHelpers.ts Show resolved Hide resolved
packages/workflow/src/NodeHelpers.ts Show resolved Hide resolved
packages/cli/src/node-types.ts Outdated Show resolved Hide resolved
packages/core/src/CreateNodeAsTool.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@burivuhster burivuhster left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my comments!
LGTM 🚀

Copy link

cypress bot commented Sep 4, 2024

n8n    Run #6726

Run Properties:  status check passed Passed #6726  •  git commit 576d6015e7: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 OlegIvaniv 🗃️ e2e/*
Project n8n
Branch Review ai-284-implement-node-as-tool-wrapper
Run status status check passed Passed #6726
Run duration 04m 47s
Commit git commit 576d6015e7: 🌳 🖥️ browsers:node18.12.0-chrome107 🤖 OlegIvaniv 🗃️ e2e/*
Committer Oleg Ivaniv
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 422
View all changes introduced in this branch ↗︎

Copy link
Contributor

github-actions bot commented Sep 4, 2024

✅ All Cypress E2E specs passed

@OlegIvaniv OlegIvaniv merged commit da44fe4 into master Sep 4, 2024
51 of 64 checks passed
@OlegIvaniv OlegIvaniv deleted the ai-284-implement-node-as-tool-wrapper branch September 4, 2024 10:06
MiloradFilipovic added a commit that referenced this pull request Sep 5, 2024
* master:
  refactor(RabbitMQ Trigger Node): Improve type-safety, add tests, and fix issues with manual triggers (#10663)
  feat(editor): Add support for nodes with multiple main inputs in new canvas (no-changelog) (#10659)
  fix(editor): Set minimum zoom to 0 to allow fitting very large workflows in new canvas (no-changelog) (#10666)
  feat(editor): Change selection to be default canvas behaviour (no-changelog) (#10668)
  feat: More hints to nodes  (#10565)
  fix(editor): Fix opening executions tab from a new, unsaved workflow (#10652)
  fix(AI Agent Node): Fix tools agent when using memory and Anthropic models (#10513)
  feat(editor): Make highlighted data pane floating (#10638)
  fix(editor): Fix workflow loading after switching to executions view in new canvas (no-changelog) (#10655)
  refactor(benchmark): Separate cloud env provisioning from running benchmarks (#10657)
  feat(core): Implement wrapping of regular nodes as AI Tools (#10641)
  refactor(editor): Remove Trial logic in personalization modal and port to script setup (#10649)
  fix(core): Declutter webhook insertion errors (#10650)
  feat: Reintroduce collaboration feature (#10602)
  feat(benchmark): Add scenario for expressions with Set node (#10647)
  feat(benchmark): Add benchmark scenario for binary files (#10648)
  build: Add `reset` script (#10627)
  feat(editor): Overhaul node insert position computation in new canvas (no-changelog) (#10637)
@github-actions github-actions bot mentioned this pull request Sep 5, 2024
@janober
Copy link
Member

janober commented Sep 5, 2024

Got released with n8n@1.58.0

riascho pushed a commit that referenced this pull request Sep 23, 2024
* as an AI Agent Tool.
* Returns the modified item (not copied)
*/
export function convertNodeToAiTool<
Copy link
Member

Choose a reason for hiding this comment

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

why did we decide to put this here? can we instead move this to core?
We have way much code in the workflow package. So unless some code actually needs to be shared between backend, frontend, and nodes, we should avoid adding it to this package 🙏🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team Released ui Enhancement in /editor-ui or /design-system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants