Class LlmConversationBuilder


  • public class LlmConversationBuilder
    extends Object
    Builder for multi-turn LLM conversation mocks with optional per-session isolation.

    Each turn() block produces one Expectation with scenario-based state advancement. On applyTo(MockServerClient), all turn expectations are registered as a group sharing a single auto-generated scenario name.

    Example:

     conversation()
         .withPath("/v1/messages")
         .withProvider(Provider.ANTHROPIC)
         .isolateBy(IsolationSource.header("x-session-id"))
         .turn()
             .whenTurnIndex(0)
             .respondingWith(completion().withToolCall(toolUse("search").withArguments("{}")))
         .andThen()
         .turn()
             .whenContainsToolResultFor("search")
             .respondingWith(completion().withText("The answer is 42."))
         .applyTo(mockServerClient);
     
    • Method Detail

      • conversation

        public static LlmConversationBuilder conversation()
        Entry point for building a multi-turn LLM conversation mock.
        Returns:
        a new LlmConversationBuilder
      • isolateBy

        public LlmConversationBuilder isolateBy​(IsolationSource source)
        Set isolation source for per-session state. When set, the isolation key is extracted from each inbound request (header, query parameter, or cookie) and used to maintain independent scenario state per value.
        Parameters:
        source - the isolation source descriptor
        Returns:
        this builder
      • turn

        public TurnBuilder turn()
        Start defining a new turn in the conversation.
        Returns:
        a TurnBuilder for configuring this turn's match predicates and response
      • applyTo

        public Expectation[] applyTo​(MockServerClient client)
        Build all turn expectations and register them with the MockServerClient.
        Parameters:
        client - the MockServerClient to register with
        Returns:
        the created expectations
      • build

        public Expectation[] build()
        Build all turn expectations without registering them.
        Returns:
        the array of expectations, one per turn