This commit is contained in:
2026-05-13 16:57:20 +07:00
parent 34a6d19303
commit c25c6a8a43
2 changed files with 74 additions and 60 deletions

View File

@@ -143,7 +143,7 @@ Since I develop src folder before I adopt SDD_FRAMEWORK.md approach, can you che
# ---------------------------------------------- 100 --------------------------------------------- # # ---------------------------------------------- 100 --------------------------------------------- #
Check NATSBridge/docs folder I want to update the content of the following files according to ASG_Framework/ASG_Framework.md: I updated src/NATSBridge.jl. Check and NATSBridge/docs folder I want to update the content of the following files according to ASG_Framework/ASG_Framework.md:
- NATSBridge/docs/requirements.md - NATSBridge/docs/requirements.md
- NATSBridge/docs/specification.md - NATSBridge/docs/specification.md
- NATSBridge/docs/ui-specification.md (you'll need to create this one) - NATSBridge/docs/ui-specification.md (you'll need to create this one)
@@ -170,3 +170,15 @@ Can you update the content of the following files according to ASG_Framework/ASG
<!-- ------------------------------------------- 100 ------------------------------------------- -->
I updated ./src/NATSBridge.jl. Specifically smartsend() and smartreceive(). Check ./docs folder I want to update the content of the following files according to /home/ton/docker-apps/sommpanion/ASG_Framework/ASG_Framework.md:
- ./docs/requirements.md
- ./docs/specification.md
- ./docs/walkthrough.md
- ./docs/architecture.md

View File

