forked from bernardladenthin/java-llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAiCompatServer.java
More file actions
1108 lines (1016 loc) · 48.3 KB
/
Copy pathOpenAiCompatServer.java
File metadata and controls
1108 lines (1016 loc) · 48.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com>
//
// SPDX-License-Identifier: MIT
package net.ladenthin.llama.server;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sun.net.httpserver.Filter;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import net.ladenthin.llama.LlamaModel;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An OpenAI-compatible HTTP endpoint over a loaded {@link LlamaModel}, built only on the JDK's
* {@code com.sun.net.httpserver.HttpServer} (no new runtime dependency). It is both embeddable and the
* {@code Main-Class} of the {@code -jar-with-dependencies} assembly.
*
* <p>Routes:
* <ul>
* <li>{@code POST /v1/chat/completions} — streaming (Server-Sent Events) and non-streaming chat
* completions, forwarded faithfully (messages/tools verbatim; streamed {@code delta.tool_calls}
* preserved).</li>
* <li>{@code POST /v1/completions} — text completion, streaming (Server-Sent Events, token by token
* via {@code generate(...)}) when {@code stream:true} and non-streaming otherwise.</li>
* <li>{@code POST /v1/embeddings} — embeddings (requires the model to be loaded in embedding
* mode).</li>
* <li>{@code GET /v1/models} — advertises the single configured model.</li>
* <li>{@code GET /metrics} — server and per-slot token/cache counters as JSON.</li>
* <li>{@code GET /slots} — the per-slot metrics array as JSON.</li>
* <li>{@code GET /health} — liveness probe returning {@code {"status":"ok"}} (no authentication).</li>
* </ul>
*
* <p>During streaming, the server emits SSE comment heartbeats on a timer so a long prompt prefill on
* CPU does not trip a client's stream-inactivity timeout before the first token. It binds to loopback by
* default and can require a bearer API key. The endpoint is a pass-through: tools are provided and
* executed by the client, not here.
*
* <p>Typical use:
* <pre>{@code
* try (LlamaModel model = new LlamaModel(new ModelParameters().setModel("models/model.gguf"));
* OpenAiCompatServer server = new OpenAiCompatServer(
* model, OpenAiServerConfig.builder().port(8080).modelId("local").build()).start()) {
* Thread.currentThread().join();
* }
* }</pre>
*/
public final class OpenAiCompatServer implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(OpenAiCompatServer.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/** The chat-completions route. */
public static final String PATH_CHAT_COMPLETIONS = "/v1/chat/completions";
/** The text-completions route. */
public static final String PATH_COMPLETIONS = "/v1/completions";
/** The embeddings route. */
public static final String PATH_EMBEDDINGS = "/v1/embeddings";
/** The rerank route (requires the model loaded in reranking mode). */
public static final String PATH_RERANK = "/v1/rerank";
/** The Anthropic Messages API route. */
public static final String PATH_MESSAGES = "/v1/messages";
/** The OpenAI Responses API route. */
public static final String PATH_RESPONSES = "/v1/responses";
/**
* The fill-in-the-middle (autocomplete) route. Deliberately the llama.cpp-native bare path (no
* {@code /v1}) so ghost-text clients such as llama.vscode and Tabby reach it unchanged.
*/
public static final String PATH_INFILL = "/infill";
/** The model-list route. */
public static final String PATH_MODELS = "/v1/models";
/** The liveness-probe route. */
public static final String PATH_HEALTH = "/health";
/** The llama.cpp-native server-properties route (context length + modalities). */
public static final String PATH_PROPS = "/props";
/** llama.cpp server metrics as JSON, including per-slot token/cache counters. */
public static final String PATH_METRICS = "/metrics";
/** llama.cpp slot state array as JSON. */
public static final String PATH_SLOTS = "/slots";
/** Ollama-native discovery route (version). */
public static final String PATH_OLLAMA_VERSION = "/api/version";
/** Ollama-native discovery route (model list). */
public static final String PATH_OLLAMA_TAGS = "/api/tags";
/** Ollama-native discovery route (model capabilities). */
public static final String PATH_OLLAMA_SHOW = "/api/show";
/** Ollama-native chat route. */
public static final String PATH_OLLAMA_CHAT = "/api/chat";
/** Ollama-native generate route (prompt completion / fill-in-the-middle). */
public static final String PATH_OLLAMA_GENERATE = "/api/generate";
private static final String CONTENT_TYPE_NDJSON = "application/x-ndjson";
private static final int HTTP_OK = 200;
private static final int HTTP_BAD_REQUEST = 400;
private static final int HTTP_UNAUTHORIZED = 401;
private static final int HTTP_NOT_FOUND = 404;
private static final int HTTP_METHOD_NOT_ALLOWED = 405;
private static final int HTTP_SERVER_ERROR = 500;
private static final int HTTP_PAYLOAD_TOO_LARGE = 413;
private static final String CONTENT_TYPE_JSON = "application/json; charset=utf-8";
private static final String CONTENT_TYPE_SSE = "text/event-stream; charset=utf-8";
private static final String BEARER_PREFIX = "Bearer ";
private static final String ERROR_TYPE_REQUEST = "invalid_request_error";
private static final String ERROR_TYPE_SERVER = "server_error";
private static final String HEALTH_BODY = "{\"status\":\"ok\"}";
private final OpenAiServerConfig config;
private final OpenAiBackend backend;
private final HttpServer http;
private final Filter corsFilter;
private final ExecutorService requestExecutor;
private final ScheduledExecutorService heartbeatExecutor;
/**
* Create a server backed by a loaded model.
*
* @param model the model to serve completions from (owned by the caller; not closed by the server)
* @param config the server configuration
* @throws IOException if the listening socket cannot be bound
*/
public OpenAiCompatServer(LlamaModel model, OpenAiServerConfig config) throws IOException {
this(new LlamaModelBackend(model, new OpenAiRequestMapper()), config);
}
/**
* Create a server backed by an arbitrary {@link OpenAiBackend}. Used by tests to drive the full HTTP
* surface without a native library or model.
*
* @param backend the inference engine seam
* @param config the server configuration
* @throws IOException if the listening socket cannot be bound
*/
OpenAiCompatServer(OpenAiBackend backend, OpenAiServerConfig config) throws IOException {
this.config = config;
this.backend = backend;
this.requestExecutor = Executors.newCachedThreadPool(namedFactory("jllama-openai-http"));
// Sized for concurrency: a heartbeat write blocks on a slow/stalled client, so a single
// shared thread would let one such client starve every other stream's keep-alives. Bound
// the impact to roughly one stalled client per thread.
this.heartbeatExecutor = Executors.newScheduledThreadPool(
Math.max(2, Runtime.getRuntime().availableProcessors()), namedFactory("jllama-openai-hb"));
this.http = HttpServer.create(new InetSocketAddress(config.getHost(), config.getPort()), 0);
this.corsFilter = buildCorsFilter(config.getCorsAllowOrigin());
register("/", this::handleNotFound);
register(PATH_HEALTH, this::handleHealth);
register(PATH_PROPS, this::handleProps);
register(PATH_METRICS, this::handleMetrics);
register(PATH_SLOTS, this::handleSlots);
// Each route is registered under its canonical path and a bare alias (clients disagree on
// whether to include the /v1 prefix), so both forms resolve to the same handler.
register(PATH_MODELS, this::handleModels);
register("/models", this::handleModels);
register(PATH_CHAT_COMPLETIONS, this::handleChatCompletions);
register("/chat/completions", this::handleChatCompletions);
register(PATH_COMPLETIONS, this::handleCompletions);
register("/completions", this::handleCompletions);
register(PATH_EMBEDDINGS, this::handleEmbeddings);
register("/embeddings", this::handleEmbeddings);
register(PATH_RERANK, this::handleRerank);
register("/rerank", this::handleRerank);
register("/reranking", this::handleRerank);
register(PATH_INFILL, this::handleInfill);
register("/v1/infill", this::handleInfill);
register(PATH_MESSAGES, this::handleAnthropicMessages);
register("/messages", this::handleAnthropicMessages);
register(PATH_RESPONSES, this::handleResponses);
register("/responses", this::handleResponses);
// Ollama-native surface (Copilot's built-in Ollama provider + Ollama-hardcoded tools).
register(PATH_OLLAMA_VERSION, this::handleOllamaVersion);
register(PATH_OLLAMA_TAGS, this::handleOllamaTags);
register(PATH_OLLAMA_SHOW, this::handleOllamaShow);
register(PATH_OLLAMA_CHAT, this::handleOllamaChat);
register(PATH_OLLAMA_GENERATE, this::handleOllamaGenerate);
http.setExecutor(requestExecutor);
}
/**
* Start accepting connections.
*
* @return this server, for chaining
*/
public OpenAiCompatServer start() {
http.start();
LOG.info("OpenAI-compatible server listening on http://{}:{}", config.getHost(), getPort());
return this;
}
/**
* The actual bound port (useful when configured with port {@code 0} for an ephemeral port).
*
* @return the port the server is listening on
*/
public int getPort() {
return http.getAddress().getPort();
}
/** Stop the server and release its thread pools. The backing model is not closed. */
@Override
public void close() {
http.stop(0);
requestExecutor.shutdownNow();
heartbeatExecutor.shutdownNow();
}
/**
* Register {@code handler} for {@code path} with the CORS filter attached. Centralised so the
* cross-cutting CORS/preflight wiring applies uniformly to every route (including the catch-all).
*/
private void register(String path, HttpHandler handler) {
http.createContext(path, handler).getFilters().add(corsFilter);
}
/**
* Build a CORS filter that stamps {@code Access-Control-Allow-Origin} on every response and answers
* {@code OPTIONS} preflights with {@code 204} + the allowed methods/headers — so browser- and
* webview-based clients (which preflight an {@code Authorization} header) are not blocked.
*/
private static Filter buildCorsFilter(String allowOrigin) {
return new Filter() {
@Override
public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
exchange.getResponseHeaders().set("Access-Control-Allow-Origin", allowOrigin);
if ("OPTIONS".equalsIgnoreCase(exchange.getRequestMethod())) {
exchange.getResponseHeaders().set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
exchange.getResponseHeaders().set("Access-Control-Allow-Headers", "Content-Type, Authorization");
exchange.getResponseHeaders().set("Access-Control-Max-Age", "86400");
exchange.sendResponseHeaders(204, -1);
exchange.close();
return;
}
chain.doFilter(exchange);
}
@Override
public String description() {
return "CORS preflight + Access-Control-Allow-Origin";
}
};
}
// ----- handlers -----
private void handleChatCompletions(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request == null) {
return;
}
JsonNode messages = request.path("messages");
if (!messages.isArray() || messages.size() == 0) {
sendError(exchange, HTTP_BAD_REQUEST, ERROR_TYPE_REQUEST, "'messages' must be a non-empty array");
return;
}
if (request.path("stream").asBoolean(false)) {
streamChat(exchange, request);
} else {
completeNonStreaming(exchange, request, backend::complete);
}
} finally {
exchange.close();
}
}
private void handleCompletions(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request != null) {
if (request.path("stream").asBoolean(false)) {
streamCompletions(exchange, request);
} else {
completeNonStreaming(exchange, request, backend::completions);
}
}
} finally {
exchange.close();
}
}
private void streamCompletions(HttpExchange exchange, JsonNode request) throws IOException {
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_SSE);
exchange.getResponseHeaders().set("Cache-Control", "no-cache");
exchange.sendResponseHeaders(HTTP_OK, 0);
try (ResponseStream out = new ResponseStream(exchange.getResponseBody())) {
ScheduledFuture<?> heartbeat = null;
try {
heartbeat = heartbeatExecutor.scheduleAtFixedRate(
() -> out.writeQuietly(OpenAiSseFormatter.heartbeat()),
config.getHeartbeatMillis(),
config.getHeartbeatMillis(),
TimeUnit.MILLISECONDS);
backend.streamCompletions(request, chunkJson -> out.writeStrict(OpenAiSseFormatter.sseData(chunkJson)));
out.writeStrict(OpenAiSseFormatter.sseDone());
} catch (IllegalArgumentException e) {
out.writeQuietly(
OpenAiSseFormatter.sseData(OpenAiSseFormatter.errorJson(message(e), ERROR_TYPE_REQUEST, null)));
} catch (IOException e) {
LOG.debug("client disconnected during stream", e);
} catch (RuntimeException e) {
LOG.warn("streaming completion failed", e);
out.writeQuietly(
OpenAiSseFormatter.sseData(OpenAiSseFormatter.errorJson(message(e), ERROR_TYPE_SERVER, null)));
} finally {
if (heartbeat != null) {
heartbeat.cancel(false);
}
}
}
}
private void handleEmbeddings(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request != null) {
completeNonStreaming(exchange, request, backend::embeddings);
}
} finally {
exchange.close();
}
}
private void handleInfill(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request != null) {
completeNonStreaming(exchange, request, backend::infill);
}
} finally {
exchange.close();
}
}
private void handleRerank(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request != null) {
completeNonStreaming(exchange, request, backend::rerank);
}
} finally {
exchange.close();
}
}
/**
* Run a non-streaming request through {@code producer} and write its JSON body, translating an
* {@link IllegalArgumentException} to {@code 400} and any other failure to {@code 500}.
*/
private void completeNonStreaming(HttpExchange exchange, JsonNode request, BodyProducer producer)
throws IOException {
final String body;
try {
body = producer.produce(request);
} catch (IllegalArgumentException e) {
sendError(exchange, HTTP_BAD_REQUEST, ERROR_TYPE_REQUEST, message(e));
return;
} catch (IOException | RuntimeException e) {
LOG.warn("request failed", e);
sendError(exchange, HTTP_SERVER_ERROR, ERROR_TYPE_SERVER, message(e));
return;
}
sendJson(exchange, HTTP_OK, body);
}
private void streamChat(HttpExchange exchange, JsonNode request) throws IOException {
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_SSE);
exchange.getResponseHeaders().set("Cache-Control", "no-cache");
exchange.sendResponseHeaders(HTTP_OK, 0);
try (ResponseStream out = new ResponseStream(exchange.getResponseBody())) {
ScheduledFuture<?> heartbeat = null;
try {
heartbeat = heartbeatExecutor.scheduleAtFixedRate(
() -> out.writeQuietly(OpenAiSseFormatter.heartbeat()),
config.getHeartbeatMillis(),
config.getHeartbeatMillis(),
TimeUnit.MILLISECONDS);
backend.stream(
request,
chunkJson -> out.writeStrict(
OpenAiSseFormatter.sseData(OpenAiSseFormatter.ensureUsageCachedTokens(chunkJson))));
out.writeStrict(OpenAiSseFormatter.sseDone());
} catch (IllegalArgumentException e) {
out.writeQuietly(
OpenAiSseFormatter.sseData(OpenAiSseFormatter.errorJson(message(e), ERROR_TYPE_REQUEST, null)));
} catch (IOException e) {
LOG.debug("client disconnected during stream", e);
} catch (RuntimeException e) {
LOG.warn("streaming chat completion failed", e);
out.writeQuietly(
OpenAiSseFormatter.sseData(OpenAiSseFormatter.errorJson(message(e), ERROR_TYPE_SERVER, null)));
} finally {
// try-with-resources closes the stream (under its lock) after the heartbeat is cancelled,
// so the close never races a still-in-flight heartbeat write.
if (heartbeat != null) {
heartbeat.cancel(false);
}
}
}
}
// ----- Ollama-native surface -----
private void handleOllamaVersion(HttpExchange exchange) throws IOException {
try {
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only GET is supported");
return;
}
sendJson(exchange, HTTP_OK, OllamaApiSupport.versionJson());
} finally {
exchange.close();
}
}
private void handleOllamaTags(HttpExchange exchange) throws IOException {
try {
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only GET is supported");
return;
}
sendJson(exchange, HTTP_OK, OllamaApiSupport.tagsJson(config.getModelId()));
} finally {
exchange.close();
}
}
private void handleOllamaShow(HttpExchange exchange) throws IOException {
try {
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only POST is supported");
return;
}
// The request body (optionally {"model":...}) is ignored: this server serves one model.
int contextLength = config.getMaxInputTokens() + config.getMaxOutputTokens();
sendJson(
exchange,
HTTP_OK,
OllamaApiSupport.showJson(config.getModelId(), contextLength, config.isSupportsVision()));
} finally {
exchange.close();
}
}
private void handleOllamaChat(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request == null) {
return;
}
JsonNode openAiRequest = OllamaApiSupport.toOpenAiChatRequest(request);
String model = request.path("model").asText(config.getModelId());
if (OllamaApiSupport.isStreaming(request)) {
streamOllamaChat(exchange, openAiRequest, model);
} else {
final String body;
try {
body = backend.complete(openAiRequest);
} catch (IllegalArgumentException e) {
sendJson(exchange, HTTP_BAD_REQUEST, ollamaError(message(e)));
return;
} catch (IOException | RuntimeException e) {
LOG.warn("ollama chat failed", e);
sendJson(exchange, HTTP_SERVER_ERROR, ollamaError(message(e)));
return;
}
sendJson(exchange, HTTP_OK, OllamaApiSupport.toOllamaChatResponse(body, model));
}
} finally {
exchange.close();
}
}
/** Stream an Ollama {@code /api/chat} response as newline-delimited JSON, ending with a done line. */
private void streamOllamaChat(HttpExchange exchange, JsonNode openAiRequest, String model) throws IOException {
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_NDJSON);
exchange.getResponseHeaders().set("Cache-Control", "no-cache");
exchange.sendResponseHeaders(HTTP_OK, 0);
final ToolCallDeltaAccumulator accumulator = new ToolCallDeltaAccumulator();
try (ResponseStream out = new ResponseStream(exchange.getResponseBody())) {
try {
backend.stream(openAiRequest, chunkJson -> {
accumulator.accept(chunkJson);
String line = OllamaApiSupport.toOllamaContentLine(chunkJson, model);
if (line != null) {
out.writeStrict(line);
}
});
out.writeStrict(OllamaApiSupport.toOllamaDoneLine(model, accumulator));
} catch (IllegalArgumentException e) {
out.writeQuietly(ollamaError(message(e)) + "\n");
} catch (IOException e) {
LOG.debug("ollama client disconnected during stream", e);
} catch (RuntimeException e) {
LOG.warn("ollama streaming chat failed", e);
out.writeQuietly(ollamaError(message(e)) + "\n");
}
}
}
private void handleOllamaGenerate(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request == null) {
return;
}
String model = request.path("model").asText(config.getModelId());
// Generation runs to completion first (there is no streaming raw-completion path), then the
// text is wrapped — as a single NDJSON content line + done line when stream is requested.
final String text;
try {
if (OllamaApiSupport.hasSuffix(request)) {
text = OllamaApiSupport.extractInfillContent(
backend.infill(OllamaApiSupport.toInfillRequest(request)));
} else {
text = OllamaApiSupport.extractCompletionText(
backend.completions(OllamaApiSupport.toOpenAiCompletionRequest(request)));
}
} catch (IllegalArgumentException e) {
sendJson(exchange, HTTP_BAD_REQUEST, ollamaError(message(e)));
return;
} catch (IOException | RuntimeException e) {
LOG.warn("ollama generate failed", e);
sendJson(exchange, HTTP_SERVER_ERROR, ollamaError(message(e)));
return;
}
if (OllamaApiSupport.isStreaming(request)) {
byte[] bytes =
OllamaApiSupport.toOllamaGenerateStream(text, model).getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_NDJSON);
exchange.sendResponseHeaders(HTTP_OK, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
} else {
sendJson(exchange, HTTP_OK, OllamaApiSupport.toOllamaGenerateResponse(text, model));
}
} finally {
exchange.close();
}
}
private static String ollamaError(String message) {
return OBJECT_MAPPER.createObjectNode().put("error", message).toString();
}
// ----- Anthropic Messages API -----
private void handleAnthropicMessages(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request == null) {
return;
}
JsonNode openAiRequest = AnthropicApiSupport.toOpenAiChatRequest(request);
String model = request.path("model").asText(config.getModelId());
if (AnthropicApiSupport.isStreaming(request)) {
streamAnthropic(exchange, openAiRequest, model);
} else {
final String body;
try {
body = backend.complete(openAiRequest);
} catch (IllegalArgumentException e) {
sendJson(exchange, HTTP_BAD_REQUEST, anthropicError(message(e)));
return;
} catch (IOException | RuntimeException e) {
LOG.warn("anthropic messages failed", e);
sendJson(exchange, HTTP_SERVER_ERROR, anthropicError(message(e)));
return;
}
sendJson(exchange, HTTP_OK, AnthropicApiSupport.toAnthropicResponse(body, model));
}
} finally {
exchange.close();
}
}
/** Stream an Anthropic {@code /v1/messages} response as the Anthropic SSE event sequence. */
private void streamAnthropic(HttpExchange exchange, JsonNode openAiRequest, String model) throws IOException {
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_SSE);
exchange.getResponseHeaders().set("Cache-Control", "no-cache");
exchange.sendResponseHeaders(HTTP_OK, 0);
final AnthropicStreamTranslator translator =
new AnthropicStreamTranslator("msg_" + Long.toHexString(System.nanoTime()), model);
try (ResponseStream out = new ResponseStream(exchange.getResponseBody())) {
ScheduledFuture<?> heartbeat = null;
try {
heartbeat = heartbeatExecutor.scheduleAtFixedRate(
() -> out.writeQuietly(OpenAiSseFormatter.heartbeat()),
config.getHeartbeatMillis(),
config.getHeartbeatMillis(),
TimeUnit.MILLISECONDS);
out.writeStrict(translator.begin());
backend.stream(withUsageChunk(openAiRequest), chunkJson -> {
String events = translator.onChunk(chunkJson);
if (!events.isEmpty()) {
out.writeStrict(events);
}
});
out.writeStrict(translator.end());
} catch (IllegalArgumentException e) {
out.writeQuietly(AnthropicApiSupport.sseEvent("error", anthropicError(message(e))));
} catch (IOException e) {
LOG.debug("anthropic client disconnected during stream", e);
} catch (RuntimeException e) {
LOG.warn("anthropic streaming failed", e);
out.writeQuietly(AnthropicApiSupport.sseEvent("error", anthropicError(message(e))));
} finally {
if (heartbeat != null) {
heartbeat.cancel(false);
}
}
}
}
/** Ensure protocol translators receive the native stream's trailing usage chunk. */
private static JsonNode withUsageChunk(JsonNode request) {
ObjectNode copy = request.deepCopy();
JsonNode existing = copy.path("stream_options");
ObjectNode streamOptions = existing.isObject() ? (ObjectNode) existing : copy.putObject("stream_options");
streamOptions.put("include_usage", true);
return copy;
}
private static String anthropicError(String message) {
ObjectNode root = OBJECT_MAPPER.createObjectNode();
root.put("type", "error");
ObjectNode error = root.putObject("error");
error.put("type", "invalid_request_error");
error.put("message", message);
return root.toString();
}
// ----- OpenAI Responses API -----
private void handleResponses(HttpExchange exchange) throws IOException {
try {
JsonNode request = requirePostJson(exchange);
if (request == null) {
return;
}
JsonNode openAiRequest = ResponsesApiSupport.toOpenAiChatRequest(request);
String model = request.path("model").asText(config.getModelId());
String responseId = "resp_" + Long.toHexString(System.nanoTime());
if (ResponsesApiSupport.isStreaming(request)) {
streamResponses(exchange, openAiRequest, model, responseId);
} else {
final String body;
try {
body = backend.complete(openAiRequest);
} catch (IllegalArgumentException e) {
sendError(exchange, HTTP_BAD_REQUEST, ERROR_TYPE_REQUEST, message(e));
return;
} catch (IOException | RuntimeException e) {
LOG.warn("responses failed", e);
sendError(exchange, HTTP_SERVER_ERROR, ERROR_TYPE_SERVER, message(e));
return;
}
sendJson(exchange, HTTP_OK, ResponsesApiSupport.toResponsesResponse(body, model, responseId));
}
} finally {
exchange.close();
}
}
/** Stream a Responses {@code /v1/responses} reply as the Responses SSE event sequence. */
private void streamResponses(HttpExchange exchange, JsonNode openAiRequest, String model, String responseId)
throws IOException {
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_SSE);
exchange.getResponseHeaders().set("Cache-Control", "no-cache");
exchange.sendResponseHeaders(HTTP_OK, 0);
final ResponsesStreamTranslator translator = new ResponsesStreamTranslator(model, responseId);
try (ResponseStream out = new ResponseStream(exchange.getResponseBody())) {
ScheduledFuture<?> heartbeat = null;
try {
heartbeat = heartbeatExecutor.scheduleAtFixedRate(
() -> out.writeQuietly(OpenAiSseFormatter.heartbeat()),
config.getHeartbeatMillis(),
config.getHeartbeatMillis(),
TimeUnit.MILLISECONDS);
out.writeStrict(translator.begin());
backend.stream(withUsageChunk(openAiRequest), chunkJson -> {
String events = translator.onChunk(chunkJson);
if (!events.isEmpty()) {
out.writeStrict(events);
}
});
out.writeStrict(translator.end());
} catch (IllegalArgumentException e) {
out.writeQuietly("event: error\ndata: "
+ OpenAiSseFormatter.errorJson(message(e), ERROR_TYPE_REQUEST, null) + "\n\n");
} catch (IOException e) {
LOG.debug("responses client disconnected during stream", e);
} catch (RuntimeException e) {
LOG.warn("responses streaming failed", e);
out.writeQuietly("event: error\ndata: "
+ OpenAiSseFormatter.errorJson(message(e), ERROR_TYPE_SERVER, null) + "\n\n");
} finally {
if (heartbeat != null) {
heartbeat.cancel(false);
}
}
}
}
private void handleModels(HttpExchange exchange) throws IOException {
try {
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only GET is supported");
return;
}
if (!authorized(exchange)) {
sendError(exchange, HTTP_UNAUTHORIZED, ERROR_TYPE_REQUEST, "Missing or invalid API key");
return;
}
sendJson(exchange, HTTP_OK, OpenAiSseFormatter.modelsJson(config.getModelId()));
} finally {
exchange.close();
}
}
private void handleHealth(HttpExchange exchange) throws IOException {
try {
// Liveness probe: deliberately unauthenticated so orchestrators can poll it without a key.
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only GET is supported");
return;
}
sendJson(exchange, HTTP_OK, HEALTH_BODY);
} finally {
exchange.close();
}
}
private void handleMetrics(HttpExchange exchange) throws IOException {
handleMetricsView(exchange, false);
}
private void handleSlots(HttpExchange exchange) throws IOException {
handleMetricsView(exchange, true);
}
private void handleMetricsView(HttpExchange exchange, boolean slotsOnly) throws IOException {
try {
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only GET is supported");
return;
}
if (!authorized(exchange)) {
sendError(exchange, HTTP_UNAUTHORIZED, ERROR_TYPE_REQUEST, "Missing or invalid API key");
return;
}
String metrics = backend.metrics();
if (slotsOnly) {
metrics = OBJECT_MAPPER.readTree(metrics).path("slots").toString();
}
sendJson(exchange, HTTP_OK, metrics);
} catch (IOException | RuntimeException e) {
LOG.warn("metrics request failed", e);
sendError(exchange, HTTP_SERVER_ERROR, ERROR_TYPE_SERVER, message(e));
} finally {
exchange.close();
}
}
private void handleProps(HttpExchange exchange) throws IOException {
// Deliberately unauthenticated, like the Ollama discovery routes (/api/version, /api/tags,
// /api/show): autocomplete/agent clients read context length + capabilities here before they
// have (or to discover whether they need) a key. It exposes only public model metadata —
// no inference, no secrets — so it intentionally bypasses authorized().
try {
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only GET is supported");
return;
}
int contextLength = config.getMaxInputTokens() + config.getMaxOutputTokens();
sendJson(
exchange,
HTTP_OK,
OpenAiSseFormatter.propsJson(config.getModelId(), contextLength, config.isSupportsVision()));
} finally {
exchange.close();
}
}
private void handleNotFound(HttpExchange exchange) throws IOException {
try {
sendError(exchange, HTTP_NOT_FOUND, ERROR_TYPE_REQUEST, "Not found: " + exchange.getRequestURI());
} finally {
exchange.close();
}
}
// ----- helpers -----
/**
* Shared preamble for the {@code POST} JSON routes: enforce the method, authentication and a JSON
* object body, sending the matching error and returning {@code null} when any precondition fails.
*/
private @Nullable JsonNode requirePostJson(HttpExchange exchange) throws IOException {
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
sendError(exchange, HTTP_METHOD_NOT_ALLOWED, ERROR_TYPE_REQUEST, "Only POST is supported");
return null;
}
if (!authorized(exchange)) {
sendError(exchange, HTTP_UNAUTHORIZED, ERROR_TYPE_REQUEST, "Missing or invalid API key");
return null;
}
String contentLength = exchange.getRequestHeaders().getFirst("Content-Length");
if (contentLength != null) {
try {
if (Long.parseLong(contentLength.trim()) > config.getMaxRequestBodyBytes()) {
sendError(exchange, HTTP_PAYLOAD_TOO_LARGE, ERROR_TYPE_REQUEST, "Request body too large");
return null;
}
} catch (NumberFormatException ignored) {
// Unparseable header — the bounded read below still caps the body.
}
}
JsonNode request;
try {
request = readBody(exchange);
} catch (RequestBodyTooLargeException e) {
sendError(exchange, HTTP_PAYLOAD_TOO_LARGE, ERROR_TYPE_REQUEST, "Request body too large");
return null;
}
if (request == null || !request.isObject()) {
sendError(exchange, HTTP_BAD_REQUEST, ERROR_TYPE_REQUEST, "Request body must be a JSON object");
return null;
}
return request;
}
private boolean authorized(HttpExchange exchange) {
if (!config.isAuthenticationEnabled()) {
return true;
}
String expected = config.getApiKey();
if (expected == null) {
return true;
}
String header = exchange.getRequestHeaders().getFirst("Authorization");
if (header == null || !header.startsWith(BEARER_PREFIX)) {
return false;
}
// Constant-time comparison so response timing does not leak how many leading bytes
// of the bearer token matched.
byte[] expectedBytes = expected.getBytes(StandardCharsets.UTF_8);
byte[] presentedBytes = header.substring(BEARER_PREFIX.length()).getBytes(StandardCharsets.UTF_8);
return MessageDigest.isEqual(expectedBytes, presentedBytes);
}
private @Nullable JsonNode readBody(HttpExchange exchange) throws IOException {
try (InputStream is = new BoundedInputStream(exchange.getRequestBody(), config.getMaxRequestBodyBytes())) {
return OBJECT_MAPPER.readTree(is);
} catch (JsonProcessingException e) {
LOG.debug("malformed request body", e);
return null;
}
}
/** Signals that the request body exceeded the configured cap. */
private static final class RequestBodyTooLargeException extends IOException {
private static final long serialVersionUID = 1L;
RequestBodyTooLargeException(long limit) {
super("Request body exceeds " + limit + " bytes");
}
}
/** Wraps a stream so reading past {@code limit} bytes throws instead of buffering unboundedly. */
private static final class BoundedInputStream extends FilterInputStream {
private final long limit;
private long count;
BoundedInputStream(InputStream in, long limit) {
super(in);
this.limit = limit;
}
@Override
public int read() throws IOException {
int b = super.read();
if (b != -1 && ++count > limit) {
throw new RequestBodyTooLargeException(limit);
}
return b;
}
@Override
public int read(byte[] buffer, int off, int len) throws IOException {
int n = super.read(buffer, off, len);
if (n > 0) {
count += n;
if (count > limit) {
throw new RequestBodyTooLargeException(limit);
}
}
return n;
}
}
private void sendJson(HttpExchange exchange, int status, String json) throws IOException {
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_JSON);
exchange.sendResponseHeaders(status, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
}
private void sendError(HttpExchange exchange, int status, String type, String message) throws IOException {
sendJson(exchange, status, OpenAiSseFormatter.errorJson(message, type, null));
}
/**
* Per-request, thread-safe wrapper over a streaming HTTP response body. Every write and the close are
* serialized on a {@code private final} lock, so the generation thread and the heartbeat-timer task
* never write to (or close) the same stream concurrently. The lock is owned by this per-request
* instance rather than shared, so independent concurrent streams never serialize against each other.
* It is {@link AutoCloseable} so callers drive it with try-with-resources, which closes the stream
* (under the lock) on every exit path.
*/
private static final class ResponseStream implements AutoCloseable {
private final OutputStream os;
private final Object lock = new Object();
ResponseStream(OutputStream os) {
this.os = os;
}
/** Write under the lock, propagating failures so a streaming generation can be cancelled. */
void writeStrict(String text) throws IOException {
synchronized (lock) {
os.write(text.getBytes(StandardCharsets.UTF_8));
os.flush();
}
}
/** Write under the lock, swallowing failures (used for heartbeats and best-effort events). */
void writeQuietly(String text) {
synchronized (lock) {
try {
os.write(text.getBytes(StandardCharsets.UTF_8));
os.flush();
} catch (IOException e) {
LOG.trace("stream write failed (client likely disconnected)", e);
}
}
}
@Override
public void close() {
synchronized (lock) {
try {
os.close();
} catch (IOException e) {
LOG.trace("stream close failed", e);
}
}
}
}
private static String message(Throwable t) {