test_agent_runner.go

Overview

The test_agent_runner.go file provides utilities for testing agents that interact with large language models (LLMs) within a session-based framework. It defines the TestAgentRunner type to simplify creating, managing, and running agent sessions with configurable initial states, as well as a MockModel to simulate LLM model behavior for controlled testing scenarios. The file also includes helper functions to collect events, parts, and text parts from streamed session events.

This code is crucial for unit and integration testing of agents in the system, facilitating controlled execution flows and validation of agent behavior without depending on live LLM backends or persistent storage.


Detailed Explanation

TestAgentRunner

TestAgentRunner is a helper struct designed to run agent sessions in tests with minimal boilerplate. It encapsulates an agent, session service, runner, and session state management.

Structure

type TestAgentRunner struct {
    agent            agent.Agent
    sessionService   session.Service
    lastSession      session.Session
    initSessionState map[string]any
    appName          string
    runner           *runner.Runner
}

Methods

Constructor

Usage Example

func TestAgent(t *testing.T) {
    ag := NewMyAgent() // agent.Agent implementation
    runner := NewTestAgentRunner(t, ag)

    eventsStream := runner.Run(t, "session1", "Hello, agent!")
    events, err := CollectEvents(eventsStream)
    if err != nil {
        t.Fatal(err)
    }
    // Assert on events
}

MockModel

MockModel is a mock implementation of the model.LLM interface, simulating an LLM for testing without external calls.

Structure

type MockModel struct {
    Requests             []*model.LLMRequest
    Responses            []*genai.Content
    StreamResponsesCount int
}

Methods

Errors

Usage

mock := &MockModel{
    Responses: []*genai.Content{
        genai.NewContentFromText("Hello from mock", genai.RoleAssistant),
    },
}

resp, err := mock.Generate(ctx, req)

Utility Functions for Event Collection

These functions simplify collecting session events or content parts from streaming session responses.


Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Mermaid Diagram

classDiagram
class TestAgentRunner {
-agent: Agent
-sessionService: SessionService
-lastSession: Session
-initSessionState: map[string]any
-appName: string
-runner: Runner
+session()
+SetInitSessionState()
+Run()
+RunContent()
+RunContentWithConfig()
}
class MockModel {
-Requests: []*LLMRequest
-Responses: []*Content
-StreamResponsesCount: int
+GenerateContent()
+Generate()
+GenerateStream()
+Name()
}
class Utilities {
+CollectEvents()
+CollectParts()
+CollectTextParts()
}
TestAgentRunner --> "1" agent.Agent
TestAgentRunner --> "1" session.Service
TestAgentRunner --> "1" runner.Runner
MockModel ..|> model.LLM
Utilities ..> session.Event
Utilities ..> genai.Part

This documentation references concepts such as the agent and runner system (Agent Execution Runner), session management (Session Management), and LLM integration (LLM Integration and Agents) for detailed background on those components.