provider

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

Package provider defines the interfaces and types that AI providers implement.

GoAI ships provider implementations in sub-packages (openai, anthropic, google, etc.). Each provider implements one or more of the model interfaces defined here.

Index

Constants

This section is empty.

Variables

View Source
var ErrFileUploadUnsupported = errors.New("goai: file upload not supported by this provider")

ErrFileUploadUnsupported is returned when a provider does not support file upload.

Functions

func TrySend

func TrySend(ctx context.Context, out chan<- StreamChunk, chunk StreamChunk) bool

TrySend sends a chunk to the output channel, returning false if the context is cancelled. This prevents goroutine leaks when the consumer stops reading and the channel buffer is full.

Convention: callers MUST check the return value and return early when false, EXCEPT for terminal sends where the function exits immediately after the call. Terminal sends may leave the return value unchecked since no further work follows.

Types

type CapableModel

type CapableModel interface {
	Capabilities() ModelCapabilities
}

CapableModel is an optional interface that LanguageModel implementations can satisfy to declare their capabilities. Use ModelCapabilitiesOf to query.

type EmbedParams

type EmbedParams struct {
	// ProviderOptions are provider-specific request parameters.
	ProviderOptions map[string]any
}

EmbedParams contains parameters for an embedding request.

type EmbedResult

type EmbedResult struct {
	// Embeddings contains the generated vectors.
	Embeddings [][]float64

	// Usage tracks token consumption.
	Usage Usage

	// ProviderMetadata contains provider-specific response data.
	ProviderMetadata map[string]map[string]any

	// Response contains provider-specific response metadata (ID, model, headers).
	Response ResponseMetadata
}

EmbedResult is the response from embedding generation.

type EmbeddingModel

type EmbeddingModel interface {
	// ModelID returns the provider-specific model identifier.
	ModelID() string

	// DoEmbed generates embeddings for the given values.
	DoEmbed(ctx context.Context, values []string, params EmbedParams) (*EmbedResult, error)

	// MaxValuesPerCall returns the maximum number of values that can be embedded in a single call.
	// Returns 0 if there is no limit.
	MaxValuesPerCall() int
}

EmbeddingModel generates vector embeddings from text.

type FileUpload added in v0.9.0

type FileUpload struct {
	// Reader is the file content to upload.
	Reader io.Reader
	// Filename is the name of the file.
	Filename string
	// MediaType is the MIME type of the file (e.g. "application/pdf").
	MediaType string
	// Purpose describes the intended use (e.g. "assistants", "vision").
	Purpose string
}

FileUpload describes a file to upload to a provider's remote storage.

type FileUploadCapableModel added in v0.9.0

type FileUploadCapableModel interface {
	FileUploader() FileUploader
}

FileUploadCapableModel is an optional interface that LanguageModel implementations can satisfy to indicate they support remote file upload.

type FileUploader added in v0.9.0

type FileUploader interface {
	// UploadFile uploads a file and returns a reference to the remote file.
	UploadFile(ctx context.Context, upload FileUpload) (*RemoteFileRef, error)
	// DeleteFile deletes a previously uploaded remote file.
	DeleteFile(ctx context.Context, ref RemoteFileRef) error
}

FileUploader uploads files to a provider's remote storage and manages their lifecycle.

type FinishReason

type FinishReason string

FinishReason indicates why generation stopped.

const (
	FinishStop          FinishReason = "stop"
	FinishToolCalls     FinishReason = "tool-calls"
	FinishLength        FinishReason = "length"
	FinishContentFilter FinishReason = "content-filter"
	FinishError         FinishReason = "error"
	FinishOther         FinishReason = "other"
)

type GenerateParams

