diff --git a/src/interface.jl b/src/interface.jl index 4fef87e..57cda40 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -176,21 +176,44 @@ function decisionMaker(a::T1, state::T2)::Dict{Symbol, Any} where {T1<:agent, T2 ) ) - _response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) - _thoughtJsonStr = _response[:response][:text] - expectedJsonExample = - """ - Here is an expected JSON format: - { - "thought": "...", - "action": {"name": "...", "input": "..."}, - "observation": "..." - } - """ - thoughtJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample) - thoughtDict = copy(JSON3.read(thoughtJsonStr)) + attempt = 0 + while attempt <= 5 + attempt += 1 + try + response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) + _responseJsonStr = response[:response][:text] + expectedJsonExample = + """ + Here is an expected JSON format: + { + "thought": "...", + "action": {"name": "...", "input": "..."}, + "observation": "..." + } + """ + responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) + thoughtDict = copy(JSON3.read(responseJsonStr)) - return thoughtDict + # check if dict has all required value + dummya::AbstractString = thoughtDict[:thought] + dummyb::AbstractString = thoughtDict[:action][:name] + dummyc::AbstractString = thoughtDict[:action][:input] + + return thoughtDict + catch e + io = IOBuffer() + showerror(io, e) + errorMsg = String(take!(io)) + st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) + println("") + @warn "Error occurred: $errorMsg\n$st" + println("") + end + + if attempt > 5 + error("DecisionMaker failed to generate a thought") + end + end end @@ -286,17 +309,39 @@ function progressValueEstimator(a::T1, state::T2)::Tuple{String, Integer} where ) ) - _response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) - _thoughtJsonStr = _response[:response][:text] - expectedJsonExample = - """ - Here is an expected JSON format: - {"evaluation": "...", "score": "..."} - """ - resultJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample) - resultDict = copy(JSON3.read(resultJsonStr)) + attempt = 0 + while attempt <= 5 + attempt += 1 + try + response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) + _responseJsonStr = response[:response][:text] + expectedJsonExample = + """ + Here is an expected JSON format: + {"evaluation": "...", "score": "..."} + """ + responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) + evaluationDict = copy(JSON3.read(responseJsonStr)) - return resultDict[:evaluation], resultDict[:score] + # check if dict has all required value + dummya::AbstractString = evaluationDict[:evaluation] + dummyb::AbstractString = evaluationDict[:score] + + return (evaluationDict[:evaluation], evaluationDict[:score]) + catch e + io = IOBuffer() + showerror(io, e) + errorMsg = String(take!(io)) + st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) + println("") + @warn "Error occurred: $errorMsg\n$st" + println("") + end + + if attempt > 5 + error("progressValueEstimator failed to generate a thought") + end + end end @@ -313,8 +358,8 @@ julia> # TODO - [] update docstring - - [WORKING] implement the function - - [PENDING] add try block. check result that it is expected before returning + - [x] implement the function + - [x] add try block. check result that it is expected before returning # Signature """ @@ -400,21 +445,38 @@ function reflector(a::T1, state::T2)::String where {T1<:agent, T2<:AbstractDict} ) ) - _response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) - _thoughtJsonStr = _response[:response][:text] + attempt = 0 + while attempt <= 5 + attempt += 1 + try + response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) + _responseJsonStr = response[:response][:text] + expectedJsonExample = + """ + Here is an expected JSON format: + {"reflection": "..."} + """ + responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) + reflectionDict = copy(JSON3.read(responseJsonStr)) - # expectedJsonExample = - # """ - # Here is an expected JSON format: - # {"evaluation": "...", "score": "..."} - # """ - # resultJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample) - # resultDict = copy(JSON3.read(resultJsonStr)) + # check if dict has all required value + dummya::AbstractString = reflectionDict[:reflection] - # return resultDict[:evaluation], resultDict[:score] + return reflectionDict[:reflection] + catch e + io = IOBuffer() + showerror(io, e) + errorMsg = String(take!(io)) + st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) + println("") + @warn "Error occurred: $errorMsg\n$st" + println("") + end - - return _thoughtJsonStr + if attempt > 5 + error("reflector failed to generate a thought") + end + end end