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

OpenCode: الإعدادات

نظرة عامة

OpenCode CLI يوفر نظام تكوين مرن ومتعدد الطبقات يناسب مختلف احتياجات المطورين - من الإعدادات السريعة إلى التخصيص العميق.


طبقات التكوين

OpenCode يطبق التكوين بهذا الترتيب (الأولوية للأعلى):

  1. Environment Variables (الأولوى القصوى)
  2. Command-line Flags (أولوية عالية)
  3. Local Config (.opencode/config.json)
  4. Project Config (.opencode-rules)
  5. Global Config (~/.opencode/config.json)
  6. Default Values (أولوية منخفضة)

التكوين السريع

الإعداد الأولي

# تشغيل الإعداد التفاعلي
opencode init

# سيطلب منك:
# 1. المزود (openai, anthropic, ollama, etc.)
# 2. مفتاح API (اختياري للمحلية)
# 3. النموذج الافتراضي
# 4. التفضيلات الأخرى

التكوين الأساسي

# تعيين المزود
opencode config set provider openai

# تعيين مفتاح API
opencode config set apikey sk-...your-key...

# تعيين النموذج الافتراضي
opencode config set model gpt-4-turbo

# التحقق من التكوين
opencode config get-all

ملفات التكوين

1. Global Config (~/.opencode/config.json)

التكوين العام لجميع المشاريع:

{
  "provider": "openai",
  "apikey": "sk-...",
  "model": "gpt-4-turbo",
  "temperature": 0.3,
  "max-tokens": 4000,
  "cache": {
    "enabled": true,
    "duration": 3600
  },
  "agents": {
    "planner": {
      "model": "claude-3-opus",
      "temperature": 0.1
    },
    "coder": {
      "model": "gpt-3.5-turbo",
      "temperature": 0.3
    },
    "reviewer": {
      "model": "claude-3-sonnet",
      "temperature": 0.1
    },
    "tester": {
      "model": "gpt-3.5-turbo",
      "temperature": 0.3
    },
    "debugger": {
      "model": "claude-3-sonnet",
      "temperature": 0.5
    }
  },
  "ui": {
    "theme": "dark",
    "language": "ar"
  }
}

إنشاء/تعديل:

# فتح في محرر
opencode config edit --global

# أو إنشاء يدوياً
mkdir -p ~/.opencode
cat > ~/.opencode/config.json << EOF
{ ... }
EOF

2. Local Config (.opencode/config.json)

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

# في جذر المشروع
mkdir -p .opencode
cat > .opencode/config.json << EOF
{
  "model": "claude-3-sonnet",
  "temperature": 0.2,
  "max-tokens": 3000,
  "project-specific": {
    "framework": "nextjs",
    "typescript": true,
    "testing": "vitest"
  }
}
EOF

3. Project Rules (.opencode-rules)

قواعد المشروع:

# .opencode-rules
project:
  name: "My Project"
  type: "Next.js 14 + TypeScript"
  description: "SaaS application"

coding_style:
  - "TypeScript strict mode"
  - "Functional Components فقط"
  - "Tailwind CSS للأنماط"
  - "أسماء componentes: PascalCase"
  - "أسماء files: kebab-case"

naming_conventions:
  components: "PascalCase"
  files: "kebab-case"
  variables: "camelCase"
  constants: "UPPER_SNAKE_CASE"

ai_preferences:
  model: "claude-3-sonnet"
  temperature: 0.2
  max_tokens: 4000
  timeout: 30000

agents:
  planner:
    model: "claude-3-opus"
    temperature: 0.1
  coder:
    model: "gpt-3.5-turbo"
    temperature: 0.3
  reviewer:
    model: "claude-3-sonnet"
    temperature: 0.1

paths:
  src: "./src"
  tests: "./tests"
  output: "./dist"
  components: "./src/components"

ignore:
  - "node_modules/**"
  - ".next/**"
  - "dist/**"
  - "*.test.ts"
  - "*.spec.ts"

preferences:
  auto_test: true
  auto_review: true
  git_integration: true
  cache_enabled: true
EOF

خيارات التكوين

Provider Options

# OpenAI
opencode config set provider openai
opencode config set apikey sk-...
opencode config set model gpt-4-turbo

# Anthropic Claude
opencode config set provider anthropic
opencode config set apikey sk-ant-...
openerate config set model claude-3-sonnet

# Google Gemini
opencode config set provider google
opencode config set apikey AI...
opencode config set model gemini-pro

# Ollama (محلي)
opencode config set provider ollama
opencode config set model codellama:7b
opencode config set apiEndpoint http://localhost:11434

# LM Studio (محلي)
opencode config set provider lmstudio
opencode config set model local-model
opencode config set apiEndpoint http://localhost:1234

Model Options

# عرض النماذج المتاحة
opencode models list

# تعيين النموذج الافتراضي
opencode config set model claude-3-sonnet

# تعيين نماذج لوكلاء مختلفين
opencode config set agent.planner.model claude-3-opus
opencode config set agent.coder.model gpt-3.5-turbo

