Skip to content

Commit 7d3840e

Browse files
committed
fix(bedrock): keep custom ARN capability overrides tied to their ARN
- Clear the base model and per-ARN limits whenever the ARN changes, from the model picker or the Custom ARN field - Store "Other" explicitly so it overrides a model named in the ARN, in the settings panel and the provider - Resolve foundation-model ARNs to the model they name and lock the selector Refs #1968
1 parent c79abfb commit 7d3840e

9 files changed

Lines changed: 261 additions & 44 deletions

File tree

‎packages/types/src/__tests__/bedrock-custom-arn.test.ts‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { resolveBedrockCustomArnBaseModelId } from "../providers/bedrock.js"
1+
import {
2+
BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL,
3+
isBedrockFoundationModelArn,
4+
resolveBedrockCustomArnBaseModelId,
5+
} from "../providers/bedrock.js"
26

37
const appProfileArn = "arn:aws:bedrock:us-west-2:123456789012:application-inference-profile/abcd1234efgh"
48

@@ -40,6 +44,32 @@ describe("resolveBedrockCustomArnBaseModelId", () => {
4044
).toBe("anthropic.claude-opus-4-8")
4145
})
4246

47+
it("lets an explicit Other choice override a model named in the ARN", () => {
48+
expect(
49+
resolveBedrockCustomArnBaseModelId(
50+
"arn:aws:bedrock:us-east-1:123456789012:inference-profile/us.anthropic.claude-opus-4-8",
51+
BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL,
52+
),
53+
).toBeUndefined()
54+
})
55+
56+
it("always resolves a foundation-model ARN to the model it names", () => {
57+
const foundationArn = "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0"
58+
59+
expect(resolveBedrockCustomArnBaseModelId(foundationArn, "anthropic.claude-opus-5-5")).toBe(
60+
"anthropic.claude-3-5-sonnet-20241022-v2:0",
61+
)
62+
expect(resolveBedrockCustomArnBaseModelId(foundationArn, BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL)).toBe(
63+
"anthropic.claude-3-5-sonnet-20241022-v2:0",
64+
)
65+
expect(
66+
resolveBedrockCustomArnBaseModelId(
67+
"arn:aws:bedrock:us-east-1::foundation-model/vendor.unlisted-model-v1:0",
68+
"anthropic.claude-opus-5-5",
69+
),
70+
).toBeUndefined()
71+
})
72+
4373
it("returns undefined when neither the setting nor the ARN identifies a model", () => {
4474
expect(resolveBedrockCustomArnBaseModelId(appProfileArn, undefined)).toBeUndefined()
4575
expect(resolveBedrockCustomArnBaseModelId(appProfileArn, "")).toBeUndefined()
@@ -56,3 +86,25 @@ describe("resolveBedrockCustomArnBaseModelId", () => {
5686
).toBe("amazon.nova-2-lite-v1:0")
5787
})
5888
})
89+
90+
describe("isBedrockFoundationModelArn", () => {
91+
it("recognizes foundation-model ARNs in any partition", () => {
92+
expect(isBedrockFoundationModelArn("arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-v2")).toBe(
93+
true,
94+
)
95+
expect(
96+
isBedrockFoundationModelArn("arn:aws-us-gov:bedrock:us-gov-west-1::foundation-model/anthropic.claude-v2"),
97+
).toBe(true)
98+
})
99+
100+
it("rejects other ARN types and missing values", () => {
101+
expect(isBedrockFoundationModelArn(appProfileArn)).toBe(false)
102+
expect(
103+
isBedrockFoundationModelArn(
104+
"arn:aws:bedrock:us-east-1:123456789012:inference-profile/us.anthropic.claude-opus-4-8",
105+
),
106+
).toBe(false)
107+
expect(isBedrockFoundationModelArn(undefined)).toBe(false)
108+
expect(isBedrockFoundationModelArn("")).toBe(false)
109+
})
110+
})

