agentic-blocks 0.1.10__py3-none-any.whl → 0.1.12__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.
@@ -18,16 +18,23 @@ def langchain_tool_to_openai_format(tool) -> Dict[str, Any]:
18
18
  """
19
19
  schema = tool.args_schema.model_json_schema()
20
20
 
21
+ # Build parameters object, including $defs if present
22
+ parameters = {
23
+ "type": "object",
24
+ "properties": schema.get("properties", {}),
25
+ "required": schema.get("required", [])
26
+ }
27
+
28
+ # Include $defs if present in the schema
29
+ if "$defs" in schema:
30
+ parameters["$defs"] = schema["$defs"]
31
+
21
32
  return {
22
33
  "type": "function",
23
34
  "function": {
24
35
  "name": schema.get("title", tool.name),
25
36
  "description": schema.get("description", ""),
26
- "parameters": {
27
- "type": "object",
28
- "properties": schema.get("properties", {}),
29
- "required": schema.get("required", [])
30
- }
37
+ "parameters": parameters
31
38
  }
32
39
  }
33
40
 
@@ -43,4 +50,134 @@ def langchain_tools_to_openai_format(tools: List) -> List[Dict[str, Any]]:
43
50
  List of dictionaries in OpenAI function calling format, compatible with
44
51
  MCPClient.list_tools() output and call_llm() tools parameter
45
52
  """
46
- return [langchain_tool_to_openai_format(tool) for tool in tools]
53
+ return [langchain_tool_to_openai_format(tool) for tool in tools]
54
+
55
+
56
+ def create_tool_registry(tools: List) -> Dict[str, Any]:
57
+ """
58
+ Create a registry mapping tool names to LangChain tool instances.
59
+
60
+ Args:
61
+ tools: List of langchain_core.tools.structured.StructuredTool instances
62
+
63
+ Returns:
64
+ Dictionary mapping tool names to tool instances
65
+ """
66
+ return {tool.name: tool for tool in tools}
67
+
68
+
69
+ def execute_tool_call(tool_call: Dict[str, Any], tool_registry: Dict[str, Any]) -> Dict[str, Any]:
70
+ """
71
+ Execute a single tool call using LangChain tool registry.
72
+
73
+ Args:
74
+ tool_call: Dictionary with 'tool_name', 'arguments', and 'tool_call_id' keys
75
+ tool_registry: Registry mapping tool names to tool instances
76
+
77
+ Returns:
78
+ Dictionary with 'tool_call_id', 'result', and 'is_error' keys
79
+ """
80
+ tool_name = tool_call.get('tool_name')
81
+ arguments = tool_call.get('arguments', {})
82
+ tool_call_id = tool_call.get('tool_call_id')
83
+
84
+ try:
85
+ if tool_name not in tool_registry:
86
+ raise ValueError(f"Tool '{tool_name}' not found in registry")
87
+
88
+ tool = tool_registry[tool_name]
89
+ result = tool.invoke(arguments)
90
+
91
+ return {
92
+ 'tool_call_id': tool_call_id,
93
+ 'result': result,
94
+ 'is_error': False
95
+ }
96
+ except Exception as e:
97
+ return {
98
+ 'tool_call_id': tool_call_id,
99
+ 'result': f"Error executing tool '{tool_name}': {str(e)}",
100
+ 'is_error': True
101
+ }
102
+
103
+
104
+ def execute_pending_tool_calls(messages, tool_registry: Dict[str, Any]) -> List[Dict[str, Any]]:
105
+ """
106
+ Execute all pending tool calls from a Messages instance and add responses back.
107
+
108
+ Args:
109
+ messages: Messages instance with pending tool calls
110
+ tool_registry: Registry mapping tool names to tool instances
111
+
112
+ Returns:
113
+ List of execution results compatible with Messages.add_tool_responses format
114
+ """
115
+ pending_tool_calls = messages.get_pending_tool_calls()
116
+ results = []
117
+
118
+ for tool_call in pending_tool_calls:
119
+ result = execute_tool_call(tool_call, tool_registry)
120
+
121
+ # Convert to format expected by Messages.add_tool_responses
122
+ if result['is_error']:
123
+ tool_response = {
124
+ 'tool_call_id': result['tool_call_id'],
125
+ 'is_error': True,
126
+ 'error': result['result']
127
+ }
128
+ else:
129
+ tool_response = {
130
+ 'tool_call_id': result['tool_call_id'],
131
+ 'is_error': False,
132
+ 'tool_response': result['result']
133
+ }
134
+
135
+ results.append(tool_response)
136
+
137
+ # Add tool response back to messages using individual method
138
+ if result['is_error']:
139
+ messages.add_tool_response(result['tool_call_id'], result['result'])
140
+ else:
141
+ messages.add_tool_response(result['tool_call_id'], str(result['result']))
142
+
143
+ return results
144
+
145
+
146
+ def execute_and_add_tool_responses(messages, tool_registry: Dict[str, Any]) -> List[Dict[str, Any]]:
147
+ """
148
+ Execute all pending tool calls and add them using Messages.add_tool_responses batch method.
149
+
150
+ Args:
151
+ messages: Messages instance with pending tool calls
152
+ tool_registry: Registry mapping tool names to tool instances
153
+
154
+ Returns:
155
+ List of execution results compatible with Messages.add_tool_responses format
156
+ """
157
+ pending_tool_calls = messages.get_pending_tool_calls()
158
+ results = []
159
+
160
+ for tool_call in pending_tool_calls:
161
+ result = execute_tool_call(tool_call, tool_registry)
162
+
163
+ # Convert to format expected by Messages.add_tool_responses
164
+ if result['is_error']:
165
+ tool_response = {
166
+ 'tool_call_id': result['tool_call_id'],
167
+ 'is_error': True,
168
+ 'error': result['result']
169
+ }
170
+ else:
171
+ tool_response = {
172
+ 'tool_call_id': result['tool_call_id'],
173
+ 'is_error': False,
174
+ 'tool_response': result['result']
175
+ }
176
+
177
+ results.append(tool_response)
178
+
179
+ # Add all responses at once using the batch method
180
+ if results:
181
+ messages.add_tool_responses(results)
182
+
183
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agentic-blocks
3
- Version: 0.1.10
3
+ Version: 0.1.12
4
4
  Summary: Simple building blocks for agentic AI systems with MCP client and conversation management
