@@ -7,6 +7,14 @@ export interface HostedChatMessage {
77 content : unknown ;
88 name ?: string ;
99 role : HostedChatRole ;
10+ tool_calls ?: Array < {
11+ id : string ;
12+ type : 'function' ;
13+ function : {
14+ arguments : string ;
15+ name : string ;
16+ } ;
17+ } > ;
1018 tool_call_id ?: string ;
1119}
1220
@@ -42,6 +50,38 @@ function normalizeNumericValue(value: unknown): number | undefined {
4250 return value ;
4351}
4452
53+ function normalizeToolCalls ( value : unknown ) : HostedChatMessage [ 'tool_calls' ] | undefined {
54+ if ( ! Array . isArray ( value ) ) {
55+ return undefined ;
56+ }
57+
58+ const toolCalls = value
59+ . filter ( isRecord )
60+ . map ( ( toolCall ) => {
61+ const id = typeof toolCall . id === 'string' ? toolCall . id . trim ( ) : '' ;
62+ const type = typeof toolCall . type === 'string' ? toolCall . type . trim ( ) . toLowerCase ( ) : '' ;
63+ const fn = isRecord ( toolCall . function ) ? toolCall . function : null ;
64+ const name = typeof fn ?. name === 'string' ? fn . name . trim ( ) : '' ;
65+ const argumentsText = typeof fn ?. arguments === 'string' ? fn . arguments : '' ;
66+
67+ if ( ! id || type !== 'function' || ! name ) {
68+ return null ;
69+ }
70+
71+ return {
72+ function : {
73+ arguments : argumentsText ,
74+ name,
75+ } ,
76+ id,
77+ type : 'function' as const ,
78+ } ;
79+ } )
80+ . filter ( ( toolCall ) : toolCall is NonNullable < HostedChatMessage [ 'tool_calls' ] > [ number ] => toolCall !== null ) ;
81+
82+ return toolCalls . length > 0 ? toolCalls : undefined ;
83+ }
84+
4585export function normalizeHostedChatRequest ( body : unknown ) : HostedChatRequest | null {
4686 if ( ! isRecord ( body ) || ! Array . isArray ( body . messages ) ) {
4787 return null ;
@@ -70,6 +110,11 @@ export function normalizeHostedChatRequest(body: unknown): HostedChatRequest | n
70110 normalized . name = message . name . trim ( ) ;
71111 }
72112
113+ const toolCalls = normalizeToolCalls ( message . tool_calls ) ;
114+ if ( toolCalls ) {
115+ normalized . tool_calls = toolCalls ;
116+ }
117+
73118 if ( typeof message . tool_call_id === 'string' && message . tool_call_id . trim ( ) ) {
74119 normalized . tool_call_id = message . tool_call_id . trim ( ) ;
75120 }
0 commit comments