configloader

package
v0.100.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 5 Imported by: 0

README

AI Config Loader (ai/configloader)

The configloader package provides a unified, simple YAML configuration file loader, designed for AI modules, supporting caching and path fallback mechanisms.

Architecture

classDiagram
    class Loader {
        -baseDir string
        -cache sync.Map
        +Load(subPath, target)
        +LoadCached(subPath, factory)
        +LoadDir(subDir, factory)
        -ReadFileWithFallback(path)
    }
  • Loader: Core struct holding a base directory (baseDir) and a thread-safe cache (sync.Map).
  • Caching Mechanism: Loaded configuration objects are cached in memory to avoid repeated disk reads and YAML parsing.
  • Fallback Mechanism: Considering differences between development and deployment environments, the loader supports automatic path fallback lookup.

Algorithm

flowchart TD
    Start(LoadCached Request) --> Check{In Cache?}
    Check -- Yes --> Return[Return Cached Object]
    Check -- No --> Read{Read File}
    Read -- Try baseDir --> Found1{Found?}
    Found1 -- Yes --> Parse[YAML Unmarshal]
    Found1 -- No --> Fallback{Try ExecDir}
    Fallback -- Found --> Parse
    Fallback -- Not Found --> Error[Return Error]
    Parse --> Store[Store in Cache]
    Store --> Return
  1. Request Config: Call LoadCached(path, factory).
  2. Check Cache: Check if memory cache already has the config object. Return directly if yes.
  3. Read File:
    • First try reading baseDir/path.
    • If failed (e.g., in test or binary running environment), try looking up relative to executable file.
  4. Parse: Use yaml.v3 to Unmarshal file content into target struct created by factory function.
  5. Cache Fill: Store successfully parsed object in cache for next use.

Methods

Method Description
Load Load single YAML file
LoadCached Load with caching
LoadDir Load all YAML files from directory
ClearCache Clear configuration cache

Use Case

Used to load Agent Prompt templates, model configurations, tool definitions and other static resource files, ensuring resources can be found correctly in different running environments.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader is a unified configuration loader for AI-related YAML files.

func NewLoader

func NewLoader(baseDir string) *Loader

NewLoader creates a new configuration loader.

func (*Loader) ClearCache

func (l *Loader) ClearCache()

ClearCache clears the configuration cache.

func (*Loader) Load

func (l *Loader) Load(subPath string, target any) error

Load loads a single YAML file and unmarshals it into target.

func (*Loader) LoadCached

func (l *Loader) LoadCached(subPath string, factory func() any) (any, error)

LoadCached loads a configuration with caching. If the file is already cached, returns the cached value. Otherwise, calls factory to create the target and caches it.

func (*Loader) LoadDir

func (l *Loader) LoadDir(subDir string, factory func(path string) (any, error)) (map[string]any, error)

LoadDir loads all YAML files from a directory. The factory function is called for each file to create the target struct.

func (*Loader) ReadFileWithFallback

func (l *Loader) ReadFileWithFallback(path string) ([]byte, error)

ReadFileWithFallback tries to read file from path relative to baseDir, then falls back to executable directory for production builds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL