forge-core 0.9.0

Core types and traits for the Forge framework
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
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
//! Shared types for the signals pipeline (product analytics + diagnostics).
//!
//! These types are used by both `forge-runtime` (server-side collection) and
//! client packages (event serialization).

use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Event types tracked by the signals pipeline.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SignalEventType {
    /// Page or screen view.
    PageView,
    /// Auto-captured backend RPC execution.
    RpcCall,
    /// Custom event from user code.
    Track,
    /// User identification (links anonymous activity to a user).
    Identify,
    /// Session started.
    SessionStart,
    /// Session ended (timeout or explicit close).
    SessionEnd,
    /// Frontend error or unhandled rejection.
    Error,
    /// Diagnostic breadcrumb for error reproduction.
    Breadcrumb,
    /// Web Vitals performance metric (LCP, CLS, INP, FCP, TTFB, etc.).
    WebVital,
    /// Background execution (job, cron, workflow step, webhook, daemon tick).
    ServerExecution,
}

impl std::fmt::Display for SignalEventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::PageView => write!(f, "page_view"),
            Self::RpcCall => write!(f, "rpc_call"),
            Self::Track => write!(f, "track"),
            Self::Identify => write!(f, "identify"),
            Self::SessionStart => write!(f, "session_start"),
            Self::SessionEnd => write!(f, "session_end"),
            Self::Error => write!(f, "error"),
            Self::Breadcrumb => write!(f, "breadcrumb"),
            Self::WebVital => write!(f, "web_vital"),
            Self::ServerExecution => write!(f, "server_execution"),
        }
    }
}

/// A single signal event ready for collection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignalEvent {
    pub event_type: SignalEventType,
    pub event_name: Option<String>,
    pub correlation_id: Option<String>,
    pub session_id: Option<Uuid>,
    pub visitor_id: Option<String>,
    pub user_id: Option<Uuid>,
    pub tenant_id: Option<Uuid>,
    pub properties: serde_json::Value,

    // Page context
    pub page_url: Option<String>,
    pub referrer: Option<String>,

    // RPC fields (denormalized for query performance)
    pub function_name: Option<String>,
    pub function_kind: Option<String>,
    pub duration_ms: Option<i32>,
    pub status: Option<String>,

    // Diagnostics
    pub error_message: Option<String>,
    pub error_stack: Option<String>,
    pub error_context: Option<serde_json::Value>,

    // Client context
    pub client_ip: Option<String>,
    pub country: Option<String>,
    pub city: Option<String>,
    pub user_agent: Option<String>,

    // Device classification (parsed from user_agent + platform header)
    pub device_type: Option<String>,
    pub browser: Option<String>,
    pub os: Option<String>,

    // Acquisition
    pub utm: Option<UtmParams>,

    // Classification
    pub is_bot: bool,

    pub timestamp: chrono::DateTime<chrono::Utc>,
}

impl SignalEvent {
    /// Create a server-initiated execution event (job, cron, workflow step,
    /// webhook, daemon tick). These runs have no client_ip/user_agent/visitor,
    /// only a function kind + name + outcome.
    pub fn server_execution(
        name: &str,
        kind: &str,
        duration_ms: i32,
        success: bool,
        error_message: Option<String>,
    ) -> Self {
        let n = name.to_string();
        Self {
            event_type: SignalEventType::ServerExecution,
            event_name: Some(n.clone()),
            correlation_id: None,
            session_id: None,
            visitor_id: None,
            user_id: None,
            tenant_id: None,
            properties: serde_json::Value::Object(serde_json::Map::new()),
            page_url: None,
            referrer: None,
            function_name: Some(n),
            function_kind: Some(kind.to_string()),
            duration_ms: Some(duration_ms),
            status: Some(if success { "success" } else { "error" }.to_string()),
            error_message,
            error_stack: None,
            error_context: None,
            client_ip: None,
            country: None,
            city: None,
            user_agent: None,
            device_type: None,
            browser: None,
            os: None,
            utm: None,
            is_bot: false,
            timestamp: chrono::Utc::now(),
        }
    }

