(
tokens: {
input: number
output: number
cacheWrite: number
cacheRead: number
total?: number
},
messageIndex: number = apiReqIndex,
status: "completed" | "cancelled" = "completed",
)
| 3049 | |
| 3050 | // Helper function to capture telemetry and update messages |
| 3051 | const captureUsageData = async ( |
| 3052 | tokens: { |
| 3053 | input: number |
| 3054 | output: number |
| 3055 | cacheWrite: number |
| 3056 | cacheRead: number |
| 3057 | total?: number |
| 3058 | }, |
| 3059 | messageIndex: number = apiReqIndex, |
| 3060 | status: "completed" | "cancelled" = "completed", |
| 3061 | ) => { |
| 3062 | if ( |
| 3063 | tokens.input > 0 || |
| 3064 | tokens.output > 0 || |
| 3065 | tokens.cacheWrite > 0 || |
| 3066 | tokens.cacheRead > 0 |
| 3067 | ) { |
| 3068 | // Update the shared variables atomically |
| 3069 | inputTokens = tokens.input |
| 3070 | outputTokens = tokens.output |
| 3071 | cacheWriteTokens = tokens.cacheWrite |
| 3072 | cacheReadTokens = tokens.cacheRead |
| 3073 | totalCost = tokens.total |
| 3074 | |
| 3075 | // Update the API request message with the latest usage data |
| 3076 | updateApiReqMsg() |
| 3077 | await this.saveClineMessages() |
| 3078 | |
| 3079 | // Update the specific message in the webview |
| 3080 | const apiReqMessage = this.clineMessages[messageIndex] |
| 3081 | if (apiReqMessage) { |
| 3082 | await this.updateClineMessage(apiReqMessage) |
| 3083 | } |
| 3084 | |
| 3085 | // Capture telemetry with provider-aware cost calculation |
| 3086 | const modelId = getModelId(this.apiConfiguration) |
| 3087 | const apiProvider = this.apiConfiguration.apiProvider |
| 3088 | const apiProtocol = getApiProtocol( |
| 3089 | apiProvider && !isRetiredProvider(apiProvider) ? apiProvider : undefined, |
| 3090 | modelId, |
| 3091 | ) |
| 3092 | |
| 3093 | // Use the appropriate cost function based on the API protocol |
| 3094 | const costResult = |
| 3095 | apiProtocol === "anthropic" |
| 3096 | ? calculateApiCostAnthropic( |
| 3097 | streamModelInfo, |
| 3098 | tokens.input, |
| 3099 | tokens.output, |
| 3100 | tokens.cacheWrite, |
| 3101 | tokens.cacheRead, |
| 3102 | ) |
| 3103 | : calculateApiCostOpenAI( |
| 3104 | streamModelInfo, |
| 3105 | tokens.input, |
| 3106 | tokens.output, |
| 3107 | tokens.cacheWrite, |
| 3108 | tokens.cacheRead, |
nothing calls this directly
no test coverage detected