agentrun-inner-test 0.0.46__py3-none-any.whl → 0.0.64__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. agentrun/__init__.py +34 -1
  2. agentrun/agent_runtime/__endpoint_async_template.py +40 -0
  3. agentrun/agent_runtime/api/control.py +1 -1
  4. agentrun/agent_runtime/endpoint.py +79 -0
  5. agentrun/credential/api/control.py +1 -1
  6. agentrun/integration/agentscope/__init__.py +2 -1
  7. agentrun/integration/agentscope/builtin.py +23 -0
  8. agentrun/integration/builtin/__init__.py +2 -0
  9. agentrun/integration/builtin/knowledgebase.py +137 -0
  10. agentrun/integration/crewai/__init__.py +2 -1
  11. agentrun/integration/crewai/builtin.py +23 -0
  12. agentrun/integration/google_adk/__init__.py +2 -1
  13. agentrun/integration/google_adk/builtin.py +23 -0
  14. agentrun/integration/langchain/__init__.py +2 -1
  15. agentrun/integration/langchain/builtin.py +23 -0
  16. agentrun/integration/langgraph/__init__.py +2 -1
  17. agentrun/integration/langgraph/builtin.py +23 -0
  18. agentrun/integration/pydantic_ai/__init__.py +2 -1
  19. agentrun/integration/pydantic_ai/builtin.py +23 -0
  20. agentrun/knowledgebase/__client_async_template.py +173 -0
  21. agentrun/knowledgebase/__init__.py +53 -0
  22. agentrun/knowledgebase/__knowledgebase_async_template.py +438 -0
  23. agentrun/knowledgebase/api/__data_async_template.py +414 -0
  24. agentrun/knowledgebase/api/__init__.py +19 -0
  25. agentrun/knowledgebase/api/control.py +606 -0
  26. agentrun/knowledgebase/api/data.py +624 -0
  27. agentrun/knowledgebase/client.py +311 -0
  28. agentrun/knowledgebase/knowledgebase.py +748 -0
  29. agentrun/knowledgebase/model.py +270 -0
  30. agentrun/memory_collection/__client_async_template.py +178 -0
  31. agentrun/memory_collection/__init__.py +37 -0
  32. agentrun/memory_collection/__memory_collection_async_template.py +457 -0
  33. agentrun/memory_collection/api/__init__.py +5 -0
  34. agentrun/memory_collection/api/control.py +610 -0
  35. agentrun/memory_collection/client.py +323 -0
  36. agentrun/memory_collection/memory_collection.py +844 -0
  37. agentrun/memory_collection/model.py +162 -0
  38. agentrun/model/api/control.py +1 -1
  39. agentrun/sandbox/aio_sandbox.py +11 -4
  40. agentrun/sandbox/api/control.py +1 -1
  41. agentrun/sandbox/browser_sandbox.py +2 -2
  42. agentrun/sandbox/model.py +0 -13
  43. agentrun/toolset/api/control.py +1 -1
  44. agentrun/toolset/toolset.py +1 -0
  45. agentrun/utils/__data_api_async_template.py +1 -0
  46. agentrun/utils/config.py +12 -0
  47. agentrun/utils/control_api.py +27 -0
  48. agentrun/utils/data_api.py +1 -0
  49. {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/METADATA +4 -2
  50. {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/RECORD +53 -34
  51. {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/WHEEL +0 -0
  52. {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/licenses/LICENSE +0 -0
  53. {agentrun_inner_test-0.0.46.dist-info → agentrun_inner_test-0.0.64.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,457 @@
1
+ """MemoryCollection 高层 API / MemoryCollection High-Level API
2
+
3
+ 此模块定义记忆集合资源的高级API。
4
+ This module defines the high-level API for memory collection resources.
5
+ """
6
+
7
+ from typing import Any, Dict, List, Optional, Tuple
8
+
9
+ from agentrun.utils.config import Config
10
+ from agentrun.utils.model import PageableInput
11
+ from agentrun.utils.resource import ResourceBase
12
+
13
+ from .model import (
14
+ MemoryCollectionCreateInput,
15
+ MemoryCollectionImmutableProps,
16
+ MemoryCollectionListInput,
17
+ MemoryCollectionListOutput,
18
+ MemoryCollectionMutableProps,
19
+ MemoryCollectionSystemProps,
20
+ MemoryCollectionUpdateInput,
21
+ )
22
+
23
+
24
+ class MemoryCollection(
25
+ MemoryCollectionMutableProps,
26
+ MemoryCollectionImmutableProps,
27
+ MemoryCollectionSystemProps,
28
+ ResourceBase,
29
+ ):
30
+ """记忆集合资源 / MemoryCollection Resource
31
+
32
+ 提供记忆集合的完整生命周期管理,包括创建、删除、更新、查询。
33
+ Provides complete lifecycle management for memory collections, including create, delete, update, and query.
34
+ """
35
+
36
+ @classmethod
37
+ def __get_client(cls):
38
+ """获取客户端实例 / Get client instance
39
+
40
+ Returns:
41
+ MemoryCollectionClient: 客户端实例 / Client instance
42
+ """
43
+ from .client import MemoryCollectionClient
44
+
45
+ return MemoryCollectionClient()
46
+
47
+ @classmethod
48
+ async def create_async(
49
+ cls, input: MemoryCollectionCreateInput, config: Optional[Config] = None
50
+ ):
51
+ """创建记忆集合(异步)
52
+
53
+ Args:
54
+ input: 记忆集合输入参数
55
+ config: 配置
56
+
57
+ Returns:
58
+ MemoryCollection: 创建的记忆集合对象
59
+ """
60
+ return await cls.__get_client().create_async(input, config=config)
61
+
62
+ @classmethod
63
+ async def delete_by_name_async(
64
+ cls, memory_collection_name: str, config: Optional[Config] = None
65
+ ):
66
+ """根据名称删除记忆集合(异步)
67
+
68
+ Args:
69
+ memory_collection_name: 记忆集合名称
70
+ config: 配置
71
+ """
72
+ return await cls.__get_client().delete_async(
73
+ memory_collection_name, config=config
74
+ )
75
+
76
+ @classmethod
77
+ async def update_by_name_async(
78
+ cls,
79
+ memory_collection_name: str,
80
+ input: MemoryCollectionUpdateInput,
81
+ config: Optional[Config] = None,
82
+ ):
83
+ """根据名称更新记忆集合(异步)
84
+
85
+ Args:
86
+ memory_collection_name: 记忆集合名称
87
+ input: 记忆集合更新输入参数
88
+ config: 配置
89
+
90
+ Returns:
91
+ MemoryCollection: 更新后的记忆集合对象
92
+ """
93
+ return await cls.__get_client().update_async(
94
+ memory_collection_name, input, config=config
95
+ )
96
+
97
+ @classmethod
98
+ async def get_by_name_async(
99
+ cls, memory_collection_name: str, config: Optional[Config] = None
100
+ ):
101
+ """根据名称获取记忆集合(异步)
102
+
103
+ Args:
104
+ memory_collection_name: 记忆集合名称
105
+ config: 配置
106
+
107
+ Returns:
108
+ MemoryCollection: 记忆集合对象
109
+ """
110
+ return await cls.__get_client().get_async(
111
+ memory_collection_name, config=config
112
+ )
113
+
114
+ @classmethod
115
+ async def _list_page_async(
116
+ cls, page_input: PageableInput, config: Config | None = None, **kwargs
117
+ ):
118
+ return await cls.__get_client().list_async(
119
+ input=MemoryCollectionListInput(
120
+ **kwargs,
121
+ **page_input.model_dump(),
122
+ ),
123
+ config=config,
124
+ )
125
+
126
+ @classmethod
127
+ async def list_all_async(
128
+ cls,
129
+ *,
130
+ memory_collection_name: Optional[str] = None,
131
+ config: Optional[Config] = None,
132
+ ) -> List[MemoryCollectionListOutput]:
133
+ """列出所有记忆集合(异步)
134
+
135
+ Args:
136
+ memory_collection_name: 记忆集合名称(可选)
137
+ config: 配置
138
+
139
+ Returns:
140
+ List[MemoryCollectionListOutput]: 记忆集合列表
141
+ """
142
+ return await cls._list_all_async(
143
+ lambda mc: mc.memory_collection_id or "",
144
+ config=config,
145
+ memory_collection_name=memory_collection_name,
146
+ )
147
+
148
+ async def update_async(
149
+ self,
150
+ input: MemoryCollectionUpdateInput,
151
+ config: Optional[Config] = None,
152
+ ):
153
+ """更新记忆集合(异步)
154
+
155
+ Args:
156
+ input: 记忆集合更新输入参数
157
+ config: 配置
158
+
159
+ Returns:
160
+ MemoryCollection: 更新后的记忆集合对象
161
+ """
162
+ if self.memory_collection_name is None:
163
+ raise ValueError(
164
+ "memory_collection_name is required to update a"
165
+ " MemoryCollection"
166
+ )
167
+
168
+ result = await self.update_by_name_async(
169
+ self.memory_collection_name, input, config=config
170
+ )
171
+ self.update_self(result)
172
+
173
+ return self
174
+
175
+ async def delete_async(self, config: Optional[Config] = None):
176
+ """删除记忆集合(异步)
177
+
178
+ Args:
179
+ config: 配置
180
+ """
181
+ if self.memory_collection_name is None:
182
+ raise ValueError(
183
+ "memory_collection_name is required to delete a"
184
+ " MemoryCollection"
185
+ )
186
+
187
+ return await self.delete_by_name_async(
188
+ self.memory_collection_name, config=config
189
+ )
190
+
191
+ async def get_async(self, config: Optional[Config] = None):
192
+ """刷新记忆集合信息(异步)
193
+
194
+ Args:
195
+ config: 配置
196
+
197
+ Returns:
198
+ MemoryCollection: 刷新后的记忆集合对象
199
+ """
200
+ if self.memory_collection_name is None:
201
+ raise ValueError(
202
+ "memory_collection_name is required to refresh a"
203
+ " MemoryCollection"
204
+ )
205
+
206
+ result = await self.get_by_name_async(
207
+ self.memory_collection_name, config=config
208
+ )
209
+ self.update_self(result)
210
+
211
+ return self
212
+
213
+ async def refresh_async(self, config: Optional[Config] = None):
214
+ """刷新记忆集合信息(异步)
215
+
216
+ Args:
217
+ config: 配置
218
+
219
+ Returns:
220
+ MemoryCollection: 刷新后的记忆集合对象
221
+ """
222
+ return await self.get_async(config=config)
223
+
224
+ @classmethod
225
+ async def to_mem0_memory_async(
226
+ cls,
227
+ memory_collection_name: str,
228
+ config: Optional[Config] = None,
229
+ history_db_path: Optional[str] = None,
230
+ ):
231
+ """将 MemoryCollection 转换为 agentrun-mem0ai Memory 客户端(异步)
232
+
233
+ Args:
234
+ memory_collection_name: 记忆集合名称
235
+ config: AgentRun 配置
236
+ history_db_path: mem0 历史数据库路径(可选)
237
+
238
+ Returns:
239
+ Memory: agentrun-mem0ai Memory 客户端实例
240
+
241
+ Raises:
242
+ ImportError: 如果未安装 agentrun-mem0ai 包
243
+ ValueError: 如果配置信息不完整
244
+
245
+ Example:
246
+ >>> memory = await MemoryCollection.to_mem0_memory_async(
247
+ ... "memoryCollection010901",
248
+ ... config=config
249
+ ... )
250
+ >>> memory.add("用户喜欢吃苹果", user_id="user123")
251
+ """
252
+ try:
253
+ from agentrun_mem0 import Memory
254
+ except ImportError as e:
255
+ raise ImportError(
256
+ "agentrun-mem0ai package is required. Install it with: pip"
257
+ " install agentrun-mem0ai"
258
+ ) from e
259
+
260
+ # 获取 MemoryCollection 配置
261
+ memory_collection = await cls.get_by_name_async(
262
+ memory_collection_name, config=config
263
+ )
264
+
265
+ # 构建 mem0 配置
266
+ mem0_config = await cls._build_mem0_config_async(
267
+ memory_collection, config, history_db_path
268
+ )
269
+
270
+ # 创建并返回 Memory 实例
271
+ return Memory.from_config(mem0_config)
272
+
273
+ @staticmethod
274
+ def _convert_vpc_endpoint_to_public(endpoint: str) -> str:
275
+ """将 VPC 内网地址转换为公网地址
276
+
277
+ Args:
278
+ endpoint: 原始 endpoint,可能是 VPC 内网地址
279
+
280
+ Returns:
281
+ str: 公网地址
282
+
283
+ Example:
284
+ >>> _convert_vpc_endpoint_to_public("https://jiuqing.cn-hangzhou.vpc.tablestore.aliyuncs.com")
285
+ "https://jiuqing.cn-hangzhou.ots.aliyuncs.com"
286
+ """
287
+ if ".vpc.tablestore.aliyuncs.com" in endpoint:
288
+ # 将 .vpc.tablestore.aliyuncs.com 替换为 .ots.aliyuncs.com
289
+ return endpoint.replace(
290
+ ".vpc.tablestore.aliyuncs.com", ".ots.aliyuncs.com"
291
+ )
292
+ return endpoint
293
+
294
+ @classmethod
295
+ async def _build_mem0_config_async(
296
+ cls,
297
+ memory_collection: "MemoryCollection",
298
+ config: Optional[Config],
299
+ history_db_path: Optional[str] = None,
300
+ ) -> Dict[str, Any]:
301
+ """构建 mem0 配置字典(异步)
302
+
303
+ Args:
304
+ memory_collection: MemoryCollection 对象
305
+ config: AgentRun 配置
306
+ history_db_path: 历史数据库路径
307
+
308
+ Returns:
309
+ Dict[str, Any]: mem0 配置字典
310
+ """
311
+ mem0_config: Dict[str, Any] = {}
312
+
313
+ # 构建 vector_store 配置
314
+ if memory_collection.vector_store_config:
315
+ vector_store_config = memory_collection.vector_store_config
316
+ provider = vector_store_config.provider or ""
317
+
318
+ if vector_store_config.config:
319
+ vs_config = vector_store_config.config
320
+ vector_store: Dict[str, Any] = {
321
+ "provider": provider,
322
+ "config": {},
323
+ }
324
+
325
+ # 根据不同的 provider 构建配置
326
+ if provider == "aliyun_tablestore":
327
+ # 获取凭证信息
328
+ effective_config = config or Config()
329
+ # 将 VPC 内网地址转换为公网地址
330
+ public_endpoint = cls._convert_vpc_endpoint_to_public(
331
+ vs_config.endpoint or ""
332
+ )
333
+ vector_store["config"] = {
334
+ "vector_dimension": vs_config.vector_dimension,
335
+ "endpoint": public_endpoint,
336
+ "instance_name": vs_config.instance_name,
337
+ "collection_name": vs_config.collection_name,
338
+ "access_key_id": effective_config.get_access_key_id(),
339
+ "access_key_secret": (
340
+ effective_config.get_access_key_secret()
341
+ ),
342
+ }
343
+ # 如果有 security_token,添加它
344
+ security_token = effective_config.get_security_token()
345
+ if security_token:
346
+ vector_store["config"]["sts_token"] = security_token
347
+ else:
348
+ # 其他 provider 的通用配置
349
+ vector_store["config"] = {
350
+ "endpoint": vs_config.endpoint,
351
+ "collection_name": vs_config.collection_name,
352
+ }
353
+ if vs_config.vector_dimension:
354
+ vector_store["config"][
355
+ "vector_dimension"
356
+ ] = vs_config.vector_dimension
357
+
358
+ mem0_config["vector_store"] = vector_store
359
+
360
+ # 构建 llm 配置
361
+ if memory_collection.llm_config:
362
+ llm_config = memory_collection.llm_config
363
+ model_service_name = llm_config.model_service_name
364
+
365
+ if model_service_name and llm_config.config:
366
+ # 使用高层 API 获取 ModelService 配置
367
+ base_url, api_key = (
368
+ await cls._resolve_model_service_config_async(
369
+ model_service_name, config
370
+ )
371
+ )
372
+
373
+ mem0_config["llm"] = {
374
+ "provider": "openai", # mem0 使用 openai 兼容接口
375
+ "config": {
376
+ "model": llm_config.config.model,
377
+ "openai_base_url": base_url,
378
+ "api_key": api_key,
379
+ },
380
+ }
381
+
382
+ # 构建 embedder 配置
383
+ if memory_collection.embedder_config:
384
+ embedder_config = memory_collection.embedder_config
385
+ model_service_name = embedder_config.model_service_name
386
+
387
+ if model_service_name and embedder_config.config:
388
+ # 使用高层 API 获取 ModelService 配置
389
+ base_url, api_key = (
390
+ await cls._resolve_model_service_config_async(
391
+ model_service_name, config
392
+ )
393
+ )
394
+
395
+ mem0_config["embedder"] = {
396
+ "provider": "openai", # mem0 使用 openai 兼容接口
397
+ "config": {
398
+ "model": embedder_config.config.model,
399
+ "openai_base_url": base_url,
400
+ "api_key": api_key,
401
+ },
402
+ }
403
+
404
+ # 添加历史数据库路径
405
+ if history_db_path:
406
+ mem0_config["history_db_path"] = history_db_path
407
+
408
+ return mem0_config
409
+
410
+ @staticmethod
411
+ async def _resolve_model_service_config_async(
412
+ model_service_name: str, config: Optional[Config]
413
+ ) -> Tuple[str, str]:
414
+ """解析 ModelService 配置获取 baseUrl 和 apiKey(异步)
415
+
416
+ Args:
417
+ model_service_name: ModelService 名称
418
+ config: AgentRun 配置
419
+
420
+ Returns:
421
+ Tuple[str, str]: (base_url, api_key)
422
+
423
+ Raises:
424
+ ValueError: 如果配置信息不完整
425
+ """
426
+ from agentrun.credential import Credential
427
+ from agentrun.model import ModelService
428
+
429
+ # 使用高层 API 获取 ModelService
430
+ model_service = await ModelService.get_by_name_async(
431
+ model_service_name, config=config
432
+ )
433
+
434
+ # 获取 provider_settings
435
+ if not model_service.provider_settings:
436
+ raise ValueError(
437
+ f"ModelService {model_service_name} providerSettings is empty"
438
+ )
439
+
440
+ base_url = model_service.provider_settings.base_url or ""
441
+ api_key = model_service.provider_settings.api_key or ""
442
+
443
+ # 如果有 credentialName,使用高层 API 获取 credential secret
444
+ credential_name = model_service.credential_name
445
+ if credential_name:
446
+ credential = await Credential.get_by_name_async(
447
+ credential_name, config=config
448
+ )
449
+ if credential.credential_secret:
450
+ api_key = credential.credential_secret
451
+
452
+ if not base_url:
453
+ raise ValueError(
454
+ f"ModelService {model_service_name} baseUrl is empty"
455
+ )
456
+
457
+ return base_url, api_key
@@ -0,0 +1,5 @@
1
+ """MemoryCollection API 模块 / MemoryCollection API Module"""
2
+
3
+ from .control import MemoryCollectionControlAPI
4
+
5
+ __all__ = ["MemoryCollectionControlAPI"]