pi_agent_rust 0.8.0

Native AI coding agent CLI - Rust port of Pi Agent
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
//! Native image inspection: upload the actual image and return the model's answer.

use super::MAX_IMAGE_FILE_SIZE_BYTES;
use super::transport::{self, Transport};
use crate::error::{Error, Result};
use crate::http::client::Client;
use crate::model::{ContentBlock, TextContent};
use crate::tools::{Tool, ToolEffects, ToolOutput, ToolUpdate};
use async_trait::async_trait;
use base64::Engine as _;
use serde_json::{Value, json};
use std::path::{Path, PathBuf};

const NAME: &str = "inspect_image";
const DEFAULT_PROMPT: &str =
    "Describe this image in detail, including visible text, diagrams, UI components, and errors.";
const MAX_ANALYSIS_BYTES: usize = 64 * 1024;

pub struct InspectImageTool {
    cwd: PathBuf,
    default_provider: Option<String>,
    default_model: Option<String>,
    mock_mode: Option<bool>,
    api_key: Option<String>,
    transport: Transport,
}

impl InspectImageTool {
    pub fn new(cwd: &Path) -> Self {
        Self::with_defaults(cwd, None, None)
    }

    pub fn with_defaults(cwd: &Path, provider: Option<String>, model: Option<String>) -> Self {
        Self {
            cwd: cwd.to_path_buf(),
            default_provider: provider,
            default_model: model,
            mock_mode: None,
            api_key: None,
            transport: Transport::default(),
        }
    }

    #[must_use]
    pub const fn with_mock(mut self, mock: bool) -> Self {
        self.mock_mode = Some(mock);
        self
    }

    #[must_use]
    pub fn with_api_key(mut self, key: Option<String>) -> Self {
        self.api_key = key;
        self
    }

    /// Override the API base for an explicitly trusted gateway or a protocol test.
    #[must_use]
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.transport = self.transport.with_base_url(base_url.into());
        self
    }

    /// Preserve the caller's HTTP configuration and optional VCR recorder.
    #[must_use]
    pub fn with_client(mut self, client: Client) -> Self {
        self.transport = self.transport.with_client(client);
        self
    }
}

#[async_trait]
#[allow(clippy::unnecessary_literal_bound)]
impl Tool for InspectImageTool {
    fn name(&self) -> &str {
        NAME
    }
    fn label(&self) -> &str {
        "Inspect Image"
    }
    fn description(&self) -> &str {
        "Send a local PNG, JPEG, WebP or GIF to a vision provider for description, OCR or diagram analysis. Returns the actual provider answer; requires a media API key."
    }
    fn parameters(&self) -> Value {
        json!({
            "type": "object", "required": ["path"],
            "properties": {
                "path": {"type": "string", "description": "Local image path (PNG, JPEG, WebP, GIF; at most 20 MiB)"},
                "prompt": {"type": "string", "description": "Question or analysis instructions for the image"},
                "provider": {"type": "string", "enum": ["openai", "anthropic", "gemini"]},
                "model": {"type": "string", "description": "Vision model ID; overrides media.vision_model"},
                "detail": {"type": "string", "enum": ["low", "high", "auto"], "description": "OpenAI image detail (default auto)"},
                "timeout_ms": {"type": "integer", "minimum": 1, "maximum": 300_000, "default": 120_000}
            }
        })
    }
    fn effects(&self) -> ToolEffects {
        ToolEffects::read().union(ToolEffects::network())
    }

