OpenCode
2 دقيقة للقراءة

OpenCode: الوكلاء

نظرة عامة

نظام الوكلاء المتعددين هو ما يجعل OpenCode فريداً. بدلاً من agent واحد يحاول فعل كل شيء، OpenCode يستخدم وكلاء متخصصين - كل واحد خبير في مجاله.


ما هي الوكلاء؟

الوكيل (Agent) = برنامج AI متخصص في مهمة محددة

الوكيلالتخصصالمهمة
Plannerالتخطيطيحلل الطلبات ويخطط الخطوات
Coderالكتابةيكتب الكود الفعلي
Reviewerالمراجعةيراجع الجودة ويقترح تحسينات
Testerالاختباريكتب ويشغل الاختبارات
Debuggerالتصحيحيبحث عن Bugs ويصلحها

سير العمل الوكلاء

┌─────────────────────────────────────────┐
│         User Request                   │
│    "أنشئ REST API مع مصادقة"          │
└─────────────┬───────────────────────────┘
              │
              ▼
┌─────────────────────────────────────────┐
│      📋 Planner Agent                  │
│  - يحلل الطلب                          │
│  - يقسم المهام                         │
│  - يحدد الترتيب                        │
│                                         │
│  Output: خطة بـ 5 خطوات                │
└─────────────┬───────────────────────────┘
              │
              ▼
┌─────────────────────────────────────────┐
│      💻 Coder Agent                     │
│  - يكتب الكود                          │
│  - ينشئ الملفات                        │
│  - يتبع أفضل الممارسات                 │
│                                         │
│  Output: 10 ملفات كود                  │
└─────────────┬───────────────────────────┘
              │
              ▼
┌─────────────────────────────────────────┐
│      🔍 Reviewer Agent                  │
│  - يراجع الكود                         │
│  - يتحقق من الجودة                     │
│  - يقترح تحسينات                       │
│                                         │
│  Output: 15 اقتراح تحسين               │
└─────────────┬───────────────────────────┘
              │
              ▼
┌─────────────────────────────────────────┐
│      🧪 Tester Agent                    │
│  - يكتب الاختبارات                     │
│  - يشغلها                              │
│  - يتحقق من التغطية                    │
│                                         │
│  Output: 25 اختبار، 90% تغطية          │
└─────────────────────────────────────────┘

الوكلاء بالتفصيل

1. Planner Agent 📋

المهمة: فهم الطلب والتخطيط

# استخدام Planner
opencode plan "REST API مع مصادقة"

# Planner سيحلل:
# - ما هي REST API؟
# - ما نوع المصادقة؟
# - ما الـ endpoints المطلوبة؟
# - ما هي dependencies؟
# - ما هو الترتيب الأمثل؟

مخرجات Planner:

خطة التنفيذ:

1. إنشاء هيكل المشروع (5 دقائق)
2. إعداد Express.js (2 دقيقة)
3. إضافة JWT authentication (10 دقائق)
4. إنشاء endpoints (15 دقيقة)
5. إضافة middleware (5 دقيقة)

الوقت الإجمالي: ~37 دقيقة

2. Coder Agent 💻

المهمة: كتابة الكود

# استخدام Coder
opencode generate "Express server with JWT auth"

# Coder سينشئ:
# - server.js
# - auth.js
# - middleware/
# - routes/
# - controllers/

يتبع Coder:

  • ✅ أفضل الممارسات
  • ✅ patterns المشروع
  • ✅ قواعد .opencode-rules
  • ✅ security best practices

3. Reviewer Agent 🔍

المهمة: مراجعة الجودة

# استخدام Reviewer
opencode review src/api/

# Reviewer سيتحقق من:
# - security vulnerabilities
# - performance issues
# - code style
# - best practices
# - documentation

مخرجات Reviewer:

تقرير المراجعة:

✅ جيد:
  - استخدام JWT للمصادقة
  - separation of concerns

⚠️ تحسينات:
  - أضف rate limiting
  - أضف input validation
  - أضف error logging

❌ مشاكل:
  - SQL injection vulnerability في line 42
  - missing error handling في /api/users

4. Tester Agent 🧪

المهمة: كتابة وتشغيل الاختبارات

# استخدام Tester
opencode test src/api/

# Tester سينشئ:
# - unit tests
# - integration tests
# - edge case tests
# - mocks

يغطي Tester:

  • ✅ الحالات الطبيعية (happy path)
  • ✅ الحالات الشاذة (error cases)
  • ✅ الحالات الحدية (edge cases)
  • ✅ التكامل (integration)

5. Debugger Agent 🐛

المهمة: إصلاح Bugs

# استخدام Debugger
opencode debug \
  --file src/api/users.ts \
  --error "TypeError: Cannot read property 'id'"

# Debugger سيبحث عن:
# - أين حدث الخطأ
# - لماذا حدث
# - كيف يصلح

سير عمل Debugger:

  1. التحليل - يقرأ stack trace
  2. البحث - يبحث عن الكود المرتبط
  3. التشخيص - يحدد السبب الجذري
  4. الإصلاح - يقترح الحلول
  5. التحقق - يختبر الحل

تعاون الوكلاء

مثال: بناء ميزة كاملة

opencode feature "user notifications"

