agents 0.3.0

Facade crate for building typed Rust agents
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
use derive_builder::Builder;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;

use crate::llm::error::{Error, LlmResult};
use crate::llm::response::{RawResponseFormat, TypedResponse};
use crate::llm::tools::{RawToolCall, RawToolDefinition, ToolCall, TypedToolSet};

/// LLM provider family.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum ProviderType {
    OpenAI,
    Anthropic,
    OpenRouter,
    WorkersAI,
    LmStudio,
    Ollama,
    Apple,
}

impl ProviderType {
    /// Returns the stable lowercase provider name used in config and artifacts.
    pub fn name(&self) -> &'static str {
        match self {
            ProviderType::OpenAI => "openai",
            ProviderType::Anthropic => "anthropic",
            ProviderType::OpenRouter => "openrouter",
            ProviderType::WorkersAI => "workers_ai",
            ProviderType::LmStudio => "lm_studio",
            ProviderType::Ollama => "ollama",
            ProviderType::Apple => "apple",
        }
    }
}

/// Strategy for selecting a provider or exact model name.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum ModelSelector {
    Any,
    Provider(ProviderType),
    Specific {
        provider: Option<ProviderType>,
        model: String,
    },
}

impl ModelSelector {
    /// Selects whichever provider the runner chooses as its default.
    pub fn any() -> Self {
        ModelSelector::Any
    }

    /// Selects an exact model name without pinning a provider.
    pub fn from_model(model: impl Into<String>) -> Self {
        ModelSelector::Specific {
            provider: None,
            model: model.into(),
        }
    }

    /// Selects the default model for one provider family.
    pub fn for_provider(provider: ProviderType) -> Self {
        ModelSelector::Provider(provider)
    }
}

/// Message role used in completion input and output.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    System,
    User,
    Assistant,
}

/// One typed input item sent to a provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum InputItem {
    Message {
        role: Role,
        content: Vec<InputContent>,
    },
    ToolCall {
        call: RawToolCall,
    },
    ToolResult {
        tool_use_id: String,
        content: String,
    },
}

impl InputItem {
    /// Builds one user text message.
    pub fn user_text(text: impl Into<String>) -> Self {
        Self::Message {
            role: Role::User,
            content: vec![InputContent::text(text)],
        }
    }

    /// Builds one assistant text message.
    pub fn assistant_text(text: impl Into<String>) -> Self {
        Self::Message {
            role: Role::Assistant,
            content: vec![InputContent::text(text)],
        }
    }

    /// Builds one system text message.
    pub fn system_text(text: impl Into<String>) -> Self {
        Self::Message {
            role: Role::System,
            content: vec![InputContent::text(text)],
        }
    }

    /// Builds one tool call transcript item.
    pub fn tool_call(
        id: impl Into<String>,
        name: impl Into<String>,
        arguments: serde_json::Value,
    ) -> Self {
        Self::ToolCall {
            call: RawToolCall {
                id: id.into(),
                name: name.into(),
                arguments,
            },
        }
    }

    /// Builds one tool result transcript item.
    pub fn tool_result(tool_use_id: impl Into<String>, content: impl Into<String>) -> Self {
        Self::ToolResult {
            tool_use_id: tool_use_id.into(),
            content: content.into(),
        }
    }
}

impl From<String> for InputItem {
    fn from(value: String) -> Self {
        Self::user_text(value)
    }
}

impl From<&str> for InputItem {
    fn from(value: &str) -> Self {
        Self::user_text(value)
    }
}

/// One content item inside a message input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum InputContent {
    Text { text: String },
    ImageUrl { url: String },
}

impl InputContent {
    /// Builds one text content part.
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text { text: text.into() }
    }

    /// Builds one image-url content part.
    pub fn image_url(url: impl Into<String>) -> Self {
        Self::ImageUrl { url: url.into() }
    }
}

impl From<String> for InputContent {
    fn from(value: String) -> Self {
        Self::text(value)
    }
}

impl From<&str> for InputContent {
    fn from(value: &str) -> Self {
        Self::text(value)
    }
}

/// Reason a provider ended generation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum FinishReason {
    Stop,
    Length,
    ToolCalls,
    ContentFilter,
    Unknown(String),
}

