tinyagent-py 0.0.1__py3-none-any.whl → 0.0.3__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.
- hooks/__init__.py +4 -0
- hooks/agno_storage_hook.py +128 -0
- hooks/gradio_callback.py +966 -0
- hooks/logging_manager.py +213 -0
- hooks/rich_ui_callback.py +559 -0
- storage/__init__.py +7 -0
- storage/agno_storage.py +114 -0
- storage/base.py +49 -0
- storage/json_file_storage.py +30 -0
- storage/postgres_storage.py +201 -0
- storage/redis_storage.py +48 -0
- storage/sqlite_storage.py +156 -0
- tinyagent_py-0.0.3.dist-info/METADATA +207 -0
- tinyagent_py-0.0.3.dist-info/RECORD +17 -0
- {tinyagent_py-0.0.1.dist-info → tinyagent_py-0.0.3.dist-info}/WHEEL +1 -1
- tinyagent_py-0.0.3.dist-info/top_level.txt +2 -0
- tinyagent/__init__.py +0 -4
- tinyagent/mcp_client.py +0 -52
- tinyagent/tiny_agent.py +0 -247
- tinyagent_py-0.0.1.dist-info/METADATA +0 -79
- tinyagent_py-0.0.1.dist-info/RECORD +0 -8
- tinyagent_py-0.0.1.dist-info/top_level.txt +0 -1
- {tinyagent_py-0.0.1.dist-info → tinyagent_py-0.0.3.dist-info}/licenses/LICENSE +0 -0
hooks/__init__.py
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
try:
|
2
|
+
import agno
|
3
|
+
from agno.storage.postgres import PostgresStorage
|
4
|
+
from agno.storage.sqlite import SqliteStorage
|
5
|
+
from agno.storage.session.agent import AgentSession
|
6
|
+
except ImportError as e:
|
7
|
+
raise ImportError("agno is not installed. Please install it with `pip install agno`.", e)
|
8
|
+
|
9
|
+
import asyncio
|
10
|
+
from typing import Optional
|
11
|
+
from agno.storage.postgres import PostgresStorage
|
12
|
+
from agno.storage.sqlite import SqliteStorage
|
13
|
+
from agno.storage.session.agent import AgentSession
|
14
|
+
|
15
|
+
class PostgresStorageHook:
|
16
|
+
def __init__(
|
17
|
+
self,
|
18
|
+
table_name: str,
|
19
|
+
db_url: Optional[str] = None,
|
20
|
+
db_engine=None,
|
21
|
+
schema: Optional[str] = "ai",
|
22
|
+
schema_version: int = 1,
|
23
|
+
auto_upgrade_schema: bool = True,
|
24
|
+
mode: str = "agent",
|
25
|
+
):
|
26
|
+
self.storage = PostgresStorage(
|
27
|
+
table_name=table_name,
|
28
|
+
db_url=db_url,
|
29
|
+
db_engine=db_engine,
|
30
|
+
schema=schema,
|
31
|
+
schema_version=schema_version,
|
32
|
+
auto_upgrade_schema=auto_upgrade_schema,
|
33
|
+
mode=mode,
|
34
|
+
)
|
35
|
+
|
36
|
+
async def __call__(self, event_name: str, agent, **kwargs):
|
37
|
+
if event_name == "agent_start":
|
38
|
+
# Load session from storage
|
39
|
+
session_id = getattr(agent, "session_id", None)
|
40
|
+
user_id = getattr(agent, "user_id", None)
|
41
|
+
if session_id:
|
42
|
+
session = self.storage.read(session_id=session_id, user_id=user_id)
|
43
|
+
if session:
|
44
|
+
# Populate agent state from session
|
45
|
+
agent.messages = session.session_data.get("messages", [])
|
46
|
+
agent.memory = session.memory
|
47
|
+
agent.metadata = session.extra_data
|
48
|
+
# You may need to adapt this depending on tinyagent's state structure
|
49
|
+
|
50
|
+
elif event_name in ("llm_end", "agent_end"):
|
51
|
+
# Save session to storage
|
52
|
+
session_id = getattr(agent, "session_id", None)
|
53
|
+
user_id = getattr(agent, "user_id", None)
|
54
|
+
if session_id:
|
55
|
+
# Create AgentSession from agent state
|
56
|
+
session_data = {
|
57
|
+
"messages": getattr(agent, "messages", []),
|
58
|
+
}
|
59
|
+
session = AgentSession(
|
60
|
+
session_id=session_id,
|
61
|
+
user_id=user_id,
|
62
|
+
memory=getattr(agent, "memory", {}),
|
63
|
+
session_data=session_data,
|
64
|
+
extra_data=getattr(agent, "metadata", {}),
|
65
|
+
agent_id=getattr(agent, "agent_id", None),
|
66
|
+
team_session_id=None,
|
67
|
+
agent_data=None,
|
68
|
+
)
|
69
|
+
await asyncio.to_thread(self.storage.upsert, session)
|
70
|
+
|
71
|
+
class SqliteStorageHook:
|
72
|
+
def __init__(
|
73
|
+
self,
|
74
|
+
table_name: str,
|
75
|
+
db_url: Optional[str] = None,
|
76
|
+
db_file: Optional[str] = None,
|
77
|
+
db_engine=None,
|
78
|
+
schema_version: int = 1,
|
79
|
+
auto_upgrade_schema: bool = True,
|
80
|
+
mode: str = "agent",
|
81
|
+
):
|
82
|
+
self.storage = SqliteStorage(
|
83
|
+
table_name=table_name,
|
84
|
+
db_url=db_url,
|
85
|
+
db_file=db_file,
|
86
|
+
db_engine=db_engine,
|
87
|
+
schema_version=schema_version,
|
88
|
+
auto_upgrade_schema=auto_upgrade_schema,
|
89
|
+
mode=mode,
|
90
|
+
)
|
91
|
+
|
92
|
+
async def __call__(self, event_name: str, agent, **kwargs):
|
93
|
+
if event_name == "agent_start":
|
94
|
+
# Load session from storage
|
95
|
+
session_id = getattr(agent, "session_id", None)
|
96
|
+
user_id = getattr(agent, "user_id", None)
|
97
|
+
print("Session ID",session_id)
|
98
|
+
print("User ID",user_id)
|
99
|
+
if session_id:
|
100
|
+
session = self.storage.read(session_id=session_id, user_id=user_id)
|
101
|
+
print(f"Session: {session}")
|
102
|
+
if session:
|
103
|
+
# Populate agent state from session
|
104
|
+
agent.messages = session.memory.get("messages", [])
|
105
|
+
agent.memory = session.memory
|
106
|
+
agent.metadata = session.extra_data
|
107
|
+
|
108
|
+
elif event_name in ("llm_end", "agent_end"):
|
109
|
+
# Save session to storage
|
110
|
+
print("Agent metadata",getattr(agent, "metadata", {}))
|
111
|
+
session_id = getattr(agent, "session_id", None)
|
112
|
+
user_id = getattr(agent, "user_id", None)
|
113
|
+
if session_id:
|
114
|
+
session_data = {
|
115
|
+
"messages": getattr(agent, "messages", []),
|
116
|
+
}
|
117
|
+
session = AgentSession(
|
118
|
+
session_id=session_id,
|
119
|
+
user_id=user_id,
|
120
|
+
memory=getattr(agent, "memory", {}),
|
121
|
+
session_data=session_data,
|
122
|
+
extra_data=getattr(agent, "metadata", {}),
|
123
|
+
agent_id=getattr(agent, "agent_id", None),
|
124
|
+
team_session_id=None,
|
125
|
+
agent_data=None,
|
126
|
+
)
|
127
|
+
await asyncio.to_thread(self.storage.upsert, session)
|
128
|
+
|