Rótulo do bloco declarado pela library (tb_type_block_set_label)

A library diz como o bloco aparece no editor ("liste as ferramentas de
%1", valores iniciais "a|b"); o editor usa a tabela dele quando conhece
o tipo e o rótulo da library quando não conhece. Assim uma library
carregada de fora não aparece só com o nome técnico.

Acessibilidade: aviso comum não repete "aviso" na descrição.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 07:50:39 -03:00
1 parent 8f046833e7
commit 2d580005b1
4 files changed
+33 -112

No files matched your search

-109
View File
@@ -1,109 +0,0 @@
/*
* mcp.c - Plugin TinyBlocks com blocos MCP (Model Context Protocol).
*
* Blocos:
* MCP.Server - literal: armazena a URL do servidor MCP
* HTTP: "http://localhost:3000/mcp"
* stdio: "stdio://npx @modelcontextprotocol/server-filesystem /tmp"
*
* MCP.Ping - input: url (text) -> output: status (text)
* Verifica se o servidor responde.
*
* MCP.ListTools - input: url (text) -> output: tools (text)
* Lista ferramentas disponíveis no servidor.
*
* MCP.Call - inputs: url, tool, args (text) -> output: result (text)
* Chama uma ferramenta com argumentos JSON.
*
* Compilar:
* make mcp-lib
*
* Carregar no editor:
* ./build/editor build/mcp.so ou Ctrl+O -> build/mcp.so
*
* Exemplo de uso no editor:
* [Literal.Text "stdio://npx @modelcontextprotocol/server-filesystem /tmp"]
* -> [MCP.ListTools] -> [IO.Print "tools"]
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks-mcp/tb_mcp.h>
/* -- Helper: lê texto de slot --- */
static const char* gs(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) {
TinyBlocks_Value v = tb_block_get_input(self, rt, idx);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
/* -- MCP.Ping --- */
static void eval_mcp_ping(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* url = gs(self, rt, 0);
char* result = tb_mcp_ping(url);
tb_block_set_output(self, 0, tb_value_text(result ? result : "erro"));
free(result);
self->dirty = false;
}
/* -- MCP.ListTools --- */
static void eval_mcp_list_tools(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* url = gs(self, rt, 0);
char* result = tb_mcp_list_tools(url);
tb_block_set_output(self, 0, tb_value_text(result ? result : "erro"));
free(result);
self->dirty = false;
}
/* -- MCP.Call --- */
static void eval_mcp_call(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* url = gs(self, rt, 0);
const char* tool = gs(self, rt, 1);
const char* args = gs(self, rt, 2);
char* result = tb_mcp_call_tool(url, tool, args);
tb_block_set_output(self, 0, tb_value_text(result ? result : "erro"));
free(result);
self->dirty = false;
}
/* -- Slots --- */
static const TinyBlocks_SlotDesc url_in[] = {{"url", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc call_in[] = {{"url", TB_VALUE_TEXT},
{"tool", TB_VALUE_TEXT},
{"args", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc status_out[] = {{"status", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc tools_out[] = {{"tools", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc result_out[] = {{"result", TB_VALUE_TEXT}};
/* -- Registro --- */
static bool mcp_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
if (!tb_mcp_init()) return false;
TinyBlocks_TypeBlock* t;
t = tb_type_block_create(rt, "MCP.Ping",
url_in, 1, status_out, 1, eval_mcp_ping);
if (t) tb_library_add(lib, &t->base);
t = tb_type_block_create(rt, "MCP.ListTools",
url_in, 1, tools_out, 1, eval_mcp_list_tools);
if (t) tb_library_add(lib, &t->base);
t = tb_type_block_create(rt, "MCP.Call",
call_in, 3, result_out, 1, eval_mcp_call);
if (t) tb_library_add(lib, &t->base);
return true;
}
static void mcp_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
tb_mcp_deinit();
}
TINYBLOCKS_DEFINE_LIBRARY(mcp,
"tinyblocks-mcp",
"1.0.0",
mcp_init,
mcp_deinit)
+8
View File
@@ -78,6 +78,12 @@ struct TinyBlocks_TypeBlock {
(tb_block_get_input) quando e se quiser. Ex.: "tente obter", que
precisa ver o erro de dentro para transforma-lo em valor. */
bool lazy_inputs;
/* Rotulo no editor (strings estaticas da library; NULL = o editor decide):
texto com %1, %2... no lugar das entradas e os valores iniciais dos
campos separados por '|'. Ex.: "liste as ferramentas de %1". */
const char* label;
const char* label_defaults;
};
/* -- Forma e tracos --- */
@@ -86,6 +92,8 @@ TB_API void tb_type_block_set_flow (TinyBlocks_TypeBlock* tb, const TinyBlocks_F
TB_API void tb_type_block_set_flags(TinyBlocks_TypeBlock* tb, uint32_t flags); /* soma (OR) */
/* Chapeu disparado por um evento (string estatica, ex.: TB_EVENT_FLAG) */
TB_API void tb_type_block_set_event(TinyBlocks_TypeBlock* tb, const char* event);
/* Rotulo do bloco no editor (%N = entrada N) e valores iniciais "a|b|c" */
TB_API void tb_type_block_set_label(TinyBlocks_TypeBlock* tb, const char* label, const char* defaults);
TB_API void tb_type_block_set_input_flags (TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags);
TB_API void tb_type_block_set_output_flags(TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags);
TB_API uint32_t tb_type_block_input_flags (const TinyBlocks_TypeBlock* tb, uint32_t i);
+16 -3
View File
@@ -163,6 +163,19 @@ static const BlockSpec* spec_of(const char* tn) {
return NULL;
}
/* Rotulo do tipo: o da tabela do editor ou o que a library declarou
* (tb_type_block_set_label); NULL = so o nome e os campos */
static const BlockSpec* spec_of_type(const TinyBlocks_TypeBlock* t) {
static BlockSpec own;
if (!t) return NULL;
const BlockSpec* sp = spec_of(t->type_name);
if (sp || !t->label) return sp;
own.type = t->type_name;
own.label = t->label;
own.defs = t->label_defaults;
return &own;
}
static int input_count(const TinyBlocks_TypeBlock* t) {
if (!t) return 0;
return t->input_desc_count < MAX_INPUTS ? (int)t->input_desc_count : MAX_INPUTS;
@@ -305,7 +318,7 @@ static void blk_defaults(EdBlock* b) {
}
}
}
const BlockSpec* sp = t ? spec_of(t->type_name) : NULL;
const BlockSpec* sp = spec_of_type(t);
const char* p = sp ? sp->defs : NULL;
for (int i = 0; i < n && p; i++) {
const char* e = strchr(p, '|');
@@ -565,7 +578,7 @@ static void build_parts(EdBlock* b) {
if (!t) { add_part(b, -1, b->type_name, (int)strlen(b->type_name)); return; }
int n = input_count(t);
bool used[MAX_INPUTS] = {0};
const BlockSpec* sp = spec_of(t->type_name);
const BlockSpec* sp = spec_of_type(t);
if (t->type_name && strcmp(t->type_name, "Port.Define") == 0) {
/* "defina [prototipo]": o nome (campo 0) nao e editavel aqui */
add_part(b, -1, "defina", 6);
@@ -3927,7 +3940,7 @@ static void render_toasts(Ed* ed, TinyBlocks_RenderableBlock* rb) {
rb_line(rb, cx + 4, cy - 4, cx - 4, cy + 4, 1.6f, xh ? txt : dim);
/* temporizador: barra que encolhe (para com o mouse em cima) */
int an = ax_push(ed, TB_A11Y_STATUS, t->text, t->x, t->y, w, h);
tb_a11y_set_description(ax(ed), an, t->ok ? "aviso" : "erro");
if (!t->ok) tb_a11y_set_description(ax(ed), an, "erro"); /* o papel ja diz "aviso" */
if (t->count > 1) {
char cnt[32];
snprintf(cnt, sizeof(cnt), "%d vezes", t->count);
+9
View File
@@ -47,6 +47,8 @@ static TinyBlocks_Block* type_clone(const TinyBlocks_Block* self, TinyBlocks* rt
t->flow = src->flow;
t->event = src->event;
t->lazy_inputs = src->lazy_inputs;
t->label = src->label;
t->label_defaults = src->label_defaults;
for (uint32_t i = 0; i < src->input_desc_count; i++)
tb_type_block_set_input_flags(t, i, tb_type_block_input_flags(src, i));
for (uint32_t i = 0; i < src->output_desc_count; i++)
@@ -65,6 +67,13 @@ void tb_type_block_set_event(TinyBlocks_TypeBlock* tb, const char* event)
if (event) tb->shape = TB_SHAPE_HAT;
}
void tb_type_block_set_label(TinyBlocks_TypeBlock* tb, const char* label, const char* defaults)
{
if (!tb) return;
tb->label = label;
tb->label_defaults = defaults;
}
void tb_type_block_set_input_flags(TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags)
{ if (tb && tb->input_flags && i < tb->input_desc_count) tb->input_flags[i] = flags; }
void tb_type_block_set_output_flags(TinyBlocks_TypeBlock* tb, uint32_t i, uint32_t flags)