syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
//! Provision registry tool for the agent
//!
//! Allows the agent to provision a new container registry for storing images.

use rig::completion::ToolDefinition;
use rig::tool::Tool;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::time::Duration;
use tokio::time::sleep;

use crate::agent::tools::error::{ErrorCategory, format_error_for_llm};
use crate::platform::api::types::{CreateRegistryRequest, RegistryTaskState};
use crate::platform::api::{PlatformApiClient, PlatformApiError};

/// Maximum time to wait for registry provisioning (5 minutes)
const PROVISIONING_TIMEOUT_SECS: u64 = 300;
/// Polling interval between status checks
const POLL_INTERVAL_SECS: u64 = 3;

/// Arguments for the provision registry tool
#[derive(Debug, Deserialize)]
pub struct ProvisionRegistryArgs {
    /// The project UUID
    pub project_id: String,
    /// Cluster ID to associate registry with
    pub cluster_id: String,
    /// Cluster name for display
    pub cluster_name: String,
    /// Cloud provider: "gcp" or "hetzner"
    pub provider: String,
    /// Region for the registry
    pub region: String,
    /// Name for the registry (auto-generated if not provided)
    pub registry_name: Option<String>,
    /// GCP project ID (required for GCP provider)
    pub gcp_project_id: Option<String>,
}

/// Error type for provision registry operations
#[derive(Debug, thiserror::Error)]
#[error("Provision registry error: {0}")]
pub struct ProvisionRegistryError(String);

/// Tool to provision a new container registry
///
/// Creates a container registry for storing Docker images used in deployments.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProvisionRegistryTool;

impl ProvisionRegistryTool {
    /// Create a new ProvisionRegistryTool
    pub fn new() -> Self {
        Self
    }
}

impl Tool for ProvisionRegistryTool {
    const NAME: &'static str = "provision_registry";

    type Error = ProvisionRegistryError;
    type Args = ProvisionRegistryArgs;
    type Output = String;

