Skip to content

feat: update permission handling to delegate event context in guarded tools#9001

Merged
Soulter merged 1 commit into
masterfrom
fix/llm_tool
Jun 25, 2026
Merged

feat: update permission handling to delegate event context in guarded tools#9001
Soulter merged 1 commit into
masterfrom
fix/llm_tool

Conversation

@Soulter

@Soulter Soulter commented Jun 25, 2026

Copy link
Copy Markdown
Member

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Delegate event objects instead of full contexts to tool handlers and extend tool invocation compatibility in the function tool manager.

Enhancements:

  • Update function tool manager to pass event objects into handler-based tools rather than full contexts.
  • Add a compatibility fallback to invoke legacy tools exposing a run method when handler or custom call are not used.
  • Clarify inline documentation around handler, call overrides, and legacy run fallbacks in the tool management logic.

Tests:

  • Adjust guarded tool permission tests to validate event delegation to handlers and the output format when additional keyword arguments are passed.
  • Update async generator handler tests to assert that handlers receive the correct event instance.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Jun 25, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 25, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
astrbot-docs 3828ebd Commit Preview URL

Branch Preview URL
Jun 25 2026, 08:19 AM

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies FunctionToolManager to pass the event object (extracted from context.context.event) instead of the full context to tool handlers, updating the associated unit tests accordingly. Feedback suggests using defensive attribute retrieval (getattr) when accessing context.context.event to prevent potential AttributeError exceptions if context or its nested properties are None.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

# @filter.llm_tool decorated tools have a handler attribute, which is the actual callable.
if self._wrapped.handler is not None:
result = self._wrapped.handler(context, **kwargs)
event = context.context.event

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential AttributeError exceptions when context is None or does not contain the expected nested attributes, it is safer to use defensive attribute retrieval (e.g., using getattr). This aligns with defensive programming practices for handling nullable or duck-typed references.

Suggested change
event = context.context.event
event = getattr(getattr(context, "context", None), "event", None)

@Soulter Soulter merged commit 473377f into master Jun 25, 2026
22 of 23 checks passed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In FunctionToolManager.call, the handler path now assumes context.context.event is always present; consider adding a small guard or clearer type hints around context to avoid brittle attribute access when used in different calling environments.
  • For the legacy run fallback, ensure the implementation actually invokes run(event, **kwargs) and handles coroutine/async-generator results in a similar way to the handler path to keep tool execution behavior consistent across interfaces.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `FunctionToolManager.call`, the handler path now assumes `context.context.event` is always present; consider adding a small guard or clearer type hints around `context` to avoid brittle attribute access when used in different calling environments.
- For the legacy `run` fallback, ensure the implementation actually invokes `run(event, **kwargs)` and handles coroutine/async-generator results in a similar way to the handler path to keep tool execution behavior consistent across interfaces.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant