artifactize 0.27.0

The AI-native collaboration layer for one-of-a-kind teammates and their agents: split a project into Artifacts with their own evals, and reuse every review while it still holds.
//! Invoke Human review actions and render the library results.

use std::{
    io::{self, Write},
    path::Path,
};

use artifactize_core::{
    config::ToolName,
    human,
    runtime::Verdict,
    tools::human::Content,
    types::{RequestId, ReviewerId},
};

use super::super::{FAILURE_EXIT, cancellation_listener, print_json};
use super::{inspect, issues::IssuesSource};

pub(super) async fn claim(
    state: &Path,
    id: RequestId,
    reviewer: Option<ReviewerId>,
) -> Result<(), String> {
    let reviewer = reviewer.map_or_else(human::default_reviewer, Ok)?;
    print_json(&human::claim_request(state, &id, &reviewer).await?)
}

pub(super) async fn unclaim(
    state: &Path,
    id: RequestId,
    reviewer: Option<ReviewerId>,
) -> Result<(), String> {
    let reviewer = reviewer.map_or_else(human::default_reviewer, Ok)?;
    print_json(&human::unclaim_request(state, &id, &reviewer).await?)
}

pub(super) async fn tool(
    state: &Path,
    id: RequestId,
    tool: ToolName,
    reviewer: Option<ReviewerId>,
    json: bool,
) -> Result<u8, String> {
    let reviewer = reviewer.map_or_else(human::default_reviewer, Ok)?;
    let (cancellation, listener) = cancellation_listener()?;
    let result = human::run_request_tool(state, &id, &reviewer, &tool, cancellation).await;
    listener.abort();
    let result = result?;
    if json {
        print_json(&result)?;
    } else {
        let mut out = io::stdout().lock();
        for content in result.content() {
            match content {
                Content::Text { text } => writeln!(out, "{text}"),
                Content::Viewer { artifact, tool } => {
                    writeln!(
                        out,
                        "Open {artifact} with {tool} in the Artifactize desktop."
                    )
                }
                Content::Launch { launched } => writeln!(
                    out,
                    "Tool {tool}: {}",
                    if *launched {
                        "launched"
                    } else {
                        "not launched"
                    }
                ),
            }
            .map_err(|e| e.to_string())?;
        }
    }
    Ok(if result.is_error() { FAILURE_EXIT } else { 0 })
}

pub(super) async fn submit(
    state: &Path,
    id: RequestId,
    verdict: Verdict,
    issues: IssuesSource,
    reviewer: Option<ReviewerId>,
) -> Result<(), String> {
    let submission = issues.submission(verdict)?;
    let reviewer = reviewer.map_or_else(human::default_reviewer, Ok)?;
    let (cancellation, listener) = cancellation_listener()?;
    let result = human::submit_and_publish(state, &id, &reviewer, &submission, cancellation).await;
    listener.abort();
    result?;
    inspect::show(state, &id).await
}