Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c2e2e5340 | |||
| 48d89033cc | |||
| befbf62e3b | |||
| 4e476bd39d | |||
| 66ced320f0 | |||
| 1817630795 | |||
| dbac0db9b9 | |||
| ce46888663 | |||
| 2a2d44ab0c | |||
| 0620983b48 |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
name = "YiemAgent"
|
name = "YiemAgent"
|
||||||
uuid = "e012c34b-7f78-48e0-971c-7abb83b6f0a2"
|
uuid = "e012c34b-7f78-48e0-971c-7abb83b6f0a2"
|
||||||
version = "0.9.0"
|
version = "0.9.1"
|
||||||
authors = ["narawat lamaiin <narawat@outlook.com>"]
|
authors = ["narawat lamaiin <narawat@outlook.com>"]
|
||||||
|
|
||||||
[deps]
|
[deps]
|
||||||
|
|||||||
+49
-7
@@ -170,7 +170,7 @@ function yiemAgent(
|
|||||||
|
|
||||||
tools = OrderedDict{String, agentTool}()
|
tools = OrderedDict{String, agentTool}()
|
||||||
registerAllTools(tools, mcpServer; eventSink=eventSink, sessionId=sessionId)
|
registerAllTools(tools, mcpServer; eventSink=eventSink, sessionId=sessionId)
|
||||||
|
eventSink("instantiate an agent")
|
||||||
# Create struct with a placeholder task, then spawn and replace it
|
# Create struct with a placeholder task, then spawn and replace it
|
||||||
agent = yiemAgent(
|
agent = yiemAgent(
|
||||||
agentState(systemPrompt, model, tools, messages),
|
agentState(systemPrompt, model, tools, messages),
|
||||||
@@ -194,7 +194,7 @@ function yiemAgent(
|
|||||||
|
|
||||||
# Spawn the background loop and attach it
|
# Spawn the background loop and attach it
|
||||||
agent._agentLoop = @spawn _agentLoop(agent)
|
agent._agentLoop = @spawn _agentLoop(agent)
|
||||||
|
eventSink("agent instantiated")
|
||||||
return agent
|
return agent
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -260,31 +260,36 @@ function _agentLoop(agent::yiemAgent)
|
|||||||
agent.inputChannel -> nothing
|
agent.inputChannel -> nothing
|
||||||
agent.followUpChannel -> nothing
|
agent.followUpChannel -> nothing
|
||||||
"""
|
"""
|
||||||
|
agent.eventSink("_agentLoop starting")
|
||||||
while true
|
while true
|
||||||
while newUserMsg === nothing
|
while newUserMsg === nothing
|
||||||
if isready(agent.inputChannel)
|
if isready(agent.inputChannel)
|
||||||
# agent process new user msg immediately after the current tool call finished.
|
# agent process new user msg immediately after the current tool call finished.
|
||||||
newUserMsg = take!(agent.inputChannel)
|
newUserMsg = take!(agent.inputChannel)
|
||||||
|
agent.eventSink("take new user message")
|
||||||
else
|
else
|
||||||
# check followUp message after _processMessage() is done
|
# check followUp message after _processMessage() is done
|
||||||
if typeof(processingTask) == Task && istaskdone(processingTask) == true
|
if typeof(processingTask) == Task && istaskdone(processingTask) == true
|
||||||
|
agent.eventSink("_processMessage() done")
|
||||||
# if agent runs is done but followUpChannel has messages,
|
# if agent runs is done but followUpChannel has messages,
|
||||||
# put new message in inputChannel instead
|
# put new message in inputChannel instead
|
||||||
if isready(agent.followUpChannel)
|
if isready(agent.followUpChannel)
|
||||||
while isready(agent.followUpChannel)
|
while isready(agent.followUpChannel)
|
||||||
|
agent.eventSink("put followUp message to input channel")
|
||||||
followUpMsg = take!(agent.followUpChannel)
|
followUpMsg = take!(agent.followUpChannel)
|
||||||
put!(agent.inputChannel, followUpMsg)
|
put!(agent.inputChannel, followUpMsg)
|
||||||
end
|
end
|
||||||
processingTask = nothing # reset
|
processingTask = nothing # reset
|
||||||
result = nothing # reset
|
result = nothing # reset
|
||||||
else # _processMessage() done and no followUp message.
|
else # _processMessage() done and no followUp message.
|
||||||
|
agent.eventSink("no followUp message")
|
||||||
result = deepcopy(agent._state.messages[end])
|
result = deepcopy(agent._state.messages[end])
|
||||||
# filter out reasoningContent in-place
|
# filter out reasoningContent in-place
|
||||||
filter!(c -> !(c isa reasoningContent), result.content)
|
filter!(c -> !(c isa reasoningContent), result.content)
|
||||||
# format output
|
# format output
|
||||||
respondToUI = _assistantMessageToOpenAI(result)
|
respondToUI = _assistantMessageToOpenAI(result)
|
||||||
put!(agent.outputChannel, respondToUI)
|
put!(agent.outputChannel, respondToUI)
|
||||||
|
agent.eventSink("put result to output channel")
|
||||||
processingTask = nothing # reset
|
processingTask = nothing # reset
|
||||||
result = nothing # reset
|
result = nothing # reset
|
||||||
end
|
end
|
||||||
@@ -314,6 +319,7 @@ function _agentLoop(agent::yiemAgent)
|
|||||||
else
|
else
|
||||||
# spawn new _processMessage() if it is not already running.
|
# spawn new _processMessage() if it is not already running.
|
||||||
if processingTask === nothing
|
if processingTask === nothing
|
||||||
|
agent.eventSink("spawn new _processMessage()")
|
||||||
# discard all messages in followUpChannel
|
# discard all messages in followUpChannel
|
||||||
while isready(agent.followUpChannel)
|
while isready(agent.followUpChannel)
|
||||||
_ = take!(agent.followUpChannel)
|
_ = take!(agent.followUpChannel)
|
||||||
@@ -335,8 +341,10 @@ function _agentLoop(agent::yiemAgent)
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
put!(processMessageInputCh, newUserMsg)
|
put!(processMessageInputCh, newUserMsg)
|
||||||
|
agent.eventSink("put new user msg to processMessageInputCh")
|
||||||
newUserMsg = nothing # reset
|
newUserMsg = nothing # reset
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
catch e
|
catch e
|
||||||
bt = catch_backtrace()
|
bt = catch_backtrace()
|
||||||
@@ -344,7 +352,7 @@ function _agentLoop(agent::yiemAgent)
|
|||||||
showerror(io, e, bt)
|
showerror(io, e, bt)
|
||||||
println(io)
|
println(io)
|
||||||
end
|
end
|
||||||
eventSink(errMsg; publish=true)
|
agent.eventSink(errMsg; publish=true)
|
||||||
|
|
||||||
# On any error, send error response and exit the loop
|
# On any error, send error response and exit the loop
|
||||||
@error "Agent loop failed" error=e
|
@error "Agent loop failed" error=e
|
||||||
@@ -395,6 +403,8 @@ function _processMessage(
|
|||||||
# loop until llmCall() response didn't use tool calls
|
# loop until llmCall() response didn't use tool calls
|
||||||
final_response = nothing
|
final_response = nothing
|
||||||
|
|
||||||
|
eventSink("▶ _processMessage: entered")
|
||||||
|
|
||||||
""" example message in inputChannel
|
""" example message in inputChannel
|
||||||
Dict(
|
Dict(
|
||||||
"role" => "user",
|
"role" => "user",
|
||||||
@@ -420,6 +430,7 @@ function _processMessage(
|
|||||||
end
|
end
|
||||||
newUserMsg = OpenAiToUserMessage(newUserMsg_openai)
|
newUserMsg = OpenAiToUserMessage(newUserMsg_openai)
|
||||||
push!(agentMsgHistory, newUserMsg)
|
push!(agentMsgHistory, newUserMsg)
|
||||||
|
eventSink("▶ _processMessage: user msg pushed")
|
||||||
end
|
end
|
||||||
# call prepareContext()
|
# call prepareContext()
|
||||||
state = agentState(systemPrompt, nothing, tools, agentMsgHistory)
|
state = agentState(systemPrompt, nothing, tools, agentMsgHistory)
|
||||||
@@ -427,6 +438,7 @@ function _processMessage(
|
|||||||
# Call formatMessagesForLLM() to format for LLM
|
# Call formatMessagesForLLM() to format for LLM
|
||||||
formattedMessages = formatMessagesForLLM(preparedContext, eventSink)
|
formattedMessages = formatMessagesForLLM(preparedContext, eventSink)
|
||||||
|
|
||||||
|
eventSink("▶ _processMessage: context")
|
||||||
eventSink("Thinking...")
|
eventSink("Thinking...")
|
||||||
""" response example
|
""" response example
|
||||||
response = JSON.Object{String, Any}("finish_reason" => "tool_calls", "index" => 0, "message" => JSON.Object{String, Any}("role" => "assistant", "content" => "", "reasoning_content" => "Here's a thinking process:\n\n1. **Identify User Request**: The user is asking for the weather in Bangkok.\n2. **Locate Relevant Tool**: I have a `getWeather` function available.\n3. **Check Function Parameters**:\n - `city` (required): City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\n - `units` (optional, default \"celsius\"): Temperature scale (\"celsius\" or \"fahrenheit\")\n4. **Prepare Parameters**:\n - `city`: \"Bangkok, Thailand\" (adding country for clarity, though just \"Bangkok\" might work, following the example format is safer)\n - `units`: Not specified, so I'll use the default (\"celsius\")\n5. **Execute Tool Call**: Call `getWeather` with `city: \"Bangkok, Thailand\"`\n6. **Anticipate Response**: The function will return current weather and forecast data for Bangkok. I'll then format it nicely for the user.\n - *Self-Correction/Verification during thought*: The prompt says \"city: City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\". I'll use \"Bangkok, Thailand\". The `units` parameter is optional, so I'll omit it to use the default.\n - Proceed with tool call.✅\n", "tool_calls" => Any[JSON.Object{String, Any}("type" => "function", "function" => JSON.Object{String, Any}("name" => "getWeather", "arguments" => "{\"city\":\"Bangkok, Thailand\"}"), "id" => "6fOilR5QPcdppbAAHluhkRyUDu3oWMAL")]))
|
response = JSON.Object{String, Any}("finish_reason" => "tool_calls", "index" => 0, "message" => JSON.Object{String, Any}("role" => "assistant", "content" => "", "reasoning_content" => "Here's a thinking process:\n\n1. **Identify User Request**: The user is asking for the weather in Bangkok.\n2. **Locate Relevant Tool**: I have a `getWeather` function available.\n3. **Check Function Parameters**:\n - `city` (required): City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\n - `units` (optional, default \"celsius\"): Temperature scale (\"celsius\" or \"fahrenheit\")\n4. **Prepare Parameters**:\n - `city`: \"Bangkok, Thailand\" (adding country for clarity, though just \"Bangkok\" might work, following the example format is safer)\n - `units`: Not specified, so I'll use the default (\"celsius\")\n5. **Execute Tool Call**: Call `getWeather` with `city: \"Bangkok, Thailand\"`\n6. **Anticipate Response**: The function will return current weather and forecast data for Bangkok. I'll then format it nicely for the user.\n - *Self-Correction/Verification during thought*: The prompt says \"city: City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\". I'll use \"Bangkok, Thailand\". The `units` parameter is optional, so I'll omit it to use the default.\n - Proceed with tool call.✅\n", "tool_calls" => Any[JSON.Object{String, Any}("type" => "function", "function" => JSON.Object{String, Any}("name" => "getWeather", "arguments" => "{\"city\":\"Bangkok, Thailand\"}"), "id" => "6fOilR5QPcdppbAAHluhkRyUDu3oWMAL")]))
|
||||||
@@ -434,12 +446,15 @@ function _processMessage(
|
|||||||
response = llmCall(formattedMessages, eventSink=eventSink)
|
response = llmCall(formattedMessages, eventSink=eventSink)
|
||||||
|
|
||||||
# Extract tool calls from LLM response content blocks
|
# Extract tool calls from LLM response content blocks
|
||||||
hasToolCalls, toolCallList, assistant_msg = _extractToolCalls(response)
|
hasToolCalls, toolCallList, assistant_msg = _extractToolCalls(response, eventSink)
|
||||||
|
|
||||||
# Add assistant message (tool calls or text) to history for next LLM turn
|
# Add assistant message (tool calls or text) to history for next LLM turn
|
||||||
push!(agentMsgHistory, assistant_msg)
|
push!(agentMsgHistory, assistant_msg)
|
||||||
|
eventSink("▶ _processMessage: LLM response")
|
||||||
|
|
||||||
if hasToolCalls && length(toolCallList) > 0
|
if hasToolCalls && length(toolCallList) > 0
|
||||||
|
eventSink("▶ _processMessage: tool calls")
|
||||||
|
|
||||||
# Build context and config for executeToolCalls
|
# Build context and config for executeToolCalls
|
||||||
|
|
||||||
config = agentLoopConfig(
|
config = agentLoopConfig(
|
||||||
@@ -452,11 +467,13 @@ function _processMessage(
|
|||||||
# call executeToolCalls()
|
# call executeToolCalls()
|
||||||
toolResultBatch = executeToolCalls(preparedContext, assistant_msg, toolCallList, config,
|
toolResultBatch = executeToolCalls(preparedContext, assistant_msg, toolCallList, config,
|
||||||
signal, eventSink)
|
signal, eventSink)
|
||||||
|
eventSink("▶ _processMessage: tool batch")
|
||||||
# save toolResults to messages
|
# save toolResults to messages
|
||||||
for toolResult in toolResultBatch.messages
|
for toolResult in toolResultBatch.messages
|
||||||
push!(agentMsgHistory, toolResult)
|
push!(agentMsgHistory, toolResult)
|
||||||
end
|
end
|
||||||
if toolResultBatch.terminate
|
if toolResultBatch.terminate
|
||||||
|
eventSink("▶ _processMessage: TERMINATE branch")
|
||||||
# If toolResultBatch requested termination, build a final response
|
# If toolResultBatch requested termination, build a final response
|
||||||
final_content = [textContent("Tool execution completed.")]
|
final_content = [textContent("Tool execution completed.")]
|
||||||
for toolResult in toolResultBatch.messages
|
for toolResult in toolResultBatch.messages
|
||||||
@@ -488,6 +505,7 @@ function _processMessage(
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
eventSink("▶ _processMessage: FINAL response")
|
||||||
# LLM did not use tool calls —
|
# LLM did not use tool calls —
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@@ -499,9 +517,11 @@ function _processMessage(
|
|||||||
end
|
end
|
||||||
|
|
||||||
eventSink(err_msg; publish=true)
|
eventSink(err_msg; publish=true)
|
||||||
|
eventSink("▶ _processMessage: CAUGHT ERROR")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
eventSink("▶ _processMessage: exited")
|
||||||
return nothing
|
return nothing
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -611,9 +631,12 @@ The message is constructed from:
|
|||||||
1) llm_useTool = JSON.Object{String, Any}("finish_reason" => "tool_calls", "index" => 0, "message" => JSON.Object{String, Any}("role" => "assistant", "content" => "", "reasoning_content" => "Here's a thinking process:\n\n1. **Identify User Request**: The user is asking for the weather in Bangkok.\n2. **Locate Relevant Tool**: I have a `getWeather` function available.\n3. **Check Function Parameters**:\n - `city` (required): City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\n - `units` (optional, default \"celsius\"): Temperature scale (\"celsius\" or \"fahrenheit\")\n4. **Prepare Parameters**:\n - `city`: \"Bangkok, Thailand\" (adding country for clarity, though just \"Bangkok\" might work, following the example format is safer)\n - `units`: Not specified, so I'll use the default (\"celsius\")\n5. **Execute Tool Call**: Call `getWeather` with `city: \"Bangkok, Thailand\"`\n6. **Anticipate Response**: The function will return current weather and forecast data for Bangkok. I'll then format it nicely for the user.\n - *Self-Correction/Verification during thought*: The prompt says \"city: City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\". I'll use \"Bangkok, Thailand\". The `units` parameter is optional, so I'll omit it to use the default.\n - Proceed with tool call.✅\n", "tool_calls" => Any[JSON.Object{String, Any}("type" => "function", "function" => JSON.Object{String, Any}("name" => "getWeather", "arguments" => "{\"city\":\"Bangkok, Thailand\"}"), "id" => "6fOilR5QPcdppbAAHluhkRyUDu3oWMAL")]))
|
1) llm_useTool = JSON.Object{String, Any}("finish_reason" => "tool_calls", "index" => 0, "message" => JSON.Object{String, Any}("role" => "assistant", "content" => "", "reasoning_content" => "Here's a thinking process:\n\n1. **Identify User Request**: The user is asking for the weather in Bangkok.\n2. **Locate Relevant Tool**: I have a `getWeather` function available.\n3. **Check Function Parameters**:\n - `city` (required): City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\n - `units` (optional, default \"celsius\"): Temperature scale (\"celsius\" or \"fahrenheit\")\n4. **Prepare Parameters**:\n - `city`: \"Bangkok, Thailand\" (adding country for clarity, though just \"Bangkok\" might work, following the example format is safer)\n - `units`: Not specified, so I'll use the default (\"celsius\")\n5. **Execute Tool Call**: Call `getWeather` with `city: \"Bangkok, Thailand\"`\n6. **Anticipate Response**: The function will return current weather and forecast data for Bangkok. I'll then format it nicely for the user.\n - *Self-Correction/Verification during thought*: The prompt says \"city: City and country, e.g., 'San Francisco, CA' or 'Tokyo, Japan'\". I'll use \"Bangkok, Thailand\". The `units` parameter is optional, so I'll omit it to use the default.\n - Proceed with tool call.✅\n", "tool_calls" => Any[JSON.Object{String, Any}("type" => "function", "function" => JSON.Object{String, Any}("name" => "getWeather", "arguments" => "{\"city\":\"Bangkok, Thailand\"}"), "id" => "6fOilR5QPcdppbAAHluhkRyUDu3oWMAL")]))
|
||||||
2) llm_notUseTool = JSON.Object{String, Any}("finish_reason" => "stop", "index" => 0, "message" => JSON.Object{String, Any}("role" => "assistant", "content" => "The weather in London, UK is currently Sunny with a temperature of 22°C.", "reasoning_content" => "The user asked for the weather in London.\nI called the `getWeather` tool for London, UK.\nThe response indicates it's Sunny and 22°C.\nI will convey this information to the user.\n"))
|
2) llm_notUseTool = JSON.Object{String, Any}("finish_reason" => "stop", "index" => 0, "message" => JSON.Object{String, Any}("role" => "assistant", "content" => "The weather in London, UK is currently Sunny with a temperature of 22°C.", "reasoning_content" => "The user asked for the weather in London.\nI called the `getWeather` tool for London, UK.\nThe response indicates it's Sunny and 22°C.\nI will convey this information to the user.\n"))
|
||||||
"""
|
"""
|
||||||
function _extractToolCalls(response)
|
function _extractToolCalls(response, eventSink=nothing)
|
||||||
hasToolCalls = false
|
hasToolCalls = false
|
||||||
toolCallList = agentToolCall[]
|
toolCallList = agentToolCall[]
|
||||||
|
if eventSink !== nothing
|
||||||
|
eventSink("▶ _extractToolCalls: parsing response")
|
||||||
|
end
|
||||||
|
|
||||||
# Helper: parse args (JSON string -> Dict, or pass through)
|
# Helper: parse args (JSON string -> Dict, or pass through)
|
||||||
parse_args(raw) = raw isa AbstractDict && !(raw isa Dict{String,Any}) ?
|
parse_args(raw) = raw isa AbstractDict && !(raw isa Dict{String,Any}) ?
|
||||||
@@ -943,9 +966,11 @@ function prepareToolCall(
|
|||||||
)::Union{preparedToolCall,immediateOutcome}
|
)::Union{preparedToolCall,immediateOutcome}
|
||||||
|
|
||||||
# pick a called tool from tool store
|
# pick a called tool from tool store
|
||||||
|
eventSink("▶ prepareToolCall: resolving")
|
||||||
tool = get(context.tools, toolCall.name, nothing)
|
tool = get(context.tools, toolCall.name, nothing)
|
||||||
|
|
||||||
if tool === nothing
|
if tool === nothing
|
||||||
|
eventSink("▶ prepareToolCall: NOT FOUND")
|
||||||
return immediateOutcome(createErrorToolResult("Tool $toolCall.name not found"), true)
|
return immediateOutcome(createErrorToolResult("Tool $toolCall.name not found"), true)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -953,6 +978,7 @@ function prepareToolCall(
|
|||||||
# 1. prepare arguments (tool-specific transform)
|
# 1. prepare arguments (tool-specific transform)
|
||||||
prepared = prepareToolCallArguments(tool, toolCall)
|
prepared = prepareToolCallArguments(tool, toolCall)
|
||||||
validatedArgs = validateToolArguments(tool, prepared)
|
validatedArgs = validateToolArguments(tool, prepared)
|
||||||
|
eventSink("▶ prepareToolCall: validated")
|
||||||
# 2. beforeToolCall hook — can block
|
# 2. beforeToolCall hook — can block
|
||||||
if config.beforeToolCall !== nothing
|
if config.beforeToolCall !== nothing
|
||||||
|
|
||||||
@@ -1032,8 +1058,10 @@ function executePreparedToolCall(
|
|||||||
eventSink,
|
eventSink,
|
||||||
)::executedOutcome
|
)::executedOutcome
|
||||||
|
|
||||||
|
eventSink("▶ execute: START")
|
||||||
try
|
try
|
||||||
result = prep.tool.execute(prep.toolCall.id, prep.args, signal, eventSink)
|
result = prep.tool.execute(prep.toolCall.id, prep.args, signal, eventSink)
|
||||||
|
eventSink("▶ execute: END")
|
||||||
return executedOutcome(result, false)
|
return executedOutcome(result, false)
|
||||||
catch e
|
catch e
|
||||||
bt = catch_backtrace()
|
bt = catch_backtrace()
|
||||||
@@ -1042,6 +1070,7 @@ function executePreparedToolCall(
|
|||||||
println(io)
|
println(io)
|
||||||
end
|
end
|
||||||
eventSink(errMsg; publish=true)
|
eventSink(errMsg; publish=true)
|
||||||
|
eventSink("▶ execute: ERROR")
|
||||||
|
|
||||||
return executedOutcome(createErrorToolResult(sprint(showerror, e)), true)
|
return executedOutcome(createErrorToolResult(sprint(showerror, e)), true)
|
||||||
end
|
end
|
||||||
@@ -1115,6 +1144,7 @@ function finalizeExecutedToolCall(
|
|||||||
eventSink
|
eventSink
|
||||||
)::finalizedOutcome
|
)::finalizedOutcome
|
||||||
|
|
||||||
|
eventSink("▶ finalize: START")
|
||||||
result = executed.result
|
result = executed.result
|
||||||
isError = executed.isError
|
isError = executed.isError
|
||||||
if config.afterToolCall !== nothing
|
if config.afterToolCall !== nothing
|
||||||
@@ -1140,6 +1170,7 @@ function finalizeExecutedToolCall(
|
|||||||
isError = true
|
isError = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
eventSink("▶ finalize: END")
|
||||||
return finalizedOutcome(prep.toolCall, result, isError)
|
return finalizedOutcome(prep.toolCall, result, isError)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1207,6 +1238,7 @@ function executeToolCallsSequential(
|
|||||||
)::agentToolCallBatch
|
)::agentToolCallBatch
|
||||||
finalizedCalls = finalizedOutcome[]
|
finalizedCalls = finalizedOutcome[]
|
||||||
messages = toolResultMessage[]
|
messages = toolResultMessage[]
|
||||||
|
eventSink("▶ executeToolCallsSequential: batch start")
|
||||||
|
|
||||||
for tc in toolCalls
|
for tc in toolCalls
|
||||||
prep = prepareToolCall(context, assistantMsg, tc, config, signal, eventSink)
|
prep = prepareToolCall(context, assistantMsg, tc, config, signal, eventSink)
|
||||||
@@ -1224,6 +1256,7 @@ function executeToolCallsSequential(
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
eventSink("▶ executeToolCallsSequential: batch end")
|
||||||
return agentToolCallBatch(messages, shouldTerminate(finalizedCalls))
|
return agentToolCallBatch(messages, shouldTerminate(finalizedCalls))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1294,6 +1327,7 @@ function executeToolCallsParallel(
|
|||||||
)::agentToolCallBatch
|
)::agentToolCallBatch
|
||||||
|
|
||||||
entries = Union{finalizedOutcome,Task}[]
|
entries = Union{finalizedOutcome,Task}[]
|
||||||
|
eventSink("▶ executeToolCallsParallel: batch start")
|
||||||
|
|
||||||
for tc in toolCalls
|
for tc in toolCalls
|
||||||
toolExecStartEvent(tc.id, tc.name, tc.arguments)
|
toolExecStartEvent(tc.id, tc.name, tc.arguments)
|
||||||
@@ -1333,6 +1367,7 @@ function executeToolCallsParallel(
|
|||||||
push!(messages, createToolResultMessage(f))
|
push!(messages, createToolResultMessage(f))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
eventSink("▶ executeToolCallsParallel: batch end")
|
||||||
return agentToolCallBatch(messages, shouldTerminate(finalizedCalls))
|
return agentToolCallBatch(messages, shouldTerminate(finalizedCalls))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1403,6 +1438,7 @@ function executeToolCalls(
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
eventSink("▶ executeToolCalls: dispatch")
|
||||||
if config.toolExecution == "sequential" || hasSequential
|
if config.toolExecution == "sequential" || hasSequential
|
||||||
return executeToolCallsSequential(context, assistantMsg, toolCalls, config, signal,
|
return executeToolCallsSequential(context, assistantMsg, toolCalls, config, signal,
|
||||||
eventSink)
|
eventSink)
|
||||||
@@ -1447,8 +1483,11 @@ OpenAiToUserMessage(msg)
|
|||||||
# => userMessage("user", [textContent("Hello")], DateTime(...))
|
# => userMessage("user", [textContent("Hello")], DateTime(...))
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
function OpenAiToUserMessage(msg::AbstractDict)::userMessage
|
function OpenAiToUserMessage(msg::AbstractDict, eventSink=nothing)::userMessage
|
||||||
content_blocks = Vector{messageContent}()
|
content_blocks = Vector{messageContent}()
|
||||||
|
if eventSink !== nothing
|
||||||
|
eventSink("▶ OpenAiToUserMessage: parsing")
|
||||||
|
end
|
||||||
|
|
||||||
raw_content = get(msg, "content", Any[])
|
raw_content = get(msg, "content", Any[])
|
||||||
if raw_content isa Vector
|
if raw_content isa Vector
|
||||||
@@ -1472,6 +1511,9 @@ function OpenAiToUserMessage(msg::AbstractDict)::userMessage
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if eventSink !== nothing
|
||||||
|
eventSink("▶ OpenAiToUserMessage: done")
|
||||||
|
end
|
||||||
return userMessage(content=content_blocks)
|
return userMessage(content=content_blocks)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -198,7 +198,7 @@ Add `tool` to `tools`, overwriting any existing tool with the same name.
|
|||||||
function registerTool(tools::OrderedDict{String, agentTool}, tool::agentTool; eventSink=nothing
|
function registerTool(tools::OrderedDict{String, agentTool}, tool::agentTool; eventSink=nothing
|
||||||
)::OrderedDict{String, agentTool}
|
)::OrderedDict{String, agentTool}
|
||||||
tools[tool.name] = tool
|
tools[tool.name] = tool
|
||||||
println("[toolRegistry] Registered tool: $(tool.name)")
|
eventSink("[toolRegistry] Registered tool: $(tool.name)")
|
||||||
return tools
|
return tools
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -282,7 +282,9 @@ Register all tools for an agent:
|
|||||||
"""
|
"""
|
||||||
function registerAllTools(tools::OrderedDict{String, agentTool}, mcpserver=nothing; eventSink=nothing, sessionId=nothing)
|
function registerAllTools(tools::OrderedDict{String, agentTool}, mcpserver=nothing; eventSink=nothing, sessionId=nothing)
|
||||||
list_t = listTool(tools, mcpserver; eventSink=eventSink, sessionId=sessionId)
|
list_t = listTool(tools, mcpserver; eventSink=eventSink, sessionId=sessionId)
|
||||||
|
eventSink("registering tools")
|
||||||
registerTool(tools, list_t; eventSink=eventSink)
|
registerTool(tools, list_t; eventSink=eventSink)
|
||||||
|
eventSink("registering tools done")
|
||||||
end
|
end
|
||||||
|
|
||||||
end # module
|
end # module
|
||||||
|
|||||||
Reference in New Issue
Block a user