Add chat-ui-htmx module: streaming chat page on Spring AI 2.0.2 with htmx and its SSE extension (vendored), MessageChatMemoryAdvisor keyed by the HTTP session, a Stop button, and the traps measured: EventSource strips one leading space per data line, model text must be HTML-escaped, a cancelled stream left the upstream connection open in 7 of 8 runs until an OkHttp interceptor calls Call.cancel (8 of 8 closed after), and the SDK push source ignores back-pressure (limitRate changes only the demand; a bounded buffer works). The model is a stub OpenAI-compatible SSE server written for the module; no real provider was run.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6Kg9RWSTJzyz6KP9X
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.BufferedSource;
|
||||
import org.springframework.ai.openai.http.okhttp.OpenAiHttpClientBuilderCustomizer;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Makes a cancelled stream actually close its HTTP connection.
|
||||
*
|
||||
* Spring AI 2.0.2's OkHttp client closes a streaming response with {@code response.body().close()}.
|
||||
* When that runs on a different thread from the one blocked reading the body, OkHttp 4.12 throws
|
||||
* {@code IllegalStateException: Unbalanced enter/exit} and leaves the connection open, so the model
|
||||
* server keeps generating. {@code Call.cancel()} is the call OkHttp documents for this and is safe from
|
||||
* any thread, so this interceptor wraps SSE response bodies to call it first.
|
||||
* Switch off with {@code chat.cancel-fix=false} to reproduce the leak (scripts/cancel-upstream.sh).
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "chat.cancel-fix", havingValue = "true", matchIfMissing = true)
|
||||
public class CancelOnCloseConfig {
|
||||
|
||||
@Bean
|
||||
OpenAiHttpClientBuilderCustomizer cancelOnClose() {
|
||||
return builder -> builder.interceptor(chain -> {
|
||||
Response response = chain.proceed(chain.request());
|
||||
ResponseBody body = response.body();
|
||||
if (body == null || body.contentType() == null || !"event-stream".equals(body.contentType().subtype())) {
|
||||
return response;
|
||||
}
|
||||
var call = chain.call();
|
||||
return response.newBuilder().body(new ResponseBody() {
|
||||
@Override public MediaType contentType() { return body.contentType(); }
|
||||
@Override public long contentLength() { return body.contentLength(); }
|
||||
@Override public BufferedSource source() { return body.source(); }
|
||||
@Override public void close() {
|
||||
call.cancel();
|
||||
try {
|
||||
body.close();
|
||||
} catch (RuntimeException ignored) {
|
||||
// the call is already cancelled; closing the body can only complain
|
||||
}
|
||||
}
|
||||
}).build();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
||||
import org.springframework.ai.chat.model.ChatModel;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ChatConfig {
|
||||
|
||||
/** In-memory window of the last N messages per conversation id (the default repository is a map). */
|
||||
@Bean
|
||||
ChatMemory chatMemory(@Value("${chat.memory.max-messages:20}") int maxMessages) {
|
||||
return MessageWindowChatMemory.builder().maxMessages(maxMessages).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ChatClient chatClient(ChatModel model, ChatMemory memory) {
|
||||
return ChatClient.builder(model)
|
||||
.defaultSystem("You are a concise assistant.")
|
||||
.defaultAdvisors(MessageChatMemoryAdvisor.builder(memory).build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import java.time.Duration;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.ServerSentEvent;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
import reactor.core.publisher.BufferOverflowStrategy;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
/**
|
||||
* Three endpoints and no JavaScript of our own:
|
||||
* POST /chat returns an HTML fragment (the user bubble and an empty assistant bubble that opens an
|
||||
* EventSource), GET /chat/stream is the Server-Sent Events stream, POST /chat/stop swaps the stream
|
||||
* element out so htmx closes the EventSource.
|
||||
*/
|
||||
@RestController
|
||||
public class ChatController {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger("chat");
|
||||
static final String CID = "conversationId";
|
||||
|
||||
private final ChatClient chatClient;
|
||||
private final AtomicInteger turns = new AtomicInteger();
|
||||
/** Test hook: how many streams were cancelled by the client (also logged). */
|
||||
public final AtomicInteger cancelled = new AtomicInteger();
|
||||
/** none | limitRate | slow | bounded: how the Flux is shaped between the model and the socket. */
|
||||
private final String backpressure;
|
||||
private final int slowMillis;
|
||||
|
||||
public ChatController(ChatClient chatClient,
|
||||
@Value("${chat.backpressure:none}") String backpressure,
|
||||
@Value("${chat.slow-millis:0}") int slowMillis) {
|
||||
this.chatClient = chatClient;
|
||||
this.backpressure = backpressure;
|
||||
this.slowMillis = slowMillis;
|
||||
}
|
||||
|
||||
/** The conversation id lives in the HTTP session, so a new browser (new cookie) is a new conversation. */
|
||||
static String conversationId(HttpSession session) {
|
||||
Object id = session.getAttribute(CID);
|
||||
if (id == null) {
|
||||
id = UUID.randomUUID().toString();
|
||||
session.setAttribute(CID, id);
|
||||
}
|
||||
return id.toString();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/chat", produces = MediaType.TEXT_HTML_VALUE)
|
||||
public String send(@RequestParam String message) {
|
||||
int turn = turns.incrementAndGet();
|
||||
String url = "/chat/stream?turn=" + turn + "&message=" + UriUtils.encodeQueryParam(message, UTF_8);
|
||||
return """
|
||||
<div class="msg user">%s</div>
|
||||
<div class="msg bot">
|
||||
<span id="out-%d"></span>
|
||||
<span id="sse-%d" hx-ext="sse" sse-connect="%s" sse-swap="token"
|
||||
sse-close="done" hx-target="#out-%d" hx-swap="beforeend">
|
||||
<span sse-swap="done" hx-target="#stop-%d" hx-swap="delete"></span>
|
||||
</span>
|
||||
<button id="stop-%d" type="button" class="stop"
|
||||
hx-post="/chat/stop?turn=%d" hx-target="#sse-%d" hx-swap="outerHTML">Stop</button>
|
||||
</div>
|
||||
""".formatted(HtmlUtils.htmlEscape(message), turn, turn,
|
||||
HtmlUtils.htmlEscape(url), turn, turn, turn, turn, turn);
|
||||
}
|
||||
|
||||
/** Replaces the stream element; htmx cleans the removed element up and closes its EventSource. */
|
||||
@PostMapping(value = "/chat/stop", produces = MediaType.TEXT_HTML_VALUE)
|
||||
public String stop(@RequestParam int turn) {
|
||||
return "<em id=\"sse-" + turn + "\"> [stopped]</em>"
|
||||
+ "<span id=\"stop-" + turn + "\" hx-swap-oob=\"delete\"></span>";
|
||||
}
|
||||
|
||||
@PostMapping("/chat/new")
|
||||
public String newConversation(HttpSession session) {
|
||||
session.removeAttribute(CID);
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<ServerSentEvent<String>> stream(@RequestParam String message,
|
||||
@RequestParam(defaultValue = "0") int turn,
|
||||
HttpSession session) {
|
||||
String cid = conversationId(session);
|
||||
AtomicLong tokens = new AtomicLong();
|
||||
AtomicLong requested = new AtomicLong();
|
||||
|
||||
Flux<String> content = chatClient.prompt()
|
||||
.user(message)
|
||||
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, cid))
|
||||
.stream()
|
||||
.content()
|
||||
.doOnRequest(n -> log.info("turn {} model-side demand request({})", turn, n == Long.MAX_VALUE ? "unbounded" : n));
|
||||
|
||||
// Optional shaping between the model and the socket (see scripts/backpressure.sh). "model-side"
|
||||
// demand is logged above this switch and "web-side" demand below it.
|
||||
content = switch (backpressure) {
|
||||
case "limitRate" -> content.limitRate(8);
|
||||
// A push source (the SDK reader thread) cannot be slowed by demand; bound the queue and
|
||||
// fail the slow client instead of buffering without limit.
|
||||
case "bounded" -> content.onBackpressureBuffer(16, BufferOverflowStrategy.ERROR);
|
||||
case "slow" -> content.delayElements(Duration.ofMillis(slowMillis));
|
||||
default -> content;
|
||||
};
|
||||
|
||||
return content
|
||||
.doOnRequest(n -> {
|
||||
requested.addAndGet(n);
|
||||
log.info("turn {} web-side demand request({})", turn, n == Long.MAX_VALUE ? "unbounded" : n);
|
||||
})
|
||||
.map(token -> {
|
||||
tokens.incrementAndGet();
|
||||
// Model text is untrusted: htmx inserts the data as HTML, so escape it here.
|
||||
// A browser strips ONE leading space from every data line (SSE spec), and Spring writes
|
||||
// "data:" with no space of its own, so a token like " world" would lose its space.
|
||||
// Prefix every line with a space that the client strips again.
|
||||
String data = " " + HtmlUtils.htmlEscape(token).replace("\n", "\n ");
|
||||
return ServerSentEvent.builder(data).event("token").build();
|
||||
})
|
||||
.concatWith(Flux.just(ServerSentEvent.<String>builder("").event("done").build()))
|
||||
.doOnSubscribe(s -> log.info("turn {} start conversation={} message={}", turn, cid, message))
|
||||
.doOnCancel(() -> cancelled.incrementAndGet())
|
||||
.doOnCancel(() -> log.info("turn {} CANCELLED by client after {} tokens", turn, tokens.get()))
|
||||
.doOnComplete(() -> log.info("turn {} complete, {} tokens", turn, tokens.get()))
|
||||
.doOnError(e -> log.info("turn {} error {}", turn, e.toString()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ChatUiApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ChatUiApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package com.ankurm.chatui.stub;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/**
|
||||
* STUB, NOT A LANGUAGE MODEL. A small OpenAI-compatible server for POST /v1/chat/completions that
|
||||
* streams Server-Sent Events chunks ("data: {chat.completion.chunk}" ... "data: [DONE]") with a
|
||||
* controllable delay between chunks. Its "answers" are templates built from the request it received,
|
||||
* so a test can see exactly which messages the client sent (that is how conversation memory is
|
||||
* observed). It does no inference.
|
||||
*
|
||||
* <pre>
|
||||
* java -cp ... com.ankurm.chatui.stub.OpenAiStubServer [port=38181] [delayMs=20]
|
||||
* POST /stub/config?delayMs=N change the delay between chunks at runtime
|
||||
* GET /stub/stats request, chunk and aborted-stream counters
|
||||
* latest user message "count N" streams N one-word chunks (w1 w2 ... wN)
|
||||
* latest user message "big N" streams N chunks of 32 KB each (to make a slow reader visible)
|
||||
* latest user message "html..." streams text containing markup, to show why the UI escapes it
|
||||
* anything else "[stub] saw N messages. Earlier user turns: a | b. You said: c."
|
||||
* </pre>
|
||||
*/
|
||||
public class OpenAiStubServer {
|
||||
|
||||
private static final JsonMapper JSON = JsonMapper.builder().build();
|
||||
private static final Pattern COUNT = Pattern.compile("^count (\\d+).*", Pattern.DOTALL);
|
||||
private static final Pattern BIG = Pattern.compile("^big (\\d+).*", Pattern.DOTALL);
|
||||
private static final String KB32 = "x".repeat(32 * 1024);
|
||||
|
||||
private static volatile long delayMs = 20;
|
||||
private static final AtomicInteger requests = new AtomicInteger();
|
||||
private static final AtomicLong chunksWritten = new AtomicLong();
|
||||
private static final AtomicInteger aborted = new AtomicInteger();
|
||||
private static final AtomicInteger finished = new AtomicInteger();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
start(args.length > 0 ? Integer.parseInt(args[0]) : 38181, args.length > 1 ? Long.parseLong(args[1]) : 20);
|
||||
}
|
||||
|
||||
/** Starts the stub (also used in-process by the tests). */
|
||||
public static HttpServer start(int port, long delay) throws IOException {
|
||||
delayMs = delay;
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0);
|
||||
server.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
|
||||
server.createContext("/stub/config", ex -> {
|
||||
String q = ex.getRequestURI().getQuery();
|
||||
if (q != null && q.startsWith("delayMs=")) {
|
||||
delayMs = Long.parseLong(q.substring(8));
|
||||
}
|
||||
reply(ex, 200, "delayMs=" + delayMs + "\n");
|
||||
});
|
||||
server.createContext("/stub/stats", ex -> reply(ex, 200,
|
||||
"requests=" + requests + " chunksWritten=" + chunksWritten
|
||||
+ " finished=" + finished + " abortedByClient=" + aborted + "\n"));
|
||||
server.createContext("/v1/chat/completions", OpenAiStubServer::completions);
|
||||
server.start();
|
||||
System.out.println("stub: listening on " + port + " delayMs=" + delayMs);
|
||||
return server;
|
||||
}
|
||||
|
||||
public static void setDelayMs(long ms) {
|
||||
delayMs = ms;
|
||||
}
|
||||
|
||||
/** Counters for tests: {requests, chunksWritten, finished, abortedByClient}. */
|
||||
public static long[] counters() {
|
||||
return new long[] {requests.get(), chunksWritten.get(), finished.get(), aborted.get()};
|
||||
}
|
||||
|
||||
private static void reply(HttpExchange ex, int code, String body) throws IOException {
|
||||
byte[] b = body.getBytes(StandardCharsets.UTF_8);
|
||||
ex.sendResponseHeaders(code, b.length);
|
||||
try (OutputStream os = ex.getResponseBody()) {
|
||||
os.write(b);
|
||||
}
|
||||
}
|
||||
|
||||
private static void completions(HttpExchange ex) throws IOException {
|
||||
int id = requests.incrementAndGet();
|
||||
JsonNode body = JSON.readTree(ex.getRequestBody());
|
||||
String model = body.path("model").asText("?");
|
||||
boolean stream = body.path("stream").asBoolean(false);
|
||||
boolean usage = body.path("stream_options").path("include_usage").asBoolean(false);
|
||||
List<String> userTurns = new ArrayList<>();
|
||||
StringBuilder roles = new StringBuilder();
|
||||
int n = 0;
|
||||
for (JsonNode m : body.path("messages")) {
|
||||
n++;
|
||||
String role = m.path("role").asText();
|
||||
roles.append(roles.isEmpty() ? "" : ",").append(role);
|
||||
if (role.equals("user")) {
|
||||
userTurns.add(text(m.path("content")));
|
||||
}
|
||||
}
|
||||
String latest = userTurns.isEmpty() ? "" : userTurns.getLast();
|
||||
List<String> earlier = userTurns.size() > 1 ? userTurns.subList(0, userTurns.size() - 1) : List.of();
|
||||
System.out.println("stub: request #" + id + " model=" + model + " stream=" + stream
|
||||
+ " messages=" + n + " roles=" + roles + " latest=\"" + latest + "\"");
|
||||
if (!stream) {
|
||||
reply(ex, 400, "{\"error\":{\"message\":\"stub only streams\"}}");
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> tokens = new ArrayList<>();
|
||||
Matcher c = COUNT.matcher(latest);
|
||||
if (c.matches()) {
|
||||
for (int i = 1; i <= Integer.parseInt(c.group(1)); i++) {
|
||||
tokens.add((i == 1 ? "" : " ") + "w" + i);
|
||||
}
|
||||
} else if (BIG.matcher(latest).matches()) {
|
||||
Matcher m = BIG.matcher(latest);
|
||||
m.matches();
|
||||
for (int i = 1; i <= Integer.parseInt(m.group(1)); i++) {
|
||||
tokens.add(KB32);
|
||||
}
|
||||
} else if (latest.startsWith("html")) {
|
||||
for (String w : "Here is <b>bold</b> and <img src=x onerror=alert(1)> & done".split("(?= )")) {
|
||||
tokens.add(w);
|
||||
}
|
||||
} else {
|
||||
String text = "[stub] saw " + n + " messages. Earlier user turns: "
|
||||
+ (earlier.isEmpty() ? "none" : String.join(" | ", earlier)) + ". You said: " + latest;
|
||||
for (String w : text.split("(?= )")) {
|
||||
tokens.add(w);
|
||||
}
|
||||
}
|
||||
|
||||
ex.getResponseHeaders().add("Content-Type", "text/event-stream");
|
||||
ex.getResponseHeaders().add("Cache-Control", "no-cache");
|
||||
ex.sendResponseHeaders(200, 0); // chunked
|
||||
long created = System.currentTimeMillis() / 1000;
|
||||
String cid = "chatcmpl-stub-" + id;
|
||||
int sent = 0;
|
||||
try (OutputStream os = ex.getResponseBody()) {
|
||||
write(os, chunk(cid, created, model, "{\"role\":\"assistant\",\"content\":\"\"}", null));
|
||||
for (String t : tokens) {
|
||||
if (delayMs > 0) {
|
||||
Thread.sleep(delayMs);
|
||||
}
|
||||
write(os, chunk(cid, created, model, "{\"content\":" + JSON.writeValueAsString(t) + "}", null));
|
||||
sent++;
|
||||
}
|
||||
write(os, chunk(cid, created, model, "{}", "stop"));
|
||||
if (usage) {
|
||||
write(os, "{\"id\":\"" + cid + "\",\"object\":\"chat.completion.chunk\",\"created\":" + created
|
||||
+ ",\"model\":\"" + model + "\",\"choices\":[],\"usage\":{\"prompt_tokens\":" + n
|
||||
+ ",\"completion_tokens\":" + sent + ",\"total_tokens\":" + (n + sent) + "}}");
|
||||
}
|
||||
os.write("data: [DONE]\n\n".getBytes(StandardCharsets.UTF_8));
|
||||
os.flush();
|
||||
finished.incrementAndGet();
|
||||
System.out.println("stub: stream #" + id + " finished after " + sent + " chunks");
|
||||
} catch (IOException e) {
|
||||
aborted.incrementAndGet();
|
||||
System.out.println("stub: stream #" + id + " client closed the connection after " + sent + " chunks");
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
private static String text(JsonNode content) {
|
||||
if (content.isString()) {
|
||||
return content.asString();
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (JsonNode part : content) {
|
||||
sb.append(part.path("text").asString(""));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String chunk(String id, long created, String model, String delta, String finish) {
|
||||
return "{\"id\":\"" + id + "\",\"object\":\"chat.completion.chunk\",\"created\":" + created
|
||||
+ ",\"model\":\"" + model + "\",\"choices\":[{\"index\":0,\"delta\":" + delta
|
||||
+ ",\"finish_reason\":" + (finish == null ? "null" : "\"" + finish + "\"") + "}]}";
|
||||
}
|
||||
|
||||
private static void write(OutputStream os, String json) throws IOException {
|
||||
os.write(("data: " + json + "\n\n").getBytes(StandardCharsets.UTF_8));
|
||||
os.flush();
|
||||
chunksWritten.incrementAndGet();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
# The OpenAI starter used as a generic OpenAI-compatible client. Defaults point at the STUB server in
|
||||
# this module (com.ankurm.chatui.stub.OpenAiStubServer). To use a real provider:
|
||||
# CHAT_BASE_URL=https://api.openai.com/v1 CHAT_MODEL=<model> OPENAI_API_KEY=<key>
|
||||
# base-url must END IN /v1: the starter does not append it, the SDK adds only /chat/completions.
|
||||
server:
|
||||
port: ${SERVER_PORT:38180}
|
||||
spring:
|
||||
ai:
|
||||
openai:
|
||||
base-url: ${CHAT_BASE_URL:http://localhost:38181/v1}
|
||||
api-key: ${OPENAI_API_KEY:not-needed}
|
||||
# SDK default read timeout is 60 s; a slow model can exceed it.
|
||||
timeout: 5m
|
||||
chat:
|
||||
options:
|
||||
model: ${CHAT_MODEL:stub-gpt}
|
||||
logging:
|
||||
pattern:
|
||||
console: "%msg%n"
|
||||
level:
|
||||
root: warn
|
||||
chat: info
|
||||
@@ -0,0 +1,29 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Chat UI with Spring AI, SSE and htmx</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="/vendor/htmx.min.js"></script>
|
||||
<script src="/vendor/htmx-ext-sse.js"></script>
|
||||
<style>
|
||||
body { font: 16px/1.5 system-ui, sans-serif; max-width: 42rem; margin: 2rem auto; padding: 0 1rem; }
|
||||
#log { min-height: 12rem; }
|
||||
.msg { padding: .5rem .75rem; margin: .5rem 0; border-radius: .5rem; white-space: pre-wrap; }
|
||||
.user { background: #e8eefc; }
|
||||
.bot { background: #f4f5f7; }
|
||||
.stop { float: right; }
|
||||
form { display: flex; gap: .5rem; }
|
||||
input[name=message] { flex: 1; padding: .5rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Chat</h1>
|
||||
<div id="log"></div>
|
||||
<form hx-post="/chat" hx-target="#log" hx-swap="beforeend" hx-on::after-request="this.reset()">
|
||||
<input name="message" autocomplete="off" placeholder="Say something" required autofocus>
|
||||
<button>Send</button>
|
||||
</form>
|
||||
<p><button hx-post="/chat/new" hx-swap="none" hx-on::after-request="document.getElementById('log').innerHTML=''">New conversation</button></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,2 @@
|
||||
d6fdc75f204e6bdefa99b69bf1e6d4ac69b8a364f77929f45c13476b4000f717 htmx.min.js
|
||||
3b5992a541619babefc4c169505af474df5c3039da51e59b96ccf9241ecd61d2 htmx-ext-sse.js
|
||||
@@ -0,0 +1,290 @@
|
||||
/*
|
||||
Server Sent Events Extension
|
||||
============================
|
||||
This extension adds support for Server Sent Events to htmx. See /www/extensions/sse.md for usage instructions.
|
||||
|
||||
*/
|
||||
|
||||
(function() {
|
||||
/** @type {import("../htmx").HtmxInternalApi} */
|
||||
var api
|
||||
|
||||
htmx.defineExtension('sse', {
|
||||
|
||||
/**
|
||||
* Init saves the provided reference to the internal HTMX API.
|
||||
*
|
||||
* @param {import("../htmx").HtmxInternalApi} api
|
||||
* @returns void
|
||||
*/
|
||||
init: function(apiRef) {
|
||||
// store a reference to the internal API.
|
||||
api = apiRef
|
||||
|
||||
// set a function in the public API for creating new EventSource objects
|
||||
if (htmx.createEventSource == undefined) {
|
||||
htmx.createEventSource = createEventSource
|
||||
}
|
||||
},
|
||||
|
||||
getSelectors: function() {
|
||||
return ['[sse-connect]', '[data-sse-connect]', '[sse-swap]', '[data-sse-swap]']
|
||||
},
|
||||
|
||||
/**
|
||||
* onEvent handles all events passed to this extension.
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {Event} evt
|
||||
* @returns void
|
||||
*/
|
||||
onEvent: function(name, evt) {
|
||||
var parent = evt.target || evt.detail.elt
|
||||
switch (name) {
|
||||
case 'htmx:beforeCleanupElement':
|
||||
var internalData = api.getInternalData(parent)
|
||||
// Try to remove remove an EventSource when elements are removed
|
||||
var source = internalData.sseEventSource
|
||||
if (source) {
|
||||
api.triggerEvent(parent, 'htmx:sseClose', {
|
||||
source,
|
||||
type: 'nodeReplaced',
|
||||
})
|
||||
internalData.sseEventSource.close()
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
// Try to create EventSources when elements are processed
|
||||
case 'htmx:afterProcessNode':
|
||||
ensureEventSourceOnElement(parent)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
/// ////////////////////////////////////////////
|
||||
// HELPER FUNCTIONS
|
||||
/// ////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* createEventSource is the default method for creating new EventSource objects.
|
||||
* it is hoisted into htmx.config.createEventSource to be overridden by the user, if needed.
|
||||
*
|
||||
* @param {string} url
|
||||
* @returns EventSource
|
||||
*/
|
||||
function createEventSource(url) {
|
||||
return new EventSource(url, { withCredentials: true })
|
||||
}
|
||||
|
||||
/**
|
||||
* registerSSE looks for attributes that can contain sse events, right
|
||||
* now hx-trigger and sse-swap and adds listeners based on these attributes too
|
||||
* the closest event source
|
||||
*
|
||||
* @param {HTMLElement} elt
|
||||
*/
|
||||
function registerSSE(elt) {
|
||||
// Add message handlers for every `sse-swap` attribute
|
||||
if (api.getAttributeValue(elt, 'sse-swap')) {
|
||||
// Find closest existing event source
|
||||
var sourceElement = api.getClosestMatch(elt, hasEventSource)
|
||||
if (sourceElement == null) {
|
||||
// api.triggerErrorEvent(elt, "htmx:noSSESourceError")
|
||||
return null // no eventsource in parentage, orphaned element
|
||||
}
|
||||
|
||||
// Set internalData and source
|
||||
var internalData = api.getInternalData(sourceElement)
|
||||
var source = internalData.sseEventSource
|
||||
|
||||
var sseSwapAttr = api.getAttributeValue(elt, 'sse-swap')
|
||||
var sseEventNames = sseSwapAttr.split(',')
|
||||
|
||||
for (var i = 0; i < sseEventNames.length; i++) {
|
||||
const sseEventName = sseEventNames[i].trim()
|
||||
const listener = function(event) {
|
||||
// If the source is missing then close SSE
|
||||
if (maybeCloseSSESource(sourceElement)) {
|
||||
return
|
||||
}
|
||||
|
||||
// If the body no longer contains the element, remove the listener
|
||||
if (!api.bodyContains(elt)) {
|
||||
source.removeEventListener(sseEventName, listener)
|
||||
return
|
||||
}
|
||||
|
||||
// swap the response into the DOM and trigger a notification
|
||||
if (!api.triggerEvent(elt, 'htmx:sseBeforeMessage', event)) {
|
||||
return
|
||||
}
|
||||
swap(elt, event.data)
|
||||
api.triggerEvent(elt, 'htmx:sseMessage', event)
|
||||
}
|
||||
|
||||
// Register the new listener
|
||||
api.getInternalData(elt).sseEventListener = listener
|
||||
source.addEventListener(sseEventName, listener)
|
||||
}
|
||||
}
|
||||
|
||||
// Add message handlers for every `hx-trigger="sse:*"` attribute
|
||||
if (api.getAttributeValue(elt, 'hx-trigger')) {
|
||||
// Find closest existing event source
|
||||
var sourceElement = api.getClosestMatch(elt, hasEventSource)
|
||||
if (sourceElement == null) {
|
||||
// api.triggerErrorEvent(elt, "htmx:noSSESourceError")
|
||||
return null // no eventsource in parentage, orphaned element
|
||||
}
|
||||
|
||||
// Set internalData and source
|
||||
var internalData = api.getInternalData(sourceElement)
|
||||
var source = internalData.sseEventSource
|
||||
|
||||
var triggerSpecs = api.getTriggerSpecs(elt)
|
||||
triggerSpecs.forEach(function(ts) {
|
||||
if (ts.trigger.slice(0, 4) !== 'sse:') {
|
||||
return
|
||||
}
|
||||
|
||||
var listener = function (event) {
|
||||
if (maybeCloseSSESource(sourceElement)) {
|
||||
return
|
||||
}
|
||||
if (!api.bodyContains(elt)) {
|
||||
source.removeEventListener(ts.trigger.slice(4), listener)
|
||||
}
|
||||
// Trigger events to be handled by the rest of htmx
|
||||
htmx.trigger(elt, ts.trigger, event)
|
||||
htmx.trigger(elt, 'htmx:sseMessage', event)
|
||||
}
|
||||
|
||||
// Register the new listener
|
||||
api.getInternalData(elt).sseEventListener = listener
|
||||
source.addEventListener(ts.trigger.slice(4), listener)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ensureEventSourceOnElement creates a new EventSource connection on the provided element.
|
||||
* If a usable EventSource already exists, then it is returned. If not, then a new EventSource
|
||||
* is created and stored in the element's internalData.
|
||||
* @param {HTMLElement} elt
|
||||
* @param {number} retryCount
|
||||
* @returns {EventSource | null}
|
||||
*/
|
||||
function ensureEventSourceOnElement(elt, retryCount) {
|
||||
if (elt == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
// handle extension source creation attribute
|
||||
if (api.getAttributeValue(elt, 'sse-connect')) {
|
||||
var sseURL = api.getAttributeValue(elt, 'sse-connect')
|
||||
if (sseURL == null) {
|
||||
return
|
||||
}
|
||||
|
||||
ensureEventSource(elt, sseURL, retryCount)
|
||||
}
|
||||
|
||||
registerSSE(elt)
|
||||
}
|
||||
|
||||
function ensureEventSource(elt, url, retryCount) {
|
||||
var source = htmx.createEventSource(url)
|
||||
|
||||
source.onerror = function(err) {
|
||||
// Log an error event
|
||||
api.triggerErrorEvent(elt, 'htmx:sseError', { error: err, source })
|
||||
|
||||
// If parent no longer exists in the document, then clean up this EventSource
|
||||
if (maybeCloseSSESource(elt)) {
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, try to reconnect the EventSource
|
||||
if (source.readyState === EventSource.CLOSED) {
|
||||
retryCount = retryCount || 0
|
||||
retryCount = Math.max(Math.min(retryCount * 2, 128), 1)
|
||||
var timeout = retryCount * 500
|
||||
window.setTimeout(function() {
|
||||
ensureEventSourceOnElement(elt, retryCount)
|
||||
}, timeout)
|
||||
}
|
||||
}
|
||||
|
||||
source.onopen = function(evt) {
|
||||
api.triggerEvent(elt, 'htmx:sseOpen', { source })
|
||||
|
||||
if (retryCount && retryCount > 0) {
|
||||
const childrenToFix = elt.querySelectorAll("[sse-swap], [data-sse-swap], [hx-trigger], [data-hx-trigger]")
|
||||
for (let i = 0; i < childrenToFix.length; i++) {
|
||||
registerSSE(childrenToFix[i])
|
||||
}
|
||||
// We want to increase the reconnection delay for consecutive failed attempts only
|
||||
retryCount = 0
|
||||
}
|
||||
}
|
||||
|
||||
api.getInternalData(elt).sseEventSource = source
|
||||
|
||||
|
||||
var closeAttribute = api.getAttributeValue(elt, "sse-close");
|
||||
if (closeAttribute) {
|
||||
// close eventsource when this message is received
|
||||
source.addEventListener(closeAttribute, function() {
|
||||
api.triggerEvent(elt, 'htmx:sseClose', {
|
||||
source,
|
||||
type: 'message',
|
||||
})
|
||||
source.close()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* maybeCloseSSESource confirms that the parent element still exists.
|
||||
* If not, then any associated SSE source is closed and the function returns true.
|
||||
*
|
||||
* @param {HTMLElement} elt
|
||||
* @returns boolean
|
||||
*/
|
||||
function maybeCloseSSESource(elt) {
|
||||
if (!api.bodyContains(elt)) {
|
||||
var source = api.getInternalData(elt).sseEventSource
|
||||
if (source != undefined) {
|
||||
api.triggerEvent(elt, 'htmx:sseClose', {
|
||||
source,
|
||||
type: 'nodeMissing',
|
||||
})
|
||||
source.close()
|
||||
// source = null
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} elt
|
||||
* @param {string} content
|
||||
*/
|
||||
function swap(elt, content) {
|
||||
api.withExtensions(elt, function(extension) {
|
||||
content = extension.transformResponse(content, null, elt)
|
||||
})
|
||||
|
||||
var swapSpec = api.getSwapSpecification(elt)
|
||||
var target = api.getTarget(elt)
|
||||
api.swap(target, content, swapSpec, { contextElement: elt })
|
||||
}
|
||||
|
||||
|
||||
function hasEventSource(node) {
|
||||
return api.getInternalData(node).sseEventSource != null
|
||||
}
|
||||
})()
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,219 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.ankurm.chatui.stub.OpenAiStubServer;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.CookieManager;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
/**
|
||||
* The whole chain on a random port: HTTP -> Spring MVC -> ChatClient + memory -> OpenAiChatModel ->
|
||||
* STUB server. Transcripts land in output/01-sse-transcript.txt, 02-memory.txt, 03-escaping.txt and
|
||||
* 04-cancel-with-fix.txt.
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class ChatFlowTest {
|
||||
|
||||
@BeforeAll
|
||||
static void stub() {
|
||||
StubSupport.ensureStarted();
|
||||
}
|
||||
|
||||
@DynamicPropertySource
|
||||
static void props(DynamicPropertyRegistry r) {
|
||||
r.add("spring.ai.openai.base-url", () -> "http://localhost:" + StubSupport.PORT + "/v1");
|
||||
}
|
||||
|
||||
@Value("${local.server.port}")
|
||||
int port;
|
||||
@Autowired
|
||||
ChatController controller;
|
||||
|
||||
/** One SSE event as a browser's EventSource would see it. */
|
||||
record Event(String name, String data) {
|
||||
}
|
||||
|
||||
HttpClient browser() {
|
||||
return HttpClient.newBuilder().cookieHandler(new CookieManager()).build();
|
||||
}
|
||||
|
||||
/** Reads the raw stream and returns (raw lines, events parsed per the WHATWG rules). */
|
||||
List<String> raw = new ArrayList<>();
|
||||
|
||||
List<Event> stream(HttpClient client, String message) throws Exception {
|
||||
raw = new ArrayList<>();
|
||||
var req = HttpRequest.newBuilder(URI.create("http://localhost:" + port + "/chat/stream?turn=1&message="
|
||||
+ URLEncoder.encode(message, StandardCharsets.UTF_8))).header("Accept", "text/event-stream").build();
|
||||
var res = client.send(req, HttpResponse.BodyHandlers.ofInputStream());
|
||||
List<Event> events = new ArrayList<>();
|
||||
String name = "message";
|
||||
StringBuilder data = new StringBuilder();
|
||||
boolean has = false;
|
||||
try (var in = new BufferedReader(new InputStreamReader(res.body(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
raw.add(line);
|
||||
if (line.isEmpty()) {
|
||||
if (has) {
|
||||
events.add(new Event(name, data.toString()));
|
||||
}
|
||||
name = "message";
|
||||
data.setLength(0);
|
||||
has = false;
|
||||
} else if (line.startsWith("event:")) {
|
||||
name = strip(line.substring(6));
|
||||
} else if (line.startsWith("data:")) {
|
||||
if (has) {
|
||||
data.append('\n');
|
||||
}
|
||||
data.append(strip(line.substring(5)));
|
||||
has = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
/** The SSE spec: remove ONE leading space from the field value. */
|
||||
static String strip(String v) {
|
||||
return v.startsWith(" ") ? v.substring(1) : v;
|
||||
}
|
||||
|
||||
static String text(List<Event> events) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
events.stream().filter(e -> e.name().equals("token")).forEach(e -> sb.append(e.data()));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sseFramesAndTheLeadingSpaceRule() throws Exception {
|
||||
var t = new Transcript("01-sse-transcript.txt");
|
||||
var events = stream(browser(), "hello world");
|
||||
t.line("$ GET /chat/stream?message=hello world (raw lines, first 14 and last 5)");
|
||||
raw.stream().limit(14).forEach(t::line);
|
||||
t.line("...");
|
||||
raw.subList(raw.size() - 5, raw.size()).forEach(l -> t.line(l.isEmpty() ? "<blank>" : l));
|
||||
String joined = text(events);
|
||||
t.line("event names seen: " + events.stream().map(Event::name).distinct().toList());
|
||||
t.line("joined token text as EventSource delivers it: " + joined);
|
||||
// What the browser would show if the server had NOT put a space in front of every data line:
|
||||
String naive = events.stream().filter(e -> e.name().equals("token")).map(e -> strip(e.data()))
|
||||
.reduce("", String::concat);
|
||||
t.line("same text if the server did not pad: " + naive);
|
||||
assertThat(joined).isEqualTo("[stub] saw 2 messages. Earlier user turns: none. You said: hello world");
|
||||
assertThat(naive).doesNotContain(" ");
|
||||
assertThat(events.getLast().name()).isEqualTo("done");
|
||||
}
|
||||
|
||||
@Test
|
||||
void memoryFollowsTheHttpSession() throws Exception {
|
||||
var t = new Transcript("02-memory.txt");
|
||||
var a = browser();
|
||||
t.line("session A, turn 1: " + text(stream(a, "My name is Asha and I like tea")));
|
||||
t.line("session A, turn 2: " + text(stream(a, "What did I just tell you?")));
|
||||
var b = browser();
|
||||
String other = text(stream(b, "What did I just tell you?"));
|
||||
t.line("session B, turn 1: " + other);
|
||||
String third = text(stream(a, "And now?"));
|
||||
t.line("session A, turn 3: " + third);
|
||||
assertThat(third).contains("saw 6 messages").contains("My name is Asha and I like tea | What did I just tell you?");
|
||||
assertThat(other).contains("saw 2 messages").contains("Earlier user turns: none");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modelTextIsEscapedBeforeItReachesInnerHtml() throws Exception {
|
||||
var t = new Transcript("03-escaping.txt");
|
||||
var events = stream(browser(), "html please");
|
||||
String joined = text(events);
|
||||
t.line("data sent to the browser: " + joined);
|
||||
assertThat(joined).contains("<b>bold</b>").contains("<img src=x onerror=alert(1)>");
|
||||
assertThat(joined).doesNotContain("<b>").doesNotContain("<img");
|
||||
}
|
||||
|
||||
@Test
|
||||
void closingTheConnectionCancelsTheFluxAndTheUpstreamStream() throws Exception {
|
||||
var t = new Transcript("04-cancel-with-fix.txt");
|
||||
OpenAiStubServer.setDelayMs(50);
|
||||
long[] before = OpenAiStubServer.counters();
|
||||
int cancelledBefore = controller.cancelled.get();
|
||||
try (var socket = new java.net.Socket("localhost", port)) {
|
||||
socket.getOutputStream().write(("GET /chat/stream?turn=9&message=big%20400 HTTP/1.1\r\nHost: localhost\r\n\r\n")
|
||||
.getBytes(StandardCharsets.US_ASCII));
|
||||
socket.getInputStream().readNBytes(40_000);
|
||||
} // closing the socket is the "client abort"
|
||||
long t0 = System.currentTimeMillis();
|
||||
long[] after = before;
|
||||
for (int i = 0; i < 100 && after[3] == before[3]; i++) {
|
||||
Thread.sleep(50);
|
||||
after = OpenAiStubServer.counters();
|
||||
}
|
||||
long took = System.currentTimeMillis() - t0;
|
||||
OpenAiStubServer.setDelayMs(20);
|
||||
t.line("client read 40000 bytes then closed the socket");
|
||||
t.line("Flux cancelled on the server: " + (controller.cancelled.get() - cancelledBefore));
|
||||
t.line("stub saw the upstream connection close: " + (after[3] - before[3]) + " (within " + took + " ms)");
|
||||
t.line("stub chunks written for this request: " + (after[1] - before[1]) + " of 400 requested");
|
||||
assertThat(controller.cancelled.get() - cancelledBefore).isEqualTo(1);
|
||||
assertThat(after[3] - before[3]).isEqualTo(1);
|
||||
assertThat(after[1] - before[1]).isLessThan(400);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
org.springframework.ai.chat.client.ChatClient chatClient;
|
||||
|
||||
@Test
|
||||
void aMemoryAdvisorWithoutAConversationIdFails() {
|
||||
var t = new Transcript("11-missing-conversation-id.txt");
|
||||
try {
|
||||
chatClient.prompt().user("hi").stream().content().blockLast();
|
||||
t.line("no exception");
|
||||
} catch (RuntimeException e) {
|
||||
t.line("stream failed: " + e.getClass().getSimpleName() + ": " + e.getMessage());
|
||||
assertThat(e.getMessage()).contains("conversationId cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whatMemoryKeepsWhenTheClientCancelsMidAnswer() throws Exception {
|
||||
var t = new Transcript("12-memory-after-cancel.txt");
|
||||
OpenAiStubServer.setDelayMs(30);
|
||||
var client = browser();
|
||||
// turn 1: ask for 300 tokens over a raw socket that shares the session cookie, read a little, hang up
|
||||
var warm = HttpRequest.newBuilder(URI.create("http://localhost:" + port + "/chat/stream?message=warm")).build();
|
||||
client.send(warm, HttpResponse.BodyHandlers.discarding()); // sets JSESSIONID in the client's cookie jar
|
||||
String cookie = client.cookieHandler().map(h -> {
|
||||
try {
|
||||
return h.get(URI.create("http://localhost:" + port + "/"), java.util.Map.of()).get("Cookie").getFirst();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}).orElseThrow();
|
||||
try (var socket = new java.net.Socket("localhost", port)) {
|
||||
socket.getOutputStream().write(("GET /chat/stream?message=count%20300 HTTP/1.1\r\nHost: localhost\r\nCookie: "
|
||||
+ cookie + "\r\n\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
socket.getInputStream().readNBytes(600);
|
||||
}
|
||||
Thread.sleep(1500);
|
||||
String next = text(stream(client, "What do you remember?"));
|
||||
OpenAiStubServer.setDelayMs(20);
|
||||
t.line("turn 1 'warm' completed, turn 2 'count 300' cancelled after a few tokens, then turn 3:");
|
||||
t.line(next);
|
||||
assertThat(next).contains("Earlier user turns: warm | count 300");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.ankurm.chatui.stub.OpenAiStubServer;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* No Spring AI, no SDK: plain OkHttp 4.12 against the stub. A reader thread is blocked reading the
|
||||
* SSE body; another thread then closes the response, or cancels the call. Which one closes the socket?
|
||||
*/
|
||||
class OkHttpCloseReproTest {
|
||||
|
||||
@BeforeAll
|
||||
static void stub() {
|
||||
StubSupport.ensureStarted();
|
||||
}
|
||||
|
||||
int run(boolean cancelInsteadOfClose, Transcript t) throws Exception {
|
||||
var http = new OkHttpClient();
|
||||
int closed = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
long[] before = OpenAiStubServer.counters();
|
||||
var req = new Request.Builder().url("http://localhost:" + StubSupport.PORT + "/v1/chat/completions")
|
||||
.post(RequestBody.create("{\"model\":\"m\",\"stream\":true,\"messages\":[{\"role\":\"user\","
|
||||
+ "\"content\":\"count 60\"}]}", MediaType.get("application/json"))).build();
|
||||
var call = http.newCall(req);
|
||||
var response = call.execute();
|
||||
var source = response.body().source();
|
||||
var reader = new Thread(() -> {
|
||||
try {
|
||||
while (source.readUtf8Line() != null) {
|
||||
// keep reading
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// closed under us
|
||||
}
|
||||
});
|
||||
reader.start();
|
||||
Thread.sleep(150);
|
||||
try {
|
||||
if (cancelInsteadOfClose) {
|
||||
call.cancel();
|
||||
} else {
|
||||
response.close();
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
if (i == 0) {
|
||||
t.line(" response.close() threw: " + e);
|
||||
}
|
||||
}
|
||||
Thread.sleep(500);
|
||||
if (OpenAiStubServer.counters()[3] - before[3] == 1) {
|
||||
closed++;
|
||||
}
|
||||
reader.join(2000);
|
||||
}
|
||||
return closed;
|
||||
}
|
||||
|
||||
@Test
|
||||
void closeFromAnotherThreadLeaksCancelDoesNot() throws Exception {
|
||||
var t = new Transcript("05-okhttp-close-vs-cancel.txt");
|
||||
t.line("plain OkHttp 4.12.0, 10 runs each, stub streaming 60 chunks at 20 ms, reader thread blocked in a read");
|
||||
int viaClose = run(false, t);
|
||||
t.line("response.close() from another thread: upstream connection closed in " + viaClose + " of 10 runs");
|
||||
int viaCancel = run(true, t);
|
||||
t.line("call.cancel() from another thread: upstream connection closed in " + viaCancel + " of 10 runs");
|
||||
assertThat(viaCancel).isEqualTo(10);
|
||||
assertThat(viaClose).isLessThanOrEqualTo(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import com.ankurm.chatui.stub.OpenAiStubServer;
|
||||
import java.io.IOException;
|
||||
|
||||
/** Starts the in-process stub once per JVM on port 38193. */
|
||||
final class StubSupport {
|
||||
static final int PORT = 38193;
|
||||
private static boolean started;
|
||||
|
||||
static synchronized void ensureStarted() {
|
||||
if (!started) {
|
||||
try {
|
||||
OpenAiStubServer.start(PORT, 20);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
started = true;
|
||||
}
|
||||
}
|
||||
|
||||
private StubSupport() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.ankurm.chatui;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
/** Appends lines to output/NN-name.txt (the file is truncated by the first call of a JVM run). */
|
||||
final class Transcript {
|
||||
private final Path file;
|
||||
|
||||
Transcript(String name) {
|
||||
this.file = Path.of("output", name);
|
||||
try {
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.writeString(file, "");
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
void line(String s) {
|
||||
System.out.println(s);
|
||||
try {
|
||||
Files.writeString(file, s + "\n", StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user