Class TurnBuilder


  • public class TurnBuilder
    extends Object
    Sub-builder for configuring a single turn within an LLM conversation mock.

    Provides fluent predicates for matching inbound requests and a response to return when all predicates match. Returns to the parent LlmConversationBuilder via andThen().

    • Method Detail

      • whenTurnIndex

        public TurnBuilder whenTurnIndex​(int n)
        Match when the conversation has exactly n assistant turns.
        Parameters:
        n - the turn index (0-based count of assistant messages)
        Returns:
        this builder
      • whenLatestMessageContains

        public TurnBuilder whenLatestMessageContains​(String text)
        Match when the latest message's text content contains the given substring.
        Parameters:
        text - the substring to match
        Returns:
        this builder
      • whenLatestMessageContains

        public TurnBuilder whenLatestMessageContains​(Pattern regex)
        Match when the latest message's text content matches the given regex.
        Parameters:
        regex - the regex pattern to match
        Returns:
        this builder
        Throws:
        IllegalArgumentException - if the regex fails to compile
      • whenLatestMessageRole

        public TurnBuilder whenLatestMessageRole​(ParsedMessage.Role role)
        Match when the latest message has the given role.
        Parameters:
        role - the role to match
        Returns:
        this builder
      • whenContainsToolResultFor

        public TurnBuilder whenContainsToolResultFor​(String toolName)
        Match when the conversation contains a tool result for a prior call of the named tool.
        Parameters:
        toolName - the tool name to look for
        Returns:
        this builder
      • respondingWith

        public TurnBuilder respondingWith​(Completion completion)
        Set the completion to return for this turn.
        Parameters:
        completion - the completion response
        Returns:
        this builder
      • andThen

        public LlmConversationBuilder andThen()
        Return to the parent conversation builder for chaining additional turns.
        Returns:
        the parent LlmConversationBuilder
      • applyTo

        public Expectation[] applyTo​(MockServerClient client)
        Shortcut: build and register all turns with the MockServerClient. Delegates to the parent builder's applyTo.
        Parameters:
        client - the MockServerClient
        Returns:
        the created expectations