    /// Create a diagnostic/audit event (auth failure, rate limit hit, etc.)
    /// with context from the request.
    pub fn diagnostic(
        event_name: &str,
        properties: serde_json::Value,
        client_ip: Option<String>,
        user_agent: Option<String>,
        visitor_id: Option<String>,
        user_id: Option<Uuid>,
        is_bot: bool,
    ) -> Self {
        Self {
            event_type: SignalEventType::Track,
            event_name: Some(event_name.to_string()),
            correlation_id: None,
            session_id: None,
            visitor_id,
            user_id,
            tenant_id: None,
            properties,
            page_url: None,
            referrer: None,
            function_name: None,
            function_kind: None,
            duration_ms: None,
            status: None,
            error_message: None,
            error_stack: None,
            error_context: None,
            client_ip,
            country: None,
            city: None,
            user_agent,
            device_type: None,
            browser: None,
            os: None,
            utm: None,
            is_bot,
            timestamp: chrono::Utc::now(),
        }
    }

    /// Create an RPC call event from function execution metadata.
    #[allow(clippy::too_many_arguments)]
    pub fn rpc_call(
        function_name: &str,
        function_kind: &str,
        duration_ms: i32,
        success: bool,
        user_id: Option<Uuid>,
        tenant_id: Option<Uuid>,
        correlation_id: Option<String>,
        client_ip: Option<String>,
        user_agent: Option<String>,
        visitor_id: Option<String>,
        is_bot: bool,
    ) -> Self {
        let name = function_name.to_string();
        Self {
            event_type: SignalEventType::RpcCall,
            event_name: Some(name.clone()),
            correlation_id,
            session_id: None,
            visitor_id,
            user_id,
            tenant_id,
            properties: serde_json::Value::Object(serde_json::Map::new()),
            page_url: None,
            referrer: None,
            function_name: Some(name),
            function_kind: Some(function_kind.to_string()),
            duration_ms: Some(duration_ms),
            status: Some(if success { "success" } else { "error" }.to_string()),
            error_message: None,
            error_stack: None,
            error_context: None,
            client_ip,
            country: None,
            city: None,
            user_agent,
            device_type: None,
            browser: None,
            os: None,
            utm: None,
            is_bot,
            timestamp: chrono::Utc::now(),
        }
    }
}

/// UTM campaign parameters for acquisition tracking.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UtmParams {
    pub source: Option<String>,
    pub medium: Option<String>,
    pub campaign: Option<String>,
    pub term: Option<String>,
    pub content: Option<String>,
}

/// Batch of events sent from client trackers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignalEventBatch {
    pub events: Vec<ClientEvent>,
    pub context: Option<ClientContext>,
}

/// A single event from the client tracker.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientEvent {
    pub event: String,
    #[serde(default)]
    pub properties: serde_json::Value,
    pub correlation_id: Option<String>,
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

/// Page view event from client.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PageViewPayload {
    pub url: String,
    pub referrer: Option<String>,
    pub title: Option<String>,
    pub utm_source: Option<String>,
    pub utm_medium: Option<String>,
    pub utm_campaign: Option<String>,
    pub utm_term: Option<String>,
    pub utm_content: Option<String>,
    pub correlation_id: Option<String>,
}

/// User identification payload.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdentifyPayload {
    pub user_id: String,
    #[serde(default)]
    pub traits: serde_json::Value,
}

/// Diagnostic error report from client.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiagnosticReport {
    pub errors: Vec<DiagnosticError>,
}

/// A single frontend error for diagnostics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiagnosticError {
    pub message: String,
    pub stack: Option<String>,
    pub context: Option<serde_json::Value>,
    pub correlation_id: Option<String>,
    pub breadcrumbs: Option<Vec<Breadcrumb>>,
    pub page_url: Option<String>,
}

/// User action breadcrumb for error reproduction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Breadcrumb {
    pub message: String,
    #[serde(default)]
    pub data: serde_json::Value,
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

/// Shared client context sent alongside event batches.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientContext {
    pub page_url: Option<String>,
    pub referrer: Option<String>,
    pub session_id: Option<String>,
}

/// Batch of Web Vitals / navigation metrics from a client tracker.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebVitalBatch {
    pub vitals: Vec<WebVitalEntry>,
    pub context: Option<ClientContext>,
}

/// A single Web Vitals sample (LCP, CLS, INP, FCP, TTFB, navigation, etc.).
///
/// `name` is the metric name (e.g. "lcp", "cls", "inp", "fcp", "ttfb",
/// "navigation", "long_task"). `value` is the numeric measurement (ms for
/// timings, unitless for CLS, etc.). Optional fields carry diagnostic
/// detail (rating, navigation type, attribution).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebVitalEntry {
    pub name: String,
    pub value: f64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rating: Option<String>,
    #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
    pub attribution: serde_json::Value,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub correlation_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

