Skip to content

The agent that grows with you

refactor(stt): shared REST transcript helper, flatten command-provider except ladder and hook override loop

Tekniuma4f17d7a4b0c
diff --git a/tools/transcription_cloud.py b/tools/transcription_cloud.py
index 92ef8d9221c..942e0c0d7a2 100644
--- a/tools/transcription_cloud.py
+++ b/tools/transcription_cloud.py
@@ -298,6 +298,22 @@ def _http_error_detail(response, extract) -> str:
return response.text[:300]
+def _rest_transcript(response, label: str, extract_detail, extract_text):
+ """Turn a multipart STT response into ``(text, body, None)`` or ``(None, None, error_envelope)``.
+
+ Non-200 -> ``"<label> API error (HTTP n): detail"``; empty text -> the
+ ``no_speech`` envelope so callers treat silence as non-fatal.
+ """
+ if response.status_code != 200:
+ detail = _http_error_detail(response, extract_detail)
+ return None, None, _error_result(f"{label} API error (HTTP {response.status_code}): {detail}")
+ body = response.json()
+ text = extract_text(body)
+ if not text:
+ return None, None, _error_result(f"{label} returned empty transcript", no_speech=True)
+ return text, body, None
+
+
def _elevenlabs_error_detail(err_body: Dict[str, Any]) -> str:
error_value = err_body.get("detail") or err_body.get("error")
if isinstance(error_value, dict):
@@ -400,24 +416,17 @@ def _transcribe_xai(
retry_exc,
)
- if response.status_code != 200:
- detail = _http_error_detail(response, lambda body: body.get("error", {}).get("message", ""))
- return _error_result(f"xAI STT API error (HTTP {response.status_code}): {detail}")
-
- result = response.json()
- transcript_text = result.get("text", "").strip()
-
- if not transcript_text:
- return _error_result("xAI STT returned empty transcript", no_speech=True)
-
+ transcript_text, result, error = _rest_transcript(
+ response, "xAI STT",
+ lambda body: body.get("error", {}).get("message", ""),
+ lambda body: body.get("text", "").strip(),
+ )
+ if error:
+ return error
logger.info(
"Transcribed %s via xAI Grok STT (lang=%s, %.1fs audio, %d chars)",
- Path(file_path).name,
- result.get("language", language),
- result.get("duration", 0),
- len(transcript_text),
+ Path(file_path).name, result.get("language", language), result.get("duration", 0), len(transcript_text),
)
-
return _ok_result(transcript_text, "xai")
except Exception as e:
@@ -443,14 +452,10 @@ def _transcribe_elevenlabs(
stt_config = _load_stt_config()
elevenlabs_config = stt_config.get("elevenlabs") or {}
base_url = str(
- elevenlabs_config.get("base_url")
- or get_env_value("ELEVENLABS_STT_BASE_URL")
- or ELEVENLABS_STT_BASE_URL
+ elevenlabs_config.get("base_url") or get_env_value("ELEVENLABS_STT_BASE_URL") or ELEVENLABS_STT_BASE_URL
).strip().rstrip("/")
# Language: hook override > stt.elevenlabs.language(_code) > stt.language.
- language_code = language or _resolve_stt_language(
- "elevenlabs", stt_config, extra_keys=("language_code",)
- ) or ""
+ language_code = language or _resolve_stt_language("elevenlabs", stt_config, extra_keys=("language_code",)) or ""
tag_audio_events = is_truthy_value(elevenlabs_config.get("tag_audio_events", False))
diarize = is_truthy_value(elevenlabs_config.get("diarize", False))
@@ -467,21 +472,15 @@ def _transcribe_elevenlabs(
f"{base_url}/speech-to-text", {"xi-api-key": api_key}, file_path, data,
)
- if response.status_code != 200:
- detail = _http_error_detail(response, _elevenlabs_error_detail)
- return _error_result(f"ElevenLabs STT API error (HTTP {response.status_code}): {detail}")
-
- transcript_text = _extract_transcript_text(response.json())
- if not transcript_text:
- return _error_result("ElevenLabs STT returned empty transcript", no_speech=True)
-
+ transcript_text, _body, error = _rest_transcript(
+ response, "ElevenLabs STT", _elevenlabs_error_detail, _extract_transcript_text,
+ )
+ if error:
+ return error
logger.info(
"Transcribed %s via ElevenLabs Scribe (%s, %d chars)",
- Path(file_path).name,
- model_name,
- len(transcript_text),
+ Path(file_path).name, model_name, len(transcript_text),
)
-
return _ok_result(transcript_text, "elevenlabs")
except Exception as e:
diff --git a/tools/transcription_command.py b/tools/transcription_command.py
index 1233a07aa0d..6049ca222b1 100644
--- a/tools/transcription_command.py
+++ b/tools/transcription_command.py
@@ -187,44 +187,23 @@ def _transcribe_command_stt(
"model": str(model),
}
command = _render_command_stt_template(command_template, placeholders)
- logger.info(
- "Transcribing %s via command STT provider '%s'...",
- audio.name, provider_name,
- )
- try:
- result = _run_command_stt(
- command,
- timeout,
- env_passthrough=_command_stt_env_passthrough(config),
- )
- except subprocess.TimeoutExpired:
- return fail(f"STT command provider '{provider_name}' timed out after {timeout:g}s")
- except subprocess.CalledProcessError as exc:
- detail_parts = [
- f"{stream}: {text.strip()}"
- for stream, text in (("stderr", exc.stderr), ("stdout", exc.stdout))
- if text
- ]
- detail = "; ".join(detail_parts) or "no command output"
- return fail(
- f"STT command provider '{provider_name}' exited with code "
- f"{exc.returncode}: {detail}"
- )
-
- try:
- transcript_text = _read_command_stt_output(
- output_path, result.stdout or "", output_format,
- )
- except RuntimeError as exc:
- return fail(str(exc))
-
+ logger.info("Transcribing %s via command STT provider '%s'...", audio.name, provider_name)
+ result = _run_command_stt(command, timeout, env_passthrough=_command_stt_env_passthrough(config))
+ transcript_text = _read_command_stt_output(output_path, result.stdout or "", output_format)
+ except subprocess.TimeoutExpired:
+ return fail(f"STT command provider '{provider_name}' timed out after {timeout:g}s")
+ except subprocess.CalledProcessError as exc:
+ detail_parts = [
+ f"{stream}: {text.strip()}" for stream, text in (("stderr", exc.stderr), ("stdout", exc.stdout)) if text
+ ]
+ detail = "; ".join(detail_parts) or "no command output"
+ return fail(f"STT command provider '{provider_name}' exited with code {exc.returncode}: {detail}")
+ except RuntimeError as exc:
+ return fail(str(exc))
except OSError as exc:
return fail(f"STT command provider '{provider_name}' failed: {exc}")
- logger.info(
- "Transcribed %s via command STT provider '%s' (%d chars)",
- audio.name, provider_name, len(transcript_text),
- )
+ logger.info("Transcribed %s via command STT provider '%s' (%d chars)", audio.name, provider_name, len(transcript_text))
return _ok_result(transcript_text, provider_name)
@@ -291,38 +270,24 @@ def _dispatch_to_plugin_provider(
available = plugin_provider.is_available()
except Exception as exc: # noqa: BLE001
logger.warning(
- "STT plugin provider '%s' is_available() raised: %s — "
- "treating as unavailable", key, exc, exc_info=True,
+ "STT plugin provider '%s' is_available() raised: %s — treating as unavailable", key, exc, exc_info=True,
)
available = False
if not available:
- logger.info(
- "STT plugin provider '%s' reports not available; returning "
- "unavailability envelope.", key,
- )
+ logger.info("STT plugin provider '%s' reports not available; returning unavailability envelope.", key)
return _error_result(
- f"STT plugin '{key}' is not available — check that its "
- "required credentials / dependencies are configured.",
+ f"STT plugin '{key}' is not available — check that its required credentials / dependencies are configured.",
provider=key,
)
logger.info("Transcribing with plugin STT provider '%s'...", key)
# The prompt travels via the ABC's ``**extra`` kwargs and is only sent when
# set, so pre-prompt providers see byte-identical calls on the no-prompt path.
- extra_kwargs: Dict[str, Any] = {}
- if prompt is not None:
- extra_kwargs["prompt"] = prompt
+ extra_kwargs: Dict[str, Any] = {} if prompt is None else {"prompt": prompt}
try:
- result = plugin_provider.transcribe(
- file_path,
- model=model,
- language=language,
- **extra_kwargs,
- )
+ result = plugin_provider.transcribe(file_path, model=model, language=language, **extra_kwargs)
except Exception as exc: # noqa: BLE001
- logger.warning(
- "STT plugin provider '%s' raised: %s", key, exc, exc_info=True,
- )
+ logger.warning("STT plugin provider '%s' raised: %s", key, exc, exc_info=True)
return _error_result(f"STT plugin '{key}' raised: {exc}", provider=key)
if not isinstance(result, dict):
@@ -335,24 +300,15 @@ def _dispatch_to_plugin_provider(
# attempts to change it are logged and dropped.
_PRE_TRANSCRIPTION_MUTABLE_FIELDS = ("prompt", "language", "model")
-
# Whisper-family models only use the final ~224 tokens of the prompt; longer
# values waste upload bytes and can trip stricter OpenAI-compatible servers.
# Enforced client-side (truncate with a warning, never error), ~4 chars/token.
_WHISPER_PROMPT_TOKEN_CAP = 224
-
-
_PROMPT_CHARS_PER_TOKEN = 4
+_WHISPER_PROMPT_CAPPED_PROVIDERS = frozenset({"local", "openai", "groq", "deepinfra"})
-_WHISPER_PROMPT_CAPPED_PROVIDERS = frozenset(
- {"local", "openai", "groq", "deepinfra"}
-)
-
-
-def _enforce_prompt_length_limit(
- prompt: Optional[str], provider: str
-) -> Optional[str]:
+def _enforce_prompt_length_limit(prompt: Optional[str], provider: str) -> Optional[str]:
"""Truncate *prompt* to the whisper-family token cap, keeping the TAIL (fail-open).
Whisper conditions on the final context window, so the most recently
@@ -421,20 +377,14 @@ def _apply_pre_transcription_hook(
"pre_transcription hook attempted to change "
"file_path (read-only) — ignoring the attempt."
)
- continue
- if key not in _PRE_TRANSCRIPTION_MUTABLE_FIELDS:
- logger.debug(
- "pre_transcription hook returned unsupported field "
- "%r — ignoring.", key,
- )
- continue
- if not isinstance(value, str):
+ elif key not in _PRE_TRANSCRIPTION_MUTABLE_FIELDS:
+ logger.debug("pre_transcription hook returned unsupported field %r — ignoring.", key)
+ elif not isinstance(value, str):
logger.debug(
- "pre_transcription hook returned non-string value "
- "%r for field %r — ignoring.", value, key,
+ "pre_transcription hook returned non-string value %r for field %r — ignoring.", value, key,
)
- continue
- overrides[key] = value
+ else:
+ overrides[key] = value
if "model" in overrides:
model = overrides["model"]