type GenerateParams struct {
	// Messages is the conversation history.
	Messages []Message

	// System is the system prompt.
	System string

	// Tools available to the model.
	Tools []ToolDefinition

	// MaxOutputTokens limits the response length. 0 means provider default.
	MaxOutputTokens int

	// Temperature controls randomness. nil means provider default.
	Temperature *float64

	// TopP controls nucleus sampling. nil means provider default.
	TopP *float64

	// TopK limits sampling to the top K tokens. nil means provider default.
	TopK *int

	// FrequencyPenalty penalizes tokens based on frequency. nil means provider default.
	FrequencyPenalty *float64

	// PresencePenalty penalizes tokens that have appeared. nil means provider default.
	PresencePenalty *float64

	// Seed for deterministic generation. nil means provider default.
	Seed *int

	// StopSequences causes generation to stop when encountered.
	StopSequences []string

	// Headers are additional HTTP headers for this request.
	Headers map[string]string

	// ProviderOptions are provider-specific request parameters.
	ProviderOptions map[string]any

	// PromptCaching enables provider-specific prompt caching.
	PromptCaching bool

	// ToolChoice controls tool selection: "auto", "none", "required", or a specific tool name.
	ToolChoice string

	// ResponseFormat requests structured JSON output matching a schema.
	// When set, providers apply their native JSON mode (OpenAI json_schema,
	// Anthropic tool trick, Gemini responseMimeType+responseSchema).
	ResponseFormat *ResponseFormat
}

GenerateParams contains all parameters for a generation request.

type GenerateResult

type GenerateResult struct {
	// Text is the generated text content.
	Text string

	// Reasoning is the model's thinking/reasoning text (when extended
	// thinking is enabled). Excludes signatures and redacted blocks —
	// those remain in ProviderMetadata for replay. Empty when the
	// provider does not return reasoning or thinking is disabled.
	Reasoning string

	// ToolCalls requested by the model.
	ToolCalls []ToolCall

	// Sources extracted from response annotations (e.g. url_citation).
	Sources []Source

	// FinishReason indicates why generation stopped.
	FinishReason FinishReason

	// Usage tracks token consumption.
	Usage Usage

	// Response contains provider-specific metadata.
	Response ResponseMetadata

	// ProviderMetadata contains provider-specific response data
	// (e.g. logprobs, prediction tokens).
	ProviderMetadata map[string]map[string]any
}

GenerateResult is the response from a non-streaming generation.

type ImageData

type ImageData struct {
	// Data is the raw image bytes.
	Data []byte

	// MediaType (e.g. "image/png").
	MediaType string
}

ImageData contains a single generated image.

type ImageModel

type ImageModel interface {
	// ModelID returns the provider-specific model identifier.
	ModelID() string

	// DoGenerate generates images from the given parameters.
	DoGenerate(ctx context.Context, params ImageParams) (*ImageResult, error)
}

ImageModel generates images from text prompts.

type ImageParams

type ImageParams struct {
	// Prompt describes the image to generate.
	Prompt string

	// N is the number of images to generate.
	N int

	// Size specifies dimensions (e.g. "1024x1024").
	Size string

	// AspectRatio (e.g. "16:9", "1:1"). Alternative to Size.
	AspectRatio string

	// ProviderOptions are provider-specific parameters.
	ProviderOptions map[string]any
}

ImageParams contains parameters for image generation.

type ImageResult

type ImageResult struct {
	Images []ImageData

	// ProviderMetadata contains provider-specific response data
	// (e.g. revisedPrompt).
	ProviderMetadata map[string]map[string]any

	// Usage tracks token or operation consumption (if reported by the provider).
	Usage Usage

	// Response contains provider-specific response metadata (ID, model, headers).
	Response ResponseMetadata
}

ImageResult is the response from image generation.

type InvalidatingTokenSource

type InvalidatingTokenSource interface {
	TokenSource
	Invalidate()
}

InvalidatingTokenSource is a TokenSource whose cached token can be cleared, forcing a fresh fetch on the next call. Supports application-level retry-on-401 logic.

type LanguageModel

type LanguageModel interface {
	// ModelID returns the provider-specific model identifier (e.g. "gpt-4o", "claude-sonnet-4-20250514").
	ModelID() string

	// DoGenerate performs a non-streaming generation request.
	DoGenerate(ctx context.Context, params GenerateParams) (*GenerateResult, error)

	// DoStream performs a streaming generation request.
	DoStream(ctx context.Context, params GenerateParams) (*StreamResult, error)
}

LanguageModel generates text and tool calls from messages.

type Message

type Message struct {
	// Role identifies the sender.
	Role Role

	// Content parts that make up this message.
	Content []Part

	// ProviderOptions are provider-specific message parameters.
	ProviderOptions map[string]any
}

Message represents a conversation message.

func NormalizeToolMessages added in v0.5.9

func NormalizeToolMessages(msgs []Message) []Message

NormalizeToolMessages prepares messages for providers that require: 1. Every assistant tool-call has a matching tool-result (orphan fix) 2. Alternating user/assistant roles (merge consecutive same-role)

