Skip to content

Commit a81d171

Browse files
authored
Improve handler shape validation guidance (#101)
* Improve handler shape validation guidance Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com> * Address PR review: improve handler validation guards and guidance consistency - Gate return-check on has_handler_function && no signature issue to avoid misleading 'MUST return' errors on invalid handler shapes - Expand check_handler_has_return skip logic to also skip arrow function bodies (=> { ... }) and generator declarations (function*) - Fix conflicting guidance in register_handler tool description: use function handler(...) instead of function handler(event) in REQUIRED line - Add zero-parameter form function handler() to system-message signature list Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com> --------- Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent 062d140 commit a81d171

5 files changed

Lines changed: 443 additions & 78 deletions

File tree

src/agent/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,11 @@ const registerHandlerTool = defineTool("register_handler", {
12921292
"Register (or update) a named JavaScript handler in the sandbox.",
12931293
"The code is compiled but NOT executed — call execute_javascript to run it.",
12941294
"",
1295-
"REQUIRED: Code must define `function handler(event) { ... return result; }`",
1295+
"REQUIRED: Code must define `function handler(...) { ... return result; }`",
1296+
"Use `async function handler(...)` only when the handler contains await.",
12961297
"The function MUST be named exactly 'handler' — not Handler, handle, main.",
1298+
"The parameter name is flexible; use 'event' by convention, or omit it if unused.",
1299+
"Do NOT use arrow handlers such as `const handler = (event) => { ... }`.",
12971300
"",
12981301
"⚠️ TO UPDATE EXISTING CODE: Use get_handler_source first!",
12991302
"When fixing errors, call get_handler_source(name) to get current code,",
@@ -1332,8 +1335,9 @@ const registerHandlerTool = defineTool("register_handler", {
13321335
code: {
13331336
type: "string",
13341337
description:
1335-
"JavaScript source code. Simple mode: use `return` for output. " +
1336-
"Module mode: define `function handler(event) { ... }`.",
1338+
"JavaScript source code. Define `function handler(event) { ... return result; }`, " +
1339+
"`function handler() { ... return result; }` if no input is needed, " +
1340+
"or `async function handler(event) { ... return result; }` if using await.",
13371341
},
13381342
},
13391343
required: ["name", "code"],
@@ -2893,9 +2897,10 @@ const HELP_TOPICS: Record<string, string> = {
28932897
"- Handlers CANNOT call other handlers — they are isolated modules",
28942898
"- YOU orchestrate: pass handler A's result as handler B's event",
28952899
"",
2896-
"TWO CODE STYLES (auto-detected):",
2897-
"- Simple: no 'function handler' → wrapped as function body, locals reset each call",
2898-
"- Module: defines 'function handler(event)' → module-level state persists",
2900+
"HANDLER CODE STYLE:",
2901+
"- Every handler must define function handler(event) or async function handler(event)",
2902+
"- The function name is mandatory; parameter names are flexible, and zero parameters are allowed",
2903+
"- Module-level state persists across execute_javascript calls",
28992904
"",
29002905
"COMMON MISTAKES:",
29012906
"- Function must be named exactly 'handler' (not Handler, handle, main)",
@@ -2949,7 +2954,7 @@ const HELP_TOPICS: Record<string, string> = {
29492954
debugging: [
29502955
"DEBUGGING TIPS:",
29512956
"- 'not a function' = you guessed a method name. Call module_info/plugin_info to verify.",
2952-
"- register_handler returns codeSize and mode (module/simple) — check these",
2957+
"- register_handler returns codeSize and mode — check these",
29532958
"- If handler errors, try a minimal version first",
29542959
"- Build up complexity gradually — add one feature at a time",
29552960
"- Use try/catch inside handlers to catch runtime errors cleanly",

src/agent/system-message.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@ EVERYTHING goes through sandbox tools — register_handler, execute_javascript,
4242
║ MANDATORY HANDLER FORMAT — YOUR CODE WILL BE REJECTED WITHOUT THIS ║
4343
╚══════════════════════════════════════════════════════════════════════╝
4444
45-
Every register_handler call MUST define: function handler(event) { ... return result; }
45+
Every register_handler call MUST define a real handler function with one of these signatures:
46+
function handler(event) { ... return result; }
47+
function handler() { ... return result; } // when no input is needed
48+
async function handler(event) { ... return result; } // only when using await
49+
4650
The function MUST be named exactly "handler". Not Handler, handle, main, run, process.
47-
Code without "function handler" is ALWAYS rejected by the validator.
51+
The parameter name is flexible; examples use "event" because execute_javascript passes JSON event data.
52+
Handlers that do not need input may omit the parameter: function handler() { ... }.
53+
Code without "function handler" or "async function handler" is ALWAYS rejected by the validator.
4854
4955
TEMPLATE — copy this structure every time:
5056
import * as pptx from "ha:pptx"; // imports at top
@@ -54,7 +60,10 @@ TEMPLATE — copy this structure every time:
5460
}
5561
5662
RULES:
57-
- function handler(event) — EXACTLY this signature, no exceptions
63+
- Use function handler(event) for normal code
64+
- Use async function handler(event) only if the handler contains await
65+
- Do NOT use arrow handlers: const handler = (event) => { ... } is rejected
66+
- Parameter names are flexible: event, input, data, args, or no parameter are all allowed
5867
- MUST return a value — handler without return = runtime error
5968
- event is JSON in, return value is JSON out
6069
- One-shot: runs once, returns, done

0 commit comments

Comments
 (0)