@@ -209,24 +209,29 @@ function calculateToolCost(
209209 }
210210}
211211
212+ interface HostedKeyCostResult {
213+ cost : number
214+ metadata ?: Record < string , unknown >
215+ }
216+
212217/**
213218 * Calculate and log hosted key cost for a tool execution.
214- * Logs to usageLog for audit trail and returns cost for accumulation in userStats .
219+ * Logs to usageLog for audit trail and returns cost + metadata for output .
215220 */
216221async function processHostedKeyCost (
217222 tool : ToolConfig ,
218223 params : Record < string , unknown > ,
219224 response : Record < string , unknown > ,
220225 executionContext : ExecutionContext | undefined ,
221226 requestId : string
222- ) : Promise < number > {
227+ ) : Promise < HostedKeyCostResult > {
223228 if ( ! tool . hosting ?. pricing ) {
224- return 0
229+ return { cost : 0 }
225230 }
226231
227232 const { cost, metadata } = calculateToolCost ( tool . hosting . pricing , params , response )
228233
229- if ( cost <= 0 ) return 0
234+ if ( cost <= 0 ) return { cost : 0 }
230235
231236 // Log to usageLog table for audit trail
232237 if ( executionContext ?. userId ) {
@@ -247,7 +252,7 @@ async function processHostedKeyCost(
247252 }
248253 }
249254
250- return cost
255+ return { cost, metadata }
251256}
252257
253258/**
@@ -643,15 +648,13 @@ export async function executeTool(
643648
644649 // Calculate hosted key cost and merge into output.cost
645650 if ( hostedKeyInfo . isUsingHostedKey && finalResult . success ) {
646- const hostedKeyCost = await processHostedKeyCost ( tool , contextParams , finalResult . output , executionContext , requestId )
651+ const { cost : hostedKeyCost , metadata } = await processHostedKeyCost ( tool , contextParams , finalResult . output , executionContext , requestId )
647652 if ( hostedKeyCost > 0 ) {
648- const existingCost = finalResult . output ?. cost || { }
649653 finalResult . output = {
650654 ...finalResult . output ,
651655 cost : {
652- input : existingCost . input || 0 ,
653- output : existingCost . output || 0 ,
654- total : ( existingCost . total || 0 ) + hostedKeyCost ,
656+ total : hostedKeyCost ,
657+ ...metadata ,
655658 } ,
656659 }
657660 }
@@ -708,15 +711,13 @@ export async function executeTool(
708711
709712 // Calculate hosted key cost and merge into output.cost
710713 if ( hostedKeyInfo . isUsingHostedKey && finalResult . success ) {
711- const hostedKeyCost = await processHostedKeyCost ( tool , contextParams , finalResult . output , executionContext , requestId )
714+ const { cost : hostedKeyCost , metadata } = await processHostedKeyCost ( tool , contextParams , finalResult . output , executionContext , requestId )
712715 if ( hostedKeyCost > 0 ) {
713- const existingCost = finalResult . output ?. cost || { }
714716 finalResult . output = {
715717 ...finalResult . output ,
716718 cost : {
717- input : existingCost . input || 0 ,
718- output : existingCost . output || 0 ,
719- total : ( existingCost . total || 0 ) + hostedKeyCost ,
719+ total : hostedKeyCost ,
720+ ...metadata ,
720721 } ,
721722 }
722723 }
0 commit comments