rucora-core 0.1.5

Core abstractions and types for the rucora LLM agent 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
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
//! Dual-track Metrics(双轨指标系统)
//!
//! 本模块提供分离的事件(ObserverEvent)与指标(ObserverMetric)系统:
//! - ObserverEvent: 结构化事件,用于日志、追踪、审计
//! - ObserverMetric: 数值指标,用于监控、告警、统计
//!
//! 参考实现: zeroclaw `observability_traits.rs`

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

use crate::provider::types::Usage;

/// 观测器事件(结构化事件)
///
/// 用于记录 Agent 执行过程中的关键事件,支持日志、追踪、审计等场景。
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "event_type", content = "payload")]
pub enum ObserverEvent {
    /// Agent 开始执行
    AgentStart {
        /// Agent 名称
        agent_name: String,
        /// Provider 名称
        provider: String,
        /// 模型名称
        model: String,
        /// 输入文本
        input_preview: String,
    },

    /// Agent 执行结束
    AgentEnd {
        /// Agent 名称
        agent_name: String,
        /// 执行步数
        steps: usize,
        /// Token 使用量
        usage: Option<Usage>,
        /// 估计成本(USD)
        cost_usd: Option<f64>,
    },

    /// LLM 请求开始
    LlmRequestStart {
        /// Provider 名称
        provider: String,
        /// 模型名称
        model: String,
        /// 消息数量
        messages_count: usize,
        /// 工具数量
        tools_count: usize,
    },

    /// LLM 响应完成
    LlmResponse {
        /// Provider 名称
        provider: String,
        /// 模型名称
        model: String,
        /// 请求耗时
        duration: Duration,
        /// 是否成功
        success: bool,
        /// Token 使用量
        usage: Option<Usage>,
    },

    /// LLM 流式响应块
    LlmStreamChunk {
        /// Provider 名称
        provider: String,
        /// 模型名称
        model: String,
        /// 块序号
        chunk_index: usize,
        /// 增量文本长度
        delta_len: usize,
    },

    /// 工具调用开始
    ToolCallStart {
        /// 工具名称
        tool: String,
        /// 调用 ID
        tool_call_id: String,
        /// 参数预览
        arguments_preview: String,
    },

    /// 工具调用完成
    ToolCallEnd {
        /// 工具名称
        tool: String,
        /// 调用 ID
        tool_call_id: String,
        /// 执行耗时
        duration: Duration,
        /// 是否成功
        success: bool,
        /// 输出预览
        output_preview: String,
    },

    /// 工具缓存命中
    ToolCacheHit {
        /// 工具名称
        tool: String,
        /// 节省的 Token 数(估算)
        tokens_saved: u64,
    },

    /// 工具缓存未命中
    ToolCacheMiss {
        /// 工具名称
        tool: String,
    },

    /// 熔断器开启
    CircuitBreakerOpen {
        /// 工具名称
        tool: String,
        /// 连续失败次数
        consecutive_failures: u32,
    },

    /// 熔断器关闭
    CircuitBreakerClose {
        /// 工具名称
        tool: String,
    },

    /// 重试事件
    ToolRetry {
        /// 工具名称
        tool: String,
        /// 重试次数
        attempt: u32,
        /// 最大重试次数
        max_retries: u32,
    },

    /// 超时事件
    ToolTimeout {
        /// 工具名称
        tool: String,
        /// 超时时间
        timeout: Duration,
    },

    /// 策略拒绝
    PolicyDenied {
        /// 工具名称
        tool: String,
        /// 规则 ID
        rule_id: String,
        /// 拒绝原因
        reason: String,
    },

    /// 步骤完成
    StepComplete {
        /// 步骤序号
        step: usize,
        /// 最大步骤数
        max_steps: usize,
        /// 本步决策类型
        decision_type: String,
    },

    /// 循环检测警告
    LoopDetected {
        /// 工具名称
        tool: String,
        /// 重复次数
        repeat_count: usize,
        /// 警告级别
        level: String,
    },

    /// Context Overflow 恢复
    ContextOverflowRecovered {
        /// 恢复策略
        strategy: String,
        /// 删除/截断的消息数
        messages_affected: usize,
    },

