forked from vhaudiquet/rhema-together
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
+6
-2
@@ -8,7 +8,7 @@ use std::sync::Arc;
|
||||
use base64::Engine;
|
||||
use rhema_bash::{AllowAll, ShellTools};
|
||||
use rhema_core::request::ThinkingEffort;
|
||||
use rhema_core::{Session, ToolResolver};
|
||||
use rhema_core::{Overlay, Session, ToolResolver};
|
||||
use rhema_host_agent::{HostAgent, HostAgentConfig};
|
||||
use rhema_sandbox::WorkspaceSeed;
|
||||
use rhema_sandbox::{Sandbox, ShellSandbox};
|
||||
@@ -18,6 +18,7 @@ use crate::error::{AppError, AppResult};
|
||||
use crate::state::Shared;
|
||||
|
||||
use super::message_tool::MessageTool;
|
||||
use super::preview_tool::PreviewTool;
|
||||
|
||||
/// The room and its configuration, read fresh for every turn.
|
||||
pub struct TurnConfig {
|
||||
@@ -310,6 +311,9 @@ pub fn build_agent(state: &Shared, config: &TurnConfig, session: Session) -> App
|
||||
.map_err(|err| AppError::bad_request(format!("provider: {err}")))?;
|
||||
let message_tool: Arc<dyn ToolResolver> =
|
||||
Arc::new(MessageTool::new(state.clone(), config.room_id.clone()));
|
||||
let preview_tool: Arc<dyn ToolResolver> =
|
||||
Arc::new(PreviewTool::new(state.clone(), config.room_id.clone()));
|
||||
let extra_tools: Arc<dyn ToolResolver> = Arc::new(Overlay::new(message_tool, preview_tool));
|
||||
let max_turn_steps =
|
||||
NonZeroU32::new(config.max_turn_steps).unwrap_or(NonZeroU32::new(48).unwrap());
|
||||
let mut sections = BTreeMap::new();
|
||||
@@ -330,7 +334,7 @@ pub fn build_agent(state: &Shared, config: &TurnConfig, session: Session) -> App
|
||||
max_turn_steps,
|
||||
thinking: ThinkingEffort::Default,
|
||||
tools: Some(shell),
|
||||
extra_resolver: Some(message_tool),
|
||||
extra_resolver: Some(extra_tools),
|
||||
shell_env: Default::default(),
|
||||
transcript: Some(Default::default()),
|
||||
summary_worker: None,
|
||||
|
||||
@@ -79,4 +79,5 @@ impl RoomRunner {
|
||||
|
||||
pub mod agent;
|
||||
pub mod message_tool;
|
||||
pub mod preview_tool;
|
||||
pub mod runner;
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,19 @@ reading, searching, editing, and running tests. Leave the tree in a state
|
||||
someone else can pick up. When you finish, say which files changed and which
|
||||
checks you ran.
|
||||
|
||||
## Preview
|
||||
|
||||
When what you changed is visible, point the room at it with the `preview`
|
||||
tool. Use `path` for a static page: an HTML file, or a folder with an
|
||||
`index.html`. The app reads it over ssh and serves it, so there is no server
|
||||
to start and nothing to keep alive. Point at a build folder, not the whole
|
||||
repository, unless every file in the tree is meant to be readable through the
|
||||
preview link.
|
||||
|
||||
Use `port` for a dev server that is already listening on 127.0.0.1 in the
|
||||
sandbox. Set one of `path` or `port`, never both. The pane opens it for
|
||||
everyone in the room.
|
||||
|
||||
{% if tools %}
|
||||
## Tools
|
||||
|
||||
|
||||
Reference in new issue
Block a user