impl From<Option<String>> for FinishReason {
    fn from(value: Option<String>) -> Self {
        match value.as_deref() {
            Some("stop") => FinishReason::Stop,
            Some("length") => FinishReason::Length,
            Some("tool_calls") => FinishReason::ToolCalls,
            Some("content_filter") => FinishReason::ContentFilter,
            Some(other) => FinishReason::Unknown(other.to_string()),
            None => FinishReason::Unknown("null".to_string()),
        }
    }
}

/// Typed completion request sent through [`crate::llm::LlmRunner`].
///
/// This is the main high-level request type for chat completions. It keeps
/// tools and structured response parsing typed at the Rust boundary.
///
/// ```rust
/// use agents::{
///     CompletionRequest, InputItem, ModelSelector, Probability, ResponseMode, TokenLimit,
/// };
///
/// let request = CompletionRequest::<(), String>::new(
///     vec![InputItem::system_text("Be concise."), InputItem::user_text("hello")],
///     ModelSelector::from_model("llama3.2:3b"),
/// )
/// .with_max_tokens(256)
/// .with_response_mode(ResponseMode::Buffered)
/// .with_top_p(Probability::new(0.9)?);
///
/// assert_eq!(request.input.len(), 2);
/// assert!(matches!(request.token_limit, TokenLimit::Max(256)));
/// # Ok::<(), agents::error::Error>(())
/// ```
#[derive(Debug, Clone, Builder)]
#[builder(setter(into))]
pub struct CompletionRequest<ToolType, ResponseType> {
    #[builder(default = "ModelSelector::Any")]
    pub model: ModelSelector,
    pub input: Vec<InputItem>,
    #[builder(default = "Temperature::ProviderDefault")]
    pub temperature: Temperature,
    #[builder(default = "TopP::ProviderDefault")]
    pub top_p: TopP,
    #[builder(default = "TopK::ProviderDefault")]
    pub top_k: TopK,
    #[builder(default = "TokenLimit::ProviderDefault")]
    pub token_limit: TokenLimit,
    #[builder(default = "ResponseMode::Buffered")]
    pub response_mode: ResponseMode,
    #[builder(default)]
    pub tools: Option<TypedToolSet<ToolType>>,
    #[builder(default = "ToolChoice::ProviderDefault")]
    pub tool_choice: ToolChoice,
    #[builder(default)]
    pub response_format: Option<TypedResponse<ResponseType>>,
}

impl<ToolType, ResponseType> CompletionRequest<ToolType, ResponseType> {
    /// Creates a request with explicit input and model selection.
    pub fn new(input: Vec<InputItem>, model: ModelSelector) -> Self {
        Self {
            model,
            input,
            temperature: Temperature::ProviderDefault,
            top_p: TopP::ProviderDefault,
            top_k: TopK::ProviderDefault,
            token_limit: TokenLimit::ProviderDefault,
            response_mode: ResponseMode::Buffered,
            tools: None,
            tool_choice: ToolChoice::ProviderDefault,
            response_format: None,
        }
    }

    /// Sets an explicit temperature value.
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = Temperature::Value(temperature);
        self
    }

    /// Sets the provider-neutral token limit.
    pub fn with_token_limit(mut self, token_limit: TokenLimit) -> Self {
        self.token_limit = token_limit;
        self
    }

    /// Convenience helper for `TokenLimit::Max`.
    pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
        self.token_limit = TokenLimit::Max(max_tokens);
        self
    }

    /// Sets top-p sampling.
    pub fn with_top_p(mut self, top_p: Probability) -> Self {
        self.top_p = TopP::Value(top_p);
        self
    }

    /// Sets top-k sampling.
    pub fn with_top_k(mut self, top_k: u32) -> Self {
        self.top_k = TopK::Value(top_k);
        self
    }

    /// Chooses buffered or streamed response handling.
    pub fn with_response_mode(mut self, response_mode: ResponseMode) -> Self {
        self.response_mode = response_mode;
        self
    }

    /// Attaches typed tool definitions to the request.
    pub fn with_tools(mut self, tools: TypedToolSet<ToolType>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Overrides the tool selection behavior for this request.
    pub fn with_tool_choice(mut self, tool_choice: ToolChoice) -> Self {
        self.tool_choice = tool_choice;
        self
    }

    /// Requests a typed structured response.
    pub fn with_typed_response(mut self, response_format: TypedResponse<ResponseType>) -> Self {
        self.response_format = Some(response_format);
        self
    }
}

