swarm-engine-core 0.1.6

Core types and orchestration for SwarmEngine
Documentation
//! RecordStream - Record のコレクション操作

use super::{ActionRecord, DependencyGraphRecord, LlmCallRecord, Record};

/// Record のストリームを操作するためのヘルパー
pub struct RecordStream<'a> {
    records: &'a [Record],
}

impl<'a> RecordStream<'a> {
    pub fn new(records: &'a [Record]) -> Self {
        Self { records }
    }

    /// Action Records のみ抽出
    pub fn actions(&self) -> impl Iterator<Item = &ActionRecord> {
        self.records.iter().filter_map(Record::as_action)
    }

    /// Llm Records のみ抽出
    pub fn llm_calls(&self) -> impl Iterator<Item = &LlmCallRecord> {
        self.records.iter().filter_map(Record::as_llm)
    }

    /// DependencyGraph Records のみ抽出
    pub fn dependency_graphs(&self) -> impl Iterator<Item = &DependencyGraphRecord> {
        self.records.iter().filter_map(Record::as_dependency_graph)
    }

    /// Worker ID でフィルタ
    pub fn by_worker(&self, worker_id: usize) -> impl Iterator<Item = &Record> {
        self.records
            .iter()
            .filter(move |r| r.worker_id() == Some(worker_id))
    }

    /// Worker ID ごとにグルーピング
    pub fn group_by_worker(&self) -> std::collections::HashMap<usize, Vec<&Record>> {
        let mut groups = std::collections::HashMap::new();
        for record in self.records {
            if let Some(worker_id) = record.worker_id() {
                groups
                    .entry(worker_id)
                    .or_insert_with(Vec::new)
                    .push(record);
            }
        }
        groups
    }

    /// タイムスタンプでソートした Iterator
    pub fn sorted_by_time(&self) -> Vec<&Record> {
        let mut sorted: Vec<_> = self.records.iter().collect();
        sorted.sort_by_key(|r| r.timestamp_ms());
        sorted
    }

    /// Record 数
    pub fn len(&self) -> usize {
        self.records.len()
    }

    /// 空かどうか
    pub fn is_empty(&self) -> bool {
        self.records.is_empty()
    }
}