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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ __pycache__/
dist/
build/
.env
swift_tts/.build/
swift_tts/*.resolved
80 changes: 80 additions & 0 deletions src/orchestrator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import logging
import os
import subprocess

logger = logging.getLogger(__name__)


class TTSError(Exception):
"""Raised when TTS output fails."""


class TTSBridge:
"""Pipes text to the Swift TTS command-line tool for spoken output."""

DEFAULT_TTS_EXECUTABLE = os.path.join(
os.path.dirname(__file__), "..", "swift_tts", ".build", "release", "swift_tts"
)

def __init__(self, tts_executable=None):
self._executable = tts_executable or self.DEFAULT_TTS_EXECUTABLE

def speak(self, text):
"""Send text to the Swift TTS tool via stdin."""
if not text or not text.strip():
raise TTSError("Cannot speak empty text.")
executable = os.path.abspath(self._executable)
if not os.path.isfile(executable):
raise TTSError(f"Swift TTS executable not found: {executable}")
logger.info("Speaking: %s...", text[:60])
try:
subprocess.run(
[executable],
input=text,
capture_output=True,
text=True,
check=True,
)
return True
except subprocess.CalledProcessError as e:
raise TTSError(f"TTS subprocess failed: {e.stderr}") from e


class OrchestratorError(Exception):
"""Raised when the orchestration pipeline encounters a fatal error."""


class Orchestrator:
"""Orchestrates VoiceParser → ClaudeAPI → TestingGuardrails → TTS output."""

def __init__(self, voice_parser, claude_api, testing_guardrails, tts_bridge):
self._voice_parser = voice_parser
self._claude_api = claude_api
self._testing_guardrails = testing_guardrails
self._tts_bridge = tts_bridge

def run_once(self):
"""
Run one full cycle:
1. Capture voice input
2. Generate code via Claude API
3. Validate generated code with pytest
4. Speak results via TTS
Returns a dict with keys: command, code, summary.
"""
logger.info("Step 1: Listening for voice command...")
command = self._voice_parser.speech_to_text()
logger.info("Command received: %s", command)

logger.info("Step 2: Generating code for command: %s", command)
code = self._claude_api.generate_code(command)
logger.info("Code generated.")

logger.info("Step 3: Validating generated code...")
summary = self._testing_guardrails.validate_code(code)
logger.info("Validation complete: %s", summary)

logger.info("Step 4: Speaking results...")
self._tts_bridge.speak(summary)

return {"command": command, "code": code, "summary": summary}
15 changes: 15 additions & 0 deletions swift_tts/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// swift-tools-version:5.5
import PackageDescription

let package = Package(
name: "swift_tts",
platforms: [
.macOS(.v12),
],
targets: [
.executableTarget(
name: "swift_tts",
path: "Sources"
),
]
)
43 changes: 43 additions & 0 deletions swift_tts/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import AVFoundation
import Foundation

// Read text from command-line arguments or stdin
var text: String
if CommandLine.arguments.count > 1 {
text = CommandLine.arguments[1...].joined(separator: " ")
} else {
var lines: [String] = []
while let line = readLine() {
lines.append(line)
}
text = lines.joined(separator: " ")
}

text = text.trimmingCharacters(in: .whitespaces)

guard !text.isEmpty else {
fputs("Error: No text provided\n", stderr)
exit(1)
}

let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: text)
utterance.rate = AVSpeechUtteranceDefaultSpeechRate
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

// Use a delegate + semaphore to wait for speech completion before exiting
class SpeechDelegate: NSObject, AVSpeechSynthesizerDelegate {
let semaphore = DispatchSemaphore(value: 0)

func speechSynthesizer(
_ synthesizer: AVSpeechSynthesizer,
didFinish utterance: AVSpeechUtterance
) {
semaphore.signal()
}
}

let delegate = SpeechDelegate()
synthesizer.delegate = delegate
synthesizer.speak(utterance)
delegate.semaphore.wait()
130 changes: 130 additions & 0 deletions tests/test_orchestrator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import os
import pytest
from unittest.mock import MagicMock, patch

from orchestrator import Orchestrator, OrchestratorError, TTSBridge, TTSError


class TestTTSBridgeSpeak:
def test_speak_raises_on_empty_text(self, tmp_path):
bridge = TTSBridge(tts_executable=str(tmp_path / "tts"))
with pytest.raises(TTSError, match="empty"):
bridge.speak("")

def test_speak_raises_on_whitespace_text(self, tmp_path):
bridge = TTSBridge(tts_executable=str(tmp_path / "tts"))
with pytest.raises(TTSError, match="empty"):
bridge.speak(" ")

def test_speak_raises_when_executable_missing(self, tmp_path):
bridge = TTSBridge(tts_executable=str(tmp_path / "nonexistent_tts"))
with pytest.raises(TTSError, match="not found"):
bridge.speak("Hello")

def test_speak_calls_subprocess_with_text(self, tmp_path):
# Create a fake executable file so the path check passes
fake_exe = tmp_path / "tts"
fake_exe.write_text("#!/bin/sh\nexit 0\n")
fake_exe.chmod(0o755)

with patch("orchestrator.subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
bridge = TTSBridge(tts_executable=str(fake_exe))
result = bridge.speak("Hello, world!")

assert result is True
mock_run.assert_called_once()
call_kwargs = mock_run.call_args[1]
assert call_kwargs["input"] == "Hello, world!"

def test_speak_raises_on_subprocess_error(self, tmp_path):
import subprocess

fake_exe = tmp_path / "tts"
fake_exe.write_text("#!/bin/sh\nexit 0\n")
fake_exe.chmod(0o755)

with patch("orchestrator.subprocess.run") as mock_run:
mock_run.side_effect = subprocess.CalledProcessError(1, "tts", stderr="fail")
bridge = TTSBridge(tts_executable=str(fake_exe))
with pytest.raises(TTSError, match="subprocess failed"):
bridge.speak("Hello")

def test_default_executable_path_is_set(self):
bridge = TTSBridge()
assert "swift_tts" in bridge._executable


class TestOrchestrator:
def _make_orchestrator(self, command="write a sort function", code="def sort(): pass",
summary="passed all 1 tests"):
voice_parser = MagicMock()
voice_parser.speech_to_text.return_value = command

claude_api = MagicMock()
claude_api.generate_code.return_value = code

testing_guardrails = MagicMock()
testing_guardrails.validate_code.return_value = summary

tts_bridge = MagicMock()
tts_bridge.speak.return_value = True

orchestrator = Orchestrator(voice_parser, claude_api, testing_guardrails, tts_bridge)
return orchestrator, voice_parser, claude_api, testing_guardrails, tts_bridge

def test_run_once_returns_expected_keys(self):
orchestrator, _, _, _, _ = self._make_orchestrator()
result = orchestrator.run_once()
assert set(result.keys()) == {"command", "code", "summary"}

def test_run_once_returns_correct_values(self):
orchestrator, _, _, _, _ = self._make_orchestrator(
command="create a sort function",
code="def sort(): pass",
summary="passed all 1 tests",
)
result = orchestrator.run_once()
assert result["command"] == "create a sort function"
assert result["code"] == "def sort(): pass"
assert result["summary"] == "passed all 1 tests"

def test_run_once_calls_voice_parser(self):
orchestrator, voice_parser, _, _, _ = self._make_orchestrator()
orchestrator.run_once()
voice_parser.speech_to_text.assert_called_once()

def test_run_once_calls_claude_api_with_command(self):
orchestrator, _, claude_api, _, _ = self._make_orchestrator(command="my command")
orchestrator.run_once()
claude_api.generate_code.assert_called_once_with("my command")

def test_run_once_calls_testing_guardrails_with_code(self):
orchestrator, _, _, testing_guardrails, _ = self._make_orchestrator(
code="def foo(): return 1"
)
orchestrator.run_once()
testing_guardrails.validate_code.assert_called_once_with("def foo(): return 1")

def test_run_once_calls_tts_bridge_with_summary(self):
orchestrator, _, _, _, tts_bridge = self._make_orchestrator(
summary="Your code passed all 3 tests."
)
orchestrator.run_once()
tts_bridge.speak.assert_called_once_with("Your code passed all 3 tests.")

def test_run_once_propagates_voice_parser_error(self):
from voice_parser import VoiceParserError

orchestrator, voice_parser, _, _, _ = self._make_orchestrator()
voice_parser.speech_to_text.side_effect = VoiceParserError("mic error")
with pytest.raises(VoiceParserError, match="mic error"):
orchestrator.run_once()

def test_run_once_propagates_claude_api_error(self):
from claude_api import ClaudeAPIError

orchestrator, _, claude_api, _, _ = self._make_orchestrator()
claude_api.generate_code.side_effect = ClaudeAPIError("API down")
with pytest.raises(ClaudeAPIError, match="API down"):
orchestrator.run_once()