File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed
typescript/ai-sdk-v5/src/basic Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -96,13 +96,19 @@ async function toolCalling() {
9696 b : z . number ( ) ,
9797 } ) ,
9898 execute : async ( params ) => {
99- const operations : Record < string , number | string > = {
100- add : params . a + params . b ,
101- subtract : params . a - params . b ,
102- multiply : params . a * params . b ,
103- divide : params . b !== 0 ? params . a / params . b : 'Cannot divide by zero' ,
104- } ;
105- return { result : operations [ params . operation ] } ;
99+ switch ( params . operation ) {
100+ case 'add' :
101+ return { result : params . a + params . b } ;
102+ case 'subtract' :
103+ return { result : params . a - params . b } ;
104+ case 'multiply' :
105+ return { result : params . a * params . b } ;
106+ case 'divide' :
107+ if ( params . b === 0 ) {
108+ return { error : 'Cannot divide by zero' } ;
109+ }
110+ return { result : params . a / params . b } ;
111+ }
106112 } ,
107113 } ) ;
108114
@@ -157,7 +163,7 @@ async function usageAccounting() {
157163 console . log ( '- Prompt Tokens:' , usage . promptTokens ) ;
158164 console . log ( '- Completion Tokens:' , usage . completionTokens ) ;
159165 if ( usage . cost ) {
160- console . log ( ' - Cost: $' + usage . cost . toFixed ( 6 ) ) ;
166+ console . log ( ` - Cost: $${ usage . cost . toFixed ( 6 ) } ` ) ;
161167 }
162168 }
163169}
You can’t perform that action at this time.
0 commit comments