|
| 1 | +"""MemoryCollection 客户端 / MemoryCollection Client |
| 2 | +
|
| 3 | +此模块提供记忆集合管理的客户端API。 |
| 4 | +This module provides the client API for memory collection management. |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from alibabacloud_agentrun20250910.models import ( |
| 10 | + CreateMemoryCollectionInput, |
| 11 | + ListMemoryCollectionsRequest, |
| 12 | + UpdateMemoryCollectionInput, |
| 13 | +) |
| 14 | + |
| 15 | +from agentrun.utils.config import Config |
| 16 | +from agentrun.utils.exception import HTTPError |
| 17 | + |
| 18 | +from .api.control import MemoryCollectionControlAPI |
| 19 | +from .memory_collection import MemoryCollection |
| 20 | +from .model import ( |
| 21 | + MemoryCollectionCreateInput, |
| 22 | + MemoryCollectionListInput, |
| 23 | + MemoryCollectionListOutput, |
| 24 | + MemoryCollectionUpdateInput, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class MemoryCollectionClient: |
| 29 | + """MemoryCollection 客户端 / MemoryCollection Client |
| 30 | +
|
| 31 | + 提供记忆集合的创建、删除、更新和查询功能。 |
| 32 | + Provides create, delete, update and query functions for memory collections. |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, config: Optional[Config] = None): |
| 36 | + """初始化客户端 / Initialize client |
| 37 | +
|
| 38 | + Args: |
| 39 | + config: 配置对象,可选 / Configuration object, optional |
| 40 | + """ |
| 41 | + self.__control_api = MemoryCollectionControlAPI(config) |
| 42 | + |
| 43 | + async def create_async( |
| 44 | + self, |
| 45 | + input: MemoryCollectionCreateInput, |
| 46 | + config: Optional[Config] = None, |
| 47 | + ): |
| 48 | + """创建记忆集合(异步) / Create memory collection asynchronously |
| 49 | +
|
| 50 | + Args: |
| 51 | + input: 记忆集合输入参数 / Memory collection input parameters |
| 52 | + config: 配置对象,可选 / Configuration object, optional |
| 53 | +
|
| 54 | + Returns: |
| 55 | + MemoryCollection: 创建的记忆集合对象 / Created memory collection object |
| 56 | +
|
| 57 | + Raises: |
| 58 | + ResourceAlreadyExistError: 资源已存在 / Resource already exists |
| 59 | + HTTPError: HTTP 请求错误 / HTTP request error |
| 60 | + """ |
| 61 | + try: |
| 62 | + result = await self.__control_api.create_memory_collection_async( |
| 63 | + CreateMemoryCollectionInput().from_map(input.model_dump()), |
| 64 | + config=config, |
| 65 | + ) |
| 66 | + |
| 67 | + return MemoryCollection.from_inner_object(result) |
| 68 | + except HTTPError as e: |
| 69 | + raise e.to_resource_error( |
| 70 | + "MemoryCollection", input.memory_collection_name |
| 71 | + ) from e |
| 72 | + |
| 73 | + async def delete_async( |
| 74 | + self, memory_collection_name: str, config: Optional[Config] = None |
| 75 | + ): |
| 76 | + """删除记忆集合(异步) |
| 77 | +
|
| 78 | + Args: |
| 79 | + memory_collection_name: 记忆集合名称 |
| 80 | + config: 配置 |
| 81 | +
|
| 82 | + Raises: |
| 83 | + ResourceNotExistError: 记忆集合不存在 |
| 84 | + """ |
| 85 | + try: |
| 86 | + result = await self.__control_api.delete_memory_collection_async( |
| 87 | + memory_collection_name, config=config |
| 88 | + ) |
| 89 | + |
| 90 | + return MemoryCollection.from_inner_object(result) |
| 91 | + |
| 92 | + except HTTPError as e: |
| 93 | + raise e.to_resource_error( |
| 94 | + "MemoryCollection", memory_collection_name |
| 95 | + ) from e |
| 96 | + |
| 97 | + async def update_async( |
| 98 | + self, |
| 99 | + memory_collection_name: str, |
| 100 | + input: MemoryCollectionUpdateInput, |
| 101 | + config: Optional[Config] = None, |
| 102 | + ): |
| 103 | + """更新记忆集合(异步) |
| 104 | +
|
| 105 | + Args: |
| 106 | + memory_collection_name: 记忆集合名称 |
| 107 | + input: 记忆集合更新输入参数 |
| 108 | + config: 配置 |
| 109 | +
|
| 110 | + Returns: |
| 111 | + MemoryCollection: 更新后的记忆集合对象 |
| 112 | +
|
| 113 | + Raises: |
| 114 | + ResourceNotExistError: 记忆集合不存在 |
| 115 | + """ |
| 116 | + try: |
| 117 | + result = await self.__control_api.update_memory_collection_async( |
| 118 | + memory_collection_name, |
| 119 | + UpdateMemoryCollectionInput().from_map(input.model_dump()), |
| 120 | + config=config, |
| 121 | + ) |
| 122 | + |
| 123 | + return MemoryCollection.from_inner_object(result) |
| 124 | + except HTTPError as e: |
| 125 | + raise e.to_resource_error( |
| 126 | + "MemoryCollection", memory_collection_name |
| 127 | + ) from e |
| 128 | + |
| 129 | + async def get_async( |
| 130 | + self, memory_collection_name: str, config: Optional[Config] = None |
| 131 | + ): |
| 132 | + """获取记忆集合(异步) |
| 133 | +
|
| 134 | + Args: |
| 135 | + memory_collection_name: 记忆集合名称 |
| 136 | + config: 配置 |
| 137 | +
|
| 138 | + Returns: |
| 139 | + MemoryCollection: 记忆集合对象 |
| 140 | +
|
| 141 | + Raises: |
| 142 | + ResourceNotExistError: 记忆集合不存在 |
| 143 | + """ |
| 144 | + try: |
| 145 | + result = await self.__control_api.get_memory_collection_async( |
| 146 | + memory_collection_name, config=config |
| 147 | + ) |
| 148 | + return MemoryCollection.from_inner_object(result) |
| 149 | + except HTTPError as e: |
| 150 | + raise e.to_resource_error( |
| 151 | + "MemoryCollection", memory_collection_name |
| 152 | + ) from e |
| 153 | + |
| 154 | + async def list_async( |
| 155 | + self, |
| 156 | + input: Optional[MemoryCollectionListInput] = None, |
| 157 | + config: Optional[Config] = None, |
| 158 | + ): |
| 159 | + """列出记忆集合(异步) |
| 160 | +
|
| 161 | + Args: |
| 162 | + input: 分页查询参数 |
| 163 | + config: 配置 |
| 164 | +
|
| 165 | + Returns: |
| 166 | + List[MemoryCollectionListOutput]: 记忆集合列表 |
| 167 | + """ |
| 168 | + if input is None: |
| 169 | + input = MemoryCollectionListInput() |
| 170 | + |
| 171 | + results = await self.__control_api.list_memory_collections_async( |
| 172 | + ListMemoryCollectionsRequest().from_map(input.model_dump()), |
| 173 | + config=config, |
| 174 | + ) |
| 175 | + return [ |
| 176 | + MemoryCollectionListOutput.from_inner_object(item) |
| 177 | + for item in results.items # type: ignore |
| 178 | + ] |
0 commit comments