use rho_protocol::SessionId;
use rmcp::model::{Tool, ToolAnnotations};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::{Map, Value};
use crate::app::{
mcp_server::tool::ServedTool,
session_control::{schema_of, Action, ControlError},
};
const NOTIFY: &str = "notify";
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub(super) struct WatchParams {
pub(super) session: SessionId,
pub(super) webhook: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub(super) struct UnwatchParams {
pub(super) session: SessionId,
}
pub(crate) fn tools() -> [(ServedTool, Tool); 2] {
let (watch, unwatch) = (ServedTool::Watch.name(), ServedTool::Unwatch.name());
[
(
ServedTool::Watch,
Tool::new(
watch,
"Have this server POST a rho session's news to a webhook: each time it stops to \
wait for a person, and once when it settles, which ends the watch. Returns where \
the session is now, as `wait` does; when it settled already, that is the news \
and this call adds no watch; `watching` says whether one is left, such as one a \
concurrent `prompt` with `notify` set. `notify` on the next `prompt` watches \
again. Calling it again replaces the session's watch. `watch_error`, when present, \
says the server could not save the change, so a restart of the server loses it.",
schema_of::<WatchParams>(),
)
.with_annotations(ToolAnnotations::with_title(watch)),
),
(
ServedTool::Unwatch,
Tool::new(
unwatch,
"Stop watching a rho session; nothing more is POSTed for it.",
schema_of::<UnwatchParams>(),
)
.with_annotations(ToolAnnotations::with_title(unwatch).idempotent(true)),
),
]
}
pub(crate) fn with_notify(action: Action, mut schema: Map<String, Value>) -> Map<String, Value> {
if matches!(action, Action::New | Action::Prompt) {
if let Some(Value::Object(properties)) = schema.get_mut("properties") {
properties.insert(
NOTIFY.to_owned(),
serde_json::json!({
"type": "string",
"description": "A webhook, by name, to POST the session's news to, as \
`watch` does, starting when this turn starts. Once the turn starts the \
call succeeds; `watch_error`, when present, says what went wrong with \
the watch.",
}),
);
}
}
schema
}
pub(super) fn take_notify(
action: Action,
arguments: &mut Map<String, Value>,
) -> Result<Option<String>, ControlError> {
if !matches!(action, Action::New | Action::Prompt) {
return Ok(None);
}
match arguments.remove(NOTIFY) {
None | Some(Value::Null) => Ok(None),
Some(Value::String(webhook)) => Ok(Some(webhook)),
Some(other) => Err(ControlError::Invalid(format!(
"invalid `{}` arguments: `{NOTIFY}` is a webhook name, not {other}",
action.name()
))),
}
}