@@ -8,6 +8,7 @@ type AnthropicToolPayloadCompatibilityOptions = {
88 toolSchemaMode ?: AnthropicToolSchemaMode ;
99 toolChoiceMode ?: AnthropicToolChoiceMode ;
1010} ;
11+ type PayloadFieldRead = { ok : true ; value : unknown } | { ok : false } ;
1112
1213function hasOpenAiAnthropicToolPayloadCompatFlag ( model : { compat ?: unknown } ) : boolean {
1314 if ( ! model . compat || typeof model . compat !== "object" || Array . isArray ( model . compat ) ) {
@@ -67,30 +68,56 @@ function normalizeOpenAiFunctionAnthropicToolDefinition(
6768 }
6869
6970 const toolObj = tool as Record < string , unknown > ;
70- if ( toolObj . function && typeof toolObj . function === "object" ) {
71+ const functionField = readPayloadField ( toolObj , "function" ) ;
72+ if ( ! functionField . ok ) {
73+ return undefined ;
74+ }
75+ if ( functionField . value && typeof functionField . value === "object" ) {
7176 return toolObj ;
7277 }
7378
74- const rawName = normalizeOptionalString ( toolObj . name ) ?? "" ;
79+ const nameField = readPayloadField ( toolObj , "name" ) ;
80+ if ( ! nameField . ok ) {
81+ return undefined ;
82+ }
83+ const rawName = normalizeOptionalString ( nameField . value ) ?? "" ;
7584 if ( ! rawName ) {
7685 return toolObj ;
7786 }
7887
88+ const inputSchemaField = readPayloadField ( toolObj , "input_schema" ) ;
89+ if ( ! inputSchemaField . ok ) {
90+ return undefined ;
91+ }
92+ const inputSchema = inputSchemaField . value ;
93+ let parameters : unknown = { type : "object" , properties : { } } ;
94+ if ( inputSchema && typeof inputSchema === "object" ) {
95+ parameters = inputSchema ;
96+ } else {
97+ const parametersField = readPayloadField ( toolObj , "parameters" ) ;
98+ if ( ! parametersField . ok ) {
99+ return undefined ;
100+ }
101+ if ( parametersField . value && typeof parametersField . value === "object" ) {
102+ parameters = parametersField . value ;
103+ }
104+ }
79105 const functionSpec : Record < string , unknown > = {
80106 name : rawName ,
81- parameters :
82- toolObj . input_schema && typeof toolObj . input_schema === "object"
83- ? toolObj . input_schema
84- : toolObj . parameters && typeof toolObj . parameters === "object"
85- ? toolObj . parameters
86- : { type : "object" , properties : { } } ,
107+ parameters,
87108 } ;
88109
89- if ( typeof toolObj . description === "string" && toolObj . description . trim ( ) ) {
90- functionSpec . description = toolObj . description ;
110+ const descriptionField = readPayloadField ( toolObj , "description" ) ;
111+ if (
112+ descriptionField . ok &&
113+ typeof descriptionField . value === "string" &&
114+ descriptionField . value . trim ( )
115+ ) {
116+ functionSpec . description = descriptionField . value ;
91117 }
92- if ( typeof toolObj . strict === "boolean" ) {
93- functionSpec . strict = toolObj . strict ;
118+ const strictField = readPayloadField ( toolObj , "strict" ) ;
119+ if ( strictField . ok && typeof strictField . value === "boolean" ) {
120+ functionSpec . strict = strictField . value ;
94121 }
95122
96123 return {
@@ -99,6 +126,14 @@ function normalizeOpenAiFunctionAnthropicToolDefinition(
99126 } ;
100127}
101128
129+ function readPayloadField ( record : Record < string , unknown > , field : string ) : PayloadFieldRead {
130+ try {
131+ return { ok : true , value : Reflect . get ( record , field ) } ;
132+ } catch {
133+ return { ok : false } ;
134+ }
135+ }
136+
102137function normalizeOpenAiStringModeAnthropicToolChoice ( toolChoice : unknown ) : unknown {
103138 if ( ! toolChoice || typeof toolChoice !== "object" || Array . isArray ( toolChoice ) ) {
104139 return toolChoice ;
0 commit comments