This commit is contained in:
2026-08-01 23:01:52 +07:00
parent 94dd44236d
commit 1cb9ca106b
+17 -65
View File
@@ -86,44 +86,6 @@ struct agentContext # Snapshot of the agent's conversa
end end
# ============================================================================
# Assistant message event types
# ============================================================================
abstract type assistantMessageEvent end # Base type for assistant message streaming events
struct startEvent <: assistantMessageEvent # Message generation started
partial::assistantMessage # The partial message at this point
end
struct textStartEvent <: assistantMessageEvent # Text content block started
contentIndex::Int64 # Index of the content block
partial::assistantMessage # The partial message at this point
end
struct textDeltaEvent <: assistantMessageEvent # Text content block received a chunk
contentIndex::Int64 # Index of the content block
delta::String # New text chunk
partial::assistantMessage # The partial message at this point
end
struct textEndEvent <: assistantMessageEvent # Text content block completed
contentIndex::Int64 # Index of the content block
content::String # Complete text content
partial::assistantMessage # The partial message at this point
end
struct doneEvent <: assistantMessageEvent # Message generation completed successfully
reason::String # Why generation stopped
usage::Usage # Token usage
message::assistantMessage # The completed message
end
struct errorEvent <: assistantMessageEvent # Message generation encountered an error
reason::String # Error reason
errorMessage::Union{String, Nothing} # Human-readable error
usage::Usage # Token usage (partial)
error::assistantMessage # The error message
end
# ============================================================================ # ============================================================================
# Agent state # Agent state
# ============================================================================ # ============================================================================
@@ -162,7 +124,6 @@ struct toolCall # A tool invocation from the LLM
id::String # Unique tool call identifier id::String # Unique tool call identifier
name::String # Tool name name::String # Tool name
arguments::Dict{String, Any} # Parsed tool arguments arguments::Dict{String, Any} # Parsed tool arguments
partialJson::Union{String, Nothing} # Raw JSON string during streaming
end end
@@ -207,23 +168,28 @@ end
mutable struct yiemAgent # High-level agent wrapper mutable struct yiemAgent # High-level agent wrapper
_state::agentState # Current state (prompt, model, messages, tools, etc.) _state::agentState # Current state (prompt, model, messages, tools, etc.)
conn::NATS.Connection # NATS connection for messaging
followUpQueue::pendingMessageQueue # Messages queued via followUp() when agent would stop input_ch::Channel # user sends prompt message to agent.
# if agent is idle, it process user message right away.
# if agent is running, it process user message after
# the current tool call finished.
followUpQueue::Channel # Messages queued via followUp() during agent is
# running. After the agent loop process all input_ch
# and the agent isn't use tool call. it then process
# followUp message
output_ch::Channel # agent respond message to user after it process all
# user message in input_ch and all followUp message.
formatMsgForLLM::Function # Convert agent messages to LLM message format formatMsgForLLM::Function # Convert agent messages to LLM message format
preprocessMessages ::Union{Function, Nothing} # Preprocess/transform messages before sending preprocessMessages ::Union{Function, Nothing} # Preprocess/transform messages before sending to LLM
streamFunction::streamFn # Stream function for streaming responses
getApiKey::Union{Function, Nothing} # Callback to retrieve API key
onPayload::Union{Function, Nothing} # Callback when a payload is sent to the API
onResponse::Union{Function, Nothing} # Callback when a full response is received
beforeToolCall::Union{Function, Nothing} # Callback invoked before executing a tool call beforeToolCall::Union{Function, Nothing} # Callback invoked before executing a tool call
afterToolCall::Union{Function, Nothing} # Callback invoked after executing a tool call afterToolCall::Union{Function, Nothing} # Callback invoked after executing a tool call
prepareNextTurn::Union{Function, Nothing} # Callback to prepare the next conversation turn prepareNextTurn::Union{Function, Nothing} # Callback to prepare the next conversation turn
prepareNextTurnWithContext::Union{Function, Nothing} # Same but receives context prepareNextTurnWithContext::Union{Function, Nothing} # Same but receives context
activeRun::Union{activeRun, Nothing} # Active run state (promise, abort controller) activeRun::Union{Bool, Nothing} # tracks the currently executing agent run state
sessionId::Union{String, Nothing} # Optional session identifier sessionId::Union{String, Nothing} # Optional session identifier
thinkingBudgets::Union{Dict{String, Int64}, Nothing} # Per-model thinking token budgets
transport::String # Transport mode ("auto" or explicit)
maxRetryDelayMs::Union{Int64, Nothing} # Maximum delay between retries (ms) maxRetryDelayMs::Union{Int64, Nothing} # Maximum delay between retries (ms)
toolExecution::toolExecutionMode # Default: run tool calls sequentially or in parallel toolExecution::toolExecutionMode # Default: run tool calls sequentially or in parallel
end end
@@ -232,44 +198,30 @@ end
function yiemAgent( function yiemAgent(
; systemPrompt::String="", ; systemPrompt::String="",
model::llmModel=llmModel{String}("", "", "unknown", "unknown", "", false, String[], modelCost(0.0, 0.0, 0.0, 0.0), 0, 0), model::llmModel=llmModel{String}("", "", "unknown", "unknown", "", false, String[], modelCost(0.0, 0.0, 0.0, 0.0), 0, 0),
thinkingLevel::thinkingLevel=THINKING_OFF,
tools::Vector{agentTool}=agentTool[], tools::Vector{agentTool}=agentTool[],
messages::Vector{agentMessage}=agentMessage[], messages::Vector{agentMessage}=agentMessage[],
formatMsgForLLM::Function=defaultformatMsgForLLM, formatMsgForLLM::Function=defaultformatMsgForLLM,
preprocessMessages ::Union{Function, Nothing}=nothing, preprocessMessages ::Union{Function, Nothing}=nothing,
streamFunction::streamFn=getDefaultStreamFn(),
getApiKey::Union{Function, Nothing}=nothing,
onPayload::Union{Function, Nothing}=nothing,
onResponse::Union{Function, Nothing}=nothing,
beforeToolCall::Union{Function, Nothing}=nothing, beforeToolCall::Union{Function, Nothing}=nothing,
afterToolCall::Union{Function, Nothing}=nothing, afterToolCall::Union{Function, Nothing}=nothing,
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,
thinkingBudgets::Union{Dict{String, Int64}, Nothing}=nothing,
transport::String="auto",
maxRetryDelayMs::Union{Int64, Nothing}=nothing, maxRetryDelayMs::Union{Int64, Nothing}=nothing,
toolExecution::toolExecutionMode=EXECUTION_PARALLEL, toolExecution::toolExecutionMode=EXECUTION_PARALLEL,
) )
new( new(
agentState(systemPrompt, model, tools, messages), agentState(systemPrompt, model, tools, messages),
Set{Tuple{Function, Ref{Bool}}}(), Channel(16),
pendingMessageQueue(QUEUE_ONE_AT_A_TIME),
pendingMessageQueue(QUEUE_ONE_AT_A_TIME),
formatMsgForLLM, formatMsgForLLM,
preprocessMessages , preprocessMessages,
streamFunction,
getApiKey,
onPayload, onPayload,
onResponse, onResponse,
beforeToolCall, beforeToolCall,
afterToolCall, afterToolCall,
prepareNextTurn, prepareNextTurn,
prepareNextTurnWithContext, prepareNextTurnWithContext,
nothing,
sessionId, sessionId,
thinkingBudgets,
transport,
maxRetryDelayMs, maxRetryDelayMs,
toolExecution, toolExecution,
) )