wenjin272 commented on code in PR #631:
URL: https://github.com/apache/flink-agents/pull/631#discussion_r3194127713


##########
api/src/main/java/org/apache/flink/agents/api/event/ChatRequestEvent.java:
##########
@@ -18,40 +18,75 @@
 
 package org.apache.flink.agents.api.event;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.flink.agents.api.Event;
 import org.apache.flink.agents.api.chat.messages.ChatMessage;
 
 import javax.annotation.Nullable;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 /** Event representing a request for chat. */
 public class ChatRequestEvent extends Event {
-    private final String model;
-    private final List<ChatMessage> messages;
-    private final @Nullable Object outputSchema;
+
+    public static final String EVENT_TYPE = "_chat_request_event";
+
+    private static final ObjectMapper MAPPER = new ObjectMapper();
 
     public ChatRequestEvent(
             String model, List<ChatMessage> messages, @Nullable Object 
outputSchema) {
-        this.model = model;
-        this.messages = messages;
-        this.outputSchema = outputSchema;
+        super(EVENT_TYPE);
+        setAttr("model", model);
+        setAttr("messages", messages);
+        if (outputSchema != null) {
+            setAttr("output_schema", outputSchema);
+        }
     }
 
     public ChatRequestEvent(String model, List<ChatMessage> messages) {
         this(model, messages, null);
     }
 
+    /**
+     * Reconstructs a typed ChatRequestEvent from a base Event, deserializing 
nested types.
+     *
+     * @param event the base event containing chat request data in attributes
+     * @return a typed ChatRequestEvent
+     */
+    @SuppressWarnings("unchecked")
+    public static ChatRequestEvent fromEvent(Event event) {
+        String model = (String) event.getAttr("model");
+        List<?> rawMessages = (List<?>) event.getAttr("messages");
+        List<ChatMessage> messages = new ArrayList<>();
+        if (rawMessages != null) {
+            for (Object m : rawMessages) {
+                if (m instanceof ChatMessage) {
+                    messages.add((ChatMessage) m);
+                } else if (m instanceof Map) {
+                    messages.add(MAPPER.convertValue(m, ChatMessage.class));
+                }
+            }
+        }
+        return new ChatRequestEvent(model, messages, 
event.getAttr("output_schema"));

Review Comment:
   Here, we do not copy the event's `id`. Therefore, `ChatRequestEvent` 
generates a new random `id`, which causes the same `ChatRequestEvent` to have 
different `id` on the sender and receiver sides.
   
   This issue also exists for other built-in Event subclasses.



##########
plan/src/main/java/org/apache/flink/agents/plan/actions/ToolCallAction.java:
##########
@@ -41,7 +41,7 @@ public static Action getToolCallAction() throws Exception {
                         ToolCallAction.class,
                         "processToolRequest",
                         new Class[] {ToolRequestEvent.class, 
RunnerContext.class}),

Review Comment:
   Here, we constrain the first parameter of `processToolRequest` to be of type 
`ToolRequestEvent`. However, in cross-language invocations, only generic Event 
objects are passed rather than specific subclasses, which results in an 
`argument type mismatch` error.
   
   I think this can be aligned with `ChatModelAction` by changing the parameter 
type to `Event` and calling `ToolRequestEvent.fromEvent(event)` within the 
function to convert the `Event` into a `ToolRequestEvent`.



##########
runtime/src/main/java/org/apache/flink/agents/runtime/actionstate/ActionStateUtil.java:
##########


Review Comment:
   The purpose of ActionState is to enable action-level recovery after a job 
failover. However, the `HashMap` storing attributes does not guarantee strict 
consistency in key order before and after a JVM restart, which may lead to 
inconsistent UUIDs.
   
   After the Event refactoring, it seems that there is no distinction made for 
`InputEvent` here. I think we can unify it into
   ```
   return String.valueOf(
                       
UUID.nameUUIDFromBytes(MAPPER.writeValueAsBytes(event.getAttributes())));
   ```
   And the `MAPPER` needs to have sorting enabled.
   ```
   private static final ObjectMapper MAPPER =
               JsonMapper.builder()
                       
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
                       .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, 
true)
                       .build();
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to