@@ -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}
0 commit comments