mixtape-core 0.5.0

An agentic AI framework for Rust
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
//! Stateless Responses API on Bedrock Runtime. No stored conversation IDs,
//! background jobs, hosted tools, public vendor endpoint, or implicit fallback.
//!
//! <https://docs.aws.amazon.com/bedrock/latest/userguide/inference-responses-api.html>

mod cache;
mod conversion;
pub use cache::{BedrockResponsesCache, BedrockResponsesCacheMode};
mod streaming;
#[cfg(test)]
mod tests;

use super::chat_completions::transport::{
    self, bounded_body, HttpResponse, OpenAiApi, RuntimeClient, SignedClient,
};
use super::telemetry::InvocationGuard;
use super::{BedrockInvocation, BedrockJsonSchema, BedrockToolChoice, InvocationOutcome};
use crate::model::{BedrockModel, InferenceProfile, ModelResponse, RuntimeBedrockModel};
use crate::models::bedrock_model_descriptor;
use crate::provider::retry::{retry_with_backoff, RetryCallback, RetryConfig, RetryInfo};
use crate::provider::{ModelProvider, ProviderError, StreamEvent};
use crate::types::{Message, StopReason, ToolDefinition};
use eventsource_stream::Eventsource;
use futures::{stream::BoxStream, StreamExt};
use serde_json::{json, Value};
use std::sync::atomic::Ordering;
use std::sync::Arc;

fn invalid(message: &str) -> ProviderError {
    ProviderError::Configuration(message.into())
}
fn protocol(message: &str) -> ProviderError {
    ProviderError::Model(message.into())
}

/// Select this provider explicitly with `Agent::builder().provider(provider)`.
/// Requests always set `store=false`, retain full output items for local replay,
/// and use only client-side function tools. Construction never invokes a model.
#[derive(Clone)]
pub struct BedrockResponsesProvider {
    client: Arc<dyn RuntimeClient>,
    base_model_id: String,
    target: String,
    model_name: String,
    max_context_tokens: usize,
    max_output_tokens: usize,
    max_tokens: i32,
    reasoning_effort: Option<String>,
    tool_choice: Option<BedrockToolChoice>,
    output_schema: Option<BedrockJsonSchema>,
    prompt_cache: Option<BedrockResponsesCache>,
    retry_config: RetryConfig,
    on_retry: Option<RetryCallback>,
    on_invocation: Option<Arc<dyn Fn(BedrockInvocation) + Send + Sync>>,
}

impl BedrockResponsesProvider {
    pub async fn new(model: impl BedrockModel) -> Result<Self, ProviderError> {
        let config = aws_config::load_from_env().await;
        Self::from_sdk_config(&config, model)
    }

    /// Reuse a refreshable SDK credential provider and an explicit Region.
    /// Endpoint overrides are rejected; transport timeouts are 10s connect/300s total.
    pub fn from_sdk_config(
        config: &aws_config::SdkConfig,
        model: impl BedrockModel,
    ) -> Result<Self, ProviderError> {
        Self::with_transport(
            Arc::new(SignedClient::from_config(config, OpenAiApi::Responses)?),
            model,
        )
    }

    fn with_transport(
        client: Arc<dyn RuntimeClient>,
        model: impl BedrockModel,
    ) -> Result<Self, ProviderError> {
        RuntimeBedrockModel::new(
            model.name(),
            model.bedrock_id(),
            model.max_context_tokens(),
            model.max_output_tokens(),
        )?;
        Ok(Self {
            client,
            base_model_id: model.bedrock_id().into(),
            target: model
                .default_inference_profile()
                .apply_to(model.bedrock_id()),
            model_name: model.name().into(),
            max_context_tokens: model.max_context_tokens(),
            max_output_tokens: model.max_output_tokens(),
            max_tokens: model.max_output_tokens().min(4096) as i32,
            reasoning_effort: None,
            tool_choice: None,
            output_schema: None,
            prompt_cache: None,
            retry_config: RetryConfig::default(),
            on_retry: None,
            on_invocation: None,
        })
    }

    fn model_id(&self) -> &str {
        bedrock_model_descriptor(&self.base_model_id)
            .map(|model| model.model_id)
            .unwrap_or(&self.base_model_id)
    }

    /// Exact request target, distinct from any model identity returned by Bedrock.
    pub fn effective_model_id(&self) -> &str {
        &self.target
    }

    pub fn with_inference_profile(self, profile: InferenceProfile) -> Result<Self, ProviderError> {
        let target = profile.apply_to(self.model_id());
        self.with_inference_target(target)
    }

    /// Runtime Responses supports system inference profiles, not application
    /// profiles. Exact system-profile ARNs must name this same selected model.
    pub fn with_inference_target(
        mut self,
        target: impl Into<String>,
    ) -> Result<Self, ProviderError> {
        let target = target.into();
        RuntimeBedrockModel::new(
            &self.model_name,
            &target,
            self.max_context_tokens,
            self.max_output_tokens,
        )?;
        let id = if target.starts_with("arn:") {
            target.splitn(6, ':').nth(5).and_then(|resource| resource.strip_prefix("inference-profile/"))
                .ok_or_else(|| invalid("Responses requires a system inference profile, not an application profile or other ARN"))?
        } else {
            &target
        };
        let base = bedrock_model_descriptor(id)
            .map(|model| model.model_id)
            .unwrap_or(id);
        if base != self.model_id() {
            return Err(invalid(
                "Inference target names a different or unverified model",
            ));
        }
        self.target = target;
        Ok(self)
    }

