node-DeepResearch/src/utils/token-tracker.ts
devin-ai-integration[bot] f99608909c
refactor: centralize token tracking and clean up console output (#3)
* refactor: centralize token tracking and clean up console output

- Remove manual token tracking in agent.ts
- Track tokens through tokenTracker.trackUsage()
- Clean up verbose console output
- Add ESLint configuration
- Fix TypeScript linting issues

Co-Authored-By: Han Xiao <han.xiao@jina.ai>

* refactor: simplify sleep function and use console.log consistently

Co-Authored-By: Han Xiao <han.xiao@jina.ai>

* refactor: remove color modifiers from console.log statements

Co-Authored-By: Han Xiao <han.xiao@jina.ai>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Han Xiao <han.xiao@jina.ai>
2025-01-31 15:25:28 +08:00

41 lines
890 B
TypeScript

import { EventEmitter } from 'events';
interface TokenUsage {
tool: string;
tokens: number;
}
class TokenTracker extends EventEmitter {
private usages: TokenUsage[] = [];
trackUsage(tool: string, tokens: number) {
this.usages.push({ tool, tokens });
this.emit('usage', { tool, tokens });
}
getTotalUsage(): number {
return this.usages.reduce((sum, usage) => sum + usage.tokens, 0);
}
getUsageBreakdown(): Record<string, number> {
return this.usages.reduce((acc, { tool, tokens }) => {
acc[tool] = (acc[tool] || 0) + tokens;
return acc;
}, {} as Record<string, number>);
}
printSummary() {
const breakdown = this.getUsageBreakdown();
console.log('Token Usage Summary:', {
total: this.getTotalUsage(),
breakdown
});
}
reset() {
this.usages = [];
}
}
export const tokenTracker = new TokenTracker();