artificial-intelligence-usage 0.0.4__tar.gz → 0.0.6__tar.gz
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.
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/PKG-INFO +19 -2
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/README.md +17 -1
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/pyproject.toml +2 -1
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/src/artificial_intelligence_usage.egg-info/PKG-INFO +19 -2
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/src/artificial_intelligence_usage.egg-info/SOURCES.txt +1 -0
- artificial_intelligence_usage-0.0.6/src/artificial_intelligence_usage.egg-info/requires.txt +1 -0
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/src/artificial_intelligence_usage.py +337 -9
- artificial_intelligence_usage-0.0.6/tests/test_artificial_intelligence_usage.py +658 -0
- artificial_intelligence_usage-0.0.4/tests/test_artificial_intelligence_usage.py +0 -260
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/setup.cfg +0 -0
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/src/artificial_intelligence_usage.egg-info/dependency_links.txt +0 -0
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/src/artificial_intelligence_usage.egg-info/entry_points.txt +0 -0
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/src/artificial_intelligence_usage.egg-info/top_level.txt +0 -0
- {artificial_intelligence_usage-0.0.4 → artificial_intelligence_usage-0.0.6}/tests/test_publish_pypi.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: artificial-intelligence-usage
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6
|
|
4
4
|
Summary: Analyze usage of artificial intelligence tool.
|
|
5
5
|
Author: Kaizten Analytics
|
|
6
6
|
License-Expression: LicenseRef-Proprietary
|
|
@@ -8,11 +8,12 @@ Classifier: Programming Language :: Python :: 3
|
|
|
8
8
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
9
|
Requires-Python: >=3.10
|
|
10
10
|
Description-Content-Type: text/markdown
|
|
11
|
+
Requires-Dist: langid==1.1.6
|
|
11
12
|
|
|
12
13
|
# Artificial Intelligence Usage
|
|
13
14
|
|
|
14
15
|
`artificial-intelligence-usage` analyzes local Codex session rollouts and prints a summary of the
|
|
15
|
-
latest session followed by daily session
|
|
16
|
+
latest session followed by daily and per-session prompt, token, and rate-limit usage.
|
|
16
17
|
|
|
17
18
|
## Behavior
|
|
18
19
|
|
|
@@ -22,6 +23,21 @@ latest session followed by daily session, turn, token, and rate-limit usage.
|
|
|
22
23
|
- Shows status details from the most recently modified session.
|
|
23
24
|
- Reports sessions from the last 14 calendar days by default, including today.
|
|
24
25
|
- Counts user turns and cumulative tokens for each session.
|
|
26
|
+
- Breaks applied prompts down into Normal, Plan, and Goal counts. Goal counts
|
|
27
|
+
include automatic goal continuations, while background tasks such as guardian
|
|
28
|
+
reviews are excluded.
|
|
29
|
+
- Shows a second table with one row per session, including its start time,
|
|
30
|
+
finish time, session name (up to 10 characters), first-prompt preview (up to
|
|
31
|
+
20 characters), GitHub repository, usage, and language. A session is finished
|
|
32
|
+
when its final rollout event is `task_complete`; otherwise its finish value is
|
|
33
|
+
`In progress`.
|
|
34
|
+
- Reads session names from `~/.codex/session_index.jsonl`. Missing names and
|
|
35
|
+
prompts display as `Unknown`.
|
|
36
|
+
- For goal-only sessions, reads the goal objective from
|
|
37
|
+
`~/.codex/goals_1.sqlite` for the prompt preview and reports its language as
|
|
38
|
+
`Unknown`.
|
|
39
|
+
- Uses `langid`, restricted to English and Spanish, for prompt language
|
|
40
|
+
detection. Sessions without a detectable user prompt show `Unknown`.
|
|
25
41
|
- Shows the remaining five-hour and weekly allowance recorded in each day's
|
|
26
42
|
last session.
|
|
27
43
|
- Ignores malformed JSON lines and incomplete status events.
|
|
@@ -31,6 +47,7 @@ The tool only reads local session files and does not modify them.
|
|
|
31
47
|
## Requirements
|
|
32
48
|
|
|
33
49
|
- Python 3.10 or newer
|
|
50
|
+
- `langid` 1.1.6 (installed automatically with the package)
|
|
34
51
|
|
|
35
52
|
## Building
|
|
36
53
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Artificial Intelligence Usage
|
|
2
2
|
|
|
3
3
|
`artificial-intelligence-usage` analyzes local Codex session rollouts and prints a summary of the
|
|
4
|
-
latest session followed by daily session
|
|
4
|
+
latest session followed by daily and per-session prompt, token, and rate-limit usage.
|
|
5
5
|
|
|
6
6
|
## Behavior
|
|
7
7
|
|
|
@@ -11,6 +11,21 @@ latest session followed by daily session, turn, token, and rate-limit usage.
|
|
|
11
11
|
- Shows status details from the most recently modified session.
|
|
12
12
|
- Reports sessions from the last 14 calendar days by default, including today.
|
|
13
13
|
- Counts user turns and cumulative tokens for each session.
|
|
14
|
+
- Breaks applied prompts down into Normal, Plan, and Goal counts. Goal counts
|
|
15
|
+
include automatic goal continuations, while background tasks such as guardian
|
|
16
|
+
reviews are excluded.
|
|
17
|
+
- Shows a second table with one row per session, including its start time,
|
|
18
|
+
finish time, session name (up to 10 characters), first-prompt preview (up to
|
|
19
|
+
20 characters), GitHub repository, usage, and language. A session is finished
|
|
20
|
+
when its final rollout event is `task_complete`; otherwise its finish value is
|
|
21
|
+
`In progress`.
|
|
22
|
+
- Reads session names from `~/.codex/session_index.jsonl`. Missing names and
|
|
23
|
+
prompts display as `Unknown`.
|
|
24
|
+
- For goal-only sessions, reads the goal objective from
|
|
25
|
+
`~/.codex/goals_1.sqlite` for the prompt preview and reports its language as
|
|
26
|
+
`Unknown`.
|
|
27
|
+
- Uses `langid`, restricted to English and Spanish, for prompt language
|
|
28
|
+
detection. Sessions without a detectable user prompt show `Unknown`.
|
|
14
29
|
- Shows the remaining five-hour and weekly allowance recorded in each day's
|
|
15
30
|
last session.
|
|
16
31
|
- Ignores malformed JSON lines and incomplete status events.
|
|
@@ -20,6 +35,7 @@ The tool only reads local session files and does not modify them.
|
|
|
20
35
|
## Requirements
|
|
21
36
|
|
|
22
37
|
- Python 3.10 or newer
|
|
38
|
+
- `langid` 1.1.6 (installed automatically with the package)
|
|
23
39
|
|
|
24
40
|
## Building
|
|
25
41
|
|
|
@@ -4,12 +4,13 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "artificial-intelligence-usage"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.6"
|
|
8
8
|
description = "Analyze usage of artificial intelligence tool."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
11
11
|
license = "LicenseRef-Proprietary"
|
|
12
12
|
authors = [{ name = "Kaizten Analytics" }]
|
|
13
|
+
dependencies = ["langid==1.1.6"]
|
|
13
14
|
classifiers = [
|
|
14
15
|
"Programming Language :: Python :: 3",
|
|
15
16
|
"Programming Language :: Python :: 3 :: Only",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: artificial-intelligence-usage
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6
|
|
4
4
|
Summary: Analyze usage of artificial intelligence tool.
|
|
5
5
|
Author: Kaizten Analytics
|
|
6
6
|
License-Expression: LicenseRef-Proprietary
|
|
@@ -8,11 +8,12 @@ Classifier: Programming Language :: Python :: 3
|
|
|
8
8
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
9
|
Requires-Python: >=3.10
|
|
10
10
|
Description-Content-Type: text/markdown
|
|
11
|
+
Requires-Dist: langid==1.1.6
|
|
11
12
|
|
|
12
13
|
# Artificial Intelligence Usage
|
|
13
14
|
|
|
14
15
|
`artificial-intelligence-usage` analyzes local Codex session rollouts and prints a summary of the
|
|
15
|
-
latest session followed by daily session
|
|
16
|
+
latest session followed by daily and per-session prompt, token, and rate-limit usage.
|
|
16
17
|
|
|
17
18
|
## Behavior
|
|
18
19
|
|
|
@@ -22,6 +23,21 @@ latest session followed by daily session, turn, token, and rate-limit usage.
|
|
|
22
23
|
- Shows status details from the most recently modified session.
|
|
23
24
|
- Reports sessions from the last 14 calendar days by default, including today.
|
|
24
25
|
- Counts user turns and cumulative tokens for each session.
|
|
26
|
+
- Breaks applied prompts down into Normal, Plan, and Goal counts. Goal counts
|
|
27
|
+
include automatic goal continuations, while background tasks such as guardian
|
|
28
|
+
reviews are excluded.
|
|
29
|
+
- Shows a second table with one row per session, including its start time,
|
|
30
|
+
finish time, session name (up to 10 characters), first-prompt preview (up to
|
|
31
|
+
20 characters), GitHub repository, usage, and language. A session is finished
|
|
32
|
+
when its final rollout event is `task_complete`; otherwise its finish value is
|
|
33
|
+
`In progress`.
|
|
34
|
+
- Reads session names from `~/.codex/session_index.jsonl`. Missing names and
|
|
35
|
+
prompts display as `Unknown`.
|
|
36
|
+
- For goal-only sessions, reads the goal objective from
|
|
37
|
+
`~/.codex/goals_1.sqlite` for the prompt preview and reports its language as
|
|
38
|
+
`Unknown`.
|
|
39
|
+
- Uses `langid`, restricted to English and Spanish, for prompt language
|
|
40
|
+
detection. Sessions without a detectable user prompt show `Unknown`.
|
|
25
41
|
- Shows the remaining five-hour and weekly allowance recorded in each day's
|
|
26
42
|
last session.
|
|
27
43
|
- Ignores malformed JSON lines and incomplete status events.
|
|
@@ -31,6 +47,7 @@ The tool only reads local session files and does not modify them.
|
|
|
31
47
|
## Requirements
|
|
32
48
|
|
|
33
49
|
- Python 3.10 or newer
|
|
50
|
+
- `langid` 1.1.6 (installed automatically with the package)
|
|
34
51
|
|
|
35
52
|
## Building
|
|
36
53
|
|
|
@@ -5,6 +5,7 @@ src/artificial_intelligence_usage.egg-info/PKG-INFO
|
|
|
5
5
|
src/artificial_intelligence_usage.egg-info/SOURCES.txt
|
|
6
6
|
src/artificial_intelligence_usage.egg-info/dependency_links.txt
|
|
7
7
|
src/artificial_intelligence_usage.egg-info/entry_points.txt
|
|
8
|
+
src/artificial_intelligence_usage.egg-info/requires.txt
|
|
8
9
|
src/artificial_intelligence_usage.egg-info/top_level.txt
|
|
9
10
|
tests/test_artificial_intelligence_usage.py
|
|
10
11
|
tests/test_publish_pypi.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
langid==1.1.6
|
|
@@ -5,16 +5,47 @@ from __future__ import annotations
|
|
|
5
5
|
|
|
6
6
|
import argparse
|
|
7
7
|
import json
|
|
8
|
+
import re
|
|
8
9
|
import shutil
|
|
10
|
+
import sqlite3
|
|
9
11
|
import subprocess
|
|
10
12
|
from collections import Counter, defaultdict
|
|
13
|
+
from dataclasses import dataclass
|
|
11
14
|
from datetime import date, datetime, timedelta
|
|
12
15
|
from pathlib import Path
|
|
13
16
|
from typing import Any
|
|
14
17
|
|
|
18
|
+
import langid
|
|
19
|
+
|
|
15
20
|
|
|
16
21
|
DEFAULT_DAYS = 14
|
|
17
22
|
SUPPORTED_TOOLS = (("Codex", "codex"), ("Claude", "claude"))
|
|
23
|
+
INTERNAL_USER_TEXT_TAGS = {
|
|
24
|
+
"codex_internal_context",
|
|
25
|
+
"environment_context",
|
|
26
|
+
"goal",
|
|
27
|
+
"goals",
|
|
28
|
+
"guardian_tool_descriptions",
|
|
29
|
+
"turn_aborted",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
langid.set_languages(["en", "es"])
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class SessionAnalysis:
|
|
37
|
+
"""Usage information extracted from one Codex session."""
|
|
38
|
+
|
|
39
|
+
turns: int
|
|
40
|
+
tokens: int
|
|
41
|
+
normal_prompts: int
|
|
42
|
+
plan_prompts: int
|
|
43
|
+
goal_prompts: int
|
|
44
|
+
repository: str
|
|
45
|
+
language: str
|
|
46
|
+
session_id: str | None
|
|
47
|
+
first_prompt: str | None
|
|
48
|
+
finish: str
|
|
18
49
|
|
|
19
50
|
|
|
20
51
|
def positive_int(value: str) -> int:
|
|
@@ -92,6 +123,21 @@ def format_timestamp(value: str | int | float | None) -> str:
|
|
|
92
123
|
return "unknown"
|
|
93
124
|
|
|
94
125
|
|
|
126
|
+
def format_time(value: str | int | float | None) -> str:
|
|
127
|
+
"""Format an ISO or Unix timestamp as a local time."""
|
|
128
|
+
try:
|
|
129
|
+
if isinstance(value, str):
|
|
130
|
+
timestamp = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
131
|
+
elif isinstance(value, (int, float)):
|
|
132
|
+
timestamp = datetime.fromtimestamp(value).astimezone()
|
|
133
|
+
else:
|
|
134
|
+
return "Unknown"
|
|
135
|
+
|
|
136
|
+
return timestamp.astimezone().strftime("%H:%M:%S")
|
|
137
|
+
except (OverflowError, OSError, ValueError):
|
|
138
|
+
return "Unknown"
|
|
139
|
+
|
|
140
|
+
|
|
95
141
|
def format_window(minutes: Any) -> str:
|
|
96
142
|
"""Return a compact label for a rate-limit window."""
|
|
97
143
|
if not isinstance(minutes, (int, float)):
|
|
@@ -263,10 +309,172 @@ def print_status(session_file: Path | None) -> None:
|
|
|
263
309
|
print()
|
|
264
310
|
|
|
265
311
|
|
|
266
|
-
def
|
|
267
|
-
"""Return
|
|
312
|
+
def prompt_type(payload: dict[str, Any]) -> str | None:
|
|
313
|
+
"""Return the prompt type for a task-started payload, if applicable."""
|
|
314
|
+
attribution = payload.get("turn_attribution")
|
|
315
|
+
if isinstance(attribution, dict):
|
|
316
|
+
trigger = attribution.get("turn_trigger")
|
|
317
|
+
elif isinstance(attribution, str):
|
|
318
|
+
trigger = attribution
|
|
319
|
+
else:
|
|
320
|
+
trigger = None
|
|
321
|
+
|
|
322
|
+
if trigger == "goal":
|
|
323
|
+
return "goal"
|
|
324
|
+
|
|
325
|
+
if trigger not in (None, "user"):
|
|
326
|
+
return None
|
|
327
|
+
|
|
328
|
+
mode = payload.get("collaboration_mode_kind")
|
|
329
|
+
if mode == "plan":
|
|
330
|
+
return "plan"
|
|
331
|
+
if mode in (None, "default"):
|
|
332
|
+
return "normal"
|
|
333
|
+
|
|
334
|
+
return None
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def github_repository(repository_url: Any) -> str:
|
|
338
|
+
"""Return ``owner/repository`` for a GitHub remote URL."""
|
|
339
|
+
if not isinstance(repository_url, str):
|
|
340
|
+
return "Unknown"
|
|
341
|
+
|
|
342
|
+
match = re.match(
|
|
343
|
+
r"^(?:https?://|ssh://git@|git@)github\.com[/:]"
|
|
344
|
+
r"(?P<owner>[^/]+)/(?P<repository>[^/#]+)/?$",
|
|
345
|
+
repository_url.strip(),
|
|
346
|
+
re.IGNORECASE,
|
|
347
|
+
)
|
|
348
|
+
if match is None:
|
|
349
|
+
return "Unknown"
|
|
350
|
+
|
|
351
|
+
repository = match.group("repository").removesuffix(".git")
|
|
352
|
+
return f"{match.group('owner')}/{repository}"
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def is_internal_user_text(text: str) -> bool:
|
|
356
|
+
"""Return whether user-role text contains Codex-injected context."""
|
|
357
|
+
match = re.match(r"^\s*<([A-Za-z0-9_-]+)(?:\s|>)", text)
|
|
358
|
+
return match is not None and match.group(1) in INTERNAL_USER_TEXT_TAGS
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def user_prompt_text(payload: dict[str, Any]) -> str | None:
|
|
362
|
+
"""Return real user-authored text from a user-message payload."""
|
|
363
|
+
content = payload.get("content")
|
|
364
|
+
if not isinstance(content, list):
|
|
365
|
+
return None
|
|
366
|
+
|
|
367
|
+
parts = []
|
|
368
|
+
for item in content:
|
|
369
|
+
if not isinstance(item, dict) or item.get("type") != "input_text":
|
|
370
|
+
continue
|
|
371
|
+
|
|
372
|
+
text = item.get("text")
|
|
373
|
+
if (
|
|
374
|
+
isinstance(text, str)
|
|
375
|
+
and text.strip()
|
|
376
|
+
and not is_internal_user_text(text)
|
|
377
|
+
):
|
|
378
|
+
parts.append(text.strip())
|
|
379
|
+
|
|
380
|
+
return "\n".join(parts) or None
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def detect_language(prompt: str | None) -> str:
|
|
384
|
+
"""Classify a prompt as English or Spanish."""
|
|
385
|
+
if prompt is None:
|
|
386
|
+
return "Unknown"
|
|
387
|
+
|
|
388
|
+
language_code, _confidence = langid.classify(prompt)
|
|
389
|
+
return {"en": "English", "es": "Spanish"}.get(language_code, "Unknown")
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def compact_text(value: str | None, maximum_length: int) -> str:
|
|
393
|
+
"""Normalize whitespace and truncate display text."""
|
|
394
|
+
if not isinstance(value, str):
|
|
395
|
+
return "Unknown"
|
|
396
|
+
|
|
397
|
+
normalized = " ".join(value.split())
|
|
398
|
+
return normalized[:maximum_length] or "Unknown"
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def read_session_names(session_index_file: Path) -> dict[str, str]:
|
|
402
|
+
"""Return the latest indexed name for each Codex session."""
|
|
403
|
+
names: dict[str, str] = {}
|
|
404
|
+
|
|
405
|
+
try:
|
|
406
|
+
with session_index_file.open("r", encoding="utf-8") as file_handle:
|
|
407
|
+
for line in file_handle:
|
|
408
|
+
try:
|
|
409
|
+
entry = json.loads(line)
|
|
410
|
+
except (json.JSONDecodeError, UnicodeDecodeError):
|
|
411
|
+
continue
|
|
412
|
+
|
|
413
|
+
session_id = entry.get("id")
|
|
414
|
+
thread_name = entry.get("thread_name")
|
|
415
|
+
if (
|
|
416
|
+
isinstance(session_id, str)
|
|
417
|
+
and session_id
|
|
418
|
+
and isinstance(thread_name, str)
|
|
419
|
+
and thread_name.strip()
|
|
420
|
+
):
|
|
421
|
+
names[session_id] = thread_name
|
|
422
|
+
except (OSError, UnicodeError):
|
|
423
|
+
pass
|
|
424
|
+
|
|
425
|
+
return names
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
def read_goal_objectives(goals_database: Path) -> dict[str, str]:
|
|
429
|
+
"""Return goal objectives keyed by their Codex thread ID."""
|
|
430
|
+
if not goals_database.is_file():
|
|
431
|
+
return {}
|
|
432
|
+
|
|
433
|
+
connection: sqlite3.Connection | None = None
|
|
434
|
+
try:
|
|
435
|
+
database_uri = f"{goals_database.resolve().as_uri()}?mode=ro"
|
|
436
|
+
connection = sqlite3.connect(database_uri, uri=True)
|
|
437
|
+
rows = connection.execute(
|
|
438
|
+
"SELECT thread_id, objective FROM thread_goals"
|
|
439
|
+
).fetchall()
|
|
440
|
+
except (OSError, sqlite3.Error):
|
|
441
|
+
return {}
|
|
442
|
+
finally:
|
|
443
|
+
if connection is not None:
|
|
444
|
+
connection.close()
|
|
445
|
+
|
|
446
|
+
return {
|
|
447
|
+
thread_id: objective
|
|
448
|
+
for thread_id, objective in rows
|
|
449
|
+
if isinstance(thread_id, str)
|
|
450
|
+
and thread_id
|
|
451
|
+
and isinstance(objective, str)
|
|
452
|
+
and objective.strip()
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def session_start_time(session_file: Path) -> str:
|
|
457
|
+
"""Return the session start time encoded in a rollout filename."""
|
|
458
|
+
match = re.match(
|
|
459
|
+
r"^rollout-\d{4}-\d{2}-\d{2}T(\d{2})-(\d{2})-(\d{2})-",
|
|
460
|
+
session_file.name,
|
|
461
|
+
)
|
|
462
|
+
if match is None:
|
|
463
|
+
return "Unknown"
|
|
464
|
+
|
|
465
|
+
return ":".join(match.groups())
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def analyze_session(session_file: Path) -> SessionAnalysis:
|
|
469
|
+
"""Return usage information extracted from one Codex session."""
|
|
268
470
|
turns = 0
|
|
269
471
|
total_tokens = 0
|
|
472
|
+
prompt_counts: Counter[str] = Counter()
|
|
473
|
+
repository = "Unknown"
|
|
474
|
+
session_id = None
|
|
475
|
+
first_prompt = None
|
|
476
|
+
last_event_is_task_complete = False
|
|
477
|
+
last_event_timestamp = None
|
|
270
478
|
|
|
271
479
|
with session_file.open("r", encoding="utf-8") as file_handle:
|
|
272
480
|
for line in file_handle:
|
|
@@ -276,6 +484,26 @@ def analyze_session(session_file: Path) -> tuple[int, int]:
|
|
|
276
484
|
continue
|
|
277
485
|
|
|
278
486
|
payload = event.get("payload") or {}
|
|
487
|
+
last_event_is_task_complete = (
|
|
488
|
+
event.get("type") == "event_msg"
|
|
489
|
+
and payload.get("type") == "task_complete"
|
|
490
|
+
)
|
|
491
|
+
last_event_timestamp = event.get("timestamp")
|
|
492
|
+
|
|
493
|
+
if event.get("type") == "session_meta":
|
|
494
|
+
candidate_session_id = payload.get("id") or payload.get("session_id")
|
|
495
|
+
if isinstance(candidate_session_id, str) and candidate_session_id:
|
|
496
|
+
session_id = candidate_session_id
|
|
497
|
+
git = payload.get("git") or {}
|
|
498
|
+
repository = github_repository(git.get("repository_url"))
|
|
499
|
+
|
|
500
|
+
if (
|
|
501
|
+
event.get("type") == "event_msg"
|
|
502
|
+
and payload.get("type") == "task_started"
|
|
503
|
+
):
|
|
504
|
+
category = prompt_type(payload)
|
|
505
|
+
if category is not None:
|
|
506
|
+
prompt_counts[category] += 1
|
|
279
507
|
|
|
280
508
|
if (
|
|
281
509
|
event.get("type") == "response_item"
|
|
@@ -283,6 +511,8 @@ def analyze_session(session_file: Path) -> tuple[int, int]:
|
|
|
283
511
|
and payload.get("role") == "user"
|
|
284
512
|
):
|
|
285
513
|
turns += 1
|
|
514
|
+
if first_prompt is None:
|
|
515
|
+
first_prompt = user_prompt_text(payload)
|
|
286
516
|
|
|
287
517
|
if (
|
|
288
518
|
event.get("type") == "event_msg"
|
|
@@ -295,7 +525,22 @@ def analyze_session(session_file: Path) -> tuple[int, int]:
|
|
|
295
525
|
if isinstance(value, (int, float)):
|
|
296
526
|
total_tokens = max(total_tokens, int(value))
|
|
297
527
|
|
|
298
|
-
return
|
|
528
|
+
return SessionAnalysis(
|
|
529
|
+
turns=turns,
|
|
530
|
+
tokens=total_tokens,
|
|
531
|
+
normal_prompts=prompt_counts["normal"],
|
|
532
|
+
plan_prompts=prompt_counts["plan"],
|
|
533
|
+
goal_prompts=prompt_counts["goal"],
|
|
534
|
+
repository=repository,
|
|
535
|
+
language=detect_language(first_prompt),
|
|
536
|
+
session_id=session_id,
|
|
537
|
+
first_prompt=first_prompt,
|
|
538
|
+
finish=(
|
|
539
|
+
format_time(last_event_timestamp)
|
|
540
|
+
if last_event_is_task_complete
|
|
541
|
+
else "In progress"
|
|
542
|
+
),
|
|
543
|
+
)
|
|
299
544
|
|
|
300
545
|
|
|
301
546
|
def build_parser() -> argparse.ArgumentParser:
|
|
@@ -315,6 +560,8 @@ def run_report(
|
|
|
315
560
|
days: int,
|
|
316
561
|
today: date | None = None,
|
|
317
562
|
tool_versions: dict[str, str] | None = None,
|
|
563
|
+
session_index_file: Path | None = None,
|
|
564
|
+
goals_database: Path | None = None,
|
|
318
565
|
) -> int:
|
|
319
566
|
"""Analyze sessions and print the status and daily usage report."""
|
|
320
567
|
current_day = today or date.today()
|
|
@@ -322,12 +569,24 @@ def run_report(
|
|
|
322
569
|
session_counts: Counter[str] = Counter()
|
|
323
570
|
turn_counts: defaultdict[str, list[int]] = defaultdict(list)
|
|
324
571
|
token_counts: Counter[str] = Counter()
|
|
572
|
+
normal_prompt_counts: Counter[str] = Counter()
|
|
573
|
+
plan_prompt_counts: Counter[str] = Counter()
|
|
574
|
+
goal_prompt_counts: Counter[str] = Counter()
|
|
325
575
|
last_session_by_date: dict[str, Path] = {}
|
|
576
|
+
session_rows: list[tuple[str, Path, SessionAnalysis]] = []
|
|
326
577
|
|
|
327
578
|
if tool_versions is None:
|
|
328
579
|
tool_versions = detect_installed_tool_versions()
|
|
329
580
|
print_installed_tool_versions(tool_versions)
|
|
330
581
|
|
|
582
|
+
codex_dir = sessions_dir.parent
|
|
583
|
+
session_names = read_session_names(
|
|
584
|
+
session_index_file or codex_dir / "session_index.jsonl"
|
|
585
|
+
)
|
|
586
|
+
goal_objectives = read_goal_objectives(
|
|
587
|
+
goals_database or codex_dir / "goals_1.sqlite"
|
|
588
|
+
)
|
|
589
|
+
|
|
331
590
|
all_session_files = list(sessions_dir.glob("*/*/*/rollout-*.jsonl"))
|
|
332
591
|
latest_session = (
|
|
333
592
|
max(all_session_files, key=lambda path: path.stat().st_mtime)
|
|
@@ -349,10 +608,14 @@ def run_report(
|
|
|
349
608
|
if not first_day <= session_date <= current_day:
|
|
350
609
|
continue
|
|
351
610
|
|
|
352
|
-
|
|
611
|
+
analysis = analyze_session(session_file)
|
|
353
612
|
session_counts[date_label] += 1
|
|
354
|
-
turn_counts[date_label].append(turns)
|
|
355
|
-
token_counts[date_label] += tokens
|
|
613
|
+
turn_counts[date_label].append(analysis.turns)
|
|
614
|
+
token_counts[date_label] += analysis.tokens
|
|
615
|
+
normal_prompt_counts[date_label] += analysis.normal_prompts
|
|
616
|
+
plan_prompt_counts[date_label] += analysis.plan_prompts
|
|
617
|
+
goal_prompt_counts[date_label] += analysis.goal_prompts
|
|
618
|
+
session_rows.append((date_label, session_file, analysis))
|
|
356
619
|
|
|
357
620
|
previous_last = last_session_by_date.get(date_label)
|
|
358
621
|
if previous_last is None or session_file.name > previous_last.name:
|
|
@@ -362,16 +625,20 @@ def run_report(
|
|
|
362
625
|
print(f"No Codex sessions found in the last {days} days in {sessions_dir}")
|
|
363
626
|
return 0
|
|
364
627
|
|
|
365
|
-
|
|
628
|
+
header = (
|
|
366
629
|
f"{'Date':<12} "
|
|
367
630
|
f"{'Sessions':>8} "
|
|
368
631
|
f"{'Turns':>8} "
|
|
369
632
|
f"{'Avg turns':>10} "
|
|
633
|
+
f"{'Normal':>8} "
|
|
634
|
+
f"{'Plan':>8} "
|
|
635
|
+
f"{'Goal':>8} "
|
|
370
636
|
f"{'Tokens':>15} "
|
|
371
637
|
f"{'5h last session':>17} "
|
|
372
638
|
f"{'Weekly last session':>20}"
|
|
373
639
|
)
|
|
374
|
-
print(
|
|
640
|
+
print(header)
|
|
641
|
+
print("-" * len(header))
|
|
375
642
|
|
|
376
643
|
for date_label in sorted(session_counts):
|
|
377
644
|
sessions = session_counts[date_label]
|
|
@@ -385,6 +652,9 @@ def run_report(
|
|
|
385
652
|
f"{sessions:>8} "
|
|
386
653
|
f"{total_turns:>8} "
|
|
387
654
|
f"{avg_turns:>10.1f} "
|
|
655
|
+
f"{normal_prompt_counts[date_label]:>8} "
|
|
656
|
+
f"{plan_prompt_counts[date_label]:>8} "
|
|
657
|
+
f"{goal_prompt_counts[date_label]:>8} "
|
|
388
658
|
f"{tokens:>15,} "
|
|
389
659
|
f"{five_hour:>17} "
|
|
390
660
|
f"{weekly:>20}"
|
|
@@ -393,18 +663,76 @@ def run_report(
|
|
|
393
663
|
total_sessions = sum(session_counts.values())
|
|
394
664
|
total_turns = sum(sum(turns) for turns in turn_counts.values())
|
|
395
665
|
total_tokens = sum(token_counts.values())
|
|
666
|
+
total_normal_prompts = sum(normal_prompt_counts.values())
|
|
667
|
+
total_plan_prompts = sum(plan_prompt_counts.values())
|
|
668
|
+
total_goal_prompts = sum(goal_prompt_counts.values())
|
|
396
669
|
overall_average = total_turns / total_sessions
|
|
397
670
|
|
|
398
|
-
print("-" *
|
|
671
|
+
print("-" * len(header))
|
|
399
672
|
print(
|
|
400
673
|
f"{'Total':<12} "
|
|
401
674
|
f"{total_sessions:>8} "
|
|
402
675
|
f"{total_turns:>8} "
|
|
403
676
|
f"{overall_average:>10.1f} "
|
|
677
|
+
f"{total_normal_prompts:>8} "
|
|
678
|
+
f"{total_plan_prompts:>8} "
|
|
679
|
+
f"{total_goal_prompts:>8} "
|
|
404
680
|
f"{total_tokens:>15,} "
|
|
405
681
|
f"{'-':>17} "
|
|
406
682
|
f"{'-':>20}"
|
|
407
683
|
)
|
|
684
|
+
|
|
685
|
+
print()
|
|
686
|
+
print("Sessions")
|
|
687
|
+
repository_width = max(
|
|
688
|
+
len("Repository"),
|
|
689
|
+
*(len(analysis.repository) for _date, _path, analysis in session_rows),
|
|
690
|
+
)
|
|
691
|
+
session_header = (
|
|
692
|
+
f"{'Date':<12} "
|
|
693
|
+
f"{'Start':<8} "
|
|
694
|
+
f"{'Finish':<11} "
|
|
695
|
+
f"{'Repository':<{repository_width}} "
|
|
696
|
+
f"{'Name':<10} "
|
|
697
|
+
f"{'First prompt':<20} "
|
|
698
|
+
f"{'Turns':>8} "
|
|
699
|
+
f"{'Normal':>8} "
|
|
700
|
+
f"{'Plan':>8} "
|
|
701
|
+
f"{'Goal':>8} "
|
|
702
|
+
f"{'Language':<8} "
|
|
703
|
+
f"{'Tokens':>15} "
|
|
704
|
+
f"{'5h last session':>17} "
|
|
705
|
+
f"{'Weekly last session':>20}"
|
|
706
|
+
)
|
|
707
|
+
print(session_header)
|
|
708
|
+
print("-" * len(session_header))
|
|
709
|
+
|
|
710
|
+
for date_label, session_file, analysis in sorted(
|
|
711
|
+
session_rows, key=lambda row: (row[0], row[1].name)
|
|
712
|
+
):
|
|
713
|
+
five_hour, weekly = session_rate_limits(session_file)
|
|
714
|
+
session_name = compact_text(session_names.get(analysis.session_id), 10)
|
|
715
|
+
first_prompt = analysis.first_prompt
|
|
716
|
+
if first_prompt is None and analysis.goal_prompts:
|
|
717
|
+
first_prompt = goal_objectives.get(analysis.session_id)
|
|
718
|
+
prompt_preview = compact_text(first_prompt, 20)
|
|
719
|
+
print(
|
|
720
|
+
f"{date_label:<12} "
|
|
721
|
+
f"{session_start_time(session_file):<8} "
|
|
722
|
+
f"{analysis.finish:<11} "
|
|
723
|
+
f"{analysis.repository:<{repository_width}} "
|
|
724
|
+
f"{session_name:<10} "
|
|
725
|
+
f"{prompt_preview:<20} "
|
|
726
|
+
f"{analysis.turns:>8} "
|
|
727
|
+
f"{analysis.normal_prompts:>8} "
|
|
728
|
+
f"{analysis.plan_prompts:>8} "
|
|
729
|
+
f"{analysis.goal_prompts:>8} "
|
|
730
|
+
f"{analysis.language:<8} "
|
|
731
|
+
f"{analysis.tokens:>15,} "
|
|
732
|
+
f"{five_hour:>17} "
|
|
733
|
+
f"{weekly:>20}"
|
|
734
|
+
)
|
|
735
|
+
|
|
408
736
|
return 0
|
|
409
737
|
|
|
410
738
|
|