Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

36.6 KB
Raw Blame
use crate::client::HttpClient;
use crate::error::{Error, Result};
use crate::output::{OutputFormat, output_success};
use crate::terminal_ui::{
    RenderedRegion as RenderedSkillSelectRegion, clear_rendered_lines, live_select_block,
    truncate_to_display_width,
};
use crate::theme;
use colored::Colorize;
use serde_json::{Value, json};
use std::collections::BTreeSet;
use std::io::{self, IsTerminal, Write};
use std::path::Path;

#[derive(Debug, Clone)]
struct InstalledSkillSummary {
    name: String,
    description: String,
}

pub async fn add(
    client: &HttpClient,
    data: &str,
    skill_names: Vec<String>,
    list_only: bool,
    wait: bool,
    yes: bool,
    show_progress: bool,
    verbose: bool,
    output_format: OutputFormat,
    compact: bool,
    parent: Option<&str>,
) -> Result<()> {
    if list_only || (!yes && !data.contains('\n')) {
        let listing = client
            .add_skill(
                data,
                false,
                show_progress,
                verbose,
                None,
                parent,
                &skill_names,
                true,
            )
            .await?;
        if list_only {
            output_success(listing, output_format, compact);
            return Ok(());
        }
        let names = listing["skills"]
            .as_array()
            .ok_or_else(|| Error::Parse("Invalid skill source listing".into()))?
            .iter()
            .filter_map(|skill| skill["path"].as_str().map(String::from))
            .collect::<Vec<_>>();
        if names.len() > 1 && !confirm_action("Install", &names)? {
            output_message_result(
                json!({"cancelled": true, "skills": names}),
                "Aborted.".into(),
                output_format,
                compact,
            );
            return Ok(());
        }
    }
    let result = client
        .add_skill(
            data,
            wait,
            show_progress,
            verbose,
            None,
            parent,
            &skill_names,
            false,
        )
        .await?;

    if !wait && matches!(output_format, OutputFormat::Table) {
        eprintln!("Note: Skill processing may continue in the background.");
        eprintln!(
            "Use 'ov task status <task_id>' to check progress, or 'ov task list' to see all tasks."
        );
    }

    output_success(result, output_format, compact);
    Ok(())
}

pub async fn list(
    client: &HttpClient,
    node_limit: i32,
    output_format: OutputFormat,
    compact: bool,
    parent: Option<&str>,
) -> Result<()> {
    let result = client.skills_list(node_limit, parent).await?;
    super::search::output_skills_list_results(&result, output_format, compact);
    Ok(())
}

pub async fn show(
    client: &HttpClient,
    name: &str,
    level: Option<i32>,
    include_files: bool,
    include_source: bool,
    output_format: OutputFormat,
    compact: bool,
    parent: Option<&str>,
) -> Result<()> {
    let include_content = level.is_none() || level == Some(2);
    let mut result = client
        .skill_show(
            name,
            include_content,
            include_files,
            include_source,
            level,
            parent,
        )
        .await?;
    if let Some(level) = level {
        filter_skill_show_level(&mut result, level);
    }
    output_skill_show(&result, output_format, compact);
    Ok(())
}

pub async fn find(
    client: &HttpClient,
    query: &str,
    node_limit: i32,
    threshold: Option<f64>,
    level: Option<Vec<i32>>,
    output_format: OutputFormat,
    compact: bool,
    parent: Option<&str>,
) -> Result<()> {
    let result = client
        .skill_find(query, node_limit, threshold, level, parent)
        .await?;
    super::search::output_skills_find_results(&result, output_format, compact, node_limit);
    Ok(())
}

