pub const EFFORT_LEVELS: &[&str] = &["minimal", "low", "medium", "high", "xhigh", "max"];
pub const DEFAULT_EFFORT_LEVELS: &[&str] = &["low", "medium", "high"];
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ThinkingControls {
pub thinks: bool,
pub enable_thinking: bool,
pub efforts: Vec<&'static str>,
}
impl ThinkingControls {
pub fn choices(&self) -> Vec<&'static str> {
if !self.thinks {
return Vec::new();
}
let mut choices = vec!["none"];
if !self.efforts.is_empty() {
choices.extend(&self.efforts);
} else if self.enable_thinking {
choices.push("thinking");
}
choices
}
}
pub fn thinking_controls(template: &str) -> ThinkingControls {
let template = strip_comments(template);
let lower = template.to_ascii_lowercase();
let thinks = lower.contains("think") || lower.contains("reasoning");
if !thinks {
return ThinkingControls::default();
}
let enable_thinking = template.contains("enable_thinking");
let efforts = if template.contains("reasoning_effort") {
let named: Vec<&'static str> = EFFORT_LEVELS
.iter()
.copied()
.filter(|level| {
blocks(&template)
.filter(|block| block.contains("reasoning_effort"))
.any(|block| quoted_literals(block).any(|lit| lit == *level))
})
.collect();
if named.len() >= 2 {
named
} else {
DEFAULT_EFFORT_LEVELS.to_vec()
}
} else {
Vec::new()
};
ThinkingControls {
thinks,
enable_thinking,
efforts,
}
}
fn strip_comments(template: &str) -> String {
let mut out = String::with_capacity(template.len());
let mut rest = template;
while let Some(start) = rest.find("{#") {
out.push_str(&rest[..start]);
match rest[start..].find("#}") {
Some(end) => rest = &rest[start + end + 2..],
None => return out,
}
}
out.push_str(rest);
out
}
fn blocks(template: &str) -> impl Iterator<Item = &str> {
let mut rest = template;
std::iter::from_fn(move || {
let start = rest.find("{%").into_iter().chain(rest.find("{{")).min()?;
let close = if rest[start..].starts_with("{%") {
"%}"
} else {
"}}"
};
let end = rest[start + 2..].find(close)?;
let block = &rest[start + 2..start + 2 + end];
rest = &rest[start + 2 + end + 2..];
Some(block)
})
}
fn quoted_literals(text: &str) -> impl Iterator<Item = &str> {
let mut rest = text;
std::iter::from_fn(move || {
let start = rest.find(['\'', '"'])?;
let quote = rest.as_bytes()[start] as char;
let after = &rest[start + 1..];
let end = after.find(quote)?;
rest = &after[end + 1..];
Some(&after[..end])
})
}
#[cfg(test)]
mod tests {
use super::*;
const QWEN3_8: &str = r#"
{%- if enable_thinking is undefined or enable_thinking is true %}
{%- set resolved_reasoning_effort = reasoning_effort|default('xhigh') %}
{%- if resolved_reasoning_effort == 'high' %}
{%- set resolved_reasoning_effort = 'xhigh' %}
{%- endif %}
{%- if resolved_reasoning_effort not in ('xhigh', 'medium', 'low') %}
{{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported types are xhigh (default), medium, and low.') }}
{%- endif %}
{%- endif %}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '<think>\n\n</think>\n\n' }}
{%- endif %}
"#;
#[test]
fn qwen3_8_names_its_levels_and_its_switch() {
let controls = thinking_controls(QWEN3_8);
assert_eq!(
controls,
ThinkingControls {
thinks: true,
enable_thinking: true,
efforts: vec!["low", "medium", "high", "xhigh"],
}
);
assert_eq!(
controls.choices(),
["none", "low", "medium", "high", "xhigh"]
);
let one_line = QWEN3_8.replace('\n', "");
assert_eq!(thinking_controls(&one_line), controls);
}
#[test]
fn a_template_that_names_no_choices_gets_the_default_levels() {
let gpt_oss = r#"
{%- if reasoning_effort is not defined %}{%- set reasoning_effort = "medium" %}{%- endif %}
{{- "Reasoning: " + reasoning_effort + "\n\n" }}
"#;
assert_eq!(
thinking_controls(gpt_oss).efforts,
DEFAULT_EFFORT_LEVELS.to_vec()
);
}
#[test]
fn a_switch_alone_offers_off_and_on() {
let gemma4 = "{%- set enable_thinking = enable_thinking | default(false) -%}\
{%- if enable_thinking -%}<|think|>{%- endif -%}";
let controls = thinking_controls(gemma4);
assert!(controls.enable_thinking && controls.efforts.is_empty());
assert_eq!(controls.choices(), ["none", "thinking"]);
}
#[test]
fn thinking_without_a_kwarg_offers_only_off() {
let kimi =
"{%- if thinking is defined and thinking is false -%}<think></think>{%- endif -%}";
assert_eq!(thinking_controls(kimi).choices(), ["none"]);
let minimax = "{%- set reasoning_content = content.split('</think>')[0] -%}";
assert_eq!(thinking_controls(minimax).choices(), ["none"]);
}
#[test]
fn a_plain_template_has_no_controls() {
let llama = "{# no thinking here #}{{- '<|start_header_id|>' + message['role'] }}";
assert_eq!(thinking_controls(llama), ThinkingControls::default());
assert!(thinking_controls(llama).choices().is_empty());
assert_eq!(thinking_controls(""), ThinkingControls::default());
}
#[test]
fn levels_are_ordered_deduplicated_and_scoped_to_the_variables_blocks() {
let t = r#"{%- if reasoning_effort == "max" or reasoning_effort == "low" or reasoning_effort == "none" %}
{%- if reasoning_effort == 'low' %}{%- set unrelated = 'high' %}"#;
assert_eq!(thinking_controls(t).efforts, vec!["low", "max"]);
}
#[test]
fn blocks_and_literals_are_walked_in_order() {
assert_eq!(
blocks("a {% if x %}b{{ y }}c {% end").collect::<Vec<_>>(),
[" if x ", " y "]
);
assert_eq!(
quoted_literals(r#"x in ('a', "b c") ~ 'd"#).collect::<Vec<_>>(),
["a", "b c"]
);
assert_eq!(strip_comments("a{# b #}c{# open"), "ac");
}
}