Skip to main content

Crate cetai_bevy

Crate cetai_bevy 

Source
Expand description

cetai-bevy — a Bevy plugin for cetai, running typed prompt! AI calls from Bevy systems on native and wasm32-unknown-unknown.

The crate provides three pieces:

  • BevyTransport, a cetai Transport built on ehttp (ureq + rustls on native, fetch on wasm) and wasm-timer;
  • CetaiPlugin, which installs that transport into the global cetai configuration and registers a per-frame system draining finished AI results back onto the main thread;
  • Cetai, a system param whose ask method runs an AI future on bevy_tasks::IoTaskPool and applies a callback to the World when it finishes.

§Example

use bevy_app::{App, Startup};
use cetai::prompt;
use cetai_bevy::{Cetai, CetaiPlugin};
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Deserialize, JsonSchema)]
struct NpcLine {
    text: String,
}

fn ask_blacksmith(ai: Cetai) {
    let mood = "grumpy";
    // The output type is inferred from the callback's parameter annotation.
    ai.ask(
        prompt!("Describe a {mood} blacksmith in one sentence"),
        |_world, result: cetai::Result<NpcLine>| {
            match result {
                Ok(line) => println!("blacksmith: {}", line.text),
                Err(err) => eprintln!("AI call failed: {err}"),
            }
        },
    );
}

fn main() {
    App::new()
        .add_plugins(CetaiPlugin::new())
        .add_systems(Startup, ask_blacksmith)
        .run();
}

§WASM notes

Everything compiles for wasm32-unknown-unknown: requests go through the browser’s fetch (subject to CORS, and only CORS-exposed response headers are visible), timers through setTimeout, and tasks through bevy’s web-task based task pools. Note that a DEEPSEEK_API_KEY baked into a shipped web build is visible to every player — prefer routing requests through your own backend via AiConfig::base_url.

Structs§

BevyTransport
HTTP transport backed by ehttp, usable on native and wasm32-unknown-unknown.
Cetai
System param starting cetai AI calls from any Bevy system.
CetaiPlugin
Bevy plugin wiring cetai into a Bevy app.