Implements /chat/ endpoint call with audio playing.

This commit is contained in:
Jonas Luz Jr. 2025-11-23 07:37:29 -03:00
parent 6739596fb0
commit b3679cdffe
2 changed files with 60 additions and 4 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.IO;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
@ -115,6 +116,41 @@ Response Body: {body}";
yield return CallEndpointCoroutine( yield return CallEndpointCoroutine(
url, "DELETE", null, timeoutInSeconds, callbackOnSuccess); url, "DELETE", null, timeoutInSeconds, callbackOnSuccess);
} }
internal static IEnumerator ReadAudioResponseCoroutine(
UnityWebRequest request,
Action<AudioClip> callbackOnSuccess)
{
byte[] audioBytes = request.downloadHandler.data;
if (audioBytes == null || audioBytes.Length == 0)
{
Debug.LogError("No audio data received.");
yield break;
}
// Save temporarily to file for loading as AudioClip
string tempPath = Path.Combine(Application.persistentDataPath, "tts_temp.ogg");
File.WriteAllBytes(tempPath, audioBytes);
using (var file = UnityWebRequestMultimedia.GetAudioClip(
"file://" + tempPath, AudioType.OGGVORBIS))
{
yield return file.SendWebRequest();
if (file.result == UnityWebRequest.Result.Success)
{
AudioClip clip = DownloadHandlerAudioClip.GetContent(file);
callbackOnSuccess?.Invoke(clip);
}
else
{
Debug.LogError($"Error loading AudioClip: {file.error}");
}
}
// Remove temporary file
File.Delete(tempPath);
}
} }
internal enum Environment internal enum Environment

View File

@ -5,6 +5,7 @@ using UnityEngine;
namespace PPGIA.X540.Project3.API namespace PPGIA.X540.Project3.API
{ {
[RequireComponent(typeof(AudioSource))]
public class ApiClientManager : MonoBehaviour public class ApiClientManager : MonoBehaviour
{ {
#region -- Inspector Fields ------------------------------------------- #region -- Inspector Fields -------------------------------------------
@ -53,6 +54,9 @@ namespace PPGIA.X540.Project3.API
#endregion ------------------------------------------------------------ #endregion ------------------------------------------------------------
#region -- Other Properties & Methods --------------------------------- #region -- Other Properties & Methods ---------------------------------
[SerializeField]
private AudioSource _audioSource;
// Property to get the appropriate API base URL // Property to get the appropriate API base URL
private string ApiBaseUrl => private string ApiBaseUrl =>
_environment == Environment.Development ? _environment == Environment.Development ?
@ -64,7 +68,11 @@ namespace PPGIA.X540.Project3.API
string.Join("/", parts.Select(p => p.Trim('/'))); string.Join("/", parts.Select(p => p.Trim('/')));
#endregion ------------------------------------------------------------ #endregion ------------------------------------------------------------
void Awake()
{
_audioSource = GetComponent<AudioSource>();
}
#region -- API Calls -------------------------------------------------- #region -- API Calls --------------------------------------------------
[ContextMenu("Test API Availability")] [ContextMenu("Test API Availability")]
public void TestApiAvailability() public void TestApiAvailability()
@ -115,22 +123,34 @@ namespace PPGIA.X540.Project3.API
[ContextMenu("Send Chat Message")] [ContextMenu("Send Chat Message")]
public void SendChatMessage() public void SendChatMessage()
{ {
// Ensure there is an active session
if (_session == null) if (_session == null)
{ {
Debug.LogWarning("No active session. Please initiate a session first."); Debug.LogWarning("No active session. Please initiate a session first.");
return; return;
} }
// Build the endpoint URL and payload
var url = EndpointUrl(_chatEndpoint, _session.SessionId); var url = EndpointUrl(_chatEndpoint, _session.SessionId);
var payload = new ChatServicePayload { message = _query }; var payload = new ChatServicePayload { message = _query };
// Make the API call. Expect an audio response.
StartCoroutine(ApiClient.CallEndpointWithPostCoroutine( StartCoroutine(ApiClient.CallEndpointWithPostCoroutine(
url, _timeoutInSeconds, payload, (request) => url, _timeoutInSeconds, payload, (request) =>
{ {
var body = request.downloadHandler?.text ?? string.Empty; StartCoroutine(ApiClient.ReadAudioResponseCoroutine(
Debug.Log($"Chat response: {body}"); request, (audioClip) =>
{
if (audioClip == null)
{
Debug.LogError("AudioClip is null after download.");
return;
}
_audioSource?.PlayOneShot(audioClip);
}));
})); }));
} }
#endregion -- API Calls ------------------------------------------------ #endregion -- API Calls ------------------------------------------------
} }
} }