Share linked tool mention parsing in the TUI (#41218)

## What changed

- Reuse the mention codec's linked-tool parser and mention-name helpers when extracting skill mentions.
- Remove the duplicate parser and environment-variable filter from the skill mention code.

## Testing

- Cover Unicode byte offsets, whitespace-trimmed paths, both supported sigils, and malformed linked mentions.

GitOrigin-RevId: 4ed3627f0caf90d49bcbc0681ff9d9b43b3736e3
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-08-27 23:10:10 +00:00
1 parent bae69125df
commit 4fea523466
2 files changed
+58 -94

No files matched your search

+54 -13
View File
@@ -1,3 +1,5 @@
//! Shared tool-mention syntax and history encoding. Callers decide which linked paths to accept.
use std::collections::HashMap;
use std::collections::VecDeque;
@@ -149,7 +151,7 @@ pub(crate) fn decode_history_mentions_with_at_mentions(
}
if bytes[index] == b'['
&& let Some((sigil, name, path, end_index)) =
parse_history_linked_mention(text, bytes, index, at_mentions_enabled)
parse_history_linked_mention(text, index, at_mentions_enabled)
{
out.push(sigil);
out.push_str(name);
@@ -176,16 +178,15 @@ pub(crate) fn decode_history_mentions_with_at_mentions(
}
}
fn parse_history_linked_mention<'a>(
text: &'a str,
text_bytes: &[u8],
fn parse_history_linked_mention(
text: &str,
start: usize,
at_mentions_enabled: bool,
) -> Option<(char, &'a str, &'a str, usize)> {
) -> Option<(char, &str, &str, usize)> {
// TUI historically wrote `$name`, but selected unified `@` mentions should preserve `@` on
// history round-trip for any canonical tool path.
if let Some((name, path, end_index)) =
parse_linked_tool_mention(text, text_bytes, start, TOOL_MENTION_SIGIL)
parse_linked_tool_mention(text, start, TOOL_MENTION_SIGIL)
&& !is_common_env_var(name)
&& is_tool_path(path)
{
@@ -194,14 +195,14 @@ fn parse_history_linked_mention<'a>(
if at_mentions_enabled {
if let Some((name, path, end_index)) =
parse_linked_tool_mention(text, text_bytes, start, PLUGIN_TEXT_MENTION_SIGIL)
parse_linked_tool_mention(text, start, PLUGIN_TEXT_MENTION_SIGIL)
&& !is_common_env_var(name)
&& is_tool_path(path)
{
return Some((PLUGIN_TEXT_MENTION_SIGIL, name, path, end_index));
}
} else if let Some((name, path, end_index)) =
parse_linked_tool_mention(text, text_bytes, start, PLUGIN_TEXT_MENTION_SIGIL)
parse_linked_tool_mention(text, start, PLUGIN_TEXT_MENTION_SIGIL)
&& !is_common_env_var(name)
&& path.starts_with("plugin://")
{
@@ -211,12 +212,13 @@ fn parse_history_linked_mention<'a>(
None
}
fn parse_linked_tool_mention<'a>(
text: &'a str,
text_bytes: &[u8],
/// Parse a linked mention at an opening `[` byte, returning its name, path, and end offset.
pub(crate) fn parse_linked_tool_mention(
text: &str,
start: usize,
sigil: char,
) -> Option<(&'a str, &'a str, usize)> {
) -> Option<(&str, &str, usize)> {
let text_bytes = text.as_bytes();
let sigil_index = start + 1;
if text_bytes.get(sigil_index) != Some(&(sigil as u8)) {
return None;
@@ -268,7 +270,7 @@ fn parse_linked_tool_mention<'a>(
Some((name, path, path_end + 1))
}
fn is_mention_name_char(byte: u8) -> bool {
pub(crate) fn is_mention_name_char(byte: u8) -> bool {
matches!(byte, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' | b'-')
}
@@ -335,6 +337,45 @@ mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn parse_linked_mentions_preserves_byte_offsets_and_trims_paths() {
let prefix = "before 🦀 ";
for sigil in [TOOL_MENTION_SIGIL, PLUGIN_TEXT_MENTION_SIGIL] {
let text =
format!("{prefix}[{sigil}tool_1-name] \n\t( skill://路径/SKILL.md ) trailing");
assert_eq!(
parse_linked_tool_mention(&text, prefix.len(), sigil),
Some((
"tool_1-name",
"skill://路径/SKILL.md",
text.len() - " trailing".len()
))
);
}
}
#[test]
fn parse_linked_mentions_rejects_malformed_links() {
for text in [
"[",
"[$",
"[$](app://tool)",
"[$tool.name](app://tool)",
"[$tool(app://tool)",
"[$tool]app://tool)",
"[$tool](app://tool",
"[$tool]()",
"[$tool]( \t )",
"[@tool](app://tool)",
] {
assert_eq!(
parse_linked_tool_mention(text, /*start*/ 0, TOOL_MENTION_SIGIL),
None,
"{text}"
);
}
}
#[test]
fn decode_history_mentions_restores_visible_tokens() {
let decoded = decode_history_mentions_with_at_mentions(