Class InvocationEnd
java.lang.Object
dev.langchain4j.invocation.InvocationEnd
Makes sure that an AI Service invocation reports its end only once, with exactly one of
AiServiceCompletedEvent,
AiServiceErrorEvent or
AiServiceCancelledEvent, also when completion, failure and
cancellation race.
AI Services call it before firing one of these events. An integration that implements AI Services itself and fires these events on its own should do the same:
if (InvocationEnd.markEnded(invocationContext)) {
listenerRegistrar.fireEvent(AiServiceCompletedEvent.builder()
.invocationContext(invocationContext)
.result(result)
.build());
}
The state is kept in the DefaultInvocationContext and is shared by all its copies made with
InvocationContext.toBuilder() that keep the same InvocationContext.invocationId().- Since:
- 1.23.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanmarkEnded(InvocationContext invocationContext) Marks the given invocation as ended.
-
Method Details
-
markEnded
Marks the given invocation as ended.- Parameters:
invocationContext- the context of the invocation- Returns:
truethe first time it is called for the given invocation, so the caller fires the end event;falseafterwards, so the caller fires nothing. Alwaystruefor an invocation without id or with anInvocationContextthat is not aDefaultInvocationContext.
-