    /// 会话开始
    SessionStart {
        /// 会话 ID
        session_id: String,
        /// 渠道
        channel: String,
    },

    /// 会话结束
    SessionEnd {
        /// 会话 ID
        session_id: String,
        /// 持续时间
        duration: Duration,
    },

    /// 错误事件
    Error {
        /// 组件名称
        component: String,
        /// 错误类型
        error_type: String,
        /// 错误消息
        message: String,
    },

    /// 心跳事件
    Heartbeat {
        /// 活跃会话数
        active_sessions: u64,
        /// 队列深度
        queue_depth: u64,
    },

    /// 原始事件(用于自定义事件)
    Raw {
        /// 事件名称
        name: String,
        /// 事件数据
        data: serde_json::Value,
    },
}

/// 观测器指标(数值指标)
///
/// 用于监控、告警、统计等场景,可以被 Prometheus、OpenTelemetry 等系统收集。
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "metric_type", content = "value")]
pub enum ObserverMetric {
    /// 请求延迟(毫秒)
    RequestLatencyMs(f64),

    /// Token 使用量
    TokensUsed {
        /// 提示词 Token 数
        prompt: u64,
        /// 输出 Token 数
        completion: u64,
        /// 总 Token 数
        total: u64,
    },

    /// 活跃会话数
    ActiveSessions(u64),

    /// 队列深度
    QueueDepth(u64),

    /// 工具调用次数
    ToolCallCount {
        /// 工具名称
        tool: String,
        /// 调用次数
        count: u64,
    },

    /// 工具调用延迟(毫秒)
    ToolLatencyMs {
        /// 工具名称
        tool: String,
        /// 平均延迟
        avg_latency: f64,
    },

    /// 错误率
    ErrorRate {
        /// 错误类型
        error_type: String,
        /// 错误率(0.0 - 1.0)
        rate: f64,
    },

    /// 缓存命中率
    CacheHitRate(f64),

    /// 熔断器状态(0=关闭, 1=半开, 2=开启)
    CircuitBreakerState {
        /// 工具名称
        tool: String,
        /// 状态值
        state: u8,
    },

    /// 内存使用量(字节)
    MemoryUsageBytes(u64),

    /// CPU 使用率(百分比)
    CpuUsagePercent(f64),

    /// 自定义计数器
    Counter {
        /// 指标名称
        name: String,
        /// 计数
        value: u64,
        /// 标签
        labels: Option<HashMap<String, String>>,
    },

    /// 自定义计量器
    Gauge {
        /// 指标名称
        name: String,
        /// 数值
        value: f64,
        /// 标签
        labels: Option<HashMap<String, String>>,
    },

    /// 直方图
    Histogram {
        /// 指标名称
        name: String,
        /// 观测值
        value: f64,
        /// 桶边界
        buckets: Vec<f64>,
    },
}

/// 指标标签
pub type MetricLabels = HashMap<String, String>;

/// 双轨观测器 trait
///
/// 同时支持事件和指标的观测器接口。
#[async_trait::async_trait]
pub trait DualTrackObserver: Send + Sync {
    /// 记录事件
    fn observe_event(&self, event: ObserverEvent);

    /// 记录指标
    fn record_metric(&self, metric: ObserverMetric);

    /// 记录带标签的指标
    fn record_metric_with_labels(&self, metric: ObserverMetric, labels: MetricLabels) {
        let _ = labels;
        self.record_metric(metric);
    }

    /// 刷新缓冲区(如果有)
    fn flush(&self) {}
}

/// 多路复用观测器
///
/// 将事件和指标分发到多个后端观测器。
pub struct MultiObserver {
    event_observers: Vec<Box<dyn DualTrackObserver>>,
}

impl MultiObserver {
    /// 创建新的多路复用观测器
    pub fn new() -> Self {
        Self {
            event_observers: Vec::new(),
        }
    }

    /// 添加观测器
    pub fn add_observer(mut self, observer: Box<dyn DualTrackObserver>) -> Self {
        self.event_observers.push(observer);
        self
    }
}

impl Default for MultiObserver {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl DualTrackObserver for MultiObserver {
    fn observe_event(&self, event: ObserverEvent) {
        for observer in &self.event_observers {
            observer.observe_event(event.clone());
        }
    }

