xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / files

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

1.7 KB
1package zen
2
3import "encoding/json"
4
5type ChatCompletionsRequest struct {
6	Model       string
7	Messages    []ChatMessage
8	Reasoning   *ChatReasoning
9	Tools       []ChatTool
10	ToolChoice  any
11	Temperature *float64
12	MaxTokens   *int
13	Stream      bool
14	Extra       map[string]any
15}
16
17type ChatMessageToolCall struct {
18	ID       string                  `json:"id"`
19	Type     string                  `json:"type"`
20	Function ChatMessageToolCallFunc `json:"function"`
21}
22
23type ChatMessageToolCallFunc struct {
24	Name      string `json:"name"`
25	Arguments string `json:"arguments"`
26}
27
28type ChatMessage struct {
29	Role       string                `json:"role"`
30	Content    string                `json:"content,omitempty"`
31	ToolCalls  []ChatMessageToolCall `json:"tool_calls,omitempty"`
32	ToolCallID string                `json:"tool_call_id,omitempty"`
33}
34
35type ChatReasoning struct {
36	Effort string `json:"effort,omitempty"`
37}
38
39type ChatTool struct {
40	Type     string           `json:"type"`
41	Function ChatToolFunction `json:"function"`
42}
43
44type ChatToolFunction struct {
45	Name        string          `json:"name"`
46	Description string          `json:"description,omitempty"`
47	Parameters  json.RawMessage `json:"parameters,omitempty"`
48}
49
50func (r ChatCompletionsRequest) MarshalJSON() ([]byte, error) {
51	base := map[string]any{
52		"model":    r.Model,
53		"messages": r.Messages,
54	}
55	if r.Reasoning != nil {
56		base["reasoning"] = r.Reasoning
57	}
58	if len(r.Tools) > 0 {
59		base["tools"] = r.Tools
60	}
61	if r.ToolChoice != nil {
62		base["tool_choice"] = r.ToolChoice
63	}
64	if r.Temperature != nil {
65		base["temperature"] = r.Temperature
66	}
67	if r.MaxTokens != nil {
68		base["max_tokens"] = r.MaxTokens
69	}
70	if r.Stream {
71		base["stream"] = r.Stream
72	}
73
74	return marshalWithExtra(base, r.Extra)
75}