Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add an opt-in `-tts-system-timeout` to recover the speech queue from a hung system TTS child while preserving unlimited playback by default. Thanks @SebTardif! (#10)
- Deliver buffered bridge frames before reporting disconnects, preventing lost pairing and hello replies, and let bridge readers exit without an error consumer. Thanks @SebTardif! (#9)
- Stop speech queues and cancel active system TTS when the bridge disconnects or the node exits. Thanks @SebTardif! (#7)
- Forward only final speech transcripts to quick actions, voice events, and agent requests. Thanks @SebTardif! (#12)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GOOS=linux GOARCH=arm64 go build -o /tmp/clawgo-linux-arm64 ./cmd/clawgo
| `-tts-system-voice` | espeak voice id (default `en-us`). |
| `-tts-system-command` | espeak-compatible executable; receives voice/rate options, `--`, then the utterance. |
| `-tts-system-rate` | Speech rate (wpm). |
| `-tts-system-timeout` | Optional deadline per system TTS utterance (for example `30s`); default `0` allows unlimited playback. |
| `-mdns-service` | Bonjour service type (default `_clawdbot-node._tcp`). |
| `-stdin` | Read transcripts from stdin (pipe/FIFO). |
| `-stdin-file` | Read transcripts from a FIFO/file instead of stdin. |
Expand Down Expand Up @@ -68,7 +69,7 @@ printf hey computer turn on the lights

Each line on the FIFO becomes a `voice.transcript`; chat responses from the outgoing session (`main` by default) are spoken via `espeak-ng`. Set `-chat-session-key` to subscribe to a different session explicitly.

A bridge disconnect or node shutdown cancels pending and active speech. Custom TTS wrappers should use `exec` to run their speech command so cancellation reaches it.
A bridge disconnect or node shutdown cancels pending and active speech. Custom TTS wrappers should use `exec` to run their speech command so cancellation reaches it. Set `-tts-system-timeout` to bound a hung speech command and let the next queued utterance start; choose a duration long enough for healthy speech. The default `0` leaves long utterances uninterrupted.

With `-stt-engine brabble`, only final utterances are routed or sent to the bridge; interim hypotheses do not trigger requests or quick actions.

Expand Down
19 changes: 15 additions & 4 deletions cmd/clawgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type NodeConfig struct {
TTSSystemVoice string
TTSSystemRate int
TTSSystemCommand string
TTSSystemTimeout time.Duration
StdinPath string
QuickActions bool
QuickPingMessage string
Expand Down Expand Up @@ -137,6 +138,7 @@ func usage() {
fmt.Fprintln(os.Stderr, " -tts-system-voice Voice id for system TTS (default en-us)")
fmt.Fprintln(os.Stderr, " -tts-system-rate Speech rate for system TTS (default 180)")
fmt.Fprintln(os.Stderr, " -tts-system-command Binary for system TTS (default espeak-ng)")
fmt.Fprintln(os.Stderr, " -tts-system-timeout Optional deadline for one system TTS child (default 0, unlimited)")
fmt.Fprintln(os.Stderr, " -stdin Read stdin lines and send voice.transcript events")
fmt.Fprintln(os.Stderr, " -stdin-file Read lines from a file/FIFO instead of stdin")
fmt.Fprintln(os.Stderr, " -ping-interval Ping interval (default 30s)")
Expand Down Expand Up @@ -177,6 +179,7 @@ func parseFlags(cmd string, args []string) NodeConfig {
ttsSystemVoice := fs.String("tts-system-voice", "en-us", "voice id for system TTS")
ttsSystemRate := fs.Int("tts-system-rate", 180, "speech rate for system TTS")
ttsSystemCommand := fs.String("tts-system-command", "espeak-ng", "binary for system TTS")
ttsSystemTimeout := fs.Duration("tts-system-timeout", 0, "optional deadline for one system TTS child; 0 keeps unlimited healthy speech")
stdinMode := fs.Bool("stdin", false, "read stdin lines for voice.transcript")
stdinFile := fs.String("stdin-file", "", "read input lines from file/FIFO")
pingInterval := fs.Duration("ping-interval", 30*time.Second, "ping interval")
Expand Down Expand Up @@ -232,6 +235,7 @@ func parseFlags(cmd string, args []string) NodeConfig {
TTSSystemVoice: strings.TrimSpace(*ttsSystemVoice),
TTSSystemRate: *ttsSystemRate,
TTSSystemCommand: strings.TrimSpace(*ttsSystemCommand),
TTSSystemTimeout: *ttsSystemTimeout,
StdinPath: strings.TrimSpace(*stdinFile),
QuickActions: *quickActions,
QuickPingMessage: strings.TrimSpace(*quickPingMessage),
Expand Down Expand Up @@ -1005,17 +1009,18 @@ type systemTTSEngine struct {
command string
voice string
rate int
timeout time.Duration
}

func newSystemTTSEngine(cmd, voice string, rate int) (*systemTTSEngine, error) {
func newSystemTTSEngine(cmd, voice string, rate int, timeout time.Duration) (*systemTTSEngine, error) {
if cmd == "" {
cmd = "espeak-ng"
}
resolved, err := exec.LookPath(cmd)
if err != nil {
return nil, err
}
return &systemTTSEngine{command: resolved, voice: voice, rate: rate}, nil
return &systemTTSEngine{command: resolved, voice: voice, rate: rate, timeout: timeout}, nil
}

func (s *systemTTSEngine) Speak(ctx context.Context, text string) error {
Expand All @@ -1031,7 +1036,13 @@ func (s *systemTTSEngine) Speak(ctx context.Context, text string) error {
args = append(args, "-s", strconv.Itoa(s.rate))
}
args = append(args, "--", trimmed)
cmd := exec.CommandContext(ctx, s.command, args...)
speakCtx := ctx
cancel := func() {}
if s.timeout > 0 {
speakCtx, cancel = context.WithTimeout(ctx, s.timeout)
}
defer cancel()
cmd := exec.CommandContext(speakCtx, s.command, args...)
cmd.WaitDelay = 2 * time.Second
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard
Expand All @@ -1043,7 +1054,7 @@ func newNodeTTSEngine(cfg NodeConfig) (TTSEngine, error) {
case "", "none":
return nil, nil
case "system":
return newSystemTTSEngine(cfg.TTSSystemCommand, cfg.TTSSystemVoice, cfg.TTSSystemRate)
return newSystemTTSEngine(cfg.TTSSystemCommand, cfg.TTSSystemVoice, cfg.TTSSystemRate, cfg.TTSSystemTimeout)
default:
return nil, fmt.Errorf("unsupported tts engine: %s", cfg.TTSEngine)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/clawgo/tts_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestTTSQueueStopCancelsActiveSpeech(t *testing.T) {
if err := os.WriteFile(script, []byte("#!/bin/sh\necho started >> "+strconv.Quote(started)+"\nexec sleep 60\n"), 0o700); err != nil {
t.Fatal(err)
}
engine, err := newSystemTTSEngine(script, "", 0)
engine, err := newSystemTTSEngine(script, "", 0, 0)
if err != nil {
t.Fatal(err)
}
Expand Down
43 changes: 43 additions & 0 deletions cmd/clawgo/tts_speak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"context"
"os/exec"
"testing"
"time"
)

func TestSystemTTSEngineSpeakHungProcessIsBoundedWhenTimeoutSet(t *testing.T) {
sleep, err := exec.LookPath("sleep")
if err != nil {
t.Skip("sleep not found")
}

engine := &systemTTSEngine{
command: sleep,
timeout: 200 * time.Millisecond,
}
start := time.Now()
if err := engine.Speak(context.Background(), "2"); err == nil {
t.Fatal("expected hung speak to return an error")
}
if elapsed := time.Since(start); elapsed > time.Second {
t.Fatalf("timed speak took %s, want about 200ms", elapsed)
}
}

func TestSystemTTSEngineSpeakUnlimitedKeepsHealthyShortUtterance(t *testing.T) {
sleep, err := exec.LookPath("sleep")
if err != nil {
t.Skip("sleep not found")
}

engine := &systemTTSEngine{command: sleep}
start := time.Now()
if err := engine.Speak(context.Background(), "0.15"); err != nil {
t.Fatalf("unlimited short speak: %v", err)
}
if elapsed := time.Since(start); elapsed < 100*time.Millisecond {
t.Fatalf("unlimited speak returned too early: %s", elapsed)
}
}
Loading