main.go

Overview

This file implements a simple example application that demonstrates how to create and run AI agents using the provided agent framework. It defines two custom sub-agents (myAgent), each generating greeting messages asynchronously. These sub-agents are composed into a single parallel agent that runs both sub-agents concurrently. The overall agent is then launched via a launcher infrastructure that handles execution lifecycle and command-line argument parsing.

The main purpose of the file is to illustrate:

Key Components

main() function

The entry point of the application. It performs the following steps:

  1. Creates two sub-agents (subAgent1 and subAgent2) using the agent.New constructor:

    • Each sub-agent is configured with a unique name, description, and a run function defined by myAgent{id: N}.Run.

    • Errors during creation are handled by logging and terminating the program.

  2. Creates a parallel agent (parallelAgent) using parallelagent.New:

    • This agent runs the two sub-agents concurrently.

    • The parallel agent itself is configured with a name, description, and the list of sub-agents.

    • Again, any creation error causes a fatal log.

  3. Prepares a launcher configuration with the parallel agent loaded through agent.NewSingleLoader.

  4. Instantiates a full-featured launcher (full.NewLauncher) and executes it with the context and configuration:

    • Passes command-line arguments (excluding the program name).

    • If the launch fails, logs the error and the expected command-line syntax.

Parameters:

Usage:

go run main.go [launcher options]

myAgent struct and Run method

myAgent is a custom agent type defined with a single field:

type myAgent struct {
    id int
}

This field identifies the agent instance.

Run method

Signature:

func (a myAgent) Run(ctx agent.InvocationContext) iter.Seq2[*session.Event, error]

Behavior:

Parameters:

Return:

Example event content:

Hello from MyAgent id: 1!

Usage:
The framework calls Run, which then asynchronously generates events for session consumption.


Implementation Details and Algorithms


Interactions with Other System Components

The file acts as a top-level orchestrator that ties these components together to demonstrate a parallel-agent use case.


Diagram: Agent Structure and Execution Flow

flowchart TD
Main["main()"]
subAgent1["myAgent{id:1}\nRun()"]
subAgent2["myAgent{id:2}\nRun()"]
parallelAgent["parallelAgent\n(parallelagent)"]
launcher["Launcher\n(full)"]
Main --> subAgent1
Main --> subAgent2
subAgent1 --> parallelAgent
subAgent2 --> parallelAgent
parallelAgent --> launcher
launcher -->|Executes| parallelAgent

Summary of Public API Elements

Element

Type

Description

main()

func()

Entry point, creates agents and launches them.

myAgent

struct

Custom agent with an integer ID.

myAgent.Run

func(agent.InvocationContext) iter.Seq2[*session.Event,error]

Generates a sequence of greeting events asynchronously.


References