Class PendingResponse<T>
java.lang.Object
dev.langchain4j.agentic.internal.PendingResponse<T>
- Type Parameters:
T- the type of the response value
- All Implemented Interfaces:
DelayedResponse<T>
A
DelayedResponse that can be completed externally, without spawning a background thread.
Unlike AsyncResponse, which immediately starts executing a supplier on a thread pool,
PendingResponse creates an initially incomplete future that must be explicitly completed
via complete(Object). This makes it suitable for scenarios where the response comes from
an external source (e.g., a human via a REST API, a message queue, or an external event) and the
workflow must survive process restarts.
After serialization/deserialization, a new incomplete CompletableFuture is created,
allowing an external system to reconnect and complete the response.
Usage with HumanInTheLoop:
HumanInTheLoop.builder()
.responseProvider(scope -> new PendingResponse<>("user-approval"))
.build();
// Later, from an external system (e.g., REST endpoint):
scope.completePendingResponse("user-approval", "approved");
-
Constructor Summary
ConstructorsConstructorDescriptionPendingResponse(String responseId) Creates a new pending response with the given unique identifier. -
Method Summary
Modifier and TypeMethodDescriptionblockingGet(long timeout, TimeUnit unit) Waits for the response with a timeout.booleanCompletes this pending response with the given value.booleancompleteExceptionally(Throwable exception) Completes this pending response exceptionally.booleanisDone()Returns the unique identifier for this pending response.toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface DelayedResponse
result
-
Constructor Details
-
PendingResponse
Creates a new pending response with the given unique identifier.- Parameters:
responseId- a unique identifier for this pending response, used to locate and complete it from external systems
-
-
Method Details
-
responseId
Returns the unique identifier for this pending response.- Returns:
- the response identifier
-
isDone
public boolean isDone()- Specified by:
isDonein interfaceDelayedResponse<T>
-
blockingGet
- Specified by:
blockingGetin interfaceDelayedResponse<T>
-
blockingGet
Waits for the response with a timeout.- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the response value
- Throws:
TimeoutException- if the wait timed out
-
complete
Completes this pending response with the given value. Any threads blocked onblockingGet()will be released.- Parameters:
value- the response value- Returns:
trueif this invocation caused the response to transition to a completed state,falseif it was already completed
-
completeExceptionally
Completes this pending response exceptionally.- Parameters:
exception- the exception- Returns:
trueif this invocation caused the response to transition to a completed state
-
toString
-