contents_processor.go

Overview

The contents_processor.go file is responsible for populating the Contents field of an LLMRequest based on the invocation context and session event history. It filters, rearranges, and processes session events to build a coherent context for large language model (LLM) requests, supporting multi-agent interaction, function call/response merging, and selective content inclusion policies.

This file primarily interacts with:

Its main goal is to prepare the conversation or tool call context properly for LLM consumption, including merging asynchronous function call responses and handling multi-agent scenarios.


Key Functions and Methods

ContentsRequestProcessor

func ContentsRequestProcessor(ctx agent.InvocationContext, req *model.LLMRequest) error

buildContentsDefault

func buildContentsDefault(agentName, invocationBranch string, events []*session.Event) ([]*genai.Content, error)

buildContentsCurrentTurnContextOnly

func buildContentsCurrentTurnContextOnly(agentName, branch string, events []*session.Event) ([]*genai.Content, error)

rearrangeEventsForLatestFunctionResponse

func rearrangeEventsForLatestFunctionResponse(events []*session.Event) ([]*session.Event, error)

rearrangeEventsForFunctionResponsesInHistory

func rearrangeEventsForFunctionResponsesInHistory(events []*session.Event) ([]*session.Event, error)

mergeFunctionResponseEvents

func mergeFunctionResponseEvents(functionResponseEvents []*session.Event) (*session.Event, error)

ConvertForeignEvent

func ConvertForeignEvent(ev *session.Event) *session.Event

Helper Functions


Important Implementation Details and Algorithms


Interaction with Other Components


Visual Diagram: Flowchart of Main Functions and Their Relationships

flowchart TD
CRP[ContentsRequestProcessor]
BCD[buildContentsDefault]
BCC[buildContentsCurrentTurnContextOnly]
RAF[rearrangeEventsForLatestFunctionResponse]
RAFH[rearrangeEventsForFunctionResponsesInHistory]
MFR[mergeFunctionResponseEvents]
CFE[ConvertForeignEvent]
CRP -->|decides function| BCD
CRP -->|decides function| BCC
BCD --> RAF
BCD --> RAFH
RAF --> MFR
RAFH --> MFR
BCD --> CFE