@@ -9,7 +9,10 @@ import {
99 ConverseStreamCommandOutput ,
1010 ImageFormat ,
1111 SystemContentBlock ,
12+ Tool ,
1213 ToolChoice ,
14+ ToolConfiguration ,
15+ ToolSpecification ,
1316} from '@aws-sdk/client-bedrock-runtime'
1417import { ChatCompletionMessageToolCall } from 'openai/resources/index'
1518
@@ -29,6 +32,7 @@ import { BaseHandler } from './base.js'
2932import { InputError , InvariantError , MIMEType } from './types.js'
3033import {
3134 consoleWarn ,
35+ convertMessageContentToString ,
3236 fetchThenParseImage ,
3337 getTimestamp ,
3438 normalizeTemperature ,
@@ -84,6 +88,7 @@ const toChatCompletionChoiceMessage = (
8488) : CompletionResponse [ 'choices' ] [ 0 ] [ 'message' ] => {
8589 if ( output ?. message ?. content === undefined ) {
8690 return {
91+ refusal : null ,
8792 content : '' ,
8893 role : 'assistant' ,
8994 }
@@ -146,13 +151,15 @@ const toChatCompletionChoiceMessage = (
146151 : ''
147152 return {
148153 role,
154+ refusal : null ,
149155 content : messageContent ,
150156 tool_calls : toolCalls ,
151157 }
152158 } else {
153159 const content = textBlocks . map ( ( textBlock ) => textBlock . text ) . join ( '\n' )
154160 return {
155161 role,
162+ refusal : null ,
156163 content,
157164 tool_calls : toolCalls ,
158165 }
@@ -187,7 +194,10 @@ export const convertMessages = async (
187194 const systemMessages : Array < SystemContentBlock > = [ ]
188195 if ( supportsSystemMessages ( model ) ) {
189196 while ( clonedMessages . length > 0 && clonedMessages [ 0 ] . role === 'system' ) {
190- systemMessages . push ( { text : clonedMessages [ 0 ] . content } )
197+ const messageContent = convertMessageContentToString (
198+ clonedMessages [ 0 ] . content
199+ )
200+ systemMessages . push ( { text : messageContent } )
191201 clonedMessages . shift ( )
192202 }
193203 }
@@ -247,7 +257,7 @@ export const convertMessages = async (
247257 toolUseId : message . tool_call_id ,
248258 content : [
249259 {
250- text : message . content ,
260+ text : convertMessageContentToString ( message . content ) ,
251261 } ,
252262 ] ,
253263 } ,
@@ -288,7 +298,7 @@ export const convertMessages = async (
288298 const text = makeTextContent ( message . role , e . text )
289299 return {
290300 text,
291- }
301+ } as ContentBlock . TextMember
292302 } else {
293303 const parsedImage = await fetchThenParseImage ( e . image_url . url )
294304 return {
@@ -298,7 +308,7 @@ export const convertMessages = async (
298308 bytes : Buffer . from ( parsedImage . content , 'base64' ) ,
299309 } ,
300310 } ,
301- }
311+ } as ContentBlock . ImageMember
302312 }
303313 } )
304314 )
@@ -354,27 +364,24 @@ export const convertToolParams = (
354364 return undefined
355365 }
356366
357- const convertedTools =
367+ const convertedTools : ( Tool | undefined ) [ ] =
358368 tools . length > 0
359369 ? tools . map ( ( tool ) => {
360- const inputSchema = tool . function . parameters
361- ? {
370+ const inputSchema : ToolSpecification [ 'inputSchema' ] | undefined = tool
371+ . function . parameters
372+ ? ( {
362373 // Bedrock and OpenAI's function parameter types are incompatible even though they both
363374 // adhere to the JSON schema, so we set the type to `any` to prevent a TypeScript error.
364375 json : tool . function . parameters as any ,
365- // TypeScript throws a type error if we don't define this field:
366- $unknown : undefined ,
367- }
376+ } satisfies ToolSpecification [ 'inputSchema' ] )
368377 : undefined
369378 return {
370- // TypeScript throws a type error if we don't define this field:
371- $unknown : undefined ,
372379 toolSpec : {
373380 name : tool . function . name ,
374381 description : tool . function . description ,
375382 inputSchema,
376383 } ,
377- }
384+ } satisfies Tool
378385 } )
379386 : undefined
380387
@@ -387,7 +394,10 @@ export const convertToolParams = (
387394 convertedToolChoice = { tool : { name : toolChoice . function . name } }
388395 }
389396
390- return { toolChoice : convertedToolChoice , tools : convertedTools }
397+ return {
398+ toolChoice : convertedToolChoice ,
399+ tools : convertedTools ,
400+ } satisfies ToolConfiguration
391401}
392402
393403const convertStopReason = (
0 commit comments