machi 0.8.1

A Web3-native AI Agent 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
413
414
//! Streaming response types for LLM operations.
//!
//! This module provides types for handling streaming responses from LLM providers,
//! enabling real-time display of generated content.

use serde::{Deserialize, Serialize};

use crate::usage::Usage;

/// A chunk of streaming response from an LLM.
///
/// # `OpenAI` API Alignment
/// This enum represents the various types of content that can be streamed
/// from an LLM provider, following `OpenAI`'s streaming response format.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum StreamChunk {
    /// Text content chunk.
    Text(String),

    /// Reasoning content chunk (for o1/o3 reasoning models).
    ReasoningContent(String),

    /// Audio content chunk (base64 encoded, for audio-capable models).
    Audio {
        /// Base64-encoded audio data.
        data: String,
        /// Audio transcript (if available).
        #[serde(skip_serializing_if = "Option::is_none")]
        transcript: Option<String>,
    },

    /// Start of a tool use/function call.
    ToolUseStart {
        /// Index of this tool call in the response.
        index: usize,
        /// Unique identifier for this tool call.
        id: String,
        /// Name of the function being called.
        name: String,
    },

    /// Partial arguments for an in-progress tool call.
    ToolUseDelta {
        /// Index of the tool call being updated.
        index: usize,
        /// Partial JSON arguments.
        partial_json: String,
    },

    /// Tool call is complete.
    ToolUseComplete {
        /// Index of the completed tool call.
        index: usize,
    },

    /// Token usage information.
    Usage(Usage),

    /// Stream is complete.
    Done {
        /// Stop reason from the model.
        stop_reason: Option<StopReason>,
    },

    /// Error during streaming.
    Error {
        /// Error message.
        message: String,
    },
}

impl StreamChunk {
    /// Creates a text chunk.
    #[inline]
    #[must_use]
    pub fn text(content: impl Into<String>) -> Self {
        Self::Text(content.into())
    }

    /// Creates a tool use start chunk.
    #[must_use]
    pub fn tool_use_start(index: usize, id: impl Into<String>, name: impl Into<String>) -> Self {
        Self::ToolUseStart {
            index,
            id: id.into(),
            name: name.into(),
        }
    }

    /// Creates a tool use delta chunk.
    #[must_use]
    pub fn tool_use_delta(index: usize, partial_json: impl Into<String>) -> Self {
        Self::ToolUseDelta {
            index,
            partial_json: partial_json.into(),
        }
    }

    /// Creates a done chunk.
    #[must_use]
    pub const fn done(stop_reason: Option<StopReason>) -> Self {
        Self::Done { stop_reason }
    }

    /// Creates an error chunk.
    #[must_use]
    pub fn error(message: impl Into<String>) -> Self {
        Self::Error {
            message: message.into(),
        }
    }

    /// Returns the text content if this is a text chunk.
    #[must_use]
    pub fn as_text(&self) -> Option<&str> {
        match self {
            Self::Text(text) => Some(text),
            _ => None,
        }
    }

    /// Returns `true` if this is a text chunk.
    #[must_use]
    pub const fn is_text(&self) -> bool {
        matches!(self, Self::Text(_))
    }

    /// Returns `true` if this is a done chunk.
    #[must_use]
    pub const fn is_done(&self) -> bool {
        matches!(self, Self::Done { .. })
    }

    /// Returns `true` if this is an error chunk.
    #[must_use]
    pub const fn is_error(&self) -> bool {
        matches!(self, Self::Error { .. })
    }

    /// Creates a reasoning content chunk.
    #[inline]
    #[must_use]
    pub fn reasoning(content: impl Into<String>) -> Self {
        Self::ReasoningContent(content.into())
    }

    /// Creates an audio chunk.
    #[must_use]
    pub fn audio(data: impl Into<String>, transcript: Option<String>) -> Self {
        Self::Audio {
            data: data.into(),
            transcript,
        }
    }

    /// Returns the reasoning content if this is a reasoning chunk.
    #[must_use]
    pub fn as_reasoning(&self) -> Option<&str> {
        match self {
            Self::ReasoningContent(content) => Some(content),
            _ => None,
        }
    }

    /// Returns `true` if this is a reasoning content chunk.
    #[must_use]
    pub const fn is_reasoning(&self) -> bool {
        matches!(self, Self::ReasoningContent(_))
    }

    /// Returns `true` if this is an audio chunk.
    #[must_use]
    pub const fn is_audio(&self) -> bool {
        matches!(self, Self::Audio { .. })
    }
}

/// Reason why the model stopped generating.
///
/// # `OpenAI` API Alignment
/// Maps to `finish_reason` in `OpenAI`'s Chat Completions API response.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum StopReason {
    /// Natural stop (end of response).
    #[default]
    Stop,
    /// Maximum token limit reached.
    Length,
    /// Model decided to call tools.
    #[serde(alias = "function_call")]
    ToolCalls,
    /// Content was filtered by safety systems.
    ContentFilter,
    /// Model is still generating (streaming only, no `finish_reason` yet).
    Null,
}

