|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import requests |
| 16 | + |
| 17 | +from veadk.consts import DEFAULT_COZELOOP_SPACE_NAME |
| 18 | +from veadk.utils.logger import get_logger |
| 19 | + |
| 20 | +logger = get_logger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class VeCozeloop: |
| 24 | + def __init__(self, api_key: str) -> None: |
| 25 | + self.api_key = api_key |
| 26 | + |
| 27 | + def create_workspace( |
| 28 | + self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME |
| 29 | + ) -> str: |
| 30 | + logger.info( |
| 31 | + f"Automatically create Cozeloop workspace with name {workspace_name}" |
| 32 | + ) |
| 33 | + |
| 34 | + URL = "https://api.coze.cn/v1/workspaces" |
| 35 | + |
| 36 | + headers = { |
| 37 | + "Authorization": f"Bearer {self.api_key}", |
| 38 | + "Content-Type": "application/json", |
| 39 | + } |
| 40 | + |
| 41 | + data = { |
| 42 | + "name": workspace_name, |
| 43 | + "description": "Created by Volcengine Agent Development Kit (VeADK)", |
| 44 | + } |
| 45 | + |
| 46 | + response = requests.post(URL, headers=headers, json=data) |
| 47 | + |
| 48 | + if response.json().get("code") == 0: |
| 49 | + workspace_id = response.json().get("data").get("id") |
| 50 | + logger.info(f"Cozeloop workspace ID: {workspace_id}") |
| 51 | + return workspace_id |
| 52 | + else: |
| 53 | + raise Exception( |
| 54 | + f"Failed to automatically create workspace: {response.json()}" |
| 55 | + ) |
| 56 | + |
| 57 | + def get_workspace_id( |
| 58 | + self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME |
| 59 | + ) -> str: |
| 60 | + logger.info( |
| 61 | + f"Automatically fetching Cozeloop workspace ID with name {workspace_name}" |
| 62 | + ) |
| 63 | + |
| 64 | + URL = "https://api.coze.cn/v1/workspaces" |
| 65 | + |
| 66 | + headers = { |
| 67 | + "Authorization": f"Bearer {self.api_key}", |
| 68 | + "Content-Type": "application/json", |
| 69 | + } |
| 70 | + |
| 71 | + data = { |
| 72 | + "page_num": 1, |
| 73 | + "page_size": 50, |
| 74 | + } |
| 75 | + |
| 76 | + response = requests.post(URL, headers=headers, json=data) |
| 77 | + |
| 78 | + if response.json().get("code") == 0: |
| 79 | + workspaces = response.json().get("data").get("workspaces", []) |
| 80 | + |
| 81 | + workspace_id = "" |
| 82 | + for workspace in workspaces: |
| 83 | + if workspace.get("name") == workspace_name: |
| 84 | + workspace_id = workspace.get("id") |
| 85 | + logger.info(f"Get Cozeloop workspace ID: {workspace_id}") |
| 86 | + return workspace_id |
| 87 | + |
| 88 | + raise Exception(f"Workspace with name {workspace_name} not found.") |
| 89 | + else: |
| 90 | + raise Exception(f"Failed to get workspace ID: {response.json()}") |
0 commit comments