octomind 0.26.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
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
// Copyright 2026 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Image processing and attachment utilities

use anyhow::Result;
use base64::{engine::general_purpose, Engine as _};
use image::{DynamicImage, ImageFormat};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

/// Image attachment for messages
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ImageAttachment {
	pub data: ImageData,
	pub media_type: String,
	pub source_type: SourceType,
	pub dimensions: Option<(u32, u32)>,
	pub size_bytes: Option<u64>,
}

/// Image data storage format
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ImageData {
	Base64(String),
	Url(String),
}

/// Source of the image
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum SourceType {
	File(PathBuf),
	Clipboard,
	Url,
}

/// Inline-graphics protocol selected for rendering an image escape sequence
/// suitable for printing via reedline's `ExternalPrinter`.
#[derive(Debug, Copy, Clone)]
enum InlineProtocol {
	/// Kitty graphics protocol — used on Kitty, Ghostty, WezTerm.
	Kitty,
	/// iTerm2 OSC 1337 inline image — used on iTerm2, Tabby, VS Code.
	ITerm2,
}

/// Image processing utilities
pub struct ImageProcessor;

impl ImageProcessor {
	/// Maximum dimensions for API transmission (Anthropic limits)
	const MAX_WIDTH: u32 = 1568;
	const MAX_HEIGHT: u32 = 1568;
	const MAX_FILE_SIZE: u64 = 5 * 1024 * 1024; // 5MB

	/// Load image from file path
	pub fn load_from_path(path: &Path) -> Result<ImageAttachment> {
		// Check file exists and size
		let metadata = std::fs::metadata(path)?;
		if metadata.len() > Self::MAX_FILE_SIZE {
			return Err(anyhow::anyhow!(
				"Image file too large: {}MB (max 5MB)",
				metadata.len() / 1024 / 1024
			));
		}

		// Load and process image
		let img = image::open(path)?;
		let format = ImageFormat::from_path(path)
			.map_err(|_| anyhow::anyhow!("Unsupported image format"))?;

		let media_type = Self::format_to_media_type(format)?;

		// Resize if needed
		let processed_img = Self::resize_if_needed(img);
		let base64_data = Self::encode_to_base64(&processed_img, format)?;

		Ok(ImageAttachment {
			data: ImageData::Base64(base64_data),
			media_type,
			source_type: SourceType::File(path.to_path_buf()),
			dimensions: Some((processed_img.width(), processed_img.height())),
			size_bytes: Some(metadata.len()),
		})
	}

	/// Load image from URL
	pub async fn load_from_url(url: &str) -> Result<ImageAttachment> {
		use reqwest::Client;

		// Validate URL format
		let parsed_url = url::Url::parse(url).map_err(|_| anyhow::anyhow!("Invalid URL format"))?;

		// Check if URL looks like an image
		if let Some(mut path) = parsed_url.path_segments() {
			if let Some(filename) = path.next_back() {
				if !Self::is_supported_image_by_name(filename) {
					return Err(anyhow::anyhow!(
						"URL does not appear to point to a supported image format: {}",
						filename
					));
				}
			}
		}

		// Download the image
		let client = Client::new();
		let response = client
			.get(url)
			.header("User-Agent", "Octomind/1.0")
			.send()
			.await?;

		if !response.status().is_success() {
			return Err(anyhow::anyhow!(
				"Failed to download image: HTTP {}",
				response.status()
			));
		}

		// Check content type
		let content_type = response
			.headers()
			.get("content-type")
			.and_then(|h| h.to_str().ok())
			.unwrap_or("")
			.to_string(); // Convert to owned String

		if !content_type.starts_with("image/") {
			return Err(anyhow::anyhow!(
				"URL does not return an image (content-type: {})",
				content_type
			));
		}

		// Download image data
		let image_bytes = response.bytes().await?;

		if image_bytes.len() > Self::MAX_FILE_SIZE as usize {
			return Err(anyhow::anyhow!(
				"Image too large: {}MB (max 5MB)",
				image_bytes.len() / 1024 / 1024
			));
		}

		// Load and process image
		let img = image::load_from_memory(&image_bytes)?;

		// Determine format from content-type or URL
		let media_type = if content_type.starts_with("image/") {
			content_type.to_string()
		} else {
			// Fallback to URL extension
			Self::guess_media_type_from_url(url).unwrap_or_else(|| "image/png".to_string())
		};

		// Resize if needed
		let processed_img = Self::resize_if_needed(img);

		// Convert to appropriate format for encoding
		let format = Self::media_type_to_format(&media_type)?;
		let base64_data = Self::encode_to_base64(&processed_img, format)?;

		Ok(ImageAttachment {
			data: ImageData::Base64(base64_data),
			media_type,
			source_type: SourceType::Url,
			dimensions: Some((processed_img.width(), processed_img.height())),
			size_bytes: Some(image_bytes.len() as u64),
		})
	}

