diff --git a/src/type.jl b/src/type.jl index aaa6c76..315b6ce 100644 --- a/src/type.jl +++ b/src/type.jl @@ -86,44 +86,6 @@ struct agentContext # Snapshot of the agent's conversa 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 # ============================================================================ @@ -162,7 +124,6 @@ struct toolCall # A tool invocation from the LLM id::String # Unique tool call identifier name::String # Tool name arguments::Dict{String, Any} # Parsed tool arguments - partialJson::Union{String, Nothing} # Raw JSON string during streaming end @@ -207,23 +168,28 @@ end mutable struct yiemAgent # High-level agent wrapper _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 - preprocessMessages ::Union{Function, Nothing} # Preprocess/transform messages before sending - 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 + preprocessMessages ::Union{Function, Nothing} # Preprocess/transform messages before sending to LLM beforeToolCall::Union{Function, Nothing} # Callback invoked before 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 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 - 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) toolExecution::toolExecutionMode # Default: run tool calls sequentially or in parallel end @@ -232,44 +198,30 @@ end function yiemAgent( ; systemPrompt::String="", 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[], messages::Vector{agentMessage}=agentMessage[], formatMsgForLLM::Function=defaultformatMsgForLLM, 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, afterToolCall::Union{Function, Nothing}=nothing, prepareNextTurn::Union{Function, Nothing}=nothing, prepareNextTurnWithContext::Union{Function, Nothing}=nothing, sessionId::Union{String, Nothing}=nothing, - thinkingBudgets::Union{Dict{String, Int64}, Nothing}=nothing, - transport::String="auto", maxRetryDelayMs::Union{Int64, Nothing}=nothing, toolExecution::toolExecutionMode=EXECUTION_PARALLEL, ) new( agentState(systemPrompt, model, tools, messages), - Set{Tuple{Function, Ref{Bool}}}(), - pendingMessageQueue(QUEUE_ONE_AT_A_TIME), - pendingMessageQueue(QUEUE_ONE_AT_A_TIME), + Channel(16), formatMsgForLLM, - preprocessMessages , - streamFunction, - getApiKey, + preprocessMessages, onPayload, onResponse, beforeToolCall, afterToolCall, prepareNextTurn, prepareNextTurnWithContext, - nothing, sessionId, - thinkingBudgets, - transport, maxRetryDelayMs, toolExecution, )