@@ -151,20 +151,37 @@ function buildInvokeUrl(region: string, runtimeArn: string): string {
151151}
152152
153153/**
154- * Invoke an AgentCore Runtime using bearer token auth (raw HTTP, no SigV4) .
155- * Used when the runtime has CUSTOM_JWT authorizer configured .
154+ * Build headers for bearer- token invoke requests .
155+ * Shared by both streaming and non-streaming invoke paths .
156156 */
157- async function invokeWithBearerTokenStreaming ( options : InvokeAgentRuntimeOptions ) : Promise < StreamingInvokeResult > {
158- const url = buildInvokeUrl ( options . region , options . runtimeArn ) ;
157+ export function buildBearerInvokeHeaders (
158+ options : Pick < InvokeAgentRuntimeOptions , 'bearerToken' | 'sessionId' | 'userId' | 'headers' > ,
159+ accept : string
160+ ) : Record < string , string > {
159161 const headers : Record < string , string > = {
160162 Authorization : `Bearer ${ options . bearerToken } ` ,
161163 'Content-Type' : 'application/json' ,
162- Accept : 'application/json, text/event-stream' ,
164+ Accept : accept ,
163165 } ;
164166 if ( options . sessionId ) {
165167 headers [ 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id' ] = options . sessionId ;
166168 }
167169 headers [ 'X-Amzn-Bedrock-AgentCore-Runtime-User-Id' ] = options . userId ?? DEFAULT_RUNTIME_USER_ID ;
170+ if ( options . headers ) {
171+ for ( const [ name , value ] of Object . entries ( options . headers ) ) {
172+ headers [ name ] = value ;
173+ }
174+ }
175+ return headers ;
176+ }
177+
178+ /**
179+ * Invoke an AgentCore Runtime using bearer token auth (raw HTTP, no SigV4).
180+ * Used when the runtime has CUSTOM_JWT authorizer configured.
181+ */
182+ async function invokeWithBearerTokenStreaming ( options : InvokeAgentRuntimeOptions ) : Promise < StreamingInvokeResult > {
183+ const url = buildInvokeUrl ( options . region , options . runtimeArn ) ;
184+ const headers = buildBearerInvokeHeaders ( options , 'application/json, text/event-stream' ) ;
168185
169186 const res = await fetch ( url , {
170187 method : 'POST' ,
@@ -250,15 +267,7 @@ async function invokeWithBearerTokenStreaming(options: InvokeAgentRuntimeOptions
250267 */
251268async function invokeWithBearerToken ( options : InvokeAgentRuntimeOptions ) : Promise < InvokeAgentRuntimeResult > {
252269 const url = buildInvokeUrl ( options . region , options . runtimeArn ) ;
253- const headers : Record < string , string > = {
254- Authorization : `Bearer ${ options . bearerToken } ` ,
255- 'Content-Type' : 'application/json' ,
256- Accept : 'application/json' ,
257- } ;
258- if ( options . sessionId ) {
259- headers [ 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id' ] = options . sessionId ;
260- }
261- headers [ 'X-Amzn-Bedrock-AgentCore-Runtime-User-Id' ] = options . userId ?? DEFAULT_RUNTIME_USER_ID ;
270+ const headers = buildBearerInvokeHeaders ( options , 'application/json' ) ;
262271
263272 const res = await fetch ( url , {
264273 method : 'POST' ,
0 commit comments