Skip to content

Conversation

@whoiskatrin
Copy link
Contributor

@whoiskatrin whoiskatrin commented Dec 12, 2025

disclaimer: this is breaking change, but man i think we need to clean this

Replaces the two-hook pattern (useAgent + useAgentChat) with a single useChat hook that provides:

  • Declarative tool config: execute for client-side, confirm: true/false for approval flow
  • Simple approve(id) / deny(id) API instead of addToolResult() with magic strings
  • Cleaner query option for auth tokens with caching support
  • Flexible initialMessages configuration
  • Centralize TOOL_CONFIRMATION protocol constants in ai-types.ts
const { messages, sendMessage, approve, deny } = useChat({
  agent: "my-agent",
  tools: {
    calc: { execute: fn },           // client, auto
    delete: { confirm: true },       // server, requires approval
  }
});

@changeset-bot
Copy link

changeset-bot bot commented Dec 12, 2025

⚠️ No Changeset found

Latest commit: c361864

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 12, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@731

commit: c361864

@claude
Copy link

claude bot commented Dec 12, 2025

Claude Code Review

Summary: Clean refactoring that simplifies the human-in-the-loop API. The new useChat hook is more intuitive than the two-hook pattern.

Issues Found

Critical:

  1. Type safety issue in use-chat.tsx:47-49: The requiresConfirmation logic is backwards for the default case:

    function requiresConfirmation(tool: Tool): boolean {
      if (tool.confirm !== undefined) return tool.confirm;
      return !tool.execute;  // ❌ Wrong: tools with execute should auto-run by default
    }

    Current behavior: tools with execute but no explicit confirm will require confirmation. This contradicts the documented behavior in tools.ts comments that say execute tools with confirm: false auto-run. Should be: return true (require confirmation by default unless they have execute, then use confirm value).

  2. Validation bypassed in utils.ts:76-78: When no schema exists in toolDef, input validation is skipped entirely:

    } else {
      result = await (executeFunc as (args: unknown) => Promise<string>)(toolInput);
    }

    This silently accepts invalid inputs. Either always validate or throw an error if schema is missing.

Moderate:

  1. Breaking protocol change in utils.ts:85: Custom denial messages now treated as errors:

    } else {
      result = `Tool execution denied: ${userResponse}`;
    }

    Any client sending a custom denial reason (not TOOL_CONFIRMATION.DENIED) will have it formatted as "Tool execution denied: ...". This may break existing integrations expecting the raw user message.

  2. Missing type export in react.tsx:9: Exports ToolConfirmationSignal but not used/documented anywhere. Either use it consistently or remove.

Minor:

  1. Test coverage incomplete: use-chat.test.tsx only tests type definitions, not runtime behavior (approve/deny/tool execution logic). Consider adding integration tests.

  2. getWeatherInformation duplicated: Exported from both tools.ts:12 and utils.ts:88. Consolidate to single location.

Positive Changes

  • Clear separation of concerns (tool config vs transport)
  • Protocol constants centralized in ai-types.ts
  • Better error handling with try/catch in approve()
  • Cleaner guide example code

Recommendation

Fix issues #1-2 before merging (type safety + validation). Consider addressing #3 if backward compatibility matters.

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 12, 2025
Syncs documentation changes from cloudflare/agents PR #731:
- Add comprehensive useChat API reference documentation
- Update human-in-the-loop guide to use new useChat hook
- Replace two-hook pattern (useAgent + useAgentChat) with single useChat hook
- Update tool configuration to use declarative confirm property
- Simplify approval flow with approve() and deny() methods

This is a breaking change that improves developer experience with:
- Simpler single-hook API
- Declarative tool configuration with execute and confirm properties
- Cleaner approval flow without magic strings
- Better TypeScript types with PendingConfirmation

Related PR: cloudflare/agents#731

Generated with Claude Code https://claude.com/claude-code

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
- Add useChat hook that wraps useAgent + useAgentChat with simpler API
- Support declarative tool configuration with confirm: true/false
- Add approve()/deny() functions instead of addToolResult() with magic strings
- Export pendingToolCalls array for easy UI rendering
- Export TOOL_CONFIRMATION constants for protocol values
- Update human-in-the-loop example to use new hook
@whoiskatrin whoiskatrin force-pushed the refactor-tools-transport-etc branch from 1bc3ddf to 73dbfdc Compare December 12, 2025 18:38
agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 12, 2025
Updates documentation to reflect the new simplified useChat hook that
replaces the useAgent + useAgentChat pattern.

## Changes

- Updated human-in-the-loop guide with new useChat hook examples
- Added declarative Tool type documentation with confirm property
- Documented new approve()/deny() API replacing addToolResult()
- Added tool behavior matrix showing execute/confirm combinations
- Updated WebSockets API reference to document both useAgent and useChat
- Simplified code examples throughout

## Breaking Changes

This documents the breaking changes introduced in cloudflare/agents#731:
- useChat replaces useAgent + useAgentChat two-hook pattern
- Tool type now uses declarative confirm property
- approve(id) and deny(id) replace addToolResult() with magic strings
- Simpler, more intuitive API for human-in-the-loop workflows

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
- Single source of truth for tool definitions (tools.ts)
- Shared TOOL_CONFIRMATION constants between client/server
- Derive toolsRequiringConfirmation from tool config (not hardcoded)
- Add isLoading state to UseChatHelpers
- Improved documentation with execution model table
- Server.ts refactored with extracted metadata helper
- Move TOOL_CONFIRMATION constants to ai-types.ts (shared client/server)
- Export from main index.ts for server-side use
- Re-export from use-chat.tsx for client convenience
- use-chat.tsx now imports from ai-types.ts (single source of truth)
- Update example to document protocol requirement
agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 12, 2025
Updates documentation to reflect the breaking changes in PR #731:
- Replace useAgent + useAgentChat two-hook pattern with unified useChat hook
- Update human-in-the-loop guide with new declarative tool configuration
- Document new approve(id)/deny(id) API replacing addToolResult()
- Add TOOL_CONFIRMATION protocol constants documentation
- Update tool configuration to use confirm property instead of manual lists
- Simplify examples to show new streamlined API

This is a breaking change that affects how users implement human-in-the-loop
workflows with the Agents SDK.

Related PR: cloudflare/agents#731
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.

1 participant