Add koog module: a tool-calling agent, a strategy graph with a Java node and the Spring Boot starter on Koog 1.3.0, tested against a scripted OpenAI-compatible endpoint

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6FHj1ptP5Fej2eFDo
This commit is contained in:
Claude
2026-11-11 11:28:34 +05:30
parent 58eaf4c2e1
commit a620d91d0a
28 changed files with 867 additions and 0 deletions
@@ -0,0 +1,110 @@
package com.ankurm.koog;
import ai.koog.agents.core.agent.AIAgent;
import ai.koog.agents.core.agent.exception.AIAgentMaxNumberOfIterationsReachedException;
import ai.koog.agents.core.tools.ToolRegistry;
import ai.koog.prompt.executor.clients.openai.OpenAIModels;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** What the agent does when the model, or a tool, misbehaves. Every case is a scripted model reply. */
class FailureModesTest {
private static JsonNode lastMessages(ScriptedOpenAIServer model) throws Exception {
String last = model.requests.get(model.requests.size() - 1);
return FirstAgentTest.JSON.readTree(last).get("messages");
}
@Test
void failuresReachTheModelAsText() throws Exception {
var t = new Transcript("03-failure-modes.txt");
// 1. a tool throws
try (var model = new ScriptedOpenAIServer()) {
model.thenToolCall("c1", "getServiceStatus", "{\"service\":\"nope\"}").thenText("There is no such service.");
String answer = OpsAgents.firstAgent(TestExecutors.openAI(model)).run("status of nope?");
JsonNode msgs = lastMessages(model);
JsonNode toolMsg = msgs.get(msgs.size() - 1);
t.line("1. The tool throws IllegalArgumentException(\"Unknown service: nope\")");
t.line(" agent.run returned: " + answer);
t.line(" LLM calls: " + model.requests.size());
t.line(" what the model was told: " + toolMsg.get("role").asText() + ": " + toolMsg.get("content").asText());
assertThat(answer).isEqualTo("There is no such service.");
assertThat(toolMsg.get("role").asText()).isEqualTo("tool");
assertThat(toolMsg.get("content").asText()).contains("Unknown service: nope");
}
// 2. the model sends arguments of the wrong type
try (var model = new ScriptedOpenAIServer()) {
model.thenToolCall("c1", "error_rate", "{\"service\":\"checkout\",\"minutes\":\"soon\"}").thenText("sorry");
OpsAgents.firstAgent(TestExecutors.openAI(model)).run("error rate?");
JsonNode msgs = lastMessages(model);
JsonNode toolMsg = msgs.get(msgs.size() - 1);
t.line("");
t.line("2. The model sends {\"minutes\":\"soon\"} for an int parameter");
t.line(" what the model was told: " + toolMsg.get("content").asText().replace("\n", " | "));
assertThat(toolMsg.get("content").asText()).contains("failed to parse arguments");
}
// 3. the model calls a tool that was never registered
try (var model = new ScriptedOpenAIServer()) {
model.thenToolCall("c1", "restartService", "{\"service\":\"checkout\"}").thenText("ok");
OpsAgents.firstAgent(TestExecutors.openAI(model)).run("restart checkout");
JsonNode msgs = lastMessages(model);
t.line("");
t.line("3. The model calls restartService, which is not registered");
t.line(" the history sent back, last two messages:");
for (int i = msgs.size() - 2; i < msgs.size(); i++) t.line(" " + msgs.get(i));
assertThat(msgs.get(msgs.size() - 1).get("content").asText()).contains("not found in the tool registry");
}
// 4. the model never stops calling tools
try (var model = new ScriptedOpenAIServer()) {
for (int i = 0; i < 20; i++) model.thenToolCall("c" + i, "getServiceStatus", "{\"service\":\"search\"}");
AIAgent<String, String> agent = AIAgent.builder()
.promptExecutor(TestExecutors.openAI(model))
.llmModel(OpenAIModels.Chat.GPT4o)
.toolRegistry(ToolRegistry.builder().tools(new ServiceTools()).build())
.maxIterations(6)
.build();
t.line("");
t.line("4. The model answers every request with another tool call, maxIterations(6)");
Throwable thrown = null;
try { agent.run("loop"); } catch (Throwable e) { thrown = e; }
t.line(" " + thrown.getClass().getSimpleName());
t.line(" LLM calls made before it stopped: " + model.requests.size());
t.line(" default maxIterations of an agent you do not configure: "
+ OpsAgents.firstAgent(TestExecutors.openAI(model)).getAgentConfig().getMaxAgentIterations());
assertThat(thrown).isInstanceOf(AIAgentMaxNumberOfIterationsReachedException.class);
assertThat(model.requests.size()).isEqualTo(3);
}
t.write();
}
@Test
void anAgentInstanceCanRunTwice_andStartsFresh() throws Exception {
var t = new Transcript("04-agent-reuse.txt");
try (var model = new ScriptedOpenAIServer()) {
model.thenText("first").thenText("second");
AIAgent<String, String> agent = AIAgent.builder()
.promptExecutor(TestExecutors.openAI(model))
.llmModel(OpenAIModels.Chat.GPT4o)
.build();
String first = agent.run("hello");
String second = agent.run("hello again");
t.line("run 1: " + first);
t.line("run 2: " + second);
JsonNode run2 = FirstAgentTest.JSON.readTree(model.requests.get(1)).get("messages");
t.line("messages sent on run 2: " + run2.size());
run2.forEach(m -> t.line(" " + m));
assertThat(first).isEqualTo("first");
assertThat(second).isEqualTo("second");
// a second run() starts from a fresh history; run 1 does not leak into it
assertThat(run2).hasSize(1);
}
t.write();
}
}
@@ -0,0 +1,59 @@
package com.ankurm.koog;
import ai.koog.agents.core.agent.AIAgent;
import ai.koog.prompt.executor.model.PromptExecutor;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class FirstAgentTest {
static final ObjectMapper JSON = new ObjectMapper();
@Test
void agentCallsATool_thenAnswers() throws Exception {
try (var model = new ScriptedOpenAIServer()) {
model.thenToolCall("call_1", "getServiceStatus", "{\"service\":\"checkout\"}")
.thenText("checkout is DEGRADED");
PromptExecutor executor = TestExecutors.openAI(model);
AIAgent<String, String> agent = OpsAgents.firstAgent(executor);
String answer = agent.run("Is checkout healthy?");
var t = new Transcript("01-first-agent-on-the-wire.txt");
t.line("agent.run(\"Is checkout healthy?\") -> " + answer);
t.line("");
t.line("HTTP calls the model endpoint received: " + model.paths.size());
model.paths.forEach(p -> t.line(" " + p));
JsonNode first = JSON.readTree(model.requests.get(0));
t.line("");
t.line("Request 1 - messages:");
first.get("messages").forEach(m -> t.line(" " + m.get("role").asText() + ": " + m.get("content").asText()));
t.line("Request 1 - tools Koog derived from ServiceTools:");
first.get("tools").forEach(tool -> t.line(" " + compact(tool.get("function"))));
JsonNode second = JSON.readTree(model.requests.get(1));
t.line("");
t.line("Request 2 - messages (the tool call and its result are now in the history):");
second.get("messages").forEach(m -> t.line(" " + compact(m)));
t.write();
assertThat(answer).isEqualTo("checkout is DEGRADED");
assertThat(model.paths).containsExactly("POST /v1/chat/completions", "POST /v1/chat/completions");
assertThat(first.get("tools")).hasSize(2);
assertThat(first.get("tools").get(0).get("function").get("name").asText()).isEqualTo("getServiceStatus");
// customName wins over the method name
assertThat(first.get("tools").get(1).get("function").get("name").asText()).isEqualTo("error_rate");
JsonNode toolMessage = second.get("messages").get(3);
assertThat(toolMessage.get("role").asText()).isEqualTo("tool");
assertThat(toolMessage.get("content").asText()).isEqualTo("\"DEGRADED\"");
}
}
static String compact(JsonNode n) {
return n.toString();
}
}
@@ -0,0 +1,82 @@
package com.ankurm.koog;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
/**
* A stand-in for api.openai.com: a local HTTP server that speaks the chat-completions wire format
* and answers from a script. Koog's real OpenAI client, tool serialisation and agent loop all run;
* only the model is scripted. Every request body is recorded so tests can show what Koog sent.
*/
final class ScriptedOpenAIServer implements AutoCloseable {
private final HttpServer server;
private final Queue<String> script = new ArrayDeque<>();
final List<String> requests = Collections.synchronizedList(new ArrayList<>());
final List<String> paths = Collections.synchronizedList(new ArrayList<>());
ScriptedOpenAIServer() throws IOException {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/", exchange -> {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
paths.add(exchange.getRequestMethod() + " " + exchange.getRequestURI().getPath());
requests.add(body);
String reply = script.poll();
if (reply == null) {
reply = text("(script exhausted)");
}
byte[] bytes = reply.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(200, bytes.length);
exchange.getResponseBody().write(bytes);
exchange.close();
});
server.start();
}
String baseUrl() { return "http://127.0.0.1:" + server.getAddress().getPort(); }
ScriptedOpenAIServer thenToolCall(String id, String tool, String argumentsJson) {
script.add(toolCall(id, tool, argumentsJson));
return this;
}
ScriptedOpenAIServer thenText(String content) {
script.add(text(content));
return this;
}
static String text(String content) {
return """
{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-4o",
"choices":[{"index":0,"finish_reason":"stop",
"message":{"role":"assistant","content":%s}}],
"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}
""".formatted(quote(content));
}
static String toolCall(String id, String tool, String argumentsJson) {
return """
{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-4o",
"choices":[{"index":0,"finish_reason":"tool_calls",
"message":{"role":"assistant","content":null,
"tool_calls":[{"id":%s,"type":"function",
"function":{"name":%s,"arguments":%s}}]}}],
"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}
""".formatted(quote(id), quote(tool), quote(argumentsJson));
}
private static String quote(String s) {
return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\"";
}
@Override public void close() { server.stop(0); }
}
@@ -0,0 +1,96 @@
package com.ankurm.koog;
import ai.koog.prompt.executor.model.PromptExecutor;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.boot.builder.SpringApplicationBuilder;
import java.io.IOException;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SpringBootStarterTest {
static ScriptedOpenAIServer model;
static {
try { model = new ScriptedOpenAIServer(); } catch (IOException e) { throw new IllegalStateException(e); }
model.thenToolCall("call_1", "error_rate", "{\"service\":\"checkout\",\"minutes\":15}")
.thenText("checkout fails 4.2% of requests over 15 minutes");
}
@DynamicPropertySource
static void koog(DynamicPropertyRegistry registry) {
registry.add("ai.koog.openai.api-key", () -> "test-key");
registry.add("ai.koog.openai.base-url", model::baseUrl);
}
@AfterAll
static void stop() { model.close(); }
@Autowired ApplicationContext context;
@Autowired TestRestTemplate http;
@Test
void theStarterWiresAnExecutor_andTheServiceBuildsTheAgent() {
String body = http.getForObject("/ask?q={q}", String.class, "How bad are checkout errors?");
var t = new Transcript("05-spring-boot-starter.txt");
t.line("PromptExecutor beans the starter registered (only OpenAI is configured):");
Arrays.stream(context.getBeanNamesForType(PromptExecutor.class)).sorted().forEach(n -> t.line(" " + n));
t.line("");
t.line("GET /ask?q=How bad are checkout errors?");
t.line(" -> " + body);
t.line("Model endpoint saw: " + model.paths);
t.line("Tool argument the model sent: minutes=15 (an int, parsed by Koog)");
t.line("");
t.line("context.getBean(PromptExecutor.class) with one provider configured:");
try {
context.getBean(PromptExecutor.class);
} catch (NoUniqueBeanDefinitionException e) {
t.line(" " + e.getClass().getSimpleName() + ": expected single matching bean but found 2: "
+ String.join(",", e.getBeanNamesFound().stream().sorted().toList()));
assertThat(e.getBeanNamesFound()).containsExactlyInAnyOrder("openAIExecutor", "multiLLMPromptExecutor");
}
try { t.write(); } catch (IOException e) { throw new IllegalStateException(e); }
assertThat(body).isEqualTo("checkout fails 4.2% of requests over 15 minutes");
assertThat(context.getBeanNamesForType(PromptExecutor.class)).contains("openAIExecutor");
assertThat(model.paths).containsExactly("POST /v1/chat/completions", "POST /v1/chat/completions");
assertThat(model.requests.get(1)).contains("\"content\":\"4.2\"");
}
@Test
void withoutAnApiKeyThereIsNoExecutor_andTheServiceCannotStart() {
Throwable failure = null;
try (var ignored = new SpringApplicationBuilder(OpsApplication.class)
.properties("server.port=0", "ai.koog.openai.api-key=", "ai.koog.openai.enabled=true")
.run()) {
// not reached
} catch (Throwable e) {
failure = e;
}
Throwable root = failure;
while (root != null && root.getCause() != null && !(root instanceof org.springframework.beans.factory.NoSuchBeanDefinitionException)) {
root = root.getCause();
}
var t = new Transcript("06-spring-boot-no-api-key.txt");
t.line("ai.koog.openai.api-key is empty, ai.koog.openai.enabled=true is set:");
t.line(" startup failed: " + (failure != null));
t.line(" cause: " + (root == null ? "none" : root.getClass().getSimpleName()));
t.line(" message: " + (root == null ? "none" : root.getMessage()));
try { t.write(); } catch (IOException e) { throw new IllegalStateException(e); }
assertThat(failure).isNotNull();
assertThat(root).isInstanceOf(org.springframework.beans.factory.NoSuchBeanDefinitionException.class);
}
}
@@ -0,0 +1,40 @@
package com.ankurm.koog;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class StrategyGraphTest {
@Test
void aJavaNodeRunsBeforeTheModel_andTheGraphRendersAsMermaid() throws Exception {
try (var model = new ScriptedOpenAIServer()) {
model.thenToolCall("call_1", "getServiceStatus", "{\"service\":\"checkout\"}")
.thenText("checkout is DEGRADED");
var agent = OpsAgents.redactingAgent(TestExecutors.openAI(model));
String answer = agent.run("My key is sk-ABCDEFGH12345678. Is checkout healthy?");
JsonNode first = FirstAgentTest.JSON.readTree(model.requests.get(0));
String userMessage = first.get("messages").get(1).get("content").asText();
var t = new Transcript("02-strategy-graph-and-redaction.txt");
t.line("Graph, as MermaidDiagramGenerator prints it:");
t.line("");
for (String l : OpsAgents.diagram().split("\n")) t.line(l);
t.line("");
t.line("The user typed: My key is sk-ABCDEFGH12345678. Is checkout healthy?");
t.line("The model got: " + userMessage);
t.line("Answer: " + answer);
t.line("Secret present in any request body: "
+ model.requests.stream().anyMatch(r -> r.contains("sk-ABCDEFGH12345678")));
t.write();
assertThat(userMessage).isEqualTo("My key is [REDACTED]. Is checkout healthy?");
assertThat(model.requests).noneMatch(r -> r.contains("sk-ABCDEFGH12345678"));
assertThat(answer).isEqualTo("checkout is DEGRADED");
assertThat(OpsAgents.diagram()).contains("[*] --> redact", "redact --> callLLM");
}
}
}
@@ -0,0 +1,17 @@
package com.ankurm.koog;
import ai.koog.prompt.executor.clients.ConnectionTimeoutConfig;
import ai.koog.prompt.executor.clients.openai.OpenAIClientSettings;
import ai.koog.prompt.executor.model.PromptExecutor;
/** A PromptExecutor whose OpenAI client talks to the scripted server instead of api.openai.com. */
final class TestExecutors {
private TestExecutors() {}
static PromptExecutor openAI(ScriptedOpenAIServer model) {
var settings = new OpenAIClientSettings(
model.baseUrl(), new ConnectionTimeoutConfig(),
"v1/chat/completions", "v1/responses", "v1/embeddings", "v1/moderations", "v1/models");
return PromptExecutor.builder().openAI("test-key", settings).build();
}
}
@@ -0,0 +1,23 @@
package com.ankurm.koog;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
/** Collects lines and writes them to output/NN-name.txt, so the test IS the transcript. */
final class Transcript {
private final List<String> lines = new ArrayList<>();
private final String file;
Transcript(String file) { this.file = file; }
void line(String s) { lines.add(s); }
void write() throws IOException {
Path dir = Path.of("output");
Files.createDirectories(dir);
Files.write(dir.resolve(file), lines);
}
}
@@ -0,0 +1,11 @@
package com.ankurm.koog;
import ai.koog.prompt.executor.model.PromptExecutor;
/** Only used by scripts/kotlin-bom-trap.sh: the first Koog call that touches kotlin.time. */
public class TrapMain {
public static void main(String[] args) {
PromptExecutor.builder().openAI("k").build();
System.out.println("no error");
}
}
+9
View File
@@ -0,0 +1,9 @@
<configuration>
<!-- Koog logs a full stack trace at ERROR for every tool failure it hands back to the model.
That is useful in production and drowns the test report, so the tests keep it off. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder><pattern>%d{HH:mm:ss.SSS} %-5level %logger{20} - %msg%n</pattern></encoder>
</appender>
<logger name="ai.koog" level="OFF"/>
<root level="WARN"><appender-ref ref="STDOUT"/></root>
</configuration>