@@ -43,7 +43,7 @@
module NATSBridge module NATSBridge
using NATS, JSON, Arrow, HTTP, UUIDs, Dates, Base64, PrettyPrinting, DataFrames using JSON, Arrow, HTTP, UUIDs, Dates, Base64, PrettyPrinting, DataFrames
# ---------------------------------------------- 100 --------------------------------------------- # # ---------------------------------------------- 100 --------------------------------------------- #
# Constants # Constants
@@ -346,13 +346,17 @@ Otherwise, it uploads the data to a fileserver (by default using `plik_oneshot_u
The function accepts a list of (dataname, data, type) tuples as input and processes each payload individually. The function accepts a list of (dataname, data, type) tuples as input and processes each payload individually.
Each payload can have a different type, enabling mixed-content messages (e.g., chat with text, images, audio). Each payload can have a different type, enabling mixed-content messages (e.g., chat with text, images, audio).
This function creates and returns the msg_envelope_v1 and its JSON string representation only.
NATS publishing must be performed by the caller.
# Function Workflow: # Function Workflow:
1. Iterates through the list of (dataname, data, type) tuples 1. Iterates through the list of (dataname, data, type) tuples
2. For each payload: extracts the type from the tuple and serializes accordingly 2. For each payload: extracts the type from the tuple and serializes accordingly
3. Compares the serialized size against `size_threshold` 3. Compares the serialized size against `size_threshold`
4. For small payloads: encodes as Base64, constructs a "direct" msg_payload_v1 4. For small payloads: encodes as Base64, constructs a "direct" msg_payload_v1
5. For large payloads: uploads to the fileserver, constructs a "link" msg_payload_v1 with the URL 5. For large payloads: uploads to the fileserver, constructs a "link" msg_payload_v1 with the URL
6. Converts envelope to JSON string and returns (NATS publishing is not performed by this function) 6. Constructs msg_envelope_v1 with all payloads and metadata
7. Converts envelope to JSON string and returns (NATS publishing is handled by the caller)
# Arguments: # Arguments:
- `subject::String` - NATS subject to publish the message to - `subject::String` - NATS subject to publish the message to
@@ -374,7 +378,6 @@ Each payload can have a different type, enabling mixed-content messages (e.g., c
- `receiver_id::String = ""` - UUID of the receiver (empty string means broadcast) - `receiver_id::String = ""` - UUID of the receiver (empty string means broadcast)
- `reply_to::String = ""` - Topic to reply to (empty string if no reply expected) - `reply_to::String = ""` - Topic to reply to (empty string if no reply expected)
- `reply_to_msg_id::String = ""` - Message ID this message is replying to - `reply_to_msg_id::String = ""` - Message ID this message is replying to
- `NATS_connection::Union{NATS.Connection, Nothing} = nothing` - Pre-existing NATS connection (reserved for future use)
- `msg_id::String = string(uuid4())` - Message ID (auto-generated UUID if not provided) - `msg_id::String = string(uuid4())` - Message ID (auto-generated UUID if not provided)
- `sender_id::String = string(uuid4())` - Sender ID (auto-generated UUID if not provided) - `sender_id::String = string(uuid4())` - Sender ID (auto-generated UUID if not provided)
@@ -438,7 +441,6 @@ function smartsend(
receiver_id::String = "", receiver_id::String = "",
reply_to::String = "", reply_to::String = "",
reply_to_msg_id::String = "", reply_to_msg_id::String = "",
NATS_connection::Union{NATS.Connection, Nothing} = nothing, # a provided connection saves establishing connection overhead.
msg_id::String = string(uuid4()), # Message ID msg_id::String = string(uuid4()), # Message ID
sender_id::String = string(uuid4()) # Sender ID sender_id::String = string(uuid4()) # Sender ID
)::Tuple{msg_envelope_v1, String} where {T1<:Any} )::Tuple{msg_envelope_v1, String} where {T1<:Any}
@@ -701,73 +703,73 @@ function _serialize_data(data::Any, payload_type::String)
end end
""" publish_message - Publish message to NATS # """ publish_message - Publish message to NATS
This function publishes a message to a NATS subject with proper # This function publishes a message to a NATS subject with proper
connection management and logging. # connection management and logging.
# Arguments: # # Arguments:
- `broker_url::String` - NATS server URL (e.g., "nats://localhost:4222") # - `broker_url::String` - NATS server URL (e.g., "nats://localhost:4222")
- `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt") # - `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt")
- `message::String` - JSON message to publish # - `message::String` - JSON message to publish
- `correlation_id::String` - Correlation ID for tracing and logging # - `correlation_id::String` - Correlation ID for tracing and logging
# Return: # # Return:
- `nothing` - This function performs publishing but returns nothing # - `nothing` - This function performs publishing but returns nothing
# Example # # Example
```jldoctest # ```jldoctest
using NATS # using NATS
# Prepare JSON message # # Prepare JSON message
message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}" # message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}"
# Publish to NATS # # Publish to NATS
publish_message("nats://localhost:4222", "my.subject", message, "abc123") # publish_message("nats://localhost:4222", "my.subject", message, "abc123")
``` # ```
""" # """
function publish_message(broker_url::String, subject::String, message::String, correlation_id::String) # function publish_message(broker_url::String, subject::String, message::String, correlation_id::String)
conn = NATS.connect(broker_url) # Create NATS connection # conn = NATS.connect(broker_url) # Create NATS connection
publish_message(conn, subject, message, correlation_id) # publish_message(conn, subject, message, correlation_id)
end # end
""" publish_message - Publish message to NATS using pre-existing connection # """ publish_message - Publish message to NATS using pre-existing connection
This function publishes a message to a NATS subject using a pre-existing NATS connection, # This function publishes a message to a NATS subject using a pre-existing NATS connection,
avoiding the overhead of connection establishment. # avoiding the overhead of connection establishment.
# Arguments: # # Arguments:
- `conn::NATS.Connection` - Pre-existing NATS connection # - `conn::NATS.Connection` - Pre-existing NATS connection
- `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt") # - `subject::String` - NATS subject to publish to (e.g., "/agent/wine/api/v1/prompt")
- `message::String` - JSON message to publish # - `message::String` - JSON message to publish
- `correlation_id::String` - Correlation ID for tracing and logging # - `correlation_id::String` - Correlation ID for tracing and logging
# Return: # # Return:
- `nothing` - This function performs publishing but returns nothing # - `nothing` - This function performs publishing but returns nothing
# Example # # Example
```jldoctest # ```jldoctest
using NATS # using NATS
# Prepare JSON message # # Prepare JSON message
message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}" # message = "{\"correlation_id\":\"abc123\",\"payload\":\"test\"}"
# Create connection once and reuse for multiple publishes # # Create connection once and reuse for multiple publishes
conn = NATS.connect("nats://localhost:4222") # conn = NATS.connect("nats://localhost:4222")
publish_message(conn, "my.subject", message, "abc123") # publish_message(conn, "my.subject", message, "abc123")
# Connection is automatically drained after publish # # Connection is automatically drained after publish
``` # ```
# Use Case: # # Use Case:
Use this version when you already have an established NATS connection and want to publish # Use this version when you already have an established NATS connection and want to publish
multiple messages without the overhead of creating a new connection for each publish. # multiple messages without the overhead of creating a new connection for each publish.
""" # """
function publish_message(conn::NATS.Connection, subject::String, message::String, correlation_id::String) # function publish_message(conn::NATS.Connection, subject::String, message::String, correlation_id::String)
try # try
NATS.publish(conn, subject, message) # Publish message to NATS # NATS.publish(conn, subject, message) # Publish message to NATS
log_trace(correlation_id, "Message published to $subject") # Log successful publish # log_trace(correlation_id, "Message published to $subject") # Log successful publish
finally # finally
NATS.drain(conn) # Ensure connection is closed properly # NATS.drain(conn) # Ensure connection is closed properly
end # end
end # end
""" smartreceive - Receive and process messages from NATS """ smartreceive - Receive and process messages from NATS