vtcode 0.107.0

A Rust-based terminal coding agent with modular architecture supporting multiple LLM providers
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
use anyhow::{Result, anyhow, bail};
use std::path::PathBuf;
use vtcode_core::constants::tools;
use vtcode_core::utils::ansi::MessageStyle;
use vtcode_tui::app::{
    AgentPaletteItem, InlineListItem, InlineListSearchConfig, InlineListSelection,
};

use super::ui::{ensure_selection_ui_available, wait_for_list_modal_selection};
use super::{SlashCommandContext, SlashCommandControl};
use crate::agent::runloop::slash_commands::{
    AgentDefinitionScope, AgentManagerAction, SubprocessManagerAction,
};

#[path = "agents_authoring.rs"]
mod authoring;
#[path = "agents/runtime.rs"]
mod runtime;

#[cfg(test)]
use runtime::{
    active_subagent_entries, background_subprocess_summary, subprocess_action_prompt,
    summarize_thread_event_preview, visible_subagent_entries,
};
use runtime::{
    apply_background_subprocess_action, close_subagent_entry, handle_list_subprocesses_text,
    handle_list_threads_text, render_active_agent_status_text, render_background_setup_guidance,
    render_background_subprocess_status_text, render_subprocess_status,
    show_active_agent_inspector, show_background_subprocess_inspector, show_threads_modal,
};

const AGENT_ACTION_PREFIX: &str = "agents:";
const AGENT_INSPECT_PREFIX: &str = "agents:inspect:";
const THREAD_INSPECT_PREFIX: &str = "agents:thread:";
const THREAD_TRANSCRIPT_PREFIX: &str = "agents:transcript:";
const THREAD_CANCEL_PREFIX: &str = "agents:cancel:";
const SUBPROCESS_TRANSCRIPT_PREFIX: &str = "subprocesses:transcript:";
const SUBPROCESS_ARCHIVE_PREFIX: &str = "subprocesses:archive:";
const SUBPROCESS_STOP_PREFIX: &str = "subprocesses:stop:";
const SUBPROCESS_CANCEL_PREFIX: &str = "subprocesses:cancel:";
const ACTIVE_AGENT_INSPECTOR_REFRESH_MS: u64 = 750;
const DEFAULT_AGENT_DESCRIPTION_TEXT: &str = "Describe when VT Code should delegate to this agent.";
const DEFAULT_AGENT_BODY_TEXT: &str = "\nYou are a focused VT Code subagent.\n\nScope:\n- Describe the tasks this agent should handle.\n- Keep behavior narrow and task-specific.\n\nConstraints:\n- Use VT Code tool ids in frontmatter such as `read_file`, `list_files`, `unified_search`, and `unified_exec`.\n- Prefer the narrowest tool set that fits the job.\n- Return concise, actionable results.\n\nOutput:\n- State what you checked.\n- Summarize findings or changes.\n- Call out verification or remaining risks when relevant.\n";
const DEFAULT_AGENT_TOOL_IDS: [&str; 3] =
    [tools::READ_FILE, tools::LIST_FILES, tools::UNIFIED_SEARCH];
const SUBAGENT_CONTROLLER_INACTIVE_MESSAGE: &str =
    "Subagent controller is not active in this session.";

pub(crate) async fn handle_manage_agents(
    mut ctx: SlashCommandContext<'_>,
    action: AgentManagerAction,
) -> Result<SlashCommandControl> {
    match action {
        AgentManagerAction::List => {
            if ctx.renderer.supports_inline_ui() {
                let mut ctx = ctx;
                if !ensure_selection_ui_available(&mut ctx, "opening subagent manager")? {
                    return Ok(SlashCommandControl::Continue);
                }
                show_agents_manager(ctx).await
            } else {
                let mut ctx = ctx;
                handle_list_agents_text(&mut ctx).await
            }
        }
        AgentManagerAction::Threads => {
            if ctx.renderer.supports_inline_ui() {
                let mut ctx = ctx;
                if !ensure_selection_ui_available(&mut ctx, "browsing delegated child threads")? {
                    return Ok(SlashCommandControl::Continue);
                }
                show_threads_modal(ctx).await
            } else {
                let mut ctx = ctx;
                handle_list_threads_text(&mut ctx).await
            }
        }
        AgentManagerAction::Create { scope, name } => {
            authoring::handle_create_agent(ctx, scope, name.as_deref()).await
        }
        AgentManagerAction::Inspect { id } => {
            let Some(controller) = ctx.tool_registry.subagent_controller() else {
                return render_missing_subagent_controller(&mut ctx);
            };
            let entry = controller.status_for(&id).await?;
            if ctx.renderer.supports_inline_ui() {
                let mut ctx = ctx;
                show_active_agent_inspector(&mut ctx, entry).await
            } else {
                let snapshot = controller.snapshot_for_thread(&id).await?;
                render_active_agent_status_text(&mut ctx, &entry, &snapshot)?;
                Ok(SlashCommandControl::Continue)
            }
        }
        AgentManagerAction::Close { id } => {
            let Some(controller) = ctx.tool_registry.subagent_controller() else {
                return render_missing_subagent_controller(&mut ctx);
            };
            let entry = controller.status_for(&id).await?;
            close_subagent_entry(&mut ctx, &controller, &entry.id, &entry.display_label).await
        }
        AgentManagerAction::Edit { name } => {
            authoring::handle_edit_agent(ctx, name.as_deref()).await
        }
        AgentManagerAction::Delete { name } => {
            let mut ctx = ctx;
            handle_delete_agent(&mut ctx, &name).await
        }
    }
}

