From 79b76fd7835d5d5952a05a0d91de7ea4a042b68f Mon Sep 17 00:00:00 2001 From: clicube Date: Thu, 1 May 2025 15:47:28 +0900 Subject: [PATCH] feat: Support AWS Bedrock Application Inference Profiles for Cost Tracking (#2078) * feat: Add support for custom model ID in AWS Bedrock provider * preserve settings when switching Act-Plan modes * Use base model ID for ApiHandler behavior determination when using a custom model on AWS Bedrock. --- .changeset/clean-needles-deliver.md | 5 + src/api/providers/bedrock.ts | 29 ++++- src/core/controller/index.ts | 22 +++- src/core/storage/state-keys.ts | 4 + src/core/storage/state.ts | 18 ++- src/shared/api.ts | 2 + .../src/components/settings/ApiOptions.tsx | 106 +++++++++++++++++- 7 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 .changeset/clean-needles-deliver.md diff --git a/.changeset/clean-needles-deliver.md b/.changeset/clean-needles-deliver.md new file mode 100644 index 00000000..2c0e5ad2 --- /dev/null +++ b/.changeset/clean-needles-deliver.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +Add support for custom model ID in AWS Bedrock provider, enabling use of Application Inference Profile. diff --git a/src/api/providers/bedrock.ts b/src/api/providers/bedrock.ts index df5a1dd3..325433f4 100644 --- a/src/api/providers/bedrock.ts +++ b/src/api/providers/bedrock.ts @@ -28,20 +28,26 @@ export class AwsBedrockHandler implements ApiHandler { const modelId = await this.getModelId() const model = this.getModel() + // This baseModelId is used to indicate the capabilities of the model. + // If the user selects a custom model, baseModelId will be set to the base model ID of the custom model. + // Otherwise, baseModelId will be the same as modelId. + const baseModelId = + (this.options.awsBedrockCustomSelected ? this.options.awsBedrockCustomModelBaseId : modelId) || modelId + // Check if this is an Amazon Nova model - if (modelId.includes("amazon.nova")) { + if (baseModelId.includes("amazon.nova")) { yield* this.createNovaMessage(systemPrompt, messages, modelId, model) return } // Check if this is a Deepseek model - if (modelId.includes("deepseek")) { + if (baseModelId.includes("deepseek")) { yield* this.createDeepseekMessage(systemPrompt, messages, modelId, model) return } const budget_tokens = this.options.thinkingBudgetTokens || 0 - const reasoningOn = modelId.includes("3-7") && budget_tokens !== 0 ? true : false + const reasoningOn = baseModelId.includes("3-7") && budget_tokens !== 0 ? true : false // Get model info and message indices for caching const userMsgIndices = messages.reduce((acc, msg, index) => (msg.role === "user" ? [...acc, index] : acc), [] as number[]) @@ -167,12 +173,23 @@ export class AwsBedrockHandler implements ApiHandler { } } - getModel(): { id: BedrockModelId; info: ModelInfo } { + getModel(): { id: string; info: ModelInfo } { const modelId = this.options.apiModelId if (modelId && modelId in bedrockModels) { const id = modelId as BedrockModelId return { id, info: bedrockModels[id] } } + + const customSelected = this.options.awsBedrockCustomSelected + const baseModel = this.options.awsBedrockCustomModelBaseId + if (customSelected && modelId && baseModel && baseModel in bedrockModels) { + // Use the user-input model ID but inherit capabilities from the base model + return { + id: modelId, + info: bedrockModels[baseModel], + } + } + return { id: bedrockDefaultModelId, info: bedrockModels[bedrockDefaultModelId], @@ -290,7 +307,7 @@ export class AwsBedrockHandler implements ApiHandler { systemPrompt: string, messages: Anthropic.Messages.MessageParam[], modelId: string, - model: { id: BedrockModelId; info: ModelInfo }, + model: { id: string; info: ModelInfo }, ): ApiStream { // Get Bedrock client with proper credentials const client = await this.getBedrockClient() @@ -482,7 +499,7 @@ export class AwsBedrockHandler implements ApiHandler { systemPrompt: string, messages: Anthropic.Messages.MessageParam[], modelId: string, - model: { id: BedrockModelId; info: ModelInfo }, + model: { id: string; info: ModelInfo }, ): ApiStream { // Get Bedrock client with proper credentials const client = await this.getBedrockClient() diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 83183cbb..0d0c841c 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -832,6 +832,8 @@ export class Controller { previousModeVsCodeLmModelSelector: newVsCodeLmModelSelector, previousModeThinkingBudgetTokens: newThinkingBudgetTokens, previousModeReasoningEffort: newReasoningEffort, + previousModeAwsBedrockCustomSelected: newAwsBedrockCustomSelected, + previousModeAwsBedrockCustomModelBaseId: newAwsBedrockCustomModelBaseId, planActSeparateModelsSetting, } = await getAllExtensionState(this.context) @@ -844,7 +846,6 @@ export class Controller { await updateGlobalState(this.context, "previousModeReasoningEffort", apiConfiguration.reasoningEffort) switch (apiConfiguration.apiProvider) { case "anthropic": - case "bedrock": case "vertex": case "gemini": case "asksage": @@ -854,6 +855,19 @@ export class Controller { case "xai": await updateGlobalState(this.context, "previousModeModelId", apiConfiguration.apiModelId) break + case "bedrock": + await updateGlobalState(this.context, "previousModeModelId", apiConfiguration.apiModelId) + await updateGlobalState( + this.context, + "previousModeAwsBedrockCustomSelected", + apiConfiguration.awsBedrockCustomSelected, + ) + await updateGlobalState( + this.context, + "previousModeAwsBedrockCustomModelBaseId", + apiConfiguration.awsBedrockCustomModelBaseId, + ) + break case "openrouter": case "cline": await updateGlobalState(this.context, "previousModeModelId", apiConfiguration.openRouterModelId) @@ -899,7 +913,6 @@ export class Controller { await updateGlobalState(this.context, "reasoningEffort", newReasoningEffort) switch (newApiProvider) { case "anthropic": - case "bedrock": case "vertex": case "gemini": case "asksage": @@ -909,6 +922,11 @@ export class Controller { case "xai": await updateGlobalState(this.context, "apiModelId", newModelId) break + case "bedrock": + await updateGlobalState(this.context, "apiModelId", newModelId) + await updateGlobalState(this.context, "awsBedrockCustomSelected", newAwsBedrockCustomSelected) + await updateGlobalState(this.context, "awsBedrockCustomModelBaseId", newAwsBedrockCustomModelBaseId) + break case "openrouter": case "cline": await updateGlobalState(this.context, "openRouterModelId", newModelId) diff --git a/src/core/storage/state-keys.ts b/src/core/storage/state-keys.ts index b0be5f8f..63b914ed 100644 --- a/src/core/storage/state-keys.ts +++ b/src/core/storage/state-keys.ts @@ -29,6 +29,8 @@ export type GlobalStateKey = | "awsBedrockEndpoint" | "awsProfile" | "awsUseProfile" + | "awsBedrockCustomSelected" + | "awsBedrockCustomModelBaseId" | "vertexProjectId" | "vertexRegion" | "lastShownAnnouncementId" @@ -60,6 +62,8 @@ export type GlobalStateKey = | "previousModeThinkingBudgetTokens" | "previousModeReasoningEffort" | "previousModeVsCodeLmModelSelector" + | "previousModeAwsBedrockCustomSelected" + | "previousModeAwsBedrockCustomModelBaseId" | "previousModeModelInfo" | "liteLlmBaseUrl" | "liteLlmModelId" diff --git a/src/core/storage/state.ts b/src/core/storage/state.ts index 857ba780..1a971ee7 100644 --- a/src/core/storage/state.ts +++ b/src/core/storage/state.ts @@ -3,7 +3,7 @@ import { DEFAULT_CHAT_SETTINGS } from "@shared/ChatSettings" import { DEFAULT_BROWSER_SETTINGS } from "@shared/BrowserSettings" import { DEFAULT_AUTO_APPROVAL_SETTINGS } from "@shared/AutoApprovalSettings" import { GlobalStateKey, SecretKey } from "./state-keys" -import { ApiConfiguration, ApiProvider, ModelInfo } from "@shared/api" +import { ApiConfiguration, ApiProvider, BedrockModelId, ModelInfo } from "@shared/api" import { HistoryItem } from "@shared/HistoryItem" import { AutoApprovalSettings } from "@shared/AutoApprovalSettings" import { BrowserSettings } from "@shared/BrowserSettings" @@ -67,6 +67,8 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { awsBedrockEndpoint, awsProfile, awsUseProfile, + awsBedrockCustomSelected, + awsBedrockCustomModelBaseId, vertexProjectId, vertexRegion, openAiBaseUrl, @@ -113,6 +115,8 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { previousModeVsCodeLmModelSelector, previousModeThinkingBudgetTokens, previousModeReasoningEffort, + previousModeAwsBedrockCustomSelected, + previousModeAwsBedrockCustomModelBaseId, qwenApiLine, liteLlmApiKey, telemetrySetting, @@ -142,6 +146,8 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { getGlobalState(context, "awsBedrockEndpoint") as Promise, getGlobalState(context, "awsProfile") as Promise, getGlobalState(context, "awsUseProfile") as Promise, + getGlobalState(context, "awsBedrockCustomSelected") as Promise, + getGlobalState(context, "awsBedrockCustomModelBaseId") as Promise, getGlobalState(context, "vertexProjectId") as Promise, getGlobalState(context, "vertexRegion") as Promise, getGlobalState(context, "openAiBaseUrl") as Promise, @@ -188,6 +194,8 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { getGlobalState(context, "previousModeVsCodeLmModelSelector") as Promise, getGlobalState(context, "previousModeThinkingBudgetTokens") as Promise, getGlobalState(context, "previousModeReasoningEffort") as Promise, + getGlobalState(context, "previousModeAwsBedrockCustomSelected") as Promise, + getGlobalState(context, "previousModeAwsBedrockCustomModelBaseId") as Promise, getGlobalState(context, "qwenApiLine") as Promise, getSecret(context, "liteLlmApiKey") as Promise, getGlobalState(context, "telemetrySetting") as Promise, @@ -258,6 +266,8 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { awsBedrockEndpoint, awsProfile, awsUseProfile, + awsBedrockCustomSelected, + awsBedrockCustomModelBaseId, vertexProjectId, vertexRegion, openAiBaseUrl, @@ -318,6 +328,8 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { previousModeVsCodeLmModelSelector, previousModeThinkingBudgetTokens, previousModeReasoningEffort, + previousModeAwsBedrockCustomSelected, + previousModeAwsBedrockCustomModelBaseId, mcpMarketplaceEnabled, telemetrySetting: telemetrySetting || "unset", planActSeparateModelsSetting, @@ -340,6 +352,8 @@ export async function updateApiConfiguration(context: vscode.ExtensionContext, a awsBedrockEndpoint, awsProfile, awsUseProfile, + awsBedrockCustomSelected, + awsBedrockCustomModelBaseId, vertexProjectId, vertexRegion, openAiBaseUrl, @@ -397,6 +411,8 @@ export async function updateApiConfiguration(context: vscode.ExtensionContext, a await updateGlobalState(context, "awsBedrockEndpoint", awsBedrockEndpoint) await updateGlobalState(context, "awsProfile", awsProfile) await updateGlobalState(context, "awsUseProfile", awsUseProfile) + await updateGlobalState(context, "awsBedrockCustomSelected", awsBedrockCustomSelected) + await updateGlobalState(context, "awsBedrockCustomModelBaseId", awsBedrockCustomModelBaseId) await updateGlobalState(context, "vertexProjectId", vertexProjectId) await updateGlobalState(context, "vertexRegion", vertexRegion) await updateGlobalState(context, "openAiBaseUrl", openAiBaseUrl) diff --git a/src/shared/api.ts b/src/shared/api.ts index ad730b0a..140bf89f 100644 --- a/src/shared/api.ts +++ b/src/shared/api.ts @@ -47,6 +47,8 @@ export interface ApiHandlerOptions { awsUseProfile?: boolean awsProfile?: string awsBedrockEndpoint?: string + awsBedrockCustomSelected?: boolean + awsBedrockCustomModelBaseId?: BedrockModelId vertexProjectId?: string vertexRegion?: string openAiBaseUrl?: string diff --git a/webview-ui/src/components/settings/ApiOptions.tsx b/webview-ui/src/components/settings/ApiOptions.tsx index 27093d14..8519bd88 100644 --- a/webview-ui/src/components/settings/ApiOptions.tsx +++ b/webview-ui/src/components/settings/ApiOptions.tsx @@ -769,6 +769,101 @@ const ApiOptions = ({ )}

+ + + { + const isCustom = e.target.value === "custom" + setApiConfiguration({ + ...apiConfiguration, + apiModelId: isCustom ? "" : e.target.value, + awsBedrockCustomSelected: isCustom, + awsBedrockCustomModelBaseId: bedrockDefaultModelId, + }) + }} + style={{ width: "100%" }}> + Select a model... + {Object.keys(bedrockModels).map((modelId) => ( + + {modelId} + + ))} + Custom + + + {apiConfiguration?.awsBedrockCustomSelected && ( +
+

+ Select "Custom" when using the Application Inference Profile in Bedrock. Enter the Application + Inference Profile ID in the Model ID field. However, be sure to encode the / in the ARN as %2F. +
+ Example: arn:aws:bedrock:us-west-2:<AWS Account + ID>:application-inference-profile%2Fxxxxxxxxxxxx +

+ + + + + + Select a model... + {Object.keys(bedrockModels).map((modelId) => ( + + {modelId} + + ))} + + +
+ )} + {(selectedModelId === "anthropic.claude-3-7-sonnet-20250219-v1:0" || + (apiConfiguration?.awsBedrockCustomSelected && + apiConfiguration?.awsBedrockCustomModelBaseId === "anthropic.claude-3-7-sonnet-20250219-v1:0")) && ( + + )} + )} @@ -1702,6 +1797,7 @@ const ApiOptions = ({ selectedProvider !== "vscode-lm" && selectedProvider !== "litellm" && selectedProvider !== "requesty" && + selectedProvider !== "bedrock" && showModelOptions && ( <> @@ -1709,7 +1805,6 @@ const ApiOptions = ({ Model {selectedProvider === "anthropic" && createDropdown(anthropicModels)} - {selectedProvider === "bedrock" && createDropdown(bedrockModels)} {selectedProvider === "vertex" && createDropdown(vertexModels)} {selectedProvider === "gemini" && createDropdown(geminiModels)} {selectedProvider === "openai-native" && createDropdown(openAiNativeModels)} @@ -1726,7 +1821,6 @@ const ApiOptions = ({ {((selectedProvider === "anthropic" && selectedModelId === "claude-3-7-sonnet-20250219") || - (selectedProvider === "bedrock" && selectedModelId === "anthropic.claude-3-7-sonnet-20250219-v1:0") || (selectedProvider === "vertex" && selectedModelId === "claude-3-7-sonnet@20250219")) && ( )} @@ -2048,6 +2142,14 @@ export function normalizeApiConfiguration(apiConfiguration?: ApiConfiguration): case "anthropic": return getProviderData(anthropicModels, anthropicDefaultModelId) case "bedrock": + if (apiConfiguration?.awsBedrockCustomSelected) { + const baseModelId = apiConfiguration.awsBedrockCustomModelBaseId + return { + selectedProvider: provider, + selectedModelId: modelId || bedrockDefaultModelId, + selectedModelInfo: (baseModelId && bedrockModels[baseModelId]) || bedrockModels[bedrockDefaultModelId], + } + } return getProviderData(bedrockModels, bedrockDefaultModelId) case "vertex": return getProviderData(vertexModels, vertexDefaultModelId)