elevenlabs-cli 0.1.8

Unofficial CLI for ElevenLabs text-to-speech API
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
use crate::cli::{AgentArgs, AgentCommands, SpellingPatience};
use crate::client::create_http_client;
use crate::output::{print_info, print_success};
use anyhow::{Context, Result};
use colored::*;
use comfy_table::Table;
use reqwest::Client;
use serde::Deserialize;
use serde_json::json;

pub async fn execute(args: AgentArgs, api_key: &str) -> Result<()> {
    let client = create_http_client();

    match args.command {
        AgentCommands::List { limit } => list_agents(&client, api_key, limit).await,
        AgentCommands::Summaries { limit } => list_agent_summaries(&client, api_key, limit).await,
        AgentCommands::Get { agent_id } => get_agent(&client, api_key, &agent_id).await,
        AgentCommands::Create {
            name,
            description,
            voice_id,
            first_message,
            system_prompt,
        } => {
            create_agent(
                &client,
                api_key,
                &name,
                description.as_deref(),
                voice_id.as_deref(),
                first_message.as_deref(),
                system_prompt.as_deref(),
            )
            .await
        }
        AgentCommands::Update {
            agent_id,
            name,
            description,
        } => {
            update_agent(
                &client,
                api_key,
                &agent_id,
                name.as_deref(),
                description.as_deref(),
            )
            .await
        }
        AgentCommands::Delete { agent_id } => delete_agent(&client, api_key, &agent_id).await,
        AgentCommands::Link { agent_id } => get_agent_link(&client, api_key, &agent_id).await,
        AgentCommands::Duplicate { agent_id, name } => {
            duplicate_agent(&client, api_key, &agent_id, &name).await
        }
        AgentCommands::Branches { agent_id } => {
            list_agent_branches(&client, api_key, &agent_id).await
        }
        AgentCommands::RenameBranch {
            agent_id,
            branch_id,
            name,
        } => rename_agent_branch(&client, api_key, &agent_id, &branch_id, &name).await,
        AgentCommands::BatchList { limit } => list_batch_calls(&client, api_key, limit).await,
        AgentCommands::BatchStatus { batch_id } => {
            get_batch_call_status(&client, api_key, &batch_id).await
        }
        AgentCommands::BatchDelete { batch_id } => {
            delete_batch_call(&client, api_key, &batch_id).await
        }
        AgentCommands::Simulate {
            agent_id,
            message,
            max_turns,
        } => simulate_conversation(&client, api_key, &agent_id, &message, max_turns).await,
        AgentCommands::UpdateTurn {
            agent_id,
            spelling_patience,
            silence_threshold_ms,
        } => {
            update_agent_turn_config(
                &client,
                api_key,
                &agent_id,
                spelling_patience,
                silence_threshold_ms,
            )
            .await
        }
        AgentCommands::WhatsappList => list_whatsapp_accounts(&client, api_key).await,
        AgentCommands::WidgetGet { agent_id } => {
            get_agent_widget(&client, api_key, &agent_id).await
        }
        AgentCommands::WidgetAvatar {
            agent_id,
            avatar_file,
        } => set_agent_widget_avatar(&client, api_key, &agent_id, &avatar_file).await,
    }
}

#[derive(Debug, Deserialize)]
struct AgentSummary {
    agent_id: String,
    name: String,
    #[serde(default)]
    created_at: Option<String>,
}

#[derive(Debug, Deserialize)]
struct AgentDetail {
    agent_id: String,
    name: String,
    #[serde(default)]
    version_id: Option<String>,
    #[serde(default)]
    branch_id: Option<String>,
    #[serde(default)]
    tags: Option<Vec<String>>,
}

#[derive(Debug, Deserialize)]
struct CreateAgentResponse {
    agent_id: String,
    #[allow(dead_code)]
    name: String,
}

#[derive(Debug, Deserialize)]
struct LinkResponse {
    url: String,
}

