Package com.tnsai.agents.interrupt
Interface InterruptibleAgent
public interface InterruptibleAgent
Interface for agents that support graceful interruption and state preservation.
Implements the AgentScope-inspired interruptability pattern, allowing:
- Graceful stopping of running agents
- State checkpointing for later resume
- Event-based interrupt handling
- Pause/resume functionality
Usage
// Setup interrupt handler
agent.onInterrupt(event -> {
if (event instanceof InterruptEvent.UserInterrupt) {
agent.checkpoint();
agent.respond("Paused. What would you like to change?");
}
});
// Start async execution
agent.chatAsync("Complex 10-step task...")
.thenAccept(result -> System.out.println(result));
// Later, interrupt if needed
agent.interrupt("User changed direction");
// Resume from checkpoint
agent.restore(previousCheckpoint);
agent.chat("New direction: focus on security");
-
Method Summary
Modifier and TypeMethodDescriptionCreates a checkpoint of the current state.checkpoint(InterruptEvent trigger) Creates a checkpoint with a specific trigger event.voidClears the interrupted state.Gets the current interrupt event, if any.Gets the most recent checkpoint, if any.voidInterrupts the current execution gracefully.voidinterrupt(InterruptEvent event) Interrupts with a specific event.voidInterrupts with a specific reason.booleanChecks if the agent is currently interrupted.booleanisPaused()Checks if the agent is currently paused.voidonInterrupt(Consumer<InterruptEvent> handler) Registers a callback for interrupt events.voidRegisters a callback for pause events.voidRegisters a callback for resume events.voidpause()Pauses execution (can be resumed without checkpoint restore).voidrestore(AgentCheckpoint checkpoint) Restores agent state from a checkpoint.voidresume()Resumes execution after a pause.
-
Method Details
-
interrupt
void interrupt()Interrupts the current execution gracefully.The agent will complete any critical operations (like tool calls) before stopping and optionally creating a checkpoint.
-
interrupt
Interrupts with a specific reason.- Parameters:
reason- Human-readable reason for the interrupt
-
interrupt
Interrupts with a specific event.- Parameters:
event- The interrupt event
-
isInterrupted
boolean isInterrupted()Checks if the agent is currently interrupted. -
pause
void pause()Pauses execution (can be resumed without checkpoint restore). -
resume
void resume()Resumes execution after a pause. -
isPaused
boolean isPaused()Checks if the agent is currently paused. -
checkpoint
AgentCheckpoint checkpoint()Creates a checkpoint of the current state.- Returns:
- The checkpoint containing full agent state
-
checkpoint
Creates a checkpoint with a specific trigger event.- Parameters:
trigger- The event that triggered the checkpoint- Returns:
- The checkpoint
-
restore
Restores agent state from a checkpoint.- Parameters:
checkpoint- The checkpoint to restore
-
getLastCheckpoint
Optional<AgentCheckpoint> getLastCheckpoint()Gets the most recent checkpoint, if any. -
onInterrupt
Registers a callback for interrupt events.- Parameters:
handler- The handler to invoke on interrupt
-
onPause
Registers a callback for pause events.- Parameters:
handler- The handler to invoke on pause
-
onResume
Registers a callback for resume events.- Parameters:
handler- The handler to invoke on resume
-
clearInterrupt
void clearInterrupt()Clears the interrupted state. -
getCurrentInterrupt
Optional<InterruptEvent> getCurrentInterrupt()Gets the current interrupt event, if any.
-