artificial-intelligence-usage 0.0.6__tar.gz → 0.0.8__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.
Files changed (13) hide show
  1. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/PKG-INFO +6 -3
  2. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/README.md +5 -2
  3. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/pyproject.toml +1 -1
  4. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/src/artificial_intelligence_usage.egg-info/PKG-INFO +6 -3
  5. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/src/artificial_intelligence_usage.py +55 -2
  6. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/tests/test_artificial_intelligence_usage.py +107 -3
  7. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/setup.cfg +0 -0
  8. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/src/artificial_intelligence_usage.egg-info/SOURCES.txt +0 -0
  9. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/src/artificial_intelligence_usage.egg-info/dependency_links.txt +0 -0
  10. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/src/artificial_intelligence_usage.egg-info/entry_points.txt +0 -0
  11. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/src/artificial_intelligence_usage.egg-info/requires.txt +0 -0
  12. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/src/artificial_intelligence_usage.egg-info/top_level.txt +0 -0
  13. {artificial_intelligence_usage-0.0.6 → artificial_intelligence_usage-0.0.8}/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.6
3
+ Version: 0.0.8
4
4
  Summary: Analyze usage of artificial intelligence tool.
5
5
  Author: Kaizten Analytics
6
6
  License-Expression: LicenseRef-Proprietary
@@ -19,7 +19,9 @@ latest session followed by daily and per-session prompt, token, and rate-limit u
19
19
 
20
20
  - Reads rollout files below `~/.codex/sessions`.
21
21
  - Shows the installed Codex and Claude CLI versions when those commands are
22
- available.
22
+ available, followed by the latest versions published under their official
23
+ npm packages. Latest-version checks require network access and display
24
+ `UNKNOWN` when a lookup fails.
23
25
  - Shows status details from the most recently modified session.
24
26
  - Reports sessions from the last 14 calendar days by default, including today.
25
27
  - Counts user turns and cumulative tokens for each session.
@@ -28,7 +30,8 @@ latest session followed by daily and per-session prompt, token, and rate-limit u
28
30
  reviews are excluded.
29
31
  - Shows a second table with one row per session, including its start time,
30
32
  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
33
+ 20 characters), model, GitHub repository, usage, and language. When a session
34
+ records multiple models, the table shows the first one. A session is finished
32
35
  when its final rollout event is `task_complete`; otherwise its finish value is
33
36
  `In progress`.
34
37
  - Reads session names from `~/.codex/session_index.jsonl`. Missing names and
@@ -7,7 +7,9 @@ latest session followed by daily and per-session prompt, token, and rate-limit u
7
7
 
8
8
  - Reads rollout files below `~/.codex/sessions`.
9
9
  - Shows the installed Codex and Claude CLI versions when those commands are
10
- available.
10
+ available, followed by the latest versions published under their official
11
+ npm packages. Latest-version checks require network access and display
12
+ `UNKNOWN` when a lookup fails.
11
13
  - Shows status details from the most recently modified session.
12
14
  - Reports sessions from the last 14 calendar days by default, including today.
13
15
  - Counts user turns and cumulative tokens for each session.
@@ -16,7 +18,8 @@ latest session followed by daily and per-session prompt, token, and rate-limit u
16
18
  reviews are excluded.
17
19
  - Shows a second table with one row per session, including its start time,
18
20
  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
21
+ 20 characters), model, GitHub repository, usage, and language. When a session
22
+ records multiple models, the table shows the first one. A session is finished
20
23
  when its final rollout event is `task_complete`; otherwise its finish value is
21
24
  `In progress`.
22
25
  - Reads session names from `~/.codex/session_index.jsonl`. Missing names and
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "artificial-intelligence-usage"
7
- version = "0.0.6"
7
+ version = "0.0.8"
8
8
  description = "Analyze usage of artificial intelligence tool."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: artificial-intelligence-usage
3
- Version: 0.0.6
3
+ Version: 0.0.8
4
4
  Summary: Analyze usage of artificial intelligence tool.
5
5
  Author: Kaizten Analytics
6
6
  License-Expression: LicenseRef-Proprietary
