1// Package openai provides an implementation of the fantasy AI SDK for OpenAI's language models.
  2package openai
  3
  4import (
  5	"slices"
  6
  7	"charm.land/fantasy"
  8)
  9
 10// ResponsesReasoningMetadata represents reasoning metadata for OpenAI Responses API.
 11type ResponsesReasoningMetadata struct {
 12	ItemID           string   `json:"item_id"`
 13	EncryptedContent *string  `json:"encrypted_content"`
 14	Summary          []string `json:"summary"`
 15}
 16
 17// Options implements the ProviderOptions interface.
 18func (*ResponsesReasoningMetadata) Options() {}
 19
 20// IncludeType represents the type of content to include for OpenAI Responses API.
 21type IncludeType string
 22
 23const (
 24	// IncludeReasoningEncryptedContent includes encrypted reasoning content.
 25	IncludeReasoningEncryptedContent IncludeType = "reasoning.encrypted_content"
 26	// IncludeFileSearchCallResults includes file search call results.
 27	IncludeFileSearchCallResults IncludeType = "file_search_call.results"
 28	// IncludeMessageOutputTextLogprobs includes message output text log probabilities.
 29	IncludeMessageOutputTextLogprobs IncludeType = "message.output_text.logprobs"
 30)
 31
 32// ServiceTier represents the service tier for OpenAI Responses API.
 33type ServiceTier string
 34
 35const (
 36	// ServiceTierAuto represents the auto service tier.
 37	ServiceTierAuto ServiceTier = "auto"
 38	// ServiceTierFlex represents the flex service tier.
 39	ServiceTierFlex ServiceTier = "flex"
 40	// ServiceTierPriority represents the priority service tier.
 41	ServiceTierPriority ServiceTier = "priority"
 42)
 43
 44// TextVerbosity represents the text verbosity level for OpenAI Responses API.
 45type TextVerbosity string
 46
 47const (
 48	// TextVerbosityLow represents low text verbosity.
 49	TextVerbosityLow TextVerbosity = "low"
 50	// TextVerbosityMedium represents medium text verbosity.
 51	TextVerbosityMedium TextVerbosity = "medium"
 52	// TextVerbosityHigh represents high text verbosity.
 53	TextVerbosityHigh TextVerbosity = "high"
 54)
 55
 56// ResponsesProviderOptions represents additional options for OpenAI Responses API.
 57type ResponsesProviderOptions struct {
 58	Include           []IncludeType    `json:"include"`
 59	Instructions      *string          `json:"instructions"`
 60	Logprobs          any              `json:"logprobs"`
 61	MaxToolCalls      *int64           `json:"max_tool_calls"`
 62	Metadata          map[string]any   `json:"metadata"`
 63	ParallelToolCalls *bool            `json:"parallel_tool_calls"`
 64	PromptCacheKey    *string          `json:"prompt_cache_key"`
 65	ReasoningEffort   *ReasoningEffort `json:"reasoning_effort"`
 66	ReasoningSummary  *string          `json:"reasoning_summary"`
 67	SafetyIdentifier  *string          `json:"safety_identifier"`
 68	ServiceTier       *ServiceTier     `json:"service_tier"`
 69	StrictJSONSchema  *bool            `json:"strict_json_schema"`
 70	TextVerbosity     *TextVerbosity   `json:"text_verbosity"`
 71	User              *string          `json:"user"`
 72}
 73
 74// responsesReasoningModelIds lists the model IDs that support reasoning for OpenAI Responses API.
 75var responsesReasoningModelIDs = []string{
 76	"o1",
 77	"o1-2024-12-17",
 78	"o3-mini",
 79	"o3-mini-2025-01-31",
 80	"o3",
 81	"o3-2025-04-16",
 82	"o4-mini",
 83	"o4-mini-2025-04-16",
 84	"codex-mini-latest",
 85	"gpt-5",
 86	"gpt-5-2025-08-07",
 87	"gpt-5-mini",
 88	"gpt-5-mini-2025-08-07",
 89	"gpt-5-nano",
 90	"gpt-5-nano-2025-08-07",
 91	"gpt-5-codex",
 92}
 93
 94// responsesModelIds lists all model IDs for OpenAI Responses API.
 95var responsesModelIDs = append([]string{
 96	"gpt-4.1",
 97	"gpt-4.1-2025-04-14",
 98	"gpt-4.1-mini",
 99	"gpt-4.1-mini-2025-04-14",
100	"gpt-4.1-nano",
101	"gpt-4.1-nano-2025-04-14",
102	"gpt-4o",
103	"gpt-4o-2024-05-13",
104	"gpt-4o-2024-08-06",
105	"gpt-4o-2024-11-20",
106	"gpt-4o-mini",
107	"gpt-4o-mini-2024-07-18",
108	"gpt-4-turbo",
109	"gpt-4-turbo-2024-04-09",
110	"gpt-4-turbo-preview",
111	"gpt-4-0125-preview",
112	"gpt-4-1106-preview",
113	"gpt-4",
114	"gpt-4-0613",
115	"gpt-4.5-preview",
116	"gpt-4.5-preview-2025-02-27",
117	"gpt-3.5-turbo-0125",
118	"gpt-3.5-turbo",
119	"gpt-3.5-turbo-1106",
120	"chatgpt-4o-latest",
121	"gpt-5-chat-latest",
122}, responsesReasoningModelIDs...)
123
124// Options implements the ProviderOptions interface.
125func (*ResponsesProviderOptions) Options() {}
126
127// NewResponsesProviderOptions creates new provider options for OpenAI Responses API.
128func NewResponsesProviderOptions(opts *ResponsesProviderOptions) fantasy.ProviderOptions {
129	return fantasy.ProviderOptions{
130		Name: opts,
131	}
132}
133
134// ParseResponsesOptions parses provider options from a map for OpenAI Responses API.
135func ParseResponsesOptions(data map[string]any) (*ResponsesProviderOptions, error) {
136	var options ResponsesProviderOptions
137	if err := fantasy.ParseOptions(data, &options); err != nil {
138		return nil, err
139	}
140	return &options, nil
141}
142
143// IsResponsesModel checks if a model ID is a Responses API model for OpenAI.
144func IsResponsesModel(modelID string) bool {
145	return slices.Contains(responsesModelIDs, modelID)
146}
147
148// IsResponsesReasoningModel checks if a model ID is a Responses API reasoning model for OpenAI.
149func IsResponsesReasoningModel(modelID string) bool {
150	return slices.Contains(responsesReasoningModelIDs, modelID)
151}