Skip to content

computer use for anthropic on bedrock #1177

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

narengogi
Copy link
Collaborator

#1176

Testing:
sample request body

{
    "model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0",
    "anthropic_beta":"computer-use-2025-01-24",
    "max_tokens": 2000,
    "tools": [
        {
            "type": "computer",
            "computer": {
                "name": "computer_20250124",
                "display_width_px": 1024,
                "display_height_px": 768,
                "display_number": 1
            }
        }
        // {
        //     "type": "bash",
        //     "bash": {
        //         "name": "bash_20250124"
        //     }
        // }
        // }
    ],
    "messages": [
        {
            "role": "user",
            "content": "make a curl request to google"
        }
    ]
}

Copy link

matter-code-review bot commented Jun 26, 2025

Code Quality new feature bug fix maintainability

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This Pull Request introduces enhanced support for various tool types, including 'computer' use, for Anthropic models when accessed via Bedrock. The Tool interface in src/types/requestBody.ts has been generalized to support arbitrary tool properties ([key: string]: any;) instead of a fixed computer property. The src/providers/anthropic/chatComplete.ts file now dynamically processes tools based on their type property, allowing for flexible tool configurations and including cache_control. For Bedrock, src/providers/bedrock/chatComplete.ts now includes a null check for tool.function before processing and refines the return logic for tool configurations. Crucially, src/providers/bedrock/utils.ts has been updated to transform generic tool definitions from the request body into Anthropic-specific tool formats, handling different tool.type values and cache_control for Bedrock-Anthropic integrations.

🔍 Impact of the Change

This change significantly improves the flexibility and compatibility of the gateway with Anthropic's advanced tool capabilities, such as 'computer-use'. It allows users to define and pass various tool types dynamically, making the system more adaptable to future tool extensions without requiring code changes. The added null check in Bedrock's tool processing enhances robustness, and the refined return logic for tool configurations ensures that tool-related parameters are only included when tools are actually present. This directly addresses the issue of supporting computer use for Anthropic on Bedrock, as referenced in the linked GitHub issue.

📁 Total Files Changed

  • src/providers/anthropic/chatComplete.ts: Modified (8 additions, 4 deletions)
  • src/providers/bedrock/chatComplete.ts: Modified (11 additions, 8 deletions)
  • src/providers/bedrock/utils.ts: Modified (20 additions, 1 deletion)
  • src/types/requestBody.ts: Modified (2 additions, 6 deletions)

Total files changed: 4
Total additions: 41
Total deletions: 19
Total changes: 60

🧪 Test Added

Manual testing was performed using a sample request body:

{
    "model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0",
    "anthropic_beta":"computer-use-2025-01-24",
    "max_tokens": 2000,
    "tools": ,
    "messages": 
}

This test verifies the end-to-end flow of passing computer-use related tools through the gateway to the Anthropic model on Bedrock.

🔒Security Vulnerabilities

No new security vulnerabilities were detected in this pull request. The changes primarily focus on feature enhancement and robustness.

Motivation

This PR addresses the issue of supporting 'computer use' and other generic tool types for Anthropic models when accessed via Bedrock, as detailed in #1176.

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)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • 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

Related Issues

#1176

Tip

Quality Recommendations

  1. Consider adding more specific type definitions for the toolOptions in src/providers/anthropic/chatComplete.ts and src/providers/bedrock/utils.ts instead of relying on any or [key: string]: any; in the Tool interface. This would improve type safety and readability.

  2. The TODO: split this into two provider options, one for tools and one for toolChoice in src/providers/bedrock/chatComplete.ts should be addressed in a follow-up PR to improve modularity and maintainability of the Bedrock configuration.

  3. Implement unit tests for the new tool transformation logic in src/providers/bedrock/utils.ts to ensure correct handling of various tool types and edge cases, especially for cache_control and missing toolOptions.

Sequence Diagram

sequenceDiagram
    participant User
    participant GatewayAPI as Gateway API Endpoint
    participant AnthropicChatComplete as Anthropic Chat Complete Config
    participant BedrockChatComplete as Bedrock Chat Complete Config
    participant BedrockUtils as Bedrock Utils
    participant AnthropicAPI as Anthropic API
    participant BedrockAPI as Bedrock API

    User->>GatewayAPI: POST /chat/completions (with tools array)
    GatewayAPI->>BedrockChatComplete: Process Bedrock Request (params)
    BedrockChatComplete->>BedrockChatComplete: Iterate params.tools
    alt If tool.function exists
        BedrockChatComplete->>BedrockChatComplete: Add { toolSpec: { name, description, inputSchema } } to tools array
    end
    alt If tool.cache_control exists and not Amazon model
        BedrockChatComplete->>BedrockChatComplete: Add { cachePoint: { type: 'ephemeral' } } to tools array
    end
    BedrockChatComplete-->>GatewayAPI: Return toolConfig (if tools.length > 0) or null

    GatewayAPI->>BedrockUtils: Call transformAnthropicAdditionalModelRequestFields(params)
    BedrockUtils->>BedrockUtils: Iterate params.tools (Tool[])
    alt If tool.type is not 'function'
        BedrockUtils->>BedrockUtils: Get toolOptions = tool[tool.type]
        BedrockUtils->>BedrockUtils: Construct Anthropic-specific tool object
        BedrockUtils->>BedrockUtils: Add { ...toolOptions, name: tool.type, type: toolOptions?.name, cache_control } to anthropicTools array
    end
    alt If anthropicTools array is not empty
        BedrockUtils->>BedrockUtils: Add anthropicTools to additionalModelRequestFields['tools']
    end
    BedrockUtils-->>GatewayAPI: Return additionalModelRequestFields

    GatewayAPI->>AnthropicChatComplete: Process Anthropic Request (params)
    AnthropicChatComplete->>AnthropicChatComplete: Iterate params.tools
    alt If tool.function exists
        AnthropicChatComplete->>AnthropicChatComplete: Add { name, description, input_schema, cache_control } to tools array
    end
    alt If tool.type exists (e.g., 'computer')
        AnthropicChatComplete->>AnthropicChatComplete: Get toolOptions = tool[tool.type]
        AnthropicChatComplete->>AnthropicChatComplete: Add { ...toolOptions, name: tool.type, type: toolOptions?.name, cache_control } to tools array
    end
    AnthropicChatComplete->>AnthropicAPI: Send Chat Completion Request (with transformed tools)
    AnthropicAPI-->>AnthropicChatComplete: Response
    AnthropicChatComplete-->>GatewayAPI: Response

    GatewayAPI->>BedrockAPI: Send Converse API Request (with transformed tools/additional fields)
    BedrockAPI-->>GatewayAPI: Response
    GatewayAPI-->>User: Return API Response
Loading

Copy link

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

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

This PR adds support for Anthropic's computer tool on Bedrock, which is a good enhancement. I've identified a few improvements that could make the implementation more robust.

@narengogi narengogi requested review from VisargD and b4s36t4 June 26, 2025 11:16
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

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