From 52eaeab5fc02ecb24ba191aef4e7515e2788563e Mon Sep 17 00:00:00 2001 From: narawat Date: Tue, 4 Aug 2026 14:51:53 +0700 Subject: [PATCH] update --- src/agentCore.jl | 41 +++++++++++++++++++++++------------------ src/type.jl | 1 + 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/agentCore.jl b/src/agentCore.jl index 2a55205..2355c73 100644 --- a/src/agentCore.jl +++ b/src/agentCore.jl @@ -35,18 +35,19 @@ julia> # Called automatically by yiemAgent constructor function _agent_loop(agent::yiemAgent) try while true - # Wait on either channel — the one with a message fires first - # Wait on either channel — the one with a message is taken first msg = nothing while msg === nothing - if isready(agent.inputChannel) - msg = take!(agent.inputChannel) - - #TODO convert raw user msg to userMessage type - - #TODO add userMessage to agent._state.messages - else + if isready(agent.inputChannel) && agent._state.activeRun == false + # allow _process_message() to run yield() + elseif isready(agent.inputChannel) && agent._state.activeRun == true + msg = fetch!(agent.inputChannel) + # Check for shutdown signal + if msg === :shutdown + msg = take!(agent.inputChannel) + end + else + error("undefined condition: isready(inputChannel)=$(isready(agent.inputChannel)), activeRun=$(agent._state.activeRun), msg=$msg") end end @@ -56,19 +57,23 @@ function _agent_loop(agent::yiemAgent) break end + # make active + agent._state.activeRun = true # Dispatch message through the processing pipeline result = _process_message(agent, msg) - - # check followUp message. if there are, add them all to agent.inputChannel + hasMore = isready(agent.followUpChannel) + while isready(agent.followUpChannel) + followMsg = take!(agent.followUpChannel) + put!(agent.inputChannel, followMsg) + hasMore = true + end - - - - # Send response to user if no user message in both agent.inputChannel and agent.followUpChannel, - # output the result - put!(agent.outputChannel, result) + if !isready(agent.inputChannel) && !hasMore + # no more messages queued — safe to send response + put!(agent.outputChannel, result) + end end catch e # On any error, send error response and exit the loop @@ -107,7 +112,7 @@ julia> # Currently returns a placeholder echo response function _process_message(agent::yiemAgent, msg) # WORKING Replace with actual processing logic - while (# ) + diff --git a/src/type.jl b/src/type.jl index 1856a56..a2d8bc0 100644 --- a/src/type.jl +++ b/src/type.jl @@ -252,6 +252,7 @@ mutable struct agentState # Mutable runtime state of an agen tools::Vector{agentTool} # Available tools messages::Vector{agentMessage} # Conversation messages pendingToolCalls::Vector{String} # Tool call IDs waiting for results + activeRun::Bool # is agent processing user message? errorMessage::Union{String, Nothing} # Last error message end