|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + geminiGenerateURL = "https://generativelanguage.googleapis.com/v1beta/models/%s:generateContent" |
| 16 | + transcribePrompt = "Transcribe this voice message exactly as spoken. Return only the transcription, no commentary." |
| 17 | + transcribeTimeout_ = 30 * time.Second |
| 18 | +) |
| 19 | + |
| 20 | +var transcribeHTTPClient = &http.Client{Timeout: transcribeTimeout_} |
| 21 | + |
| 22 | +// TranscribeConfig holds the Gemini model and API key used for audio transcription. |
| 23 | +type TranscribeConfig struct { |
| 24 | + Model string // e.g. "gemini-2.0-flash" |
| 25 | + APIKey string |
| 26 | +} |
| 27 | + |
| 28 | +// transcribeViaGemini calls the native Gemini generateContent API with inline audio data. |
| 29 | +// This bypasses the OpenAI-compat layer which does not reliably support input_audio. |
| 30 | +func transcribeViaGemini(ctx context.Context, cfg TranscribeConfig, audioData []byte, mimeType string) (string, error) { |
| 31 | + reqBody := map[string]any{ |
| 32 | + "contents": []map[string]any{ |
| 33 | + { |
| 34 | + "parts": []map[string]any{ |
| 35 | + {"text": transcribePrompt}, |
| 36 | + { |
| 37 | + "inline_data": map[string]any{ |
| 38 | + "mime_type": mimeType, |
| 39 | + "data": base64.StdEncoding.EncodeToString(audioData), |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + body, err := json.Marshal(reqBody) |
| 48 | + if err != nil { |
| 49 | + return "", fmt.Errorf("transcribe: marshal: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + url := fmt.Sprintf(geminiGenerateURL, cfg.Model) |
| 53 | + req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body)) |
| 54 | + if err != nil { |
| 55 | + return "", fmt.Errorf("transcribe: request: %w", err) |
| 56 | + } |
| 57 | + req.Header.Set("Content-Type", "application/json") |
| 58 | + req.Header.Set("x-goog-api-key", cfg.APIKey) |
| 59 | + |
| 60 | + resp, err := transcribeHTTPClient.Do(req) |
| 61 | + if err != nil { |
| 62 | + return "", fmt.Errorf("transcribe: %w", err) |
| 63 | + } |
| 64 | + defer resp.Body.Close() |
| 65 | + |
| 66 | + respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1*1024*1024)) |
| 67 | + if err != nil { |
| 68 | + return "", fmt.Errorf("transcribe: read: %w", err) |
| 69 | + } |
| 70 | + if resp.StatusCode != 200 { |
| 71 | + return "", fmt.Errorf("transcribe: HTTP %d: %s", resp.StatusCode, string(respBody)) |
| 72 | + } |
| 73 | + |
| 74 | + var result struct { |
| 75 | + Candidates []struct { |
| 76 | + Content struct { |
| 77 | + Parts []struct { |
| 78 | + Text string `json:"text"` |
| 79 | + } `json:"parts"` |
| 80 | + } `json:"content"` |
| 81 | + } `json:"candidates"` |
| 82 | + } |
| 83 | + if err := json.Unmarshal(respBody, &result); err != nil { |
| 84 | + return "", fmt.Errorf("transcribe: parse: %w", err) |
| 85 | + } |
| 86 | + if len(result.Candidates) == 0 || len(result.Candidates[0].Content.Parts) == 0 { |
| 87 | + return "", fmt.Errorf("transcribe: empty response") |
| 88 | + } |
| 89 | + return result.Candidates[0].Content.Parts[0].Text, nil |
| 90 | +} |
0 commit comments