agentic-blocks 0.1.13__py3-none-any.whl → 0.1.15__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.
@@ -8,16 +8,16 @@ from typing import Dict, Any, List
8
8
  def langchain_tool_to_openai_format(tool) -> Dict[str, Any]:
9
9
  """
10
10
  Convert a LangChain StructuredTool to OpenAI function calling format.
11
-
11
+
12
12
  Args:
13
13
  tool: A langchain_core.tools.structured.StructuredTool instance
14
-
14
+
15
15
  Returns:
16
- Dictionary in OpenAI function calling format, compatible with
16
+ Dictionary in OpenAI function calling format, compatible with
17
17
  MCPClient.list_tools() output and call_llm() tools parameter
18
18
  """
19
19
  schema = tool.args_schema.model_json_schema()
20
-
20
+
21
21
  # Resolve $ref references by flattening $defs into the schema
22
22
  # OpenAI doesn't support $ref/$defs, so we need to inline all definitions
23
23
  def resolve_refs(obj, defs):
@@ -38,39 +38,39 @@ def langchain_tool_to_openai_format(tool) -> Dict[str, Any]:
38
38
  return [resolve_refs(item, defs) for item in obj]
39
39
  else:
40
40
  return obj
41
-
41
+
42
42
  # Get definitions for reference resolution
43
43
  defs = schema.get("$defs", {})
44
-
44
+
45
45
  # Resolve references in properties
46
46
  resolved_properties = resolve_refs(schema.get("properties", {}), defs)
47
-
47
+
48
48
  # Build parameters object without $defs
49
49
  parameters = {
50
50
  "type": "object",
51
51
  "properties": resolved_properties,
52
- "required": schema.get("required", [])
52
+ "required": schema.get("required", []),
53
53
  }
54
-
54
+
55
55
  return {
56
56
  "type": "function",
57
57
  "function": {
58
58
  "name": schema.get("title", tool.name),
59
59
  "description": tool.description or schema.get("description", ""),
60
- "parameters": parameters
61
- }
60
+ "parameters": parameters,
61
+ },
62
62
  }
63
63
 
64
64
 
65
65
  def langchain_tools_to_openai_format(tools: List) -> List[Dict[str, Any]]:
66
66
  """
67
67
  Convert a list of LangChain StructuredTools to OpenAI function calling format.
68
-
68
+
69
69
  Args:
70
70
  tools: List of langchain_core.tools.structured.StructuredTool instances
71
-
71
+
72
72
  Returns:
73
- List of dictionaries in OpenAI function calling format, compatible with
73
+ List of dictionaries in OpenAI function calling format, compatible with
74
74
  MCPClient.list_tools() output and call_llm() tools parameter
75
75
  """
76
76
  return [langchain_tool_to_openai_format(tool) for tool in tools]
@@ -79,128 +79,130 @@ def langchain_tools_to_openai_format(tools: List) -> List[Dict[str, Any]]:
79
79
  def create_tool_registry(tools: List) -> Dict[str, Any]:
80
80
  """
81
81
  Create a registry mapping tool names to LangChain tool instances.
82
-
82
+
83
83
  Args:
84
84
  tools: List of langchain_core.tools.structured.StructuredTool instances
85
-
85
+
86
86
  Returns:
87
87
  Dictionary mapping tool names to tool instances
88
88
  """
89
89
  return {tool.name: tool for tool in tools}
90
90
 
91
91
 
92
- def execute_tool_call(tool_call: Dict[str, Any], tool_registry: Dict[str, Any]) -> Dict[str, Any]:
92
+ def execute_tool_call(
93
+ tool_call: Dict[str, Any], tool_registry: Dict[str, Any]
94
+ ) -> Dict[str, Any]:
93
95
  """
94
96
  Execute a single tool call using LangChain tool registry.
95
-
97
+
96
98
  Args:
97
99
  tool_call: Dictionary with 'tool_name', 'arguments', and 'tool_call_id' keys
98
100
  tool_registry: Registry mapping tool names to tool instances
99
-
101
+
100
102
  Returns:
101
103
  Dictionary with 'tool_call_id', 'result', and 'is_error' keys
102
104
  """
103
- tool_name = tool_call.get('tool_name')
104
- arguments = tool_call.get('arguments', {})
105
- tool_call_id = tool_call.get('tool_call_id')
106
-
105
+ tool_name = tool_call.get("tool_name")
106
+ arguments = tool_call.get("arguments", {})
107
+ tool_call_id = tool_call.get("tool_call_id")
108
+
107
109
  try:
108
110
  if tool_name not in tool_registry:
109
111
  raise ValueError(f"Tool '{tool_name}' not found in registry")
110
-
112
+
111
113
  tool = tool_registry[tool_name]
112
114
  result = tool.invoke(arguments)
113
-
114
- return {
115
- 'tool_call_id': tool_call_id,
116
- 'result': result,
117
- 'is_error': False
118
- }
115
+
116
+ return {"tool_call_id": tool_call_id, "result": result, "is_error": False}
119
117
  except Exception as e:
120
118
  return {
121
- 'tool_call_id': tool_call_id,
122
- 'result': f"Error executing tool '{tool_name}': {str(e)}",
123
- 'is_error': True
119
+ "tool_call_id": tool_call_id,
120
+ "result": f"Error executing tool '{tool_name}': {str(e)}",
121
+ "is_error": True,
124
122
  }
125
123
 
126
124
 