	/// Load image from clipboard
	pub fn load_from_clipboard() -> Result<Option<ImageAttachment>> {
		use arboard::Clipboard;

		let mut clipboard =
			Clipboard::new().map_err(|_| anyhow::anyhow!("Failed to access clipboard"))?;

		match clipboard.get_image() {
			Ok(img_data) => {
				let attachment = Self::convert_clipboard_image(img_data)?;
				Ok(Some(attachment))
			}
			Err(_) => Ok(None), // No image in clipboard
		}
	}

	/// Convert clipboard image data to attachment
	fn convert_clipboard_image(img_data: arboard::ImageData) -> Result<ImageAttachment> {
		// Convert RGBA bytes to DynamicImage
		let width = img_data.width;
		let height = img_data.height;
		let bytes = img_data.bytes;

		let img = image::RgbaImage::from_raw(width as u32, height as u32, bytes.into_owned())
			.ok_or_else(|| anyhow::anyhow!("Failed to create image from clipboard data"))?;

		let dynamic_img = DynamicImage::ImageRgba8(img);
		let processed_img = Self::resize_if_needed(dynamic_img);

		// Encode as PNG for clipboard images
		let base64_data = Self::encode_to_base64(&processed_img, ImageFormat::Png)?;

		Ok(ImageAttachment {
			data: ImageData::Base64(base64_data),
			media_type: "image/png".to_string(),
			source_type: SourceType::Clipboard,
			dimensions: Some((processed_img.width(), processed_img.height())),
			size_bytes: None, // Unknown for clipboard
		})
	}

	/// Resize image if it exceeds maximum dimensions
	fn resize_if_needed(img: DynamicImage) -> DynamicImage {
		let (width, height) = (img.width(), img.height());

		if width <= Self::MAX_WIDTH && height <= Self::MAX_HEIGHT {
			return img;
		}

		// Calculate new dimensions maintaining aspect ratio
		let ratio =
			(Self::MAX_WIDTH as f32 / width as f32).min(Self::MAX_HEIGHT as f32 / height as f32);

		let new_width = (width as f32 * ratio) as u32;
		let new_height = (height as f32 * ratio) as u32;

		img.resize(new_width, new_height, image::imageops::FilterType::Lanczos3)
	}

	/// Encode image to base64
	fn encode_to_base64(img: &DynamicImage, format: ImageFormat) -> Result<String> {
		let mut buffer = Vec::new();
		img.write_to(&mut std::io::Cursor::new(&mut buffer), format)?;
		Ok(general_purpose::STANDARD.encode(&buffer))
	}

	/// Convert ImageFormat to MIME type
	fn format_to_media_type(format: ImageFormat) -> Result<String> {
		let media_type = match format {
			ImageFormat::Png => "image/png",
			ImageFormat::Jpeg => "image/jpeg",
			ImageFormat::Gif => "image/gif",
			ImageFormat::WebP => "image/webp",
			ImageFormat::Bmp => "image/bmp",
			_ => return Err(anyhow::anyhow!("Unsupported image format for vision API")),
		};
		Ok(media_type.to_string())
	}

	/// Show image preview in terminal
	pub fn show_preview(attachment: &ImageAttachment) -> Result<()> {
		if let ImageData::Base64(ref data) = attachment.data {
			let img_bytes = general_purpose::STANDARD.decode(data)?;
			let img = image::load_from_memory(&img_bytes)?;

			// Show metadata
			if let Some((width, height)) = attachment.dimensions {
				crate::log_info!("📸 Image: {}x{} ({})", width, height, attachment.media_type);
			}

			if let Some(size) = attachment.size_bytes {
				crate::log_info!("📏 Size: {:.1}KB", size as f64 / 1024.0);
			}

			// Display small preview using viuer
			let config = viuer::Config {
				width: Some(40),
				height: Some(20),
				absolute_offset: false,
				..Default::default()
			};

			if let Err(e) = viuer::print(&img, &config) {
				crate::log_debug!("⚠️  Preview not available: {}", e);
			}
		}
		Ok(())
	}

