chat

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
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 added in v1.20.6

func DetectMimeType(filePath string) string

DetectMimeType returns the MIME type for a file based on its extension. This is the canonical implementation used across all packages for consistency. For binary file types (images, PDF), returns the specific MIME type. For text-based files, returns "text/plain". Unrecognized extensions return "application/octet-stream".

func IsSupportedMimeType added in v1.20.6

func IsSupportedMimeType(mimeType string) bool

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 added in v1.22.0

func IsTextFile(filePath string) bool

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 ReadFileForInline added in v1.22.0

func ReadFileForInline(filePath string) (string, error)

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 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 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"`

	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"`

	// 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 added in v1.20.6

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"`
	ImageURL *MessageImageURL `json:"image_url,omitempty"`
	File     *MessageFile     `json:"file,omitempty"`
}

type MessagePartType

type MessagePartType string
const (
	MessagePartTypeText     MessagePartType = "text"
	MessagePartTypeImageURL MessagePartType = "image_url"
	MessagePartTypeFile     MessagePartType = "file"
)

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"`
	RateLimit *RateLimit            `json:"rate_limit,omitempty"`
}

MessageStreamResponse represents a streaming response from the model

type RateLimit added in v1.19.7

type RateLimit struct {
	Limit      int64 `json:"limit,omitempty"`
	Remaining  int64 `json:"remaining,omitempty"`
	Reset      int64 `json:"reset,omitempty"`
	RetryAfter int64 `json:"retry_after,omitempty"`
}

type Usage

type Usage struct {
	InputTokens       int64 `json:"input_tokens"`
	OutputTokens      int64 `json:"output_tokens"`
	CachedInputTokens int64 `json:"cached_input_tokens"`
	CacheWriteTokens  int64 `json:"cached_write_tokens"`
	ReasoningTokens   int64 `json:"reasoning_tokens,omitempty"`
}

Jump to

Keyboard shortcuts

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