Package com.tnsai.context.integration
Class AgentContextManager
java.lang.Object
com.tnsai.context.integration.AgentContextManager
- All Implemented Interfaces:
com.tnsai.context.ContextManagerHandle
Manages context graphs for an agent.
AgentContextManager provides the integration layer between TnsAI agents and the Context Graphs feature, enabling:
- Automatic context snapshotting before/after decisions
- Decision tracing with full provenance
- Explaining past decisions
- Finding precedent decisions
This class is a facade that delegates to three focused sub-managers:
SnapshotManager— context snapshot creation, queries, diffsDecisionTracer— decision tracing with BDI mapping, queriesConversationManager— conversation lifecycle, entity tracking, variables
Usage Example
// Enable context graph on agent
AgentContextManager contextManager = new AgentContextManager(
agent.getAgentId(),
new InMemorySnapshotStore(),
new InMemoryDecisionTraceStore()
);
// Execute with tracing
String response = contextManager.executeWithTrace(
"Process discount request",
() -> agent.chat("Should we approve 20% discount?"),
agent::getBeliefs,
agent::getDesires,
agent::getIntentions
);
// Explain recent decisions
List<DecisionTrace> traces = contextManager.getRecentDecisions(10);
for (DecisionTrace trace : traces) {
System.out.println(trace.explainWhy());
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tnsai.context.ContextManagerHandle
com.tnsai.context.ContextManagerHandle.Factory -
Constructor Summary
ConstructorsConstructorDescriptionAgentContextManager(String agentId, com.tnsai.context.snapshot.SnapshotStore snapshotStore, com.tnsai.context.decision.DecisionTraceStore traceStore) Creates an agent context manager.AgentContextManager(String agentId, com.tnsai.context.snapshot.SnapshotStore snapshotStore, com.tnsai.context.decision.DecisionTraceStore traceStore, BDIContextMapper bdiMapper) Creates an agent context manager with custom BDI mapper. -
Method Summary
Modifier and TypeMethodDescriptionintcleanupOldSnapshots(Instant olderThan) intcleanupOldTraces(Instant olderThan) voidvoidcom.tnsai.context.snapshot.ContextSnapshotcreateSnapshot(com.tnsai.context.snapshot.SnapshotTrigger trigger) com.tnsai.context.snapshot.ContextSnapshotcreateSnapshot(com.tnsai.context.snapshot.SnapshotTrigger trigger, List<com.tnsai.models.agent.Belief> beliefs, List<com.tnsai.models.agent.Desire> desires, List<com.tnsai.models.agent.Intention> intentions) com.tnsai.context.decision.DecisionTracecreateTrace(com.tnsai.context.decision.DecisionTrace.Builder builder) Optional<com.tnsai.context.snapshot.SnapshotDiff> diffSnapshots(String beforeId, String afterId) Computes diff between two snapshots.void<T> TexecuteWithTrace(String description, Supplier<T> operation) <T> TexecuteWithTrace(String description, Supplier<T> operation, Supplier<List<com.tnsai.models.agent.Belief>> beliefs, Supplier<List<com.tnsai.models.agent.Desire>> desires, Supplier<List<com.tnsai.models.agent.Intention>> intentions) explainDecision(String traceId) List<com.tnsai.context.decision.DecisionTrace> findDecisionsByAction(String action) List<com.tnsai.context.decision.DecisionTrace> List<com.tnsai.context.decision.DecisionTrace> List<com.tnsai.context.decision.DecisionTrace> findPrecedents(com.tnsai.context.decision.DecisionTrace referenceTrace, double threshold) Optional<com.tnsai.context.snapshot.ContextSnapshot> findSnapshot(String snapshotId) List<com.tnsai.context.decision.DecisionTrace> Optional<com.tnsai.context.decision.DecisionTrace> List<com.tnsai.context.decision.DecisionTrace> Gets the BDI mapper.intList<com.tnsai.context.decision.DecisionTrace> getRecentDecisions(int limit) List<com.tnsai.context.snapshot.ContextSnapshot> getRecentSnapshots(int limit) intcom.tnsai.context.snapshot.SnapshotStore.SnapshotStoreStatisticscom.tnsai.context.snapshot.SnapshotStoreGets the snapshot store.com.tnsai.context.decision.DecisionTraceStore.DecisionTraceStatisticscom.tnsai.context.decision.DecisionTraceStoreGets the trace store.Collection<com.tnsai.context.entity.Entity> getVariable(String key) <T> Optional<T> getVariable(String key, Class<T> type) voidremoveVariable(String key) voidsetVariable(String key, Object value) voidstartConversation(String conversationId) summarizeRecentDecisions(int limit) voidtrackEntity(com.tnsai.context.entity.Entity entity) voiduntrackEntity(String entityId)
-
Constructor Details
-
AgentContextManager
public AgentContextManager(String agentId, com.tnsai.context.snapshot.SnapshotStore snapshotStore, com.tnsai.context.decision.DecisionTraceStore traceStore) Creates an agent context manager.- Parameters:
agentId- the agent's IDsnapshotStore- store for context snapshotstraceStore- store for decision traces
-
AgentContextManager
public AgentContextManager(String agentId, com.tnsai.context.snapshot.SnapshotStore snapshotStore, com.tnsai.context.decision.DecisionTraceStore traceStore, BDIContextMapper bdiMapper) Creates an agent context manager with custom BDI mapper.- Parameters:
agentId- the agent's IDsnapshotStore- store for context snapshotstraceStore- store for decision tracesbdiMapper- custom BDI mapper
-
-
Method Details
-
createSnapshot
public com.tnsai.context.snapshot.ContextSnapshot createSnapshot(com.tnsai.context.snapshot.SnapshotTrigger trigger, List<com.tnsai.models.agent.Belief> beliefs, List<com.tnsai.models.agent.Desire> desires, List<com.tnsai.models.agent.Intention> intentions) - Specified by:
createSnapshotin interfacecom.tnsai.context.ContextManagerHandle
-
createSnapshot
public com.tnsai.context.snapshot.ContextSnapshot createSnapshot(com.tnsai.context.snapshot.SnapshotTrigger trigger) - Specified by:
createSnapshotin interfacecom.tnsai.context.ContextManagerHandle
-
executeWithTrace
public <T> T executeWithTrace(String description, Supplier<T> operation, Supplier<List<com.tnsai.models.agent.Belief>> beliefs, Supplier<List<com.tnsai.models.agent.Desire>> desires, Supplier<List<com.tnsai.models.agent.Intention>> intentions) - Specified by:
executeWithTracein interfacecom.tnsai.context.ContextManagerHandle
-
executeWithTrace
- Specified by:
executeWithTracein interfacecom.tnsai.context.ContextManagerHandle
-
createTrace
public com.tnsai.context.decision.DecisionTrace createTrace(com.tnsai.context.decision.DecisionTrace.Builder builder) - Specified by:
createTracein interfacecom.tnsai.context.ContextManagerHandle
-
findTrace
- Specified by:
findTracein interfacecom.tnsai.context.ContextManagerHandle
-
getRecentDecisions
- Specified by:
getRecentDecisionsin interfacecom.tnsai.context.ContextManagerHandle
-
getAllDecisions
- Specified by:
getAllDecisionsin interfacecom.tnsai.context.ContextManagerHandle
-
findDecisionsByAction
- Specified by:
findDecisionsByActionin interfacecom.tnsai.context.ContextManagerHandle
-
findSuccessfulDecisions
- Specified by:
findSuccessfulDecisionsin interfacecom.tnsai.context.ContextManagerHandle
-
findFailedDecisions
- Specified by:
findFailedDecisionsin interfacecom.tnsai.context.ContextManagerHandle
-
findDecisionsInCurrentConversation
- Specified by:
findDecisionsInCurrentConversationin interfacecom.tnsai.context.ContextManagerHandle
-
findPrecedents
public List<com.tnsai.context.decision.DecisionTrace> findPrecedents(com.tnsai.context.decision.DecisionTrace referenceTrace, double threshold) - Specified by:
findPrecedentsin interfacecom.tnsai.context.ContextManagerHandle
-
explainDecision
- Specified by:
explainDecisionin interfacecom.tnsai.context.ContextManagerHandle
-
summarizeRecentDecisions
- Specified by:
summarizeRecentDecisionsin interfacecom.tnsai.context.ContextManagerHandle
-
findSnapshot
- Specified by:
findSnapshotin interfacecom.tnsai.context.ContextManagerHandle
-
getRecentSnapshots
- Specified by:
getRecentSnapshotsin interfacecom.tnsai.context.ContextManagerHandle
-
diffSnapshots
public Optional<com.tnsai.context.snapshot.SnapshotDiff> diffSnapshots(String beforeId, String afterId) Computes diff between two snapshots.- Parameters:
beforeId- first snapshot IDafterId- second snapshot ID- Returns:
- Optional containing the diff
-
trackEntity
public void trackEntity(com.tnsai.context.entity.Entity entity) - Specified by:
trackEntityin interfacecom.tnsai.context.ContextManagerHandle
-
untrackEntity
- Specified by:
untrackEntityin interfacecom.tnsai.context.ContextManagerHandle
-
getTrackedEntities
- Specified by:
getTrackedEntitiesin interfacecom.tnsai.context.ContextManagerHandle
-
clearTrackedEntities
public void clearTrackedEntities()- Specified by:
clearTrackedEntitiesin interfacecom.tnsai.context.ContextManagerHandle
-
setVariable
- Specified by:
setVariablein interfacecom.tnsai.context.ContextManagerHandle
-
getVariable
- Specified by:
getVariablein interfacecom.tnsai.context.ContextManagerHandle
-
getVariable
- Specified by:
getVariablein interfacecom.tnsai.context.ContextManagerHandle
-
removeVariable
- Specified by:
removeVariablein interfacecom.tnsai.context.ContextManagerHandle
-
clearVariables
public void clearVariables()- Specified by:
clearVariablesin interfacecom.tnsai.context.ContextManagerHandle
-
startConversation
- Specified by:
startConversationin interfacecom.tnsai.context.ContextManagerHandle
-
ensureConversation
- Specified by:
ensureConversationin interfacecom.tnsai.context.ContextManagerHandle
-
startConversation
- Specified by:
startConversationin interfacecom.tnsai.context.ContextManagerHandle
-
endConversation
public void endConversation()- Specified by:
endConversationin interfacecom.tnsai.context.ContextManagerHandle
-
getCurrentConversationId
- Specified by:
getCurrentConversationIdin interfacecom.tnsai.context.ContextManagerHandle
-
getTraceStatistics
public com.tnsai.context.decision.DecisionTraceStore.DecisionTraceStatistics getTraceStatistics()- Specified by:
getTraceStatisticsin interfacecom.tnsai.context.ContextManagerHandle
-
getSnapshotStatistics
public com.tnsai.context.snapshot.SnapshotStore.SnapshotStoreStatistics getSnapshotStatistics()- Specified by:
getSnapshotStatisticsin interfacecom.tnsai.context.ContextManagerHandle
-
getDecisionCount
public int getDecisionCount()- Specified by:
getDecisionCountin interfacecom.tnsai.context.ContextManagerHandle
-
getSnapshotCount
public int getSnapshotCount()- Specified by:
getSnapshotCountin interfacecom.tnsai.context.ContextManagerHandle
-
cleanupOldTraces
- Specified by:
cleanupOldTracesin interfacecom.tnsai.context.ContextManagerHandle
-
cleanupOldSnapshots
- Specified by:
cleanupOldSnapshotsin interfacecom.tnsai.context.ContextManagerHandle
-
getAgentId
- Specified by:
getAgentIdin interfacecom.tnsai.context.ContextManagerHandle
-
getBdiMapper
Gets the BDI mapper.- Returns:
- BDI context mapper
-
getSnapshotStore
public com.tnsai.context.snapshot.SnapshotStore getSnapshotStore()Gets the snapshot store.- Returns:
- snapshot store
-
getTraceStore
public com.tnsai.context.decision.DecisionTraceStore getTraceStore()Gets the trace store.- Returns:
- decision trace store
-