utils

package
v2.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendInstructions

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

AppendInstructions appends instructions to the genai.GenerateContentConfig.SystemInstruction system instruction.

func CanonicalSchemaJSON

func CanonicalSchemaJSON(s *jsonschema.Schema) ([]byte, error)

CanonicalSchemaJSON marshals the schema to JSON, parses it back, and re-emits it with object keys sorted alphabetically (recursively). Arrays keep their original order, except the unordered set keywords (see unorderedArrayKeywords), whose elements are sorted so that equivalent schemas canonicalize identically.

func Content

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

Content is a convenience function that returns the genai.Content in the event.

func EventBelongsToBranch added in v2.3.0

func EventBelongsToBranch(invocationBranch, eventBranch string) bool

EventBelongsToBranch reports whether an event on eventBranch is visible to an invocation running on invocationBranch.

An event belongs to its own branch and to every descendant of it, so a child agent sees what its parent said and not the other way round. Branch nodes are delimited with a dot, and the prefix match requires that dot so that "agent_0" does not match "agent_00".

The single definition, because prompt assembly and anything reasoning about what a prompt contains have to agree on it.

func FunctionCalls

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

Belows are useful utilities that help working with genai.Content included in types.Event. TODO: Use generics. FunctionCalls extracts all FunctionCall parts from the content.

func FunctionDecls

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

FunctionDecls extracts all Function declarations from the GenerateContentConfig.

func FunctionResponses

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

FunctionResponses extracts all FunctionResponse parts from the content.

func GenerateFunctionCallID

func GenerateFunctionCallID(ctx context.Context) string

GenerateFunctionCallID generates a new function call ID. The ID is obtained through the platform package, so a UUID provider installed on ctx (see platform.WithUUIDProvider) controls it.

func IsProsePart added in v2.3.0

func IsProsePart(p *genai.Part) bool

IsProsePart reports whether p is plain text meant to be read, and nothing else.

Exactly one field of a genai.Part is meant to be set, so a part carrying any of the actionable payloads is not prose whatever else is on it. Callers that filter on this drop such a part rather than reducing it to its text: the text is not what makes it dangerous, and dropping is the conservative half of the choice.

A thought is not prose either. It is the model's private reasoning rather than anything it chose to say, and it should not be stored or replayed as though the model had said it.

func Must

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

func PopulateClientFunctionCallID

func PopulateClientFunctionCallID(ctx context.Context, c *genai.Content)

PopulateClientFunctionCallID sets the function call ID field if it is empty. Since the ID field is optional, some models don't fill the field, but the LLMAgent depends on the IDs to map FunctionCall and FunctionResponse events in the event stream.

func RemoveClientFunctionCallID

func RemoveClientFunctionCallID(c *genai.Content)

RemoveClientFunctionCallID removes the function call ID field that was set by populateClientFunctionCallID. This is necessary when FunctionCall or FunctionResponse are sent back to the model.

func TextParts

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

TextParts extracts all Text parts from the content.

func ValidateMapOnSchema

func ValidateMapOnSchema(args map[string]any, schema *genai.Schema, isInput bool) error

ValidateMapOnSchema validates a map against a schema.

func ValidateOutputSchema

func ValidateOutputSchema(output string, schema *genai.Schema) (map[string]any, error)

ValidateOutputSchema validates an output JSON string against a schema.

Types

This section is empty.