pub(crate) async fn handle_manage_subprocesses(
    mut ctx: SlashCommandContext<'_>,
    action: SubprocessManagerAction,
) -> Result<SlashCommandControl> {
    let Some(controller) = ctx.tool_registry.subagent_controller() else {
        return render_missing_subagent_controller(&mut ctx);
    };

    match action {
        SubprocessManagerAction::ToggleDefault => {
            if !controller.background_subagents_enabled()
                || controller.configured_default_background_agent().is_none()
            {
                if ctx.renderer.supports_inline_ui() {
                    ctx.handle.show_local_agents();
                }
                render_background_setup_guidance(&mut ctx)?;
                return Ok(SlashCommandControl::Continue);
            }
            let entry = controller.toggle_default_background_subagent().await?;
            render_subprocess_status(&mut ctx, &entry)?;
            Ok(SlashCommandControl::Continue)
        }
        SubprocessManagerAction::Refresh => {
            let entries = controller.refresh_background_processes().await?;
            if entries.is_empty() {
                ctx.renderer
                    .line(MessageStyle::Info, "No managed background subprocesses.")?;
            } else {
                ctx.renderer.line(
                    MessageStyle::Info,
                    &format!("Refreshed {} background subprocesses.", entries.len()),
                )?;
            }
            Ok(SlashCommandControl::Continue)
        }
        SubprocessManagerAction::List => {
            if ctx.renderer.supports_inline_ui() {
                ctx.handle.show_local_agents();
                return Ok(SlashCommandControl::Continue);
            }
            handle_list_subprocesses_text(&mut ctx).await
        }
        SubprocessManagerAction::Inspect { id } => {
            let entry = controller.background_snapshot(&id).await?;
            if ctx.renderer.supports_inline_ui() {
                show_background_subprocess_inspector(&mut ctx, entry.entry).await
            } else {
                render_background_subprocess_status_text(&mut ctx, &entry)?;
                Ok(SlashCommandControl::Continue)
            }
        }
        SubprocessManagerAction::Stop { id } => {
            let entry =
                apply_background_subprocess_action(&mut ctx, &controller, &id, false).await?;
            render_subprocess_status(&mut ctx, &entry)?;
            Ok(SlashCommandControl::Continue)
        }
        SubprocessManagerAction::Cancel { id } => {
            let entry =
                apply_background_subprocess_action(&mut ctx, &controller, &id, true).await?;
            render_subprocess_status(&mut ctx, &entry)?;
            Ok(SlashCommandControl::Continue)
        }
    }
}

fn render_missing_subagent_controller(
    ctx: &mut SlashCommandContext<'_>,
) -> Result<SlashCommandControl> {
    ctx.renderer
        .line(MessageStyle::Info, SUBAGENT_CONTROLLER_INACTIVE_MESSAGE)?;
    Ok(SlashCommandControl::Continue)
}

