swarm-engine-core 0.1.6

Core types and orchestration for SwarmEngine
Documentation
//! LlmCallRecord - LLM呼び出しの記録

use serde::{Deserialize, Serialize};

use crate::util::epoch_millis;

/// LLM呼び出しの記録
///
/// LlmDebugEvent から変換可能(swarm-engine-llm crate で実装)。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmCallRecord {
    /// 呼び出し種別("decide", "call_raw" 等)
    pub call_type: String,
    /// プロンプト
    pub prompt: String,
    /// レスポンス
    pub response: String,
    /// モデル名
    pub model: String,
    /// エンドポイント
    pub endpoint: String,
    /// LoRAアダプター名
    pub lora: Option<String>,
    /// レイテンシ(ミリ秒)
    pub latency_ms: u64,
    /// タイムスタンプ(Unix epoch ms)
    pub timestamp_ms: u64,
    /// Worker ID
    pub worker_id: Option<usize>,
    /// エラー(あれば)
    pub error: Option<String>,
}

impl LlmCallRecord {
    pub fn new(call_type: impl Into<String>, model: impl Into<String>) -> Self {
        Self {
            call_type: call_type.into(),
            prompt: String::new(),
            response: String::new(),
            model: model.into(),
            endpoint: String::new(),
            lora: None,
            latency_ms: 0,
            timestamp_ms: epoch_millis(),
            worker_id: None,
            error: None,
        }
    }

    pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt = prompt.into();
        self
    }

    pub fn response(mut self, response: impl Into<String>) -> Self {
        self.response = response.into();
        self
    }

    pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint = endpoint.into();
        self
    }

    pub fn lora(mut self, lora: impl Into<String>) -> Self {
        self.lora = Some(lora.into());
        self
    }

    pub fn latency_ms(mut self, latency: u64) -> Self {
        self.latency_ms = latency;
        self
    }

    pub fn worker_id(mut self, id: usize) -> Self {
        self.worker_id = Some(id);
        self
    }

    pub fn error(mut self, error: impl Into<String>) -> Self {
        self.error = Some(error.into());
        self
    }

    /// 成功したか(エラーがなく、レスポンスがある)
    pub fn is_success(&self) -> bool {
        self.error.is_none() && !self.response.is_empty()
    }
}