From d7eb5cc5b6cb773177a4e2508bc2b7089ea9017d Mon Sep 17 00:00:00 2001 From: LuciferDono Date: Sat, 7 Mar 2026 09:27:07 +0530 Subject: [PATCH] fix: repair broken Record() method and improve error handling in APIManager - Replace non-functional StartCoroutine("hi!") in Record() with proper delegation to ARAppVoiceManager via new public StartRecording() method - Add StartRecording() to ARAppVoiceManager as public API for external callers, avoiding the anti-pattern of simulating UI button clicks - Add null check with debug warning when voiceManager is not assigned - Include actual error details (www.error) in failure responses instead of generic "There was an error" message - Re-enable sendButton after request completes (was left disabled on failure) - Only trigger TTS playback on successful API responses --- Assets/Scripts/APIManager.cs | 24 ++++++++++++++----- .../Scripts/Whisper&TTS/ARAppVoiceManager.cs | 8 +++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/APIManager.cs b/Assets/Scripts/APIManager.cs index 39d6cf8..4961620 100644 --- a/Assets/Scripts/APIManager.cs +++ b/Assets/Scripts/APIManager.cs @@ -28,8 +28,14 @@ public void SendPrompt() public void Record() { - - StartCoroutine("hi!"); + if (voiceManager != null) + { + voiceManager.StartRecording(); + } + else + { + Debug.LogWarning("APIManager: Cannot record — voiceManager is not assigned."); + } } private IEnumerator SendDataToGas() @@ -58,14 +64,16 @@ private IEnumerator SendDataToGas() } else { - response = "There was an error"; + response = $"Request failed: {www.error}"; + Debug.LogError($"APIManager: {response}"); } - + Debug.Log(response); responseText.text = response; - // Speak the new AI response - if (ttsManager != null && !string.IsNullOrEmpty(response)) + // Speak the AI response only on success + if (www.result == UnityWebRequest.Result.Success + && ttsManager != null && !string.IsNullOrEmpty(response)) { ttsManager.Speak(response); } @@ -73,5 +81,9 @@ private IEnumerator SendDataToGas() // Hide spinner if (spinner != null) spinner.SetActive(false); + + // Re-enable send button + if (sendButton != null) + sendButton.interactable = true; } } diff --git a/Assets/Scripts/Whisper&TTS/ARAppVoiceManager.cs b/Assets/Scripts/Whisper&TTS/ARAppVoiceManager.cs index 7ebffe6..6994d46 100644 --- a/Assets/Scripts/Whisper&TTS/ARAppVoiceManager.cs +++ b/Assets/Scripts/Whisper&TTS/ARAppVoiceManager.cs @@ -126,6 +126,14 @@ private void ResetUI() recordButton.interactable = true; } + /// + /// Public entry point for starting voice recording from external scripts. + /// + public void StartRecording() + { + OnRecordButtonPressed(); + } + public string GetLastTranscription() { return lastTranscription;