async fn show_agents_manager(mut ctx: SlashCommandContext<'_>) -> Result<SlashCommandControl> {
    ctx.handle.show_list_modal(
        "Subagents".to_string(),
        vec![
            "Manage effective subagents, active agents, and custom definitions.".to_string(),
            "Use Enter to inspect, create, edit, or delete definitions.".to_string(),
        ],
        vec![
            action_item(
                "Browse agents",
                "List effective and shadowed definitions with source badges",
                Some("Recommended"),
                "browse effective shadowed agents",
                "browse",
            ),
            action_item(
                "Browse active agents",
                "Inspect delegated runs without switching the main session",
                None,
                "active agents delegated inspector",
                "threads",
            ),
            action_item(
                "Create project agent",
                "Guided flow for `.vtcode/agents/<name>.md` with VT Code-native frontmatter",
                Some("Project"),
                "create project agent guided authoring",
                "create-project",
            ),
            action_item(
                "Create user agent",
                "Guided flow for `~/.vtcode/agents/<name>.md` with VT Code-native frontmatter",
                Some("User"),
                "create user agent guided authoring",
                "create-user",
            ),
            action_item(
                "Edit custom agent",
                "Guided editor for native `.vtcode` agents; imported files still open in your editor",
                None,
                "edit custom agent guided authoring",
                "edit",
            ),
            action_item(
                "Delete custom agent",
                "Pick a project or user agent file and remove it",
                None,
                "delete custom agent file",
                "delete",
            ),
        ],
        Some(InlineListSelection::ConfigAction(format!(
            "{AGENT_ACTION_PREFIX}browse"
        ))),
        Some(InlineListSearchConfig {
            label: "Search subagent actions".to_string(),
            placeholder: Some("browse, create, edit, thread".to_string()),
        }),
    );

    let Some(selection) = wait_for_list_modal_selection(&mut ctx).await else {
        return Ok(SlashCommandControl::Continue);
    };
    let InlineListSelection::ConfigAction(action) = selection else {
        return Ok(SlashCommandControl::Continue);
    };

    match action.as_str() {
        value if value == format!("{AGENT_ACTION_PREFIX}browse") => show_agent_catalog(ctx).await,
        value if value == format!("{AGENT_ACTION_PREFIX}threads") => show_threads_modal(ctx).await,
        value if value == format!("{AGENT_ACTION_PREFIX}create-project") => {
            authoring::handle_create_agent(ctx, Some(AgentDefinitionScope::Project), None).await
        }
        value if value == format!("{AGENT_ACTION_PREFIX}create-user") => {
            authoring::handle_create_agent(ctx, Some(AgentDefinitionScope::User), None).await
        }
        value if value == format!("{AGENT_ACTION_PREFIX}edit") => {
            authoring::handle_edit_agent(ctx, None).await
        }
        value if value == format!("{AGENT_ACTION_PREFIX}delete") => {
            let Some(name) = select_custom_agent_name(&mut ctx, "Delete custom agent").await?
            else {
                return Ok(SlashCommandControl::Continue);
            };
            if confirm_delete_agent(&mut ctx, &name).await? {
                handle_delete_agent(&mut ctx, &name).await
            } else {
                Ok(SlashCommandControl::Continue)
            }
        }
        _ => Ok(SlashCommandControl::Continue),
    }
}

async fn show_agent_catalog(mut ctx: SlashCommandContext<'_>) -> Result<SlashCommandControl> {
    let Some(controller) = ctx.tool_registry.subagent_controller() else {
        return render_missing_subagent_controller(&mut ctx);
    };

    let specs = controller.effective_specs().await;
    let shadowed = controller.shadowed_specs().await;
    if specs.is_empty() && shadowed.is_empty() {
        ctx.renderer.line(
            MessageStyle::Info,
            "No subagent definitions are currently loaded.",
        )?;
        return Ok(SlashCommandControl::Continue);
    }

    let mut items = Vec::new();
    for spec in &specs {
        items.push(InlineListItem {
            title: spec.name.clone(),
            subtitle: Some(agent_subtitle(spec, false)),
            badge: Some(agent_badge(spec)),
            indent: 0,
            selection: Some(InlineListSelection::ConfigAction(format!(
                "{AGENT_INSPECT_PREFIX}{}",
                spec.name
            ))),
            search_value: Some(format!(
                "{} {} {}",
                spec.name,
                spec.description,
                spec.source.label()
            )),
        });
    }
    for spec in &shadowed {
        items.push(InlineListItem {
            title: format!("{} (shadowed)", spec.name),
            subtitle: Some(agent_subtitle(spec, true)),
            badge: Some("Shadowed".to_string()),
            indent: 0,
            selection: None,
            search_value: Some(format!(
                "{} shadowed {} {}",
                spec.name,
                spec.description,
                spec.source.label()
            )),
        });
    }

    let selected = items.iter().find_map(|item| item.selection.clone());
    ctx.handle.show_list_modal(
        "Loaded subagents".to_string(),
        vec![
            format!(
                "{} effective definition(s), {} shadowed definition(s).",
                specs.len(),
                shadowed.len()
            ),
            "Select an effective definition to inspect details.".to_string(),
        ],
        items,
        selected,
        Some(InlineListSearchConfig {
            label: "Search subagents".to_string(),
            placeholder: Some("name, source, description".to_string()),
        }),
    );

    let Some(selection) = wait_for_list_modal_selection(&mut ctx).await else {
        return Ok(SlashCommandControl::Continue);
    };
    let InlineListSelection::ConfigAction(action) = selection else {
        return Ok(SlashCommandControl::Continue);
    };
    let Some(name) = action.strip_prefix(AGENT_INSPECT_PREFIX) else {
        return Ok(SlashCommandControl::Continue);
    };
    let spec = specs
        .into_iter()
        .find(|spec| spec.name == name)
        .ok_or_else(|| anyhow!("Unknown agent {}", name))?;
    render_agent_details(
        &mut ctx,
        &spec,
        shadowed.iter().filter(|entry| entry.name == name).count(),
    )?;
    Ok(SlashCommandControl::Continue)
}