@@ -19,7 +19,9 @@ latest session followed by daily and per-session prompt, token, and rate-limit u
19
19
 
20
20
  - Reads rollout files below `~/.codex/sessions`.
21
21
  - Shows the installed Codex and Claude CLI versions when those commands are
22
- available.
22
+ available, followed by the latest versions published under their official
23
+ npm packages. Latest-version checks require network access and display
24
+ `UNKNOWN` when a lookup fails.
23
25
  - Shows status details from the most recently modified session.
24
26
  - Reports sessions from the last 14 calendar days by default, including today.
25
27
  - Counts user turns and cumulative tokens for each session.
@@ -28,7 +30,8 @@ latest session followed by daily and per-session prompt, token, and rate-limit u
28
30
  reviews are excluded.
29
31
  - Shows a second table with one row per session, including its start time,
30
32
  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
33
+ 20 characters), model, GitHub repository, usage, and language. When a session
34
+ records multiple models, the table shows the first one. A session is finished
32
35
  when its final rollout event is `task_complete`; otherwise its finish value is
33
36
  `In progress`.
34
37
  - Reads session names from `~/.codex/session_index.jsonl`. Missing names and
@@ -9,6 +9,8 @@ import re
9
9
  import shutil
10
10
  import sqlite3
11
11
  import subprocess
12
+ import urllib.error
13
+ import urllib.request
12
14
  from collections import Counter, defaultdict
13
15
  from dataclasses import dataclass
14
16
  from datetime import date, datetime, timedelta
@@ -20,9 +22,15 @@ import langid
20
22
 
21
23
  DEFAULT_DAYS = 14
22
24
  SUPPORTED_TOOLS = (("Codex", "codex"), ("Claude", "claude"))
