Package com.tnsai.thread
Class AgentThread
java.lang.Object
com.tnsai.thread.AgentThread
Represents a single execution thread for an agent.
An AgentThread provides:
- Session-scoped conversation state
- Resource quota tracking and enforcement
- Checkpoint/recovery capabilities
- Parent-child thread hierarchies
- Lifecycle management (pause, resume, cancel)
Usage Example
// Create a thread
AgentThread thread = AgentThread.create(ThreadSpec.defaults());
// Execute with an agent
String response = thread.execute(agent, "Hello!");
// Check usage
System.out.println("Tokens used: " + thread.getUsage().getTotalTokens());
// Pause for later
thread.pause();
// Resume
thread.resume();
String response2 = thread.execute(agent, "Continue...");
// Create child thread for sub-task
AgentThread child = thread.createChild(ThreadSpec.shortLived());
child.execute(subAgent, "Sub-task");
- Since:
- 0.0.2
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(Consumer<ThreadEvent> listener) Add an event listener.voidcancel()Cancel the thread.voidCreate a checkpoint of current state.voidcomplete()Mark as completed.static AgentThreadcreate(ThreadSpec config) Create a new root thread with generated ID.static AgentThreadcreate(ThreadSpec config, com.tnsai.state.Checkpointer checkpointer) Create a new root thread with a checkpointer.createChild(ThreadSpec childSpec) Create a child thread.Execute a chat message within this thread context.<T> TExecute with structured output.Get active (non-terminal) children.Get all child threads.intgetSpec()getState()Get thread-local state.<T> TGet a state value.<T> TGet a state value with default.getUsage()booleanisRoot()voidpause()Pause the thread.voidremoveListener(Consumer<ThreadEvent> listener) Remove an event listener.voidremoveState(String key) Remove a state value.voidrestore()Restore from latest checkpoint.voidresume()Resume a paused thread.voidSet a state value.toString()static AgentThreadwithId(String threadId, ThreadSpec config, com.tnsai.state.Checkpointer checkpointer) Create a thread with a specific ID (for recovery).
-
Method Details
-
create
Create a new root thread with generated ID. -
create
Create a new root thread with a checkpointer. -
withId
public static AgentThread withId(String threadId, ThreadSpec config, com.tnsai.state.Checkpointer checkpointer) Create a thread with a specific ID (for recovery). -
execute
Execute a chat message within this thread context.- Parameters:
agent- The agent to execute withmessage- The user message- Returns:
- The agent's response
- Throws:
ThreadException- if execution fails or quota exceeded
-
execute
Execute with structured output. Executes the agent and attempts to parse the response as the specified type.- Parameters:
agent- the agent to executemessage- the message to sendresponseType- the expected response type (must be String.class or a JSON-serializable class)- Returns:
- the response parsed as the specified type
- Throws:
ThreadException- if parsing fails
-
pause
public void pause()Pause the thread. State is preserved for later resumption. -
resume
public void resume()Resume a paused thread. -
cancel
public void cancel()Cancel the thread. -
complete
public void complete()Mark as completed. -
checkpoint
public void checkpoint()Create a checkpoint of current state. -
restore
public void restore()Restore from latest checkpoint. -
createChild
Create a child thread. -
getChildren
Get all child threads. -
getActiveChildren
Get active (non-terminal) children. -
getState
Get thread-local state. -
getState
Get a state value. -
getState
Get a state value with default. -
setState
Set a state value. -
removeState
Remove a state value. -
addListener
Add an event listener. -
removeListener
Remove an event listener. -
getThreadId
-
getSpec
-
getUsage
-
getThreadState
-
getCreatedAt
-
getParent
-
getLastError
-
getRetryCount
public int getRetryCount() -
isRoot
public boolean isRoot() -
toString
-