yoagent 0.25.9

Simple, effective agent loop with tool execution and event streaming
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
//! Google Vertex AI provider.
//!
//! Similar to Google Generative AI but uses OAuth2 authentication
//! and a different base URL pattern with project/location.
//!
//! The API key in StreamConfig is expected to be an OAuth2 access token.
//!
//! Prompt caching behaves exactly as in [`super::google`], including the
//! reasoning for why explicit `CachedContent` is deliberately not wired — see
//! that module's docs rather than re-deriving it here.
//! Callers are responsible for obtaining the token (e.g., via service account JWT).

use super::model::ModelConfig;
use super::traits::*;
use crate::types::*;
use tokio::sync::mpsc;

pub struct GoogleVertexProvider;

impl GoogleVertexProvider {
    /// Build the Vertex AI URL from model config.
    /// Expects base_url in format: `https://{region}-aiplatform.googleapis.com/v1/projects/{project}/locations/{region}/publishers/google/models`
    fn vertex_url(model_config: &ModelConfig, model: &str) -> String {
        format!(
            "{}/{}:streamGenerateContent?alt=sse",
            model_config.base_url, model
        )
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl StreamProvider for GoogleVertexProvider {
    fn protocol(&self) -> Option<crate::provider::ApiProtocol> {
        Some(crate::provider::ApiProtocol::GoogleVertex)
    }

    async fn stream(
        &self,
        config: StreamConfig,
        tx: mpsc::UnboundedSender<StreamEvent>,
        cancel: tokio_util::sync::CancellationToken,
    ) -> Result<Message, ProviderError> {
        if config.output_schema.is_some() {
            tracing::warn!(
                "structured outputs are not yet wired for the Google Vertex provider; output_schema will be ignored"
            );
        }
        let model_config = config
            .model_config
            .as_ref()
            .ok_or_else(|| ProviderError::Other("ModelConfig required".into()))?;

        // Vertex's URL pattern differs from the Gemini API's.
        let vertex_url = Self::vertex_url(model_config, &config.model);

        // Create a modified model config with the Vertex URL pattern
        let mut vertex_model = model_config.clone();
        // Vertex authenticates with an OAuth2 Bearer token, not a Gemini API
        // key.
        vertex_model.headers.insert(
            "authorization".to_string(),
            format!("Bearer {}", config.api_key),
        );

        // Build request body same as Google (same content format)
        let body = build_vertex_request_body(&config);

        let client = reqwest::Client::new();
        let mut request = client
            .post(&vertex_url)
            .header("content-type", "application/json");

        for (k, v) in &vertex_model.headers {
            request = request.header(k, v);
        }

        let response = request
            .json(&body)
            .send()
            .await
            .map_err(|e| ProviderError::Network(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status();
            let retry_after = parse_retry_after(response.headers());
            let body = response.text().await.unwrap_or_default();
            return Err(ProviderError::classify_with_retry_after(
                status.as_u16(),
                &format!("Vertex AI error {}: {}", status, body),
                retry_after,
            ));
        }

        // Same SSE format as the Gemini API: one parser for both.
        super::google::parse_stream(response, &config.model, &model_config.provider, tx, cancel)
            .await
    }
}

/// Build the request body for Vertex AI (same format as Google GenAI).
fn build_vertex_request_body(config: &StreamConfig) -> serde_json::Value {
    // Same format as Google GenAI
    let mut contents: Vec<serde_json::Value> = Vec::new();

    for msg in &config.messages {
        match msg {
            Message::User { content, .. } => {
                let parts: Vec<serde_json::Value> = content
                    .iter()
                    .filter(|c| !matches!(c, Content::Text { text } if text.is_empty()))
                    .filter_map(|c| match c {
                        Content::Text { text } => Some(serde_json::json!({"text": text})),
                        Content::Image { data, mime_type } => Some(serde_json::json!({
                            "inlineData": {"mimeType": mime_type, "data": data},
                        })),
                        _ => None,
                    })
                    .collect();
                contents.push(serde_json::json!({"role": "user", "parts": parts}));
            }
            Message::Assistant { content, .. } => {
                let parts: Vec<serde_json::Value> = content
                    .iter()
                    .filter(|c| !matches!(c, Content::Text { text } if text.is_empty()))
                    .filter_map(|c| match c {
                        Content::Text { text } => Some(serde_json::json!({"text": text})),
                        Content::ToolCall {
                            name,
                            arguments,
                            provider_metadata,
                            ..
                        } => {
                            let mut part = serde_json::json!({
                                "functionCall": {"name": name, "args": arguments},
                            });
                            if let Some(sig) = provider_metadata
                                .as_ref()
                                .and_then(|m| m.get("thought_signature"))
                                .and_then(|v| v.as_str())
                            {
                                part["thoughtSignature"] = serde_json::json!(sig);
                            }
                            Some(part)
                        }
                        _ => None,
                    })
                    .collect();
                contents.push(serde_json::json!({"role": "model", "parts": parts}));
            }
            Message::ToolResult {
                tool_name, content, ..
            } => {
                let text = super::tool_result_text(content);

                let mut parts = vec![serde_json::json!({
                    "functionResponse": {"name": tool_name, "response": {"result": text}}
                })];

                for c in content {
                    if let Content::Image { data, mime_type } = c {
                        parts.push(serde_json::json!({
                            "inlineData": {"mimeType": mime_type, "data": data},
                        }));
                    }
                }

                contents.push(serde_json::json!({
                    "role": "user",
                    "parts": parts,
                }));
            }
        }
    }

    let mut body = serde_json::json!({"contents": contents});

    if !config.system_prompt.is_empty() {
        body["systemInstruction"] = serde_json::json!({"parts": [{"text": config.system_prompt}]});
    }

    let mut gen_config = serde_json::json!({});
    if let Some(max) = config.max_tokens {
        gen_config["maxOutputTokens"] = serde_json::json!(max);
    }
    if let Some(temp) = config.temperature {
        gen_config["temperature"] = serde_json::json!(temp);
    }
    // Thinking: same thinkingConfig as the Gemini API (level on 3+, budget on 2.x).
    if let Some(thinking) = super::google::gemini_thinking_config(config) {
        gen_config["thinkingConfig"] = thinking;
    }
    if gen_config != serde_json::json!({}) {
        body["generationConfig"] = gen_config;
    }

    if !config.tools.is_empty() {
        let declarations: Vec<serde_json::Value> = config
            .tools
            .iter()
            .map(|t| {
                serde_json::json!({
                    "name": t.name,
                    "description": t.description,
                    "parameters": t.parameters,
                })
            })
            .collect();
        body["tools"] = serde_json::json!([{"functionDeclarations": declarations}]);
    }

    body
}

#[cfg(test)]
mod tests {
    use super::*;

    fn config(level: ThinkingLevel) -> StreamConfig {
        StreamConfig {
            model: "gemini-2.5-pro".into(),
            system_prompt: "".into(),
            messages: vec![Message::user("hi")],
            tools: vec![],
            thinking_level: level,
            api_key: "token".into(),
            max_tokens: None,
            temperature: None,
            model_config: None,
            cache_config: CacheConfig::default(),
            output_schema: None,
        }
    }

    #[test]
    fn tool_call_thought_signature_is_replayed() {
        // Parity with the Gemini API provider: signatures captured into
        // provider_metadata must ride back on functionCall parts.
        let mut c = config(ThinkingLevel::Off);
        c.messages = vec![
            Message::user("go"),
            Message::assistant(
                vec![Content::ToolCall {
                    id: "vertex-fc-0".into(),
                    name: "get_weather".into(),
                    arguments: serde_json::json!({"city": "Paris"}),
                    provider_metadata: Some(serde_json::json!({"thought_signature": "sig-9"})),
                }],
                StopReason::ToolUse,
                "m",
                "vertex",
                Usage::default(),
            ),
        ];
        let body = build_vertex_request_body(&c);
        let part = &body["contents"][1]["parts"][0];
        assert_eq!(part["functionCall"]["name"], "get_weather");
        assert_eq!(part["thoughtSignature"], "sig-9");
    }

    #[test]
    fn thinking_level_sets_thinking_config() {
        let body = build_vertex_request_body(&config(ThinkingLevel::High));
        assert_eq!(
            body["generationConfig"]["thinkingConfig"]["thinkingBudget"],
            24576
        );
        assert_eq!(
            body["generationConfig"]["thinkingConfig"]["includeThoughts"],
            true
        );
    }

    #[test]
    fn xhigh_and_max_clamp_to_the_flash_budget_ceiling() {
        for level in [ThinkingLevel::XHigh, ThinkingLevel::Max] {
            let body = build_vertex_request_body(&config(level));
            assert_eq!(
                body["generationConfig"]["thinkingConfig"]["thinkingBudget"],
                24576
            );
        }
        let body = build_vertex_request_body(&config(ThinkingLevel::Medium));
        assert_eq!(
            body["generationConfig"]["thinkingConfig"]["thinkingBudget"],
            8192
        );
    }

    #[test]
    fn thinking_off_omits_thinking_config() {
        let body = build_vertex_request_body(&config(ThinkingLevel::Off));
        assert!(body["generationConfig"]["thinkingConfig"].is_null());
    }

    fn vertex_thinking(model: &str, level: ThinkingLevel) -> serde_json::Value {
        let mut c = config(level);
        c.model = model.into();
        build_vertex_request_body(&c)["generationConfig"]["thinkingConfig"].clone()
    }

    #[test]
    fn gemini_3_on_vertex_sends_thinking_level_not_budget() {
        // Vertex ids arrive bare or as full publisher resource paths.
        for model in [
            "gemini-3.1-pro-preview",
            "publishers/google/models/gemini-3.1-pro-preview",
            "projects/p/locations/global/publishers/google/models/gemini-3.1-pro-preview",
        ] {
            assert_eq!(
                vertex_thinking(model, ThinkingLevel::High),
                serde_json::json!({"thinkingLevel": "HIGH", "includeThoughts": true}),
                "{model}"
            );
            // Off sends nothing: 3.1 Pro then thinks at its default (HIGH).
            assert!(
                vertex_thinking(model, ThinkingLevel::Off).is_null(),
                "{model}"
            );
            // 3.1 Pro has no MINIMAL rung.
            assert_eq!(
                vertex_thinking(model, ThinkingLevel::Minimal)["thinkingLevel"],
                "LOW",
                "{model}"
            );
        }
        // Image models on Vertex: only the levels the Vertex table lists.
        assert_eq!(
            vertex_thinking("gemini-3.1-flash-image", ThinkingLevel::Low)["thinkingLevel"],
            "MINIMAL"
        );
        assert_eq!(
            vertex_thinking("gemini-3-pro-image", ThinkingLevel::Low)["thinkingLevel"],
            "HIGH"
        );
        assert!(vertex_thinking("gemini-3.1-flash-image", ThinkingLevel::Off).is_null());
        assert_eq!(
            vertex_thinking(
                "projects/p/locations/global/publishers/google/models/gemini-3.5-flash-lite",
                ThinkingLevel::Minimal
            )["thinkingLevel"],
            "MINIMAL"
        );
        // Positive control: the 2.5 path id on the same route keeps the budget.
        assert_eq!(
            vertex_thinking(
                "projects/p/locations/global/publishers/google/models/gemini-2.5-pro",
                ThinkingLevel::High
            ),
            serde_json::json!({"thinkingBudget": 24576, "includeThoughts": true})
        );
    }

    #[test]
    fn vertex_honours_the_google_compat_override() {
        let mut c = config(ThinkingLevel::Medium);
        c.model = "gemini-3.8-flash".into();
        let mut mc = ModelConfig::custom(
            crate::provider::ApiProtocol::GoogleVertex,
            "vertex",
            "https://example.invalid",
            "gemini-3.8-flash",
            "G",
        );
        mc.google = Some(crate::provider::GoogleCompat::force_thinking_budget());
        c.model_config = Some(mc);
        assert_eq!(
            build_vertex_request_body(&c)["generationConfig"]["thinkingConfig"],
            serde_json::json!({"thinkingBudget": 8192, "includeThoughts": true})
        );
    }

    #[test]
    fn tool_result_sends_every_text_block() {
        // An MCP server may answer in several text blocks (ESLint's: an intro,
        // the findings, a closing note); the model must see all of them.
        let mut c = config(ThinkingLevel::Off);
        c.messages = vec![Message::ToolResult {
            tool_call_id: "call-1".into(),
            tool_name: "lint".into(),
            content: vec![
                Content::Text {
                    text: "Here are the results:".into(),
                },
                Content::Text {
                    text: "[{\"line\":3}]".into(),
                },
            ],
            is_error: false,
            timestamp: 0,
        }];
        let body = build_vertex_request_body(&c);
        assert_eq!(
            body["contents"][0]["parts"][0]["functionResponse"]["response"]["result"],
            "Here are the results:\n[{\"line\":3}]"
        );
    }

    #[test]
    fn tool_result_with_an_image_sends_every_text_block_and_the_image() {
        let mut c = config(ThinkingLevel::Off);
        c.messages = vec![Message::ToolResult {
            tool_call_id: "call-1".into(),
            tool_name: "screenshot".into(),
            content: vec![
                Content::Text { text: "a".into() },
                Content::Image {
                    data: "AAAA".into(),
                    mime_type: "image/png".into(),
                },
                Content::Text { text: "b".into() },
            ],
            is_error: false,
            timestamp: 0,
        }];
        let body = build_vertex_request_body(&c);
        let parts = &body["contents"][0]["parts"];
        assert_eq!(parts[0]["functionResponse"]["response"]["result"], "a\nb");
        assert_eq!(parts[1]["inlineData"]["data"], "AAAA");
        assert_eq!(parts.as_array().unwrap().len(), 2);
    }
}