perf: optimize Lua scripting runtime and lightweight chat access#1459
perf: optimize Lua scripting runtime and lightweight chat access#1459SameDesu123 wants to merge 4 commits into
Conversation
cubicj
left a comment
There was a problem hiding this comment.
One compatibility concern: returning true or false from the Lua-facing setChatVar() / setState() is useful, but it changes the behavior of existing setter APIs. Existing scripts may call these setters in return position assuming they return nothing, and that can now leak a boolean into the trigger result.
Could you check the compatibility impact here?
|
@cubicj Thanks for catching this. I pushed a follow-up commit that keeps the compatibility behavior for the existing Lua-facing setters: For scripts that explicitly need to know whether a write changed state, I added opt-in APIs: Validated with:
|
cubicj
left a comment
There was a problem hiding this comment.
Verified the follow-up. Thanks for taking the compatibility feedback. Existing Lua-facing setChatVar() / setState() stay no-return, and changed/skipped is now opt-in through the new *Changed APIs.
Check, tests, and build pass locally. GitHub checks pass.
Approving — thanks for the update!
PR Checklist
Summary
Optimize Lua scripting runtime behavior by reducing unnecessary engine re-creation, skipping redundant chat variable writes, and adding lightweight chat access APIs for scripts.
This should help Lua-heavy characters and modules avoid avoidable rendering and scripting overhead, especially when multiple Lua triggers or display-related scripts are used.
Related Issues
None.
Changes
Added per-script identity keys for Lua trigger runtime caches.
code !== state.codepath closes and recreates the runtime instead of leaving hash-keyed engines behind.engineKeyoption is optional.Added module trigger runtime metadata.
getModuleTriggerRuntimes()preserves each trigger's source module id and index for stable runtime keys.getModuleTriggers()remains available for existing callers.Optimized
setChatVar.setChatVarnow skips writes when the stored value is already identical.truewhen a value changed andfalsewhen the write was skipped.setState.Added lightweight Lua chat access APIs.
getChatData(id, index)returns only the message text.getChatRole(id, index)returns only the message role.getRecentChats(id, count)returns only the latest N messages.getFullChat(id)for scripts that only need a small part of the chat.Impact
This is intended to be backward compatible.
Existing Lua APIs such as
getChat,getFullChat,setState, and button triggers remain available. The new chat access APIs are additive, and thesetChatVarbehavior only skips redundant writes where the final stored value would be unchanged.The main expected impact is better performance for Lua-heavy workflows, especially display/status-panel scripts and setups with multiple Lua triggers in the same mode.
No model request or response behavior is changed.
Additional Notes
Tested in dev server.
Footnotes
Modifies the behavior of prompting, requesting, or handling responses from AI models. ↩
Over 80% of the code is AI generated. ↩