update
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
module agentCore
|
||||
|
||||
# export prompt
|
||||
|
||||
using JSON, DataStructures, Dates, UUIDs, HTTP, Random, PrettyPrinting, Serialization,
|
||||
DataFrames, Serde
|
||||
using GeneralUtils
|
||||
using ..type, ..util, ..llmfunction
|
||||
|
||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||
|
||||
"""
|
||||
Private agent loop. Runs in a background `@spawn` task.
|
||||
|
||||
Waits on `input_ch` and `followUpQueue`, processing whichever has a message first.
|
||||
On each iteration, dispatches the message through `_process_message` and sends the result
|
||||
to `output_ch`. Exits on `:shutdown` signal.
|
||||
|
||||
# Arguments
|
||||
- `agent::yiemAgent`: The agent whose loop to run
|
||||
|
||||
# Returns
|
||||
- `nothing` — the loop runs until `:shutdown` is received or an error occurs
|
||||
|
||||
# Notes
|
||||
- This function is automatically spawned as a background task when a `yiemAgent` is created.
|
||||
- On any error, logs the error with `@error` and exits the loop.
|
||||
- Message priority: `input_ch` messages are checked before `followUpQueue` messages.
|
||||
|
||||
# Examples
|
||||
```jldoctest
|
||||
julia> # Called automatically by yiemAgent constructor
|
||||
```
|
||||
"""
|
||||
function _agent_loop(agent::yiemAgent) #WORKING
|
||||
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.input_ch)
|
||||
msg = take!(agent.input_ch)
|
||||
else
|
||||
yield()
|
||||
end
|
||||
end
|
||||
|
||||
# Check for shutdown signal
|
||||
if msg === :shutdown
|
||||
#TODO make sure every running tools ended properly
|
||||
break
|
||||
end
|
||||
|
||||
#TODO convert raw user msg to userMessage type
|
||||
|
||||
#TODO add userMessage to agent._state.messages
|
||||
|
||||
|
||||
if isready(agent.followUpQueue)
|
||||
msg = take!(agent.followUpQueue)
|
||||
end
|
||||
|
||||
# @spawn. Dispatch message through the processing pipeline.
|
||||
result = _process_message(agent, msg)
|
||||
|
||||
# Send response to user
|
||||
put!(agent.output_ch, result)
|
||||
end
|
||||
catch e
|
||||
# On any error, send error response and exit the loop
|
||||
@error "Agent loop failed" error=e
|
||||
end
|
||||
end
|
||||
|
||||
"""
|
||||
Process a single message through the agent pipeline.
|
||||
|
||||
This is the core processing function where LLM calls, tool execution, and response generation
|
||||
should be implemented. Currently a placeholder that echoes back the received message.
|
||||
|
||||
# Arguments
|
||||
- `agent::yiemAgent`: The agent processing the message
|
||||
- `msg`: The message to process (from `input_ch` or `followUpQueue`)
|
||||
|
||||
# Returns
|
||||
- An `assistantMessage` instance with the processed response
|
||||
|
||||
# Notes
|
||||
- Implement the full processing pipeline:
|
||||
1. Add `msg` to `agent._state.messages`
|
||||
2. Call `agent.formatMsgForLLM(agent._state)` 1 to format for LLM
|
||||
3. If `agent.preprocessMessages` is set, call it on the formatted messages
|
||||
4. Call the LLM (blocking — the task waits here)
|
||||
5. If agent has tools, handle tool calls in a loop
|
||||
6. Build `assistantMessage` and return it
|
||||
|
||||
# Examples
|
||||
```jldoctest
|
||||
julia> # Currently returns a placeholder echo response
|
||||
```
|
||||
"""
|
||||
function _process_message(agent::yiemAgent, msg)
|
||||
# WORKING Replace with actual processing logic
|
||||
# check steering message
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# 1. call agent.
|
||||
# 2. Call agent.formatMsgForLLM(agent._state) to format for LLM
|
||||
# 3. If preprocessMessages is set, call agent.preprocessMessages(...)
|
||||
# 4. Call the LLM (blocking — the task waits here)
|
||||
# 5. If agent has tools, handle tool calls in a loop
|
||||
# 6. Build assistantMessage and return it
|
||||
|
||||
# Placeholder: echo back the message as a simple response
|
||||
@warn "TODO: implement _process_message"
|
||||
return assistantMessage(
|
||||
role="assistant",
|
||||
content=[textContent("Received: $(msg)")],
|
||||
api="", model="", usage=nothing,
|
||||
stopReason="end_turn",
|
||||
errorMessage=nothing,
|
||||
timestamp=now(),
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # end of module
|
||||
Reference in New Issue
Block a user