chatsanitize

package
v2.35.6 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAnthropicProviderToolPromptUnsafe = xerrors.New(
	"anthropic prompt still contains invalid provider-executed tool history after guard",
)

ErrAnthropicProviderToolPromptUnsafe reports that the pre-request guard could not repair provider-executed tool history into a prompt shape Anthropic will accept.

Functions

func AnthropicProviderToolResultTextPart

func AnthropicProviderToolResultTextPart(
	part fantasy.MessagePart,
) (fantasy.TextPart, bool)

AnthropicProviderToolResultTextPart converts a provider-executed tool result into text so unsafe provider-tool structure can be removed without losing the result payload.

func AnthropicToolResultOutputText

func AnthropicToolResultOutputText(output fantasy.ToolResultOutputContent) string

AnthropicToolResultOutputText converts a tool result payload into the text that should remain in the prompt when provider-tool metadata is unsafe.

func ApplyAnthropicProviderToolGuard

func ApplyAnthropicProviderToolGuard(
	ctx context.Context,
	logger slog.Logger,
	provider string,
	modelName string,
	messages []fantasy.Message,
) ([]fantasy.Message, error)

ApplyAnthropicProviderToolGuard fail-closes unsafe Anthropic provider-tool history immediately before a provider request is issued. It returns a sanitized prompt on success, or nil with ErrAnthropicProviderToolPromptUnsafe when the prompt still cannot be repaired safely.

func HasAnthropicSignedReasoningOptions added in v2.34.0

func HasAnthropicSignedReasoningOptions(options fantasy.ProviderOptions) bool

HasAnthropicSignedReasoningOptions reports whether provider options contain Anthropic reasoning data that must be replayed without mutation.

func IsAllowedAnthropicProviderToolName

func IsAllowedAnthropicProviderToolName(name string) bool

IsAllowedAnthropicProviderToolName reports whether name is an Anthropic provider-executed tool name we know how to serialize.

func IsSerializableAnthropicProviderToolCall

func IsSerializableAnthropicProviderToolCall(part fantasy.MessagePart) bool

IsSerializableAnthropicProviderToolCall reports whether part can be serialized as an Anthropic provider-executed tool call.

func IsSerializableAnthropicProviderToolResult

func IsSerializableAnthropicProviderToolResult(
	part fantasy.MessagePart,
	matchedCall fantasy.MessagePart,
) bool

IsSerializableAnthropicProviderToolResult reports whether part can be serialized as an Anthropic provider-executed tool result for matchedCall.

func LogAnthropicProviderToolSanitization

func LogAnthropicProviderToolSanitization(
	ctx context.Context,
	logger slog.Logger,
	phase string,
	provider string,
	modelName string,
	stats AnthropicProviderToolSanitizationStats,
	extra ...slog.Field,
)

LogAnthropicProviderToolSanitization logs prompt changes made while removing invalid Anthropic provider-executed tool history.

func SanitizeAnthropicProviderToolStepContent

func SanitizeAnthropicProviderToolStepContent(
	ctx context.Context,
	logger slog.Logger,
	provider string,
	modelName string,
	phase string,
	step int,
	finishReason fantasy.FinishReason,
	content []fantasy.Content,
) []fantasy.Content

SanitizeAnthropicProviderToolStepContent removes invalid Anthropic provider-executed tool content from a streamed step and logs removals.

Types

type AnthropicProviderToolHistoryViolation

type AnthropicProviderToolHistoryViolation struct {
	MessageIndex int
	PartIndex    int
	ID           string
	Reason       string
}

AnthropicProviderToolHistoryViolation describes an invalid provider-executed tool history block in an Anthropic prompt.

func AnthropicProviderToolPartsToRemove

func AnthropicProviderToolPartsToRemove(
	provider string,
	parts []fantasy.MessagePart,
) (map[int]struct{}, []AnthropicProviderToolHistoryViolation)

AnthropicProviderToolPartsToRemove returns provider-executed tool parts that cannot be serialized safely in a single Anthropic assistant message. Violation MessageIndex values refer to the synthetic assistant message, so they are always 0.

func ValidateAnthropicProviderToolHistory

func ValidateAnthropicProviderToolHistory(
	messages []fantasy.Message,
) []AnthropicProviderToolHistoryViolation

ValidateAnthropicProviderToolHistory returns violations found in messages with invalid Anthropic provider-executed tool history blocks.

type AnthropicProviderToolSanitizationStats

type AnthropicProviderToolSanitizationStats struct {
	RemovedToolCalls   int
	RemovedToolResults int
	DroppedMessages    int
}

AnthropicProviderToolSanitizationStats describes prompt changes made while removing invalid Anthropic provider-executed tool history.

func SanitizeAnthropicProviderToolContent

func SanitizeAnthropicProviderToolContent(
	provider string,
	content []fantasy.Content,
) ([]fantasy.Content, AnthropicProviderToolSanitizationStats)

SanitizeAnthropicProviderToolContent removes invalid Anthropic provider-executed tool blocks from streamed content when doing so does not mutate signed reasoning replay state.

func SanitizeAnthropicProviderToolHistory

func SanitizeAnthropicProviderToolHistory(
	provider string,
	messages []fantasy.Message,
) ([]fantasy.Message, AnthropicProviderToolSanitizationStats)

SanitizeAnthropicProviderToolHistory removes Anthropic provider-executed tool history that cannot be serialized safely.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL