@parcha/agentrun-import 0.0.0-stage → 0.1.0-beta.56
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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +100 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +63 -0
- package/dist/home.d.ts +91 -0
- package/dist/home.js +296 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +10 -0
- package/dist/lines.d.ts +74 -0
- package/dist/lines.js +149 -0
- package/dist/price.d.ts +49 -0
- package/dist/price.js +40 -0
- package/dist/read.d.ts +37 -0
- package/dist/read.js +54 -0
- package/dist/reader.d.ts +69 -0
- package/dist/reader.js +14 -0
- package/dist/readers/claude-code.d.ts +10 -0
- package/dist/readers/claude-code.js +239 -0
- package/dist/readers/codex.d.ts +1 -0
- package/dist/readers/codex.js +242 -0
- package/dist/readers/index.d.ts +12 -0
- package/dist/readers/index.js +13 -0
- package/dist/readers/pi.d.ts +1 -0
- package/dist/readers/pi.js +99 -0
- package/dist/record-index.d.ts +43 -0
- package/dist/record-index.js +192 -0
- package/dist/record-key.d.ts +11 -0
- package/dist/record-key.js +98 -0
- package/dist/text.d.ts +15 -0
- package/dist/text.js +60 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.js +4 -0
- package/dist/usage.d.ts +16 -0
- package/dist/usage.js +46 -0
- package/dist/wrapped.d.ts +49 -0
- package/dist/wrapped.js +104 -0
- package/package.json +58 -4
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// The keys of the records an import has read (a reader's "u:<uuid>", "m:<message id>:<request id>", "l:<message id>"),
|
|
2
|
+
// packed into bytes for the index that holds one per record across a source (RecordIndex): a large home has tens of
|
|
3
|
+
// millions. One-to-one: two keys pack the same only when they are the same, so no record is taken for a copy that is not
|
|
4
|
+
// one. Packed keys are the index's alone; nothing reads them back. The first byte says how the rest is packed:
|
|
5
|
+
// 1 "u:" and a uuid as Claude Code writes it (lowercase hex, 8-4-4-4-12): its 16 bytes
|
|
6
|
+
// 2-5 any other key made only of [0-9A-Za-z_:]: six bits a char; the byte is 2 + its length mod 4
|
|
7
|
+
// 0 anything else: its UTF-16 code units, two bytes each, low byte first
|
|
8
|
+
export const UUID = 1;
|
|
9
|
+
const SIXES = 2;
|
|
10
|
+
const AS_IS = 0;
|
|
11
|
+
// Each char code's value: a lowercase hex digit's (else -1), and its place among the 64 letters (else -1).
|
|
12
|
+
const HEX = new Int8Array(128).fill(-1);
|
|
13
|
+
for (let i = 0; i < 16; i += 1)
|
|
14
|
+
HEX["0123456789abcdef".charCodeAt(i)] = i;
|
|
15
|
+
const LETTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_:";
|
|
16
|
+
const SIX = new Int8Array(128).fill(-1);
|
|
17
|
+
for (let i = 0; i < 64; i += 1)
|
|
18
|
+
SIX[LETTERS.charCodeAt(i)] = i;
|
|
19
|
+
export const packed = () => ({ bytes: new Uint8Array(64), length: 0 });
|
|
20
|
+
/** "u:<uuid>" into `out` as its 16 bytes; false (out unset) for any other key. */
|
|
21
|
+
function uuidInto(key, out) {
|
|
22
|
+
if (key.length !== 38 || key.charCodeAt(0) !== 117 || key.charCodeAt(1) !== 58)
|
|
23
|
+
return false;
|
|
24
|
+
out[0] = UUID;
|
|
25
|
+
let at = 1;
|
|
26
|
+
let high = -1;
|
|
27
|
+
for (let i = 2; i < 38; i += 1) {
|
|
28
|
+
const code = key.charCodeAt(i);
|
|
29
|
+
// The hyphens at 8, 13, 18 and 23 of the uuid.
|
|
30
|
+
if (i === 10 || i === 15 || i === 20 || i === 25) {
|
|
31
|
+
if (code !== 45)
|
|
32
|
+
return false;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const digit = code < 128 ? HEX[code] : -1;
|
|
36
|
+
if (digit === -1)
|
|
37
|
+
return false;
|
|
38
|
+
if (high === -1)
|
|
39
|
+
high = digit;
|
|
40
|
+
else {
|
|
41
|
+
out[at++] = (high << 4) | digit;
|
|
42
|
+
high = -1;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
/** A key of the 64 letters into `out`, six bits a char: its packed length, -1 for any other key. */
|
|
48
|
+
function sixesInto(key, out) {
|
|
49
|
+
const n = key.length;
|
|
50
|
+
out[0] = SIXES + (n % 4);
|
|
51
|
+
let at = 1;
|
|
52
|
+
let bits = 0;
|
|
53
|
+
let held = 0;
|
|
54
|
+
for (let i = 0; i < n; i += 1) {
|
|
55
|
+
const code = key.charCodeAt(i);
|
|
56
|
+
const six = code < 128 ? SIX[code] : -1;
|
|
57
|
+
if (six === -1)
|
|
58
|
+
return -1;
|
|
59
|
+
held = (held << 6) | six;
|
|
60
|
+
bits += 6;
|
|
61
|
+
if (bits >= 8) {
|
|
62
|
+
bits -= 8;
|
|
63
|
+
out[at++] = (held >> bits) & 0xff;
|
|
64
|
+
held &= (1 << bits) - 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (bits > 0)
|
|
68
|
+
out[at++] = (held << (8 - bits)) & 0xff;
|
|
69
|
+
return at;
|
|
70
|
+
}
|
|
71
|
+
/** `key` packed into `out`, its buffer grown when the key needs more. */
|
|
72
|
+
export function packInto(key, out) {
|
|
73
|
+
if (out.bytes.length < 1 + key.length * 2)
|
|
74
|
+
out.bytes = new Uint8Array(Math.max(1 + key.length * 2, out.bytes.length * 2));
|
|
75
|
+
const bytes = out.bytes;
|
|
76
|
+
if (uuidInto(key, bytes)) {
|
|
77
|
+
out.length = 17;
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
const six = sixesInto(key, bytes);
|
|
81
|
+
if (six !== -1) {
|
|
82
|
+
out.length = six;
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
bytes[0] = AS_IS;
|
|
86
|
+
for (let i = 0; i < key.length; i += 1) {
|
|
87
|
+
const code = key.charCodeAt(i);
|
|
88
|
+
bytes[1 + i * 2] = code & 0xff;
|
|
89
|
+
bytes[2 + i * 2] = code >> 8;
|
|
90
|
+
}
|
|
91
|
+
out.length = 1 + key.length * 2;
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/** `key` packed, as bytes of its own. */
|
|
95
|
+
export const packKey = (key) => {
|
|
96
|
+
const { bytes, length } = packInto(key, packed());
|
|
97
|
+
return bytes.slice(0, length);
|
|
98
|
+
};
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Turn } from "./types.js";
|
|
2
|
+
export declare const isObject: (value: unknown) => value is Record<string, unknown>;
|
|
3
|
+
export declare const str: (value: unknown) => string | undefined;
|
|
4
|
+
export declare const timeOf: (value: unknown) => number | undefined;
|
|
5
|
+
/** Text from a string or a list of blocks; a block that is not text says what it was. */
|
|
6
|
+
export declare const textOf: (value: unknown) => string;
|
|
7
|
+
export declare const effortOf: (value: unknown) => string | undefined;
|
|
8
|
+
export declare const argsOf: (value: unknown) => Record<string, unknown>;
|
|
9
|
+
export declare const INTERRUPTED: RegExp;
|
|
10
|
+
/** The person's own words in a user message's text, as they wrote them (spaces and lines kept, so a cut still matches
|
|
11
|
+
* a secret in them): none when the CLI wrote all of it (a slash command, its output, its preamble), or when there are
|
|
12
|
+
* no words at all. */
|
|
13
|
+
export declare function personsWords(text: string): string | undefined;
|
|
14
|
+
/** The effort most of `turns`' requests ran at, as the model of most requests is a run's model: the first seen of a tie. */
|
|
15
|
+
export declare function mostEffortOf(turns: readonly Turn[]): string | undefined;
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2
|
+
export const str = (value) => (typeof value === "string" && value !== "" ? value : undefined);
|
|
3
|
+
export const timeOf = (value) => {
|
|
4
|
+
const at = typeof value === "number" ? value : typeof value === "string" ? Date.parse(value) : Number.NaN;
|
|
5
|
+
return Number.isFinite(at) ? at : undefined;
|
|
6
|
+
};
|
|
7
|
+
/** Text from a string or a list of blocks; a block that is not text says what it was. */
|
|
8
|
+
export const textOf = (value) => {
|
|
9
|
+
if (typeof value === "string")
|
|
10
|
+
return value;
|
|
11
|
+
if (!Array.isArray(value))
|
|
12
|
+
return isObject(value) && typeof value.text === "string" ? value.text : "";
|
|
13
|
+
return value
|
|
14
|
+
.map((block) => (typeof block === "string" ? block : isObject(block) ? (typeof block.text === "string" ? block.text : block.type === "image" || block.type === "input_image" ? "[image]" : textOf(block.content)) : ""))
|
|
15
|
+
.filter(Boolean)
|
|
16
|
+
.join("\n");
|
|
17
|
+
};
|
|
18
|
+
export const effortOf = (value) => (typeof value === "string" && /^[\w-]{1,32}$/.test(value) ? value : undefined);
|
|
19
|
+
export const argsOf = (value) => {
|
|
20
|
+
if (isObject(value))
|
|
21
|
+
return value;
|
|
22
|
+
if (typeof value === "string") {
|
|
23
|
+
try {
|
|
24
|
+
const parsed = JSON.parse(value);
|
|
25
|
+
if (isObject(parsed))
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// Not JSON: kept as the tool's one input.
|
|
30
|
+
}
|
|
31
|
+
return { input: value };
|
|
32
|
+
}
|
|
33
|
+
return {};
|
|
34
|
+
};
|
|
35
|
+
export const INTERRUPTED = /^\[Request interrupted by user/;
|
|
36
|
+
// What Claude Code writes into a user message for the person's slash command (/clear, /model, /init, /resume …): the
|
|
37
|
+
// command's name, message and arguments, each in its own element, then its output and the preamble it puts before a
|
|
38
|
+
// local command's output. Only these elements, whole: a prompt the person wrote around them is theirs.
|
|
39
|
+
const CLI_ELEMENT = /<(command-name|command-message|command-args|local-command-stdout|local-command-stderr)>[\s\S]*?<\/\1>/g;
|
|
40
|
+
const CLI_PREAMBLE = /^Caveat: The messages below were generated by the user while running local commands\b/;
|
|
41
|
+
/** The person's own words in a user message's text, as they wrote them (spaces and lines kept, so a cut still matches
|
|
42
|
+
* a secret in them): none when the CLI wrote all of it (a slash command, its output, its preamble), or when there are
|
|
43
|
+
* no words at all. */
|
|
44
|
+
export function personsWords(text) {
|
|
45
|
+
const words = text.replace(CLI_ELEMENT, " ").trim();
|
|
46
|
+
const line = words.replace(/\s+/g, " ");
|
|
47
|
+
return line && !CLI_PREAMBLE.test(line) && !INTERRUPTED.test(line) ? words : undefined;
|
|
48
|
+
}
|
|
49
|
+
/** The effort most of `turns`' requests ran at, as the model of most requests is a run's model: the first seen of a tie. */
|
|
50
|
+
export function mostEffortOf(turns) {
|
|
51
|
+
const efforts = new Map();
|
|
52
|
+
for (const turn of turns)
|
|
53
|
+
if (turn.role === "assistant" && turn.effort)
|
|
54
|
+
efforts.set(turn.effort, (efforts.get(turn.effort) ?? 0) + 1);
|
|
55
|
+
let most;
|
|
56
|
+
for (const [effort, n] of efforts)
|
|
57
|
+
if (most === undefined || n > efforts.get(most))
|
|
58
|
+
most = effort;
|
|
59
|
+
return most;
|
|
60
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export type ToolCall = {
|
|
2
|
+
type: "toolCall";
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
arguments: Record<string, unknown>;
|
|
6
|
+
};
|
|
7
|
+
/** A request's tokens, the same for every source (normalizeUsage): `input` is the input neither read from nor written to
|
|
8
|
+
* the cache; `cacheWrite1h`, the part of `cacheWrite` kept for an hour, which Anthropic prices higher; `reasoning`, the
|
|
9
|
+
* part of `output` the model spent reasoning, where the source says (OpenAI's). */
|
|
10
|
+
export type Usage = {
|
|
11
|
+
input: number;
|
|
12
|
+
output: number;
|
|
13
|
+
cacheRead: number;
|
|
14
|
+
cacheWrite: number;
|
|
15
|
+
cacheWrite1h?: number;
|
|
16
|
+
reasoning?: number;
|
|
17
|
+
};
|
|
18
|
+
/** An assistant turn is one model request: `model` the one it ran on ("provider/model"), `effort` the effort or
|
|
19
|
+
* thinking level it ran at, `usage` its tokens, each absent when the source does not say; `key`, the source's own id of
|
|
20
|
+
* the request (Claude Code's message id with its request id, Codex's response id); `error`, why it failed. A note is a
|
|
21
|
+
* line the harness itself wrote in the conversation: no request, nor the person's. */
|
|
22
|
+
export type Turn = ({
|
|
23
|
+
role: "user";
|
|
24
|
+
text: string;
|
|
25
|
+
at: number;
|
|
26
|
+
} | {
|
|
27
|
+
role: "note";
|
|
28
|
+
author: string;
|
|
29
|
+
text: string;
|
|
30
|
+
at: number;
|
|
31
|
+
} | {
|
|
32
|
+
role: "assistant";
|
|
33
|
+
text: string;
|
|
34
|
+
calls: ToolCall[];
|
|
35
|
+
at: number;
|
|
36
|
+
costUsd?: number;
|
|
37
|
+
key?: string;
|
|
38
|
+
model?: string;
|
|
39
|
+
effort?: string;
|
|
40
|
+
usage?: Usage;
|
|
41
|
+
error?: string;
|
|
42
|
+
} | {
|
|
43
|
+
role: "toolResult";
|
|
44
|
+
callId: string;
|
|
45
|
+
name: string;
|
|
46
|
+
text: string;
|
|
47
|
+
isError: boolean;
|
|
48
|
+
at: number;
|
|
49
|
+
}) & {
|
|
50
|
+
/** The source's own ids of the records the turn was read from (Claude Code's uuids): a later import counts them once. */
|
|
51
|
+
uuids?: string[];
|
|
52
|
+
};
|
|
53
|
+
export type Answer = Extract<Turn, {
|
|
54
|
+
role: "assistant";
|
|
55
|
+
}>;
|
|
56
|
+
export type Ended = "completed" | "failed" | "stopped";
|
|
57
|
+
/** The longest gap between two records counted as time at work, as WakaTime counts it: a longer gap is a session left
|
|
58
|
+
* open and counts nothing. Claude Code's Bash tool runs a call for up to 10 minutes with nothing written. */
|
|
59
|
+
export declare const ACTIVE_GAP_MS: number;
|
|
60
|
+
export type SessionOf<S extends string = string> = {
|
|
61
|
+
source: S;
|
|
62
|
+
/** The source's own id for the session. */
|
|
63
|
+
id: string;
|
|
64
|
+
file: string;
|
|
65
|
+
/** Its id in `file`, where the file holds many sessions (its locator's id): absent for a file of one session. */
|
|
66
|
+
within?: string;
|
|
67
|
+
cwd?: string;
|
|
68
|
+
/** "provider/model", from the session's latest model. */
|
|
69
|
+
model?: string;
|
|
70
|
+
startedAt?: string;
|
|
71
|
+
endedAt?: string;
|
|
72
|
+
state: Ended;
|
|
73
|
+
/** The whole session's cost, when the source records it; absent is unknown. */
|
|
74
|
+
costUsd?: number;
|
|
75
|
+
firstPrompt?: string;
|
|
76
|
+
/** The source's own title for the session, as Claude Code shows it: the person's (/rename's custom-title), else the one
|
|
77
|
+
* it wrote itself (ai-title; summary in older files, the one whose last message this session holds), the latest. */
|
|
78
|
+
sourceTitle?: string;
|
|
79
|
+
/** The first of the person's own words (personsWords): the first user text that is not a CLI's command, its output or
|
|
80
|
+
* its preamble. The run's title when the source wrote none. */
|
|
81
|
+
titleText?: string;
|
|
82
|
+
/** A subagent's or another session's child: not imported as a run; its usage is its parent's. */
|
|
83
|
+
child: boolean;
|
|
84
|
+
/** A child's parent session, when its file names it (Codex's parent thread). */
|
|
85
|
+
parent?: string;
|
|
86
|
+
/** Read only when asked for: a dry run reads none. */
|
|
87
|
+
turns: Turn[];
|
|
88
|
+
users: number;
|
|
89
|
+
/** Time at work: the gaps between its records of at most ACTIVE_GAP_MS each. */
|
|
90
|
+
activeMs: number;
|
|
91
|
+
/** Records it holds that were read already (in an earlier file of the import, or earlier in this one): copies. */
|
|
92
|
+
copies: number;
|
|
93
|
+
/** The effort or thinking level most of its requests ran at, as the source names it; with its turns only. */
|
|
94
|
+
effort?: string;
|
|
95
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// The one shape every source's sessions are read into.
|
|
2
|
+
/** The longest gap between two records counted as time at work, as WakaTime counts it: a longer gap is a session left
|
|
3
|
+
* open and counts nothing. Claude Code's Bash tool runs a call for up to 10 minutes with nothing written. */
|
|
4
|
+
export const ACTIVE_GAP_MS = 15 * 60_000;
|
package/dist/usage.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Usage } from "./types.js";
|
|
2
|
+
export type UsageFormat = "anthropic" | "openai" | "pi";
|
|
3
|
+
/** A request's usage as `format` writes it, as Usage; undefined unless input and output are counts (an absent cache
|
|
4
|
+
* count is 0) and the input left is not negative. The 1h part of the cache write, or the reasoning part of the output,
|
|
5
|
+
* is kept only when it is a count of at most the whole. */
|
|
6
|
+
export declare function normalizeUsage(format: UsageFormat, raw: unknown): Usage | undefined;
|
|
7
|
+
/** The buckets a total of requests keeps, each a sum; cacheWrite1h is 0 when no request named one. */
|
|
8
|
+
export type Tokens = {
|
|
9
|
+
input: number;
|
|
10
|
+
output: number;
|
|
11
|
+
cacheRead: number;
|
|
12
|
+
cacheWrite: number;
|
|
13
|
+
cacheWrite1h: number;
|
|
14
|
+
};
|
|
15
|
+
export declare const noTokens: () => Tokens;
|
|
16
|
+
export declare function addTokens(into: Tokens, usage: Usage): Tokens;
|
package/dist/usage.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// One reading of a request's token counts for every provider, so every number this package (and what imports it)
|
|
2
|
+
// publishes agrees: `input` is the input neither read from nor written to the cache.
|
|
3
|
+
// anthropic input_tokens already leaves the cache out; cache_creation.ephemeral_1h_input_tokens is the 1h part.
|
|
4
|
+
// openai input_tokens holds cached_input_tokens (and a cache write where one is counted): subtracted. output_tokens
|
|
5
|
+
// holds reasoning_output_tokens, kept as `reasoning` beside it. Codex writes OpenAI's counts.
|
|
6
|
+
// pi already split as Usage is, cacheWrite1h included.
|
|
7
|
+
import { isObject } from "./text.js";
|
|
8
|
+
/** Each format's names for input, cache read, cache write and output, and whether its input holds its cache counts. */
|
|
9
|
+
const NAMES = {
|
|
10
|
+
anthropic: { input: "input_tokens", cacheRead: "cache_read_input_tokens", cacheWrite: "cache_creation_input_tokens", output: "output_tokens", cachedIn: false },
|
|
11
|
+
openai: { input: "input_tokens", cacheRead: "cached_input_tokens", cacheWrite: "cache_write_input_tokens", output: "output_tokens", cachedIn: true },
|
|
12
|
+
pi: { input: "input", cacheRead: "cacheRead", cacheWrite: "cacheWrite", output: "output", cachedIn: false },
|
|
13
|
+
};
|
|
14
|
+
const count = (value) => (Number.isSafeInteger(value) && value >= 0 ? value : undefined);
|
|
15
|
+
/** A request's usage as `format` writes it, as Usage; undefined unless input and output are counts (an absent cache
|
|
16
|
+
* count is 0) and the input left is not negative. The 1h part of the cache write, or the reasoning part of the output,
|
|
17
|
+
* is kept only when it is a count of at most the whole. */
|
|
18
|
+
export function normalizeUsage(format, raw) {
|
|
19
|
+
if (!isObject(raw))
|
|
20
|
+
return undefined;
|
|
21
|
+
const names = NAMES[format];
|
|
22
|
+
const usage = { input: count(raw[names.input]), output: count(raw[names.output]), cacheRead: count(raw[names.cacheRead] ?? 0), cacheWrite: count(raw[names.cacheWrite] ?? 0) };
|
|
23
|
+
if (usage.input === undefined || usage.output === undefined || usage.cacheRead === undefined || usage.cacheWrite === undefined)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (names.cachedIn)
|
|
26
|
+
usage.input -= usage.cacheRead + usage.cacheWrite;
|
|
27
|
+
if (usage.input < 0)
|
|
28
|
+
return undefined;
|
|
29
|
+
const out = usage;
|
|
30
|
+
const long = count(format === "anthropic" ? (isObject(raw.cache_creation) ? raw.cache_creation.ephemeral_1h_input_tokens : undefined) : format === "pi" ? raw.cacheWrite1h : undefined);
|
|
31
|
+
if (long !== undefined && long <= out.cacheWrite)
|
|
32
|
+
out.cacheWrite1h = long;
|
|
33
|
+
const reasoning = format === "openai" ? count(raw.reasoning_output_tokens) : undefined;
|
|
34
|
+
if (reasoning !== undefined && reasoning <= out.output)
|
|
35
|
+
out.reasoning = reasoning;
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
export const noTokens = () => ({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cacheWrite1h: 0 });
|
|
39
|
+
export function addTokens(into, usage) {
|
|
40
|
+
into.input += usage.input;
|
|
41
|
+
into.output += usage.output;
|
|
42
|
+
into.cacheRead += usage.cacheRead;
|
|
43
|
+
into.cacheWrite += usage.cacheWrite;
|
|
44
|
+
into.cacheWrite1h += usage.cacheWrite1h ?? 0;
|
|
45
|
+
return into;
|
|
46
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ParseOptions } from "./home.js";
|
|
2
|
+
import { type Usage } from "./types.js";
|
|
3
|
+
import { type Tokens } from "./usage.js";
|
|
4
|
+
export type Wrapped = {
|
|
5
|
+
year: number;
|
|
6
|
+
/** Sessions whose first request is in the year, their prompts (`turns`, the person's own) with them. */
|
|
7
|
+
sessions: number;
|
|
8
|
+
turns: number;
|
|
9
|
+
/** Requests in the year, each once, subagents' with them. */
|
|
10
|
+
requests: number;
|
|
11
|
+
tokens: Tokens;
|
|
12
|
+
/** List price (the catalog's) of the requests it could price, USD; `unpricedRequests` counts those it could not (an
|
|
13
|
+
* unlisted model, no usage), so a sum that left some out says so. */
|
|
14
|
+
cost: number;
|
|
15
|
+
unpricedRequests: number;
|
|
16
|
+
/** Local days with a request, ascending: that day's requests and tokens (input, output and cache together). */
|
|
17
|
+
days: {
|
|
18
|
+
day: string;
|
|
19
|
+
requests: number;
|
|
20
|
+
tokens: number;
|
|
21
|
+
}[];
|
|
22
|
+
/** Requests by local hour of the day (24) and by weekday (7, Sunday first). */
|
|
23
|
+
hours: number[];
|
|
24
|
+
weekdays: number[];
|
|
25
|
+
/** Time at work: the gaps between a session's requests of the year, each counted when it is under ACTIVE_GAP_MS. */
|
|
26
|
+
activeMs: number;
|
|
27
|
+
/** By reader's harness, the way serve runs it, and by model ("provider/model", "unknown" for a request naming none). */
|
|
28
|
+
harnesses: {
|
|
29
|
+
harness: string;
|
|
30
|
+
sessions: number;
|
|
31
|
+
requests: number;
|
|
32
|
+
tokens: number;
|
|
33
|
+
}[];
|
|
34
|
+
models: {
|
|
35
|
+
model: string;
|
|
36
|
+
requests: number;
|
|
37
|
+
tokens: Tokens;
|
|
38
|
+
cost: number;
|
|
39
|
+
}[];
|
|
40
|
+
sources: {
|
|
41
|
+
source: string;
|
|
42
|
+
sessions: number;
|
|
43
|
+
requests: number;
|
|
44
|
+
}[];
|
|
45
|
+
};
|
|
46
|
+
/** `year` under `home`, from parseHome's reads: its options (`state` and `requests` aside). */
|
|
47
|
+
export declare function wrappedHome(home: string, year: number, options?: Omit<ParseOptions, "state" | "requests"> & {
|
|
48
|
+
price?: (model: string | undefined, usage: Usage) => Promise<number | undefined> | number | undefined;
|
|
49
|
+
}): Promise<Wrapped>;
|
package/dist/wrapped.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// A year of work under a home, as numbers: what the year used, by which harnesses and models, and when. Every figure is
|
|
2
|
+
// bucketed by its REQUEST's time in the machine's local zone (a session that spans New Year splits); a session counts in
|
|
3
|
+
// `sessions` for the year its first request falls in. Text-free as parseHome's totals are: counts, names and times only,
|
|
4
|
+
// never a prompt, a title, a path or a project.
|
|
5
|
+
import { builtinCatalog, priceRequests } from "./price.js";
|
|
6
|
+
import { parseHome } from "./home.js";
|
|
7
|
+
import { READERS } from "./readers/index.js";
|
|
8
|
+
import { ACTIVE_GAP_MS } from "./types.js";
|
|
9
|
+
import { addTokens, noTokens } from "./usage.js";
|
|
10
|
+
const all = (t) => t.input + t.output + t.cacheRead + t.cacheWrite;
|
|
11
|
+
const day = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
12
|
+
const by = (map, key, make) => map.get(key) ?? map.set(key, make()).get(key);
|
|
13
|
+
/** `year` under `home`, from parseHome's reads: its options (`state` and `requests` aside). */
|
|
14
|
+
export async function wrappedHome(home, year, options = {}) {
|
|
15
|
+
const prices = options.catalog === false ? undefined : (options.catalog ?? (await builtinCatalog()));
|
|
16
|
+
const out = { year, sessions: 0, turns: 0, requests: 0, tokens: noTokens(), cost: 0, unpricedRequests: 0, days: [], hours: new Array(24).fill(0), weekdays: new Array(7).fill(0), activeMs: 0, harnesses: [], models: [], sources: [] };
|
|
17
|
+
const days = new Map();
|
|
18
|
+
const harnesses = new Map();
|
|
19
|
+
const models = new Map();
|
|
20
|
+
const sources = new Map();
|
|
21
|
+
// The usage of each model's requests of the year, priced once the read is over (a rejection of the price then rejects
|
|
22
|
+
// this call, with no promise left floating), and each request that carries none, which no price can cover.
|
|
23
|
+
const usages = new Map();
|
|
24
|
+
const harnessOf = (source) => (options.readers ?? READERS)[source]?.harness ?? source;
|
|
25
|
+
await parseHome(home, {
|
|
26
|
+
...options,
|
|
27
|
+
requests: (totals, requests, started) => {
|
|
28
|
+
const mine = requests.filter((r) => Number.isFinite(r.at) && new Date(r.at).getFullYear() === year).sort((a, b) => a.at - b.at);
|
|
29
|
+
const counted = started !== undefined && new Date(started).getFullYear() === year;
|
|
30
|
+
if (counted) {
|
|
31
|
+
out.sessions += 1;
|
|
32
|
+
out.turns += totals.turns;
|
|
33
|
+
by(sources, totals.source, () => ({ sessions: 0, requests: 0 })).sessions += 1;
|
|
34
|
+
by(harnesses, harnessOf(totals.source), () => ({ sessions: 0, requests: 0, tokens: 0 })).sessions += 1;
|
|
35
|
+
}
|
|
36
|
+
for (let i = 0; i < mine.length; i++) {
|
|
37
|
+
const r = mine[i];
|
|
38
|
+
const when = new Date(r.at);
|
|
39
|
+
const held = all(addTokens(noTokens(), r.usage ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }));
|
|
40
|
+
out.requests += 1;
|
|
41
|
+
if (r.usage)
|
|
42
|
+
addTokens(out.tokens, r.usage);
|
|
43
|
+
out.hours[when.getHours()] += 1;
|
|
44
|
+
out.weekdays[when.getDay()] += 1;
|
|
45
|
+
const d = by(days, day(when), () => ({ requests: 0, tokens: 0 }));
|
|
46
|
+
d.requests += 1;
|
|
47
|
+
d.tokens += held;
|
|
48
|
+
by(sources, totals.source, () => ({ sessions: 0, requests: 0 })).requests += 1;
|
|
49
|
+
const h = by(harnesses, harnessOf(totals.source), () => ({ sessions: 0, requests: 0, tokens: 0 }));
|
|
50
|
+
h.requests += 1;
|
|
51
|
+
h.tokens += held;
|
|
52
|
+
const m = by(models, r.model ?? "unknown", () => ({ requests: 0, tokens: noTokens(), cost: 0 }));
|
|
53
|
+
m.requests += 1;
|
|
54
|
+
if (r.usage)
|
|
55
|
+
addTokens(m.tokens, r.usage);
|
|
56
|
+
if (i > 0 && r.at - mine[i - 1].at < ACTIVE_GAP_MS)
|
|
57
|
+
out.activeMs += r.at - mine[i - 1].at;
|
|
58
|
+
// A request with no usage (pi leaves it off an aborted request) is unpriced; known zero usage is free and priced.
|
|
59
|
+
if (r.usage)
|
|
60
|
+
by(usages, r.model ?? "unknown", () => []).push(r.usage);
|
|
61
|
+
else
|
|
62
|
+
out.unpricedRequests += 1;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const round = (n) => Math.round(n * 1e6) / 1e6;
|
|
67
|
+
// One price per model: the caller's per request, or the catalog's over all of the model's requests at once (one rounding
|
|
68
|
+
// of the model's sum, so many requests of a fraction of a cent each still add up). A model it cannot price leaves only
|
|
69
|
+
// its own requests out, counted in `unpricedRequests`.
|
|
70
|
+
for (const [model, list] of usages) {
|
|
71
|
+
const known = model === "unknown" ? undefined : model;
|
|
72
|
+
let usd;
|
|
73
|
+
// A request of known zero tokens is free at any price: it is neither priced nor counted as unpriced.
|
|
74
|
+
const paid = list.filter((usage) => usage.input + usage.output + usage.cacheRead + usage.cacheWrite > 0);
|
|
75
|
+
if (paid.length === 0)
|
|
76
|
+
continue;
|
|
77
|
+
if (options.price) {
|
|
78
|
+
// The caller's price answers per request: one it cannot price is left out alone.
|
|
79
|
+
for (const usage of paid) {
|
|
80
|
+
const one = await options.price(known, usage);
|
|
81
|
+
if (one === undefined)
|
|
82
|
+
out.unpricedRequests += 1;
|
|
83
|
+
else
|
|
84
|
+
usd = (usd ?? 0) + one;
|
|
85
|
+
}
|
|
86
|
+
if (usd === undefined)
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
else if (prices)
|
|
90
|
+
usd = await priceRequests(paid.map((usage) => ({ ...(known ? { model: known } : {}), usage })), prices);
|
|
91
|
+
if (usd === undefined)
|
|
92
|
+
out.unpricedRequests += paid.length;
|
|
93
|
+
else {
|
|
94
|
+
out.cost += usd;
|
|
95
|
+
models.get(model).cost = usd;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
out.cost = round(out.cost);
|
|
99
|
+
out.days = [...days].sort(([a], [b]) => (a < b ? -1 : 1)).map(([d, v]) => ({ day: d, ...v }));
|
|
100
|
+
out.harnesses = [...harnesses].map(([harness, v]) => ({ harness, ...v })).sort((a, b) => b.tokens - a.tokens || (a.harness < b.harness ? -1 : 1));
|
|
101
|
+
out.models = [...models].map(([model, v]) => ({ model, ...v, cost: round(v.cost) })).sort((a, b) => all(b.tokens) - all(a.tokens) || (a.model < b.model ? -1 : 1));
|
|
102
|
+
out.sources = [...sources].map(([source, v]) => ({ source, ...v })).sort((a, b) => b.requests - a.requests || (a.source < b.source ? -1 : 1));
|
|
103
|
+
return out;
|
|
104
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-import",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.56",
|
|
4
|
+
"description": "Reads the coding-agent sessions under a home (Claude Code, Codex, pi) into one Session shape, with normalized token usage, subagents under their parent, byte-offset cursors for re-reads, and per-session totals.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agentrun",
|
|
7
|
+
"claude-code",
|
|
8
|
+
"codex",
|
|
9
|
+
"pi",
|
|
10
|
+
"sessions",
|
|
11
|
+
"usage"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"author": "Parcha Labs, Inc.",
|
|
16
|
+
"bin": {
|
|
17
|
+
"agentrun-import": "./dist/cli.js"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=24.22.3"
|
|
21
|
+
},
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"NOTICE"
|
|
36
|
+
],
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
40
|
+
"prepack": "npm run build",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
|
+
"test": "node --test --test-concurrency=8 'test/*.test.mjs'"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@earendil-works/pi-ai": "1.5.2"
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/Parcha-ai/agentrun.git",
|
|
50
|
+
"directory": "packages/import"
|
|
51
|
+
},
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/Parcha-ai/agentrun/issues"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://agentrun.ai",
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public",
|
|
58
|
+
"tag": "beta"
|
|
59
|
+
}
|
|
60
|
+
}
|