Skip to content

Commit 47ec5e1

Browse files
authored
Merge pull request #366 from watson-developer-cloud/rc-2.2.1
Watson Unity SDK v2.2.1
2 parents 465363d + 766a1ae commit 47ec5e1

File tree

9 files changed

+199
-83
lines changed

9 files changed

+199
-83
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22
==========
3+
## Version 2.2.1
4+
_2018-04-12_
5+
* Fixed: Serialization/Deserialization of generic object in the `assistant` service([361](https://github.com/watson-developer-cloud/unity-sdk/issues/361), [363](https://github.com/watson-developer-cloud/unity-sdk/pull/363)).
6+
37
## Version 2.2.0
48
_2018-04-09_
59
* New: Updated Visual Recognition with Core ML support ([4182](https://zenhub.innovate.ibm.com/app/workspace/o/watson/developer-experience/issues/4182), [357](https://github.com/watson-developer-cloud/unity-sdk/pull/357)).

Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_NAME = "Watson Developer Cloud Unity SDK"
3838
# could be handy for archiving the generated documentation or if some version
3939
# control system is used.
4040

41-
PROJECT_NUMBER = 2.2.0
41+
PROJECT_NUMBER = 2.2.1
4242

4343
# Using the PROJECT_BRIEF tag one can provide an optional one line description
4444
# for a project that appears at the top of each page and should give viewer a

Examples/ServiceExamples/Scripts/ExampleAssistant.cs

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public class ExampleAssistant : MonoBehaviour
4646

4747
private fsSerializer _serializer = new fsSerializer();
4848

49-
private string _inputString = "Turn on the winshield wipers";
49+
private string _inputString = "Hello";
50+
private string _conversationString0 = "unlock the door";
51+
private string _conversationString1 = "turn on the ac";
52+
private string _conversationString2 = "turn down the radio";
5053

5154
private static string _createdWorkspaceName = "unity-sdk-example-workspace-delete";
5255
private static string _createdWorkspaceDescription = "A Workspace created by the Unity SDK Assistant example script. Please delete this.";
@@ -61,6 +64,7 @@ public class ExampleAssistant : MonoBehaviour
6164
private static string _createdExample = "untiyExample";
6265
private static string _dialogNodeName = "untiyDialognode";
6366
private static string _dialogNodeDesc = "Unity SDK Integration test dialog node";
67+
private Dictionary<string, object> _context = null;
6468

6569
private bool _listWorkspacesTested = false;
6670
private bool _createWorkspaceTested = false;
@@ -109,53 +113,6 @@ public class ExampleAssistant : MonoBehaviour
109113
void Start()
110114
{
111115
LogSystem.InstallDefaultReactors();
112-
113-
Runnable.Run(GetCredentials());
114-
}
115-
116-
private IEnumerator GetCredentials()
117-
{
118-
//Credentials credentials = new Credentials(_username, _password, _url);
119-
120-
#region Get Credentials via internal service
121-
VcapCredentials vcapCredentials = new VcapCredentials();
122-
fsData data = null;
123-
124-
string result = null;
125-
126-
var vcapUrl = Environment.GetEnvironmentVariable("VCAP_URL");
127-
var vcapUsername = Environment.GetEnvironmentVariable("VCAP_USERNAME");
128-
var vcapPassword = Environment.GetEnvironmentVariable("VCAP_PASSWORD");
129-
130-
using (SimpleGet simpleGet = new SimpleGet(vcapUrl, vcapUsername, vcapPassword))
131-
{
132-
while (!simpleGet.IsComplete)
133-
yield return null;
134-
135-
result = simpleGet.Result;
136-
}
137-
138-
// Add in a parent object because Unity does not like to deserialize root level collection types.
139-
result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");
140-
141-
// Convert json to fsResult
142-
fsResult r = fsJsonParser.Parse(result, out data);
143-
if (!r.Succeeded)
144-
throw new WatsonException(r.FormattedMessages);
145-
146-
// Convert fsResult to VcapCredentials
147-
object obj = vcapCredentials;
148-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
149-
if (!r.Succeeded)
150-
throw new WatsonException(r.FormattedMessages);
151-
152-
// Set credentials from imported credntials
153-
Credential credential = vcapCredentials.VCAP_SERVICES["conversation"];
154-
_username = credential.Username.ToString();
155-
_password = credential.Password.ToString();
156-
_url = credential.Url.ToString();
157-
//_workspaceId = credential.WorkspaceId.ToString();
158-
#endregion
159116

160117
// Create credential and instantiate service
161118
Credentials credentials = new Credentials(_username, _password, _url);
@@ -203,10 +160,42 @@ private IEnumerator Examples()
203160
input.Add("text", _inputString);
204161
MessageRequest messageRequest = new MessageRequest()
205162
{
206-
Input = input,
207-
AlternateIntents = true
163+
Input = input
208164
};
209165
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest);
166+
while (!_messageTested)
167+
yield return null;
168+
_messageTested = false;
169+
170+
input["text"] = _conversationString0;
171+
MessageRequest messageRequest0 = new MessageRequest()
172+
{
173+
Input = input,
174+
Context = _context
175+
};
176+
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest0);
177+
while (!_messageTested)
178+
yield return null;
179+
_messageTested = false;
180+
181+
input["text"] = _conversationString1;
182+
MessageRequest messageRequest1 = new MessageRequest()
183+
{
184+
Input = input,
185+
Context = _context
186+
};
187+
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest1);
188+
while (!_messageTested)
189+
yield return null;
190+
_messageTested = false;
191+
192+
input["text"] = _conversationString2;
193+
MessageRequest messageRequest2 = new MessageRequest()
194+
{
195+
Input = input,
196+
Context = _context
197+
};
198+
_service.Message(OnMessage, OnFail, _workspaceId, messageRequest2);
210199
while (!_messageTested)
211200
yield return null;
212201

@@ -677,9 +666,40 @@ private void OnListIntents(IntentCollection response, Dictionary<string, object>
677666
_listIntentsTested = true;
678667
}
679668

680-
private void OnMessage(MessageResponse response, Dictionary<string, object> customData)
669+
private void OnMessage(object response, Dictionary<string, object> customData)
681670
{
682671
Log.Debug("ExampleAssistant.OnMessage()", "Response: {0}", customData["json"].ToString());
672+
673+
// Convert resp to fsdata
674+
fsData fsdata = null;
675+
fsResult r = _serializer.TrySerialize(response.GetType(), response, out fsdata);
676+
if (!r.Succeeded)
677+
throw new WatsonException(r.FormattedMessages);
678+
679+
// Convert fsdata to MessageResponse
680+
MessageResponse messageResponse = new MessageResponse();
681+
object obj = messageResponse;
682+
r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
683+
if (!r.Succeeded)
684+
throw new WatsonException(r.FormattedMessages);
685+
686+
// Set context for next round of messaging
687+
object _tempContext = null;
688+
(response as Dictionary<string, object>).TryGetValue("context", out _tempContext);
689+
690+
if (_tempContext != null)
691+
_context = _tempContext as Dictionary<string, object>;
692+
else
693+
Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
694+
695+
// Get intent
696+
object tempIntentsObj = null;
697+
(response as Dictionary<string, object>).TryGetValue("intents", out tempIntentsObj);
698+
object tempIntentObj = (tempIntentsObj as List<object>)[0];
699+
object tempIntent = null;
700+
(tempIntentObj as Dictionary<string, object>).TryGetValue("intent", out tempIntent);
701+
string intent = tempIntent.ToString();
702+
683703
_messageTested = true;
684704
}
685705

Scripts/Services/Assistant/v1/Assistant.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using IBM.Watson.DeveloperCloud.Logging;
2323
using IBM.Watson.DeveloperCloud.Utilities;
2424
using System;
25+
using MiniJSON;
2526

2627
namespace IBM.Watson.DeveloperCloud.Services.Assistant.v1
2728
{
@@ -108,24 +109,47 @@ public Assistant(Credentials credentials)
108109
/// <param name="workspaceId">Unique identifier of the workspace.</param>
109110
/// <param name="request">The message to be sent. This includes the user's input, along with optional intents, entities, and context from the last response. (optional)</param>
110111
/// <param name="nodesVisitedDetails">Whether to include additional diagnostic information about the dialog nodes that were visited during processing of the message. (optional, default to false)</param>
111-
/// <returns><see cref="MessageResponse" />MessageResponse</returns>
112112
/// <param name="customData">A Dictionary<string, object> of data that will be passed to the callback. The raw json output from the REST call will be passed in this object as the value of the 'json' key.</string></param>
113-
public bool Message(SuccessCallback<MessageResponse> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null)
113+
public bool Message(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null)
114114
{
115115
if (successCallback == null)
116116
throw new ArgumentNullException("successCallback");
117117
if (failCallback == null)
118118
throw new ArgumentNullException("failCallback");
119119

120+
IDictionary<string, string> requestDict = new Dictionary<string, string>();
121+
if (request.Context != null)
122+
requestDict.Add("context", Json.Serialize(request.Context));
123+
if (request.Input != null)
124+
requestDict.Add("input", Json.Serialize(request.Input));
125+
if(request.AlternateIntents != null)
126+
requestDict.Add("alternate_intents", Json.Serialize(request.AlternateIntents));
127+
if (request.Entities != null)
128+
requestDict.Add("entities", Json.Serialize(request.Entities));
129+
if (request.Intents != null)
130+
requestDict.Add("intents", Json.Serialize(request.Intents));
131+
if (request.Output != null)
132+
requestDict.Add("output", Json.Serialize(request.Output));
133+
134+
int iterator = 0;
135+
StringBuilder stringBuilder = new StringBuilder("{");
136+
foreach (KeyValuePair<string, string> property in requestDict)
137+
{
138+
string delimeter = iterator < requestDict.Count - 1 ? "," : "";
139+
stringBuilder.Append(string.Format("\"{0}\": {1}{2}", property.Key, property.Value, delimeter));
140+
iterator++;
141+
}
142+
stringBuilder.Append("}");
143+
144+
string stringToSend = stringBuilder.ToString();
145+
120146
MessageRequestObj req = new MessageRequestObj();
121147
req.SuccessCallback = successCallback;
122148
req.FailCallback = failCallback;
123149
req.CustomData = customData == null ? new Dictionary<string, object>() : customData;
124150
req.Parameters["version"] = VersionDate;
125151
req.Headers["Content-Type"] = "application/json";
126-
fsData data = null;
127-
_serializer.TrySerialize(request.Input, out data);
128-
req.Send = Encoding.UTF8.GetBytes(data.ToString());
152+
req.Send = Encoding.UTF8.GetBytes(stringToSend);
129153
req.OnResponse = OnMessageResponse;
130154

131155
RESTConnector connector = RESTConnector.GetConnector(Credentials, string.Format("/v1/workspaces/{0}/message", workspaceId));
@@ -140,7 +164,7 @@ private class MessageRequestObj : RESTConnector.Request
140164
/// <summary>
141165
/// The success callback.
142166
/// </summary>
143-
public SuccessCallback<MessageResponse> SuccessCallback { get; set; }
167+
public SuccessCallback<object> SuccessCallback { get; set; }
144168
/// <summary>
145169
/// The fail callback.
146170
/// </summary>
@@ -153,23 +177,17 @@ private class MessageRequestObj : RESTConnector.Request
153177

154178
private void OnMessageResponse(RESTConnector.Request req, RESTConnector.Response resp)
155179
{
156-
MessageResponse result = new MessageResponse();
157-
fsData data = null;
180+
object result = null;
181+
string data = "";
158182
Dictionary<string, object> customData = ((MessageRequestObj)req).CustomData;
159183

160184
if (resp.Success)
161185
{
162186
try
163187
{
164-
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
165-
if (!r.Succeeded)
166-
throw new WatsonException(r.FormattedMessages);
167-
168-
object obj = result;
169-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
170-
if (!r.Succeeded)
171-
throw new WatsonException(r.FormattedMessages);
172-
188+
// For deserializing into a generic object
189+
data = Encoding.UTF8.GetString(resp.Data);
190+
result = Json.Deserialize(data);
173191
customData.Add("json", data);
174192
}
175193
catch (Exception e)

Scripts/Services/Assistant/v1/IAssistant.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace IBM.Watson.DeveloperCloud.Services.Assistant.v1
3737

3838
public interface IAssistant
3939
{
40-
bool Message(SuccessCallback<MessageResponse> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null);
40+
bool Message(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceId, MessageRequest request = null, bool? nodesVisitedDetails = null, Dictionary<string, object> customData = null);
4141
bool CreateWorkspace(SuccessCallback<Workspace> successCallback, FailCallback failCallback, CreateWorkspace properties = null, Dictionary<string, object> customData = null);
4242

4343
bool DeleteWorkspace(SuccessCallback<object> successCallback, FailCallback failCallback, string workspaceId, Dictionary<string, object> customData = null);

Scripts/Services/Assistant/v1/Model/MessageRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MessageRequest
3131
/// </summary>
3232
/// <value>An input object that includes the input text.</value>
3333
[fsProperty("input")]
34-
public Dictionary<string, object> Input { get; set; }
34+
public object Input { get; set; }
3535
/// <summary>
3636
/// Whether to return more than one intent. Set to `true` to return all matching intents.
3737
/// </summary>
@@ -43,7 +43,7 @@ public class MessageRequest
4343
/// </summary>
4444
/// <value>State information for the conversation. Continue a conversation by including the context object from the previous response.</value>
4545
[fsProperty("context")]
46-
public Dictionary<string, object> Context { get; set; }
46+
public object Context { get; set; }
4747
/// <summary>
4848
/// Entities to use when evaluating the message. Include entities from the previous response to continue using those entities rather than detecting entities in the new input.
4949
/// </summary>

Scripts/Services/Assistant/v1/Model/MessageResponse.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,37 @@ public class MessageResponse
3131
/// </summary>
3232
/// <value>The user input from the request.</value>
3333
[fsProperty("input")]
34-
public Dictionary<string, object> Input { get; set; }
34+
public object Input { get; set; }
3535
/// <summary>
3636
/// An array of intents recognized in the user input, sorted in descending order of confidence.
3737
/// </summary>
3838
/// <value>An array of intents recognized in the user input, sorted in descending order of confidence.</value>
3939
[fsProperty("intents")]
40-
public Dictionary<string, object> Intents { get; set; }
40+
public object Intents { get; set; }
4141
/// <summary>
4242
/// An array of entities identified in the user input.
4343
/// </summary>
4444
/// <value>An array of entities identified in the user input.</value>
4545
[fsProperty("entities")]
46-
public Dictionary<string, object> Entities { get; set; }
46+
public object Entities { get; set; }
4747
/// <summary>
4848
/// Whether to return more than one intent. A value of `true` indicates that all matching intents are returned.
4949
/// </summary>
5050
/// <value>Whether to return more than one intent. A value of `true` indicates that all matching intents are returned.</value>
5151
[fsProperty("alternate_intents")]
52-
public Dictionary<string, object> AlternateIntents { get; set; }
52+
public object AlternateIntents { get; set; }
5353
/// <summary>
5454
/// State information for the conversation.
5555
/// </summary>
5656
/// <value>State information for the conversation.</value>
5757
[fsProperty("context")]
58-
public Dictionary<string, object> Context { get; set; }
58+
public object Context { get; set; }
5959
/// <summary>
6060
/// Output from the dialog, including the response to the user, the nodes that were triggered, and log messages.
6161
/// </summary>
6262
/// <value>Output from the dialog, including the response to the user, the nodes that were triggered, and log messages.</value>
6363
[fsProperty("output")]
64-
public Dictionary<string, object> Output { get; set; }
64+
public object Output { get; set; }
6565
}
6666

6767
}

0 commit comments

Comments
 (0)