contents_processor_test.go

Overview

This file contains comprehensive unit tests for the ContentsRequestProcessor function and related content processing utilities within the llminternal package. Its primary focus is to validate the behavior of how session events and their associated LLM-generated content are transformed into a sequence of genai.Content items that populate an LLMRequest. These tests cover multiple scenarios including content filtering by agent settings (IncludeContents), branch filtering, event conversion for foreign agents, and complex rearrangements of function call/response events.

The file is essential for ensuring the correctness of the content preparation pipeline, which is a critical step in generating accurate and context-aware prompts for language model invocations during agent execution. It also tests the conversion of non-local events ("foreign" events) into user-context content, preserving conversational context in multi-agent environments.


Detailed Explanations

Types

testModel

type testModel struct {
	model.LLM
}

fakeSession

type fakeSession struct {
	events []*session.Event
}

Functions and Methods

TestContentsRequestProcessor_IncludeContents(t *testing.T)

TestContentsRequestProcessor(t *testing.T)

TestConvertForeignEvent(t *testing.T)

TestContentsRequestProcessor_NonLLMAgent(t *testing.T)

TestContentsRequestProcessor_Rearrange(t *testing.T)

NewContentFromFunctionCall(fc *genai.FunctionCall, role string) *genai.Content

NewContentFromFunctionResponse(fr *genai.FunctionResponse, role string) *genai.Content


Important Implementation Details


Interaction with Other System Components

The file does not itself implement the ContentsRequestProcessor but exercises it extensively with mocks and fake sessions, validating its logic.


Usage Examples

Basic Test Case Example

t.Run("helloAndGoodBye", func(t *testing.T) {
    testAgent := utils.Must(llmagent.New(llmagent.Config{
        Name: "testAgent",
        Model: &testModel{},
        IncludeContents: "default",
    }))

    ctx := icontext.NewInvocationContext(t.Context(), icontext.InvocationContextParams{
        Agent: testAgent,
        Session: &fakeSession{events: helloAndGoodBye},
    })

    req := &model.LLMRequest{}
    if err := llminternal.ContentsRequestProcessor(ctx, req); err != nil {
        t.Fatalf("contentsRequestProcessor failed: %v", err)
    }
    got := req.Contents
    want := []*genai.Content{
        genai.NewContentFromText("hello", "user"),
        genai.NewContentFromText("good bye", "user"),
    }
    if diff := cmp.Diff(want, got); diff != "" {
        t.Errorf("Contents mismatch (-want +got):\n%s", diff)
    }
})

Visual Diagram

flowchart TD
Start[Test Function] --> SetupAgent[Create Agent with Config]
SetupAgent --> SetupContext[Create Invocation Context]
SetupContext --> SetupSession[Create Fake Session with Events]
SetupSession --> CallProcessor[Call ContentsRequestProcessor]
CallProcessor --> VerifyOutput[Compare LLMRequest.Contents with Expected]
VerifyOutput --> End[Test Pass/Fail]
subgraph Test Loop
SetupAgent
SetupContext
SetupSession
CallProcessor
VerifyOutput
end
TestCases[Multiple Test Cases] --> Test Loop

This flowchart illustrates the common workflow of test cases in this file:


References