Skip to main content

artifactize_core/
inspect.rs

1//! Shared inspection reads and JSON projections. Interfaces add rendering and paging only.
2pub(crate) mod logs;
3pub mod session;
4pub use crate::store::{Issue, Request, RequestView};
5pub use logs::{DEFAULT_TAIL as DEFAULT_LOG_TAIL_LINES, LogOptions, LogQuery, Stream, logs};
6
7use crate::{config::read_workspace_config, types::RequestId};
8use serde::Serialize;
9use serde_json::{Value, json};
10use std::path::Path;
11
12#[derive(Debug, Serialize)]
13pub struct ConfigCheck {
14    pub ok: bool,
15    pub artifacts: usize,
16    pub evals: usize,
17    #[serde(skip_serializing_if = "Vec::is_empty")]
18    pub notes: Vec<String>,
19}
20pub fn config_check(repo: &Path) -> Result<ConfigCheck, String> {
21    let config = read_workspace_config(repo).map_err(|error| error.to_string())?;
22    Ok(ConfigCheck {
23        notes: config
24            .artifacts
25            .values()
26            .filter_map(|artifact| {
27                crate::scope::validate_file_target(&config.root, artifact)
28                    .err()
29                    .map(|error| error.to_string())
30            })
31            .collect(),
32        ok: true,
33        artifacts: config.artifacts.len(),
34        evals: config.evals.len(),
35    })
36}
37
38pub async fn request_show(
39    state: &Path,
40    id: &RequestId,
41    now: time::OffsetDateTime,
42) -> Result<Value, String> {
43    Ok(crate::query::request_output(
44        &request(state, id).await?,
45        now,
46    ))
47}
48
49/// The exact saved request with its recorded definition, not the review it may share.
50pub async fn request(state: &Path, id: &RequestId) -> Result<RequestView, String> {
51    crate::store::read_request_id(state, id).await
52}
53
54/// A request's place in its saved Run, including the Run's sibling requests for Detail.
55pub struct RequestContext {
56    pub target: crate::review::Target,
57    pub run: crate::runs::RunView,
58    pub requests: Vec<RequestView>,
59}
60
61pub async fn request_context(state: &Path, id: &RequestId) -> Result<RequestContext, String> {
62    let exact = request(state, id).await?;
63    let target = crate::review::Target::from(exact.request.as_ref());
64    let (run, requests) = crate::store::read_run_requests_by_id(state, &target.run).await?;
65    Ok(RequestContext {
66        target,
67        run,
68        requests,
69    })
70}
71
72/// Default to the same number of requests shown on one Review page.
73pub const DEFAULT_REQUEST_LIMIT: u32 = crate::review::search::PAGE as u32;
74
75pub async fn request_list(
76    state: &Path,
77    run: Option<&crate::types::RunId>,
78    repo: Option<&Path>,
79    now: time::OffsetDateTime,
80) -> Result<Vec<Value>, String> {
81    let views = requests(state, run, repo, DEFAULT_REQUEST_LIMIT).await?;
82    Ok(request_list_output(&views, now))
83}
84
85/// Read at most `limit` saved requests, newest Run first, without Run definitions.
86pub async fn requests(
87    state: &Path,
88    run: Option<&crate::types::RunId>,
89    repo: Option<&Path>,
90    limit: u32,
91) -> Result<Vec<RequestView>, String> {
92    crate::store::read_request_list(state, run.cloned(), repo, limit).await
93}
94
95pub fn request_list_output(views: &[RequestView], now: time::OffsetDateTime) -> Vec<Value> {
96    views
97        .iter()
98        .map(|view| crate::query::request_output(view, now))
99        .collect()
100}
101
102pub async fn remote_status(state: Option<&Path>, repo: Option<&Path>) -> Result<Value, String> {
103    Ok(remote_output(
104        crate::auth::remote::status(state, repo).await?.as_ref(),
105    ))
106}
107
108pub fn remote_output(status: Option<&crate::auth::remote::Status>) -> Value {
109    match status {
110        Some(status) => {
111            let mut value = json!(status);
112            value["configured"] = json!(true);
113            value
114        }
115        None => json!({"configured": false}),
116    }
117}
118
119pub async fn session_show(
120    state: &Path,
121    selector: &session::Selector,
122    summary: bool,
123) -> Result<session::Shown, String> {
124    session::show(state, selector, summary).await
125}