runner.go

Overview

This file implements the Runner, a core component responsible for managing the execution lifecycle of AI agents within user sessions. The Runner coordinates user input handling, session state updates, agent selection, event streaming, and integration with artifact and memory services. It facilitates multi-agent conversations by selecting the appropriate agent from an agent hierarchy based on session history and transfer policies.

The Runner acts as the execution runtime for ADK agents, bridging user messages to agent invocations, and maintaining session consistency by appending generated events and managing artifacts. It supports streaming outputs and handles saving inline input blobs as artifacts prior to agent execution.


Main Types and Functions

Config

type Config struct {
	AppName          string
	Agent            agent.Agent
	SessionService   session.Service
	ArtifactService  artifact.Service  // optional
	MemoryService    memory.Service    // optional
}

New

func New(cfg Config) (*Runner, error)

Runner

type Runner struct {
	appName         string
	rootAgent       agent.Agent
	sessionService  session.Service
	artifactService artifact.Service
	memoryService   memory.Service
	parents         parentmap.Map
}

Run

func (r *Runner) Run(ctx context.Context, userID, sessionID string, msg *genai.Content, cfg agent.RunConfig) iter.Seq2[*session.Event, error]

appendMessageToSession

func (r *Runner) appendMessageToSession(ctx agent.InvocationContext, storedSession session.Session, msg *genai.Content, saveInputBlobsAsArtifacts bool) error

findAgentToRun

func (r *Runner) findAgentToRun(session session.Session) (agent.Agent, error)

isTransferableAcrossAgentTree

func (r *Runner) isTransferableAcrossAgentTree(agentToRun agent.Agent) bool

findAgent (helper function)

func findAgent(curAgent agent.Agent, targetName string) agent.Agent

Implementation Details and Algorithms


Interactions with Other Components


Visual Diagram of Runner Structure and Workflow

flowchart TD
Config[Config]
New["New(cfg Config)"]
Runner[Runner]
Run["Run(ctx, userID, sessionID, msg, cfg)"]
GetSession[sessionService.Get]
FindAgent[findAgentToRun]
SetupCtx[Setup InvocationContext]
AppendMsg[appendMessageToSession]
AgentRun[agent.Run]
YieldEvents[Yield Events]
AppendEvent[sessionService.AppendEvent]
ArtifactSave[Save Input Blobs as Artifacts]
TransferCheck[isTransferableAcrossAgentTree]
Config --> New --> Runner
Runner --> Run
Run --> GetSession
Run --> FindAgent
Run --> SetupCtx
Run --> AppendMsg
AppendMsg --> ArtifactSave
Run --> AgentRun
AgentRun --> YieldEvents
YieldEvents --> AppendEvent
FindAgent --> TransferCheck

References to Related Topics


This documentation describes the structure, purpose, and detailed operation of the runner.go file, which is central to orchestrating agent executions within user sessions, handling inputs, outputs, and multi-agent conversation flows.