Duplicate Code Opportunity
Summary
- Pattern: Token usage record construction — metrics increment calls and JSONL record object shape — is duplicated between the HTTP and WebSocket token tracker modules
- Locations:
containers/api-proxy/token-tracker-http.js lines 241–270, containers/api-proxy/token-tracker-ws.js lines 201–220
- Impact: ~20 lines duplicated; a bug in the record schema (e.g. wrong field name, missing field) must be fixed in two places independently
Evidence
token-tracker-http.js lines 241–270:
if (metricsRef) {
metricsRef.increment('input_tokens_total', { provider }, normalized.input_tokens);
metricsRef.increment('output_tokens_total', { provider }, normalized.output_tokens);
}
// Build log record
const record = {
_schema: TOKEN_USAGE_SCHEMA,
timestamp: new Date().toISOString(),
request_id: requestId,
provider,
model: model || 'unknown',
path: reqPath,
status: proxyRes.statusCode,
streaming,
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
duration_ms: duration,
response_bytes: totalBytes,
};
writeTokenUsage(record);
token-tracker-ws.js lines 201–220 (structurally identical, minor value differences):
if (metricsRef) {
metricsRef.increment('input_tokens_total', { provider }, normalized.input_tokens);
metricsRef.increment('output_tokens_total', { provider }, normalized.output_tokens);
}
const record = {
_schema: TOKEN_USAGE_SCHEMA,
timestamp: new Date().toISOString(),
request_id: requestId,
provider,
model: streamingModel || 'unknown',
path: reqPath,
status: 101,
streaming: true,
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
duration_ms: duration,
response_bytes: totalBytes - headerBytes,
};
writeTokenUsage(record);
Suggested Refactoring
Extract a shared buildTokenUsageRecord(normalized, opts) helper in containers/api-proxy/token-persistence.js or a new token-tracker-utils.js:
function buildTokenUsageRecord(normalized, { requestId, provider, model, reqPath, status, streaming, duration, responseBytes }) {
return {
_schema: TOKEN_USAGE_SCHEMA,
timestamp: new Date().toISOString(),
request_id: requestId,
provider,
model: model || 'unknown',
path: reqPath,
status,
streaming,
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
duration_ms: duration,
response_bytes: responseBytes,
};
}
function incrementTokenMetrics(metricsRef, provider, normalized) {
if (!metricsRef) return;
metricsRef.increment('input_tokens_total', { provider }, normalized.input_tokens);
metricsRef.increment('output_tokens_total', { provider }, normalized.output_tokens);
}
Both tracker modules then call these helpers instead of inlining the logic.
Affected Files
containers/api-proxy/token-tracker-http.js — lines 241–270
containers/api-proxy/token-tracker-ws.js — lines 201–220
Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-21
Generated by Duplicate Code Detector · ● 10.6M · ◷
Duplicate Code Opportunity
Summary
containers/api-proxy/token-tracker-http.jslines 241–270,containers/api-proxy/token-tracker-ws.jslines 201–220Evidence
token-tracker-http.jslines 241–270:token-tracker-ws.jslines 201–220 (structurally identical, minor value differences):Suggested Refactoring
Extract a shared
buildTokenUsageRecord(normalized, opts)helper incontainers/api-proxy/token-persistence.jsor a newtoken-tracker-utils.js:Both tracker modules then call these helpers instead of inlining the logic.
Affected Files
containers/api-proxy/token-tracker-http.js— lines 241–270containers/api-proxy/token-tracker-ws.js— lines 201–220Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-21