buildGeminiHTTPRequest creates an HTTP request for the Gemini API
(ctx context.Context, contents []GeminiContent, chatOpts uctypes.WaveChatOpts)
| 56 | |
| 57 | // buildGeminiHTTPRequest creates an HTTP request for the Gemini API |
| 58 | func buildGeminiHTTPRequest(ctx context.Context, contents []GeminiContent, chatOpts uctypes.WaveChatOpts) (*http.Request, error) { |
| 59 | opts := chatOpts.Config |
| 60 | |
| 61 | if opts.Model == "" { |
| 62 | return nil, errors.New("ai:model is required") |
| 63 | } |
| 64 | if opts.APIToken == "" { |
| 65 | return nil, errors.New("ai:apitoken is required") |
| 66 | } |
| 67 | if opts.Endpoint == "" { |
| 68 | return nil, errors.New("ai:endpoint is required") |
| 69 | } |
| 70 | |
| 71 | maxTokens := opts.MaxTokens |
| 72 | if maxTokens <= 0 { |
| 73 | maxTokens = GeminiDefaultMaxTokens |
| 74 | } |
| 75 | |
| 76 | // Build request body |
| 77 | reqBody := &GeminiRequest{ |
| 78 | Contents: contents, |
| 79 | GenerationConfig: &GeminiGenerationConfig{ |
| 80 | MaxOutputTokens: int32(maxTokens), |
| 81 | Temperature: 0.7, // Default temperature |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | // Map thinking level for Gemini 3+ models |
| 86 | if opts.ThinkingLevel != "" && strings.Contains(opts.Model, "gemini-3") { |
| 87 | geminiThinkingLevel := "high" |
| 88 | if opts.ThinkingLevel == uctypes.ThinkingLevelLow { |
| 89 | geminiThinkingLevel = "low" |
| 90 | } |
| 91 | reqBody.GenerationConfig.ThinkingConfig = &GeminiThinkingConfig{ |
| 92 | ThinkingLevel: geminiThinkingLevel, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Add system instruction if provided |
| 97 | if len(chatOpts.SystemPrompt) > 0 { |
| 98 | systemText := strings.Join(chatOpts.SystemPrompt, "\n\n") |
| 99 | reqBody.SystemInstruction = &GeminiContent{ |
| 100 | Parts: []GeminiMessagePart{ |
| 101 | {Text: systemText}, |
| 102 | }, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Add tools if provided |
| 107 | var allTools []uctypes.ToolDefinition |
| 108 | allTools = append(allTools, chatOpts.Tools...) |
| 109 | allTools = append(allTools, chatOpts.TabTools...) |
| 110 | |
| 111 | if len(allTools) > 0 { |
| 112 | var functionDeclarations []GeminiFunctionDeclaration |
| 113 | for _, tool := range allTools { |
| 114 | // Only include tools whose capabilities are met |
| 115 | if !tool.HasRequiredCapabilities(opts.Capabilities) { |
no test coverage detected