5
5
  Author-email: Magnus Bjelkenhed <bjelkenhed@gmail.com>
6
6
  License: MIT
@@ -2,9 +2,9 @@ agentic_blocks/__init__.py,sha256=LJy2tzTwX9ZjPw8dqkXOWiude7ZDDIaBIvaLC8U4d_Y,43
2
2
  agentic_blocks/llm.py,sha256=q2Utx2NiffLrp7VHmQIRZtBtrnQSMFtqCeYs9ynj0Ww,5335
3
3
  agentic_blocks/mcp_client.py,sha256=15mIN_Qw0OVNJAvfgO3jVZS4-AU4TtvEQSFDlL9ruqA,9773
4
4
  agentic_blocks/messages.py,sha256=dxaR_-IiH8n7pZygFDiUt8n7mMOm--FRuN-xCQEuewo,11758
5
- agentic_blocks/utils/tools_utils.py,sha256=9l8uJobUo8DaipKTrIS_Ztaheggd0QkqiQanpXJW4dw,1451
6
- agentic_blocks-0.1.10.dist-info/licenses/LICENSE,sha256=r4IcBaAjTv3-yfjXgDPuRD953Qci0Y0nQn5JfHwLyBY,1073
7
- agentic_blocks-0.1.10.dist-info/METADATA,sha256=TUtbZhPOcUfqCOj_Z3WEHlhyL_kX4SqGz-Nhf1ehESU,11944
8
- agentic_blocks-0.1.10.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- agentic_blocks-0.1.10.dist-info/top_level.txt,sha256=-1a4RAemqicXLU1rRzw4QHV3KlNeQDNxVs3m2gAT238,15
10
- agentic_blocks-0.1.10.dist-info/RECORD,,
5
+ agentic_blocks/utils/tools_utils.py,sha256=p1q0qcyQqAEYxoGNbWTRcn4R9CpTH0QkdWOc7OiGveg,5931
6
+ agentic_blocks-0.1.12.dist-info/licenses/LICENSE,sha256=r4IcBaAjTv3-yfjXgDPuRD953Qci0Y0nQn5JfHwLyBY,1073
7
+ agentic_blocks-0.1.12.dist-info/METADATA,sha256=vcmqy_lp9Nr5jirQeSHwqcx8hOoknXilYiJ9DYvtuxQ,11944
8
+ agentic_blocks-0.1.12.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
+ agentic_blocks-0.1.12.dist-info/top_level.txt,sha256=-1a4RAemqicXLU1rRzw4QHV3KlNeQDNxVs3m2gAT238,15
10
+ agentic_blocks-0.1.12.dist-info/RECORD,,