/// Final typed completion response.
///
/// Providers always return a sequence of output items. When a typed response
/// schema is configured, message content may contain
/// [`OutputContent::Structured`] values instead of plain text.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionResponse<ToolType = (), ResponseType = String> {
    pub provider: ProviderType,
    pub model: String,
    pub output: Vec<OutputItem<ToolType, ResponseType>>,
    pub usage: Usage,
    pub finish_reason: FinishReason,
}

#[cfg(test)]
mod tests {
    use super::{InputContent, InputItem, Role};

    #[test]
    fn input_item_from_string_defaults_to_user_text() {
        let item = InputItem::from("hello");

        match item {
            InputItem::Message { role, content } => {
                assert_eq!(role, Role::User);
                assert_eq!(content.len(), 1);
                assert!(
                    matches!(content.first(), Some(InputContent::Text { text }) if text == "hello")
                );
            }
            other => panic!("expected user text message, got {other:?}"),
        }
    }

    #[test]
    fn input_content_from_string_defaults_to_text() {
        let content = InputContent::from("hello");
        assert!(matches!(content, InputContent::Text { text } if text == "hello"));
    }
}

/// One typed output item emitted by a provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum OutputItem<ToolType = (), ResponseType = String> {
    Message {
        role: Role,
        content: Vec<OutputContent<ResponseType>>,
    },
    ToolCall {
        call: ToolCall<ToolType>,
    },
    Reasoning {
        text: String,
    },
}

/// Content carried by a typed output message.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum OutputContent<ResponseType = String> {
    Text { text: String },
    Structured { value: ResponseType },
}

/// Whether a response should be buffered or streamed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ResponseMode {
    Buffered,
    Stream,
}

impl ResponseMode {
    /// Returns `true` when the caller requested streamed events.
    pub fn is_streaming(self) -> bool {
        matches!(self, Self::Stream)
    }
}

/// Probability value constrained to the `[0.0, 1.0]` range.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Probability(f32);

impl Probability {
    /// Creates a validated probability in the inclusive `[0.0, 1.0]` range.
    pub fn new(value: f32) -> LlmResult<Self> {
        if (0.0..=1.0).contains(&value) {
            Ok(Self(value))
        } else {
            Err(Error::InvalidRequest {
                reason: format!("probability must be between 0.0 and 1.0, got {value}"),
            })
        }
    }

    /// Returns the raw float value.
    pub fn value(self) -> f32 {
        self.0
    }
}

/// Temperature configuration for generation.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Temperature {
    ProviderDefault,
    Value(f32),
}

impl Temperature {
    /// Converts the setting into the optional provider wire value.
    pub fn as_option(self) -> Option<f32> {
        match self {
            Self::ProviderDefault => None,
            Self::Value(value) => Some(value),
        }
    }
}

/// Token limit configuration for generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TokenLimit {
    ProviderDefault,
    Max(u32),
}

impl TokenLimit {
    /// Converts the setting into the optional provider wire value.
    pub fn as_option(self) -> Option<u32> {
        match self {
            Self::ProviderDefault => None,
            Self::Max(value) => Some(value),
        }
    }
}

/// Top-p sampling configuration.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TopP {
    ProviderDefault,
    Value(Probability),
}

impl TopP {
    /// Converts the setting into the optional provider wire value.
    pub fn as_option(self) -> Option<f32> {
        match self {
            Self::ProviderDefault => None,
            Self::Value(value) => Some(value.value()),
        }
    }
}

/// Top-k sampling configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TopK {
    ProviderDefault,
    Value(u32),
}

impl TopK {
    /// Converts the setting into the optional provider wire value.
    pub fn as_option_i32(self) -> Option<i32> {
        match self {
            Self::ProviderDefault => None,
            Self::Value(value) => i32::try_from(value).ok(),
        }
    }
}

