mirror of
https://github.com/m5stack/ESP-Claw.git
synced 2026-05-20 11:51:49 -07:00
5.5 KiB
5.5 KiB
Lua Script Execution
Use this skill when the user wants to inspect existing Lua scripts, run a script, inspect async jobs, or stop async jobs.
Core Constraints
- Call the direct
cap_luacapability entrypoints. pathmust be a relative.luapath.- If the user asks to stop, cancel, close, or clear an async script, you must actually call
lua_stop_async_job,lua_stop_all_async_jobs, orlua_run_script_asyncwithreplace:truein the same turn. Do not claim the job is stopped based only on context. - Do not deactivate this skill while async Lua jobs are still running, or you lose the stop/list entrypoints needed to manage them.
Before Execution
- Use
cap_lua_listand calllua_list_scriptsfirst to confirm the target script path. - Prefer an existing script when it already matches or is close to the requested behavior. Only create a new script when reuse is insufficient.
- If you need the source first, follow the
cap_lua_listrule and useread_file("scripts/<relative_path>"). - Expect shipped demos and built-in examples to appear under
builtin/.
lua_run_script
Use this for short, one-shot work when output is needed in the current turn.
Input:
path: required.args: optional, must be an object or array.timeout_ms: optional; when provided it must be a positive integer.
Implementation behavior:
- If
timeout_msis omitted or0, sync execution uses the default timeout of60000ms. - Lua reads the parameters from the global
args. - For IM-triggered runs, the firmware may auto-inject
channel,chat_id, andsession_idintoargswhen they are absent. print(...)output is captured.- When there is no output, the result is
Lua script completed with no output.. - When output is too long,
[output truncated]is appended at the end. - On execution failure, the error text is appended after any captured output. For the console wrapper, this often means only the last non-empty error line is shown.
lua_run_script_async
Use this for loops, animations, monitors, watchers, and other long-running tasks.
Input:
path: required.args: optional, must be an object or array.timeout_ms: optional, must be a non-negative integer.name: optional logical job name.exclusive: optional mutex/resource-group name such asdisplay.replace: optional boolean.
Implementation behavior:
timeout_ms=0means run until cancelled, which is also the default behavior.- If
nameis omitted, the runner uses the script basename as the job name. - The async runner verifies that the script file exists before queueing the job.
- The system can run at most 4 async Lua jobs concurrently and keeps 16 total job slots; old terminal jobs may be recycled.
name / exclusive / replace
These fields define async takeover behavior.
name: active jobs with the same name cannot coexist.exclusive: active jobs in the same exclusive group cannot coexist.- If
replaceis omitted orfalse, a conflict returns an error immediately. - If
replace=true, the runner first asks the conflicting job to stop and then submits the new job. - Stopping the conflicting job is cooperative and waits 2000 ms by default; if that wait times out, takeover fails.
Recommended practice:
- Always set
namefor long-running scripts. - Set
exclusivewhen the job owns a singleton resource such as display or audio. - Use
replace:trueonly when the user explicitly wants to switch over to the new job.
Async Job Inspection
Use:
lua_list_async_jobslua_get_async_job
Status values:
queuedrunningdonefailedtimeoutstopped
Details:
lua_list_async_jobsaccepts an optionalstatusfilter and supports all statuses above plusall.lua_get_async_jobcan query byjob_idorname.- Name lookup only matches active jobs. For terminal jobs, prefer
job_id. lua_get_async_jobreturnsjob_id,name,status,exclusive,runtime_s,path,args, andsummary.
Stopping Async Jobs
Use:
lua_stop_async_joblua_stop_all_async_jobs
Input:
lua_stop_async_job: passjob_idorname, with optionalwait_ms.lua_stop_all_async_jobs: optionalexclusivefilter and optionalwait_ms.
Implementation behavior:
- If
wait_msis omitted or0, the default wait is2000ms. - Stopping is cooperative; the script observes cancellation through the Lua hook.
- A successful single stop usually returns
OK: stopped job <id> (status=stopped). - If the target is already terminal, the tool returns
OK: job <id> already terminal .... - If the wait times out, the tool returns
WARN: stop requested ... but task did not exit within ..., which means stop was requested but the job may still be unwinding. lua_stop_all_async_jobsreturns aggregate counts for stopped jobs and jobs still unwinding.
Recommended Flow
- Use
cap_lua_listfirst to confirm the target script path. - Prefer running or adapting an existing script when one is already suitable. Create a new script only when reuse does not satisfy the request.
- Use
lua_run_scriptfor short work andlua_run_script_asyncfor long-running work. - For built-in examples, run them from
builtin/*.luawhen checking baseline behavior. For iterative changes, copy them to a stabletemp/*.luapath and keep stablename/exclusive. - Inspect state with
lua_list_async_jobsfirst, then uselua_get_async_jobwhen you need details. - When the user asks to stop a job, call the stop tool first and then answer based on the returned result.