11// src/adapters/bedrock/index.ts
22
3- import { BedrockModelHandler } from "@/adapters/bedrock-handlers/base-handler" ;
4- import { AnthropicBedrockHandler } from "@/adapters/bedrock-handlers/anthropic-handler" ;
5- import { MetaBedrockHandler } from "@/adapters/bedrock-handlers/meta-llama-handler" ;
6- import { CohereBedrockHandler } from "@/adapters/bedrock-handlers/cohere-handler" ;
7- import { MistralBedrockHandler } from "@/adapters/bedrock-handlers/mistral-handler" ;
8- import { OpenAIBedrockHandler } from "@/adapters/bedrock-handlers/open-ai-handler" ;
9- import {
10- BedrockRequest ,
11- BedrockRequestBody ,
12- } from "@/types/providers/bedrock/common.type" ;
13- import { ProviderAdapter } from "./base" ;
14- import {
15- StandardRequest ,
16- StandardResponse ,
17- } from "@/types/standard-message.type" ;
18- import {
19- BedrockMessageResponse ,
20- ProviderRequest ,
21- } from "@/types/providers/common.type" ;
3+ import { BedrockModelHandler } from '@/adapters/bedrock-handlers/base-handler' ;
4+ import { AnthropicBedrockHandler } from '@/adapters/bedrock-handlers/anthropic-handler' ;
5+ import { MetaBedrockHandler } from '@/adapters/bedrock-handlers/meta-llama-handler' ;
6+ import { CohereBedrockHandler } from '@/adapters/bedrock-handlers/cohere-handler' ;
7+ import { MistralBedrockHandler } from '@/adapters/bedrock-handlers/mistral-handler' ;
8+ import { OpenAIBedrockHandler } from '@/adapters/bedrock-handlers/open-ai-handler' ;
9+ import { BedrockRequest , BedrockRequestBody } from '@/types/providers/bedrock/common.type' ;
10+ import { ProviderAdapter } from './base' ;
11+ import { StandardRequest , StandardResponse } from '@/types/standard-message.type' ;
12+ import { BedrockMessageResponse , ProviderRequest } from '@/types/providers/common.type' ;
2213
2314export class BedrockAdapter extends ProviderAdapter {
2415 private handlers : BedrockModelHandler [ ] ;
@@ -40,30 +31,26 @@ export class BedrockAdapter extends ProviderAdapter {
4031 canHandleRequest ( req : ProviderRequest ) : boolean {
4132 let confidence = 0 ;
4233
43- if (
44- req . headers [ "x-amzn-bedrock-model-id" ] ||
45- req . headers [ "x-amz-bedrock-model-id" ]
46- ) {
34+ if ( req . headers [ 'x-amzn-bedrock-model-id' ] || req . headers [ 'x-amz-bedrock-model-id' ] ) {
4735 confidence += 3 ;
4836 }
4937
50- if ( req . headers [ " authorization" ] ?. includes ( " AWS4-HMAC-SHA256" ) ) {
38+ if ( req . headers [ ' authorization' ] ?. includes ( ' AWS4-HMAC-SHA256' ) ) {
5139 confidence += 2 ;
5240 }
5341
54- if ( req . headers [ " x-amz-security-token" ] ) {
42+ if ( req . headers [ ' x-amz-security-token' ] ) {
5543 confidence += 2 ;
5644 }
5745
58- const bedrockUrlPattern =
59- / ^ \/ m o d e l \/ [ a - z A - Z 0 - 9 \. \- \_ : ] + \/ i n v o k e (?: - w i t h - r e s p o n s e - s t r e a m ) ? $ / ;
46+ const bedrockUrlPattern = / ^ \/ m o d e l \/ [ a - z A - Z 0 - 9 . _ : - ] + \/ i n v o k e (?: - w i t h - r e s p o n s e - s t r e a m ) ? $ / ;
6047 if ( bedrockUrlPattern . test ( req . url ) ) {
6148 confidence += 1 ;
6249 }
6350
6451 if (
65- req . headers [ " host" ] ?. includes ( " bedrock-runtime" ) ||
66- req . headers [ " host" ] ?. includes ( " bedrock.amazonaws.com" )
52+ req . headers [ ' host' ] ?. includes ( ' bedrock-runtime' ) ||
53+ req . headers [ ' host' ] ?. includes ( ' bedrock.amazonaws.com' )
6754 ) {
6855 confidence += 2 ;
6956 }
@@ -73,16 +60,15 @@ export class BedrockAdapter extends ProviderAdapter {
7360
7461 getModelId ( req : ProviderRequest ) : string {
7562 const headerModelId =
76- req . headers [ "x-amzn-bedrock-model-id" ] ||
77- req . headers [ "x-amz-bedrock-model-id" ] ;
63+ req . headers [ 'x-amzn-bedrock-model-id' ] || req . headers [ 'x-amz-bedrock-model-id' ] ;
7864
7965 if ( headerModelId ) {
8066 return headerModelId ;
8167 }
8268
83- const match = req . url . match ( / \/ m o d e l \/ ( [ ^ \ /] + ) \/ i n v o k e / ) ;
69+ const match = req . url . match ( / \/ m o d e l \/ ( [ ^ / ] + ) \/ i n v o k e / ) ;
8470 if ( ! match ) {
85- throw new Error ( " Invalid Bedrock URL format" ) ;
71+ throw new Error ( ' Invalid Bedrock URL format' ) ;
8672 }
8773 return match [ 1 ] ;
8874 }
@@ -94,17 +80,15 @@ export class BedrockAdapter extends ProviderAdapter {
9480 const handler = this . handlers . find ( ( h ) => h . canHandleRequest ( modelId ) ) ;
9581
9682 if ( ! handler ) {
97- console . warn (
98- `No handler found for Bedrock model: ${ modelId } , using Anthropic as fallback`
99- ) ;
83+ console . warn ( `No handler found for Bedrock model: ${ modelId } , using Anthropic as fallback` ) ;
10084 return this . handlers [ 0 ] ; // Fallback to Anthropic
10185 }
10286
10387 return handler ;
10488 }
10589
10690 transformRequest ( req : BedrockRequest ) : StandardRequest {
107- const body = typeof req . body === " string" ? JSON . parse ( req . body ) : req . body ;
91+ const body = typeof req . body === ' string' ? JSON . parse ( req . body ) : req . body ;
10892 const modelId = this . getModelId ( req ) ;
10993
11094 // Store for use in transformResponse
0 commit comments