pub async fn update(
    client: &HttpClient,
    skill_names: Vec<String>,
    wait: bool,
    yes: bool,
    output_format: OutputFormat,
    compact: bool,
    parent: Option<&str>,
) -> Result<()> {
    let update_all = skill_names.is_empty();
    let names = resolve_installed_skill_names(client, skill_names).await?;
    if names.is_empty() {
        output_message_result(
            serde_json::json!({ "updated": [], "total": 0 }),
            "No skills to update.".to_string(),
            output_format,
            compact,
        );
        return Ok(());
    }
    if !yes && !confirm_action("Update", &names)? {
        output_message_result(
            serde_json::json!({ "cancelled": true, "skills": names }),
            "Aborted.".to_string(),
            output_format,
            compact,
        );
        return Ok(());
    }

    let mut updated = Vec::new();
    let mut skipped = Vec::new();
    for name in names {
        let result = async {
            let detail = client.skill_show(&name, false, false, true, Some(0), parent).await?;
            if detail["source"]["type"].as_str() == Some("git") {
                let mut body = json!({"from_source": true, "wait": wait});
                if let Some(parent) = parent {
                    body["target_uri"] = json!(parent);
                }
                client.put(&format!("/api/v1/skills/{}", name), &body).await
            } else if !update_all && can_prompt() {
                print!("Source for skill '{}' is not a Git source. Enter a local path or Git URL (blank to abort): ", name);
                io::stdout().flush()?;
                let mut input = String::new();
                io::stdin().read_line(&mut input)?;
                let data = input.trim();
                if data.is_empty() {
                    return Err(Error::Client("No update source supplied.".into()));
                }
                client.skill_update(&name, data, wait, false, false, None, parent).await
            } else {
                Err(Error::Client(format!("Skill '{}' has no recorded Git source; supply a local path or Git URL interactively.", name)))
            }
        }.await;
        match result {
            Ok(result) => updated.push(result),
            Err(error) if update_all => {
                skipped.push(json!({"name": name, "reason": error.to_string()}))
            }
            Err(error) => return Err(error),
        }
    }
    let total = updated.len();
    let skipped_total = skipped.len();
    output_success(
        serde_json::json!({
            "updated": updated,
            "total": total,
            "skipped": skipped,
            "skipped_total": skipped_total,
        }),
        output_format,
        compact,
    );
    Ok(())
}

pub async fn remove(
    client: &HttpClient,
    skill_names: Vec<String>,
    all: bool,
    yes: bool,
    output_format: OutputFormat,
    compact: bool,
    parent: Option<&str>,
) -> Result<()> {
    if all && !skill_names.is_empty() {
        return Err(Error::Client(
            "Pass either skill names or --all, not both.".to_string(),
        ));
    }
    let requested_names = normalize_skill_names(skill_names)?;
    let names = if all {
        resolve_installed_skill_names(client, Vec::new()).await?
    } else if !requested_names.is_empty() {
        requested_names
    } else {
        if yes || !can_prompt() {
            return Err(Error::Client(
                "Specify at least one skill name, or pass --all.".to_string(),
            ));
        }
        match prompt_remove_skill_selection(client).await? {
            Some(selected) => selected,
            None => {
                output_message_result(
                    serde_json::json!({ "cancelled": true, "skills": [] }),
                    "Aborted.".to_string(),
                    output_format,
                    compact,
                );
                return Ok(());
            }
        }
    };
    if names.is_empty() {
        output_message_result(
            serde_json::json!({ "removed": [], "total": 0 }),
            "No skills to remove.".to_string(),
            output_format,
            compact,
        );
        return Ok(());
    }
    if !yes && !confirm_action("Remove", &names)? {
        output_message_result(
            serde_json::json!({ "cancelled": true, "skills": names }),
            "Aborted.".to_string(),
            output_format,
            compact,
        );
        return Ok(());
    }

    let removed_names = names.clone();
    let mut removed = Vec::new();
    for name in names {
        removed.push(client.skill_remove(&name, parent).await?);
    }
    let total = removed.len();
    output_message_result(
        serde_json::json!({
            "removed": removed,
            "removed_names": removed_names,
            "total": total,
        }),
        format!(
            "Removed {} skill(s): {}.",
            total,
            format_name_list(&removed_names)
        ),
        output_format,
        compact,
    );
    Ok(())
}

pub async fn validate(
    _client: &HttpClient,
    path: &str,
    strict: bool,
    output_format: OutputFormat,
    compact: bool,
) -> Result<()> {
    let result = validate_skill_path(path, strict)?;
    if result
        .get("valid")
        .and_then(Value::as_bool)
        .is_some_and(|valid| !valid)
    {
        let errors = result
            .get("errors")
            .and_then(Value::as_array)
            .map(|items| {
                items
                    .iter()
                    .filter_map(|item| {
                        item.as_str()
                            .or_else(|| item.get("message").and_then(Value::as_str))
                    })
                    .collect::<Vec<_>>()
                    .join("; ")
            })
            .filter(|text| !text.is_empty())
            .unwrap_or_else(|| "Skill validation failed".to_string());
        if matches!(output_format, OutputFormat::Json) {
            output_success(result, output_format, compact);
        }
        return Err(Error::Client(errors));
    }
    output_skill_validate_success(&result, output_format, compact);
    Ok(())
}

