acton-ai 0.26.0

An agentic AI framework where each agent is an actor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
//! Core message type definitions.
//!
//! All messages implement Send + Sync + Debug + Clone + 'static as required by acton-reactive.

use crate::agent::AgentConfig;
use crate::llm::SamplingParams;
use crate::types::{AgentId, CorrelationId, TaskId};
use acton_reactive::prelude::*;
use serde::{Deserialize, Serialize};

// =============================================================================
// Kernel Messages
// =============================================================================

/// Request to spawn a new agent.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct SpawnAgent {
    /// Configuration for the new agent
    pub config: AgentConfig,
}

/// Response after spawning an agent.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct AgentSpawned {
    /// The ID of the newly spawned agent
    pub agent_id: AgentId,
}

/// Request to stop an agent.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct StopAgent {
    /// The ID of the agent to stop
    pub agent_id: AgentId,
}

/// Request for agent status.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct GetAgentStatus {
    /// The ID of the agent to query
    pub agent_id: AgentId,
}

/// Response with agent status.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct AgentStatusResponse {
    /// The ID of the agent
    pub agent_id: AgentId,
    /// The current state of the agent
    pub state: String,
    /// Number of messages in conversation
    pub conversation_length: usize,
}

/// Message routed between agents.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct RouteMessage {
    /// The agent sending the message
    pub from: AgentId,
    /// The agent receiving the message
    pub to: AgentId,
    /// The message content
    pub payload: String,
}

// =============================================================================
// Agent Messages
// =============================================================================

/// User prompt sent to an agent.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct UserPrompt {
    /// Unique identifier for this request-response cycle
    pub correlation_id: CorrelationId,
    /// The user's message content
    pub content: String,
}

impl UserPrompt {
    /// Creates a new UserPrompt with a fresh correlation ID.
    #[must_use]
    pub fn new(content: impl Into<String>) -> Self {
        Self {
            correlation_id: CorrelationId::new(),
            content: content.into(),
        }
    }
}

/// Request the current status of an agent (read-only).
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct GetStatus;

/// Internal message to update agent state.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct UpdateState {
    /// The new state string
    pub new_state: String,
}

// =============================================================================
// Conversation Messages
// =============================================================================

/// A message in a conversation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Message {
    /// The role of the message sender
    pub role: MessageRole,
    /// The content of the message
    pub content: String,
    /// Optional tool calls in this message
    pub tool_calls: Option<Vec<ToolCall>>,
    /// ID of the tool call this message responds to
    pub tool_call_id: Option<String>,
}

impl Message {
    /// Creates a new user message.
    #[must_use]
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::User,
            content: content.into(),
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Creates a new assistant message.
    #[must_use]
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::Assistant,
            content: content.into(),
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Creates a new assistant message with tool calls.
    #[must_use]
    pub fn assistant_with_tools(content: impl Into<String>, tool_calls: Vec<ToolCall>) -> Self {
        Self {
            role: MessageRole::Assistant,
            content: content.into(),
            tool_calls: Some(tool_calls),
            tool_call_id: None,
        }
    }

    /// Creates a new tool response message.
    #[must_use]
    pub fn tool(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::Tool,
            content: content.into(),
            tool_calls: None,
            tool_call_id: Some(tool_call_id.into()),
        }
    }

    /// Creates a new system message.
    #[must_use]
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: MessageRole::System,
            content: content.into(),
            tool_calls: None,
            tool_call_id: None,
        }
    }
}

/// The role of a message sender.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// System instructions
    System,
    /// User input
    User,
    /// Assistant response
    Assistant,
    /// Tool response
    Tool,
}

impl std::fmt::Display for MessageRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::System => write!(f, "system"),
            Self::User => write!(f, "user"),
            Self::Assistant => write!(f, "assistant"),
            Self::Tool => write!(f, "tool"),
        }
    }
}

// =============================================================================
// LLM Messages
// =============================================================================

/// Request to the LLM provider.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct LLMRequest {
    /// Correlation ID for matching request to response
    pub correlation_id: CorrelationId,
    /// The agent making the request
    pub agent_id: AgentId,
    /// The messages to send to the LLM
    pub messages: Vec<Message>,
    /// Optional tool definitions available to the LLM
    pub tools: Option<Vec<ToolDefinition>>,
    /// Optional sampling parameters for this request
    pub sampling: Option<SamplingParams>,
}

