edgecrab-tools 0.1.0

Tool registry, ToolHandler trait, and 50+ tool implementations
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
490
491
//! # advanced — Advanced tool implementations
//!
//! Provides real implementations for media-generation tools when API keys
//! are present, plus stub implementations for tools requiring additional
//! external infrastructure.
//!
//! ## Image Generation
//!
//! `GenerateImageTool` supports two backends, checked in order:
//! 1. **FAL.ai** (`FAL_KEY` env var) — uses `fal-ai/flux-pro` model
//! 2. **OpenAI DALL-E 3** (`OPENAI_API_KEY` env var) — 1024×1024 by default
//!
//! Generated images are saved to `~/.edgecrab/generated/` and returned as
//! `MEDIA:/absolute/path` so gateway platforms can deliver them natively.

use async_trait::async_trait;
use serde_json::json;

use edgecrab_types::{Platform, ToolError, ToolSchema};

use crate::registry::{ToolContext, ToolHandler};

// ─── GenerateImageTool ────────────────────────────────────────────────

pub struct GenerateImageTool;

impl GenerateImageTool {
    /// Returns `true` when at least one image-generation API key is set.
    fn backend() -> Option<&'static str> {
        if std::env::var("FAL_KEY").is_ok() {
            Some("fal")
        } else if std::env::var("OPENAI_API_KEY").is_ok() {
            Some("openai")
        } else {
            None
        }
    }
}

#[async_trait]
impl ToolHandler for GenerateImageTool {
    fn name(&self) -> &'static str {
        "generate_image"
    }
    fn toolset(&self) -> &'static str {
        "media"
    }
    fn emoji(&self) -> &'static str {
        "🎨"
    }

    fn is_available(&self) -> bool {
        Self::backend().is_some()
    }

    fn schema(&self) -> ToolSchema {
        ToolSchema {
            name: "generate_image".into(),
            description: "Generate an image from a text description using FAL.ai FLUX or OpenAI DALL-E 3. Returns the file path of the saved image.".into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "prompt": {
                        "type": "string",
                        "description": "Detailed description of the image to generate"
                    },
                    "size": {
                        "type": "string",
                        "description": "Image size: 'square' (1024x1024), 'landscape' (1536x1024), 'portrait' (1024x1536). Default: square",
                        "enum": ["square", "landscape", "portrait"]
                    },
                    "quality": {
                        "type": "string",
                        "description": "Quality: 'standard' or 'hd'. Default: standard (DALL-E only)",
                        "enum": ["standard", "hd"]
                    }
                },
                "required": ["prompt"]
            }),
            strict: None,
        }
    }

    async fn execute(
        &self,
        args: serde_json::Value,
        _ctx: &ToolContext,
    ) -> Result<String, ToolError> {
        let prompt = args["prompt"]
            .as_str()
            .ok_or_else(|| ToolError::InvalidArgs {
                tool: "generate_image".into(),
                message: "prompt is required".into(),
            })?;
        let size = args["size"].as_str().unwrap_or("square");
        let quality = args["quality"].as_str().unwrap_or("standard");

        // Ensure output directory exists
        let out_dir = dirs::home_dir()
            .unwrap_or_else(|| std::path::PathBuf::from("."))
            .join(".edgecrab")
            .join("generated");
        tokio::fs::create_dir_all(&out_dir)
            .await
            .map_err(|e| ToolError::Other(format!("Cannot create output dir: {e}")))?;

        let filename = format!("{}.png", uuid::Uuid::new_v4().to_string().replace('-', ""));
        let out_path = out_dir.join(&filename);

        match Self::backend() {
            Some("fal") => {
                generate_fal(prompt, size, &out_path).await?;
            }
            Some("openai") | Some(_) => {
                generate_openai(prompt, size, quality, &out_path).await?;
            }
            None => {
                return Err(ToolError::Unavailable {
                    tool: "generate_image".into(),
                    reason: "Set FAL_KEY (for FLUX) or OPENAI_API_KEY (for DALL-E 3) to enable."
                        .into(),
                });
            }
        }

        let path_str = out_path.to_string_lossy().to_string();
        Ok(format!("Image generated: MEDIA:{path_str}"))
    }
}

inventory::submit!(&GenerateImageTool as &dyn ToolHandler);

// ─── FAL.ai backend ───────────────────────────────────────────────────