fn validate_skill_path(path: &str, strict: bool) -> Result<Value> {
    let path_obj = Path::new(path);
    if !path_obj.exists() {
        return Err(Error::Client(format!(
            "Skill path '{}' does not exist.",
            path
        )));
    }

    let skill_file = if path_obj.is_dir() {
        let skill_file = path_obj.join("SKILL.md");
        if !skill_file.is_file() {
            return Err(Error::Client(format!(
                "SKILL.md not found in '{}'.",
                path_obj.display()
            )));
        }
        skill_file
    } else if path_obj.is_file() {
        if path_obj.file_name().and_then(|name| name.to_str()) != Some("SKILL.md") {
            return Err(Error::Client(
                "Validate expects a SKILL.md file or a skill directory.".to_string(),
            ));
        }
        path_obj.to_path_buf()
    } else {
        return Err(Error::Client(format!(
            "Skill path '{}' is not a file or directory.",
            path
        )));
    };

    let content = std::fs::read_to_string(&skill_file).map_err(|e| {
        Error::Client(format!(
            "Failed to read skill file '{}': {}",
            skill_file.display(),
            e
        ))
    })?;
    let skill_dir_name = skill_file
        .parent()
        .and_then(|parent| parent.file_name())
        .and_then(|name| name.to_str())
        .unwrap_or("")
        .to_string();

    Ok(validate_skill_content(
        &content,
        strict,
        &skill_file.to_string_lossy(),
        &skill_dir_name,
    ))
}

fn validate_skill_content(
    content: &str,
    strict: bool,
    source_path: &str,
    skill_dir_name: &str,
) -> Value {
    let mut errors = Vec::new();
    let mut warnings = Vec::new();

    let parsed = match parse_skill_md(content) {
        Ok(parsed) => parsed,
        Err(message) => {
            return json!({
                "valid": false,
                "strict": strict,
                "message": "Skill validation failed.",
                "name": "",
                "description": "",
                "tags": [],
                "allowed_tools": [],
                "body_lines": 0,
                "source_path": source_path,
                "skill_dir_name": skill_dir_name,
                "errors": [validation_issue("yaml_format", &message, "data")],
                "warnings": [],
            });
        }
    };

    let name = yaml_mapping_get_str(&parsed.meta, "name")
        .unwrap_or("")
        .trim()
        .to_string();
    let description = yaml_mapping_get_str(&parsed.meta, "description")
        .unwrap_or("")
        .trim()
        .to_string();
    let tags = yaml_value_as_string_array(yaml_mapping_get(&parsed.meta, "tags"));
    let allowed_tools = yaml_value_as_string_array(
        yaml_mapping_get(&parsed.meta, "allowed_tools")
            .or_else(|| yaml_mapping_get(&parsed.meta, "allowed-tools")),
    );

    if name.is_empty() {
        errors.push(validation_issue(
            "name_required",
            "name is required",
            "name",
        ));
    }
    if description.is_empty() {
        errors.push(validation_issue(
            "description_required",
            "description is required",
            "description",
        ));
    }

    if !name.is_empty() {
        if !skill_dir_name.is_empty() && name != skill_dir_name {
            push_mode_issue(
                &mut errors,
                &mut warnings,
                strict,
                "name_matches_directory",
                &format!(
                    "name '{}' does not match directory name '{}'",
                    name, skill_dir_name
                ),
                "name",
            );
        }
        if name.chars().count() > 64 {
            push_mode_issue(
                &mut errors,
                &mut warnings,
                strict,
                "name_max_length",
                "name must not exceed 64 characters",
                "name",
            );
        }
        if !is_valid_skill_name(&name) {
            push_mode_issue(
                &mut errors,
                &mut warnings,
                strict,
                "name_allowed_characters",
                "name may only contain letters, numbers, underscores, and hyphens",
                "name",
            );
        }
    }

    if description.chars().count() > 1024 {
        push_mode_issue(
            &mut errors,
            &mut warnings,
            strict,
            "description_max_length",
            "description must not exceed 1024 characters",
            "description",
        );
    }

    let body_lines = parsed.body.lines().count();
    if strict && body_lines > 500 {
        warnings.push(validation_issue(
            "body_max_lines",
            "SKILL.md body exceeds 500 lines",
            "content",
        ));
    }

    json!({
        "valid": errors.is_empty(),
        "strict": strict,
        "message": if errors.is_empty() { "Skill validation succeeded." } else { "Skill validation failed." },
        "name": name,
        "description": description,
        "tags": tags,
        "allowed_tools": allowed_tools,
        "body_lines": body_lines,
        "source_path": source_path,
        "skill_dir_name": skill_dir_name,
        "errors": errors,
        "warnings": warnings,
    })
}

