/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
using System;
using Meta.Voice;
using Meta.WitAi.Configuration;
using Meta.WitAi.Data.Configuration;
using Meta.WitAi.Json;
using UnityEngine;
namespace Meta.WitAi.Requests
{
///
/// A class used to track Wit text 'Message' requests
///
[Serializable]
public class WitUnityRequest : VoiceServiceRequest
{
///
/// The configuration to be used for the request
///
public WitConfiguration Configuration { get; private set; }
///
/// Unity request wrapper for Wit
///
private readonly WitVRequest _request;
///
/// Endpoint to be used
///
public string Endpoint { get; set; }
///
/// Endpoint to be used
///
public bool ShouldPost { get; set; }
///
/// Apply configuration
///
///
///
///
public WitUnityRequest(WitConfiguration newConfiguration, NLPRequestInputType newDataType, WitRequestOptions newOptions, VoiceServiceRequestEvents newEvents) : base(newDataType, newOptions, newEvents)
{
// Apply configuration & generate request
Configuration = newConfiguration;
// Generate a message WitVRequest
if (InputType == NLPRequestInputType.Text)
{
_request = new WitMessageVRequest(Configuration, newOptions.RequestId, SetDownloadProgress);
Endpoint = Configuration.GetEndpointInfo().Message;
_request.Timeout = Mathf.RoundToInt(Configuration.timeoutMS / 1000f);
ShouldPost = false;
}
// Generate an audio WitVRequest
else if (InputType == NLPRequestInputType.Audio)
{
// TODO: T121060485: Add audio support to WitVRequest
Endpoint = Configuration.GetEndpointInfo().Speech;
ShouldPost = true;
}
// Initialized
_initialized = true;
SetState(VoiceRequestState.Initialized);
}
///
/// Ignore state changes unless setup
///
private bool _initialized = false;
protected override void SetState(VoiceRequestState newState)
{
if (_initialized)
{
base.SetState(newState);
}
}
///
/// Get send error options
///
protected override string GetSendError()
{
if (Configuration == null)
{
return "Cannot send request without a valid configuration.";
}
if (_request == null)
{
return "Request creation failed.";
}
return base.GetSendError();
}
///
/// Performs a wit message request
///
/// Callback that handles send completion
protected override void HandleSend()
{
// Send message request
if (_request is WitMessageVRequest messageRequest)
{
messageRequest.MessageRequest(Endpoint, ShouldPost,
Options.Text, Options.QueryParams,
HandleFinalNlpResponse);
}
}
///
/// Set status code prior to handling response
///
protected override void HandleFinalNlpResponse(WitResponseNode responseData, string error)
{
StatusCode = _request.ResponseCode;
base.HandleFinalNlpResponse(responseData, error);
}
///
/// Handle cancellation
///
protected override void HandleCancel()
{
if (_request != null)
{
_request.Cancel();
}
}
#region AUDIO CALLBACKS
// TODO: T121060485: Add audio support to WitVRequest
///
/// Error returned if audio cannot be activated
///
protected override string GetActivateAudioError()
{
return "Audio request not yet implemented";
}
///
/// Activates audio & calls activated callback once complete
///
protected override void HandleAudioActivation()
{
SetAudioInputState(VoiceAudioInputState.On);
}
///
/// Deactivates audio asap & calls deactivated callback once complete
///
protected override void HandleAudioDeactivation()
{
SetAudioInputState(VoiceAudioInputState.Off);
}
#endregion
}
}