-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat: expose conversation history to tools via ToolContext.conversation_history #2905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,7 @@ def __init__( | |
| turn_input: list[TResponseInputItem] | None = None, | ||
| _approvals: dict[str, _ApprovalRecord] | None = None, | ||
| tool_input: Any | None = None, | ||
| _generated_items: list[Any] | None = None, | ||
| ) -> None: | ||
| """Preserve the v0.7 positional constructor while accepting new context fields.""" | ||
| resolved_usage = Usage() if usage is _MISSING else cast(Usage, usage) | ||
|
|
@@ -81,6 +82,7 @@ def __init__( | |
| turn_input=list(turn_input or []), | ||
| _approvals={} if _approvals is None else _approvals, | ||
| tool_input=tool_input, | ||
| _generated_items=list(_generated_items or []), | ||
| ) | ||
| self.tool_name = ( | ||
| _assert_must_pass_tool_name() if tool_name is _MISSING else cast(str, tool_name) | ||
|
|
@@ -104,6 +106,16 @@ def __init__( | |
| self.agent = agent | ||
| self.run_config = run_config | ||
|
|
||
| @property | ||
| def conversation_history(self) -> list[Any]: | ||
| """The items generated so far in the current agent run. | ||
|
|
||
| This is a snapshot of the conversation history at the time the tool was invoked, | ||
| allowing tools to inspect prior messages, tool calls, and other items produced | ||
| during the run. Returns a copy so that mutations do not affect the run state. | ||
| """ | ||
| return list(self._generated_items) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Returning Useful? React with 👍 / 👎. |
||
|
|
||
| @property | ||
| def qualified_tool_name(self) -> str: | ||
| """Return the tool name qualified by namespace when available.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This history assignment only runs in
get_single_step_result_from_response, but resumed approval flows execute tools throughresolve_interrupted_turnand can use a rebuilt/overriddenRunContextWrapper(for example after deserializingRunState) where_generated_itemsis empty. In that path,ToolContext.from_agent_contextcopies stale history, so tools resumed after an interruption see missing prior conversation items; initialize_generated_itemsin the resume path before tool execution as well.Useful? React with 👍 / 👎.