Skip to main content

cetai_bevy/
lib.rs

1//! cetai-bevy — a [Bevy](https://bevyengine.org) plugin for
2//! [cetai](https://docs.rs/cetai), running typed `prompt!` AI calls from Bevy
3//! systems on native and `wasm32-unknown-unknown`.
4//!
5//! The crate provides three pieces:
6//!
7//! - [`BevyTransport`], a cetai [`Transport`](cetai::Transport) built on
8//!   `ehttp` (ureq + rustls on native, `fetch` on wasm) and `wasm-timer`;
9//! - [`CetaiPlugin`], which installs that transport into the global cetai
10//!   configuration and registers a per-frame system draining finished AI
11//!   results back onto the main thread;
12//! - [`Cetai`], a system param whose [`ask`](Cetai::ask) method runs an AI
13//!   future on [`bevy_tasks::IoTaskPool`] and applies a callback to the
14//!   [`World`](bevy_ecs::world::World) when it finishes.
15//!
16//! # Example
17//!
18//! ```no_run
19//! use bevy_app::{App, Startup};
20//! use cetai::prompt;
21//! use cetai_bevy::{Cetai, CetaiPlugin};
22//! use schemars::JsonSchema;
23//! use serde::Deserialize;
24//!
25//! #[derive(Deserialize, JsonSchema)]
26//! struct NpcLine {
27//!     text: String,
28//! }
29//!
30//! fn ask_blacksmith(ai: Cetai) {
31//!     let mood = "grumpy";
32//!     // The output type is inferred from the callback's parameter annotation.
33//!     ai.ask(
34//!         prompt!("Describe a {mood} blacksmith in one sentence"),
35//!         |_world, result: cetai::Result<NpcLine>| {
36//!             match result {
37//!                 Ok(line) => println!("blacksmith: {}", line.text),
38//!                 Err(err) => eprintln!("AI call failed: {err}"),
39//!             }
40//!         },
41//!     );
42//! }
43//!
44//! fn main() {
45//!     App::new()
46//!         .add_plugins(CetaiPlugin::new())
47//!         .add_systems(Startup, ask_blacksmith)
48//!         .run();
49//! }
50//! ```
51//!
52//! # WASM notes
53//!
54//! Everything compiles for `wasm32-unknown-unknown`: requests go through the
55//! browser's `fetch` (subject to CORS, and only CORS-exposed response headers
56//! are visible), timers through `setTimeout`, and tasks through bevy's
57//! web-task based task pools. Note that a `DEEPSEEK_API_KEY` baked into a
58//! shipped web build is visible to every player — prefer routing requests
59//! through your own backend via `AiConfig::base_url`.
60
61mod param;
62mod plugin;
63mod transport;
64
65pub use param::Cetai;
66pub use plugin::CetaiPlugin;
67pub use transport::BevyTransport;