mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
dd9adf8a04
* feat: add ElevenLabs Scribe STT transcriber and Telegram SendVoice support
Add ElevenLabsTranscriber as an alternative speech-to-text provider using
the ElevenLabs Scribe API (scribe_v1). This enables voice message
transcription for users who already have an ElevenLabs API key, without
requiring a separate Groq account.
Changes:
- Add ElevenLabsTranscriber implementing the Transcriber interface
- Update DetectTranscriber to check providers.elevenlabs.api_key first,
falling back to Groq for backward compatibility
- Add ElevenLabs to ProvidersConfig
- Add "voice" media type for OGG files with "voice" in filename
- Add SendVoice support in Telegram channel for voice bubble messages
- Add comprehensive tests for ElevenLabs transcriber
Configuration:
"providers": {
"elevenlabs": {
"api_key": "sk_your_key_here"
}
}
Closes #1503 (partial)
* fix: move voice-bubble detection into Telegram channel to avoid regression in other channels
Address review feedback: keep inferMediaType returning "audio" for all
OGG files. Voice-bubble detection (SendVoice vs SendAudio) is now done
inside the Telegram channel based on filename, so other channels that
map "audio" explicitly are unaffected.
* fix: align VoiceConfig struct tags to pass golines formatter
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix(agent): use ModelName in loop test added by upstream
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
142 lines
4.4 KiB
Go
142 lines
4.4 KiB
Go
package voice
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/logger"
|
|
"github.com/sipeed/picoclaw/pkg/utils"
|
|
)
|
|
|
|
// ElevenLabsTranscriber uses the ElevenLabs Scribe API for speech-to-text.
|
|
type ElevenLabsTranscriber struct {
|
|
apiKey string
|
|
apiBase string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewElevenLabsTranscriber(apiKey string) *ElevenLabsTranscriber {
|
|
logger.DebugCF("voice", "Creating ElevenLabs transcriber", map[string]any{"has_api_key": apiKey != ""})
|
|
|
|
return &ElevenLabsTranscriber{
|
|
apiKey: apiKey,
|
|
apiBase: "https://api.elevenlabs.io",
|
|
httpClient: &http.Client{
|
|
Timeout: 120 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (t *ElevenLabsTranscriber) Transcribe(ctx context.Context, audioFilePath string) (*TranscriptionResponse, error) {
|
|
logger.InfoCF("voice", "Starting ElevenLabs transcription", map[string]any{"audio_file": audioFilePath})
|
|
|
|
audioFile, err := os.Open(audioFilePath)
|
|
if err != nil {
|
|
logger.ErrorCF("voice", "Failed to open audio file", map[string]any{"path": audioFilePath, "error": err})
|
|
return nil, fmt.Errorf("failed to open audio file: %w", err)
|
|
}
|
|
defer audioFile.Close()
|
|
|
|
fileInfo, err := audioFile.Stat()
|
|
if err != nil {
|
|
logger.ErrorCF("voice", "Failed to get file info", map[string]any{"path": audioFilePath, "error": err})
|
|
return nil, fmt.Errorf("failed to get file info: %w", err)
|
|
}
|
|
|
|
logger.DebugCF("voice", "Audio file details", map[string]any{
|
|
"size_bytes": fileInfo.Size(),
|
|
"file_name": filepath.Base(audioFilePath),
|
|
})
|
|
|
|
var requestBody bytes.Buffer
|
|
writer := multipart.NewWriter(&requestBody)
|
|
|
|
part, err := writer.CreateFormFile("file", filepath.Base(audioFilePath))
|
|
if err != nil {
|
|
logger.ErrorCF("voice", "Failed to create form file", map[string]any{"error": err})
|
|
return nil, fmt.Errorf("failed to create form file: %w", err)
|
|
}
|
|
|
|
if _, err = io.Copy(part, audioFile); err != nil {
|
|
logger.ErrorCF("voice", "Failed to copy file content", map[string]any{"error": err})
|
|
return nil, fmt.Errorf("failed to copy file content: %w", err)
|
|
}
|
|
|
|
if err = writer.WriteField("model_id", "scribe_v1"); err != nil {
|
|
return nil, fmt.Errorf("failed to write model_id field: %w", err)
|
|
}
|
|
|
|
if err = writer.Close(); err != nil {
|
|
logger.ErrorCF("voice", "Failed to close multipart writer", map[string]any{"error": err})
|
|
return nil, fmt.Errorf("failed to close multipart writer: %w", err)
|
|
}
|
|
|
|
url := t.apiBase + "/v1/speech-to-text"
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, &requestBody)
|
|
if err != nil {
|
|
logger.ErrorCF("voice", "Failed to create request", map[string]any{"error": err})
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
req.Header.Set("Xi-Api-Key", t.apiKey)
|
|
|
|
logger.DebugCF("voice", "Sending transcription request to ElevenLabs API", map[string]any{
|
|
"url": url,
|
|
"request_size_bytes": requestBody.Len(),
|
|
"file_size_bytes": fileInfo.Size(),
|
|
})
|
|
|
|
resp, err := t.httpClient.Do(req)
|
|
if err != nil {
|
|
logger.ErrorCF("voice", "Failed to send request", map[string]any{"error": err})
|
|
return nil, fmt.Errorf("failed to send request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
logger.ErrorCF("voice", "Failed to read response", map[string]any{"error": err})
|
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
logger.ErrorCF("voice", "ElevenLabs API error", map[string]any{
|
|
"status_code": resp.StatusCode,
|
|
"response": string(body),
|
|
})
|
|
return nil, fmt.Errorf("ElevenLabs API error (status %d): %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
logger.DebugCF("voice", "Received response from ElevenLabs API", map[string]any{
|
|
"status_code": resp.StatusCode,
|
|
"response_size_bytes": len(body),
|
|
})
|
|
|
|
var result TranscriptionResponse
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
logger.ErrorCF("voice", "Failed to unmarshal response", map[string]any{"error": err})
|
|
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
|
}
|
|
|
|
logger.InfoCF("voice", "ElevenLabs transcription completed successfully", map[string]any{
|
|
"text_length": len(result.Text),
|
|
"language": result.Language,
|
|
"transcription_preview": utils.Truncate(result.Text, 50),
|
|
})
|
|
|
|
return &result, nil
|
|
}
|
|
|
|
func (t *ElevenLabsTranscriber) Name() string {
|
|
return "elevenlabs"
|
|
}
|