async fn list_agents(client: &Client, api_key: &str, limit: Option<u32>) -> Result<()> {
    print_info("Fetching agents...");

    let url = "https://api.elevenlabs.io/v1/agents";
    let mut request = client.get(url).header("xi-api-key", api_key);

    if let Some(lim) = limit {
        request = request.query(&[("limit", lim.to_string())]);
    }

    let response = request.send().await.context("Failed to fetch agents")?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    let agents: Vec<AgentSummary> = response.json().await.context("Failed to parse response")?;

    let mut table = Table::new();
    table.set_header(vec!["ID", "Name", "Created"]);

    for agent in &agents {
        table.add_row(vec![
            agent.agent_id.yellow(),
            agent.name.cyan(),
            agent.created_at.clone().unwrap_or_default().as_str().into(),
        ]);
    }

    println!("{}", table);
    println!("\nTotal agents: {}", agents.len().to_string().green());

    Ok(())
}

async fn get_agent(client: &Client, api_key: &str, agent_id: &str) -> Result<()> {
    print_info(&format!("Fetching agent '{}'...", agent_id.cyan()));

    let url = format!("https://api.elevenlabs.io/v1/agents/{}", agent_id);
    let response = client
        .get(&url)
        .header("xi-api-key", api_key)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    let agent: AgentDetail = response.json().await?;

    let mut table = Table::new();
    table.set_header(vec!["Property", "Value"]);
    table.add_row(vec!["ID", &agent.agent_id.yellow()]);
    table.add_row(vec!["Name", &agent.name.cyan()]);
    table.add_row(vec![
        "Version ID",
        agent.version_id.unwrap_or_default().as_str(),
    ]);
    table.add_row(vec![
        "Branch ID",
        agent.branch_id.unwrap_or_default().as_str(),
    ]);

    if let Some(tags) = &agent.tags {
        table.add_row(vec!["Tags", &tags.join(", ").as_str()]);
    }

    println!("{}", table);
    Ok(())
}

async fn create_agent(
    client: &Client,
    api_key: &str,
    name: &str,
    description: Option<&str>,
    voice_id: Option<&str>,
    first_message: Option<&str>,
    system_prompt: Option<&str>,
) -> Result<()> {
    print_info(&format!("Creating agent '{}'...", name.cyan()));

    let mut body = json!({
        "name": name
    });

    if let Some(desc) = description {
        body["description"] = json!(desc);
    }

    // Add conversation config with voice settings if provided
    let mut conversation_config = json!({});
    let mut has_config = false;

    if let Some(vid) = voice_id {
        conversation_config["agent"]["voice_id"] = json!(vid);
        has_config = true;
    }
    if let Some(msg) = first_message {
        conversation_config["agent"]["first_message"] = json!(msg);
        has_config = true;
    }
    if let Some(prompt) = system_prompt {
        conversation_config["agent"]["prompt"]["prompt"] = json!(prompt);
        has_config = true;
    }

    if has_config {
        body["conversation_config"] = conversation_config;
    }

    let url = "https://api.elevenlabs.io/v1/agents";
    let response = client
        .post(url)
        .header("xi-api-key", api_key)
        .json(&body)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    let result: CreateAgentResponse = response.json().await?;
    print_success("Agent created successfully!");
    print_info(&format!("Agent ID: {}", result.agent_id.yellow()));

    Ok(())
}

async fn update_agent(
    client: &Client,
    api_key: &str,
    agent_id: &str,
    name: Option<&str>,
    description: Option<&str>,
) -> Result<()> {
    print_info(&format!("Updating agent '{}'...", agent_id.cyan()));

    let mut body = json!({});

    if let Some(n) = name {
        body["name"] = json!(n);
    }
    if let Some(d) = description {
        body["description"] = json!(d);
    }

    let url = format!("https://api.elevenlabs.io/v1/agents/{}", agent_id);
    let response = client
        .patch(&url)
        .header("xi-api-key", api_key)
        .json(&body)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    print_success("Agent updated successfully!");
    Ok(())
}

async fn delete_agent(client: &Client, api_key: &str, agent_id: &str) -> Result<()> {
    print_info(&format!("Deleting agent '{}'...", agent_id.cyan()));

    let url = format!("https://api.elevenlabs.io/v1/agents/{}", agent_id);
    let response = client
        .delete(&url)
        .header("xi-api-key", api_key)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    print_success(&format!("Agent '{}' deleted successfully", agent_id));
    Ok(())
}

