unigateway 1.2.1

Lightweight, local-first LLM gateway for developers. A stable, single-binary unified entry point for all your AI tools and models.
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
use anyhow::{Context, Result, bail};
use std::path::Path;

use crate::config::{GatewayState, ProviderModelOptions};

pub struct GuideParams<'a> {
    pub service_id: Option<&'a str>,
    pub service_name: Option<&'a str>,
    pub provider_name: &'a str,
    pub provider_type: &'a str,
    pub endpoint_id: &'a str,
    pub default_model: Option<&'a str>,
    pub fast_model: Option<&'a str>,
    pub strong_model: Option<&'a str>,
    pub base_url: Option<&'a str>,
    pub api_key: &'a str,
    pub model_mapping: Option<&'a str>,
    pub backup_provider_name: Option<&'a str>,
    pub backup_provider_type: Option<&'a str>,
    pub backup_endpoint_id: Option<&'a str>,
    pub backup_default_model: Option<&'a str>,
    pub backup_base_url: Option<&'a str>,
    pub backup_api_key: Option<&'a str>,
    pub backup_model_mapping: Option<&'a str>,
}

pub struct GuideModeOutput {
    pub id: String,
    pub key: String,
}

pub struct GuideResult {
    pub modes: Vec<GuideModeOutput>,
}

struct GuideModePlan {
    id: String,
    name: String,
    routing_strategy: &'static str,
    bindings: Vec<(i64, i64)>,
}

pub(crate) fn planned_modes(
    service_id: Option<&str>,
    service_name: Option<&str>,
    fast_model: Option<&str>,
    strong_model: Option<&str>,
) -> Vec<(String, String)> {
    if let Some(service_id) = service_id {
        return vec![(
            service_id.to_string(),
            service_name.unwrap_or(service_id).to_string(),
        )];
    }

    if fast_model.is_some() || strong_model.is_some() {
        let mut modes = Vec::new();
        if fast_model.is_some() {
            modes.push(("fast".to_string(), "Fast".to_string()));
        }
        if strong_model.is_some() {
            modes.push(("strong".to_string(), "Strong".to_string()));
        }
        return modes;
    }

    vec![("default".to_string(), "Default".to_string())]
}

fn guide_mode_plans(
    service_id: Option<&str>,
    service_name: Option<&str>,
    fast_model: Option<&str>,
    strong_model: Option<&str>,
    primary_provider_id: i64,
    secondary_provider_id: Option<i64>,
) -> Vec<GuideModePlan> {
    let mut plans = Vec::new();
    let modes = planned_modes(service_id, service_name, fast_model, strong_model);

    for (id, name) in modes {
        let mut bindings = vec![(primary_provider_id, 0)];
        let routing_strategy = if let Some(secondary_provider_id) = secondary_provider_id {
            bindings.push((secondary_provider_id, 1));
            "fallback"
        } else {
            "round_robin"
        };

        plans.push(GuideModePlan {
            id,
            name,
            routing_strategy,
            bindings,
        });
    }

    plans
}

pub async fn create_service(config_path: &str, service_id: &str, name: &str) -> Result<()> {
    let state = GatewayState::load(Path::new(config_path)).await?;
    state.create_service(service_id, name).await;
    state.persist_if_dirty().await
}

pub async fn create_provider(
    config_path: &str,
    name: &str,
    provider_type: &str,
    endpoint_id: &str,
    base_url: Option<&str>,
    api_key: &str,
    model_mapping: Option<&str>,
) -> Result<i64> {
    let state = GatewayState::load(Path::new(config_path)).await?;
    let id = state
        .create_provider(
            name,
            provider_type,
            endpoint_id,
            base_url,
            api_key,
            model_mapping,
        )
        .await;
    state.persist_if_dirty().await?;
    Ok(id)
}

