xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / files

go-opencode-ai-zen-sdk / types_gemini.go

3.3 KB
1package zen
2
3import "encoding/json"
4
5type GeminiRequest struct {
6	Contents          []GeminiContent
7	SystemInstruction *GeminiContent
8	GenerationConfig  *GeminiGenerationConfig
9	Tools             []GeminiTool
10	ToolConfig        *GeminiToolConfig
11	Stream            bool
12	Extra             map[string]any
13}
14
15type GeminiContent struct {
16	Role  string       `json:"role,omitempty"`
17	Parts []GeminiPart `json:"parts"`
18}
19
20type GeminiFunctionCall struct {
21	Name string          `json:"name"`
22	Args json.RawMessage `json:"args,omitempty"`
23}
24
25type GeminiFunctionResponse struct {
26	Name     string                     `json:"name"`
27	Response GeminiFunctionResponseBody `json:"response"`
28}
29
30// GeminiFunctionResponseBody is the value of functionResponse.response sent to
31// Gemini. When Content is non-nil it is used verbatim (allowing callers to
32// provide structured JSON). Otherwise a plain {"output": <Output>} object is
33// marshalled, which is the simplest form accepted by the API.
34type GeminiFunctionResponseBody struct {
35	Output  string          `json:"output,omitempty"`
36	Content json.RawMessage `json:"-"` // if set, marshalled as the entire response object
37}
38
39func (b GeminiFunctionResponseBody) MarshalJSON() ([]byte, error) {
40	if len(b.Content) > 0 {
41		return b.Content, nil
42	}
43	return json.Marshal(struct {
44		Output string `json:"output"`
45	}{Output: b.Output})
46}
47
48type GeminiPart struct {
49	Text             string                  `json:"text,omitempty"`
50	FunctionCall     *GeminiFunctionCall     `json:"functionCall,omitempty"`
51	FunctionResponse *GeminiFunctionResponse `json:"functionResponse,omitempty"`
52	ThoughtSignature string                  `json:"thoughtSignature,omitempty"`
53}
54
55type GeminiTool struct {
56	FunctionDeclarations []GeminiFunctionDeclaration `json:"functionDeclarations,omitempty"`
57}
58
59type GeminiFunctionDeclaration struct {
60	Name        string          `json:"name"`
61	Description string          `json:"description,omitempty"`
62	Parameters  json.RawMessage `json:"parameters,omitempty"`
63}
64
65type GeminiToolConfig struct {
66	FunctionCallingConfig *GeminiFunctionCallingConfig `json:"functionCallingConfig,omitempty"`
67}
68
69type GeminiFunctionCallingConfig struct {
70	Mode                 string   `json:"mode,omitempty"`
71	AllowedFunctionNames []string `json:"allowedFunctionNames,omitempty"`
72}
73
74type GeminiGenerationConfig struct {
75	Temperature     *float64              `json:"temperature,omitempty"`
76	MaxOutputTokens *int                  `json:"maxOutputTokens,omitempty"`
77	ThinkingConfig  *GeminiThinkingConfig `json:"thinkingConfig,omitempty"`
78}
79
80type GeminiThinkingConfig struct {
81	ThinkingBudget  *int   `json:"thinkingBudget,omitempty"`
82	IncludeThoughts *bool  `json:"includeThoughts,omitempty"`
83	ThinkingLevel   string `json:"thinkingLevel,omitempty"`
84}
85
86func (r GeminiRequest) MarshalJSON() ([]byte, error) {
87	base := map[string]any{
88		"contents": r.Contents,
89	}
90	if r.SystemInstruction != nil {
91		base["systemInstruction"] = r.SystemInstruction
92	}
93	if r.GenerationConfig != nil {
94		base["generationConfig"] = r.GenerationConfig
95	}
96	if len(r.Tools) > 0 {
97		base["tools"] = r.Tools
98	}
99	if r.ToolConfig != nil {
100		base["toolConfig"] = r.ToolConfig
101	}
102	// Note: Gemini streaming is controlled by the URL (:streamGenerateContent?alt=sse),
103	// not a body field. The Stream field is intentionally omitted here.
104
105	return marshalWithExtra(base, r.Extra)
106}