praisonaiagents 0.0.18__py3-none-any.whl → 0.0.20__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,7 +17,8 @@ from .main import (
17
17
  clean_triple_backticks,
18
18
  error_logs,
19
19
  register_display_callback,
20
- display_callbacks,
20
+ sync_display_callbacks,
21
+ async_display_callbacks,
21
22
  )
22
23
 
23
24
  __all__ = [
@@ -35,5 +36,6 @@ __all__ = [
35
36
  'clean_triple_backticks',
36
37
  'error_logs',
37
38
  'register_display_callback',
38
- 'display_callbacks',
39
+ 'sync_display_callbacks',
40
+ 'async_display_callbacks',
39
41
  ]
praisonaiagents/main.py CHANGED
@@ -12,6 +12,7 @@ from rich.text import Text
12
12
  from rich.markdown import Markdown
13
13
  from rich.logging import RichHandler
14
14
  from rich.live import Live
15
+ import asyncio
15
16
 
16
17
  LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
17
18
 
@@ -25,12 +26,49 @@ logging.basicConfig(
25
26
  # Global list to store error logs
26
27
  error_logs = []
27
28
 
28
- # Global callback registry
29
- display_callbacks = {}
29
+ # Separate registries for sync and async callbacks
30
+ sync_display_callbacks = {}
31
+ async_display_callbacks = {}
30
32
 
31
- def register_display_callback(display_type: str, callback_fn):
32
- """Register a callback function for a specific display type."""
33
- display_callbacks[display_type] = callback_fn
33
+ # At the top of the file, add display_callbacks to __all__
34
+ __all__ = [
35
+ 'error_logs',
36
+ 'register_display_callback',
37
+ 'sync_display_callbacks',
38
+ 'async_display_callbacks',
39
+ # ... other exports
40
+ ]
41
+
42
+ def register_display_callback(display_type: str, callback_fn, is_async: bool = False):
43
+ """Register a synchronous or asynchronous callback function for a specific display type.
44
+
45
+ Args:
46
+ display_type (str): Type of display event ('interaction', 'self_reflection', etc.)
47
+ callback_fn: The callback function to register
48
+ is_async (bool): Whether the callback is asynchronous
49
+ """
50
+ if is_async:
51
+ async_display_callbacks[display_type] = callback_fn
52
+ else:
53
+ sync_display_callbacks[display_type] = callback_fn
54
+
55
+ async def execute_callback(display_type: str, **kwargs):
56
+ """Execute both sync and async callbacks for a given display type.
57
+
58
+ Args:
59
+ display_type (str): Type of display event
60
+ **kwargs: Arguments to pass to the callback functions
61
+ """
62
+ # Execute synchronous callback if registered
63
+ if display_type in sync_display_callbacks:
64
+ callback = sync_display_callbacks[display_type]
65
+ loop = asyncio.get_event_loop()
66
+ await loop.run_in_executor(None, lambda: callback(**kwargs))
67
+
68
+ # Execute asynchronous callback if registered
69
+ if display_type in async_display_callbacks:
70
+ callback = async_display_callbacks[display_type]
71
+ await callback(**kwargs)
34
72
 
35
73
  def _clean_display_content(content: str, max_length: int = 20000) -> str:
36
74
  """Helper function to clean and truncate content for display."""
@@ -53,11 +91,10 @@ def _clean_display_content(content: str, max_length: int = 20000) -> str:
53
91
  return content.strip()
54
92
 
55
93
  def display_interaction(message, response, markdown=True, generation_time=None, console=None):
56
- """Display the interaction between user and assistant."""
94
+ """Synchronous version of display_interaction."""
57
95
  if console is None:
58
96
  console = Console()
59
97
 
60
- # Handle multimodal content (list)
61
98
  if isinstance(message, list):
62
99
  text_content = next((item["text"] for item in message if item["type"] == "text"), "")
63
100
  message = text_content
@@ -65,16 +102,16 @@ def display_interaction(message, response, markdown=True, generation_time=None,
65
102
  message = _clean_display_content(str(message))
66
103
  response = _clean_display_content(str(response))
67
104
 
68
- # Execute callback if registered
69
- if 'interaction' in display_callbacks:
70
- display_callbacks['interaction'](
105
+ # Execute synchronous callback if registered
106
+ if 'interaction' in sync_display_callbacks:
107
+ sync_display_callbacks['interaction'](
71
108
  message=message,
72
109
  response=response,
73
110
  markdown=markdown,
74
111
  generation_time=generation_time
75
112
  )
76
113
 
77
- # Existing display logic...
114
+ # Rest of the display logic...
78
115
  if generation_time:
79
116
  console.print(Text(f"Response generated in {generation_time:.1f}s", style="dim"))
80
117
 
@@ -93,8 +130,8 @@ def display_self_reflection(message: str, console=None):
93
130
  message = _clean_display_content(str(message))
94
131
 
95
132
  # Execute callback if registered
96
- if 'self_reflection' in display_callbacks:
97
- display_callbacks['self_reflection'](message=message)
133
+ if 'self_reflection' in sync_display_callbacks:
134
+ sync_display_callbacks['self_reflection'](message=message)
98
135
 
99
136
  console.print(Panel.fit(Text(message, style="bold yellow"), title="Self Reflection", border_style="magenta"))
100
137
 
@@ -106,8 +143,8 @@ def display_instruction(message: str, console=None):
106
143
  message = _clean_display_content(str(message))
107
144
 
108
145
  # Execute callback if registered
109
- if 'instruction' in display_callbacks:
110
- display_callbacks['instruction'](message=message)
146
+ if 'instruction' in sync_display_callbacks:
147
+ sync_display_callbacks['instruction'](message=message)
111
148
 
112
149
  console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
113
150
 
@@ -119,8 +156,8 @@ def display_tool_call(message: str, console=None):
119
156
  message = _clean_display_content(str(message))
120
157
 
121
158
  # Execute callback if registered
122
- if 'tool_call' in display_callbacks:
123
- display_callbacks['tool_call'](message=message)
159
+ if 'tool_call' in sync_display_callbacks:
160
+ sync_display_callbacks['tool_call'](message=message)
124
161
 
125
162
  console.print(Panel.fit(Text(message, style="bold cyan"), title="Tool Call", border_style="green"))
126
163
 
@@ -132,8 +169,8 @@ def display_error(message: str, console=None):
132
169
  message = _clean_display_content(str(message))
133
170
 
134
171
  # Execute callback if registered
135
- if 'error' in display_callbacks:
136
- display_callbacks['error'](message=message)
172
+ if 'error' in sync_display_callbacks:
173
+ sync_display_callbacks['error'](message=message)
137
174
 
138
175
  console.print(Panel.fit(Text(message, style="bold red"), title="Error", border_style="red"))
139
176
  error_logs.append(message)
@@ -150,8 +187,114 @@ def display_generating(content: str = "", start_time: Optional[float] = None):
150
187
  content = _clean_display_content(str(content))
151
188
 
152
189
  # Execute callback if registered
153
- if 'generating' in display_callbacks:
154
- display_callbacks['generating'](
190
+ if 'generating' in sync_display_callbacks:
191
+ sync_display_callbacks['generating'](
192
+ content=content,
193
+ elapsed_time=elapsed_str.strip() if elapsed_str else None
194
+ )
195
+
196
+ return Panel(Markdown(content), title=f"Generating...{elapsed_str}", border_style="green")
197
+
198
+ # Async versions with 'a' prefix
199
+ async def adisplay_interaction(message, response, markdown=True, generation_time=None, console=None):
200
+ """Async version of display_interaction."""
201
+ if console is None:
202
+ console = Console()
203
+
204
+ if isinstance(message, list):
205
+ text_content = next((item["text"] for item in message if item["type"] == "text"), "")
206
+ message = text_content
207
+
208
+ message = _clean_display_content(str(message))
209
+ response = _clean_display_content(str(response))
210
+
211
+ # Execute callbacks
212
+ await execute_callback(
213
+ 'interaction',
214
+ message=message,
215
+ response=response,
216
+ markdown=markdown,
217
+ generation_time=generation_time
218
+ )
219
+
220
+ # Rest of the display logic...
221
+ if generation_time:
222
+ console.print(Text(f"Response generated in {generation_time:.1f}s", style="dim"))
223
+
224
+ if markdown:
225
+ console.print(Panel.fit(Markdown(message), title="Message", border_style="cyan"))
226
+ console.print(Panel.fit(Markdown(response), title="Response", border_style="cyan"))
227
+ else:
228
+ console.print(Panel.fit(Text(message, style="bold green"), title="Message", border_style="cyan"))
229
+ console.print(Panel.fit(Text(response, style="bold blue"), title="Response", border_style="cyan"))
230
+
231
+ async def adisplay_self_reflection(message: str, console=None):
232
+ """Async version of display_self_reflection."""
233
+ if not message or not message.strip():
234
+ return
235
+ if console is None:
236
+ console = Console()
237
+ message = _clean_display_content(str(message))
238
+
239
+ if 'self_reflection' in async_display_callbacks:
240
+ await async_display_callbacks['self_reflection'](message=message)
241
+
242
+ console.print(Panel.fit(Text(message, style="bold yellow"), title="Self Reflection", border_style="magenta"))
243
+
244
+ async def adisplay_instruction(message: str, console=None):
245
+ """Async version of display_instruction."""
246
+ if not message or not message.strip():
247
+ return
248
+ if console is None:
249
+ console = Console()
250
+ message = _clean_display_content(str(message))
251
+
252
+ if 'instruction' in async_display_callbacks:
253
+ await async_display_callbacks['instruction'](message=message)
254
+
255
+ console.print(Panel.fit(Text(message, style="bold blue"), title="Instruction", border_style="cyan"))
256
+
257
+ async def adisplay_tool_call(message: str, console=None):
258
+ """Async version of display_tool_call."""
259
+ if not message or not message.strip():
260
+ return
261
+ if console is None:
262
+ console = Console()
263
+ message = _clean_display_content(str(message))
264
+
265
+ if 'tool_call' in async_display_callbacks:
266
+ await async_display_callbacks['tool_call'](message=message)
267
+
268
+ console.print(Panel.fit(Text(message, style="bold cyan"), title="Tool Call", border_style="green"))
269
+
270
+ async def adisplay_error(message: str, console=None):
271
+ """Async version of display_error."""
272
+ if not message or not message.strip():
273
+ return
274
+ if console is None:
275
+ console = Console()
276
+ message = _clean_display_content(str(message))
277
+
278
+ if 'error' in async_display_callbacks:
279
+ await async_display_callbacks['error'](message=message)
280
+
281
+ console.print(Panel.fit(Text(message, style="bold red"), title="Error", border_style="red"))
282
+ error_logs.append(message)
283
+
284
+ async def adisplay_generating(content: str = "", start_time: Optional[float] = None):
285
+ """Async version of display_generating."""
286
+ if not content or not str(content).strip():
287
+ return Panel("", title="", border_style="green")
288
+
289
+ elapsed_str = ""
290
+ if start_time is not None:
291
+ elapsed = time.time() - start_time
292
+ elapsed_str = f" {elapsed:.1f}s"
293
+
294
+ content = _clean_display_content(str(content))
295
+
296
+ if 'generating' in async_display_callbacks:
297
+ await async_display_callbacks['generating'](
155
298
  content=content,
156
299
  elapsed_time=elapsed_str.strip() if elapsed_str else None
157
300
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: praisonaiagents
3
- Version: 0.0.18
3
+ Version: 0.0.20
4
4
  Summary: Praison AI agents for completing complex tasks with Self Reflection Agents
5
5
  Author: Mervin Praison
6
6
  Requires-Dist: pydantic
@@ -1,5 +1,5 @@
1
- praisonaiagents/__init__.py,sha256=bXQwi56S1iZOXs_TyXs3doxVxAeRAyqKVyZEIoBOipM,853
2
- praisonaiagents/main.py,sha256=QP45NxcrVs4PboVWcVkvLcsqiU7EzgE1ukH13jcYKfo,6985
1
+ praisonaiagents/__init__.py,sha256=KKB8sfpTh1Lf0gz9ULe6a0sA2JpGqOevH80RpM8p0oM,923
2
+ praisonaiagents/main.py,sha256=7Phfe0gdxHzbhPb3WRzBTfq9CaLq0K31M5DM_4oCiCQ,12451
3
3
  praisonaiagents/agent/__init__.py,sha256=sKO8wGEXvtCrvV1e834r1Okv0XAqAxqZCqz6hKLiTvA,79
4
4
  praisonaiagents/agent/agent.py,sha256=_UmUWGbZjd3tApPX2T6RPB5Pll3Gos97XBhhg_zmfn8,30662
5
5
  praisonaiagents/agents/__init__.py,sha256=7RDeQNSqZg5uBjD4M_0p_F6YgfWuDuxPFydPU50kDYc,120
@@ -16,7 +16,7 @@ praisonaiagents/process/__init__.py,sha256=lkYbL7Hn5a0ldvJtkdH23vfIIZLIcanK-65C0
16
16
  praisonaiagents/process/process.py,sha256=4qXdrCDQPH5MtvHvdJVURXKNgSl6ae3OYTiqAF_A2ZU,24295
17
17
  praisonaiagents/task/__init__.py,sha256=VL5hXVmyGjINb34AalxpBMl-YW9m5EDcRkMTKkSSl7c,80
18
18
  praisonaiagents/task/task.py,sha256=UiiWgLDOdX_w0opP8h8-u-leVZlq1CkpGUmf7L2qyJs,3110
19
- praisonaiagents-0.0.18.dist-info/METADATA,sha256=JtO_vQUfxUb2EM2x_PSt1HqTf3biR65fyz4tg0_PF0Q,233
20
- praisonaiagents-0.0.18.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
21
- praisonaiagents-0.0.18.dist-info/top_level.txt,sha256=_HsRddrJ23iDx5TTqVUVvXG2HeHBL5voshncAMDGjtA,16
22
- praisonaiagents-0.0.18.dist-info/RECORD,,
19
+ praisonaiagents-0.0.20.dist-info/METADATA,sha256=HEoTvC97N36YxNBbb7VEJgB7tWZCvGOR-tpcAYMwEus,233
20
+ praisonaiagents-0.0.20.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
21
+ praisonaiagents-0.0.20.dist-info/top_level.txt,sha256=_HsRddrJ23iDx5TTqVUVvXG2HeHBL5voshncAMDGjtA,16
22
+ praisonaiagents-0.0.20.dist-info/RECORD,,