async fn get_agent_link(client: &Client, api_key: &str, agent_id: &str) -> Result<()> {
    print_info(&format!("Getting link for agent '{}'...", agent_id.cyan()));

    let url = format!("https://api.elevenlabs.io/v1/agents/{}/link", agent_id);
    let response = client
        .get(&url)
        .header("xi-api-key", api_key)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    let link: LinkResponse = response.json().await?;
    print_success(&format!("Agent link: {}", link.url.green()));

    Ok(())
}

async fn duplicate_agent(client: &Client, api_key: &str, agent_id: &str, name: &str) -> Result<()> {
    print_info(&format!(
        "Duplicating agent '{}' as '{}'...",
        agent_id.cyan(),
        name.cyan()
    ));

    let url = format!("https://api.elevenlabs.io/v1/agents/{}/duplicate", agent_id);
    let body = json!({ "name": name });
    let response = client
        .post(&url)
        .header("xi-api-key", api_key)
        .json(&body)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    let result: CreateAgentResponse = response.json().await?;
    print_success("Agent duplicated successfully!");
    print_info(&format!("New Agent ID: {}", result.agent_id.yellow()));

    Ok(())
}

async fn list_agent_summaries(client: &Client, api_key: &str, limit: Option<u32>) -> Result<()> {
    print_info("Fetching agent summaries...");

    let url = "https://api.elevenlabs.io/v1/convai/agents/summaries";
    let mut request = client.get(url).header("xi-api-key", api_key);

    if let Some(lim) = limit {
        request = request.query(&[("limit", lim.to_string())]);
    }

    let response = request
        .send()
        .await
        .context("Failed to fetch agent summaries")?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    #[derive(Deserialize)]
    struct AgentSummaryItem {
        agent_id: String,
        name: String,
        #[serde(default)]
        description: Option<String>,
    }

    let summaries: Vec<AgentSummaryItem> =
        response.json().await.context("Failed to parse response")?;

    let mut table = Table::new();
    table.set_header(vec!["ID", "Name", "Description"]);

    for summary in &summaries {
        let desc: String = summary
            .description
            .as_deref()
            .unwrap_or("-")
            .chars()
            .take(40)
            .collect();
        table.add_row(vec![
            summary.agent_id.yellow(),
            summary.name.cyan(),
            desc.as_str().into(),
        ]);
    }

    println!("{}", table);
    println!("\nTotal agents: {}", summaries.len().to_string().green());

    Ok(())
}

async fn list_agent_branches(client: &Client, api_key: &str, agent_id: &str) -> Result<()> {
    print_info(&format!(
        "Fetching branches for agent '{}'...",
        agent_id.cyan()
    ));

    let url = format!("https://api.elevenlabs.io/v1/agents/{}/branches", agent_id);
    let response = client
        .get(&url)
        .header("xi-api-key", api_key)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    #[derive(Deserialize)]
    struct BranchInfo {
        branch_id: String,
        name: String,
        #[serde(default)]
        created_at: Option<String>,
    }

    let branches: Vec<BranchInfo> = response.json().await?;

    let mut table = Table::new();
    table.set_header(vec!["Branch ID", "Name", "Created"]);

    for branch in &branches {
        table.add_row(vec![
            branch.branch_id.yellow(),
            branch.name.cyan(),
            branch.created_at.as_deref().unwrap_or("-").into(),
        ]);
    }

    println!("{}", table);
    print_success(&format!("Found {} branch(es)", branches.len()));
    Ok(())
}

async fn rename_agent_branch(
    client: &Client,
    api_key: &str,
    agent_id: &str,
    branch_id: &str,
    name: &str,
) -> Result<()> {
    print_info(&format!(
        "Renaming branch '{}' to '{}'...",
        branch_id.cyan(),
        name.cyan()
    ));

    let url = format!(
        "https://api.elevenlabs.io/v1/agents/{}/branches/{}",
        agent_id, branch_id
    );
    let body = json!({ "name": name });

    let response = client
        .patch(&url)
        .header("xi-api-key", api_key)
        .json(&body)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    print_success(&format!("Branch renamed to '{}'", name.green()));
    Ok(())
}

