claudeshift-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ from __future__ import annotations
2
+
3
+ import csv
4
+ import json
5
+ import os
6
+ import uuid
7
+ from dataclasses import asdict
8
+ from pathlib import Path
9
+
10
+ from claudeshift.telemetry.analytics import UsageReport
11
+ from claudeshift.telemetry.parser import UsageEvent
12
+
13
+ _FORMULA_PREFIXES = ("=", "+", "-", "@", "\t", "\r")
14
+
15
+
16
+ def _sanitize_csv_cell(value: str) -> str:
17
+ """Neutralize Excel/Sheets CSV formula injection on untrusted text fields."""
18
+ if value and value[0] in _FORMULA_PREFIXES:
19
+ return f"'{value}"
20
+ return value
21
+
22
+
23
+ def export_csv(events: list[UsageEvent], output_path: Path) -> Path:
24
+ output_path.parent.mkdir(parents=True, exist_ok=True)
25
+ fieldnames = [
26
+ "timestamp",
27
+ "provider",
28
+ "project",
29
+ "session_id",
30
+ "model",
31
+ "input_tokens",
32
+ "output_tokens",
33
+ "cache_write_tokens",
34
+ "cache_read_tokens",
35
+ "total_tokens",
36
+ "cost_usd",
37
+ ]
38
+ tmp_path = output_path.with_suffix(f".{uuid.uuid4().hex[:6]}.tmp")
39
+ try:
40
+ with tmp_path.open("w", encoding="utf-8", newline="") as fh:
41
+ writer = csv.DictWriter(fh, fieldnames=fieldnames)
42
+ writer.writeheader()
43
+ for ev in events:
44
+ writer.writerow(
45
+ {
46
+ "timestamp": _sanitize_csv_cell(ev.timestamp),
47
+ "provider": _sanitize_csv_cell(ev.provider),
48
+ "project": _sanitize_csv_cell(ev.project),
49
+ "session_id": _sanitize_csv_cell(ev.session_id),
50
+ "model": _sanitize_csv_cell(ev.model),
51
+ "input_tokens": ev.input_tokens,
52
+ "output_tokens": ev.output_tokens,
53
+ "cache_write_tokens": ev.cache_write_tokens,
54
+ "cache_read_tokens": ev.cache_read_tokens,
55
+ "total_tokens": ev.total_tokens,
56
+ "cost_usd": f"{ev.cost_usd:.6f}",
57
+ }
58
+ )
59
+ os.replace(tmp_path, output_path)
60
+ except Exception:
61
+ tmp_path.unlink(missing_ok=True)
62
+ raise
63
+ return output_path.resolve()
64
+
65
+
66
+ def export_json(report: UsageReport, events: list[UsageEvent], output_path: Path) -> Path:
67
+ output_path.parent.mkdir(parents=True, exist_ok=True)
68
+ payload = {
69
+ "summary": {
70
+ "generated_at": report.generated_at,
71
+ "plan": report.plan.id,
72
+ "plan_source": report.plan_source,
73
+ "budget_usd": report.budget_usd,
74
+ "window_cost_usd": report.window_cost_usd,
75
+ "window_tokens": report.window_tokens,
76
+ "window_pct": report.window_pct,
77
+ "alert_level": report.alert_level,
78
+ "burn_usd_per_hour": report.burn_usd_per_hour,
79
+ "today_cost_usd": report.today_cost_usd,
80
+ "total_cost_usd": report.total_cost_usd,
81
+ },
82
+ "models": [asdict(m) for m in report.models],
83
+ "projects": [asdict(p) for p in report.projects],
84
+ "events": [e.to_dict() for e in events],
85
+ }
86
+ tmp_path = output_path.with_suffix(f".{uuid.uuid4().hex[:6]}.tmp")
87
+ try:
88
+ tmp_path.write_text(json.dumps(payload, indent=2, ensure_ascii=False), encoding="utf-8")
89
+ os.replace(tmp_path, output_path)
90
+ except Exception:
91
+ tmp_path.unlink(missing_ok=True)
92
+ raise
93
+ return output_path.resolve()
@@ -0,0 +1,400 @@
1
+ from __future__ import annotations
2
+
3
+ import contextlib
4
+ import json
5
+ import os
6
+ import re
7
+ import uuid
8
+ from dataclasses import asdict, dataclass
9
+ from datetime import datetime, timezone
10
+ from pathlib import Path
11
+ from typing import Any
12
+
13
+ from claudeshift.telemetry.pricing import estimate_event_cost_usd
14
+
15
+ CACHE_SCHEMA_VERSION = 2
16
+
17
+
18
+ @dataclass(frozen=True)
19
+ class UsageEvent:
20
+ provider: str
21
+ timestamp: str # ISO-8601 UTC
22
+ epoch: float
23
+ session_id: str
24
+ project: str
25
+ model: str
26
+ input_tokens: int
27
+ output_tokens: int
28
+ cache_write_tokens: int
29
+ cache_read_tokens: int
30
+ cost_usd: float
31
+
32
+ @property
33
+ def total_tokens(self) -> int:
34
+ return (
35
+ self.input_tokens
36
+ + self.output_tokens
37
+ + self.cache_write_tokens
38
+ + self.cache_read_tokens
39
+ )
40
+
41
+ def to_dict(self) -> dict[str, Any]:
42
+ return asdict(self)
43
+
44
+ @classmethod
45
+ def from_dict(cls, d: dict[str, Any]) -> UsageEvent:
46
+ return cls(
47
+ provider=str(d["provider"]),
48
+ timestamp=str(d["timestamp"]),
49
+ epoch=float(d["epoch"]),
50
+ session_id=str(d["session_id"]),
51
+ project=str(d["project"]),
52
+ model=str(d["model"]),
53
+ input_tokens=int(d["input_tokens"]),
54
+ output_tokens=int(d["output_tokens"]),
55
+ cache_write_tokens=int(d["cache_write_tokens"]),
56
+ cache_read_tokens=int(d["cache_read_tokens"]),
57
+ cost_usd=float(d["cost_usd"]),
58
+ )
59
+
60
+
61
+ def _safe_non_neg_int(val: Any) -> int:
62
+ if isinstance(val, bool) or val is None:
63
+ return 0
64
+ try:
65
+ num = int(val)
66
+ except (TypeError, ValueError):
67
+ return 0
68
+ return max(0, num)
69
+
70
+
71
+ def _parse_iso_timestamp(raw: Any, fallback_epoch: float) -> tuple[str, float]:
72
+ if isinstance(raw, int | float) and not isinstance(raw, bool) and raw > 0:
73
+ with contextlib.suppress(OSError, OverflowError, ValueError):
74
+ dt = datetime.fromtimestamp(float(raw), tz=timezone.utc)
75
+ return dt.isoformat(timespec="seconds"), dt.timestamp()
76
+ if isinstance(raw, str) and raw.strip():
77
+ s = raw.strip().replace("Z", "+00:00")
78
+ with contextlib.suppress(ValueError):
79
+ dt = datetime.fromisoformat(s)
80
+ dt = dt.replace(tzinfo=timezone.utc) if dt.tzinfo is None else dt.astimezone(timezone.utc)
81
+ return dt.isoformat(timespec="seconds"), dt.timestamp()
82
+ dt = datetime.fromtimestamp(fallback_epoch, tz=timezone.utc)
83
+ return dt.isoformat(timespec="seconds"), dt.timestamp()
84
+
85
+
86
+ def _extract_project_name(file_path: Path, provider: str) -> str:
87
+ parent = file_path.parent.name
88
+ if provider == "claude":
89
+ cleaned = parent.strip("-")
90
+ if "--" in cleaned:
91
+ parts = [p for p in cleaned.split("--") if p]
92
+ return parts[-1] if parts else cleaned
93
+ return cleaned or "default"
94
+ return parent or "default"
95
+
96
+
97
+ def _read_codex_default_model(target_home: Path) -> str:
98
+ """Read configured model from ~/.codex/config.toml as a last-resort session fallback."""
99
+ toml_path = target_home / ".codex" / "config.toml"
100
+ if not toml_path.is_file():
101
+ return ""
102
+ with contextlib.suppress(OSError):
103
+ text = toml_path.read_text(encoding="utf-8", errors="replace")
104
+ match = re.search(r'^\s*model\s*=\s*["\']([^"\']+)["\']', text, re.MULTILINE)
105
+ if match:
106
+ return match.group(1).strip()
107
+ return ""
108
+
109
+
110
+ def _extract_model_from_obj(obj: dict[str, Any]) -> str:
111
+ for key in ("model", "model_name", "modelName"):
112
+ val = obj.get(key)
113
+ if isinstance(val, str) and val.strip():
114
+ return val.strip()
115
+
116
+ payload = obj.get("payload")
117
+ if isinstance(payload, dict):
118
+ for key in ("model", "model_name", "modelName"):
119
+ val = payload.get(key)
120
+ if isinstance(val, str) and val.strip():
121
+ return val.strip()
122
+ meta = payload.get("metadata")
123
+ if isinstance(meta, dict) and isinstance(meta.get("model"), str):
124
+ return meta["model"].strip()
125
+ collab = payload.get("collaboration_mode")
126
+ if isinstance(collab, dict):
127
+ settings = collab.get("settings")
128
+ if isinstance(settings, dict) and isinstance(settings.get("model"), str):
129
+ return settings["model"].strip()
130
+ return ""
131
+
132
+
133
+ def _parse_claude_file(file_path: Path) -> tuple[list[UsageEvent], str]:
134
+ fallback_epoch = file_path.stat().st_mtime
135
+ project = _extract_project_name(file_path, "claude")
136
+ dedup: dict[str, UsageEvent] = {}
137
+ ordered_keys: list[str] = []
138
+
139
+ with file_path.open("r", encoding="utf-8-sig", errors="replace") as fh:
140
+ for line_no, raw_line in enumerate(fh, 1):
141
+ line = raw_line.strip()
142
+ if not line or '"usage"' not in line:
143
+ continue
144
+ try:
145
+ obj = json.loads(line)
146
+ except json.JSONDecodeError:
147
+ continue
148
+ if not isinstance(obj, dict):
149
+ continue
150
+
151
+ msg = obj.get("message")
152
+ if isinstance(msg, dict) and isinstance(msg.get("usage"), dict):
153
+ usage = msg["usage"]
154
+ model = str(msg.get("model") or obj.get("model") or "unknown")
155
+ msg_id = str(msg.get("id") or obj.get("requestId") or f"line:{line_no}")
156
+ elif isinstance(obj.get("usage"), dict):
157
+ usage = obj["usage"]
158
+ model = str(obj.get("model") or "unknown")
159
+ msg_id = str(obj.get("requestId") or f"line:{line_no}")
160
+ else:
161
+ continue
162
+
163
+ in_tok = _safe_non_neg_int(usage.get("input_tokens"))
164
+ out_tok = _safe_non_neg_int(usage.get("output_tokens"))
165
+ cw_tok = _safe_non_neg_int(usage.get("cache_creation_input_tokens"))
166
+ cr_tok = _safe_non_neg_int(usage.get("cache_read_input_tokens"))
167
+ if (in_tok + out_tok + cw_tok + cr_tok) <= 0:
168
+ continue
169
+
170
+ ts_iso, epoch = _parse_iso_timestamp(obj.get("timestamp"), fallback_epoch)
171
+ session_id = str(obj.get("sessionId") or file_path.stem)
172
+ cost = estimate_event_cost_usd(model, in_tok, out_tok, cw_tok, cr_tok)
173
+
174
+ event = UsageEvent(
175
+ provider="claude",
176
+ timestamp=ts_iso,
177
+ epoch=epoch,
178
+ session_id=session_id,
179
+ project=project,
180
+ model=model,
181
+ input_tokens=in_tok,
182
+ output_tokens=out_tok,
183
+ cache_write_tokens=cw_tok,
184
+ cache_read_tokens=cr_tok,
185
+ cost_usd=cost,
186
+ )
187
+
188
+ if msg_id not in dedup:
189
+ ordered_keys.append(msg_id)
190
+ dedup[msg_id] = event
191
+ elif event.total_tokens >= dedup[msg_id].total_tokens:
192
+ dedup[msg_id] = event
193
+
194
+ return [dedup[k] for k in ordered_keys], ""
195
+
196
+
197
+ def _parse_codex_file(
198
+ file_path: Path,
199
+ configured_model: str = "",
200
+ ) -> tuple[list[UsageEvent], str]:
201
+ fallback_epoch = file_path.stat().st_mtime
202
+ project = _extract_project_name(file_path, "codex")
203
+ session_id = file_path.stem
204
+ current_model = configured_model or ""
205
+ plan_hint = ""
206
+ last_cumulative_sig: tuple[int, int, int] | None = None
207
+ events: list[UsageEvent] = []
208
+
209
+ with file_path.open("r", encoding="utf-8-sig", errors="replace") as fh:
210
+ for raw_line in fh:
211
+ line = raw_line.strip()
212
+ if not line:
213
+ continue
214
+ try:
215
+ obj = json.loads(line)
216
+ except json.JSONDecodeError:
217
+ continue
218
+ if not isinstance(obj, dict):
219
+ continue
220
+
221
+ payload = obj.get("payload") if isinstance(obj.get("payload"), dict) else obj
222
+
223
+ found_model = _extract_model_from_obj(obj)
224
+ if found_model:
225
+ current_model = found_model
226
+
227
+ cwd = payload.get("cwd") or obj.get("cwd")
228
+ if isinstance(cwd, str) and cwd.strip():
229
+ cwd_name = Path(cwd.replace("\\", "/")).name
230
+ if cwd_name:
231
+ project = cwd_name
232
+
233
+ if isinstance(payload.get("id"), str) and obj.get("type") == "session_meta":
234
+ session_id = payload["id"]
235
+
236
+ rl = payload.get("rate_limits")
237
+ if isinstance(rl, dict):
238
+ pt = rl.get("plan_type") or rl.get("limit_name")
239
+ if isinstance(pt, str) and pt.strip():
240
+ plan_hint = pt.strip()
241
+
242
+ usage: dict[str, Any] | None = None
243
+ info = payload.get("info")
244
+ if isinstance(info, dict):
245
+ total_u = info.get("total_token_usage")
246
+ last_u = info.get("last_token_usage")
247
+ if isinstance(total_u, dict):
248
+ sig = (
249
+ _safe_non_neg_int(total_u.get("input_tokens")),
250
+ _safe_non_neg_int(total_u.get("output_tokens")),
251
+ _safe_non_neg_int(total_u.get("cached_input_tokens")),
252
+ )
253
+ if sig == last_cumulative_sig:
254
+ continue
255
+ last_cumulative_sig = sig
256
+ if isinstance(last_u, dict):
257
+ usage = last_u
258
+ elif isinstance(total_u, dict):
259
+ usage = total_u
260
+ elif isinstance(payload.get("usage"), dict):
261
+ usage = payload["usage"]
262
+
263
+ if not isinstance(usage, dict):
264
+ continue
265
+
266
+ raw_in = _safe_non_neg_int(usage.get("input_tokens") or usage.get("prompt_tokens"))
267
+ out_tok = _safe_non_neg_int(usage.get("output_tokens") or usage.get("completion_tokens"))
268
+ cr_tok = _safe_non_neg_int(usage.get("cached_input_tokens"))
269
+ in_tok = max(0, raw_in - cr_tok) if cr_tok > 0 and raw_in >= cr_tok else raw_in
270
+
271
+ if (in_tok + out_tok + cr_tok) <= 0:
272
+ continue
273
+
274
+ resolved_model = found_model or current_model or "codex-unknown"
275
+ ts_iso, epoch = _parse_iso_timestamp(obj.get("timestamp"), fallback_epoch)
276
+ cost = estimate_event_cost_usd(resolved_model, in_tok, out_tok, 0, cr_tok)
277
+
278
+ events.append(
279
+ UsageEvent(
280
+ provider="codex",
281
+ timestamp=ts_iso,
282
+ epoch=epoch,
283
+ session_id=str(obj.get("session_id") or session_id),
284
+ project=project,
285
+ model=resolved_model,
286
+ input_tokens=in_tok,
287
+ output_tokens=out_tok,
288
+ cache_write_tokens=0,
289
+ cache_read_tokens=cr_tok,
290
+ cost_usd=cost,
291
+ )
292
+ )
293
+ return events, plan_hint
294
+
295
+
296
+ def discover_log_files(target_home: Path, provider: str = "claude") -> list[tuple[str, Path]]:
297
+ files: list[tuple[str, Path]] = []
298
+ providers = ["claude", "codex"] if provider == "all" else [provider]
299
+
300
+ for pid in providers:
301
+ if pid == "claude":
302
+ base = target_home / ".claude" / "projects"
303
+ if base.is_dir():
304
+ for p in base.rglob("*.jsonl"):
305
+ if p.is_file():
306
+ files.append(("claude", p))
307
+ elif pid == "codex":
308
+ base = target_home / ".codex" / "sessions"
309
+ if base.is_dir():
310
+ for p in base.rglob("*.jsonl"):
311
+ if p.is_file():
312
+ files.append(("codex", p))
313
+ return files
314
+
315
+
316
+ class TelemetryScanner:
317
+ """High-speed incremental JSONL scanner backed by mtime+size caching."""
318
+
319
+ def __init__(self, target_home: Path, vault_dir: Path) -> None:
320
+ self.target_home = target_home
321
+ self.cache_file = vault_dir / "_telemetry_cache.json"
322
+
323
+ def _load_cache(self) -> dict[str, Any]:
324
+ if not self.cache_file.is_file():
325
+ return {"version": CACHE_SCHEMA_VERSION, "files": {}}
326
+ with contextlib.suppress(Exception):
327
+ data = json.loads(self.cache_file.read_text(encoding="utf-8"))
328
+ if data.get("version") == CACHE_SCHEMA_VERSION and isinstance(data.get("files"), dict):
329
+ return data
330
+ return {"version": CACHE_SCHEMA_VERSION, "files": {}}
331
+
332
+ def _save_cache(self, cache: dict[str, Any]) -> None:
333
+ self.cache_file.parent.mkdir(parents=True, exist_ok=True)
334
+ tmp = self.cache_file.with_suffix(f".{uuid.uuid4().hex[:6]}.tmp")
335
+ tmp.write_text(json.dumps(cache), encoding="utf-8")
336
+ os.replace(tmp, self.cache_file)
337
+
338
+ def scan(
339
+ self, provider: str = "claude", use_cache: bool = True
340
+ ) -> tuple[list[UsageEvent], dict[str, Any]]:
341
+ cache = self._load_cache() if use_cache else {"version": CACHE_SCHEMA_VERSION, "files": {}}
342
+ cached_files: dict[str, Any] = cache.get("files", {})
343
+
344
+ discovered = discover_log_files(self.target_home, provider=provider)
345
+ codex_default_model = _read_codex_default_model(self.target_home)
346
+
347
+ all_events: list[UsageEvent] = []
348
+ cache_hits = 0
349
+ parsed_fresh = 0
350
+ dirty = False
351
+ latest_plan_hint = ""
352
+ latest_hint_mtime = -1
353
+
354
+ for pid, fpath in discovered:
355
+ try:
356
+ st = fpath.stat()
357
+ except OSError:
358
+ continue
359
+
360
+ key = str(fpath.resolve())
361
+ sig = [st.st_mtime_ns, st.st_size]
362
+ entry = cached_files.get(key)
363
+
364
+ if use_cache and entry and entry.get("sig") == sig:
365
+ cache_hits += 1
366
+ file_hint = str(entry.get("plan_hint") or "")
367
+ for raw_ev in entry.get("events", []):
368
+ all_events.append(UsageEvent.from_dict(raw_ev))
369
+ else:
370
+ parsed_fresh += 1
371
+ if pid == "claude":
372
+ events, file_hint = _parse_claude_file(fpath)
373
+ else:
374
+ events, file_hint = _parse_codex_file(fpath, configured_model=codex_default_model)
375
+ all_events.extend(events)
376
+ if use_cache:
377
+ cached_files[key] = {
378
+ "sig": sig,
379
+ "plan_hint": file_hint,
380
+ "events": [e.to_dict() for e in events],
381
+ }
382
+ dirty = True
383
+
384
+ if file_hint and st.st_mtime_ns >= latest_hint_mtime:
385
+ latest_hint_mtime = st.st_mtime_ns
386
+ latest_plan_hint = file_hint
387
+
388
+ if use_cache and dirty:
389
+ cache["files"] = cached_files
390
+ with contextlib.suppress(OSError):
391
+ self._save_cache(cache)
392
+
393
+ all_events.sort(key=lambda e: e.epoch)
394
+ stats: dict[str, Any] = {
395
+ "files_scanned": len(discovered),
396
+ "cache_hits": cache_hits,
397
+ "parsed_fresh": parsed_fresh,
398
+ "plan_hint": latest_plan_hint,
399
+ }
400
+ return all_events, stats