use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, PoisonError, Weak};
use std::time::Duration;
use futures::FutureExt;
use futures::future::BoxFuture;
use futures::stream::FuturesUnordered;
use monty_types::{CallArgs, ExcType, MontyException, MontyObject, ObjectRef, PrintWriterCallback, ResourceLimits};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::json;
use strands::interrupt::{InterruptError, InterruptSource};
use strands::types::{JsonValue, ToolResultBlock, ToolResultContent, ToolResultStatus, ToolSpec};
use strands::{CancelSignal, RefusedCall, Tool, ToolAgent, ToolContext, ToolOutcome, ToolStream};
use tokio::sync::Semaphore;
use crate::env::Env;
mod in_process;
#[cfg(feature = "monty-pool")]
mod worker;
pub const DEFAULT_PROGRAMMATIC_TOOL_CALLER_DESCRIPTION: &str = concat!(
"Execute Python code that orchestrates the agent's other tools, each exposed as an async function ",
"taking keyword arguments -- always `await` them, e.g. `result = await read(path=\"/etc/hosts\")`. ",
"The code runs in an async context, so `await` and `asyncio.gather(...)` work without boilerplate. ",
"A tool result that is structured content or JSON text (an object or array) arrives parsed, as a ",
"dict/list; anything else arrives as a string. A tool that fails raises RuntimeError, which you can ",
"catch. ",
"Only text sent to `print()` is returned to you: a tool's return value stays in the code's local ",
"scope unless you print it. A tool whose name is not a valid Python identifier (for example ",
"`fetch-url` or `ns.fetch`) is also available with those characters replaced by underscores ",
"(`fetch_url`, `ns_fetch`). The code runs in a sandboxed Python subset: `asyncio`, `json`, `re`, ",
"`math`, `datetime`, `collections`, `itertools`, `functools`, `dataclasses`, `typing`, `base64` ",
"are importable; filesystem, network, and subprocess access do not exist -- use the agent's tools ",
"for those. Generators (`yield`), class inheritance, `match`, and `del` are not supported. Use this ",
"to chain, loop over, filter, or parallelize tool calls in a single turn instead of one tool call ",
"per model round-trip, keeping large intermediate results out of the conversation."
);
pub(crate) const MAX_CONCURRENT_TOOL_CALLS: usize = 10;
pub const DEFAULT_PROGRAMMATIC_TOOL_CALLER_TIMEOUT: Duration = Duration::from_secs(900);
pub const MONTY_WORKER_VERSION: &str = "1.5.0";
pub const MONTY_WORKER_ENV: &str = "MONTY_BIN";
const DEFAULT_NAME: &str = "programmatic_tool_caller";
const USER_CODE_FILENAME: &str = "<programmatic_tool_caller>";
const MAX_OUTPUT_CHARS: usize = 200_000;
const MAX_VALUE_DEPTH: usize = 128;
const CODE_DESCRIPTION: &str = "Python code to execute. Use `await tool_name(...)` to call tools.";
const CANCELLED_CALL: &str = "programmatic_tool_caller was cancelled; no further tool calls are made.";
const NO_INNER_INTERRUPTS: &str = "Interrupts are not supported from programmatic_tool_caller.";
#[derive(Debug, Clone, Copy, PartialEq)]
struct Limits {
max_duration: Duration,
max_memory: usize,
max_suspensions: usize,
}
impl Limits {
fn resource_limits(self) -> ResourceLimits {
ResourceLimits {
max_feed_duration: Some(self.max_duration),
max_memory: Some(self.max_memory),
max_suspensions: self.max_suspensions,
..ResourceLimits::default()
}
}
}
const LIMITS: Limits = Limits {
max_duration: Duration::from_secs(60),
max_memory: 256 * 1024 * 1024,
max_suspensions: 1_000,
};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Isolation {
#[default]
Auto,
Subprocess(PathBuf),
InProcess,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, bon::Builder)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ProgrammaticToolCallerConfig {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub allowed_tools: Option<Vec<String>>,
#[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
pub timeout: Option<Option<f64>>,
}
fn present<'de, D, T>(deserializer: D) -> Result<Option<Option<T>>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
Option::<T>::deserialize(deserializer).map(Some)
}
#[derive(Debug, Clone, Default, bon::Builder)]
#[builder(on(String, into))]
pub struct MakeProgrammaticToolCallerOptions {
pub allowed_tools: Option<Vec<String>>,
pub name: Option<String>,
pub description: Option<String>,
pub timeout: Option<Option<Duration>>,
#[builder(default)]
pub isolation: Isolation,
}
#[derive(Clone)]
pub struct ProgrammaticToolCaller {
inner: Arc<Inner>,
}
struct Inner {
name: String,
spec: ToolSpec,
allowed_tools: Option<Vec<String>>,
timeout: Option<Duration>,
isolation: Isolation,
limits: Limits,
threaded: bool,
}
static INSTANCES: Mutex<Vec<Weak<Inner>>> = Mutex::new(Vec::new());
fn register(inner: &Arc<Inner>) {
let mut instances = INSTANCES.lock().unwrap_or_else(PoisonError::into_inner);
instances.retain(|instance| instance.strong_count() > 0);
instances.push(Arc::downgrade(inner));
}
fn is_caller(tool: &dyn Tool) -> bool {
let name = tool.name().as_ptr();
INSTANCES
.lock()
.unwrap_or_else(PoisonError::into_inner)
.iter()
.filter_map(Weak::upgrade)
.any(|inner| std::ptr::eq(inner.name.as_ptr(), name))
}
#[must_use]
pub fn programmatic_tool_caller(config: ProgrammaticToolCallerConfig) -> ProgrammaticToolCaller {
let timeout = config
.timeout
.map(|timeout| timeout.map(|seconds| Duration::try_from_secs_f64(seconds).unwrap_or(Duration::ZERO)));
make_programmatic_tool_caller(MakeProgrammaticToolCallerOptions {
allowed_tools: config.allowed_tools,
timeout,
..MakeProgrammaticToolCallerOptions::default()
})
}
#[must_use]
pub fn make_programmatic_tool_caller(options: MakeProgrammaticToolCallerOptions) -> ProgrammaticToolCaller {
let name = options.name.unwrap_or_else(|| DEFAULT_NAME.to_owned());
let description = options
.description
.unwrap_or_else(|| DEFAULT_PROGRAMMATIC_TOOL_CALLER_DESCRIPTION.to_owned());
let schema = json!({
"type": "object",
"properties": {"code": {"type": "string", "description": CODE_DESCRIPTION}},
"required": ["code"],
"additionalProperties": false
});
let inner = Arc::new(Inner {
spec: ToolSpec::new(&name, description).with_input_schema(schema),
name,
allowed_tools: options.allowed_tools,
timeout: options
.timeout
.unwrap_or(Some(DEFAULT_PROGRAMMATIC_TOOL_CALLER_TIMEOUT)),
isolation: options.isolation,
limits: LIMITS,
threaded: true,
});
register(&inner);
ProgrammaticToolCaller { inner }
}
impl ProgrammaticToolCaller {
#[cfg(test)]
fn with_limits(self, limits: Limits, isolation: Isolation, threaded: bool) -> Self {
let inner = Arc::new(Inner {
name: self.inner.name.clone(),
spec: self.inner.spec.clone(),
allowed_tools: self.inner.allowed_tools.clone(),
timeout: self.inner.timeout,
isolation,
limits,
threaded,
});
register(&inner);
Self { inner }
}
}
impl fmt::Debug for ProgrammaticToolCaller {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProgrammaticToolCaller")
.field("name", &self.inner.name)
.field("allowed_tools", &self.inner.allowed_tools)
.field("timeout", &self.inner.timeout)
.field("isolation", &self.inner.isolation)
.finish_non_exhaustive()
}
}
impl Tool for ProgrammaticToolCaller {
fn name(&self) -> &str {
&self.inner.name
}
fn description(&self) -> &str {
&self.inner.spec.description
}
fn tool_spec(&self) -> ToolSpec {
self.inner.spec.clone()
}
fn stream(&self, context: ToolContext) -> ToolStream {
let inner = self.inner.clone();
ToolStream::from_future(async move {
let tool_use_id = context.tool_use().tool_use_id.clone();
let result = match inner.execute(&context).await {
Ok(text) => ToolResultBlock::success(&tool_use_id, vec![ToolResultContent::text(text)]),
Err(message) => {
ToolResultBlock::error(&tool_use_id, vec![ToolResultContent::text(format!("Error: {message}"))])
}
};
ToolOutcome::from(result)
})
}
}
enum Stop {
Cancelled,
TimedOut,
Failed(String),
}
enum Engine {
InProcess,
Worker { path: PathBuf, explicit: bool },
}
static WARNED_IN_PROCESS: AtomicBool = AtomicBool::new(false);
fn warn_in_process(reason: &str) {
if !WARNED_IN_PROCESS.swap(true, Ordering::Relaxed) {
tracing::warn!(
"reason=<{reason}> | programmatic_tool_caller runs the interpreter in-process with no crash isolation and no cap on its total memory, install the worker with cargo install monty-runtime --version {MONTY_WORKER_VERSION} --locked or set {MONTY_WORKER_ENV}"
);
}
}
struct WorkerSearch<'a> {
path: Option<&'a std::ffi::OsStr>,
exe: Option<&'a Path>,
}
const WORKSPACE_SEARCH_DEPTH: usize = 6;
fn worker_binary_name() -> String {
format!("monty{}", std::env::consts::EXE_SUFFIX)
}
fn worker_on_path(path: Option<&std::ffi::OsStr>) -> Option<PathBuf> {
let binary = worker_binary_name();
std::env::split_paths(path?)
.map(|dir| dir.join(&binary))
.find(|candidate| candidate.is_file())
}
fn worker_in_workspace(exe: Option<&Path>) -> Option<PathBuf> {
let binary = worker_binary_name();
exe?.ancestors()
.skip(1)
.take(WORKSPACE_SEARCH_DEPTH)
.filter(|dir| dir.join("Cargo.toml").is_file())
.flat_map(|dir| ["debug", "release"].map(|profile| dir.join("target").join(profile).join(&binary)))
.find(|candidate| candidate.is_file())
}
impl Isolation {
fn engine(&self, env: &Env, search: &WorkerSearch<'_>) -> Engine {
match self {
Self::InProcess => Engine::InProcess,
Self::Subprocess(path) => Engine::Worker {
path: path.clone(),
explicit: true,
},
Self::Auto => auto_engine(env, search),
}
}
}
fn auto_engine(env: &Env, search: &WorkerSearch<'_>) -> Engine {
if let Some(named) = env.get(MONTY_WORKER_ENV).filter(|named| !named.is_empty()) {
let path = PathBuf::from(named);
if path.is_file() {
return Engine::Worker { path, explicit: true };
}
tracing::debug!(
"{MONTY_WORKER_ENV}=<{}> | no file at the named monty worker",
path.display()
);
}
let found = worker_on_path(search.path).or_else(|| worker_in_workspace(search.exe));
match found {
Some(path) => Engine::Worker { path, explicit: false },
None => Engine::InProcess,
}
}
impl Inner {
async fn execute(&self, context: &ToolContext) -> Result<String, String> {
let Some(code) = context.tool_use().input.get("code").and_then(JsonValue::as_str) else {
return Err("Invalid input: expected string for \"code\".".to_owned());
};
let cancel = context.cancel_signal().clone();
let output = Arc::new(Output::default());
let host = Host {
agent: context.agent().clone(),
functions: Arc::new(self.functions(context.agent())),
slots: Arc::new(Semaphore::new(MAX_CONCURRENT_TOOL_CALLS)),
output: output.clone(),
cancel: cancel.clone(),
limits: self.limits,
};
let deadline = async {
match self.timeout {
Some(timeout) => tokio::time::sleep(timeout).await,
None => futures::future::pending().await,
}
};
let outcome = if cancel.is_cancelled() {
Err(Stop::Cancelled)
} else {
tokio::select! {
biased;
() = cancel.cancelled() => Err(Stop::Cancelled),
() = deadline => Err(Stop::TimedOut),
outcome = self.drive(&host, code.to_owned()) => outcome,
}
};
match outcome {
Ok(()) => {
let text = output.text();
Ok(if text.is_empty() {
"(no output)".to_owned()
} else {
text
})
}
Err(Stop::Cancelled) => Err("Execution cancelled.".to_owned()),
Err(Stop::TimedOut) => {
let seconds = self.timeout.map_or(0.0, |timeout| timeout.as_secs_f64());
Err(output.with_error(&format!("Execution error: timed out after {seconds} seconds.")))
}
Err(Stop::Failed(message)) => Err(message),
}
}
async fn drive(&self, host: &Host, code: String) -> Result<(), Stop> {
let path = std::env::var_os("PATH");
let exe = std::env::current_exe().ok();
let search = WorkerSearch {
path: path.as_deref(),
exe: exe.as_deref(),
};
match self.isolation.engine(&Env::process(), &search) {
Engine::InProcess => {
if self.isolation == Isolation::Auto {
warn_in_process("no monty worker found");
}
in_process::drive(host, code, self.threaded).await
}
Engine::Worker { path, explicit } => self.drive_worker(host, code, &path, explicit).await,
}
}
#[cfg(feature = "monty-pool")]
async fn drive_worker(&self, host: &Host, code: String, path: &Path, explicit: bool) -> Result<(), Stop> {
match worker::drive(host, &code, path).await {
Err(worker::Failure::Unavailable(error)) if !explicit => {
warn_in_process(&format!(
"the monty worker at {} did not start: {error}",
path.display()
));
in_process::drive(host, code, self.threaded).await
}
Err(worker::Failure::Unavailable(error)) => Err(Stop::Failed(host.output.with_error(&format!(
"Execution error: the monty worker at {} did not start: {error}",
path.display()
)))),
Err(worker::Failure::Stopped(stop)) => Err(stop),
Ok(()) => Ok(()),
}
}
#[cfg(not(feature = "monty-pool"))]
async fn drive_worker(&self, host: &Host, code: String, path: &Path, explicit: bool) -> Result<(), Stop> {
if explicit {
return Err(Stop::Failed(format!(
"Execution error: running the interpreter in the monty worker at {} needs the strands-harness feature \"monty-pool\".",
path.display()
)));
}
warn_in_process("the strands-harness feature \"monty-pool\" is off");
in_process::drive(host, code, self.threaded).await
}
fn functions(&self, agent: &ToolAgent) -> HashMap<String, String> {
let available: Vec<String> = agent
.tool_registry()
.list()
.into_iter()
.filter(|tool| !is_caller(tool.as_ref()))
.map(|tool| tool.name().to_owned())
.filter(|name| self.allowed_tools.as_ref().is_none_or(|allowed| allowed.contains(name)))
.collect();
let mut functions: HashMap<String, String> =
available.iter().map(|name| (name.clone(), name.clone())).collect();
let mut sorted = available.clone();
sorted.sort();
let mut aliases: BTreeMap<String, String> = BTreeMap::new();
let mut ambiguous: HashSet<String> = HashSet::new();
for name in sorted {
if is_identifier(&name) {
continue;
}
let alias: String = name
.chars()
.map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' })
.collect();
if !is_identifier(&alias) || available.contains(&alias) {
continue;
}
if aliases.contains_key(&alias) {
ambiguous.insert(alias);
continue;
}
aliases.insert(alias, name);
}
for alias in &ambiguous {
aliases.remove(alias);
tracing::warn!("alias=<{alias}> | multiple tools normalize to the alias, no alias injected");
}
functions.extend(aliases);
functions
}
}
fn is_identifier(name: &str) -> bool {
let mut chars = name.chars();
chars.next().is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}
type CallResult = Result<MontyObject, MontyException>;
type Pending = FuturesUnordered<BoxFuture<'static, (u32, CallResult)>>;
struct Host {
agent: ToolAgent,
functions: Arc<HashMap<String, String>>,
slots: Arc<Semaphore>,
output: Arc<Output>,
cancel: CancelSignal,
limits: Limits,
}
impl Host {
fn call_future(&self, call_id: u32, tool_name: String, input: JsonValue) -> BoxFuture<'static, (u32, CallResult)> {
let agent = self.agent.clone();
let slots = self.slots.clone();
let cancel = self.cancel.clone();
async move {
let _slot = slots.acquire_owned().await;
if cancel.is_cancelled() {
return (call_id, Err(runtime_error(CANCELLED_CALL)));
}
let refused_name = tool_name.clone();
let result = agent
.call_tool_without_interrupts(&tool_name, input, move |error| approval_refusal(&refused_name, error))
.await;
(call_id, call_result(&tool_name, result))
}
.boxed()
}
fn lookup(&self, name: &str) -> Option<MontyObject> {
self.functions
.contains_key(name)
.then(|| MontyObject::function(name.to_owned(), None))
}
}
fn prepare_call(
functions: &HashMap<String, String>,
function: &str,
args: &CallArgs,
) -> Result<(String, JsonValue), MontyException> {
let Some(tool_name) = functions.get(function) else {
return Err(MontyException::new(
ExcType::NameError,
Some(format!("name '{function}' is not defined")),
));
};
if args.args().next().is_some() {
return Err(type_error(format!(
"{function}() takes keyword arguments only, e.g. {function}(key=value)"
)));
}
let mut input = serde_json::Map::new();
for (key, value) in args.kwargs() {
let value = to_json(value, 0).ok_or_else(|| {
type_error(format!(
"{function}() argument is nested more than {MAX_VALUE_DEPTH} levels deep"
))
})?;
input.insert(key.as_str().map_or_else(|| key.to_string(), str::to_owned), value);
}
Ok((tool_name.clone(), JsonValue::Object(input)))
}
fn type_error(message: String) -> MontyException {
MontyException::new(ExcType::TypeError, Some(message))
}
fn runtime_error(message: impl Into<String>) -> MontyException {
MontyException::new(ExcType::RuntimeError, Some(message.into()))
}
fn call_result(tool_name: &str, result: Result<ToolResultBlock, RefusedCall>) -> CallResult {
let result = match result {
Ok(result) => result,
Err(refused) => return Err(runtime_error(refusal(tool_name, &refused))),
};
if result.status == ToolResultStatus::Error {
let text = extract_text(&result.content);
let text = if text.is_empty() {
"Unknown error".to_owned()
} else {
text
};
return Err(runtime_error(format!("Tool '{tool_name}' error: {text}")));
}
to_monty(&unwrap_result(&result), 0).ok_or_else(|| {
runtime_error(format!(
"Tool '{tool_name}' returned a result nested more than {MAX_VALUE_DEPTH} levels deep"
))
})
}
fn refusal(tool_name: &str, refused: &RefusedCall) -> String {
let text = match &refused.result {
Some(result) => extract_text(&result.content),
None => String::new(),
};
let text = if !text.is_empty() {
text
} else if refused.result.is_some() {
approval_refusal(tool_name, &refused.interrupts)
} else {
interrupt_refusal(tool_name, &refused.interrupts)
};
format!("Failed to execute tool '{tool_name}': {text}")
}
fn interrupt_refusal(tool_name: &str, error: &InterruptError) -> String {
match error.interrupts.first() {
Some(interrupt) if interrupt.source != InterruptSource::Tool => approval_refusal(tool_name, error),
_ => NO_INNER_INTERRUPTS.to_owned(),
}
}
fn approval_refusal(tool_name: &str, error: &InterruptError) -> String {
let approval = match error
.interrupts
.first()
.map(|interrupt| (&interrupt.reason, &interrupt.name))
{
Some((Some(JsonValue::String(reason)), _)) => reason.clone(),
Some((Some(JsonValue::Null) | None, name)) => name.clone(),
Some((Some(reason), _)) => reason.to_string(),
None => "no reason given".to_owned(),
};
format!(
"Tool '{tool_name}' needs approval ({approval}), which programmatic_tool_caller cannot prompt for; call it as a normal tool call instead."
)
}
fn extract_text(content: &[ToolResultContent]) -> String {
content
.iter()
.filter_map(|block| match block {
ToolResultContent::Text(text) => Some(text.text.clone()),
ToolResultContent::Json(json) => Some(json.json.to_string()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n")
}
fn unwrap_result(result: &ToolResultBlock) -> JsonValue {
if let [ToolResultContent::Json(json)] = result.content.as_slice() {
return json.json.clone();
}
let text = extract_text(&result.content);
if result.content.len() == 1
&& (text.starts_with('{') || text.starts_with('['))
&& let Ok(value) = serde_json::from_str(&text)
{
return value;
}
JsonValue::String(text)
}
fn to_monty(value: &JsonValue, depth: usize) -> Option<MontyObject> {
if depth > MAX_VALUE_DEPTH {
return None;
}
Some(match value {
JsonValue::Null => MontyObject::none(),
JsonValue::Bool(value) => MontyObject::bool(*value),
JsonValue::Number(number) => number.as_i64().map_or_else(
|| MontyObject::float(number.as_f64().unwrap_or(f64::NAN)),
MontyObject::int,
),
JsonValue::String(text) => MontyObject::string(text.clone()),
JsonValue::Array(items) => MontyObject::list(
items
.iter()
.map(|item| to_monty(item, depth + 1))
.collect::<Option<Vec<_>>>()?,
),
JsonValue::Object(map) => MontyObject::dict(
map.iter()
.map(|(key, value)| Some((MontyObject::string(key.clone()), to_monty(value, depth + 1)?)))
.collect::<Option<Vec<_>>>()?,
),
})
}
fn to_json(value: ObjectRef<'_>, depth: usize) -> Option<JsonValue> {
if depth > MAX_VALUE_DEPTH {
return None;
}
let number = |value: Option<f64>| {
value
.and_then(serde_json::Number::from_f64)
.map_or(JsonValue::Null, JsonValue::Number)
};
Some(match value.type_name() {
"NoneType" => JsonValue::Null,
"bool" => value.as_bool().map_or(JsonValue::Null, JsonValue::Bool),
"int" => value.as_int().map_or_else(|| number(value.as_float()), JsonValue::from),
"float" => number(value.as_float()),
"str" => JsonValue::String(value.as_str().unwrap_or_default().to_owned()),
"dict" if value.pairs().is_some() => JsonValue::Object(
value
.pairs()
.into_iter()
.flatten()
.map(|(key, value)| {
let key = key.as_str().map_or_else(|| key.to_string(), str::to_owned);
Some((key, to_json(value, depth + 1)?))
})
.collect::<Option<_>>()?,
),
_ => match value.items() {
Some(items) => JsonValue::Array(
items
.into_iter()
.map(|item| to_json(item, depth + 1))
.collect::<Option<_>>()?,
),
None => JsonValue::String(value.to_string()),
},
})
}
#[derive(Debug, Default)]
struct Output {
buffer: Mutex<OutputBuffer>,
}
#[derive(Debug, Default)]
struct OutputBuffer {
text: String,
chars: usize,
}
impl Output {
fn write(&self, text: &str) {
let mut buffer = self.buffer.lock().unwrap_or_else(PoisonError::into_inner);
let room = (MAX_OUTPUT_CHARS + 1).saturating_sub(buffer.chars);
if room == 0 {
return;
}
let kept: String = text.chars().take(room).collect();
buffer.chars += kept.chars().count();
buffer.text.push_str(&kept);
}
fn raw(&self) -> String {
self.buffer.lock().unwrap_or_else(PoisonError::into_inner).text.clone()
}
fn text(&self) -> String {
cap(&self.raw(), MAX_OUTPUT_CHARS)
}
fn with_error(&self, message: &str) -> String {
let raw = self.raw();
let raw_chars = raw.chars().count();
let message_limit = MAX_OUTPUT_CHARS.saturating_sub(raw_chars).max(MAX_OUTPUT_CHARS / 2);
let capped = cap(message, message_limit);
let printed = cap(&raw, MAX_OUTPUT_CHARS - capped.chars().count().min(message_limit));
if printed.is_empty() {
capped
} else {
format!("{printed}\n\n{capped}")
}
}
}
struct Printer(Arc<Output>);
impl PrintWriterCallback for Printer {
fn stdout_write(&mut self, output: Cow<'_, str>) -> Result<(), MontyException> {
self.0.write(&output);
Ok(())
}
fn stdout_push(&mut self, end: char) -> Result<(), MontyException> {
self.0.write(end.encode_utf8(&mut [0; 4]));
Ok(())
}
}
fn cap(raw: &str, limit: usize) -> String {
if raw.chars().count() <= limit {
return raw.trim().to_owned();
}
let kept: String = raw.chars().take(limit).collect();
format!("{}\n[output truncated at {limit} characters]", kept.trim())
}
#[cfg(test)]
mod tests;