Package com.tnsai.session
Class AgentSession
java.lang.Object
com.tnsai.session.AgentSession
Represents a complete agent session with all conversation turns.
An AgentSession captures the full history of an agent's interaction, including user messages, assistant responses, tool calls, and outcomes.
Usage
AgentSession session = AgentSession.start("my-agent", "/path/to/project");
session.addTurn(ConversationTurn.user("Fix the bug"));
session.addTurn(ConversationTurn.assistant("I'll analyze the code..."));
session.complete(SessionOutcome.SUCCESS);
Thread safety: turns are held in a CopyOnWriteArrayList, so
addTurn(ConversationTurn) and the read methods
(getTurns(), getTurnsByType(TurnType),
getLastTurn(), getFirstUserMessage()) are safe to call
concurrently. Metadata and tags follow the same concurrent-access intent
but individual compound operations (e.g. add-tag-if-absent) are not atomic.
- Author:
- TnsAI Team
-
Method Summary
Modifier and TypeMethodDescriptionaddAssistantMessage(String content) Adds an assistant message turn.addMetadata(String key, Object value) Adds metadata to the session.Adds a tag to the session.Adds multiple tags.addToolCall(String toolName, Map<String, Object> arguments) Adds a tool call turn.addToolResult(String toolName, String result) Adds a tool result turn.addTurn(ConversationTurn turn) Adds a turn to the session.addUserMessage(String content) Adds a user message turn.voidcancel()Marks the session as cancelled.voidcomplete(SessionOutcome outcome) Marks the session as complete.voidMarks the session as failed.Gets the session duration.Gets the first user message.getId()Gets the last turn.getTags()intGets the number of turns.getTurns()getTurnsByType(TurnType type) Gets turns of a specific type.booleanChecks if session is complete.booleanChecks if session was successful.static AgentSessionrestore(String id, String agentName, Instant startTime, Instant endTime, String projectPath, List<ConversationTurn> turns, Map<String, Object> metadata, List<String> tags, SessionOutcome outcome) Restores a session from saved data.static AgentSessionStarts a new session.static AgentSessionStarts a new session with custom ID.voidsuccess()Marks the session as successful.toString()
-
Method Details
-
start
Starts a new session.- Parameters:
agentName- the agent nameprojectPath- the project path- Returns:
- new session
-
start
Starts a new session with custom ID.- Parameters:
id- the session IDagentName- the agent nameprojectPath- the project path- Returns:
- new session
-
restore
public static AgentSession restore(String id, String agentName, Instant startTime, Instant endTime, String projectPath, List<ConversationTurn> turns, Map<String, Object> metadata, List<String> tags, SessionOutcome outcome) Restores a session from saved data. The persistedstartTimeis honored when non-null; otherwise the current time is used. -
addTurn
Adds a turn to the session.- Parameters:
turn- the conversation turn- Returns:
- this session for chaining
-
addUserMessage
Adds a user message turn.- Parameters:
content- the message content- Returns:
- this session
-
addAssistantMessage
Adds an assistant message turn.- Parameters:
content- the message content- Returns:
- this session
-
addToolCall
Adds a tool call turn.- Parameters:
toolName- the tool namearguments- the tool arguments- Returns:
- this session
-
addToolResult
Adds a tool result turn.- Parameters:
toolName- the tool nameresult- the tool result- Returns:
- this session
-
addMetadata
Adds metadata to the session.- Parameters:
key- the metadata keyvalue- the metadata value- Returns:
- this session
-
addTag
Adds a tag to the session.- Parameters:
tag- the tag- Returns:
- this session
-
addTags
Adds multiple tags.- Parameters:
newTags- the tags to add- Returns:
- this session
-
complete
Marks the session as complete.- Parameters:
outcome- the session outcome
-
success
public void success()Marks the session as successful. -
fail
Marks the session as failed.- Parameters:
error- the error message
-
cancel
public void cancel()Marks the session as cancelled. -
getId
-
getAgentName
-
getStartTime
-
getEndTime
-
getProjectPath
-
getTurns
-
getMetadata
-
getTags
-
getOutcome
-
getDuration
Gets the session duration.- Returns:
- duration or null if not ended
-
getTurnCount
public int getTurnCount()Gets the number of turns.- Returns:
- turn count
-
getTurnsByType
Gets turns of a specific type.- Parameters:
type- the turn type- Returns:
- list of matching turns
-
getLastTurn
Gets the last turn.- Returns:
- last turn or null
-
getFirstUserMessage
Gets the first user message.- Returns:
- first user message or null
-
isComplete
public boolean isComplete()Checks if session is complete.- Returns:
- true if ended
-
isSuccess
public boolean isSuccess()Checks if session was successful.- Returns:
- true if success
-
toString
-