async fn handle_list_agents_text(ctx: &mut SlashCommandContext<'_>) -> Result<SlashCommandControl> {
    let Some(controller) = ctx.tool_registry.subagent_controller() else {
        ctx.renderer.line(
            MessageStyle::Info,
            "Subagent controller is not active in this session.",
        )?;
        return Ok(SlashCommandControl::Continue);
    };

    let specs = controller.effective_specs().await;
    let shadowed = controller.shadowed_specs().await;
    let threads = controller.status_entries().await;

    ctx.renderer.line(
        MessageStyle::Info,
        &format!(
            "Loaded {} effective subagents ({} shadowed definitions).",
            specs.len(),
            shadowed.len()
        ),
    )?;
    for spec in specs {
        ctx.renderer.line(
            MessageStyle::Output,
            &format!("{} {}", spec.name, agent_subtitle(&spec, false)),
        )?;
    }

    if threads.is_empty() {
        ctx.renderer
            .line(MessageStyle::Info, "No delegated child threads yet.")?;
    } else {
        ctx.renderer.line(
            MessageStyle::Info,
            &format!("{} delegated child thread(s):", threads.len()),
        )?;
        for entry in threads {
            ctx.renderer.line(
                MessageStyle::Output,
                &format!(
                    "{} {} {}",
                    entry.id,
                    entry.agent_name,
                    status_label(entry.status)
                ),
            )?;
        }
    }

    Ok(SlashCommandControl::Continue)
}

async fn legacy_create_agent_scaffold(
    ctx: &mut SlashCommandContext<'_>,
    scope: AgentDefinitionScope,
    name: &str,
) -> Result<SlashCommandControl> {
    validate_agent_name(name)?;
    let path = match scope {
        AgentDefinitionScope::Project => ctx
            .config
            .workspace
            .join(".vtcode/agents")
            .join(format!("{name}.md")),
        AgentDefinitionScope::User => dirs::home_dir()
            .ok_or_else(|| anyhow!("Cannot resolve home directory for user-scope agent"))?
            .join(".vtcode/agents")
            .join(format!("{name}.md")),
    };

    if path.exists() {
        bail!("Agent file already exists at {}", path.display());
    }

    std::fs::create_dir_all(
        path.parent()
            .ok_or_else(|| anyhow!("Invalid agent destination {}", path.display()))?,
    )?;
    std::fs::write(&path, scaffold_agent_markdown(name))?;

    if let Some(controller) = ctx.tool_registry.subagent_controller() {
        let _ = controller.reload().await;
        refresh_agent_palette(ctx.handle, controller.as_ref()).await;
    }

    ctx.renderer.line(
        MessageStyle::Info,
        &format!(
            "Created agent scaffold at {} with VT Code-native subagent frontmatter.",
            path.display()
        ),
    )?;
    Ok(SlashCommandControl::Continue)
}

async fn legacy_open_agent_editor(
    ctx: SlashCommandContext<'_>,
    name: &str,
) -> Result<SlashCommandControl> {
    let path = resolve_custom_agent_path(&ctx, name).await?;
    super::apps::handle_launch_editor(ctx, Some(path.display().to_string())).await
}