    fn record_metric(&self, metric: ObserverMetric) {
        for observer in &self.event_observers {
            observer.record_metric(metric.clone());
        }
    }

    fn flush(&self) {
        for observer in &self.event_observers {
            observer.flush();
        }
    }
}

/// 日志观测器
///
/// 将事件记录为结构化日志。
pub struct LoggingObserver {
    level: LogLevel,
}

impl LoggingObserver {
    /// 创建新的日志观测器
    pub fn new() -> Self {
        Self {
            level: LogLevel::Info,
        }
    }

    /// 设置日志级别
    pub fn with_level(mut self, level: LogLevel) -> Self {
        self.level = level;
        self
    }
}

impl Default for LoggingObserver {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl DualTrackObserver for LoggingObserver {
    #[allow(clippy::cognitive_complexity)]
    fn observe_event(&self, event: ObserverEvent) {
        match event {
            ObserverEvent::AgentStart { ref agent_name, .. } => {
                tracing::info!(agent_name, "agent.start");
            }
            ObserverEvent::AgentEnd {
                ref agent_name,
                steps,
                ..
            } => {
                tracing::info!(agent_name, steps, "agent.end");
            }
            ObserverEvent::LlmRequestStart {
                ref provider,
                ref model,
                ..
            } => {
                tracing::debug!(provider, model, "llm.request.start");
            }
            ObserverEvent::LlmResponse {
                ref provider,
                success,
                ..
            } => {
                if success {
                    tracing::debug!(provider, "llm.response.success");
                } else {
                    tracing::warn!(provider, "llm.response.failure");
                }
            }
            ObserverEvent::ToolCallStart { ref tool, .. } => {
                tracing::debug!(tool, "tool.call.start");
            }
            ObserverEvent::ToolCallEnd {
                ref tool, success, ..
            } => {
                if success {
                    tracing::debug!(tool, "tool.call.end");
                } else {
                    tracing::warn!(tool, "tool.call.failure");
                }
            }
            ObserverEvent::ToolCacheHit { ref tool, .. } => {
                tracing::debug!(tool, "tool.cache.hit");
            }
            ObserverEvent::ToolCacheMiss { ref tool } => {
                tracing::debug!(tool, "tool.cache.miss");
            }
            ObserverEvent::Error {
                ref component,
                ref message,
                ..
            } => {
                tracing::error!(component, message, "observer.error");
            }
            ObserverEvent::LoopDetected {
                ref tool,
                repeat_count,
                ..
            } => {
                tracing::warn!(tool, repeat_count, "loop.detected");
            }
            _ => {
                if self.level <= LogLevel::Debug {
                    tracing::debug!(event = ?event, "observer.event");
                }
            }
        }
    }

    #[allow(clippy::cognitive_complexity)]
    fn record_metric(&self, metric: ObserverMetric) {
        match metric {
            ObserverMetric::RequestLatencyMs(latency) => {
                tracing::debug!(latency_ms = latency, "metric.request_latency");
            }
            ObserverMetric::TokensUsed {
                prompt,
                completion,
                total,
            } => {
                tracing::debug!(prompt, completion, total, "metric.tokens_used");
            }
            ObserverMetric::ActiveSessions(count) => {
                tracing::debug!(count, "metric.active_sessions");
            }
            ObserverMetric::ToolCallCount { ref tool, count } => {
                tracing::debug!(tool, count, "metric.tool_call_count");
            }
            ObserverMetric::CacheHitRate(rate) => {
                tracing::debug!(rate, "metric.cache_hit_rate");
            }
            _ => {
                tracing::debug!(metric = ?metric, "metric.recorded");
            }
        }
    }
}

/// 详细日志观测器
///
/// 记录所有事件的详细信息,用于调试。
pub struct VerboseObserver;

impl VerboseObserver {
    /// 创建新的详细观测器
    pub fn new() -> Self {
        Self
    }
}

impl Default for VerboseObserver {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl DualTrackObserver for VerboseObserver {
    fn observe_event(&self, event: ObserverEvent) {
        tracing::trace!(event = ?event, "verbose.event");
    }

