mom: add centralized logging, usage tracking, and improve prompt caching

Major improvements to mom's logging and cost reporting:

Centralized Logging System:
- Add src/log.ts with type-safe logging functions
- Colored console output (green=user, yellow=mom, dim=details)
- Consistent format: [HH:MM:SS] [context] message
- Replace scattered console.log/error calls throughout codebase

Usage Tracking & Cost Reporting:
- Track tokens (input, output, cache read/write) and costs per run
- Display summary at end of each run in console and Slack thread
- Example: 💰 Usage: 12,543 in + 847 out (5,234 cache read) = $0.0234

Prompt Caching Optimization:
- Move recent messages from system prompt to user message
- System prompt now mostly static (only changes with memory files)
- Enables effective use of Anthropic's prompt caching
- Significantly reduces costs on subsequent requests

Model & Cost Improvements:
- Switch from Claude Opus 4.5 to Sonnet 4.5 (~40% cost reduction)
- Fix Claude Opus 4.5 cache pricing in ai package (was 3x too expensive)
- Add manual override in generate-models.ts until upstream fix merges
- Submitted PR to models.dev: https://github.com/sst/models.dev/pull/439

UI/UX Improvements:
- Extract actual text from tool results instead of JSON wrapper
- Cleaner Slack thread formatting with duration and labels
- Tool args formatting shows paths with offset:limit notation
- Add chalk for colored terminal output

Dependencies:
- Add chalk package for terminal colors
This commit is contained in:
Mario Zechner 2025-11-26 18:04:16 +01:00
parent 82d4ac93e1
commit 213bc4df1c
11 changed files with 478 additions and 63 deletions

23
package-lock.json generated
View file

@ -1058,6 +1058,8 @@
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
@ -3769,8 +3771,8 @@
"version": "0.9.4",
"license": "MIT",
"dependencies": {
"@mariozechner/pi-ai": "^0.9.3",
"@mariozechner/pi-tui": "^0.9.3"
"@mariozechner/pi-ai": "^0.9.4",
"@mariozechner/pi-tui": "^0.9.4"
},
"devDependencies": {
"@types/node": "^24.3.0",
@ -3836,9 +3838,9 @@
"version": "0.9.4",
"license": "MIT",
"dependencies": {
"@mariozechner/pi-agent-core": "^0.9.3",
"@mariozechner/pi-ai": "^0.9.3",
"@mariozechner/pi-tui": "^0.9.3",
"@mariozechner/pi-agent-core": "^0.9.4",
"@mariozechner/pi-ai": "^0.9.4",
"@mariozechner/pi-tui": "^0.9.4",
"chalk": "^5.5.0",
"diff": "^8.0.2",
"glob": "^11.0.3"
@ -3875,11 +3877,12 @@
"license": "MIT",
"dependencies": {
"@anthropic-ai/sandbox-runtime": "^0.0.16",
"@mariozechner/pi-agent-core": "^0.9.3",
"@mariozechner/pi-ai": "^0.9.3",
"@mariozechner/pi-agent-core": "^0.9.4",
"@mariozechner/pi-ai": "^0.9.4",
"@sinclair/typebox": "^0.34.0",
"@slack/socket-mode": "^2.0.0",
"@slack/web-api": "^7.0.0",
"chalk": "^5.6.2",
"diff": "^8.0.2"
},
"bin": {
@ -3916,7 +3919,7 @@
"version": "0.9.4",
"license": "MIT",
"dependencies": {
"@mariozechner/pi-agent-core": "^0.9.3",
"@mariozechner/pi-agent-core": "^0.9.4",
"chalk": "^5.5.0"
},
"bin": {
@ -3982,8 +3985,8 @@
"license": "MIT",
"dependencies": {
"@lmstudio/sdk": "^1.5.0",
"@mariozechner/pi-ai": "^0.9.3",
"@mariozechner/pi-tui": "^0.9.3",
"@mariozechner/pi-ai": "^0.9.4",
"@mariozechner/pi-tui": "^0.9.4",
"docx-preview": "^0.3.7",
"jszip": "^3.10.1",
"lucide": "^0.544.0",