ai-gains 1.5.0 → 1.5.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-gains",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "Interactive browser dashboard for AI development session tracking",
5
5
  "main": "src/server.js",
6
6
  "bin": {
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
  const fs = require('fs');
3
+ const readline = require('readline');
3
4
 
4
5
  const transcriptPath = process.argv[2];
5
6
  if (!transcriptPath) {
@@ -8,12 +9,10 @@ if (!transcriptPath) {
8
9
  }
9
10
 
10
11
  const CHUNK_SIZE = 4096;
11
- const fd = fs.openSync(transcriptPath, 'r');
12
- const { size } = fs.fstatSync(fd);
13
12
 
14
- // Scan from the start for the first entry with a timestamp
13
+ // Scan from the start for the first entry with a top-level timestamp
15
14
  function findFirstTimestamp() {
16
- const rl = require('readline').createInterface({
15
+ const rl = readline.createInterface({
17
16
  input: fs.createReadStream(transcriptPath),
18
17
  crlfDelay: Infinity
19
18
  });
@@ -23,8 +22,8 @@ function findFirstTimestamp() {
23
22
  try {
24
23
  const entry = JSON.parse(line);
25
24
  if (entry.timestamp) {
26
- rl.close();
27
25
  resolve(entry.timestamp);
26
+ rl.close();
28
27
  }
29
28
  } catch {}
30
29
  });
@@ -32,13 +31,44 @@ function findFirstTimestamp() {
32
31
  });
33
32
  }
34
33
 
35
- // Scan from the end for the last entry with a timestamp
36
- function findLastTimestamp() {
34
+ function getEntryText(entry) {
35
+ const content = entry.message?.content;
36
+ if (!content) return '';
37
+ if (typeof content === 'string') return content;
38
+ if (Array.isArray(content)) {
39
+ return content
40
+ .filter(c => c.type === 'text')
41
+ .map(c => c.text || '')
42
+ .join(' ');
43
+ }
44
+ return '';
45
+ }
46
+
47
+ // A user message that contains the expanded /ai-gains skill content.
48
+ // 'estimated_human_time_minutes' is unique to SKILL.md and won't appear
49
+ // in normal conversation or the UserPromptSubmit hook injection.
50
+ function isAiGainsInvocation(entry) {
51
+ if (entry.type !== 'user' && entry.message?.role !== 'user') return false;
52
+ return getEntryText(entry).includes('estimated_human_time_minutes');
53
+ }
54
+
55
+ function isAssistant(entry) {
56
+ return entry.type === 'assistant' || entry.message?.role === 'assistant';
57
+ }
58
+
59
+ // Scan from the end. Find the last assistant timestamp that precedes the
60
+ // most recent /ai-gains skill invocation. Falls back to the last timestamp
61
+ // overall if no ai-gains invocation is found (e.g. during a dry-run).
62
+ function findEndTimestamp() {
63
+ const fd = fs.openSync(transcriptPath, 'r');
64
+ const { size } = fs.fstatSync(fd);
37
65
  let offset = size;
38
66
  let remainder = '';
39
- let lastTimestamp = null;
67
+ let phase = 'looking_for_aigains'; // then 'looking_for_assistant'
68
+ let lastTimestamp = null; // fallback
69
+ let result = null;
40
70
 
41
- while (offset > 0) {
71
+ outer: while (offset > 0) {
42
72
  const readSize = Math.min(CHUNK_SIZE, offset);
43
73
  offset -= readSize;
44
74
  const buf = Buffer.alloc(readSize);
@@ -52,24 +82,34 @@ function findLastTimestamp() {
52
82
  if (!line) continue;
53
83
  try {
54
84
  const entry = JSON.parse(line);
55
- if (entry.timestamp) {
85
+
86
+ // Track the absolute last timestamp as a fallback
87
+ if (lastTimestamp === null && entry.timestamp) {
56
88
  lastTimestamp = entry.timestamp;
57
- break;
89
+ }
90
+
91
+ if (phase === 'looking_for_aigains') {
92
+ if (isAiGainsInvocation(entry)) {
93
+ phase = 'looking_for_assistant';
94
+ }
95
+ } else {
96
+ // phase === 'looking_for_assistant'
97
+ if (isAssistant(entry) && entry.timestamp) {
98
+ result = entry.timestamp;
99
+ break outer;
100
+ }
58
101
  }
59
102
  } catch {}
60
103
  }
61
- if (lastTimestamp) break;
62
104
  }
63
105
 
64
106
  fs.closeSync(fd);
65
- return lastTimestamp;
107
+ return result || lastTimestamp;
66
108
  }
67
109
 
68
110
  (async () => {
69
- const [start_time, end_time] = await Promise.all([
70
- findFirstTimestamp(),
71
- Promise.resolve(findLastTimestamp())
72
- ]);
111
+ const start_time = await findFirstTimestamp();
112
+ const end_time = findEndTimestamp();
73
113
 
74
114
  if (!start_time || !end_time) {
75
115
  console.error('Could not extract timestamps from transcript');