struct ParsedSkillMd {
    meta: serde_yaml::Mapping,
    body: String,
}

fn parse_skill_md(content: &str) -> std::result::Result<ParsedSkillMd, String> {
    let first_line_end = content
        .find('\n')
        .ok_or_else(|| "SKILL.md must have YAML frontmatter".to_string())?;
    if !is_frontmatter_delimiter(&content[..first_line_end]) {
        return Err("SKILL.md must have YAML frontmatter".to_string());
    }

    let frontmatter_start = first_line_end + 1;
    let mut line_start = frontmatter_start;
    let (frontmatter, body) = loop {
        let Some(relative_line_end) = content[line_start..].find('\n') else {
            return Err("SKILL.md must have closing YAML frontmatter delimiter".to_string());
        };
        let line_end = line_start + relative_line_end;
        if is_frontmatter_delimiter(&content[line_start..line_end]) {
            break (
                &content[frontmatter_start..line_start],
                &content[line_end + 1..],
            );
        }
        line_start = line_end + 1;
    };
    let meta: serde_yaml::Value = serde_yaml::from_str(frontmatter)
        .map_err(|e| format!("Invalid YAML frontmatter: {}", e))?;
    let Some(meta) = meta.as_mapping().cloned() else {
        return Err("Invalid YAML frontmatter".to_string());
    };
    Ok(ParsedSkillMd {
        meta,
        body: body.trim().to_string(),
    })
}

fn is_frontmatter_delimiter(line: &str) -> bool {
    let line = line.strip_suffix('\r').unwrap_or(line);
    line.strip_prefix("---")
        .is_some_and(|suffix| suffix.chars().all(char::is_whitespace))
}

fn yaml_mapping_get<'a>(
    mapping: &'a serde_yaml::Mapping,
    key: &str,
) -> Option<&'a serde_yaml::Value> {
    mapping.get(serde_yaml::Value::String(key.to_string()))
}

fn yaml_mapping_get_str<'a>(mapping: &'a serde_yaml::Mapping, key: &str) -> Option<&'a str> {
    yaml_mapping_get(mapping, key).and_then(serde_yaml::Value::as_str)
}

fn yaml_value_as_string_array(value: Option<&serde_yaml::Value>) -> Vec<String> {
    match value {
        Some(serde_yaml::Value::Sequence(items)) => items
            .iter()
            .filter_map(serde_yaml::Value::as_str)
            .map(ToString::to_string)
            .collect(),
        Some(value) => value
            .as_str()
            .map(|value| vec![value.to_string()])
            .unwrap_or_default(),
        None => Vec::new(),
    }
}

fn is_valid_skill_name(name: &str) -> bool {
    name.chars()
        .all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '-')
}

fn validation_issue(rule: &str, message: &str, field: &str) -> Value {
    json!({
        "rule": rule,
        "message": message,
        "field": field,
    })
}

fn push_mode_issue(
    errors: &mut Vec<Value>,
    warnings: &mut Vec<Value>,
    strict: bool,
    rule: &str,
    message: &str,
    field: &str,
) {
    let issue = validation_issue(rule, message, field);
    if strict {
        errors.push(issue);
    } else {
        warnings.push(issue);
    }
}

fn output_skill_validate_success(result: &Value, output_format: OutputFormat, compact: bool) {
    if matches!(output_format, OutputFormat::Json) {
        output_success(result, output_format, compact);
        return;
    }

    let name = result.get("name").and_then(Value::as_str).unwrap_or("");
    let description = result
        .get("description")
        .and_then(Value::as_str)
        .unwrap_or("");
    let mut lines = vec![
        "Skill validation succeeded.".to_string(),
        format!("name: {}", name),
        format!("description: {}", description),
    ];

    if let Some(warnings) = result.get("warnings").and_then(Value::as_array)
        && !warnings.is_empty()
    {
        lines.push(String::new());
        lines.push("warnings:".to_string());
        for warning in warnings {
            let message = warning
                .get("message")
                .and_then(Value::as_str)
                .unwrap_or("Validation warning");
            lines.push(format!("- {}", message));
        }
    }

    println!("{}", lines.join("\n"));
}

