long_running_function_test.go

Overview

This file contains automated tests for validating the behavior and integration of long-running function tools within the agent framework. Specifically, it focuses on the creation, execution, and event lifecycle of tools flagged as long-running operations via the functiontool package. The tests simulate interactions with a mock LLM model and an LLM agent (llmagent), verifying that function calls, responses, and event streams correctly reflect the expected long-running flow.

The tests employ generics, mock utilities, and event collectors to verify the correctness of function invocation, ID propagation, and multi-step workflows typical in asynchronous or deferred processing scenarios.

The file primarily tests the interaction between:

Detailed Explanation of Classes, Functions, and Methods

1. TestNewLongRunningFunctionTool(t *testing.T)

Purpose:
Verifies that a function tool configured as long-running is created correctly with the expected name, description, and long-running flag.

Key Steps:

Usage Example:

handler := func(ctx tool.Context, input SumArgs) (SumResult, error) {
    return SumResult{Result: "Processing sum"}, nil
}
sumTool, err := functiontool.New(functiontool.Config{
    Name: "sum",
    Description: "sums two integers",
    IsLongRunning: true,
}, handler)

2. NewContentFromFunctionResponseWithID(name string, response map[string]any, id, role string) *genai.Content

Purpose:
Constructs a genai.Content object from a function response, manually assigning an ID and role to the response part.

Parameters:

Returns:
A pointer to a genai.Content instance with the ID set on the function response part.

Usage: Used primarily in tests to simulate function responses with specific IDs, aiding in event stream validation.


3. TestLongRunningFunctionFlow(t *testing.T)

Purpose:
Tests the general workflow of a long-running function tool whose handler returns a map[string]string result indicating a status.

Details:


4. TestLongRunningStringFunctionFlow(t *testing.T)

Purpose:
Similar to TestLongRunningFunctionFlow, but tests a long-running function tool whose handler returns a string result instead of a map.

Details:


5. testLongRunningFunctionFlow[Out any](t *testing.T, increaseByOne func(ctx tool.Context, x IncArgs) (Out, error), resultKey string, callCount *int)

Purpose:
Generic test helper to execute and verify the workflow of a long-running function tool with any output type.

Parameters:

Workflow:

  1. Creates a slice of mock genai.Content responses simulating function call and subsequent LLM text responses.

  2. Creates a mock LLM model (testutil.MockModel) initialized with these responses.

  3. Creates a long-running function tool with the given handler.

  4. Instantiates an LLM agent (llmagent) with the mock model and tool.

  5. Runs the agent via a test runner (testutil.NewTestAgentRunner).

  6. Collects and validates the initial event stream and LLM requests.

  7. Extracts the function call event ID to simulate follow-up function responses with different result contents.

  8. Runs multiple subtests simulating continued polling of the function result, checking:

    • Number of LLM requests issued,

    • Event stream contents,

    • Proper merging and propagation of function call IDs and responses.

  9. Verifies the handler is only called once, confirming no redundant executions.

Key Assertions:


6. TestLongRunningToolIDsAreSet(t *testing.T)

Purpose:
Verifies that the long-running function tool correctly sets unique IDs on function call events and that these IDs are propagated properly in the agent event stream.

Workflow:


Important Implementation Details and Algorithms


Interaction with Other Parts of the System

This file verifies and validates how these components cooperate during the execution of long-running functions in an agent-driven environment, ensuring that IDs, event streams, and tool invocations behave as expected.


Visual Diagram of File Structure and Workflow

flowchart TD
TestNewLongRunningFunctionTool -->|Creates| functiontool.New
TestLongRunningFunctionFlow --> testLongRunningFunctionFlow
TestLongRunningStringFunctionFlow --> testLongRunningFunctionFlow
testLongRunningFunctionFlow -->|Uses| MockModel
testLongRunningFunctionFlow -->|Creates| longRunningTool
testLongRunningFunctionFlow -->|Creates| llmagent.New
testLongRunningFunctionFlow -->|Runs| TestAgentRunner.Run
testLongRunningFunctionFlow -->|Validates| EventStream
TestLongRunningToolIDsAreSet -->|Creates| longRunningTool
TestLongRunningToolIDsAreSet -->|Creates| llmagent.New
TestLongRunningToolIDsAreSet -->|Runs| TestAgentRunner.Run
TestLongRunningToolIDsAreSet -->|Validates| EventsWithIDs
NewContentFromFunctionResponseWithID -->|Helper for| testLongRunningFunctionFlow

Summary of Key Functions and Their Relationships

This file ensures that the long-running function tools integrate properly with LLM agents and that event streams reflect the correct states and IDs through consecutive interactions. It plays a crucial role in validating asynchronous function invocation patterns in the agent framework.