provider_options.go

 1// Package openaicompat provides an implementation of the fantasy AI SDK for OpenAI-compatible APIs.
 2package openaicompat
 3
 4import (
 5	"encoding/json"
 6
 7	"charm.land/fantasy"
 8	"charm.land/fantasy/providers/openai"
 9)
10
11// Global type identifiers for OpenAI-compatible provider data.
12const (
13	TypeProviderOptions = Name + ".options"
14)
15
16// Register OpenAI-compatible provider-specific types with the global registry.
17func init() {
18	fantasy.RegisterProviderType(TypeProviderOptions, func(data []byte) (fantasy.ProviderOptionsData, error) {
19		var v ProviderOptions
20		if err := json.Unmarshal(data, &v); err != nil {
21			return nil, err
22		}
23		return &v, nil
24	})
25}
26
27// ProviderOptions represents additional options for the OpenAI-compatible provider.
28type ProviderOptions struct {
29	User            *string                 `json:"user"`
30	ReasoningEffort *openai.ReasoningEffort `json:"reasoning_effort"`
31}
32
33// ReasoningData represents reasoning data for OpenAI-compatible provider.
34type ReasoningData struct {
35	ReasoningContent string `json:"reasoning_content"`
36}
37
38// Options implements the ProviderOptions interface.
39func (*ProviderOptions) Options() {}
40
41// MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.
42func (o ProviderOptions) MarshalJSON() ([]byte, error) {
43	type plain ProviderOptions
44	return fantasy.MarshalProviderType(TypeProviderOptions, plain(o))
45}
46
47// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.
48func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
49	type plain ProviderOptions
50	var p plain
51	if err := fantasy.UnmarshalProviderType(data, &p); err != nil {
52		return err
53	}
54	*o = ProviderOptions(p)
55	return nil
56}
57
58// NewProviderOptions creates new provider options for the OpenAI-compatible provider.
59func NewProviderOptions(opts *ProviderOptions) fantasy.ProviderOptions {
60	return fantasy.ProviderOptions{
61		Name: opts,
62	}
63}
64
65// ParseOptions parses provider options from a map for OpenAI-compatible provider.
66func ParseOptions(data map[string]any) (*ProviderOptions, error) {
67	var options ProviderOptions
68	if err := fantasy.ParseOptions(data, &options); err != nil {
69		return nil, err
70	}
71	return &options, nil
72}