Skip to content

Commit 9998de9

Browse files
committed
feat(AssistantV2): add support for stateless messages
1 parent 7e5ea7c commit 9998de9

27 files changed

+605
-31
lines changed

Scripts/Services/Assistant/V2/AssistantService.cs

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ private void OnDeleteSessionResponse(RESTConnector.Request req, RESTConnector.Re
242242
((RequestObject<object>)req).Callback(response, resp.Error);
243243
}
244244
/// <summary>
245-
/// Send user input to assistant.
245+
/// Send user input to assistant (stateful).
246246
///
247-
/// Send user input to an assistant and receive a response.
247+
/// Send user input to an assistant and receive a response, with conversation state (including context data)
248+
/// stored by Watson Assistant for the duration of the session.
248249
///
249250
/// There is no rate limit for this operation.
250251
/// </summary>
@@ -257,9 +258,12 @@ private void OnDeleteSessionResponse(RESTConnector.Request req, RESTConnector.Re
257258
/// **Note:** Currently, the v2 API does not support creating assistants.</param>
258259
/// <param name="sessionId">Unique identifier of the session.</param>
259260
/// <param name="input">An input object that includes the input text. (optional)</param>
260-
/// <param name="context">State information for the conversation. The context is stored by the assistant on a
261-
/// per-session basis. You can use this property to set or modify context variables, which can also be accessed
262-
/// by dialog nodes. (optional)</param>
261+
/// <param name="context">Context data for the conversation. You can use this property to set or modify context
262+
/// variables, which can also be accessed by dialog nodes. The context is stored by the assistant on a
263+
/// per-session basis.
264+
///
265+
/// **Note:** The total size of the context data stored for a stateful session cannot exceed 100KB.
266+
/// (optional)</param>
263267
/// <returns><see cref="MessageResponse" />MessageResponse</returns>
264268
public bool Message(Callback<MessageResponse> callback, string assistantId, string sessionId, MessageInput input = null, MessageContext context = null)
265269
{
@@ -332,5 +336,96 @@ private void OnMessageResponse(RESTConnector.Request req, RESTConnector.Response
332336
if (((RequestObject<MessageResponse>)req).Callback != null)
333337
((RequestObject<MessageResponse>)req).Callback(response, resp.Error);
334338
}
339+
/// <summary>
340+
/// Send user input to assistant (stateless).
341+
///
342+
/// Send user input to an assistant and receive a response, with conversation state (including context data)
343+
/// managed by your application.
344+
///
345+
/// There is no rate limit for this operation.
346+
/// </summary>
347+
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
348+
/// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson
349+
/// Assistant user interface, open the assistant settings and click **API Details**. For information about
350+
/// creating assistants, see the
351+
/// [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-add#assistant-add-task).
352+
///
353+
/// **Note:** Currently, the v2 API does not support creating assistants.</param>
354+
/// <param name="input">An input object that includes the input text. (optional)</param>
355+
/// <param name="context">Context data for the conversation. You can use this property to set or modify context
356+
/// variables, which can also be accessed by dialog nodes. The context is not stored by the assistant. To
357+
/// maintain session state, include the context from the previous response.
358+
///
359+
/// **Note:** The total size of the context data for a stateless session cannot exceed 250KB. (optional)</param>
360+
/// <returns><see cref="MessageResponseStateless" />MessageResponseStateless</returns>
361+
public bool MessageStateless(Callback<MessageResponseStateless> callback, string assistantId, MessageInputStateless input = null, MessageContextStateless context = null)
362+
{
363+
if (callback == null)
364+
throw new ArgumentNullException("`callback` is required for `MessageStateless`");
365+
if (string.IsNullOrEmpty(assistantId))
366+
throw new ArgumentNullException("`assistantId` is required for `MessageStateless`");
367+
368+
RequestObject<MessageResponseStateless> req = new RequestObject<MessageResponseStateless>
369+
{
370+
Callback = callback,
371+
HttpMethod = UnityWebRequest.kHttpVerbPOST,
372+
DisableSslVerification = DisableSslVerification
373+
};
374+
375+
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
376+
{
377+
req.Headers.Add(kvp.Key, kvp.Value);
378+
}
379+
380+
ClearCustomRequestHeaders();
381+
382+
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "MessageStateless"))
383+
{
384+
req.Headers.Add(kvp.Key, kvp.Value);
385+
}
386+
387+
req.Parameters["version"] = VersionDate;
388+
req.Headers["Content-Type"] = "application/json";
389+
req.Headers["Accept"] = "application/json";
390+
391+
JObject bodyObject = new JObject();
392+
if (input != null)
393+
bodyObject["input"] = JToken.FromObject(input);
394+
if (context != null)
395+
bodyObject["context"] = JToken.FromObject(context);
396+
req.Send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bodyObject));
397+
398+
req.OnResponse = OnMessageStatelessResponse;
399+
400+
Connector.URL = GetServiceUrl() + string.Format("/v2/assistants/{0}/message", assistantId);
401+
Authenticator.Authenticate(Connector);
402+
403+
return Connector.Send(req);
404+
}
405+
406+
private void OnMessageStatelessResponse(RESTConnector.Request req, RESTConnector.Response resp)
407+
{
408+
DetailedResponse<MessageResponseStateless> response = new DetailedResponse<MessageResponseStateless>();
409+
foreach (KeyValuePair<string, string> kvp in resp.Headers)
410+
{
411+
response.Headers.Add(kvp.Key, kvp.Value);
412+
}
413+
response.StatusCode = resp.HttpResponseCode;
414+
415+
try
416+
{
417+
string json = Encoding.UTF8.GetString(resp.Data);
418+
response.Result = JsonConvert.DeserializeObject<MessageResponseStateless>(json);
419+
response.Response = json;
420+
}
421+
catch (Exception e)
422+
{
423+
Log.Error("AssistantService.OnMessageStatelessResponse()", "Exception: {0}", e.ToString());
424+
resp.Success = false;
425+
}
426+
427+
if (((RequestObject<MessageResponseStateless>)req).Callback != null)
428+
((RequestObject<MessageResponseStateless>)req).Callback(response, resp.Error);
429+
}
335430
}
336431
}

