pub mod http;
use std::sync::Arc;
use std::sync::mpsc;
use std::time::Duration;
use aylis_core::prop::PropKind;
use aylis_core::{Answer, Asked, Component, ComponentId, EventArgs, EventCtx, MethodInfo, Poster, PropValue, Ui, properties};
use serde_json::{Map, Value, json};
#[derive(Clone, Debug, PartialEq)]
pub struct ClaudeCode {
pub path: std::path::PathBuf,
pub in_path: bool,
}
pub fn claude_executable() -> Option<ClaudeCode> {
use std::path::{Path, PathBuf};
let names: &[&str] = if cfg!(windows) { &["claude.exe", "claude.cmd", "claude"] } else { &["claude"] };
let first_in = |dir: &Path| names.iter().map(|n| dir.join(n)).find(|p| p.is_file());
if let Some(paths) = std::env::var_os("PATH") {
if let Some(path) = std::env::split_paths(&paths).find_map(|d| first_in(&d)) {
return Some(ClaudeCode { path, in_path: true });
}
}
let home = std::env::var_os(if cfg!(windows) { "USERPROFILE" } else { "HOME" }).map(PathBuf::from);
let mut dirs: Vec<PathBuf> = Vec::new();
if let Some(home) = &home {
dirs.push(home.join(".local").join("bin"));
dirs.push(home.join(".claude").join("local"));
}
if let Some(appdata) = std::env::var_os("APPDATA").filter(|_| cfg!(windows)) {
dirs.push(PathBuf::from(appdata).join("npm"));
}
if let Some(path) = dirs.iter().find_map(|d| first_in(d)) {
return Some(ClaudeCode { path, in_path: false });
}
let bundled = if cfg!(windows) {
std::env::var_os("APPDATA").map(|a| PathBuf::from(a).join("Claude").join("claude-code"))
} else if cfg!(target_os = "macos") {
home.as_ref().map(|h| h.join("Library/Application Support/Claude/claude-code"))
} else {
None
}?;
let version = |p: &Path| -> Vec<u64> { p.file_name().map(|n| n.to_string_lossy().split('.').map(|s| s.parse().unwrap_or(0)).collect()).unwrap_or_default() };
let mut versions: Vec<PathBuf> = std::fs::read_dir(&bundled).ok()?.flatten().map(|e| e.path()).filter(|p| p.is_dir()).collect();
versions.sort_by_key(|p| std::cmp::Reverse(version(p)));
versions.iter().find_map(|v| {
let inner = std::fs::read_dir(v).ok()?.flatten().map(|e| e.path()).filter(|p| p.is_dir());
std::iter::once(v.clone()).chain(inner).find_map(|d| first_in(&d))
})
.map(|path| ClaudeCode { path, in_path: false })
}
pub const PROTOCOL: &str = "2025-06-18";
const DEFAULT_TIMEOUT: u32 = 30_000;
const READ_TOOLS: &[&str] = &["ui_forms", "ui_tree", "ui_get", "ui_items", "ui_rows"];
const READ_DATA_OPS: &[&str] = &["first", "prior", "next", "last", "move_to", "locate", "value", "text", "record", "info", "field_texts", "field_error", "lookup"];
pub struct McpServer {
pub active: bool,
pub port: u32,
pub token: String,
pub server_name: String,
pub instructions: String,
pub expose_ui: bool,
pub read_only: bool,
pub forms: String,
pub call_timeout: u32,
running: Option<http::HttpServer>,
attempted: bool,
last_error: String,
}
impl Default for McpServer {
fn default() -> Self {
Self {
active: false,
port: 0,
token: String::new(),
server_name: String::new(),
instructions: String::new(),
expose_ui: false,
read_only: false,
forms: String::new(),
call_timeout: DEFAULT_TIMEOUT,
running: None,
attempted: false,
last_error: String::new(),
}
}
}
impl McpServer {
pub fn url(&self) -> String {
self.running.as_ref().map(http::HttpServer::url).unwrap_or_default()
}
pub fn running(&self) -> bool {
self.running.is_some()
}
pub fn last_error(&self) -> String {
self.last_error.clone()
}
pub fn client_command(&self, ui: &Ui, id: ComponentId) -> String {
if self.running.is_none() {
return String::new();
}
let name = self.display_name(ui, id).replace(|c: char| !c.is_ascii_alphanumeric() && c != '-' && c != '_', "-");
let claude = match claude_executable() {
Some(ClaudeCode { path, in_path: false }) => format!("\"{}\"", path.display()),
_ => "claude".to_string(),
};
format!("{claude} mcp add --transport http {name} {} --header \"Authorization: Bearer {}\"", self.url(), self.token)
}
fn display_name(&self, ui: &Ui, id: ComponentId) -> String {
if !self.server_name.trim().is_empty() {
return self.server_name.trim().to_string();
}
ui.name(id).filter(|n| !n.is_empty()).unwrap_or("aylis-app").to_string()
}
fn allowed_forms(&self) -> Vec<String> {
self.forms.split([',', ';']).map(str::trim).filter(|f| !f.is_empty()).map(str::to_string).collect()
}
fn set_s(&mut self, _: String) {}
fn set_b(&mut self, _: bool) {}
fn sync(&mut self, ctx: &mut EventCtx<'_>) {
if ctx.ui.design_mode() {
return;
}
if !self.active {
self.running = None;
self.attempted = false;
return;
}
if self.running.is_some() || self.attempted {
return;
}
self.attempted = true;
match self.start(ctx.ui.poster(), ctx.id) {
Ok(server) => {
self.last_error.clear();
self.running = Some(server);
}
Err(e) => {
self.last_error = e.clone();
ctx.fire("on_error", EventArgs::Text(e));
}
}
}
fn start(&mut self, poster: Poster, id: ComponentId) -> Result<http::HttpServer, String> {
let port = u16::try_from(self.port).map_err(|_| format!("MCP: porta inválida: {}", self.port))?;
if self.token.is_empty() {
self.token = random_token()?;
}
let timeout = Duration::from_millis(u64::from(if self.call_timeout == 0 { DEFAULT_TIMEOUT } else { self.call_timeout }));
http::start(port, self.token.clone(), "aylis-mcp", Arc::new(move |message| handle_message(message, &poster, id, timeout)))
}
}
impl Component for McpServer {
fn class_name(&self) -> &'static str {
"McpServer"
}
properties! {
active: bool => "Behavior",
port: u32 => "Behavior",
token: String => "Behavior",
server_name: String => "Behavior",
instructions: String => "Behavior",
expose_ui: bool => "Behavior",
read_only: bool => "Behavior",
forms: String => "Behavior",
call_timeout: u32 as Duration => "Behavior";
events: on_call, on_error;
computed: url: String => url / set_s, running: bool => running / set_b, last_error: String => last_error / set_s
}
fn methods(&self) -> &'static [MethodInfo] {
const METHODS: &[MethodInfo] = &[MethodInfo::new("client_command", &[], Some(PropKind::String))];
METHODS
}
fn invoke(&mut self, ctx: &mut EventCtx<'_>, method: &str, _args: &[PropValue]) -> Result<PropValue, String> {
match method {
"client_command" => Ok(PropValue::String(self.client_command(ctx.ui, ctx.id))),
_ => Err(format!("McpServer não tem o método '{method}'")),
}
}
fn property_changed(&mut self, ctx: &mut EventCtx<'_>, name: &str) {
if matches!(name, "port" | "token" | "call_timeout") && self.running.is_some() {
self.running = None;
self.attempted = false;
}
if name == "active" {
self.attempted = false;
}
self.sync(ctx);
}
fn timer_interval(&self) -> Option<f64> {
(self.active && self.running.is_none() && !self.attempted).then_some(0.2)
}
fn timer(&mut self, ctx: &mut EventCtx<'_>) {
self.sync(ctx);
}
}
#[cfg(not(target_arch = "wasm32"))]
fn random_token() -> Result<String, String> {
let mut bytes = [0u8; 32];
getrandom::fill(&mut bytes).map_err(|e| format!("MCP: sem gerador aleatório do sistema: {e}"))?;
Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())
}
#[cfg(target_arch = "wasm32")]
fn random_token() -> Result<String, String> {
Err("MCP: o navegador não abre portas (use o app de desktop)".into())
}
pub struct McpTool {
pub tool_name: String,
pub description: String,
pub params: String,
pub read_only: bool,
pub enabled: bool,
}
impl Default for McpTool {
fn default() -> Self {
Self { tool_name: String::new(), description: String::new(), params: String::new(), read_only: false, enabled: true }
}
}
impl McpTool {
pub fn arguments(args: &EventArgs) -> Value {
args.text().and_then(|t| serde_json::from_str(t).ok()).unwrap_or_else(|| json!({}))
}
pub fn reply(ui: &mut Ui, result: impl Into<Value>) {
let text = match result.into() {
Value::String(s) => s,
other => serde_json::to_string_pretty(&other).unwrap_or_default(),
};
ui.reply("result", PropValue::String(text));
}
pub fn name_in(&self, ui: &Ui, id: ComponentId) -> String {
let name = self.tool_name.trim();
if name.is_empty() { ui.name(id).unwrap_or_default().to_string() } else { name.to_string() }
}
pub fn schema(&self) -> Result<Value, String> {
let text = self.params.trim();
if text.is_empty() {
return Ok(json!({ "type": "object", "properties": {} }));
}
if text.starts_with('{') {
let v: Value = serde_json::from_str(text).map_err(|e| format!("params: JSON inválido: {e}"))?;
if v.get("type").is_some() {
return Ok(v);
}
return Ok(json!({ "type": "object", "properties": v }));
}
let mut properties = Map::new();
let mut required = Vec::new();
for part in text.split(',').map(str::trim).filter(|p| !p.is_empty()) {
let (name, kind) = part.split_once(':').map_or((part, "string"), |(n, k)| (n.trim(), k.trim()));
let (name, optional) = match name.strip_suffix('?') {
Some(n) => (n.trim(), true),
None => (name, false),
};
if name.is_empty() || !name.chars().all(|c| c.is_alphanumeric() || c == '_') {
return Err(format!("params: nome inválido: '{part}'"));
}
let kind = match kind.to_ascii_lowercase().as_str() {
"" | "string" | "text" | "texto" => "string",
"integer" | "int" | "inteiro" => "integer",
"number" | "float" | "numero" | "número" => "number",
"boolean" | "bool" => "boolean",
"object" | "objeto" => "object",
"array" | "lista" => "array",
other => return Err(format!("params: tipo desconhecido '{other}' em '{part}'")),
};
properties.insert(name.to_string(), json!({ "type": kind }));
if !optional {
required.push(Value::String(name.to_string()));
}
}
Ok(json!({ "type": "object", "properties": properties, "required": required }))
}
}
impl Component for McpTool {
fn class_name(&self) -> &'static str {
"McpTool"
}
properties! {
tool_name: String => "Behavior",
description: String => "Behavior",
params: String => "Behavior",
read_only: bool => "Behavior",
enabled: bool => "Behavior";
events: on_execute
}
fn default_event(&self) -> Option<&'static str> {
Some("on_execute")
}
}
pub fn register(ui: &mut Ui) {
ui.register_class::<McpServer>("System");
ui.register_class::<McpTool>("System");
}
fn handle_message(message: Value, poster: &Poster, server: ComponentId, timeout: Duration) -> Option<Value> {
let id = message.get("id").cloned()?; let method = message.get("method").and_then(Value::as_str).unwrap_or_default().to_string();
let params = message.get("params").cloned().unwrap_or(Value::Null);
let result = match method.as_str() {
"initialize" => on_ui(poster, timeout, move |ui, reply| reply.send(initialize(ui, server, ¶ms))),
"ping" => Ok(json!({})),
"tools/list" => on_ui(poster, timeout, move |ui, reply| reply.send(json!({ "tools": tools(ui, server) }))),
"tools/call" => {
let name = params.get("name").and_then(Value::as_str).unwrap_or_default().to_string();
let args = params.get("arguments").cloned().filter(Value::is_object).unwrap_or_else(|| json!({}));
on_ui(poster, timeout, move |ui, reply| call_tool(ui, server, name, args, reply)).or_else(|e| Ok(text_result(e, true)))
}
_ => return Some(http::rpc_error(id, -32601, &format!("método desconhecido: {method}"))),
};
Some(match result {
Ok(r) => json!({ "jsonrpc": "2.0", "id": id, "result": r }),
Err(e) => http::rpc_error(id, -32603, &e),
})
}
struct Reply(mpsc::Sender<Value>);
impl Reply {
fn send(&self, value: Value) {
let _ = self.0.send(value);
}
}
fn on_ui(poster: &Poster, timeout: Duration, f: impl FnOnce(&mut Ui, Reply) + Send + 'static) -> Result<Value, String> {
let (tx, rx) = mpsc::channel();
poster.post(move |ui| f(ui, Reply(tx)));
rx.recv_timeout(timeout).map_err(|_| "o app não respondeu a tempo".to_string())
}
fn text_result(text: impl Into<String>, is_error: bool) -> Value {
json!({ "content": [{ "type": "text", "text": text.into() }], "isError": is_error })
}
fn json_text(v: &Value) -> String {
match v {
Value::String(s) => s.clone(),
Value::Null => "ok".into(),
other => serde_json::to_string_pretty(other).unwrap_or_default(),
}
}
fn initialize(ui: &Ui, server: ComponentId, params: &Value) -> Value {
let (name, mut instructions, expose_ui) = match ui.get::<McpServer>(server) {
Some(s) => (s.display_name(ui, server), s.instructions.trim().to_string(), s.expose_ui),
None => ("aylis-app".to_string(), String::new(), false),
};
if expose_ui {
if !instructions.is_empty() {
instructions.push_str("\n\n");
}
instructions.push_str("As ferramentas ui_* usam a tela do app pelos componentes (sem olhar pixels): ui_forms lista os forms, ui_tree mostra os componentes de um form (nome, classe, caminho), ui_get lê propriedades, ui_items os itens que um controle desenha (abas, linhas, nós), ui_rows os registros de um ClientDataSet. O alvo é o nome ou o caminho (frmClientes.edtNome).");
}
json!({
"protocolVersion": params.get("protocolVersion").and_then(Value::as_str).unwrap_or(PROTOCOL),
"capabilities": { "tools": { "listChanged": false } },
"serverInfo": { "name": name, "version": env!("CARGO_PKG_VERSION") },
"instructions": instructions,
})
}
fn app_tools(ui: &Ui, server: ComponentId) -> Vec<(ComponentId, String)> {
ui.children(server)
.iter()
.filter_map(|&c| ui.get::<McpTool>(c).filter(|t| t.enabled).map(|t| (c, t.name_in(ui, c))))
.filter(|(_, n)| !n.is_empty())
.collect()
}
fn tool(name: &str, description: &str, properties: Value, required: &[&str], read_only: bool) -> Value {
json!({
"name": name,
"description": description,
"inputSchema": { "type": "object", "properties": properties, "required": required },
"annotations": { "readOnlyHint": read_only },
})
}
pub fn tools(ui: &Ui, server: ComponentId) -> Vec<Value> {
let Some(srv) = ui.get::<McpServer>(server) else { return Vec::new() };
let mut out: Vec<Value> = app_tools(ui, server)
.into_iter()
.filter_map(|(id, name)| {
let t = ui.get::<McpTool>(id)?;
let schema = t.schema().unwrap_or_else(|_| json!({ "type": "object" }));
Some(json!({ "name": name, "description": t.description, "inputSchema": schema, "annotations": { "readOnlyHint": t.read_only } }))
})
.collect();
if srv.expose_ui {
out.extend(ui_tools().into_iter().filter(|t| !srv.read_only || READ_TOOLS.contains(&t["name"].as_str().unwrap_or_default())));
if srv.read_only {
out.push(data_tool(true));
}
}
out
}
fn ui_tools() -> Vec<Value> {
let s = |d: &str| json!({ "type": "string", "description": d });
let target = s("o componente: nome ou caminho (frmClientes.edtNome)");
vec![
tool("ui_forms", "Os forms criados (e DataModules): nome, classe, título, se está aberto (`open`) e visível.", json!({}), &[], true),
tool("ui_tree", "A árvore de componentes de um form: nome, classe, caminho, visível e filhos.", json!({ "form": s("o form (vazio: todos)") }), &[], true),
tool("ui_get", "As propriedades de um componente (todas, com tipo e valor) ou só uma (`prop`).", json!({ "target": target, "prop": s("a propriedade, opcional") }), &["target"], true),
tool("ui_items", "Os itens que um controle desenha sozinho (abas, linhas de lista e grid, nós de árvore, itens de menu): papel, texto, selecionado, marcado.", json!({ "target": target }), &["target"], true),
tool("ui_rows", "Os registros de um ClientDataSet (como o grid mostra, sem rolar), a partir de `offset` (padrão 0), até `limit` (padrão 50), e o total.", json!({ "target": target, "offset": { "type": "integer" }, "limit": { "type": "integer" } }), &["target"], true),
tool("ui_set", "Muda uma propriedade (`value` é o valor JSON, como no layout: texto, número, booleano, \"#rrggbb\").", json!({ "target": target, "prop": s("a propriedade"), "value": {} }), &["target", "prop", "value"], false),
tool("ui_click", "Clica num controle (botão, caixa, item de menu), como o usuário.", json!({ "target": target }), &["target"], false),
tool("ui_click_item", "Clica num item desenhado (de ui_items) pelo texto (`item`) ou pela posição (`index`); `action`: click, select, expand ou collapse.", json!({ "target": target, "item": s("o texto do item"), "index": { "type": "integer" }, "action": s("click | select | expand | collapse") }), &["target"], false),
tool("ui_type", "Põe o foco no controle e digita o texto, como o teclado (passa pelas máscaras e validações).", json!({ "target": target, "text": s("o texto") }), &["target", "text"], false),
tool("ui_key", "Aperta uma tecla no form do controle (\"Enter\", \"Tab\", \"Escape\", \"Ctrl+S\"); com `target`, o foco vai antes para ele.", json!({ "target": target, "key": s("a tecla") }), &["target", "key"], false),
tool("ui_invoke", "Chama um método do componente (os do ui_get de cada classe: refresh, clear...).", json!({ "target": target, "method": s("o método"), "args": { "type": "array" } }), &["target", "method"], false),
data_tool(false),
]
}
fn data_tool(read_only: bool) -> Value {
let ops = if read_only { READ_DATA_OPS.join(", ") } else { "first, next, prior, last, locate, record, info, edit, insert, set_values, post, cancel, delete, apply...".to_string() };
tool(
"ui_data",
&format!("Uma operação num ClientDataSet (`op`: {ops}), com os campos dela (`field`, `value`, `values`, `index`)."),
json!({ "target": { "type": "string" }, "op": { "type": "string" }, "field": { "type": "string" }, "value": {}, "values": { "type": "object" }, "index": { "type": "integer" } }),
&["target", "op"],
read_only,
)
}
fn call_tool(ui: &mut Ui, server: ComponentId, name: String, args: Value, reply: Reply) {
if !ui.get::<McpServer>(server).is_some_and(|s| s.active) {
return reply.send(text_result("o servidor MCP foi desligado", true));
}
if ui.has_handler(server, "on_call") {
let info = json!({ "tool": name, "arguments": args }).to_string();
match ui.ask(server, "on_call", &EventArgs::Text(info)) {
Asked::Answered(Answer::Refused(m)) => return reply.send(text_result(refused(m), true)),
Asked::Answered(Answer::Accepted(_)) => {}
Asked::Deferred(token) => {
return ui.on_answer(token, move |ui, answer| match answer {
Answer::Refused(m) => reply.send(text_result(refused(m), true)),
Answer::Accepted(_) => run_tool(ui, server, &name, &args, reply),
});
}
}
}
run_tool(ui, server, &name, &args, reply);
}
fn refused(message: String) -> String {
if message.trim().is_empty() { "chamada recusada pelo app".into() } else { message }
}
fn run_tool(ui: &mut Ui, server: ComponentId, name: &str, args: &Value, reply: Reply) {
if let Some((tool, _)) = app_tools(ui, server).into_iter().find(|(_, n)| n == name) {
return execute_tool(ui, tool, args, reply);
}
let Some(srv) = ui.get::<McpServer>(server) else { return reply.send(text_result("o servidor MCP foi liberado", true)) };
let listed = srv.expose_ui && (!srv.read_only || READ_TOOLS.contains(&name) || name == "ui_data");
if !listed || !name.starts_with("ui_") {
return reply.send(text_result(format!("ferramenta desconhecida: {name}"), true));
}
let result = ui_tool(ui, server, name, args);
ui.process_events();
reply.send(match result {
Ok(v) => text_result(json_text(&v), false),
Err(e) => text_result(e, true),
});
}
fn execute_tool(ui: &mut Ui, tool: ComponentId, args: &Value, reply: Reply) {
if !ui.has_handler(tool, "on_execute") {
let name = ui.name(tool).unwrap_or_default().to_string();
return reply.send(text_result(format!("a ferramenta '{name}' não tem o on_execute ligado"), true));
}
match ui.ask(tool, "on_execute", &EventArgs::Text(args.to_string())) {
Asked::Answered(answer) => {
ui.process_events();
reply.send(answer_result(ui, answer));
}
Asked::Deferred(token) => ui.on_answer(token, move |ui, answer| reply.send(answer_result(ui, answer))),
}
}
fn answer_result(ui: &Ui, answer: Answer) -> Value {
match answer {
Answer::Refused(m) => text_result(if m.trim().is_empty() { "a ferramenta recusou".into() } else { m }, true),
Answer::Accepted(values) => {
let text = values.iter().rev().find(|(k, _)| k == "result").map(|(_, v)| match v {
PropValue::String(s) => s.clone(),
other => json_text(&aylis_json::prop_to_json(ui, other)),
});
text_result(text.unwrap_or_else(|| "ok".into()), false)
}
}
}
fn reach(ui: &Ui, server: ComponentId, value: Option<&Value>) -> Result<ComponentId, String> {
let id = crate::resolve(ui, value.ok_or("falta \"target\"")?)?;
let mut at = Some(id);
while let Some(c) = at {
let class = ui.component(c).map(|c| c.class_name()).unwrap_or_default();
if c == server || class == "McpServer" || class == "McpTool" {
return Err("o servidor MCP e as ferramentas dele não podem ser comandados por aqui".into());
}
at = ui.parent(c);
}
form_allowed(ui, server, ui.root_of(id))?;
Ok(id)
}
fn form_allowed(ui: &Ui, server: ComponentId, form: ComponentId) -> Result<(), String> {
let allowed = ui.get::<McpServer>(server).map(McpServer::allowed_forms).unwrap_or_default();
let name = ui.name(form).unwrap_or_default();
if allowed.is_empty() || allowed.iter().any(|f| f.eq_ignore_ascii_case(name)) {
Ok(())
} else {
Err(format!("o form '{name}' não está liberado para o agente"))
}
}
fn ui_tool(ui: &mut Ui, server: ComponentId, name: &str, args: &Value) -> Result<Value, String> {
let read_only = ui.get::<McpServer>(server).is_some_and(|s| s.read_only);
let str_arg = |key: &str| args.get(key).and_then(Value::as_str).unwrap_or_default().to_string();
match name {
"ui_forms" => Ok(Value::Array(
ui.roots()
.iter()
.filter(|&&r| form_allowed(ui, server, r).is_ok() && !ui.name(r).unwrap_or_default().is_empty())
.map(|&r| {
let class = ui.component(r).map(|c| c.class_name()).unwrap_or_default();
let caption = ui.get_prop(r, "caption").ok().and_then(|v| v.to_str().ok().map(str::to_string));
let open = ui.is_open(r);
json!({ "name": ui.name(r), "class": class, "caption": caption, "open": open, "visible": open && ui.control(r).is_some_and(|c| c.visible()) })
})
.collect(),
)),
"ui_tree" => {
let form = str_arg("form");
if form.is_empty() {
let roots: Vec<ComponentId> = ui.roots().iter().copied().filter(|&r| form_allowed(ui, server, r).is_ok()).collect();
return Ok(Value::Array(roots.into_iter().map(|r| crate::inspect::tree(ui, Some(r))).collect()));
}
let id = reach(ui, server, Some(&Value::String(form)))?;
Ok(crate::inspect::tree(ui, Some(id)))
}
"ui_get" => {
let id = reach(ui, server, args.get("target"))?;
match args.get("prop").and_then(Value::as_str).filter(|p| !p.is_empty()) {
Some(prop) => crate::get(ui, id, prop),
None => Ok(crate::inspect::props(ui, id)),
}
}
"ui_items" => {
let id = reach(ui, server, args.get("target"))?;
crate::execute(ui, &json!({ "cmd": "items", "target": id.to_u64() }))
}
"ui_rows" => {
let id = reach(ui, server, args.get("target"))?;
rows(ui, id, args)
}
"ui_data" => {
let id = reach(ui, server, args.get("target"))?;
let op = str_arg("op");
if read_only && !READ_DATA_OPS.contains(&op.as_str()) {
return Err(format!("o servidor está só para leitura: a operação '{op}' não vale"));
}
let mut command = args.as_object().cloned().unwrap_or_default();
command.insert("cmd".into(), "data".into());
command.insert("target".into(), id.to_u64().into());
crate::execute(ui, &Value::Object(command))
}
_ if read_only => Err(format!("o servidor está só para leitura: '{name}' não vale")),
"ui_set" => {
let id = reach(ui, server, args.get("target"))?;
let prop = str_arg("prop");
if prop.is_empty() {
return Err("falta \"prop\"".into());
}
let value = args.get("value").cloned().unwrap_or(Value::Null);
crate::execute(ui, &json!({ "cmd": "set", "target": id.to_u64(), "props": { prop: value } }))
}
"ui_click" => {
let id = reach(ui, server, args.get("target"))?;
crate::execute(ui, &json!({ "cmd": "click", "target": id.to_u64() }))
}
"ui_click_item" => {
let id = reach(ui, server, args.get("target"))?;
let mut command = json!({ "cmd": "click_item", "target": id.to_u64() });
for key in ["item", "index", "action"] {
if let Some(v) = args.get(key) {
command[key] = v.clone();
}
}
crate::execute(ui, &command)
}
"ui_type" => {
let id = reach(ui, server, args.get("target"))?;
ui.set_focus(id);
crate::execute(ui, &json!({ "cmd": "type", "target": id.to_u64(), "text": str_arg("text") }))
}
"ui_key" => {
let id = reach(ui, server, args.get("target"))?;
if ui.root_of(id) != id {
ui.set_focus(id);
}
crate::execute(ui, &json!({ "cmd": "key", "target": id.to_u64(), "key": str_arg("key") }))
}
"ui_invoke" => {
let id = reach(ui, server, args.get("target"))?;
crate::execute(ui, &json!({ "cmd": "invoke", "target": id.to_u64(), "method": str_arg("method"), "args": args.get("args").cloned().unwrap_or_else(|| json!([])) }))
}
_ => Err(format!("ferramenta desconhecida: {name}")),
}
}
fn rows(ui: &Ui, id: ComponentId, args: &Value) -> Result<Value, String> {
if ui.get::<aylis_data::ClientDataSet>(id).is_none() {
return Err(format!("'{}' não é um ClientDataSet", ui.name(id).unwrap_or("?")));
}
let v = aylis_data::view(ui, id);
let count = v.record_count();
let offset = args.get("offset").and_then(Value::as_u64).unwrap_or(0) as usize;
let limit = args.get("limit").and_then(Value::as_u64).unwrap_or(50).clamp(1, 1000) as usize;
let names = v.field_names();
let list: Vec<Value> = (offset..count.min(offset.saturating_add(limit)))
.map(|i| {
let mut row = Map::new();
for name in &names {
let value = v.value_at(i, name).map(|x| aylis_data::json::value_to_json(&x)).unwrap_or(Value::Null);
row.insert(name.clone(), value);
}
Value::Object(row)
})
.collect();
Ok(json!({ "count": count, "offset": offset, "rows": list }))
}