From 14bac755ea87596ca1e486f93e60b0abde415a9b Mon Sep 17 00:00:00 2001 From: narawat Date: Fri, 7 Aug 2026 13:28:09 +0700 Subject: [PATCH] update --- src/agentCore.jl | 79 +++++++++++++++++++++++++++++++++++++++++++++--- src/type.jl | 40 ------------------------ 2 files changed, 74 insertions(+), 45 deletions(-) diff --git a/src/agentCore.jl b/src/agentCore.jl index 21c7dfa..4de1215 100644 --- a/src/agentCore.jl +++ b/src/agentCore.jl @@ -1,6 +1,6 @@ module agentCore -export _agent_loop +export _agent_loop, OpenAiToUserMessage using JSON, DataStructures, Dates, UUIDs, HTTP, Random, PrettyPrinting, Serialization, DataFrames, Serde @@ -184,14 +184,23 @@ function _process_message(agent::yiemAgent)::assistantMessage Dict("type" => "text", "text" => "Do you have something similar to the one in the image?"), Dict( "type" => "image_url", - "image_url" => Dict("url" => data_uri) + "image_url" => Dict("url" => "data:mime_type;base64,image2_base64_string") ) ] ), """ - # check agent.inputChannel if there are, use OpenAiToUserMessage() to convert them into - # userMessage type and push to agent._state.messages. + # Drain inputChannel and convert OpenAI-format messages to userMessage type + while isready(agent.inputChannel) + raw_msg = take!(agent.inputChannel) + if raw_msg === :shutdown + # Re-emit shutdown signal for the loop to handle + put!(agent.inputChannel, :shutdown) + break + end + user_msg = OpenAiToUserMessage(raw_msg) + push!(agent._state.messages, user_msg) + end # call agent.prepareContext() @@ -1008,14 +1017,66 @@ end +""" + OpenAiToUserMessage(msg::Dict) -> userMessage +Converts an OpenAI-format message dictionary into a `userMessage` type. +Parses `content` blocks: `text` blocks become `textContent`, `image_url` blocks +have their data URI (`data:;base64,`) parsed via regex to extract the +base64 data and MIME type as separate `imageContent` fields. +The OpenAI-format dictionary: +``` +Dict( + "role" => "user", + "content" => [ + Dict("type" => "text", "text" => "..."), + Dict("type" => "image_url", "image_url" => Dict("url" => "data:image/png;base64,...")) + ] +) +``` +# Arguments +- `msg`: A dictionary with `"role"` and `"content"` keys in OpenAI format +# Returns +- `userMessage`: Instance with `content` as `Vector{messageContent}` +# Examples +```julia +msg = Dict("role" => "user", "content" => [Dict("type" => "text", "text" => "Hello")]) +OpenAiToUserMessage(msg) +# => userMessage("user", [textContent("Hello")], DateTime(...)) +``` +""" +function OpenAiToUserMessage(msg::Dict)::userMessage + content_blocks = Vector{messageContent}() + raw_content = get(msg, "content", Any[]) + if raw_content isa Vector + for block in raw_content + if block isa Dict + block_type = get(block, "type", "") + if block_type == "text" + text = get(block, "text", "") + push!(content_blocks, textContent(text)) + elseif block_type == "image_url" + image_url = get(block, "image_url", Dict()) + url = get(image_url, "url", "") + m = match(r"^data:([a-z0-9/_-]+);base64,(.+)$", url) + if m !== nothing + push!(content_blocks, imageContent(m.captures[2], m.captures[1])) + else + push!(content_blocks, imageContent(url, "image/png")) + end + end + end + end + end + return userMessage(content=content_blocks) +end @@ -1087,4 +1148,12 @@ end -end # end of module \ No newline at end of file + + + + + + + + +end # end of module diff --git a/src/type.jl b/src/type.jl index 264cdd7..f573e67 100644 --- a/src/type.jl +++ b/src/type.jl @@ -139,51 +139,11 @@ struct textContent <: messageContent # Plain text message content text::String # The text content end -""" -Create plain text message content. - -# Arguments -- `text::String`: The text content - -# Returns -- A new `textContent` instance - -# Examples -```julia -julia> content = textContent("Hello, world!") -textContent("Hello, world!") -``` -""" -function textContent(; text="") - return textContent(text) -end - struct imageContent <: messageContent # Image message content data::String # Base64-encoded image data mimeType::String # MIME type (e.g., "image/png") end -""" -Create image message content. - -# Arguments -- `data::String`: Base64-encoded image data -- `mimeType::String`: MIME type (e.g., "image/png") - -# Returns -- A new `imageContent` instance - -# Examples -```julia -julia> img = imageContent(data="base64data...", mimeType="image/png") -imageContent("base64data...", "image/png") -``` -""" -function imageContent(; data="", mimeType="") - return imageContent(data, mimeType) -end - - # ------------------------------------------------------------------------------------------------ # # Tool types # # ------------------------------------------------------------------------------------------------ #