diff --git a/cozepy/workflows/runs/__init__.py b/cozepy/workflows/runs/__init__.py index fc709b0..89a1c12 100644 --- a/cozepy/workflows/runs/__init__.py +++ b/cozepy/workflows/runs/__init__.py @@ -6,7 +6,7 @@ from cozepy.auth import Auth from cozepy.model import AsyncIteratorHTTPResponse, AsyncStream, CozeModel, IteratorHTTPResponse, Stream from cozepy.request import Requester -from cozepy.util import remove_url_trailing_slash +from cozepy.util import remove_none_values, remove_url_trailing_slash if TYPE_CHECKING: from .run_histories import AsyncWorkflowsRunsRunHistoriesClient, WorkflowsRunsRunHistoriesClient @@ -163,6 +163,7 @@ def create( workflow_id: str, parameters: Optional[Dict[str, Any]] = None, bot_id: Optional[str] = None, + app_id: Optional[str] = None, is_async: bool = False, ext: Optional[Dict[str, Any]] = None, ) -> WorkflowRunResult: @@ -179,6 +180,7 @@ def create( list of parameters on the arrangement page of the specified workflow. :param bot_id: The associated Bot ID required for some workflow executions, such as workflows with database nodes, variable nodes, etc. + :param app_id: The app_id is required for some workflow executions, :param is_async: Whether to run asynchronously. :param ext: Used to specify some additional fields in the format of Map[String][String]. :return: The result of the workflow execution @@ -188,10 +190,11 @@ def create( "workflow_id": workflow_id, "parameters": parameters, "bot_id": bot_id, + "app_id": app_id, "is_async": is_async, "ext": ext, } - return self._requester.request("post", url, False, WorkflowRunResult, body=body) + return self._requester.request("post", url, False, WorkflowRunResult, body=remove_none_values(body)) def stream( self, @@ -213,6 +216,7 @@ def stream( list of parameters on the arrangement page of the specified workflow. :param bot_id: The associated Bot ID required for some workflow executions, such as workflows with database nodes, variable nodes, etc. + :param app_id: The app_id is required for some workflow executions, :param ext: Used to specify some additional fields in the format of Map[String][String]. :return: The result of the workflow execution """ @@ -220,8 +224,8 @@ def stream( body = { "workflow_id": workflow_id, "parameters": parameters, - "app_id": app_id, "bot_id": bot_id, + "app_id": app_id, "ext": ext, } resp: IteratorHTTPResponse[str] = self._requester.request( @@ -229,7 +233,7 @@ def stream( url, True, None, - body=body, + body=remove_none_values(body), ) return Stream( resp._raw_response, resp.data, fields=["id", "event", "data"], handler=_sync_workflow_stream_handler @@ -293,6 +297,7 @@ async def create( workflow_id: str, parameters: Optional[Dict[str, Any]] = None, bot_id: Optional[str] = None, + app_id: Optional[str] = None, is_async: bool = False, ext: Optional[Dict[str, Any]] = None, ) -> WorkflowRunResult: @@ -309,6 +314,7 @@ async def create( list of parameters on the arrangement page of the specified workflow. :param bot_id: The associated Bot ID required for some workflow executions, such as workflows with database nodes, variable nodes, etc. + :param app_id: The app_id is required for some workflow executions, :param is_async: Whether to run asynchronously. :param ext: Used to specify some additional fields in the format of Map[String][String]. :return: The result of the workflow execution @@ -318,10 +324,11 @@ async def create( "workflow_id": workflow_id, "parameters": parameters, "bot_id": bot_id, + "app_id": app_id, "is_async": is_async, "ext": ext, } - return await self._requester.arequest("post", url, False, WorkflowRunResult, body=body) + return await self._requester.arequest("post", url, False, WorkflowRunResult, body=remove_none_values(body)) async def stream( self, @@ -329,6 +336,7 @@ async def stream( workflow_id: str, parameters: Optional[Dict[str, Any]] = None, bot_id: Optional[str] = None, + app_id: Optional[str] = None, ext: Optional[Dict[str, Any]] = None, ) -> AsyncIterator[WorkflowEvent]: """ @@ -342,6 +350,7 @@ async def stream( list of parameters on the arrangement page of the specified workflow. :param bot_id: The associated Bot ID required for some workflow executions, such as workflows with database nodes, variable nodes, etc. + :param app_id: The app_id is required for some workflow executions, :param ext: Used to specify some additional fields in the format of Map[String][String]. :return: The result of the workflow execution """ @@ -350,6 +359,7 @@ async def stream( "workflow_id": workflow_id, "parameters": parameters, "bot_id": bot_id, + "app_id": app_id, "ext": ext, } resp: AsyncIteratorHTTPResponse[str] = await self._requester.arequest( @@ -357,7 +367,7 @@ async def stream( url, True, None, - body=body, + body=remove_none_values(body), ) async for item in AsyncStream( resp.data, diff --git a/examples/workflow_async.py b/examples/workflow_async.py index 277437f..0afd1a7 100644 --- a/examples/workflow_async.py +++ b/examples/workflow_async.py @@ -6,7 +6,7 @@ import os import time -from cozepy import COZE_COM_BASE_URL, WorkflowExecuteStatus, setup_logging +from cozepy import COZE_COM_BASE_URL, Coze, TokenAuth, WorkflowExecuteStatus, setup_logging # Get an access_token through personal access token or oauth. coze_api_token = os.getenv("COZE_API_TOKEN") @@ -19,8 +19,6 @@ if is_debug: setup_logging(logging.DEBUG) -from cozepy import Coze, TokenAuth, Message, ChatStatus, MessageContentType # noqa - # Init the Coze client through the access_token. coze = Coze(auth=TokenAuth(token=coze_api_token), base_url=coze_api_base) diff --git a/examples/workflow_stream.py b/examples/workflow_stream.py index feb6148..7caf840 100644 --- a/examples/workflow_stream.py +++ b/examples/workflow_stream.py @@ -2,6 +2,7 @@ This example describes how to use the workflow interface to stream chat. """ +import json import os from cozepy import COZE_COM_BASE_URL @@ -18,7 +19,9 @@ coze = Coze(auth=TokenAuth(token=coze_api_token), base_url=coze_api_base) # Create a workflow instance in Coze, copy the last number from the web link as the workflow's ID. -workflow_id = "workflow id" +workflow_id = os.getenv("COZE_WORKFLOW_ID") or "workflow id" +# parameters +parameters = json.loads(os.getenv("COZE_PARAMETERS") or "{}") # The stream interface will return an iterator of WorkflowEvent. Developers should iterate @@ -44,5 +47,6 @@ def handle_workflow_iterator(stream: Stream[WorkflowEvent]): handle_workflow_iterator( coze.workflows.runs.stream( workflow_id=workflow_id, + parameters=parameters, ) ) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index e53f941..ef26b9a 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -25,10 +25,7 @@ def mock_create_workflows_runs(respx_mock, is_async: bool): "/v1/workflow/run", json={ "workflow_id": "id", - "parameters": None, - "bot_id": None, "is_async": is_async, - "ext": None, }, ).mock(res._raw_response) return res