impl LLMRequest {
    /// Creates a simple request with just user content.
    ///
    /// IDs are generated internally - users don't need to manage them.
    /// This is the simplest way to create an LLM request.
    ///
    /// # Example
    ///
    /// ```
    /// use acton_ai::messages::LLMRequest;
    ///
    /// let request = LLMRequest::simple("What is the capital of France?");
    /// assert!(!request.messages.is_empty());
    /// ```
    #[must_use]
    pub fn simple(content: impl Into<String>) -> Self {
        Self {
            correlation_id: CorrelationId::new(),
            agent_id: AgentId::new(),
            messages: vec![Message::user(content)],
            tools: None,
            sampling: None,
        }
    }

    /// Creates a request with a system prompt and user content.
    ///
    /// IDs are generated internally.
    ///
    /// # Example
    ///
    /// ```
    /// use acton_ai::messages::LLMRequest;
    ///
    /// let request = LLMRequest::with_system(
    ///     "You are a helpful assistant.",
    ///     "What is 2 + 2?"
    /// );
    /// assert_eq!(request.messages.len(), 2);
    /// ```
    #[must_use]
    pub fn with_system(system: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            correlation_id: CorrelationId::new(),
            agent_id: AgentId::new(),
            messages: vec![Message::system(system), Message::user(content)],
            tools: None,
            sampling: None,
        }
    }

    /// Creates a builder for advanced request configuration.
    ///
    /// Use the builder when you need to:
    /// - Set explicit correlation or agent IDs (for tracking/persistence)
    /// - Add multiple messages
    /// - Include tool definitions
    ///
    /// # Example
    ///
    /// ```
    /// use acton_ai::messages::LLMRequest;
    ///
    /// let request = LLMRequest::builder()
    ///     .system("You are a helpful assistant.")
    ///     .user("Hello!")
    ///     .build();
    /// ```
    #[must_use]
    pub fn builder() -> LLMRequestBuilder {
        LLMRequestBuilder::default()
    }
}

/// Builder for constructing LLM requests with advanced options.
///
/// Use `LLMRequest::builder()` to create an instance.
///
/// # Example
///
/// ```
/// use acton_ai::messages::LLMRequest;
/// use acton_ai::types::CorrelationId;
///
/// let request = LLMRequest::builder()
///     .correlation_id(CorrelationId::new())
///     .system("You are an expert.")
///     .user("Explain Rust ownership.")
///     .build();
/// ```
#[derive(Default)]
pub struct LLMRequestBuilder {
    correlation_id: Option<CorrelationId>,
    agent_id: Option<AgentId>,
    messages: Vec<Message>,
    tools: Option<Vec<ToolDefinition>>,
    sampling: Option<SamplingParams>,
}

impl LLMRequestBuilder {
    /// Sets an explicit correlation ID.
    ///
    /// Use this when you need to track requests across systems
    /// or match requests to responses manually.
    #[must_use]
    pub fn correlation_id(mut self, id: CorrelationId) -> Self {
        self.correlation_id = Some(id);
        self
    }

    /// Sets an explicit agent ID.
    ///
    /// Use this in multi-agent scenarios where you need to
    /// identify which agent made the request.
    #[must_use]
    pub fn agent_id(mut self, id: AgentId) -> Self {
        self.agent_id = Some(id);
        self
    }

    /// Adds a system message.
    #[must_use]
    pub fn system(mut self, content: impl Into<String>) -> Self {
        self.messages.push(Message::system(content));
        self
    }

    /// Adds a user message.
    #[must_use]
    pub fn user(mut self, content: impl Into<String>) -> Self {
        self.messages.push(Message::user(content));
        self
    }

    /// Adds an assistant message.
    #[must_use]
    pub fn assistant(mut self, content: impl Into<String>) -> Self {
        self.messages.push(Message::assistant(content));
        self
    }

    /// Adds a custom message.
    #[must_use]
    pub fn message(mut self, message: Message) -> Self {
        self.messages.push(message);
        self
    }

    /// Adds multiple messages.
    #[must_use]
    pub fn messages(mut self, messages: impl IntoIterator<Item = Message>) -> Self {
        self.messages.extend(messages);
        self
    }

