$npx -y skills add OthmanAdi/openui-forge --skill openui-forge-rustOpenUI generative UI with Rust Axum backend. Async SSE streaming with reqwest and async-stream.
| 1 | # OpenUI Forge — Rust |
| 2 | |
| 3 | Build generative UI apps with a React frontend + Rust Axum backend. Async SSE streaming to OpenAI-compatible NDJSON. |
| 4 | |
| 5 | ## Activation Triggers |
| 6 | |
| 7 | - "openui rust", "openui axum", "openui rust backend" |
| 8 | - "generative ui rust", "rust streaming ui backend" |
| 9 | |
| 10 | ## Prerequisites |
| 11 | |
| 12 | - Node.js >= 22 (24 LTS recommended) + React >= 18.3.1 (19+ recommended) (frontend) |
| 13 | - Rust >= 1.75 with Cargo (backend) |
| 14 | - `OPENAI_API_KEY` environment variable set |
| 15 | |
| 16 | ## Quick Start |
| 17 | |
| 18 | 1. Create the React frontend and install OpenUI deps: |
| 19 | ```bash |
| 20 | npm install @openuidev/react-ui @openuidev/react-headless @openuidev/react-lang lucide-react zod |
| 21 | ``` |
| 22 | 2. Generate the system prompt: |
| 23 | ```bash |
| 24 | npx @openuidev/cli generate ./src/lib/library.ts --out backend/system-prompt.txt |
| 25 | ``` |
| 26 | 3. Create the Rust backend (see Full Code below) |
| 27 | 4. Run: `cargo run` on `:3001`, frontend on `:3000` |
| 28 | |
| 29 | ## Full Code |
| 30 | |
| 31 | ### Backend: `backend/Cargo.toml` |
| 32 | |
| 33 | ```toml |
| 34 | [package] |
| 35 | name = "openui-backend" |
| 36 | version = "0.1.0" |
| 37 | edition = "2021" |
| 38 | |
| 39 | [dependencies] |
| 40 | axum = "0.8" |
| 41 | http = "1" |
| 42 | tokio = { version = "1", features = ["full"] } |
| 43 | reqwest = { version = "0.13", features = ["json", "stream"] } |
| 44 | serde = { version = "1", features = ["derive"] } |
| 45 | serde_json = "1" |
| 46 | async-stream = "0.3" |
| 47 | futures = "0.3" |
| 48 | tower-http = { version = "0.6", features = ["cors"] } |
| 49 | dotenvy = "0.15" |
| 50 | ``` |
| 51 | |
| 52 | ### Backend: `backend/src/main.rs` |
| 53 | |
| 54 | ```rust |
| 55 | use axum::{ |
| 56 | extract::{Json, State}, |
| 57 | response::sse::{Event, Sse}, |
| 58 | routing::post, |
| 59 | Router, |
| 60 | }; |
| 61 | use futures::stream::Stream; |
| 62 | use http::HeaderValue; |
| 63 | use reqwest::Client; |
| 64 | use serde::{Deserialize, Serialize}; |
| 65 | use std::{convert::Infallible, fs, net::SocketAddr, sync::Arc}; |
| 66 | use tower_http::cors::{Any, CorsLayer}; |
| 67 | |
| 68 | #[derive(Deserialize)] |
| 69 | struct ChatRequest { |
| 70 | messages: Vec<Message>, |
| 71 | } |
| 72 | |
| 73 | #[derive(Serialize, Deserialize, Clone)] |
| 74 | struct Message { |
| 75 | role: String, |
| 76 | content: String, |
| 77 | } |
| 78 | |
| 79 | #[derive(Clone)] |
| 80 | struct AppState { |
| 81 | system_prompt: String, |
| 82 | } |
| 83 | |
| 84 | #[tokio::main] |
| 85 | async fn main() { |
| 86 | dotenvy::dotenv().ok(); |
| 87 | let system_prompt = fs::read_to_string("system-prompt.txt") |
| 88 | .expect("system-prompt.txt not found"); |
| 89 | let state = Arc::new(AppState { system_prompt }); |
| 90 | |
| 91 | let cors = CorsLayer::new() |
| 92 | .allow_origin("http://localhost:3000".parse::<HeaderValue>().unwrap()) |
| 93 | .allow_methods([http::Method::POST]) |
| 94 | .allow_headers(Any); |
| 95 | |
| 96 | let app = Router::new() |
| 97 | .route("/api/chat", post(chat_handler)) |
| 98 | .layer(cors) |
| 99 | .with_state(state); |
| 100 | |
| 101 | let addr = SocketAddr::from(([0, 0, 0, 0], 3001)); |
| 102 | println!("Rust backend listening on {addr}"); |
| 103 | let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); |
| 104 | axum::serve(listener, app).await.unwrap(); |
| 105 | } |
| 106 | |
| 107 | async fn chat_handler( |
| 108 | State(state): State<Arc<AppState>>, |
| 109 | Json(req): Json<ChatRequest>, |
| 110 | ) -> Sse<impl Stream<Item = Result<Event, Infallible>>> { |
| 111 | let mut messages = vec![Message { role: "system".into(), content: state.system_prompt.clone() }]; |
| 112 | messages.extend(req.messages); |
| 113 | |
| 114 | let api_key = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set"); |
| 115 | let client = Client::new(); |
| 116 | |
| 117 | let stream = async_stream::stream! { |
| 118 | let resp = client |
| 119 | .post("https://api.openai.com/v1/chat/completions") |
| 120 | .bearer_auth(&api_key) |
| 121 | .json(&serde_json::json!({ |
| 122 | "model": std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.5".into()), |
| 123 | "stream": true, |
| 124 | "messages": messages, |
| 125 | })) |
| 126 | .send() |
| 127 | .await; |
| 128 | |
| 129 | if let Ok(resp) = resp { |
| 130 | let mut bytes_stream = resp.bytes_stream(); |
| 131 | use futures::StreamExt; |
| 132 | let mut buffer = String::new(); |
| 133 | while let Some(Ok(chunk)) = bytes_stream.next().await { |
| 134 | buffer.push_str(&String::from_utf8_lossy(&chunk)); |
| 135 | while let Some(pos) = buffer.find("\n\n") { |
| 136 | let line = buffer[..pos].to_string(); |
| 137 | buffer = buffer[pos + 2..].to_string(); |
| 138 | if line.starts_with("data: ") { |
| 139 | yield Ok(Event::default().data(&line[6..])); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | Sse::new(stream) |
| 147 | } |
| 148 | ``` |
| 149 | |
| 150 | ### Frontend: `app/chat/page.tsx` |
| 151 | |
| 152 | ```tsx |
| 153 | "use client"; |
| 154 | import { FullScreen } from "@openuidev/react-ui"; |
| 155 | import { openuiChatLibrary } from "@openuidev/react-ui/genui-lib"; |
| 156 | import { |
| 157 | openAIAdapter, |
| 158 | openAIMessageFormat, |
| 159 | } from "@openuidev/react-headless"; |
| 160 | |
| 161 | export default function ChatPage() { |
| 162 | return ( |
| 163 | <FullScreen |
| 164 | componentLibrary={openuiChatLibrary} |
| 165 | streamProtocol={openAIAdapter()} |
| 166 | messageFormat={openAIMessageFormat} |
| 167 | apiUrl="http://localhost:3001/api/chat" |
| 168 | /> |
| 169 | ); |
| 170 | } |
| 171 | ``` |
| 172 | |
| 173 | > The Rust backend re-emits SSE via Axum's `Sse<...>` response (Axum wraps each `Event::d |