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 | |
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 OpenAIIChatClientadapterAzure.AI.OpenAIβ Azure OpenAI client SDKAzure.Identityβ Azure Entra ID authenticationOllamaSharpβ OllamaIChatClientimplementationMarkdigβ Markdown processingYamlDotNetβ YAML frontmatter parsingMicrosoft.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 routingSpectre.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 | |
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, orconfig.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:
- Loads
AGENTS.md(global instructions) - Loads
.powergentic/agents/*.agent.md(root-level named agents) - Loads
.github/agents/*.agent.md(GitHub convention) - 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
AgentDefinitionobjects
ChatOrchestrator¶
Location: Pga.Core/Chat/ChatOrchestrator.cs
The core engine that manages the LLM conversation loop:
- Builds a system prompt from global + agent-specific instructions
- Creates an
IChatClientviaLlmProviderFactory - Configures function-calling middleware (via
ChatClientBuilder.UseFunctionInvocation) - Processes user messages through the LLM
- Handles tool call iterations (up to 25 per message)
- 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
AIToollists (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 | |
Profile Resolution¶
| Text Only | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Microsoft.Extensions.AI Integration¶
PGA is built on the Microsoft.Extensions.AI abstraction layer, which provides:
IChatClientβ Unified interface for all LLM providersChatMessage/ChatRoleβ Message representationAIFunction/AIToolβ Function-calling abstractionsFunctionCallContent/FunctionResultContentβ Tool call/result typesChatClientBuilder.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:
- Create a class implementing
IAgentToolinPga.Core/Tools/:
| C# | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
- Register it in
ToolRegistry.CreateDefault():
| C# | |
|---|---|
1 | |
Adding a New LLM Provider¶
To add a new LLM provider:
- Add the provider's NuGet package to
Pga.Core - Add a new case in
LlmProviderFactory.CreateChatClient():
| C# | |
|---|---|
1 | |
- Implement the factory method that returns an
IChatClient - Add the necessary fields to
LlmProfile(with[JsonPropertyName]attributes) - Add validation in
LlmProfile.Validate() - 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 |