@@ -0,0 +1,360 @@
1+package zen
2+
3+import (
4+ "encoding/json"
5+ "errors"
6+ "strings"
7+)
8+
9+type ToolChoiceType string
10+
11+const (
12+ ToolChoiceAuto ToolChoiceType = "auto"
13+ ToolChoiceNone ToolChoiceType = "none"
14+ ToolChoiceRequired ToolChoiceType = "required"
15+ ToolChoiceTool ToolChoiceType = "tool"
16+)
17+
18+type NormalizedToolChoice struct {
19+ Type ToolChoiceType
20+ Name string
21+}
22+
23+type NormalizedTool struct {
24+ Name string
25+ Description string
26+ Parameters json.RawMessage
27+}
28+
29+type NormalizedReasoning struct {
30+ Effort string
31+ BudgetTokens int
32+}
33+
34+type NormalizedMessage struct {
35+ Role string
36+ Content string
37+}
38+
39+type NormalizedRequest struct {
40+ Model string
41+ System string
42+ Messages []NormalizedMessage
43+ Tools []NormalizedTool
44+ ToolChoice *NormalizedToolChoice
45+ Reasoning *NormalizedReasoning
46+ Temperature *float64
47+ MaxTokens *int
48+ Stream bool
49+ Endpoint EndpointType
50+ Extra map[string]any
51+}
52+
53+func (r NormalizedRequest) ToResponsesRequest() (*ResponsesRequest, error) {
54+ req := &ResponsesRequest{
55+ Model: r.Model,
56+ Instructions: r.System,
57+ Temperature: r.Temperature,
58+ MaxOutputTokens: r.MaxTokens,
59+ Stream: r.Stream,
60+ Extra: r.Extra,
61+ }
62+
63+ if len(r.Messages) == 0 {
64+ req.Input = ""
65+ } else {
66+ messages := make([]ResponsesInputMessage, 0, len(r.Messages))
67+ for _, m := range r.Messages {
68+ messages = append(messages, ResponsesInputMessage(m))
69+ }
70+ req.Input = messages
71+ }
72+
73+ if r.Reasoning != nil && r.Reasoning.Effort != "" {
74+ req.Reasoning = &ResponsesReasoning{Effort: r.Reasoning.Effort}
75+ }
76+
77+ if len(r.Tools) > 0 {
78+ req.Tools = make([]ResponsesTool, 0, len(r.Tools))
79+ for _, t := range r.Tools {
80+ req.Tools = append(req.Tools, ResponsesTool{
81+ Type: "function",
82+ Function: ResponsesToolFunction(t),
83+ })
84+ }
85+ }
86+
87+ if r.ToolChoice != nil {
88+ choice, err := mapOpenAIToolChoice(*r.ToolChoice)
89+ if err != nil {
90+ return nil, err
91+ }
92+ req.ToolChoice = choice
93+ }
94+
95+ return req, nil
96+}
97+
98+func (r NormalizedRequest) ToChatCompletionsRequest() (*ChatCompletionsRequest, error) {
99+ messages := make([]ChatMessage, 0, len(r.Messages)+1)
100+ if strings.TrimSpace(r.System) != "" {
101+ messages = append(messages, ChatMessage{Role: "system", Content: r.System})
102+ }
103+ for _, m := range r.Messages {
104+ messages = append(messages, ChatMessage(m))
105+ }
106+
107+ req := &ChatCompletionsRequest{
108+ Model: r.Model,
109+ Messages: messages,
110+ Temperature: r.Temperature,
111+ MaxTokens: r.MaxTokens,
112+ Stream: r.Stream,
113+ Extra: r.Extra,
114+ }
115+
116+ if r.Reasoning != nil && r.Reasoning.Effort != "" {
117+ req.Reasoning = &ChatReasoning{Effort: r.Reasoning.Effort}
118+ }
119+
120+ if len(r.Tools) > 0 {
121+ req.Tools = make([]ChatTool, 0, len(r.Tools))
122+ for _, t := range r.Tools {
123+ req.Tools = append(req.Tools, ChatTool{
124+ Type: "function",
125+ Function: ChatToolFunction(t),
126+ })
127+ }
128+ }
129+
130+ if r.ToolChoice != nil {
131+ choice, err := mapOpenAIToolChoice(*r.ToolChoice)
132+ if err != nil {
133+ return nil, err
134+ }
135+ req.ToolChoice = choice
136+ }
137+
138+ return req, nil
139+}
140+
141+func (r NormalizedRequest) ToMessagesRequest() (*MessagesRequest, error) {
142+ system, messages := normalizeAnthropicMessages(r.System, r.Messages)
143+ req := &MessagesRequest{
144+ Model: r.Model,
145+ System: system,
146+ Messages: messages,
147+ Temperature: r.Temperature,
148+ MaxTokens: r.MaxTokens,
149+ Stream: r.Stream,
150+ Extra: r.Extra,
151+ }
152+
153+ if r.Reasoning != nil {
154+ budget := r.Reasoning.BudgetTokens
155+ if budget == 0 && r.Reasoning.Effort != "" {
156+ budget = mapEffortToBudget(r.Reasoning.Effort)
157+ }
158+ if budget > 0 {
159+ req.Thinking = &AnthropicThinking{Type: "enabled", BudgetTokens: budget}
160+ }
161+ }
162+
163+ if r.ToolChoice != nil && r.ToolChoice.Type == ToolChoiceNone {
164+ return req, nil
165+ }
166+
167+ if len(r.Tools) > 0 {
168+ req.Tools = make([]AnthropicTool, 0, len(r.Tools))
169+ for _, t := range r.Tools {
170+ req.Tools = append(req.Tools, AnthropicTool{
171+ Name: t.Name,
172+ Description: t.Description,
173+ InputSchema: t.Parameters,
174+ })
175+ }
176+ }
177+
178+ if r.ToolChoice != nil {
179+ choice, err := mapAnthropicToolChoice(*r.ToolChoice)
180+ if err != nil {
181+ return nil, err
182+ }
183+ req.ToolChoice = choice
184+ }
185+
186+ return req, nil
187+}
188+
189+func (r NormalizedRequest) ToGeminiRequest() (*GeminiRequest, error) {
190+ contents := make([]GeminiContent, 0, len(r.Messages))
191+ for _, m := range r.Messages {
192+ role := strings.ToLower(strings.TrimSpace(m.Role))
193+ if role == "assistant" {
194+ role = "model"
195+ }
196+ contents = append(contents, GeminiContent{
197+ Role: role,
198+ Parts: []GeminiPart{{Text: m.Content}},
199+ })
200+ }
201+
202+ var systemInstruction *GeminiContent
203+ if strings.TrimSpace(r.System) != "" {
204+ systemInstruction = &GeminiContent{
205+ Role: "system",
206+ Parts: []GeminiPart{{Text: r.System}},
207+ }
208+ }
209+
210+ req := &GeminiRequest{
211+ Contents: contents,
212+ SystemInstruction: systemInstruction,
213+ Stream: r.Stream,
214+ Extra: r.Extra,
215+ }
216+
217+ config := &GeminiGenerationConfig{
218+ Temperature: r.Temperature,
219+ MaxOutputTokens: r.MaxTokens,
220+ }
221+ if r.Reasoning != nil && r.Reasoning.Effort != "" {
222+ level := mapEffortToThinkingLevel(r.Reasoning.Effort)
223+ if level != "" {
224+ config.ThinkingConfig = &GeminiThinkingConfig{ThinkingLevel: level}
225+ }
226+ }
227+ if config.Temperature != nil || config.MaxOutputTokens != nil || config.ThinkingConfig != nil {
228+ req.GenerationConfig = config
229+ }
230+
231+ if len(r.Tools) > 0 {
232+ tool := GeminiTool{FunctionDeclarations: make([]GeminiFunctionDeclaration, 0, len(r.Tools))}
233+ for _, t := range r.Tools {
234+ tool.FunctionDeclarations = append(tool.FunctionDeclarations, GeminiFunctionDeclaration(t))
235+ }
236+ req.Tools = []GeminiTool{tool}
237+ }
238+
239+ if r.ToolChoice != nil {
240+ config, err := mapGeminiToolChoice(*r.ToolChoice)
241+ if err != nil {
242+ return nil, err
243+ }
244+ if config != nil {
245+ req.ToolConfig = config
246+ }
247+ }
248+
249+ return req, nil
250+}
251+
252+func mapOpenAIToolChoice(choice NormalizedToolChoice) (any, error) {
253+ switch choice.Type {
254+ case ToolChoiceAuto:
255+ return "auto", nil
256+ case ToolChoiceNone:
257+ return "none", nil
258+ case ToolChoiceRequired:
259+ return "required", nil
260+ case ToolChoiceTool:
261+ if strings.TrimSpace(choice.Name) == "" {
262+ return nil, errors.New("zen: tool choice name is required")
263+ }
264+ return map[string]any{
265+ "type": "function",
266+ "function": map[string]any{
267+ "name": choice.Name,
268+ },
269+ }, nil
270+ default:
271+ return nil, errors.New("zen: unsupported tool choice")
272+ }
273+}
274+
275+func mapAnthropicToolChoice(choice NormalizedToolChoice) (*AnthropicToolChoice, error) {
276+ switch choice.Type {
277+ case ToolChoiceAuto:
278+ return &AnthropicToolChoice{Type: "auto"}, nil
279+ case ToolChoiceRequired:
280+ return &AnthropicToolChoice{Type: "any"}, nil
281+ case ToolChoiceTool:
282+ if strings.TrimSpace(choice.Name) == "" {
283+ return nil, errors.New("zen: tool choice name is required")
284+ }
285+ return &AnthropicToolChoice{Type: "tool", Name: choice.Name}, nil
286+ case ToolChoiceNone:
287+ return nil, nil
288+ default:
289+ return nil, errors.New("zen: unsupported tool choice")
290+ }
Cut short: this commit's diff is over 1000 lines.