elisym-client 0.4.8

CLI agent runner for the elisym protocol
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
mod agent;
mod args;
mod banner;
pub(crate) mod config;
pub(crate) mod crypto;
pub(crate) mod error;
pub(crate) mod global_config;
pub(crate) mod llm;
pub(crate) mod protocol;


use std::io::Write as _;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use clap::Parser;
use console::style;
use dialoguer::{Confirm, Input, Password, Select};
use nostr_sdk::{Keys, ToBech32};
use solana_sdk::signature::Keypair;
use solana_sdk::signer::Signer;
use zeroize::Zeroizing;

use self::args::{Cli, Commands};
use self::config::{AgentConfig, LlmSection, PaymentSection};
use self::error::{CliError, Result};

use crate::util::{format_sol, format_sol_compact, sol_to_lamports};

/// Initialize tracing subscriber with a reloadable filter.
/// Returns a handle that can silence tracing when TUI is active.
fn init_tracing() {
    use tracing_subscriber::EnvFilter;
    use tracing_subscriber::prelude::*;
    use tracing_subscriber::reload;

    let base_filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new("info"))
        .add_directive("nostr_relay_pool=off".parse().unwrap());

    let (filter_layer, handle) = reload::Layer::new(base_filter);

    let fmt_layer = tracing_subscriber::fmt::layer();

    let subscriber = tracing_subscriber::registry()
        .with(filter_layer)
        .with(fmt_layer);

    let _ = tracing::subscriber::set_global_default(subscriber);

    // Store the handle for later use
    TRACING_RELOAD_HANDLE.get_or_init(|| std::sync::Mutex::new(handle));
}

static TRACING_RELOAD_HANDLE: std::sync::OnceLock<
    std::sync::Mutex<tracing_subscriber::reload::Handle<tracing_subscriber::EnvFilter, tracing_subscriber::Registry>>
> = std::sync::OnceLock::new();

/// Silence all tracing output (call before TUI starts).
fn silence_tracing() {
    if let Some(handle) = TRACING_RELOAD_HANDLE.get() {
        if let Ok(handle) = handle.lock() {
            let _ = handle.reload(tracing_subscriber::EnvFilter::new("off"));
        }
    }
}

/// Run an async operation with an animated spinner.
async fn with_spinner<F, T>(message: &str, fut: F) -> T
where
    F: std::future::Future<Output = T>,
{
    let stop = Arc::new(AtomicBool::new(false));
    let stop_clone = Arc::clone(&stop);
    let msg = message.to_string();

    let handle = tokio::spawn(async move {
        let frames = ["", "", "", "", "", "", "", "", "", ""];
        let mut i = 0;
        loop {
            if stop_clone.load(Ordering::Relaxed) {
                print!("\r\x1b[2K  {} {}\n", style("").green(), style("Connected.").dim());
                let _ = std::io::stdout().flush();
                break;
            }
            print!("\r\x1b[2K  {} {}", style(frames[i % frames.len()]).cyan(), style(&msg).dim());
            let _ = std::io::stdout().flush();
            i += 1;
            tokio::time::sleep(std::time::Duration::from_millis(80)).await;
        }
    });

    let result = fut.await;
    stop.store(true, Ordering::Relaxed);
    let _ = handle.await;
    result
}

// ── model fetching ───────────────────────────────────────────────────

/// Fetch available models from the provider API, with hardcoded fallback.
fn fetch_models(provider: &str, api_key: &str) -> Vec<String> {
    println!("  {} Fetching models...", style("~").dim());

    let result = tokio::task::block_in_place(|| {
        tokio::runtime::Handle::current().block_on(fetch_models_async(provider, api_key))
    });

    match result {
        Ok(models) if !models.is_empty() => models,
        Ok(_) => {
            println!("  {} No models returned, using defaults.", style("!").yellow());
            fallback_models(provider)
        }
        Err(e) => {
            println!("  {} Could not fetch models: {}", style("!").yellow(), e);
            fallback_models(provider)
        }
    }
}

fn fallback_models(provider: &str) -> Vec<String> {
    match provider {
        "anthropic" => vec![
            "claude-sonnet-4-20250514".into(),
            "claude-haiku-4-5-20251001".into(),
            "claude-opus-4-20250514".into(),
        ],
        _ => vec!["gpt-4o".into(), "gpt-4o-mini".into(), "gpt-4-turbo".into()],
    }
}