Call this before provider-specific message conversion.

func ReorderAssistantParts added in v0.5.9

func ReorderAssistantParts(msgs []Message) []Message

ReorderAssistantParts sorts assistant message parts so text/reasoning come before tool-call parts. Anthropic/Bedrock require this ordering.

type ModalitySet

type ModalitySet struct {
	Text  bool
	Audio bool
	Image bool
	Video bool
	PDF   bool
}

ModalitySet lists supported content modalities.

type ModelCapabilities

type ModelCapabilities struct {
	// Temperature indicates the model accepts a temperature parameter.
	Temperature bool

	// Reasoning indicates the model supports extended thinking/reasoning.
	Reasoning bool

	// Attachment indicates the model supports file attachments.
	Attachment bool

	// ToolCall indicates the model supports tool/function calling.
	ToolCall bool

	// FileUpload indicates the model supports remote file upload.
	FileUpload bool

	// InputModalities lists supported input types.
	InputModalities ModalitySet

	// OutputModalities lists supported output types.
	OutputModalities ModalitySet
}

ModelCapabilities describes what features a model supports.

func ModelCapabilitiesOf

func ModelCapabilitiesOf(m LanguageModel) ModelCapabilities

ModelCapabilitiesOf returns the model's capabilities if it implements CapableModel, or a zero-value ModelCapabilities otherwise.

type Part

type Part struct {
	// Type identifies this part's kind.
	Type PartType

	// Text content (for PartText and PartReasoning).
	Text string

	// URL for images (data:image/png;base64,... format).
	URL string

	// Tool call fields (for PartToolCall and PartToolResult).
	ToolCallID string
	ToolName   string
	ToolInput  json.RawMessage

	// ToolOutput is the result text (for PartToolResult).
	ToolOutput string

	// CacheControl directive (e.g. "ephemeral") for prompt caching.
	CacheControl string

	// Detail level for image parts ("low", "high", "auto").
	Detail string

	// MediaType of the content (for PartImage, PartFile).
	MediaType string

	// Filename of the content (for PartFile).
	Filename string

	// RemoteRef is a reference to an uploaded remote file (for PartFile, PartImage).
	// When set, the provider uses the remote reference instead of inline data.
	RemoteRef *RemoteFileRef

	// ProviderOptions are provider-specific part parameters.
	ProviderOptions map[string]any
}

Part is a single content element within a message. The Type field determines which other fields are populated.

type PartType

type PartType string

PartType identifies the kind of content in a message part.

const (
	PartText       PartType = "text"
	PartReasoning  PartType = "reasoning"
	PartImage      PartType = "image"
	PartToolCall   PartType = "tool-call"
	PartToolResult PartType = "tool-result"
	PartFile       PartType = "file"
)

type RemoteFileRef added in v0.9.0

type RemoteFileRef struct {
	// Provider identifies which provider owns this file.
	Provider string
	// ID is the provider-specific file identifier.
	ID string
	// URI is the provider-specific file URI (e.g. for Gemini).
	URI string
	// Filename is the original file name.
	Filename string
	// MediaType is the MIME type of the file.
	MediaType string
	// ExpiresAt is when the remote file expires (zero if unknown).
	ExpiresAt time.Time
	// Data holds the raw file bytes for fallback on providers without native file APIs.
	Data []byte
}

RemoteFileRef is a reference to an uploaded remote file.

type ResponseFormat

type ResponseFormat struct {
	// Name identifies the schema (used by OpenAI's json_schema mode).
	Name string

	// Schema is the JSON Schema that the output must conform to.
	Schema json.RawMessage
}

ResponseFormat requests structured JSON output matching a schema. Used by GenerateObject/StreamObject to enable provider-specific JSON mode.

type ResponseMetadata

type ResponseMetadata struct {
	// ID is the provider's response identifier.
	ID string

	// Model is the actual model used (may differ from requested).
	Model string

	// Headers are selected response headers.
	Headers map[string]string

	// ProviderMetadata contains provider-specific metadata (e.g. iterations,
	// context_management, container, citations, reasoning signatures).
	ProviderMetadata map[string]any
}

ResponseMetadata contains provider-specific response information.

type Role

type Role string

Role identifies the sender of a message.

const (
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
)

type Source

