WitUnityRequest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. using System;
  9. using Meta.Voice;
  10. using Meta.WitAi.Configuration;
  11. using Meta.WitAi.Data.Configuration;
  12. using Meta.WitAi.Json;
  13. using UnityEngine;
  14. namespace Meta.WitAi.Requests
  15. {
  16. /// <summary>
  17. /// A class used to track Wit text 'Message' requests
  18. /// </summary>
  19. [Serializable]
  20. public class WitUnityRequest : VoiceServiceRequest
  21. {
  22. /// <summary>
  23. /// The configuration to be used for the request
  24. /// </summary>
  25. public WitConfiguration Configuration { get; private set; }
  26. /// <summary>
  27. /// Unity request wrapper for Wit
  28. /// </summary>
  29. private readonly WitVRequest _request;
  30. /// <summary>
  31. /// Endpoint to be used
  32. /// </summary>
  33. public string Endpoint { get; set; }
  34. /// <summary>
  35. /// Endpoint to be used
  36. /// </summary>
  37. public bool ShouldPost { get; set; }
  38. /// <summary>
  39. /// Apply configuration
  40. /// </summary>
  41. /// <param name="newConfiguration"></param>
  42. /// <param name="newOptions"></param>
  43. /// <param name="newEvents"></param>
  44. public WitUnityRequest(WitConfiguration newConfiguration, NLPRequestInputType newDataType, WitRequestOptions newOptions, VoiceServiceRequestEvents newEvents) : base(newDataType, newOptions, newEvents)
  45. {
  46. // Apply configuration & generate request
  47. Configuration = newConfiguration;
  48. // Generate a message WitVRequest
  49. if (InputType == NLPRequestInputType.Text)
  50. {
  51. _request = new WitMessageVRequest(Configuration, newOptions.RequestId);
  52. Endpoint = Configuration.GetEndpointInfo().Message;
  53. _request.Timeout = Mathf.RoundToInt(Configuration.timeoutMS / 1000f);
  54. ShouldPost = false;
  55. }
  56. // Generate an audio WitVRequest
  57. else if (InputType == NLPRequestInputType.Audio)
  58. {
  59. // TODO: T121060485: Add audio support to WitVRequest
  60. Endpoint = Configuration.GetEndpointInfo().Speech;
  61. ShouldPost = true;
  62. }
  63. // Initialized
  64. _initialized = true;
  65. SetState(VoiceRequestState.Initialized);
  66. }
  67. /// <summary>
  68. /// Ignore state changes unless setup
  69. /// </summary>
  70. private bool _initialized = false;
  71. protected override void SetState(VoiceRequestState newState)
  72. {
  73. if (_initialized)
  74. {
  75. base.SetState(newState);
  76. }
  77. }
  78. /// <summary>
  79. /// Get send error options
  80. /// </summary>
  81. protected override string GetSendError()
  82. {
  83. if (Configuration == null)
  84. {
  85. return "Cannot send request without a valid configuration.";
  86. }
  87. if (_request == null)
  88. {
  89. return "Request creation failed.";
  90. }
  91. return base.GetSendError();
  92. }
  93. /// <summary>
  94. /// Performs a wit message request
  95. /// </summary>
  96. /// <param name="onSendComplete">Callback that handles send completion</param>
  97. protected override void HandleSend()
  98. {
  99. // Send message request
  100. if (_request is WitMessageVRequest messageRequest)
  101. {
  102. messageRequest.MessageRequest(Endpoint, ShouldPost,
  103. Options.Text, Options.QueryParams,
  104. HandleNlpResponse, SetDownloadProgress);
  105. }
  106. }
  107. /// <summary>
  108. /// Set status code prior to handling response
  109. /// </summary>
  110. protected override void HandleNlpResponse(WitResponseNode responseData, string error)
  111. {
  112. StatusCode = _request.ResponseCode;
  113. base.HandleNlpResponse(responseData, error);
  114. }
  115. /// <summary>
  116. /// Handle cancellation
  117. /// </summary>
  118. protected override void HandleCancel()
  119. {
  120. if (_request != null)
  121. {
  122. _request.Cancel();
  123. }
  124. }
  125. #region AUDIO CALLBACKS
  126. // TODO: T121060485: Add audio support to WitVRequest
  127. /// <summary>
  128. /// Error returned if audio cannot be activated
  129. /// </summary>
  130. protected override string GetActivateAudioError()
  131. {
  132. return "Audio request not yet implemented";
  133. }
  134. /// <summary>
  135. /// Activates audio & calls activated callback once complete
  136. /// </summary>
  137. protected override void HandleAudioActivation()
  138. {
  139. SetAudioInputState(VoiceAudioInputState.On);
  140. }
  141. /// <summary>
  142. /// Deactivates audio asap & calls deactivated callback once complete
  143. /// </summary>
  144. protected override void HandleAudioDeactivation()
  145. {
  146. SetAudioInputState(VoiceAudioInputState.Off);
  147. }
  148. #endregion
  149. }
  150. }