Temperature Options

# درجة الحرارة (0.0 - 1.0)
# 0.0 = دقيق جداً (قواعد، regex)
# 0.5 = متوسط (معظم المهام)
# 1.0 = إبداعي (أسماء، تصميم)

opencode config set temperature 0.3

# لكل وكيل
opencode config set agent.planner.temperature 0.1
opencode config set agent.coder.temperature 0.3

Token Limits

# الحد الأقصى لل tokens
opencode config set max-tokens 4000

# لكل وكيل
opencode config set agent.planner.max-tokens 1000
opencode config set agent.coder.max-tokens 4000

Cache Options

# تفعيل caching
opencode config set cache.enabled true

# مدة caching (ثواني)
opencode config set cache.duration 3600

# حجم cache (MB)
opencode config set cache.max-size 500

# مسح cache
opencode cache clear

UI Options

# السمة
opencode config set ui.theme dark

# اللغة
opencode config set ui.language ar

# Verbosity
opencode config set ui.verbosity verbose
# options: silent, normal, verbose, debug

Environment Variables

# في ~/.bashrc أو ~/.zshrc

# المزود
export OPENCODE_PROVIDER=openai

# مفتاح API
export OPENCODE_APIKEY=sk-...

# النموذج
export OPENCODE_MODEL=gpt-4-turbo

# Endpoint (للمحلية)
export OPENCODE_API_ENDPOINT=http://localhost:11434

# Cache
export OPENCODE_CACHE_ENABLED=true
export OPENCODE_CACHE_DIR=~/.opencode/cache

# UI
export OPENCODE_THEME=dark
export OPENCODE_LANGUAGE=ar

Command-line Flags

# تجاوز التكوين لأمر واحد
opencode ask "سؤال" \
  --provider anthropic \
  --model claude-3-opus \
  --temperature 0.5 \
  --max-tokens 2000

# استخدام ملف تكوين مخصص
opencode ask "سؤال" --config path/to/config.json

# مستوى verbosity
opencode ask "سؤال" --verbose
opencode ask "سؤال" --quiet
openerate ask "سؤال" --debug

التكوين المتقدم

1. Profiles

# إنشاء profiles
opencode profiles create dev \
  --model gpt-3.5-turbo \
  --temperature 0.3

opencode profiles create prod \
  --model claude-3-opus \
  --temperature 0.1

# استخدام profile
opencode ask "سؤال" --profile dev

# تعيين default profile
opencode config set default-profile prod

2. Templates

# إنشاء template
opencode templates create saas-starter

# تعديل template
opencode templates edit saas-starter

# استخدام template
opencode project create --template saas-starter "My App"

3. Hooks

# before-hook - ينفذ قبل الأمر
opencode hooks add before-generate \
  "npm run lint"

# after-hook - ينفذ بعد الأمر
opencode hooks add after-generate \
  "npm run format"

# on-error - ينفذ عند الخطأ
opencode hooks add on-error \
  "npm run lint:fix"

استكشاف الأخطاء

عرض التكوين الحالي

# عرض جميع الإعدادات
opencode config get-all

# عرض إعداد محدد
opencode config get provider
opencode config get model

# عرض تكوين المشروع
opencode config --project

# عرض تكوين الوكلاء
opencode config --agents

إعادة تعيين التكوين

# إعادة تعيين إعداد
opencode config unset temperature

# إعادة تعيين الكل
opencode config reset-all

# إعادة التهيئة
opencode init --force

نصائح التكوين

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

# 1. استخدم init للإعداد الأولي
opencode init

# 2. أنشئ .opencode-rules
cat > .opencode-rules << EOF
project: { ... }
EOF

# 3. خصص الوكلاء
opencode config set agent.planner.model claude-3-opus
openerate config set agent.coder.model gpt-3.5-turbo

# 4. فعّل caching
opencode config set cache.enabled true

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

# 🚫 لا تضع API key في الكود
export OPENCODE_APIKEY=sk-...  # ❌ في .env
# استخدم:
echo "apikey" >> .gitignore   # ✅

# 🚫 لا تستخدم temperature عالية للمهام الدقيقة
opencode config set temperature 0.9  # ❌ للمهام الدقيقة
opencode config set temperature 0.1  # ✅

# 🚫 لا تستخدم نموذج ضعيف لـ Planner
opencode config set agent.planner.model gpt-3.5-turbo  # ❌
opencode config set agent.planner.model claude-3-opus  # ✅

الخلاصة

التكوين الفعال في OpenCode:

استخدم init - للإعداد السريع ✅ أنشئ .opencode-rules - للقواعد الخاصة ✅ خصّص الوكلاء - كل وكيل له نموذج ✅ فعّل caching - لتوفير الوقت والمال ✅ استخدم profiles - لسيناريوهات مختلفة

ابدأ اليوم:

npm install -g @ohmyopencode/cli
opencode init
opencode config get-all