type Source struct {
	// ID is the source identifier (provider-assigned or generated).
	ID string

	// Type identifies the source kind (e.g. "url", "document").
	// Maps to Vercel's sourceType field.
	Type string

	// URL is the citation URL.
	URL string

	// Title is the citation title.
	Title string

	// StartIndex is the start character offset in the text.
	StartIndex int

	// EndIndex is the end character offset in the text.
	EndIndex int

	// ProviderMetadata contains provider-specific source data.
	ProviderMetadata map[string]any
}

Source represents a citation or reference from the model's response. Matches Vercel AI SDK's LanguageModelV2Source.

type StopCause added in v0.7.2

type StopCause string

StopCause classifies how a multi-step tool loop terminated. It is carried on the final ChunkFinish emitted by goai's loop and on FinishInfo so sync consumers (OnFinish hook) and stream consumers both see a consistent signal. Provider-level chunks leave this empty.

const (
	// StopCauseNatural indicates the loop ended because the model did not
	// request further tool calls.
	StopCauseNatural StopCause = "natural"
	// StopCauseMaxSteps indicates MaxSteps was reached with tool calls still pending.
	StopCauseMaxSteps StopCause = "max-steps"
	// StopCausePredicate indicates WithStopWhen returned true.
	StopCausePredicate StopCause = "predicate"
	// StopCauseBeforeStep indicates OnBeforeStep returned Stop=true.
	StopCauseBeforeStep StopCause = "before-step"
	// StopCauseAbort indicates the stream terminated on an error path.
	StopCauseAbort StopCause = "abort"
	// StopCauseEmpty indicates the provider closed its stream without sending
	// any meaningful chunks (no text, no tool calls, no finish reason). This
	// is not an error path; it is a distinct no-op response that consumers may
	// want to treat differently from both "natural" (model completed) and
	// "abort" (error).
	//
	// Emission scope (FIX 38 - narrowed): emitted only by the streaming
	// multi-step tool-loop path (`streamWithToolLoop`, entered when
	// MaxSteps>1 AND at least one configured tool has an Execute function).
	// Sync (GenerateText / DoGenerate) paths do not emit this cause.
	// Streaming single-shot paths (MaxSteps=1 or no executable tools, which
	// bypass streamWithToolLoop) ALSO do not emit this cause - they hardcode
	// StopCauseNatural regardless of chunk content, consistent with
	// Vercel's single-iteration streaming behavior. Consumers that need a
	// distinct "empty response" signal on single-shot streams should
	// inspect len(TextResult.Text) and len(TextResult.ToolCalls) directly.
	// A latent semantic question (should single-shot empty streams also
	// emit StopCauseEmpty?) is tracked at the fireOnFinish call site in
	// generate.go - see FIX 32 TODO.
	StopCauseEmpty StopCause = "empty"
	// StopCauseNoExecutableTools indicates the model returned tool calls but
	// no tool in the configured tool set has an Execute function. The loop
	// cannot proceed (there is nothing to execute) and exits cleanly. This is
	// semantically distinct from StopCauseNatural ("model stopped on its
	// own") because here the model still wants to continue but the consumer
	// has not provided executable tools.
	//
	// Emission scope: sync-only (GenerateText). StreamText does NOT emit this
	// cause because the streaming tool-loop path (streamWithToolLoop) is only
	// entered when at least one executable tool is configured; a streaming
	// call with zero executable tools takes the single-shot path and reports
	// StopCauseNatural. Consumers needing a uniform signal across sync and
	// stream can inspect ToolCalls on the last step and treat a non-empty
	// list with no corresponding Execute function as equivalent.
	StopCauseNoExecutableTools StopCause = "no-executable-tools"
)

func (StopCause) IsValid added in v0.7.2

func (s StopCause) IsValid() bool

IsValid reports whether s is one of the StopCause constants declared in this package. The empty string ("") is NOT considered valid (it is used internally as a sentinel for "not yet classified" and to mark provider-level chunks that predate the loop classifier).

Consumers may use IsValid to defend against typos or downstream code that constructs a StopCause from arbitrary text (StopCause is a string alias so construction of unknown values cannot be prevented by the type system alone).

type StreamChunk

