add clippy warning for string_slice (#5422)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
@@ -1365,17 +1365,9 @@ impl Agent {
|
||||
.unwrap_or(&content)
|
||||
.trim()
|
||||
.to_string();
|
||||
tracing::debug!(
|
||||
"Cleaned content for parsing: {}",
|
||||
&clean_content[..std::cmp::min(200, clean_content.len())]
|
||||
);
|
||||
|
||||
// try to parse json response from the LLM
|
||||
tracing::debug!("Attempting to parse recipe content as JSON");
|
||||
let (instructions, activities) =
|
||||
if let Ok(json_content) = serde_json::from_str::<Value>(&clean_content) {
|
||||
tracing::debug!("Successfully parsed JSON content");
|
||||
|
||||
let instructions = json_content
|
||||
.get("instructions")
|
||||
.ok_or_else(|| anyhow!("Missing 'instructions' in json response"))?
|
||||
|
||||
@@ -249,7 +249,7 @@ fn extract_json_from_line(line: &str) -> Option<String> {
|
||||
return None;
|
||||
}
|
||||
|
||||
let potential_json = &line[start..=end];
|
||||
let potential_json = line.get(start..=end)?;
|
||||
if serde_json::from_str::<Value>(potential_json).is_ok() {
|
||||
Some(potential_json.to_string())
|
||||
} else {
|
||||
|
||||
@@ -9,7 +9,7 @@ use tokio::process::Command;
|
||||
|
||||
use super::base::{ConfigKey, Provider, ProviderMetadata, ProviderUsage, Usage};
|
||||
use super::errors::ProviderError;
|
||||
use super::utils::RequestLog;
|
||||
use super::utils::{filter_extensions_from_system_prompt, RequestLog};
|
||||
use crate::config::{Config, GooseMode};
|
||||
use crate::conversation::message::{Message, MessageContent};
|
||||
use crate::model::ModelConfig;
|
||||
@@ -103,28 +103,6 @@ impl ClaudeCodeProvider {
|
||||
None
|
||||
}
|
||||
|
||||
/// Filter out the Extensions section from the system prompt
|
||||
fn filter_extensions_from_system_prompt(&self, system: &str) -> String {
|
||||
// Find the Extensions section and remove it
|
||||
if let Some(extensions_start) = system.find("# Extensions") {
|
||||
// Look for the next major section that starts with #
|
||||
let after_extensions = &system[extensions_start..];
|
||||
if let Some(next_section_pos) = after_extensions[1..].find("\n# ") {
|
||||
// Found next section, keep everything before Extensions and after the next section
|
||||
let before_extensions = &system[..extensions_start];
|
||||
let next_section_start = extensions_start + next_section_pos + 1;
|
||||
let after_next_section = &system[next_section_start..];
|
||||
format!("{}{}", before_extensions.trim_end(), after_next_section)
|
||||
} else {
|
||||
// No next section found, just remove everything from Extensions onward
|
||||
system[..extensions_start].trim_end().to_string()
|
||||
}
|
||||
} else {
|
||||
// No Extensions section found, return original
|
||||
system.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert goose messages to the format expected by claude CLI
|
||||
fn messages_to_claude_format(&self, _system: &str, messages: &[Message]) -> Result<Value> {
|
||||
let mut claude_messages = Vec::new();
|
||||
@@ -303,8 +281,7 @@ impl ClaudeCodeProvider {
|
||||
ProviderError::RequestFailed(format!("Failed to format messages: {}", e))
|
||||
})?;
|
||||
|
||||
// Create a filtered system prompt without Extensions section
|
||||
let filtered_system = self.filter_extensions_from_system_prompt(system);
|
||||
let filtered_system = filter_extensions_from_system_prompt(system);
|
||||
|
||||
if std::env::var("GOOSE_CLAUDE_CODE_DEBUG").is_ok() {
|
||||
println!("=== CLAUDE CODE PROVIDER DEBUG ===");
|
||||
|
||||
@@ -9,7 +9,7 @@ use tokio::process::Command;
|
||||
|
||||
use super::base::{ConfigKey, Provider, ProviderMetadata, ProviderUsage, Usage};
|
||||
use super::errors::ProviderError;
|
||||
use super::utils::RequestLog;
|
||||
use super::utils::{filter_extensions_from_system_prompt, RequestLog};
|
||||
use crate::conversation::message::{Message, MessageContent};
|
||||
use crate::model::ModelConfig;
|
||||
use rmcp::model::Tool;
|
||||
@@ -112,34 +112,11 @@ impl CursorAgentProvider {
|
||||
None
|
||||
}
|
||||
|
||||
/// Filter out the Extensions section from the system prompt
|
||||
fn filter_extensions_from_system_prompt(&self, system: &str) -> String {
|
||||
// Find the Extensions section and remove it
|
||||
if let Some(extensions_start) = system.find("# Extensions") {
|
||||
// Look for the next major section that starts with #
|
||||
let after_extensions = &system[extensions_start..];
|
||||
if let Some(next_section_pos) = after_extensions[1..].find("\n# ") {
|
||||
// Found next section, keep everything before Extensions and after the next section
|
||||
let before_extensions = &system[..extensions_start];
|
||||
let next_section_start = extensions_start + next_section_pos + 1;
|
||||
let after_next_section = &system[next_section_start..];
|
||||
format!("{}{}", before_extensions.trim_end(), after_next_section)
|
||||
} else {
|
||||
// No next section found, just remove everything from Extensions onward
|
||||
system[..extensions_start].trim_end().to_string()
|
||||
}
|
||||
} else {
|
||||
// No Extensions section found, return original
|
||||
system.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert goose messages to a simple prompt format for cursor-agent CLI
|
||||
fn messages_to_cursor_agent_format(&self, system: &str, messages: &[Message]) -> String {
|
||||
let mut full_prompt = String::new();
|
||||
|
||||
// Add system prompt
|
||||
let filtered_system = self.filter_extensions_from_system_prompt(system);
|
||||
let filtered_system = filter_extensions_from_system_prompt(system);
|
||||
full_prompt.push_str(&filtered_system);
|
||||
full_prompt.push_str("\n\n");
|
||||
|
||||
@@ -267,7 +244,7 @@ impl CursorAgentProvider {
|
||||
println!("Original system prompt length: {} chars", system.len());
|
||||
println!(
|
||||
"Filtered system prompt length: {} chars",
|
||||
self.filter_extensions_from_system_prompt(system).len()
|
||||
filter_extensions_from_system_prompt(system).len()
|
||||
);
|
||||
println!("Full prompt: {}", prompt);
|
||||
println!("Model: {}", self.model.model_name);
|
||||
|
||||
@@ -133,7 +133,9 @@ pub fn parse_streaming_response(sse_data: &str) -> Result<Message> {
|
||||
continue;
|
||||
}
|
||||
|
||||
let json_str = &line[6..]; // Remove "data: " prefix
|
||||
let Some(json_str) = line.get(6..) else {
|
||||
continue;
|
||||
}; // Remove "data: " prefix
|
||||
if json_str.trim().is_empty() || json_str.trim() == "[DONE]" {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use tokio::process::Command;
|
||||
|
||||
use super::base::{Provider, ProviderMetadata, ProviderUsage, Usage};
|
||||
use super::errors::ProviderError;
|
||||
use super::utils::RequestLog;
|
||||
use super::utils::{filter_extensions_from_system_prompt, RequestLog};
|
||||
use crate::conversation::message::{Message, MessageContent};
|
||||
|
||||
use crate::model::ModelConfig;
|
||||
@@ -103,28 +103,6 @@ impl GeminiCliProvider {
|
||||
None
|
||||
}
|
||||
|
||||
/// Filter out the Extensions section from the system prompt
|
||||
fn filter_extensions_from_system_prompt(&self, system: &str) -> String {
|
||||
// Find the Extensions section and remove it
|
||||
if let Some(extensions_start) = system.find("# Extensions") {
|
||||
// Look for the next major section that starts with #
|
||||
let after_extensions = &system[extensions_start..];
|
||||
if let Some(next_section_pos) = after_extensions[1..].find("\n# ") {
|
||||
// Found next section, keep everything before Extensions and after the next section
|
||||
let before_extensions = &system[..extensions_start];
|
||||
let next_section_start = extensions_start + next_section_pos + 1;
|
||||
let after_next_section = &system[next_section_start..];
|
||||
format!("{}{}", before_extensions.trim_end(), after_next_section)
|
||||
} else {
|
||||
// No next section found, just remove everything from Extensions onward
|
||||
system[..extensions_start].trim_end().to_string()
|
||||
}
|
||||
} else {
|
||||
// No Extensions section found, return original
|
||||
system.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute gemini CLI command with simple text prompt
|
||||
async fn execute_command(
|
||||
&self,
|
||||
@@ -135,8 +113,7 @@ impl GeminiCliProvider {
|
||||
// Create a simple prompt combining system + conversation
|
||||
let mut full_prompt = String::new();
|
||||
|
||||
// Add system prompt
|
||||
let filtered_system = self.filter_extensions_from_system_prompt(system);
|
||||
let filtered_system = filter_extensions_from_system_prompt(system);
|
||||
full_prompt.push_str(&filtered_system);
|
||||
full_prompt.push_str("\n\n");
|
||||
|
||||
|
||||
@@ -169,7 +169,9 @@ impl GithubCopilotProvider {
|
||||
if !tline.starts_with("data: ") {
|
||||
continue;
|
||||
}
|
||||
let payload = &tline[6..];
|
||||
let Some(payload) = tline.get(6..) else {
|
||||
continue;
|
||||
};
|
||||
if payload == "[DONE]" {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ impl SageMakerTgiProvider {
|
||||
// Remove any remaining HTML-like tags using a simple pattern
|
||||
// This is a basic implementation - for production use, consider using a proper HTML parser
|
||||
while let Some(start) = result.find('<') {
|
||||
if let Some(end) = result[start..].find('>') {
|
||||
if let Some(end) = result.get(start..).and_then(|s| s.find('>')) {
|
||||
result.replace_range(start..start + end + 1, "");
|
||||
} else {
|
||||
break;
|
||||
|
||||
@@ -47,6 +47,31 @@ pub fn convert_image(image: &ImageContent, image_format: &ImageFormat) -> Value
|
||||
}
|
||||
}
|
||||
|
||||
pub fn filter_extensions_from_system_prompt(system: &str) -> String {
|
||||
let Some(extensions_start) = system.find("# Extensions") else {
|
||||
return system.to_string();
|
||||
};
|
||||
|
||||
let Some(after_extensions) = system.get(extensions_start + 1..) else {
|
||||
return system.to_string();
|
||||
};
|
||||
|
||||
if let Some(next_section_pos) = after_extensions.find("\n# ") {
|
||||
let Some(before) = system.get(..extensions_start) else {
|
||||
return system.to_string();
|
||||
};
|
||||
let Some(after) = system.get(extensions_start + next_section_pos + 1..) else {
|
||||
return system.to_string();
|
||||
};
|
||||
format!("{}{}", before.trim_end(), after)
|
||||
} else {
|
||||
system
|
||||
.get(..extensions_start)
|
||||
.map(|s| s.trim_end().to_string())
|
||||
.unwrap_or_else(|| system.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn check_context_length_exceeded(text: &str) -> bool {
|
||||
let check_phrases = [
|
||||
"too long",
|
||||
|
||||
@@ -275,7 +275,9 @@ data: [DONE]
|
||||
if !line.starts_with("data: ") {
|
||||
continue;
|
||||
}
|
||||
let payload = &line[6..];
|
||||
let Some(payload) = line.get(6..) else {
|
||||
continue;
|
||||
};
|
||||
if payload == "[DONE]" {
|
||||
break;
|
||||
}
|
||||
@@ -325,7 +327,9 @@ data: [DONE]
|
||||
if !line.starts_with("data: ") {
|
||||
continue;
|
||||
}
|
||||
let payload = &line[6..];
|
||||
let Some(payload) = line.get(6..) else {
|
||||
continue;
|
||||
};
|
||||
if payload == "[DONE]" {
|
||||
break;
|
||||
}
|
||||
@@ -377,7 +381,9 @@ data: [DONE]
|
||||
if !line.starts_with("data: ") {
|
||||
continue;
|
||||
}
|
||||
let payload = &line[6..];
|
||||
let Some(payload) = line.get(6..) else {
|
||||
continue;
|
||||
};
|
||||
if payload == "[DONE]" {
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user