/// Tool selection mode for a completion request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ToolChoice {
    ProviderDefault,
    Auto,
    Required,
    Specific { name: String },
    None,
}

/// One streamed completion event.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CompletionEvent<ToolType, ResponseType> {
    TextDelta { text: String },
    ReasoningDelta { text: String },
    ToolCall { call: ToolCall<ToolType> },
    Done(CompletionResponse<ToolType, ResponseType>),
}

/// Stream of typed completion events returned by [`crate::llm::LlmRunner::chat_stream`].
pub struct CompletionEventStream<ToolType, ResponseType> {
    receiver: mpsc::Receiver<crate::llm::error::LlmResult<CompletionEvent<ToolType, ResponseType>>>,
}

impl<ToolType, ResponseType> CompletionEventStream<ToolType, ResponseType> {
    /// Wraps a raw receiver into a typed stream handle.
    pub fn new(
        receiver: mpsc::Receiver<
            crate::llm::error::LlmResult<CompletionEvent<ToolType, ResponseType>>,
        >,
    ) -> Self {
        Self { receiver }
    }

    /// Awaits the next streamed completion event.
    pub async fn recv(
        &mut self,
    ) -> Option<crate::llm::error::LlmResult<CompletionEvent<ToolType, ResponseType>>> {
        self.receiver.recv().await
    }
}

/// Untyped completion request sent directly to a provider implementation.
///
/// Provider adapters should use this type instead of [`CompletionRequest`] when
/// translating requests into provider wire formats.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RawCompletionRequest {
    pub model: ModelSelector,
    pub input: Vec<RawInputItem>,
    pub temperature: Temperature,
    pub top_p: TopP,
    pub top_k: TopK,
    pub token_limit: TokenLimit,
    pub response_mode: ResponseMode,
    pub tools: Option<Vec<RawToolDefinition>>,
    pub tool_choice: ToolChoice,
    pub response_format: Option<RawResponseFormat>,
}

/// Untyped completion response returned directly by a provider implementation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RawCompletionResponse {
    pub provider: ProviderType,
    pub model: String,
    pub output: Vec<RawOutputItem>,
    pub usage: Usage,
    pub finish_reason: FinishReason,
}

/// Untyped input item used by provider implementations.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum RawInputItem {
    Message {
        role: Role,
        content: Vec<RawInputContent>,
    },
    ToolCall {
        call: RawToolCall,
    },
    ToolResult {
        tool_use_id: String,
        content: String,
    },
}

/// Untyped content item used by provider implementations.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum RawInputContent {
    Text { text: String },
    ImageUrl { url: String },
}

/// Untyped output item used by provider implementations.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum RawOutputItem {
    Message {
        role: Role,
        content: Vec<RawOutputContent>,
    },
    ToolCall {
        call: RawToolCall,
    },
    Reasoning {
        text: String,
    },
}

/// Untyped output content used by provider implementations.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum RawOutputContent {
    Text { text: String },
    Json { value: serde_json::Value },
}

/// One streamed raw completion event.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum RawCompletionEvent {
    TextDelta { text: String },
    ReasoningDelta { text: String },
    ToolCall { call: RawToolCall },
    Done(RawCompletionResponse),
}

/// Stream of raw completion events for provider implementations.
pub struct RawCompletionEventStream {
    receiver: mpsc::Receiver<crate::llm::error::LlmResult<RawCompletionEvent>>,
}

impl RawCompletionEventStream {
    /// Wraps a raw receiver into an event stream handle.
    pub fn new(receiver: mpsc::Receiver<crate::llm::error::LlmResult<RawCompletionEvent>>) -> Self {
        Self { receiver }
    }

    /// Awaits the next streamed raw completion event.
    pub async fn recv(&mut self) -> Option<crate::llm::error::LlmResult<RawCompletionEvent>> {
        self.receiver.recv().await
    }
}

/// Token accounting attached to a provider response.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Usage {
    pub prompt_tokens: u32,
    pub completion_tokens: u32,
    pub total_tokens: u32,
}

/// Provider, model, finish reason, and token usage for one completion response.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct UsageMetrics {
    pub response_id: u64,
    pub provider: ProviderType,
    pub model: String,
    pub finish_reason: FinishReason,
    pub usage: Usage,
}