Skip to content

Commit 1c5425e

Browse files
author
Theodore Li
committed
Add helper method for adding hosted key cost
1 parent d7ea0af commit 1c5425e

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

apps/sim/tools/index.ts

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,37 @@ function stripInternalFields(output: Record<string, unknown>): Record<string, un
339339
return result
340340
}
341341

342+
/**
343+
* Apply post-execution hosted-key cost tracking to a successful tool result.
344+
* Reports custom dimension usage, calculates cost, and merges it into the output.
345+
*/
346+
async function applyHostedKeyCostToResult(
347+
finalResult: ToolResponse,
348+
tool: ToolConfig,
349+
params: Record<string, unknown>,
350+
executionContext: ExecutionContext | undefined,
351+
requestId: string
352+
): Promise<void> {
353+
await reportCustomDimensionUsage(tool, params, finalResult.output, executionContext, requestId)
354+
355+
const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(
356+
tool,
357+
params,
358+
finalResult.output,
359+
executionContext,
360+
requestId
361+
)
362+
if (hostedKeyCost > 0) {
363+
finalResult.output = {
364+
...finalResult.output,
365+
cost: {
366+
total: hostedKeyCost,
367+
...metadata,
368+
},
369+
}
370+
}
371+
}
372+
342373
/**
343374
* Normalizes a tool ID by stripping resource ID suffix (UUID/tableId).
344375
* Workflow tools: 'workflow_executor_<uuid>' -> 'workflow_executor'
@@ -744,32 +775,14 @@ export async function executeTool(
744775
const endTimeISO = endTime.toISOString()
745776
const duration = endTime.getTime() - startTime.getTime()
746777

747-
// Post-execution: report custom dimension usage and calculate cost
748778
if (hostedKeyInfo.isUsingHostedKey && finalResult.success) {
749-
await reportCustomDimensionUsage(
750-
tool,
751-
contextParams,
752-
finalResult.output,
753-
executionContext,
754-
requestId
755-
)
756-
757-
const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(
779+
await applyHostedKeyCostToResult(
780+
finalResult,
758781
tool,
759782
contextParams,
760-
finalResult.output,
761783
executionContext,
762784
requestId
763785
)
764-
if (hostedKeyCost > 0) {
765-
finalResult.output = {
766-
...finalResult.output,
767-
cost: {
768-
total: hostedKeyCost,
769-
...metadata,
770-
},
771-
}
772-
}
773786
}
774787

775788
// Strip internal fields (keys starting with _) from output before returning
@@ -818,32 +831,14 @@ export async function executeTool(
818831
const endTimeISO = endTime.toISOString()
819832
const duration = endTime.getTime() - startTime.getTime()
820833

821-
// Post-execution: report custom dimension usage and calculate cost
822834
if (hostedKeyInfo.isUsingHostedKey && finalResult.success) {
823-
await reportCustomDimensionUsage(
835+
await applyHostedKeyCostToResult(
836+
finalResult,
824837
tool,
825838
contextParams,
826-
finalResult.output,
827839
executionContext,
828840
requestId
829841
)
830-
831-
const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(
832-
tool,
833-
contextParams,
834-
finalResult.output,
835-
executionContext,
836-
requestId
837-
)
838-
if (hostedKeyCost > 0) {
839-
finalResult.output = {
840-
...finalResult.output,
841-
cost: {
842-
total: hostedKeyCost,
843-
...metadata,
844-
},
845-
}
846-
}
847842
}
848843

849844
// Strip internal fields (keys starting with _) from output before returning

0 commit comments

Comments
 (0)