async fn handle_delete_agent(
    ctx: &mut SlashCommandContext<'_>,
    name: &str,
) -> Result<SlashCommandControl> {
    let path = resolve_custom_agent_path(ctx, name).await?;
    std::fs::remove_file(&path)?;
    if let Some(controller) = ctx.tool_registry.subagent_controller() {
        let _ = controller.reload().await;
        refresh_agent_palette(ctx.handle, controller.as_ref()).await;
    }
    ctx.renderer.line(
        MessageStyle::Info,
        &format!("Deleted agent definition {}.", path.display()),
    )?;
    Ok(SlashCommandControl::Continue)
}

async fn select_custom_agent_name(
    ctx: &mut SlashCommandContext<'_>,
    title: &str,
) -> Result<Option<String>> {
    let controller = ctx
        .tool_registry
        .subagent_controller()
        .ok_or_else(|| anyhow!("Subagent controller is not active in this session"))?;
    let specs = controller
        .effective_specs()
        .await
        .into_iter()
        .filter(|spec| spec.file_path.is_some())
        .collect::<Vec<_>>();
    if specs.is_empty() {
        ctx.renderer.line(
            MessageStyle::Info,
            "No editable custom agents are currently loaded.",
        )?;
        return Ok(None);
    }

    let items = specs
        .iter()
        .map(|spec| InlineListItem {
            title: spec.name.clone(),
            subtitle: Some(agent_subtitle(spec, false)),
            badge: Some(agent_badge(spec)),
            indent: 0,
            selection: Some(InlineListSelection::ConfigAction(format!(
                "{AGENT_INSPECT_PREFIX}{}",
                spec.name
            ))),
            search_value: Some(format!(
                "{} {} {}",
                spec.name,
                spec.description,
                spec.source.label()
            )),
        })
        .collect::<Vec<_>>();
    let selected = items.first().and_then(|item| item.selection.clone());
    ctx.handle.show_list_modal(
        title.to_string(),
        vec!["Select a project or user-scope agent definition.".to_string()],
        items,
        selected,
        Some(InlineListSearchConfig {
            label: "Search custom agents".to_string(),
            placeholder: Some("name, description, source".to_string()),
        }),
    );

    let Some(selection) = wait_for_list_modal_selection(ctx).await else {
        return Ok(None);
    };
    let InlineListSelection::ConfigAction(action) = selection else {
        return Ok(None);
    };
    Ok(action
        .strip_prefix(AGENT_INSPECT_PREFIX)
        .map(ToString::to_string))
}

async fn confirm_delete_agent(ctx: &mut SlashCommandContext<'_>, name: &str) -> Result<bool> {
    ctx.handle.show_list_modal(
        "Delete custom agent".to_string(),
        vec![format!(
            "Delete `{name}` from disk? This cannot be undone automatically."
        )],
        vec![
            InlineListItem {
                title: "Delete agent".to_string(),
                subtitle: Some("Remove the selected definition file".to_string()),
                badge: Some("Confirm".to_string()),
                indent: 0,
                selection: Some(InlineListSelection::ConfigAction(
                    "agents:confirm-delete".to_string(),
                )),
                search_value: Some("confirm delete".to_string()),
            },
            InlineListItem {
                title: "Cancel".to_string(),
                subtitle: Some("Keep the agent definition".to_string()),
                badge: None,
                indent: 0,
                selection: Some(InlineListSelection::ConfigAction(
                    "agents:cancel-delete".to_string(),
                )),
                search_value: Some("cancel".to_string()),
            },
        ],
        Some(InlineListSelection::ConfigAction(
            "agents:cancel-delete".to_string(),
        )),
        None,
    );
    let Some(selection) = wait_for_list_modal_selection(ctx).await else {
        return Ok(false);
    };
    Ok(matches!(
        selection,
        InlineListSelection::ConfigAction(action) if action == "agents:confirm-delete"
    ))
}

async fn resolve_custom_agent_path(ctx: &SlashCommandContext<'_>, name: &str) -> Result<PathBuf> {
    let controller = ctx
        .tool_registry
        .subagent_controller()
        .ok_or_else(|| anyhow!("Subagent controller is not active in this session"))?;
    let spec = controller
        .effective_specs()
        .await
        .into_iter()
        .find(|spec| spec.matches_name(name))
        .ok_or_else(|| anyhow!("Unknown agent {}", name))?;
    let path = spec.file_path.ok_or_else(|| {
        anyhow!(
            "Agent {} is built-in or plugin-provided and cannot be edited here",
            name
        )
    })?;
    Ok(path)
}