Scripts/Services/Assistant/V2/Model/MessageContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ namespace IBM.Watson.Assistant.V2.Model
2525
public class MessageContext
2626
{
2727
/// <summary>
28-
/// Information that is shared by all skills used by the Assistant.
28+
/// Session context data that is shared by all skills used by the Assistant.
2929
/// </summary>
3030
[JsonProperty("global", NullValueHandling = NullValueHandling.Ignore)]
3131
public MessageContextGlobal Global { get; set; }
3232
/// <summary>
33-
/// Information specific to particular skills used by the Assistant.
33+
/// Information specific to particular skills used by the assistant.
3434
///
35-
/// **Note:** Currently, only a single property named `main skill` is supported. This object contains variables
36-
/// that apply to the dialog skill used by the assistant.
35+
/// **Note:** Currently, only a single child property is supported, containing variables that apply to the
36+
/// dialog skill used by the assistant.
3737
/// </summary>
3838
[JsonProperty("skills", NullValueHandling = NullValueHandling.Ignore)]
3939
public MessageContextSkills Skills { get; set; }

Scripts/Services/Assistant/V2/Model/MessageContextGlobal.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace IBM.Watson.Assistant.V2.Model
2121
{
2222
/// <summary>
23-
/// Information that is shared by all skills used by the Assistant.
23+
/// Session context data that is shared by all skills used by the Assistant.
2424
/// </summary>
2525
public class MessageContextGlobal
2626
{
@@ -29,5 +29,10 @@ public class MessageContextGlobal
2929
/// </summary>
3030
[JsonProperty("system", NullValueHandling = NullValueHandling.Ignore)]
3131
public MessageContextGlobalSystem System { get; set; }
32+
/// <summary>
33+
/// The session ID.
34+
/// </summary>
35+
[JsonProperty("session_id", NullValueHandling = NullValueHandling.Ignore)]
36+
public virtual string SessionId { get; private set; }
3237
}
3338
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using Newtonsoft.Json;
19+
20+
namespace IBM.Watson.Assistant.V2.Model
21+
{
22+
/// <summary>
23+
/// Session context data that is shared by all skills used by the Assistant.
24+
/// </summary>
25+
public class MessageContextGlobalStateless
26+
{
27+
/// <summary>
28+
/// Built-in system properties that apply to all skills used by the assistant.
29+
/// </summary>
30+
[JsonProperty("system", NullValueHandling = NullValueHandling.Ignore)]
31+
public MessageContextGlobalSystem System { get; set; }
32+
/// <summary>
33+
/// The unique identifier of the session.
34+
/// </summary>
35+
[JsonProperty("session_id", NullValueHandling = NullValueHandling.Ignore)]
36+
public string SessionId { get; set; }
37+
}
38+
}

Scripts/Services/Assistant/V2/Model/MessageContextGlobalStateless.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Services/Assistant/V2/Model/MessageContextSkill.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
namespace IBM.Watson.Assistant.V2.Model
2222
{
2323
/// <summary>
24-
/// Contains information specific to a particular skill used by the Assistant.
24+
/// Contains information specific to a particular skill used by the Assistant. The property name must be the same as
25+
/// the name of the skill (for example, `main skill`).
2526
/// </summary>
2627
public class MessageContextSkill
2728
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Cloud.SDK.Model;
19+
using Newtonsoft.Json;
20+
21+
namespace IBM.Watson.Assistant.V2.Model
22+
{
23+
/// <summary>
24+
/// System context data used by the skill.
25+
/// </summary>
26+
public class MessageContextSkillSystem: DynamicModel<object>
27+
{
28+
/// <summary>
29+
/// An encoded string representing the current conversation state. By saving this value and then sending it in
30+
/// the context of a subsequent message request, you can restore the conversation to the same state. This can be
31+
/// useful if you need to return to an earlier point in the conversation. If you are using stateful sessions,
32+
/// you can also use a stored state value to restore a paused conversation whose session has expired.
33+
/// </summary>
34+
[JsonProperty("state", NullValueHandling = NullValueHandling.Ignore)]
35+
public string State { get; set; }
36+
}
37+
}

Scripts/Services/Assistant/V2/Model/MessageContextSkillSystem.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Services/Assistant/V2/Model/MessageContextSkills.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
namespace IBM.Watson.Assistant.V2.Model
2121
{
2222
/// <summary>
23-
/// Information specific to particular skills used by the Assistant.
23+
/// Information specific to particular skills used by the assistant.
2424
///
25-
/// **Note:** Currently, only a single property named `main skill` is supported. This object contains variables that
26-
/// apply to the dialog skill used by the assistant.
25+
/// **Note:** Currently, only a single child property is supported, containing variables that apply to the dialog
26+
/// skill used by the assistant.
2727
/// </summary>
2828
public class MessageContextSkills: DynamicModel<MessageContextSkill>
2929
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using Newtonsoft.Json;
19+
20+
namespace IBM.Watson.Assistant.V2.Model
21+
{
22+
/// <summary>
23+
/// MessageContextStateless.
24+
/// </summary>
25+
public class MessageContextStateless
26+
{
27+
/// <summary>
28+
/// Session context data that is shared by all skills used by the Assistant.
29+
/// </summary>
30+
[JsonProperty("global", NullValueHandling = NullValueHandling.Ignore)]
31+
public MessageContextGlobalStateless Global { get; set; }
32+
/// <summary>
33+
/// Information specific to particular skills used by the assistant.
34+
///
35+
/// **Note:** Currently, only a single child property is supported, containing variables that apply to the
36+
/// dialog skill used by the assistant.
37+
/// </summary>
38+
[JsonProperty("skills", NullValueHandling = NullValueHandling.Ignore)]
39+
public MessageContextSkills Skills { get; set; }
40+
}
41+
}

Scripts/Services/Assistant/V2/Model/MessageContextStateless.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Services/Assistant/V2/Model/MessageInput.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ public class MessageTypeValue
4949
[JsonProperty("text", NullValueHandling = NullValueHandling.Ignore)]
5050
public string Text { get; set; }
5151
/// <summary>
52-
/// Optional properties that control how the assistant responds.
53-
/// </summary>
54-
[JsonProperty("options", NullValueHandling = NullValueHandling.Ignore)]
55-
public MessageInputOptions Options { get; set; }
56-
/// <summary>
5752
/// Intents to use when evaluating the user input. Include intents from the previous response to continue using
5853
/// those intents rather than trying to recognize intents in the new input.
5954
/// </summary>
@@ -70,5 +65,10 @@ public class MessageTypeValue
7065
/// </summary>
7166
[JsonProperty("suggestion_id", NullValueHandling = NullValueHandling.Ignore)]
7267
public string SuggestionId { get; set; }
68+
/// <summary>
69+
/// Optional properties that control how the assistant responds.
70+
/// </summary>
71+
[JsonProperty("options", NullValueHandling = NullValueHandling.Ignore)]
72+
public MessageInputOptions Options { get; set; }
7373
}
7474
}

0 commit comments

Comments
 (0)