async fn list_batch_calls(client: &Client, api_key: &str, limit: Option<u32>) -> Result<()> {
    print_info("Fetching batch calls...");

    let url = "https://api.elevenlabs.io/v1/convai/batch-calling";
    let mut request = client.get(url).header("xi-api-key", api_key);

    if let Some(lim) = limit {
        request = request.query(&[("limit", lim.to_string())]);
    }

    let response = request
        .send()
        .await
        .context("Failed to fetch batch calls")?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    #[derive(Deserialize)]
    struct BatchCallInfo {
        batch_id: String,
        #[serde(default)]
        status: Option<String>,
        #[serde(default)]
        total_calls: Option<u32>,
        #[serde(default)]
        total_calls_finished: Option<u32>,
        #[serde(default)]
        created_at: Option<String>,
    }

    let batches: Vec<BatchCallInfo> = response.json().await?;

    if batches.is_empty() {
        print_info("No batch calls found");
        return Ok(());
    }

    let mut table = Table::new();
    table.set_header(vec!["Batch ID", "Status", "Calls", "Finished", "Created"]);

    for batch in &batches {
        let total_str = batch
            .total_calls
            .map(|n| n.to_string())
            .unwrap_or_else(|| "-".to_string());
        let finished_str = batch
            .total_calls_finished
            .map(|n| n.to_string())
            .unwrap_or_else(|| "-".to_string());
        table.add_row(vec![
            batch.batch_id.yellow(),
            batch.status.as_deref().unwrap_or("-").into(),
            total_str.into(),
            finished_str.into(),
            batch.created_at.as_deref().unwrap_or("-").into(),
        ]);
    }

    println!("{}", table);
    print_success(&format!("Found {} batch call(s)", batches.len()));
    Ok(())
}

async fn get_batch_call_status(client: &Client, api_key: &str, batch_id: &str) -> Result<()> {
    print_info(&format!(
        "Fetching batch call '{}' status...",
        batch_id.cyan()
    ));

    let url = format!(
        "https://api.elevenlabs.io/v1/convai/batch-calling/{}",
        batch_id
    );
    let response = client
        .get(&url)
        .header("xi-api-key", api_key)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    #[derive(Deserialize)]
    struct BatchCallDetail {
        batch_id: String,
        status: String,
        #[serde(default)]
        total_calls: u32,
        #[serde(default)]
        total_calls_finished: u32,
        #[serde(default)]
        total_calls_dispatched: Option<u32>,
        #[serde(default)]
        created_at: Option<String>,
    }

    let batch: BatchCallDetail = response.json().await?;

    let mut table = Table::new();
    table.set_header(vec!["Property", "Value"]);
    table.add_row(vec!["Batch ID", &batch.batch_id.yellow()]);
    table.add_row(vec!["Status", &batch.status.cyan()]);
    table.add_row(vec!["Total Calls", &batch.total_calls.to_string()]);
    table.add_row(vec!["Finished", &batch.total_calls_finished.to_string()]);
    if let Some(dispatched) = batch.total_calls_dispatched {
        table.add_row(vec!["Dispatched", &dispatched.to_string()]);
    }
    if let Some(created) = &batch.created_at {
        table.add_row(vec!["Created", created]);
    }

    println!("{}", table);
    Ok(())
}

async fn delete_batch_call(client: &Client, api_key: &str, batch_id: &str) -> Result<()> {
    print_info(&format!("Deleting batch call '{}'...", batch_id.cyan()));

    let url = format!(
        "https://api.elevenlabs.io/v1/convai/batch-calling/{}",
        batch_id
    );
    let response = client
        .delete(&url)
        .header("xi-api-key", api_key)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    print_success(&format!(
        "Batch call '{}' deleted successfully",
        batch_id.green()
    ));
    Ok(())
}

