CacheIdentity (speech/openai_tts.go)

clawbench · internal/speech/openai_tts.go · go · MIT
voice, Voice / Speech

Implements an OpenAI-compatible TTS provider with caching, streaming support, and error handling for speech synthesis.

// Source: https://github.com/clawbench-dev/clawbench | License: MIT | Copyright 2025 xulongzhe
// Provided via justclaw.it.com; keep this notice with the code.
package speech

import (
	"bufio"
	"context"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"time"
)

// openAISpeechPath is the OpenAI-compatible speech synthesis path.
const openAISpeechPath = "/v1/audio/speech"

// stream_format values accepted by the endpoint. SSE streams chunks; audio
// returns the whole body at once (and is the endpoint default).
const (
	streamFormatSSE   = "sse"
	streamFormatAudio = "audio"
)

// maxSSELineBytes bounds a single SSE `data:` line. Audio chunks arrive
// base64-encoded, so one line is roughly 4/3 of a chunk — a few KB in
// practice

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key