async fn fetch_models_async(provider: &str, api_key: &str) -> std::result::Result<Vec<String>, reqwest::Error> {
    let client = reqwest::Client::new();

    match provider {
        "anthropic" => {
            let resp: serde_json::Value = client
                .get("https://api.anthropic.com/v1/models")
                .header("anthropic-version", "2023-06-01")
                .header("x-api-key", api_key)
                .query(&[("limit", "1000")])
                .send()
                .await?
                .json()
                .await?;

            let mut models: Vec<String> = resp["data"]
                .as_array()
                .map(|arr| {
                    arr.iter()
                        .filter_map(|m| Some(m["id"].as_str()?.to_string()))
                        .collect()
                })
                .unwrap_or_default();
            models.sort();
            Ok(models)
        }
        "openai" => {
            let resp: serde_json::Value = client
                .get("https://api.openai.com/v1/models")
                .header("Authorization", format!("Bearer {}", api_key))
                .send()
                .await?
                .json()
                .await?;

            let mut models: Vec<String> = resp["data"]
                .as_array()
                .map(|arr| {
                    arr.iter()
                        .filter_map(|m| {
                            let id = m["id"].as_str()?;
                            // Only keep chat-compatible models
                            let is_chat = (id.starts_with("gpt-")
                                || id.starts_with("o1")
                                || id.starts_with("o3")
                                || id.starts_with("o4")
                                || id.starts_with("chatgpt-"))
                                && !id.contains("instruct")
                                && !id.contains("realtime")
                                && !id.contains("audio")
                                && !id.contains("tts")
                                && !id.contains("whisper")
                                && !id.contains("davinci")
                                && !id.contains("babbage");
                            if is_chat {
                                Some(id.to_string())
                            } else {
                                None
                            }
                        })
                        .collect()
                })
                .unwrap_or_default();
            models.sort();
            models.dedup();
            Ok(models)
        }
        _ => Ok(vec![]),
    }
}

pub async fn run() -> Result<()> {
    init_tracing();

    let cli = Cli::parse();

    match cli.command {
        Some(Commands::Init) => { cmd_init()?; },
        Some(Commands::Start { name, headless, price }) => cmd_start(name, headless, price).await?,
        Some(Commands::List) => cmd_list()?,
        Some(Commands::Status { name }) => cmd_status(&name)?,
        Some(Commands::Delete { name }) => cmd_delete(&name)?,
        Some(Commands::Config { name }) => cmd_config(&name)?,
        Some(Commands::Wallet { name }) => cmd_wallet(&name)?,
        Some(Commands::Send { name, address, amount }) => cmd_send(&name, &address, &amount)?,
        None => {
            // No subcommand — show banner and help
            print!("{}", style(banner::BANNER).cyan());
            println!("  Run {} to get started.\n", style("elisym init").cyan());
            Cli::parse_from(["elisym", "--help"]);
        }
    }

    Ok(())
}

// ── init ──────────────────────────────────────────────────────────────