type StreamChunk struct {
	// Type identifies this chunk's kind.
	Type StreamChunkType

	// Text content (for ChunkText and ChunkReasoning).
	Text string

	// Tool call fields (for ChunkToolCall and ChunkToolCallStreamStart).
	ToolCallID string
	ToolName   string
	ToolInput  string

	// FinishReason (for ChunkStepFinish and ChunkFinish).
	FinishReason FinishReason

	// Usage (for ChunkFinish, may also appear on ChunkStepFinish).
	Usage Usage

	// Error carries the provider/goai error when Type == ChunkError.
	//   - Nil for all non-ChunkError chunk types.
	//   - May wrap APIError, NetworkError, or plain errors produced by the
	//     provider or goai's tool loop (e.g. tool execution failures that are
	//     surfaced as chunks rather than returned via Err()).
	//   - Providers set this field directly; consumers should use
	//     errors.Is / errors.As to branch on specific error categories rather
	//     than comparing error values.
	Error error

	// Response metadata (populated on ChunkFinish with ID, Model from the provider).
	Response ResponseMetadata

	// Metadata for provider-specific data (e.g. thoughtSignature).
	Metadata map[string]any

	// StoppedBy classifies how the tool loop terminated. Only meaningful on
	// the final ChunkFinish emitted by goai's tool loop. Values:
	//
	//	""                     - single-step provider chunk or unknown
	//	"natural"              - the loop ended because the model did not request
	//	                         additional tool calls (FinishStop or equivalent)
	//	"max-steps"            - MaxSteps was reached while tool calls were still
	//	                         pending (StepsExhausted)
	//	"predicate"            - WithStopWhen predicate returned true
	//	"before-step"          - OnBeforeStep hook returned Stop=true
	//	"abort"                - the stream terminated on an error path
	//	"empty"                - provider closed its stream with no chunks
	//	                         (streaming-only; see StopCauseEmpty godoc)
	//	"no-executable-tools"  - model returned tool calls but no tool in the
	//	                         configured set has an Execute function
	//	                         (sync-only; see StopCauseNoExecutableTools godoc)
	StoppedBy StopCause
}

StreamChunk is a single event in a streaming response. The Type field determines which other fields are populated.

type StreamChunkType

type StreamChunkType string

StreamChunkType identifies the kind of streaming chunk.

const (
	ChunkText                StreamChunkType = "text"
	ChunkReasoning           StreamChunkType = "reasoning"
	ChunkToolCall            StreamChunkType = "tool_call"
	ChunkToolCallDelta       StreamChunkType = "tool_call_delta"
	ChunkToolCallStreamStart StreamChunkType = "tool_call_streaming_start"
	ChunkToolResult          StreamChunkType = "tool_result"
	ChunkStepFinish          StreamChunkType = "step_finish"
	ChunkFinish              StreamChunkType = "finish"
	ChunkError               StreamChunkType = "error"
)

type StreamResult

type StreamResult struct {
	// Stream emits chunks as they arrive. The channel is closed when the stream ends.
	Stream <-chan StreamChunk
}

StreamResult wraps a streaming response channel.

type Token

type Token struct {
	// Value is the token string (API key, OAuth access token, etc.).
	Value string

	// ExpiresAt is when the token expires. Zero value means no expiry.
	ExpiresAt time.Time
}

Token represents an authentication token with optional expiry.

type TokenFetchFunc

type TokenFetchFunc func(ctx context.Context) (*Token, error)

TokenFetchFunc fetches a fresh token. Used by CachedTokenSource.

type TokenSource

type TokenSource interface {
	// Token returns a valid token. Implementations must be safe for concurrent use.
	Token(ctx context.Context) (string, error)
}

TokenSource provides authentication tokens for API requests. Providers accept a TokenSource to support dynamic credentials (OAuth, service accounts, device flow) beyond static API keys.

func CachedTokenSource

func CachedTokenSource(fetchFn TokenFetchFunc) TokenSource

CachedTokenSource creates a TokenSource that caches tokens until expiry. The fetchFn is called lazily on first use and again when the cached token expires. It is safe for concurrent use.

The returned TokenSource also implements InvalidatingTokenSource, allowing application-level retry-on-401 logic to force a token refresh.

func StaticToken

func StaticToken(key string) TokenSource

StaticToken creates a TokenSource that always returns the given key. Use this for simple API key authentication.

type ToolCall

