artifactize-provider 0.27.2

Managed credentials, model discovery and direct HTTP turns for Artifactize.
Documentation
use rig_core::message::{
    AssistantContent, DocumentSourceKind, Image, LocalCallId, Reasoning, ReasoningContent,
    ToolCall, ToolFunction, ToolName,
};

use super::*;

const KEY: &str = "abcdefgh";

fn response(choice: Vec<AssistantContent>) -> CompletionResponse {
    CompletionResponse::new(choice, Default::default(), "fixture", Value::Null)
}

fn reasoning(text: &str, private: bool) -> AssistantContent {
    let mut content = vec![ReasoningContent::Summary(text.into())];
    if private {
        content.push(ReasoningContent::Encrypted("opaque reasoning".into()));
    }
    AssistantContent::Reasoning(Reasoning { id: None, content }.sealed("fixture"))
}

fn visible_text(choice: &[AssistantContent]) -> String {
    let mut joined = String::new();
    for item in choice {
        match item {
            AssistantContent::Text(text) => joined.push_str(&text.text),
            AssistantContent::Reasoning(sealed) => {
                for part in &sealed.open(sealed.issuer()).unwrap().content {
                    if let ReasoningContent::Summary(text) = part {
                        joined.push_str(text);
                    }
                }
            }
            AssistantContent::Image(image) => joined.push_str(&image.data.to_string()),
            AssistantContent::ToolCall(_) => unreachable!(),
        }
    }
    joined
}

#[test]
fn output_does_not_reconstruct_credentials_in_content_order_or_the_public_view() {
    let inventory = Inventory::new([KEY.to_owned()]);
    for (head, middle, tail, spelling) in [
        ("abc", "def", "gh", KEY),
        ("a%", "62cdef", "gh", "a%62cdefgh"),
        (r"ab\u00", "63def", "gh", concat!(r"ab\u00", "63defgh")),
    ] {
        for item in [
            reasoning(middle, false),
            reasoning(middle, true),
            AssistantContent::Image(Image {
                data: DocumentSourceKind::url(middle),
                ..Image::default()
            }),
        ] {
            let choice = vec![
                AssistantContent::text(head),
                item,
                AssistantContent::text(tail),
            ];
            assert_eq!(visible_text(&choice), spelling);
            let protected = protect_response(response(choice), &inventory).unwrap();
            let text = visible_text(&protected.choice);
            assert!(!text.contains(spelling));
            assert!(!text.contains(KEY));
        }
    }
}

#[test]
fn a_tool_call_cannot_complete_a_credential_between_two_text_items() {
    let inventory = Inventory::new([KEY.to_owned()]);
    for (head, summary, middle, tail) in [
        ("abc", None, "def", "gh"),
        ("a%", None, "62cdef", "gh"),
        ("ab", Some("cd"), "ef", "gh"),
    ] {
        let call = ToolCall::new(
            LocalCallId::new().into(),
            ToolFunction::new(
                ToolName::new("fixture").unwrap(),
                json!({"nested": [middle]}),
            ),
        );
        let mut choice = vec![AssistantContent::text(head)];
        if let Some(summary) = summary {
            choice.push(reasoning(summary, true));
        }
        choice.extend([
            AssistantContent::ToolCall(call),
            AssistantContent::text(tail),
        ]);
        let failure = protect_response(response(choice), &inventory).unwrap_err();
        assert_eq!(failure.code, Code::ProviderError);
    }
}