artifactize_core/
verify_tools.rs1mod catalog;
3mod error;
4mod input;
5mod page;
6mod read;
7mod tool;
8
9pub use catalog::{Hints, Tool, tools};
10pub use error::{ArgumentsError, Error};
11pub use input::Call;
12pub use page::{CursorError, Page};
13
14use std::{future::Future, time::Duration};
15
16use serde::Serialize;
17use tokio_util::sync::CancellationToken;
18
19use crate::{project::VerifyOptions, runs, types::RunId};
20use input::{Command, DEFAULT_WAIT_SECONDS, Plugin, Verify};
21
22const WAIT_CALL_LIMIT: Duration = Duration::from_secs(44);
24
25#[derive(Serialize)]
26#[serde(untagged)]
27pub enum Output {
28 Observation(runs::RunObservation),
29 Result(Box<runs::RunResultPage>),
30 Cancellation(runs::CancelAcknowledgement),
31 Inspection(Page),
32 Plugin(PluginResult),
33}
34
35#[derive(Serialize)]
36#[serde(tag = "action", rename_all = "lowercase")]
37pub enum PluginResult {
38 Added(crate::config::plugins::PluginInfo),
39 Removed {
40 #[serde(rename = "type")]
41 name: crate::config::ArtifactType,
42 },
43}
44
45pub(crate) fn chat_tool(name: &str) -> bool {
47 matches!(
48 tool::ToolId::parse(name),
49 Some(tool::ToolId::Verify(_) | tool::ToolId::Read(_))
50 )
51}
52
53impl Call {
54 pub async fn execute<F: Future<Output = Result<(), String>>>(
57 self,
58 context: &runs::Context,
59 cancel: CancellationToken,
60 progress: impl FnMut(runs::RunEvent) -> F,
61 ) -> Result<Output, Error> {
62 let command = match self.command {
63 Command::Verify(command) => command,
64 Command::Plugin(plugin) => {
65 use crate::config::plugins;
66 let result = match plugin {
67 Plugin::Add(args) => PluginResult::Added(
68 plugins::add(context.repo(), &args.path).map_err(Error::Config)?,
69 ),
70 Plugin::Remove(args) => {
71 plugins::remove(context.repo(), &args.name).map_err(Error::Config)?;
72 PluginResult::Removed { name: args.name }
73 }
74 };
75 return Ok(Output::Plugin(result));
76 }
77 Command::Inspect(inspection) => {
78 return Ok(Output::Inspection(
79 read::inspect(context, inspection).await?,
80 ));
81 }
82 };
83 Ok(match command {
84 Verify::Start(args) => {
85 let options = VerifyOptions {
86 jobs: args.jobs.map(|limit| limit.get() as usize),
87 modes: runs::RunModes {
88 speculate: args.speculate,
89 },
90 comment: args.comment,
91 ..Default::default()
92 };
93 Output::Observation(
94 runs::start(context, args.scope.selection(), &options, args.request_key)
95 .await?,
96 )
97 }
98 Verify::Status(run) => Output::Observation(runs::status(context, &run).await?),
99 Verify::Wait(args) => Output::Observation(
100 tokio::time::timeout(
101 WAIT_CALL_LIMIT,
102 runs::wait(context, &args.run, Some(args.duration()), cancel),
103 )
104 .await
105 .map_err(|_| Error::Timeout)??,
106 ),
107 Verify::Watch(run) => Output::Observation(
108 tokio::time::timeout(
109 WAIT_CALL_LIMIT,
110 runs::watch(
111 context,
112 &run,
113 Some(Duration::from_secs(DEFAULT_WAIT_SECONDS)),
114 cancel,
115 progress,
116 ),
117 )
118 .await
119 .map_err(|_| Error::Timeout)??,
120 ),
121 Verify::Result(args) => Output::Result(Box::new(
122 runs::result(context, &args.run, args.cursor).await?,
123 )),
124 Verify::Cancel(run) => Output::Cancellation(runs::cancel(context, &run).await?),
125 })
126 }
127
128 pub fn started_run(&self) -> bool {
130 matches!(self.command, Command::Verify(Verify::Start(_)))
131 }
132}
133
134impl Output {
135 pub fn observed_run(&self) -> Option<&RunId> {
136 match self {
137 Self::Observation(observation) => Some(&observation.run),
138 _ => None,
139 }
140 }
141}