	/// Render the image into a terminal-graphics escape sequence String, suitable
	/// for printing via reedline's `ExternalPrinter` while a prompt is live.
	///
	/// Why this exists separately from `show_preview`: viuer writes directly to
	/// stdout (which reedline owns during input), and Kitty graphics sends an OK
	/// response back on stdin (`\x1b_Gi=N;OK\x1b\\`) that reedline reads as user
	/// input — visibly leaking `Gi=N;OK` into the buffer.
	///
	/// Fix: hand-build the Kitty escape with `q=2` (quiet — terminal sends NO
	/// response, ever, per the Kitty graphics protocol) or fall back to iTerm2
	/// OSC 1337 (fire-and-forget by design). Both protocols render at full
	/// quality — no half-block downgrade.
	///
	/// Returns `None` on terminals without inline-graphics support so the caller
	/// can fall back to a text-only notification.
	pub fn render_inline_escape(attachment: &ImageAttachment) -> Option<String> {
		let ImageData::Base64(ref data) = attachment.data else {
			return None;
		};
		let proto = Self::detect_inline_protocol()?;

		// Re-encode at display resolution so the escape sequence stays small.
		// The full-resolution base64 can be 500 KB+ which makes the terminal
		// stutter when written through reedline's ExternalPrinter. A 40-cell
		// preview only needs ~320 px wide; the terminal still scales by aspect
		// ratio on its end. Falls back to the original base64 if re-encoding
		// fails for any reason.
		let preview_b64 = Self::shrink_for_preview(data).unwrap_or_else(|_| data.clone());
		let cols = 40_u32;
		Some(match proto {
			InlineProtocol::Kitty => Self::build_kitty_escape(&preview_b64, cols),
			InlineProtocol::ITerm2 => Self::build_iterm2_escape(&preview_b64, cols),
		})
	}

	/// Decode the base64 PNG, resize so the longer side is ≤ 320 px, re-encode
	/// as PNG, return the new base64 string. Aspect ratio preserved.
	fn shrink_for_preview(b64: &str) -> Result<String> {
		const MAX_DIM: u32 = 320;
		let bytes = general_purpose::STANDARD.decode(b64)?;
		let img = image::load_from_memory(&bytes)?;
		let (w, h) = (img.width(), img.height());
		let resized = if w > MAX_DIM || h > MAX_DIM {
			let ratio = (MAX_DIM as f32 / w as f32).min(MAX_DIM as f32 / h as f32);
			let nw = (w as f32 * ratio) as u32;
			let nh = (h as f32 * ratio) as u32;
			img.resize(nw, nh, image::imageops::FilterType::Triangle)
		} else {
			img
		};
		let mut buf = Vec::new();
		resized.write_to(&mut std::io::Cursor::new(&mut buf), ImageFormat::Png)?;
		Ok(general_purpose::STANDARD.encode(&buf))
	}

	fn detect_inline_protocol() -> Option<InlineProtocol> {
		if std::env::var("KITTY_WINDOW_ID").is_ok() {
			return Some(InlineProtocol::Kitty);
		}
		if let Ok(term) = std::env::var("TERM") {
			if term.contains("kitty") {
				return Some(InlineProtocol::Kitty);
			}
		}
		match std::env::var("TERM_PROGRAM").as_deref() {
			// Ghostty and WezTerm both implement the Kitty graphics protocol natively
			// and produce its highest-quality output.
			Ok("ghostty") | Ok("WezTerm") => Some(InlineProtocol::Kitty),
			// iTerm2, Tabby, VS Code's terminal use the iTerm2 OSC 1337 protocol.
			Ok("iTerm.app") | Ok("Tabby") | Ok("vscode") => Some(InlineProtocol::ITerm2),
			_ => None,
		}
	}

