tuitbot-server 0.1.49

HTTP API server for Tuitbot autonomous X growth assistant
Documentation
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
//! Vault selection endpoints for receiving Ghostwriter selections from the
//! Obsidian plugin and retrieving stored selections.

use std::sync::Arc;

use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde::{Deserialize, Serialize};

use tuitbot_core::config::DeploymentMode;
use tuitbot_core::context::{graph_expansion, retrieval};
use tuitbot_core::storage::vault_selections;

use crate::account::AccountContext;
use crate::state::AppState;
use crate::ws::{AccountWsEvent, WsEvent};

// ---------------------------------------------------------------------------
// POST /api/vault/send-selection
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
pub struct SendSelectionRequest {
    pub vault_name: String,
    pub file_path: String,
    pub selected_text: String,
    #[serde(default)]
    pub heading_context: Option<String>,
    #[serde(default)]
    pub selection_start_line: i64,
    #[serde(default)]
    pub selection_end_line: i64,
    #[serde(default)]
    pub note_title: Option<String>,
    #[serde(default)]
    pub frontmatter_tags: Option<Vec<String>>,
}

#[derive(Serialize)]
pub struct SendSelectionResponse {
    pub status: String,
    pub session_id: String,
    pub composer_url: String,
}

#[derive(Serialize)]
struct SelectionError {
    error: String,
    message: String,
}

fn error_response(status: StatusCode, error: &str, message: &str) -> Response {
    (
        status,
        Json(SelectionError {
            error: error.to_string(),
            message: message.to_string(),
        }),
    )
        .into_response()
}

pub async fn send_selection(
    State(state): State<Arc<AppState>>,
    ctx: AccountContext,
    Json(body): Json<SendSelectionRequest>,
) -> Response {
    // Cloud mode privacy gate — vault selections must not be accepted in
    // cloud deployments because the raw text never leaves the user's device.
    if state.deployment_mode == DeploymentMode::Cloud {
        return error_response(
            StatusCode::FORBIDDEN,
            "cloud_mode_prohibited",
            "Vault selections are not available in cloud mode",
        );
    }

    // Validate vault_name
    if body.vault_name.is_empty() || body.vault_name.len() > 255 {
        return error_response(
            StatusCode::UNPROCESSABLE_ENTITY,
            "validation_error",
            "vault_name must be 1-255 characters",
        );
    }

    // Validate file_path
    if body.file_path.is_empty() || !body.file_path.ends_with(".md") {
        return error_response(
            StatusCode::UNPROCESSABLE_ENTITY,
            "validation_error",
            "file_path must be non-empty and end in .md",
        );
    }

    // Validate selected_text
    if body.selected_text.trim().is_empty() {
        return error_response(
            StatusCode::UNPROCESSABLE_ENTITY,
            "validation_error",
            "selected_text must not be empty",
        );
    }

    if body.selected_text.len() > 10_000 {
        return error_response(
            StatusCode::PAYLOAD_TOO_LARGE,
            "payload_too_large",
            "selected_text exceeds 10000 character limit",
        );
    }

    // Validate line range
    if body.selection_end_line < body.selection_start_line {
        return error_response(
            StatusCode::UNPROCESSABLE_ENTITY,
            "validation_error",
            "selection_end_line must be >= selection_start_line",
        );
    }

    // Rate limit: 10 per minute per account
    match vault_selections::count_recent_for(&state.db, &ctx.account_id, 60).await {
        Ok(count) if count >= 10 => {
            return error_response(
                StatusCode::TOO_MANY_REQUESTS,
                "rate_limited",
                "Rate limit exceeded. Try again in 6 seconds.",
            );
        }
        Err(e) => {
            tracing::error!(error = %e, "failed to check selection rate limit");
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                "internal_error",
                "Failed to check rate limit",
            );
        }
        _ => {}
    }

    // Generate session_id and expires_at
    let session_id = uuid::Uuid::new_v4().to_string();
    let expires_at = chrono::Utc::now()
        .checked_add_signed(chrono::Duration::minutes(30))
        .unwrap_or_else(chrono::Utc::now)
        .format("%Y-%m-%dT%H:%M:%S")
        .to_string();

    // Resolve block identity (best-effort)
    let (resolved_node_id, resolved_chunk_id) = retrieval::resolve_selection_identity(
        &state.db,
        &ctx.account_id,
        &body.file_path,
        body.heading_context.as_deref(),
    )
    .await
    .unwrap_or((None, None));

    // Serialize frontmatter_tags to JSON string
    let tags_json = body
        .frontmatter_tags
        .as_ref()
        .and_then(|tags| serde_json::to_string(tags).ok());

    // Insert
    let insert_result = vault_selections::insert_selection(
        &state.db,
        &ctx.account_id,
        &session_id,
        &body.vault_name,
        &body.file_path,
        &body.selected_text,
        body.heading_context.as_deref(),
        body.selection_start_line,
        body.selection_end_line,
        body.note_title.as_deref(),
        tags_json.as_deref(),
        resolved_node_id,
        resolved_chunk_id,
        &expires_at,
    )
    .await;

    if let Err(e) = insert_result {
        tracing::error!(error = %e, "failed to store vault selection");
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            "internal_error",
            "Failed to store selection",
        );
    }

    // Emit WebSocket event (best-effort, ignore send errors)
    let _ = state.event_tx.send(AccountWsEvent {
        account_id: ctx.account_id,
        event: WsEvent::SelectionReceived {
            session_id: session_id.clone(),
        },
    });

    let composer_url = format!("/compose?selection={session_id}");

    (
        StatusCode::OK,
        Json(SendSelectionResponse {
            status: "received".to_string(),
            session_id,
            composer_url,
        }),
    )
        .into_response()
}

