123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230(*
* This file is part of the Watson Conversation Service OCaml API project.
*
* Copyright 2016-2017 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)openWcs_libopenWcs_tmoduleMake(Wcs_call:sigvalmessage:credential->string->message_request->message_responseend)=structletpretty_json_strings=(Yojson.Basic.pretty_to_string(Yojson.Basic.from_strings))letbefore_default(msg_req:message_request):message_request=msg_reqletafter_default(msg_resp:message_response):message_response=msg_respletuser_input_default()=print_string"H: ";flushstdout;input_linestdinletoutput_defaulttxt=print_string"C: ";print_endlinetxtletinterpret?(before=before_default)?(after=after_default)(wcs_cred:credential):(string->message_request->string*message_response*jsonoption)=letrecinterpret(ws_id:string)(req_msg:message_request):string*message_response*jsonoption=letreq_msg=beforereq_msginLog.debug"Wcs_bot"("Request:\n"^(Wcs_pretty.message_requestreq_msg));letresp=Wcs_call.messagewcs_credws_idreq_msginLog.debug"Wcs_bot"("Response:\n"^(Wcs_pretty.message_responseresp));letresp=afterrespinletctx=resp.msg_rsp_contextinletoutput=resp.msg_rsp_outputinbeginmatchContext.take_actionsctxwith|ctx,Some[act]->letk={act_name=ws_id;act_agent="client";act_type_="conversation";act_parameters=Json.assoc["context",ctx];act_result_variable=act.act_result_variable;}inletact_ctx=beginmatchJson.getact.act_parameters"context"with|None->`Null|Somectx->ctxendinletact_parameters=Json.setact.act_parameters"context"(Context.set_continuationact_ctxk)inletact={actwithact_parameters=act_parameters}ininterpret_actionactoutput|ctx,Some(_::_::_)->assertfalse(* XXX TODO XXX *)|ctx,Some[]|ctx,None->letctx,skip_user_input=Context.take_skip_user_inputctxinbeginmatchContext.get_returnctxwith|Somev->beginmatchContext.get_continuationctxwith|Somek->letk_txt=ifskip_user_inputthenreq_msg.msg_req_input.in_textelse""inletk_ctx=beginmatchJson.getk.act_parameters"context"with|Somectx->ctx|None->`Nullendinletk_ctx=beginmatchk.act_result_variablewith|None->k_ctx|Somelbl->letprefix=String.sublbl08inletvar=String.sublbl8(String.lengthlbl-8)inassert(prefix="context.");Json.setk_ctxvarvendinletk_ctx,k_skip_user_input=Context.take_skip_user_inputk_ctxinifk_skip_user_inputthenletk_parameters=Json.assign[k.act_parameters;Json.assoc["text",Json.stringk_txt];Json.assoc["context",k_ctx];]inletk={kwithact_parameters=k_parameters}ininterpret_actionkoutputelseletk_resp={respwithmsg_rsp_context=k_ctx}in(k.act_name,k_resp,Context.get_returnk_ctx)|None->(ws_id,resp,Somev)end|None->ifskip_user_inputtheninterpretws_id{req_msgwithmsg_req_context=Somectx;msg_req_output=Someoutput;}else(ws_id,{respwithmsg_rsp_context=ctx},None)endendandinterpret_actionactoutput=beginmatchact.act_agent,act.act_type_with|"client","conversation"->letctx=beginmatchJson.getact.act_parameters"context"with|None->`Null|Somectx->ctxendinlettxt=beginmatchJson.get_stringact.act_parameters"text"with|None->""|Somes->sendinletreq_msg={msg_req_input={in_text=txt};msg_req_alternate_intents=false;msg_req_context=Somectx;msg_req_entities=None;msg_req_intents=None;msg_req_output=Someoutput;}ininterpretact.act_namereq_msg|_->assertfalseendininterpretletexec?(before=before_default)?(after=after_default)?(user_input=user_input_default)?(output=output_default)(wcs_cred:credential)(workspace_id:string)(ctx_init:json)(txt_init:string):json=letinterpret=interpret~before~afterwcs_credinletrecloopws_idctxtxt=letreq={msg_req_input={in_text=txt};msg_req_alternate_intents=false;msg_req_context=Somectx;msg_req_entities=None;msg_req_intents=None;msg_req_output=None;}inletws_id,rsp,return=interpretws_idreqinList.iteroutputrsp.msg_rsp_output.out_text;beginmatchreturnwith|Somev->v|None->lettxt=user_input()inletctx=rsp.msg_rsp_contextinloopws_idctxtxtendinloopworkspace_idctx_inittxt_initletget_credentialfile_name_opt=begintryletfile_name=beginmatchfile_name_optwith|Somefile_name->file_name|None->Sys.getenv"WCS_CRED"endinJson.read_json_fileWcs_j.read_credentialfile_namewith|Not_found->Log.error"Wcs_bot"None("no credential file")|exn->Log.error"Wcs_bot"None(Printexc.to_stringexn)endend