127
- def execute_pending_tool_calls(messages, tool_registry: Dict[str, Any]) -> List[Dict[str, Any]]:
125
+ def execute_pending_tool_calls(
126
+ messages, tool_registry: Dict[str, Any]
127
+ ) -> List[Dict[str, Any]]:
128
128
  """
129
129
  Execute all pending tool calls from a Messages instance and add responses back.
130
-
130
+
131
131
  Args:
132
132
  messages: Messages instance with pending tool calls
133
133
  tool_registry: Registry mapping tool names to tool instances
134
-
134
+
135
135
  Returns:
136
136
  List of execution results compatible with Messages.add_tool_responses format
137
137
  """
138
138
  pending_tool_calls = messages.get_pending_tool_calls()
139
139
  results = []
140
-
140
+
141
141
  for tool_call in pending_tool_calls:
142
142
  result = execute_tool_call(tool_call, tool_registry)
143
-
143
+
144
144
  # Convert to format expected by Messages.add_tool_responses
145
- if result['is_error']:
145
+ if result["is_error"]:
146
146
  tool_response = {
147
- 'tool_call_id': result['tool_call_id'],
148
- 'is_error': True,
149
- 'error': result['result']
147
+ "tool_call_id": result["tool_call_id"],
148
+ "is_error": True,
149
+ "error": result["result"],
150
150
  }
151
151
  else:
152
152
  tool_response = {
153
- 'tool_call_id': result['tool_call_id'],
154
- 'is_error': False,
155
- 'tool_response': result['result']
153
+ "tool_call_id": result["tool_call_id"],
154
+ "is_error": False,
155
+ "tool_response": result["result"],
156
156
  }
157
-
157
+
158
158
  results.append(tool_response)
159
-
159
+
160
160
  # Add tool response back to messages using individual method
161
- if result['is_error']:
162
- messages.add_tool_response(result['tool_call_id'], result['result'])
163
- else:
164
- messages.add_tool_response(result['tool_call_id'], str(result['result']))
165
-
161
+ # if result['is_error']:
162
+ # messages.add_tool_response(result['tool_call_id'], result['result'])
163
+ # else:
164
+ # messages.add_tool_response(result['tool_call_id'], str(result['result']))
165
+
166
166
  return results
167
167
 
168
168
 
169
- def execute_and_add_tool_responses(messages, tool_registry: Dict[str, Any]) -> List[Dict[str, Any]]:
169
+ def execute_and_add_tool_responses(
170
+ messages, tool_registry: Dict[str, Any]
171
+ ) -> List[Dict[str, Any]]:
170
172
  """
171
173
  Execute all pending tool calls and add them using Messages.add_tool_responses batch method.
172
-
174
+
173
175
  Args:
174
176
  messages: Messages instance with pending tool calls
175
177
  tool_registry: Registry mapping tool names to tool instances
176
-
178
+
177
179
  Returns:
178
180
  List of execution results compatible with Messages.add_tool_responses format
179
181
  """
180
182
  pending_tool_calls = messages.get_pending_tool_calls()
181
183
  results = []
182
-
184
+
183
185
  for tool_call in pending_tool_calls:
184
186
  result = execute_tool_call(tool_call, tool_registry)
185
-
187
+
186
188
  # Convert to format expected by Messages.add_tool_responses
187
- if result['is_error']:
189
+ if result["is_error"]:
188
190
  tool_response = {
189
- 'tool_call_id': result['tool_call_id'],
190
- 'is_error': True,
191
- 'error': result['result']
191
+ "tool_call_id": result["tool_call_id"],
192
+ "is_error": True,
193
+ "error": result["result"],
192
194
  }
193
195
  else:
194
196
  tool_response = {
195
- 'tool_call_id': result['tool_call_id'],
196
- 'is_error': False,
197
- 'tool_response': result['result']
197
+ "tool_call_id": result["tool_call_id"],
198
+ "is_error": False,
199
+ "tool_response": result["result"],
198
200
  }
199
-
201
+
200
202
  results.append(tool_response)
201
-
203
+
202
204
  # Add all responses at once using the batch method
203
205
  if results:
204
206
  messages.add_tool_responses(results)
205
-
206
- return results
207
+
208
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agentic-blocks
3
- Version: 0.1.13
3
+ Version: 0.1.15
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=eyqPZRNMTAURuau64ohNCsmxlNM2cOmnmTAYGbZYk8A,7003
6
- agentic_blocks-0.1.13.dist-info/licenses/LICENSE,sha256=r4IcBaAjTv3-yfjXgDPuRD953Qci0Y0nQn5JfHwLyBY,1073
7
- agentic_blocks-0.1.13.dist-info/METADATA,sha256=0l7qEy5vcodKloGqlhpMKMKqAakpjabU7kL0ZM-mOvE,11944
8
- agentic_blocks-0.1.13.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- agentic_blocks-0.1.13.dist-info/top_level.txt,sha256=-1a4RAemqicXLU1rRzw4QHV3KlNeQDNxVs3m2gAT238,15
10
- agentic_blocks-0.1.13.dist-info/RECORD,,
5
+ agentic_blocks/utils/tools_utils.py,sha256=8py7_4AW_UDniGJVD9oQbS1wD5j_LP2ZZXsxOf2qppo,6816
6
+ agentic_blocks-0.1.15.dist-info/licenses/LICENSE,sha256=r4IcBaAjTv3-yfjXgDPuRD953Qci0Y0nQn5JfHwLyBY,1073
7
+ agentic_blocks-0.1.15.dist-info/METADATA,sha256=azBIAKivWA7RKJq_vJP-gwt3gPuAqYrcjJqtWb0A04E,11944
8
+ agentic_blocks-0.1.15.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
+ agentic_blocks-0.1.15.dist-info/top_level.txt,sha256=-1a4RAemqicXLU1rRzw4QHV3KlNeQDNxVs3m2gAT238,15
10
+ agentic_blocks-0.1.15.dist-info/RECORD,,