Class TakeLeaseAdvisor

java.lang.Object
ai.badmonkey.agentspaces.springai.worker.TakeLeaseAdvisor
All Implemented Interfaces:
org.springframework.ai.chat.client.advisor.api.Advisor, org.springframework.ai.chat.client.advisor.api.CallAdvisor, org.springframework.ai.chat.client.advisor.api.StreamAdvisor, org.springframework.core.Ordered

public class TakeLeaseAdvisor extends Object implements org.springframework.ai.chat.client.advisor.api.CallAdvisor, org.springframework.ai.chat.client.advisor.api.StreamAdvisor
Renews the in-flight take lease on every model round-trip. Ordered after Spring AI's ToolCallingAdvisor, it runs inside the tool loop, once per round-trip; a streamed round-trip also renews on an interval while it runs. A worker making progress keeps its claim however long its agentic loop takes; a worker that crashes or hangs stops renewing, and its task reappears one lease later. Leases can therefore be short, which makes recovery fast.

The advisor does nothing when no take is in flight, so it is safe on every ChatClient; the auto-configuration adds it to all of them.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Inside the tool loop: after ToolCallingAdvisor.

    Fields inherited from interface org.springframework.ai.chat.client.advisor.api.Advisor

    DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
    TakeLeaseAdvisor(org.springframework.scheduling.TaskScheduler scheduler, Duration streamRenewInterval)
    Creates the advisor over the worker loop's thread-bound take.
    TakeLeaseAdvisor(org.springframework.scheduling.TaskScheduler scheduler, Duration streamRenewInterval, Supplier<Optional<ai.badmonkey.agentspaces.agent.TakeContext>> current)
    Creates the advisor over a supplied take, for tests and custom hosts.
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.ai.chat.client.ChatClientResponse
    adviseCall(org.springframework.ai.chat.client.ChatClientRequest request, org.springframework.ai.chat.client.advisor.api.CallAdvisorChain chain)
     
    reactor.core.publisher.Flux<org.springframework.ai.chat.client.ChatClientResponse>
    adviseStream(org.springframework.ai.chat.client.ChatClientRequest request, org.springframework.ai.chat.client.advisor.api.StreamAdvisorChain chain)
     
     
    int
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ORDER

      public static final int ORDER
      Inside the tool loop: after ToolCallingAdvisor.
      See Also:
  • Constructor Details

    • TakeLeaseAdvisor

      public TakeLeaseAdvisor(org.springframework.scheduling.TaskScheduler scheduler, Duration streamRenewInterval)
      Creates the advisor over the worker loop's thread-bound take.
      Parameters:
      scheduler - the scheduler stream renewals run on
      streamRenewInterval - the renewal interval while a stream runs
    • TakeLeaseAdvisor

      public TakeLeaseAdvisor(org.springframework.scheduling.TaskScheduler scheduler, Duration streamRenewInterval, Supplier<Optional<ai.badmonkey.agentspaces.agent.TakeContext>> current)
      Creates the advisor over a supplied take, for tests and custom hosts.
      Parameters:
      scheduler - the scheduler stream renewals run on
      streamRenewInterval - the renewal interval while a stream runs
      current - supplies the take in flight, if any
  • Method Details

    • getName

      public String getName()
      Specified by:
      getName in interface org.springframework.ai.chat.client.advisor.api.Advisor
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered
    • adviseCall

      public org.springframework.ai.chat.client.ChatClientResponse adviseCall(org.springframework.ai.chat.client.ChatClientRequest request, org.springframework.ai.chat.client.advisor.api.CallAdvisorChain chain)
      Specified by:
      adviseCall in interface org.springframework.ai.chat.client.advisor.api.CallAdvisor
    • adviseStream

      public reactor.core.publisher.Flux<org.springframework.ai.chat.client.ChatClientResponse> adviseStream(org.springframework.ai.chat.client.ChatClientRequest request, org.springframework.ai.chat.client.advisor.api.StreamAdvisorChain chain)
      Specified by:
      adviseStream in interface org.springframework.ai.chat.client.advisor.api.StreamAdvisor