Chuyển đến nội dung
Skillpedia
Lập trình chung7.8k · #13002

shell-python-heredoc

Skill này làm gì

Skill này chỉ cho AI agent cách chạy một script Python nhiều dòng qua shell khi sandbox liên tục lỗi. Nó dành cho những người có script cần nhiều lệnh import hoặc thông báo lỗi rõ ràng.

Khi nào nên dùng

  • ✓Khi sandbox mã đã lỗi từ hai lần trở lên với các thông báo lỗi chưa rõ.
  • ✓Khi một script có nhiều lệnh import, như reportlab hoặc pandas, và quá dài để viết trong một dòng.
  • ✓Khi bạn cần thấy stdout và stderr để biết điều gì đã sai.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/shell-python-heredoc/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/shell-python-heredoc/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Chạy script ReportLab nhiều dòng của tôi qua shell vì sandbox đã lỗi hai lần. Hãy cho tôi xem đầu ra.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent bọc script trong một heredoc với dấu kết thúc có nháy, rồi chạy bằng python3 với giới hạn thời gian. Sau đó nó đọc stdout và stderr để xem công việc có thành công không.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Shell Python Heredoc (Fallback Pattern)

Overview

When execute_code_sandbox fails repeatedly (e.g., with "unknown error"), use run_shell with a Python heredoc as a reliable fallback for executing multi-line Python scripts with imports.

When to Use

  • execute_code_sandbox has failed 2+ times with unexplained errors
  • You need to run Python code with multiple imports (e.g., ReportLab, pandas)
  • The script is too complex for a one-liner but doesn't require persistence between calls
  • You need better error visibility from raw stdout/stderr

How to Use

Basic Pattern

run_shell(
    command="""python3 << 'EOF'
import sys
# Your Python code here
print("Hello from heredoc")
EOF""",
    timeout=300
)

Key Syntax Rules

  1. Use << 'EOF' (with single quotes) to prevent shell variable expansion
  2. Indent Python code relative to the heredoc delimiter
  3. End with EOF on its own line (no leading whitespace)
  4. Capture output from stdout/stderr for debugging

Example: Generating a PDF with ReportLab

run_shell(
    command="""python3 << 'EOF'
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

c = canvas.Canvas("output.pdf", pagesize=letter)
c.drawString(100, 750, "Hello PDF")
c.save()
print("PDF created successfully")
EOF""",
    timeout=300
)

Example: Multi-file Data Processing

run_shell(
    command="""python3 << 'EOF'
import pandas as pd
import json

# Load and process data
df = pd.read_csv("data.csv")
summary = df.describe()

# Output results
print(summary.to_string())
with open("summary.json", "w") as f:
    json.dump(summary.to_dict(), f)
print("Processing complete")
EOF""",
    timeout=300
)

Advantages Over execute_code_sandbox

  • More reliable for complex imports (no sandbox isolation issues)
  • Full stdout/stderr access for debugging
  • Longer timeout flexibility (60s default vs 30s)
  • Direct filesystem access without ARTIFACT_PATH indirection

Disadvantages

  • No persistent state between calls (each heredoc is a fresh process)
  • Verbose for simple one-liners
  • Shell escaping may be needed for complex string literals

Troubleshooting

Issue Solution
EOF not recognized Ensure EOF is on its own line with no leading spaces
Import errors Verify packages are installed in the shell environment
File not found Use absolute paths or check working directory with pwd
Syntax errors Check Python indentation within heredoc

See Also

  • Use execute_code_sandbox for simpler scripts when available
  • Use shell_agent for tasks requiring autonomous iteration and error recovery
  • Consider writing scripts to .py files for complex, reusable code
Câu hỏi thường gặp

Tại sao dấu kết thúc phải nằm ở đầu dòng?

Shell chỉ nhận ra dấu kết thúc khi nó không có khoảng trắng ở đầu.

Tại sao dùng dấu kết thúc có nháy?

Dấu có nháy ngăn shell thay đổi biến hoặc ký hiệu bên trong mã Python.

Script có giữ trạng thái giữa các lần chạy không?

Skill nói cách này dành cho các công việc không cần giữ trạng thái giữa các lần gọi.

Chi tiết

Mô tả gốc của tác giả

Execute complex Python code via run_shell heredoc when execute_code_sandbox fails

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#13002
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự