main.go

Overview

The main.go file implements a command-line executable that constructs and launches a sequential AI agent pipeline for automated Python code generation, review, and refactoring. It leverages the Google AI Developer Kit (ADK) to create three specialized sub-agents—Code Writer, Code Reviewer, and Code Refactorer—and orchestrates their execution in a fixed sequence using a SequentialAgent. This pipeline accepts a user specification, generates Python code, reviews it for quality and correctness, and refactors it based on review feedback. The final orchestrated agent is launched via the ADK framework's launcher subsystem, enabling runtime interaction.

This file primarily demonstrates agent composition, LLM integration, and workflow orchestration within the ADK ecosystem, referencing topics on LLM Integration and Agents, Agent Workflow Management, and Sequential Agent.


Detailed Explanation

Imports and Package


main Function

The main function is the entry point that performs the following steps:

1. Context Initialization

ctx := context.Background()

Initializes a background context used across API calls and agent creation for cancellation and tracing.

2. Model Creation

model, err := gemini.NewModel(ctx, "gemini-2.5-flash", &genai.ClientConfig{})

If model creation fails, the program logs a fatal error and terminates.


3. Sub-Agent Definitions

Three specialized LLM agents are created using llmagent.New with different instructions and output keys:

a. Code Writer Agent
codeWriterAgent, err := llmagent.New(llmagent.Config{
	Name:        "CodeWriterAgent",
	Model:       model,
	Instruction: "...",
	Description: "Writes initial Python code based on a specification.",
	OutputKey:   "generated_code",
})
b. Code Reviewer Agent
codeReviewerAgent, err := llmagent.New(llmagent.Config{
	Name:        "CodeReviewerAgent",
	Model:       model,
	Instruction: "...",
	Description: "Reviews code and provides feedback.",
	OutputKey:   "review_comments",
})
c. Code Refactorer Agent
codeRefactorerAgent, err := llmagent.New(llmagent.Config{
	Name:        "CodeRefactorerAgent",
	Model:       model,
	Instruction: "...",
	Description: "Refactors code based on review comments.",
	OutputKey:   "refactored_code",
})

Each agent creation includes error handling to abort execution if initialization fails.

Usage example for sub-agent instantiation:

agent, err := llmagent.New(llmagent.Config{...})
if err != nil {
	log.Fatalf("failed to create agent: %s", err)
}

4. Sequential Agent Creation

codePipelineAgent, err := sequentialagent.New(sequentialagent.Config{
	AgentConfig: agent.Config{
		Name:      "CodePipelineAgent",
		SubAgents: []agent.Agent{codeWriterAgent, codeReviewerAgent, codeRefactorerAgent},
		Description: "Executes a sequence of code writing, reviewing, and refactoring.",
	},
})

5. Agent Launching

config := &launcher.Config{
	AgentLoader: agent.NewSingleLoader(rootAgent),
}
l := full.NewLauncher()
if err = l.Execute(ctx, config, os.Args[1:]); err != nil {
	log.Fatalf("Run failed: %v\n\n%s", err, l.CommandLineSyntax())
}

The launcher manages the lifecycle, CLI parsing, and runtime environment according to REST API and Web Launchers.


Implementation Details


Interaction with Other System Components


Usage Example

To run the pipeline, invoke the compiled binary with user specification input as arguments or via interactive session (depending on launcher capabilities). The pipeline will:

  1. Receive a specification string.

  2. Generate Python code fulfilling the spec.

  3. Review the generated code.

  4. Refactor the code based on review.

  5. Output the final improved Python code snippet.


Diagram: Agent Structure and Workflow

flowchart TD
A[main.go] --> B[Gemini Model]
A --> C[CodeWriterAgent]
C --> D[generated_code]
D --> E[CodeReviewerAgent]
E --> F[review_comments]
F --> G[CodeRefactorerAgent]
G --> H[refactored_code]
H --> I[SequentialAgent: Runs C->E->G]
I --> J[Launcher: Executes SequentialAgent]

This file exemplifies orchestrating multiple LLM-powered agents into a cohesive AI pipeline using the ADK framework, demonstrating modular design, instruction templating, and sequential workflow management concepts from related topics.