Skip to content

Conversation

@prnake
Copy link
Contributor

@prnake prnake commented Nov 13, 2025

close #1928 ,支持用户自定义

Summary by CodeRabbit

  • New Features
    • Added support for AWS authentication with regional model prefix configuration. Users can now specify AccessKey, SecretAccessKey, Region, and Model Prefix when configuring AWS channels, enabling region-specific model routing and customizable model prefix settings for enhanced AWS integration flexibility.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 13, 2025

Walkthrough

This pull request adds support for a configurable AWS model prefix to accommodate AWS Bedrock's updated cross-region deployment naming scheme (e.g., global, eu, jp, us). It introduces a new AWS secret format accepting a region-specific prefix, updates the AWS client initialization and model ID resolution logic, and extends the UI to support the new format option.

Changes

Cohort / File(s) Summary
AWS Constants and Configuration
dto/channel_settings.go
Added new constant AwsKeyTypeAKSKRegionPrefix = "ak_sk_region_prefix" to define the 4-parameter AWS secret format (AccessKey, SecretAccessKey, Region, Prefix).
AWS Adaptor Structure
relay/channel/aws/adaptor.go
Added public field ModelPrefix string to the Adaptor struct to carry prefix information through AWS requests.
AWS Client Initialization and Model Routing
relay/channel/aws/relay-aws.go
Reworked newAwsClient to return (client, modelPrefix, hasCustomPrefix, error) and handle the new 4-parameter secret format. Updated doAwsClientRequest to store prefix in adaptor and conditionally route model IDs with custom prefix or region-derived prefix. Extended awsModelCrossRegion to accept and prioritize a configurable prefix parameter.
UI Configuration
web/src/components/table/channels/modals/EditChannelModal.jsx
Added new AWS key format option ak_sk_region_prefix with label "AccessKey / SecretAccessKey / Region / Prefix" and updated conditional prompts to guide users on the new 4-parameter format.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Modal as EditChannelModal
    participant Backend as relay-aws.go
    participant AWS as AWS Bedrock

    Client->>Modal: Select AWS channel config
    Modal->>Modal: Choose ak_sk_region_prefix format
    Modal->>Backend: Submit config with (ak, sk, region, prefix)
    
    Backend->>Backend: newAwsClient()
    Backend->>Backend: Parse 4-param secret
    Backend->>Backend: Extract modelPrefix from param
    Backend->>Backend: Set hasCustomPrefix = true
    Backend->>Backend: Create AWS client with region + credentials
    Backend->>Backend: Return (client, modelPrefix, true, nil)
    
    Backend->>Backend: doAwsClientRequest()
    Backend->>Backend: Receive modelPrefix & hasCustomPrefix
    Backend->>Backend: Store a.ModelPrefix = modelPrefix
    
    alt hasCustomPrefix is true
        Backend->>Backend: awsModelId = modelPrefix + "." + awsModelId
    else hasCustomPrefix is false
        Backend->>Backend: awsModelCrossRegion(awsModelId, regionPrefix, "")
        Backend->>Backend: Use region-derived prefix if empty configured
    end
    
    Backend->>AWS: Call with adjusted awsModelId
    AWS-->>Backend: Response
    Backend-->>Client: Success
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Areas requiring attention:
    • Logic flow in newAwsClient for secret format parsing and prefix extraction; verify all four format branches (2-param, 3-param, 4-param, and default) are correctly handled
    • Interaction between hasCustomPrefix flag and cross-region routing logic in doAwsClientRequest to ensure prefix precedence is correct
    • Consistency of prefix parameter propagation through awsModelCrossRegion and verify backward compatibility for existing 2-param and 3-param formats
    • UI prompt strings and placeholder logic in EditChannelModal to ensure user guidance is clear for the new format

Suggested reviewers

  • seefs001

Poem

🐰 A prefix hops through regions far,
Global, JP, EU—what a par!
Bedrock routes with newfound grace,
Four parameters find their place.
Config, adaptor, modal sync—
The AWS model maze, now linked! 🌐

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: support custom model prefix for AWS' accurately reflects the main change: enabling users to configure custom model prefixes (global, eu, jp, us) for AWS Bedrock models.
Linked Issues check ✅ Passed The PR implements support for custom AWS model prefixes as required by issue #1928, enabling users to specify region prefixes (global, eu, jp, us) via a new ak_sk_region_prefix secret format.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing custom model prefix support: new constant, struct field, secret format handling, model ID resolution logic, and UI updates for the new format.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
relay/channel/aws/relay-aws.go (2)

28-86: Handle edge case: empty prefix in 4-parameter format

When users provide a 4-parameter secret with an empty prefix (e.g., ak|sk|region|), the code sets modelPrefix = "" and hasCustomPrefix = true. Line 103 in doAwsClientRequest would then construct .model-id, which is invalid.

Consider adding validation after line 73:

 region := awsSecret[2]
 prefix := awsSecret[3]
