vtcode-llm 0.187.0

LLM provider abstraction, client implementations, and streaming for VT Code
Documentation
use super::*;

const CLEARED_ARGS: &str = "{\"cleared\":\"tool_input\"}";

fn function_part(id: Option<&str>, name: &str, args: Value) -> Part {
    Part::FunctionCall {
        function_call: GeminiFunctionCall {
            id: id.map(str::to_string),
            name: name.to_string(),
            args,
        },
        thought_signature: Some("sig".to_string()),
    }
}

fn tool_call(id: &str, name: &str, arguments: &str) -> ToolCall {
    ToolCall {
        id: id.to_string(),
        call_type: "function".to_string(),
        function: Some(FunctionCall {
            namespace: None,
            name: name.to_string(),
            arguments: arguments.to_string(),
        }),
        text: None,
        thought_signature: None,
    }
}

fn assistant_message(tool_calls: Option<Vec<ToolCall>>) -> Message {
    Message {
        tool_calls,
        ..Message::assistant("working".to_string())
    }
}

fn part_args(part: &Part) -> Option<&Value> {
    match part {
        Part::FunctionCall { function_call, .. } => Some(&function_call.args),
        _ => None,
    }
}

fn part_signature(part: &Part) -> Option<&str> {
    match part {
        Part::FunctionCall { thought_signature, .. } => thought_signature.as_deref(),
        _ => None,
    }
}

#[test]
fn sync_replaces_arguments_by_id_then_name() {
    let mut parts = vec![
        function_part(Some("call_1"), "read_file", json!({"path": "big.rs"})),
        function_part(None, "grep_file", json!({"pattern": "x"})),
    ];
    let message = assistant_message(Some(vec![
        tool_call("call_1", "read_file", CLEARED_ARGS),
        tool_call("call_2", "grep_file", CLEARED_ARGS),
    ]));

    sync_preserved_parts_with_tool_calls(&mut parts, &message);

    assert_eq!(part_args(&parts[0]), Some(&json!({"cleared": "tool_input"})));
    assert_eq!(part_args(&parts[1]), Some(&json!({"cleared": "tool_input"})));
    // Thought signatures stay on the part: Gemini requires them.
    assert_eq!(part_signature(&parts[0]), Some("sig"));
}

#[test]
fn sync_leaves_unmatched_parts_untouched() {
    let original_args = json!({"path": "keep.rs"});
    let mut parts = vec![function_part(Some("call_other"), "write_file", original_args.clone())];
    let message = assistant_message(Some(vec![tool_call("call_1", "read_file", CLEARED_ARGS)]));

    sync_preserved_parts_with_tool_calls(&mut parts, &message);

    assert_eq!(part_args(&parts[0]), Some(&original_args));
}

#[test]
fn sync_keeps_original_when_tool_call_arguments_unparseable() {
    let original_args = json!({"path": "big.rs"});
    let mut parts = vec![function_part(Some("call_1"), "read_file", original_args.clone())];
    let message = assistant_message(Some(vec![tool_call("call_1", "read_file", "not-json")]));

    sync_preserved_parts_with_tool_calls(&mut parts, &message);

    assert_eq!(part_args(&parts[0]), Some(&original_args));
}

#[test]
fn sync_ignores_messages_without_tool_calls() {
    let original_args = json!({"path": "big.rs"});
    let mut parts = vec![function_part(Some("call_1"), "read_file", original_args.clone())];
    let message = assistant_message(None);

    sync_preserved_parts_with_tool_calls(&mut parts, &message);

    assert_eq!(part_args(&parts[0]), Some(&original_args));
}