gent-link 0.2.1

Connect Codex and Claude Code chats across machines with the Gent CLI and desktop companion.
use super::*;

#[derive(Subcommand)]
pub(crate) enum Command {
    /// List owner-visible chat delegations and retired grant IDs.
    List,
    /// Read durable chat-edit audit records, including requests from other nodes.
    History {
        #[arg(long = "chat", alias = "agent", value_name = "CHAT_ID")]
        administrator: Option<String>,
        #[arg(long)]
        before: Option<u64>,
    },
    /// Delegate exact-pair editing within selectors (*, same-node, node:UUID, group:NAME or chat UUID).
    Grant {
        administrator: String,
        actor: String,
        peer: String,
        #[arg(long, value_enum, num_args=1.., required=true)]
        permission: Vec<PermissionName>,
        /// Also permit selector-rule edits affecting hidden and future matching chats.
        #[arg(long)]
        selector_rules: bool,
        #[arg(long, default_value_t = 86400)]
        ttl_seconds: u64,
    },
    /// Stop future edits. Already accepted permission rules remain in force.
    Revoke { grant: String },
}

pub(crate) async fn run(
    home: &std::path::Path,
    request_id: &str,
    command: Command,
) -> Result<Value> {
    match command {
        Command::List => owner(home, OwnerOp::PermissionAdmins).await,
        Command::History {
            administrator,
            before,
        } => {
            owner(
                home,
                OwnerOp::PermissionAdminHistory {
                    administrator,
                    before,
                },
            )
            .await
        }
        Command::Grant {
            administrator,
            actor,
            peer,
            permission,
            selector_rules,
            ttl_seconds,
        } => {
            configure(
                home,
                request_id,
                ConfigurationEdit::GrantPermissionAdmin {
                    grant_id: request_id.into(),
                    administrator,
                    actor: selector(actor)?,
                    peer: selector(peer)?,
                    permissions: permission
                        .into_iter()
                        .map(|p| match p {
                            PermissionName::Enumerate => Permission::Enumerate,
                            PermissionName::Receive => Permission::Receive,
                            PermissionName::Send => Permission::Send,
                        })
                        .collect(),
                    selector_rules,
                    lifetime_seconds: ttl_seconds,
                },
            )
            .await
        }
        Command::Revoke { grant } => {
            configure(
                home,
                request_id,
                ConfigurationEdit::RevokePermissionAdmin { grant_id: grant },
            )
            .await
        }
    }
}