lite-agent 0.12.0__py3-none-any.whl → 0.13.0__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.

Potentially problematic release.


This version of lite-agent might be problematic. Click here for more details.

lite_agent/runner.py CHANGED
@@ -1,9 +1,10 @@
1
1
  import json
2
+ import inspect
2
3
  from collections.abc import AsyncGenerator, Sequence
3
4
  from datetime import datetime, timedelta, timezone
4
5
  from os import PathLike
5
6
  from pathlib import Path
6
- from typing import Any, Literal, cast
7
+ from typing import Any, Literal, cast, get_args, get_origin
7
8
 
8
9
  from funcall import Context
9
10
 
@@ -112,6 +113,59 @@ class Runner:
112
113
  """Normalize record_to parameter to Path object if provided."""
113
114
  return Path(record_to) if record_to else None
114
115
 
116
+ def _tool_expects_history_context(self, tool_calls: Sequence["ToolCall"]) -> bool:
117
+ """Check if any of the tool calls expect HistoryContext in their signatures.
118
+
119
+ Returns True if any tool function has a Context[HistoryContext[...]] parameter,
120
+ False if they expect Context[...] without HistoryContext wrapper.
121
+ """
122
+ if not tool_calls:
123
+ return False
124
+
125
+ for tool_call in tool_calls:
126
+ tool_func = self.agent.fc.function_registry.get(tool_call.function.name)
127
+ if not tool_func:
128
+ continue
129
+
130
+ # Get function signature
131
+ sig = inspect.signature(tool_func)
132
+
133
+ # Check each parameter for Context annotation
134
+ for param in sig.parameters.values():
135
+ if param.annotation == inspect.Parameter.empty:
136
+ continue
137
+
138
+ # Check if parameter is Context[...]
139
+ origin = get_origin(param.annotation)
140
+ if origin is not None:
141
+ # Check if it's Context type (compare by string name to handle import differences)
142
+ origin_name = getattr(origin, '__name__', str(origin))
143
+ if 'Context' in origin_name:
144
+ args = get_args(param.annotation)
145
+ if args:
146
+ # Check if the Context contains HistoryContext
147
+ inner_type = args[0]
148
+ inner_origin = get_origin(inner_type)
149
+ if inner_origin is not None:
150
+ inner_name = getattr(inner_origin, '__name__', str(inner_origin))
151
+ if 'HistoryContext' in inner_name:
152
+ logger.debug(f"Tool {tool_call.function.name} expects HistoryContext")
153
+ return True
154
+ # Also check for direct HistoryContext class
155
+ elif hasattr(inner_type, '__name__') and 'HistoryContext' in inner_type.__name__:
156
+ logger.debug(f"Tool {tool_call.function.name} expects HistoryContext")
157
+ return True
158
+
159
+ # Also handle direct annotation checking
160
+ if hasattr(param.annotation, '__name__'):
161
+ annotation_str = str(param.annotation)
162
+ if 'HistoryContext' in annotation_str:
163
+ logger.debug(f"Tool {tool_call.function.name} expects HistoryContext (direct)")
164
+ return True
165
+
166
+ logger.debug("No tools expect HistoryContext")
167
+ return False
168
+
115
169
  async def _handle_tool_calls(self, tool_calls: "Sequence[ToolCall] | None", includes: Sequence[AgentChunkType], context: "Any | None" = None) -> AsyncGenerator[AgentChunk, None]: # noqa: ANN401
116
170
  """Handle tool calls and yield appropriate chunks."""
117
171
  if not tool_calls:
@@ -184,23 +238,33 @@ class Runner:
184
238
  )
185
239
  return # Stop processing other tool calls after transfer
186
240
 
187
- # Auto-inject history messages into context
188
- if context is not None and not isinstance(context, Context):
189
- # If user provided a plain object, wrap it in Context first
241
+ # Check if tools expect HistoryContext wrapper
242
+ expects_history = self._tool_expects_history_context(tool_calls)
243
+
244
+ if expects_history:
245
+ # Auto-inject history messages into context for tools that expect HistoryContext
246
+ if context is not None and not isinstance(context, Context):
247
+ # If user provided a plain object, wrap it in Context first
248
+ context = Context(context)
249
+
250
+ if isinstance(context, Context):
251
+ # Extract original value and wrap in HistoryContext
252
+ original_value = context.value
253
+ wrapped = HistoryContext(
254
+ history_messages=self.messages.copy(),
255
+ data=original_value,
256
+ )
257
+ context = Context(wrapped)
258
+ else:
259
+ # No context provided, create HistoryContext with only history messages
260
+ wrapped = HistoryContext(history_messages=self.messages.copy())
261
+ context = Context(wrapped)
262
+ elif context is not None and not isinstance(context, Context):
263
+ # Tools don't expect HistoryContext, wrap user object in Context
190
264
  context = Context(context)
