Skip to main content

Crate highflame_shield

Crate highflame_shield 

Source
Expand description

§highflame-shield

Async Rust SDK for the highflame-shield guardrails service.

§Quick start

use highflame_shield::{ShieldClient, ShieldClientOptions, ShieldRequest};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ShieldClient::new(
        ShieldClientOptions::new(std::env::var("HIGHFLAME_API_KEY")?),
    );

    let resp = client
        .guard_prompt("Hello world", None, None)
        .await?;

    println!("decision: {}", resp.decision);
    assert!(resp.allowed());
    Ok(())
}

§Service-key flow

Keys that start with hf_sk are automatically exchanged for a short-lived RS256 JWT. The JWT is cached and transparently refreshed 60 s before expiry. Concurrent callers serialise behind a single tokio::sync::Mutex so only one exchange is ever in-flight.

use highflame_shield::{ShieldClient, ShieldClientOptions};

let client = ShieldClient::new(
    ShieldClientOptions::new("hf_sk_my_service_key")
        .token_url("https://studio.api-dev.highflame.dev/api/cli-auth/token")
        .base_url("https://shield.api-dev.highflame.dev"),
);

// First call exchanges the key for a JWT automatically.
let resp = client.guard_prompt("test", None, None).await?;
println!("{}", resp.decision);

§SSE streaming

use futures_util::{pin_mut, StreamExt as _};
use highflame_shield::{ShieldClient, ShieldClientOptions, ShieldRequest};

let client = ShieldClient::new(ShieldClientOptions::new("my-jwt"));
let req = ShieldRequest {
    content: "Hello".to_string(),
    content_type: "prompt".to_string(),
    action: "process_prompt".to_string(),
    ..Default::default()
};

// `stream()` returns an `impl Stream` that is not `Unpin` — pin it before
// iterating so `StreamExt::next()` can take a mutable reference.
let stream = client.stream(&req).await?;
pin_mut!(stream);
while let Some(event) = stream.next().await {
    let ev = event?;
    println!("event: {} data: {:?}", ev.r#type, ev.data);
}

Re-exports§

pub use error::ShieldError;
pub use stream::ShieldStreamEvent;
pub use types::DetectorInfo;
pub use types::DetectorResult;
pub use types::DeterminingPolicy;
pub use types::FileContext;
pub use types::HealthResponse;
pub use types::ListDetectorsResponse;
pub use types::McpContext;
pub use types::ModelContext;
pub use types::RootCause;
pub use types::SessionDelta;
pub use types::ShieldRequest;
pub use types::ShieldResponse;
pub use types::ToolContext;
pub use types::TokenResponse;

Modules§

error
Error types for the Shield SDK.
stream
SSE streaming support for POST /v1/guard/stream.
types
Request and response types for the highflame-shield API.

Structs§

ShieldClient
Async HTTP client for highflame-shield.
ShieldClientOptions
Configuration for ShieldClient.