fn normalize_skill_names(names: Vec<String>) -> Result<Vec<String>> {
    let mut seen = BTreeSet::new();
    let mut out = Vec::new();
    for name in names {
        let trimmed = name.trim();
        if trimmed.is_empty() {
            return Err(Error::Client("Skill name cannot be empty.".to_string()));
        }
        if seen.insert(trimmed.to_string()) {
            out.push(trimmed.to_string());
        }
    }
    Ok(out)
}

async fn resolve_installed_skill_names(
    client: &HttpClient,
    requested: Vec<String>,
) -> Result<Vec<String>> {
    let requested = normalize_skill_names(requested)?;
    if !requested.is_empty() {
        return Ok(requested);
    }

    Ok(list_installed_skills(client)
        .await?
        .into_iter()
        .map(|skill| skill.name)
        .collect())
}

async fn list_installed_skills(client: &HttpClient) -> Result<Vec<InstalledSkillSummary>> {
    let result = client.skills_list(10000, None).await?;
    let skills = result
        .get("skills")
        .and_then(Value::as_array)
        .ok_or_else(|| Error::Parse("skills list response missing skills array".to_string()))?
        .iter()
        .filter_map(|item| {
            let name = item.get("name").and_then(Value::as_str)?.to_string();
            let description = item
                .get("description")
                .and_then(Value::as_str)
                .unwrap_or("")
                .trim()
                .to_string();
            Some(InstalledSkillSummary { name, description })
        })
        .collect();
    Ok(skills)
}

async fn prompt_remove_skill_selection(client: &HttpClient) -> Result<Option<Vec<String>>> {
    let skills = list_installed_skills(client).await?;
    if skills.is_empty() {
        return Ok(Some(Vec::new()));
    }
    prompt_multi_select_skills("Select skill(s) to remove", &skills)
}

fn can_prompt() -> bool {
    io::stdin().is_terminal() && io::stdout().is_terminal()
}

fn prompt_multi_select_skills(
    prompt: &str,
    skills: &[InstalledSkillSummary],
) -> Result<Option<Vec<String>>> {
    use crossterm::{
        cursor,
        event::{self, Event, KeyCode, KeyEventKind, KeyModifiers},
        execute, terminal,
    };

    if skills.is_empty() {
        return Ok(Some(Vec::new()));
    }

    struct RawGuard {
        hide_cursor: bool,
    }

    impl RawGuard {
        fn enter() -> Result<Self> {
            terminal::enable_raw_mode()?;
            let mut stdout = io::stdout();
            if let Err(error) = execute!(stdout, cursor::Hide) {
                let _ = terminal::disable_raw_mode();
                return Err(error.into());
            }
            Ok(Self { hide_cursor: true })
        }
    }

    impl Drop for RawGuard {
        fn drop(&mut self) {
            let _ = terminal::disable_raw_mode();
            if self.hide_cursor {
                let _ = execute!(io::stdout(), cursor::Show);
            }
        }
    }

    let _raw_guard = RawGuard::enter()?;
    let mut current = 0usize;
    let mut checked = vec![false; skills.len()];

    // Initial render
    let lines = skill_multi_select_lines(prompt, skills, current, &checked);
    let mut rendered_region =
        RenderedSkillSelectRegion::from_lines(&lines, live_skill_select_columns());
    print!("{}", live_select_block(&lines));
    io::stdout().flush()?;

    loop {
        match event::read()? {
            Event::Key(key) if key.kind == KeyEventKind::Press => {
                match key.code {
                    KeyCode::Up => {
                        current = if current == 0 {
                            skills.len().saturating_sub(1)
                        } else {
                            current - 1
                        };
                    }
                    KeyCode::Down => current = (current + 1) % skills.len(),
                    KeyCode::Char(' ') => checked[current] = !checked[current],
                    KeyCode::Char('a') | KeyCode::Char('A') => {
                        let select_all = checked.iter().any(|value| !*value);
                        checked.fill(select_all);
                    }
                    KeyCode::Enter | KeyCode::Char('\n') | KeyCode::Char('\r') => {
                        clear_rendered_region(&rendered_region)?;
                        let selected = selected_skill_names(skills, current, &checked);
                        return Ok(Some(selected));
                    }
                    KeyCode::Esc => {
                        clear_rendered_region(&rendered_region)?;
                        return Ok(None);
                    }
                    KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                        clear_rendered_region(&rendered_region)?;
                        return Err(Error::Client("Aborted.".to_string()));
                    }
                    _ => continue,
                }
                clear_rendered_region(&rendered_region)?;
                let lines = skill_multi_select_lines(prompt, skills, current, &checked);
                rendered_region =
                    RenderedSkillSelectRegion::from_lines(&lines, live_skill_select_columns());
                print!("{}", live_select_block(&lines));
                io::stdout().flush()?;
            }
            Event::Resize(_, _) => {
                clear_rendered_region(&rendered_region)?;
                let lines = skill_multi_select_lines(prompt, skills, current, &checked);
                rendered_region =
                    RenderedSkillSelectRegion::from_lines(&lines, live_skill_select_columns());
                print!("{}", live_select_block(&lines));
                io::stdout().flush()?;
            }
            _ => {}
        }
    }

    fn clear_rendered_region(region: &RenderedSkillSelectRegion) -> Result<()> {
        clear_rendered_lines(region.rows_to_clear(live_skill_select_columns()))
    }
}

