from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.agents.llm_agent import LlmAgent
# --- 常量 ---GEMINI_MODEL ="gemini-2.5-flash"# --- 1. 为每个流水线阶段定义子 Agent ---# 代码编写 Agent# 接收初始规范(来自用户查询)并编写代码。code_writer_agent = LlmAgent(
name="CodeWriterAgent",
model=GEMINI_MODEL,
instruction="""
You are a Python Code Generator.
Based *only* on the user's request, write Python code that fulfills the requirement.
Output *only* the complete Python code block, enclosed in triple backticks (```python ... ```).
Do not add any other text before or after the code block.
""",
description="Writes initial Python code based on a specification.",
output_key="generated_code")
# 代码审查 Agent# 接收前一个 Agent 生成的代码(从状态中读取)并提供反馈。code_reviewer_agent = LlmAgent(
name="CodeReviewerAgent",
model=GEMINI_MODEL,
instruction="""
You are an expert Python Code Reviewer.
Your task is to provide constructive feedback on the provided code.
**Code to Review:**
```python
{generated_code} ```
**评审标准:**
1. **正确性:** 代码是否按预期工作?是否存在逻辑错误?
2. **可读性:** 代码是否清晰易懂?是否遵循 PEP 8 风格指南?
3. **效率:** 代码是否足够高效?是否存在明显的性能瓶颈?
4. **边界情况:** 代码是否能优雅地处理潜在的边界情况或无效输入?
5. **最佳实践:** 代码是否遵循常见的 Python 最佳实践?
**输出:**
以简明的项目符号列表提供反馈。专注于最重要的改进点。
如果代码非常完善且无需修改,只需说明:“未发现重大问题”。
仅输出评审意见或“未发现重大问题”声明。
```python
{generated_code} ```
**审查评论:**
{review_comments} **任务:**
仔细根据审查评论的建议重构原始代码。
如果审查评论指出“未发现重大问题”,则返回原始代码,不作更改。
确保最终代码完整、可运行,并包含必要的导入和文档字符串。
**输出:**
仅输出最终重构的 Python 代码块,用三重反引号 (```python ... ```) 包裹。
不要在代码块前后添加任何其他文本。
go91 行展开收起expand_more
model, err:=gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
iferr!=nil {
returnfmt.Errorf("failed to create model: %v", err)
}
codeWriterAgent, err:=llmagent.New(llmagent.Config{
Name: "CodeWriterAgent",
Model: model,
Description: "Writes initial Go code based on a specification.",
Instruction: `You are a Go Code Generator.
Based *only* on the user's request, write Go code that fulfills the requirement.
Output *only* the complete Go code block, enclosed in triple backticks ('''go ... ''').
Do not add any other text before or after the code block.`,
OutputKey: "generated_code",
})
iferr!=nil {
returnfmt.Errorf("failed to create code writer agent: %v", err)
}
codeReviewerAgent, err:=llmagent.New(llmagent.Config{
Name: "CodeReviewerAgent",
Model: model,
Description: "Reviews code and provides feedback.",
Instruction: `You are an expert Go Code Reviewer.
Your task is to provide constructive feedback on the provided code.
**Code to Review:**
'''go
{generated_code}
'''
**Review Criteria:**
1. **Correctness:** Does the code work as intended? Are there logic errors?
2. **Readability:** Is the code clear and easy to understand? Follows Go style guidelines?
3. **Idiomatic Go:** Does the code use Go's features in a natural and standard way?
4. **Edge Cases:** Does the code handle potential edge cases or invalid inputs gracefully?
5. **Best Practices:** Does the code follow common Go best practices?
**Output:**
Provide your feedback as a concise, bulleted list. Focus on the most important points for improvement.
If the code is excellent and requires no changes, simply state: "No major issues found."
Output *only* the review comments or the "No major issues" statement.`,
OutputKey: "review_comments",
})
iferr!=nil {
returnfmt.Errorf("failed to create code reviewer agent: %v", err)
}
codeRefactorerAgent, err:=llmagent.New(llmagent.Config{
Name: "CodeRefactorerAgent",
Model: model,
Description: "Refactors code based on review comments.",
Instruction: `You are a Go Code Refactoring AI.
Your goal is to improve the given Go code based on the provided review comments.
**Original Code:**
'''go
{generated_code}
'''
**Review Comments:**
{review_comments}
**Task:**
Carefully apply the suggestions from the review comments to refactor the original code.
If the review comments state "No major issues found," return the original code unchanged.
Ensure the final code is complete, functional, and includes necessary imports.
**Output:**
Output *only* the final, refactored Go code block, enclosed in triple backticks ('''go ... ''').
Do not add any other text before or after the code block.`,
OutputKey: "refactored_code",
})
iferr!=nil {
returnfmt.Errorf("failed to create code refactorer agent: %v", err)
}
codePipelineAgent, err:=sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: appName,
Description: "Executes a sequence of code writing, reviewing, and refactoring.",
SubAgents: []agent.Agent{
codeWriterAgent,
codeReviewerAgent,
codeRefactorerAgent,
},
},
})
iferr!=nil {
returnfmt.Errorf("failed to create sequential agent: %v", err)
}