pub async fn bind_provider(config_path: &str, service_id: &str, provider_id: i64) -> Result<()> {
    let state = GatewayState::load(Path::new(config_path)).await?;
    state
        .bind_provider_to_service(service_id, provider_id)
        .await
        .with_context(|| format!("bind provider_id {} to service {}", provider_id, service_id))?;
    state.persist_if_dirty().await
}

pub async fn create_api_key(
    config_path: &str,
    key: &str,
    service_id: &str,
    quota_limit: Option<i64>,
    qps_limit: Option<f64>,
    concurrency_limit: Option<i64>,
) -> Result<()> {
    let state = GatewayState::load(Path::new(config_path)).await?;
    state
        .create_api_key(key, service_id, quota_limit, qps_limit, concurrency_limit)
        .await;
    state.persist_if_dirty().await?;
    println!("✅ Created API key '{}' for service '{}'", key, service_id);
    Ok(())
}

pub async fn list_services(config_path: &str, json: bool) -> Result<()> {
    let state = GatewayState::load(Path::new(config_path)).await?;
    let services = state.list_services().await;

    if json {
        println!("{}", serde_json::to_string_pretty(&services)?);
    } else {
        if services.is_empty() {
            println!("No services found.");
            return Ok(());
        }

        println!("{:<20} {:<20}", "ID", "NAME");
        println!("{:-<20} {:-<20}", "", "");

        for service in services {
            println!("{:<20} {:<20}", service.0, service.1);
        }
    }

    Ok(())
}

pub async fn list_providers(config_path: &str, json: bool) -> Result<()> {
    let state = GatewayState::load(Path::new(config_path)).await?;
    let providers = state.list_providers().await;

    if json {
        println!("{}", serde_json::to_string_pretty(&providers)?);
    } else {
        if providers.is_empty() {
            println!("No providers found.");
            return Ok(());
        }

        println!(
            "{:<4} {:<20} {:<15} {:<30} {:<20}",
            "ID", "NAME", "TYPE", "BASE URL", "ENDPOINT ID"
        );
        println!("{:-<4} {:-<20} {:-<15} {:-<30} {:-<20}", "", "", "", "", "");

        for (id, name, provider_type, base_url, endpoint_id) in providers {
            let base_url = base_url.as_deref().unwrap_or("-");
            let endpoint_id = endpoint_id.as_deref().unwrap_or("-");

            println!(
                "{:<4} {:<20} {:<15} {:<30} {:<20}",
                id,
                name,
                provider_type,
                truncate(base_url, 30),
                truncate(endpoint_id, 20)
            );
        }
    }

    Ok(())
}

fn truncate(s: &str, max_len: usize) -> String {
    if s.chars().count() > max_len {
        let s: String = s.chars().take(max_len.saturating_sub(3)).collect();
        format!("{}...", s)
    } else {
        s.to_string()
    }
}

use console::style;
use dialoguer::{Input, Select, theme::ColorfulTheme};

pub async fn interactive_create_service(config_path: &str) -> Result<()> {
    let theme = ColorfulTheme::default();
    let id: String = Input::with_theme(&theme)
        .with_prompt("Service ID (e.g. 'default')")
        .default("default".to_string())
        .interact_text()?;

    let name: String = Input::with_theme(&theme)
        .with_prompt("Service Name")
        .default("Default Service".to_string())
        .interact_text()?;

    create_service(config_path, &id, &name).await?;
    println!(
        "{} Service '{}' created.",
        style("✅").green(),
        style(id).bold()
    );
    Ok(())
}

