From 7d9b8aaf73f061030c8e655da6dcfc795387c112 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Mar 2026 15:16:04 +0000 Subject: [PATCH 1/2] Initial plan From 9223664a1a11b69658cf90df5f6468e000523480 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Mar 2026 15:19:41 +0000 Subject: [PATCH 2/2] Implement Python orchestration layer, Swift TTS component, and tests Co-authored-by: tathagata <68151+tathagata@users.noreply.github.com> --- .gitignore | 2 + src/orchestrator.py | 80 +++++++++++++++++++++ swift_tts/Package.swift | 15 ++++ swift_tts/Sources/main.swift | 43 ++++++++++++ tests/test_orchestrator.py | 130 +++++++++++++++++++++++++++++++++++ 5 files changed, 270 insertions(+) create mode 100644 src/orchestrator.py create mode 100644 swift_tts/Package.swift create mode 100644 swift_tts/Sources/main.swift create mode 100644 tests/test_orchestrator.py diff --git a/.gitignore b/.gitignore index 9075a1b..b6911a5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ __pycache__/ dist/ build/ .env +swift_tts/.build/ +swift_tts/*.resolved diff --git a/src/orchestrator.py b/src/orchestrator.py new file mode 100644 index 0000000..a363003 --- /dev/null +++ b/src/orchestrator.py @@ -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} diff --git a/swift_tts/Package.swift b/swift_tts/Package.swift new file mode 100644 index 0000000..5d04d55 --- /dev/null +++ b/swift_tts/Package.swift @@ -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" + ), + ] +) diff --git a/swift_tts/Sources/main.swift b/swift_tts/Sources/main.swift new file mode 100644 index 0000000..6b295a4 --- /dev/null +++ b/swift_tts/Sources/main.swift @@ -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() diff --git a/tests/test_orchestrator.py b/tests/test_orchestrator.py new file mode 100644 index 0000000..697cb8a --- /dev/null +++ b/tests/test_orchestrator.py @@ -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()