async fn simulate_conversation(
    client: &Client,
    api_key: &str,
    agent_id: &str,
    message: &str,
    max_turns: u32,
) -> Result<()> {
    print_info(&format!(
        "Simulating conversation with agent '{}'...",
        agent_id.cyan()
    ));
    print_info(&format!("Initial message: \"{}\"", message.cyan()));
    print_info(&format!("Max turns: {}", max_turns));

    let url = format!(
        "https://api.elevenlabs.io/v1/convai/agents/{}/simulate",
        agent_id
    );

    let body = json!({
        "messages": [
            {
                "role": "user",
                "content": message
            }
        ],
        "max_turns": max_turns
    });

    let response = client
        .post(&url)
        .header("xi-api-key", api_key)
        .json(&body)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    #[derive(Deserialize)]
    struct SimulationMessage {
        role: String,
        content: String,
    }

    #[derive(Deserialize)]
    struct SimulationResponse {
        messages: Vec<SimulationMessage>,
        #[serde(default)]
        conversation_id: Option<String>,
    }

    let result: SimulationResponse = response.json().await?;

    println!("\n{}", "Conversation Simulation:".bold().underline());

    if let Some(cid) = &result.conversation_id {
        println!("Conversation ID: {}", cid.yellow());
    }
    println!();

    for msg in &result.messages {
        match msg.role.as_str() {
            "user" => println!("{} {}", "[User]:".blue().bold(), msg.content),
            "assistant" | "agent" => println!("{} {}", "[Agent]:".green().bold(), msg.content),
            _ => println!("{}: {}", msg.role, msg.content),
        }
    }

    print_success(&format!(
        "Simulation complete ({} messages)",
        result.messages.len()
    ));
    Ok(())
}

async fn update_agent_turn_config(
    client: &Client,
    api_key: &str,
    agent_id: &str,
    spelling_patience: Option<SpellingPatience>,
    silence_threshold_ms: Option<u32>,
) -> Result<()> {
    print_info(&format!(
        "Updating turn configuration for agent '{}'...",
        agent_id.cyan()
    ));

    let mut turn_config = json!({});

    if let Some(sp) = spelling_patience {
        let patience_str = match sp {
            SpellingPatience::Auto => "auto",
            SpellingPatience::Low => "low",
            SpellingPatience::Medium => "medium",
            SpellingPatience::High => "high",
        };
        turn_config["spelling_patience"] = json!(patience_str);
        print_info(&format!(
            "Setting spelling patience to: {}",
            patience_str.cyan()
        ));
    }

    if let Some(threshold) = silence_threshold_ms {
        turn_config["silence_threshold_ms"] = json!(threshold);
        print_info(&format!(
            "Setting silence threshold to: {}ms",
            threshold.to_string().cyan()
        ));
    }

    let body = json!({
        "conversation_config": {
            "turn": turn_config
        }
    });

    let url = format!("https://api.elevenlabs.io/v1/agents/{}", agent_id);
    let response = client
        .patch(&url)
        .header("xi-api-key", api_key)
        .json(&body)
        .send()
        .await?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    print_success("Turn configuration updated successfully!");
    Ok(())
}

async fn list_whatsapp_accounts(client: &Client, api_key: &str) -> Result<()> {
    print_info("Fetching WhatsApp accounts...");

    // Get all agents and their WhatsApp accounts
    let url = "https://api.elevenlabs.io/v1/agents";
    let response = client
        .get(url)
        .header("xi-api-key", api_key)
        .send()
        .await
        .context("Failed to fetch agents")?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    #[derive(Deserialize)]
    struct AgentWithWhatsapp {
        agent_id: String,
        name: String,
        #[serde(default)]
        whatsapp_accounts: Option<Vec<WhatsAppAccount>>,
    }

    #[derive(Deserialize)]
    struct WhatsAppAccount {
        #[allow(dead_code)]
        phone_number_id: String,
        #[serde(default)]
        phone_number: Option<String>,
        #[serde(default)]
        business_account_id: Option<String>,
    }

    let agents: Vec<AgentWithWhatsapp> =
        response.json().await.context("Failed to parse response")?;

    let mut table = Table::new();
    table.set_header(vec![
        "Agent ID",
        "Agent Name",
        "WhatsApp Phone",
        "Business Account",
    ]);

    let mut found_any = false;
    for agent in &agents {
        if let Some(accounts) = &agent.whatsapp_accounts {
            for account in accounts {
                found_any = true;
                table.add_row(vec![
                    agent.agent_id.yellow(),
                    agent.name.cyan(),
                    account.phone_number.as_deref().unwrap_or("-").into(),
                    account.business_account_id.as_deref().unwrap_or("-").into(),
                ]);
            }
        }
    }

    if !found_any {
        print_info("No WhatsApp accounts found connected to any agent");
        return Ok(());
    }

    println!("{}", table);
    print_success("WhatsApp accounts listed successfully");
    Ok(())
}

