loader.go

Overview

The loader.go file defines the Loader interface and two concrete implementations—singleLoader and multiLoader—for managing and retrieving AI agents in the system by their names. This abstraction enables flexible loading and access to agents, whether there is a single root agent or multiple named agents managed together.

The primary responsibilities of this file are:

This file plays a critical role in the AI Agent Framework by enabling agent discovery and management, supporting higher-level workflows that invoke agents dynamically by name.


Interfaces and Types

Loader Interface

type Loader interface {
    ListAgents() []string
    LoadAgent(name string) (Agent, error)
    RootAgent() Agent
}

singleLoader

type singleLoader struct {
    root Agent
}

Methods

Usage Example

rootAgent := ... // some Agent instance
loader := NewSingleLoader(rootAgent)

names := loader.ListAgents() // returns []string{rootAgent.Name()}
agent, err := loader.LoadAgent("") // returns rootAgent

multiLoader

type multiLoader struct {
    agentMap map[string]Agent
    root     Agent
}

Construction

func NewMultiLoader(root Agent, agents ...Agent) (Loader, error)

Methods

Usage Example

rootAgent := ... // Agent instance
agent1 := ...    // Agent instance
agent2 := ...    // Agent instance

loader, err := NewMultiLoader(rootAgent, agent1, agent2)
if err != nil {
    // handle duplicate name error
}

allNames := loader.ListAgents() // returns slice with all agent names
agent, err := loader.LoadAgent("agent1") // returns agent1 instance

Important Implementation Details


Interaction with Other System Components


Mermaid Diagram: Loader Structure and Relationships

classDiagram
class Loader {
+ListAgents()
+LoadAgent()
+RootAgent()
}
class singleLoader {
-root: Agent
+ListAgents()
+LoadAgent()
+RootAgent()
}
class multiLoader {
-agentMap: map[string]Agent
-root: Agent
+ListAgents()
+LoadAgent()
+RootAgent()
}
Loader <|.. singleLoader
Loader <|.. multiLoader

Summary

The loader.go file provides a clear and extendable abstraction for managing collections of agents, facilitating agent discovery and retrieval by name. The distinction between single and multi-agent loaders simplifies usage depending on application complexity. Error handling ensures robustness when accessing agents dynamically.

For detailed information about the Agent interface and its lifecycle, refer to the subtopics: