use std::path::PathBuf;
use std::process::Stdio;
use std::sync::LazyLock;
use regex::Regex;
use tokio::process::Command;
use crate::diagnostics::{append_warning, is_notebook_not_found};
use crate::error::NbError;
use crate::gate;
use crate::git::derive_git_notebook_name;
use crate::git_env::scrub_git_env;
use crate::git_signing::apply_git_signing_env;
use crate::nb_program::nb_program;
use crate::transaction::Transaction;
use crate::validate::{parse_qualified_selector, validate_notebook_name};
use crate::{Config, NbClient};
use super::read::notebook_dir_from_env;
static ANSI_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|[ -/]*[0-~])").unwrap()
});
fn strip_ansi(text: &str) -> String {
ANSI_REGEX.replace_all(text, "").into_owned()
}
pub(super) enum ShowClassification {
Textual,
NonTextual { actual_type: String },
ProbeFailure,
}
const FOLDER_REQUIRED_MESSAGE: &str = "This server is configured to require `folder` for new notes. Use the `nb.mkdir` tool to create new folders and the `nb.folders` tool to list existing folders.";
impl NbClient {
pub fn new(config: &Config) -> anyhow::Result<Self> {
let default_notebook = config
.notebook
.as_deref()
.map(String::from)
.or_else(derive_git_notebook_name);
Ok(Self {
default_notebook,
create_notebook: config.create_notebook,
disable_git_signing: config.disable_git_signing,
allow_top_level_notes: config.allow_top_level_notes,
gate_timeout: config.gate_timeout,
})
}
pub async fn transaction(
&self,
notebook: Option<&str>,
) -> Result<Transaction, NbError> {
let notebook = self.resolve_notebook_name(notebook)?;
if notebook_dir_from_env(¬ebook).is_none() {
self.ensure_notebook(¬ebook).await?;
}
Ok(Transaction::new(self.clone(), notebook, self.gate_timeout))
}
pub fn gate_timeout(&self) -> std::time::Duration {
self.gate_timeout
}
pub(crate) async fn show_notebook_path_unguarded(
&self,
notebook: Option<&str>,
) -> Result<PathBuf, NbError> {
let notebook = self.resolve_notebook_name(notebook)?;
if let Some(path) = notebook_dir_from_env(¬ebook) {
return Ok(path);
}
self.ensure_notebook(¬ebook).await?;
let output = self
.exec_vec(vec![
"notebooks".to_string(),
"show".to_string(),
notebook.clone(),
"--path".to_string(),
])
.await?;
let path = output.trim();
if path.is_empty() {
return Err(NbError::CommandFailed {
command: format!("nb notebooks show {notebook} --path"),
stderr: "nb notebooks path output was empty".to_string(),
exit_code: None,
});
}
Ok(PathBuf::from(path))
}
pub(super) async fn with_notebook_gate<F, T>(
&self,
notebook: &str,
f: F,
) -> Result<T, NbError>
where
F: std::future::Future<Output = Result<T, NbError>>,
{
let root = self.show_notebook_path_unguarded(Some(notebook)).await?;
let key = gate::git_common_dir_realpath(&root)?;
let _hold =
gate::acquire_notebook(key, self.gate_timeout, false).await?;
f.await
}
pub(super) fn require_folder_for_new_note(
&self,
folder: Option<&str>,
) -> Result<(), NbError> {
if self.allow_top_level_notes
|| folder.is_some_and(|value| !value.trim().is_empty())
{
return Ok(());
}
Err(NbError::ValidationError {
reason: FOLDER_REQUIRED_MESSAGE.to_string(),
location: None,
})
}
pub(super) async fn resolve_target_selector(
&self,
id: &str,
notebook: Option<&str>,
) -> Result<(String, String), NbError> {
if let Some((embedded_notebook, path)) = parse_qualified_selector(id)?
{
let notebook = match notebook {
Some(value) => {
validate_notebook_name(value)?;
if value != embedded_notebook {
return Err(NbError::ValidationError {
reason: format!(
"ambiguous selector: id targets notebook `{embedded_notebook}`, but notebook field is `{value}`"
),
location: None,
});
}
embedded_notebook.to_string()
},
_ => embedded_notebook.to_string(),
};
self.ensure_existing_notebook(¬ebook).await?;
return Ok((notebook, format!("{}:{}", embedded_notebook, path)));
}
let notebook = self.resolve_notebook(notebook).await?;
Ok((notebook.clone(), format!("{}:{}", notebook, id)))
}
pub(super) fn append_notebook_warning(
&self,
output: String,
notebook: &str,
) -> String {
let Some(default_notebook) = self.default_notebook.as_deref() else {
return output;
};
if default_notebook == notebook {
return output;
}
append_warning(
output,
format!(
"Warning: wrote to notebook `{notebook}`, not the project default notebook `{default_notebook}`. If this was unintended, move or delete the note and retry with the correct notebook/folder."
),
)
}
pub(super) fn resolve_notebook_name(
&self,
notebook: Option<&str>,
) -> Result<String, NbError> {
if let Some(name) = notebook {
validate_notebook_name(name)?;
return Ok(name.to_string());
}
if let Some(name) = self.default_notebook.as_deref() {
validate_notebook_name(name)?;
return Ok(name.to_string());
}
Err(NbError::ValidationError {
reason:
"notebook not configured; set --notebook or NB_MCP_NOTEBOOK"
.to_string(),
location: None,
})
}
pub(super) async fn resolve_notebook(
&self,
notebook: Option<&str>,
) -> Result<String, NbError> {
let name = self.resolve_notebook_name(notebook)?;
self.ensure_notebook(&name).await?;
Ok(name)
}
pub(super) async fn ensure_notebook(
&self,
notebook: &str,
) -> Result<(), NbError> {
match self.check_notebook(notebook).await {
Ok(()) => Ok(()),
Err(
err
@ (NbError::ExecutableNotFound { .. } | NbError::Io { .. }),
) => Err(err),
Err(NbError::NotFound { .. }) => {
if !self.create_notebook {
return Err(NbError::ValidationError {
reason: format!(
"notebook not found; create it with the nb CLI (`nb notebooks add {}`) \
or remove --no-create-notebook",
notebook
),
location: None,
});
}
self.exec_vec(vec![
"notebooks".to_string(),
"add".to_string(),
notebook.to_string(),
])
.await?;
Ok(())
},
Err(err) => Err(err),
}
}
pub(super) async fn ensure_existing_notebook(
&self,
notebook: &str,
) -> Result<(), NbError> {
match self.check_notebook(notebook).await {
Ok(()) => Ok(()),
Err(
err
@ (NbError::ExecutableNotFound { .. } | NbError::Io { .. }),
) => Err(err),
Err(err @ NbError::NotFound { .. }) => Err(err),
Err(err) => Err(err),
}
}
async fn check_notebook(&self, notebook: &str) -> Result<(), NbError> {
let show_result = self
.exec_vec(vec![
"notebooks".to_string(),
"show".to_string(),
notebook.to_string(),
"--path".to_string(),
])
.await;
match show_result {
Ok(output) => {
if output.trim().is_empty() {
return Err(NbError::CommandFailed {
command: format!(
"nb notebooks show {notebook} --path"
),
stderr: "nb notebooks path output was empty"
.to_string(),
exit_code: None,
});
}
Ok(())
},
Err(err) => {
match err {
NbError::ExecutableNotFound { .. }
| NbError::Io { .. } => Err(err),
NbError::CommandFailed { ref stderr, .. }
if is_notebook_not_found(stderr, notebook) =>
{
Err(NbError::NotFound {
selector: format!("{notebook}:"),
})
},
other => Err(other),
}
},
}
}
pub(super) async fn exec(&self, args: &[&str]) -> Result<String, NbError> {
tracing::debug!(?args, "executing nb command");
let mut command = Command::new(nb_program());
scrub_git_env(&mut command);
command
.args(args)
.stdin(Stdio::null()) .stdout(Stdio::piped())
.stderr(Stdio::piped());
if self.disable_git_signing {
apply_git_signing_env(&mut command);
}
let joined = format!("nb {}", args.join(" "));
let output = command
.spawn()
.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
NbError::ExecutableNotFound {
path: "nb".to_string(),
}
} else {
NbError::Io {
path: PathBuf::from("nb"),
source: e.into(),
}
}
})?
.wait_with_output()
.await
.map_err(|e| NbError::Io {
path: PathBuf::from("nb"),
source: e.into(),
})?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
Ok(strip_ansi(&stdout))
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr_text = if stderr.is_empty() {
strip_ansi(&stdout)
} else {
strip_ansi(&stderr)
};
Err(NbError::CommandFailed {
command: joined,
stderr: stderr_text,
exit_code: output.status.code(),
})
}
}
pub(super) async fn exec_vec(
&self,
args: Vec<String>,
) -> Result<String, NbError> {
let args_ref: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
self.exec(&args_ref).await
}
}