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
//! Result types for `do_generate` and `do_stream`.
use std::collections::HashMap;
use futures::Stream;
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use ts_rs::TS;
use crate::error::AiMuxError;
use crate::shared::FileData;
use crate::stream_part::StreamPart;
use crate::types::{FinishReason, ProviderMetadata, ResponseMetadata, Usage, Warning};
use serde_json::Value;
/// A content item in the generation result.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum GenerateContent {
/// Generated text.
Text {
text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_metadata: Option<ProviderMetadata>,
},
/// A tool call requested by the model.
ToolCall {
tool_call_id: String,
tool_name: String,
input: serde_json::Value,
/// Whether the tool call will be executed by the provider.
/// If false/unset, the tool call is executed by the client.
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_executed: Option<bool>,
/// Whether the tool is dynamic (defined at runtime, e.g. MCP tools).
#[serde(default, skip_serializing_if = "Option::is_none")]
dynamic: Option<bool>,
/// Provider-assigned thought signature (e.g. Google Gemini
/// `thoughtSignature`). Must be echoed back verbatim on the follow-up
/// turn when the tool result is sent.
#[serde(default, skip_serializing_if = "Option::is_none")]
thought_signature: Option<String>,
/// Additional provider-specific metadata for the tool call.
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_metadata: Option<ProviderMetadata>,
},
/// A source / citation (e.g. URL citation from search-preview models).
Source {
id: String,
source_type: String,
url: Option<String>,
title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_metadata: Option<ProviderMetadata>,
},
/// A reasoning / thinking segment produced by the model.
///
/// Mirrors the Vercel AI SDK `reasoning` content type. `provider_metadata`
/// carries provider-specific data such as the Anthropic thinking-block
/// signature (`providerMetadata.anthropic.signature`).
Reasoning {
text: String,
provider_metadata: Option<ProviderMetadata>,
},
/// A file generated by the model (e.g. an image or document).
///
/// Mirrors the Vercel AI SDK `file` content type. `data` is a tagged union
/// covering base64 data, URL, or provider reference.
File {
data: FileData,
media_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_metadata: Option<ProviderMetadata>,
},
/// A tool result from a provider-executed tool (e.g. xAI file_search,
/// web_search). Emitted alongside the preceding `ToolCall` when the
/// provider executes the tool server-side.
ToolResult {
tool_call_id: String,
tool_name: String,
result: Value,
/// Whether the result is an error or error message.
#[serde(default, skip_serializing_if = "Option::is_none")]
is_error: Option<bool>,
/// Whether the result is preliminary (replaces prior, e.g. image previews).
#[serde(default, skip_serializing_if = "Option::is_none")]
preliminary: Option<bool>,
/// Whether the tool is dynamic (defined at runtime, e.g. MCP tools).
#[serde(default, skip_serializing_if = "Option::is_none")]
dynamic: Option<bool>,
/// Additional provider-specific metadata for the tool result.
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_metadata: Option<ProviderMetadata>,
},
}
// ── M7: top-level aggregation types ─────────────────────────────────────────
//
// Simplified views extracted from `GenerateContent` for the top-level fields of
// `GenerateTextResult`. They omit provider-internal metadata so the common case
// (reading reasoning/sources/files) doesn't require digging through `raw.content`.
/// A reasoning / thinking segment, extracted to the top level (M7).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct ReasoningPart {
/// The reasoning text.
pub text: String,
}
/// A source / citation, extracted to the top level (M7).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct SourcePart {
pub id: String,
pub source_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
}
/// A file generated by the model, extracted to the top level (M7).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct FilePart {
pub data: FileData,
pub media_type: String,
}
/// Aggregated result of `stream_text().consume()` (M11). Mirrors
/// `GenerateTextResult`'s user-facing fields (without `raw`, since streaming
/// has no `GenerateResult` equivalent).
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct StreamTextResultAggregated {
/// The generated text (concatenated `TextDelta`).
pub text: String,
/// Reasoning / thinking segments.
#[serde(default)]
pub reasoning: Vec<ReasoningPart>,
/// Concatenated reasoning text.
#[serde(default)]
pub reasoning_text: String,
/// Tool calls requested by the model.
#[serde(default)]
pub tool_calls: Vec<crate::tool::ToolCall>,
/// Sources / citations.
#[serde(default)]
pub sources: Vec<SourcePart>,
/// Files generated by the model.
#[serde(default)]
pub files: Vec<FilePart>,
/// Why generation stopped.
pub finish_reason: crate::types::FinishReason,
/// Raw provider-specific finish reason string.
#[serde(default)]
pub raw_finish_reason: Option<String>,
/// Token usage.
#[serde(default)]
pub usage: crate::types::Usage,
/// Total token usage across all steps. In single-step mode (aimux's
/// default), `total_usage` equals `usage`. Provided for AI SDK parity.
#[serde(default)]
pub total_usage: crate::types::Usage,
/// Warnings from the provider.
#[serde(default)]
pub warnings: Vec<crate::types::Warning>,
/// Provider-specific metadata from the Finish chunk.
#[serde(default)]
pub provider_metadata: Option<serde_json::Value>,
/// Response metadata (id, timestamp, model_id) if emitted by the stream.
#[serde(default)]
pub response: Option<crate::types::ResponseMetadata>,
/// Assistant messages ready to append for the next turn.
#[serde(default)]
pub response_messages: Vec<crate::message::ModelMessage>,
}
/// Result of `LanguageModel::do_generate` (non-streaming).
#[derive(Debug, Serialize, Deserialize, TS)]
#[ts(export)]
pub struct GenerateResult {
/// Generated content items (text, tool calls, etc.).
pub content: Vec<GenerateContent>,
/// Why generation stopped.
pub finish_reason: FinishReason,
/// Token usage.
pub usage: Usage,
/// Warnings issued by the provider.
pub warnings: Vec<Warning>,
/// Provider-specific metadata.
pub provider_metadata: Option<ProviderMetadata>,
/// Response metadata (id, timestamp, model_id).
pub response: ResponseMetadata,
/// The request body that was sent (for debugging).
pub request_body: Option<serde_json::Value>,
/// Response headers.
pub response_headers: Option<HashMap<String, String>>,
}
/// Result of `LanguageModel::do_stream` (streaming).
pub struct StreamResult {
/// The stream of `StreamPart` items.
pub stream: Pin<Box<dyn Stream<Item = Result<StreamPart, AiMuxError>> + Send>>,
/// The request body that was sent (for debugging).
pub request_body: Option<serde_json::Value>,
/// Response headers.
pub response_headers: Option<HashMap<String, String>>,
}
impl std::fmt::Debug for StreamResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StreamResult")
.field("request_body", &self.request_body)
.field("response_headers", &self.response_headers)
.field("stream", &"<stream>")
.finish()
}
}