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


##########
python/flink_agents/runtime/remote_execution_environment.py:
##########
@@ -0,0 +1,226 @@
+################################################################################
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+# limitations under the License.
+#################################################################################
+from typing import Any, Dict, Generator, List, Optional
+
+import cloudpickle
+from pyflink.common import Row
+from pyflink.common.typeinfo import PickledBytesTypeInfo, RowTypeInfo
+from pyflink.datastream import (
+    DataStream,
+    KeyedProcessFunction,
+    KeyedStream,
+    KeySelector,
+)
+from pyflink.table import StreamTableEnvironment, Table
+from pyflink.util.java_utils import invoke_method
+
+from flink_agents.api.event import InputEvent
+from flink_agents.api.execution_enviroment import (
+    AgentBuilder,
+    AgentInstance,
+    AgentsExecutionEnvironment,
+)
+from flink_agents.api.workflow import Workflow
+from flink_agents.plan.workflow_plan import WorkflowPlan
+
+
+class RemoteAgentBuilder(AgentBuilder):
+    """RemoteAgentBuilder for integrating datastream and agent."""
+
+    __input: DataStream
+    __workflow_plan: WorkflowPlan = None
+    __output: DataStream = None
+    __t_env: StreamTableEnvironment
+
+    def __init__(
+        self, input: DataStream, t_env: Optional[StreamTableEnvironment] = None
+    ) -> None:
+        """Init method of RemoteAgentBuilder."""
+        self.__input = input
+        self.__t_env = t_env
+
+    def apply(self, workflow: Workflow) -> "AgentBuilder":
+        """Set workflow of execution environment.
+
+        Parameters
+        ----------
+        workflow : Workflow
+            The workflow user defined to run in execution environment.
+        """
+        if self.__workflow_plan is not None:
+            err_msg = "RemoteAgentBuilder doesn't support apply multiple 
workflows yet."
+            raise RuntimeError(err_msg)
+        self.__workflow_plan = WorkflowPlan.from_workflow(workflow)
+        return self
+
+    def to_datastream(self) -> DataStream:
+        """Get output datastream of workflow execution.
+
+        Returns:
+        -------
+        DataStream
+            Output datastream of agent execution.
+        """
+        j_data_stream_output = invoke_method(
+            None,
+            "org.apache.flink.agents.runtime.FlinkAgent",
+            "connectToWorkflow",
+            [
+                self.__input._j_data_stream,
+                self.__workflow_plan.model_dump_json(serialize_as_any=True),
+            ],
+            [
+                "org.apache.flink.streaming.api.datastream.DataStream",
+                "java.lang.String",
+            ],
+        )
+        output_stream = DataStream(j_data_stream_output)
+        self.__output = output_stream.map(lambda x: 
cloudpickle.loads(x).output)
+        return self.__output
+
+    def to_table(self) -> Table:
+        """Get output Table of workflow execution.
+
+        Returns:
+        -------
+        Table
+            Output Table of agent execution.
+        """
+        return self.__t_env.from_data_stream(self.to_datastream())
+
+    def to_list(self) -> List[Dict[str, Any]]:
+        """Get output list of workflow execution.
+
+        This method is not supported for remote execution environments.
+        """
+        msg = "RemoteAgentBuilder does not support to_list."
+        raise NotImplementedError(msg)
+
+    def build(self) -> AgentInstance:
+        """Build agent instance.
+
+        This method is not supported for RemoteAgentBuilder.
+        """
+        msg = (
+            "RemoteAgentBuilder does not support build Agent Instance and run 
individually, must run as"
+            "a flink job."
+        )
+        raise NotImplementedError(msg)

Review Comment:
   I think keep consistency makes sense, so I changed this implementation. But 
when run an agent with datastream/table , it must be executed as a flink job,  
so I keep the usage in example.



-- 
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