25
+ NPM_PACKAGES = {
26
+ "Codex": "@openai/codex",
27
+ "Claude": "@anthropic-ai/claude-code",
28
+ }
29
+ NPM_REGISTRY_TIMEOUT = 3
23
30
  INTERNAL_USER_TEXT_TAGS = {
24
31
  "codex_internal_context",
25
32
  "environment_context",
33
+ "external_codex_apps",
26
34
  "goal",
27
35
  "goals",
28
36
  "guardian_tool_descriptions",
@@ -42,6 +50,7 @@ class SessionAnalysis:
42
50
  plan_prompts: int
43
51
  goal_prompts: int
44
52
  repository: str
53
+ model: str
45
54
  language: str
46
55
  session_id: str | None
47
56
  first_prompt: str | None
@@ -95,7 +104,33 @@ def detect_installed_tool_versions() -> dict[str, str]:
95
104
  return versions
96
105
 
97
106
 
98
- def print_installed_tool_versions(versions: dict[str, str]) -> None:
107
+ def latest_tool_version(package: str) -> str:
108
+ """Return the latest npm version for a package, or ``UNKNOWN`` on failure."""
109
+ package_path = package.replace("/", "%2f")
110
+ url = f"https://registry.npmjs.org/{package_path}/latest"
111
+
112
+ try:
113
+ with urllib.request.urlopen(url, timeout=NPM_REGISTRY_TIMEOUT) as response:
114
+ metadata = json.loads(response.read())
115
+ except (OSError, TimeoutError, ValueError, urllib.error.URLError):
116
+ return "UNKNOWN"
117
+
118
+ version = metadata.get("version") if isinstance(metadata, dict) else None
119
+ return version.strip() if isinstance(version, str) and version.strip() else "UNKNOWN"
120
+
121
+
122
+ def detect_latest_tool_versions(installed_versions: dict[str, str]) -> dict[str, str]:
123
+ """Return latest npm versions for the tools that are currently installed."""
124
+ return {
125
+ name: latest_tool_version(NPM_PACKAGES[name])
126
+ for name in installed_versions
127
+ if name in NPM_PACKAGES
128
+ }
129
+
130
+
131
+ def print_installed_tool_versions(
132
+ versions: dict[str, str], latest_versions: dict[str, str] | None = None
133
+ ) -> None:
99
134
  """Print installed AI command-line tool versions when any are present."""
100
135
  if not versions:
101
136
  return
@@ -105,6 +140,8 @@ def print_installed_tool_versions(versions: dict[str, str]) -> None:
105
140
  for name, _command in SUPPORTED_TOOLS:
106
141
  if name in versions:
107
142
  print(f"{name + ':':<16} {versions[name]}")
143
+ latest_version = (latest_versions or {}).get(name, "UNKNOWN")
144
+ print(f"{name + ' latest version:':<26} {latest_version}")
108
145
  print()
109
146
 
110
147
 
@@ -471,6 +508,7 @@ def analyze_session(session_file: Path) -> SessionAnalysis:
471
508
  total_tokens = 0
472
509
  prompt_counts: Counter[str] = Counter()
473
510
  repository = "Unknown"
511
+ model = "Unknown"
474
512
  session_id = None
475
513
  first_prompt = None
476
514
  last_event_is_task_complete = False
@@ -497,6 +535,11 @@ def analyze_session(session_file: Path) -> SessionAnalysis:
497
535
  git = payload.get("git") or {}
498
536
  repository = github_repository(git.get("repository_url"))
499
537
 
538
+ if event.get("type") == "turn_context" and model == "Unknown":
539
+ candidate_model = payload.get("model")
540
+ if isinstance(candidate_model, str) and candidate_model:
541
+ model = candidate_model
542
+
500
543
  if (
501
544
  event.get("type") == "event_msg"
502
545
  and payload.get("type") == "task_started"
@@ -532,6 +575,7 @@ def analyze_session(session_file: Path) -> SessionAnalysis:
532
575
  plan_prompts=prompt_counts["plan"],
533
576
  goal_prompts=prompt_counts["goal"],
534
577
  repository=repository,
578
+ model=model,
535
579
  language=detect_language(first_prompt),
536
580
  session_id=session_id,
537
581
  first_prompt=first_prompt,
@@ -562,6 +606,7 @@ def run_report(
562
606
  tool_versions: dict[str, str] | None = None,
563
607
  session_index_file: Path | None = None,
564
608
  goals_database: Path | None = None,
609
+ latest_tool_versions: dict[str, str] | None = None,
565
610
  ) -> int:
566
611
  """Analyze sessions and print the status and daily usage report."""
567
612
  current_day = today or date.today()
@@ -577,7 +622,9 @@ def run_report(
577
622
 
578
623
  if tool_versions is None:
579
624
  tool_versions = detect_installed_tool_versions()
580
- print_installed_tool_versions(tool_versions)
625
+ if latest_tool_versions is None:
626
+ latest_tool_versions = detect_latest_tool_versions(tool_versions)
627
+ print_installed_tool_versions(tool_versions, latest_tool_versions)
581
628
 
582
629
  codex_dir = sessions_dir.parent
583
630
  session_names = read_session_names(
@@ -688,6 +735,10 @@ def run_report(
688
735
  len("Repository"),
689
736
  *(len(analysis.repository) for _date, _path, analysis in session_rows),
690
737
  )
738
+ model_width = max(
739
+ len("Model"),
740
+ *(len(analysis.model) for _date, _path, analysis in session_rows),
741
+ )
691
742
  session_header = (
692
743
  f"{'Date':<12} "
693
744
  f"{'Start':<8} "
@@ -695,6 +746,7 @@ def run_report(
695
746
  f"{'Repository':<{repository_width}} "
696
747
  f"{'Name':<10} "
697
748
  f"{'First prompt':<20} "
749
+ f"{'Model':<{model_width}} "
698
750
  f"{'Turns':>8} "
699
751
  f"{'Normal':>8} "
700
752
  f"{'Plan':>8} "
@@ -723,6 +775,7 @@ def run_report(
723
775
  f"{analysis.repository:<{repository_width}} "
724
776
  f"{session_name:<10} "
725
777
  f"{prompt_preview:<20} "
778
+ f"{analysis.model:<{model_width}} "
726
779
  f"{analysis.turns:>8} "
727
780
  f"{analysis.normal_prompts:>8} "
728
781
  f"{analysis.plan_prompts:>8} "
@@ -119,6 +119,55 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
119
119
  timeout=5,
120
120
  )
121
121
 
122
+ def test_latest_tool_version_reads_npm_latest_metadata(self):
123
+ response = io.BytesIO(b'{"version":"1.2.3"}')
124
+ with patch.object(
125
+ artificial_intelligence_usage.urllib.request,
126
+ "urlopen",
127
+ return_value=response,
128
+ ) as urlopen:
129
+ version = artificial_intelligence_usage.latest_tool_version(
130
+ "@openai/codex"
131
+ )
132
+
133
+ self.assertEqual(version, "1.2.3")
134
+ urlopen.assert_called_once_with(
135
+ "https://registry.npmjs.org/@openai%2fcodex/latest",
136
+ timeout=3,
137
+ )
138
+
139
+ def test_latest_tool_version_returns_unknown_when_lookup_fails(self):
140
+ cases = (
141
+ OSError("offline"),
142
+ ValueError("invalid JSON"),
143
+ )
144
+ for error in cases:
145
+ with self.subTest(error=error), patch.object(
146
+ artificial_intelligence_usage.urllib.request,
147
+ "urlopen",
148
+ side_effect=error,
149
+ ):
150
+ self.assertEqual(
151
+ artificial_intelligence_usage.latest_tool_version(
152
+ "@anthropic-ai/claude-code"
153
+ ),
154
+ "UNKNOWN",
155
+ )
156
+
157
+ def test_latest_tool_version_returns_unknown_for_missing_version(self):
158
+ response = io.BytesIO(b'{"name":"@openai/codex"}')
159
+ with patch.object(
160
+ artificial_intelligence_usage.urllib.request,
161
+ "urlopen",
162
+ return_value=response,
163
+ ):
164
+ self.assertEqual(
165
+ artificial_intelligence_usage.latest_tool_version(
166
+ "@openai/codex"
167
+ ),
168
+ "UNKNOWN",
169
+ )
170
+
122
171
  def test_report_includes_installed_codex_and_claude_versions(self):
123
172
  with tempfile.TemporaryDirectory() as directory:
124
173
  output = io.StringIO()
@@ -131,12 +180,18 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
131
180
  "Codex": "codex-cli 1.2.3",
132
181
  "Claude": "2.0.1 (Claude Code)",
133
182
  },
183
+ latest_tool_versions={
184
+ "Codex": "1.3.0",
185
+ "Claude": "2.1.0",
186
+ },
134
187
  )
135
188
 
136
189
  report = output.getvalue()
137
190
  self.assertIn("Installed AI tools", report)
138
191
  self.assertIn("Codex: codex-cli 1.2.3", report)
192
+ self.assertIn("Codex latest version: 1.3.0", report)
139
193
  self.assertIn("Claude: 2.0.1 (Claude Code)", report)
194
+ self.assertIn("Claude latest version: 2.1.0", report)
140
195
 
141
196
  def test_analyze_session_ignores_malformed_lines_and_uses_maximum_tokens(self):
142
197
  with tempfile.TemporaryDirectory() as directory:
@@ -166,6 +221,7 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
166
221
  plan_prompts=0,
167
222
  goal_prompts=0,
168
223
  repository="Unknown",
224
+ model="Unknown",
169
225
  language="Unknown",
170
226
  session_id=None,
171
227
  first_prompt=None,
@@ -223,6 +279,7 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
223
279
  plan_prompts=1,
224
280
  goal_prompts=1,
225
281
  repository="Unknown",
282
+ model="Unknown",
226
283
  language="Unknown",
227
284
  session_id=None,
228
285
  first_prompt=None,
@@ -246,6 +303,8 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
246
303
  }
247
304
  },
248
305
  },
306
+ {"type": "turn_context", "payload": {"model": "gpt-test"}},
307
+ {"type": "turn_context", "payload": {"model": "gpt-later"}},
249
308
  {
250
309
  "type": "response_item",
251
310
  "payload": {
@@ -294,6 +353,7 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
294
353
  analysis = artificial_intelligence_usage.analyze_session(session_file)
295
354
 
296
355
  self.assertEqual(analysis.repository, "kaizten/example")
356
+ self.assertEqual(analysis.model, "gpt-test")
297
357
  self.assertEqual(analysis.language, "Spanish")
298
358
  self.assertEqual(analysis.session_id, "session-example")
299
359
  self.assertEqual(
@@ -306,6 +366,49 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
306
366
  "09:08:07",
307
367
  )
308
368
 
369
+ def test_analyze_session_skips_external_codex_apps_context_for_first_prompt(self):
370
+ with tempfile.TemporaryDirectory() as directory:
371
+ session_file = Path(directory) / "rollout-test.jsonl"
372
+ events = [
373
+ {
374
+ "type": "response_item",
375
+ "payload": {
376
+ "type": "message",
377
+ "role": "user",
378
+ "content": [
379
+ {
380
+ "type": "input_text",
381
+ "text": (
382
+ "<external_codex_apps>Connected app context"
383
+ "</external_codex_apps>"
384
+ ),
385
+ }
386
+ ],
387
+ },
388
+ },
389
+ {
390
+ "type": "response_item",
391
+ "payload": {
392
+ "type": "message",
393
+ "role": "user",
394
+ "content": [
395
+ {
396
+ "type": "input_text",
397
+ "text": "Summarize the latest report.",
398
+ }
399
+ ],
400
+ },
401
+ },
402
+ ]
403
+ session_file.write_text(
404
+ "\n".join(json.dumps(event) for event in events) + "\n",
405
+ encoding="utf-8",
406
+ )
407
+
408
+ analysis = artificial_intelligence_usage.analyze_session(session_file)
409
+
410
+ self.assertEqual(analysis.first_prompt, "Summarize the latest report.")
411
+
309
412
  def test_analyze_session_uses_only_a_terminal_task_completion_as_finish(self):
310
413
  with tempfile.TemporaryDirectory() as directory:
311
414
  root = Path(directory)
@@ -576,14 +679,15 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
576
679
  self.assertRegex(report, r"Start\s+Finish\s+Repository")
577
680
  self.assertIn("Name", report)
578
681
  self.assertIn("First prompt", report)
682
+ self.assertRegex(report, r"First prompt\s+Model\s+Turns")
579
683
  self.assertIn("Repository", report)
580
684
  self.assertRegex(
581
685
  report,
582
- r"2026-10-04\s+09:00:00\s+10:05:06\s+kaizten/example\s+Updated se\s+Please update the us\s+1\s+1\s+1\s+1\s+English\s+100",
686
+ r"2026-10-04\s+09:00:00\s+10:05:06\s+kaizten/example\s+Updated se\s+Please update the us\s+gpt-test\s+1\s+1\s+1\s+1\s+English\s+100",
583
687
  )
584
688
  self.assertRegex(
585
689
  report,
586
- r"2026-10-04\s+10:00:00\s+In progress\s+Unknown\s+Unknown\s+Please update the us\s+2\s+1\s+0\s+0\s+English\s+250",
690
+ r"2026-10-04\s+10:00:00\s+In progress\s+Unknown\s+Unknown\s+Please update the us\s+Unknown\s+2\s+1\s+0\s+0\s+English\s+250",
587
691
  )
588
692
  self.assertLess(report.index("09:00:00"), report.index("10:00:00"))
589
693
  daily_report = report[report.index("Date") :]
@@ -639,7 +743,7 @@ class ArtificialIntelligenceUsageTests(unittest.TestCase):
639
743
  session_table = output.getvalue()[output.getvalue().index("Sessions") :]
640
744
  self.assertRegex(
641
745
  session_table,
642
- r"11:00:00\s+In progress\s+Unknown\s+Unknown\s+Run the existing tes\s+0\s+0\s+0\s+1\s+Unknown\s+75",
746
+ r"11:00:00\s+In progress\s+Unknown\s+Unknown\s+Run the existing tes\s+Unknown\s+0\s+0\s+0\s+1\s+Unknown\s+75",
643
747
  )
644
748
 
645
749
  def test_main_can_be_called_repeatedly(self):