fn cmd_init() -> Result<String> {
    print!("{}", style(banner::BANNER).cyan());
    println!("  {}\n", style("Create a new agent").bold());
    println!("  {}", style("Your agent is an AI that lives on the elisym network.").dim());
    println!("  {}", style("It can earn SOL by completing tasks for other agents (provider),").dim());
    println!("  {}", style("or pay other agents to do work for you (customer).").dim());
    println!();
    println!("  {}", style("Let's set it up step by step. Type \"back\" at any prompt to go back.").dim());
    println!();

    // Wizard state
    let mut name = String::new();
    let mut description = String::new();
    let mut network = String::new();
    let mut rpc_url_opt: Option<String> = None;
    let mut provider = String::new();
    let mut api_key = String::new();
    let mut model = String::new();
    let mut max_tokens: u32 = 4096;
    let mut encryption_password: Option<Zeroizing<String>> = None;

    let mut step: usize = 0;
    loop {
        match step {
            // Step 0: Agent name (no back — first step)
            0 => {
                println!("  {}", style("A unique name for your agent (used in commands like `start my-agent`)").dim());
                let input: String = Input::new()
                    .with_prompt("Agent name")
                    .interact_text()?;

                if input.is_empty() {
                    println!("  {} Name cannot be empty.", style("!").yellow());
                    continue;
                }

                if config::config_path(&input)?.exists() {
                    println!(
                        "  {} Agent '{}' already exists. Choose a different name.",
                        style("!").yellow(),
                        style(&input).cyan()
                    );
                    continue;
                }

                name = input;
                step += 1;
            }

            // Step 1: Description
            1 => {
                println!("  {}", style("What does your agent do? This is shown to other agents on the network.").dim());
                let input: String = Input::new()
                    .with_prompt("Description (or \"back\")")
                    .default("An elisym AI agent".into())
                    .interact_text()?;

                if input.eq_ignore_ascii_case("back") {
                    step -= 1;
                    continue;
                }

                description = input;
                step += 1;
            }

            // Step 2: Solana network
            2 => {
                println!("  {}", style("Solana is used for payments between agents.").dim());
                println!("  {}", style("Use devnet for testing (free SOL via faucet.solana.com).").dim());
                let options = &[
                    "devnet (default, testing)",
                    "mainnet (coming soon)",
                    "testnet (coming soon)",
                    "\u{2190} Back",
                ];
                let idx = Select::new()
                    .with_prompt("Solana network")
                    .items(options)
                    .default(0)
                    .interact()?;

                if idx == 3 {
                    step -= 1;
                    continue;
                }

                if idx == 1 || idx == 2 {
                    println!("  {}", style("⚠ This network is not available yet.").yellow());
                    continue;
                }

                network = "devnet".to_string();
                step += 1;
            }

            // Step 3: RPC URL
            3 => {
                println!("  {}", style("Solana RPC endpoint. The default works fine — change only if you have a custom node.").dim());
                let default_rpc = match network.as_str() {
                    "devnet" => "https://api.devnet.solana.com",
                    "testnet" => "https://api.testnet.solana.com",
                    _ => "https://api.mainnet-beta.solana.com",
                };
                let input: String = Input::new()
                    .with_prompt("RPC URL (or \"back\")")
                    .default(default_rpc.into())
                    .interact_text()?;

                if input.eq_ignore_ascii_case("back") {
                    step -= 1;
                    continue;
                }

                rpc_url_opt = if input == default_rpc { None } else { Some(input) };
                step += 1;
            }

            // Step 4: LLM provider
            4 => {
                println!("  {}", style("Your agent uses an LLM to process tasks. Pick a provider and enter your API key.").dim());
                let options = &["Anthropic (Claude)", "OpenAI (GPT)", "\u{2190} Back"];
                let idx = Select::new()
                    .with_prompt("LLM provider")
                    .items(options)
                    .default(0)
                    .interact()?;

                if idx == 2 {
                    step -= 1;
                    continue;
                }

                provider = match idx {
                    0 => "anthropic".to_string(),
                    _ => "openai".to_string(),
                };
                step += 1;
            }

            // Step 5: API key
            5 => {
                let label = if provider == "anthropic" {
                    "Anthropic (Claude)"
                } else {
                    "OpenAI (GPT)"
                };
                let input: String = Password::new()
                    .with_prompt(format!("{} API key (or \"back\")", label))
                    .interact()?;

                if input.eq_ignore_ascii_case("back") {
                    step -= 1;
                    continue;
                }

                if input.is_empty() {
                    println!("  {} API key cannot be empty.", style("!").yellow());
                    continue;
                }

                api_key = input;
                step += 1;
            }

            // Step 6: Model selection (fetched from API)
            6 => {
                println!("  {}", style("Which model your agent will use. Faster models = lower cost, smarter models = better results.").dim());
                let mut models = fetch_models(&provider, &api_key);
                models.push("\u{2190} Back".to_string());

                let idx = Select::new()
                    .with_prompt("Model")
                    .items(&models)
                    .default(0)
                    .interact()?;

                if idx == models.len() - 1 {
                    step -= 1;
                    continue;
                }

                model = models[idx].clone();
                step += 1;
            }

            // Step 7: Max tokens
            7 => {
                println!("  {}", style("Maximum length of each LLM response (in tokens). 4096 is good for most tasks.").dim());
                let input: String = Input::new()
                    .with_prompt("Max tokens per LLM response (or \"back\")")
                    .default("4096".into())
                    .interact_text()?;

                if input.eq_ignore_ascii_case("back") {
                    step -= 1;
                    continue;
                }

                match input.parse::<u32>() {
                    Ok(val) if val > 0 => {
                        max_tokens = val;
                        step += 1;
                    }
                    _ => {
                        println!("  {} Invalid number.", style("!").yellow());
                    }
                }
            }

            // Step 8: Password encryption
            8 => {
                println!("  {}", style("Set a password to encrypt secret keys (Nostr, Solana, API keys).").dim());
                println!("  {}", style("Leave empty for no encryption. Type \"back\" to go back.").dim());
                let pw: String = Password::new()
                    .with_prompt("Password (empty = no encryption)")
                    .allow_empty_password(true)
                    .interact()?;

                if pw.eq_ignore_ascii_case("back") {
                    step -= 1;
                    continue;
                }

                if pw.is_empty() {
                    encryption_password = None;
                    println!("  {} Keys will be stored unencrypted.", style("~").dim());
                } else {
                    let pw_confirm: String = Password::new()
                        .with_prompt("Confirm password")
                        .allow_empty_password(true)
                        .interact()?;
                    if pw != pw_confirm {
                        println!("  {} Passwords don't match.", style("!").yellow());
                        continue;
                    }
                    encryption_password = Some(Zeroizing::new(pw));
                    println!("  {} Keys will be encrypted.", style("*").green());
                }
                step += 1;
            }

            // Done — build config
            _ => break,
        }
    }

    // Generate Nostr keypair
    let keys = Keys::generate();
    let secret_key = keys.secret_key().to_secret_hex();

    // Generate Solana keypair
    let solana_keypair = Keypair::new();
    let solana_secret_key = bs58::encode(solana_keypair.to_bytes()).into_string();
    let solana_address = solana_keypair.pubkey().to_string();

    let llm_section = LlmSection {
        provider: provider.clone(),
        api_key,
        model,
        max_tokens,
    };

    let mut cfg = AgentConfig {
        name: name.clone(),
        description,
        capabilities: vec!["general".to_string()],
        relays: vec![
            "wss://relay.damus.io".into(),
            "wss://nos.lol".into(),
            "wss://relay.nostr.band".into(),
        ],
        secret_key,
        payment: PaymentSection {
            chain: "solana".to_string(),
            network: network.clone(),
            rpc_url: rpc_url_opt,
            job_price: 10_000_000, // 0.01 SOL default
            payment_timeout_secs: 120,
            solana_secret_key,
        },
        llm: Some(llm_section),
        customer_llm: None,
        encryption: None,
        recovery: Default::default(),
    };

    if let Some(ref password) = encryption_password {
        cfg.encrypt_secrets(password)?;
    }
    config::save_config(&cfg)?;
    // encryption_password is zeroized automatically on drop (Zeroizing<String>)

    let npub = keys.public_key().to_bech32().unwrap_or_default();
    println!("\n  {} Agent {} created!", style("").green().bold(), style(&name).cyan().bold());
    println!();
    println!("     {}  {}", style("npub").dim(), style(&npub).dim());
    println!("     {}  {}", style("wallet").dim(), style(&solana_address).dim());
    println!("     {}  {}", style("network").dim(), style(&network).cyan());
    println!("     {}  {}", style("config").dim(), style(config::config_path(&name)?.display()).dim());

    if network != "mainnet" {
        println!(
            "\n  Get devnet SOL   {}",
            style("https://faucet.solana.com").cyan()
        );
    }
    println!("  Start agent      {}\n", style(format!("elisym start {}", name)).cyan());

    Ok(name)
}

