haki-core 0.1.0

Agent loop and session management for haki
Documentation
//! Core message types for the agent loop.
//!
//! haki-llm owns the wire types (Message, Role). This module adds the richer
//! in-memory types needed by the agent loop: tool calls, tool results, and
//! multi-block content.

use serde::{Deserialize, Serialize};

// Re-export the LLM wire types so callers only import from haki-core.
pub use haki_llm::{Message, Role};

// ─── Tool types ───────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    pub id: String,
    pub name: String,
    pub input: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
    pub tool_call_id: String,
    pub content: String,
    pub is_error: bool,
}

// ─── Rich content for multi-block messages ────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlock {
    Text { text: String },
    ToolUse(ToolCall),
    ToolResult(ToolResult),
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    #[test]
    fn tool_call_roundtrip() {
        let tc = ToolCall {
            id: "call_1".into(),
            name: "bash".into(),
            input: serde_json::json!({ "command": "ls" }),
        };
        let json = serde_json::to_string(&tc).unwrap();
        let back: ToolCall = serde_json::from_str(&json).unwrap();
        assert_eq!(back.name, "bash");
    }

    #[test]
    fn tool_result_error_flag() {
        let tr = ToolResult {
            tool_call_id: "call_1".into(),
            content: "permission denied".into(),
            is_error: true,
        };
        assert!(tr.is_error);
    }

    #[test]
    fn content_block_text_tag() {
        let block = ContentBlock::Text { text: "hello".into() };
        let json = serde_json::to_value(&block).unwrap();
        assert_eq!(json["type"], "text");
    }
}