-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhttp.rs
More file actions
1345 lines (1238 loc) · 43.6 KB
/
Copy pathhttp.rs
File metadata and controls
1345 lines (1238 loc) · 43.6 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
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use axum::extract::{Path, Query, State};
use axum::http::{HeaderMap, HeaderValue, Method};
use axum::{
Json, Router,
http::StatusCode,
response::IntoResponse,
routing::{get, post},
};
use serde::Deserialize;
use tokio::sync::Mutex;
use tower_http::cors::{Any, CorsLayer};
use crate::config::{ApiKeyConfig, HttpConfig};
use crate::context::{self, ContextParams};
use crate::health;
use crate::llm::{EmbedModel, OrchestratorModel, RerankModel};
use crate::profile::VaultProfile;
use crate::search;
use crate::serve::RecentWrites;
use crate::store::Store;
use crate::writer::{
self, AppendInput, CreateNoteInput, DeleteMode, EditFrontmatterInput, EditInput, EditMode,
FrontmatterOp, RewriteInput, UpdateMetadataInput,
};
// ---------------------------------------------------------------------------
// Shared state
// ---------------------------------------------------------------------------
#[derive(Clone)]
pub struct ApiState {
pub store: Arc<Mutex<Store>>,
pub embedder: Arc<Mutex<Box<dyn EmbedModel + Send>>>,
pub vault_path: Arc<std::path::PathBuf>,
pub profile: Arc<Option<VaultProfile>>,
pub orchestrator: Option<Arc<Mutex<Box<dyn OrchestratorModel + Send>>>>,
pub reranker: Option<Arc<Mutex<Box<dyn RerankModel + Send>>>>,
pub http_config: Arc<HttpConfig>,
pub no_auth: bool,
pub recent_writes: RecentWrites,
pub rate_limiter: Arc<RateLimiter>,
}
// ---------------------------------------------------------------------------
// Rate limiter (in-memory token bucket)
// ---------------------------------------------------------------------------
pub struct RateLimiter {
buckets: std::sync::Mutex<HashMap<String, RateBucket>>,
limit: u32, // requests per minute, 0 = unlimited
}
struct RateBucket {
tokens: u32,
last_refill: Instant,
}
impl RateLimiter {
pub fn new(limit: u32) -> Self {
Self {
buckets: std::sync::Mutex::new(HashMap::new()),
limit,
}
}
/// Check if a request is allowed. Returns Ok(()) or Err with retry-after seconds.
pub fn check(&self, key: &str) -> Result<(), u64> {
if self.limit == 0 {
return Ok(());
}
let mut buckets = self.buckets.lock().unwrap();
let bucket = buckets.entry(key.to_string()).or_insert(RateBucket {
tokens: self.limit,
last_refill: Instant::now(),
});
// Refill tokens based on elapsed time
let elapsed = bucket.last_refill.elapsed().as_secs_f64();
let refill = (elapsed * self.limit as f64 / 60.0) as u32;
if refill > 0 {
bucket.tokens = (bucket.tokens + refill).min(self.limit);
bucket.last_refill = Instant::now();
}
if bucket.tokens > 0 {
bucket.tokens -= 1;
Ok(())
} else {
let retry_after = (60.0 / self.limit as f64).ceil() as u64;
Err(retry_after)
}
}
}
// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------
pub struct ApiError {
pub status: StatusCode,
pub message: String,
pub headers: Vec<(String, String)>,
}
impl IntoResponse for ApiError {
fn into_response(self) -> axum::response::Response {
let body = serde_json::json!({ "error": self.message });
let mut response = (self.status, Json(body)).into_response();
for (name, value) in &self.headers {
if let (Ok(n), Ok(v)) = (
axum::http::header::HeaderName::from_bytes(name.as_bytes()),
HeaderValue::from_str(value),
) {
response.headers_mut().insert(n, v);
}
}
response
}
}
impl ApiError {
pub fn unauthorized(msg: &str) -> Self {
Self {
status: StatusCode::UNAUTHORIZED,
message: msg.to_string(),
headers: vec![],
}
}
pub fn forbidden(msg: &str) -> Self {
Self {
status: StatusCode::FORBIDDEN,
message: msg.to_string(),
headers: vec![],
}
}
pub fn bad_request(msg: &str) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
message: msg.to_string(),
headers: vec![],
}
}
pub fn not_found(msg: &str) -> Self {
Self {
status: StatusCode::NOT_FOUND,
message: msg.to_string(),
headers: vec![],
}
}
pub fn internal(msg: &str) -> Self {
Self {
status: StatusCode::INTERNAL_SERVER_ERROR,
message: msg.to_string(),
headers: vec![],
}
}
pub fn rate_limited(retry_after: u64) -> Self {
Self {
status: StatusCode::TOO_MANY_REQUESTS,
message: format!("Rate limit exceeded. Retry after {retry_after}s"),
headers: vec![("retry-after".to_string(), retry_after.to_string())],
}
}
}
// ---------------------------------------------------------------------------
// Auth helpers
// ---------------------------------------------------------------------------
/// Validate API key from Authorization header. Returns the matching key config.
pub fn validate_api_key<'a>(key: &str, config: &'a HttpConfig) -> Option<&'a ApiKeyConfig> {
config.api_keys.iter().find(|k| k.key == key)
}
/// Check if a permission level allows the requested operation.
pub fn check_permission(permission: &str, is_write: bool) -> bool {
if !is_write {
return true;
}
permission == "write"
}
/// Extract and validate auth from request headers, then check rate limit.
pub fn authorize(
headers: &axum::http::HeaderMap,
state: &ApiState,
is_write: bool,
) -> Result<(), ApiError> {
if state.no_auth {
state
.rate_limiter
.check("no_auth")
.map_err(ApiError::rate_limited)?;
return Ok(());
}
let auth = headers
.get("authorization")
.and_then(|v| v.to_str().ok())
.ok_or_else(|| ApiError::unauthorized("Missing Authorization header"))?;
let key = auth
.strip_prefix("Bearer ")
.ok_or_else(|| ApiError::unauthorized("Authorization must use Bearer scheme"))?;
let key_config = validate_api_key(key, &state.http_config)
.ok_or_else(|| ApiError::unauthorized("Invalid API key"))?;
if !check_permission(&key_config.permissions, is_write) {
return Err(ApiError::forbidden(
"Insufficient permissions: write access required",
));
}
state
.rate_limiter
.check(key)
.map_err(ApiError::rate_limited)?;
Ok(())
}
/// Generate a new API key with `eg_` prefix + 32 hex chars.
pub fn generate_api_key() -> String {
use rand::Rng;
let mut rng = rand::rng();
let hex: String = (0..32)
.map(|_| format!("{:x}", rng.random_range(0..16u8)))
.collect();
format!("eg_{hex}")
}
// ---------------------------------------------------------------------------
// Request body / query structs
// ---------------------------------------------------------------------------
#[derive(Debug, Deserialize)]
struct SearchBody {
query: String,
top_n: Option<usize>,
}
#[derive(Debug, Deserialize)]
struct ReadSectionQuery {
file: String,
heading: String,
}
#[derive(Debug, Deserialize)]
struct ListQuery {
folder: Option<String>,
#[serde(default)]
tags: Vec<String>,
limit: Option<usize>,
created_by: Option<String>,
}
#[derive(Debug, Deserialize)]
struct ContextBody {
topic: String,
budget: Option<usize>,
}
// -- Write request bodies --
#[derive(Debug, Deserialize)]
struct CreateBody {
content: String,
filename: Option<String>,
type_hint: Option<String>,
#[serde(default)]
tags: Vec<String>,
folder: Option<String>,
}
#[derive(Debug, Deserialize)]
struct AppendBody {
file: String,
content: String,
}
#[derive(Debug, Deserialize)]
struct EditBody {
file: String,
heading: String,
content: String,
mode: Option<String>,
}
#[derive(Debug, Deserialize)]
struct RewriteBody {
file: String,
content: String,
preserve_frontmatter: Option<bool>,
}
#[derive(Debug, Deserialize)]
struct EditFrontmatterBody {
file: String,
operations: Vec<serde_json::Value>,
}
#[derive(Debug, Deserialize)]
struct MoveBody {
file: String,
new_folder: String,
}
#[derive(Debug, Deserialize)]
struct ArchiveBody {
file: String,
}
#[derive(Debug, Deserialize)]
struct UnarchiveBody {
file: String,
}
#[derive(Debug, Deserialize)]
struct UpdateMetadataBody {
file: String,
tags: Option<Vec<String>>,
aliases: Option<Vec<String>>,
}
#[derive(Debug, Deserialize)]
struct DeleteBody {
file: String,
mode: Option<String>,
}
// ---------------------------------------------------------------------------
// CORS
// ---------------------------------------------------------------------------
fn cors_layer(origins: &[String]) -> CorsLayer {
if origins.is_empty() {
return CorsLayer::new();
}
if origins.iter().any(|o| o == "*") {
return CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any);
}
let origins: Vec<HeaderValue> = origins.iter().filter_map(|o| o.parse().ok()).collect();
CorsLayer::new()
.allow_origin(origins)
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
.allow_headers(Any)
.allow_credentials(true)
}
// ---------------------------------------------------------------------------
// Router
// ---------------------------------------------------------------------------
/// Build the axum router with all API endpoints.
pub fn build_router(state: ApiState) -> Router {
let cors = cors_layer(&state.http_config.cors_origins);
Router::new()
.route("/api/health-check", get(health_check))
.route("/api/search", post(handle_search))
.route("/api/read/{*file}", get(handle_read))
.route("/api/read-section", get(handle_read_section))
.route("/api/list", get(handle_list))
.route("/api/vault-map", get(handle_vault_map))
.route("/api/who/{name}", get(handle_who))
.route("/api/project/{name}", get(handle_project))
.route("/api/context", post(handle_context))
.route("/api/health", get(handle_health))
// Write endpoints
.route("/api/create", post(handle_create))
.route("/api/append", post(handle_append))
.route("/api/edit", post(handle_edit))
.route("/api/rewrite", post(handle_rewrite))
.route("/api/edit-frontmatter", post(handle_edit_frontmatter))
.route("/api/move", post(handle_move))
.route("/api/archive", post(handle_archive))
.route("/api/unarchive", post(handle_unarchive))
.route("/api/update-metadata", post(handle_update_metadata))
.route("/api/delete", post(handle_delete))
// Migration endpoints
.route("/api/migrate/preview", post(handle_migrate_preview))
.route("/api/migrate/apply", post(handle_migrate_apply))
.route("/api/migrate/undo", post(handle_migrate_undo))
// OpenAPI / ChatGPT plugin discovery (no auth required)
.route("/openapi.json", get(handle_openapi))
.route("/.well-known/ai-plugin.json", get(handle_plugin_manifest))
.layer(cors)
.with_state(state)
}
async fn health_check() -> &'static str {
"ok"
}
async fn handle_openapi(State(state): State<ApiState>) -> impl IntoResponse {
let default_url = format!(
"http://{}:{}",
state.http_config.host, state.http_config.port
);
let server_url = state
.http_config
.plugin
.public_url
.as_deref()
.unwrap_or(&default_url);
let spec = crate::openapi::build_openapi_spec(server_url);
Json(spec)
}
async fn handle_plugin_manifest(State(state): State<ApiState>) -> impl IntoResponse {
let default_url = format!(
"http://{}:{}",
state.http_config.host, state.http_config.port
);
let server_url = state
.http_config
.plugin
.public_url
.as_deref()
.unwrap_or(&default_url);
let manifest = crate::openapi::build_plugin_manifest(&state.http_config, server_url);
Json(manifest)
}
// ---------------------------------------------------------------------------
// Read endpoint handlers
// ---------------------------------------------------------------------------
async fn handle_search(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<SearchBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let top_n = body.top_n.unwrap_or(10);
let store = state.store.lock().await;
let mut embedder = state.embedder.lock().await;
let mut orch_guard = match &state.orchestrator {
Some(o) => Some(o.lock().await),
None => None,
};
let mut rerank_guard = match &state.reranker {
Some(r) => Some(r.lock().await),
None => None,
};
let mut config = search::SearchConfig {
orchestrator: orch_guard
.as_mut()
.map(|g| g.as_mut() as &mut dyn OrchestratorModel),
reranker: rerank_guard
.as_mut()
.map(|g| g.as_mut() as &mut dyn RerankModel),
store: &store,
rerank_candidates: 30,
};
let output = search::search_with_intelligence(&body.query, top_n, &mut *embedder, &mut config)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(output.results)))
}
async fn handle_read(
State(state): State<ApiState>,
headers: HeaderMap,
Path(file): Path<String>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let store = state.store.lock().await;
let ctx = ContextParams {
store: &store,
vault_path: &state.vault_path,
profile: state.profile.as_ref().as_ref(),
};
let note =
context::context_read(&ctx, &file).map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(note)))
}
async fn handle_read_section(
State(state): State<ApiState>,
headers: HeaderMap,
Query(params): Query<ReadSectionQuery>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let store = state.store.lock().await;
let result = context::read_section(&store, &state.vault_path, ¶ms.file, ¶ms.heading)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(result)))
}
async fn handle_list(
State(state): State<ApiState>,
headers: HeaderMap,
Query(params): Query<ListQuery>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let store = state.store.lock().await;
let ctx = ContextParams {
store: &store,
vault_path: &state.vault_path,
profile: state.profile.as_ref().as_ref(),
};
let limit = params.limit.unwrap_or(20);
let items = context::context_list(
&ctx,
params.folder.as_deref(),
¶ms.tags,
params.created_by.as_deref(),
limit,
)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(items)))
}
async fn handle_vault_map(
State(state): State<ApiState>,
headers: HeaderMap,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let store = state.store.lock().await;
let ctx = ContextParams {
store: &store,
vault_path: &state.vault_path,
profile: state.profile.as_ref().as_ref(),
};
let map = context::vault_map(&ctx).map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(map)))
}
async fn handle_who(
State(state): State<ApiState>,
headers: HeaderMap,
Path(name): Path<String>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let store = state.store.lock().await;
let ctx = ContextParams {
store: &store,
vault_path: &state.vault_path,
profile: state.profile.as_ref().as_ref(),
};
let person =
context::context_who(&ctx, &name).map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(person)))
}
async fn handle_project(
State(state): State<ApiState>,
headers: HeaderMap,
Path(name): Path<String>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let store = state.store.lock().await;
let ctx = ContextParams {
store: &store,
vault_path: &state.vault_path,
profile: state.profile.as_ref().as_ref(),
};
let proj =
context::context_project(&ctx, &name).map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(proj)))
}
async fn handle_context(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<ContextBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let budget = body.budget.unwrap_or(32000);
let store = state.store.lock().await;
let mut embedder = state.embedder.lock().await;
let ctx = ContextParams {
store: &store,
vault_path: &state.vault_path,
profile: state.profile.as_ref().as_ref(),
};
let bundle = context::context_topic_with_search(&ctx, &body.topic, budget, &mut *embedder)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(bundle)))
}
async fn handle_health(
State(state): State<ApiState>,
headers: HeaderMap,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, false)?;
let store = state.store.lock().await;
let profile_ref = state.profile.as_ref().as_ref();
let config = health::HealthConfig {
daily_folder: profile_ref.and_then(|p| p.structure.folders.daily.clone()),
inbox_folder: profile_ref.and_then(|p| p.structure.folders.inbox.clone()),
};
let report = health::generate_health_report(&store, &config)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!(report)))
}
// ---------------------------------------------------------------------------
// Write helpers
// ---------------------------------------------------------------------------
/// Record a write to the recent-writes map so the file watcher skips re-indexing.
async fn record_write(recent_writes: &RecentWrites, path: &std::path::Path) {
if let Ok(meta) = std::fs::metadata(path)
&& let Ok(mtime) = meta.modified()
{
recent_writes.lock().await.insert(path.to_path_buf(), mtime);
}
}
/// Parse a JSON operations array into `Vec<FrontmatterOp>`.
fn parse_frontmatter_ops(operations: &[serde_json::Value]) -> Result<Vec<FrontmatterOp>, ApiError> {
let mut ops = Vec::with_capacity(operations.len());
for op_val in operations {
let op_str = op_val.get("op").and_then(|v| v.as_str()).ok_or_else(|| {
ApiError::bad_request("each operation must have an \"op\" string field")
})?;
match op_str {
"set" => {
let key = op_val.get("key").and_then(|v| v.as_str()).ok_or_else(|| {
ApiError::bad_request("\"set\" operation requires a \"key\" field")
})?;
let value = op_val
.get("value")
.and_then(|v| v.as_str())
.ok_or_else(|| {
ApiError::bad_request("\"set\" operation requires a \"value\" field")
})?;
ops.push(FrontmatterOp::Set(key.to_string(), value.to_string()));
}
"remove" => {
let key = op_val.get("key").and_then(|v| v.as_str()).ok_or_else(|| {
ApiError::bad_request("\"remove\" operation requires a \"key\" field")
})?;
ops.push(FrontmatterOp::Remove(key.to_string()));
}
"add_tag" => {
let value = op_val
.get("value")
.and_then(|v| v.as_str())
.ok_or_else(|| {
ApiError::bad_request("\"add_tag\" operation requires a \"value\" field")
})?;
ops.push(FrontmatterOp::AddTag(value.to_string()));
}
"remove_tag" => {
let value = op_val
.get("value")
.and_then(|v| v.as_str())
.ok_or_else(|| {
ApiError::bad_request("\"remove_tag\" operation requires a \"value\" field")
})?;
ops.push(FrontmatterOp::RemoveTag(value.to_string()));
}
"add_alias" => {
let value = op_val
.get("value")
.and_then(|v| v.as_str())
.ok_or_else(|| {
ApiError::bad_request("\"add_alias\" operation requires a \"value\" field")
})?;
ops.push(FrontmatterOp::AddAlias(value.to_string()));
}
"remove_alias" => {
let value = op_val
.get("value")
.and_then(|v| v.as_str())
.ok_or_else(|| {
ApiError::bad_request(
"\"remove_alias\" operation requires a \"value\" field",
)
})?;
ops.push(FrontmatterOp::RemoveAlias(value.to_string()));
}
unknown => {
return Err(ApiError::bad_request(&format!(
"unknown frontmatter operation: \"{unknown}\""
)));
}
}
}
Ok(ops)
}
// ---------------------------------------------------------------------------
// Write endpoint handlers
// ---------------------------------------------------------------------------
async fn handle_create(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<CreateBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let mut embedder = state.embedder.lock().await;
let input = CreateNoteInput {
content: body.content,
filename: body.filename,
type_hint: body.type_hint,
tags: body.tags,
folder: body.folder,
created_by: "http-api".into(),
};
let result = writer::create_note(
input,
&store,
&mut *embedder,
&state.vault_path,
state.profile.as_ref().as_ref(),
)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_append(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<AppendBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let mut embedder = state.embedder.lock().await;
let input = AppendInput {
file: body.file,
content: body.content,
modified_by: "http-api".into(),
};
let result = writer::append_to_note(input, &store, &mut *embedder, &state.vault_path)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_edit(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<EditBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let mode = match body.mode.as_deref().unwrap_or("append") {
"replace" => EditMode::Replace,
"prepend" => EditMode::Prepend,
_ => EditMode::Append,
};
let input = EditInput {
file: body.file,
heading: body.heading,
content: body.content,
mode,
modified_by: "http-api".into(),
};
let result = writer::edit_note(&store, &state.vault_path, &input, None)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_rewrite(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<RewriteBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let input = RewriteInput {
file: body.file,
content: body.content,
preserve_frontmatter: body.preserve_frontmatter.unwrap_or(true),
modified_by: "http-api".into(),
};
let result = writer::rewrite_note(&store, &state.vault_path, &input)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_edit_frontmatter(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<EditFrontmatterBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let ops = parse_frontmatter_ops(&body.operations)?;
let store = state.store.lock().await;
let input = EditFrontmatterInput {
file: body.file,
operations: ops,
modified_by: "http-api".into(),
};
let result = writer::edit_frontmatter(&store, &state.vault_path, &input)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_move(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<MoveBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let result = writer::move_note(&body.file, &body.new_folder, &store, &state.vault_path)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_archive(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<ArchiveBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let result = writer::archive_note(
&body.file,
&store,
&state.vault_path,
state.profile.as_ref().as_ref(),
)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_unarchive(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<UnarchiveBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let mut embedder = state.embedder.lock().await;
let result = writer::unarchive_note(&body.file, &store, &mut *embedder, &state.vault_path)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
async fn handle_update_metadata(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<UpdateMetadataBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let input = UpdateMetadataInput {
file: body.file,
tags: body.tags,
aliases: body.aliases,
modified_by: "http-api".into(),
};
let result = writer::update_metadata(input, &store, &state.vault_path)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
let full_path = state.vault_path.join(&result.path);
record_write(&state.recent_writes, &full_path).await;
Ok(Json(serde_json::json!(result)))
}
// ---------------------------------------------------------------------------
// Migration endpoint handlers
// ---------------------------------------------------------------------------
async fn handle_migrate_preview(
State(state): State<ApiState>,
headers: HeaderMap,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let profile_ref = state.profile.as_ref().as_ref();
let preview = crate::migrate::generate_preview(&store, &state.vault_path, profile_ref)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::to_value(&preview).unwrap()))
}
#[derive(Deserialize)]
struct MigrateApplyBody {
preview: serde_json::Value,
}
async fn handle_migrate_apply(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<MigrateApplyBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let preview: crate::migrate::MigrationPreview = serde_json::from_value(body.preview)
.map_err(|e| ApiError::bad_request(&format!("Invalid preview: {e}")))?;
let result = crate::migrate::apply_preview(&preview, &store, &state.vault_path)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::to_value(&result).unwrap()))
}
async fn handle_migrate_undo(
State(state): State<ApiState>,
headers: HeaderMap,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let result = crate::migrate::undo_last(&store, &state.vault_path)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::to_value(&result).unwrap()))
}
async fn handle_delete(
State(state): State<ApiState>,
headers: HeaderMap,
Json(body): Json<DeleteBody>,
) -> Result<impl IntoResponse, ApiError> {
authorize(&headers, &state, true)?;
let store = state.store.lock().await;
let mode = match body.mode.as_deref().unwrap_or("soft") {
"hard" => DeleteMode::Hard,
_ => DeleteMode::Soft,
};
let archive_folder = state
.profile
.as_ref()
.as_ref()
.and_then(|p| p.structure.folders.archive.as_deref())
.unwrap_or("04-Archive");
writer::delete_note(&store, &state.vault_path, &body.file, mode, archive_folder)
.map_err(|e| ApiError::internal(&format!("{e:#}")))?;
Ok(Json(serde_json::json!({
"deleted": body.file,
"mode": body.mode.as_deref().unwrap_or("soft"),
})))
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use std::time::SystemTime;
use axum::body::Body;
use tower::ServiceExt;
fn test_http_config() -> HttpConfig {
HttpConfig {
enabled: true,
port: 3000,
host: "127.0.0.1".to_string(),
rate_limit: 0,
cors_origins: vec![],
api_keys: vec![
ApiKeyConfig {
key: "eg_readkey".into(),
name: "reader".into(),
permissions: "read".into(),
},
ApiKeyConfig {
key: "eg_writekey".into(),
name: "writer".into(),
permissions: "write".into(),
},
],
plugin: crate::config::PluginConfig::default(),
}
}
/// Dummy embedder that returns zero vectors. Only used for constructing
/// `ApiState` in tests that don't exercise search/context endpoints.
struct DummyEmbedder;
impl crate::llm::EmbedModel for DummyEmbedder {
fn embed_batch(&mut self, texts: &[&str]) -> anyhow::Result<Vec<Vec<f32>>> {
Ok(texts.iter().map(|_| vec![0.0; 384]).collect())
}
fn token_count(&self, text: &str) -> usize {
text.split_whitespace().count()
}
fn dim(&self) -> usize {
384
}
}
fn test_api_state() -> ApiState {
let store = Store::open_memory().expect("in-memory store");
let config = test_http_config();
let rate_limiter = Arc::new(RateLimiter::new(config.rate_limit));
ApiState {