Code Runner Skill
This skill enables you to run code snippets in multiple programming languages directly from the command line.
When to Use This Skill
Use this skill when:
- - The user wants to run or execute a code snippet
- Testing algorithm implementations or logic
- Verifying expected output of code
- Running quick scripts or one-liners
- Checking syntax or runtime behavior
- Demonstrating code functionality
Supported Languages
The following languages are supported (requires the interpreter/compiler to be installed):
| Language | Command | File Extension |
|---|
| JavaScript | INLINECODE0 | INLINECODE1 |
| TypeScript |
ts-node |
.ts |
| Python |
python |
.py |
| Java |
java (compile & run) |
.java |
| C |
gcc (compile & run) |
.c |
| C++ |
g++ (compile & run) |
.cpp |
| Go |
go run |
.go |
| Rust |
rustc (compile & run) |
.rs |
| Ruby |
ruby |
.rb |
| PHP |
php |
.php |
| Perl |
perl |
.pl |
| Lua |
lua |
.lua |
| R |
Rscript |
.r |
| Swift |
swift |
.swift |
| Kotlin |
kotlin |
.kts |
| Scala |
scala |
.scala |
| Groovy |
groovy |
.groovy |
| Dart |
dart |
.dart |
| Julia |
julia |
.jl |
| Haskell |
runhaskell |
.hs |
| Clojure |
clojure |
.clj |
| F# |
dotnet fsi |
.fsx |
| C# |
dotnet script |
.csx |
| PowerShell |
pwsh |
.ps1 |
| Bash |
bash |
.sh |
| Batch |
cmd /c |
.bat |
| CoffeeScript |
coffee |
.coffee |
| Crystal |
crystal |
.cr |
| Elixir |
elixir |
.exs |
| Nim |
nim compile --run |
.nim |
| OCaml |
ocaml |
.ml |
| Racket |
racket |
.rkt |
| Scheme |
scheme |
.scm |
| Lisp |
sbcl --script |
.lisp |
See references/LANGUAGES.md for detailed language configuration.
How to Run Code
Step 1: Identify the Language
Determine the programming language from:
- - User's explicit request (e.g., "run this Python code")
- File extension if provided
- Code syntax patterns
Step 2: Execute Using the Runner Script
⚠️ Important for AI Agents: Use stdin to avoid escaping issues with quotes, backslashes, and special characters.
Recommended Method (stdin):
CODEBLOCK0
Alternative Method (CLI argument - for simple code only):
CODEBLOCK1
Example - JavaScript:
CODEBLOCK2
Example - Python:
CODEBLOCK3
Example - Java (multi-line):
CODEBLOCK4
Example - Multi-line code from variable:
CODEBLOCK5
Step 3: Return Results
- - Show the output (stdout) to the user
- If there are errors (stderr), explain what went wrong
- Suggest fixes for common errors
Platform Notes
Windows
- - Use
cmd /c for batch scripts - PowerShell scripts require
pwsh or INLINECODE70 - Path separators use backslash INLINECODE71
macOS / Linux
- - Bash scripts work natively
- Swift available on macOS
- Use
#!/usr/bin/env shebang for portable scripts
Error Handling
Common issues and solutions:
- 1. Command not found: The language interpreter is not installed or not in PATH
- Suggest installing the required runtime
- Provide installation instructions
- 2. Syntax errors: Code has syntax issues
- Show the error message
- Point to the line number if available
- 3. Runtime errors: Code runs but fails during execution
- Display the stack trace
- Explain the error type
- 4. Timeout: Code takes too long (default: 30 seconds)
- Warn about infinite loops
- Suggest optimizations
Security Considerations
⚠️ Important: Running arbitrary code can be dangerous. Always:
- 1. Review the code before execution
- Be cautious with code that:
- Accesses the file system
- Makes network requests
- Executes system commands
- Modifies environment variables
- 3. Consider running in a sandboxed environment for untrusted code
Examples
Example 1: Run a JavaScript calculation
echo "console.log(Array.from({length: 10}, (_, i) => i * i))" | node scripts/run-code.cjs javascript
Output: INLINECODE73
Example 2: Run Python with imports
echo "import math; print(math.factorial(10))" | node scripts/run-code.cjs python
Output: INLINECODE74
Example 3: Test a Go function
echo 'package main; import "fmt"; func main() { fmt.Println("Hello from Go!") }' | node scripts/run-code.cjs go
Output: INLINECODE75
代码运行技能
该技能使您能够直接从命令行运行多种编程语言的代码片段。
何时使用此技能
在以下情况下使用此技能:
- - 用户想要运行或执行代码片段
- 测试算法实现或逻辑
- 验证代码的预期输出
- 运行快速脚本或单行代码
- 检查语法或运行时行为
- 演示代码功能
支持的语言
支持以下语言(需要安装相应的解释器/编译器):
| 语言 | 命令 | 文件扩展名 |
|---|
| JavaScript | node | .js |
| TypeScript |
ts-node | .ts |
| Python | python | .py |
| Java | java(编译并运行) | .java |
| C | gcc(编译并运行) | .c |
| C++ | g++(编译并运行) | .cpp |
| Go | go run | .go |
| Rust | rustc(编译并运行) | .rs |
| Ruby | ruby | .rb |
| PHP | php | .php |
| Perl | perl | .pl |
| Lua | lua | .lua |
| R | Rscript | .r |
| Swift | swift | .swift |
| Kotlin | kotlin | .kts |
| Scala | scala | .scala |
| Groovy | groovy | .groovy |
| Dart | dart | .dart |
| Julia | julia | .jl |
| Haskell | runhaskell | .hs |
| Clojure | clojure | .clj |
| F# | dotnet fsi | .fsx |
| C# | dotnet script | .csx |
| PowerShell | pwsh | .ps1 |
| Bash | bash | .sh |
| Batch | cmd /c | .bat |
| CoffeeScript | coffee | .coffee |
| Crystal | crystal | .cr |
| Elixir | elixir | .exs |
| Nim | nim compile --run | .nim |
| OCaml | ocaml | .ml |
| Racket | racket | .rkt |
| Scheme | scheme | .scm |
| Lisp | sbcl --script | .lisp |
详细语言配置请参见 references/LANGUAGES.md。
如何运行代码
步骤1:识别语言
通过以下方式确定编程语言:
- - 用户的明确请求(例如运行这段Python代码)
- 提供的文件扩展名
- 代码语法模式
步骤2:使用运行脚本执行
⚠️ 对AI代理重要提示:使用标准输入以避免引号、反斜杠和特殊字符的转义问题。
推荐方法(标准输入):
bash
echo <代码> | node scripts/run-code.cjs <语言ID>
替代方法(CLI参数 - 仅适用于简单代码):
bash
node scripts/run-code.cjs <语言ID> <代码>
示例 - JavaScript:
bash
echo console.log(Hello, World!) | node scripts/run-code.cjs javascript
示例 - Python:
bash
echo print(Hello, World!) | node scripts/run-code.cjs python
示例 - Java(多行):
bash
echo public class Test {
public static void main(String[] args) {
System.out.println(\Hello from Java!\);
}
} | node scripts/run-code.cjs java
示例 - 从变量传入多行代码:
bash
在bash中
CODE=import math
print(Pi:, math.pi)
print(Result:, math.factorial(5))
echo $CODE | node scripts/run-code.cjs python
在PowerShell中(内联here-string)
@
import math
print(Pi:, math.pi)
print(Result:, math.factorial(5))
@ | node scripts/run-code.cjs python
步骤3:返回结果
- - 向用户显示输出(标准输出)
- 如果有错误(标准错误),解释出错原因
- 针对常见错误建议修复方法
平台说明
Windows
- - 批处理脚本使用 cmd /c
- PowerShell脚本需要 pwsh 或 powershell
- 路径分隔符使用反斜杠 \
macOS / Linux
- - Bash脚本原生支持
- Swift在macOS上可用
- 可移植脚本使用 #!/usr/bin/env shebang
错误处理
常见问题及解决方案:
- 1. 命令未找到:语言解释器未安装或不在PATH中
- 建议安装所需的运行时
- 提供安装说明
- 2. 语法错误:代码存在语法问题
- 显示错误信息
- 如果可用,指向行号
- 3. 运行时错误:代码运行但执行失败
- 显示堆栈跟踪
- 解释错误类型
- 4. 超时:代码执行时间过长(默认:30秒)
- 警告可能存在无限循环
- 建议优化
安全考虑
⚠️ 重要提示:运行任意代码可能存在危险。始终:
- 1. 在执行前审查代码
- 对以下类型的代码保持警惕:
- 访问文件系统
- 发起网络请求
- 执行系统命令
- 修改环境变量
- 3. 对于不可信代码,考虑在沙盒环境中运行
示例
示例1:运行JavaScript计算
bash
echo console.log(Array.from({length: 10}, (_, i) => i * i)) | node scripts/run-code.cjs javascript
输出:[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
示例2:运行带导入的Python代码
bash
echo import math; print(math.factorial(10)) | node scripts/run-code.cjs python
输出:3628800
示例3:测试Go函数
bash
echo package main; import fmt; func main() { fmt.Println(Hello from Go!) } | node scripts/run-code.cjs go
输出:Hello from Go!