@@ -15,6 +15,7 @@ import {
1515 calculateCost ,
1616 prepareToolExecution ,
1717 prepareToolsWithUsageControl ,
18+ sumToolCosts ,
1819 trackForcedToolUsage ,
1920} from '@/providers/utils'
2021import { executeTool } from '@/tools'
@@ -123,28 +124,25 @@ export const avianProvider: ProviderConfig = {
123124 )
124125
125126 const streamingResult = {
126- stream : createReadableStreamFromAvianStream (
127- streamResponse as any ,
128- ( content , usage ) => {
129- streamingResult . execution . output . content = content
130- streamingResult . execution . output . tokens = {
131- input : usage . prompt_tokens ,
132- output : usage . completion_tokens ,
133- total : usage . total_tokens ,
134- }
127+ stream : createReadableStreamFromAvianStream ( streamResponse as any , ( content , usage ) => {
128+ streamingResult . execution . output . content = content
129+ streamingResult . execution . output . tokens = {
130+ input : usage . prompt_tokens ,
131+ output : usage . completion_tokens ,
132+ total : usage . total_tokens ,
133+ }
135134
136- const costResult = calculateCost (
137- request . model ,
138- usage . prompt_tokens ,
139- usage . completion_tokens
140- )
141- streamingResult . execution . output . cost = {
142- input : costResult . input ,
143- output : costResult . output ,
144- total : costResult . total ,
145- }
135+ const costResult = calculateCost (
136+ request . model ,
137+ usage . prompt_tokens ,
138+ usage . completion_tokens
139+ )
140+ streamingResult . execution . output . cost = {
141+ input : costResult . input ,
142+ output : costResult . output ,
143+ total : costResult . total ,
146144 }
147- ) ,
145+ } ) ,
148146 execution : {
149147 success : true ,
150148 output : {
@@ -205,7 +203,7 @@ export const avianProvider: ProviderConfig = {
205203 total : currentResponse . usage ?. total_tokens || 0 ,
206204 }
207205 const toolCalls = [ ]
208- const toolResults = [ ]
206+ const toolResults : Record < string , unknown > [ ] = [ ]
209207 const currentMessages = [ ...allMessages ]
210208 let iterationCount = 0
211209 let hasUsedForcedTool = false
@@ -325,7 +323,7 @@ export const avianProvider: ProviderConfig = {
325323 } )
326324
327325 let resultContent : any
328- if ( result . success ) {
326+ if ( result . success && result . output ) {
329327 toolResults . push ( result . output )
330328 resultContent = result . output
331329 } else {
@@ -456,28 +454,27 @@ export const avianProvider: ProviderConfig = {
456454 const accumulatedCost = calculateCost ( request . model , tokens . input , tokens . output )
457455
458456 const streamingResult = {
459- stream : createReadableStreamFromAvianStream (
460- streamResponse as any ,
461- ( content , usage ) => {
462- streamingResult . execution . output . content = content
463- streamingResult . execution . output . tokens = {
464- input : tokens . input + usage . prompt_tokens ,
465- output : tokens . output + usage . completion_tokens ,
466- total : tokens . total + usage . total_tokens ,
467- }
457+ stream : createReadableStreamFromAvianStream ( streamResponse as any , ( content , usage ) => {
458+ streamingResult . execution . output . content = content
459+ streamingResult . execution . output . tokens = {
460+ input : tokens . input + usage . prompt_tokens ,
461+ output : tokens . output + usage . completion_tokens ,
462+ total : tokens . total + usage . total_tokens ,
463+ }
468464
469- const streamCost = calculateCost (
470- request . model ,
471- usage . prompt_tokens ,
472- usage . completion_tokens
473- )
474- streamingResult . execution . output . cost = {
475- input : accumulatedCost . input + streamCost . input ,
476- output : accumulatedCost . output + streamCost . output ,
477- total : accumulatedCost . total + streamCost . total ,
478- }
465+ const streamCost = calculateCost (
466+ request . model ,
467+ usage . prompt_tokens ,
468+ usage . completion_tokens
469+ )
470+ const tc = sumToolCosts ( toolResults )
471+ streamingResult . execution . output . cost = {
472+ input : accumulatedCost . input + streamCost . input ,
473+ output : accumulatedCost . output + streamCost . output ,
474+ toolCost : tc || undefined ,
475+ total : accumulatedCost . total + streamCost . total + tc ,
479476 }
480- ) ,
477+ } ) ,
481478 execution : {
482479 success : true ,
483480 output : {
@@ -508,6 +505,7 @@ export const avianProvider: ProviderConfig = {
508505 cost : {
509506 input : accumulatedCost . input ,
510507 output : accumulatedCost . output ,
508+ toolCost : undefined as number | undefined ,
511509 total : accumulatedCost . total ,
512510 } ,
513511 } ,
0 commit comments