Skip to content

Architecture

This document describes the internal architecture of PGA β€” the project structure, key components, and how they interact.


Solution Structure

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
PowergenticAgent/
β”œβ”€β”€ Directory.Build.props          # Shared build settings (net10.0, nullable, etc.)
β”œβ”€β”€ PowergenticAgent.sln           # Solution file
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ Pga.Core/                  # Core library β€” agents, tools, chat, config
β”‚   β”‚   β”œβ”€β”€ Agents/                # Agent system (loading, parsing, scoping)
β”‚   β”‚   β”œβ”€β”€ Chat/                  # Chat orchestrator and message history
β”‚   β”‚   β”œβ”€β”€ Configuration/         # Config models and manager
β”‚   β”‚   β”œβ”€β”€ Extensions/            # DI registration helpers
β”‚   β”‚   β”œβ”€β”€ Providers/             # LLM provider factory
β”‚   β”‚   └── Tools/                 # Built-in tool implementations
β”‚   └── Pga.Cli/                   # CLI application β€” commands and rendering
β”‚       β”œβ”€β”€ Commands/              # CLI command definitions
β”‚       └── Rendering/             # Terminal output formatting
β”œβ”€β”€ tests/
β”‚   └── Pga.Tests/                 # Unit tests
β”‚       β”œβ”€β”€ Agents/
β”‚       β”œβ”€β”€ Configuration/
β”‚       └── Tools/
└── docs/                          # Documentation

Projects

Pga.Core

The core library containing all business logic. Has no dependency on the CLI or any UI framework. Could be embedded in other applications.

Dependencies:

  • Microsoft.Extensions.AI β€” AI abstractions (IChatClient, AIFunction, ChatMessage)
  • Microsoft.Extensions.AI.OpenAI β€” OpenAI/Azure OpenAI IChatClient adapter
  • Azure.AI.OpenAI β€” Azure OpenAI client SDK
  • Azure.Identity β€” Azure Entra ID authentication
  • OllamaSharp β€” Ollama IChatClient implementation
  • Markdig β€” Markdown processing
  • YamlDotNet β€” YAML frontmatter parsing
  • Microsoft.Extensions.Hosting β€” Dependency injection support

Pga.Cli

The console application. References Pga.Core and adds CLI-specific concerns: command parsing, interactive REPL, and rich terminal rendering.

Additional dependencies:

  • System.CommandLine β€” CLI argument parsing and command routing
  • Spectre.Console β€” Rich terminal output (panels, tables, figlet text, colors)

Build output: The assembly name is pga (not Pga.Cli), and it's configured for single-file self-contained publishing.

Pga.Tests

xUnit-based unit tests covering agents, configuration, and tools.


Component Architecture

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Pga.Cli                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ Program  β”‚  β”‚ Commands  β”‚  β”‚  ConsoleRenderer     β”‚ β”‚
β”‚  β”‚ (entry)  β”‚  β”‚ (chat,    β”‚  β”‚  (Spectre.Console)   β”‚ β”‚
β”‚  β”‚          β”‚  β”‚  explain, β”‚  β”‚                      β”‚ β”‚
β”‚  β”‚          β”‚  β”‚  suggest, β”‚  β”‚                      β”‚ β”‚
β”‚  β”‚          β”‚  β”‚  config,  β”‚  β”‚                      β”‚ β”‚
β”‚  β”‚          β”‚  β”‚  init)    β”‚  β”‚                      β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚ references
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Pga.Core                           β”‚
β”‚                                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  ConfigManager    β”‚  β”‚  ChatOrchestrator          β”‚  β”‚
β”‚  β”‚  (load/save       β”‚  β”‚  (conversation loop,       β”‚  β”‚
β”‚  β”‚   config files)   β”‚  β”‚   tool calling,            β”‚  β”‚
β”‚  β”‚                   β”‚  β”‚   message history)         β”‚  β”‚
β”‚  β”‚  PgaConfiguration β”‚  β”‚                            β”‚  β”‚
β”‚  β”‚  LlmProfile       β”‚  β”‚                            β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                    β”‚                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  AgentLoader      β”‚  β”‚  LlmProviderFactory        β”‚  β”‚
β”‚  β”‚  (discover +      β”‚  β”‚  (creates IChatClient      β”‚  β”‚
β”‚  β”‚   parse agents)   β”‚  β”‚   from profile config)     β”‚  β”‚
β”‚  β”‚                   β”‚  β”‚                            β”‚  β”‚
β”‚  β”‚  AgentParser      β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚
β”‚  β”‚  AgentDefinition  β”‚  β”‚  β”‚ Azure OpenAI Client  β”‚  β”‚  β”‚
β”‚  β”‚  AgentScope       β”‚  β”‚  β”‚ Ollama Client        β”‚  β”‚  β”‚
β”‚  β”‚  AgentCollection  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  ToolRegistry                                     β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚  β”‚
β”‚  β”‚  β”‚ shell_executeβ”‚ β”‚ file_read  β”‚ β”‚ file_write  β”‚ β”‚  β”‚
β”‚  β”‚  β”‚ file_edit    β”‚ β”‚file_search β”‚ β”‚ grep_search β”‚ β”‚  β”‚
β”‚  β”‚  β”‚directory_listβ”‚ β”‚git_ops     β”‚ β”‚ web_fetch   β”‚ β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚  β”‚
β”‚  β”‚                                                   β”‚  β”‚
β”‚  β”‚  ToolSafety (approval logic)                      β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components