/// Response from signal ingestion endpoints.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignalResponse {
    pub ok: bool,
    /// Server-assigned session ID (returned on first event).
    pub session_id: Option<Uuid>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn signal_event_type_display_produces_snake_case() {
        assert_eq!(SignalEventType::PageView.to_string(), "page_view");
        assert_eq!(SignalEventType::RpcCall.to_string(), "rpc_call");
        assert_eq!(SignalEventType::Track.to_string(), "track");
        assert_eq!(SignalEventType::Identify.to_string(), "identify");
        assert_eq!(SignalEventType::SessionStart.to_string(), "session_start");
        assert_eq!(SignalEventType::SessionEnd.to_string(), "session_end");
        assert_eq!(SignalEventType::Error.to_string(), "error");
        assert_eq!(SignalEventType::Breadcrumb.to_string(), "breadcrumb");
        assert_eq!(SignalEventType::WebVital.to_string(), "web_vital");
        assert_eq!(
            SignalEventType::ServerExecution.to_string(),
            "server_execution"
        );
    }

    #[tokio::test]
    async fn signal_event_type_serde_round_trip() {
        let variants = [
            SignalEventType::PageView,
            SignalEventType::RpcCall,
            SignalEventType::Track,
            SignalEventType::Identify,
            SignalEventType::SessionStart,
            SignalEventType::SessionEnd,
            SignalEventType::Error,
            SignalEventType::Breadcrumb,
            SignalEventType::WebVital,
            SignalEventType::ServerExecution,
        ];

        for variant in variants {
            let json = serde_json::to_string(&variant).unwrap();
            let deserialized: SignalEventType = serde_json::from_str(&json).unwrap();
            assert_eq!(variant, deserialized);
        }
    }

    #[tokio::test]
    async fn rpc_call_sets_correct_fields_on_success() {
        let user_id = Uuid::parse_str("a1a2a3a4-b1b2-c1c2-d1d2-e1e2e3e4e5e6").unwrap();
        let tenant_id = Uuid::parse_str("f1f2f3f4-a1a2-b1b2-c1c2-d1d2d3d4d5d6").unwrap();

        let event = SignalEvent::rpc_call(
            "get_users",
            "query",
            42,
            true,
            Some(user_id),
            Some(tenant_id),
            Some("corr-123".to_string()),
            Some("127.0.0.1".to_string()),
            Some("test-agent".to_string()),
            Some("visitor-abc".to_string()),
            false,
        );

        assert_eq!(event.event_type, SignalEventType::RpcCall);
        assert_eq!(event.function_name.as_deref(), Some("get_users"));
        assert_eq!(event.function_kind.as_deref(), Some("query"));
        assert_eq!(event.duration_ms, Some(42));
        assert_eq!(event.status.as_deref(), Some("success"));
        assert!(event.device_type.is_none());
        assert!(event.browser.is_none());
        assert!(event.os.is_none());
        assert!(event.session_id.is_none());
        assert_eq!(
            event.properties,
            serde_json::Value::Object(serde_json::Map::new())
        );
    }

    #[tokio::test]
    async fn rpc_call_sets_error_status_when_not_success() {
        let event = SignalEvent::rpc_call(
            "create_user",
            "mutation",
            100,
            false,
            None,
            None,
            None,
            None,
            None,
            None,
            false,
        );

        assert_eq!(event.status.as_deref(), Some("error"));
    }

    #[tokio::test]
    async fn client_event_deserializes_with_timestamp() {
        let json = r#"{
            "event": "click",
            "properties": {"button": "submit"},
            "correlation_id": "abc",
            "timestamp": "2025-01-15T10:30:00Z"
        }"#;

        let event: ClientEvent = serde_json::from_str(json).unwrap();
        assert_eq!(event.event, "click");
        assert!(event.timestamp.is_some());
        assert_eq!(event.correlation_id.as_deref(), Some("abc"));
    }

    #[tokio::test]
    async fn client_event_deserializes_without_timestamp() {
        let json = r#"{
            "event": "click"
        }"#;

        let event: ClientEvent = serde_json::from_str(json).unwrap();
        assert_eq!(event.event, "click");
        assert!(event.timestamp.is_none());
        assert_eq!(event.properties, serde_json::Value::Null);
    }

    #[tokio::test]
    async fn page_view_payload_deserializes_with_all_utm_fields() {
        let json = r#"{
            "url": "https://example.com/page",
            "referrer": "https://google.com",
            "title": "Home",
            "utm_source": "google",
            "utm_medium": "cpc",
            "utm_campaign": "spring",
            "utm_term": "rust framework",
            "utm_content": "banner",
            "correlation_id": "corr-456"
        }"#;

        let payload: PageViewPayload = serde_json::from_str(json).unwrap();
        assert_eq!(payload.url, "https://example.com/page");
        assert_eq!(payload.referrer.as_deref(), Some("https://google.com"));
        assert_eq!(payload.title.as_deref(), Some("Home"));
        assert_eq!(payload.utm_source.as_deref(), Some("google"));
        assert_eq!(payload.utm_medium.as_deref(), Some("cpc"));
        assert_eq!(payload.utm_campaign.as_deref(), Some("spring"));
        assert_eq!(payload.utm_term.as_deref(), Some("rust framework"));
        assert_eq!(payload.utm_content.as_deref(), Some("banner"));
    }

    #[tokio::test]
    async fn page_view_payload_deserializes_with_only_url() {
        let json = r#"{"url": "https://example.com"}"#;

        let payload: PageViewPayload = serde_json::from_str(json).unwrap();
        assert_eq!(payload.url, "https://example.com");
        assert!(payload.referrer.is_none());
        assert!(payload.title.is_none());
        assert!(payload.utm_source.is_none());
        assert!(payload.utm_medium.is_none());
        assert!(payload.utm_campaign.is_none());
        assert!(payload.utm_term.is_none());
        assert!(payload.utm_content.is_none());
    }

    #[tokio::test]
    async fn diagnostic_error_deserializes_with_breadcrumbs() {
        let json = r#"{
            "message": "TypeError: null is not an object",
            "stack": "at foo.js:10",
            "breadcrumbs": [
                {"message": "clicked button", "data": {}, "timestamp": null},
                {"message": "navigated to /settings", "data": {"from": "/home"}}
            ]
        }"#;

        let error: DiagnosticError = serde_json::from_str(json).unwrap();
        assert_eq!(error.message, "TypeError: null is not an object");
        assert_eq!(error.stack.as_deref(), Some("at foo.js:10"));
        let breadcrumbs = error.breadcrumbs.unwrap();
        assert_eq!(breadcrumbs.len(), 2);
        assert_eq!(breadcrumbs[0].message, "clicked button");
        assert_eq!(breadcrumbs[1].message, "navigated to /settings");
    }

    #[tokio::test]
    async fn diagnostic_error_deserializes_with_null_breadcrumbs() {
        let json = r#"{
            "message": "ReferenceError: x is not defined",
            "stack": null,
            "context": null,
            "correlation_id": null,
            "breadcrumbs": null,
            "page_url": null
        }"#;

        let error: DiagnosticError = serde_json::from_str(json).unwrap();
        assert_eq!(error.message, "ReferenceError: x is not defined");
        assert!(error.breadcrumbs.is_none());
    }

    #[tokio::test]
    async fn signal_response_serializes_with_session_id() {
        let session_id = Uuid::parse_str("11111111-2222-3333-4444-555555555555").unwrap();
        let response = SignalResponse {
            ok: true,
            session_id: Some(session_id),
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"ok\":true"));
        assert!(json.contains("\"session_id\":\"11111111-2222-3333-4444-555555555555\""));
    }

    #[tokio::test]
    async fn signal_response_serializes_not_ok_with_no_session() {
        let response = SignalResponse {
            ok: false,
            session_id: None,
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"ok\":false"));
        assert!(json.contains("\"session_id\":null"));
    }

    #[tokio::test]
    async fn identify_payload_deserializes_with_traits() {
        let json = r#"{
            "user_id": "user-42",
            "traits": {"plan": "pro", "team_size": 5}
        }"#;

        let payload: IdentifyPayload = serde_json::from_str(json).unwrap();
        assert_eq!(payload.user_id, "user-42");
        assert_eq!(payload.traits["plan"], "pro");
        assert_eq!(payload.traits["team_size"], 5);
    }

    #[tokio::test]
    async fn identify_payload_deserializes_without_traits() {
        let json = r#"{"user_id": "user-99"}"#;

        let payload: IdentifyPayload = serde_json::from_str(json).unwrap();
        assert_eq!(payload.user_id, "user-99");
        assert_eq!(payload.traits, serde_json::Value::Null);
    }

    #[tokio::test]
    async fn client_context_deserializes_with_all_fields_none() {
        let json = r#"{
            "page_url": null,
            "referrer": null,
            "session_id": null
        }"#;

        let ctx: ClientContext = serde_json::from_str(json).unwrap();
        assert!(ctx.page_url.is_none());
        assert!(ctx.referrer.is_none());
        assert!(ctx.session_id.is_none());
    }
}