agent_chain_core/utils/interactive_env.rs
1//! Utilities for working with interactive environments.
2//!
3//! Adapted from langchain_core/utils/interactive_env.py
4
5use std::io::IsTerminal;
6
7/// Determine if running within an interactive environment.
8///
9/// This function attempts to detect if the current process is running
10/// in an interactive environment like a REPL or Jupyter notebook.
11///
12/// # Returns
13///
14/// `true` if running in an interactive environment, `false` otherwise.
15///
16/// # Example
17///
18/// ```
19/// use agent_chain_core::utils::interactive_env::is_interactive_env;
20///
21/// let is_interactive = is_interactive_env();
22/// // Returns true if running in a REPL, false otherwise
23/// ```
24pub fn is_interactive_env() -> bool {
25 std::env::var("RUST_INTERACTIVE").is_ok() || std::io::stdin().is_terminal()
26}
27
28/// Check if running in a CI environment.
29///
30/// # Returns
31///
32/// `true` if running in a CI environment, `false` otherwise.
33pub fn is_ci_env() -> bool {
34 std::env::var("CI").is_ok()
35 || std::env::var("CONTINUOUS_INTEGRATION").is_ok()
36 || std::env::var("GITHUB_ACTIONS").is_ok()
37 || std::env::var("GITLAB_CI").is_ok()
38 || std::env::var("TRAVIS").is_ok()
39 || std::env::var("CIRCLECI").is_ok()
40 || std::env::var("JENKINS_URL").is_ok()
41}
42
43/// Check if running in a testing environment.
44///
45/// # Returns
46///
47/// `true` if running in a testing environment, `false` otherwise.
48pub fn is_test_env() -> bool {
49 std::env::var("RUST_TEST").is_ok() || cfg!(test)
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_is_interactive_env() {
58 let _ = is_interactive_env();
59 }
60
61 #[test]
62 fn test_is_ci_env() {
63 let _ = is_ci_env();
64 }
65
66 #[test]
67 fn test_is_test_env() {
68 assert!(is_test_env());
69 }
70}