fix(ai): type bedrock bearer auth middleware

This commit is contained in:
Armin Ronacher
2026-04-16 23:50:29 +02:00
parent 22085a9a17
commit b789900570

View File

@@ -15,11 +15,14 @@ import {
type ConverseStreamMetadataEvent, type ConverseStreamMetadataEvent,
ImageFormat, ImageFormat,
type Message, type Message,
type ServiceInputTypes,
type ServiceOutputTypes,
type SystemContentBlock, type SystemContentBlock,
type ToolChoice, type ToolChoice,
type ToolConfiguration, type ToolConfiguration,
ToolResultStatus, ToolResultStatus,
} from "@aws-sdk/client-bedrock-runtime"; } from "@aws-sdk/client-bedrock-runtime";
import type { FinalizeRequestMiddleware } from "@smithy/types";
import { calculateCost } from "../models.js"; import { calculateCost } from "../models.js";
import type { import type {
@@ -184,18 +187,30 @@ export const streamBedrock: StreamFunction<"bedrock-converse-stream", BedrockOpt
// Inject bearer token middleware after SigV4 signing // Inject bearer token middleware after SigV4 signing
if (bearerToken) { if (bearerToken) {
client.middlewareStack.addRelativeTo( const bearerTokenAuthMiddleware: FinalizeRequestMiddleware<ServiceInputTypes, ServiceOutputTypes> =
(next) => async (args: any) => { (next) => async (args) => {
if (args.request?.headers) { const request = args.request;
args.request.headers["authorization"] = `Bearer ${bearerToken}`; if (
delete args.request.headers["x-amz-date"]; typeof request === "object" &&
delete args.request.headers["x-amz-security-token"]; request !== null &&
delete args.request.headers["x-amz-content-sha256"]; "headers" in request &&
typeof request.headers === "object" &&
request.headers !== null
) {
const headers = request.headers as Record<string, string>;
headers.authorization = `Bearer ${bearerToken}`;
delete headers["x-amz-date"];
delete headers["x-amz-security-token"];
delete headers["x-amz-content-sha256"];
} }
return next(args); return next(args);
}, };
{ relation: "after", toMiddleware: "awsAuthMiddleware", name: "bearerTokenAuth" },
); client.middlewareStack.addRelativeTo(bearerTokenAuthMiddleware, {
relation: "after",
toMiddleware: "awsAuthMiddleware",
name: "bearerTokenAuth",
});
} }
const cacheRetention = resolveCacheRetention(options.cacheRetention); const cacheRetention = resolveCacheRetention(options.cacheRetention);