# Local Command STT Wrapper Template

## Required Env Var

```bash
export HERMES_LOCAL_STT_COMMAND="/path/to/wrapper.sh {input_path} {model} {output_dir} {language}"
```

## Wrapper Script Requirements

The wrapper script receives four positional arguments:
1. `input_path` - path to the audio file (e.g. `.ogg` from Telegram)
2. `model` - model name from config (e.g. `base`)
3. `output_dir` - directory where transcript.txt must be written
4. `language` - language code (e.g. `de`)

The script must:
- Convert audio to WAV (16kHz mono) if needed
- Run transcription
- Write transcript to `{output_dir}/transcript.txt`
- Print transcript to stdout (optional, for logging)
- Exit 0 on success, non-zero on failure

## Example: faster-whisper Wrapper

```bash
#!/bin/sh
# /opt/data/stt-wrapper.sh
# Wrapper for Hermes local_command STT (faster-whisper)

INPUT_PATH="$1"
MODEL="$2"
OUTPUT_DIR="$3"
LANGUAGE="$4"

# Ensure output directory exists
mkdir -p "$OUTPUT_DIR"

# Ensure Python can find faster-whisper (adjust path as needed)
export PYTHONPATH=/opt/data/home/.local/lib/python3.14/site-packages

exec /usr/local/bin/python3 -c "
import subprocess, sys, os, tempfile
input_file = sys.argv[1]
output_dir = sys.argv[3]
language = sys.argv[4]
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
    wav_path = tmp.name
subprocess.run(['ffmpeg', '-y', '-i', input_file, '-ar', '16000', '-ac', '1', wav_path], capture_output=True, check=True)
from faster_whisper import WhisperModel
model = WhisperModel('base', device='cpu', compute_type='int8')
segments, info = model.transcribe(wav_path, language=language)
text = ' '.join([s.text for s in segments]).strip()
out_file = os.path.join(output_dir, 'transcript.txt')
with open(out_file, 'w', encoding='utf-8') as f:
    f.write(text)
print(text)
os.unlink(wav_path)
" "$INPUT_PATH" "$MODEL" "$OUTPUT_DIR" "$LANGUAGE"
```

## Key Points

- **Make it executable**: `chmod +x /path/to/wrapper.sh`
- **ffmpeg must be available** in PATH for audio conversion
- **Python path** must include the site-packages where faster-whisper is installed
- **The `{output_dir}` is a temp directory** created by Hermes per transcription request
- **Language handling**: The script should pass the language to Whisper; empty/None lets Whisper auto-detect. The language code **must be a valid Whisper language code** (ISO 639-1 like `de`, `en`, `fr`). Invalid codes (e.g., `test`) cause `ValueError: 'test' is not a valid language code`. Use `de` for German, leave empty string for auto-detect.
- **Input format**: Hermes's `_prepare_local_audio` converts the original audio (e.g., .ogg from Telegram) to a 16kHz mono WAV file in a temp directory before calling the wrapper. The wrapper receives this WAV file as `input_path`. The wrapper should still run ffmpeg to ensure 16kHz mono (idempotent).
- **Output directory**: The wrapper must ensure `output_dir` exists before writing `transcript.txt` (use `mkdir -p "$OUTPUT_DIR"`). Hermes creates this per-request but the wrapper should be defensive.
- **Debugging**: Add debug logging to `/tmp/stt-wrapper-debug.log` to verify arguments received from Hermes:
  ```bash
  echo "DEBUG: argc=$# INPUT_PATH='$INPUT_PATH' MODEL='$MODEL' OUTPUT_DIR='$OUTPUT_DIR' LANGUAGE='$LANGUAGE'" >> /tmp/stt-wrapper-debug.log
  ls -la "$INPUT_PATH" >> /tmp/stt-wrapper-debug.log 2>&1
  ```

## For s6/Container Deployments

**Option A: Container environment (requires root)**
```bash
echo '/path/to/wrapper.sh {input_path} {model} {output_dir} {language}' > /run/s6/container_environment/HERMES_LOCAL_STT_COMMAND
```
The `with-contenv` shebang in the run script automatically sources this.

**Option B: Service env directory (no root, but run script must source it)**
```bash
mkdir -p /run/service/gateway-default/env
echo '/path/to/wrapper.sh {input_path} {model} {output_dir} {language}' > /run/service/gateway-default/env/HERMES_LOCAL_STT_COMMAND
```

The service run script **must** source these env vars (the `with-contenv` shebang does NOT do this automatically):
```bash
#!/command/with-contenv sh
# ... existing content ...
if [ -d /run/service/gateway-default/env ]; then
    for f in /run/service/gateway-default/env/*; do
        [ -f "$f" ] && export "$(basename "$f")=$(cat "$f")"
    done
fi
# ... rest of run script ...
```

Then restart the service:
```bash
s6-svc -t /run/service/gateway-default
```

**Verify** the env var is in the gateway process:
```bash
cat /proc/$(pgrep -f "gateway run")/environ | tr '\0' '\n' | grep HERMES_LOCAL_STT_COMMAND
```

## Disable Transcript Echo

To prevent Hermes from sending the transcript as a separate message before the agent reply:

```bash
hermes config set stt.echo_transcripts false
```

Or in config.yaml:
```yaml
stt:
  echo_transcripts: false
```