openaichat

package
v0.14.0-beta.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	OpenAIChatDefaultMaxTokens = 4096
)

Variables

This section is empty.

Functions

func ConvertAIChatToUIChat

func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error)

ConvertAIChatToUIChat converts stored chat to UI format

func ConvertToolResultsToNativeChatMessage

func ConvertToolResultsToNativeChatMessage(toolResults []uctypes.AIToolResult) ([]uctypes.GenAIMessage, error)

ConvertToolResultsToNativeChatMessage converts tool results to OpenAI tool messages

func GetFunctionCallInputByToolCallId

func GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *uctypes.AIFunctionCallInput

GetFunctionCallInputByToolCallId searches for a tool call by ID in the chat history

func RemoveToolUseCall added in v0.14.0

func RemoveToolUseCall(chatId string, callId string) error

func UpdateToolUseData

func UpdateToolUseData(chatId string, callId string, newToolUseData uctypes.UIMessageDataToolUse) error

UpdateToolUseData updates the ToolUseData for a specific tool call in the chat history

Types

type ChatContentPart added in v0.14.0

type ChatContentPart struct {
	Type     string        `json:"type"`                // "text" or "image_url"
	Text     string        `json:"text,omitempty"`      // for type "text"
	ImageUrl *ChatImageUrl `json:"image_url,omitempty"` // for type "image_url"

	FileName   string `json:"filename,omitempty"`   // internal: original filename
	PreviewUrl string `json:"previewurl,omitempty"` // internal: 128x128 webp preview
	MimeType   string `json:"mimetype,omitempty"`   // internal: original mimetype
}

type ChatImageUrl added in v0.14.0

type ChatImageUrl struct {
	Url    string `json:"url"`
	Detail string `json:"detail,omitempty"` // "auto", "low", "high"
}

type ChatRequest

type ChatRequest struct {
	Model               string               `json:"model"`
	Messages            []ChatRequestMessage `json:"messages"`
	Stream              bool                 `json:"stream"`
	MaxTokens           int                  `json:"max_tokens,omitempty"`            // legacy
	MaxCompletionTokens int                  `json:"max_completion_tokens,omitempty"` // newer
	Temperature         float64              `json:"temperature,omitempty"`
	Tools               []ToolDefinition     `json:"tools,omitempty"`       // if you use tools
	ToolChoice          any                  `json:"tool_choice,omitempty"` // "auto", "none", or struct
}

type ChatRequestMessage

type ChatRequestMessage struct {
	Role         string            `json:"role"`                   // "system","user","assistant","tool"
	Content      string            `json:"-"`                      // plain text (used when ContentParts is nil)
	ContentParts []ChatContentPart `json:"-"`                      // multimodal parts (used when images present)
	ToolCalls    []ToolCall        `json:"tool_calls,omitempty"`   // assistant tool-call message
	ToolCallID   string            `json:"tool_call_id,omitempty"` // for role:"tool"
	Name         string            `json:"name,omitempty"`         // tool name on role:"tool"
}

func (*ChatRequestMessage) FindToolCallIndex

func (cm *ChatRequestMessage) FindToolCallIndex(toolCallId string) int

func (ChatRequestMessage) MarshalJSON added in v0.14.0

func (cm ChatRequestMessage) MarshalJSON() ([]byte, error)

func (*ChatRequestMessage) UnmarshalJSON added in v0.14.0

func (cm *ChatRequestMessage) UnmarshalJSON(data []byte) error

type ChatUsage

type ChatUsage struct {
	Model        string `json:"model,omitempty"`
	InputTokens  int    `json:"prompt_tokens,omitempty"`
	OutputTokens int    `json:"completion_tokens,omitempty"`
	TotalTokens  int    `json:"total_tokens,omitempty"`
}

type ContentDelta

type ContentDelta struct {
	Role      string          `json:"role,omitempty"`
	Content   string          `json:"content,omitempty"`
	ToolCalls []ToolCallDelta `json:"tool_calls,omitempty"`
}

This is the important part:

type StoredChatMessage

type StoredChatMessage struct {
	MessageId string             `json:"messageid"`
	Message   ChatRequestMessage `json:"message"`
	Usage     *ChatUsage         `json:"usage,omitempty"`
}

StoredChatMessage is the stored message type

func ConvertAIMessageToStoredChatMessage

func ConvertAIMessageToStoredChatMessage(aiMsg uctypes.AIMessage) (*StoredChatMessage, error)

ConvertAIMessageToStoredChatMessage converts an AIMessage to StoredChatMessage These messages are ALWAYS role "user"

func RunChatStep

RunChatStep executes a chat step using the chat completions API

func (*StoredChatMessage) Copy

func (*StoredChatMessage) GetMessageId

func (m *StoredChatMessage) GetMessageId() string

func (*StoredChatMessage) GetRole

func (m *StoredChatMessage) GetRole() string

func (*StoredChatMessage) GetUsage

func (m *StoredChatMessage) GetUsage() *uctypes.AIUsage

type StreamChoice

type StreamChoice struct {
	Index        int          `json:"index"`
	Delta        ContentDelta `json:"delta"`
	FinishReason *string      `json:"finish_reason"` // "stop", "length" | "tool_calls" | "content_filter"
}

type StreamChunk

type StreamChunk struct {
	ID      string         `json:"id"`
	Object  string         `json:"object"`
	Created int64          `json:"created"`
	Model   string         `json:"model"`
	Choices []StreamChoice `json:"choices"`
}

type ToolCall

type ToolCall struct {
	ID          string                        `json:"id"`
	Type        string                        `json:"type"` // "function"
	Function    ToolFunctionCall              `json:"function"`
	ToolUseData *uctypes.UIMessageDataToolUse `json:"toolusedata,omitempty"` // Internal field (must be cleaned before sending to API)
}

type ToolCallDelta

type ToolCallDelta struct {
	Index    int                `json:"index"`
	ID       string             `json:"id,omitempty"`   // only on first chunk
	Type     string             `json:"type,omitempty"` // "function"
	Function *ToolFunctionDelta `json:"function,omitempty"`
}

type ToolDefinition

type ToolDefinition struct {
	Type     string          `json:"type"` // "function"
	Function ToolFunctionDef `json:"function"`
}

type ToolFunctionCall

type ToolFunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // raw JSON string
}

type ToolFunctionDef

type ToolFunctionDef struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Parameters  map[string]any `json:"parameters,omitempty"` // or jsonschema struct
}

type ToolFunctionDelta

type ToolFunctionDelta struct {
	Name      string `json:"name,omitempty"`      // only on first chunk
	Arguments string `json:"arguments,omitempty"` // streamed, append across chunks
}

Jump to

Keyboard shortcuts

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