|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package gemini implements the Google Gemini backend for WaveTerm's AI chat system. |
| 5 | +// |
| 6 | +// This package provides a complete implementation of the UseChatBackend interface |
| 7 | +// for Google's Gemini API, including: |
| 8 | +// - Streaming chat responses via Server-Sent Events (SSE) |
| 9 | +// - Function calling (tool use) support |
| 10 | +// - Multi-modal input support (text, images, PDFs) |
| 11 | +// - Proper message conversion and state management |
| 12 | +// |
| 13 | +// # API Type |
| 14 | +// |
| 15 | +// The Gemini backend uses the API type constant: |
| 16 | +// uctypes.APIType_GoogleGemini = "google-gemini" |
| 17 | +// |
| 18 | +// # Supported Features |
| 19 | +// |
| 20 | +// - Text messages |
| 21 | +// - Image uploads (JPEG, PNG, etc.) - inline base64 encoding |
| 22 | +// - PDF document uploads - inline base64 encoding |
| 23 | +// - Text file attachments |
| 24 | +// - Directory listings |
| 25 | +// - Function/tool calling with structured arguments |
| 26 | +// - Streaming responses with real-time token delivery |
| 27 | +// |
| 28 | +// # Usage |
| 29 | +// |
| 30 | +// The backend is automatically registered and can be obtained via: |
| 31 | +// |
| 32 | +// backend, err := aiusechat.GetBackendByAPIType(uctypes.APIType_GoogleGemini) |
| 33 | +// |
| 34 | +// To use the Gemini API, you need: |
| 35 | +// 1. A Google AI API key |
| 36 | +// 2. Configure the chat with APIType_GoogleGemini |
| 37 | +// 3. Set the Model (e.g., "gemini-2.0-flash-exp") |
| 38 | +// 4. Provide the API key in the Config.APIToken field |
| 39 | +// |
| 40 | +// # Configuration Example |
| 41 | +// |
| 42 | +// chatOpts := uctypes.WaveChatOpts{ |
| 43 | +// ChatId: "my-chat-id", |
| 44 | +// ClientId: "my-client-id", |
| 45 | +// Config: uctypes.AIOptsType{ |
| 46 | +// APIType: uctypes.APIType_GoogleGemini, |
| 47 | +// Model: "gemini-2.0-flash-exp", |
| 48 | +// APIToken: "your-google-api-key", |
| 49 | +// MaxTokens: 8192, |
| 50 | +// Capabilities: []string{ |
| 51 | +// uctypes.AICapabilityTools, |
| 52 | +// uctypes.AICapabilityImages, |
| 53 | +// uctypes.AICapabilityPdfs, |
| 54 | +// }, |
| 55 | +// }, |
| 56 | +// Tools: []uctypes.ToolDefinition{...}, |
| 57 | +// SystemPrompt: []string{"You are a helpful assistant."}, |
| 58 | +// } |
| 59 | +// |
| 60 | +// # Message Format |
| 61 | +// |
| 62 | +// The Gemini backend uses the GeminiChatMessage type internally, which stores: |
| 63 | +// - MessageId: Unique identifier for idempotency |
| 64 | +// - Role: "user" or "model" (model is Gemini's term for assistant) |
| 65 | +// - Parts: Array of message parts (text, inline data, function calls/responses) |
| 66 | +// - Usage: Token usage metadata |
| 67 | +// |
| 68 | +// # Function Calling |
| 69 | +// |
| 70 | +// Function calling is supported via Gemini's native function calling feature: |
| 71 | +// - Tools are converted to Gemini's FunctionDeclaration format |
| 72 | +// - Function calls are streamed with real-time argument updates |
| 73 | +// - Function responses are sent back as user messages with FunctionResponse parts |
| 74 | +// |
| 75 | +// # API Endpoint |
| 76 | +// |
| 77 | +// By default, the backend uses: |
| 78 | +// https://generativelanguage.googleapis.com/v1beta/models/{model}:streamGenerateContent |
| 79 | +// |
| 80 | +// You can override this by setting Config.BaseURL. |
| 81 | +// |
| 82 | +// # Error Handling |
| 83 | +// |
| 84 | +// The backend properly handles: |
| 85 | +// - Content blocking/safety filters |
| 86 | +// - Token limit errors |
| 87 | +// - Network errors |
| 88 | +// - Malformed responses |
| 89 | +// - Context cancellation |
| 90 | +// |
| 91 | +// All errors are properly propagated through the SSE stream. |
| 92 | +// |
| 93 | +// # Limitations |
| 94 | +// |
| 95 | +// - File uploads must be provided as base64-encoded inline data |
| 96 | +// - Images and PDFs use inline data, not file upload URIs |
| 97 | +// - Multi-turn conversations require proper role alternation (user/model) |
| 98 | +// - Some advanced Gemini features like caching are not yet implemented |
| 99 | +package gemini |
0 commit comments