tinyagent-py 0.0.8__py3-none-any.whl → 0.0.9__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.
@@ -0,0 +1,120 @@
1
+ import sys
2
+ import cloudpickle
3
+ from typing import Dict, Any
4
+
5
+
6
+ def clean_response(resp: Dict[str, Any]) -> Dict[str, Any]:
7
+ """
8
+ Clean the response from code execution, keeping only relevant fields.
9
+
10
+ Args:
11
+ resp: Raw response dictionary from code execution
12
+
13
+ Returns:
14
+ Cleaned response with only essential fields
15
+ """
16
+ return {k: v for k, v in resp.items() if k in ['printed_output', 'return_value', 'stderr', 'error_traceback']}
17
+
18
+
19
+ def make_session_blob(ns: dict) -> bytes:
20
+ """
21
+ Create a serialized blob of the session namespace, excluding unserializable objects.
22
+
23
+ Args:
24
+ ns: Namespace dictionary to serialize
25
+
26
+ Returns:
27
+ Serialized bytes of the clean namespace
28
+ """
29
+ clean = {}
30
+ for name, val in ns.items():
31
+ try:
32
+ # Try serializing just this one object
33
+ cloudpickle.dumps(val)
34
+ except Exception:
35
+ # drop anything that fails
36
+ continue
37
+ else:
38
+ clean[name] = val
39
+
40
+ return cloudpickle.dumps(clean)
41
+
42
+
43
+ def _run_python(code: str, globals_dict: Dict[str, Any] = None, locals_dict: Dict[str, Any] = None):
44
+ """
45
+ Execute Python code in a controlled environment with proper error handling.
46
+
47
+ Args:
48
+ code: Python code to execute
49
+ globals_dict: Global variables dictionary
50
+ locals_dict: Local variables dictionary
51
+
52
+ Returns:
53
+ Dictionary containing execution results
54
+ """
55
+ import contextlib
56
+ import traceback
57
+ import io
58
+ import ast
59
+
60
+ # Make copies to avoid mutating the original parameters
61
+ globals_dict = globals_dict or {}
62
+ locals_dict = locals_dict or {}
63
+ updated_globals = globals_dict.copy()
64
+ updated_locals = locals_dict.copy()
65
+
66
+ # Pre-import essential modules into the global namespace
67
+ # This ensures they're available for imports inside functions
68
+ essential_modules = ['requests', 'json', 'os', 'sys', 'time', 'datetime', 're', 'random', 'math']
69
+
70
+ for module_name in essential_modules:
71
+ try:
72
+ module = __import__(module_name)
73
+ updated_globals[module_name] = module
74
+ #print(f"✓ {module_name} module loaded successfully")
75
+ except ImportError:
76
+ print(f"⚠️ Warning: {module_name} module not available")
77
+
78
+ tree = ast.parse(code, mode="exec")
79
+ compiled = compile(tree, filename="<ast>", mode="exec")
80
+ stdout_buf = io.StringIO()
81
+ stderr_buf = io.StringIO()
82
+
83
+ # Execute with stdout+stderr capture and exception handling
84
+ error_traceback = None
85
+ output = None
86
+
87
+ with contextlib.redirect_stdout(stdout_buf), contextlib.redirect_stderr(stderr_buf):
88
+ try:
89
+ # Merge all variables into globals to avoid scoping issues with generator expressions
90
+ # When exec() is called with both globals and locals, generator expressions can't
91
+ # access local variables. By using only globals, everything runs in global scope.
92
+ merged_globals = updated_globals.copy()
93
+ merged_globals.update(updated_locals)
94
+
95
+ # Execute with only globals - this fixes generator expression scoping issues
96
+ output = exec(code, merged_globals)
97
+
98
+ # Update both dictionaries with any new variables created during execution
99
+ for key, value in merged_globals.items():
100
+ if key not in updated_globals and key not in updated_locals:
101
+ updated_locals[key] = value
102
+ elif key in updated_locals or key not in updated_globals:
103
+ updated_locals[key] = value
104
+ updated_globals[key] = value
105
+ except Exception:
106
+ # Capture the full traceback as a string
107
+ error_traceback = traceback.format_exc()
108
+
109
+ printed_output = stdout_buf.getvalue()
110
+ stderr_output = stderr_buf.getvalue()
111
+ error_traceback_output = error_traceback
112
+
113
+ return {
114
+ "printed_output": printed_output,
115
+ "return_value": output,
116
+ "stderr": stderr_output,
117
+ "error_traceback": error_traceback_output,
118
+ "updated_globals": updated_globals,
119
+ "updated_locals": updated_locals
120
+ }
@@ -1,4 +1,5 @@
1
1
  #from .rich_ui_agent import RichUICallback
2
2
  from .rich_ui_callback import RichUICallback
3
+ from .rich_code_ui_callback import RichCodeUICallback
3
4
  from .logging_manager import LoggingManager
4
- __all__ = ["RichUICallback", "LoggingManager"]
5
+ __all__ = ["RichUICallback", "RichCodeUICallback", "LoggingManager"]
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinyagent-py
3
- Version: 0.0.8
4
- Summary: Tiny Agent with MCP Client and Extendable Hooks, Tiny but powerful
3
+ Version: 0.0.9
4
+ Summary: TinyAgent with MCP Client, Code Agent (Thinking, Planning, and Executing in Python), and Extendable Hooks, Tiny but powerful
5
5
  Author-email: Mahdi Golchin <golchin@askdev.ai>
6
6
  Project-URL: Homepage, https://github.com/askbudi/tinyagent
7
7
  Project-URL: Bug Tracker, https://github.com/askbudi/tinyagent/issues
@@ -25,10 +25,19 @@ Provides-Extra: sqlite
25
25
  Requires-Dist: aiosqlite>=0.18.0; extra == "sqlite"
26
26
  Provides-Extra: gradio
27
27
  Requires-Dist: gradio>=3.50.0; extra == "gradio"
28
+ Provides-Extra: code
29
+ Requires-Dist: jinja2; extra == "code"
30
+ Requires-Dist: pyyaml; extra == "code"
31
+ Requires-Dist: cloudpickle; extra == "code"
32
+ Requires-Dist: modal; extra == "code"
28
33
  Provides-Extra: all
29
34
  Requires-Dist: asyncpg>=0.27.0; extra == "all"
30
35
  Requires-Dist: aiosqlite>=0.18.0; extra == "all"
31
36
  Requires-Dist: gradio>=3.50.0; extra == "all"
37
+ Requires-Dist: jinja2; extra == "all"
38
+ Requires-Dist: pyyaml; extra == "all"
39
+ Requires-Dist: cloudpickle; extra == "all"
40
+ Requires-Dist: modal; extra == "all"
32
41
  Dynamic: license-file
33
42
 
34
43
  # TinyAgent
