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

refactor(nodes): invocation registration logic #7826

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

psychedelicious
Copy link
Collaborator

Summary

  • Add separate InvocationRegistry class to handle invocation registration. Previously it was all in BaseInvocation and BaseInvocationOutput. This was a bit awkward as logic for execution was mixed up with registration. The actual logic is the same, but the responsibilities are split up in a more logical way.
  • Simplify the invocation typeadapter caching to use @lru_cache(maxsize=1) instead of what effectively was a bespoke version of the same logic.
  • Update all tests and consumers to use the new methods.
  • Improved type annotations for registration logic.

Related Issues / Discussions

n/a

QA Instructions

App should still work. Custom nodes should still work. Works for me. No issues expected as its pretty straightforward.

Merge Plan

n/a

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • Documentation added / updated (if applicable)
  • Updated What's New copy (if doing a release after this PR)

Sorry, something went wrong.

@github-actions github-actions bot added python PRs that change python files invocations PRs that change invocations services PRs that change app services python-tests PRs that change python tests labels Mar 23, 2025
Copy link
Collaborator

@jazzhaiku jazzhaiku left a comment

Choose a reason for hiding this comment

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

Nice change! The code is clearer with these 2 classes separated.
I've left some suggestions for improvement.

@@ -340,6 +249,105 @@ def invoke_internal(self, context: InvocationContext, services: "InvocationServi
TBaseInvocation = TypeVar("TBaseInvocation", bound=BaseInvocation)


class InvocationRegistry:
Copy link
Collaborator

Choose a reason for hiding this comment

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

@psychedelicious
I’d recommend using regular instance methods instead of classmethods.
This allows clients to create and manage their own instances, or you can use the singleton pattern if a global instance is needed.

Same convenience, but more flexible and easier to test.

@classmethod
@lru_cache(maxsize=1)
def get_invocation_typeadapter(cls) -> TypeAdapter[Any]:
"""Gets a pydantic TypeAdapter for the union of all invocation types.
Copy link
Collaborator

Choose a reason for hiding this comment

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

@psychedelicious

If the invocation allowlist or denylist is changed, the cache should be cleared

If the allowlist/denylist are passed in as arguments, then the lru_cache will handle this clearing automatically, reducing the burden on clients and avoiding bugs if they forget.

@psychedelicious
Copy link
Collaborator Author

I've left some suggestions for improvement.

Thanks! There's no urgency to this PR - I just remembered I've had this branch in my local clone for a couple weeks and figured I should push it. I'll review the suggestions when I have spare time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invocations PRs that change invocations python PRs that change python files python-tests PRs that change python tests services PRs that change app services
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants