Skip to content

feat: add tool approval feature for human-in-the-loop workflows#79

Draft
iwasrobbed wants to merge 1 commit into
lazy-hq:mainfrom
iwasrobbed:tools
Draft

feat: add tool approval feature for human-in-the-loop workflows#79
iwasrobbed wants to merge 1 commit into
lazy-hq:mainfrom
iwasrobbed:tools

Conversation

@iwasrobbed

Copy link
Copy Markdown

🤖 Generated with Claude Code | Opus 4.5 by directly reading Vercel's AI SDK's latest main branch

TODO

  • Test this for my use cases (wanted to get the PR up first)

Summary

Adds tool approval functionality allowing tools to require user confirmation before execution, matching the Vercel AI SDK's needsApproval pattern.

  • NeedsApproval enum with Always, Never, and Dynamic variants
  • ToolApprovalRequest and ToolApprovalResponse types for the approval flow
  • needs_approval field on Tool struct and builder
  • pending_tool_approvals() and has_pending_approvals() methods on response types
  • Message::ToolApproval variant for sending approval responses
  • Non-blocking approval flow (returns immediately with pending requests)

Usage

// Tool that always requires approval
let tool = Tool::builder()
    .name("delete_file")
    .needs_approval(NeedsApproval::Always)
    // ...

// Dynamic approval based on input
let tool = Tool::builder()
    .name("send_payment")
    .needs_approval(NeedsApproval::Dynamic(Arc::new(|input, _ctx| {
        input["amount"].as_f64().unwrap_or(0.0) > 1000.0
    })))
    // ...

Test plan

  • Unit tests for NeedsApproval enum variants
  • Unit tests for ToolApprovalRequest/ToolApprovalResponse serialization
  • Unit tests for collect_tool_approvals() and has_pending_approval_requests()
  • All 78 unit tests + 10 doc tests pass

Related Issue

Closes #77

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist

  • I have read the Contributing Guidelines.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • Any dependent changes have been merged and published in downstream modules.

Port Vercel AI SDK's tool approval feature to Rust, allowing tools to
require user confirmation before execution.

- Add NeedsApproval enum (Always, Never, Dynamic) matching Vercel's
  boolean | function pattern
- Add ToolApprovalRequest/Response types with approval_id flow
- Add needs_approval field to Tool struct with builder support
- Implement non-blocking approval flow in generate_text and stream_text
- Add pending_tool_approvals() and has_pending_approvals() helpers
- Add Message::ToolApproval variant for approval responses
- Update all provider conversions to handle new message types
- Add comprehensive unit tests (29 new tests)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: tool approvals

1 participant