@@ -258,35 +258,37 @@ import {
258
258
handleConversationTurnEvent
259
259
} from ' @aws-amplify/backend-ai/conversation/runtime' ;
260
260
261
+ const jsonSchema = {
262
+ json: {
263
+ type: ' object' ,
264
+ properties: {
265
+ ' operator' : {
266
+ ' type' : ' string' ,
267
+ ' enum' : [' +' , ' -' , ' *' , ' /' ],
268
+ ' description' : ' The arithmetic operator to use'
269
+ },
270
+ ' operands' : {
271
+ ' type' : ' array' ,
272
+ ' items' : {
273
+ ' type' : ' number'
274
+ },
275
+ ' minItems' : 2 ,
276
+ ' maxItems' : 2 ,
277
+ ' description' : ' Two numbers to perform the operation on'
278
+ }
279
+ },
280
+ required: [' operator' , ' operands' ]
281
+ }
282
+ } as const ;
283
+ // declare as const to allow the input type to be derived from the JSON schema in the tool handler definition.
284
+
261
285
const calculator = createExecutableTool (
262
286
' calculator' ,
263
287
' Returns the result of a simple calculation' ,
264
- {
265
- json: {
266
- type: ' object' ,
267
- properties: {
268
- ' operator' : {
269
- ' type' : ' string' ,
270
- ' enum' : [' +' , ' -' , ' *' , ' /' ],
271
- ' description' : ' The arithmetic operator to use'
272
- },
273
- ' operands' : {
274
- ' type' : ' array' ,
275
- ' items' : {
276
- ' type' : ' number'
277
- },
278
- ' minItems' : 2 ,
279
- ' maxItems' : 2 ,
280
- ' description' : ' Two numbers to perform the operation on'
281
- }
282
- },
283
- required: [' operator' , ' operands' ]
284
- }
285
- },
288
+ jsonSchema ,
286
289
// input type is derived from the JSON schema
287
290
(input ) => {
288
- const [a, b] = input .operands as [number , number ];
289
-
291
+ const [a, b] = input .operands ;
290
292
switch (input .operator ) {
291
293
case ' +' : return Promise .resolve ({ text: (a + b ).toString () });
292
294
case ' -' : return Promise .resolve ({ text: (a - b ).toString () });
0 commit comments