dacp 0.3.0__py3-none-any.whl → 0.3.2__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.
dacp/tools.py CHANGED
@@ -1,55 +1,105 @@
1
- from typing import Dict, Any, Callable
1
+ """
2
+ DACP Tools - Built-in tool implementations.
3
+
4
+ This module provides the core tool functionality including tool registry,
5
+ execution, and built-in tools like file_writer.
6
+ """
7
+
8
+ import logging
2
9
  from pathlib import Path
10
+ from typing import Dict, Any, Callable
11
+
12
+ logger = logging.getLogger("dacp.tools")
13
+
14
+ # Global tool registry
15
+ TOOL_REGISTRY: Dict[str, Callable[[Dict[str, Any]], Dict[str, Any]]] = {}
16
+
17
+
18
+ def register_tool(name: str, func: Callable[[Dict[str, Any]], Dict[str, Any]]) -> None:
19
+ """
20
+ Register a tool function.
21
+
22
+ Args:
23
+ name: Name of the tool
24
+ func: Function that takes args dict and returns result dict
25
+ """
26
+ TOOL_REGISTRY[name] = func
27
+ logger.info(f"🔧 Tool '{name}' registered")
3
28
 
4
- TOOL_REGISTRY: Dict[str, Callable[..., Dict[str, Any]]] = {}
5
29
 
30
+ def execute_tool(name: str, args: Dict[str, Any]) -> Dict[str, Any]:
31
+ """
32
+ Execute a tool by name with given arguments.
33
+
34
+ Args:
35
+ name: Name of the tool to execute
36
+ args: Arguments to pass to the tool
6
37
 
7
- def register_tool(tool_id: str, func: Callable[..., Dict[str, Any]]) -> None:
8
- """Register a tool function."""
9
- TOOL_REGISTRY[tool_id] = func
38
+ Returns:
39
+ Tool execution result
10
40
 
41
+ Raises:
42
+ ValueError: If tool is not found
43
+ """
44
+ if name not in TOOL_REGISTRY:
45
+ available_tools = list(TOOL_REGISTRY.keys())
46
+ logger.error(
47
+ f"❌ Tool '{name}' not found. " f"Available tools: {available_tools}"
48
+ )
49
+ raise ValueError(f"Tool '{name}' not found. Available tools: {available_tools}")
11
50
 
12
- def run_tool(tool_id: str, args: Dict[str, Any]) -> Dict[str, Any]:
13
- """Run a registered tool with the given arguments."""
14
- if tool_id not in TOOL_REGISTRY:
15
- raise ValueError(f"Unknown tool: {tool_id}")
51
+ logger.debug(f"🛠️ Executing tool '{name}' with args: {args}")
16
52
 
17
- tool_func = TOOL_REGISTRY[tool_id]
18
- return tool_func(**args)
53
+ try:
54
+ result = TOOL_REGISTRY[name](args)
55
+ logger.debug(f"✅ Tool '{name}' completed successfully")
56
+ return result
57
+ except Exception as e:
58
+ logger.error(f"❌ Tool '{name}' failed: {type(e).__name__}: {e}")
59
+ raise
19
60
 
20
61
 
21
- def file_writer(path: str, content: str) -> Dict[str, Any]:
62
+ def file_writer(args: Dict[str, Any]) -> Dict[str, Any]:
22
63
  """
23
- Write content to a file, creating parent directories if they don't exist.
64
+ Write content to a file, creating directories as needed.
24
65
 
25
66
  Args:
26
- path: File path to write to
27
- content: Content to write to the file
67
+ args: Dictionary containing 'path' and 'content'
28
68
 
29
69
  Returns:
30
- Dict with success status and file path
70
+ Success status and file information
31
71
  """
72
+ path = args.get("path")
73
+ content = args.get("content", "")
74
+
75
+ if not path:
76
+ raise ValueError("file_writer requires 'path' argument")
77
+
32
78
  try:
33
79
  # Create parent directories if they don't exist
34
- Path(path).parent.mkdir(parents=True, exist_ok=True)
80
+ file_path = Path(path)
81
+ file_path.parent.mkdir(parents=True, exist_ok=True)
35
82
 
36
- # Write the content to the file
37
- with open(path, "w", encoding="utf-8") as f:
83
+ # Write content to file
84
+ with open(file_path, "w", encoding="utf-8") as f:
38
85
  f.write(content)
39
86
 
87
+ logger.info(f"✅ File written successfully: {path}")
88
+
40
89
  return {
41
90
  "success": True,
42
- "path": path,
91
+ "path": str(file_path),
43
92
  "message": f"Successfully wrote {len(content)} characters to {path}",
44
93
  }
94
+
45
95
  except Exception as e:
96
+ logger.error(f"❌ Failed to write file {path}: {e}")
46
97
  return {
47
98
  "success": False,
48
99
  "path": path,
49
100
  "error": str(e),
50
- "message": f"Failed to write to {path}: {e}",
51
101
  }
52
102
 
53
103
 
54
- # Register the built-in file_writer tool
104
+ # Register built-in tools
55
105
  register_tool("file_writer", file_writer)