关于 arkts-sta-playground
通过 ArkTS-Sta Playground 的 HTTP API 运行 ArkTS-Sta 代码的技能,适合语法学习、片段调试和原型验证。支持文件或直接输入代码,并可输出 JSON 结果。
主要功能
- 01运行 .ets 文件或直接传入代码
- 02调用 Playground HTTP API 编译并运行
- 03返回输出与错误信息
- 04支持 JSON 格式结果
- 05可设置请求超时
适用场景
- 01测试 ArkTS 语法和语言特性
- 02集成前验证代码逻辑
- 03定位代码片段的编译错误
- 04批量测试多个 .ets 文件
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ openharmonyinsight/ openharmony-skills --skill arkts-sta-playground安装后在 Claude Code 中输入/arkts-sta-playground确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/test_simple.etsscripts/requirements.txtrun_playground.py详细介绍
ArkTS-Sta Playground 运行器
概述
此技能通过 ArkTS-Sta Playground HTTP API 运行 ArkTS-Sta 代码,无需浏览器自动化,即可快速、可靠地执行代码。
快速入门
基本用法
运行 ArkTS-Sta 文件:
python3 scripts/run_playground.py path/to/code.ets
直接以字符串形式运行代码:
python3 scripts/run_playground.py --code "let x: number = 42; console.log(x);"
获取 JSON 输出,以便进行程序化解析:
python3 scripts/run_playground.py --json --code "console.log('Hello');"
安装要求
安装 Python 依赖项:
pip install -r scripts/requirements.txt
所需软件包:requests>=2.31.0
用法模式
模式 1:快速测试代码
测试 ArkTS-Sta 语法或验证代码逻辑时:
python3 scripts/run_playground.py --code "
enum Numbers {
A = 10,
B = 2.57,
C = 0x2B7F,
D = -1.5,
E = 12
}
"
模式 2:批量测试
测试多个文件时:
for file in test/*.ets; do
python3 scripts/run_playground.py --json "$file" > "results/$(basename $file .ets).json"
done
工作原理
脚本使用以下 HTTP API 端点:
- 将你的 ArkTS-Sta 代码发送到
https://arkts-play.cn.bz-openlab.ru:10443/compile - 接收编译结果和输出
- 返回格式化结果(成功状态、输出、错误)
API 端点
基本 URL: https://arkts-play.cn.bz-openlab.ru:10443/compile
方法: POST
请求:
{
"code": "your ArkTS-Sta code here"
}
响应:
{
"output": "execution output or empty",
"error": "error message if compilation failed, null otherwise"
}
故障排除
连接问题
如果遇到连接错误:
- 检查互联网连接
- 确认 API 端点可访问
- 检查防火墙设置
# Test connectivity
curl -X POST https://arkts-play.cn.bz-openlab.ru:10443/compile \
-H "Content-Type: application/json" \
-d '{"code":"let x: number = 42;"}'
超时问题
如果 API 响应较慢,请延长超时时间:
python3 scripts/run_playground.py --timeout 60 path/to/code.ets
SSL 证书错误
如果遇到 SSL 证书问题,你可能需要:
- 确保系统的 CA 证书为最新版本
- 或修改脚本以禁用 SSL 验证(不建议在生产环境中这样做)
常见用例
学习 ArkTS 语法
测试和探索 ArkTS 语言特性:
python3 scripts/run_playground.py --code "
// Test union types
let value: string | number = 'hello';
value = 42;
console.log(value);
"
快速验证原型
在将代码集成到项目之前验证代码逻辑:
python3 scripts/run_playground.py --code "
function calculateSum(a: number, b: number): number {
return a + b;
}
console.log('Sum:', calculateSum(10, 20));
"
调试代码片段
查找代码中的编译错误:
# Run with JSON output for programmatic checking
python3 scripts/run_playground.py --json problem.ets
# Check exit status
if python3 scripts/run_playground.py code.ets; then
echo "Compilation successful"
else
echo "Compilation failed"
fi
脚本输出
使用 --json 标志时,脚本会返回以下 JSON 结构:
{
"success": true,
"output": "Execution result here...",
"error": null,
"has_error": false
}
字段:
success:表示 API 请求是否成功的布尔值output:代码输出或编译输出error:编译失败时的错误消息,否则为nullhas_error:表示代码是否存在编译错误的布尔值
示例:测试浮点枚举
测试非整数值的枚举是否会导致错误:
python3 scripts/run_playground.py --code "
enum Numbers {
A = 10,
B = 2.57,
C = 0x2B7F,
D = -1.5,
E = 12
}
"
预期结果:应因枚举值必须为整数而报错。
限制
- 需要能够访问 API 端点
- 可能会受到 API 速率限制
- 编译输出格式取决于 API 响应
高效使用技巧
- 使用 JSON 输出进行程序化处理(
--json) - 使用 Shell 循环批量测试多个文件
- 在脚本中检查退出代码以判断成功或失败
评论
支持 Markdown