Interface AgenticScope
- All Superinterfaces:
LangChain4jManaged
- All Known Implementing Classes:
DefaultAgenticScope
The AgenticScope class represents a common environment where agents belonging to the same
agentic system can share their state.
It maintains the state of the computation, tracks agent invocations, and provides
methods to allow agents to interact with the shared state.
Agents can register their calls, and the context of interactions is stored for later retrieval. The class also provides methods to read and write state, manage agent invocations, and retrieve the context as a conversation.
-
Field Summary
Fields inherited from interface LangChain4jManaged
CURRENT -
Method Summary
Modifier and TypeMethodDescriptionReturns all agent invocations recorded in this scope, in execution order.agentInvocations(Class<?> agentType) Returns all agent invocations for agents of the given type.agentInvocations(String agentName) Returns all agent invocations for the agent with the given name.default booleancompletePendingResponse(String responseId, Object value) Completes aPendingResponsestored in this scope's state.contextAsConversation(Object... agents) Returns the conversation context as a human-readable string, optionally filtered by agent instances.contextAsConversation(String... agentNames) Returns the conversation context as a human-readable string, optionally filtered by agent names.booleanChecks whether the shared state contains a non-blank value for the given typed key.booleanChecks whether the shared state contains a non-blank value for the given key.memoryId()Returns the unique memory identifier for this scope.Returns the identifiers of allPendingResponseinstances stored in this scope's state that have not yet been completed.<T> TReads the value associated with the given typed key from the shared state.Reads the value associated with the given key from the shared state.<T> TReads the value associated with the given key from the shared state, returning a default value if the key is not present.state()Returns a live view of the entire shared state map.<T> voidwriteState(Class<? extends TypedKey<T>> key, T value) Writes a value into the shared state using a strongly typed key.voidwriteState(String key, Object value) Writes a value into the shared state under the given key.voidwriteStates(Map<String, Object> newState) Writes multiple key-value pairs into the shared state at once.
-
Method Details
-
memoryId
Object memoryId()Returns the unique memory identifier for this scope. This ID is used to associate the scope with a specific conversation or session, and to look up persisted scopes from a store.- Returns:
- the memory identifier
-
writeState
-
writeState
-
writeStates
-
hasState
Checks whether the shared state contains a non-blank value for the given key.- Parameters:
key- the state key- Returns:
trueif the key exists and its value is non-null (and non-blank for strings)
-
hasState
-
readState
Reads the value associated with the given key from the shared state. If the value is aDelayedResponse, this method blocks until the response is available.- Parameters:
key- the state key- Returns:
- the value, or
nullif the key is not present
-
readState
Reads the value associated with the given key from the shared state, returning a default value if the key is not present. If the value is aDelayedResponse, this method blocks until the response is available.- Type Parameters:
T- the type of the value- Parameters:
key- the state keydefaultValue- the value to return if the key is not present- Returns:
- the value, or
defaultValueif the key is not present
-
readState
Reads the value associated with the given typed key from the shared state. The key's name and default value are derived from theTypedKeyclass. If the value is aDelayedResponse, this method blocks until the response is available.- Type Parameters:
T- the type of the value- Parameters:
key- the typed key class- Returns:
- the value, or the key's default value if not present
-
state
-
contextAsConversation
Returns the conversation context as a human-readable string, optionally filtered by agent names. Each entry shows the user message and the agent's response.- Parameters:
agentNames- the names of the agents to include; if empty, all agents are included- Returns:
- the conversation context as a formatted string
-
contextAsConversation
Returns the conversation context as a human-readable string, optionally filtered by agent instances. Each entry shows the user message and the agent's response.- Parameters:
agents- the agent instances to include; if empty, all agents are included- Returns:
- the conversation context as a formatted string
-
agentInvocations
List<AgentInvocation> agentInvocations()Returns all agent invocations recorded in this scope, in execution order.- Returns:
- an unmodifiable list of all agent invocations
-
agentInvocations
Returns all agent invocations for the agent with the given name.- Parameters:
agentName- the name of the agent- Returns:
- a list of invocations matching the agent name
-
agentInvocations
Returns all agent invocations for agents of the given type.- Parameters:
agentType- the class of the agent- Returns:
- a list of invocations matching the agent type
-
completePendingResponse
Completes aPendingResponsestored in this scope's state. This is typically called by an external system (e.g., a REST endpoint) to provide a human's response after a process restart or when using a polling/event-driven model.- Parameters:
responseId- the unique identifier of the pending responsevalue- the value to complete the response with- Returns:
trueif a matching pending response was found and completed
-
pendingResponseIds
Returns the identifiers of allPendingResponseinstances stored in this scope's state that have not yet been completed.- Returns:
- a set of pending response identifiers
-