feat: add static previews to the room pane
The preview tool could only name a port, so a static build needed a server the terminal had to keep alive. `preview` now accepts a path: the app reads the file or folder over the room's ssh sandbox and serves it from a signed host, with `..` and dotfiles refused and a 16 MiB cap. The target is stored with the room and replayed as a durable event. The pane shows a static path with a reload control and keeps the port field as a local fallback.
This commit is contained in:
1 parent
5be17cf4ef
commit
36de0aace7
20 files changed
+1046
-145
No files matched your search
@@ -0,0 +1,110 @@
|
||||
//! The `preview` tool: point the room's pane at a port or a static build.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use rhema_core::request::ToolSpec;
|
||||
use rhema_core::tool::{ReplaySafety, ToolCall, ToolContext, ToolResolver, ToolResult};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::models::PreviewTarget;
|
||||
use crate::state::Shared;
|
||||
|
||||
use super::agent;
|
||||
|
||||
/// The resolver handed to the agent beside `message`.
|
||||
pub struct PreviewTool {
|
||||
state: Shared,
|
||||
room: String,
|
||||
}
|
||||
|
||||
impl PreviewTool {
|
||||
/// A tool pointing `room`'s preview.
|
||||
pub fn new(state: Shared, room: String) -> Self {
|
||||
Self { state, room }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ToolResolver for PreviewTool {
|
||||
fn specs(&self) -> Vec<ToolSpec> {
|
||||
vec![ToolSpec::new(
|
||||
"preview",
|
||||
"Point the room's preview pane at what you changed. Use `path` for a \
|
||||
static page (an HTML file, or a folder with index.html): the app reads \
|
||||
it over ssh and serves it, no server needed. Use `port` for a dev server \
|
||||
already listening in the sandbox. Set exactly one. Point a static preview \
|
||||
at a build folder, not the repository root, unless the whole tree is meant \
|
||||
to be public.",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "A workspace-relative HTML file or folder with index.html."
|
||||
},
|
||||
"port": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 65535,
|
||||
"description": "A port listening on 127.0.0.1 inside the sandbox."
|
||||
}
|
||||
}
|
||||
}),
|
||||
)]
|
||||
}
|
||||
|
||||
fn replay_safety(&self, _name: &str) -> ReplaySafety {
|
||||
ReplaySafety::Unsafe
|
||||
}
|
||||
|
||||
async fn resolve(&self, call: ToolCall, _ctx: &ToolContext) -> ToolResult {
|
||||
let port = match call.args.get("port") {
|
||||
None | Some(serde_json::Value::Null) => None,
|
||||
Some(value) => match value.as_u64().and_then(|port| u16::try_from(port).ok()) {
|
||||
Some(port) if port > 0 => Some(port),
|
||||
_ => return ToolResult::failure(call.id, "`port` must be between 1 and 65535"),
|
||||
},
|
||||
};
|
||||
let path = match call.args.get("path") {
|
||||
None | Some(serde_json::Value::Null) => None,
|
||||
Some(value) => match value.as_str() {
|
||||
Some(text) if !text.trim().is_empty() => Some(text.trim().to_owned()),
|
||||
_ => return ToolResult::failure(call.id, "`path` must be a non-empty string"),
|
||||
},
|
||||
};
|
||||
let target = match (port, path) {
|
||||
(Some(port), None) => PreviewTarget::Port { port },
|
||||
(None, Some(path)) => {
|
||||
let (workspace, sandbox_target) =
|
||||
match agent::load_room_target(&self.state, &self.room) {
|
||||
Ok(found) => found,
|
||||
Err(err) => return ToolResult::failure(call.id, err.message),
|
||||
};
|
||||
match crate::preview::static_target(&self.state, &workspace, &sandbox_target, &path)
|
||||
.await
|
||||
{
|
||||
Ok(target) => target,
|
||||
Err(message) => return ToolResult::failure(call.id, message),
|
||||
}
|
||||
}
|
||||
(Some(_), Some(_)) => {
|
||||
return ToolResult::failure(call.id, "set `path` or `port`, not both");
|
||||
}
|
||||
(None, None) => {
|
||||
return ToolResult::failure(call.id, "set `path` or `port`");
|
||||
}
|
||||
};
|
||||
if let Err(err) = crate::preview::publish(&self.state, &self.room, &target) {
|
||||
return ToolResult::failure(
|
||||
call.id,
|
||||
format!("could not open the preview: {}", err.message),
|
||||
);
|
||||
}
|
||||
let answer = match &target {
|
||||
PreviewTarget::Port { port } => format!("The room preview now shows port {port}."),
|
||||
PreviewTarget::Static { path, .. } => {
|
||||
format!("The room preview now serves {path}.")
|
||||
}
|
||||
};
|
||||
ToolResult::success(call.id, answer)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user