ConfigManager

Location: Pga.Core/Configuration/ConfigManager.cs

Manages the ~/.powergentic/ configuration files. Supports JSON (.json) and YAML (.yaml, .yml) formats via pluggable IConfigProvider implementations (JsonConfigProvider, YamlConfigProvider). Handles:

  • Loading configuration from config.json, config.yaml, or config.yml (with defaults if no file exists)
  • Merging local override files (config.local.json, config.local.yaml, config.local.yml) on top of the base config
  • Saving configuration in the same format as the source file
  • Profile CRUD operations (upsert, remove, get)
  • Profile resolution (CLI override β†’ agent preference β†’ auto-select β†’ default)
  • Validation

AgentLoader

Location: Pga.Core/Agents/AgentLoader.cs

Discovers and loads agent definitions from a project directory:

  1. Loads AGENTS.md (global instructions)
  2. Loads .powergentic/agents/*.agent.md (root-level named agents)
  3. Loads .github/agents/*.agent.md (GitHub convention)
  4. Recursively discovers scoped agents in subdirectories

Returns an AgentCollection with resolution logic.

AgentMarkdownParser

Location: Pga.Core/Agents/AgentMarkdownParser.cs

Parses agent markdown files:

  • Extracts YAML frontmatter (between --- delimiters)
  • Deserializes frontmatter into agent metadata (name, description, profile, tools)
  • Returns AgentDefinition objects

ChatOrchestrator

Location: Pga.Core/Chat/ChatOrchestrator.cs

The core engine that manages the LLM conversation loop:

  1. Builds a system prompt from global + agent-specific instructions
  2. Creates an IChatClient via LlmProviderFactory
  3. Configures function-calling middleware (via ChatClientBuilder.UseFunctionInvocation)
  4. Processes user messages through the LLM
  5. Handles tool call iterations (up to 25 per message)
  6. Fires events for UI feedback (OnToolInvocation, OnToolResult, OnStreamingToken, OnToolApprovalNeeded)

LlmProviderFactory

Location: Pga.Core/Providers/LlmProviderFactory.cs

Static factory that creates IChatClient instances from profile configurations:

Provider Implementation
azure-openai AzureOpenAIClient β†’ .GetChatClient() β†’ .AsIChatClient()
azure-ai-foundry Same as azure-openai
ollama OllamaApiClient (implements IChatClient natively)

ToolRegistry

Location: Pga.Core/Tools/ToolRegistry.cs

Registry of all available tools. Provides:

  • Registration and lookup by name
  • Filtered AITool lists (whitelist/blacklist support for agents)
  • CreateDefault() factory that registers all 9 built-in tools

ToolSafety

Location: Pga.Core/Tools/ToolSafety.cs

Enforces tool execution safety:

  • Checks each tool invocation against the configured safety mode
  • Calls the approval callback when prompting is required
  • Three modes: auto-approve, prompt-writes, prompt-always

ConsoleRenderer

Location: Pga.Cli/Rendering/ConsoleRenderer.cs

Static class handling all terminal output via Spectre.Console:

  • Banner, prompts, assistant messages, tool calls
  • Color-coded feedback (success/error/warning/info)
  • Tool approval prompts
  • Help and status displays

Data Flow

Interactive Chat Session

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
User types message
        β”‚
        β–Ό
  ChatCommand.RunInteractiveChat()
        β”‚
        β–Ό
  ChatOrchestrator.SendMessageAsync(input)
        β”‚
        β”œβ”€β”€ 1. Add user message to MessageHistory
        β”‚
        β”œβ”€β”€ 2. Build IChatClient with tool-calling middleware
        β”‚       └── LlmProviderFactory.CreateChatClient(profile)
        β”‚
        β”œβ”€β”€ 3. Call IChatClient.GetResponseAsync(messages, options)
        β”‚       └── options include AITool list from ToolRegistry
        β”‚
        β”œβ”€β”€ 4. Process response
        β”‚       β”œβ”€β”€ If tool calls requested:
        β”‚       β”‚   β”œβ”€β”€ Fire OnToolInvocation event
        β”‚       β”‚   β”œβ”€β”€ Check ToolSafety (may fire OnToolApprovalNeeded)
        β”‚       β”‚   β”œβ”€β”€ Execute tool function
        β”‚       β”‚   β”œβ”€β”€ Fire OnToolResult event
        β”‚       β”‚   β”œβ”€β”€ Add tool result to messages
        β”‚       β”‚   └── Loop back to step 3 (up to 25 iterations)
        β”‚       β”‚
        β”‚       └── If text response:
        β”‚           └── Return response text
        β”‚
        β–Ό
  ConsoleRenderer.RenderAssistantMessage(response)

Profile Resolution

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
--profile CLI flag?  ──Yes──▢  Use specified profile
        β”‚
        No
        β”‚
        β–Ό
Agent has profile field?  ──Yes──▢  Use agent's profile
        β”‚
        No
        β”‚
        β–Ό
Auto-select enabled?  ──Yes──▢  Evaluate rules, use first match
        β”‚
        No
        β”‚
        β–Ό
Use defaultProfile from config

Microsoft.Extensions.AI Integration

PGA is built on the Microsoft.Extensions.AI abstraction layer, which provides:

  • IChatClient β€” Unified interface for all LLM providers
  • ChatMessage / ChatRole β€” Message representation
  • AIFunction / AITool β€” Function-calling abstractions
  • FunctionCallContent / FunctionResultContent β€” Tool call/result types
  • ChatClientBuilder.UseFunctionInvocation() β€” Automatic tool-calling middleware

Info

This means PGA can support any LLM provider that implements IChatClient, making it straightforward to add new providers in the future.


Adding a New Tool

To add a new built-in tool:

  1. Create a class implementing IAgentTool in Pga.Core/Tools/:
C#
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class MyNewTool : IAgentTool
{
    public string Name => "my_new_tool";
    public string Description => "Description of what the tool does";
    public ToolSafetyLevel SafetyLevel => ToolSafetyLevel.ReadOnly;

    public AIFunction ToAIFunction()
    {
        return AIFunctionFactory.Create(
            (string param1, int param2) =>
            {
                // Tool implementation
                return "result";
            },
            name: Name,
            description: Description);
    }
}
  1. Register it in ToolRegistry.CreateDefault():
C#
1
registry.Register(new MyNewTool());

Adding a New LLM Provider

To add a new LLM provider:

  1. Add the provider's NuGet package to Pga.Core
  2. Add a new case in LlmProviderFactory.CreateChatClient():
C#
1
"my-provider" => CreateMyProviderClient(profile),
  1. Implement the factory method that returns an IChatClient
  2. Add the necessary fields to LlmProfile (with [JsonPropertyName] attributes)
  3. Add validation in LlmProfile.Validate()
  4. Update the interactive wizard in ConfigCommand.CreateAddProfileCommand()

Build Configuration

Directory.Build.props

Shared settings applied to all projects:

Setting Value
TargetFramework net10.0
ImplicitUsings enable
Nullable enable
LangVersion latest

Pga.Cli Publish Settings

Setting Value Purpose
PublishSingleFile true Single executable output
SelfContained true Includes .NET runtime
PublishTrimmed true Removes unused code
AssemblyName pga Binary is named pga