Repository navigation
Expand file tree
/
Copy pathserver.cpp
More file actions
1554 lines (1416 loc) · 72 KB
/
Copy pathserver.cpp
File metadata and controls
1554 lines (1416 loc) · 72 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
/**
* HTTP server for Ragger Memory implementation
*/
#include "build_info.h"
#include "server.h"
#include "memory.h"
#include "user_store.h"
#include "sqlite/backend.h"
#include "lang.h"
#include "Logger.h"
#include "auth.h"
#include "config.h"
#include "config_access.h"
#include "embedder.h"
#include "util/fs.h"
#include "inference.h"
#include <curl/curl.h>
#include "summarizer.h"
#include "summarizer_service.h"
#include "nlohmann_json.hpp"
#include "httplib.h"
// Dashboard HTML/JS, embedded at build time (web/dashboard.html -> generated
// dashboard_html.inc). Provides `static const char* DASHBOARD_HTML`.
#include "dashboard_html.inc"
#include <chrono>
#include <condition_variable>
#include <filesystem>
#include <fstream>
#include <set>
#include <mutex>
#include <iostream>
#include <optional>
#include <pwd.h>
#include <format>
#include <sstream>
#include <thread>
#include <unordered_map>
#include <atomic>
#include <signal.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
namespace ragger {
namespace fs = std::filesystem;
using json = nlohmann::json;
// Global signal flags (async-signal-safe)
static std::atomic<bool> g_housekeeping_requested{false};
static std::atomic<bool> g_config_reload_requested{false};
static std::atomic<bool> g_shutdown_requested{false};
// Set by POST /restart: like shutdown, but main() re-execs the daemon in
// place afterward (rebinding listeners — the only way a port change takes).
static std::atomic<bool> g_restart_requested{false};
static void sigusr1_handler(int) {
g_housekeeping_requested.store(true, std::memory_order_relaxed);
}
static void sighup_handler(int) {
g_config_reload_requested.store(true, std::memory_order_relaxed);
}
static void sigterm_handler(int) {
g_shutdown_requested.store(true, std::memory_order_relaxed);
}
// Serializes a json body with the "application/json" content-type and an
// optional non-200 status, replacing the repeated
// res.set_content(json{...}.dump(), "application/json") pattern used
// throughout this file's handlers.
static void respond_json(httplib::Response& res, const json& body, int status = 200) {
res.status = status;
res.set_content(body.dump(), "application/json");
}
// Serialise a vector of SearchResults to a JSON array.
// score is always included (0.0 for metadata-only searches).
static json search_results_to_json(const std::vector<SearchResult>& results) {
json arr = json::array();
for (const auto& r : results)
arr.push_back({{"id", r.id}, {"text", r.text}, {"score", r.score},
{"metadata", r.metadata}, {"timestamp", r.timestamp}});
return arr;
}
struct Server::Impl {
// Two listener instances so we can bind AF_UNIX and AF_INET at the same
// time. cpp-httplib::Server::listen() blocks the calling thread and binds
// exactly one socket, so each listener gets its own Server and its own
// thread. Routes are registered on both via setup_routes(httplib::Server&).
//
// tcp_svr is a pointer because it's either a plain httplib::Server or an
// httplib::SSLServer, decided once at construction time from [server]
// cert/key. httplib::SSLServer derives from Server (virtual dtor), so a
// base-class pointer works for both; setup_routes()/listen()/stop() are
// all virtual or take the base type.
httplib::Server unix_svr;
std::unique_ptr<httplib::Server> tcp_svr;
bool tls_enabled_ = false;
RaggerMemory& memory;
UserStore users_;
std::string host;
int port;
std::string server_token_;
std::optional<UserInfo> default_user_;
std::unique_ptr<InferenceClient> inference_;
std::unique_ptr<SummarizerService> summarizer_;
// Per-user memory cache (username → RaggerMemory)
std::unordered_map<std::string, std::unique_ptr<RaggerMemory>> user_memories_;
std::mutex user_memories_mutex_;
// SSE activity-log tailing: byte offset already streamed to /events.
std::uintmax_t activity_log_offset_ = 0;
// Housekeeping timer
std::atomic<bool> timer_running_{false};
std::thread timer_thread_;
std::string pid_file_;
Impl(RaggerMemory& mem, const std::string& h, int p)
: memory(mem), users_(config().resolved_db_path()), host(h), port(p)
{
bootstrap_auth();
init_inference();
tcp_svr = make_tcp_server();
if (!config().server_name.empty()) {
httplib::Headers server_header{{"Server", config().server_name}};
unix_svr.set_default_headers(server_header);
tcp_svr->set_default_headers(server_header);
}
setup_routes(unix_svr);
setup_routes(*tcp_svr);
warmup();
}
/// Build the TCP listener — plain httplib::Server, or an SSLServer when
/// [server] cert + key are both configured and load successfully. Any
/// TLS problem (partial config, unloadable cert/key, construction
/// failure) is logged as a warning (stderr + activity.log) and the
/// daemon falls back to plain HTTP rather than refusing to start — a
/// working insecure daemon beats a daemon that won't come up at all.
std::unique_ptr<httplib::Server> make_tcp_server() {
const auto& cfg = config();
const bool have_cert = !cfg.tls_cert.empty();
const bool have_key = !cfg.tls_key.empty();
if (have_cert != have_key) {
// Only one of cert/key set — misconfiguration. Warn and fall
// back to plain HTTP; enforcing TLS on a half-configured pair
// isn't possible.
std::string msg = std::format(lang::WARN_TLS_PARTIAL_CONFIG,
have_cert ? "cert" : "key");
Diskerror::Logger::warn(msg);
std::cerr << msg << "\n";
return std::make_unique<httplib::Server>();
}
if (!have_cert && !have_key) {
return std::make_unique<httplib::Server>();
}
try {
auto svr = std::make_unique<httplib::SSLServer>(
cfg.tls_cert.c_str(), cfg.tls_key.c_str());
if (!svr->is_valid()) {
std::string msg = std::format(lang::WARN_TLS_INVALID_CERT,
cfg.tls_cert, cfg.tls_key);
Diskerror::Logger::warn(msg);
std::cerr << msg << "\n";
return std::make_unique<httplib::Server>();
}
tls_enabled_ = true;
Diskerror::Logger::info(std::format(lang::MSG_TLS_ENABLED, cfg.tls_cert));
return svr;
} catch (const std::exception& e) {
std::string msg = std::format(lang::WARN_TLS_SETUP_FAILED, e.what());
Diskerror::Logger::warn(msg);
std::cerr << msg << "\n";
return std::make_unique<httplib::Server>();
}
}
void warmup() {
// Pre-load embedding cache so first request isn't slow
try {
auto result = memory.search("warmup", 1, 0.0f);
Diskerror::Logger::info(std::format(lang::MSG_WARMUP_EMBEDDING_CACHE, memory.count()));
} catch (const std::exception& e) {
Diskerror::Logger::info(std::format(lang::MSG_WARMUP_ERROR, e.what()));
}
// Preload default model on local inference engines
if (inference_) {
preload_local_model(inference_->model);
}
}
/// Preload a model if its endpoint is local (non-commercial).
/// Runs in background thread to avoid blocking startup.
void preload_local_model(const std::string& model_name) {
if (model_name.empty() || !inference_) return;
try {
auto& ep = inference_->resolve_endpoint(model_name);
if (!ep.is_local()) return;
} catch (...) {
return;
}
std::thread([this, model_name]() {
auto err = inference_->ensure_model_loaded(model_name);
if (err.empty()) {
Diskerror::Logger::info(std::format(lang::MSG_PRELOADED_MODEL, model_name));
} else {
// WARN, not info — preload is best-effort but a misconfigured or
// unreachable management endpoint is the kind of thing the user
// wants to see (issue #47). The err string already carries the
// tried URL via ERR_ENGINE_UNREACHABLE / ERR_MODEL_LOAD_*.
Diskerror::Logger::warn(std::format(lang::MSG_MODEL_PRELOAD_SKIPPED, err));
}
}).detach();
}
/// Run one housekeeping pass: purge old turns, catch up missing
/// summaries, and backfill any NULL embeddings.
void run_housekeeping() {
const auto& cfg = config();
float max_age_hours = cfg.cleanup_max_age_hours;
// 1. Purge old conversation entries — use the main memory backend,
// not a separate sqlite::Backend. Opening a second connection to the
// same file while the main backend holds it can produce SQLite
// BUSY/LOCKED contention that silently prevents the backfill UPDATE
// in step 3 from committing.
if (max_age_hours > 0) {
try {
int deleted = memory.cleanup_old_conversations(max_age_hours);
if (deleted > 0) {
Diskerror::Logger::info(std::format(
lang::MSG_HOUSEKEEPING_EXPIRED, 0, deleted));
}
} catch (const std::exception& e) {
Diskerror::Logger::critical(std::format(lang::ERR_CLEANUP_DB, e.what()));
}
}
// 2. Catch up unsummarized turns and draft summaries.
if (summarizer_) {
summarizer_->enqueue_catch_up();
}
// 3. Backfill NULL embeddings (deferred stores, partial rebuilds).
// If embeddings are degraded, try to recover first — a rebuild may
// have fixed the drift since the last housekeeping tick.
if (memory.embeddings_degraded()) {
memory.try_recover_embeddings();
}
if (!memory.embeddings_degraded()) {
try {
// Finish an interrupted re-embed before the ordinary backfill.
// This resumes the remaining rows with the promoted identity;
// the plain backfill below then handles deferred stores.
memory.resume_interrupted_reembed();
int filled = memory.backfill_embeddings();
if (filled > 0) {
Diskerror::Logger::info(std::format(
lang::MSG_BACKFILLED_EMBEDDINGS, filled));
}
} catch (const std::exception& e) {
Diskerror::Logger::warn(std::format(lang::ERR_CLEANUP_DB, e.what()));
}
}
// 4. (Reindex housekeeping removed in Step 10 — the custom text index
// is maintained incrementally by the write path; use
// `ragger reindex [table|all]` for manual rebuilds.)
}
// Per-user housekeeping locks: username → fd
std::unordered_map<std::string, int> housekeeping_locks_;
std::mutex housekeeping_locks_mutex_;
/// Try to acquire housekeeping lock for a specific user.
/// Lock file: /tmp/ragger/housekeeping-{username}.lock
/// Returns true if this instance now owns housekeeping for that user.
bool acquire_user_housekeeping_lock(const std::string& username) {
std::lock_guard<std::mutex> lock(housekeeping_locks_mutex_);
if (housekeeping_locks_.count(username)) return true; // already own it
fs::create_directories("/tmp/ragger");
std::string lock_path = "/tmp/ragger/housekeeping-" + username + ".lock";
int fd = open(lock_path.c_str(), O_WRONLY | O_CREAT, 0644);
if (fd < 0) return false;
if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
::close(fd);
return false; // another instance owns it
}
// Write our PID
ftruncate(fd, 0);
auto pid_str = std::to_string(getpid());
(void)write(fd, pid_str.c_str(), pid_str.size());
housekeeping_locks_[username] = fd;
Diskerror::Logger::info(std::format(lang::MSG_HOUSEKEEPING_OWNER, username));
return true;
}
/// Start background timer for periodic housekeeping.
/// Housekeeping only acts on users whose locks we hold.
void start_housekeeping_timer() {
int interval = config().housekeeping_interval;
if (interval == 0) {
Diskerror::Logger::info(lang::MSG_HOUSEKEEPING_DISABLED);
return;
}
timer_running_ = true;
timer_thread_ = std::thread([this, interval]() {
while (timer_running_) {
for (int i = 0; i < interval && timer_running_; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
// Check for shutdown (or restart, which stops listeners
// the same way — main() re-execs afterward).
if (g_shutdown_requested.load(std::memory_order_relaxed) ||
g_restart_requested.load(std::memory_order_relaxed)) {
Diskerror::Logger::info(
g_restart_requested.load(std::memory_order_relaxed)
? "Restart signal received; stopping listeners to re-exec"
: "Shutdown signal received; stopping listeners");
timer_running_ = false;
if (unix_svr.is_running()) unix_svr.stop();
if (tcp_svr && tcp_svr->is_running()) tcp_svr->stop();
break;
}
// Check for signal-triggered housekeeping
if (g_housekeeping_requested.exchange(false)) {
Diskerror::Logger::info(lang::MSG_HOUSEKEEPING_SIGNAL);
run_housekeeping();
}
// Check for config reload (SIGHUP)
if (g_config_reload_requested.exchange(false)) {
int n = reload_config();
if (n > 0) {
Diskerror::Logger::info(std::format(ragger::lang::MSG_CONFIG_RELOADED_N, n));
// Re-initialize inference client if endpoints changed
try {
inference_ = std::make_unique<InferenceClient>(
InferenceClient::from_config(config()));
Diskerror::Logger::info(lang::MSG_INFERENCE_CLIENT_OK);
} catch (const std::exception& e) {
Diskerror::Logger::critical(std::format(lang::ERR_INFERENCE_CLIENT_FAIL, e.what()));
}
} else {
Diskerror::Logger::info(lang::MSG_CONFIG_RELOADED_NONE);
}
}
}
if (timer_running_) {
run_housekeeping();
}
}
});
}
void stop_housekeeping_timer() {
timer_running_ = false;
if (timer_thread_.joinable()) {
timer_thread_.join();
}
// Release all user housekeeping locks
std::lock_guard<std::mutex> lock(housekeeping_locks_mutex_);
for (auto& [username, fd] : housekeeping_locks_) {
flock(fd, LOCK_UN);
::close(fd);
}
housekeeping_locks_.clear();
}
void init_inference() {
const auto& cfg = config();
auto client = InferenceClient::from_config(cfg);
if (!client.endpoints.empty()) {
inference_ = std::make_unique<InferenceClient>(std::move(client));
Diskerror::Logger::info(std::format(lang::MSG_INFERENCE_ENABLED, inference_->endpoints.size()));
}
}
void bootstrap_auth() {
// Single-user mode: ensure token exists
server_token_ = ensure_token();
if (!server_token_.empty()) {
std::string token_hash = hash_token(server_token_);
auto user = users_.get_user_by_token_hash(token_hash);
if (!user) {
// Auto-create the single user
std::string username = "default";
struct passwd* pw = getpwuid(getuid());
if (pw) username = pw->pw_name;
int user_id = users_.create_user(username, token_hash);
user = UserInfo{user_id, username, token_hash};
Diskerror::Logger::info(std::format(lang::MSG_CREATED_USER, username, user_id));
}
default_user_ = user;
}
Diskerror::Logger::info(lang::MSG_SINGLE_USER_MODE);
}
/// True if this request must present a valid token. False for requests
/// arriving on the unix socket (empty remote_addr) or from localhost.
bool request_requires_auth(const httplib::Request& req) const {
if (req.remote_addr.empty()) return false; // unix socket
if (req.remote_addr == "127.0.0.1" || req.remote_addr == "::1") {
return false;
}
return true;
}
std::optional<UserInfo> _check_auth(const httplib::Request& req) {
const auto& cfg = config();
// Auth bypass: unix socket and localhost requests are pre-authenticated
// as the default (single) user.
if (!request_requires_auth(req) && default_user_) {
return default_user_;
}
// Parse "Bearer <token>" from the Authorization header.
auto auth_header = req.get_header_value("Authorization");
const std::string bearer_prefix = "Bearer ";
std::string token;
if (auth_header.substr(0, bearer_prefix.size()) == bearer_prefix) {
token = auth_header.substr(bearer_prefix.size());
} else if (req.has_param("token")) {
// Browser navigation / EventSource can't set headers — accept the
// token as a query parameter (?token=...) for the dashboard and
// its SSE stream. Same trust model: holding the token = authorized.
token = req.get_param_value("token");
} else {
return std::nullopt;
}
// Hash and lookup in database
std::string token_hash = hash_token(token);
auto user = users_.get_user_by_token_hash(token_hash);
if (user) {
return user;
}
// Fallback: direct comparison with server token
if (token == server_token_ && default_user_) {
return default_user_;
}
return std::nullopt;
}
/// Get per-user memory (or fallback to common).
/// In single-user mode, always returns the main memory instance.
RaggerMemory& _get_memory(const std::string& /*username*/) {
// Single-user mode: always return the main memory
return memory;
}
// ---- Route handlers (named for navigability) ---------------------------
void handle_store(const UserInfo& user, const httplib::Request& req,
httplib::Response& res) {
auto body = json::parse(req.body);
std::string text = body.value("text", "");
json metadata = body.value("metadata", json::object());
if (text.empty()) {
Diskerror::Logger::debug("POST /store 400");
res.status = 400;
res.set_content(lang::HTTP_MISSING_TEXT, "text/plain");
return;
}
if (!metadata.contains("collection") || metadata["collection"].get<std::string>().empty())
metadata["collection"] = "memory";
if (!metadata.contains("source") || metadata["source"].get<std::string>().empty())
metadata["source"] = user.username;
auto& mem = _get_memory(user.username);
// Route curated decisions to the L6 decisions table; everything else
// (fact/preference/lesson/unspecified) lands in summaries via store().
std::string category = metadata.value("category", "");
if (category == "decision") {
std::string tags = metadata.value("tags", "");
// Default "current" (the only status the recall pipeline
// surfaces). Callers can pass metadata.status="roadmap" for
// planned/future work that shouldn't clutter every session's
// recall until it's actually done.
std::string status = metadata.value("status", "current");
int did = mem.store_decision(text, status, tags);
Diskerror::Logger::debug("POST /store 200 (decision)");
respond_json(res, json{{"id", std::to_string(did)},
{"status", "stored"},
{"table", "decisions"}});
return;
}
std::string id = mem.store(text, metadata);
Diskerror::Logger::debug("POST /store 200");
respond_json(res, json{{"id", id}, {"status", "stored"}});
}
void handle_search(const UserInfo& user, const httplib::Request& req,
httplib::Response& res) {
auto body = json::parse(req.body);
std::string query = body.value("query", "");
if (query.empty()) {
Diskerror::Logger::debug("POST /search 400");
res.status = 400; res.set_content(lang::HTTP_MISSING_QUERY, "text/plain"); return;
}
int limit = body.value("limit", config().default_search_limit);
float min_score = body.value("min_score", config().default_min_score);
auto collections = body.value("collections", std::vector<std::string>{});
auto t0 = std::chrono::high_resolution_clock::now();
auto& mem = _get_memory(user.username);
auto sr = mem.search(query, limit, min_score, collections);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - t0).count();
json timing = sr.timing;
timing["total_ms"] = ms;
Diskerror::Logger::trace(std::format(lang::DBG_QUERY_LOG, query, sr.results.size(), ms));
Diskerror::Logger::debug(std::format(lang::DBG_HTTP, "POST", "/search", "200"));
respond_json(res, json{{"results", search_results_to_json(sr.results)},
{"timing", timing}});
}
// POST /turn: ingest one agent-pushed conversation turn for background
// summarization. No-op unless [server] capture_turns is true.
void handle_turn(const UserInfo& user, const httplib::Request& req,
httplib::Response& res) {
auto body = json::parse(req.body);
std::string user_text = body.value("user", "");
std::string assistant = body.value("assistant", "");
std::string model = body.value("model", "");
std::string session_id = body.value("session_id", "");
std::string session_name = body.value("session_name", "");
std::string name_source = body.value("name_source", "");
if (user_text.empty()) {
Diskerror::Logger::debug("POST /turn 400");
respond_json(res, json{{"error", "missing required field: user"}}, 400);
return;
}
auto& mem = _get_memory(user.username);
auto result = capture_turn(mem, user_text, assistant, model, session_id,
session_name, name_source);
// Raw text is written to both turns and summaries (as a NULL-model_id
// placeholder) by capture_turn. The summarizer picks it up on the next
// housekeeping tick (every 60s) via enqueue_catch_up — no immediate
// LLM call here.
Diskerror::Logger::debug(std::format(lang::DBG_HTTP, "POST", "/turn", "200"));
respond_json(res, json{{"status", result.captured ? "captured" : "disabled"},
{"turn_id", result.turn_id}});
}
// GET /session/<guid>[?recipe=name]: assemble a recipe-shaped context payload.
void handle_session(const UserInfo& user, const httplib::Request& req,
httplib::Response& res) {
std::string sid = req.matches[1];
std::string recipe_name = req.get_param_value("recipe");
auto& mem = _get_memory(user.username);
auto ctx = build_context(mem, sid, recipe_name);
if (!ctx.enabled) {
respond_json(res, json{{"status", "disabled"}});
return;
}
json chunks = json::array();
for (const auto& c : ctx.chunks)
chunks.push_back({{"kind", c.kind}, {"text", c.text}, {"timestamp", c.timestamp}});
Diskerror::Logger::debug(std::format(lang::DBG_HTTP, "GET", "/session", "200"));
respond_json(res, json{{"status", "ok"},
{"session_id", sid},
{"recipe", ctx.recipe_name},
{"chunks", chunks}});
}
void handle_delete_memory(const UserInfo& user, const httplib::Request& req,
httplib::Response& res) {
int id = std::stoi(req.matches[1]);
auto& mem = _get_memory(user.username);
if (mem.delete_memory(id)) {
Diskerror::Logger::debug("DELETE /memory 200");
respond_json(res, json{{"id", id}, {"status", "deleted"}});
} else {
Diskerror::Logger::debug("DELETE /memory 404");
res.status = 404; res.set_content(lang::HTTP_MEMORY_NOT_FOUND, "text/plain");
}
}
void handle_delete_batch(const UserInfo& user, const httplib::Request& req,
httplib::Response& res) {
auto body = json::parse(req.body);
if (!body.contains("ids") || !body["ids"].is_array()) {
Diskerror::Logger::debug("POST /delete_batch 400");
res.status = 400; res.set_content(lang::HTTP_MISSING_IDS, "text/plain"); return;
}
auto ids = body["ids"].get<std::vector<int>>();
auto& mem = _get_memory(user.username);
int deleted = mem.delete_batch(ids);
Diskerror::Logger::debug("POST /delete_batch 200");
respond_json(res, json{{"deleted", deleted}});
}
void handle_search_by_metadata(const UserInfo& user, const httplib::Request& req,
httplib::Response& res) {
auto body = json::parse(req.body);
if (!body.contains("metadata") || !body["metadata"].is_object()) {
Diskerror::Logger::debug("POST /search_by_metadata 400");
res.status = 400; res.set_content(lang::HTTP_MISSING_METADATA, "text/plain"); return;
}
auto results = _get_memory(user.username).search_by_metadata(
body["metadata"], body.value("limit", 0),
body.value("after", ""), body.value("before", ""));
Diskerror::Logger::debug("POST /search_by_metadata 200");
respond_json(res, json{{"results", search_results_to_json(results)},
{"count", results.size()}});
}
// ---- Route registration -----------------------------------------------
void setup_routes(httplib::Server& svr) {
// GET /health — no auth required
svr.Get("/health", [this](const httplib::Request&, httplib::Response& res) {
respond_json(res, json{{"status", "ok"},
{"version", RAGGER_VERSION},
{"commit", RAGGER_COMMIT},
{"built", RAGGER_BUILD_DATE},
{"memories", memory.count()},
{"embeddable_rows", memory.count_embeddable_rows()},
{"degraded", memory.embeddings_degraded()}});
Diskerror::Logger::debug("GET /health 200");
});
// Wrap a handler with bearer auth + uniform error handling.
auto guarded = [this](auto handler) {
return [this, handler](const httplib::Request& req, httplib::Response& res) {
auto user = _check_auth(req);
if (!user) {
Diskerror::Logger::debug(std::format("{} {} 401", req.method, req.path));
res.status = 401;
res.set_content(lang::HTTP_UNAUTHORIZED, "text/plain");
return;
}
try {
handler(*user, req, res);
} catch (const json::exception& e) {
Diskerror::Logger::debug(std::format("{} {} 400", req.method, req.path));
res.status = 400;
res.set_content(std::format(lang::HTTP_JSON_ERROR, e.what()), "text/plain");
} catch (const std::exception& e) {
Diskerror::Logger::critical(std::format(lang::ERR_ROUTE_FAILED, req.method, req.path, e.what()));
res.status = 500;
res.set_content(std::string("ERROR: ") + e.what(), "text/plain");
}
};
};
svr.Get("/count", guarded([this](const UserInfo& user, const httplib::Request&,
httplib::Response& res) {
respond_json(res, json{{"count", _get_memory(user.username).count()}});
Diskerror::Logger::debug("GET /count 200");
}));
svr.Post("/store", guarded([this](const UserInfo& u, const httplib::Request& req, httplib::Response& res) { handle_store(u, req, res); }));
svr.Post("/search", guarded([this](const UserInfo& u, const httplib::Request& req, httplib::Response& res) { handle_search(u, req, res); }));
svr.Post("/turn", guarded([this](const UserInfo& u, const httplib::Request& req, httplib::Response& res) { handle_turn(u, req, res); }));
svr.Get(R"(/session/([^/]+))", guarded([this](const UserInfo& u, const httplib::Request& req, httplib::Response& res) { handle_session(u, req, res); }));
svr.Delete(R"(/memory/(\d+))", guarded([this](const UserInfo& u, const httplib::Request& req, httplib::Response& res) { handle_delete_memory(u, req, res); }));
svr.Post("/delete_batch", guarded([this](const UserInfo& u, const httplib::Request& req, httplib::Response& res) { handle_delete_batch(u, req, res); }));
svr.Post("/search_by_metadata", guarded([this](const UserInfo& u, const httplib::Request& req, httplib::Response& res) { handle_search_by_metadata(u, req, res); }));
svr.Get("/user/token", guarded([this](const UserInfo& user, const httplib::Request&,
httplib::Response& res) {
struct passwd* pw = getpwnam(user.username.c_str());
if (!pw) { res.status = 404; res.set_content(std::format(R"({{"error":"{}"}})", lang::HTTP_SYSTEM_USER_NOT_FOUND), "application/json"); return; }
std::string token_file = std::string(pw->pw_dir) + "/.ragger/token";
std::ifstream f(token_file);
if (!f) { res.status = 404; res.set_content(std::format(R"({{"error":"{}"}})", lang::HTTP_NO_TOKEN_FILE), "application/json"); return; }
std::string token;
std::getline(f, token);
size_t s = token.find_first_not_of(" \t\r\n");
size_t e = token.find_last_not_of(" \t\r\n");
if (s != std::string::npos) token = token.substr(s, e - s + 1);
Diskerror::Logger::debug("GET /user/token 200");
respond_json(res, json{{"token", token}, {"username", user.username}});
}));
// ---- Dashboard (embedded HTML/JS) --------------------------------
// GET /dashboard[?token=...] -> the single-page control panel. The
// page then talks to /config and /events with the same token.
svr.Get("/dashboard", guarded([this](const UserInfo&, const httplib::Request&,
httplib::Response& res) {
res.set_content(DASHBOARD_HTML, "text/html; charset=utf-8");
Diskerror::Logger::debug("GET /dashboard 200");
}));
// ---- Config API (dashboard + programmatic) -----------------------
// GET /config -> all settings as {key: {value, ...schema}}
// GET /config/<key> -> one setting
// PUT /config/<key> -> set one setting (body: {"value": "..."})
// empty value reverts to schema default.
svr.Get("/config", guarded([this](const UserInfo&, const httplib::Request&,
httplib::Response& res) {
res.set_content(build_config_json().dump(), "application/json");
Diskerror::Logger::debug("GET /config 200");
}));
svr.Get(R"(/config/([A-Za-z0-9_]+))", guarded([this](const UserInfo&,
const httplib::Request& req, httplib::Response& res) {
std::string key = req.matches[1];
const auto* meta = lang::config_meta(key);
if (!meta) { res.status = 404; res.set_content(R"({"error":"unknown key"})", "application/json"); return; }
auto v = get_config_value(config(), key);
res.set_content(config_entry_json(*meta, v.value_or("")).dump(), "application/json");
}));
svr.Put(R"(/config/([A-Za-z0-9_]+))", guarded([this](const UserInfo&,
const httplib::Request& req, httplib::Response& res) {
std::string key = req.matches[1];
std::string value;
if (!req.body.empty()) {
auto body = json::parse(req.body);
value = body.value("value", "");
}
auto r = set_config_persisted(key, value);
if (!r) {
switch (r.error()) {
case ConfigSetError::UnknownKey:
res.status = 404; res.set_content(R"({"error":"unknown key"})", "application/json"); break;
case ConfigSetError::Locked:
res.status = 403; res.set_content(R"({"error":"key is locked"})", "application/json"); break;
case ConfigSetError::InvalidValue:
res.status = 400; res.set_content(R"({"error":"invalid value"})", "application/json"); break;
}
Diskerror::Logger::debug(std::format("PUT /config/{} {}", key, res.status));
return;
}
// Echo the resolved (post-default) value plus apply flags.
auto v = get_config_value(config(), key);
json out = {{"key", key}, {"value", v.value_or("")},
{"restart_required", r->restart_required},
{"rebuild_required", r->rebuild_required}};
res.set_content(out.dump(), "application/json");
Diskerror::Logger::debug(std::format("PUT /config/{} 200", key));
}));
// GET /stats -> one-shot status snapshot (server + table sizes).
svr.Get("/stats", guarded([this](const UserInfo&, const httplib::Request&,
httplib::Response& res) {
res.set_content(build_stats_json().dump(), "application/json");
}));
// GET /models -> ONNX embedding model choices from ~/.ragger/models/.
// Scans for provider/model dirs (two-level: sentence-transformers/all-MiniLM-L6-v2)
// and legacy flat dirs (all-MiniLM-L6-v2). A directory is a valid ONNX model
// when it contains tokenizer.json AND (model.onnx or onnx/model.onnx).
// Returns {models: [{name:"all-MiniLM-L6-v2", path:"sentence-transformers/all-MiniLM-L6-v2"}, ...]}
// "name" is the display name (model only, unless two providers share a
// model name — then "provider/model"). "path" is the canonical key to
// store in the config and resolve with resolved_model_dir().
svr.Get("/models", guarded([this](const UserInfo&, const httplib::Request&,
httplib::Response& res) {
namespace fs = std::filesystem;
std::error_code ec;
const std::string dir = ragger_base_dir() + "/models";
auto is_onnx_model = [](const fs::path& p) -> bool {
return fs::exists(p / "tokenizer.json") &&
(fs::exists(p / "model.onnx") || fs::exists(p / "onnx" / "model.onnx"));
};
// Collect all valid models as (display_name, canonical_path) pairs.
// canonical_path is relative to the models dir.
struct ModelInfo { std::string name; std::string path; };
std::vector<ModelInfo> models;
// Track model-name frequency for collision detection.
std::map<std::string, int> name_count;
for (auto& top : fs::directory_iterator(dir, ec)) {
if (!top.is_directory(ec)) continue;
std::string top_name = top.path().filename().string();
if (!top_name.empty() && top_name[0] == '.') continue;
// Only scan provider/model two-level layout. Legacy flat
// model dirs (e.g. all-MiniLM-L6-v2/ at the top level) are
// kept for backward compat but not offered as new choices.
for (auto& sub : fs::directory_iterator(top.path(), ec)) {
if (!sub.is_directory(ec)) continue;
std::string sub_name = sub.path().filename().string();
if (!sub_name.empty() && sub_name[0] == '.') continue;
if (is_onnx_model(sub.path())) {
models.push_back({sub_name, top_name + "/" + sub_name});
name_count[sub_name]++;
}
}
}
// Build response: disambiguate colliding display names with provider prefix.
json arr = json::array();
for (auto& m : models) {
std::string display = (name_count[m.name] > 1) ? m.path : m.name;
arr.push_back(json{{"name", display}, {"path", m.path}});
}
// Sort by display name for stable UI ordering.
std::sort(arr.begin(), arr.end(),
[](const json& a, const json& b) { return a["name"] < b["name"]; });
respond_json(res, json{{"models", arr}});
}));
// GET /models/info?path=<provider/model> -> read config.json from a
// local model dir and return embedding dimensions and metadata.
// Returns {dimensions: N, model_type: "...", hidden_size: N}
svr.Get("/models/info", guarded([this](const UserInfo&, const httplib::Request& req,
httplib::Response& res) {
std::string model_path = req.get_param_value("path");
if (model_path.empty()) {
res.status = 400;
res.set_content(R"({"error":"path required"})", "application/json");
return;
}
std::string dir = ragger_base_dir() + "/models/" + model_path;
std::string cfg_file = dir + "/config.json";
if (!std::filesystem::exists(cfg_file)) {
res.status = 404;
res.set_content(R"({"error":"config.json not found"})", "application/json");
return;
}
std::ifstream ifs(cfg_file);
auto cfg_json = json::parse(ifs, nullptr, false);
if (cfg_json.is_discarded()) {
res.status = 500;
res.set_content(R"({"error":"failed to parse config.json"})", "application/json");
return;
}
int hidden_size = cfg_json.value("hidden_size", 0);
std::string model_type = cfg_json.value("model_type", "");
respond_json(res, json{
{"dimensions", hidden_size},
{"model_type", model_type},
{"hidden_size", hidden_size}
});
}));
// GET /models/external?host=X&port=Y&key=Z -> query a remote
// /v1/models endpoint and return available model names.
svr.Get("/models/external", guarded([this](const UserInfo&, const httplib::Request& req,
httplib::Response& res) {
std::string host = req.get_param_value("host");
std::string port_s = req.get_param_value("port");
std::string key = req.get_param_value("key");
if (host.empty() || port_s.empty()) {
res.status = 400;
res.set_content(R"({"error":"host and port required"})", "application/json");
return;
}
int port = std::stoi(port_s);
Embedder probe_emb(host, port, "", key);
auto models = probe_emb.list_remote_models();
json arr = json::array();
for (const auto& m : models) arr.push_back(m);
respond_json(res, json{{"models", arr}});
}));
// POST /models/external/probe -> send a test embedding request to a
// remote endpoint and return the vector dimensionality.
// Body: {"host":"...", "port":N, "model":"...", "key":"..."}
svr.Post("/models/external/probe", guarded([this](const UserInfo&, const httplib::Request& req,
httplib::Response& res) {
auto body = json::parse(req.body, nullptr, false);
if (body.is_discarded()) {
res.status = 400;
res.set_content(R"({"error":"invalid JSON"})", "application/json");
return;
}
std::string host = body.value("host", "");
int port = body.value("port", 0);
std::string model = body.value("model", "");
std::string key = body.value("key", "");
if (host.empty() || port == 0 || model.empty()) {
res.status = 400;
res.set_content(R"({"error":"host, port, and model required"})", "application/json");
return;
}
Embedder probe_emb(host, port, model, key);
int dims = probe_emb.probe_dimensions();
if (dims == 0) {
res.status = 502;
res.set_content(R"({"error":"probe failed — endpoint unreachable or returned no embedding"})",
"application/json");
return;
}
if (!probe_emb.last_probe_sane()) {
respond_json(res, json{
{"error", "probe returned an implausible embedding — "
"this model likely isn't an embedding model"},
{"dimensions", dims}
}, 502);
return;
}
std::string served = probe_emb.last_served_model();
bool mismatch = !served.empty() && served != model;
respond_json(res, json{
{"dimensions", dims},
{"served_model", served},
{"model_mismatch", mismatch}
});
}));
// GET /models/huggingface?q=<search> -> search HuggingFace for ONNX
// embedding models. Returns {models: [{id, downloads}, ...]} sorted
// by popularity. Combines sentence-similarity + feature-extraction.
svr.Get("/models/huggingface", guarded([this](const UserInfo&, const httplib::Request& req,
httplib::Response& res) {
std::string query = req.get_param_value("q");
// Build HF API URL — search both pipeline tags for embedding models.
auto fetch_hf = [&](const std::string& tag) -> json {
std::string url = "https://huggingface.co/api/models?"
"pipeline_tag=" + tag +
"&library=onnx&sort=downloads&direction=-1&limit=20";
if (!query.empty()) url += "&search=" + query;
CURL* curl = curl_easy_init();
if (!curl) return json::array();
std::string buf;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
+[](char* p, size_t s, size_t n, void* ud) -> size_t {
static_cast<std::string*>(ud)->append(p, s * n);
return s * n;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf);
CURLcode rc = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (rc != CURLE_OK) return json::array();
auto j = json::parse(buf, nullptr, false);
return j.is_array() ? j : json::array();
};
// Merge results from both pipeline tags, dedup by modelId.
json results = json::array();
std::set<std::string> seen;
for (const auto& tag : {"sentence-similarity", "feature-extraction"}) {
for (const auto& m : fetch_hf(tag)) {
std::string mid = m.value("modelId", "");
if (mid.empty() || seen.count(mid)) continue;
seen.insert(mid);
results.push_back(json{
{"id", mid},
{"downloads", m.value("downloads", 0)}
});
}
}
respond_json(res, json{{"models", results}});
}));
// POST /models/download -> download an ONNX model from HuggingFace
// to ~/.ragger/models/<provider>/<model>/.
// Body: {"repo": "sentence-transformers/all-MiniLM-L6-v2"}
svr.Post("/models/download", guarded([this](const UserInfo&, const httplib::Request& req,
httplib::Response& res) {
auto body = json::parse(req.body, nullptr, false);
if (body.is_discarded() || !body.contains("repo")) {
res.status = 400;
res.set_content(R"({"error":"'repo' required"})", "application/json");
return;
}
std::string repo = body["repo"].get<std::string>();
// Validate repo format: "provider/model"
auto slash = repo.find('/');
if (slash == std::string::npos || slash == 0 || slash == repo.size() - 1) {
res.status = 400;
res.set_content(R"({"error":"repo must be 'provider/model'"})", "application/json");
return;
}
std::string dest = ragger_base_dir() + "/models/" + repo;
std::string hf_base = "https://huggingface.co/" + repo + "/resolve/main";
// Check if already present (model.onnx or onnx/model.onnx)
if (std::filesystem::exists(dest + "/model.onnx") ||
std::filesystem::exists(dest + "/onnx/model.onnx")) {
respond_json(res, json{{"status", "already_present"}, {"path", repo}});
return;
}
std::filesystem::create_directories(dest);
std::filesystem::create_directories(dest + "/onnx");