// ---------------------------------------------------------------------------
// GET /api/vault/selection/{session_id}
// ---------------------------------------------------------------------------

#[derive(Serialize)]
pub struct GetSelectionResponse {
    pub session_id: String,
    pub vault_name: String,
    pub file_path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selected_text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub heading_context: Option<String>,
    pub selection_start_line: i64,
    pub selection_end_line: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note_title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frontmatter_tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolved_node_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolved_chunk_id: Option<i64>,
    pub privacy_envelope: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub graph_neighbors: Option<Vec<super::NeighborItem>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub graph_state: Option<String>,
}

pub async fn get_selection(
    State(state): State<Arc<AppState>>,
    ctx: AccountContext,
    Path(session_id): Path<String>,
) -> Response {
    let selection =
        match vault_selections::get_selection_by_session(&state.db, &ctx.account_id, &session_id)
            .await
        {
            Ok(Some(sel)) => sel,
            Ok(None) => {
                return error_response(
                    StatusCode::NOT_FOUND,
                    "not_found",
                    "Selection not found or expired",
                );
            }
            Err(e) => {
                tracing::error!(error = %e, "failed to get vault selection");
                return error_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "internal_error",
                    "Failed to retrieve selection",
                );
            }
        };

    let is_cloud = state.deployment_mode == DeploymentMode::Cloud;

    // Cloud mode: omit selected_text (privacy invariant)
    let selected_text = if is_cloud {
        None
    } else {
        Some(selection.selected_text)
    };

    // Parse frontmatter_tags from JSON string
    let frontmatter_tags: Option<Vec<String>> = selection
        .frontmatter_tags
        .as_deref()
        .and_then(|s| serde_json::from_str(s).ok());

    // Auto-expand graph neighbors when a resolved node exists.
    let (graph_neighbors, graph_state) = if let Some(node_id) = selection.resolved_node_id {
        let result = crate::routes::rag_helpers::resolve_graph_suggestions(
            &state,
            &ctx.account_id,
            node_id,
            graph_expansion::DEFAULT_MAX_NEIGHBORS,
        )
        .await;
        let state_str = serde_json::to_value(&result.graph_state)
            .ok()
            .and_then(|v| v.as_str().map(String::from));
        let items: Vec<super::NeighborItem> = result
            .neighbors
            .into_iter()
            .map(|n| super::NeighborItem::from_graph_neighbor(n, is_cloud))
            .collect();
        (Some(items), state_str)
    } else {
        (None, None)
    };

    (
        StatusCode::OK,
        Json(GetSelectionResponse {
            session_id: selection.session_id,
            vault_name: selection.vault_name,
            file_path: selection.file_path,
            selected_text,
            heading_context: selection.heading_context,
            selection_start_line: selection.selection_start_line,
            selection_end_line: selection.selection_end_line,
            note_title: selection.note_title,
            frontmatter_tags,
            resolved_node_id: selection.resolved_node_id,
            resolved_chunk_id: selection.resolved_chunk_id,
            privacy_envelope: state.deployment_mode.privacy_envelope().to_string(),
            graph_neighbors,
            graph_state,
        }),
    )
        .into_response()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn send_selection_request_deserialization() {
        let json = r##"{
            "vault_name": "marketing",
            "file_path": "notes/test.md",
            "selected_text": "Hello world",
            "heading_context": "# Title > ## Section",
            "selection_start_line": 10,
            "selection_end_line": 15,
            "note_title": "Test",
            "frontmatter_tags": ["rust", "async"]
        }"##;
        let req: SendSelectionRequest = serde_json::from_str(json).expect("deserialize");
        assert_eq!(req.vault_name, "marketing");
        assert_eq!(req.file_path, "notes/test.md");
        assert_eq!(req.selected_text, "Hello world");
        assert_eq!(req.heading_context.as_deref(), Some("# Title > ## Section"));
        assert_eq!(req.selection_start_line, 10);
        assert_eq!(req.selection_end_line, 15);
        assert_eq!(req.note_title.as_deref(), Some("Test"));
        assert_eq!(
            req.frontmatter_tags,
            Some(vec!["rust".to_string(), "async".to_string()])
        );
    }

    #[test]
    fn send_selection_request_defaults() {
        let json = r#"{
            "vault_name": "v",
            "file_path": "n.md",
            "selected_text": "text"
        }"#;
        let req: SendSelectionRequest = serde_json::from_str(json).expect("deserialize");
        assert!(req.heading_context.is_none());
        assert_eq!(req.selection_start_line, 0);
        assert_eq!(req.selection_end_line, 0);
        assert!(req.note_title.is_none());
        assert!(req.frontmatter_tags.is_none());
    }

    #[test]
    fn send_selection_response_serialization() {
        let resp = SendSelectionResponse {
            status: "received".to_string(),
            session_id: "abc-123".to_string(),
            composer_url: "/compose?selection=abc-123".to_string(),
        };
        let json = serde_json::to_value(&resp).expect("serialize");
        assert_eq!(json["status"], "received");
        assert_eq!(json["session_id"], "abc-123");
        assert_eq!(json["composer_url"], "/compose?selection=abc-123");
    }

    #[test]
    fn get_selection_response_omits_none_fields() {
        let resp = GetSelectionResponse {
            session_id: "s".to_string(),
            vault_name: "v".to_string(),
            file_path: "f.md".to_string(),
            selected_text: None,
            heading_context: None,
            selection_start_line: 0,
            selection_end_line: 0,
            note_title: None,
            frontmatter_tags: None,
            resolved_node_id: None,
            resolved_chunk_id: None,
            privacy_envelope: "local_first".to_string(),
            graph_neighbors: None,
            graph_state: None,
        };
        let json = serde_json::to_value(&resp).expect("serialize");
        assert!(json.get("selected_text").is_none());
        assert!(json.get("heading_context").is_none());
        assert!(json.get("note_title").is_none());
        assert!(json.get("frontmatter_tags").is_none());
        assert!(json.get("resolved_node_id").is_none());
        assert!(json.get("resolved_chunk_id").is_none());
        assert!(json.get("graph_neighbors").is_none());
        assert!(json.get("graph_state").is_none());
        assert_eq!(json["privacy_envelope"], "local_first");
    }

    #[test]
    fn get_selection_response_includes_privacy_envelope() {
        let resp = GetSelectionResponse {
            session_id: "s".to_string(),
            vault_name: "v".to_string(),
            file_path: "f.md".to_string(),
            selected_text: Some("text".to_string()),
            heading_context: None,
            selection_start_line: 0,
            selection_end_line: 0,
            note_title: None,
            frontmatter_tags: None,
            resolved_node_id: None,
            resolved_chunk_id: None,
            privacy_envelope: "user_controlled".to_string(),
            graph_neighbors: None,
            graph_state: None,
        };
        let json = serde_json::to_value(&resp).expect("serialize");
        assert_eq!(json["privacy_envelope"], "user_controlled");
        assert_eq!(json["selected_text"], "text");
    }
}