lc_agents/handoffs/
handoff.rs1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::collections::HashMap;
6
7#[derive(Debug)]
9pub struct Handoff {
10 pub target_agent: String,
12 pub task: String,
14 pub context: Option<HandoffContext>,
16}
17
18#[derive(Debug)]
20pub struct HandoffContext {
21 pub original_request: String,
23 pub current_result: Option<String>,
25 pub conversation_summary: Option<String>,
29 pub metadata: HashMap<String, Value>,
31}
32
33impl HandoffContext {
34 pub fn new(original_request: impl Into<String>) -> Self {
36 Self {
37 original_request: original_request.into(),
38 current_result: None,
39 conversation_summary: None,
40 metadata: HashMap::new(),
41 }
42 }
43
44 pub fn with_summary(mut self, summary: impl Into<String>) -> Self {
46 self.conversation_summary = Some(summary.into());
47 self
48 }
49
50 pub fn with_result(mut self, result: impl Into<String>) -> Self {
52 self.current_result = Some(result.into());
53 self
54 }
55}
56
57pub struct HandoffResult {
59 pub agent_name: String,
61 pub result: String,
63 pub next_handoff: Option<Box<Handoff>>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct HandoffRecord {
70 pub from_agent: String,
72 pub to_agent: String,
74 pub task: String,
76 pub result: String,
78 pub timestamp: String,
80}
81
82#[derive(Debug)]
84#[non_exhaustive]
85pub enum HandoffError {
86 AgentNotFound(String),
88 ExecutionError(String),
90 HandoffCycleDetected(String),
92 MaxHandoffDepthExceeded(usize),
94}
95
96impl std::fmt::Display for HandoffError {
97 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98 match self {
99 HandoffError::AgentNotFound(name) => {
100 write!(f, "Agent does not exist: {}", name)
101 }
102 HandoffError::ExecutionError(msg) => write!(f, "Agent execution error: {}", msg),
103 HandoffError::HandoffCycleDetected(name) => {
104 write!(f, "handoff cycle detected: {} already in the handoff chain, cyclic handoff rejected", name)
105 }
106 HandoffError::MaxHandoffDepthExceeded(depth) => {
107 write!(f, "handoff depth exceeded the limit: {}", depth)
108 }
109 }
110 }
111}
112
113impl std::error::Error for HandoffError {}