type ToolCall struct {
	// ID is a unique identifier for this tool call.
	ID string

	// Name of the tool to invoke.
	Name string

	// Input is the JSON-encoded arguments.
	Input json.RawMessage

	// Metadata carries provider-specific data that must be preserved across
	// tool round-trips (e.g., Google's thoughtSignature).
	Metadata map[string]any
}

ToolCall represents the model's request to invoke a tool.

type ToolDefinition

type ToolDefinition struct {
	// Name is the tool's identifier.
	Name string

	// Description explains what the tool does (used by the model to decide when to call it).
	Description string

	// InputSchema is the JSON Schema for the tool's input parameters.
	InputSchema json.RawMessage

	// ProviderDefinedType, when non-empty, marks this as a provider-defined tool
	// (e.g. "computer_20250124", "bash_20250124", "text_editor_20250124").
	// Providers use this to emit the correct API type instead of "custom".
	ProviderDefinedType string

	// ProviderDefinedOptions holds provider-specific tool configuration
	// (e.g. displayWidthPx for computer use). Providers interpret these as needed.
	ProviderDefinedOptions map[string]any
}

ToolDefinition describes a tool available to the model.

type ToolResult added in v0.7.2

type ToolResult struct {
	// ToolCallID matches the ID of the originating ToolCall.
	ToolCallID string

	// ToolName is the name of the tool that was invoked.
	ToolName string

	// Output is the stringified result sent to the model. For failed calls
	// this is "error: <message>" (matching what the tool-result message
	// carries); predicates should inspect Error or IsError to distinguish
	// errors from deliberately empty successful outputs.
	Output string

	// Error is the error returned by the tool's Execute function, or
	// goai.ErrUnknownTool when the model requested a tool not in the
	// configured set. Nil on success.
	//
	// The standard encoding/json package cannot marshal the error
	// interface directly (it would emit "{}"). ToolResult implements
	// MarshalJSON to surface Error as a string ("Error.Error()") in
	// JSON output so the field is useful in observability / logging.
	Error error

	// IsError is a convenience boolean equivalent to Error != nil.
	IsError bool
}

ToolResult represents the outcome of executing a single tool call.

It is a structured companion to the tool_result message content: where tool-result messages carry the string payload destined for the LLM, a ToolResult exposes the same information to in-process consumers (e.g. StopCondition predicates) without forcing them to parse message parts.

Output is the exact string sent back to the model (stringified JSON for structured results, or "error: <detail>" when the tool call failed). Error is non-nil iff the tool returned an error or panicked; IsError is true in the same condition and is provided for ergonomics when predicates only need the boolean signal.

func (ToolResult) MarshalJSON added in v0.7.2

func (r ToolResult) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler so that ToolResult round-trips through encoding/json in a useful form. The Error field (an interface) would otherwise marshal as "{}"; we emit it as a string using err.Error(). All other fields use their natural JSON representation.

FIX 39 - empty-string error fidelity. When r.Error is non-nil but r.Error.Error() == "" (e.g. errors.New("")), we still emit the Error key (as "") and IsError=true. Previously the Error field had `omitempty`, so an empty message was dropped and UnmarshalJSON resurrected a synthesized placeholder instead of honoring the empty message the producer sent. Emitting Error unconditionally when IsError is true preserves the exact string (including "") across round-trips. Producers who never use empty-message errors are unaffected: the wire form gains at most one `"Error":""` field.

FIX 40 - sentinel identity is NOT preserved. If the producer set r.Error to a sentinel (e.g. ErrUnknownTool from this package), UnmarshalJSON reconstructs a plain errors.New(msg); callers doing `errors.Is(r.Error, ErrUnknownTool)` after a JSON round-trip will get false. Reconstructing sentinels from strings requires a registry and is out of scope. Consumers relying on sentinel-based dispatch should either avoid JSON round-trips for that data or add an out-of-band error-code field.

func (*ToolResult) UnmarshalJSON added in v0.7.2

func (r *ToolResult) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler so that ToolResult round-trips through encoding/json. The Error field was marshaled as a string (see MarshalJSON); here we reconstruct it via errors.New when present and preserve the invariant IsError == (Error != nil). If the JSON explicitly sets IsError=true but has no Error key, we synthesize a placeholder error so the invariant is preserved; conversely if Error is set but IsError is false, we coerce IsError to true.

