juncture-core 0.2.0

Core types and traits for Juncture state machine framework
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
use serde::{Deserialize, Serialize};

/// Message type for LLM conversations.
///
/// Used in agent workflows with message-based state.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Message {
    /// Unique message identifier
    pub id: String,
    /// Message role (system, human, ai, tool)
    pub role: Role,
    /// Message content (text or multimodal)
    pub content: Content,
    /// Tool calls made by the AI (for AI messages)
    pub tool_calls: Vec<ToolCall>,
    /// Tool call ID this message responds to (for tool messages)
    pub tool_call_id: Option<String>,
    /// Optional name for the message sender
    pub name: Option<String>,
    /// Token usage information from LLM API responses
    pub usage: Option<TokenUsage>,
}

/// Message role
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum Role {
    /// System message
    System,
    /// Human/user message
    Human,
    /// AI/assistant message
    Ai,
    /// Tool result message
    Tool,
}

/// Message content
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Content {
    /// Simple text content
    Text(String),
    /// Multimodal content with multiple parts
    MultiPart(Vec<ContentPart>),
}

/// Content part for multimodal messages
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ContentPart {
    /// Text content
    Text { text: String },
    /// Image data
    Image(ImageData),
    /// Extended thinking content (Anthropic API)
    ///
    /// Contains the model's internal reasoning process without affecting tool calls.
    Thinking {
        text: String,
        signature: Option<String>,
    },
}

/// Image data for multimodal content
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ImageData {
    /// Media type (e.g., "image/png", "image/jpeg")
    pub media_type: String,
    /// Image source data
    pub source: ImageSource,
}

/// Image source for multimodal content
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ImageSource {
    /// Base64-encoded image data
    Base64(String),
    /// Image URL
    Url(String),
}

/// Tool call within a message
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ToolCall {
    /// Unique tool call identifier
    pub id: String,
    /// Tool name
    pub name: String,
    /// Tool arguments as JSON value
    pub arguments: serde_json::Value,
}

/// Token usage information from LLM API responses
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct TokenUsage {
    /// Number of input tokens
    pub input_tokens: u64,
    /// Number of output tokens
    pub output_tokens: u64,
    /// Total tokens used
    pub total_tokens: u64,
}

/// Special sentinel: remove all messages
///
/// Used to clear the entire messages list.
pub const REMOVE_ALL_MESSAGES: &str = "__remove_all__";

/// Built-in state for simple chat agents
///
/// Provides a zero-config entry point for simple chat agents with a
/// single `messages` field using the messages reducer semantics.
///
/// # Examples
///
/// ```
/// use juncture_core::state::messages::{MessagesState, Message};
///
/// let mut state = MessagesState::default();
/// state.messages.push(Message::human("Hello"));
/// state.messages.push(Message::ai("Hi there!"));
/// ```
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct MessagesState {
    /// Message history using append+merge+delete semantics
    pub messages: Vec<Message>,
}

/// Update type for `MessagesState`
///
/// All fields are optional to support partial updates.
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct MessagesStateUpdate {
    /// Optional messages update
    pub messages: Option<Vec<Message>>,
}

impl crate::State for MessagesState {
    type Update = MessagesStateUpdate;
    type FieldVersions = crate::state::FieldVersions;

    fn apply(&mut self, update: Self::Update) -> crate::FieldsChanged {
        let mut changed = crate::FieldsChanged(0);

        if let Some(messages) = update.messages {
            messages_reducer(&mut self.messages, messages);
            changed.0 |= 1 << 0;
        }

        changed
    }

    fn reset_ephemeral(&mut self) {
        // No ephemeral fields in MessagesState
    }
}

impl MessagesState {
    /// Apply an update with structured error propagation for reducer violations
    ///
    /// The messages reducer is an append+merge+delete reducer that never
    /// conflicts, so this always succeeds. Provided for trait consistency.
    ///
    /// # Errors
    ///
    /// This method never returns an error, as the messages reducer has no
    /// write-conflict semantics. The `Result` return type is for API
    /// consistency with `State::try_apply()`.
    pub fn try_apply_messages(
        &mut self,
        update: MessagesStateUpdate,
    ) -> Result<crate::FieldsChanged, crate::error::InvalidUpdateError> {
        Ok(crate::State::apply(self, update))
    }
}

/// Messages reducer with append+merge+delete semantics
///
/// Handles message updates, deletions, and appends.
/// - If message ID matches existing message: update it
/// - If message ID starts with "__remove__:": delete that message
/// - If message is `REMOVE_ALL_MESSAGES`: clear all messages
/// - Otherwise: append the message
pub fn messages_reducer(current: &mut Vec<Message>, incoming: Vec<Message>) {
    for msg in incoming {
        if msg.id == REMOVE_ALL_MESSAGES {
            current.clear();
        } else if msg.id.starts_with("__remove__:") {
            let target_id = &msg.id["__remove__:".len()..];
            current.retain(|m| m.id != target_id);
        } else if let Some(existing) = current.iter_mut().find(|m| m.id == msg.id) {
            *existing = msg;
        } else {
            current.push(msg);
        }
    }
}