    pub fn with_max_tokens(mut self, max_tokens: i32) -> Self {
        self.max_tokens = max_tokens;
        self
    }
    pub fn with_max_retries(mut self, attempts: usize) -> Self {
        self.retry_config.max_attempts = attempts;
        self
    }
    pub fn with_retry_config(mut self, config: RetryConfig) -> Self {
        self.retry_config = config;
        self
    }
    pub fn with_reasoning_effort(mut self, effort: impl Into<String>) -> Self {
        self.reasoning_effort = Some(effort.into());
        self
    }
    pub fn with_tool_choice(mut self, choice: BedrockToolChoice) -> Self {
        self.tool_choice = Some(choice);
        self
    }
    pub fn with_output_schema(mut self, schema: BedrockJsonSchema) -> Self {
        self.output_schema = Some(schema);
        self
    }

    pub fn with_prompt_cache(mut self, cache: BedrockResponsesCache) -> Self {
        self.prompt_cache = Some(cache);
        self
    }

    pub fn with_retry_callback<F>(mut self, callback: F) -> Self
    where
        F: Fn(RetryInfo) + Send + Sync + 'static,
    {
        self.on_retry = Some(Arc::new(callback));
        self
    }

    /// Never includes prompt/output bodies, reasoning, or credentials. Dropping a
    /// request or stream emits Cancelled once; callbacks must not panic or block.
    pub fn with_invocation_callback<F>(mut self, callback: F) -> Self
    where
        F: Fn(BedrockInvocation) + Send + Sync + 'static,
    {
        self.on_invocation = Some(Arc::new(callback));
        self
    }

    /// Local validation is not proof of account access or model/data eligibility.
    pub fn validate_configuration(&self) -> Result<(), ProviderError> {
        if self.max_tokens <= 0 || self.max_tokens as usize > self.max_output_tokens {
            return Err(invalid(
                "max_tokens must be positive and within the configured output ceiling",
            ));
        }
        if self.retry_config.max_attempts == 0 {
            return Err(invalid("At least one request attempt is required"));
        }
        let model_id = self.model_id();
        if !matches!(
            model_id,
            "openai.gpt-6-astra"
                | "openai.gpt-5.6-sol"
                | "openai.gpt-5.6-terra"
                | "openai.gpt-5.6-luna"
                | "moonshotai.kimi-k3"
        ) {
            return Err(invalid("Responses on Bedrock Runtime is not verified for this model; no fallback is selected"));
        }
        if self.target == model_id {
            return Err(invalid(
                "This model requires an explicit system inference-profile ID or ARN",
            ));
        }
        // Validate default targets as well as targets set by the builder.
        self.clone().with_inference_target(self.target.clone())?;
        if let Some(effort) = &self.reasoning_effort {
            let supported = if model_id == "moonshotai.kimi-k3" {
                matches!(effort.as_str(), "low" | "high" | "max")
            } else {
                matches!(effort.as_str(), "low" | "medium" | "high" | "xhigh" | "max")
            };
            if !supported {
                return Err(invalid(
                    "Responses reasoning effort is not verified for this model/value",
                ));
            }
        }
        if let Some(schema) = &self.output_schema {
            // GPT cards list native structured outputs as unsupported on Runtime.
            if model_id != "moonshotai.kimi-k3" {
                return Err(invalid(
                    "Native JSON schema output is not verified for this model on Runtime Responses",
                ));
            }
            super::openai_controls::validate_schema(schema)?;
        }
        if let Some(cache) = &self.prompt_cache {
            cache.validate(model_id)?;
        }
        Ok(())
    }

    fn request(
        &self,
        messages: &[Message],
        tools: &[ToolDefinition],
        system: Option<&str>,
        streaming: bool,
    ) -> Result<Vec<u8>, ProviderError> {
        self.validate_configuration()?;
        let mut body = json!({
            "model": self.target,
            "input": conversion::input(messages, system, self.model_id())?,
            "store": false,
            "include": ["reasoning.encrypted_content"],
            "max_output_tokens": self.max_tokens,
            "stream": streaming,
        });
        let definitions = conversion::tools(tools)?;
        if !definitions.is_empty() {
            body["tools"] = json!(definitions);
        }
        super::openai_controls::apply_tool_choice(
            &mut body,
            tools,
            self.tool_choice.as_ref(),
            true,
        )?;
        if let Some(effort) = &self.reasoning_effort {
            body["reasoning"] = json!({"effort": effort});
        }
        if let Some(schema) = &self.output_schema {
            body["text"] = json!({"format": {"type": "json_schema", "name": schema.name, "schema": schema.schema, "strict": true}});
        }
        if let Some(cache) = &self.prompt_cache {
            cache.apply(&mut body, messages, system)?;
        }
        serde_json::to_vec(&body).map_err(|_| invalid("Unable to serialize Responses request"))
    }