// ── config ────────────────────────────────────────────────────────────

fn cmd_config(name: &str) -> Result<()> {
    let mut cfg = config::load_config(name)?;
    let password: Option<Zeroizing<String>> = if cfg.is_encrypted() {
        let p = Zeroizing::new(Password::new()
            .with_prompt("Password")
            .interact()?);
        cfg.decrypt_secrets(&p)?;
        Some(p)
    } else {
        None
    };

    println!("{}\n", style(format!("Configure agent: {}", name)).bold());

    loop {
        let main_options = &["Provider settings", "Done"];
        let main_idx = Select::new()
            .with_prompt("Settings")
            .items(main_options)
            .default(0)
            .interact()?;

        match main_idx {
            // Provider settings
            0 => {
                loop {
                    let sub_options = &["Job price", "LLM provider", "\u{2190} Back"];
                    let sub_idx = Select::new()
                        .with_prompt("Provider settings")
                        .items(sub_options)
                        .default(0)
                        .interact()?;

                    match sub_idx {
                        // Job price
                        0 => {
                            println!(
                                "  {} Current price: {} SOL",
                                style("~").dim(),
                                format_sol(cfg.payment.job_price)
                            );
                            loop {
                                let input: String = Input::new()
                                    .with_prompt("Job price in SOL")
                                    .default(format_sol(cfg.payment.job_price))
                                    .interact_text()?;

                                match sol_to_lamports(&input) {
                                    Some(new_price) => {
                                        if let Some(err) = agent::validate_job_price(new_price) {
                                            println!("  {} {}", style("!").yellow(), err);
                                            continue;
                                        }
                                        cfg.payment.job_price = new_price;
                                        save_config_encrypted(&mut cfg, &password)?;
                                        println!(
                                            "  {} Price set to {} SOL",
                                            style("*").green(),
                                            format_sol(cfg.payment.job_price)
                                        );
                                        break;
                                    }
                                    _ => {
                                        println!("  {} Invalid amount.", style("!").yellow());
                                    }
                                }
                            }
                        }
                        // LLM settings
                        1 => {
                            if let Some(ref llm) = cfg.llm {
                                println!(
                                    "  {} Current: {} / {}",
                                    style("~").dim(),
                                    style(&llm.provider).cyan(),
                                    style(&llm.model).cyan().bold(),
                                );
                                let llm_options = &[
                                    "Change model",
                                    "Change max tokens",
                                    "Change provider + API key",
                                    "\u{2190} Back",
                                ];
                                let llm_idx = Select::new()
                                    .with_prompt("LLM settings")
                                    .items(llm_options)
                                    .default(0)
                                    .interact()?;

                                match llm_idx {
                                    // Change model only
                                    0 => {
                                        let llm = cfg.llm.as_ref().unwrap();
                                        let mut models = fetch_models(&llm.provider, &llm.api_key);
                                        // Put current model first if present
                                        if let Some(pos) = models.iter().position(|m| m == &llm.model) {
                                            models.swap(0, pos);
                                        }
                                        let model_idx = Select::new()
                                            .with_prompt("Model")
                                            .items(&models)
                                            .default(0)
                                            .interact()?;
                                        cfg.llm.as_mut().unwrap().model = models[model_idx].clone();
                                        save_config_encrypted(&mut cfg, &password)?;
                                        println!(
                                            "  {} Model set to {}",
                                            style("*").green(),
                                            style(&cfg.llm.as_ref().unwrap().model).cyan().bold(),
                                        );
                                    }
                                    // Change max tokens
                                    1 => {
                                        let current = cfg.llm.as_ref().unwrap().max_tokens;
                                        let new_tokens: u32 = Input::new()
                                            .with_prompt("Max tokens per LLM response")
                                            .default(current)
                                            .interact_text()?;
                                        cfg.llm.as_mut().unwrap().max_tokens = new_tokens;
                                        save_config_encrypted(&mut cfg, &password)?;
                                        println!("  {} Max tokens set to {}", style("*").green(), new_tokens);
                                    }
                                    // Full reconfigure
                                    2 => {
                                        if let Some(llm) = prompt_llm_section()? {
                                            cfg.llm = Some(llm);
                                            save_config_encrypted(&mut cfg, &password)?;
                                            println!("  {} Provider LLM saved.", style("*").green());
                                        }
                                    }
                                    _ => {}
                                }
                            } else {
                                // No LLM configured yet — full setup
                                if let Some(llm) = prompt_llm_section()? {
                                    cfg.llm = Some(llm);
                                    save_config_encrypted(&mut cfg, &password)?;
                                    println!("  {} Provider LLM saved.", style("*").green());
                                }
                            }
                        }
                        // Back
                        _ => break,
                    }
                }
            }
            // Done
            _ => break,
        }
    }

    // password is zeroized automatically on drop (Zeroizing<String>)

    println!(
        "\n  {} All changes saved for '{}'.",
        style("*").green(),
        style(name).cyan()
    );
    println!("  Restart agent to apply changes.");

    Ok(())
}