impl StopReason {
    /// Returns the string representation.
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Stop => "stop",
            Self::Length => "length",
            Self::ToolCalls => "tool_calls",
            Self::ContentFilter => "content_filter",
            Self::Null => "null",
        }
    }

    /// Parse from a string (case-insensitive).
    ///
    /// Handles various provider-specific finish reason strings:
    /// - `OpenAI`: "stop", "length", "`tool_calls`", "`content_filter`", "`function_call`"
    /// - Anthropic: "`end_turn`", "`max_tokens`", "`tool_use`"
    /// - Ollama: "stop", "length"
    #[must_use]
    pub fn parse(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "length" | "max_tokens" => Self::Length,
            "tool_calls" | "tool_use" | "function_call" => Self::ToolCalls,
            "content_filter" => Self::ContentFilter,
            "null" => Self::Null,
            // "stop", "end_turn", and any other value defaults to Stop
            _ => Self::Stop,
        }
    }

    /// Returns `true` if the model completed normally.
    #[must_use]
    pub const fn is_complete(&self) -> bool {
        matches!(self, Self::Stop | Self::ToolCalls)
    }

    /// Returns `true` if the model was cut off due to length.
    #[must_use]
    pub const fn is_truncated(&self) -> bool {
        matches!(self, Self::Length)
    }

    /// Returns `true` if content was filtered.
    #[must_use]
    pub const fn is_filtered(&self) -> bool {
        matches!(self, Self::ContentFilter)
    }

    /// Returns `true` if the model called tools/functions.
    #[must_use]
    pub const fn is_tool_call(&self) -> bool {
        matches!(self, Self::ToolCalls)
    }
}

impl std::fmt::Display for StopReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Aggregator for building complete content from stream chunks.
#[derive(Debug, Clone, Default)]
pub struct StreamAggregator {
    /// Accumulated text content.
    text: String,
    /// Accumulated reasoning content (for o1/o3 models).
    reasoning_content: String,
    /// Tool calls being built.
    tool_calls: std::collections::BTreeMap<usize, ToolCallBuilder>,
    /// Total usage.
    usage: Option<Usage>,
    /// Final stop reason.
    stop_reason: Option<StopReason>,
}

#[derive(Debug, Clone, Default)]
struct ToolCallBuilder {
    id: String,
    name: String,
    arguments: String,
}

impl StreamAggregator {
    /// Creates a new aggregator.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Applies a stream chunk to the aggregator.
    pub fn apply(&mut self, chunk: &StreamChunk) {
        match chunk {
            StreamChunk::Text(text) => {
                self.text.push_str(text);
            }
            StreamChunk::ToolUseStart { index, id, name } => {
                self.tool_calls.insert(
                    *index,
                    ToolCallBuilder {
                        id: id.clone(),
                        name: name.clone(),
                        arguments: String::new(),
                    },
                );
            }
            StreamChunk::ToolUseDelta {
                index,
                partial_json,
            } => {
                if let Some(tc) = self.tool_calls.get_mut(index) {
                    tc.arguments.push_str(partial_json);
                }
            }
            StreamChunk::ReasoningContent(content) => {
                self.reasoning_content.push_str(content);
            }
            StreamChunk::Audio { .. }
            | StreamChunk::ToolUseComplete { .. }
            | StreamChunk::Error { .. } => {}
            StreamChunk::Usage(usage) => {
                self.usage = Some(*usage);
            }
            StreamChunk::Done { stop_reason } => {
                self.stop_reason = *stop_reason;
            }
        }
    }

    /// Returns the current accumulated text.
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Returns the accumulated reasoning content.
    #[must_use]
    pub fn reasoning_content(&self) -> &str {
        &self.reasoning_content
    }

    /// Returns `true` if there is reasoning content.
    #[must_use]
    pub const fn has_reasoning_content(&self) -> bool {
        !self.reasoning_content.is_empty()
    }

    /// Returns the accumulated usage.
    #[must_use]
    pub const fn usage(&self) -> Option<Usage> {
        self.usage
    }

    /// Returns the stop reason.
    #[must_use]
    pub const fn stop_reason(&self) -> Option<StopReason> {
        self.stop_reason
    }

    /// Returns `true` if any tool calls have been started.
    #[must_use]
    pub fn has_tool_calls(&self) -> bool {
        !self.tool_calls.is_empty()
    }

    /// Builds the final tool calls.
    #[must_use]
    pub fn build_tool_calls(&self) -> Vec<crate::message::ToolCall> {
        self.tool_calls
            .values()
            .map(|tc| crate::message::ToolCall::function(&tc.id, &tc.name, &tc.arguments))
            .collect()
    }

    /// Converts the accumulated stream data into a [`ChatResponse`].
    ///
    /// Constructs an assistant message with either text content, tool calls,
    /// or both, along with usage statistics and stop reason.
    #[must_use]
    pub fn into_chat_response(self) -> crate::chat::ChatResponse {
        use crate::chat::ChatResponse;
        use crate::message::{Content, Message, Role};

        let tool_calls = self.build_tool_calls();
        let has_text = !self.text.is_empty();
        let has_tools = !tool_calls.is_empty();

        let mut message = match (has_text, has_tools) {
            (_, true) => {
                let mut msg = Message::assistant_tool_calls(tool_calls);
                if has_text {
                    msg.content = Some(Content::text(self.text));
                }
                msg
            }
            _ => Message::new(Role::Assistant, Content::text(self.text)),
        };

        if !self.reasoning_content.is_empty() {
            message.reasoning_content = Some(self.reasoning_content);
        }

        let mut response = ChatResponse::new(message);
        if let Some(reason) = self.stop_reason {
            response = response.with_stop_reason(reason);
        }
        if let Some(usage) = self.usage {
            response = response.with_usage(usage);
        }
        response
    }
}