forked from universal-tool-calling-protocol/code-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_mode_client.py
More file actions
482 lines (390 loc) 路 17.1 KB
/
Copy pathcode_mode_client.py
File metadata and controls
482 lines (390 loc) 路 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
"""
CodeModeClient - Python implementation for executing Python code with UTCP tool access.
"""
import asyncio
import io
import json
import re
import sys
from contextlib import redirect_stdout, redirect_stderr
from typing import Any, Dict, List, Optional, Callable
from collections.abc import Coroutine
class Tool:
"""Represents a UTCP tool definition."""
def __init__(self, name: str, description: str, inputs: Dict, outputs: Dict, tags: List[str], tool_call_template: Dict):
self.name = name
self.description = description
self.inputs = inputs
self.outputs = outputs
self.tags = tags
self.tool_call_template = tool_call_template
class CodeModeClient:
"""
CodeModeClient provides Python code execution capabilities with UTCP tool access.
This allows executing Python code that can directly call registered tools as functions.
"""
AGENT_PROMPT_TEMPLATE = """
## UTCP CodeMode Tool Usage Guide (Python)
You have access to a CodeModeClient that allows you to execute Python code with access to registered tools. Follow this workflow:
### 1. Tool Discovery Phase
**Always start by discovering available tools:**
- Tools are organized by manual namespace (e.g., `manual_name.tool_name`)
- Use hierarchical access patterns: `await manual.tool(param=value)`
- Multiple manuals can contain tools with the same name - namespaces prevent conflicts
### 2. Interface Introspection
**Understand tool contracts before using them:**
- Access `__interfaces` to see all available Python interface definitions
- Use `__get_tool_interface('manual.tool')` to get specific tool interfaces
- Interfaces show required inputs, expected outputs, and descriptions
- Look for "Access as: manual.tool(args)" comments for usage patterns
### 3. Code Execution Guidelines
**When writing code for `call_tool_chain`:**
- Use `await manual.tool(param=value)` syntax for all tool calls
- Tools are async functions that return dictionaries
- You have access to standard Python built-ins: `print`, `json`, `math`, `datetime`, etc.
- All print output is automatically captured and returned
- Build properly structured input objects based on interface definitions
- Handle errors appropriately with try/except blocks
- Chain tool calls by using results from previous calls
### 4. Best Practices
- **Discover first, code second**: Always explore available tools before writing execution code
- **Respect namespaces**: Use full `manual.tool` names to avoid conflicts
- **Parse interfaces**: Use interface information to construct proper input objects
- **Error handling**: Wrap tool calls in try/except for robustness
- **Data flow**: Chain tools by passing outputs as inputs to subsequent tools
### 5. Available Runtime Context
- `__interfaces`: String containing all Python interface definitions
- `__get_tool_interface(tool_name)`: Function to get specific tool interface
- All registered tools as `manual.tool` functions
- Standard Python built-ins for data processing
Remember: Always discover and understand available tools before attempting to use them in code execution.
""".strip()
def __init__(self):
"""Initialize the CodeModeClient."""
self._tools: List[Tool] = []
self._tool_functions: Dict[str, Callable] = {}
self._tool_interface_cache: Dict[str, str] = {}
@classmethod
def create(cls, config: Optional[Dict] = None) -> "CodeModeClient":
"""
Create a new CodeModeClient instance.
Args:
config: Optional configuration dictionary
Returns:
A new CodeModeClient instance
"""
client = cls()
# In a real implementation, this would initialize UTCP SDK connection
# For now, we provide the structure
return client
def register_manual(self, manual_config: Dict) -> Dict[str, Any]:
"""
Register a UTCP manual with tools.
Args:
manual_config: Manual configuration dictionary
Returns:
Registration result with success status
"""
# In a real implementation, this would register with UTCP SDK
# For now, we provide the structure
return {"success": True, "errors": []}
def add_tool(self, tool: Tool, tool_function: Callable):
"""
Add a tool to the client.
Args:
tool: Tool definition
tool_function: Async function to call for this tool
"""
self._tools.append(tool)
self._tool_functions[tool.name] = tool_function
async def get_tools(self) -> List[Tool]:
"""Get all registered tools."""
return self._tools
async def call_tool(self, tool_name: str, args: Dict[str, Any]) -> Any:
"""
Call a registered tool by name.
Args:
tool_name: Name of the tool to call
args: Arguments to pass to the tool
Returns:
Tool execution result
"""
if tool_name not in self._tool_functions:
raise ValueError(f"Tool '{tool_name}' not found")
tool_func = self._tool_functions[tool_name]
return await tool_func(args)
@staticmethod
def _sanitize_identifier(name: str) -> str:
"""
Sanitize an identifier to be a valid Python identifier.
Args:
name: The name to sanitize
Returns:
Sanitized identifier
"""
# Replace non-alphanumeric characters with underscore
sanitized = re.sub(r'[^a-zA-Z0-9_]', '_', name)
# Ensure first character is not a number
if sanitized and sanitized[0].isdigit():
sanitized = '_' + sanitized
return sanitized
def tool_to_python_interface(self, tool: Tool) -> str:
"""
Convert a Tool object into a Python interface string.
Args:
tool: The Tool object to convert
Returns:
Python interface as a string
"""
if tool.name in self._tool_interface_cache:
return self._tool_interface_cache[tool.name]
# Generate hierarchical interface structure
if '.' in tool.name:
parts = tool.name.split('.')
manual_name = self._sanitize_identifier(parts[0])
tool_name = '_'.join(self._sanitize_identifier(p) for p in parts[1:])
access_pattern = f"{manual_name}.{tool_name}"
# Generate interface content
input_interface = self._json_schema_to_python_docstring(tool.inputs, "Args")
output_interface = self._json_schema_to_python_docstring(tool.outputs, "Returns")
interface_content = f"""
class {manual_name}:
async def {tool_name}(self, **kwargs) -> dict:
\"\"\"
{tool.description}
{input_interface}
{output_interface}
Tags: {', '.join(tool.tags)}
\"\"\"
pass
"""
else:
# No manual namespace
sanitized_tool_name = self._sanitize_identifier(tool.name)
access_pattern = sanitized_tool_name
input_interface = self._json_schema_to_python_docstring(tool.inputs, "Args")
output_interface = self._json_schema_to_python_docstring(tool.outputs, "Returns")
interface_content = f"""
async def {sanitized_tool_name}(**kwargs) -> dict:
\"\"\"
{tool.description}
{input_interface}
{output_interface}
Tags: {', '.join(tool.tags)}
\"\"\"
pass
"""
interface_string = f"{interface_content}\n# Access as: {access_pattern}(**kwargs)"
self._tool_interface_cache[tool.name] = interface_string
return interface_string
def _json_schema_to_python_docstring(self, schema: Dict, section_name: str) -> str:
"""
Convert JSON Schema to Python docstring format.
Args:
schema: JSON Schema dictionary
section_name: Section name (e.g., "Args", "Returns")
Returns:
Docstring section as string
"""
if not schema or schema.get('type') != 'object':
return f"{section_name}:\n Any"
properties = schema.get('properties', {})
required = schema.get('required', [])
if not properties:
return f"{section_name}:\n dict: Any dictionary"
lines = [f"{section_name}:"]
for prop_name, prop_schema in properties.items():
is_required = prop_name in required
description = prop_schema.get('description', '')
py_type = self._json_type_to_python(prop_schema)
required_marker = '' if is_required else ' (optional)'
lines.append(f" {prop_name} ({py_type}){required_marker}: {description}")
return '\n'.join(lines)
def _json_type_to_python(self, schema: Dict) -> str:
"""Convert JSON Schema type to Python type hint."""
schema_type = schema.get('type')
if schema.get('enum'):
return 'str' # Simplified
type_map = {
'string': 'str',
'number': 'float',
'integer': 'int',
'boolean': 'bool',
'array': 'list',
'object': 'dict',
'null': 'None'
}
if isinstance(schema_type, list):
return ' | '.join(type_map.get(t, 'Any') for t in schema_type)
return type_map.get(schema_type, 'Any')
async def get_all_tools_python_interfaces(self) -> str:
"""
Convert all registered tools to Python interface definitions.
Returns:
Complete Python interface definition string
"""
tools = await self.get_tools()
interfaces = [self.tool_to_python_interface(tool) for tool in tools]
header = "# Auto-generated Python interfaces for UTCP tools\n"
return header + '\n\n'.join(interfaces)
async def call_tool_chain(self, code: str, timeout: int = 30) -> Dict[str, Any]:
"""
Execute Python code with access to registered tools and capture output.
Args:
code: Python code to execute
timeout: Optional timeout in seconds (default: 30)
Returns:
Dictionary containing 'result' and 'logs' keys
"""
tools = await self.get_tools()
# Create execution context
logs = []
context = await self._create_execution_context(tools, logs)
try:
# Execute with timeout
result = await asyncio.wait_for(
self._run_code(code, context, logs),
timeout=timeout
)
return {"result": result, "logs": logs}
except asyncio.TimeoutError:
error_msg = f"Code execution timed out after {timeout}s"
return {"result": None, "logs": logs + [f"[ERROR] {error_msg}"]}
except Exception as error:
error_msg = f"Code execution failed: {str(error)}"
return {"result": None, "logs": logs + [f"[ERROR] {error_msg}"]}
async def _run_code(self, code: str, context: Dict[str, Any], logs: List[str]) -> Any:
"""
Run Python code in the execution context.
Args:
code: Python code to execute
context: Execution context dictionary
logs: List to append logs to
Returns:
Execution result
"""
# Capture stdout and stderr
stdout_capture = io.StringIO()
stderr_capture = io.StringIO()
# Prepare the code for execution
# Wrap in async function to support await
wrapped_code = f"""
async def __code_mode_exec():
{self._indent_code(code, 4)}
"""
try:
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
# Execute the wrapped async function definition
exec(wrapped_code, context)
# Now call and await the function
result = await context['__code_mode_exec']()
context['__result'] = result
# Capture any print output
stdout_content = stdout_capture.getvalue()
if stdout_content:
logs.extend(stdout_content.rstrip().split('\n'))
stderr_content = stderr_capture.getvalue()
if stderr_content:
logs.extend(['[ERROR] ' + line for line in stderr_content.rstrip().split('\n')])
return context.get('__result')
except Exception as e:
# Capture error in stderr
stderr_content = stderr_capture.getvalue()
if stderr_content:
logs.extend(['[ERROR] ' + line for line in stderr_content.rstrip().split('\n')])
raise
def _indent_code(self, code: str, spaces: int) -> str:
"""Indent code by specified number of spaces."""
indent = ' ' * spaces
return '\n'.join(indent + line if line.strip() else line
for line in code.split('\n'))
async def _create_execution_context(self, tools: List[Tool], logs: List[str]) -> Dict[str, Any]:
"""
Create the execution context for running Python code.
Args:
tools: Array of tools to make available
logs: List to capture console output
Returns:
Execution context dictionary
"""
# Custom print function that captures output
def captured_print(*args, **kwargs):
output = io.StringIO()
kwargs['file'] = output
print(*args, **kwargs)
log_line = output.getvalue().rstrip()
if log_line:
logs.append(log_line)
# Build context with basic Python utilities
context = {
# Built-ins
'print': captured_print,
'len': len,
'range': range,
'enumerate': enumerate,
'zip': zip,
'map': map,
'filter': filter,
'sum': sum,
'min': min,
'max': max,
'abs': abs,
'round': round,
'sorted': sorted,
'reversed': reversed,
'list': list,
'dict': dict,
'set': set,
'tuple': tuple,
'str': str,
'int': int,
'float': float,
'bool': bool,
# Modules
'json': json,
'asyncio': asyncio,
# Interface introspection
'__interfaces': await self.get_all_tools_python_interfaces(),
'__get_tool_interface': lambda tool_name: next(
(self.tool_to_python_interface(t) for t in tools if t.name == tool_name),
None
),
}
# Add tool functions organized by manual name
for tool in tools:
if '.' in tool.name:
parts = tool.name.split('.')
manual_name = self._sanitize_identifier(parts[0])
tool_name = '_'.join(self._sanitize_identifier(p) for p in parts[1:])
# Create manual namespace object if it doesn't exist
if manual_name not in context:
# Create a simple class to hold tool methods
context[manual_name] = type(manual_name, (), {})()
# Create the tool function
async def make_tool_func(t_name=tool.name):
async def tool_func(**kwargs):
try:
return await self.call_tool(t_name, kwargs)
except Exception as error:
raise RuntimeError(f"Error calling tool '{t_name}': {str(error)}")
return tool_func
# Add tool method to namespace
setattr(context[manual_name], tool_name, await make_tool_func())
else:
# No namespace, add directly to context
sanitized_tool_name = self._sanitize_identifier(tool.name)
async def make_tool_func(t_name=tool.name):
async def tool_func(**kwargs):
try:
return await self.call_tool(t_name, kwargs)
except Exception as error:
raise RuntimeError(f"Error calling tool '{t_name}': {str(error)}")
return tool_func
context[sanitized_tool_name] = await make_tool_func()
return context
async def close(self):
"""Close the client and cleanup resources."""
# In a real implementation, this would close UTCP SDK connections
self._tools.clear()
self._tool_functions.clear()
self._tool_interface_cache.clear()