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
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random, MQTTClient
using GeneralUtils
using ..type, ..util, ..llmfunction
@@ -89,18 +89,45 @@ function conversation(a::T) where {T<:agent}
while true
println("---> conversation loop")
println("---> a.receiveUserMsgChannel ready = $(isready(a.receiveUserMsgChannel))")
#TODO add "newtopic" command to delete history
# check for incoming user message
if isready(a.receiveUserMsgChannel)
println("---> success 1")
incomingMsg = take!(a.receiveUserMsgChannel)
println("---> success 2")
@show incomingMsg
incomingPayload = incomingMsg[:payload]
if incomingPayload[:text] == "newtopic"
clearhistory(a)
@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
# # if new user message available
# # if new message == "newtopic"
# #TODO add "newtopic" command to delete history

View File

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