Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@ai-sdk/provider": "^3.0.10",
"@aws-sdk/client-bedrock": "^3.1057.0",
"@aws-sdk/client-s3": "^3.1062.0",
"@aws-sdk/credential-providers": "^3.1057.0",
"@duckdb/node-api": "1.5.3-r.1",
"@e2b/code-interpreter": "^1.5.1",
"@electron-toolkit/preload": "^3.0.2",
Expand Down
23 changes: 17 additions & 6 deletions src/main/presenter/llmProviderPresenter/aiSdk/providerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { createGoogleGenerativeAI } from '@ai-sdk/google'
import { createVertex } from '@ai-sdk/google-vertex'
import { createOpenAI } from '@ai-sdk/openai'
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'
import { fromNodeProviderChain } from '@aws-sdk/credential-providers'
import { ProxyAgent } from 'undici'
import { proxyConfig } from '../../proxyConfig'
import { createReasoningMiddleware } from './middlewares/reasoningMiddleware'
Expand Down Expand Up @@ -640,16 +641,26 @@ export function createAiSdkProviderContext(

case 'aws-bedrock': {
const bedrockProvider = params.provider as AWS_BEDROCK_PROVIDER
const provider = createAmazonBedrock({
const credential = bedrockProvider.credential
const useProfileAuth = credential?.authMode === 'profile' && credential?.profile

const bedrockOptions: Record<string, unknown> = {
apiKey: bedrockProvider.apiKey || undefined,
baseURL: bedrockProvider.baseUrl || undefined,
region: bedrockProvider.credential?.region || process.env.AWS_REGION || 'us-east-1',
accessKeyId: bedrockProvider.credential?.accessKeyId || process.env.AWS_ACCESS_KEY_ID,
secretAccessKey:
bedrockProvider.credential?.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY,
region: credential?.region || process.env.AWS_REGION || 'us-east-1',
headers: params.defaultHeaders,
fetch
})
}

if (useProfileAuth) {
bedrockOptions.credentialProvider = fromNodeProviderChain({ profile: credential.profile })
} else {
bedrockOptions.accessKeyId = credential?.accessKeyId || process.env.AWS_ACCESS_KEY_ID
bedrockOptions.secretAccessKey =
credential?.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY
}

const provider = createAmazonBedrock(bedrockOptions as any)

return {
providerOptionsKey: 'bedrock',
Expand Down
6 changes: 1 addition & 5 deletions src/main/presenter/llmProviderPresenter/providerRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,11 @@ const PROVIDER_ID_REGISTRY = new Map<string, AiSdkProviderDefinition>([
runtimeKind: 'aws-bedrock',
behaviorPreset: 'anthropic',
modelSource: 'bedrock',
checkStrategy: 'generate-text',
checkStrategy: 'fetch-models',
credentialStrategy: 'bedrock',
keyStatusStrategy: 'none',
routeStrategy: 'none',
embeddingStrategy: 'none',
checkModelId: 'anthropic.claude-3-5-sonnet-20240620-v1:0',
checkPrompt: 'Hi',
checkTemperature: 0.2,
checkMaxTokens: 16,
providerDbSourceId: 'amazon-bedrock'
})
],
Expand Down
50 changes: 34 additions & 16 deletions src/main/presenter/llmProviderPresenter/providers/aiSdkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
VERTEX_PROVIDER
} from '@shared/presenter'
import { BedrockClient, ListFoundationModelsCommand } from '@aws-sdk/client-bedrock'
import { fromNodeProviderChain } from '@aws-sdk/credential-providers'
import { ProxyAgent } from 'undici'
import { BaseLLMProvider, SUMMARY_TITLES_PROMPT } from '../baseProvider'
import {
Expand Down Expand Up @@ -1743,25 +1744,37 @@ export class AiSdkProvider extends BaseLLMProvider {

private async fetchBedrockModels(): Promise<MODEL_META[]> {
const provider = this.provider as AWS_BEDROCK_PROVIDER
const accessKeyId = provider.credential?.accessKeyId || process.env.BEDROCK_ACCESS_KEY_ID
const secretAccessKey =
provider.credential?.secretAccessKey || process.env.BEDROCK_SECRET_ACCESS_KEY
const region = provider.credential?.region || process.env.BEDROCK_REGION
const credential = provider.credential
const region = credential?.region || process.env.AWS_REGION
const useProfile = credential?.authMode === 'profile' && credential?.profile

if (!useProfile) {
const accessKeyId = credential?.accessKeyId || process.env.AWS_ACCESS_KEY_ID
const secretAccessKey = credential?.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY
if (!accessKeyId || !secretAccessKey || !region) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return this.mapConfigDbModels(this.definition.providerDbSourceId).filter((model) =>
model.id.startsWith('anthropic.')
)
}
}

if (!accessKeyId || !secretAccessKey || !region) {
if (!region) {
return this.mapConfigDbModels(this.definition.providerDbSourceId).filter((model) =>
model.id.startsWith('anthropic.')
)
}

try {
const client = new BedrockClient({
credentials: {
accessKeyId,
secretAccessKey
},
region
})
const clientConfig: Record<string, unknown> = { region }
if (useProfile) {
clientConfig.credentials = fromNodeProviderChain({ profile: credential.profile })
} else {
clientConfig.credentials = {
accessKeyId: credential?.accessKeyId || process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: credential?.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY
}
}
const client = new BedrockClient(clientConfig as any)
const response = await client.send(new ListFoundationModelsCommand({}))
return (
response.modelSummaries
Expand Down Expand Up @@ -2208,10 +2221,15 @@ export class AiSdkProvider extends BaseLLMProvider {
}
case 'bedrock': {
const provider = this.provider as AWS_BEDROCK_PROVIDER
const accessKeyId = provider.credential?.accessKeyId || process.env.BEDROCK_ACCESS_KEY_ID
const secretAccessKey =
provider.credential?.secretAccessKey || process.env.BEDROCK_SECRET_ACCESS_KEY
const region = provider.credential?.region || process.env.BEDROCK_REGION
const credential = provider.credential
const region = credential?.region || process.env.AWS_REGION

if (credential?.authMode === 'profile') {
return credential.profile && region ? null : 'Missing AWS profile name or region'
}

const accessKeyId = credential?.accessKeyId || process.env.AWS_ACCESS_KEY_ID
const secretAccessKey = credential?.secretAccessKey || process.env.AWS_SECRET_ACCESS_KEY
return accessKeyId && secretAccessKey && region ? null : 'Missing AWS Bedrock credentials'
}
case 'none':
Expand Down
Loading