function_test.go

Overview

The function_test.go file contains unit and example tests for the Function Tool functionality within the system. It verifies the ability of the functiontool package to wrap native Go functions as tools that can be invoked by an AI agent or large language model (LLM). The tests cover creation, execution, JSON schema customization, input/output handling, error cases, and integration with Gemini-based LLM models.

This file ensures that function tools correctly:

The tests simulate real-world usage scenarios of function tools in the context of an LLM-driven agent workflow, validating their behavior end-to-end.


Detailed Explanations

ExampleNew()

A concise usage example demonstrating how to create a simple sum function tool.

func ExampleNew()

TestFunctionTool_Simple()

func TestFunctionTool_Simple(t *testing.T)

TestFunctionTool_DifferentFunctionDeclarations_ConsolidatedInOneGenAiTool()

func TestFunctionTool_DifferentFunctionDeclarations_ConsolidatedInOneGenAiTool(t *testing.T)

TestFunctionTool_ReturnsBasicType()

func TestFunctionTool_ReturnsBasicType(t *testing.T)

TestFunctionTool_CustomSchema()

func TestFunctionTool_CustomSchema(t *testing.T)

Helper Functions

newGeminiTestClientConfig()

func newGeminiTestClientConfig(t *testing.T, rrfile string) *genai.ClientConfig

readFirstResponseT any

func readFirstResponse[T any](s iter.Seq2[*model.LLMResponse, error]) (T, error)

toolDeclaration()

func toolDeclaration(cfg *genai.GenerateContentConfig) *genai.FunctionDeclaration

stringify()

func stringify(v any) string

Important Implementation Details


File Interaction with Other Components


Visual Diagram

flowchart TD
A[Create Function Tool] --> B[Infer JSON Schemas]
B --> C[ProcessRequest embeds function declaration in LLMRequest]
C --> D[Send LLMRequest to Gemini Model]
D --> E[LLM generates content with FunctionCall]
E --> F[Extract FunctionCall from LLM response]
F --> G[FunctionTool.Run deserializes arguments]
G --> H[Invoke wrapped Go function handler]
H --> I[Return result or error]
I --> J[Serialize output to JSON map]
J --> K[Return result to agent and LLM]
K --> L[Agent appends results and continues]
subgraph Test Scenarios
M[TestFunctionTool_Simple]
N[TestFunctionTool_DifferentFunctionDeclarations]
O[TestFunctionTool_ReturnsBasicType]
P[TestFunctionTool_CustomSchema]
end
M -.-> A
N -.-> A
O -.-> A
P -.-> A

This flowchart illustrates the lifecycle of a function tool within the test scenarios, highlighting creation, schema inference, request processing, LLM interaction, function invocation, and result serialization.


Usage Summary

This file is essential for validating the robustness and correctness of function tool integration within the AI agent tooling framework. It ensures that native Go logic can be safely and effectively exposed as callable tools for LLM-driven agents.


Relevant Topics