    #[allow(clippy::too_many_lines)]
    async fn execute(
        &self,
        _tool_call_id: &str,
        args: Value,
        _on_update: Option<Box<dyn Fn(ToolUpdate) + Send + Sync>>,
    ) -> Result<ToolOutput> {
        let path_str = transport::required(&args, NAME, "path")?;
        let path = self.cwd.join(path_str);
        if !path.is_file() {
            return Err(Error::tool(
                NAME,
                format!("image file not found: {}", path.display()),
            ));
        }
        let metadata = std::fs::metadata(&path)
            .map_err(|error| Error::tool(NAME, format!("cannot stat image file: {error}")))?;
        if metadata.len() > MAX_IMAGE_FILE_SIZE_BYTES {
            return Err(Error::tool(
                NAME,
                format!("image file exceeds 20 MiB limit: {} bytes", metadata.len()),
            ));
        }
        let ext = path
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("")
            .to_ascii_lowercase();
        let mime = match ext.as_str() {
            "png" => "image/png",
            "jpg" | "jpeg" => "image/jpeg",
            "webp" => "image/webp",
            "gif" => "image/gif",
            "svg" => "image/svg+xml",
            "bmp" => "image/bmp",
            _ => {
                return Err(Error::tool(
                    NAME,
                    format!("unsupported image extension: .{ext}"),
                ));
            }
        };
        let prompt = transport::optional(&args, NAME, "prompt")?.unwrap_or(DEFAULT_PROMPT);
        if prompt.trim().is_empty() || prompt.len() > 128 * 1024 {
            return Err(Error::tool(
                NAME,
                "prompt must be nonempty and at most 128 KiB",
            ));
        }
        let detail = transport::optional(&args, NAME, "detail")?.unwrap_or("auto");
        if !matches!(detail, "auto" | "low" | "high") {
            return Err(Error::tool(NAME, "detail must be low, high or auto"));
        }
        let timeout = transport::timeout(&args, NAME, 120_000)?;
        let is_mock = self
            .mock_mode
            .unwrap_or_else(|| std::env::var("PI_MEDIA_MOCK").unwrap_or_default() == "1");
        if is_mock {
            return Ok(ToolOutput {
                content: vec![ContentBlock::Text(TextContent::new(format!(
                    "Image Analysis for {path_str} ({mime}, {} bytes):\nPrompt: {prompt}\nCanned test fixture inspection completed successfully; no provider request was made.",
                    metadata.len()
                )))],
                details: Some(
                    json!({"path": path.display().to_string(), "mime_type": mime, "size_bytes": metadata.len(),
                    "provider": self.default_provider, "model": self.default_model, "mock": true}),
                ),
                is_error: false,
            });
        }
        let env_provider = std::env::var("PI_VISION_PROVIDER").ok();
        let provider = transport::provider(
            NAME,
            transport::optional(&args, NAME, "provider")?
                .or(self.default_provider.as_deref())
                .or(env_provider.as_deref())
                .unwrap_or("gemini"),
        )?;
        let fallback_model = match provider {
            "openai" => "gpt-4.1-mini",
            "anthropic" => "claude-opus-5",
            "gemini" => "gemini-2.5-flash",
            _ => {
                return Err(Error::tool(
                    NAME,
                    "vision provider must be openai, anthropic or gemini",
                ));
            }
        };
        let env_model = std::env::var("PI_VISION_MODEL").ok();
        let model = transport::model_id(
            NAME,
            transport::optional(&args, NAME, "model")?
                .or(self.default_model.as_deref())
                .or(env_model.as_deref())
                .unwrap_or(fallback_model),
        )?;
        let api = self
            .transport
            .api(NAME, provider, self.api_key.as_deref(), timeout)?;
        if matches!(mime, "image/svg+xml" | "image/bmp") {
            return Err(Error::tool(
                NAME,
                "convert SVG/BMP to PNG, JPEG, WebP or GIF before vision inspection",
            ));
        }
        let bytes = transport::read_capped(&path, NAME, MAX_IMAGE_FILE_SIZE_BYTES)?;
        if transport::image_mime(&bytes) != Some(mime) {
            return Err(Error::tool(
                NAME,
                "image bytes are truncated, unsupported, or do not match the file extension",
            ));
        }
        let size = bytes.len();
        let encoded = base64::engine::general_purpose::STANDARD.encode(&bytes);
        let (endpoint, payload) = match provider {
            "openai" => (
                "chat/completions".to_string(),
                json!({
                    "model": model, "stream": false, "max_completion_tokens": 4096,
                    "messages": [{"role": "user", "content": [
                        {"type": "image_url", "image_url": {"url": format!("data:{mime};base64,{encoded}"), "detail": detail}},
                        {"type": "text", "text": prompt}
                    ]}]
                }),
            ),
            "anthropic" => (
                "messages".to_string(),
                json!({
                    "model": model, "max_tokens": 4096,
                    "messages": [{"role": "user", "content": [
                        {"type": "image", "source": {"type": "base64", "media_type": mime, "data": encoded}},
                        {"type": "text", "text": prompt}
                    ]}]
                }),
            ),
            _ => (
                transport::gemini_path(NAME, model)?,
                json!({
                    "contents": [{"role": "user", "parts": [
                        {"inlineData": {"mimeType": mime, "data": encoded}}, {"text": prompt}
                    ]}],
                    "generationConfig": {"maxOutputTokens": 4096}
                }),
            ),
        };
        let response = api
            .post(&endpoint, &payload, 1024 * 1024)
            .await?
            .json(NAME)?;
        let analysis = parse_analysis(provider, &response)?;
        let analysis = api.scrub(&analysis, MAX_ANALYSIS_BYTES);
        Ok(ToolOutput {
            content: vec![ContentBlock::Text(TextContent::new(analysis))],
            details: Some(
                json!({"path": path.display().to_string(), "mime_type": mime, "size_bytes": size,
                "provider": provider, "model": model, "mock": false}),
            ),
            is_error: false,
        })
    }
}

