( stats: TokenStats, )
| 193 | } |
| 194 | |
| 195 | export function tokenStatsToStatsigMetrics( |
| 196 | stats: TokenStats, |
| 197 | ): Record<string, number> { |
| 198 | const metrics: Record<string, number> = { |
| 199 | total_tokens: stats.total, |
| 200 | human_message_tokens: stats.humanMessages, |
| 201 | assistant_message_tokens: stats.assistantMessages, |
| 202 | local_command_output_tokens: stats.localCommandOutputs, |
| 203 | other_tokens: stats.other, |
| 204 | } |
| 205 | |
| 206 | stats.attachments.forEach((count, type) => { |
| 207 | metrics[`attachment_${type}_count`] = count |
| 208 | }) |
| 209 | |
| 210 | stats.toolRequests.forEach((tokens, tool) => { |
| 211 | metrics[`tool_request_${tool}_tokens`] = tokens |
| 212 | }) |
| 213 | |
| 214 | stats.toolResults.forEach((tokens, tool) => { |
| 215 | metrics[`tool_result_${tool}_tokens`] = tokens |
| 216 | }) |
| 217 | |
| 218 | const duplicateTotal = [...stats.duplicateFileReads.values()].reduce( |
| 219 | (sum, d) => sum + d.tokens, |
| 220 | 0, |
| 221 | ) |
| 222 | |
| 223 | metrics.duplicate_read_tokens = duplicateTotal |
| 224 | metrics.duplicate_read_file_count = stats.duplicateFileReads.size |
| 225 | |
| 226 | if (stats.total > 0) { |
| 227 | metrics.human_message_percent = Math.round( |
| 228 | (stats.humanMessages / stats.total) * 100, |
| 229 | ) |
| 230 | metrics.assistant_message_percent = Math.round( |
| 231 | (stats.assistantMessages / stats.total) * 100, |
| 232 | ) |
| 233 | metrics.local_command_output_percent = Math.round( |
| 234 | (stats.localCommandOutputs / stats.total) * 100, |
| 235 | ) |
| 236 | metrics.duplicate_read_percent = Math.round( |
| 237 | (duplicateTotal / stats.total) * 100, |
| 238 | ) |
| 239 | |
| 240 | const toolRequestTotal = [...stats.toolRequests.values()].reduce( |
| 241 | (sum, v) => sum + v, |
| 242 | 0, |
| 243 | ) |
| 244 | const toolResultTotal = [...stats.toolResults.values()].reduce( |
| 245 | (sum, v) => sum + v, |
| 246 | 0, |
| 247 | ) |
| 248 | |
| 249 | metrics.tool_request_percent = Math.round( |
| 250 | (toolRequestTotal / stats.total) * 100, |
| 251 | ) |
| 252 | metrics.tool_result_percent = Math.round( |
no test coverage detected