pycoze 0.1.94__py3-none-any.whl → 0.1.96__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
pycoze/access/lib.py ADDED
@@ -0,0 +1,47 @@
1
+ import os
2
+ import sys
3
+ import importlib
4
+
5
+
6
+ class ChangeDirectoryAndPath:
7
+ """Context manager to change the current working directory and sys.path."""
8
+
9
+ def __init__(self, module_path):
10
+ self.module_path = module_path
11
+ self.old_path = None
12
+
13
+ def __enter__(self):
14
+ self.old_path = os.getcwd()
15
+ sys.path.insert(0, self.module_path)
16
+ os.chdir(self.module_path)
17
+ return self
18
+
19
+ def __exit__(self, exc_type, exc_value, traceback):
20
+ sys.path.remove(self.module_path)
21
+ os.chdir(self.old_path)
22
+
23
+
24
+ class ModuleManager:
25
+ """Context manager for handling module imports and sys.modules state."""
26
+
27
+ def __init__(self, module_path):
28
+ self.module_path = module_path
29
+ self.original_modules = sys.modules.copy()
30
+
31
+ def __enter__(self):
32
+ """Enter the runtime context related to this object."""
33
+ self.change_dir = ChangeDirectoryAndPath(self.module_path)
34
+ self.change_dir.__enter__()
35
+ return self
36
+
37
+ def __exit__(self, exc_type, exc_value, traceback):
38
+ """Exit the runtime context related to this object."""
39
+ self.change_dir.__exit__(exc_type, exc_value, traceback)
40
+ self.cleanup_modules()
41
+
42
+ def cleanup_modules(self):
43
+ """Restore the original sys.modules state."""
44
+ importlib.invalidate_caches()
45
+ for key in list(sys.modules.keys()):
46
+ if key not in self.original_modules:
47
+ del sys.modules[key]
@@ -1,28 +1,12 @@
1
1
  import sys
2
2
  import os
3
3
  import importlib
4
- from langchain.agents import tool as _tool
4
+ from langchain.agents import tool as to_agent_tool
5
5
  import types
6
6
  import langchain_core
7
+ from .lib import ChangeDirectoryAndPath, ModuleManager
7
8
 
8
9
 
9
- class ChangeDirectoryAndPath:
10
- """Context manager to change the current working directory and sys.path."""
11
-
12
- def __init__(self, module_path):
13
- self.module_path = module_path
14
- self.old_path = None
15
-
16
- def __enter__(self):
17
- self.old_path = os.getcwd()
18
- sys.path.insert(0, self.module_path)
19
- os.chdir(self.module_path)
20
- return self
21
-
22
- def __exit__(self, exc_type, exc_value, traceback):
23
- sys.path.remove(self.module_path)
24
- os.chdir(self.old_path)
25
-
26
10
 
27
11
  def wrapped_tool(tool, module_path):
28
12
  """Wrap the tool function to include additional logging and path management."""
@@ -48,11 +32,8 @@ def import_tools(tool_id):
48
32
  print(f"Tool {tool_id} not found")
49
33
  return []
50
34
 
51
- # Save the current sys.modules state
52
- original_modules = sys.modules.copy()
53
-
54
35
  try:
55
- with ChangeDirectoryAndPath(module_path):
36
+ with ModuleManager(module_path) as manager:
56
37
  module = importlib.import_module("tool")
57
38
  export_tools = getattr(module, "export_tools")
58
39
  valid_tools = []
@@ -60,23 +41,16 @@ def import_tools(tool_id):
60
41
  assert isinstance(tool, langchain_core.tools.StructuredTool) or isinstance(
61
42
  tool, types.FunctionType
62
43
  ), f"Tool is not a StructuredTool or function: {tool}"
63
- if isinstance(tool, types.FunctionType) and not isinstance(
64
- tool, langchain_core.tools.StructuredTool
65
- ):
66
- valid_tools.append(_tool(tool))
44
+ if not isiisinstance(tool, langchain_core.tools.StructuredTool):
45
+ tool = to_agent_tool(tool)
46
+ valid_tools.append(to_agent_tool(tool))
67
47
  export_tools = valid_tools
68
48
 
69
49
  except Exception as e:
70
50
  print(f"Error loading tool {tool_id}: {e}")
71
51
  return []
72
52
 
73
- # Unload modules and restore sys.modules state
74
- importlib.invalidate_caches()
75
- for key in list(sys.modules.keys()):
76
- if key not in original_modules:
77
- del sys.modules[key]
78
-
79
53
  for tool in export_tools:
80
54
  tool.func = wrapped_tool(tool, module_path)
81
55
 
82
- return export_tools
56
+ return export_tools
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.94
3
+ Version: 0.1.96
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -1,7 +1,8 @@
1
1
  pycoze/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  pycoze/access/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- pycoze/access/tool_for_bot.py,sha256=SbAyQJ9BtuLquVrXnmHJl8t_cdID-AYvdwXQvY67u-Y,2608
4
+ pycoze/access/lib.py,sha256=yeWWFauplfv9AVWbNXL4hAGqgdTXygqs42a23peDN6M,1514
5
+ pycoze/access/tool_for_bot.py,sha256=ZVwI3_6gw5eJQ9qyGSwTTcGx9zAoSDPtdWGNzRMdhTU,1855
5
6
  pycoze/ai/__init__.py,sha256=odM2lgYSnApxw4AzLV_5AyaxL5_MLfydOSB1VmLGFyA,75
6
7
  pycoze/ai/vram_reserve.py,sha256=QbqaA8qv87cnEpOVDMygi0BNMxuhLYwj1UKfR_D5BD4,4340
7
8
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
@@ -20,8 +21,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
20
21
  pycoze/utils/__init__.py,sha256=TNJhFfY7JYdLlzuP9GvgxfNXUtbgH_NUUJSqHXCxJn4,78
21
22
  pycoze/utils/arg.py,sha256=kA3KBQzXc2WlH5XbF8kfikfpqljiKaW7oY_GE4Qyffc,753
22
23
  pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
23
- pycoze-0.1.94.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
24
- pycoze-0.1.94.dist-info/METADATA,sha256=jjabhgJm9ZlQmWh0-Y0Xoh9t3BDCBvYRpif09RLi5Vs,725
25
- pycoze-0.1.94.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
26
- pycoze-0.1.94.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
27
- pycoze-0.1.94.dist-info/RECORD,,
24
+ pycoze-0.1.96.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
25
+ pycoze-0.1.96.dist-info/METADATA,sha256=GeUJ7Kg7xc6-wzpQPPX9pUBd5HCPQ__nOvYDuXKEiH4,725
26
+ pycoze-0.1.96.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
27
+ pycoze-0.1.96.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
28
+ pycoze-0.1.96.dist-info/RECORD,,