/// Shared LLM configuration flow used by both init and config commands.
/// Returns None if the user backs out at the first prompt.
fn prompt_llm_section() -> Result<Option<LlmSection>> {
    // Provider
    let provider_options = &["Anthropic (Claude)", "OpenAI (GPT)", "\u{2190} Back"];
    let provider_idx = Select::new()
        .with_prompt("LLM provider")
        .items(provider_options)
        .default(0)
        .interact()?;

    if provider_idx == 2 {
        return Ok(None);
    }

    let provider = match provider_idx {
        0 => "anthropic",
        _ => "openai",
    };

    // API key
    let label = provider_options[provider_idx];
    let api_key: String = Password::new()
        .with_prompt(format!("{} API key", label))
        .interact()?;
    if api_key.is_empty() {
        println!("  {} API key cannot be empty.", style("!").yellow());
        return Ok(None);
    }

    // Model (fetched from API)
    let models = fetch_models(provider, &api_key);
    let model_idx = Select::new()
        .with_prompt("Model")
        .items(&models)
        .default(0)
        .interact()?;

    // Max tokens
    let max_tokens: u32 = Input::new()
        .with_prompt("Max tokens per LLM response")
        .default(4096)
        .interact_text()?;

    Ok(Some(LlmSection {
        provider: provider.to_string(),
        api_key,
        model: models[model_idx].to_string(),
        max_tokens,
    }))
}

// ── capability LLM helper ─────────────────────────────────────────────

// ── start ─────────────────────────────────────────────────────────────

