Documentation
¶
Index ¶
- Constants
- func DetectMimeType(filePath string) string
- func DetectMimeTypeByContent(data []byte) string
- func FormatDimensionNote(r *ImageResizeResult) string
- func IsImageFile(filePath string) bool
- func IsImageMimeType(mimeType string) bool
- func IsSupportedMimeType(mimeType string) bool
- func IsTextFile(filePath string) bool
- func ProcessAttachmentWithMetadata(part MessagePart) (Document, *ImageResizeResult, error)
- func ReadFileForInline(filePath string) (string, error)
- type Document
- type DocumentSource
- type FinishReason
- type ImageResizeResult
- type ImageURLDetail
- type Message
- type MessageDelta
- type MessageFile
- type MessageImageURL
- type MessagePart
- type MessagePartType
- type MessageRole
- type MessageStream
- type MessageStreamChoice
- type MessageStreamResponse
- type Usage
Constants ¶
const ( // MaxImageDimension is the maximum width or height for images sent to LLM providers. MaxImageDimension = 2000 // MaxImageBytes is the maximum file size for images sent to LLM providers (4.5MB, // below Anthropic's 5MB limit). MaxImageBytes = 4_500_000 )
const ( // MaxInlineBinarySize is the maximum byte size of a binary file (PDF, image, // etc.) that will be read into memory for inline attachment. MaxInlineBinarySize = 20 * 1024 * 1024 // 20 MB )
const MaxInlineFileSize = 5 * 1024 * 1024 // 5MB
MaxInlineFileSize is the maximum size of a text file that can be inlined directly into a message. Files larger than this are skipped with a warning. This is much smaller than the general upload limit because inline content expands token usage significantly.
Variables ¶
This section is empty.
Functions ¶
func DetectMimeType ¶
DetectMimeType returns the MIME type for a file by reading its first 512 bytes and inspecting the content (magic bytes). For text-based files that http.DetectContentType cannot distinguish (e.g. source code vs plain text), it falls back to extension matching. This is the canonical implementation used across all packages for consistency.
func DetectMimeTypeByContent ¶
DetectMimeTypeByContent detects the MIME type of data by inspecting its content (magic bytes). Returns the MIME type or "application/octet-stream" if unknown. This supplements extension-based detection for files with missing or wrong extensions.
func FormatDimensionNote ¶
func FormatDimensionNote(r *ImageResizeResult) string
FormatDimensionNote produces a human-readable note describing the resize mapping. This helps the model translate coordinates from the resized image back to the original.
Because ResizeImage uses fitDimensions (which preserves aspect ratio), the X and Y scale factors are always equal in practice. If they ever differ (e.g. because the function is called with a manually constructed ImageResizeResult), we emit separate per-axis factors so that coordinate mapping remains correct.
func IsImageFile ¶
IsImageFile returns true if the file at the given path is a supported image based on its extension. Supported formats: JPEG, PNG, GIF, WebP.
func IsImageMimeType ¶
IsImageMimeType returns true if the MIME type is a supported image type.
func IsSupportedMimeType ¶
IsSupportedMimeType returns true if the MIME type is supported for file attachments. Supported types include images (jpeg, png, gif, webp) and documents (pdf, text, markdown).
func IsTextFile ¶
IsTextFile determines if a file at the given path is a text file that should be inlined into the message rather than uploaded via a provider's file API. It first checks the file extension against a broad allowlist of known text extensions. For unknown extensions, it falls back to reading the first 8KB and checking if the content is valid UTF-8 with no null bytes.
func ProcessAttachmentWithMetadata ¶ added in v1.55.0
func ProcessAttachmentWithMetadata(part MessagePart) (Document, *ImageResizeResult, error)
ProcessAttachmentWithMetadata is like ProcessAttachment but also returns the ImageResizeResult when the attachment was an image that went through ResizeImage. The metadata is nil for non-image attachments.
Callers that need to emit a dimension note (for model coordinate-mapping) should use this variant and call FormatDimensionNote on the returned metadata.
func ReadFileForInline ¶
ReadFileForInline reads a text file and wraps it in an XML-like tag with the file path for context. Returns the formatted content and any error.
Types ¶
type Document ¶ added in v1.55.0
type Document struct {
// Name is the display name of the document (e.g. "report.pdf").
Name string `json:"name"`
// MimeType is the post-processing MIME type of the document. For images
// this is always "image/jpeg" or "image/png" regardless of the original
// format. For text files it is the exact MIME (e.g. "text/plain",
// "text/markdown", "text/html"). For binary documents it is the original
// MIME (e.g. "application/pdf").
MimeType string `json:"mime_type"`
// Size is the byte length of the document content (InlineData or InlineText).
// Optional; zero means unknown.
Size int64 `json:"size,omitempty"`
// Source holds the actual document content.
Source DocumentSource `json:"source"`
}
Document represents a file attachment in a message part. It carries the file name, post-processing MIME type, and the actual content via Source.
The MimeType field always reflects the final MIME that the attachment system will use when sending to the provider (e.g. "image/jpeg" after image normalisation, never the original "image/bmp").
func ProcessAttachment ¶ added in v1.55.0
func ProcessAttachment(_ context.Context, part MessagePart) (Document, error)
ProcessAttachment converts a raw MessagePart into a Document with fully resolved Source.InlineData or Source.InlineText. It is called once when a message is assembled — never at inference time.
Supported input types:
MessagePartTypeFile: reads the file from the local filesystem, detects its MIME type, and either inlines text content (text/* files) or reads binary bytes (images are transcoded+resized via ResizeImage; PDFs and other supported types are read verbatim).
MessagePartTypeImageURL: handles data: URIs (decoded inline). Remote http(s):// URLs are not supported; callers should download the file locally first and pass it as a MessagePartTypeFile instead.
MessagePartTypeDocument: if Source.InlineData or Source.InlineText is already set, the document is returned as-is after applying image transcoding to any image/* InlineData. A Document with no inline content is an error.
type DocumentSource ¶ added in v1.55.0
type DocumentSource struct {
// InlineText holds the raw text for text/* MIME types (TXT, MD, HTML, CSV, …).
// Used for StrategyTXT attachments.
InlineText string `json:"inline_text,omitempty"`
// InlineData holds binary content (images, PDFs, Office docs, …) that is
// base64-encoded when sent to the provider. Used for StrategyB64 attachments.
InlineData []byte `json:"inline_data,omitempty"`
}
DocumentSource holds the actual content of a document. Exactly one of the fields should be set.
type FinishReason ¶
type FinishReason string
FinishReason represents the reason why the model finished generating a response
const ( // FinishReasonStop means the model reached a natural stopping point or the max tokens FinishReasonStop FinishReason = "stop" // FinishReasonLength means the model reached the token limit FinishReasonLength FinishReason = "length" // FinishReasonToolCalls means the model called a tool FinishReasonToolCalls FinishReason = "tool_calls" // FinishReasonNull means no finish reason was provided FinishReasonNull FinishReason = "null" )
type ImageResizeResult ¶
type ImageResizeResult struct {
// Data is the (possibly re-encoded) image bytes.
Data []byte
// MimeType is the MIME type of the output image.
MimeType string
// OriginalWidth and OriginalHeight are the dimensions of the input image.
OriginalWidth, OriginalHeight int
// Width and Height are the dimensions of the output image.
Width, Height int
// Resized indicates whether the image was actually modified.
Resized bool
}
ImageResizeResult holds the output of an image resize operation.
func ResizeImage ¶
func ResizeImage(data []byte, mimeType string) (*ImageResizeResult, error)
ResizeImage takes raw image bytes and ensures they fit within provider limits (max 2000×2000 pixels, max 4.5 MB). If the image already fits, it is returned unchanged. Otherwise it is scaled down (preserving aspect ratio) and re-encoded.
The function tries to produce the smallest output by comparing PNG and JPEG encoding, and progressively reducing JPEG quality and dimensions if needed.
func ResizeImageBase64 ¶
func ResizeImageBase64(b64Data, mimeType string) (b64Result string, metadata *ImageResizeResult, err error)
ResizeImageBase64 is a convenience wrapper around ResizeImage that accepts and returns base64-encoded image data. The base64-encoded result is returned separately to avoid mutating the ImageResizeResult.Data field.
type ImageURLDetail ¶
type ImageURLDetail string
const ( ImageURLDetailHigh ImageURLDetail = "high" ImageURLDetailLow ImageURLDetail = "low" ImageURLDetailAuto ImageURLDetail = "auto" )
type Message ¶
type Message struct {
Role MessageRole `json:"role"`
Content string `json:"content"`
MultiContent []MessagePart `json:"multi_content,omitempty"`
// This property is used for the "reasoning" feature supported by deepseek-reasoner
// which is not in the official documentation.
// the doc from deepseek:
// - https://api-docs.deepseek.com/api/create-chat-completion#responses
ReasoningContent string `json:"reasoning_content,omitempty"`
// ThinkingSignature is used for Anthropic's extended thinking feature
ThinkingSignature string `json:"thinking_signature,omitempty"`
ThoughtSignature []byte `json:"thought_signature,omitempty"`
FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
// For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
// ToolDefinitions contains the definitions of tools referenced in ToolCalls.
// This is used to provide tool metadata (name, description, category) when loading historical sessions.
ToolDefinitions []tools.Tool `json:"tool_definitions,omitempty"`
// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
ToolCallID string `json:"tool_call_id,omitempty"`
// IsError indicates the tool call failed (only for Role=tool messages).
IsError bool `json:"is_error,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
// Usage tracks token usage for this message (only set for assistant messages)
Usage *Usage `json:"usage,omitempty"`
// Model is the model that generated this message (only set for assistant messages)
Model string `json:"model,omitempty"`
// Cost is the cost of this message in dollars (only set for assistant messages)
Cost float64 `json:"cost,omitempty"`
// FinishReason indicates why the model stopped generating for this message.
// "stop" = natural end, "tool_calls" = tool invocation, "length" = token limit.
// Only set for assistant messages.
FinishReason FinishReason `json:"finish_reason,omitempty"`
// CacheControl indicates whether this message is a cached message (only used by anthropic)
CacheControl bool `json:"cache_control,omitempty"`
}
type MessageDelta ¶
type MessageDelta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
ThinkingSignature string `json:"thinking_signature,omitempty"`
ThoughtSignature []byte `json:"thought_signature,omitempty"`
FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
}
MessageDelta represents a delta/chunk in a streaming response
type MessageFile ¶
type MessageFile struct {
Path string `json:"path,omitempty"` // Local file path (used for upload)
FileID string `json:"file_id,omitempty"` // Provider-specific file ID (after upload)
MimeType string `json:"mime_type,omitempty"` // MIME type of the file
}
MessageFile represents a file attachment that can be uploaded to a provider's file storage.
type MessageImageURL ¶
type MessageImageURL struct {
URL string `json:"url,omitempty"`
Detail ImageURLDetail `json:"detail,omitempty"`
}
type MessagePart ¶
type MessagePart struct {
Type MessagePartType `json:"type,omitempty"`
Text string `json:"text,omitempty"`
// Note: superseded by Document+MessagePartTypeDocument. Will be removed in a future release.
ImageURL *MessageImageURL `json:"image_url,omitempty"`
// Note: superseded by Document+MessagePartTypeDocument. Will be removed in a future release.
File *MessageFile `json:"file,omitempty"`
// Document is set when Type is MessagePartTypeDocument.
Document *Document `json:"document,omitempty"`
}
type MessagePartType ¶
type MessagePartType string
const ( MessagePartTypeText MessagePartType = "text" // MessagePartTypeImageURL is superseded by MessagePartTypeDocument. Will be removed in a future release. MessagePartTypeImageURL MessagePartType = "image_url" // MessagePartTypeFile is superseded by MessagePartTypeDocument. Will be removed in a future release. MessagePartTypeFile MessagePartType = "file" )
const MessagePartTypeDocument MessagePartType = "document"
MessagePartTypeDocument is the part type for a structured document attachment. Use this type when attaching files (images, PDFs, text, Office docs, etc.) to a message. The Document field must be set when this type is used.
This supersedes MessagePartTypeFile and MessagePartTypeImageURL, which are deprecated but remain supported for backward compatibility.
type MessageRole ¶
type MessageRole string
const ( MessageRoleSystem MessageRole = "system" MessageRoleUser MessageRole = "user" MessageRoleAssistant MessageRole = "assistant" MessageRoleTool MessageRole = "tool" )
type MessageStream ¶
type MessageStream interface {
// Recv gets the next completion chunk
Recv() (MessageStreamResponse, error)
// Close closes the stream
Close()
}
MessageStream interface represents a stream of chat completions
type MessageStreamChoice ¶
type MessageStreamChoice struct {
Index int `json:"index"`
Delta MessageDelta `json:"delta"`
FinishReason FinishReason `json:"finish_reason,omitempty"`
}
MessageStreamChoice represents a choice in a streaming response
type MessageStreamResponse ¶
type MessageStreamResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []MessageStreamChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
MessageStreamResponse represents a streaming response from the model