    fn guard(&self, streaming: bool) -> InvocationGuard {
        InvocationGuard::new(
            BedrockInvocation::new(
                &self.base_model_id,
                self.target.clone(),
                if streaming {
                    "responses_stream"
                } else {
                    "responses"
                },
                Some(self.client.region().into()),
            ),
            self.on_invocation.clone(),
            Some(0),
        )
    }

    async fn send(
        &self,
        body: Vec<u8>,
        streaming: bool,
        guard: &mut InvocationGuard,
    ) -> Result<HttpResponse, ProviderError> {
        let response = retry_with_backoff(
            || {
                guard.attempts.fetch_add(1, Ordering::Relaxed);
                let body = body.clone();
                let request_id = guard.last_request_id.clone();
                async move {
                    let response = self.client.send(body, streaming).await?;
                    *request_id.lock().unwrap_or_else(|error| error.into_inner()) =
                        response.request_id.clone();
                    if let Some(error) = transport::status_error(response.status) {
                        return Err(error);
                    }
                    Ok(response)
                }
            },
            &self.retry_config,
            &self.on_retry,
        )
        .await;
        match response {
            Ok(response) => {
                let expected = if streaming {
                    "text/event-stream"
                } else {
                    "application/json"
                };
                if !response.content_type.as_deref().is_some_and(|value| {
                    value
                        .split(';')
                        .next()
                        .unwrap_or("")
                        .trim()
                        .eq_ignore_ascii_case(expected)
                }) {
                    guard.finish(InvocationOutcome::Failed);
                    return Err(protocol("Unexpected Bedrock Responses content type"));
                }
                guard.record.request_id = response.request_id.clone();
                Ok(response)
            }
            Err(error) => {
                guard.finish(InvocationOutcome::Failed);
                Err(error)
            }
        }
    }
}

#[async_trait::async_trait]
impl ModelProvider for BedrockResponsesProvider {
    fn name(&self) -> &str {
        &self.model_name
    }
    fn max_context_tokens(&self) -> usize {
        self.max_context_tokens
    }
    fn max_output_tokens(&self) -> usize {
        self.max_output_tokens
    }

    fn estimate_message_tokens(&self, messages: &[Message]) -> usize {
        // Debug intentionally redacts replay data. Count the serialized request
        // shape instead, so opaque reasoning cannot disappear from the estimate.
        match conversion::input(messages, None, self.model_id()).and_then(|input| {
            serde_json::to_string(&input).map_err(|_| invalid("Cannot estimate Responses input"))
        }) {
            Ok(input) => self.estimate_token_count(&input),
            Err(_) => self.max_context_tokens,
        }
    }

    async fn generate(
        &self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
        system_prompt: Option<String>,
    ) -> Result<ModelResponse, ProviderError> {
        let body = self.request(&messages, &tools, system_prompt.as_deref(), false)?;
        let mut guard = self.guard(false);
        let response = self.send(body, false, &mut guard).await?;
        let result = async {
            let mut bytes = Vec::new();
            let mut body = bounded_body(response.body);
            while let Some(chunk) = body.next().await {
                bytes.extend(chunk?);
            }
            let value: Value = serde_json::from_slice(&bytes)
                .map_err(|_| protocol("Invalid JSON in Bedrock Responses response"))?;
            conversion::response(&value, self.model_id(), &mut guard.record)
        }
        .await;
        guard.finish(match &result {
            Ok(response)
                if matches!(
                    response.stop_reason,
                    StopReason::EndTurn | StopReason::ToolUse
                ) =>
            {
                InvocationOutcome::Completed
            }
            Ok(_) => InvocationOutcome::Rejected,
            Err(_) => InvocationOutcome::Failed,
        });
        result
    }

    async fn generate_stream(
        &self,
        messages: Vec<Message>,
        tools: Vec<ToolDefinition>,
        system_prompt: Option<String>,
    ) -> Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError> {
        let body = self.request(&messages, &tools, system_prompt.as_deref(), true)?;
        let mut guard = self.guard(true);
        let response = self.send(body, true, &mut guard).await?;
        let model = self.model_id().to_owned();
        let stream = async_stream::stream! {
            let mut events = bounded_body(response.body).eventsource();
            let mut assembler = streaming::StreamAssembler::new(model);
            while let Some(event) = events.next().await {
                let result = match event {
                    Ok(event) => assembler.push(&event.event, &event.data, &mut guard.record),
                    Err(_) => Err(protocol("Bedrock Responses SSE framing or transport failed")),
                };
                match result {
                    Ok(events) => {
                        if let Some(outcome) = assembler.outcome() { guard.finish(outcome); }
                        for event in events { yield Ok(event); }
                        if assembler.outcome().is_some() { return; }
                    }
                    Err(error) => { guard.finish(InvocationOutcome::Failed); yield Err(error); return; }
                }
            }
            guard.finish(InvocationOutcome::Failed);
            yield Err(protocol("Bedrock Responses stream ended before a terminal response event"));
        };
        Ok(Box::pin(stream))
    }
}