‎packages/types/src/providers/bedrock.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -742,24 +742,37 @@ export const BEDROCK_SERVICE_TIER_PRICING = {
742742

743743
const isBedrockModelId = (id: string): id is BedrockModelId => Object.prototype.hasOwnProperty.call(bedrockModels, id)
744744

745+
/** Stored in `awsCustomArnBaseModelId` when the user explicitly chooses "Other" for a custom ARN. */
746+
export const BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL = "other"
747+
748+
export const isBedrockFoundationModelArn = (arn: string | undefined): boolean =>
749+
!!arn && /^arn:[^:]+:bedrock:[^:]+:[^:]*:foundation-model\//.test(arn)
750+
745751
/**
746-
* Resolves the registry model a custom ARN points to: the user's explicit choice when it is a known
747-
* model, otherwise a model named at the end of the ARN (e.g. `inference-profile/us.anthropic.claude-opus-4-8`).
748-
* Returns undefined when the ARN doesn't identify its model (e.g. application inference profiles).
752+
* Resolves the registry model a custom ARN points to:
753+
* - a foundation-model ARN always resolves to the model it names, because that model is invoked;
754+
* - an explicit "Other" choice resolves to undefined, even when the ARN names a model;
755+
* - otherwise the user's choice when it is a known model, then a model named at the end of the ARN
756+
* (e.g. `inference-profile/us.anthropic.claude-opus-4-8`).
757+
* Returns undefined when the model is unknown (e.g. application inference profiles).
749758
*/
750759
export const resolveBedrockCustomArnBaseModelId = (
751760
customArn: string | undefined,
752761
baseModelId: string | undefined,
753762
): BedrockModelId | undefined => {
754-
if (baseModelId && isBedrockModelId(baseModelId)) {
755-
return baseModelId
763+
const namedModelId = customArn
764+
? (Object.keys(bedrockModels) as BedrockModelId[]).find(
765+
(id) => customArn.endsWith(`/${id}`) || customArn.endsWith(`.${id}`),
766+
)
767+
: undefined
768+
769+
if (isBedrockFoundationModelArn(customArn)) {
770+
return namedModelId
756771
}
757772

758-
if (!customArn) {
773+
if (baseModelId === BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL) {
759774
return undefined
760775
}
761776

762-
return (Object.keys(bedrockModels) as BedrockModelId[]).find(
763-
(id) => customArn.endsWith(`/${id}`) || customArn.endsWith(`.${id}`),
764-
)
777+
return baseModelId && isBedrockModelId(baseModelId) ? baseModelId : namedModelId
765778
}

‎src/api/providers/__tests__/bedrock.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
bedrockModels,
6161
SERVICE_TIER_KEY,
6262
ApiProviderError,
63+
BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL,
6364
BEDROCK_DEFAULT_CONTEXT,
6465
BEDROCK_MAX_TOKENS,
6566
type ProviderSettings,
@@ -2082,6 +2083,40 @@ describe("AwsBedrockHandler", () => {
20822083

20832084
expect(handler.getModel().id).toBe("anthropic.claude-3-5-sonnet-20241022-v2:0")
20842085
})
2086+
2087+
it("uses the generic fallback for an explicit Other choice even when the ARN names a model", async () => {
2088+
const profileArn =
2089+
"arn:aws:bedrock:us-east-1:123456789012:inference-profile/us.anthropic.claude-opus-4-8"
2090+
const handler = createCustomArnHandler({
2091+
awsCustomArn: profileArn,
2092+
awsRegion: "us-east-1",
2093+
awsCustomArnBaseModelId: BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL,
2094+
awsBedrock1MContext: true,
2095+
})
2096+
2097+
expect(handler.getModel().info).toMatchObject({
2098+
contextWindow: BEDROCK_DEFAULT_CONTEXT,
2099+
maxTokens: BEDROCK_MAX_TOKENS,
2100+
})
2101+
const input = await sendFirstRequest(handler)
2102+
expect(input.modelId).toBe(profileArn)
2103+
expect(input.additionalModelRequestFields).toBeUndefined()
2104+
expect(input.inferenceConfig?.temperature).toBeDefined()
2105+
})
2106+
2107+
it("keeps the named model for a foundation-model ARN even with an explicit Other choice", () => {
2108+
const handler = createCustomArnHandler({
2109+
awsCustomArn:
2110+
"arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0",
2111+
awsRegion: "us-east-1",
2112+
awsCustomArnBaseModelId: BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL,
2113+
})
2114+
2115+
expect(handler.getModel()).toMatchObject({
2116+
id: "anthropic.claude-3-5-sonnet-20241022-v2:0",
2117+
info: { contextWindow: bedrockModels["anthropic.claude-3-5-sonnet-20241022-v2:0"].contextWindow },
2118+
})
2119+
})
20852120
})
20862121

20872122
describe("isAdaptiveThinkingModel detection", () => {

‎src/api/providers/bedrock.ts‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
SERVICE_TIER_KEY,
3838
ApiProviderError,
3939
resolveBedrockCustomArnBaseModelId,
40+
BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL,
4041
} from "@roo-code/types"
4142
import { TelemetryService } from "@roo-code/telemetry"
4243

@@ -1139,8 +1140,26 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
11391140
}
11401141
}
11411142

1142-
private getCustomArnBaseModelId(): BedrockModelId | undefined {
1143-
return resolveBedrockCustomArnBaseModelId(this.options.awsCustomArn, this.options.awsCustomArnBaseModelId)
1143+
/**
1144+
* Model ID used to look up model info for a custom ARN. Foundation-model ARNs use the model they name;
1145+
* an explicit "Other" choice uses the generic fallback instead of a model named in the ARN.
1146+
*/
1147+
private getCustomArnLookupModelId(): string {
1148+
if (this.arnInfo.modelType === "foundation-model") {
1149+
return this.arnInfo.modelId
1150+
}
1151+
1152+
const baseModelId = resolveBedrockCustomArnBaseModelId(
1153+
this.options.awsCustomArn,
1154+
this.options.awsCustomArnBaseModelId,
1155+
)
1156+
if (baseModelId) {
1157+
return baseModelId
1158+
}
1159+
1160+
return this.options.awsCustomArnBaseModelId === BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL
1161+
? BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL
1162+
: this.arnInfo.modelId
11441163
}
11451164

11461165
/**
@@ -1149,7 +1168,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
11491168
*/
11501169
private getCapabilityModelId(modelId: string): string {
11511170
if (this.options.awsCustomArn && modelId === this.options.awsCustomArn) {
1152-
return this.getCustomArnBaseModelId() ?? this.arnInfo?.modelId ?? modelId
1171+
return this.getCustomArnLookupModelId()
11531172
}
11541173

11551174
return this.parseBaseModelId(modelId)
@@ -1242,10 +1261,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
12421261

12431262
// If custom ARN is provided, use it
12441263
if (this.options.awsCustomArn) {
1245-
// A foundation-model ARN already names the model that will be invoked.
1246-
const baseModelId =
1247-
this.arnInfo.modelType === "foundation-model" ? undefined : this.getCustomArnBaseModelId()
1248-
modelConfig = this.getModelById(baseModelId ?? this.arnInfo.modelId, this.arnInfo.modelType)
1264+
modelConfig = this.getModelById(this.getCustomArnLookupModelId(), this.arnInfo.modelType)
12491265

12501266
//If the user entered an ARN for a foundation-model they've done the same thing as picking from our list of options.
12511267
//We leave the model data matching the same as if a drop-down input method was used by not overwriting the model ID with the user input ARN

‎webview-ui/src/components/settings/providers/BedrockCustomArn.tsx‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
33

44
import {
55
type ProviderSettings,
6+
BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL,
67
BEDROCK_DEFAULT_CONTEXT,
78
BEDROCK_MAX_TOKENS,
89
bedrockModels,
10+
isBedrockFoundationModelArn,
911
resolveBedrockCustomArnBaseModelId,
1012
} from "@roo-code/types"
1113

1214
import { validateBedrockArn } from "@src/utils/validate"
1315
import { useAppTranslation } from "@src/i18n/TranslationContext"
1416
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@src/components/ui"
1517

16-
const OTHER_BASE_MODEL = "other"
1718
const baseModelIds = Object.keys(bedrockModels).sort((a, b) => a.localeCompare(b))
1819

1920
const toPositiveInteger = (value: string): number | undefined => {
@@ -36,24 +37,41 @@ export const BedrockCustomArn = ({ apiConfiguration, setApiConfigurationField }:
3637

3738
const baseModelId =
3839
resolveBedrockCustomArnBaseModelId(apiConfiguration.awsCustomArn, apiConfiguration.awsCustomArnBaseModelId) ??
39-
OTHER_BASE_MODEL
40+
BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL
41+
// A foundation-model ARN invokes the model it names, so the underlying model can't be changed.
42+
const isFoundationModelArn = isBedrockFoundationModelArn(apiConfiguration.awsCustomArn)
4043

41-
const onBaseModelChange = (value: string) => {
42-
setApiConfigurationField("awsCustomArnBaseModelId", value === OTHER_BASE_MODEL ? "" : value)
43-
// Different models have different output, thinking and context defaults.
44+
// Different models have different output, thinking and context defaults.
45+
const resetModelSettings = () => {
4446
setApiConfigurationField("modelMaxTokens", undefined)
4547
setApiConfigurationField("modelMaxThinkingTokens", undefined)
4648
setApiConfigurationField("reasoningEffort", undefined)
4749
setApiConfigurationField("awsModelContextWindow", undefined)
4850
}
4951

52+
const onBaseModelChange = (value: string) => {
53+
setApiConfigurationField("awsCustomArnBaseModelId", value)
54+
resetModelSettings()
55+
}
56+
57+
// The underlying model and limits belong to one ARN, so a different ARN starts from detection again.
58+
const onArnInput = (value: string) => {
59+
if (value === (apiConfiguration.awsCustomArn ?? "")) {
60+
return
61+
}
62+
setApiConfigurationField("awsCustomArn", value)
63+
setApiConfigurationField("awsCustomArnBaseModelId", "")
64+
resetModelSettings()
65+
}
66+
5067
return (
5168
<>
5269
<VSCodeTextField
5370
value={apiConfiguration?.awsCustomArn || ""}
54-
onInput={(e) => setApiConfigurationField("awsCustomArn", (e.target as HTMLInputElement).value)}
71+
onInput={(e) => onArnInput((e.target as HTMLInputElement).value)}
5572
placeholder={t("settings:placeholders.customArn")}
56-
className="w-full">
73+
className="w-full"
74+
data-testid="custom-arn-input">
5775
<label className="block font-medium mb-1">{t("settings:labels.customArn")}</label>
5876
</VSCodeTextField>
5977
<div className="text-sm text-vscode-descriptionForeground -mt-2">
@@ -78,12 +96,12 @@ export const BedrockCustomArn = ({ apiConfiguration, setApiConfigurationField }:
7896
)}
7997
<div data-testid="custom-arn-base-model">
8098
<label className="block font-medium mb-1">{t("settings:providers.awsCustomArnBaseModel")}</label>
81-
<Select value={baseModelId} onValueChange={onBaseModelChange}>
99+
<Select value={baseModelId} onValueChange={onBaseModelChange} disabled={isFoundationModelArn}>
82100
<SelectTrigger className="w-full">
83101
<SelectValue placeholder={t("settings:common.select")} />
84102
</SelectTrigger>
85103
<SelectContent>
86-
<SelectItem value={OTHER_BASE_MODEL}>
104+
<SelectItem value={BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL}>
87105
{t("settings:providers.awsCustomArnBaseModelOther")}
88106
</SelectItem>
89107
{baseModelIds.map((id) => (
@@ -97,7 +115,7 @@ export const BedrockCustomArn = ({ apiConfiguration, setApiConfigurationField }:
97115
{t("settings:providers.awsCustomArnBaseModelDesc")}
98116
</div>
99117
</div>
100-
{baseModelId === OTHER_BASE_MODEL && (
118+
{baseModelId === BEDROCK_CUSTOM_ARN_OTHER_BASE_MODEL && (
101119
<>
102120
<VSCodeTextField
103121
value={apiConfiguration.awsModelContextWindow?.toString() ?? ""}

0 commit comments

Comments
 (0)