/// Generate an image via FAL.ai FLUX API and save to `out_path`.
async fn generate_fal(
    prompt: &str,
    size: &str,
    out_path: &std::path::Path,
) -> Result<(), ToolError> {
    // FAL.ai image_size presets
    let image_size = match size {
        "landscape" => "landscape_16_9",
        "portrait" => "portrait_16_9",
        _ => "square_hd", // square
    };

    let fal_key = std::env::var("FAL_KEY").unwrap_or_default();
    let client = reqwest::Client::new();

    let body = json!({
        "prompt": prompt,
        "image_size": image_size,
        "num_inference_steps": 28,
        "guidance_scale": 3.5,
        "num_images": 1,
        "enable_safety_checker": false,
        "output_format": "png",
        "sync_mode": true
    });

    let resp = client
        .post("https://fal.run/fal-ai/flux-pro/v1.1")
        .header("Authorization", format!("Key {fal_key}"))
        .header("Content-Type", "application/json")
        .json(&body)
        .timeout(std::time::Duration::from_secs(120))
        .send()
        .await
        .map_err(|e| ToolError::Other(format!("FAL request failed: {e}")))?;

    if !resp.status().is_success() {
        let status = resp.status();
        let text = resp.text().await.unwrap_or_default();
        return Err(ToolError::Other(format!("FAL API error {status}: {text}")));
    }

    let json: serde_json::Value = resp
        .json()
        .await
        .map_err(|e| ToolError::Other(format!("FAL response parse error: {e}")))?;

    let image_url = json["images"][0]["url"]
        .as_str()
        .ok_or_else(|| ToolError::Other("FAL response missing image URL".into()))?;

    download_image(image_url, out_path).await
}

// ─── OpenAI DALL-E 3 backend ──────────────────────────────────────────

/// Generate an image via OpenAI DALL-E 3 API and save to `out_path`.
async fn generate_openai(
    prompt: &str,
    size: &str,
    quality: &str,
    out_path: &std::path::Path,
) -> Result<(), ToolError> {
    let api_key = std::env::var("OPENAI_API_KEY").unwrap_or_default();
    let client = reqwest::Client::new();

    let dall_e_size = match size {
        "landscape" => "1792x1024",
        "portrait" => "1024x1792",
        _ => "1024x1024",
    };

    let body = json!({
        "model": "dall-e-3",
        "prompt": prompt,
        "n": 1,
        "size": dall_e_size,
        "quality": quality,
        "response_format": "url"
    });

    let resp = client
        .post("https://api.openai.com/v1/images/generations")
        .bearer_auth(&api_key)
        .json(&body)
        .timeout(std::time::Duration::from_secs(120))
        .send()
        .await
        .map_err(|e| ToolError::Other(format!("DALL-E request failed: {e}")))?;

    if !resp.status().is_success() {
        let status = resp.status();
        let text = resp.text().await.unwrap_or_default();
        return Err(ToolError::Other(format!(
            "DALL-E API error {status}: {text}"
        )));
    }

    let json: serde_json::Value = resp
        .json()
        .await
        .map_err(|e| ToolError::Other(format!("DALL-E response parse error: {e}")))?;

    let image_url = json["data"][0]["url"]
        .as_str()
        .ok_or_else(|| ToolError::Other("DALL-E response missing image URL".into()))?;

    download_image(image_url, out_path).await
}

// ─── Shared download helper ───────────────────────────────────────────

/// Download an image URL and write raw bytes to `out_path`.
async fn download_image(url: &str, out_path: &std::path::Path) -> Result<(), ToolError> {
    let resp = reqwest::get(url)
        .await
        .map_err(|e| ToolError::Other(format!("Image download failed: {e}")))?;
    let bytes = resp
        .bytes()
        .await
        .map_err(|e| ToolError::Other(format!("Image read failed: {e}")))?;
    tokio::fs::write(out_path, &bytes)
        .await
        .map_err(|e| ToolError::Other(format!("Image write failed: {e}")))
}

// ─── SendMessageTool ──────────────────────────────────────────────────

pub struct SendMessageTool;

#[async_trait]
impl ToolHandler for SendMessageTool {
    fn name(&self) -> &'static str {
        "send_message"
    }

    fn toolset(&self) -> &'static str {
        "messaging"
    }

    fn emoji(&self) -> &'static str {
        "📨"
    }

    fn is_available(&self) -> bool {
        // Available when gateway is running OR when explicitly configured
        true
    }

    fn check_fn(&self, ctx: &ToolContext) -> bool {
        // Expose send_message only when a real outbound gateway transport is
        // available for this session. This keeps CLI/cron schemas honest while
        // still making messaging a core capability in live gateway sessions.
        ctx.platform != Platform::Cron && ctx.gateway_sender.is_some()
    }

    fn schema(&self) -> ToolSchema {
        ToolSchema {
            name: "send_message".into(),
            description:
                "Send a message to a user via a platform channel, or list available targets.\n\n\
                IMPORTANT: When the user asks to send to a specific channel or person, \
                call send_message(action='list') FIRST to see available targets, then \
                send to the correct one. If the user names only a platform \
                (for example 'telegram'), use the platform home channel."
                    .into(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "enum": ["send", "list"],
                        "description": "Action: 'send' sends a message, 'list' returns available targets."
                    },
                    "target": {
                        "type": "string",
                        "description": "Preferred delivery target. Format: 'platform' (home channel), 'platform:recipient', or 'platform:chat_id:thread_id'. Examples: 'telegram', 'telegram:-1001234567890:17', 'discord:#bot-home', 'email:alice@example.com'."
                    },
                    "platform": {
                        "type": "string",
                        "description": "Backward-compatible platform field when 'target' is not provided."
                    },
                    "recipient": {
                        "type": "string",
                        "description": "Backward-compatible recipient field. Empty or omitted uses the platform home channel."
                    },
                    "message": {
                        "type": "string",
                        "description": "Message content to send"
                    }
                },
                "required": []
            }),
            strict: None,
        }
    }

    async fn execute(
        &self,
        args: serde_json::Value,
        ctx: &ToolContext,
    ) -> Result<String, ToolError> {
        let action = args["action"].as_str().unwrap_or("send");

        let sender = ctx
            .gateway_sender
            .as_ref()
            .ok_or_else(|| ToolError::Unavailable {
                tool: "send_message".into(),
                reason: "Gateway not running — send_message requires an active gateway.".into(),
            })?;

        if action == "list" {
            return match sender.list_targets().await {
                Ok(targets) => Ok(json!({ "targets": targets }).to_string()),
                Err(e) => Err(ToolError::Other(format!("Failed to list targets: {e}"))),
            };
        }

        // action == "send"
        let (platform, recipient) = parse_send_target(&args)?;
        let message = args["message"]
            .as_str()
            .ok_or_else(|| ToolError::InvalidArgs {
                tool: "send_message".into(),
                message: "'message' is required when action='send'".into(),
            })?;

        match sender.send_message(platform, recipient, message).await {
            Ok(()) => Ok(json!({
                "status": "sent",
                "platform": platform,
                "recipient": recipient,
            })
            .to_string()),
            Err(e) => Err(ToolError::Other(format!("Send failed: {e}"))),
        }
    }
}