+if prefix == "" {
+    return nil, "", false, errors.New("prefix cannot be empty in 4-parameter format")
+}
 client = bedrockruntime.New(bedrockruntime.Options{

181-192: Critical: Cross-region logic may bypass map lookups

There's a logic flaw that could break existing region mappings. When hasCustomPrefix = false (2-param/3-param formats), doAwsClientRequest passes the region-derived modelPrefix (e.g., "ap") as the configuredPrefix parameter at line 109. The new check at line 183 uses this directly if non-empty, bypassing the map lookup at line 187.

This breaks the existing behavior where regions like "ap-southeast-1" should map to "jp" prefix according to issue #1928, since the map awsRegionCrossModelPrefixMap likely contains this mapping.

The configuredPrefix parameter should only receive the custom prefix from the 4-parameter format. For 2-param/3-param cases, pass an empty string to ensure map lookup is used:

 func awsModelCrossRegion(awsModelId, awsRegionPrefix, configuredPrefix string) string {
-    // 如果配置了prefix,优先使用配置的prefix
-    if configuredPrefix != "" {
-        return configuredPrefix + "." + awsModelId
-    }
-    // 否则从region推导
     modelPrefix, find := awsRegionCrossModelPrefixMap[awsRegionPrefix]
     if !find {
         return awsModelId
     }
     return modelPrefix + "." + awsModelId
 }

And in doAwsClientRequest at line 109:

-        awsModelId = awsModelCrossRegion(awsModelId, awsRegionPrefix, modelPrefix)
+        awsModelId = awsModelCrossRegion(awsModelId, awsRegionPrefix, "")

Since the 4-parameter case already handles custom prefixes at line 103, the configuredPrefix parameter becomes unused and could be removed entirely from the function signature.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4419be9 and 3affe98.

📒 Files selected for processing (4)
  • dto/channel_settings.go (1 hunks)
  • relay/channel/aws/adaptor.go (1 hunks)
  • relay/channel/aws/relay-aws.go (4 hunks)
  • web/src/components/table/channels/modals/EditChannelModal.jsx (3 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-08-27T02:15:25.448Z
Learnt from: AAEE86
Repo: QuantumNous/new-api PR: 1658
File: web/src/components/table/channels/modals/EditChannelModal.jsx:555-569
Timestamp: 2025-08-27T02:15:25.448Z
Learning: In EditChannelModal.jsx, the applyModelMapping function transforms the models list by replacing original model names (mapping values) with display names (mapping keys). The database stores this transformed list containing mapped keys. On channel load, data.models contains these mapped display names, making the initialization filter if (data.models.includes(key)) correct.

Applied to files:

  • web/src/components/table/channels/modals/EditChannelModal.jsx
📚 Learning: 2025-08-27T02:15:25.448Z
Learnt from: AAEE86
Repo: QuantumNous/new-api PR: 1658
File: web/src/components/table/channels/modals/EditChannelModal.jsx:555-569
Timestamp: 2025-08-27T02:15:25.448Z
Learning: In EditChannelModal.jsx, the database stores mapped keys (display names) in the models field after applying model mapping transformations. When loading a channel, data.models contains the mapped keys, not the original model names. The filtering logic if (data.models.includes(key)) in the initialization is correct.

Applied to files:

  • web/src/components/table/channels/modals/EditChannelModal.jsx
🧬 Code graph analysis (1)
relay/channel/aws/relay-aws.go (3)
service/http_client.go (2)
  • NewProxyHttpClient (66-134)
  • GetHttpClient (49-51)
relay/channel/aws/adaptor.go (1)
  • Adaptor (28-35)
types/error.go (1)
  • ErrorCodeChannelAwsClientError (56-56)
🔇 Additional comments (6)
relay/channel/aws/adaptor.go (1)

34-34: LGTM!

The new ModelPrefix field provides a clean way to carry the configured prefix through the request lifecycle. The field is properly exported and documented.

dto/channel_settings.go (1)

23-23: LGTM!

The new constant follows the existing naming pattern and correctly represents the 4-parameter AWS key format.

web/src/components/table/channels/modals/EditChannelModal.jsx (3)

1497-1500: LGTM!

The new AWS key format option is properly integrated into the selector, maintaining consistency with the existing options.


1587-1594: LGTM!

The batch mode placeholder correctly reflects the 4-parameter format when ak_sk_region_prefix is selected.


1792-1794: LGTM!

The single key mode placeholder properly handles the new region-prefix format.

relay/channel/aws/relay-aws.go (1)

101-111: ****

The review comment's specific details are inaccurate. The actual cross-region mapping shows "ap" → "apac", not "ap" → "jp" as mentioned. Additionally, no reference to issue #1928 exists in the codebase. The comment in adaptor.go lists "jp" as a possible prefix value, but it is not present in awsRegionCrossModelPrefixMap.

The current implementation correctly handles 2-param and 3-param formats by deriving the region prefix from the region parameter (e.g., "ap-southeast-1""ap"), which then participates in the cross-region model mapping logic. The 4-param format uses an explicitly configured prefix. All three formats are handled as designed.

Likely an incorrect or invalid review comment.

a.AwsClient = awsCli
a.ModelPrefix = modelPrefix

println(info.UpstreamModelName)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Remove debug println statement

This debug statement should be removed before merging to production.

Apply this diff:

-println(info.UpstreamModelName)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
println(info.UpstreamModelName)
🤖 Prompt for AI Agents
In relay/channel/aws/relay-aws.go around line 97 there is a leftover debug
println(info.UpstreamModelName); remove this debug print before merging — either
delete the println call or replace it with an appropriate structured logger call
at the correct log level if the model name must be recorded (e.g., use existing
logger.Debug/Info with context), ensuring no stray stdout prints remain.

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.

claude-sonnet-4-5-20250929 AWS bedrock 新部署区域

1 participant