Skip to content
Tracon

TruncatingAIFunction

Namespace Tracon · Assembly Tracon.Core.dll

Wraps an AI.AIFunction so a result over a byte limit is trimmed and handed to the model inside an envelope that states how many bytes were dropped, instead of running unbounded.

public sealed class TruncatingAIFunction : DelegatingAIFunction

object ← AITool ← AIFunctionDeclaration ← AIFunction ← DelegatingAIFunction ← TruncatingAIFunction

DelegatingAIFunction.ToString(), DelegatingAIFunction.GetService(Type, object?), DelegatingAIFunction.Name, DelegatingAIFunction.Description, DelegatingAIFunction.JsonSchema, DelegatingAIFunction.ReturnJsonSchema, DelegatingAIFunction.JsonSerializerOptions, DelegatingAIFunction.UnderlyingMethod, DelegatingAIFunction.AdditionalProperties, AIFunction.InvokeAsync(AIFunctionArguments?, CancellationToken), AIFunction.AsDeclarationOnly(), AIFunction.UnderlyingMethod, AIFunction.JsonSerializerOptions, AIFunctionDeclaration.JsonSchema, AIFunctionDeclaration.ReturnJsonSchema, AITool.ToString(), AITool.GetService(Type, object?), AITool.GetService<TService>(object?), AITool.Name, AITool.Description, AITool.AdditionalProperties, object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Installed by the tool registry as the layer directly around the real function — inside ApprovalRequiredAIFunction: trimming must see only the tool’s own output, never the pending-approval signal that wrapper produces instead of running the body.

Bounding the output inside the tool’s own body is always better: the tool knows its data, this only counts bytes. This class is the last defence for the day that bound is forgotten.

Creates a new output-trimming wrapper.

public TruncatingAIFunction(AIFunction innerFunction, int maxOutputBytes)

innerFunction AIFunction

The tool to wrap.

maxOutputBytes int

The most bytes (UTF-8) a result may carry before it is trimmed.

ArgumentOutOfRangeException

maxOutputBytes is below TruncatingAIFunction.MinimumEnvelopeBytes.

The largest an empty-content envelope can ever be — the worst case is Int32.MaxValue for the dropped-byte count, since that value is bounded by Encoding.UTF8’s own int-typed byte count.

public static readonly int MinimumEnvelopeBytes

int

A maxOutputBytes below this value is rejected by the constructor: below it, no input could ever be represented by a compliant envelope, and the alternative would be a silent budget violation instead of a startup-time error.

InvokeCoreAsync(AIFunctionArguments, CancellationToken)

Section titled “ InvokeCoreAsync(AIFunctionArguments, CancellationToken)”

Invokes the AI.AIFunction and returns its result.

protected override ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken)

arguments AIFunctionArguments

The arguments to pass to the function’s invocation.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

ValueTask<object?>

The result of the function’s execution.