klaude-code 1.7.1__py3-none-any.whl → 1.9.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.
Files changed (36) hide show
  1. klaude_code/auth/base.py +101 -0
  2. klaude_code/auth/claude/__init__.py +6 -0
  3. klaude_code/auth/claude/exceptions.py +9 -0
  4. klaude_code/auth/claude/oauth.py +172 -0
  5. klaude_code/auth/claude/token_manager.py +26 -0
  6. klaude_code/auth/codex/token_manager.py +10 -50
  7. klaude_code/cli/auth_cmd.py +127 -46
  8. klaude_code/cli/config_cmd.py +4 -2
  9. klaude_code/cli/cost_cmd.py +343 -0
  10. klaude_code/cli/list_model.py +248 -200
  11. klaude_code/cli/main.py +2 -0
  12. klaude_code/command/prompt-commit.md +73 -0
  13. klaude_code/command/status_cmd.py +8 -8
  14. klaude_code/config/assets/builtin_config.yaml +36 -3
  15. klaude_code/config/config.py +24 -5
  16. klaude_code/config/thinking.py +4 -4
  17. klaude_code/core/prompt.py +1 -1
  18. klaude_code/llm/anthropic/client.py +28 -3
  19. klaude_code/llm/claude/__init__.py +3 -0
  20. klaude_code/llm/claude/client.py +95 -0
  21. klaude_code/llm/codex/client.py +1 -1
  22. klaude_code/llm/registry.py +3 -1
  23. klaude_code/protocol/llm_param.py +2 -1
  24. klaude_code/protocol/sub_agent/__init__.py +1 -2
  25. klaude_code/session/session.py +4 -4
  26. klaude_code/ui/renderers/metadata.py +6 -26
  27. klaude_code/ui/rich/theme.py +6 -5
  28. klaude_code/ui/terminal/selector.py +25 -2
  29. klaude_code/ui/utils/common.py +46 -0
  30. {klaude_code-1.7.1.dist-info → klaude_code-1.9.0.dist-info}/METADATA +37 -5
  31. {klaude_code-1.7.1.dist-info → klaude_code-1.9.0.dist-info}/RECORD +33 -27
  32. klaude_code/command/prompt-jj-describe.md +0 -32
  33. klaude_code/core/prompts/prompt-sub-agent-oracle.md +0 -22
  34. klaude_code/protocol/sub_agent/oracle.py +0 -91
  35. {klaude_code-1.7.1.dist-info → klaude_code-1.9.0.dist-info}/WHEEL +0 -0
  36. {klaude_code-1.7.1.dist-info → klaude_code-1.9.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,343 @@
1
+ """Cost command for aggregating usage statistics across all sessions."""
2
+
3
+ from collections import defaultdict
4
+ from dataclasses import dataclass, field
5
+ from datetime import datetime
6
+ from pathlib import Path
7
+
8
+ import typer
9
+ from rich.box import Box
10
+ from rich.console import Console
11
+ from rich.table import Table
12
+
13
+ from klaude_code.command.status_cmd import format_cost, format_tokens
14
+ from klaude_code.protocol import model
15
+ from klaude_code.session.codec import decode_jsonl_line
16
+
17
+ ASCII_HORIZONAL = Box(" -- \n \n -- \n \n -- \n -- \n \n -- \n")
18
+
19
+
20
+ @dataclass
21
+ class ModelUsageStats:
22
+ """Aggregated usage stats for a single model."""
23
+
24
+ model_name: str
25
+ input_tokens: int = 0
26
+ output_tokens: int = 0
27
+ cached_tokens: int = 0
28
+ cost_usd: float = 0.0
29
+ cost_cny: float = 0.0
30
+
31
+ @property
32
+ def total_tokens(self) -> int:
33
+ return self.input_tokens + self.output_tokens
34
+
35
+ def add_usage(self, usage: model.Usage) -> None:
36
+ self.input_tokens += usage.input_tokens
37
+ self.output_tokens += usage.output_tokens
38
+ self.cached_tokens += usage.cached_tokens
39
+ if usage.total_cost is not None:
40
+ if usage.currency == "CNY":
41
+ self.cost_cny += usage.total_cost
42
+ else:
43
+ self.cost_usd += usage.total_cost
44
+
45
+
46
+ @dataclass
47
+ class DailyStats:
48
+ """Aggregated stats for a single day."""
49
+
50
+ date: str
51
+ by_model: dict[str, ModelUsageStats] = field(default_factory=lambda: dict[str, ModelUsageStats]())
52
+
53
+ def add_task_metadata(self, meta: model.TaskMetadata, date_str: str) -> None:
54
+ """Add a TaskMetadata to this day's stats."""
55
+ del date_str # unused, date is already set
56
+ if not meta.usage or not meta.model_name:
57
+ return
58
+
59
+ model_key = meta.model_name
60
+ if model_key not in self.by_model:
61
+ self.by_model[model_key] = ModelUsageStats(model_name=model_key)
62
+
63
+ self.by_model[model_key].add_usage(meta.usage)
64
+
65
+ def get_subtotal(self) -> ModelUsageStats:
66
+ """Get subtotal across all models for this day."""
67
+ subtotal = ModelUsageStats(model_name="(subtotal)")
68
+ for stats in self.by_model.values():
69
+ subtotal.input_tokens += stats.input_tokens
70
+ subtotal.output_tokens += stats.output_tokens
71
+ subtotal.cached_tokens += stats.cached_tokens
72
+ subtotal.cost_usd += stats.cost_usd
73
+ subtotal.cost_cny += stats.cost_cny
74
+ return subtotal
75
+
76
+
77
+ def iter_all_sessions() -> list[tuple[str, Path]]:
78
+ """Iterate over all sessions across all projects.
79
+
80
+ Returns list of (session_id, events_file_path) tuples.
81
+ """
82
+ projects_dir = Path.home() / ".klaude" / "projects"
83
+ if not projects_dir.exists():
84
+ return []
85
+
86
+ sessions: list[tuple[str, Path]] = []
87
+ for project_dir in projects_dir.iterdir():
88
+ if not project_dir.is_dir():
89
+ continue
90
+ sessions_dir = project_dir / "sessions"
91
+ if not sessions_dir.exists():
92
+ continue
93
+ for session_dir in sessions_dir.iterdir():
94
+ if not session_dir.is_dir():
95
+ continue
96
+ events_file = session_dir / "events.jsonl"
97
+ meta_file = session_dir / "meta.json"
98
+ # Skip sub-agent sessions by checking meta.json
99
+ if meta_file.exists():
100
+ import json
101
+
102
+ try:
103
+ meta = json.loads(meta_file.read_text(encoding="utf-8"))
104
+ if meta.get("sub_agent_state") is not None:
105
+ continue
106
+ except (json.JSONDecodeError, OSError):
107
+ pass
108
+ if events_file.exists():
109
+ sessions.append((session_dir.name, events_file))
110
+
111
+ return sessions
112
+
113
+
114
+ def extract_task_metadata_from_events(events_path: Path) -> list[tuple[str, model.TaskMetadataItem]]:
115
+ """Extract TaskMetadataItem entries from events.jsonl with their dates.
116
+
117
+ Returns list of (date_str, TaskMetadataItem) tuples.
118
+ """
119
+ results: list[tuple[str, model.TaskMetadataItem]] = []
120
+ try:
121
+ content = events_path.read_text(encoding="utf-8")
122
+ except OSError:
123
+ return results
124
+
125
+ for line in content.splitlines():
126
+ item = decode_jsonl_line(line)
127
+ if isinstance(item, model.TaskMetadataItem):
128
+ date_str = item.created_at.strftime("%Y-%m-%d")
129
+ results.append((date_str, item))
130
+
131
+ return results
132
+
133
+
134
+ def aggregate_all_sessions() -> dict[str, DailyStats]:
135
+ """Aggregate usage stats from all sessions, grouped by date.
136
+
137
+ Returns dict mapping date string to DailyStats.
138
+ """
139
+ daily_stats: dict[str, DailyStats] = defaultdict(lambda: DailyStats(date=""))
140
+
141
+ sessions = iter_all_sessions()
142
+ for _session_id, events_path in sessions:
143
+ metadata_items = extract_task_metadata_from_events(events_path)
144
+ for date_str, metadata_item in metadata_items:
145
+ if daily_stats[date_str].date == "":
146
+ daily_stats[date_str] = DailyStats(date=date_str)
147
+
148
+ # Process main agent metadata
149
+ daily_stats[date_str].add_task_metadata(metadata_item.main_agent, date_str)
150
+
151
+ # Process sub-agent metadata
152
+ for sub_meta in metadata_item.sub_agent_task_metadata:
153
+ daily_stats[date_str].add_task_metadata(sub_meta, date_str)
154
+
155
+ return dict(daily_stats)
156
+
157
+
158
+ def format_cost_dual(cost_usd: float, cost_cny: float) -> tuple[str, str]:
159
+ """Format costs for both currencies."""
160
+ usd_str = format_cost(cost_usd if cost_usd > 0 else None, "USD")
161
+ cny_str = format_cost(cost_cny if cost_cny > 0 else None, "CNY")
162
+ return usd_str, cny_str
163
+
164
+
165
+ def format_date_display(date_str: str) -> str:
166
+ """Format date string YYYY-MM-DD to 'YYYY M-D WEEKDAY' for table display."""
167
+ try:
168
+ dt = datetime.strptime(date_str, "%Y-%m-%d")
169
+ weekday = dt.strftime("%a").upper()
170
+ return f"{dt.year} {dt.month}-{dt.day} {weekday}"
171
+ except (ValueError, TypeError):
172
+ return date_str
173
+
174
+
175
+ def render_cost_table(daily_stats: dict[str, DailyStats]) -> Table:
176
+ """Render the cost table using rich."""
177
+ table = Table(
178
+ title="Usage Statistics",
179
+ show_header=True,
180
+ header_style="bold",
181
+ border_style="bright_black dim",
182
+ padding=(0, 1, 0, 2),
183
+ box=ASCII_HORIZONAL,
184
+ )
185
+
186
+ table.add_column("Date", style="cyan", no_wrap=True)
187
+ table.add_column("Model", no_wrap=True)
188
+ table.add_column("Input", justify="right", no_wrap=True)
189
+ table.add_column("Output", justify="right", no_wrap=True)
190
+ table.add_column("Cache", justify="right", no_wrap=True)
191
+ table.add_column("Total", justify="right", no_wrap=True)
192
+ table.add_column("USD", justify="right", no_wrap=True)
193
+ table.add_column("CNY", justify="right", no_wrap=True)
194
+
195
+ # Sort dates
196
+ sorted_dates = sorted(daily_stats.keys())
197
+
198
+ # Track global totals by model
199
+ global_by_model: dict[str, ModelUsageStats] = {}
200
+
201
+ def sort_by_cost(stats: ModelUsageStats) -> tuple[float, float]:
202
+ """Sort key: USD desc, then CNY desc."""
203
+ return (-stats.cost_usd, -stats.cost_cny)
204
+
205
+ for date_str in sorted_dates:
206
+ day = daily_stats[date_str]
207
+ sorted_models = [s.model_name for s in sorted(day.by_model.values(), key=sort_by_cost)]
208
+
209
+ first_row = True
210
+ for model_name in sorted_models:
211
+ stats = day.by_model[model_name]
212
+ usd_str, cny_str = format_cost_dual(stats.cost_usd, stats.cost_cny)
213
+
214
+ # Accumulate to global totals
215
+ if model_name not in global_by_model:
216
+ global_by_model[model_name] = ModelUsageStats(model_name=model_name)
217
+ global_by_model[model_name].input_tokens += stats.input_tokens
218
+ global_by_model[model_name].output_tokens += stats.output_tokens
219
+ global_by_model[model_name].cached_tokens += stats.cached_tokens
220
+ global_by_model[model_name].cost_usd += stats.cost_usd
221
+ global_by_model[model_name].cost_cny += stats.cost_cny
222
+
223
+ table.add_row(
224
+ format_date_display(date_str) if first_row else "",
225
+ f"- {model_name}",
226
+ format_tokens(stats.input_tokens),
227
+ format_tokens(stats.output_tokens),
228
+ format_tokens(stats.cached_tokens),
229
+ format_tokens(stats.total_tokens),
230
+ usd_str,
231
+ cny_str,
232
+ )
233
+ first_row = False
234
+
235
+ # Add subtotal row for this day
236
+ subtotal = day.get_subtotal()
237
+ usd_str, cny_str = format_cost_dual(subtotal.cost_usd, subtotal.cost_cny)
238
+ table.add_row(
239
+ "",
240
+ "[cyan] (subtotal)[/cyan]",
241
+ f"[cyan]{format_tokens(subtotal.input_tokens)}[/cyan]",
242
+ f"[cyan]{format_tokens(subtotal.output_tokens)}[/cyan]",
243
+ f"[cyan]{format_tokens(subtotal.cached_tokens)}[/cyan]",
244
+ f"[cyan]{format_tokens(subtotal.total_tokens)}[/cyan]",
245
+ f"[cyan]{usd_str}[/cyan]",
246
+ f"[cyan]{cny_str}[/cyan]",
247
+ )
248
+
249
+ # Add separator between days
250
+ if date_str != sorted_dates[-1]:
251
+ table.add_section()
252
+
253
+ # Add final section for totals
254
+ table.add_section()
255
+
256
+ # Build date range label for Total
257
+ if sorted_dates:
258
+ first_date = format_date_display(sorted_dates[0])
259
+ last_date = format_date_display(sorted_dates[-1])
260
+ if first_date == last_date:
261
+ total_label = f"[bold]Total[/bold]\n[dim]{first_date}[/dim]"
262
+ else:
263
+ total_label = f"[bold]Total[/bold]\n[dim]{first_date} ~[/dim]\n[dim]{last_date}[/dim]"
264
+ else:
265
+ total_label = "[bold]Total[/bold]"
266
+
267
+ # Add per-model totals
268
+ sorted_global_models = [s.model_name for s in sorted(global_by_model.values(), key=sort_by_cost)]
269
+ first_total_row = True
270
+ for model_name in sorted_global_models:
271
+ # Add empty row before first model to align with Total date range
272
+ if first_total_row:
273
+ table.add_row(total_label, "", "", "", "", "", "", "")
274
+ first_total_row = False
275
+ stats = global_by_model[model_name]
276
+ usd_str, cny_str = format_cost_dual(stats.cost_usd, stats.cost_cny)
277
+ table.add_row(
278
+ "",
279
+ f"- {model_name}",
280
+ format_tokens(stats.input_tokens),
281
+ format_tokens(stats.output_tokens),
282
+ format_tokens(stats.cached_tokens),
283
+ format_tokens(stats.total_tokens),
284
+ usd_str,
285
+ cny_str,
286
+ )
287
+ first_total_row = False
288
+
289
+ # Add grand total row
290
+ grand_total = ModelUsageStats(model_name="(total)")
291
+ for stats in global_by_model.values():
292
+ grand_total.input_tokens += stats.input_tokens
293
+ grand_total.output_tokens += stats.output_tokens
294
+ grand_total.cached_tokens += stats.cached_tokens
295
+ grand_total.cost_usd += stats.cost_usd
296
+ grand_total.cost_cny += stats.cost_cny
297
+
298
+ usd_str, cny_str = format_cost_dual(grand_total.cost_usd, grand_total.cost_cny)
299
+ table.add_row(
300
+ "",
301
+ "[bold] (total)[/bold]",
302
+ f"[bold]{format_tokens(grand_total.input_tokens)}[/bold]",
303
+ f"[bold]{format_tokens(grand_total.output_tokens)}[/bold]",
304
+ f"[bold]{format_tokens(grand_total.cached_tokens)}[/bold]",
305
+ f"[bold]{format_tokens(grand_total.total_tokens)}[/bold]",
306
+ f"[bold]{usd_str}[/bold]",
307
+ f"[bold]{cny_str}[/bold]",
308
+ )
309
+
310
+ return table
311
+
312
+
313
+ def cost_command(
314
+ days: int | None = typer.Option(None, "--days", "-d", help="Limit to last N days"),
315
+ ) -> None:
316
+ """Display aggregated usage statistics across all sessions."""
317
+ daily_stats = aggregate_all_sessions()
318
+
319
+ if not daily_stats:
320
+ typer.echo("No usage data found.")
321
+ raise typer.Exit(0)
322
+
323
+ # Filter by days if specified
324
+ if days is not None:
325
+ cutoff = datetime.now().strftime("%Y-%m-%d")
326
+ from datetime import timedelta
327
+
328
+ cutoff_date = datetime.now() - timedelta(days=days)
329
+ cutoff = cutoff_date.strftime("%Y-%m-%d")
330
+ daily_stats = {k: v for k, v in daily_stats.items() if k >= cutoff}
331
+
332
+ if not daily_stats:
333
+ typer.echo(f"No usage data found in the last {days} days.")
334
+ raise typer.Exit(0)
335
+
336
+ table = render_cost_table(daily_stats)
337
+ console = Console()
338
+ console.print(table)
339
+
340
+
341
+ def register_cost_commands(app: typer.Typer) -> None:
342
+ """Register cost command to the given Typer app."""
343
+ app.command("cost")(cost_command)