Class CurrentInvocation

java.lang.Object
dev.langchain4j.internal.CurrentInvocation

public final class CurrentInvocation extends Object
Holds the InvocationContext.invocationId() of the AI Service invocation that is doing its own work on the current thread (for example running a tool, a guardrail, retrieval or the chatRequestTransformer), so that an AI Service invoked by that work can record it as its parent invocation.

It is set only around that work, not around code of the caller (the handlers of a TokenStream, the subscriber of a returned publisher, the continuations of a returned future), so an AI Service invoked by the caller's code normally has no parent invocation.

  • Method Details

    • invocationId

      public static UUID invocationId()
    • invocationId

      public static UUID invocationId(ChatRequestOptions options)
      Returns:
      the invocation id that the listeners of a chat model call get: the one in the given options (which can be null) if any, otherwise the current one, otherwise null.
    • runWith

      public static void runWith(InvocationContext invocationContext, Runnable action)
      Runs the given action with the id of the given invocation context as the current one, then restores the previous one. Does nothing special when the invocation context is null.
    • callWith

      public static <T> T callWith(InvocationContext invocationContext, Supplier<T> action)
      Calls the given action with the id of the given invocation context as the current one, then restores the previous one. Does nothing special when the invocation context is null.
    • callWithId

      public static <T> T callWithId(UUID invocationId, Supplier<T> action)
      Calls the given action with the given invocation id as the current one, then restores the previous one. Does nothing special when the invocation id is null.
    • callWithIdOrWithout

      public static <T> T callWithIdOrWithout(UUID invocationId, Supplier<T> action)
      Calls the given action with the given invocation id as the current one, or with no current invocation when the id is null, then restores the previous one. Used where an id captured earlier must apply exactly, also when it is null.
    • runWithIdOrWithout

      public static void runWithIdOrWithout(UUID invocationId, Runnable action)
      Runs the given action like callWithIdOrWithout(UUID, Supplier).
    • runWithout

      public static void runWithout(Runnable action)
      Runs the given action with no current invocation, then restores the previous one. Used to notify listeners: their code is not part of the work of the invocation.
    • callWithout

      public static <T> T callWithout(Supplier<T> action)
      Calls the given action with no current invocation, then restores the previous one. Used when an AI Service is invoked during the work of another invocation: its listeners and callbacks are not part of that work.