feat: update lua_module_call_capability

This commit is contained in:
loop
2026-04-23 18:37:55 +08:00
committed by Xiangao Lu
parent 8534fce616
commit f426888f01
4 changed files with 82 additions and 221 deletions
@@ -0,0 +1,82 @@
-- Generic capability.call demo.
-- Supported args:
-- cap_name: target capability id/name
-- payload: optional Lua table payload
-- payload_json: optional JSON string payload, used when payload is not a table
-- session_id: optional call context session id
-- channel: optional call context channel
-- chat_id: optional call context chat id
-- source_cap: optional call context source cap, defaults to "lua_builtin_demo"
local capability = require("capability")
local a = type(args) == "table" and args or {}
local function string_arg(key, default)
local value = a[key]
if type(value) == "string" and value ~= "" then
return value
end
return default
end
local function build_call_opts()
local opts = {
source_cap = string_arg("source_cap", "lua_builtin_demo"),
}
local session_id = string_arg("session_id", nil)
local channel = string_arg("channel", nil)
local chat_id = string_arg("chat_id", nil)
if session_id then
opts.session_id = session_id
end
if channel then
opts.channel = channel
end
if chat_id then
opts.chat_id = chat_id
end
return opts
end
local function build_payload()
if type(a.payload) == "table" then
return a.payload
end
local payload_json = string_arg("payload_json", nil)
if payload_json then
return payload_json
end
return nil
end
local function run()
local cap_name = string_arg("cap_name", nil)
local payload = build_payload()
local ok, out, err
if not cap_name then
error("[basic_capability_call] args.cap_name is required")
end
print(string.format("[basic_capability_call] calling capability: %s", cap_name))
ok, out, err = capability.call(cap_name, payload, build_call_opts())
print(string.format(
"[basic_capability_call] result cap=%s ok=%s out=%s err=%s",
tostring(cap_name), tostring(ok), tostring(out), tostring(err)))
if not ok then
error(string.format("%s failed: %s", cap_name, tostring(err or out)))
end
print("[basic_capability_call] done")
end
local ok, err = xpcall(run, debug.traceback)
if not ok then
error(err)
end
@@ -1,91 +0,0 @@
-- QQ send-file demo.
-- Optional args:
-- chat_id: target QQ chat, e.g. "c2c:<openid>" or "group:<group_openid>"
-- path: local file path for qq_send_file; if omitted a demo file is created
-- caption: optional caption for qq_send_file
-- If chat_id is omitted, capability.call() may inherit it from the current QQ runtime context.
local capability = require("capability")
local storage = require("storage")
local a = type(args) == "table" and args or {}
local function string_arg(key, default)
local value = a[key]
if type(value) == "string" and value ~= "" then
return value
end
return default
end
local function build_call_opts()
local opts = {
channel = "qq",
source_cap = "lua_builtin_demo",
}
if type(a.session_id) == "string" and a.session_id ~= "" then
opts.session_id = a.session_id
end
if type(a.chat_id) == "string" and a.chat_id ~= "" then
opts.chat_id = a.chat_id
end
return opts
end
local function ensure_demo_file()
local root = storage.get_root_dir()
local demo_dir = storage.join_path(root, "demo")
local demo_path = storage.join_path(demo_dir, "qq_cap_send_file_demo.txt")
local chat_id = string_arg("chat_id", "<inherit-from-context>")
local caption = string_arg("caption", "file from lua qq capability demo")
local lines = {
"ESP-Claw QQ send-file demo file",
"chat_id=" .. chat_id,
"caption=" .. caption,
"This file is generated by lua_module_call_capability/lua_scripts/qq_cap_send_file.lua",
}
storage.mkdir(demo_dir)
storage.write_file(demo_path, table.concat(lines, "\n") .. "\n")
return demo_path
end
local function run()
local chat_id = string_arg("chat_id", nil)
local file_path = string_arg("path", nil)
local payload = {
caption = string_arg("caption", "file from lua qq capability demo"),
}
local ok, out, err
if not file_path then
file_path = ensure_demo_file()
print("[qq_cap_send_file] no args.path provided, generated demo file: " .. file_path)
end
if not storage.exists(file_path) then
error("[qq_cap_send_file] file does not exist: " .. tostring(file_path))
end
payload.path = file_path
if chat_id then
payload.chat_id = chat_id
end
print("[qq_cap_send_file] sending QQ file...")
ok, out, err = capability.call("qq_send_file", payload, build_call_opts())
print(string.format(
"[qq_cap_send_file] result ok=%s out=%s err=%s",
tostring(ok), tostring(out), tostring(err)))
if not ok then
error(string.format("qq_send_file failed: %s", tostring(err or out)))
end
print("[qq_cap_send_file] done")
end
local ok, err = xpcall(run, debug.traceback)
if not ok then
error(err)
end
@@ -1,69 +0,0 @@
-- QQ send-image demo.
-- Optional args:
-- chat_id: target QQ chat, e.g. "c2c:<openid>" or "group:<group_openid>"
-- path: local image path for qq_send_image
-- caption: optional caption for qq_send_image
-- If chat_id is omitted, capability.call() may inherit it from the current QQ runtime context.
local capability = require("capability")
local storage = require("storage")
local a = type(args) == "table" and args or {}
local function string_arg(key, default)
local value = a[key]
if type(value) == "string" and value ~= "" then
return value
end
return default
end
local function build_call_opts()
local opts = {
channel = "qq",
source_cap = "lua_builtin_demo",
}
if type(a.session_id) == "string" and a.session_id ~= "" then
opts.session_id = a.session_id
end
if type(a.chat_id) == "string" and a.chat_id ~= "" then
opts.chat_id = a.chat_id
end
return opts
end
local function run()
local chat_id = string_arg("chat_id", nil)
local image_path = string_arg("path", "/fatfs/static/ESP-Claw.png")
local payload = {
caption = string_arg("caption", "image from lua qq capability demo"),
}
local ok, out, err
if not storage.exists(image_path) then
error("[qq_cap_send_image] file does not exist: " .. tostring(image_path))
end
payload.path = image_path
if chat_id then
payload.chat_id = chat_id
end
print("[qq_cap_send_image] sending QQ image...")
ok, out, err = capability.call("qq_send_image", payload, build_call_opts())
print(string.format(
"[qq_cap_send_image] result ok=%s out=%s err=%s",
tostring(ok), tostring(out), tostring(err)))
if not ok then
error(string.format("qq_send_image failed: %s", tostring(err or out)))
end
print("[qq_cap_send_image] done")
end
local ok, err = xpcall(run, debug.traceback)
if not ok then
error(err)
end
@@ -1,61 +0,0 @@
-- QQ send-message demo.
-- Optional args:
-- chat_id: target QQ chat, e.g. "c2c:<openid>" or "group:<group_openid>"
-- message: text to send with qq_send_message
-- If chat_id is omitted, capability.call() may inherit it from the current QQ runtime context.
local capability = require("capability")
local a = type(args) == "table" and args or {}
local function string_arg(key, default)
local value = a[key]
if type(value) == "string" and value ~= "" then
return value
end
return default
end
local function build_call_opts()
local opts = {
channel = "qq",
source_cap = "lua_builtin_demo",
}
if type(a.session_id) == "string" and a.session_id ~= "" then
opts.session_id = a.session_id
end
if type(a.chat_id) == "string" and a.chat_id ~= "" then
opts.chat_id = a.chat_id
end
return opts
end
local function run()
local chat_id = string_arg("chat_id", nil)
local payload = {
message = string_arg("message", "hello from lua qq capability demo"),
}
local ok, out, err
if chat_id then
payload.chat_id = chat_id
end
print("[qq_cap_send_message] sending QQ message...")
ok, out, err = capability.call("qq_send_message", payload, build_call_opts())
print(string.format(
"[qq_cap_send_message] result ok=%s out=%s err=%s",
tostring(ok), tostring(out), tostring(err)))
if not ok then
error(string.format("qq_send_message failed: %s", tostring(err or out)))
end
print("[qq_cap_send_message] done")
end
local ok, err = xpcall(run, debug.traceback)
if not ok then
error(err)
end