artifactize-core 0.27.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
//! Repository-bound inspection and lossless detail paging, with no CLI duplication.
use super::{
    error::Error,
    input::{Inspection, Read},
    page::Page,
};
use crate::{
    cache, config, diagnostics, inspect, llm, platform, project, query, runs, store,
    types::RequestId,
};
use serde_json::to_string;
use tokio_util::sync::CancellationToken;

pub(super) async fn inspect(
    context: &runs::Context,
    Inspection { read, mut page }: Inspection,
    cancel: CancellationToken,
) -> Result<Page, Error> {
    let repo = context.repo();
    let state = Some(context.state());
    let text = match read {
        Read::Status(args) => to_string(
            &project::status(
                repo,
                state,
                args.scope.selection(),
                &project::VerifyOptions::default(),
                CancellationToken::new(),
            )
            .await?,
        )?,
        Read::Graph(args) => {
            let config = config::read_workspace_config(repo).map_err(Error::Config)?;
            let selection = args.scope.selection();
            if args.impact {
                to_string(&query::graph(&config, &query::impact(&config, selection)?)?)?
            } else {
                to_string(&query::graph(&config, selection)?)?
            }
        }
        Read::Logs(args) => {
            verify_request_project(context, &args.id).await?;
            to_string(&inspect::logs(context.state(), &args.id, args.query).await?)?
        }
        Read::Plugins => to_string(&config::plugins::list(repo).map_err(Error::Config)?)?,
        Read::Config => to_string(&inspect::config_check(repo)?)?,
        Read::Expand(args) => to_string(
            &config::expand::expand(repo, args.artifact.as_ref()).map_err(Error::Config)?,
        )?,
        Read::Symbols => to_string(&config::expand::symbols(repo).map_err(Error::Config)?)?,
        Read::Runs(args) => to_string(
            &runs::list(
                context.state(),
                Some(repo),
                args.limit(),
                args.offset.unwrap_or_default(),
            )
            .await?,
        )?,
        Read::Run(show) => {
            let bound = show.duration();
            if page.is_initial() && !bound.is_zero() {
                runs::wait_for_change(context, &show.run, bound, cancel).await?;
                page = page.refreshed();
            }
            to_string(&query::run_output(
                &runs::view(context, &show.run).await?,
                page.as_of(),
            ))?
        }
        Read::Requests(args) => {
            if let Some(run) = &args.run {
                runs::view(context, run).await?;
            }
            to_string(
                &inspect::request_list(
                    context.state(),
                    args.run.as_ref(),
                    Some(repo),
                    page.as_of(),
                )
                .await?,
            )?
        }
        Read::Request(id) => {
            verify_request_project(context, &id).await?;
            to_string(&inspect::request_show(context.state(), &id, page.as_of()).await?)?
        }
        Read::Session(args) => {
            let located = inspect::session::locate(context.state(), &args.reference).await?;
            runs::view(context, &located.request.request.run_id).await?;
            to_string(
                &inspect::session_show(context.state(), &args.reference, args.summary).await?,
            )?
        }
        Read::Tools(options) => to_string(
            &diagnostics::check_tools(repo, state, &options, CancellationToken::new()).await?,
        )?,
        Read::Doctor => to_string(&diagnostics::doctor(state, Some(repo)).await?)?,
        Read::Models(provider) => {
            to_string(&llm::models::list(provider, state, Some(repo)).await?)?
        }
        Read::Remote => to_string(&inspect::remote_status(state, Some(repo)).await?)?,
        Read::Cache(args) => {
            let records = cache::show(context.state(), &args.key, args.history).await?;
            if args.history {
                to_string(&records)?
            } else {
                to_string(&records.into_iter().next_back())?
            }
        }
    };
    // Scrub the complete document before cursor offsets are calculated so a credential
    // split by a page boundary cannot be reconstructed by attaching the pages.
    let secrets =
        crate::auth::managed_secrets(state, Some(repo)).map_err(|_| Error::CredentialInventory)?;
    if !secrets.is_empty() {
        let mut value = serde_json::from_str(&text)?;
        if secrets.scrub_json(&mut value) {
            return page.project(context, &to_string(&value)?);
        }
    }
    page.project(context, &text)
}

async fn verify_request_project(context: &runs::Context, request: &RequestId) -> Result<(), Error> {
    let repo = store::read_request_repo(context.state(), request).await?;
    if !platform::paths_equal(&repo, context.repo()) {
        return Err(Error::RequestOutsideProject);
    }
    Ok(())
}

#[cfg(test)]
mod tests;