/// Get agent widget configuration
async fn get_agent_widget(client: &Client, api_key: &str, agent_id: &str) -> Result<()> {
    print_info(&format!(
        "Fetching widget configuration for agent '{}'...",
        agent_id
    ));

    let url = format!(
        "https://api.elevenlabs.io/v1/convai/agents/{}/widget/",
        agent_id
    );

    let response = client
        .get(&url)
        .header("xi-api-key", api_key)
        .send()
        .await
        .context("Failed to fetch widget configuration")?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    let widget: serde_json::Value = response.json().await.context("Failed to parse response")?;

    println!("\n{}", "Widget Configuration:".bold().underline());
    println!(
        "  Agent ID: {}",
        widget["agent_id"].as_str().unwrap_or("-").cyan()
    );

    if let Some(config) = widget.get("widget_config") {
        println!(
            "  Widget Enabled: {}",
            config["enabled"].as_bool().unwrap_or(false)
        );
        println!("  Position: {}", config["position"].as_str().unwrap_or("-"));

        if let Some(avatar) = config.get("avatar") {
            println!(
                "  Avatar URL: {}",
                avatar["url"].as_str().unwrap_or("-").green()
            );
        }

        if let Some(greeting) = config.get("greeting") {
            println!("  Greeting: {}", greeting.as_str().unwrap_or("-"));
        }
    }

    print_success("Widget configuration retrieved");
    Ok(())
}

/// Set agent widget avatar
async fn set_agent_widget_avatar(
    client: &Client,
    api_key: &str,
    agent_id: &str,
    avatar_file: &str,
) -> Result<()> {
    print_info(&format!(
        "Setting widget avatar for agent '{}' from '{}'...",
        agent_id, avatar_file
    ));

    let file_path = std::path::Path::new(avatar_file);
    if !file_path.exists() {
        return Err(anyhow::anyhow!("File not found: {}", avatar_file));
    }

    let file_name = file_path
        .file_name()
        .and_then(|n| n.to_str())
        .ok_or_else(|| anyhow::anyhow!("Invalid file name"))?;

    let mime = if avatar_file.ends_with(".png") {
        "image/png"
    } else if avatar_file.ends_with(".jpg") || avatar_file.ends_with(".jpeg") {
        "image/jpeg"
    } else if avatar_file.ends_with(".webp") {
        "image/webp"
    } else {
        "image/png"
    };

    let file_data = tokio::fs::read(avatar_file)
        .await
        .context("Failed to read avatar file")?;

    let part = reqwest::multipart::Part::bytes(file_data)
        .file_name(file_name.to_string())
        .mime_str(mime)
        .map_err(|e| anyhow::anyhow!("Invalid mime type: {}", e))?;

    let form = reqwest::multipart::Form::new().part("avatar_file", part);

    let url = format!(
        "https://api.elevenlabs.io/v1/convai/agents/{}/avatar",
        agent_id
    );

    let response = client
        .post(&url)
        .header("xi-api-key", api_key)
        .multipart(form)
        .send()
        .await
        .context("Failed to set widget avatar")?;

    if !response.status().is_success() {
        let error = response.text().await?;
        return Err(anyhow::anyhow!("API error: {}", error));
    }

    let result: serde_json::Value = response.json().await.context("Failed to parse response")?;

    println!("\n{}", "Widget Avatar:".bold().underline());
    println!(
        "  Agent ID: {}",
        result["agent_id"].as_str().unwrap_or("-").cyan()
    );
    println!(
        "  Avatar URL: {}",
        result["avatar_url"].as_str().unwrap_or("-").green()
    );

    print_success("Widget avatar set successfully");
    Ok(())
}