    /// Sets the tool definitions available to the LLM.
    #[must_use]
    pub fn tools(mut self, tools: Vec<ToolDefinition>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Adds a single tool definition.
    #[must_use]
    pub fn tool(mut self, tool: ToolDefinition) -> Self {
        self.tools.get_or_insert_with(Vec::new).push(tool);
        self
    }

    /// Sets the sampling parameters for this request.
    #[must_use]
    pub fn sampling(mut self, params: SamplingParams) -> Self {
        self.sampling = Some(params);
        self
    }

    /// Builds the LLM request.
    ///
    /// IDs are auto-generated if not explicitly set.
    #[must_use]
    pub fn build(self) -> LLMRequest {
        LLMRequest {
            correlation_id: self.correlation_id.unwrap_or_default(),
            agent_id: self.agent_id.unwrap_or_default(),
            messages: self.messages,
            tools: self.tools,
            sampling: self.sampling,
        }
    }
}

/// Complete response from the LLM (non-streaming).
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct LLMResponse {
    /// Correlation ID matching the request
    pub correlation_id: CorrelationId,
    /// The generated content
    pub content: String,
    /// Tool calls requested by the LLM
    pub tool_calls: Option<Vec<ToolCall>>,
    /// The reason the model stopped generating
    pub stop_reason: StopReason,
}

/// The reason the LLM stopped generating.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopReason {
    /// Normal completion
    EndTurn,
    /// Reached maximum tokens
    MaxTokens,
    /// Model wants to call tools
    ToolUse,
    /// User-initiated stop
    StopSequence,
}

// =============================================================================
// Streaming LLM Messages
// =============================================================================

/// Indicates the start of a streaming LLM response.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct LLMStreamStart {
    /// Correlation ID for this stream
    pub correlation_id: CorrelationId,
}

/// A single token in a streaming response.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct LLMStreamToken {
    /// Correlation ID for this stream
    pub correlation_id: CorrelationId,
    /// The token text
    pub token: String,
}

/// A tool call in a streaming response.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct LLMStreamToolCall {
    /// Correlation ID for this stream
    pub correlation_id: CorrelationId,
    /// The tool call
    pub tool_call: ToolCall,
}

/// Indicates the end of a streaming LLM response.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct LLMStreamEnd {
    /// Correlation ID for this stream
    pub correlation_id: CorrelationId,
    /// The reason the stream ended
    pub stop_reason: StopReason,
}

// =============================================================================
// Tool Messages
// =============================================================================

/// Definition of a tool that can be called by the LLM.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolDefinition {
    /// The name of the tool
    pub name: String,
    /// Description of what the tool does
    pub description: String,
    /// JSON Schema for the tool's input parameters
    pub input_schema: serde_json::Value,
}

/// A tool call requested by the LLM.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolCall {
    /// Unique ID for this tool call
    pub id: String,
    /// The name of the tool to call
    pub name: String,
    /// The arguments to pass to the tool (as JSON)
    pub arguments: serde_json::Value,
}

/// Request to execute a tool.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct ExecuteTool {
    /// Correlation ID for matching to response
    pub correlation_id: CorrelationId,
    /// The tool call to execute
    pub tool_call: ToolCall,
    /// The agent requesting the tool execution
    pub requesting_agent: AgentId,
}

/// Response from tool execution.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct ToolResponse {
    /// Correlation ID matching the request
    pub correlation_id: CorrelationId,
    /// The ID of the tool call this responds to
    pub tool_call_id: String,
    /// The result of the tool execution (success content or error message)
    pub result: Result<String, String>,
}

// =============================================================================
// System Events (Pub/Sub)
// =============================================================================

/// System-wide events broadcast via the broker.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub enum SystemEvent {
    /// An agent was spawned
    AgentSpawned {
        /// The ID of the spawned agent
        id: AgentId,
    },
    /// An agent stopped
    AgentStopped {
        /// The ID of the stopped agent
        id: AgentId,
        /// The reason for stopping
        reason: String,
    },
    /// A tool was registered
    ToolRegistered {
        /// The name of the registered tool
        name: String,
    },
    /// Rate limit was hit
    RateLimitHit {
        /// The provider that hit the limit
        provider: String,
        /// Seconds until retry is allowed
        retry_after_secs: u64,
    },
}