    fn record_metric(&self, metric: ObserverMetric) {
        tracing::trace!(metric = ?metric, "verbose.metric");
    }
}

/// 日志级别
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

/// 指标聚合器
///
/// 用于聚合多个指标值,计算统计信息。
pub struct MetricAggregator {
    values: Vec<f64>,
}

impl MetricAggregator {
    /// 创建新的聚合器
    pub fn new() -> Self {
        Self { values: Vec::new() }
    }

    /// 添加值
    pub fn add(&mut self, value: f64) {
        self.values.push(value);
    }

    /// 获取平均值
    pub fn average(&self) -> f64 {
        if self.values.is_empty() {
            0.0
        } else {
            self.values.iter().sum::<f64>() / self.values.len() as f64
        }
    }

    /// 获取最小值
    pub fn min(&self) -> f64 {
        self.values.iter().cloned().fold(f64::INFINITY, f64::min)
    }

    /// 获取最大值
    pub fn max(&self) -> f64 {
        self.values
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max)
    }

    /// 获取百分位数
    pub fn percentile(&self, p: f64) -> f64 {
        assert!(
            (0.0..=100.0).contains(&p),
            "percentile p must be between 0.0 and 100.0, got {}",
            p
        );
        if self.values.is_empty() {
            return 0.0;
        }

        let mut sorted = self.values.clone();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let index = ((p / 100.0) * (sorted.len() - 1) as f64).round() as usize;
        sorted[index.min(sorted.len() - 1)]
    }

    /// 获取样本数
    pub fn count(&self) -> usize {
        self.values.len()
    }

    /// 清空
    pub fn clear(&mut self) {
        self.values.clear();
    }
}

impl Default for MetricAggregator {
    fn default() -> Self {
        Self::new()
    }
}

/// 事件构建器
pub struct EventBuilder {
    event: Option<ObserverEvent>,
}

impl EventBuilder {
    /// 创建新的事件构建器
    pub fn new() -> Self {
        Self { event: None }
    }

    /// 设置 Agent 开始事件
    pub fn agent_start(
        mut self,
        agent_name: impl Into<String>,
        provider: impl Into<String>,
        model: impl Into<String>,
    ) -> Self {
        self.event = Some(ObserverEvent::AgentStart {
            agent_name: agent_name.into(),
            provider: provider.into(),
            model: model.into(),
            input_preview: String::new(),
        });
        self
    }

    /// 设置工具调用开始事件
    pub fn tool_call_start(
        mut self,
        tool: impl Into<String>,
        tool_call_id: impl Into<String>,
    ) -> Self {
        self.event = Some(ObserverEvent::ToolCallStart {
            tool: tool.into(),
            tool_call_id: tool_call_id.into(),
            arguments_preview: String::new(),
        });
        self
    }

    /// 构建事件
    pub fn build(self) -> Option<ObserverEvent> {
        self.event
    }
}

impl Default for EventBuilder {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_observer_event_serialization() {
        let event = ObserverEvent::AgentStart {
            agent_name: "test".to_string(),
            provider: "openai".to_string(),
            model: "gpt-4".to_string(),
            input_preview: "hello".to_string(),
        };

        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("AgentStart"));
        assert!(json.contains("test"));
    }

    #[test]
    fn test_observer_metric_serialization() {
        let metric = ObserverMetric::TokensUsed {
            prompt: 100,
            completion: 50,
            total: 150,
        };

        let json = serde_json::to_string(&metric).unwrap();
        assert!(json.contains("TokensUsed"));
        assert!(json.contains("100"));
    }

    #[test]
    fn test_metric_aggregator() {
        let mut agg = MetricAggregator::new();
        agg.add(1.0);
        agg.add(2.0);
        agg.add(3.0);
        agg.add(4.0);
        agg.add(5.0);

        assert_eq!(agg.average(), 3.0);
        assert_eq!(agg.min(), 1.0);
        assert_eq!(agg.max(), 5.0);
        assert_eq!(agg.count(), 5);
        assert_eq!(agg.percentile(50.0), 3.0);
    }

    #[test]
    fn test_event_builder() {
        let event = EventBuilder::new()
            .agent_start("my_agent", "openai", "gpt-4")
            .build();

        assert!(matches!(event, Some(ObserverEvent::AgentStart { .. })));
    }
}