1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Opinionated read-only dashboard for Polaris sessions.
//!
//! Register [`DashboardPlugin`] on a [`polaris_ai::system::server::Server`] to
//! mount the bundled `SvelteKit` SPA at a configurable base path. The dashboard
//! is read-only and consumes session, run, and span data exposed by
//! [`HttpPlugin`](polaris_ai::sessions::HttpPlugin) and
//! [`TracingPlugin`](polaris_ai::plugins::TracingPlugin).
//!
//! The SPA is not built by this crate. It must be compiled and built in its own
//! repository; the build output is dropped into `assets/` and embedded at
//! compile time via `rust-embed`. With an empty `assets/`, the crate still
//! compiles and every dashboard route returns a "bundle missing" notice instead
//! of the UI.
//!
//! # Required backend plugins
//!
//! The SPA calls `/v1/sessions/*` (mounted by `HttpPlugin`) and
//! `/v1/tracing/*` (mounted by `TracingPlugin` when its `dashboard`
//! feature is active). Per the upstream plugin stack, that requires:
//!
//! - `ServerInfoPlugin` (foundation)
//! - `AppPlugin` (HTTP router)
//! - `ModelsPlugin`, `ToolsPlugin` (transitive deps of `TracingPlugin`
//! when `dashboard` is on)
//! - `SessionsPlugin` + `HttpPlugin` (sessions REST surface)
//! - `TracingPlugin` (the `/v1/tracing/*` runs / span-tree endpoints)
//!
//! `SpanStorePlugin` is optional but recommended for restart-safe run
//! history. Hosts that omit `TracingPlugin` will see "Failed to load
//! runs: 404" in the runs pane — the bundled `examples/serve.rs` is a
//! minimal complete wiring.
//!
//! # Replacing the SPA
//!
//! This crate is framework-agnostic: it embeds whatever static bundle lives in
//! `assets/` and serves it: `index.html` at the base path, embedded files for
//! matching sub-paths, and `index.html` as the fallback for everything else
//! (client-side routing). It does no templating and injects no config. Any
//! framework (React, Vue, Solid, plain HTML, …) works in place of the bundled
//! `SvelteKit` SPA as long as the bundle satisfies this contract:
//!
//! 1. **Client-routed, rooted at `index.html`** — there is no SSR; unmatched
//! paths under the base return `index.html` verbatim.
//! 2. **Asset URLs resolve under the mount base** — the SPA's build-time base
//! must equal [`DashboardPlugin`]'s `base_path` (the bundled SPA is built for
//! `/dashboard`). A mismatch 404s every asset; [`DashboardPlugin`] logs a
//! warning when it can detect one.
//! 3. **Same-origin, relative API calls** to the documented contracts:
//! `/v1/sessions/*` (from `HttpPlugin`) and `/v1/tracing/*` (from
//! `TracingPlugin` or [`OtelTracingPlugin`]), consuming the [`RunSummary`] /
//! [`SpanTree`] JSON shapes. The host supplies no API base — the SPA assumes
//! same origin.
//! 4. **No host-injected configuration** — the SPA must self-configure.
//!
//! # Auth
//!
//! Authentication is driven by whatever `AuthProvider` the host server
//! registers on `polaris_ai::app::HttpRouter::set_auth`. The dashboard plugin
//! does not register its own provider — its routes inherit the global
//! middleware applied by `polaris_ai::app::AppPlugin`.
//!
//! # Example
//!
//! ```no_run
//! use std::sync::Arc;
//! use polaris_ai::app::{AppConfig, AppPlugin};
//! use polaris_ai::sessions::{HttpPlugin, InMemoryStore, SessionsPlugin};
//! use polaris_ai::system::server::Server;
//! use polaris_dashboard::DashboardPlugin;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let mut server = Server::new();
//! server
//! .add_plugins(AppPlugin::new(AppConfig::new()))
//! .add_plugins(SessionsPlugin::new(Arc::new(InMemoryStore::new())))
//! .add_plugins(HttpPlugin::new())
//! .add_plugins(DashboardPlugin::default());
//! server.run().await?;
//! # Ok(())
//! # }
//! ```
pub use ;
use Router;
use Path;
use ;
use ;
use get;
use ;
use ;
use Server;
use RustEmbed;
/// Embedded SPA bundle.
///
/// The SPA is built in its own repository; its build output is dropped into
/// this crate's `assets/` directory before `cargo build` runs, and embedded at
/// compile time. When the bundle is missing, every route returns a helpful
/// error instead of panicking.
;
/// Plugin that serves the bundled Polaris sessions dashboard SPA over HTTP.
///
/// The plugin only hosts static assets; all session data comes from
/// [`HttpPlugin`](polaris_ai::sessions::HttpPlugin), which must be registered
/// alongside it.
///
/// # Routes Provided
///
/// Mounted relative to the configured base path (default `/dashboard`):
///
/// | Method | Path | Description |
/// |--------|------|-------------|
/// | `GET` | `{base}` | SPA shell (`index.html`). |
/// | `GET` | `{base}/` | SPA shell — trailing-slash form. |
/// | `GET` | `{base}/{*path}` | Embedded asset, or the SPA shell as a client-routing fallback. |
///
/// # Resources Provided
///
/// | Resource | Scope | Description |
/// |----------|-------|-------------|
/// | _none_ | — | This plugin only mounts HTTP routes against [`HttpRouter`]. |
///
/// # APIs Provided
///
/// None.
///
/// # Dependencies
///
/// - [`AppPlugin`] — provides the [`HttpRouter`] the SPA is mounted on. This is
/// the only build-order dependency.
///
/// The SPA needs a data backend at runtime, but those are *not* build
/// dependencies of this plugin — register them yourself:
/// [`SessionsPlugin`](polaris_ai::sessions::SessionsPlugin) +
/// [`HttpPlugin`](polaris_ai::sessions::HttpPlugin) for the `/v1/sessions/*`
/// surface, and a `/v1/tracing/*` provider (the upstream `TracingPlugin` via
/// the default `native-tracing` feature, or [`OtelTracingPlugin`]). Omitting
/// the tracing surface yields "Failed to load runs: 404" in the runs pane.
///
/// # Lifecycle
///
/// - **Feature `native-tracing` (default-on):** pulls in the upstream
/// `TracingPlugin` HTTP surface (`/v1/tracing/*`) the runs pane reads. Drop it
/// (`default-features = false`) only when substituting [`OtelTracingPlugin`]
/// as the trace surface — the two conflict on `/v1/tracing/runs`.
/// - **`build()` panics** if no [`AppPlugin`] (the [`HttpRouter`] provider) has
/// been registered before this plugin.
///
/// # Example
///
/// ```no_run
/// use std::sync::Arc;
/// use polaris_ai::app::{AppConfig, AppPlugin};
/// use polaris_ai::sessions::{HttpPlugin, InMemoryStore, SessionsPlugin};
/// use polaris_ai::system::server::Server;
/// use polaris_dashboard::DashboardPlugin;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut server = Server::new();
/// server
/// .add_plugins(AppPlugin::new(AppConfig::new()))
/// .add_plugins(SessionsPlugin::new(Arc::new(InMemoryStore::new())))
/// .add_plugins(HttpPlugin::new())
/// .add_plugins(DashboardPlugin::new("/admin"));
/// server.run().await?;
/// # Ok(())
/// # }
/// ```
async
async
/// Extract the base path a SPA bundle baked into its asset URLs, if detectable.
///
/// Looks for the first SvelteKit-style `/_app/` asset reference and returns the
/// prefix in front of it (`/dashboard/_app/...` → `/dashboard`, `/_app/...` →
/// `""`). Returns `None` for bundles without that marker — the crate then makes
/// no claim about the SPA's base and skips the mismatch check.