	/// Build a Kitty graphics escape sequence with `q=2` (silent — no responses).
	/// Only `c` (cell width) is specified; the terminal scales the image height
	/// from the aspect ratio. Reserving a fixed `r=20` rows confused reedline's
	/// prompt redraw and pushed the cursor off-screen on screenshot pastes.
	/// Per the Kitty protocol, each escape ≤ 4096 bytes, so the base64 payload
	/// is split into chunks: first chunk carries the metadata; subsequent ones
	/// declare `m=1` (more) or `m=0` (last).
	fn build_kitty_escape(b64: &str, cols: u32) -> String {
		const CHUNK: usize = 4096;
		let bytes = b64.as_bytes();
		let chunk_count = bytes.len().div_ceil(CHUNK);
		let mut out = String::with_capacity(bytes.len() + chunk_count * 32);
		for (i, chunk) in bytes.chunks(CHUNK).enumerate() {
			let more = if i + 1 < chunk_count { 1 } else { 0 };
			let chunk_str = std::str::from_utf8(chunk).unwrap_or("");
			if i == 0 {
				out.push_str(&format!(
					"\x1b_Ga=T,f=100,q=2,c={},m={};{}\x1b\\",
					cols, more, chunk_str
				));
			} else {
				out.push_str(&format!("\x1b_Gm={};{}\x1b\\", more, chunk_str));
			}
		}
		out
	}

	/// Build an iTerm2 OSC 1337 inline-image escape — fire-and-forget, no ACK.
	/// `preserveAspectRatio=1` lets the terminal compute the height from `width`.
	fn build_iterm2_escape(b64: &str, cols: u32) -> String {
		format!(
			"\x1b]1337;File=inline=1;width={};preserveAspectRatio=1:{}\x07",
			cols, b64
		)
	}

	/// Check if file is a supported image format
	pub fn is_supported_image(path: &Path) -> bool {
		if let Some(extension) = path.extension() {
			if let Some(ext_str) = extension.to_str() {
				Self::is_supported_extension(ext_str)
			} else {
				false
			}
		} else {
			false
		}
	}

	/// Check if filename has supported image extension
	pub fn is_supported_image_by_name(filename: &str) -> bool {
		if let Some(ext) = filename.split('.').next_back() {
			Self::is_supported_extension(ext)
		} else {
			false
		}
	}

	/// Check if extension is supported
	fn is_supported_extension(ext: &str) -> bool {
		matches!(
			ext.to_lowercase().as_str(),
			"png" | "jpg" | "jpeg" | "gif" | "webp" | "bmp"
		)
	}

	/// Guess media type from URL
	fn guess_media_type_from_url(url: &str) -> Option<String> {
		if let Some(ext) = url.split('.').next_back() {
			match ext.to_lowercase().as_str() {
				"png" => Some("image/png".to_string()),
				"jpg" | "jpeg" => Some("image/jpeg".to_string()),
				"gif" => Some("image/gif".to_string()),
				"webp" => Some("image/webp".to_string()),
				"bmp" => Some("image/bmp".to_string()),
				_ => None,
			}
		} else {
			None
		}
	}

	/// Convert media type to ImageFormat
	fn media_type_to_format(media_type: &str) -> Result<ImageFormat> {
		match media_type {
			"image/png" => Ok(ImageFormat::Png),
			"image/jpeg" => Ok(ImageFormat::Jpeg),
			"image/gif" => Ok(ImageFormat::Gif),
			"image/webp" => Ok(ImageFormat::WebP),
			"image/bmp" => Ok(ImageFormat::Bmp),
			_ => Ok(ImageFormat::Png), // Default to PNG
		}
	}

	/// Get supported image extensions for autocomplete
	pub fn supported_extensions() -> &'static [&'static str] {
		&["png", "jpg", "jpeg", "gif", "webp", "bmp"]
	}

	/// Check if input string is a URL
	pub fn is_url(input: &str) -> bool {
		input.starts_with("http://") || input.starts_with("https://")
	}
}

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

	#[test]
	fn test_supported_extensions() {
		let extensions = ImageProcessor::supported_extensions();
		assert!(extensions.contains(&"png"));
		assert!(extensions.contains(&"jpg"));
	}

	#[test]
	fn test_is_supported_image() {
		assert!(ImageProcessor::is_supported_image(Path::new("test.png")));
		assert!(ImageProcessor::is_supported_image(Path::new("test.JPG")));
		assert!(!ImageProcessor::is_supported_image(Path::new("test.txt")));
	}
}