utils.go

Overview

The utils.go file provides a collection of utility functions designed to facilitate working with genai.Content messages and related types within the AI agent framework. These utilities primarily support handling function call and response IDs, extracting specific parts from genai.Content, and augmenting LLM requests with system instructions. The file serves as a helper layer to streamline interactions with the content structures used in event streams and LLM agent requests, ensuring reliable ID management and convenient content manipulation.


Functions and Methods

PopulateClientFunctionCallID

func PopulateClientFunctionCallID(c *genai.Content)

RemoveClientFunctionCallID

func RemoveClientFunctionCallID(c *genai.Content)

Content

func Content(ev *session.Event) *genai.Content

FunctionCalls

func FunctionCalls(c *genai.Content) []*genai.FunctionCall

FunctionResponses

func FunctionResponses(c *genai.Content) []*genai.FunctionResponse

TextParts

func TextParts(c *genai.Content) []string

FunctionDecls

func FunctionDecls(c *genai.GenerateContentConfig) []*genai.FunctionDeclaration

Must

func Must[T agent.Agent](a T, err error) T

AppendInstructions

func AppendInstructions(r *model.LLMRequest, instructions ...string)

Important Implementation Details


Interaction with Other System Components


Flowchart of Main Utilities and Their Relationships

flowchart TD
A[Content from session.Event]
B[Extract FunctionCalls]
C[Extract FunctionResponses]
D[Extract TextParts]
E[Populate FunctionCall IDs]
F[Remove FunctionCall IDs]
G[Extract FunctionDecls from GenerateContentConfig]
H[Append Instructions to LLMRequest]
I["Must (Agent Helper)"]
A --> B
A --> C
A --> D
B --> E
B & C --> F
G --> H
I --> H

Usage Examples

Assigning FunctionCall IDs Before Sending to Agent

content := getGenAIContent()
utils.PopulateClientFunctionCallID(content)
// Now content function calls have unique IDs for event tracking

Removing Client-Generated IDs Before Returning to Model

content := getGenAIContent()
utils.RemoveClientFunctionCallID(content)
// Client IDs cleared to avoid leaking internal IDs

Extracting Text from Content

textSegments := utils.TextParts(content)
for _, text := range textSegments {
    fmt.Println(text)
}

Appending Instructions to LLMRequest

req := &model.LLMRequest{}
utils.AppendInstructions(req, "First instruction", "Second instruction")
// req.Config.SystemInstruction now contains appended instructions

Using Must for Agent Creation

agent, err := agent.NewMyAgent(params)
agent = utils.Must(agent, err)
// agent is guaranteed non-nil, or panic occurred

This file is central to handling content-related operations in the AI agent ecosystem, ensuring consistent ID management, part extraction, and request augmentation, thereby supporting the robust functioning of agents, sessions, and LLM interactions.

For more detailed concepts on agent lifecycle and invocation, see Agent Lifecycle and Callbacks and LLM Integration and Agents. For session-related event handling, refer to Session Management.