fn parse_analysis(provider: &str, response: &Value) -> Result<String> {
    let incomplete = || {
        Error::tool(
            NAME,
            "vision response was refused, incomplete, or missing its completion marker",
        )
    };
    let text = match provider {
        "openai" => {
            let choice = response.pointer("/choices/0").ok_or_else(incomplete)?;
            if choice["finish_reason"] != "stop"
                || choice
                    .pointer("/message/refusal")
                    .is_some_and(|value| !value.is_null())
            {
                return Err(incomplete());
            }
            let content = &choice["message"]["content"];
            content
                .as_str()
                .map_or_else(|| text_blocks(content, true), str::to_string)
        }
        "anthropic" => {
            if !matches!(
                response["stop_reason"].as_str(),
                Some("end_turn" | "stop_sequence")
            ) {
                return Err(incomplete());
            }
            text_blocks(&response["content"], true)
        }
        "gemini" => {
            if response
                .pointer("/promptFeedback/blockReason")
                .is_some_and(|value| !value.is_null())
            {
                return Err(incomplete());
            }
            let candidate = response.pointer("/candidates/0").ok_or_else(incomplete)?;
            if candidate["finishReason"] != "STOP" {
                return Err(incomplete());
            }
            text_blocks(&candidate["content"]["parts"], false)
        }
        _ => return Err(Error::tool(NAME, "unsupported vision response")),
    };
    if text.trim().is_empty() || text.len() > MAX_ANALYSIS_BYTES {
        return Err(Error::tool(
            NAME,
            "vision provider returned no analysis or exceeded the 64 KiB analysis limit",
        ));
    }
    Ok(text)
}