impl Message {
    /// Create a human message
    pub fn human(content: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            role: Role::Human,
            content: Content::Text(content.into()),
            tool_calls: vec![],
            tool_call_id: None,
            name: None,
            usage: None,
        }
    }

    /// Create an AI message
    pub fn ai(content: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            role: Role::Ai,
            content: Content::Text(content.into()),
            tool_calls: vec![],
            tool_call_id: None,
            name: None,
            usage: None,
        }
    }

    /// Create an AI message with tool calls
    pub fn ai_with_tool_calls(content: impl Into<String>, tool_calls: Vec<ToolCall>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            role: Role::Ai,
            content: Content::Text(content.into()),
            tool_calls,
            tool_call_id: None,
            name: None,
            usage: None,
        }
    }

    /// Create a system message
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            role: Role::System,
            content: Content::Text(content.into()),
            tool_calls: vec![],
            tool_call_id: None,
            name: None,
            usage: None,
        }
    }

    /// Create a tool result message
    pub fn tool_result(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            role: Role::Tool,
            content: Content::Text(content.into()),
            tool_calls: vec![],
            tool_call_id: Some(tool_call_id.into()),
            name: None,
            usage: None,
        }
    }

    /// Check if message has tool calls
    #[must_use]
    pub const fn has_tool_calls(&self) -> bool {
        !self.tool_calls.is_empty()
    }

    /// Extract text content from the message
    ///
    /// For `Content::Text`, returns the text directly.
    /// For `Content::MultiPart`, returns the text of the first `ContentPart::Text` found,
    /// or an empty string if no text part exists.
    #[must_use]
    pub fn content_text(&self) -> &str {
        match &self.content {
            Content::Text(s) => s,
            Content::MultiPart(parts) => parts
                .iter()
                .find_map(|p| match p {
                    ContentPart::Text { text } => Some(text.as_str()),
                    _ => None,
                })
                .unwrap_or(""),
        }
    }

    /// Create a remove message sentinel
    #[must_use]
    pub fn remove(id: impl Into<String>) -> Self {
        let id = id.into();
        Self {
            id: format!("__remove__:{id}"),
            role: Role::System,
            content: Content::Text(String::new()),
            tool_calls: vec![],
            tool_call_id: None,
            name: None,
            usage: None,
        }
    }

    /// Create a remove-all message sentinel
    ///
    /// This message clears the entire messages list when processed
    /// by the messages reducer. The sentinel has a special ID
    /// (`REMOVE_ALL_MESSAGES`) that triggers the clear operation.
    #[must_use]
    pub fn remove_all() -> Self {
        Self {
            id: REMOVE_ALL_MESSAGES.to_string(),
            role: Role::System,
            content: Content::Text(String::new()),
            tool_calls: vec![],
            tool_call_id: None,
            name: None,
            usage: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::trait_::State;

    #[test]
    fn test_messages_state_default() {
        let state = MessagesState::default();
        assert!(state.messages.is_empty());
    }

    #[test]
    fn test_messages_state_apply() {
        let mut state = MessagesState::default();

        let update = MessagesStateUpdate {
            messages: Some(vec![Message::human("Hello")]),
        };

        let changed = state.apply(update);
        assert_eq!(state.messages.len(), 1);
        assert!(!changed.is_empty());
        assert!(changed.has_field(0));
    }

    #[test]
    fn test_messages_state_apply_merge() {
        let mut state = MessagesState {
            messages: vec![Message::human("Hello")],
        };

        let update = MessagesStateUpdate {
            messages: Some(vec![Message::ai("Hi there!")]),
        };

        state.apply(update);
        assert_eq!(state.messages.len(), 2);
    }

    #[test]
    fn test_messages_state_apply_none() {
        let mut state = MessagesState {
            messages: vec![Message::human("Hello")],
        };

        let update = MessagesStateUpdate { messages: None };

        let changed = state.apply(update);
        assert_eq!(state.messages.len(), 1);
        assert!(changed.is_empty());
    }

    #[test]
    fn test_messages_state_reset_ephemeral() {
        let mut state = MessagesState {
            messages: vec![Message::human("Hello")],
        };

        state.reset_ephemeral();
        // No-op for MessagesState since it has no ephemeral fields
        assert_eq!(state.messages.len(), 1);
    }

    #[test]
    fn test_messages_state_serialization() {
        let state = MessagesState {
            messages: vec![Message::human("Hello")],
        };

        let json = serde_json::to_string(&state).unwrap();
        let deserialized: MessagesState = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.messages.len(), 1);
        assert_eq!(deserialized.messages[0].role, Role::Human);
    }

    #[test]
    fn test_messages_state_update_serialization() {
        let update = MessagesStateUpdate {
            messages: Some(vec![Message::ai("Hi!")]),
        };

        let json = serde_json::to_string(&update).unwrap();
        let deserialized: MessagesStateUpdate = serde_json::from_str(&json).unwrap();

        assert!(deserialized.messages.is_some());
        assert_eq!(deserialized.messages.unwrap().len(), 1);
    }
}

// Rust guideline compliant 2026-05-20