pub async fn interactive_create_provider(config_path: &str) -> Result<()> {
    let theme = ColorfulTheme::default();

    let name: String = Input::with_theme(&theme)
        .with_prompt("Provider Name")
        .interact_text()?;

    let provider_types = vec!["openai", "anthropic"];
    let selection = Select::with_theme(&theme)
        .with_prompt("Provider Type")
        .items(&provider_types)
        .default(0)
        .interact()?;
    let provider_type = provider_types[selection];

    let endpoint_id: String = Input::with_theme(&theme)
        .with_prompt("Endpoint ID (e.g. 'openai:global')")
        .interact_text()?;

    let base_url: String = Input::with_theme(&theme)
        .with_prompt("Base URL (optional)")
        .allow_empty(true)
        .interact_text()?;

    let api_key: String = Input::with_theme(&theme)
        .with_prompt("API Key")
        .interact_text()?;

    let base_url = if base_url.is_empty() {
        None
    } else {
        Some(base_url.as_str())
    };

    let id = create_provider(
        config_path,
        &name,
        provider_type,
        &endpoint_id,
        base_url,
        &api_key,
        None,
    )
    .await?;

    println!(
        "{} Provider '{}' created with ID: {}",
        style("✅").green(),
        style(name).bold(),
        id
    );
    Ok(())
}

pub async fn interactive_create_api_key(config_path: &str) -> Result<()> {
    let theme = ColorfulTheme::default();

    let key: String = Input::with_theme(&theme)
        .with_prompt("API Key Value (leave empty to generate)")
        .allow_empty(true)
        .interact_text()?;

    let key = if key.is_empty() {
        format!("ugk_{}", hex::encode(rand::random::<[u8; 16]>()))
    } else {
        key
    };

    let service_id: String = Input::with_theme(&theme)
        .with_prompt("Service ID to bind")
        .default("default".to_string())
        .interact_text()?;

    create_api_key(config_path, &key, &service_id, None, None, None).await?;

    // Ask for AI Agent integration preference
    let agent_options = vec![
        "OpenClaw",
        "Claude Code",
        "Cursor",
        "OpenCode",
        "Droid",
        "Cline",
        "OpenHands",
        "Zed",
        "Codex",
        "Trae",
        "Other / None",
    ];
    let agent_selection = Select::with_theme(&theme)
        .with_prompt("Which AI Agent will use this key?")
        .items(&agent_options)
        .default(0)
        .interact()?;

    println!("\n{} API Key created successfully!", style("✅").green());
    println!("   Key: {}", style(&key).cyan().bold());
    println!("   Service: {}", style(&service_id).dim());

    let bind_addr = crate::types::AppConfig::from_env().bind;
    let _base_url = format!(
        "http://{}/v1",
        crate::cli::modes::user_bind_address(&bind_addr)
    );

    match agent_selection {
        0 => {
            // OpenClaw
            println!(
                "\n🎉 {}",
                style("Ready to use with OpenClaw!").green().bold()
            );
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::OpenClaw,
                )
            );
        }
        1 => {
            // Claude Code
            println!(
                "\n🎉 {}",
                style("Ready to use with Claude Code!").green().bold()
            );
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::ClaudeCode,
                )
            );
        }
        2 => {
            // Cursor
            println!("\n🎉 {}", style("Ready to use with Cursor!").green().bold());
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::Cursor,
                )
            );
        }
        3 => {
            // OpenCode
            println!(
                "\n🎉 {}",
                style("Ready to use with OpenCode!").green().bold()
            );
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::OpenCode,
                )
            );
        }
        4 => {
            // Droid
            println!("\n🎉 {}", style("Ready to use with Droid!").green().bold());
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::Droid,
                )
            );
        }
        5 => {
            // Cline
            println!("\n🎉 {}", style("Ready to use with Cline!").green().bold());
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::Cline,
                )
            );
        }
        6 => {
            // OpenHands
            println!(
                "\n🎉 {}",
                style("Ready to use with OpenHands!").green().bold()
            );
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::OpenHands,
                )
            );
        }
        7 => {
            // Zed
            println!("\n🎉 {}", style("Ready to use with Zed!").green().bold());
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::Zed,
                )
            );
        }
        8 => {
            // Codex
            println!("\n🎉 {}", style("Ready to use with Codex!").green().bold());
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::Codex,
                )
            );
        }
        9 => {
            // Trae
            println!("\n🎉 {}", style("Ready to use with Trae!").green().bold());
            println!(
                "{}",
                crate::cli::render::integrations::render_integration_output_for_tool(
                    None,
                    Some(&key),
                    Some(&bind_addr),
                    crate::cli::render::integrations::IntegrationTool::Trae,
                )
            );
        }
        _ => {
            println!("\n💡 Use `ug integrations` to see more configuration examples.");
        }
    }

    println!("\n🚀 Ready! Use `ug help` for more commands.");
    Ok(())
}