// =============================================================================
// Multi-Agent Messages (Phase 6)
// =============================================================================

/// Direct message between agents.
///
/// Agents can send messages directly to other agents via the kernel's routing.
/// The kernel forwards messages to the target agent.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct AgentMessage {
    /// The agent sending the message
    pub from: AgentId,
    /// The target agent to receive the message
    pub to: AgentId,
    /// The message content
    pub content: String,
    /// Optional metadata (JSON value for extensibility)
    pub metadata: Option<serde_json::Value>,
}

impl AgentMessage {
    /// Creates a new agent message.
    #[must_use]
    pub fn new(from: AgentId, to: AgentId, content: impl Into<String>) -> Self {
        Self {
            from,
            to,
            content: content.into(),
            metadata: None,
        }
    }

    /// Adds metadata to the message.
    #[must_use]
    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
        self.metadata = Some(metadata);
        self
    }
}

/// Request to delegate a task to another agent.
///
/// The delegating agent creates this message to assign work to a specialist agent.
/// The task_id serves as a correlation ID for tracking the result.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct DelegateTask {
    /// The agent delegating the task
    pub from: AgentId,
    /// The agent to perform the task
    pub to: AgentId,
    /// Unique identifier for this task
    pub task_id: TaskId,
    /// The type of task (e.g., "code_review", "summarize", "translate")
    pub task_type: String,
    /// Task payload as JSON
    pub payload: serde_json::Value,
    /// Optional deadline for the task
    pub deadline: Option<std::time::Duration>,
}

impl DelegateTask {
    /// Creates a new task delegation.
    #[must_use]
    pub fn new(
        from: AgentId,
        to: AgentId,
        task_type: impl Into<String>,
        payload: serde_json::Value,
    ) -> Self {
        Self {
            from,
            to,
            task_id: TaskId::new(),
            task_type: task_type.into(),
            payload,
            deadline: None,
        }
    }

    /// Sets a deadline for task completion.
    #[must_use]
    pub fn with_deadline(mut self, deadline: std::time::Duration) -> Self {
        self.deadline = Some(deadline);
        self
    }
}

/// Acknowledgment that a task was accepted.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct TaskAccepted {
    /// The task that was accepted
    pub task_id: TaskId,
    /// The agent that accepted the task
    pub agent_id: AgentId,
}

/// Notification that a delegated task completed successfully.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct TaskCompleted {
    /// The task that completed
    pub task_id: TaskId,
    /// The result of the task as JSON
    pub result: serde_json::Value,
}

/// Notification that a delegated task failed.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct TaskFailed {
    /// The task that failed
    pub task_id: TaskId,
    /// The error message
    pub error: String,
}

/// Announcement of agent capabilities for discovery.
///
/// Agents broadcast this message to announce what they can do.
/// Other agents or the kernel can track these capabilities.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct AnnounceCapabilities {
    /// The agent announcing its capabilities
    pub agent_id: AgentId,
    /// List of capability strings (e.g., "code_review", "translation", "summarization")
    pub capabilities: Vec<String>,
}

impl AnnounceCapabilities {
    /// Creates a new capability announcement.
    #[must_use]
    pub fn new(agent_id: AgentId, capabilities: Vec<String>) -> Self {
        Self {
            agent_id,
            capabilities,
        }
    }
}

/// Request to find an agent with a specific capability.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct FindCapableAgent {
    /// The capability to search for
    pub capability: String,
    /// Correlation ID for the response
    pub correlation_id: CorrelationId,
}

impl FindCapableAgent {
    /// Creates a new capability search request.
    #[must_use]
    pub fn new(capability: impl Into<String>) -> Self {
        Self {
            capability: capability.into(),
            correlation_id: CorrelationId::new(),
        }
    }
}

/// Response to a capability search request.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct CapableAgentFound {
    /// The correlation ID from the request
    pub correlation_id: CorrelationId,
    /// The agent with the capability, if found
    pub agent_id: Option<AgentId>,
    /// The capability that was searched for
    pub capability: String,
}

/// Incoming message from another agent (delivered to agent).
///
/// This is what an agent receives when another agent sends it a message.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct IncomingAgentMessage {
    /// The agent that sent the message
    pub from: AgentId,
    /// The message content
    pub content: String,
    /// Optional metadata
    pub metadata: Option<serde_json::Value>,
}

