mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Polish remembered thread context
This commit is contained in:
@@ -23,11 +23,16 @@ pub(crate) fn encode_history_mentions(text: &str, mentions: &[LinkedMention]) ->
|
||||
}
|
||||
|
||||
let mut mentions_by_name: HashMap<&str, VecDeque<&str>> = HashMap::new();
|
||||
let mut thread_mentions = VecDeque::new();
|
||||
for mention in mentions {
|
||||
mentions_by_name
|
||||
.entry(mention.mention.as_str())
|
||||
.or_default()
|
||||
.push_back(mention.path.as_str());
|
||||
if mention.path.starts_with("thread://") {
|
||||
thread_mentions.push_back(mention);
|
||||
} else {
|
||||
mentions_by_name
|
||||
.entry(mention.mention.as_str())
|
||||
.or_default()
|
||||
.push_back(mention.path.as_str());
|
||||
}
|
||||
}
|
||||
|
||||
let bytes = text.as_bytes();
|
||||
@@ -35,6 +40,27 @@ pub(crate) fn encode_history_mentions(text: &str, mentions: &[LinkedMention]) ->
|
||||
let mut index = 0usize;
|
||||
|
||||
while index < bytes.len() {
|
||||
if bytes[index] == b'#'
|
||||
&& let Some((queue_index, _mention)) = thread_mentions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, mention)| {
|
||||
!mention.mention.is_empty()
|
||||
&& text[index + 1..].starts_with(mention.mention.as_str())
|
||||
})
|
||||
.max_by_key(|(_, mention)| mention.mention.len())
|
||||
&& let Some(mention) = thread_mentions.remove(queue_index)
|
||||
{
|
||||
out.push('[');
|
||||
out.push('#');
|
||||
out.push_str(mention.mention.as_str());
|
||||
out.push_str("](");
|
||||
out.push_str(mention.path.as_str());
|
||||
out.push(')');
|
||||
index += '#'.len_utf8() + mention.mention.len();
|
||||
continue;
|
||||
}
|
||||
|
||||
if bytes[index] == TOOL_MENTION_SIGIL as u8 {
|
||||
let name_start = index + 1;
|
||||
if let Some(first) = bytes.get(name_start)
|
||||
@@ -79,9 +105,10 @@ pub(crate) fn decode_history_mentions(text: &str) -> DecodedHistoryText {
|
||||
|
||||
while index < bytes.len() {
|
||||
if bytes[index] == b'['
|
||||
&& let Some((name, path, end_index)) = parse_history_linked_mention(text, bytes, index)
|
||||
&& let Some((sigil, name, path, end_index)) =
|
||||
parse_history_linked_mention(text, bytes, index)
|
||||
{
|
||||
out.push(TOOL_MENTION_SIGIL);
|
||||
out.push(sigil);
|
||||
out.push_str(name);
|
||||
mentions.push(LinkedMention {
|
||||
mention: name.to_string(),
|
||||
@@ -108,14 +135,15 @@ fn parse_history_linked_mention<'a>(
|
||||
text: &'a str,
|
||||
text_bytes: &[u8],
|
||||
start: usize,
|
||||
) -> Option<(&'a str, &'a str, usize)> {
|
||||
) -> Option<(char, &'a str, &'a str, usize)> {
|
||||
// TUI writes `$name`, but may read plugin `[@name](plugin://...)` links from other clients.
|
||||
if let Some(mention @ (name, path, _)) =
|
||||
parse_linked_tool_mention(text, text_bytes, start, TOOL_MENTION_SIGIL)
|
||||
&& !is_common_env_var(name)
|
||||
&& is_tool_path(path)
|
||||
{
|
||||
return Some(mention);
|
||||
let (name, path, end_index) = mention;
|
||||
return Some((TOOL_MENTION_SIGIL, name, path, end_index));
|
||||
}
|
||||
|
||||
if let Some(mention @ (name, path, _)) =
|
||||
@@ -123,7 +151,16 @@ fn parse_history_linked_mention<'a>(
|
||||
&& !is_common_env_var(name)
|
||||
&& path.starts_with("plugin://")
|
||||
{
|
||||
return Some(mention);
|
||||
let (name, path, end_index) = mention;
|
||||
return Some((TOOL_MENTION_SIGIL, name, path, end_index));
|
||||
}
|
||||
|
||||
if let Some(mention @ (_name, path, _)) =
|
||||
parse_linked_freeform_mention(text, text_bytes, start, '#')
|
||||
&& path.starts_with("thread://")
|
||||
{
|
||||
let (name, path, end_index) = mention;
|
||||
return Some(('#', name, path, end_index));
|
||||
}
|
||||
|
||||
None
|
||||
@@ -186,6 +223,57 @@ fn parse_linked_tool_mention<'a>(
|
||||
Some((name, path, path_end + 1))
|
||||
}
|
||||
|
||||
fn parse_linked_freeform_mention<'a>(
|
||||
text: &'a str,
|
||||
text_bytes: &[u8],
|
||||
start: usize,
|
||||
sigil: char,
|
||||
) -> Option<(&'a str, &'a str, usize)> {
|
||||
let sigil_index = start + 1;
|
||||
if text_bytes.get(sigil_index) != Some(&(sigil as u8)) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let name_start = sigil_index + 1;
|
||||
let mut name_end = name_start;
|
||||
while let Some(next_byte) = text_bytes.get(name_end)
|
||||
&& *next_byte != b']'
|
||||
{
|
||||
name_end += 1;
|
||||
}
|
||||
if name_end == name_start || text_bytes.get(name_end) != Some(&b']') {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut path_start = name_end + 1;
|
||||
while let Some(next_byte) = text_bytes.get(path_start)
|
||||
&& next_byte.is_ascii_whitespace()
|
||||
{
|
||||
path_start += 1;
|
||||
}
|
||||
if text_bytes.get(path_start) != Some(&b'(') {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut path_end = path_start + 1;
|
||||
while let Some(next_byte) = text_bytes.get(path_end)
|
||||
&& *next_byte != b')'
|
||||
{
|
||||
path_end += 1;
|
||||
}
|
||||
if text_bytes.get(path_end) != Some(&b')') {
|
||||
return None;
|
||||
}
|
||||
|
||||
let path = text[path_start + 1..path_end].trim();
|
||||
if path.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let name = &text[name_start..name_end];
|
||||
Some((name, path, path_end + 1))
|
||||
}
|
||||
|
||||
fn is_mention_name_char(byte: u8) -> bool {
|
||||
matches!(byte, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' | b'-')
|
||||
}
|
||||
@@ -278,6 +366,20 @@ mod tests {
|
||||
assert_eq!(decoded.mentions, Vec::<LinkedMention>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_history_mentions_restores_thread_links_with_hash_sigil() {
|
||||
let decoded = decode_history_mentions("Remember [#Favorite hobbies](thread://thread-1).");
|
||||
|
||||
assert_eq!(decoded.text, "Remember #Favorite hobbies.");
|
||||
assert_eq!(
|
||||
decoded.mentions,
|
||||
vec![LinkedMention {
|
||||
mention: "Favorite hobbies".to_string(),
|
||||
path: "thread://thread-1".to_string(),
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_history_mentions_links_bound_mentions_in_order() {
|
||||
let text = "$figma then $sample then $figma then $other";
|
||||
@@ -303,4 +405,26 @@ mod tests {
|
||||
"[$figma](app://figma-app) then [$sample](plugin://sample@test) then [$figma](/tmp/figma/SKILL.md) then $other"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_history_mentions_links_bound_thread_mentions() {
|
||||
let text = "Remember #Favorite hobbies then ask about #Travel";
|
||||
let encoded = encode_history_mentions(
|
||||
text,
|
||||
&[
|
||||
LinkedMention {
|
||||
mention: "Favorite hobbies".to_string(),
|
||||
path: "thread://thread-1".to_string(),
|
||||
},
|
||||
LinkedMention {
|
||||
mention: "Travel".to_string(),
|
||||
path: "thread://thread-2".to_string(),
|
||||
},
|
||||
],
|
||||
);
|
||||
assert_eq!(
|
||||
encoded,
|
||||
"Remember [#Favorite hobbies](thread://thread-1) then ask about [#Travel](thread://thread-2)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user