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
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
//! Create deployment config tool for the agent
//!
//! Allows the agent to create a new deployment configuration for a service.

use rig::completion::ToolDefinition;
use rig::tool::Tool;
use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::agent::tools::error::{ErrorCategory, format_error_for_llm};
use crate::platform::api::types::{
    CloudProvider, CloudRunnerConfigInput, CreateDeploymentConfigRequest,
    build_cloud_runner_config_v2,
};
use crate::platform::api::{PlatformApiClient, PlatformApiError};
use crate::platform::session::PlatformSession;
use std::str::FromStr;

/// Arguments for the create deployment config tool
#[derive(Debug, Deserialize)]
pub struct CreateDeploymentConfigArgs {
    /// Service name for the deployment
    pub service_name: String,
    /// Repository ID from GitHub integration
    pub repository_id: i64,
    /// Full repository name (e.g., "owner/repo")
    pub repository_full_name: String,
    /// Port the service listens on
    pub port: i32,
    /// Git branch to deploy from
    pub branch: String,
    /// Target type: "kubernetes" or "cloud_runner"
    pub target_type: String,
    /// Cloud provider: "gcp", "hetzner", or "azure"
    pub provider: String,
    /// Environment ID for deployment
    pub environment_id: String,
    /// Path to Dockerfile relative to repo root
    pub dockerfile_path: Option<String>,
    /// Build context path relative to repo root
    pub build_context: Option<String>,
    /// Cluster ID (required for kubernetes target)
    pub cluster_id: Option<String>,
    /// Registry ID (optional - will provision new if not provided)
    pub registry_id: Option<String>,
    /// Enable auto-deploy on push (defaults to true)
    #[serde(default = "default_auto_deploy")]
    pub auto_deploy_enabled: bool,
    /// CPU allocation (for GCP Cloud Run or Azure Container Apps)
    pub cpu: Option<String>,
    /// Memory allocation (for GCP Cloud Run or Azure Container Apps)
    pub memory: Option<String>,
    /// Minimum instances/replicas
    pub min_instances: Option<i32>,
    /// Maximum instances/replicas
    pub max_instances: Option<i32>,
    /// Whether the service should be publicly accessible
    pub is_public: Option<bool>,
}

fn default_auto_deploy() -> bool {
    true
}

/// Error type for create deployment config operations
#[derive(Debug, thiserror::Error)]
#[error("Create deployment config error: {0}")]
pub struct CreateDeploymentConfigError(String);

/// Tool to create a new deployment configuration
///
/// Creates a deployment config that defines how to build and deploy a service.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CreateDeploymentConfigTool;

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

impl Tool for CreateDeploymentConfigTool {
    const NAME: &'static str = "create_deployment_config";

    type Error = CreateDeploymentConfigError;
    type Args = CreateDeploymentConfigArgs;
    type Output = String;