ما يحدث خلف الكواليس:

  1. Planner يخطط:

    1. إنشاء notification service
    2. إضافة UI components
    3. ربط بقاعدة البيانات
    4. إضافة WebSocket support
    5. كتابة اختبارات
    
  2. Coder يكتب الكود:

    - services/notification.service.ts
    - components/NotificationList.tsx
    - components/NotificationItem.tsx
    - api/notifications.ts
    
  3. Reviewer يراجع:

    ✅ types صحيحة
    ⚠️ أضف error handling
    ⚠️ أضف rate limiting
    
  4. Tester يختبر:

    ✅ 20 اختبار
    ✅ 95% تغطية
    ⚠️ أضف اختبار لـ edge case
    
  5. Debugger يصلح (إذا لزم):

    ✅ أصلح 3 bugs صغيرة
    

تخصيص الوكلاء

تعيين نماذج مختلفة

# Planner - يحتاج فهم عميق
opencode config set agent.planner.model claude-3-opus

# Coder - يحتاج توليد سريع
openerate config set agent.coder.model gpt-3.5-turbo

# Reviewer - يحتاج دقة
opencode config set agent.reviewer.model claude-3-sonnet

# Tester - يمكن أن يكون أسرع
opencode config set agent.tester.model gpt-3.5-turbo

# Debugger - يحتاج تحليل
opencode config set agent.debugger.model claude-3-sonnet

تخصيص temperature

# Planner - دقيق
opencode config set agent.planner.temperature 0.1

# Coder - متوسط
opencode config set agent.coder.temperature 0.3

# Reviewer - دقيق
openerate config set agent.reviewer.temperature 0.1

# Tester - متوسط
opencode config set agent.tester.temperature 0.3

# Debugger - منفتح
opencode config set agent.debugger.temperature 0.5

تخصيص max tokens

# Planner - يحتاج output صغير
opencode config set agent.planner.max-tokens 1000

# Coder - يحتاج output كبير
openerate config set agent.coder.max-tokens 4000

# Reviewer - output متوسط
opencode config set agent.reviewer.max-tokens 2000

أوضاع الوكلاء

1. الوضع التلقائي (Autonomous)

opencode agent --autonomous "بناء REST API"

الوكلاء يعملون بدون تدخل بشري:

  • Planner → Coder → Reviewer → Tester → Debugger
  • كل وكيل يمرر النتيجة للوكيل التالي
  • لا يطلب موافقة

متى تستخدم:

  • المشاريع الكبيرة الواضحة
  • عندما تسرعك المهمة
  • عندما تثق بالوكلاء

2. الوضع التفاعلي (Interactive)

opencode agent --interactive "بناء REST API"

كل وكيل يطلب موافقة:

📋 Planner: أنشأت خطة بـ 5 خطوات. أوافق؟ [y/n]

💻 Coder: سأنشئ 10 ملفات. أوافق؟ [y/n]

🔍 Reviewer: وجدت 3 مشاكل. أصلحها؟ [y/n]

🧪 Tester: سأنشئ 20 اختبار. أوافق؟ [y/n]

متى تستخدم:

  • المشاريع الحرجة
  • عندما تريد سيطرة كاملة
  • عندما تتعلم

3. الوضع المخصص (Custom)

# تحديد وكلاء محددة
opencode agent \
  --agents planner,coder \
  "REST API"

# تخطي المراجعة والاختبار
opencode agent \
  --skip reviewer,tester \
  "prototype سريع"

نصائح الوكلاء

✅ DO (افعل هذا):

# 1. استخدم الوضع المناسب
opencode agent --autonomous "مهمة كبيرة واضحة"
opencode agent --interactive "مهمة حرجة"

# 2. خصص النماذج
opencode config set agent.planner.model claude-3-opus
opencode config set agent.coder.model gpt-3.5-turbo

# 3. راجع عمل الوكلاء
opencode review src/ --check-agents

# 4. راقب الأداء
opencode stats --agents

❌ DON'T (لا تفعل هذا):

# 🚫 لا تستخدم autonomous للمهام الحرجة
opencode agent --autonomous "production code"# 🚫 لا تستخدم نموذج ضعيف لـ Planner
opencode config set agent.planner.model gpt-3.5-turbo ❌

# 🚫 لا تتخطى Reviewer للمهام الكبيرة
opencode agent --skip reviewer "ميزة كبيرة"# 🚫 لا تنسى Tester
opencode agent --skip tester "production code"

الخلاصة

نظام الوكلاء هو ما يجعل OpenCode قوياً:

تخصص - كل وكيل خبير في مجاله ✅ تعاون - الوكلاء يعملون معاً ✅ مرونة - تخصيص حسب احتياجاتك ✅ جودة - مراجعة واختبار تلقائي ✅ سرعة - عمل موازٍ بين الوكلاء

استخدم الوكلاء بحكمة:

# للمشاريع الكبيرة - autonomous
opencode agent --autonomous "project كامل"

# للمشاريع الحرجة - interactive
opencode agent --interactive "feature حرجة"

# للنماذج - اختر بحكمة
opencode config set agent.planner.model claude-3-opus
opencode config set agent.coder.model gpt-3.5-turbo

# راقب دائماً
opencode review src/ --check-agents

ابدأ اليوم:

npm install -g @ohmyopencode/cli
opencode init
opencode agent --interactive "hello world"