fn selected_skill_names(
    skills: &[InstalledSkillSummary],
    current: usize,
    checked: &[bool],
) -> Vec<String> {
    let mut selected = skills
        .iter()
        .zip(checked)
        .filter(|(_, checked)| **checked)
        .map(|(skill, _)| skill.name.clone())
        .collect::<Vec<_>>();
    if selected.is_empty()
        && let Some(skill) = skills.get(current)
    {
        selected.push(skill.name.clone());
    }
    selected
}

fn skill_multi_select_lines(
    prompt: &str,
    skills: &[InstalledSkillSummary],
    current: usize,
    checked: &[bool],
) -> Vec<String> {
    let terminal_width = crossterm::terminal::size()
        .map(|(columns, _)| columns as usize)
        .unwrap_or(100)
        .clamp(48, 140);
    let item_width = terminal_width.saturating_sub(8).max(32);
    let mut lines = Vec::new();
    lines.push(format!(
        "{} {}",
        theme::prompt("?").bold(),
        theme::strong(prompt)
    ));
    lines.push(format!(
        "  {}",
        theme::muted("Up/Down move, Space toggle, Enter confirm, A toggle all, Esc cancel")
    ));
    lines.push(String::new());
    for (index, skill) in skills.iter().enumerate() {
        let active = index == current;
        let cursor = if active { ">" } else { " " };
        let check = if checked.get(index).copied().unwrap_or(false) {
            "[x]"
        } else {
            "[ ]"
        };
        let label = skill_selection_label(skill, item_width);
        let label = if active {
            theme::selection(label).bold().to_string()
        } else {
            theme::body(label).to_string()
        };
        lines.push(format!("  {cursor} {check} {label}"));
    }
    lines
}

fn skill_selection_label(skill: &InstalledSkillSummary, width: usize) -> String {
    let label = if skill.description.is_empty() {
        skill.name.clone()
    } else {
        format!("{} - {}", skill.name, skill.description)
    };
    truncate_to_display_width(&label, width)
}

fn live_skill_select_columns() -> usize {
    crossterm::terminal::size()
        .map(|(columns, _)| columns as usize)
        .unwrap_or(100)
        .max(1)
}

#[cfg(test)]
fn rendered_skill_select_rows(lines: &[String], columns: usize) -> usize {
    crate::terminal_ui::rendered_row_count(lines, columns)
}

fn confirm_action(action: &str, names: &[String]) -> Result<bool> {
    if !can_prompt() {
        return Err(Error::Client(format!(
            "{} requires confirmation. Pass --yes to skip the prompt.",
            action
        )));
    }

    print!(
        "{} {} skill(s): {}? [y/N]: ",
        action,
        names.len(),
        format_name_list(names)
    );
    io::stdout().flush()?;

    let mut answer = String::new();
    io::stdin().read_line(&mut answer)?;
    let answer = answer.trim().to_ascii_lowercase();
    Ok(answer == "y" || answer == "yes")
}