@@ -52,7 +61,11 @@ Inspired by:
52
61
  - [Build your own Tiny Agent](https://askdev.ai/github/askbudi/tinyagent)
53
62
 
54
63
  ## Overview
55
- This is a tiny agent that uses MCP and LiteLLM to interact with a model. You have full control over the agent, you can add any tools you like from MCP and extend the agent using its event system.
64
+ This is a tiny agent framework that uses MCP and LiteLLM to interact with language models. You have full control over the agent, you can add any tools you like from MCP and extend the agent using its event system.
65
+
66
+ **Two Main Components:**
67
+ - **TinyAgent**: Core agent with MCP tool integration and extensible hooks
68
+ - **TinyCodeAgent**: Specialized agent for secure Python code execution with pluggable providers
56
69
 
57
70
  ## Installation
58
71
 
@@ -64,6 +77,10 @@ pip install tinyagent-py
64
77
  # Install with all optional dependencies
65
78
  pip install tinyagent-py[all]
66
79
 
80
+ # Install with Code Agent support
81
+ pip install tinyagent-py[code]
82
+
83
+
67
84
  # Install with PostgreSQL support
68
85
  pip install tinyagent-py[postgres]
69
86
 
@@ -73,6 +90,10 @@ pip install tinyagent-py[sqlite]
73
90
  # Install with Gradio UI support
74
91
  pip install tinyagent-py[gradio]
75
92
 
93
+
94
+
95
+
96
+
76
97
  ```
77
98
 
78
99
  ### Using uv
@@ -80,6 +101,10 @@ pip install tinyagent-py[gradio]
80
101
  # Basic installation
81
102
  uv pip install tinyagent-py
82
103
 
104
+ # Install with Code Agent support
105
+ uv pip install tinyagent-py[code]
106
+
107
+
83
108
  # Install with PostgreSQL support
84
109
  uv pip install tinyagent-py[postgres]
85
110
 
@@ -92,11 +117,11 @@ uv pip install tinyagent-py[gradio]
92
117
  # Install with all optional dependencies
93
118
  uv pip install tinyagent-py[all]
94
119
 
95
- # Install with development tools
96
- uv pip install tinyagent-py[dev]
97
120
  ```
98
121
 
99
122
  ## Usage
123
+
124
+ ### TinyAgent (Core Agent)
100
125
  [![AskDev.AI | Chat with TinyAgent](https://img.shields.io/badge/AskDev.AI-Chat_with_TinyAgent-blue?style=flat-square)](https://askdev.ai/github/askbudi/tinyagent)
101
126
 
102
127
 
@@ -133,6 +158,114 @@ I need accommodation in Toronto between 15th to 20th of May. Give me 5 options f
133
158
  await test_agent(task, model="gpt-4.1-mini")
134
159
  ```
135
160
 
161
+ ## TinyCodeAgent - Code Execution Made Easy
162
+
163
+ TinyCodeAgent is a specialized agent for executing Python code with enterprise-grade reliability and extensible execution providers.
164
+
165
+ ### Quick Start with TinyCodeAgent
166
+
167
+ ```python
168
+ import asyncio
169
+ from tinyagent import TinyCodeAgent
170
+
171
+ async def main():
172
+ # Initialize with minimal configuration
173
+ agent = TinyCodeAgent(
174
+ model="gpt-4.1-mini",
175
+ api_key="your-openai-api-key"
176
+ )
177
+
178
+ try:
179
+ # Ask the agent to solve a coding problem
180
+ result = await agent.run("Calculate the factorial of 10 and explain the algorithm")
181
+ print(result)
182
+ finally:
183
+ await agent.close()
184
+
185
+ asyncio.run(main())
186
+ ```
187
+
188
+ ### TinyCodeAgent with Gradio UI
189
+
190
+ Launch a complete web interface for interactive code execution:
191
+
192
+ ```python
193
+ from tinyagent.code_agent.example import run_example
194
+ import asyncio
195
+
196
+ # Run the full example with Gradio interface
197
+ asyncio.run(run_example())
198
+ ```
199
+
200
+ ### Key Features
201
+
202
+ - **🔒 Secure Execution**: Sandboxed Python code execution using Modal.com or other providers
203
+ - **🔧 Extensible Providers**: Switch between Modal, Docker, local execution, or cloud functions
204
+ - **🎯 Built for Enterprise**: Production-ready with proper logging, error handling, and resource cleanup
205
+ - **📁 File Support**: Upload and process files through the Gradio interface
206
+ - **🛠️ Custom Tools**: Add your own tools and functions easily
207
+ - **📊 Session Persistence**: Code state persists across executions
208
+
209
+ ### Provider System
210
+
211
+ TinyCodeAgent uses a pluggable provider system - change execution backends with minimal code changes:
212
+
213
+ ```python
214
+ # Use Modal (default) - great for production
215
+ agent = TinyCodeAgent(provider="modal")
216
+
217
+ # Future providers (coming soon)
218
+ # agent = TinyCodeAgent(provider="docker")
219
+ # agent = TinyCodeAgent(provider="local")
220
+ # agent = TinyCodeAgent(provider="lambda")
221
+ ```
222
+
223
+ ### Example Use Cases
224
+
225
+ **Web Scraping:**
226
+ ```python
227
+ result = await agent.run("""
228
+ What are trending spaces on huggingface today?
229
+ """)
230
+ # Agent will create a python tool to request HuggingFace API and find trending spaces
231
+ ```
232
+
233
+ **Use code to solve a task:**
234
+ ```python
235
+ response = await agent.run(dedent("""
236
+ Suggest me 13 tags for my Etsy Listing, each tag should be multiworded and maximum 20 characters. Each word should be used only once in the whole corpus, And tags should cover different ways people are searching for the product on Etsy.
237
+ - You should use your coding abilities to check your answer pass the criteria and continue your job until you get to the answer.
238
+
239
+ My Product is **Wedding Invitation Set of 3, in sage green color, with a gold foil border.**
240
+ """),max_turns=20)
241
+
242
+ print(response)
243
+ # LLM is not good at this task, counting characters, avoid duplicates, but with the power of code, tiny model like gpt-4.1-mini can do it without any problem.
244
+ ```
245
+
246
+
247
+ ### Configuration Options
248
+
249
+ ```python
250
+ from tinyagent import TinyCodeAgent
251
+ from tinyagent.code_agent.tools import get_weather, get_traffic
252
+
253
+ # Full configuration example
254
+ agent = TinyCodeAgent(
255
+ model="gpt-4.1-mini",
256
+ api_key="your-api-key",
257
+ provider="modal",
258
+ tools=[get_weather, get_traffic],
259
+ authorized_imports=["requests", "pandas", "numpy"],
260
+ provider_config={
261
+ "pip_packages": ["requests", "pandas"],
262
+ "sandbox_name": "my-code-sandbox"
263
+ }
264
+ )
265
+ ```
266
+
267
+ For detailed documentation, see the [TinyCodeAgent README](tinyagent/code_agent/README.md).
268
+
136
269
  ## How the TinyAgent Hook System Works
137
270
 
138
271
  TinyAgent is designed to be **extensible** via a simple, event-driven hook (callback) system. This allows you to add custom logic, logging, UI, memory, or any other behavior at key points in the agent's lifecycle.
@@ -0,0 +1,31 @@
1
+ tinyagent/__init__.py,sha256=-3ZN8unMZDrA366BET1HKp-fnFCyXCAD1fPVbHkJSsY,172
2
+ tinyagent/mcp_client.py,sha256=9dmLtJ8CTwKWKTH6K9z8CaCQuaicOH9ifAuNyX7Kdo0,6030
3
+ tinyagent/memory_manager.py,sha256=tAaZZdxBJ235wJIyr04n3f2Damok4s2UXunTtur_p-4,44916
4
+ tinyagent/tiny_agent.py,sha256=_qHQc2yroSI1Ihn5Ex3FH_ml7aaSNC9-yR540C16Cxs,41125
5
+ tinyagent/code_agent/__init__.py,sha256=YSOblSwRS1QcAYUu--GvF4fKeQX1KRTj9P8CWySY3pY,327
6
+ tinyagent/code_agent/example.py,sha256=qC6i3auUT1YwXS9WK1Ovq-9oDOUgzRxDegYdlVcVcfA,5861
7
+ tinyagent/code_agent/helper.py,sha256=oZnpo-_H3cB12LxNN7Ztd-31EiUcuI2UpWP69xuF8oE,7205
8
+ tinyagent/code_agent/modal_sandbox.py,sha256=3FaTLESkdQmEO9A5dMmMOOkd0w2cnGGpfXngvojQxJ8,16317
9
+ tinyagent/code_agent/tiny_code_agent.py,sha256=D0stvCao0dvrpY2ChxGxf048SEUDW-yHgJ_9Aluw2Ek,23142
10
+ tinyagent/code_agent/utils.py,sha256=rEnrKv9l8knjUroBQhz6Q8q6ehCt9IAsJtM-m3ZMNjY,4231
11
+ tinyagent/code_agent/providers/__init__.py,sha256=myfy9qsBDjNOhcgXJ2E9jO1q5eo6jHp43I2k0k8esLY,136
12
+ tinyagent/code_agent/providers/base.py,sha256=Hm8jrD60QovgQHTwiFE1pKDHPk1cwGbUdzuSwic9Rjc,5832
13
+ tinyagent/code_agent/providers/modal_provider.py,sha256=myq0WKiNi8G0MpUUGo7olbGFVmcZ3iMa8XTmT537PZg,8576
14
+ tinyagent/code_agent/tools/__init__.py,sha256=0XtrgYBgBayOffW50KyrlmrXXs9iu6z1DHu7-D8WGqY,94
15
+ tinyagent/code_agent/tools/example_tools.py,sha256=YbXb7PKuvvxh-LV12Y4n_Ez3RyLA95gWOcZrKsa7UHg,1203
16
+ tinyagent/hooks/__init__.py,sha256=RZow2r0XHLJ3-tnmecScdc0_wrEdmOy5dtXqoiRME5Y,254
17
+ tinyagent/hooks/gradio_callback.py,sha256=TS28i4UUIEWsl_e75tRXyVTOKzH7at8nmIl8d3ZTYag,53493
18
+ tinyagent/hooks/logging_manager.py,sha256=UpdmpQ7HRPyer-jrmQSXcBwi409tV9LnGvXSHjTcYTI,7935
19
+ tinyagent/hooks/rich_code_ui_callback.py,sha256=PLcu5MOSoP4oZR3BtvcV9DquxcIT_d0WzSlkvaDcGOk,19820
20
+ tinyagent/hooks/rich_ui_callback.py,sha256=5iCNOiJmhc1lOL7ZjaOt5Sk3rompko4zu_pAxfTVgJQ,22897
21
+ tinyagent/storage/__init__.py,sha256=7qwfdD4smCl891xaRuiReSUgfOJFy7jJZsN0ul1iQdY,173
22
+ tinyagent/storage/base.py,sha256=GGAMvOoslmm1INLFG_jtwOkRk2Qg39QXx-1LnN7fxDI,1474
23
+ tinyagent/storage/json_file_storage.py,sha256=SYD8lvTHu2-FEHm1tZmsrcgEOirBrlUsUM186X-UPgI,1114
24
+ tinyagent/storage/postgres_storage.py,sha256=IGwan8UXHNnTZFK1F8x4kvMDex3GAAGWUg9ePx_5IF4,9018
25
+ tinyagent/storage/redis_storage.py,sha256=hu3y7wHi49HkpiR-AW7cWVQuTVOUk1WaB8TEPGUKVJ8,1742
26
+ tinyagent/storage/sqlite_storage.py,sha256=ZyOYe0d_oHO1wOIT8FxKIbc67tP_0e_8FnM2Zq8Pwj8,5915
27
+ tinyagent_py-0.0.9.dist-info/licenses/LICENSE,sha256=YIogcVQnknaaE4K-oaQylFWo8JGRBWnwmGb3fWB_Pww,1064
28
+ tinyagent_py-0.0.9.dist-info/METADATA,sha256=5yLZvD5bKtQWg0cORuKGme3XaOy_Cnz7zHx-6QUwJpA,13249
29
+ tinyagent_py-0.0.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
30
+ tinyagent_py-0.0.9.dist-info/top_level.txt,sha256=Ny8aJNchZpc2Vvhp3306L5vjceJakvFxBk-UjjVeA_I,10
31
+ tinyagent_py-0.0.9.dist-info/RECORD,,
@@ -1,20 +0,0 @@
1
- tinyagent/__init__.py,sha256=GrD21npMQGzl9ZYKYTP8VxHLzCfJCvA0oTKQZTkmnCw,117
2
- tinyagent/mcp_client.py,sha256=9dmLtJ8CTwKWKTH6K9z8CaCQuaicOH9ifAuNyX7Kdo0,6030
3
- tinyagent/memory_manager.py,sha256=tAaZZdxBJ235wJIyr04n3f2Damok4s2UXunTtur_p-4,44916
4
- tinyagent/tiny_agent.py,sha256=_qHQc2yroSI1Ihn5Ex3FH_ml7aaSNC9-yR540C16Cxs,41125
5
- tinyagent/hooks/__init__.py,sha256=UztCHjoqF5JyDolbWwkBsBZkWguDQg23l2GD_zMHt-s,178
6
- tinyagent/hooks/gradio_callback.py,sha256=TS28i4UUIEWsl_e75tRXyVTOKzH7at8nmIl8d3ZTYag,53493
7
- tinyagent/hooks/logging_manager.py,sha256=UpdmpQ7HRPyer-jrmQSXcBwi409tV9LnGvXSHjTcYTI,7935
8
- tinyagent/hooks/rich_code_ui_callback.py,sha256=PLcu5MOSoP4oZR3BtvcV9DquxcIT_d0WzSlkvaDcGOk,19820
9
- tinyagent/hooks/rich_ui_callback.py,sha256=5iCNOiJmhc1lOL7ZjaOt5Sk3rompko4zu_pAxfTVgJQ,22897
10
- tinyagent/storage/__init__.py,sha256=7qwfdD4smCl891xaRuiReSUgfOJFy7jJZsN0ul1iQdY,173
11
- tinyagent/storage/base.py,sha256=GGAMvOoslmm1INLFG_jtwOkRk2Qg39QXx-1LnN7fxDI,1474
12
- tinyagent/storage/json_file_storage.py,sha256=SYD8lvTHu2-FEHm1tZmsrcgEOirBrlUsUM186X-UPgI,1114
13
- tinyagent/storage/postgres_storage.py,sha256=IGwan8UXHNnTZFK1F8x4kvMDex3GAAGWUg9ePx_5IF4,9018
14
- tinyagent/storage/redis_storage.py,sha256=hu3y7wHi49HkpiR-AW7cWVQuTVOUk1WaB8TEPGUKVJ8,1742
15
- tinyagent/storage/sqlite_storage.py,sha256=ZyOYe0d_oHO1wOIT8FxKIbc67tP_0e_8FnM2Zq8Pwj8,5915
16
- tinyagent_py-0.0.8.dist-info/licenses/LICENSE,sha256=YIogcVQnknaaE4K-oaQylFWo8JGRBWnwmGb3fWB_Pww,1064
17
- tinyagent_py-0.0.8.dist-info/METADATA,sha256=NW-v0Ra2BWiKlI26hYma4lMaHxegGfkFwlR-2C04Qmg,9094
18
- tinyagent_py-0.0.8.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
- tinyagent_py-0.0.8.dist-info/top_level.txt,sha256=Ny8aJNchZpc2Vvhp3306L5vjceJakvFxBk-UjjVeA_I,10
20
- tinyagent_py-0.0.8.dist-info/RECORD,,