    async fn definition(&self, _prompt: String) -> ToolDefinition {
        ToolDefinition {
            name: Self::NAME.to_string(),
            description: r#"Provision a new container registry for storing Docker images.

A container registry is required for deployments. This tool starts provisioning
and polls until completion (may take 1-3 minutes).

**Parameters:**
- project_id: The project UUID
- cluster_id: Cluster ID to associate the registry with
- cluster_name: Cluster name for display purposes
- provider: "gcp" or "hetzner"
- region: Region for the registry (e.g., "us-central1", "nbg1")
- registry_name: Name for the registry (optional - defaults to "main")
- gcp_project_id: Required for GCP provider

**Prerequisites:**
- User must be authenticated
- Provider must be connected
- Cluster must exist (use list_deployment_capabilities to find clusters)

**Async Behavior:**
- Provisioning takes 1-3 minutes
- This tool polls until complete or failed
- Returns registry details on success

**Returns:**
- registry_id: The created registry ID
- registry_name, region, provider
- registry_url: URL for pushing images
- status: "completed" or error details"#
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "project_id": {
                        "type": "string",
                        "description": "The UUID of the project"
                    },
                    "cluster_id": {
                        "type": "string",
                        "description": "Cluster ID to associate registry with"
                    },
                    "cluster_name": {
                        "type": "string",
                        "description": "Cluster name for display"
                    },
                    "provider": {
                        "type": "string",
                        "enum": ["gcp", "hetzner"],
                        "description": "Cloud provider"
                    },
                    "region": {
                        "type": "string",
                        "description": "Region for the registry"
                    },
                    "registry_name": {
                        "type": "string",
                        "description": "Name for the registry (defaults to 'main')"
                    },
                    "gcp_project_id": {
                        "type": "string",
                        "description": "GCP project ID (required for GCP)"
                    }
                },
                "required": ["project_id", "cluster_id", "cluster_name", "provider", "region"]
            }),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        // Validate required fields
        if args.project_id.trim().is_empty() {
            return Ok(format_error_for_llm(
                "provision_registry",
                ErrorCategory::ValidationFailed,
                "project_id cannot be empty",
                Some(vec!["Use list_projects to find valid project IDs"]),
            ));
        }

        if args.cluster_id.trim().is_empty() {
            return Ok(format_error_for_llm(
                "provision_registry",
                ErrorCategory::ValidationFailed,
                "cluster_id cannot be empty",
                Some(vec![
                    "Use list_deployment_capabilities to find available clusters",
                ]),
            ));
        }

        // Validate provider
        let valid_providers = ["gcp", "hetzner"];
        if !valid_providers.contains(&args.provider.as_str()) {
            return Ok(format_error_for_llm(
                "provision_registry",
                ErrorCategory::ValidationFailed,
                &format!(
                    "Invalid provider '{}'. Must be 'gcp' or 'hetzner'",
                    args.provider
                ),
                Some(vec![
                    "Use list_deployment_capabilities to see connected providers",
                ]),
            ));
        }

        // GCP requires gcp_project_id
        if args.provider == "gcp" && args.gcp_project_id.is_none() {
            return Ok(format_error_for_llm(
                "provision_registry",
                ErrorCategory::ValidationFailed,
                "gcp_project_id is required for GCP provider",
                Some(vec![
                    "The GCP project ID can be found in the GCP Console",
                    "This is different from the Syncable project_id",
                ]),
            ));
        }

        // Create the API client
        let client = match PlatformApiClient::new() {
            Ok(c) => c,
            Err(e) => {
                return Ok(format_api_error("provision_registry", e));
            }
        };

        // Generate registry name if not provided
        let registry_name = args
            .registry_name
            .as_deref()
            .map(sanitize_registry_name)
            .unwrap_or_else(|| "main".to_string());

        // Build the request
        let request = CreateRegistryRequest {
            project_id: args.project_id.clone(),
            cluster_id: args.cluster_id.clone(),
            cluster_name: args.cluster_name.clone(),
            registry_name: registry_name.clone(),
            cloud_provider: args.provider.clone(),
            region: args.region.clone(),
            gcp_project_id: args.gcp_project_id.clone(),
        };

        // Start provisioning
        let response = match client.create_registry(&args.project_id, &request).await {
            Ok(r) => r,
            Err(e) => {
                return Ok(format_api_error("provision_registry", e));
            }
        };

        let task_id = response.task_id;

        // Poll for completion with timeout
        let start = std::time::Instant::now();
        loop {
            if start.elapsed().as_secs() > PROVISIONING_TIMEOUT_SECS {
                return Ok(format_error_for_llm(
                    "provision_registry",
                    ErrorCategory::Timeout,
                    &format!(
                        "Registry provisioning timed out after {} seconds. Task ID: {}",
                        PROVISIONING_TIMEOUT_SECS, task_id
                    ),
                    Some(vec![
                        "The provisioning may still complete in the background",
                        "Use the platform UI to check the registry status",
                        &format!("Task ID for reference: {}", task_id),
                    ]),
                ));
            }

            sleep(Duration::from_secs(POLL_INTERVAL_SECS)).await;

            let status = match client.get_registry_task_status(&task_id).await {
                Ok(s) => s,
                Err(e) => {
                    return Ok(format_error_for_llm(
                        "provision_registry",
                        ErrorCategory::NetworkError,
                        &format!("Failed to get task status: {}", e),
                        Some(vec![
                            "The provisioning may still be running",
                            &format!("Task ID: {}", task_id),
                        ]),
                    ));
                }
            };

            match status.status {
                RegistryTaskState::Completed => {
                    let registry_url = status.output.registry_url.clone();
                    let final_registry_name = status
                        .output
                        .registry_name
                        .clone()
                        .unwrap_or_else(|| registry_name.clone());

                    // The task_id serves as the registry identifier for now
                    // The actual registry ID may be returned in the output after provisioning completes
                    let result = json!({
                        "success": true,
                        "task_id": task_id,
                        "registry_name": final_registry_name,
                        "region": args.region,
                        "provider": args.provider,
                        "registry_url": registry_url,
                        "status": "completed",
                        "message": format!(
                            "Registry '{}' provisioned successfully",
                            final_registry_name
                        ),
                        "next_steps": [
                            "The registry is now ready for use",
                            "Use list_deployment_capabilities to get the full registry details",
                            "Docker images will be pushed to this registry during deployments"
                        ]
                    });

                    return serde_json::to_string_pretty(&result).map_err(|e| {
                        ProvisionRegistryError(format!("Failed to serialize: {}", e))
                    });
                }
                RegistryTaskState::Failed => {
                    let error_msg = status
                        .error
                        .map(|e| e.message)
                        .unwrap_or_else(|| "Unknown error".to_string());

                    return Ok(format_error_for_llm(
                        "provision_registry",
                        ErrorCategory::ExternalCommandFailed,
                        &format!("Registry provisioning failed: {}", error_msg),
                        Some(vec![
                            "Check provider connectivity",
                            "Verify cluster and region are valid",
                            "The provider may have resource limits",
                        ]),
                    ));
                }
                RegistryTaskState::Cancelled => {
                    return Ok(format_error_for_llm(
                        "provision_registry",
                        ErrorCategory::UserCancelled,
                        "Registry provisioning was cancelled",
                        Some(vec!["The task was cancelled externally"]),
                    ));
                }
                RegistryTaskState::Processing | RegistryTaskState::Unknown => {
                    // Continue polling
                }
            }
        }
    }
}

/// Sanitize registry name (lowercase, alphanumeric, hyphens)
fn sanitize_registry_name(name: &str) -> String {
    name.to_lowercase()
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '-' {
                c
            } else {
                '-'
            }
        })
        .collect::<String>()
        .trim_matches('-')
        .to_string()
}

/// Format a PlatformApiError for LLM consumption
fn format_api_error(tool_name: &str, error: PlatformApiError) -> String {
    match error {
        PlatformApiError::Unauthorized => format_error_for_llm(
            tool_name,
            ErrorCategory::PermissionDenied,
            "Not authenticated - please run `sync-ctl auth login` first",
            Some(vec!["Run: sync-ctl auth login"]),
        ),
        PlatformApiError::NotFound(msg) => format_error_for_llm(
            tool_name,
            ErrorCategory::ResourceUnavailable,
            &format!("Resource not found: {}", msg),
            Some(vec![
                "The project or cluster ID may be incorrect",
                "Use list_deployment_capabilities to find valid IDs",
            ]),
        ),
        PlatformApiError::PermissionDenied(msg) => format_error_for_llm(
            tool_name,
            ErrorCategory::PermissionDenied,
            &format!("Permission denied: {}", msg),
            Some(vec!["Contact the project admin for access"]),
        ),
        PlatformApiError::RateLimited => format_error_for_llm(
            tool_name,
            ErrorCategory::ResourceUnavailable,
            "Rate limit exceeded",
            Some(vec!["Wait a moment before retrying"]),
        ),
        PlatformApiError::HttpError(e) => format_error_for_llm(
            tool_name,
            ErrorCategory::NetworkError,
            &format!("Network error: {}", e),
            Some(vec!["Check network connectivity"]),
        ),
        PlatformApiError::ParseError(msg) => format_error_for_llm(
            tool_name,
            ErrorCategory::InternalError,
            &format!("Failed to parse API response: {}", msg),
            None,
        ),
        PlatformApiError::ApiError { status, message } => format_error_for_llm(
            tool_name,
            ErrorCategory::ExternalCommandFailed,
            &format!("API error ({}): {}", status, message),
            Some(vec!["Check the error message for details"]),
        ),
        PlatformApiError::ServerError { status, message } => format_error_for_llm(
            tool_name,
            ErrorCategory::ExternalCommandFailed,
            &format!("Server error ({}): {}", status, message),
            Some(vec!["Try again later"]),
        ),
        PlatformApiError::ConnectionFailed => format_error_for_llm(
            tool_name,
            ErrorCategory::NetworkError,
            "Could not connect to Syncable API",
            Some(vec!["Check your internet connection"]),
        ),
    }
}

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

    #[test]
    fn test_tool_name() {
        assert_eq!(ProvisionRegistryTool::NAME, "provision_registry");
    }

    #[test]
    fn test_tool_creation() {
        let tool = ProvisionRegistryTool::new();
        assert!(format!("{:?}", tool).contains("ProvisionRegistryTool"));
    }

    #[test]
    fn test_sanitize_registry_name() {
        assert_eq!(sanitize_registry_name("My Registry"), "my-registry");
        assert_eq!(sanitize_registry_name("test_name"), "test-name");
        assert_eq!(sanitize_registry_name("--test--"), "test");
        assert_eq!(sanitize_registry_name("MAIN"), "main");
    }
}