fn text_blocks(content: &Value, typed: bool) -> String {
    content
        .as_array()
        .into_iter()
        .flatten()
        .filter(|block| (!typed || block["type"] == "text") && block["thought"] != true)
        .filter_map(|block| block["text"].as_str())
        .collect::<Vec<_>>()
        .join("\n")
}

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

    #[test]
    fn all_three_native_vision_envelopes_upload_the_image_and_return_the_peer_answer() {
        let cases = [
            (
                "openai",
                "chat/completions",
                json!({"choices":[{"finish_reason":"stop","message":{"content":"A red diagram."}}]}),
            ),
            (
                "anthropic",
                "messages",
                json!({"stop_reason":"end_turn","content":[{"type":"text","text":"A red diagram."}]}),
            ),
            (
                "gemini",
                "models/fixture-vision:generateContent",
                json!({"candidates":[{"finishReason":"STOP","content":{"parts":[{"text":"hidden reasoning","thought":true},{"text":"A red diagram."}]}}]}),
            ),
        ];
        for (provider, path, response) in cases {
            let (endpoint, worker) = peer(
                200,
                "application/json",
                serde_json::to_vec(&response).unwrap(),
            );
            let dir = tempfile::tempdir().unwrap();
            std::fs::write(dir.path().join("input.png"), super::super::MIN_VALID_PNG).unwrap();
            let tool = InspectImageTool::with_defaults(
                dir.path(),
                Some(provider.into()),
                Some("fixture-vision".into()),
            )
            .with_mock(false)
            .with_api_key(Some("test-vision-secret".into()))
            .with_base_url(endpoint);
            let runtime = asupersync::runtime::RuntimeBuilder::current_thread()
                .build()
                .unwrap();
            let output = runtime
                .block_on(tool.execute(
                    "vision",
                    json!({"path":"input.png","prompt":"Read the diagram"}),
                    None,
                ))
                .unwrap();
            assert!(
                matches!(&output.content[0], ContentBlock::Text(text) if text.text == "A red diagram.")
            );
            assert_eq!(output.details.as_ref().unwrap()["provider"], provider);
            let request = worker.join().unwrap();
            assert!(request.headers.starts_with(&format!("POST /v1/{path} ")));
            let image = match provider {
                "openai" => request
                    .body
                    .pointer("/messages/0/content/0/image_url/url")
                    .unwrap()
                    .as_str()
                    .unwrap()
                    .strip_prefix("data:image/png;base64,")
                    .unwrap(),
                "anthropic" => request
                    .body
                    .pointer("/messages/0/content/0/source/data")
                    .unwrap()
                    .as_str()
                    .unwrap(),
                _ => request
                    .body
                    .pointer("/contents/0/parts/0/inlineData/data")
                    .unwrap()
                    .as_str()
                    .unwrap(),
            };
            assert_eq!(
                base64::engine::general_purpose::STANDARD
                    .decode(image)
                    .unwrap(),
                super::super::MIN_VALID_PNG
            );
            let headers = request.headers.to_ascii_lowercase();
            let auth_header = match provider {
                "anthropic" => "x-api-key: test-vision-secret",
                "gemini" => "x-goog-api-key: test-vision-secret",
                _ => "authorization: bearer test-vision-secret",
            };
            assert!(headers.contains(auth_header));
            assert!(
                !request
                    .headers
                    .lines()
                    .next()
                    .unwrap()
                    .contains("test-vision-secret")
            );
        }
    }

    #[test]
    fn refusal_partial_empty_and_missing_terminal_responses_are_errors() {
        for response in [
            json!({}),
            json!({"choices":[]}),
            json!({"choices":[{"finish_reason":"length","message":{"content":"partial"}}]}),
            json!({"choices":[{"finish_reason":"stop","message":{"content":"","refusal":"declined"}}]}),
        ] {
            assert!(parse_analysis("openai", &response).is_err());
        }
        assert!(
            parse_analysis(
                "anthropic",
                &json!({"stop_reason":"max_tokens","content":[{"type":"text","text":"partial"}]})
            )
            .is_err()
        );
        assert!(
            parse_analysis(
                "gemini",
                &json!({"promptFeedback":{"blockReason":"SAFETY"}})
            )
            .is_err()
        );
    }

    #[test]
    fn authentication_failure_is_not_analysis_and_does_not_echo_the_key() {
        let key = "test-secret-never-show-this";
        let (endpoint, worker) = peer(
            401,
            "application/json",
            serde_json::to_vec(&json!({"error":{"message":format!("invalid token {key}")}}))
                .unwrap(),
        );
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("input.png"), super::super::MIN_VALID_PNG).unwrap();
        let tool = InspectImageTool::with_defaults(dir.path(), Some("openai".into()), None)
            .with_mock(false)
            .with_api_key(Some(key.into()))
            .with_base_url(endpoint);
        let runtime = asupersync::runtime::RuntimeBuilder::current_thread()
            .build()
            .unwrap();
        let error = runtime
            .block_on(tool.execute("vision-error", json!({"path":"input.png"}), None))
            .unwrap_err()
            .to_string();
        assert!(error.contains("HTTP 401"));
        assert!(!error.contains(key));
        worker.join().unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn non_utf8_working_directory_does_not_panic_while_serializing_details() {
        use std::os::unix::ffi::OsStringExt as _;
        let dir = tempfile::tempdir().unwrap();
        let cwd = dir
            .path()
            .join(std::ffi::OsString::from_vec(vec![b'i', 0xff]));
        // APFS rejects a name that is not valid UTF-8 outright (EILSEQ), so the
        // panic this test guards against is unreachable there: there is no such
        // working directory to serialize. Skipping is the honest result on those
        // filesystems; panicking would report a filesystem policy as a pi bug.
        match std::fs::create_dir(&cwd) {
            Ok(()) => {}
            Err(error) if error.raw_os_error() == Some(rustix::io::Errno::ILSEQ.raw_os_error()) => {
                eprintln!("SKIP non-UTF-8 working directory {cwd:?}: filesystem returned {error}");
                return;
            }
            // Any other errno is a real failure, not a filesystem policy.
            // ubs:ignore-next-line test fixture — the arm EILSEQ does not take
            Err(error) => panic!("create non-UTF-8 working directory {cwd:?}: {error}"),
        }
        std::fs::write(cwd.join("input.png"), super::super::MIN_VALID_PNG).unwrap();
        let tool = InspectImageTool::new(&cwd).with_mock(true);
        let output = futures::executor::block_on(tool.execute(
            "non-utf8",
            json!({"path":"input.png"}),
            None,
        ))
        .unwrap();
        assert!(
            output.details.as_ref().unwrap()["path"]
                .as_str()
                .unwrap()
                .ends_with("input.png")
        );
    }
}