Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Fix Pi TypeBox schema generation

Sakil Ansari88269ac2ee70
diff --git a/src/cli/client_adapter.c b/src/cli/client_adapter.c
index 356f89584..3f6139794 100644
--- a/src/cli/client_adapter.c
+++ b/src/cli/client_adapter.c
@@ -139,6 +139,102 @@ static void emit_header(adapter_sb_t *sb, const char *client) {
"// are overwritten; edit outside it, or remove the markers to take ownership.\n");
}
+static void append_typebox_type(adapter_sb_t *sb, yyjson_val *val, const char *description,
+ bool include_description) {
+ yyjson_val *type_val = yyjson_obj_get(val, "type");
+ yyjson_val *enum_val = yyjson_obj_get(val, "enum");
+ yyjson_val *items_val = yyjson_obj_get(val, "items");
+ const char *type_str = yyjson_get_str(type_val);
+
+ if (type_str && strcmp(type_str, "string") == 0 && enum_val && yyjson_is_arr(enum_val) &&
+ yyjson_arr_size(enum_val) > 0) {
+ sb_append(sb, "Type.Union([");
+ size_t enum_idx = 0;
+ size_t enum_max = yyjson_arr_size(enum_val);
+ bool first_enum = true;
+ for (enum_idx = 0; enum_idx < enum_max; enum_idx++) {
+ yyjson_val *enum_item = yyjson_arr_get(enum_val, enum_idx);
+ const char *enum_str = yyjson_get_str(enum_item);
+ if (!enum_str) {
+ continue;
+ }
+ if (!first_enum) {
+ sb_append(sb, ", ");
+ }
+ first_enum = false;
+ sb_append(sb, "Type.Literal(");
+ sb_append_js_string(sb, enum_str);
+ sb_append(sb, ")");
+ }
+ if (first_enum) {
+ sb_append(sb, "Type.String()");
+ } else {
+ sb_append(sb, "]");
+ if (include_description && description) {
+ sb_append(sb, ", { description: ");
+ sb_append_js_string(sb, description);
+ sb_append(sb, " }");
+ }
+ sb_append(sb, ")");
+ return;
+ }
+ sb_append(sb, ")");
+ return;
+ }
+
+ const char *typebox_name = "Any";
+ if (type_str && strcmp(type_str, "string") == 0) {
+ typebox_name = "String";
+ } else if (type_str && strcmp(type_str, "integer") == 0) {
+ typebox_name = "Integer";
+ } else if (type_str && strcmp(type_str, "number") == 0) {
+ typebox_name = "Number";
+ } else if (type_str && strcmp(type_str, "boolean") == 0) {
+ typebox_name = "Boolean";
+ }
+
+ if (type_str && strcmp(type_str, "array") == 0) {
+ sb_append(sb, "Type.Array(");
+ if (items_val) {
+ append_typebox_type(sb, items_val, NULL, false);
+ } else {
+ sb_append(sb, "Type.Any()");
+ }
+ if (include_description && description) {
+ sb_append(sb, ", { description: ");
+ sb_append_js_string(sb, description);
+ sb_append(sb, " }");
+ }
+ sb_append(sb, ")");
+ return;
+ }
+
+ sb_append(sb, "Type.");
+ sb_append(sb, typebox_name);
+ sb_append(sb, "(");
+ if (include_description && description) {
+ sb_append(sb, "{ description: ");
+ sb_append_js_string(sb, description);
+ sb_append(sb, " }");
+ }
+ sb_append(sb, ")");
+}
+
+static bool typebox_property_is_required(yyjson_val *required, const char *prop_name) {
+ if (!required || !yyjson_is_arr(required)) {
+ return false;
+ }
+ size_t idx = 0;
+ size_t max = yyjson_arr_size(required);
+ for (idx = 0; idx < max; idx++) {
+ const char *required_name = yyjson_get_str(yyjson_arr_get(required, idx));
+ if (required_name && strcmp(required_name, prop_name) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
static void convert_json_schema_to_typebox(adapter_sb_t *sb, const char *schema_str) {
if (!schema_str) {
sb_append(sb, "Type.Object({})");
@@ -160,6 +256,7 @@ static void convert_json_schema_to_typebox(adapter_sb_t *sb, const char *schema_
}
yyjson_val *properties = yyjson_obj_get(root, "properties");
+ yyjson_val *required = yyjson_obj_get(root, "required");
if (!properties) {
sb_append(sb, "Type.Object({})");
yyjson_doc_free(doc);
@@ -178,9 +275,6 @@ static void convert_json_schema_to_typebox(adapter_sb_t *sb, const char *schema_
continue;
}
- /* Suppress unused parameter warning */
- (void)val;
-
if (!first_prop) {
sb_append(sb, ",\n");
}
@@ -188,7 +282,18 @@ static void convert_json_schema_to_typebox(adapter_sb_t *sb, const char *schema_
sb_append(sb, " ");
sb_append(sb, prop_name);
- sb_append(sb, ": Type.String()");
+ sb_append(sb, ": ");
+
+ yyjson_val *description_val = yyjson_obj_get(val, "description");
+ const char *description = yyjson_get_str(description_val);
+ bool is_required = typebox_property_is_required(required, prop_name);
+ if (!is_required) {
+ sb_append(sb, "Type.Optional(");
+ }
+ append_typebox_type(sb, val, description, true);
+ if (!is_required) {
+ sb_append(sb, ")");
+ }
}
sb_append(sb, "\n })");
diff --git a/tests/test_agent_clients.c b/tests/test_agent_clients.c
index ca5123600..ca6d8b676 100644
--- a/tests/test_agent_clients.c
+++ b/tests/test_agent_clients.c
@@ -1133,6 +1133,8 @@ TEST(client_adapter_pi_emits_parameters_and_execute) {
/* TypeBox parameters schema is embedded with Type.Object syntax. */
ASSERT_NOT_NULL(strstr(js, "Type.Object("));
ASSERT_NOT_NULL(strstr(js, "Type.String("));
+ /* A numeric registry property must remain numeric in the generated schema. */
+ ASSERT_NOT_NULL(strstr(js, "depth: Type.Integer("));
/* TypeBox import should be present. */
ASSERT_NOT_NULL(strstr(js, "import { Type } from 'typebox';"));
/* Raw JSON output is required so the bridge can parse the MCP result; the