impl From<AgentMessage> for IncomingAgentMessage {
    fn from(msg: AgentMessage) -> Self {
        Self {
            from: msg.from,
            content: msg.content,
            metadata: msg.metadata,
        }
    }
}

/// Incoming task delegation (delivered to agent).
///
/// This is what an agent receives when another agent delegates a task to it.
#[acton_message]
#[derive(Serialize, Deserialize)]
pub struct IncomingTask {
    /// The agent that delegated the task
    pub from: AgentId,
    /// The task identifier
    pub task_id: TaskId,
    /// The type of task
    pub task_type: String,
    /// The task payload
    pub payload: serde_json::Value,
    /// Optional deadline
    pub deadline: Option<std::time::Duration>,
}

impl IncomingTask {
    /// Creates an IncomingTask from a DelegateTask message.
    #[must_use]
    pub fn from_delegate(msg: &DelegateTask) -> Self {
        Self {
            from: msg.from.clone(),
            task_id: msg.task_id.clone(),
            task_type: msg.task_type.clone(),
            payload: msg.payload.clone(),
            deadline: msg.deadline,
        }
    }
}

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

    #[test]
    fn user_prompt_creates_correlation_id() {
        let prompt1 = UserPrompt::new("Hello");
        let prompt2 = UserPrompt::new("World");

        assert_ne!(prompt1.correlation_id, prompt2.correlation_id);
    }

    #[test]
    fn message_user_creation() {
        let msg = Message::user("Hello, agent!");

        assert_eq!(msg.role, MessageRole::User);
        assert_eq!(msg.content, "Hello, agent!");
        assert!(msg.tool_calls.is_none());
        assert!(msg.tool_call_id.is_none());
    }

    #[test]
    fn message_assistant_creation() {
        let msg = Message::assistant("I can help with that.");

        assert_eq!(msg.role, MessageRole::Assistant);
        assert_eq!(msg.content, "I can help with that.");
    }

    #[test]
    fn message_assistant_with_tools() {
        let tool_calls = vec![ToolCall {
            id: "tc_123".to_string(),
            name: "search".to_string(),
            arguments: serde_json::json!({"query": "Rust actors"}),
        }];

        let msg = Message::assistant_with_tools("Let me search for that.", tool_calls);

        assert_eq!(msg.role, MessageRole::Assistant);
        assert!(msg.tool_calls.is_some());
        assert_eq!(msg.tool_calls.as_ref().unwrap().len(), 1);
    }

    #[test]
    fn message_tool_response() {
        let msg = Message::tool("tc_123", "Search results: ...");

        assert_eq!(msg.role, MessageRole::Tool);
        assert_eq!(msg.tool_call_id, Some("tc_123".to_string()));
    }

    #[test]
    fn message_role_display() {
        assert_eq!(MessageRole::System.to_string(), "system");
        assert_eq!(MessageRole::User.to_string(), "user");
        assert_eq!(MessageRole::Assistant.to_string(), "assistant");
        assert_eq!(MessageRole::Tool.to_string(), "tool");
    }

    #[test]
    fn tool_definition_serialization() {
        let tool = ToolDefinition {
            name: "calculator".to_string(),
            description: "Performs basic arithmetic".to_string(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "expression": {"type": "string"}
                },
                "required": ["expression"]
            }),
        };

        let json = serde_json::to_string(&tool).unwrap();
        let deserialized: ToolDefinition = serde_json::from_str(&json).unwrap();

        assert_eq!(tool, deserialized);
    }

    #[test]
    fn system_event_agent_spawned() {
        let agent_id = AgentId::new();
        let event = SystemEvent::AgentSpawned {
            id: agent_id.clone(),
        };

        if let SystemEvent::AgentSpawned { id } = event {
            assert_eq!(id, agent_id);
        } else {
            panic!("Expected AgentSpawned event");
        }
    }

    #[test]
    fn stop_reason_serialization() {
        let reasons = vec![
            StopReason::EndTurn,
            StopReason::MaxTokens,
            StopReason::ToolUse,
            StopReason::StopSequence,
        ];

        for reason in reasons {
            let json = serde_json::to_string(&reason).unwrap();
            let deserialized: StopReason = serde_json::from_str(&json).unwrap();
            assert_eq!(reason, deserialized);
        }
    }

    // LLMRequest convenience method tests
    #[test]
    fn llm_request_simple_creates_user_message() {
        let request = LLMRequest::simple("Hello");

        assert_eq!(request.messages.len(), 1);
        assert_eq!(request.messages[0].role, MessageRole::User);
        assert_eq!(request.messages[0].content, "Hello");
        assert!(request.tools.is_none());
    }

    #[test]
    fn llm_request_simple_generates_ids() {
        let request1 = LLMRequest::simple("Hello");
        let request2 = LLMRequest::simple("World");

        assert_ne!(request1.correlation_id, request2.correlation_id);
        assert_ne!(request1.agent_id, request2.agent_id);
    }

    #[test]
    fn llm_request_with_system_creates_two_messages() {
        let request = LLMRequest::with_system("Be helpful", "Hello");

        assert_eq!(request.messages.len(), 2);
        assert_eq!(request.messages[0].role, MessageRole::System);
        assert_eq!(request.messages[0].content, "Be helpful");
        assert_eq!(request.messages[1].role, MessageRole::User);
        assert_eq!(request.messages[1].content, "Hello");
    }

    #[test]
    fn llm_request_builder_basic() {
        let request = LLMRequest::builder().user("Hello").build();

        assert_eq!(request.messages.len(), 1);
        assert_eq!(request.messages[0].content, "Hello");
    }

    #[test]
    fn llm_request_builder_with_system_and_user() {
        let request = LLMRequest::builder()
            .system("Be concise")
            .user("What is 2+2?")
            .build();

        assert_eq!(request.messages.len(), 2);
        assert_eq!(request.messages[0].role, MessageRole::System);
        assert_eq!(request.messages[1].role, MessageRole::User);
    }

    #[test]
    fn llm_request_builder_with_explicit_ids() {
        let corr_id = CorrelationId::new();
        let agent_id = AgentId::new();

        let request = LLMRequest::builder()
            .correlation_id(corr_id.clone())
            .agent_id(agent_id.clone())
            .user("Hello")
            .build();

        assert_eq!(request.correlation_id, corr_id);
        assert_eq!(request.agent_id, agent_id);
    }

    #[test]
    fn llm_request_builder_with_tools() {
        let tool = ToolDefinition {
            name: "calculator".to_string(),
            description: "Math".to_string(),
            input_schema: serde_json::json!({}),
        };

        let request = LLMRequest::builder()
            .user("Calculate 2+2")
            .tool(tool.clone())
            .build();

        assert!(request.tools.is_some());
        assert_eq!(request.tools.as_ref().unwrap().len(), 1);
        assert_eq!(request.tools.as_ref().unwrap()[0].name, "calculator");
    }

    #[test]
    fn llm_request_builder_with_multiple_tools() {
        let tools = vec![
            ToolDefinition {
                name: "calc".to_string(),
                description: "Math".to_string(),
                input_schema: serde_json::json!({}),
            },
            ToolDefinition {
                name: "search".to_string(),
                description: "Search".to_string(),
                input_schema: serde_json::json!({}),
            },
        ];

        let request = LLMRequest::builder().user("Hello").tools(tools).build();

        assert!(request.tools.is_some());
        assert_eq!(request.tools.as_ref().unwrap().len(), 2);
    }

    #[test]
    fn llm_request_builder_with_assistant() {
        let request = LLMRequest::builder()
            .user("Hello")
            .assistant("Hi there!")
            .user("How are you?")
            .build();

        assert_eq!(request.messages.len(), 3);
        assert_eq!(request.messages[1].role, MessageRole::Assistant);
    }

    #[test]
    fn llm_request_builder_with_custom_message() {
        let custom_msg = Message::tool("tc_123", "Result: 4");

        let request = LLMRequest::builder()
            .user("Calculate 2+2")
            .message(custom_msg)
            .build();

        assert_eq!(request.messages.len(), 2);
        assert_eq!(request.messages[1].role, MessageRole::Tool);
    }

    #[test]
    fn llm_request_builder_generates_ids_when_not_set() {
        let request1 = LLMRequest::builder().user("Hello").build();
        let request2 = LLMRequest::builder().user("World").build();

        assert_ne!(request1.correlation_id, request2.correlation_id);
        assert_ne!(request1.agent_id, request2.agent_id);
    }
}