Skip to content

Commit da7d55a

Browse files
committed
fix(applet): name the cause when a view is rejected, document table cell types
1 parent 0c5968d commit da7d55a

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎crates/jcode-app-core/src/tool/applet.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,12 @@ fn resolve_placement(raw: Option<&Value>, ctx: &ToolContext) -> Result<Placement
184184
}
185185

186186
fn parse_view(view: Value) -> Result<View> {
187-
serde_json::from_value(view).context("invalid view (see the node list in the tool description)")
187+
// Tool errors are shown with `{}`, which drops `.context` causes, so the
188+
// serde reason goes in the message itself. Without it the agent can only
189+
// guess which node or field is wrong.
190+
serde_json::from_value(view).map_err(|error| {
191+
anyhow!("invalid view: {error} (see the node list in the tool description)")
192+
})
188193
}
189194

190195
#[async_trait]
@@ -196,7 +201,7 @@ impl Tool for AppletTool {
196201
fn description(&self) -> &str {
197202
"Show interactive native UI (an applet) in Jcode Desktop. By default the card replaces this tool call in the transcript. \
198203
View is a tree of nodes, each {\"type\":...}. Layout: stack{direction:vertical|horizontal,gap,padding,align,children}, grid{min_column_width,children}, scroll{max_height,children}, card{title,children} (don't nest), tabs{bind,tabs:[{id,label,children}]}, spacer, divider. \
199-
Content: text{text,style:body|title|heading|caption|mono,tone,max_lines}, markdown{text}, code{text,language}, image{source:{url|data|asset},alt,aspect_ratio}, icon{name}, key_value{rows:[{key,value}]}, table{columns,rows}, progress{value 0-1,label}, empty{title,detail}, error{message,retry}. \
204+
Content: text{text,style:body|title|heading|caption|mono,tone,max_lines}, markdown{text}, code{text,language}, image{source:{url|data|asset},alt,aspect_ratio}, icon{name}, key_value{rows:[{key,value}]}, table{columns:[string],rows:[[string]]}, progress{value 0-1,label}, empty{title,detail}, error{message,retry}. \
200205
Controls (always pills): button{label,variant:primary|secondary|compact|danger,on_press}, chip{label,on_press}, toggle{label,bind}, input{bind,placeholder,multiline,on_submit}, select{bind,options:[{value,label}]}, list{children:[list_item{title,subtitle,meta,badges,on_press}]}. Escape hatch: html{source,height}. \
201206
Spacing tokens none|xs|sm|md|lg|xl; tones default|dim|accent|success|warning|danger. Inputs/toggles/selects/tabs bind to keys in `state`. \
202207
Actions are {action,args}. Custom names come back to you as an `[applet action]` message with the current state (or as this tool's output with wait=true). \

‎crates/jcode-app-core/src/tool/applet_tests.rs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ fn documented_shapes_validate() {
155155
let description = AppletTool::new().description().to_string();
156156
assert!(description.contains("select{bind,options:[{value,label}]}"));
157157
assert!(description.contains("host.send_prompt{prompt}"));
158+
assert!(description.contains("table{columns:[string],rows:[[string]]}"));
158159
let view = json!({"type":"stack","children":[
159160
{"type":"select","bind":"env","options":[{"value":"a","label":"A"}]},
160161
{"type":"tabs","bind":"t","tabs":[{"id":"x","label":"X","children":[]}]},
@@ -164,7 +165,8 @@ fn documented_shapes_validate() {
164165
{"type":"input","bind":"q","placeholder":"p","multiline":true},
165166
{"type":"list","children":[{"type":"list_item","title":"a","subtitle":"b","meta":"c","badges":["d"],"on_press":{"action":"open"}}]},
166167
{"type":"progress","value":0.5,"label":"half"},
167-
{"type":"text","text":"t","style":"caption","tone":"dim","max_lines":2}
168+
{"type":"text","text":"t","style":"caption","tone":"dim","max_lines":2},
169+
{"type":"table","columns":["Name","Email"],"rows":[["Ada","[email protected]"]]}
168170
]});
169171
let document: jcode_applet_types::Document =
170172
serde_json::from_value(json!({"revision":1,"title":"T","state":{"env":"a"},"view":view}))
@@ -176,3 +178,25 @@ fn documented_shapes_validate() {
176178
)
177179
.expect("documented shapes validate");
178180
}
181+
182+
/// A rejected view must say why. Tool errors are rendered with `{}`, so a
183+
/// cause attached via `.context` never reaches the agent, which then retries
184+
/// blind with variations of the same mistake.
185+
#[tokio::test]
186+
async fn invalid_view_error_names_the_cause() {
187+
let _guard = crate::storage::lock_test_env();
188+
let _home = temp_home();
189+
let error = AppletTool::new()
190+
.execute(
191+
json!({"title":"T","view":{"type":"table",
192+
"columns":[{"key":"name","label":"Name"}],
193+
"rows":[{"name":"Ada"}]}}),
194+
context("applet-invalid-view", "call-1"),
195+
)
196+
.await
197+
.expect_err("object-shaped table columns are rejected");
198+
let shown = format!("{error}");
199+
assert!(shown.starts_with("invalid view: "), "{shown}");
200+
assert!(shown.contains("invalid `table` node"), "{shown}");
201+
assert!(shown.contains("expected a string"), "{shown}");
202+
}

0 commit comments

Comments
 (0)