artifactize-core 0.27.2

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
//! Shared inspection reads and JSON projections. Interfaces add rendering and paging only.
pub(crate) mod logs;
pub mod session;
pub use crate::store::{Issue, Request, RequestView};
pub use logs::{DEFAULT_TAIL as DEFAULT_LOG_TAIL_LINES, LogOptions, LogQuery, Stream, logs};

use crate::{config::read_workspace_config, types::RequestId};
use serde::Serialize;
use serde_json::{Value, json};
use std::path::Path;

#[derive(Debug, Serialize)]
pub struct ConfigCheck {
    pub ok: bool,
    pub artifacts: usize,
    pub evals: usize,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub notes: Vec<String>,
}
pub fn config_check(repo: &Path) -> Result<ConfigCheck, String> {
    let config = read_workspace_config(repo).map_err(|error| error.to_string())?;
    Ok(ConfigCheck {
        notes: config
            .artifacts
            .values()
            .filter_map(|artifact| {
                crate::scope::validate_file_target(&config.root, artifact)
                    .err()
                    .map(|error| error.to_string())
            })
            .collect(),
        ok: true,
        artifacts: config.artifacts.len(),
        evals: config.evals.len(),
    })
}

pub async fn request_show(
    state: &Path,
    id: &RequestId,
    now: time::OffsetDateTime,
) -> Result<Value, String> {
    Ok(crate::query::request_output(
        &request(state, id).await?,
        now,
    ))
}

/// The exact saved request with its recorded definition, not the review it may share.
pub async fn request(state: &Path, id: &RequestId) -> Result<RequestView, String> {
    crate::store::read_request_id(state, id).await
}

/// A request's place in its saved Run, including the Run's sibling requests for Detail.
pub struct RequestContext {
    pub target: crate::review::Target,
    pub run: crate::runs::RunView,
    pub requests: Vec<RequestView>,
}

pub async fn request_context(state: &Path, id: &RequestId) -> Result<RequestContext, String> {
    let exact = request(state, id).await?;
    let target = crate::review::Target::from(exact.request.as_ref());
    let (run, requests) = crate::store::read_run_requests_by_id(state, &target.run).await?;
    Ok(RequestContext {
        target,
        run,
        requests,
    })
}

pub async fn request_list(
    state: &Path,
    run: Option<&crate::types::RunId>,
    repo: Option<&Path>,
    now: time::OffsetDateTime,
) -> Result<Vec<Value>, String> {
    let views = requests(state, run, repo).await?;
    Ok(request_list_output(&views, now))
}

/// Read saved requests through one selection for all output projections.
pub async fn requests(
    state: &Path,
    run: Option<&crate::types::RunId>,
    repo: Option<&Path>,
) -> Result<Vec<RequestView>, String> {
    crate::store::read_requests_for_run(state, run.cloned(), repo).await
}

pub fn request_list_output(views: &[RequestView], now: time::OffsetDateTime) -> Vec<Value> {
    views
        .iter()
        .map(|view| crate::query::request_output(view, now))
        .collect()
}

pub async fn remote_status(state: Option<&Path>, repo: Option<&Path>) -> Result<Value, String> {
    Ok(remote_output(
        crate::auth::remote::status(state, repo).await?.as_ref(),
    ))
}

pub fn remote_output(status: Option<&crate::auth::remote::Status>) -> Value {
    match status {
        Some(status) => {
            let mut value = json!(status);
            value["configured"] = json!(true);
            value
        }
        None => json!({"configured": false}),
    }
}

pub async fn session_show(
    state: &Path,
    selector: &session::Selector,
    summary: bool,
) -> Result<session::Shown, String> {
    session::show(state, selector, summary).await
}