Add tool-confirmation module: Spring AI 2.1 human-in-the-loop tool calls (requiresConfirmation, ToolCallConfirmationPolicy, ToolCallDecisions pause and resume), a server-side approval store with approver check, replay guard and audit log, and an approver page over SSE and htmx; 17 transcripts. The model is a scripted ChatModel; the htmx page was not opened in a browser. Spring Boot 4.1.3, Spring AI 2.1.0, Java 25.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5x1ufsjyvu9RJFUUb
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.ankurm.toolconfirmation;
|
||||
|
||||
import com.ankurm.toolconfirmation.support.Describe;
|
||||
import com.ankurm.toolconfirmation.support.Env;
|
||||
import com.ankurm.toolconfirmation.support.ScriptedChatModel;
|
||||
import com.ankurm.toolconfirmation.support.Transcript;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
import org.springframework.ai.chat.model.ChatResponse;
|
||||
import org.springframework.ai.model.tool.ToolCallDecision;
|
||||
import org.springframework.ai.model.tool.ToolCallDecisions;
|
||||
import org.springframework.ai.model.tool.ToolExecutionResult;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/** What the model is told when a person says no, with and without a message. */
|
||||
class DenyTest {
|
||||
|
||||
@Test
|
||||
void aDenialIsAToolResultTheModelCanReadAndReactTo() {
|
||||
Env env = new Env();
|
||||
env.model.callTools(ScriptedChatModel.call("call_1", "bookFlight", "{\"flightNumber\":\"VX205\"}"),
|
||||
ScriptedChatModel.call("call_2", "cancelBooking", "{\"reference\":\"BK-9\"}"))
|
||||
.respond("Understood, I will ask which seat you prefer.");
|
||||
|
||||
ChatResponse reply = env.ask("Rebook me on VX205");
|
||||
|
||||
try (Transcript t = new Transcript("03-deny-messages.txt", "What the model receives when a call is denied")) {
|
||||
ToolCallDecisions decisions = ToolCallDecisions.of(
|
||||
ToolCallDecision.deny("call_1"),
|
||||
ToolCallDecision.deny("call_2", "The user wants an aisle seat, ask again."));
|
||||
ToolExecutionResult result = env.resume(Env.user("Rebook me on VX205"), reply, decisions);
|
||||
Message last = result.conversationHistory().get(result.conversationHistory().size() - 1);
|
||||
|
||||
t.line("DEFAULT_DENY_MESSAGE: %s", ToolCallDecision.DEFAULT_DENY_MESSAGE);
|
||||
t.blank();
|
||||
t.line("history the model is sent next:");
|
||||
t.line("%s", Describe.messages(result.conversationHistory()));
|
||||
t.blank();
|
||||
t.line("ledger (nothing ran): %s", env.ledger.entries());
|
||||
t.line("returnDirect(): %s", result.returnDirect());
|
||||
|
||||
String answer = env.client.prompt().messages(result.conversationHistory()).call().content();
|
||||
t.line("model's next answer: %s", answer);
|
||||
|
||||
assertThat(env.ledger.entries()).isEmpty();
|
||||
assertThat(last.toString()).contains(ToolCallDecision.DEFAULT_DENY_MESSAGE)
|
||||
.contains("The user wants an aisle seat, ask again.");
|
||||
assertThat(result.returnDirect()).isFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user