Skip to main content

vtcode_core/llm/
utils.rs

1//! Shared utilities for LLM request/response processing
2//!
3//! This module provides common functions used across multiple providers
4//! to eliminate duplicate code and reduce allocations.
5
6use anyhow::{Context, Result};
7use serde_json::Value;
8
9use crate::llm::provider::{LLMRequest, LLMResponse, LLMStreamEvent, ToolCall};
10
11/// Parse chat request from OpenAI-compatible format
12pub fn parse_chat_request_openai_format(value: &Value, default_model: &str) -> Option<LLMRequest> {
13    crate::llm::providers::common::parse_chat_request_openai_format(value, default_model)
14}
15
16/// Parse response from OpenAI-compatible format
17pub fn parse_response_openai_format(
18    response: Value,
19    _provider_name: &str,
20    model: String,
21    include_cache: bool,
22    reasoning_content: Option<String>,
23) -> Result<LLMResponse> {
24    let choices = response
25        .get("choices")
26        .context("Missing choices in response")?
27        .as_array()
28        .context("Choices must be an array")?;
29
30    if choices.is_empty() {
31        anyhow::bail!("No choices in response");
32    }
33
34    let first_choice = &choices[0];
35    let message = first_choice
36        .get("message")
37        .context("Missing message in choice")?;
38
39    let content = message
40        .get("content")
41        .and_then(|c| c.as_str())
42        .unwrap_or("")
43        .to_string();
44
45    // Extract usage information
46    let usage = response.get("usage");
47    let input_tokens = usage
48        .and_then(|u| u.get("prompt_tokens"))
49        .and_then(|t| t.as_u64())
50        .unwrap_or(0);
51
52    let output_tokens = usage
53        .and_then(|u| u.get("completion_tokens"))
54        .and_then(|t| t.as_u64())
55        .unwrap_or(0);
56
57    // Extract function call if present
58    let tool_call = message.get("function_call").and_then(|fc| {
59        let name = fc.get("name")?.as_str()?;
60        let arguments = fc.get("arguments")?.as_str()?;
61
62        Some(ToolCall::function(
63            "call_001".to_string(), // Generate a simple ID
64            name.to_string(),
65            arguments.to_string(),
66        ))
67    });
68
69    // Use the provider's LLMResponse which has different structure
70    let mut llm_response = LLMResponse {
71        content: None,
72        tool_calls: None,
73        model,
74        usage: Some(crate::llm::provider::Usage {
75            prompt_tokens: u32::try_from(input_tokens).unwrap_or(u32::MAX),
76            completion_tokens: u32::try_from(output_tokens).unwrap_or(u32::MAX),
77            total_tokens: u32::try_from(input_tokens.saturating_add(output_tokens))
78                .unwrap_or(u32::MAX),
79            cached_prompt_tokens: if include_cache {
80                response
81                    .get("cache_hit")
82                    .and_then(|c| c.as_bool())
83                    .map(|_| 0)
84            } else {
85                None
86            },
87            cache_creation_tokens: None,
88            cache_read_tokens: None,
89        }),
90        finish_reason: crate::llm::provider::FinishReason::Stop,
91        reasoning: reasoning_content,
92        reasoning_details: None,
93        tool_references: Vec::new(),
94        request_id: None,
95        organization_id: None,
96        compaction: None,
97    };
98
99    // Set content based on function call or regular content
100    if let Some(tool_call) = tool_call {
101        llm_response.content = None;
102        llm_response.tool_calls = Some(vec![tool_call]);
103    } else {
104        llm_response.content = Some(content);
105        llm_response.tool_calls = None;
106    }
107
108    Ok(llm_response)
109}
110
111/// Parse stream event from OpenAI-compatible format
112pub fn parse_stream_event_openai_format(
113    json: Value,
114    _provider_name: &str,
115) -> Option<LLMStreamEvent> {
116    let choices = json.get("choices")?.as_array()?;
117    if choices.is_empty() {
118        return None;
119    }
120
121    let delta = choices[0].get("delta")?;
122    let content = delta.get("content").and_then(|c| c.as_str())?;
123
124    Some(LLMStreamEvent::Token {
125        delta: content.to_string(),
126    })
127}
128
129/// Extract reasoning content from text (for providers that support reasoning)
130///
131/// Supports the following reasoning tag patterns:
132/// - <think></think>
133/// - <thought></thought>
134/// - <reasoning></reasoning>
135/// - <analysis></analysis>
136/// - <thinking></thinking>
137///
138/// Returns (reasoning_parts, cleaned_content) where reasoning_parts contains
139/// the extracted reasoning text (without tags) and cleaned_content is the
140/// remaining content with reasoning sections removed.
141pub fn extract_reasoning_content(content: &str) -> (Vec<String>, Option<String>) {
142    if let Some((deprecated_reasoning, deprecated_content)) =
143        extract_deprecated_reasoning_sections(content)
144    {
145        let reasoning_parts = deprecated_reasoning
146            .map(|value| vec![value])
147            .unwrap_or_default();
148        return (reasoning_parts, deprecated_content);
149    }
150
151    // Use the robust split_reasoning_from_text function that handles all tag types
152    let (segments, cleaned_content) = crate::llm::providers::split_reasoning_from_text(content);
153
154    let reasoning_parts: Vec<String> = segments.into_iter().map(|s| s.text).collect();
155
156    let final_content = if let Some(cleaned) = cleaned_content {
157        let trimmed = cleaned.trim();
158        if trimmed.is_empty() {
159            None
160        } else {
161            Some(trimmed.to_string())
162        }
163    } else {
164        None
165    };
166
167    (reasoning_parts, final_content)
168}
169
170fn extract_deprecated_reasoning_sections(
171    content: &str,
172) -> Option<(Option<String>, Option<String>)> {
173    let mut reasoning_lines: Vec<String> = Vec::new();
174    let mut content_lines: Vec<String> = Vec::new();
175    let mut active_section: Option<&str> = None;
176    let mut saw_reasoning = false;
177    let mut saw_content = false;
178    let mut saw_first_key = false;
179
180    for line in content.lines() {
181        let trimmed = line.trim_end();
182        let trimmed_start = trimmed.trim_start();
183
184        if trimmed_start.is_empty() {
185            if let Some(section) = active_section {
186                match section {
187                    "reasoning" => reasoning_lines.push(String::new()),
188                    "content" => content_lines.push(String::new()),
189                    _ => {}
190                }
191            }
192            continue;
193        }
194
195        if let Some(rest) = trimmed_start.strip_prefix("reasoning:") {
196            saw_first_key = true;
197            saw_reasoning = true;
198            active_section = Some("reasoning");
199            let value = rest.trim_start();
200            if !matches!(value, "|" | "|-" | "|+" | ">" | ">-" | ">+") && !value.is_empty() {
201                reasoning_lines.push(value.to_string());
202            }
203            continue;
204        }
205
206        if let Some(rest) = trimmed_start.strip_prefix("content:") {
207            saw_first_key = true;
208            saw_content = true;
209            active_section = Some("content");
210            let value = rest.trim_start();
211            if !matches!(value, "|" | "|-" | "|+" | ">" | ">-" | ">+") && !value.is_empty() {
212                content_lines.push(value.to_string());
213            }
214            continue;
215        }
216
217        if !saw_first_key {
218            return None;
219        }
220
221        if let Some(section) = active_section {
222            match section {
223                "reasoning" => reasoning_lines.push(trimmed_start.to_string()),
224                "content" => content_lines.push(trimmed_start.to_string()),
225                _ => {}
226            }
227        }
228    }
229
230    if !(saw_reasoning && saw_content) {
231        return None;
232    }
233
234    let reasoning = join_deprecated_section(&reasoning_lines);
235    let content = join_deprecated_section(&content_lines);
236
237    if reasoning.is_none() && content.is_none() {
238        None
239    } else {
240        Some((reasoning, content))
241    }
242}
243
244fn join_deprecated_section(lines: &[String]) -> Option<String> {
245    if lines.is_empty() {
246        return None;
247    }
248
249    let joined = lines.join("\n");
250    let trimmed = joined.trim();
251    if trimmed.is_empty() {
252        None
253    } else {
254        Some(trimmed.to_string())
255    }
256}
257
258pub use vtcode_commons::tokens::{
259    estimate_tokens as estimate_token_count, truncate_to_tokens as truncate_to_token_limit,
260};
261
262/// Create a consistent error message for LLM errors
263pub fn format_llm_error(provider_name: &str, error_message: &str) -> String {
264    format!("[{}] {}", provider_name, error_message.trim())
265}
266
267/// Validate that a model string is not empty and reasonable
268pub fn validate_model_string(model: &str) -> Result<()> {
269    if model.is_empty() {
270        anyhow::bail!("Model cannot be empty")
271    }
272
273    if model.len() > 100 {
274        anyhow::bail!("Model name too long (max 100 characters)")
275    }
276
277    // Basic sanity check for model name format
278    if !model
279        .chars()
280        .all(|c| c.is_alphanumeric() || c == '-' || c == '_' || c == '.' || c == ':')
281    {
282        anyhow::bail!("Model contains invalid characters. Only alphanumeric, -, _, ., : allowed")
283    }
284
285    Ok(())
286}
287
288#[cfg(test)]
289mod tests {
290    use super::*;
291    use crate::llm::provider::{AssistantPhase, MessageRole};
292
293    #[test]
294    fn test_parse_chat_request_openai_format() {
295        let json = serde_json::json!({
296            "model": "gpt-5",
297            "messages": [
298                {"role": "user", "content": "Hello"},
299                {"role": "assistant", "content": "Hi there", "phase": "commentary"}
300            ],
301            "temperature": 0.7,
302            "max_tokens": 100
303        });
304
305        let request = parse_chat_request_openai_format(&json, "default-model").unwrap();
306        assert_eq!(request.model, "gpt-5");
307        assert_eq!(request.messages.len(), 2);
308        assert_eq!(request.messages[0].role, MessageRole::User);
309        assert_eq!(request.messages[0].content.as_text(), "Hello");
310        assert_eq!(request.messages[0].phase, None);
311        assert_eq!(request.messages[1].phase, Some(AssistantPhase::Commentary));
312        assert_eq!(request.temperature, Some(0.7));
313        assert_eq!(request.max_tokens, Some(100));
314    }
315
316    #[test]
317    fn test_parse_chat_request_openai_format_ignores_phase_for_non_assistant_roles() {
318        let json = serde_json::json!({
319            "messages": [
320                {"role": "user", "content": "Hello", "phase": "commentary"},
321                {"role": "tool", "content": "{}", "tool_call_id": "call_1", "phase": "final_answer"}
322            ]
323        });
324
325        let request = parse_chat_request_openai_format(&json, "default-model").unwrap();
326        assert_eq!(request.messages[0].phase, None);
327        assert_eq!(request.messages[1].phase, None);
328    }
329
330    #[test]
331    fn test_parse_response_openai_format() {
332        let response = serde_json::json!({
333            "choices": [{
334                "message": {
335                    "content": "Hello world",
336                    "role": "assistant"
337                }
338            }],
339            "usage": {
340                "prompt_tokens": 10,
341                "completion_tokens": 5
342            },
343            "model": "gpt-5"
344        });
345
346        let result =
347            parse_response_openai_format(response, "test", "gpt-5".to_string(), false, None)
348                .unwrap();
349        assert_eq!(result.content_text(), "Hello world");
350        let usage = result.usage.expect("usage should be present");
351        assert_eq!(usage.prompt_tokens, 10);
352        assert_eq!(usage.completion_tokens, 5);
353    }
354
355    #[test]
356    fn test_extract_reasoning_content() {
357        let content = "Some text <reasoning>This is reasoning</reasoning> More text";
358        let (reasoning, main) = extract_reasoning_content(content);
359
360        assert_eq!(reasoning.len(), 1);
361        assert_eq!(reasoning[0], "This is reasoning");
362        assert_eq!(main.unwrap(), "Some text  More text");
363    }
364
365    #[test]
366    fn test_extract_reasoning_content_deprecated_format() {
367        let content = "reasoning: Need to run cargo clippy.\ncontent: Need to run cargo clippy.";
368        let (reasoning, main) = extract_reasoning_content(content);
369
370        assert_eq!(reasoning.len(), 1);
371        assert_eq!(reasoning[0], "Need to run cargo clippy.");
372        assert_eq!(main.as_deref(), Some("Need to run cargo clippy."));
373    }
374
375    #[test]
376    fn test_extract_reasoning_content_think_tags() {
377        let content = "Let me think <think>I need to analyze this problem</think>The answer is 42";
378        let (reasoning, main) = extract_reasoning_content(content);
379
380        assert_eq!(reasoning.len(), 1);
381        assert_eq!(reasoning[0], "I need to analyze this problem");
382        assert_eq!(main.unwrap(), "Let me think The answer is 42");
383    }
384
385    #[test]
386    fn test_extract_reasoning_content_analysis_tags() {
387        let content = "<analysis>Breaking down the requirements</analysis>Here is the solution";
388        let (reasoning, main) = extract_reasoning_content(content);
389
390        assert_eq!(reasoning.len(), 1);
391        assert_eq!(reasoning[0], "Breaking down the requirements");
392        assert_eq!(main.unwrap(), "Here is the solution");
393    }
394
395    #[test]
396    fn test_extract_reasoning_content_thinking_tags() {
397        let content = "<thinking>First, I'll check the dependencies</thinking>Now implementing";
398        let (reasoning, main) = extract_reasoning_content(content);
399
400        assert_eq!(reasoning.len(), 1);
401        assert_eq!(reasoning[0], "First, I'll check the dependencies");
402        assert_eq!(main.unwrap(), "Now implementing");
403    }
404
405    #[test]
406    fn test_extract_reasoning_content_multiple_tags() {
407        // Multiple reasoning sections with different tag types
408        let content = "<think>Step 1: Plan</think> text <analysis>Step 2: Analyze</think> end";
409        let (reasoning, main) = extract_reasoning_content(content);
410
411        // Note: Current implementation may merge adjacent reasoning segments
412        // Testing that at least one reasoning section is extracted
413        assert!(!reasoning.is_empty());
414        assert!(
415            reasoning
416                .iter()
417                .any(|r| r.contains("Step 1") || r.contains("Step 2"))
418        );
419        let main_text = main.unwrap();
420        assert!(main_text.contains("text") || main_text.contains("end"));
421    }
422
423    #[test]
424    fn test_estimate_token_count() {
425        assert_eq!(estimate_token_count("Hello world"), 3); // ~11 chars / 4
426        assert_eq!(estimate_token_count(""), 0); // empty input
427        assert_eq!(estimate_token_count("a"), 1); // minimum 1
428    }
429
430    #[test]
431    fn test_truncate_to_token_limit() {
432        let text = "Hello world this is a longer text that should be truncated";
433        let truncated = truncate_to_token_limit(text, 3);
434        assert!(truncated.len() < text.len());
435        assert!(!truncated.contains("truncated"));
436    }
437
438    #[test]
439    fn test_format_llm_error() {
440        let error = format_llm_error("OpenAI", "Rate limit exceeded");
441        assert_eq!(error, "[OpenAI] Rate limit exceeded");
442    }
443
444    #[test]
445    fn test_validate_model_string() {
446        validate_model_string("gpt-5").unwrap();
447        validate_model_string("claude-haiku-4-5").unwrap();
448        assert!(validate_model_string("").is_err());
449        assert!(validate_model_string(&"a".repeat(101)).is_err());
450        assert!(validate_model_string("invalid@model").is_err());
451    }
452}