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 OpenRouter-specific provider data.
12const (
13	TypeProviderOptions = Name + ".options"
14)
15
16// ProviderOptions represents additional options for the OpenAI-compatible provider.
17type ProviderOptions struct {
18	User            *string                 `json:"user"`
19	ReasoningEffort *openai.ReasoningEffort `json:"reasoning_effort"`
20}
21
22// ReasoningData represents reasoning data for OpenAI-compatible provider.
23type ReasoningData struct {
24	ReasoningContent string `json:"reasoning_content"`
25}
26
27// Options implements the ProviderOptions interface.
28func (*ProviderOptions) Options() {}
29
30// MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.
31func (o ProviderOptions) MarshalJSON() ([]byte, error) {
32	type plain ProviderOptions
33	raw, err := json.Marshal(plain(o))
34	if err != nil {
35		return nil, err
36	}
37	return json.Marshal(struct {
38		Type string          `json:"type"`
39		Data json.RawMessage `json:"data"`
40	}{
41		Type: TypeProviderOptions,
42		Data: raw,
43	})
44}
45
46// UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.
47func (o *ProviderOptions) UnmarshalJSON(data []byte) error {
48	type plain ProviderOptions
49	var oo plain
50	err := json.Unmarshal(data, &oo)
51	if err != nil {
52		return err
53	}
54	*o = ProviderOptions(oo)
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}