Skip to main content

webizen_studio/
endpoints.rs

1//! Centralized local-daemon endpoints and runtime capability detection.
2//!
3//! Previously these `127.0.0.1` URLs were scattered as string literals through
4//! `studio_canvas.rs`. In the web demo there is no local daemon, so every one of
5//! those fetches failed and spammed the console. This module gives them one home
6//! and a single capability gate (`is_native_host`) so the network effects only
7//! fire where a daemon can actually exist.
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum HostSurface {
11    PublicWeb,
12    DesktopWebview,
13}
14
15const DEFAULT_DAEMON_PORT: u16 = 8080;
16/// Local native-LLM / handshake WebSocket.
17pub const NATIVE_WS: &str = "ws://127.0.0.1:4242";
18/// Custom web-module sandbox RPC socket.
19pub const MODULE_RPC_WS: &str = "ws://127.0.0.1:9001";
20
21/// `{DAEMON_HTTP}/manifest` — workspace manifest GET/POST.
22pub fn daemon_http() -> String {
23    format!("http://127.0.0.1:{}", settings_port())
24}
25
26#[cfg(target_arch = "wasm32")]
27fn settings_port() -> u16 {
28    js_sys::Reflect::get(
29        &js_sys::global(),
30        &wasm_bindgen::JsValue::from_str("__WEBIZEN_SETTINGS_PORT"),
31    )
32    .ok()
33    .and_then(|v| v.as_f64())
34    .map(|v| v as u16)
35    .filter(|port| *port > 0)
36    .unwrap_or(DEFAULT_DAEMON_PORT)
37}
38
39#[cfg(not(target_arch = "wasm32"))]
40fn settings_port() -> u16 {
41    DEFAULT_DAEMON_PORT
42}
43
44/// Manifest URL.
45pub fn manifest_url() -> String {
46    format!("{}/manifest", daemon_http())
47}
48
49/// `{DAEMON_HTTP}/manifest/history` — Quin WAL deploy checkpoints.
50pub fn manifest_history_url() -> String {
51    format!("{}/manifest/history", daemon_http())
52}
53
54/// Live Qualia Portal design studio (T2 WASM stack).
55pub fn portal_design_studio_url() -> String {
56    format!("{}/design-studio.html", daemon_http())
57}
58
59/// Assets catalog URL.
60pub fn assets_catalog_url() -> String {
61    format!("{}/api/assets/catalog", daemon_http())
62}
63
64/// Assets enqueue URL.
65pub fn assets_enqueue_url() -> String {
66    format!("{}/api/assets/enqueue", daemon_http())
67}
68
69/// Job URL.
70pub fn job_url(job_id: &str) -> String {
71    format!("{}/api/jobs/{job_id}", daemon_http())
72}
73
74/// Manifest replay URL.
75pub fn manifest_replay_url(revision: u64) -> String {
76    format!("{}/manifest/replay/{revision}", daemon_http())
77}
78
79/// `{DAEMON_HTTP}/generate_pane` — keyword/domain pane layout planner.
80pub fn generate_pane_url() -> String {
81    format!("{}/generate_pane", daemon_http())
82}
83
84/// Manifest undo-chain URL.
85pub fn manifest_undo_chain_url() -> String {
86    format!("{}/manifest/undo-chain", daemon_http())
87}
88
89/// Manifest undo-frame URL.
90pub fn manifest_undo_frame_url(stack_index: u16) -> String {
91    format!(
92        "{}/manifest/undo-frame?stack_index={stack_index}",
93        daemon_http()
94    )
95}
96
97/// `{DAEMON_HTTP}/telemetry` — server-sent telemetry stream.
98pub fn telemetry_url() -> String {
99    format!("{}/telemetry", daemon_http())
100}
101
102/// Logs page URL.
103pub fn logs_page_url() -> String {
104    format!("{}/logs", daemon_http())
105}
106
107/// Native LLM / handshake WebSocket endpoint (desktop daemon).
108pub fn native_handshake_ws() -> &'static str {
109    NATIVE_WS
110}
111
112/// Human-readable label for the active host surface (studio status chips).
113pub fn host_surface_label(surface: HostSurface) -> &'static str {
114    match surface {
115        HostSurface::PublicWeb => "Public web demo",
116        HostSurface::DesktopWebview => "Desktop webview",
117    }
118}
119
120/// All host surfaces the studio can run in (picker + capability docs).
121pub fn all_host_surfaces() -> [HostSurface; 2] {
122    [HostSurface::DesktopWebview, HostSurface::PublicWeb]
123}
124
125/// Probe whether the native LLM handshake port is reachable (TCP preflight).
126#[cfg(not(target_arch = "wasm32"))]
127pub fn probe_native_handshake_port() -> bool {
128    let addr = NATIVE_WS
129        .trim_start_matches("ws://")
130        .trim_start_matches("wss://");
131    std::net::TcpStream::connect(addr).is_ok()
132}
133
134#[cfg(target_arch = "wasm32")]
135/// Tauri bridge — invokes the host command and decodes the JSON response.
136pub fn probe_native_handshake_port() -> bool {
137    false
138}
139
140/// Bundled Shoelace path (offline Tauri desktop). GH Pages demo uses CDN.
141pub const SHOELACE_VENDOR_BASE: &str = "/vendor/shoelace";
142pub const SHOELACE_CDN_BASE: &str =
143    "https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.0/cdn";
144
145/// Shoelace base URL.
146pub fn shoelace_base() -> &'static str {
147    if is_native_host() {
148        SHOELACE_VENDOR_BASE
149    } else {
150        SHOELACE_CDN_BASE
151    }
152}
153
154/// Shoelace stylesheet href.
155pub fn shoelace_stylesheet_href() -> String {
156    format!("{}/themes/dark.css", shoelace_base())
157}
158
159/// Shoelace autoloader src.
160pub fn shoelace_autoloader_src() -> String {
161    format!("{}/shoelace-autoloader.js", shoelace_base())
162}
163
164/// True when a local Webizen daemon could plausibly be reached.
165///
166/// On native builds this is always true. On wasm it is true only inside the
167/// Tauri desktop webview (detected via `window.__TAURI__`); a plain browser tab
168/// (the GitHub Pages demo) returns false so callers can skip daemon traffic.
169#[cfg(target_arch = "wasm32")]
170pub fn is_native_host() -> bool {
171    current_host_surface() != HostSurface::PublicWeb
172}
173
174/// True when a local Webizen daemon could plausibly be reached (always, native).
175#[cfg(not(target_arch = "wasm32"))]
176pub fn is_native_host() -> bool {
177    true
178}
179
180#[cfg(target_arch = "wasm32")]
181/// Tauri bridge — invokes the host command and decodes the JSON response.
182pub fn current_host_surface() -> HostSurface {
183    let tauri_present = js_sys::Reflect::get(
184        &js_sys::global(),
185        &wasm_bindgen::JsValue::from_str("__TAURI__"),
186    )
187    .map(|v| !v.is_undefined() && !v.is_null())
188    .unwrap_or(false);
189
190    if tauri_present {
191        HostSurface::DesktopWebview
192    } else {
193        HostSurface::PublicWeb
194    }
195}
196
197#[cfg(not(target_arch = "wasm32"))]
198/// Non-desktop fallback.
199pub fn current_host_surface() -> HostSurface {
200    HostSurface::DesktopWebview
201}
202
203/// `true` when the pane supports the browser.
204pub fn supports_browser_pane() -> bool {
205    matches!(current_host_surface(), HostSurface::DesktopWebview)
206}