pub async fn guide(config_path: &str, params: GuideParams<'_>) -> Result<GuideResult> {
    let state = GatewayState::load(Path::new(config_path)).await?;
    let primary_provider_id = state
        .create_provider_with_models(
            params.provider_name,
            params.provider_type,
            params.endpoint_id,
            params.base_url,
            params.api_key,
            ProviderModelOptions {
                default_model: params.default_model,
                model_mapping: params.model_mapping,
            },
        )
        .await;

    let secondary_provider_id = match (
        params.backup_provider_name,
        params.backup_provider_type,
        params.backup_endpoint_id,
        params.backup_api_key,
    ) {
        (Some(name), Some(provider_type), Some(endpoint_id), Some(api_key)) => Some(
            state
                .create_provider_with_models(
                    name,
                    provider_type,
                    endpoint_id,
                    params.backup_base_url,
                    api_key,
                    ProviderModelOptions {
                        default_model: params.backup_default_model,
                        model_mapping: params.backup_model_mapping,
                    },
                )
                .await,
        ),
        (None, None, None, None) => None,
        _ => bail!("backup provider requires name, provider_type, endpoint_id, and api_key"),
    };

    let planned = guide_mode_plans(
        params.service_id,
        params.service_name,
        params.fast_model,
        params.strong_model,
        primary_provider_id,
        secondary_provider_id,
    );
    let default_mode = planned.first().map(|mode| mode.id.clone());
    let mut modes = Vec::new();
    for plan in planned {
        let key = format!("ugk_{}", hex::encode(rand::random::<[u8; 16]>()));
        state.create_service(&plan.id, &plan.name).await;
        state
            .set_service_routing_strategy(&plan.id, plan.routing_strategy)
            .await?;

        // Determine which model to use for this mode
        let model_options = match plan.id.as_str() {
            "fast" => ProviderModelOptions {
                default_model: params.fast_model,
                model_mapping: None,
            },
            "strong" => ProviderModelOptions {
                default_model: params.strong_model,
                model_mapping: None,
            },
            _ => ProviderModelOptions {
                default_model: params.default_model,
                model_mapping: params.model_mapping,
            },
        };

        // Update provider with model options for this specific mode's binding if necessary
        // Actually, the current schema stores default_model on the provider itself.
        // If we want different modes to use DIFFERENT models on the SAME provider,
        // we might need to adjust binder or just set it globally for now if only one provider is used.
        // But the user said "manually set which is fast, which is strong".
        // For simplicity, we create the provider with its "default_model" first.
        // If multi-mode, we might need provider-per-mode or model-mapped-to-mode.
        // The previous implementation used model mapping like "fast=...".

        if model_options.default_model.is_some() {
            state
                .set_provider_model_options(primary_provider_id, model_options)
                .await?;
        }

        for (provider_id, priority) in &plan.bindings {
            state
                .bind_provider_to_service_with_priority(&plan.id, *provider_id, *priority)
                .await?;
        }
        state.create_api_key(&key, &plan.id, None, None, None).await;
        modes.push(GuideModeOutput { id: plan.id, key });
    }

    if let Some(default_mode) = default_mode {
        state.set_default_mode(&default_mode).await?;
    }

    state.persist_if_dirty().await?;
    Ok(GuideResult { modes })
}