fn parse_send_target(args: &serde_json::Value) -> Result<(&str, &str), ToolError> {
    if let Some(target) = args["target"].as_str() {
        let trimmed = target.trim();
        if trimmed.is_empty() {
            return Err(ToolError::InvalidArgs {
                tool: "send_message".into(),
                message: "'target' cannot be empty when provided".into(),
            });
        }

        if let Some((platform, recipient)) = trimmed.split_once(':') {
            return Ok((platform.trim(), recipient.trim()));
        }

        return Ok((trimmed, ""));
    }

    let platform = args["platform"]
        .as_str()
        .ok_or_else(|| ToolError::InvalidArgs {
            tool: "send_message".into(),
            message: "Either 'target' or 'platform' is required when action='send'".into(),
        })?;
    let recipient = args["recipient"].as_str().unwrap_or("");
    Ok((platform, recipient))
}

inventory::submit!(&SendMessageTool as &dyn ToolHandler);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::registry::GatewaySender;
    use async_trait::async_trait;

    struct MockGatewaySender;

    #[async_trait]
    impl GatewaySender for MockGatewaySender {
        async fn send_message(
            &self,
            _platform: &str,
            _recipient: &str,
            _message: &str,
        ) -> Result<(), String> {
            Ok(())
        }

        async fn list_targets(&self) -> Result<Vec<String>, String> {
            Ok(vec!["telegram".into()])
        }
    }

    #[test]
    fn generate_image_unavailable_without_key() {
        // is_available() / backend() depends on env vars — just verify the logic is consistent
        let has_backend = GenerateImageTool::backend().is_some();
        assert_eq!(GenerateImageTool.is_available(), has_backend);
    }

    #[test]
    fn send_message_is_available() {
        assert!(SendMessageTool.is_available());
    }

    #[test]
    fn send_message_check_fn_requires_gateway_sender() {
        let ctx = ToolContext::test_context();
        assert!(!SendMessageTool.check_fn(&ctx));

        let mut ctx = ToolContext::test_context();
        ctx.gateway_sender = Some(std::sync::Arc::new(MockGatewaySender));
        assert!(SendMessageTool.check_fn(&ctx));
    }

    #[tokio::test]
    async fn send_message_requires_gateway_sender() {
        let ctx = ToolContext::test_context();
        let result = SendMessageTool
            .execute(
                json!({"action": "send", "platform": "telegram", "recipient": "123", "message": "hi"}),
                &ctx,
            )
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn generate_image_returns_unavailable_when_no_key() {
        // Only run if no API key is configured
        if GenerateImageTool::backend().is_some() {
            return; // Skip in CI with live keys
        }
        let ctx = ToolContext::test_context();
        let result = GenerateImageTool
            .execute(json!({"prompt": "a cat"}), &ctx)
            .await;
        assert!(matches!(result, Err(ToolError::Unavailable { .. })));
    }

    #[test]
    fn send_message_target_platform_only_uses_home_channel_mode() {
        let args = json!({"action": "send", "target": "telegram"});
        let (platform, recipient) = parse_send_target(&args).expect("target");
        assert_eq!(platform, "telegram");
        assert_eq!(recipient, "");
    }

    #[test]
    fn send_message_target_preserves_thread_suffix() {
        let args = json!({"action": "send", "target": "telegram:-1001234567890:17"});
        let (platform, recipient) = parse_send_target(&args).expect("target");
        assert_eq!(platform, "telegram");
        assert_eq!(recipient, "-1001234567890:17");
    }
}