fn format_name_list(names: &[String]) -> String {
    const MAX_NAMES: usize = 5;
    if names.len() <= MAX_NAMES {
        return names.join(", ");
    }
    format!(
        "{}, and {} more",
        names[..MAX_NAMES].join(", "),
        names.len() - MAX_NAMES
    )
}

fn output_skill_show(result: &Value, output_format: OutputFormat, compact: bool) {
    if matches!(output_format, OutputFormat::Table)
        && let Some(rendered) = render_skill_show_for_table(result)
    {
        println!(
            "{}",
            crate::output::append_profile_to_rendered(rendered, result)
        );
        return;
    }
    output_success(result, output_format, compact);
}

fn filter_skill_show_level(value: &mut Value, level: i32) {
    let Some(obj) = value.as_object_mut() else {
        return;
    };

    match level {
        0 => {
            obj.remove("overview");
            obj.remove("content");
        }
        1 => {
            obj.remove("abstract");
            obj.remove("content");
        }
        2 => {
            obj.remove("abstract");
            obj.remove("overview");
        }
        _ => {}
    }
}

fn render_skill_show_for_table(value: &Value) -> Option<String> {
    let obj = value.as_object()?;
    if !obj.contains_key("skill_md_uri") && !obj.contains_key("abstract") {
        return None;
    }

    let mut lines = Vec::new();
    push_section(&mut lines, "metadata");
    push_kv(&mut lines, "name", obj.get("name"));
    push_kv(&mut lines, "description", obj.get("description"));
    push_kv(&mut lines, "uri", obj.get("uri"));
    push_kv(&mut lines, "root_uri", obj.get("root_uri"));
    push_kv(&mut lines, "skill_md_uri", obj.get("skill_md_uri"));
    push_kv(&mut lines, "tags", obj.get("tags"));
    push_kv(&mut lines, "allowed_tools", obj.get("allowed_tools"));

    if let Some(abstract_text) = obj.get("abstract").and_then(Value::as_str) {
        push_text_section(&mut lines, "L0 abstract", abstract_text);
    }
    if let Some(overview) = obj.get("overview").and_then(Value::as_str) {
        push_text_section(&mut lines, "L1 overview", overview);
    }
    if let Some(content) = obj.get("content").and_then(Value::as_str) {
        push_text_section(&mut lines, "L2 SKILL.md", content);
    }
    if let Some(files) = obj.get("files").and_then(Value::as_array) {
        push_section(&mut lines, "auxiliary files");
        if files.is_empty() {
            lines.push("(none)".to_string());
        } else {
            for file in files {
                let name = file
                    .get("path")
                    .or_else(|| file.get("name"))
                    .and_then(Value::as_str)
                    .unwrap_or("");
                let kind = file.get("kind").and_then(Value::as_str).unwrap_or("file");
                let uri = file.get("uri").and_then(Value::as_str).unwrap_or("");
                lines.push(format!("- {} [{}]", name, kind));
                if !uri.is_empty() {
                    lines.push(format!("  uri: {}", uri));
                }
            }
        }
    }
    if let Some(source) = obj.get("source").and_then(Value::as_object) {
        push_section(&mut lines, "source");
        for (key, value) in source {
            push_kv(&mut lines, key, Some(value));
        }
    }

    Some(lines.join("\n"))
}

fn push_section(lines: &mut Vec<String>, title: &str) {
    if !lines.is_empty() {
        lines.push(String::new());
    }
    lines.push(format!("[{}]", title));
}

fn push_text_section(lines: &mut Vec<String>, title: &str, text: &str) {
    push_section(lines, title);
    lines.push(text.trim_end().to_string());
}

fn push_kv(lines: &mut Vec<String>, key: &str, value: Option<&Value>) {
    let Some(value) = value else {
        return;
    };
    if value.is_null() {
        return;
    }
    lines.push(format!("{}: {}", key, format_show_value(value)));
}

