Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ DEV_EVAL_DEFAULT_INCLUDE=".py,.js,.java,.ts,.tsx,.jsx,.c,.cpp,.h,.hpp"
# 默认排除的文件类型
DEV_EVAL_DEFAULT_EXCLUDE=".md,.txt,.json,.lock,.gitignore"

# ===== 其他可选配置 =====
# 日志级别,可以是 DEBUG, INFO, WARNING, ERROR
# ===== Orchestration Configuration =====
# Enable orchestrated code review with specialized agents
USE_ORCHESTRATION="false"

# ===== Other Optional Configuration =====
# Log level, can be DEBUG, INFO, WARNING, ERROR
LOG_LEVEL="INFO"
10 changes: 9 additions & 1 deletion codedog/chains/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from codedog.chains.code_review.base import CodeReviewChain
from codedog.chains.code_review.orchestrated import OrchestratedCodeReviewChain
from codedog.chains.code_review.factory import CodeReviewChainFactory
from codedog.chains.pr_summary.base import PRSummaryChain
from codedog.chains.pr_summary.translate_pr_summary_chain import TranslatePRSummaryChain

__all__ = ["PRSummaryChain", "CodeReviewChain", "TranslatePRSummaryChain"]
__all__ = [
"PRSummaryChain",
"CodeReviewChain",
"OrchestratedCodeReviewChain",
"CodeReviewChainFactory",
"TranslatePRSummaryChain"
]
57 changes: 57 additions & 0 deletions codedog/chains/code_review/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Factory for creating code review chains.
"""

from typing import Dict, Optional

from langchain_core.language_models import BaseLanguageModel

from codedog.chains.code_review.base import CodeReviewChain
from codedog.chains.code_review.orchestrated import OrchestratedCodeReviewChain


class CodeReviewChainFactory:
"""
Factory for creating code review chains.

This factory supports creating both traditional and orchestrated
code review chains based on configuration.
"""

@staticmethod
def create_chain(
llm: BaseLanguageModel,
use_orchestration: bool = False,
models: Optional[Dict[str, BaseLanguageModel]] = None,
**kwargs
):
"""
Create a code review chain.

Args:
llm: Language model for traditional chain or default model for orchestrated chain
use_orchestration: Whether to use orchestration
models: Dictionary of language models for different agents (for orchestration)
**kwargs: Additional arguments for chain creation

Returns:
CodeReviewChain or OrchestratedCodeReviewChain instance
"""
if use_orchestration:
# Prepare models dictionary
if models is None:
models = {"default": llm}
elif "default" not in models:
models["default"] = llm

# Create orchestrated chain
return OrchestratedCodeReviewChain.from_llms(
models=models,
**kwargs
)
else:
# Create traditional chain
return CodeReviewChain.from_llm(
llm=llm,
**kwargs
)
123 changes: 123 additions & 0 deletions codedog/chains/code_review/orchestrated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Orchestrated code review chain using specialized agents.
"""

from __future__ import annotations

import asyncio
from typing import Any, Dict, List, Optional

from langchain_core.language_models import BaseLanguageModel
from langchain_core.callbacks.manager import (
AsyncCallbackManagerForChainRun,
CallbackManagerForChainRun,
)
from langchain.chains.base import Chain
from pydantic import Field

from codedog.models import ChangeFile, CodeReview, PullRequest
from codedog.processors import PullRequestProcessor
from codedog.orchestration.orchestrator import CodeReviewOrchestrator


class OrchestratedCodeReviewChain(Chain):
"""
Orchestrated code review chain using specialized agents.

This chain uses the orchestration architecture to coordinate
multiple specialized agents for a comprehensive code review.
"""

orchestrator: CodeReviewOrchestrator = Field(exclude=True)
"""Orchestrator for code review."""

processor: PullRequestProcessor = Field(
exclude=True, default_factory=PullRequestProcessor.build
)
"""PR data processor."""

_input_keys: List[str] = ["pull_request"]
_output_keys: List[str] = ["code_reviews"]

@property
def _chain_type(self) -> str:
return "orchestrated_code_review_chain"

@property
def input_keys(self) -> List[str]:
"""Will be whatever keys the prompt expects.

:meta private:
"""
return self._input_keys

@property
def output_keys(self) -> List[str]:
"""Will always return text key.

:meta private:
"""
return self._output_keys

def _call(
self,
inputs: Dict[str, Any],
run_manager: Optional[CallbackManagerForChainRun] = None,
) -> Dict[str, Any]:
"""
Synchronous call is not supported for orchestrated review.
Use async call instead.
"""
raise NotImplementedError(
"Orchestrated code review only supports async calls. Use acall instead."
)

async def _acall(
self,
inputs: Dict[str, Any],
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
) -> Dict[str, Any]:
"""
Perform orchestrated code review asynchronously.
"""
_run_manager = run_manager or AsyncCallbackManagerForChainRun.get_noop_manager()
await _run_manager.on_text(inputs["pull_request"].json() + "\n")

pr: PullRequest = inputs["pull_request"]
code_files: List[ChangeFile] = self.processor.get_diff_code_files(pr)

# Use orchestrator to review files
code_reviews = await self.orchestrator.review_files(code_files)

return {"code_reviews": code_reviews}

@classmethod
def from_llms(
cls,
*,
models: Dict[str, BaseLanguageModel],
**kwargs,
) -> OrchestratedCodeReviewChain:
"""
Create an orchestrated code review chain from language models.

Args:
models: Dictionary of language models for different agents
Keys should include: 'coordinator', 'security', 'performance',
'readability', 'architecture', 'documentation', 'default'

Returns:
OrchestratedCodeReviewChain instance
"""
# Ensure we have a default model
if "default" not in models and len(models) > 0:
# Use the first model as default
models["default"] = next(iter(models.values()))

# Create orchestrator
orchestrator = CodeReviewOrchestrator(models)

return cls(
orchestrator=orchestrator,
processor=PullRequestProcessor(),
)
26 changes: 26 additions & 0 deletions codedog/orchestration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Orchestration module for CodeDog.

This module contains the orchestration architecture for coordinating
specialized agents in the code review process.
"""

from codedog.orchestration.coordinator import ReviewCoordinator
from codedog.orchestration.orchestrator import CodeReviewOrchestrator
from codedog.orchestration.agents import (
SecurityReviewAgent,
PerformanceReviewAgent,
ReadabilityReviewAgent,
ArchitectureReviewAgent,
DocumentationReviewAgent
)

__all__ = [
"ReviewCoordinator",
"CodeReviewOrchestrator",
"SecurityReviewAgent",
"PerformanceReviewAgent",
"ReadabilityReviewAgent",
"ArchitectureReviewAgent",
"DocumentationReviewAgent"
]
Loading
Loading