This commit is contained in:
2026-08-11 18:28:03 +07:00
parent ae3e432b02
commit 578e8f55bd
3 changed files with 30 additions and 41 deletions
+22 -33
View File
@@ -56,9 +56,8 @@ mutable struct yiemAgent <: agent # High-level agent wrapper
sessionId::Union{String, Nothing} # Optional session identifier sessionId::Union{String, Nothing} # Optional session identifier
maxRetryDelayMs::Union{Int64, Nothing} # Maximum delay between retries (ms) maxRetryDelayMs::Union{Int64, Nothing} # Maximum delay between retries (ms)
parallelToolExecute::Bool # Default: false parallelToolExecute::Bool # Default: false
agentEventSink::Function # agent emits its status via this function agentEventSink::Function # agent emits its status via this function
_tool_store::Any # Reference to the toolStore for runtime registration end
end
""" """
Create a new yiemAgent instance with a background loop task. Create a new yiemAgent instance with a background loop task.
@@ -82,17 +81,9 @@ on `inputChannel` and `followUpChannel` channels concurrently.
- `maxRetryDelayMs::Union{Int64, Nothing}`: Maximum delay between retries in milliseconds (default: `nothing`) - `maxRetryDelayMs::Union{Int64, Nothing}`: Maximum delay between retries in milliseconds (default: `nothing`)
- `parallelToolExecute::Bool`: Run tool calls in parallel (default: `false`) - `parallelToolExecute::Bool`: Run tool calls in parallel (default: `false`)
- `agentEventSink::Function`: Callback to receive agent events - `agentEventSink::Function`: Callback to receive agent events
- `tool_store::Union{Any, Nothing}`: toolStore for runtime tool registration (default: `nothing`)
# Returns # Returns
- A new `yiemAgent` instance with an active background task - A new `yiemAgent` instance with an active background task
# Examples
```julia
julia> store = toolStore(name="agent1")
julia> tools = loadTools(store, "src/tools")
julia> agent = yiemAgent(systemPrompt="You are a helpful assistant", model=my_model, tools=tools, llmCall=..., tool_store=store)
yiemAgent(agentState(...), Channel(...), Channel(...), Channel(...), ..., store)
""" """
function yiemAgent( function yiemAgent(
toolsFolderPath::String, toolsFolderPath::String,
@@ -104,14 +95,13 @@ function yiemAgent(
prepareContext::Function=prepareContext, prepareContext::Function=prepareContext,
formatMsgForLLM::Function=formatMsgForLLM, formatMsgForLLM::Function=formatMsgForLLM,
beforeToolCall::Function=beforeToolCall, beforeToolCall::Function=beforeToolCall,
afterToolCall::Function, #WORKING afterToolCall::Function=afterToolCall, #WORKING
# prepareNextTurn::Union{Function, Nothing}=nothing, # prepareNextTurn::Union{Function, Nothing}=nothing,
# prepareNextTurnWithContext::Union{Function, Nothing}=nothing, # prepareNextTurnWithContext::Union{Function, Nothing}=nothing,
sessionId::Union{String, Nothing}=nothing, sessionId::Union{String, Nothing}=nothing,
maxRetryDelayMs::Union{Int64, Nothing}=nothing, maxRetryDelayMs::Union{Int64, Nothing}=nothing,
parallelToolExecute::Bool=false, parallelToolExecute::Bool=false,
agentEventSink::Function, agentEventSink::Function,
tool_store::Union{Any, Nothing}=nothing,
) )
# Create channels: input (user -> agent), followUp (async queue), output (agent -> user) # Create channels: input (user -> agent), followUp (async queue), output (agent -> user)
inputChannel = Channel(16) inputChannel = Channel(16)
@@ -140,7 +130,6 @@ function yiemAgent(
maxRetryDelayMs, maxRetryDelayMs,
parallelToolExecute, parallelToolExecute,
agentEventSink, agentEventSink,
tool_store,
) )
# Spawn the background loop and attach it # Spawn the background loop and attach it
@@ -823,28 +812,28 @@ function finalizeExecutedToolCall(
signal::Union{Nothing,abortSignal}, signal::Union{Nothing,abortSignal},
)::finalizedOutcome )::finalizedOutcome
result = executed.result result = executed.result
isError = executed.isError isError = executed.isError
if config.afterToolCall !== nothing if config.afterToolCall !== nothing
try try
after = config.afterToolCall( after = config.afterToolCall(
afterToolCallContext(assistantMsg, prep.toolCall, prep.args, result, isError, context), signal afterToolCallContext(assistantMsg, prep.toolCall, prep.args, result, isError, context), signal
) )
if after !== nothing if after !== nothing
result = merge(result, dict(:content=>get(after,:content,result.content), result = merge(result, dict(:content=>get(after,:content,result.content),
:details=>get(after,:details,result.details), :details=>get(after,:details,result.details),
:usage=>get(after,:usage,result.usage), :usage=>get(after,:usage,result.usage),
:terminate=>get(after,:terminate,result.terminate))) :terminate=>get(after,:terminate,result.terminate)))
isError = get(after, :isError, isError) isError = get(after, :isError, isError)
end end
catch err catch err
result = createErrorToolResult(sprint(showerror, err)) result = createErrorToolResult(sprint(showerror, err))
isError = true isError = true
end
end end
end
return finalizedOutcome(prep.toolCall, result, isError) return finalizedOutcome(prep.toolCall, result, isError)
end end
""" """
+8 -8
View File
@@ -3,7 +3,7 @@ module utils
export clearhistory, availableWineToText, prepareContext, formatMsgForLLM, validateRequiredArgs, export clearhistory, availableWineToText, prepareContext, formatMsgForLLM, validateRequiredArgs,
validateToolArguments, _userMessageToOpenAI, validateToolArguments, _userMessageToOpenAI,
_assistantMessageToOpenAI, _toolResultMessageToOpenAI, _messageContentToBlocks, _assistantMessageToOpenAI, _toolResultMessageToOpenAI, _messageContentToBlocks,
beforeToolCall beforeToolCall, afterToolCall
using UUIDs, Dates, DataStructures, HTTP, JSON using UUIDs, Dates, DataStructures, HTTP, JSON
using GeneralUtils using GeneralUtils
@@ -221,7 +221,9 @@ function formatMsgForLLM(ctx::agentContext)::Dict{String, Any}
end end
#TODO #TODO
function beforeToolCall(context::beforeToolCallContext, signal::abortSignal)::beforeToolCallResult function beforeToolCall(context::beforeToolCallContext, signal::abortSignal
)::beforeToolCallResult
# final context check # final context check
# seek user approval via UI # seek user approval via UI
@@ -232,14 +234,12 @@ function beforeToolCall(context::beforeToolCallContext, signal::abortSignal)::be
end end
#TODO #TODO
function afterToolCall(context::beforeToolCallContext, signal::abortSignal)::beforeToolCallResult function afterToolCall(context::beforeToolCallContext, signal::abortSignal
# final context check )::Union{agentToolResult, Nothing}
# seek user approval via UI # modify context.result if needed and return agentToolResult
# other check return nothing
return beforeToolCallResult(false, "N/A")
end end