This commit is contained in:
2026-08-07 13:28:09 +07:00
parent 2de4fa07c1
commit 14bac755ea
2 changed files with 74 additions and 45 deletions
+74 -5
View File
@@ -1,6 +1,6 @@
module agentCore module agentCore
export _agent_loop export _agent_loop, OpenAiToUserMessage
using JSON, DataStructures, Dates, UUIDs, HTTP, Random, PrettyPrinting, Serialization, using JSON, DataStructures, Dates, UUIDs, HTTP, Random, PrettyPrinting, Serialization,
DataFrames, Serde 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" => "text", "text" => "Do you have something similar to the one in the image?"),
Dict( Dict(
"type" => "image_url", "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 # Drain inputChannel and convert OpenAI-format messages to userMessage type
# userMessage type and push to agent._state.messages. 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() # 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:<mime>;base64,<data>`) 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
end # end of module
-40
View File
@@ -139,51 +139,11 @@ struct textContent <: messageContent # Plain text message content
text::String # The text content text::String # The text content
end 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 struct imageContent <: messageContent # Image message content
data::String # Base64-encoded image data data::String # Base64-encoded image data
mimeType::String # MIME type (e.g., "image/png") mimeType::String # MIME type (e.g., "image/png")
end 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 # # Tool types #
# ------------------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------------------ #