    async fn definition(&self, _prompt: String) -> ToolDefinition {
        ToolDefinition {
            name: Self::NAME.to_string(),
            description: r#"Create a new deployment configuration for a service.

A deployment config defines how to build and deploy a service, including:
- Source repository and branch
- Dockerfile location and build context
- Target (Cloud Runner or Kubernetes)
- Port configuration
- CPU/memory allocation (for Cloud Runner deployments)
- Auto-deploy settings

**Required Parameters:**
- service_name: Name for the service (lowercase, hyphens allowed)
- repository_id: GitHub repository ID (from platform GitHub integration)
- repository_full_name: Full repo name like "owner/repo"
- port: Port the service listens on
- branch: Git branch to deploy from (e.g., "main")
- target_type: "kubernetes" or "cloud_runner"
- provider: "gcp", "hetzner", or "azure"
- environment_id: Environment to deploy to

**Optional Parameters:**
- dockerfile_path: Path to Dockerfile (default: "Dockerfile")
- build_context: Build context path (default: ".")
- cluster_id: Required for kubernetes target
- registry_id: Container registry ID (provisions new if not provided)
- auto_deploy_enabled: Enable auto-deploy on push (default: true)
- cpu: CPU allocation (e.g., "1" for GCP Cloud Run, "0.5" for Azure ACA)
- memory: Memory allocation (e.g., "512Mi" for GCP, "1.0Gi" for Azure)
- min_instances: Minimum instances/replicas (default: 0)
- max_instances: Maximum instances/replicas (default: 10)
- is_public: Whether the service should be publicly accessible (default: true)

**Prerequisites:**
- User must be authenticated
- GitHub repository must be connected to the project
- Provider must be connected (check with check_provider_connection)
- For kubernetes: cluster must exist (check with list_deployment_capabilities)

**Returns:**
- config_id: The created deployment config ID
- service_name, branch, target_type, provider
- next_steps: How to trigger a deployment"#
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "service_name": {
                        "type": "string",
                        "description": "Name for the service (lowercase, hyphens allowed)"
                    },
                    "repository_id": {
                        "type": "integer",
                        "description": "GitHub repository ID from platform integration"
                    },
                    "repository_full_name": {
                        "type": "string",
                        "description": "Full repository name (e.g., 'owner/repo')"
                    },
                    "port": {
                        "type": "integer",
                        "description": "Port the service listens on"
                    },
                    "branch": {
                        "type": "string",
                        "description": "Git branch to deploy from"
                    },
                    "target_type": {
                        "type": "string",
                        "enum": ["kubernetes", "cloud_runner"],
                        "description": "Deployment target type"
                    },
                    "provider": {
                        "type": "string",
                        "enum": ["gcp", "hetzner", "azure"],
                        "description": "Cloud provider"
                    },
                    "environment_id": {
                        "type": "string",
                        "description": "Environment ID for deployment"
                    },
                    "dockerfile_path": {
                        "type": "string",
                        "description": "Path to Dockerfile relative to repo root"
                    },
                    "build_context": {
                        "type": "string",
                        "description": "Build context path relative to repo root"
                    },
                    "cluster_id": {
                        "type": "string",
                        "description": "Cluster ID (required for kubernetes target)"
                    },
                    "registry_id": {
                        "type": "string",
                        "description": "Registry ID (optional - provisions new if not provided)"
                    },
                    "auto_deploy_enabled": {
                        "type": "boolean",
                        "description": "Enable auto-deploy on push (default: true)"
                    },
                    "cpu": {
                        "type": "string",
                        "description": "CPU allocation (e.g., '1' for GCP Cloud Run, '0.5' for Azure ACA)"
                    },
                    "memory": {
                        "type": "string",
                        "description": "Memory allocation (e.g., '512Mi' for GCP, '1.0Gi' for Azure)"
                    },
                    "min_instances": {
                        "type": "integer",
                        "description": "Minimum instances/replicas (default: 0)"
                    },
                    "max_instances": {
                        "type": "integer",
                        "description": "Maximum instances/replicas (default: 10)"
                    },
                    "is_public": {
                        "type": "boolean",
                        "description": "Whether the service should be publicly accessible (default: true)"
                    }
                },
                "required": [
                    "service_name", "repository_id", "repository_full_name",
                    "port", "branch", "target_type", "provider", "environment_id"
                ]
            }),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        // Load project_id from session (authoritative source — prevents stale IDs from LLM context)
        let session = match PlatformSession::load() {
            Ok(s) => s,
            Err(_) => {
                return Ok(format_error_for_llm(
                    "create_deployment_config",
                    ErrorCategory::InternalError,
                    "Failed to load platform session",
                    Some(vec!["Try authenticating with `sync-ctl auth login`"]),
                ));
            }
        };

        if !session.is_project_selected() {
            return Ok(format_error_for_llm(
                "create_deployment_config",
                ErrorCategory::ValidationFailed,
                "No project selected",
                Some(vec!["Use select_project to choose a project first"]),
            ));
        }

        let project_id = session.project_id.clone().unwrap_or_default();

        if args.service_name.trim().is_empty() {
            return Ok(format_error_for_llm(
                "create_deployment_config",
                ErrorCategory::ValidationFailed,
                "service_name cannot be empty",
                Some(vec![
                    "Use analyze_project to discover suggested service names",
                    "Service name should be lowercase with hyphens",
                ]),
            ));
        }

        // Validate target_type
        let valid_targets = ["kubernetes", "cloud_runner"];
        if !valid_targets.contains(&args.target_type.as_str()) {
            return Ok(format_error_for_llm(
                "create_deployment_config",
                ErrorCategory::ValidationFailed,
                &format!(
                    "Invalid target_type '{}'. Must be 'kubernetes' or 'cloud_runner'",
                    args.target_type
                ),
                Some(vec![
                    "Use 'cloud_runner' for GCP Cloud Run, Hetzner containers, or Azure Container Apps",
                    "Use 'kubernetes' for deploying to a K8s cluster",
                ]),
            ));
        }

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

        // Kubernetes target requires cluster_id
        if args.target_type == "kubernetes" && args.cluster_id.is_none() {
            return Ok(format_error_for_llm(
                "create_deployment_config",
                ErrorCategory::ValidationFailed,
                "cluster_id is required for kubernetes target",
                Some(vec![
                    "Use list_deployment_capabilities to find available clusters",
                    "Or use 'cloud_runner' target which doesn't require a cluster",
                ]),
            ));
        }

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

        // Build cloud runner config if deploying to cloud_runner
        let cloud_runner_config = if args.target_type == "cloud_runner" {
            let provider_enum = CloudProvider::from_str(&args.provider).ok();

            // Fetch provider_account_id from credentials when provider is azure or gcp
            let mut gcp_project_id = None;
            let mut subscription_id = None;
            if let Some(ref provider) = provider_enum {
                if matches!(provider, CloudProvider::Gcp | CloudProvider::Azure) {
                    if let Ok(credential) = client
                        .check_provider_connection(provider, &project_id)
                        .await
                    {
                        if let Some(cred) = credential {
                            match provider {
                                CloudProvider::Gcp => gcp_project_id = cred.provider_account_id,
                                CloudProvider::Azure => subscription_id = cred.provider_account_id,
                                _ => {}
                            }
                        }
                    }
                }
            }

            let config_input = CloudRunnerConfigInput {
                provider: provider_enum,
                region: None, // Region is set at environment level or by deploy_service
                gcp_project_id,
                cpu: args.cpu.clone(),
                memory: args.memory.clone(),
                min_instances: args.min_instances,
                max_instances: args.max_instances,
                is_public: args.is_public,
                subscription_id,
                ..Default::default()
            };
            Some(build_cloud_runner_config_v2(&config_input))
        } else {
            None
        };

        // Build the request
        // Note: Send both field name variants (dockerfile/dockerfilePath, context/buildContext)
        // for backend compatibility - different endpoints may expect different field names
        let request = CreateDeploymentConfigRequest {
            project_id,
            service_name: args.service_name.clone(),
            repository_id: args.repository_id,
            repository_full_name: args.repository_full_name.clone(),
            dockerfile_path: args.dockerfile_path.clone(),
            dockerfile: args.dockerfile_path.clone(), // Alias for backend compatibility
            build_context: args.build_context.clone(),
            context: args.build_context.clone(), // Alias for backend compatibility
            port: args.port,
            branch: args.branch.clone(),
            target_type: args.target_type.clone(),
            cloud_provider: args.provider.clone(),
            environment_id: args.environment_id.clone(),
            cluster_id: args.cluster_id.clone(),
            registry_id: args.registry_id.clone(),
            auto_deploy_enabled: args.auto_deploy_enabled,
            is_public: args.is_public,
            cloud_runner_config,
            secrets: None,
        };

        // Create the deployment config
        match client.create_deployment_config(&request).await {
            Ok(config) => {
                let result = json!({
                    "success": true,
                    "config_id": config.id,
                    "service_name": config.service_name,
                    "branch": config.branch,
                    "target_type": args.target_type,
                    "provider": args.provider,
                    "auto_deploy_enabled": args.auto_deploy_enabled,
                    "message": format!(
                        "Deployment config created for service '{}' on {} ({})",
                        config.service_name, args.target_type, args.provider
                    ),
                    "next_steps": [
                        format!("Use trigger_deployment with config_id '{}' to deploy", config.id),
                        "Use get_deployment_status to monitor deployment progress",
                        if args.auto_deploy_enabled {
                            "Auto-deploy is enabled - pushing to the branch will trigger deployments"
                        } else {
                            "Auto-deploy is disabled - deployments must be triggered manually"
                        }
                    ]
                });

                serde_json::to_string_pretty(&result)
                    .map_err(|e| CreateDeploymentConfigError(format!("Failed to serialize: {}", e)))
            }
            Err(e) => Ok(format_api_error("create_deployment_config", e)),
        }
    }
}

