diff --git a/crates/tui/src/client.rs b/crates/tui/src/client.rs index 25c92188..9efc2b13 100644 --- a/crates/tui/src/client.rs +++ b/crates/tui/src/client.rs @@ -61,7 +61,10 @@ pub(super) fn from_api_tool_name(name: &str) -> String { break; } } - if let Ok(code) = u32::from_str_radix(&hex, 16) + // Only decode if we got exactly 6 hex digits (matching encoder output). + // Fewer digits means a truncated/malformed sequence — pass through as-is. + if hex.len() == 6 + && let Ok(code) = u32::from_str_radix(&hex, 16) && let Some(decoded) = std::char::from_u32(code) { if let Some('-') = iter.peek().copied() { @@ -747,7 +750,10 @@ impl DeepSeekClient { ); anyhow::bail!("Failed to list models: HTTP {status}: {error_text}"); } - let response_text = response.text().await.unwrap_or_default(); + let response_text = response + .text() + .await + .context("Failed to read models response body")?; parse_models_response(&response_text) } @@ -1295,8 +1301,8 @@ pub(super) fn parse_usage(usage: Option<&Value>) -> Usage { }); Usage { - input_tokens: input_tokens as u32, - output_tokens: output_tokens as u32, + input_tokens: input_tokens.min(u64::from(u32::MAX)) as u32, + output_tokens: output_tokens.min(u64::from(u32::MAX)) as u32, prompt_cache_hit_tokens, prompt_cache_miss_tokens, reasoning_tokens, diff --git a/crates/tui/src/client/chat.rs b/crates/tui/src/client/chat.rs index e8fb42c4..751698a7 100644 --- a/crates/tui/src/client/chat.rs +++ b/crates/tui/src/client/chat.rs @@ -167,7 +167,10 @@ impl DeepSeekClient { anyhow::bail!("Failed to call DeepSeek Chat API: HTTP {status}: {error_text}"); } - let response_text = response.text().await.unwrap_or_default(); + let response_text = response + .text() + .await + .context("Failed to read Chat API response body")?; let value: Value = serde_json::from_str(&response_text).context("Failed to parse Chat API JSON")?; parse_chat_message(&value) @@ -431,12 +434,13 @@ impl DeepSeekClient { } } - // Close any open blocks - if thinking_started { - yield Ok(StreamEvent::ContentBlockStop { index: content_index.saturating_sub(1) }); - } - if text_started { - yield Ok(StreamEvent::ContentBlockStop { index: content_index.saturating_sub(1) }); + // Close any open blocks — use the current content_index + // (which points to the next unused slot, so -1 gives the + // last-opened block) but guard against underflow when no + // content block was ever opened. + if thinking_started || text_started { + let idx = content_index.saturating_sub(1); + yield Ok(StreamEvent::ContentBlockStop { index: idx }); } release_stream_buffer(byte_buf); @@ -1974,6 +1978,8 @@ fn provider_accepts_reasoning_content(provider: ApiProvider) -> bool { | ApiProvider::Novita | ApiProvider::Fireworks | ApiProvider::Siliconflow + | ApiProvider::SiliconflowCn + | ApiProvider::Volcengine | ApiProvider::Arcee | ApiProvider::Sglang ) diff --git a/crates/tui/src/commands/mod.rs b/crates/tui/src/commands/mod.rs index dd10da10..54dc6280 100644 --- a/crates/tui/src/commands/mod.rs +++ b/crates/tui/src/commands/mod.rs @@ -668,8 +668,8 @@ pub fn execute(cmd: &str, app: &mut App) -> CommandResult { _ => { // Third source: skills (lowest precedence after native and user-config). // Try to run a skill whose name matches the command. - if skills::run_skill_by_name(app, command, arg).is_some() { - return skills::run_skill_by_name(app, command, arg).unwrap(); + if let Some(result) = skills::run_skill_by_name(app, command, arg) { + return result; } let suggestions = suggest_command_names(command, 3); if suggestions.is_empty() { diff --git a/crates/tui/src/tools/file.rs b/crates/tui/src/tools/file.rs index 671f1366..3600c933 100644 --- a/crates/tui/src/tools/file.rs +++ b/crates/tui/src/tools/file.rs @@ -114,7 +114,9 @@ impl ToolSpec for ReadFileTool { "start_line must be 1-based and greater than 0".to_string(), )); } - Some(v) => v as usize, + Some(v) => usize::try_from(v).map_err(|_| { + ToolError::invalid_input("start_line exceeds platform addressable range".to_string()) + })?, None => 1, }; @@ -124,7 +126,14 @@ impl ToolSpec for ReadFileTool { "max_lines must be greater than 0".to_string(), )); } - Some(v) => std::cmp::min(v as usize, HARD_MAX_READ_LINES), + Some(v) => { + let converted = usize::try_from(v).map_err(|_| { + ToolError::invalid_input( + "max_lines exceeds platform addressable range".to_string(), + ) + })?; + std::cmp::min(converted, HARD_MAX_READ_LINES) + } None => DEFAULT_READ_LINES, }; @@ -292,7 +301,9 @@ fn clean_pdf_text(raw: &str) -> String { if any_content { let start = out.find(|c: char| c != '\n').unwrap_or(0); // Walk back from end to find the last non-newline character. - let end = out.rfind(|c: char| c != '\n').map_or(out.len(), |i| i + 1); + let end = out + .rfind(|c: char| c != '\n') + .map_or(out.len(), |i| i + out[i..].chars().next().map_or(1, |c| c.len_utf8())); out[start..end].to_string() } else { String::new() diff --git a/crates/tui/src/tools/search.rs b/crates/tui/src/tools/search.rs index 221d760b..0174011a 100644 --- a/crates/tui/src/tools/search.rs +++ b/crates/tui/src/tools/search.rs @@ -100,8 +100,9 @@ impl ToolSpec for GrepFilesTool { async fn execute(&self, input: Value, context: &ToolContext) -> Result { let pattern_str = required_str(&input, "pattern")?; let path_str = optional_str(&input, "path").unwrap_or("."); - let context_lines = - usize::try_from(optional_u64(&input, "context_lines", 2)).unwrap_or(usize::MAX); + let context_lines = usize::try_from(optional_u64(&input, "context_lines", 2)) + .unwrap_or(usize::MAX) + .min(1000); let case_insensitive = optional_bool(&input, "case_insensitive", false); let max_results = usize::try_from(optional_u64(&input, "max_results", MAX_RESULTS as u64)) .unwrap_or(MAX_RESULTS);