Codex: مهارات Codex CLI و Sandbox والأمثلة

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

CLI هو وضع الاستخدام الأكثر مرونة لـ Codex. يغطي هذا الدرس المهارات وإعداد sandbox والأمثلة العملية في بيئة CLI.

📋 المتطلبات المسبقة: فهم عمليات Codex CLI الأساسية

1. ما ستتعلمه


2. إدارة مهارات CLI

(1) عرض المهارات المتاحة

BASH
codex --list-skills

(2) استخدام مهارة

BASH
# Specify Skill to execute task
codex --skill code-review "Review src/auth.ts"

# Combine options
codex --skill security --sandbox readonly "Scan for security vulnerabilities"

(3) موقع مهارات مخصصة

BASH
# Specify Skills directory
codex --skills-dir ./my-skills "Add login functionality"

(4) صيغة ملف المهارة

MARKDOWN
<!-- .codex/skills/api-endpoint.md -->
# API Endpoint Generator

## Role
FastAPI endpoint development expert

## Rules
- Use Pydantic models
- Include error handling
- Add Swagger documentation
- Write pytest tests

## Workflow
1. Define request/response models
2. Implement endpoint logic
3. Add error handling
4. Write tests
5. Ensure pytest passes

3. أمان Sandbox بالتفصيل

(1) طرق تنفيذ Sandbox

الطريقة الوصف مستوى الأمان
Docker عزل بحاويات الأعلى
chroot عزل الأدلة متوسط
على مستوى العملية قيود الأذونات أساسي

(2) إعداد Docker Sandbox

TOML
[sandbox]
type = "docker"
image = "codex-sandbox:latest"
memory = "2g"
cpus = 2
timeout = 600

# Mount volumes
volumes = [
  "./src:/workspace/src",
  "./tests:/workspace/tests",
]

# Environment variables
env = { "NODE_ENV" = "test" }

(3) مصفوفة أذونات Sandbox

العملية readonly workspace-write full-access
قراءة الملفات
كتابة مساحة العمل
كتابة النظام
تنفيذ الأوامر ⚠️ تأكيد
الوصول للشبكة

▶ مثال 1: إعداد الأمان لأليس

TOML
# Alice's sandbox config
[sandbox]
type = "docker"
memory = "4g"
cpus = 4

# Allowed operations
allowed_commands = [
  "npm test",
  "npm run lint",
  "git status",
  "git diff",
  "tsc --noEmit",
]

# Blocked operations
blocked_commands = [
  "rm -rf /",
  "sudo",
  "curl * | sh",
]

# Resource limits
max_file_size = "10MB"
max_execution_time = 300

4. مجموعة أمثلة عملية

(1) تهيئة المشروع

BASH
codex "Initialize Next.js 14 project: TypeScript + Tailwind + ESLint + Prettier, create standard directory structure"

(2) إصلاح الأخطاء

BASH
# Fix from error log
cat error.log | codex --full-auto "Analyze error log, locate root cause, fix code"

# Fix from Issue
codex "Fix Issue #42: Users not receiving verification email after registration"

(3) توليد الاختبارات

BASH
# Generate tests for all source files
codex --full-auto "Generate corresponding .test.ts for all .ts files under src/, using vitest"

# Supplement test coverage
codex "Check test coverage, supplement tests for files below 80%"

(4) إعادة هيكلة الكود

BASH
# Framework migration
codex "Refactor all Class components to functional components + Hooks"

# API migration
codex "Migrate REST API calls to tRPC, maintain functionality"

(5) توليد التوثيق

BASH
# API documentation
codex "Generate OpenAPI/Swagger docs for all API endpoints"

# README
codex "Generate README.md based on project structure and code"

▶ مثال 2: يوم بوب

BASH
# Morning: pull code + fix lint
git pull
codex --full-auto "Fix all lint errors"

# Forenoon: new feature
codex "Add user avatar upload with crop and compression"

# Afternoon: bug fix
codex "Fix payment timeout issue, add retry mechanism"

# Evening: tests
codex "Supplement integration tests for today's changes"

# Night: documentation
codex "Update API docs and CHANGELOG"

5. تقنيات Prompt لـ CLI

(1) إشارات ملفات دقيقة

BASH
# Good practice
codex "Modify src/auth/login.ts, add 2FA support"

# Bad practice
codex "Add a feature to login"

(2) تضمين التحقق

BASH
codex "Optimize database query performance, ensure npm test passes, compare execution time before and after optimization"

(3) تنفيذ خطوة بخطوة

BASH
# Complex task in steps
codex "Step 1: Create the Order model. Output the model definition when done, I'll confirm before continuing to step 2."

(4) الإشارة إلى كود موجود

BASH
codex "Create UserProfile component, follow the style of src/components/UserCard.tsx"

❓ أسئلة شائعة

س هل مهارات CLI هي نفس مهارات App؟
ج نفس الصيغة، لكن CLI يتطلب التحديد عبر معلمة --skill، بينما App يتيح الاختيار عبر واجهة المستخدم.
س هل يؤثر sandbox على الأداء؟
ج Docker sandbox لديه بعض تكلفة بدء التشغيل (~2-5 ثوان)، لكن فروق أداء التشغيل ضئيلة. Sandbox على مستوى العملية ليس لها تكلفة تذكر.
س هل يمكن لـ CLI تشغيل مهام متعددة في نفس الوقت؟
ج افتح نوافذ طرفية متعددة، كل منها يشغل نسخة Codex. استخدم Worktrees لتجنب تعارضات الملفات.
س كيف أعرض سجلات تنفيذ CLI التفصيلية؟
ج استخدم معلمة --verbose أو --log-file لتحديد ملف سجل.
س هل يمكن للوضع غير التفاعلي لـ CLI التعامل مع طلبات التأكيد؟
ج يتطلب --approval-policy approve أو --full-auto. وإلا سيتوقف الوضع غير التفاعلي بانتظار التأكيد.

📖 ملخص


📝 تمارين

  1. أساسي (⭐): استخدم مهارات CLI لإكمال مراجعة كود.
  2. متوسط (⭐⭐): إعداد Docker sandbox، نفّذ مهمة بداخله.
  3. متقدم (⭐⭐⭐): اكتب نص أتمتة CLI يغطي جميع مهام Codex للتطوير اليومي.
Web-Tutorial.com

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

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

100%