Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add app_id support to workflow #181

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions cozepy/workflows/runs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -213,23 +216,24 @@ 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
"""
url = f"{self._base_url}/v1/workflow/stream_run"
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(
"post",
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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -318,17 +324,19 @@ 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,
*,
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]:
"""
Expand All @@ -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
"""
Expand All @@ -350,14 +359,15 @@ 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(
"post",
url,
True,
None,
body=body,
body=remove_none_values(body),
)
async for item in AsyncStream(
resp.data,
Expand Down
4 changes: 1 addition & 3 deletions examples/workflow_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion examples/workflow_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -44,5 +47,6 @@ def handle_workflow_iterator(stream: Stream[WorkflowEvent]):
handle_workflow_iterator(
coze.workflows.runs.stream(
workflow_id=workflow_id,
parameters=parameters,
)
)
3 changes: 0 additions & 3 deletions tests/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading