Skip to main content

argentor_core/
lib.rs

1//! Core types and error definitions for the Argentor framework.
2//!
3//! This crate provides the foundational types shared across all Argentor crates,
4//! including error handling, message representations, and tool call abstractions.
5//!
6//! # Main types
7//!
8//! - [`ArgentorError`] — Unified error enum for all Argentor subsystems.
9//! - [`ArgentorResult`] — Convenience alias for `Result<T, ArgentorError>`.
10//! - [`Role`] — Message role (user, assistant, system, tool).
11//! - [`Message`] — A single message within a conversation session.
12//! - [`ToolCall`] — Represents an LLM-initiated tool invocation request.
13//! - [`ToolResult`] — The result returned after executing a tool call.
14
15/// Approval types for human-in-the-loop workflows.
16pub mod approval;
17/// Distributed correlation context for request tracing across agent boundaries.
18pub mod correlation;
19/// Data residency configuration for multi-region compliance (GDPR, HIPAA, etc.).
20pub mod data_residency;
21/// Error aggregation, deduplication, and trending for production diagnostics.
22pub mod error_aggregator;
23/// Pub/sub event bus for decoupled component communication.
24pub mod event_bus;
25/// Multi-format metrics export for monitoring integration.
26pub mod metrics_export;
27/// OpenTelemetry distributed tracing support.
28pub mod telemetry;
29
30use chrono::{DateTime, Utc};
31use serde::{Deserialize, Serialize};
32use std::collections::HashMap;
33use uuid::Uuid;
34
35// --- Error types ---
36
37/// Top-level error type for the Argentor framework.
38///
39/// Each variant corresponds to a subsystem that can produce errors.
40#[derive(Debug, thiserror::Error)]
41pub enum ArgentorError {
42    /// An error originating from the agent execution loop.
43    #[error("Agent error: {0}")]
44    Agent(String),
45
46    /// An error from an outbound HTTP request (e.g. LLM API call).
47    #[error("HTTP error: {0}")]
48    Http(String),
49
50    /// An error related to session persistence or lookup.
51    #[error("Session error: {0}")]
52    Session(String),
53
54    /// An error in configuration parsing or validation.
55    #[error("Config error: {0}")]
56    Config(String),
57
58    /// An error raised by a skill during invocation.
59    #[error("Skill error: {0}")]
60    Skill(String),
61
62    /// An error from a communication channel (e.g. WebSocket, CLI).
63    #[error("Channel error: {0}")]
64    Channel(String),
65
66    /// An error from the API gateway layer.
67    #[error("Gateway error: {0}")]
68    Gateway(String),
69
70    /// A security-related error (permissions, TLS, rate limiting).
71    #[error("Security error: {0}")]
72    Security(String),
73
74    /// A JSON serialization or deserialization error.
75    #[error("JSON error: {0}")]
76    Json(#[from] serde_json::Error),
77
78    /// A standard I/O error.
79    #[error("IO error: {0}")]
80    Io(#[from] std::io::Error),
81
82    /// An error from the multi-agent orchestrator.
83    #[error("Orchestrator error: {0}")]
84    Orchestrator(String),
85}
86
87/// A convenience `Result` alias using [`ArgentorError`].
88pub type ArgentorResult<T> = Result<T, ArgentorError>;
89
90// --- Message types ---
91
92/// The role of the participant that authored a [`Message`].
93#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
94#[serde(rename_all = "lowercase")]
95pub enum Role {
96    /// A human end-user.
97    User,
98    /// The AI assistant.
99    Assistant,
100    /// A system-level instruction or prompt.
101    System,
102    /// Output produced by a tool invocation.
103    Tool,
104}
105
106/// A single message exchanged within a conversation session.
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct Message {
109    /// Unique identifier for this message.
110    pub id: Uuid,
111    /// The role of the message author.
112    pub role: Role,
113    /// The textual content of the message.
114    pub content: String,
115    /// The session this message belongs to.
116    pub session_id: Uuid,
117    /// UTC timestamp of when the message was created.
118    pub timestamp: DateTime<Utc>,
119    /// Arbitrary key-value metadata attached to the message.
120    #[serde(default)]
121    pub metadata: HashMap<String, serde_json::Value>,
122}
123
124impl Message {
125    /// Creates a new message with the given role, content, and session ID.
126    pub fn new(role: Role, content: impl Into<String>, session_id: Uuid) -> Self {
127        Self {
128            id: Uuid::new_v4(),
129            role,
130            content: content.into(),
131            session_id,
132            timestamp: Utc::now(),
133            metadata: HashMap::new(),
134        }
135    }
136
137    /// Creates a new message with [`Role::User`].
138    pub fn user(content: impl Into<String>, session_id: Uuid) -> Self {
139        Self::new(Role::User, content, session_id)
140    }
141
142    /// Creates a new message with [`Role::Assistant`].
143    pub fn assistant(content: impl Into<String>, session_id: Uuid) -> Self {
144        Self::new(Role::Assistant, content, session_id)
145    }
146
147    /// Creates a new message with [`Role::System`].
148    pub fn system(content: impl Into<String>, session_id: Uuid) -> Self {
149        Self::new(Role::System, content, session_id)
150    }
151}
152
153// --- Tool types ---
154
155/// A request from the LLM to invoke a specific tool.
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct ToolCall {
158    /// Unique identifier assigned by the LLM for this tool call.
159    pub id: String,
160    /// Name of the tool to invoke.
161    pub name: String,
162    /// JSON arguments to pass to the tool.
163    pub arguments: serde_json::Value,
164}
165
166/// The result returned after executing a [`ToolCall`].
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct ToolResult {
169    /// The ID of the [`ToolCall`] this result corresponds to.
170    pub call_id: String,
171    /// The textual output produced by the tool.
172    pub content: String,
173    /// Whether the tool execution ended in an error.
174    pub is_error: bool,
175}
176
177impl ToolResult {
178    /// Creates a successful tool result.
179    pub fn success(call_id: impl Into<String>, content: impl Into<String>) -> Self {
180        Self {
181            call_id: call_id.into(),
182            content: content.into(),
183            is_error: false,
184        }
185    }
186
187    /// Creates an error tool result.
188    pub fn error(call_id: impl Into<String>, content: impl Into<String>) -> Self {
189        Self {
190            call_id: call_id.into(),
191            content: content.into(),
192            is_error: true,
193        }
194    }
195}