Skip to content

Commit

Permalink
style: Update enum values to uppercase snake_case
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Sep 27, 2024
1 parent a4d0a06 commit fbcf306
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 229 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ chat = coze.chat.create(
],
)
start = int(time.time())
while chat.status == ChatStatus.in_progress:
while chat.status == ChatStatus.IN_PROGRESS:
if int(time.time()) - start > 120:
# too long, cancel chat
coze.chat.cancel(conversation_id=chat.conversation_id, chat_id=chat.chat_id)
Expand All @@ -87,7 +87,7 @@ chat_iterator = coze.chat.stream(
],
)
for event in chat_iterator:
if event.event == ChatEventType.conversation_message_delta:
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
print('got message delta:', event.message.content)
```

Expand Down Expand Up @@ -145,7 +145,7 @@ conversation = coze.conversations.retrieve(conversation_id=conversation.id)
message = coze.conversations.messages.create(
conversation_id=conversation.id,
content='how are you?',
content_type=MessageContentType.text,
content_type=MessageContentType.TEXT,
)

# retrieve message
Expand All @@ -156,7 +156,7 @@ coze.conversations.messages.update(
conversation_id=conversation.id,
message_id=message.id,
content='hey, how are you?',
content_type=MessageContentType.text,
content_type=MessageContentType.TEXT,
)

# delete message
Expand Down Expand Up @@ -199,11 +199,11 @@ result = coze.workflows.runs.create(
# stream workflow run
def handle_workflow_iterator(iterator: WorkflowEventIterator):
for event in iterator:
if event.event == WorkflowEventType.message:
if event.event == WorkflowEventType.MESSAGE:
print('got message', event.message)
elif event.event == WorkflowEventType.error:
elif event.event == WorkflowEventType.ERROR:
print('got error', event.error)
elif event.event == WorkflowEventType.interrupt:
elif event.event == WorkflowEventType.INTERRUPT:
handle_workflow_iterator(coze.workflows.runs.resume(
workflow_id='workflow id',
event_id=event.interrupt.interrupt_data.event_id,
Expand Down
55 changes: 27 additions & 28 deletions cozepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
from .auth import OAuthToken, DeviceAuthCode, ApplicationOAuth, Auth, TokenAuth, JWTAuth
from .auth import ApplicationOAuth, Auth, DeviceAuthCode, JWTAuth, OAuthToken, TokenAuth
from .bots import (
BotPromptInfo,
BotOnboardingInfo,
Bot,
BotModelInfo,
BotOnboardingInfo,
BotPluginAPIInfo,
BotPluginInfo,
Bot,
BotPromptInfo,
SimpleBot,
)
from .chat import (
MessageRole,
MessageType,
MessageContentType,
MessageObjectStringType,
ChatStatus,
MessageObjectString,
Message,
Chat,
ChatEventType,
ChatEvent,
ChatChatIterator,
ChatEvent,
ChatEventType,
ChatStatus,
Message,
MessageContentType,
MessageObjectString,
MessageObjectStringType,
MessageRole,
MessageType,
)
from .config import (
COZE_COM_BASE_URL,
COZE_CN_BASE_URL,
DEFAULT_TIMEOUT,
COZE_COM_BASE_URL,
DEFAULT_CONNECTION_LIMITS,
DEFAULT_TIMEOUT,
)
from .conversations import Conversation
from .coze import Coze
from .exception import CozeError, CozeAPIError, CozeEventError
from .exception import CozeAPIError, CozeError, CozeEventError
from .files import File
from .model import (
TokenPaged,
NumberPaged,
LastIDPaged,
NumberPaged,
TokenPaged,
)
from .request import HTTPClient
from .version import VERSION
from .workflows.runs import (
WorkflowRunResult,
WorkflowEventType,
WorkflowEventMessage,
WorkflowEventInterruptData,
WorkflowEventInterrupt,
WorkflowEventError,
WorkflowEvent,
WorkflowEventError,
WorkflowEventInterrupt,
WorkflowEventInterruptData,
WorkflowEventIterator,
WorkflowEventMessage,
WorkflowEventType,
WorkflowRunResult,
)
from .workspaces import WorkspaceRoleType, WorkspaceType, Workspace
from .request import HTTPClient

from .version import VERSION
from .workspaces import Workspace, WorkspaceRoleType, WorkspaceType

__all__ = [
"VERSION",
Expand Down
18 changes: 5 additions & 13 deletions cozepy/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from authlib.jose import jwt

from cozepy.config import COZE_COM_BASE_URL
from cozepy.model import CozeModel
from cozepy.request import Requester
from cozepy.config import COZE_COM_BASE_URL


def _random_hex(length):
Expand Down Expand Up @@ -48,9 +48,7 @@ class ApplicationOAuth(object):
App OAuth process to support obtaining token and refreshing token.
"""

def __init__(
self, client_id: str, client_secret: str = "", base_url: str = COZE_COM_BASE_URL
):
def __init__(self, client_id: str, client_secret: str = "", base_url: str = COZE_COM_BASE_URL):
self._client_id = client_id
self._client_secret = client_secret
self._base_url = base_url
Expand All @@ -62,22 +60,16 @@ def jwt_auth(self, private_key: str, kid: str, ttl: int) -> OAuthToken:
"""
Get the token by jwt with jwt auth flow.
"""
jwt_token = self._gen_jwt(
self._api_endpoint, private_key, self._client_id, kid, 3600
)
jwt_token = self._gen_jwt(self._api_endpoint, private_key, self._client_id, kid, 3600)
url = f"{self._base_url}/api/permission/oauth2/token"
headers = {"Authorization": f"Bearer {jwt_token}"}
body = {
"duration_seconds": ttl,
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
}
return self._requester.request(
"post", url, OAuthToken, headers=headers, body=body
)
return self._requester.request("post", url, OAuthToken, headers=headers, body=body)

def _gen_jwt(
self, api_endpoint: str, private_key: str, client_id: str, kid: str, ttl: int
):
def _gen_jwt(self, api_endpoint: str, private_key: str, client_id: str, kid: str, ttl: int):
now = int(time.time())
header = {"alg": "RS256", "typ": "JWT", "kid": kid}
payload = {
Expand Down
22 changes: 7 additions & 15 deletions cozepy/bots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class BotModelInfo(CozeModel):


class BotMode(IntEnum):
SingleAgent = 0
MultiAgent = 1
SingleAgentWorkflow = 2
SINGL_EAGENT = 0
MULTI_AGENT = 1
SINGLE_AGENT_WORKFLOW = 2


class BotPluginAPIInfo(CozeModel):
Expand Down Expand Up @@ -122,9 +122,7 @@ def create(
"description": description,
"icon_file_id": icon_file_id,
"prompt_info": prompt_info.model_dump() if prompt_info else None,
"onboarding_info": onboarding_info.model_dump()
if onboarding_info
else None,
"onboarding_info": onboarding_info.model_dump() if onboarding_info else None,
}

return self._requester.request("post", url, Bot, body=body)
Expand Down Expand Up @@ -165,9 +163,7 @@ def update(
"description": description,
"icon_file_id": icon_file_id,
"prompt_info": prompt_info.model_dump() if prompt_info else None,
"onboarding_info": onboarding_info.model_dump()
if onboarding_info
else None,
"onboarding_info": onboarding_info.model_dump() if onboarding_info else None,
}

return self._requester.request("post", url, None, body=body)
Expand Down Expand Up @@ -207,9 +203,7 @@ def retrieve(self, *, bot_id: str) -> Bot:

return self._requester.request("get", url, Bot, params=params)

def list(
self, *, space_id: str, page_num: int = 1, page_size: int = 20
) -> NumberPaged[SimpleBot]:
def list(self, *, space_id: str, page_num: int = 1, page_size: int = 20) -> NumberPaged[SimpleBot]:
"""
Get the bots published as API service.
查看指定空间发布到 Bot as API 渠道的 Bot 列表。
Expand All @@ -233,9 +227,7 @@ def list(
"page_size": page_size,
"page_index": page_num,
}
data = self._requester.request(
"get", url, self._PrivateListPublishedBotsV1Data, params=params
)
data = self._requester.request("get", url, self._PrivateListPublishedBotsV1Data, params=params)
return NumberPaged(
items=data.space_bots,
page_num=page_num,
Expand Down
Loading

0 comments on commit fbcf306

Please sign in to comment.