6312
Ga
go-opencode-ai-zen-sdk / files
go-opencode-ai-zen-sdk / normalized.go
16.3 KB
1package zen 2 3import ( 4 "encoding/base64" 5 "encoding/json" 6 "errors" 7 "sort" 8 "strings" 9) 10 11type ToolChoiceType string 12 13const ( 14 ToolChoiceAuto ToolChoiceType = "auto" 15 ToolChoiceNone ToolChoiceType = "none" 16 ToolChoiceRequired ToolChoiceType = "required" 17 ToolChoiceTool ToolChoiceType = "tool" 18) 19 20type NormalizedToolChoice struct { 21 Type ToolChoiceType 22 Name string 23} 24 25type NormalizedTool struct { 26 Name string 27 Description string 28 Parameters json.RawMessage 29} 30 31type NormalizedReasoning struct { 32 Effort string 33 BudgetTokens int 34} 35 36type NormalizedToolCall struct { 37 ID string 38 Name string 39 Arguments json.RawMessage 40 ThoughtSignature string 41} 42 43const geminiSignatureSeparator = "|ts=" 44 45func encodeGeminiToolCallID(callID, signature string) string { 46 if signature == "" { 47 return callID 48 } 49 if strings.Contains(callID, geminiSignatureSeparator) { 50 return callID 51 } 52 encoded := base64.RawURLEncoding.EncodeToString([]byte(signature)) 53 return callID + geminiSignatureSeparator + encoded 54} 55 56func decodeGeminiThoughtSignature(callID string) (string, string) { 57 idx := strings.Index(callID, geminiSignatureSeparator) 58 if idx == -1 { 59 return callID, "" 60 } 61 base := callID[:idx] 62 encoded := callID[idx+len(geminiSignatureSeparator):] 63 if encoded == "" { 64 return base, "" 65 } 66 decoded, err := base64.RawURLEncoding.DecodeString(encoded) 67 if err != nil { 68 return base, "" 69 } 70 return base, string(decoded) 71} 72 73func sortedNormalizedTools(tools []NormalizedTool) []NormalizedTool { 74 if len(tools) <= 1 { 75 return tools 76 } 77 sorted := make([]NormalizedTool, len(tools)) 78 copy(sorted, tools) 79 sort.Slice(sorted, func(i, j int) bool { 80 return sorted[i].Name < sorted[j].Name 81 }) 82 return sorted 83} 84 85type NormalizedMessage struct { 86 Role string 87 Content string 88 ToolCalls []NormalizedToolCall // set on assistant messages that invoked tools 89 ToolCallID string // set on tool-result messages (role "tool") 90 FunctionName string // set on tool-result messages (role "tool"): name of the called function; required by Gemini 91} 92 93type NormalizedRequest struct { 94 Model string 95 System string 96 Messages []NormalizedMessage 97 Tools []NormalizedTool 98 ToolChoice *NormalizedToolChoice 99 Reasoning *NormalizedReasoning 100 Temperature *float64 101 MaxTokens *int 102 Stream bool 103 Endpoint EndpointType 104 Extra map[string]any 105} 106 107func (r NormalizedRequest) ToResponsesRequest() (*ResponsesRequest, error) { 108 req := &ResponsesRequest{ 109 Model: r.Model, 110 Temperature: r.Temperature, 111 MaxOutputTokens: r.MaxTokens, 112 Stream: r.Stream, 113 Extra: r.Extra, 114 } 115 116 messages := make([]NormalizedMessage, 0, len(r.Messages)+1) 117 if strings.TrimSpace(r.System) != "" { 118 messages = append(messages, NormalizedMessage{Role: "system", Content: r.System}) 119 } 120 messages = append(messages, r.Messages...) 121 122 if len(messages) == 0 { 123 req.Input = "" 124 } else { 125 items := make([]any, 0, len(messages)) 126 for _, m := range messages { 127 // Tool result message → function_call_output item. 128 if strings.ToLower(strings.TrimSpace(m.Role)) == "tool" { 129 items = append(items, ResponsesFunctionCallOutput{ 130 Type: "function_call_output", 131 CallID: m.ToolCallID, 132 Output: m.Content, 133 }) 134 continue 135 } 136 // Assistant message with tool calls → optional text item + function_call items. 137 if strings.ToLower(strings.TrimSpace(m.Role)) == "assistant" && len(m.ToolCalls) > 0 { 138 if strings.TrimSpace(m.Content) != "" { 139 items = append(items, ResponsesInputMessage{ 140 Role: m.Role, 141 Content: []ResponsesInputContent{{ 142 Type: "output_text", 143 Text: m.Content, 144 }}, 145 }) 146 } 147 for _, tc := range m.ToolCalls { 148 items = append(items, ResponsesFunctionCall{ 149 Type: "function_call", 150 CallID: tc.ID, 151 Name: tc.Name, 152 Arguments: string(tc.Arguments), 153 }) 154 } 155 continue 156 } 157 158 role := strings.ToLower(strings.TrimSpace(m.Role)) 159 contentType := "input_text" 160 if role == "assistant" { 161 contentType = "output_text" 162 } 163 items = append(items, ResponsesInputMessage{ 164 Role: m.Role, 165 Content: []ResponsesInputContent{{ 166 Type: contentType, 167 Text: m.Content, 168 }}, 169 }) 170 } 171 req.Input = items 172 } 173 174 if r.Reasoning != nil { 175 reasoning := &ResponsesReasoning{Effort: r.Reasoning.Effort} 176 if reasoning.Summary == "" { 177 reasoning.Summary = "auto" 178 } 179 if reasoning.Effort != "" || reasoning.Summary != "" { 180 req.Reasoning = reasoning 181 } 182 } 183 184 if len(r.Tools) > 0 { 185 tools := sortedNormalizedTools(r.Tools) 186 req.Tools = make([]ResponsesTool, 0, len(tools)) 187 for _, t := range tools { 188 req.Tools = append(req.Tools, ResponsesTool{ 189 Type: "function", 190 Name: t.Name, 191 Description: t.Description, 192 Parameters: t.Parameters, 193 }) 194 } 195 } 196 197 if r.ToolChoice != nil { 198 choice, err := mapOpenAIToolChoice(*r.ToolChoice) 199 if err != nil { 200 return nil, err 201 } 202 req.ToolChoice = choice 203 } 204 205 return req, nil 206} 207 208func (r NormalizedRequest) ToChatCompletionsRequest() (*ChatCompletionsRequest, error) { 209 messages := make([]ChatMessage, 0, len(r.Messages)+1) 210 if strings.TrimSpace(r.System) != "" { 211 messages = append(messages, ChatMessage{Role: "system", Content: r.System}) 212 } 213 for _, m := range r.Messages { 214 cm := ChatMessage{Role: m.Role, Content: m.Content, ToolCallID: m.ToolCallID} 215 if len(m.ToolCalls) > 0 { 216 cm.ToolCalls = make([]ChatMessageToolCall, 0, len(m.ToolCalls)) 217 for _, tc := range m.ToolCalls { 218 cm.ToolCalls = append(cm.ToolCalls, ChatMessageToolCall{ 219 ID: tc.ID, 220 Type: "function", 221 Function: ChatMessageToolCallFunc{ 222 Name: tc.Name, 223 Arguments: string(tc.Arguments), 224 }, 225 }) 226 } 227 } 228 messages = append(messages, cm) 229 } 230 231 req := &ChatCompletionsRequest{ 232 Model: r.Model, 233 Messages: messages, 234 Temperature: r.Temperature, 235 MaxTokens: r.MaxTokens, 236 Stream: r.Stream, 237 Extra: r.Extra, 238 } 239 240 if r.Reasoning != nil && r.Reasoning.Effort != "" { 241 if req.Extra == nil { 242 req.Extra = map[string]any{} 243 } 244 // OpenAI-compatible reasoning uses reasoning_effort, not reasoning. 245 req.Extra["reasoning_effort"] = r.Reasoning.Effort 246 } 247 248 if len(r.Tools) > 0 { 249 tools := sortedNormalizedTools(r.Tools) 250 req.Tools = make([]ChatTool, 0, len(tools)) 251 for _, t := range tools { 252 req.Tools = append(req.Tools, ChatTool{ 253 Type: "function", 254 Function: ChatToolFunction(t), 255 }) 256 } 257 } 258 259 if r.ToolChoice != nil { 260 choice, err := mapOpenAIToolChoice(*r.ToolChoice) 261 if err != nil { 262 return nil, err 263 } 264 req.ToolChoice = choice 265 } 266 267 return req, nil 268} 269 270func (r NormalizedRequest) ToMessagesRequest() (*MessagesRequest, error) { 271 system, messages := normalizeAnthropicMessages(r.System, r.Messages) 272 273 // Anthropic's messages API requires max_tokens; apply a default when the 274 // caller did not specify one so the normalized path works out of the box. 275 maxTokens := r.MaxTokens 276 277 // Resolve the thinking budget early so we can ensure max_tokens > budget_tokens, 278 // which is required by Anthropic's API. 279 var thinkingBudget int 280 if r.Reasoning != nil { 281 thinkingBudget = r.Reasoning.BudgetTokens 282 if thinkingBudget == 0 && r.Reasoning.Effort != "" { 283 thinkingBudget = mapEffortToBudget(r.Reasoning.Effort) 284 } 285 } 286 287 if maxTokens == nil { 288 defaultMax := 1024 289 // When reasoning is active, max_tokens must be strictly greater than 290 // budget_tokens. Default to twice the budget so there is room for the 291 // actual response text. 292 if thinkingBudget > 0 && thinkingBudget >= defaultMax { 293 defaultMax = thinkingBudget * 2 294 } 295 maxTokens = &defaultMax 296 } 297 298 req := &MessagesRequest{ 299 Model: r.Model, 300 System: system, 301 Messages: messages, 302 Temperature: r.Temperature, 303 MaxTokens: maxTokens, 304 Stream: r.Stream, 305 Extra: r.Extra, 306 } 307 308 if thinkingBudget > 0 { 309 req.Thinking = &AnthropicThinking{Type: "enabled", BudgetTokens: thinkingBudget} 310 } 311 312 if r.ToolChoice != nil && r.ToolChoice.Type == ToolChoiceNone { 313 return req, nil 314 } 315 316 if len(r.Tools) > 0 { 317 tools := sortedNormalizedTools(r.Tools) 318 req.Tools = make([]AnthropicTool, 0, len(tools)) 319 for _, t := range tools { 320 req.Tools = append(req.Tools, AnthropicTool{ 321 Name: t.Name, 322 Description: t.Description, 323 InputSchema: t.Parameters, 324 }) 325 } 326 } 327 328 if r.ToolChoice != nil { 329 choice, err := mapAnthropicToolChoice(*r.ToolChoice) 330 if err != nil { 331 return nil, err 332 } 333 req.ToolChoice = choice 334 } 335 336 return req, nil 337} 338 339func (r NormalizedRequest) ToGeminiRequest() (*GeminiRequest, error) { 340 systemText, messages := splitSystemMessages(r.System, r.Messages) 341 342 // Build a call-id → function-name index from all assistant tool calls so 343 // that tool-result messages can have their FunctionName derived 344 // automatically when the caller did not set it explicitly. 345 callIDToName := make(map[string]string) 346 for _, m := range messages { 347 if strings.ToLower(strings.TrimSpace(m.Role)) == "assistant" { 348 for _, tc := range m.ToolCalls { 349 if tc.ID != "" && tc.Name != "" { 350 callIDToName[tc.ID] = tc.Name 351 } 352 } 353 } 354 } 355 356 contents := make([]GeminiContent, 0, len(messages)) 357 for _, m := range messages { 358 role := strings.ToLower(strings.TrimSpace(m.Role)) 359 360 // Tool result: role "tool" → user turn with functionResponse parts. 361 if role == "tool" { 362 name := m.FunctionName 363 if name == "" { 364 name = callIDToName[m.ToolCallID] 365 } 366 if name == "" { 367 return nil, errors.New("zen: tool result message is missing FunctionName (required by Gemini)") 368 } 369 contents = append(contents, GeminiContent{ 370 Role: "user", 371 Parts: []GeminiPart{{ 372 FunctionResponse: &GeminiFunctionResponse{ 373 Name: name, 374 Response: GeminiFunctionResponseBody{Output: m.Content}, 375 }, 376 }}, 377 }) 378 continue 379 } 380 381 if role == "assistant" { 382 role = "model" 383 } 384 385 // Assistant message with tool calls → model turn with functionCall parts. 386 if role == "model" && len(m.ToolCalls) > 0 { 387 parts := make([]GeminiPart, 0, len(m.ToolCalls)) 388 for _, tc := range m.ToolCalls { 389 signature := tc.ThoughtSignature 390 if signature == "" { 391 _, signature = decodeGeminiThoughtSignature(tc.ID) 392 } 393 parts = append(parts, GeminiPart{ 394 FunctionCall: &GeminiFunctionCall{ 395 Name: tc.Name, 396 Args: tc.Arguments, 397 }, 398 ThoughtSignature: signature, 399 }) 400 } 401 contents = append(contents, GeminiContent{Role: role, Parts: parts}) 402 continue 403 } 404 405 contents = append(contents, GeminiContent{ 406 Role: role, 407 Parts: []GeminiPart{{Text: m.Content}}, 408 }) 409 } 410 411 var systemInstruction *GeminiContent 412 if strings.TrimSpace(systemText) != "" { 413 systemInstruction = &GeminiContent{ 414 Role: "system", 415 Parts: []GeminiPart{{Text: systemText}}, 416 } 417 } 418 419 req := &GeminiRequest{ 420 Contents: contents, 421 SystemInstruction: systemInstruction, 422 Stream: r.Stream, 423 Extra: r.Extra, 424 } 425 426 config := &GeminiGenerationConfig{ 427 Temperature: r.Temperature, 428 MaxOutputTokens: r.MaxTokens, 429 } 430 if r.Reasoning != nil { 431 thinking := &GeminiThinkingConfig{} 432 if r.Reasoning.BudgetTokens > 0 { 433 budget := r.Reasoning.BudgetTokens 434 thinking.ThinkingBudget = &budget 435 } 436 if r.Reasoning.Effort != "" { 437 level := mapEffortToThinkingLevel(r.Reasoning.Effort) 438 if level != "" { 439 thinking.ThinkingLevel = level 440 } 441 } 442 if thinking.ThinkingBudget != nil || thinking.ThinkingLevel != "" { 443 include := true 444 thinking.IncludeThoughts = &include 445 config.ThinkingConfig = thinking 446 } 447 } 448 if config.Temperature != nil || config.MaxOutputTokens != nil || config.ThinkingConfig != nil { 449 req.GenerationConfig = config 450 } 451 452 if len(r.Tools) > 0 { 453 tools := sortedNormalizedTools(r.Tools) 454 tool := GeminiTool{FunctionDeclarations: make([]GeminiFunctionDeclaration, 0, len(tools))} 455 for _, t := range tools { 456 tool.FunctionDeclarations = append(tool.FunctionDeclarations, GeminiFunctionDeclaration(t)) 457 } 458 req.Tools = []GeminiTool{tool} 459 } 460 461 if r.ToolChoice != nil { 462 config, err := mapGeminiToolChoice(*r.ToolChoice) 463 if err != nil { 464 return nil, err 465 } 466 if config != nil { 467 req.ToolConfig = config 468 } 469 } 470 471 return req, nil 472} 473 474func mapOpenAIToolChoice(choice NormalizedToolChoice) (any, error) { 475 switch choice.Type { 476 case ToolChoiceAuto: 477 return "auto", nil 478 case ToolChoiceNone: 479 return "none", nil 480 case ToolChoiceRequired: 481 return "required", nil 482 case ToolChoiceTool: 483 if strings.TrimSpace(choice.Name) == "" { 484 return nil, errors.New("zen: tool choice name is required") 485 } 486 return map[string]any{ 487 "type": "function", 488 "function": map[string]any{ 489 "name": choice.Name, 490 }, 491 }, nil 492 default: 493 return nil, errors.New("zen: unsupported tool choice") 494 } 495} 496 497func mapAnthropicToolChoice(choice NormalizedToolChoice) (*AnthropicToolChoice, error) { 498 switch choice.Type { 499 case ToolChoiceAuto: 500 return &AnthropicToolChoice{Type: "auto"}, nil 501 case ToolChoiceRequired: 502 return &AnthropicToolChoice{Type: "any"}, nil 503 case ToolChoiceTool: 504 if strings.TrimSpace(choice.Name) == "" { 505 return nil, errors.New("zen: tool choice name is required") 506 } 507 return &AnthropicToolChoice{Type: "tool", Name: choice.Name}, nil 508 case ToolChoiceNone: 509 return nil, nil 510 default: 511 return nil, errors.New("zen: unsupported tool choice") 512 } 513} 514 515func normalizeAnthropicMessages(system string, msgs []NormalizedMessage) (string, []AnthropicMessage) { 516 combinedSystem := strings.TrimSpace(system) 517 out := make([]AnthropicMessage, 0, len(msgs)) 518 519 for _, m := range msgs { 520 role := strings.ToLower(strings.TrimSpace(m.Role)) 521 if role == "system" || role == "developer" { 522 if strings.TrimSpace(m.Content) != "" { 523 if combinedSystem != "" { 524 combinedSystem += "\n\n" 525 } 526 combinedSystem += m.Content 527 } 528 continue 529 } 530 531 // Tool result: role "tool" maps to a "user" message with a tool_result block. 532 if role == "tool" { 533 out = append(out, AnthropicMessage{ 534 Role: "user", 535 Content: []AnthropicContentBlock{ 536 {Type: "tool_result", ToolUseID: m.ToolCallID, Content: m.Content}, 537 }, 538 }) 539 continue 540 } 541 542 // Assistant message with tool calls: emit content blocks of type "tool_use". 543 if role == "assistant" && len(m.ToolCalls) > 0 { 544 blocks := make([]AnthropicContentBlock, 0, len(m.ToolCalls)+1) 545 if strings.TrimSpace(m.Content) != "" { 546 blocks = append(blocks, AnthropicContentBlock{Type: "text", Text: m.Content}) 547 } 548 for _, tc := range m.ToolCalls { 549 blocks = append(blocks, AnthropicContentBlock{ 550 Type: "tool_use", 551 ID: tc.ID, 552 Name: tc.Name, 553 Input: tc.Arguments, 554 }) 555 } 556 out = append(out, AnthropicMessage{Role: "assistant", Content: blocks}) 557 continue 558 } 559 560 out = append(out, AnthropicMessage{ 561 Role: role, 562 Content: m.Content, 563 }) 564 } 565 566 return combinedSystem, out 567} 568 569func splitSystemMessages(system string, msgs []NormalizedMessage) (string, []NormalizedMessage) { 570 combinedSystem := strings.TrimSpace(system) 571 out := make([]NormalizedMessage, 0, len(msgs)) 572 573 for _, m := range msgs { 574 role := strings.ToLower(strings.TrimSpace(m.Role)) 575 if role == "system" || role == "developer" { 576 if strings.TrimSpace(m.Content) != "" { 577 if combinedSystem != "" { 578 combinedSystem += "\n\n" 579 } 580 combinedSystem += m.Content 581 } 582 continue 583 } 584 out = append(out, m) 585 } 586 587 return combinedSystem, out 588} 589 590func mapEffortToBudget(effort string) int { 591 switch strings.ToLower(strings.TrimSpace(effort)) { 592 case "low": 593 return 1024 594 case "medium": 595 return 2048 596 case "high": 597 return 4096 598 default: 599 return 0 600 } 601} 602 603func mapEffortToThinkingLevel(effort string) string { 604 switch strings.ToLower(strings.TrimSpace(effort)) { 605 case "low": 606 return "low" 607 case "medium": 608 return "medium" 609 case "high": 610 return "high" 611 default: 612 return "" 613 } 614} 615 616func mapGeminiToolChoice(choice NormalizedToolChoice) (*GeminiToolConfig, error) { 617 switch choice.Type { 618 case ToolChoiceAuto: 619 return &GeminiToolConfig{FunctionCallingConfig: &GeminiFunctionCallingConfig{Mode: "AUTO"}}, nil 620 case ToolChoiceNone: 621 return &GeminiToolConfig{FunctionCallingConfig: &GeminiFunctionCallingConfig{Mode: "NONE"}}, nil 622 case ToolChoiceRequired: 623 return &GeminiToolConfig{FunctionCallingConfig: &GeminiFunctionCallingConfig{Mode: "ANY"}}, nil 624 case ToolChoiceTool: 625 if strings.TrimSpace(choice.Name) == "" { 626 return nil, errors.New("zen: tool choice name is required") 627 } 628 return &GeminiToolConfig{FunctionCallingConfig: &GeminiFunctionCallingConfig{Mode: "ANY", AllowedFunctionNames: []string{choice.Name}}}, nil 629 default: 630 return nil, errors.New("zen: unsupported tool choice") 631 } 632}