Pi Agent: Non-Interactive Mode

آخر تحديث: 2026-08-31

--- title: "الوضع غير التفاعلي" description: "أتقن وضع Pi Agent غير التفاعلي (الدفعة) لأتمتة خطوط المهام وعمليات الأنابيب وتكامل السكريبتات." order: 6 lang: ar

الوضع التفاعلي للاستكشاف؛ الوضع غير التفاعلي للإنتاج — بدون إشراف، معالجة دفعات، تنسيق خطوط الأنابيب.


1. ما هو الوضع غير التفاعلي

الوضع غير التفاعلي يعني أن الوكيل يستقبل المدخلات وينفذ تلقائياً بدون تدخل بشري. مناسب لـ:


2. الاستخدام غير التفاعلي عبر CLI

(1) تنفيذ فردي

BASH
pi-agent run "Explain what this code does: def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)"

(2) قراءة الإدخال من ملف

BASH
pi-agent run --input code.py "Review the code quality of this file"

(3) أنابيب من الإدخال القياسي

BASH
cat error.log | pi-agent run "Analyze these error logs and find the most common issues"

(4) الإخراج إلى ملف

BASH
pi-agent run --input data.csv "Analyze data trends" --output report.md

3. المعالجة الدفعية عبر Python

(1) دفعة أساسية

PYTHON
from pi_agent import Agent

agent = Agent(name="reviewer", system_prompt="You are a code review expert")

files = ["main.py", "utils.py", "config.py"]

for f in files:
    result = agent.run(f"Review code quality of {f}, give scores and improvement suggestions")
    print(f"=== {f} ===")
    print(result)
    print()

(2) دفعة متوازية

PYTHON
import asyncio
from pi_agent import AsyncAgent

async def review_file(filename):
    agent = AsyncAgent(name="reviewer")
    result = await agent.run(f"Review {filename}")
    return filename, result

async def main():
    files = ["main.py", "utils.py", "config.py", "tests.py"]
    tasks = [review_file(f) for f in files]
    results = await asyncio.gather(*tasks)

    for filename, result in results:
        print(f"=== {filename} ===")
        print(result)

asyncio.run(main())

4. الأنابيب وتكامل السكريبتات

مثال 1: مُولّد رسائل Git commit (الصعوبة: ⭐⭐)

BASH
git diff --staged | pi-agent run "Generate a concise git commit message based on the code changes"

مثال 2: خط تحليل السجلات (الصعوبة: ⭐⭐)

BASH
tail -100 /var/log/app.log | pi-agent run --skill log_analyzer "Extract errors and warnings, sort by frequency"

مثال 3: تقرير اختبار مؤتمت (الصعوبة: ⭐⭐⭐)

PYTHON
from pi_agent import Agent
import subprocess

agent = Agent(name="qa", system_prompt="You are a QA engineer, analyze test results and generate reports")

result = subprocess.run(["pytest", "--tb=short"], capture_output=True, text=True)
report = agent.run(f"Analyze the following test results and generate a report:\n{result.stdout}\n{result.stderr}")

with open("test_report.md", "w") as f:
    f.write(report.text)

5. رموز الخروج ومعالجة الأخطاء

رمز الخروج المعنى
0 نجاح
1 خطأ عام
2 خطأ تكوين
3 فشل استدعاء API
4 خطأ تنفيذ أداة
BASH
pi-agent run "Check deployment status" --skill devops
if [ $? -eq 0 ]; then
    echo "Check passed"
else
    echo "Check failed, exit code: $?"
fi

Python:

PYTHON
from pi_agent import Agent, AgentError

agent = Agent(name="checker")
try:
    result = agent.run("Verify configuration file")
    print("Verification passed:", result.text)
except AgentError as e:
    print(f"Execution failed: {e}")
    print(f"Error code: {e.code}")

6. تكامل المهام المجدولة

BASH
# crontab -e
0 9 * * * pi-agent run --skill daily_report "Generate today's project progress report" --output /reports/daily.md
0 0 * * 0 pi-agent run --skill code_review "Review this week's code changes" --output /reports/weekly.md

❓ أسئلة شائعة

س هل يدعم الوضع غير التفاعلي التدفق؟
ج ليس افتراضياً، لكن يمكنك استخدام علامة --stream. ومع ذلك، الأنابيب عادةً لا تحتاج للتدفق.
س هل هناك حد لطول إدخال الأنابيب؟
ج لا حد صارم، لكنه مقيد بنافذة سياق النموذج. الإدخال الطويل جداً يُقتطع أو يُقسم تلقائياً.
س ماذا عن حدود معدل API أثناء المعالجة الدفعية؟
ج Pi Agent يتضمن إعادة محاولة مدمجة. يمكنك أيضاً إضافة asyncio.sleep() في الكود للتحكم في تكرار الطلبات.

📖 ملخص


📝 تمارين

  1. أساسي (الصعوبة: ⭐): نفّذ مهمة واحدة بـ pi-agent run واحفظ النتائج في ملف.
  2. متوسط (الصعوبة: ⭐⭐): اكتب سكريبت دفعات لمراجعة جميع ملفات Python في دليل.
  3. متقدم (الصعوبة: ⭐⭐⭐): أعد git hook يُولّد تلقائياً رسائل commit باستخدام Pi Agent قبل الالتزام.
Web-Tutorial.com

فريق Web-Tutorial التقني

منصة دروس برمجية يديرها عدة مطورين. كل درس يتم كتابته ومراجعته بواسطة مطورين متخصصين في المجال. نعمل على ضمان دقة وموثوقية المحتوى — إذا لاحظت أي مشكلة، فيرجى إخبارنا.

100%