/// 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![
                "The user needs to authenticate with the Syncable platform",
                "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 ID may be incorrect",
                "The repository may not be connected to the project",
                "Use list_projects to find valid project IDs",
            ]),
        ),
        PlatformApiError::PermissionDenied(msg) => format_error_for_llm(
            tool_name,
            ErrorCategory::PermissionDenied,
            &format!("Permission denied: {}", msg),
            Some(vec![
                "The user does not have permission to create deployment configs",
                "Contact the project admin for access",
            ]),
        ),
        PlatformApiError::RateLimited => format_error_for_llm(
            tool_name,
            ErrorCategory::ResourceUnavailable,
            "Rate limit exceeded - please try again later",
            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",
                "The Syncable API may be temporarily unavailable",
            ]),
        ),
        PlatformApiError::ParseError(msg) => format_error_for_llm(
            tool_name,
            ErrorCategory::InternalError,
            &format!("Failed to parse API response: {}", msg),
            Some(vec!["This may be a temporary API issue"]),
        ),
        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",
                "The repository may not be properly connected",
            ]),
        ),
        PlatformApiError::ServerError { status, message } => format_error_for_llm(
            tool_name,
            ErrorCategory::ExternalCommandFailed,
            &format!("Server error ({}): {}", status, message),
            Some(vec![
                "The Syncable API is experiencing issues",
                "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",
                "The Syncable API may be temporarily unavailable",
            ]),
        ),
    }
}

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

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

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

    #[test]
    fn test_default_auto_deploy() {
        assert!(default_auto_deploy());
    }
}