use super::{
error::Error,
input::{Inspection, Read},
page::Page,
};
use crate::{
cache, config, diagnostics, inspect, llm, platform, project, query, runs, store,
types::{RequestId, RunId},
};
use serde_json::to_string;
use time::OffsetDateTime;
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();
}
run_text(context, &show.run, page.as_of()).await?
}
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())?
}
}
};
page.project(context, &scrubbed(context, text)?)
}
pub(super) async fn run(context: &runs::Context, run: &RunId) -> Result<String, Error> {
scrubbed(
context,
run_text(context, run, OffsetDateTime::now_utc()).await?,
)
}
async fn run_text(
context: &runs::Context,
run: &RunId,
as_of: OffsetDateTime,
) -> Result<String, Error> {
Ok(to_string(&query::run_output(
&runs::view(context, run).await?,
as_of,
))?)
}
fn scrubbed(context: &runs::Context, text: String) -> Result<String, Error> {
let secrets = crate::auth::managed_secrets(Some(context.state()), Some(context.repo()))
.map_err(|_| Error::CredentialInventory)?;
if !secrets.is_empty() {
let mut value = serde_json::from_str(&text)?;
if secrets.scrub_json(&mut value) {
return Ok(to_string(&value)?);
}
}
Ok(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;