This commit is contained in:
narawat lamaiin
2024-04-18 18:04:51 +07:00
parent 64f488dbb3
commit 371d4b149b
2 changed files with 34 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ module interface
export addNewMessage, conversation export addNewMessage, conversation
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random using JSON3, DataStructures, Dates, UUIDs, HTTP, Random, MQTTClient
using GeneralUtils using GeneralUtils
using ..type, ..util, ..llmfunction using ..type, ..util, ..llmfunction
@@ -89,18 +89,45 @@ function conversation(a::T) where {T<:agent}
while true while true
println("---> conversation loop") println("---> conversation loop")
println("---> a.receiveUserMsgChannel ready = $(isready(a.receiveUserMsgChannel))") println("---> a.receiveUserMsgChannel ready = $(isready(a.receiveUserMsgChannel))")
#TODO add "newtopic" command to delete history
# check for incoming user message # check for incoming user message
if isready(a.receiveUserMsgChannel) if isready(a.receiveUserMsgChannel)
println("---> success 1")
incomingMsg = take!(a.receiveUserMsgChannel) incomingMsg = take!(a.receiveUserMsgChannel)
println("---> success 2") incomingPayload = incomingMsg[:payload]
if incomingPayload[:text] == "newtopic"
clearhistory(a)
@show incomingMsg @show incomingMsg
msgMeta = deepcopy(a.msgMeta)
msgMeta[:sendTopic] = incomingMsg[:msgMeta][:replyTopic]
msgMeta[:senderName] = "agent-backend"
msgMeta[:senderId] = a.id
msgMeta[:receiverName] = "agent-frontend"
msgMeta[:receiverId] = incomingMsg[:msgMeta][:senderId]
msgMeta[:replyTopic] = a.config[:receivemsg][:prompt]
msgMeta[:msgId] = string(uuid4())
msgMeta[:replyToMsgId] = incomingMsg[:msgMeta][:msgId]
outgoingMsg = Dict(
:msgMeta=> msgMeta,
:payload=> Dict(
:name=> a.name, # will be shown in frontend as agent name
:text => "Okay. What shall we talk about?",
)
)
client, connection = MakeConnection(a.config[:mqttinfo][:broker],
a.config[:mqttinfo][:port])
connect(client, connection)
@show outgoingMsg
publish(client, outgoingMsg[:msgMeta][:sendTopic],
JSON3.write(outgoingMsg))
disconnect(client)
println("published")
else
end
end end
# # if new user message available
# # if new message == "newtopic" # # if new message == "newtopic"
# #TODO add "newtopic" command to delete history # #TODO add "newtopic" command to delete history

View File

@@ -109,7 +109,6 @@ abstract type agent end
) )
# communication # communication
mqttClient::Any= nothing # store mqtt client for use in various internal functions
msgMeta::Dict{Symbol, Any} # a template for msgMeta msgMeta::Dict{Symbol, Any} # a template for msgMeta
# put incoming message here. waiting for further processing # put incoming message here. waiting for further processing
@@ -120,7 +119,6 @@ end
function sommelier( function sommelier(
receiveUserMsgChannel::Channel, receiveUserMsgChannel::Channel,
receiveInternalMsgChannel::Channel, receiveInternalMsgChannel::Channel,
mqttClient,
msgMeta::Dict= GeneralUtils.generate_msgMeta("N/A"), msgMeta::Dict= GeneralUtils.generate_msgMeta("N/A"),
config::Dict = Dict( config::Dict = Dict(
:frontend=> Dict( :frontend=> Dict(
@@ -160,7 +158,6 @@ function sommelier(
name= name, name= name,
id= id, id= id,
config= config, config= config,
mqttClient= mqttClient,
msgMeta= msgMeta, msgMeta= msgMeta,
maxHistoryMsg= maxHistoryMsg, maxHistoryMsg= maxHistoryMsg,
tools= tools, tools= tools,