fn format_show_value(value: &Value) -> String {
    match value {
        Value::String(text) => text.clone(),
        Value::Array(items) => {
            if items.is_empty() {
                "[]".to_string()
            } else {
                items
                    .iter()
                    .map(format_show_value)
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        }
        Value::Bool(value) => value.to_string(),
        Value::Number(value) => value.to_string(),
        Value::Object(_) => serde_json::to_string(value).unwrap_or_default(),
        Value::Null => String::new(),
    }
}

fn output_message_result(
    result: serde_json::Value,
    message: String,
    output_format: OutputFormat,
    compact: bool,
) {
    match output_format {
        OutputFormat::Json => output_success(result, output_format, compact),
        OutputFormat::Table => {
            println!(
                "{}",
                crate::output::append_profile_to_rendered(message, &result)
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        RenderedSkillSelectRegion, filter_skill_show_level, parse_skill_md,
        render_skill_show_for_table, rendered_skill_select_rows,
    };
    use serde_json::json;

    #[test]
    fn skill_show_table_renders_complete_skill_information() {
        let result = json!({
            "name": "demo-skill",
            "description": "Demo skill",
            "uri": "viking://agent/skills/demo-skill",
            "root_uri": "viking://agent/skills/demo-skill",
            "skill_md_uri": "viking://agent/skills/demo-skill/SKILL.md",
            "tags": ["test"],
            "allowed_tools": ["shell"],
            "abstract": "name: demo-skill\ndescription: Demo skill",
            "overview": "Overview text",
            "content": "# demo-skill\n\nUse it.",
            "files": [
                {
                    "name": "SKILL.md",
                    "path": "SKILL.md",
                    "uri": "viking://agent/skills/demo-skill/SKILL.md",
                    "kind": "definition",
                    "is_dir": false
                },
                {
                    "name": "helper.md",
                    "path": "references/helper.md",
                    "uri": "viking://agent/skills/demo-skill/references/helper.md",
                    "kind": "auxiliary",
                    "is_dir": false
                }
            ],
            "source": {
                "tracked": false,
                "message": "Skill source metadata is not tracked yet."
            }
        });

        let rendered = render_skill_show_for_table(&result).expect("skill show table");
        assert!(rendered.contains("[metadata]"));
        assert!(rendered.contains("name: demo-skill"));
        assert!(rendered.contains("[L0 abstract]"));
        assert!(rendered.contains("[L1 overview]"));
        assert!(rendered.contains("[L2 SKILL.md]"));
        assert!(rendered.contains("[auxiliary files]"));
        assert!(rendered.contains("references/helper.md [auxiliary]"));
        assert!(rendered.contains("[source]"));
    }

    #[test]
    fn skill_show_level_one_filters_other_levels_before_rendering() {
        let mut result = json!({
            "name": "demo-skill",
            "skill_md_uri": "viking://agent/skills/demo-skill/SKILL.md",
            "abstract": "L0 text",
            "overview": "L1 text",
            "content": "# L2 text"
        });

        filter_skill_show_level(&mut result, 1);

        let rendered = render_skill_show_for_table(&result).expect("skill show table");
        assert!(!rendered.contains("[L0 abstract]"));
        assert!(!rendered.contains("L0 text"));
        assert!(rendered.contains("[L1 overview]"));
        assert!(rendered.contains("L1 text"));
        assert!(!rendered.contains("[L2 SKILL.md]"));
        assert!(!rendered.contains("# L2 text"));
    }

    #[test]
    fn skill_validate_parser_accepts_delimiter_trailing_whitespace() {
        let parsed =
            parse_skill_md("---   \nname: demo-skill\ndescription: Demo skill\n--- \n\n# Demo\n")
                .expect("frontmatter delimiter with trailing whitespace should parse");

        assert_eq!(
            parsed
                .meta
                .get(serde_yaml::Value::String("name".to_string()))
                .and_then(serde_yaml::Value::as_str),
            Some("demo-skill")
        );
        assert_eq!(parsed.body, "# Demo");
    }

    #[test]
    fn skill_remove_selector_counts_wrapped_ansi_rows() {
        let lines = vec![
            "\u{1b}[32m12345678901\u{1b}[0m".to_string(),
            "short".to_string(),
        ];

        assert_eq!(rendered_skill_select_rows(&lines, 10), 3);
    }

    #[test]
    fn skill_remove_selector_recomputes_clear_rows_after_resize() {
        let lines = vec!["x".repeat(90)];
        let region = RenderedSkillSelectRegion::from_lines(&lines, 90);

        assert_eq!(rendered_skill_select_rows(&lines, 90), 1);
        assert_eq!(rendered_skill_select_rows(&lines, 30), 3);
        assert_eq!(region.rows_to_clear(30), 3);
    }
}