@shadevkumar/agent-wrapped 0.0.0-stage → 0.2.0

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,196 @@
1
+ import { existsSync, readdirSync } from "node:fs";
2
+ import { extname, join } from "node:path";
3
+ import { DatabaseSync } from "node:sqlite";
4
+ import { coverage, lastJsonLine, namesFromGitUrl, namesFromPath, toDate } from "../files.js";
5
+ import { commandKind, emptyHarvest } from "../types.js";
6
+ // Codex keeps, in ~/.codex:
7
+ // - state_N.sqlite `threads`: one row per thread, with its token total
8
+ // - thread_history_N.sqlite: every message and turn, kept long term
9
+ // - sessions/**/rollout-*.jsonl: token splits, only for recent threads
10
+ // These are Codex's internal files, so a query that no longer fits the
11
+ // schema is recorded as missing coverage instead of stopping the run.
12
+ export function harvestCodex(home) {
13
+ const root = join(home, ".codex");
14
+ const h = emptyHarvest();
15
+ const human = new Map(); // thread id -> started by a person
16
+ const state = openLatest(root, "state");
17
+ if (state) {
18
+ try {
19
+ const rows = state
20
+ .prepare("select id, rollout_path, created_at, cwd, tokens_used, model, git_origin_url, source from threads")
21
+ .all();
22
+ const threadDates = [];
23
+ for (const t of rows) {
24
+ human.set(t.id, !String(t.source ?? "").startsWith("{"));
25
+ if (t.cwd)
26
+ for (const n of namesFromPath(t.cwd))
27
+ h.privateNames.add(n);
28
+ if (t.git_origin_url)
29
+ for (const n of namesFromGitUrl(t.git_origin_url))
30
+ h.privateNames.add(n);
31
+ const at = toDate(Number(t.created_at) * 1000);
32
+ if (!at)
33
+ continue;
34
+ threadDates.push(at);
35
+ const model = String(t.model || "unknown");
36
+ const split = t.rollout_path && existsSync(t.rollout_path) ? rolloutUsage(t.rollout_path) : null;
37
+ if (split) {
38
+ h.usage.push({ agent: "codex", model, at, ...split });
39
+ }
40
+ else if (Number(t.tokens_used) > 0) {
41
+ h.totals.push({ agent: "codex", model, at, tokens: Number(t.tokens_used) });
42
+ }
43
+ }
44
+ h.coverage.push(coverage("codex", "threads", threadDates));
45
+ }
46
+ catch {
47
+ h.coverage.push(coverage("codex", "threads", []));
48
+ }
49
+ finally {
50
+ state.close();
51
+ }
52
+ }
53
+ const history = openLatest(root, "thread_history");
54
+ if (history) {
55
+ try {
56
+ const items = history
57
+ .prepare("select thread_id, created_at_ms, item_json from thread_items where item_type = 'userMessage'")
58
+ .all();
59
+ for (const item of items) {
60
+ if (human.get(item.thread_id) === false)
61
+ continue; // written by a parent agent
62
+ const at = toDate(Number(item.created_at_ms));
63
+ const text = cleanCodexPrompt(messageText(item.item_json));
64
+ if (at && text)
65
+ h.prompts.push({ agent: "codex", text, at, session: item.thread_id });
66
+ }
67
+ h.coverage.push(coverage("codex", "prompts", h.prompts.map((p) => p.at)));
68
+ }
69
+ catch {
70
+ h.coverage.push(coverage("codex", "prompts", []));
71
+ }
72
+ try {
73
+ const turns = history
74
+ .prepare("select started_at, duration_ms from thread_turns where duration_ms > 0")
75
+ .all();
76
+ for (const t of turns) {
77
+ const at = toDate(Number(t.started_at) * 1000);
78
+ if (at)
79
+ h.turns.push({ agent: "codex", at, durationMs: Number(t.duration_ms) });
80
+ }
81
+ }
82
+ catch {
83
+ // Without turn timings the story skips agent hours; prompts still count.
84
+ }
85
+ try {
86
+ for (const t of history.prepare("select started_at from thread_turns where status = 'interrupted'").all()) {
87
+ const at = toDate(Number(t.started_at) * 1000);
88
+ if (at)
89
+ h.actions.push({ agent: "codex", at, kind: "stop" });
90
+ }
91
+ }
92
+ catch {
93
+ // Older histories don't record how a turn ended.
94
+ }
95
+ try {
96
+ // Each item also holds the command's output or the file's diff, often
97
+ // far larger than the rest, so only its start is read: the command or
98
+ // the first file path is near the top.
99
+ const items = history
100
+ .prepare(`select item_type, created_at_ms, substr(item_json, 1, 1200) as head from thread_items
101
+ where item_type in ('commandExecution', 'fileChange')`)
102
+ .all();
103
+ for (const item of items) {
104
+ const at = toDate(Number(item.created_at_ms));
105
+ if (!at)
106
+ continue;
107
+ if (item.item_type === "fileChange") {
108
+ const path = firstString(item.head, "path");
109
+ if (path)
110
+ h.actions.push({ agent: "codex", at, kind: "edit", ext: extname(path).toLowerCase() });
111
+ }
112
+ else {
113
+ const kind = commandKind(firstString(item.head, "command") ?? "");
114
+ if (kind)
115
+ h.actions.push({ agent: "codex", at, kind });
116
+ }
117
+ }
118
+ }
119
+ catch {
120
+ // Without these the story skips what the agents did; the rest still works.
121
+ }
122
+ finally {
123
+ history.close();
124
+ }
125
+ }
126
+ return h;
127
+ }
128
+ // The first "key":"value" string in a piece of JSON that may be cut off.
129
+ function firstString(json, key) {
130
+ const m = json.match(new RegExp(`"${key}":("(?:[^"\\\\]|\\\\.)*")`));
131
+ if (!m)
132
+ return null;
133
+ try {
134
+ return JSON.parse(m[1]);
135
+ }
136
+ catch {
137
+ return null;
138
+ }
139
+ }
140
+ // The Codex app wraps a typed request with IDE context and tagged blocks.
141
+ // Returns only what the person typed, or null for text Codex wrote itself.
142
+ export function cleanCodexPrompt(raw) {
143
+ if (raw.includes('source="ambient-'))
144
+ return null;
145
+ const markers = [...raw.matchAll(/^## My request(?: for Codex)?:[ \t]*/gm)];
146
+ const last = markers.at(-1);
147
+ if (!last && raw.trimStart().startsWith("# "))
148
+ return null;
149
+ const typed = last ? raw.slice(last.index + last[0].length) : raw;
150
+ const text = typed.replace(/<([A-Za-z_][\w-]*)\b[^>]*>[\s\S]*?<\/\1>/g, "").trim();
151
+ return text || null;
152
+ }
153
+ function messageText(json) {
154
+ try {
155
+ const content = JSON.parse(json)?.content;
156
+ if (!Array.isArray(content))
157
+ return "";
158
+ return content.map((c) => (typeof c?.text === "string" ? c.text : "")).join("\n");
159
+ }
160
+ catch {
161
+ return "";
162
+ }
163
+ }
164
+ // The thread's final running total. `input_tokens` includes cached tokens.
165
+ function rolloutUsage(path) {
166
+ const row = lastJsonLine(path, '"token_count"', (r) => r.type === "event_msg" && r.payload?.type === "token_count" && !!r.payload.info?.total_token_usage);
167
+ const total = row?.payload.info.total_token_usage;
168
+ if (!total)
169
+ return null;
170
+ const cached = total.cached_input_tokens ?? 0;
171
+ return {
172
+ input: (total.input_tokens ?? 0) - cached,
173
+ cacheRead: cached,
174
+ cacheWrite: total.cache_write_input_tokens ?? 0,
175
+ cacheWrite1h: 0,
176
+ output: total.output_tokens ?? 0,
177
+ };
178
+ }
179
+ // Codex numbers its database files (state_5.sqlite); open the newest, read-only.
180
+ function openLatest(root, prefix) {
181
+ if (!existsSync(root))
182
+ return null;
183
+ const pattern = new RegExp(`^${prefix}_(\\d+)\\.sqlite$`);
184
+ const newest = readdirSync(root)
185
+ .map((f) => ({ f, n: Number(f.match(pattern)?.[1] ?? -1) }))
186
+ .filter((x) => x.n >= 0)
187
+ .sort((a, b) => b.n - a.n)[0];
188
+ if (!newest)
189
+ return null;
190
+ try {
191
+ return new DatabaseSync(join(root, newest.f), { readOnly: true });
192
+ }
193
+ catch {
194
+ return null;
195
+ }
196
+ }
@@ -0,0 +1,93 @@
1
+ Copyright 2024 The Geist Project Authors (https://github.com/vercel/geist-font) Geist-Italic[wght].ttf: Copyright 2024 The Geist Project Authors (https://github.com/vercel/geist-font)
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ http://scripts.sil.org/OFL
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -0,0 +1,93 @@
1
+ Copyright 2020 The Newsreader Project Authors (http://github.com/productiontype/Newsreader)
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ http://scripts.sil.org/OFL
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.