191
-
192
- if isinstance(context, Context):
193
- # Extract original value and wrap in HistoryContext
194
- original_value = context.value
195
- wrapped = HistoryContext(
196
- history_messages=self.messages.copy(),
197
- data=original_value,
198
- )
199
- context = Context(wrapped)
200
- else:
201
- # No context provided, create HistoryContext with only history messages
202
- wrapped = HistoryContext(history_messages=self.messages.copy())
203
- context = Context(wrapped)
265
+ elif context is None:
266
+ # Provide empty context for tools that don't expect HistoryContext
267
+ context = Context(None)
204
268
 
205
269
  async for tool_call_chunk in self.agent.handle_tool_calls(tool_calls, context=context):
206
270
  # if tool_call_chunk.type == "function_call" and tool_call_chunk.type in includes:
@@ -511,9 +575,10 @@ class Runner:
511
575
  max_steps: int = 20,
512
576
  includes: list[AgentChunkType] | None = None,
513
577
  record_to: PathLike | str | None = None,
578
+ context: Context | None = None,
514
579
  ) -> list[AgentChunk]:
515
580
  """Run the agent until it completes and return the final message."""
516
- resp = self.run(user_input, max_steps, includes, record_to=record_to)
581
+ resp = self.run(user_input, max_steps, includes, record_to=record_to, context=context)
517
582
  return await self._collect_all_chunks(resp)
518
583
 
519
584
  def _analyze_last_assistant_message(self) -> tuple[list[AssistantToolCall], dict[str, str]]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lite-agent
3
- Version: 0.12.0
3
+ Version: 0.13.0
4
4
  Summary: A lightweight, extensible framework for building AI agent.
5
5
  Author-email: Jianqi Pan <jannchie@gmail.com>
6
6
  License: MIT
@@ -7,7 +7,7 @@ lite_agent/context.py,sha256=qnqj4JVYL0pkle1YptwM_CXJUWTeLaYtI1_MGtJYaHI,1225
7
7
  lite_agent/loggers.py,sha256=XkNkdqwD_nQGfhQJ-bBWT7koci_mMkNw3aBpyMhOICw,57
8
8
  lite_agent/message_transfers.py,sha256=N9ViK7Gxqqa1sd3V_hkNuQ9fUipg7M95l-sVBBG2Id4,5357
9
9
  lite_agent/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
- lite_agent/runner.py,sha256=p-Dw4RNYfRnIwnbt8oJv8e4qI84X7OUWLxgyITMnGe8,45090
10
+ lite_agent/runner.py,sha256=H6gOeynLyLFr-ZTeEvBReuGdURkCVuP6T919_3AQ1CE,48568
11
11
  lite_agent/processors/__init__.py,sha256=ybpAzpMBIE9v5I24wIBZRXeaOaPNTmoKH13aofgNI6Q,234
12
12
  lite_agent/processors/completion_event_processor.py,sha256=zoWvs8dfrIkCSITGtS-4Hpve3WFCA0UUsMvYifL2fw0,13010
13
13
  lite_agent/processors/response_event_processor.py,sha256=Jr3cj1ItJ8aq9UBhEEjDwWDnPNOZ2ZXjWJ3-g4ghkhM,8514
@@ -30,6 +30,6 @@ lite_agent/utils/message_builder.py,sha256=J-yycL9pXSO9MbgC5NEGqvoP1LC2Nxe9r2YRW
30
30
  lite_agent/utils/message_converter.py,sha256=5HmNncTl71TD2M_6Ezz1Tnfavzna8DQYb4-D47Du7mA,9165
31
31
  lite_agent/utils/message_state_manager.py,sha256=rFUyqyd_7NdJRtyqsAWGcfwrDIlD6gK2dBDSDx1eGBs,5766
32
32
  lite_agent/utils/metrics.py,sha256=RzOEhCWxbLmmIEkzaxOJ6tAdthI8dv2Foc98Lq8afOQ,1915
33
- lite_agent-0.12.0.dist-info/METADATA,sha256=CvsMgd3DNYoZTvD0bTirNibch5S9eH8K-hjwWvEjFZo,3538
34
- lite_agent-0.12.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
35
- lite_agent-0.12.0.dist-info/RECORD,,
33
+ lite_agent-0.13.0.dist-info/METADATA,sha256=dw2mkbA3X3ZY8bV1xxeW9ybNZnHcFFFXSeTdLl4vuyc,3538
34
+ lite_agent-0.13.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
35
+ lite_agent-0.13.0.dist-info/RECORD,,