user shell command filtering

This commit is contained in:
kevin zhao
2025-11-04 15:56:16 -08:00
parent f0eb141e02
commit 459c53c45a
2 changed files with 12 additions and 7 deletions
+8 -2
View File
@@ -15,6 +15,12 @@ use uuid::Uuid;
use crate::user_instructions::UserInstructions;
fn is_user_shell_command_prefix(text: &str) -> bool {
let trimmed = text.trim_start();
let lowered = trimmed.to_ascii_lowercase();
lowered.starts_with("<user_shell_command>") || lowered.starts_with("<user_shell_command_output>")
}
fn is_session_prefix(text: &str) -> bool {
let trimmed = text.trim_start();
let lowered = trimmed.to_ascii_lowercase();
@@ -31,7 +37,7 @@ fn parse_user_message(message: &[ContentItem]) -> Option<UserMessageItem> {
for content_item in message.iter() {
match content_item {
ContentItem::InputText { text } => {
if is_session_prefix(text) {
if is_session_prefix(text) || is_user_shell_command_prefix(text) {
return None;
}
content.push(UserInput::Text { text: text.clone() });
@@ -42,7 +48,7 @@ fn parse_user_message(message: &[ContentItem]) -> Option<UserMessageItem> {
});
}
ContentItem::OutputText { text } => {
if is_session_prefix(text) {
if is_session_prefix(text) || is_user_shell_command_prefix(text) {
return None;
}
warn!("Output text in user message: {}", text);
+4 -5
View File
@@ -95,10 +95,9 @@ impl SessionTask for UserShellCommandTask {
}
let call_id = Uuid::new_v4().to_string();
let cwd_display = turn_context.cwd.to_string_lossy();
let raw_command = self.command.clone();
let command_text = format!(
"<user_shell_command cwd=\"{cwd_display}\">\n{raw_command}\n</user_shell_command>"
"<user_shell_command>\n{raw_command}\n</user_shell_command>"
);
let command_items = [build_user_message(command_text)];
session
@@ -153,7 +152,7 @@ impl SessionTask for UserShellCommandTask {
None => {
let aborted_message = "command aborted by user".to_string();
let aborted_text =
format!("<user_shell_output>\n{aborted_message}\n</user_shell_output>");
format!("<user_shell_command_output>\n{aborted_message}\n</user_shell_command_output>");
let output_items = [build_user_message(aborted_text)];
session
.record_conversation_items(turn_context.as_ref(), &output_items)
@@ -191,7 +190,7 @@ impl SessionTask for UserShellCommandTask {
let output_payload = format_exec_output_for_model(&output);
let output_text =
format!("<user_shell_output>\n{output_payload}\n</user_shell_output>");
format!("<user_shell_command_output>\n{output_payload}\n</user_shell_command_output>");
let output_items = [build_user_message(output_text)];
session
.record_conversation_items(turn_context.as_ref(), &output_items)
@@ -224,7 +223,7 @@ impl SessionTask for UserShellCommandTask {
.await;
let output_payload = format_exec_output_for_model(&exec_output);
let output_text =
format!("<user_shell_output>\n{output_payload}\n</user_shell_output>");
format!("<user_shell_command_output>\n{output_payload}\n</user_shell_command_output>");
let output_items = [build_user_message(output_text)];
session
.record_conversation_items(turn_context.as_ref(), &output_items)