fn render_agent_details(
    ctx: &mut SlashCommandContext<'_>,
    spec: &vtcode_config::SubagentSpec,
    shadowed_count: usize,
) -> Result<()> {
    ctx.renderer.line(
        MessageStyle::Info,
        &format!("{} [{}]", spec.name, spec.source.label()),
    )?;
    ctx.renderer.line(
        MessageStyle::Output,
        &format!("Description: {}", spec.description),
    )?;
    ctx.renderer.line(
        MessageStyle::Output,
        &format!(
            "Mode: {}",
            if spec.is_read_only() {
                "read-only"
            } else {
                "write-capable"
            }
        ),
    )?;
    if let Some(path) = spec.file_path.as_ref() {
        ctx.renderer
            .line(MessageStyle::Output, &format!("File: {}", path.display()))?;
    }
    if shadowed_count > 0 {
        ctx.renderer.line(
            MessageStyle::Info,
            &format!("{shadowed_count} lower-priority definition(s) are shadowed."),
        )?;
    }
    for warning in &spec.warnings {
        ctx.renderer
            .line(MessageStyle::Warning, &format!("Warning: {}", warning))?;
    }
    Ok(())
}

fn validate_agent_name(name: &str) -> Result<()> {
    let trimmed = name.trim();
    if trimmed.is_empty() {
        bail!("Agent name cannot be empty");
    }
    if !trimmed
        .chars()
        .all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '-')
    {
        bail!("Agent name must use lowercase letters, digits, or hyphens");
    }
    Ok(())
}

fn scaffold_agent_markdown(name: &str) -> String {
    format!(
        "---\nname: {name}\ndescription: {description}\ntools:\n  - {read_file}\n  - {list_files}\n  - {unified_search}\nmodel: inherit\ncolor: blue\nreasoning_effort: medium\n---\n{body}",
        description = DEFAULT_AGENT_DESCRIPTION_TEXT,
        read_file = DEFAULT_AGENT_TOOL_IDS[0],
        list_files = DEFAULT_AGENT_TOOL_IDS[1],
        unified_search = DEFAULT_AGENT_TOOL_IDS[2],
        body = DEFAULT_AGENT_BODY_TEXT,
    )
}

fn action_item(
    title: &str,
    subtitle: &str,
    badge: Option<&str>,
    search_value: &str,
    action: &str,
) -> InlineListItem {
    InlineListItem {
        title: title.to_string(),
        subtitle: Some(subtitle.to_string()),
        badge: badge.map(ToString::to_string),
        indent: 0,
        selection: Some(InlineListSelection::ConfigAction(format!(
            "{AGENT_ACTION_PREFIX}{action}"
        ))),
        search_value: Some(search_value.to_string()),
    }
}

fn agent_badge(spec: &vtcode_config::SubagentSpec) -> String {
    match spec.file_path {
        Some(_) => spec.source.label().to_string(),
        None => "Built-in".to_string(),
    }
}

fn agent_subtitle(spec: &vtcode_config::SubagentSpec, shadowed: bool) -> String {
    let mut parts = vec![spec.source.label().to_string(), spec.description.clone()];
    if spec.is_read_only() {
        parts.push("read-only".to_string());
    }
    if shadowed {
        parts.push("shadowed".to_string());
    }
    parts.join(" | ")
}

fn status_label(status: vtcode_core::subagents::SubagentStatus) -> &'static str {
    match status {
        vtcode_core::subagents::SubagentStatus::Queued => "[queued]",
        vtcode_core::subagents::SubagentStatus::Running => "[running]",
        vtcode_core::subagents::SubagentStatus::Waiting => "[waiting]",
        vtcode_core::subagents::SubagentStatus::Completed => "[completed]",
        vtcode_core::subagents::SubagentStatus::Failed => "[failed]",
        vtcode_core::subagents::SubagentStatus::Closed => "[closed]",
    }
}

async fn refresh_agent_palette(
    handle: &vtcode_tui::app::InlineHandle,
    controller: &vtcode_core::subagents::SubagentController,
) {
    let specs = controller.effective_specs().await;
    handle.configure_agent_palette(
        specs
            .into_iter()
            .map(|spec| AgentPaletteItem {
                name: spec.name,
                description: Some(spec.description),
            })
            .collect(),
    );
}

#[cfg(test)]
#[path = "agents/tests.rs"]
mod tests;