mirror of
https://github.com/m5stack/ESP-Claw.git
synced 2026-05-20 11:51:49 -07:00
3.3 KiB
3.3 KiB
Lua Script Editing
Use this skill when the user wants to create, modify, or organize Lua scripts. The goal is to use cap_lua script storage correctly instead of routing through console commands.
Core Constraints
- Use
lua_write_scriptfor writes. - Prefer reusing or adapting an existing script. Create a new script only when existing scripts cannot satisfy the requirement.
pathmust be a relative.luapath such asbuiltin/foo.lua,temp/foo.lua, oruser/bar.lua.- Do not use absolute paths, do not include
.., and do not write non-.luafiles. lua_write_scriptcreates parent directories automatically.overwritedefaults totrue. Only passfalsewhen the user explicitly wants create-only behavior.- Scripts still being validated should stay under
temp/. Move them touser/only after they are confirmed. - Built-in scripts under
builtin/should usually be treated as references. Prefer adapting them intotemp/oruser/instead of editing the built-in path in place. - Do not assume extra Lua modules exist. Only
requiremodules explicitly documented by the activelua_module_*skills.
Recommended Flow
- Use
cap_lua_listand calllua_list_scriptsfirst to see whether a close script already exists, especially underbuiltin/. - Reuse or adapt the closest existing script whenever possible. Create a new script only when existing scripts do not meet the requirement.
- If you want to reuse an existing script, read its source with
read_file. When the source comes frombuiltin/, usually rewrite it intotemp/oruser/instead of editing the built-in path. - Write new scripts under
temp/*.luafirst, then move them touser/*.luaafter confirmation. - Reuse the same path during iteration instead of creating
foo2.lua,foo3.lua, and so on.
lua_write_script
Purpose: write or overwrite a Lua script.
Required parameters:
pathcontent
Optional parameters:
overwrite
Implementation behavior:
- The script size limit is 16 KiB.
- On success, the tool returns
OK: wrote Lua script <path> (<bytes> bytes). - Common failure reasons include invalid path, missing content, existing script with
overwrite=false, storage preparation failure, or file write failure.
Runtime-Facing Rules While Authoring
- The runtime exposes tool
argsto Lua as the globalargs. argsmust be an object or array. Do not construct other JSON types.- When execution is triggered from an IM session, the firmware may inject
channel,chat_id, andsession_idintoargsif they are absent. - JSON integers are preserved as Lua integers when possible, but GPIOs, coordinates, and counters should still be made explicitly integral.
print(...)output is captured by the execution capability, so preferprintfor diagnostics.
Quality Rules To Keep
- Reuse before creating. If an existing script is close, modify it instead of starting from scratch.
- Never busy-wait for timing. Use
delay.delay_ms(ms)inside loops. - For hardware resources, open them in
run(), release them incleanup(), and wrap execution inxpcall(run, debug.traceback). - When an API expects integers, use
math.floor(...)or another explicit conversion. - If the script needs to send IM replies, prefer call the corresponding IM capability directly.