FIX 39 uses a pointer-to-string alias so that a producer's empty Error value ("") is distinguishable from "key absent entirely" (nil pointer). Empty string preserves the producer's errors.New("") faithfully; nil + IsError=true falls back to the placeholder. FIX 40: sentinel identity is not recovered (see MarshalJSON godoc).

type Usage

type Usage struct {
	InputTokens      int
	OutputTokens     int
	TotalTokens      int
	ReasoningTokens  int
	CacheReadTokens  int
	CacheWriteTokens int
}

Usage tracks token consumption for a request.

Directories

Path Synopsis
Package anthropic provides an Anthropic language model implementation for GoAI.
Package anthropic provides an Anthropic language model implementation for GoAI.
Package azure provides an Azure OpenAI language model implementation for GoAI.
Package azure provides an Azure OpenAI language model implementation for GoAI.
Package bedrock provides an AWS Bedrock language model implementation for GoAI.
Package bedrock provides an AWS Bedrock language model implementation for GoAI.
Package cerebras provides a cerebras language model implementation for GoAI.
Package cerebras provides a cerebras language model implementation for GoAI.
Package cloudflare provides a Cloudflare Workers AI language and embedding model implementation for GoAI, using the OpenAI-compatible endpoints.
Package cloudflare provides a Cloudflare Workers AI language and embedding model implementation for GoAI, using the OpenAI-compatible endpoints.
Package cohere provides a Cohere language model and embedding implementation for GoAI.
Package cohere provides a Cohere language model and embedding implementation for GoAI.
Package compat provides a generic OpenAI-compatible language model for GoAI.
Package compat provides a generic OpenAI-compatible language model for GoAI.
Package deepinfra provides a DeepInfra language model implementation for GoAI.
Package deepinfra provides a DeepInfra language model implementation for GoAI.
Package deepseek provides a deepseek language model implementation for GoAI.
Package deepseek provides a deepseek language model implementation for GoAI.
Package fireworks provides a fireworks language model implementation for GoAI.
Package fireworks provides a fireworks language model implementation for GoAI.
Package fptcloud provides an FPT Smart Cloud AI Marketplace language and embedding model implementation for GoAI, using the OpenAI-compatible endpoints.
Package fptcloud provides an FPT Smart Cloud AI Marketplace language and embedding model implementation for GoAI, using the OpenAI-compatible endpoints.
Package google provides a Google Gemini language model implementation for GoAI.
Package google provides a Google Gemini language model implementation for GoAI.
Package groq provides a groq language model implementation for GoAI.
Package groq provides a groq language model implementation for GoAI.
Package llamacpp provides a llama.cpp server language model implementation for GoAI.
Package llamacpp provides a llama.cpp server language model implementation for GoAI.
Package minimax provides a MiniMax language model implementation for GoAI.
Package minimax provides a MiniMax language model implementation for GoAI.
Package mistral provides a mistral language model implementation for GoAI.
Package mistral provides a mistral language model implementation for GoAI.
Package nvidia provides an NVIDIA NIM language and embedding model implementation for GoAI, using the OpenAI-compatible endpoints.
Package nvidia provides an NVIDIA NIM language and embedding model implementation for GoAI, using the OpenAI-compatible endpoints.
Package ollama provides a native Ollama language model implementation for GoAI.
Package ollama provides a native Ollama language model implementation for GoAI.
Package openai provides an OpenAI language model implementation for GoAI.
Package openai provides an OpenAI language model implementation for GoAI.
Package openrouter provides an OpenRouter language model implementation for GoAI.
Package openrouter provides an OpenRouter language model implementation for GoAI.
Package perplexity provides a perplexity language model implementation for GoAI.
Package perplexity provides a perplexity language model implementation for GoAI.
Package requesty provides a Requesty language model implementation for GoAI.
Package requesty provides a Requesty language model implementation for GoAI.
Package runpod provides a RunPod language model implementation for GoAI.
Package runpod provides a RunPod language model implementation for GoAI.
Package together provides a together language model implementation for GoAI.
Package together provides a together language model implementation for GoAI.
Package vertex provides a Google Cloud Vertex AI language model implementation for GoAI.
Package vertex provides a Google Cloud Vertex AI language model implementation for GoAI.
Package vllm provides a vLLM language model implementation for GoAI.
Package vllm provides a vLLM language model implementation for GoAI.
Package xai provides a xai language model implementation for GoAI.
Package xai provides a xai language model implementation for GoAI.

Jump to

Keyboard shortcuts

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