async fn cmd_start(name: Option<String>, headless: bool, price: Option<String>) -> Result<()> {
    let name = match name {
        Some(n) => n,
        None => select_or_create_agent()?,
    };

    let mut cfg = config::load_config(&name)?;
    let enc_password: Option<Zeroizing<String>> = if cfg.is_encrypted() {
        let p = Zeroizing::new(Password::new()
            .with_prompt(format!("Password for '{}'", name))
            .interact()?);
        cfg.decrypt_secrets(&p)?;
        Some(p)
    } else {
        None
    };

    println!("{}", style(banner::BANNER).cyan());
    println!("  Starting agent {}...\n", style(&name).cyan().bold());

    // Show wallet status (no relay connection needed)
    let solana = agent::build_solana_provider(&cfg)?;
    display_wallet_status(&solana, &cfg)?;

    let balance = solana.balance().unwrap_or(0);
    if balance == 0 && cfg.payment.network != "mainnet" {
        println!(
            "\n  {} Wallet is empty. Get devnet SOL: {}",
            style("!").yellow(),
            style("https://faucet.solana.com").cyan()
        );
    }
    drop(solana);

    println!();

    // ── LLM setup ──────────────────────────────────────────────
    if cfg.llm.is_none() {
        println!("  {} No LLM configured. Let's set one up.\n", style("!").yellow());
        match prompt_llm_section()? {
            Some(llm_section) => {
                cfg.llm = Some(llm_section);
                save_config_encrypted(&mut cfg, &enc_password)?;
                println!("  {} LLM saved.\n", style("*").green());
            }
            None => {
                return Err(CliError::Other("LLM is required to run skills".into()));
            }
        }
    }

    // ── Load skills ────────────────────────────────────────────
    let skills_dir = std::env::current_dir()
        .unwrap_or_else(|_| std::path::PathBuf::from("."))
        .join("skills");
    let all_skills = crate::skill::loader::load_skills_from_dir(&skills_dir)?;

    if all_skills.is_empty() {
        println!(
            "  {} No skills found in {}\n",
            style("!").yellow(),
            style(skills_dir.display()).dim()
        );
        println!("  Create a skill directory with a SKILL.md to get started.");
        println!("  Example: ./skills/my-skill/SKILL.md\n");
        return Err(CliError::Other("no skills configured".into()));
    }

    // ── Skill selection ────────────────────────────────────────
    let selected_skill = if all_skills.len() == 1 {
        println!("  {} Skill: {}{}",
            style("*").green(),
            style(all_skills[0].name()).cyan().bold(),
            style(all_skills[0].description()).dim(),
        );
        all_skills.into_iter().next().unwrap()
    } else {
        let skill_names: Vec<String> = all_skills
            .iter()
            .map(|s| format!("{}{}", s.name(), s.description()))
            .collect();
        let idx = Select::new()
            .with_prompt("Select skill")
            .items(&skill_names)
            .default(0)
            .interact()?;
        println!();
        let mut skills = all_skills;
        skills.swap_remove(idx)
    };

    let mut registry = crate::skill::SkillRegistry::new();
    let skill_name = selected_skill.name().to_string();
    let skill_description = selected_skill.description().to_string();
    let skill_caps: Vec<String> = selected_skill.capabilities().to_vec();
    registry.register(selected_skill);

    // ── Price ──────────────────────────────────────────────────
    if let Some(ref price_str) = price {
        // --price flag: set price without interactive prompt (--price 0 = free mode)
        let lamports = sol_to_lamports(price_str)
            .ok_or_else(|| CliError::Other(format!("invalid --price value: {}", price_str)))?;
        if let Some(err) = agent::validate_job_price(lamports) {
            return Err(CliError::Other(err));
        }
        if lamports != cfg.payment.job_price {
            cfg.payment.job_price = lamports;
            save_config_encrypted(&mut cfg, &enc_password)?;
        }
        if cfg.payment.job_price == 0 {
            println!(
                "  {} FREE MODE — payments disabled, jobs processed for free\n",
                style("!").yellow().bold()
            );
        } else {
            println!(
                "  {} Price: {} SOL per job\n",
                style("*").green(),
                style(format_sol(cfg.payment.job_price)).green().bold()
            );
        }
    } else {
        // Interactive price prompt
        println!("  {}", style("How much to charge per job (in SOL). 3% protocol fee is deducted. Enter 0 for free mode.").dim());
        loop {
            let price_input: String = Input::new()
                .with_prompt("Job price in SOL")
                .default(format_sol(cfg.payment.job_price))
                .interact_text()?;

            match sol_to_lamports(&price_input) {
                Some(new_price) => {
                    if let Some(err) = agent::validate_job_price(new_price) {
                        println!("  {} {}", style("!").yellow(), err);
                        continue;
                    }
                    if new_price != cfg.payment.job_price {
                        cfg.payment.job_price = new_price;
                        save_config_encrypted(&mut cfg, &enc_password)?;
                    }
                    if cfg.payment.job_price == 0 {
                        println!(
                            "  {} FREE MODE — payments disabled\n",
                            style("!").yellow().bold()
                        );
                    } else {
                        println!(
                            "  {} Price: {} SOL per job\n",
                            style("*").green(),
                            style(format_sol(cfg.payment.job_price)).green().bold()
                        );
                    }
                    break;
                }
                _ => {
                    println!("  {} Invalid amount.", style("!").yellow());
                }
            }
        }
    }

    // Update config description and capabilities from skill
    let mut skill_config_changed = false;
    if skill_description != cfg.description {
        cfg.description = skill_description;
        skill_config_changed = true;
    }
    if skill_caps != cfg.capabilities {
        cfg.capabilities = skill_caps.clone();
        skill_config_changed = true;
    }
    if skill_config_changed {
        save_config_encrypted(&mut cfg, &enc_password)?;
    }

    // Build LLM client
    let llm_client = Arc::new(llm::LlmClient::new(
        cfg.llm.as_ref().expect("LLM already validated above"),
    )?);

    let ctx = crate::skill::SkillContext {
        llm: Some(llm_client),
        agent_name: cfg.name.clone(),
        agent_description: cfg.description.clone(),
        event_tx: None, // will be replaced after event channel is created
    };

    let runtime_config = crate::runtime::RuntimeConfig {
        job_price: cfg.payment.job_price,
        payment_timeout_secs: cfg.payment.payment_timeout_secs,
        max_concurrent_jobs: 10,
        recovery_max_retries: cfg.recovery.max_retries,
        recovery_interval_secs: cfg.recovery.interval_secs,
        network: cfg.payment.network.clone(),
    };

    let agent_node = with_spinner(
        "Connecting to relays and publishing capabilities...",
        agent::build_agent(&cfg),
    ).await?;

    let agent_node = Arc::new(agent_node);

    // Get wallet balance for TUI header
    let wallet_balance = {
        let solana2 = agent::build_solana_provider(&cfg)?;
        solana2.balance().unwrap_or(0)
    };

    let (event_tx, event_rx) = crate::tui::create_event_channel();

    let ledger = Arc::new(tokio::sync::Mutex::new(
        crate::ledger::JobLedger::open(&name)?,
    ));

    let ctx = crate::skill::SkillContext {
        event_tx: Some(event_tx.clone()),
        ..ctx
    };

    let mut runtime = crate::runtime::AgentRuntime::new(
        Arc::clone(&agent_node),
        registry,
        ctx,
        runtime_config,
        event_tx.clone(),
        Arc::clone(&ledger),
    );
    let transport: Box<dyn crate::transport::Transport> = Box::new(
        crate::transport::nostr::NostrTransport::new(
            agent_node,
            vec![elisym_core::DEFAULT_KIND_OFFSET],
            event_tx.clone(),
            cfg.recovery.delivery_retries,
        ),
    );

    if headless {
        // Headless mode: run runtime directly, logs go to stdout
        println!("  {} Running in headless mode (no TUI)\n", style("*").green());

        // Drain events to log and prevent channel backpressure
        let mut event_rx = event_rx;
        let log_handle = tokio::spawn(async move {
            while let Some(event) = event_rx.recv().await {
                log_event(&event);
            }
        });

        let result = runtime.run(transport).await;
        drop(log_handle);
        result?;
    } else {
        // Silence tracing before TUI to prevent log corruption
        silence_tracing();

        // Create retry channel for manual recovery from TUI
        let (retry_tx, retry_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
        runtime.set_retry_rx(retry_rx);

        let gc = global_config::load_global_config();
        let mut app = crate::tui::App::new(
            name.clone(),
            skill_name,
            cfg.payment.job_price,
            wallet_balance,
            cfg.payment.network.clone(),
            gc.tui.sound_enabled,
            gc.tui.sound_volume,
        );
        app.set_ledger(Arc::clone(&ledger));
        app.set_retry_tx(retry_tx);

        crate::tui::event::run_tui(app, event_rx, runtime, transport).await?;
    }

    Ok(())
}

/// Let the user pick an existing agent or create a new one.
fn select_or_create_agent() -> Result<String> {
    let agents = config::list_agents()?;
    if agents.is_empty() {
        println!("No agents configured. Running init wizard...\n");
        return cmd_init();
    }

    let mut options: Vec<String> = agents.clone();
    options.push("+ Create new agent".into());

    let idx = Select::new()
        .with_prompt("Select agent to start")
        .items(&options)
        .default(0)
        .interact()?;

    if idx == agents.len() {
        return cmd_init();
    }

    Ok(agents[idx].clone())
}

// ── list ──────────────────────────────────────────────────────────────

fn cmd_list() -> Result<()> {
    let agents = config::list_agents()?;
    if agents.is_empty() {
        println!("No agents configured. Run {} to create one.", style("elisym init").cyan());
        return Ok(());
    }

    println!("{}", style("Configured agents:").bold());
    for name in &agents {
        match config::load_config_public(name) {
            Ok(cfg) => {
                println!(
                    "  {}{} [{}]",
                    style(name).cyan(),
                    cfg.description,
                    cfg.capabilities.join(", ")
                );
            }
            Err(_) => {
                println!("  {}{}", style(name).cyan(), style("(config error)").red());
            }
        }
    }
    Ok(())
}

// ── status ────────────────────────────────────────────────────────────

fn cmd_status(name: &str) -> Result<()> {
    let cfg = config::load_config_public(name)?;

    println!("{}", style(format!("Agent: {}", cfg.name)).bold());
    println!("  description:  {}", cfg.description);
    println!("  capabilities: {}", cfg.capabilities.join(", "));
    println!("  relays:       {}", cfg.relays.join(", "));
    println!("  chain:        {}", cfg.payment.chain);
    println!("  network:      {}", cfg.payment.network);
    println!(
        "  job price:    {} SOL ({} lamports)",
        format_sol(cfg.payment.job_price),
        cfg.payment.job_price
    );
    if let Some(addr) = cfg.payment.solana_address() {
        println!("  wallet:       {}", addr);
    }
    if let Some(ref llm) = cfg.llm {
        println!("  llm provider: {}", llm.provider);
        println!("  llm model:    {}", llm.model);
        println!("  max tokens:   {}", llm.max_tokens);
    } else {
        println!("  llm:          {}", style("not configured").dim());
    }
    println!("  config:       {}", config::config_path(name)?.display());

    Ok(())
}

// ── delete ────────────────────────────────────────────────────────────

fn cmd_delete(name: &str) -> Result<()> {
    let confirmed = Confirm::new()
        .with_prompt(format!("Delete agent '{}' and all its data?", name))
        .default(false)
        .interact()?;

    if !confirmed {
        println!("Cancelled.");
        return Ok(());
    }

    config::delete_agent(name)?;
    println!("Deleted agent '{}'.", style(name).cyan());
    Ok(())
}

// ── wallet ────────────────────────────────────────────────────────────

fn cmd_wallet(name: &str) -> Result<()> {
    let mut cfg = config::load_config(name)?;
    if cfg.is_encrypted() {
        unlock_config(&mut cfg)?;
    }

    print!("{}", style(banner::BANNER).cyan());
    println!(
        "  Wallet for agent {}\n",
        style(name).cyan().bold()
    );

    let solana = agent::build_solana_provider(&cfg)?;
    display_wallet_status(&solana, &cfg)?;

    Ok(())
}

// ── send ─────────────────────────────────────────────────────────────

fn cmd_send(name: &str, address: &str, amount: &str) -> Result<()> {
    let mut cfg = config::load_config(name)?;
    if cfg.is_encrypted() {
        unlock_config(&mut cfg)?;
    }

    // Validate destination address early
    let dest_pubkey: solana_sdk::pubkey::Pubkey = address
        .parse()
        .map_err(|_| CliError::Other(format!("invalid Solana address: {}", address)))?;

    let solana = agent::build_solana_provider(&cfg)?;

    // Self-transfer warning
    let own_address = solana.address();
    if dest_pubkey.to_string() == own_address {
        println!(
            "  {} Destination is the agent's own wallet ({})",
            style("!").yellow().bold(),
            style(&own_address).dim(),
        );
        let proceed = Confirm::new()
            .with_prompt("Send to yourself?")
            .default(false)
            .interact()?;
        if !proceed {
            println!("  Cancelled.");
            return Ok(());
        }
    }

    // Convert SOL to lamports
    let base_amount = sol_to_lamports(amount)
        .ok_or_else(|| CliError::Other(format!("invalid SOL amount: {}", amount)))?;
    let unit_label = "SOL";

    // Show current balance
    let balance = solana.balance().unwrap_or(0);
    println!("  Balance: {} SOL ({} lamports)", format_sol(balance), balance);
    println!(
        "  Sending {} {} to {}",
        style(format_sol_compact(base_amount)).bold(),
        unit_label,
        style(address).dim()
    );

    let confirmed = Confirm::new()
        .with_prompt("Confirm send?")
        .default(false)
        .interact()?;

    if !confirmed {
        println!("  Cancelled.");
        return Ok(());
    }

    // Use a dummy reference key (not needed for direct sends, but required by the format)
    let reference = Keypair::new().pubkey().to_string();
    let request_json = serde_json::json!({
        "recipient": address,
        "amount": base_amount,
        "reference": reference,
    }).to_string();

    use elisym_core::PaymentProvider;
    match solana.pay(&request_json) {
        Ok(result) => {
            println!(
                "\n  {} Sent {} {}",
                style("*").green(),
                style(format_sol_compact(base_amount)).bold(),
                unit_label
            );
            println!("  Signature: {}", style(&result.payment_id).dim());

            // Show updated balance
            if let Ok(new_balance) = solana.balance() {
                println!(
                    "  Balance:   {} SOL ({} lamports)",
                    format_sol(new_balance),
                    new_balance
                );
            }
        }
        Err(e) => {
            println!("  {} Send failed: {}", style("!").red(), e);
            return Err(e.into());
        }
    }

    Ok(())
}

// ── wallet helpers ───────────────────────────────────────────────────

fn display_wallet_status(solana: &elisym_core::SolanaPaymentProvider, cfg: &AgentConfig) -> Result<()> {
    let address = solana.address();
    let balance = solana.balance().unwrap_or(0);

    println!("\n  {} {}", style("").cyan(), style("Wallet").bold());
    println!("     Network  {}", style(&cfg.payment.network).cyan());
    println!("     Address  {}", style(&address).dim());
    println!(
        "     Balance  {} SOL {}",
        style(format_sol(balance)).green().bold(),
        style(format!("({} lamports)", balance)).dim(),
    );

    Ok(())
}

/// Prompt for password and decrypt secrets if the config is encrypted.
fn unlock_config(cfg: &mut config::AgentConfig) -> Result<()> {
    if !cfg.is_encrypted() {
        return Ok(());
    }
    let max_attempts = 3;
    for attempt in 1..=max_attempts {
        let password = Zeroizing::new(Password::new()
            .with_prompt("Password")
            .interact()?);
        match cfg.decrypt_secrets(&password) {
            Ok(()) => return Ok(()),
            Err(e) if attempt == max_attempts => return Err(e),
            Err(_) => {
                println!(
                    "  {} Wrong password ({}/{})",
                    style("!").yellow(),
                    attempt,
                    max_attempts,
                );
                // Throttle brute-force attempts
                std::thread::sleep(std::time::Duration::from_secs(1));
            }
        }
    }
    unreachable!()
}

/// Log an AppEvent to stdout in headless mode.
fn log_event(event: &crate::tui::AppEvent) {
    use crate::tui::AppEvent;
    match event {
        AppEvent::JobReceived { job_id, customer_id, .. } => {
            println!("[job] received {} from {}", &job_id[..12.min(job_id.len())], &customer_id[..12.min(customer_id.len())]);
        }
        AppEvent::PaymentRequested { job_id, price, fee } => {
            println!("[job] {} requesting payment: {} lamports (fee {})", &job_id[..12.min(job_id.len())], price, fee);
        }
        AppEvent::PaymentReceived { job_id, net_amount } => {
            println!("[job] {} payment received: {} lamports", &job_id[..12.min(job_id.len())], net_amount);
        }
        AppEvent::PaymentTimeout { job_id } => {
            println!("[job] {} payment timeout", &job_id[..12.min(job_id.len())]);
        }
        AppEvent::SkillStarted { job_id, skill_name } => {
            println!("[job] {} skill started: {}", &job_id[..12.min(job_id.len())], skill_name);
        }
        AppEvent::LlmRound { job_id, round, max_rounds } => {
            println!("[job] {} LLM round {}/{}", &job_id[..12.min(job_id.len())], round, max_rounds);
        }
        AppEvent::ToolStarted { job_id, tool_name } => {
            println!("[job] {} tool started: {}", &job_id[..12.min(job_id.len())], tool_name);
        }
        AppEvent::ToolCompleted { job_id, tool_name, output_len } => {
            println!("[job] {} tool completed: {} ({} bytes)", &job_id[..12.min(job_id.len())], tool_name, output_len);
        }
        AppEvent::ToolFailed { job_id, tool_name, error } => {
            println!("[job] {} tool failed: {}{}", &job_id[..12.min(job_id.len())], tool_name, error);
        }
        AppEvent::JobCompleted { job_id, result_len } => {
            println!("[job] {} completed ({} bytes)", &job_id[..12.min(job_id.len())], result_len);
        }
        AppEvent::JobFailed { job_id, error } => {
            println!("[job] {} failed: {}", &job_id[..12.min(job_id.len())], error);
        }
        AppEvent::WalletBalance(bal) => {
            println!("[wallet] balance: {} lamports", bal);
        }
        AppEvent::Ping { from } => {
            println!("[ping] from {}", &from[..12.min(from.len())]);
        }
    }
}

/// Save config, re-encrypting secrets if a password is provided.
/// Encrypts → saves → decrypts back so the in-memory config stays usable.
fn save_config_encrypted(cfg: &mut config::AgentConfig, password: &Option<Zeroizing<String>>) -> Result<()> {
    if let Some(ref p) = password {
        cfg.encrypt_secrets(p)?;
        config::save_config(cfg)?;
        cfg.decrypt_secrets(p)?;
    } else {
        config::save_config(cfg)?;
    }
    Ok(())
}