openai

package
v0.4.2-beta Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

Package openai provides OpenAI API client implementations. This file contains the factory function for creating OpenAI threads.

Package openai provides a client implementation for interacting with OpenAI and OpenAI-compatible AI models. It implements the LLM Thread interface for managing conversations, tool execution, and message processing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractMessages

func ExtractMessages(data []byte, toolResults map[string]tooltypes.StructuredToolResult) ([]llmtypes.Message, error)

ExtractMessages converts the internal message format to the common format

func ExtractResponsesMessages

func ExtractResponsesMessages(rawMessages []byte, toolResults map[string]tooltypes.StructuredToolResult) ([]llmtypes.Message, error)

ExtractResponsesMessages extracts messages from Responses API conversation data. This is a wrapper around the responses package's ExtractMessages function.

func GetAPIKeyEnvVar

func GetAPIKeyEnvVar(config llmtypes.Config) string

GetAPIKeyEnvVar returns the API key environment variable name from configuration. Priority: custom api_key_env_var > platform default > fallback to OPENAI_API_KEY.

func GetBaseURL

func GetBaseURL(config llmtypes.Config) string

GetBaseURL returns the base URL resolved from environment, config, and platform defaults.

func GetConfiguredBaseURL

func GetConfiguredBaseURL(config llmtypes.Config) string

GetConfiguredBaseURL returns only explicit base URL overrides from environment or config.

func IsOpenAIModel

func IsOpenAIModel(model string) bool

IsOpenAIModel checks if the given model is a valid OpenAI model (reasoning or non-reasoning).

func IsReasoningModel

func IsReasoningModel(model string) bool

IsReasoningModel checks if the given model supports reasoning capabilities.

func NewThread

func NewThread(config llmtypes.Config) (llmtypes.Thread, error)

NewThread creates a new OpenAI thread based on the configuration. It dispatches between the Chat Completions API and the Responses API based on api_mode.

func RecordUsesResponsesMode

func RecordUsesResponsesMode(metadata map[string]any, rawMessages []byte) bool

RecordUsesResponsesMode determines if a conversation should be interpreted using Responses API parsing.

func StreamResponsesMessages

func StreamResponsesMessages(rawMessages []byte, toolResults map[string]tooltypes.StructuredToolResult) ([]responses.StreamableMessage, error)

StreamResponsesMessages parses raw Responses API messages into streamable format. This is a wrapper around the responses package's StreamMessages function.

Types

type StreamableMessage

type StreamableMessage struct {
	Kind       string // "text", "tool-use", "tool-result", "thinking"
	Role       string // "user", "assistant", "system"
	Content    string // Text content
	RawItem    json.RawMessage
	ToolName   string // For tool use/result
	ToolCallID string // For matching tool results
	Input      string // For tool use (JSON string)
}

StreamableMessage contains parsed message data for streaming

func StreamMessages

func StreamMessages(rawMessages json.RawMessage, toolResults map[string]tooltypes.StructuredToolResult) ([]StreamableMessage, error)

StreamMessages parses raw messages into streamable format for conversation streaming

type Thread

type Thread struct {
	*base.Thread // Embedded base thread for shared functionality
	// contains filtered or unexported fields
}

Thread implements the Thread interface using OpenAI's API. It embeds base.Thread to inherit common functionality.

func NewOpenAIThread

func NewOpenAIThread(config llmtypes.Config) (*Thread, error)

NewOpenAIThread creates a new thread with OpenAI's API

func (*Thread) AddUserMessage

func (t *Thread) AddUserMessage(ctx context.Context, message string, imagePaths ...string)

AddUserMessage adds a user message with optional images to the thread

func (*Thread) CompactContext

func (t *Thread) CompactContext(ctx context.Context) error

CompactContext performs comprehensive context compacting by creating a detailed summary

func (*Thread) GetMessages

func (t *Thread) GetMessages() ([]llmtypes.Message, error)

GetMessages returns the current messages in the thread

func (*Thread) Provider

func (t *Thread) Provider() string

Provider returns the provider name for this thread.

func (*Thread) SaveConversation

func (t *Thread) SaveConversation(ctx context.Context, summarize bool) error

SaveConversation saves the current thread to the conversation store

func (*Thread) SendMessage

func (t *Thread) SendMessage(
	ctx context.Context,
	message string,
	handler llmtypes.MessageHandler,
	opt llmtypes.MessageOpt,
) (finalOutput string, err error)

SendMessage sends a message to the LLM and processes the response

func (*Thread) ShortSummary

func (t *Thread) ShortSummary(ctx context.Context) (string, error)

ShortSummary generates a concise summary of the conversation using a faster model.

func (*Thread) SwapContext

func (t *Thread) SwapContext(_ context.Context, summary string) error

SwapContext replaces the conversation history with a summary message. This implements the hooks.ContextSwapper interface.

Directories

Path Synopsis
Package copilotdefaults builds OpenAI-compatible model defaults from the Copilot model catalog.
Package copilotdefaults builds OpenAI-compatible model defaults from the Copilot model catalog.
preset
codex
Package codex provides preset configurations for Codex CLI models.
Package codex provides preset configurations for Codex CLI models.
openai
Package openai provides preset configurations for OpenAI models
Package openai provides preset configurations for OpenAI models
Package responses implements storage types for the OpenAI Responses API.
Package responses implements storage types for the OpenAI Responses API.

Jump to

Keyboard shortcuts

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