@parcha/agentrun-import 0.1.0-beta.58 → 0.1.0-beta.60

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 (46) hide show
  1. package/README.md +111 -20
  2. package/dist/cli.js +23 -8
  3. package/dist/home.d.ts +24 -1
  4. package/dist/home.js +394 -146
  5. package/dist/index.d.ts +7 -5
  6. package/dist/index.js +4 -2
  7. package/dist/internal.d.ts +18 -0
  8. package/dist/internal.js +12 -0
  9. package/dist/lines.d.ts +24 -1
  10. package/dist/lines.js +135 -25
  11. package/dist/mirror.d.ts +13 -0
  12. package/dist/mirror.js +15 -0
  13. package/dist/pool-worker.d.ts +6 -0
  14. package/dist/pool-worker.js +50 -0
  15. package/dist/pool.d.ts +106 -0
  16. package/dist/pool.js +288 -0
  17. package/dist/price.d.ts +13 -5
  18. package/dist/price.js +31 -21
  19. package/dist/read.d.ts +14 -4
  20. package/dist/read.js +41 -7
  21. package/dist/reader.d.ts +38 -8
  22. package/dist/readers/claude-code.d.ts +24 -0
  23. package/dist/readers/claude-code.js +51 -35
  24. package/dist/readers/codex.d.ts +60 -1
  25. package/dist/readers/codex.js +494 -52
  26. package/dist/readers/copilot.d.ts +4 -0
  27. package/dist/readers/copilot.js +224 -0
  28. package/dist/readers/index.d.ts +2 -0
  29. package/dist/readers/index.js +4 -0
  30. package/dist/readers/line-readers.d.ts +7 -0
  31. package/dist/readers/line-readers.js +10 -0
  32. package/dist/readers/opencode.d.ts +42 -0
  33. package/dist/readers/opencode.js +920 -0
  34. package/dist/readers/pi.js +2 -2
  35. package/dist/record-index.d.ts +32 -1
  36. package/dist/record-index.js +133 -13
  37. package/dist/scan-wasm.d.ts +1 -0
  38. package/dist/scan-wasm.js +2 -0
  39. package/dist/scan.d.ts +21 -0
  40. package/dist/scan.js +164 -0
  41. package/dist/text.d.ts +3 -0
  42. package/dist/text.js +36 -2
  43. package/dist/types.d.ts +20 -3
  44. package/dist/usage.d.ts +6 -2
  45. package/dist/usage.js +42 -1
  46. package/package.json +4 -2
@@ -4,9 +4,9 @@ import { basename, dirname, join } from "node:path";
4
4
  import { addRequest, setUsage } from "../counts.js";
5
5
  import { lineReader } from "../lines.js";
6
6
  import { entries } from "../reader.js";
7
- import { addRecord, ownerOf, recordIndex } from "../record-index.js";
7
+ import { addNew, ownerOf, recordIndex } from "../record-index.js";
8
8
  import { packed, packInto } from "../record-key.js";
9
- import { argsOf, effortOf, INTERRUPTED, isObject, str, textOf } from "../text.js";
9
+ import { argsOf, effortOf, FAILED, INTERRUPTED, isObject, str, textOf } from "../text.js";
10
10
  import { normalizeUsage } from "../usage.js";
11
11
  // The titles Claude Code writes into a session's file, each again as it changes, and the field each holds it in.
12
12
  const TITLES = new Map([
@@ -106,32 +106,13 @@ function record(value, at, { state, seen, keep, add }) {
106
106
  // stream's start): the last line's usage is the request's.
107
107
  const key = keyOf(message, value);
108
108
  const usage = normalizeUsage("anthropic", message.usage);
109
- if (counts) {
110
- // A totals read: the request's model, usage and time alone, its later lines' usage over it, as below.
111
- // Started small: a session's file often holds a request or two, and its cursor keeps the index.
112
- const indexes = (own.indexes ??= recordIndex(2));
113
- const packed = key ? packInto(key, PACKED) : undefined;
114
- const index = packed ? ownerOf(indexes, packed) : -1;
115
- if (index !== -1) {
116
- if (usage)
117
- setUsage(counts, index, usage);
118
- return;
119
- }
120
- if (key && (seen.has(`m:${key}`) || (str(message.id) && seen.has(`l:${message.id}`)))) {
121
- for (const call of calls)
122
- own.copied.add(call.id);
123
- return false;
124
- }
125
- if (!key && !text && calls.length === 0)
126
- return;
127
- const added = addRequest(counts, session.model, usage, at);
128
- if (packed) {
129
- addRecord(indexes, packed, added);
130
- seen.add(`m:${key}`);
131
- }
132
- return;
133
- }
134
- const held = key ? own.keyed.get(key) : undefined;
109
+ // A request this file holds already, by its key: as its Answer, or a totals read's as its index in its counts (its
110
+ // model, usage and time alone), by its key packed. Started small: a session's file often holds a request or two, and
111
+ // its cursor keeps the index.
112
+ const indexes = counts ? (own.indexes ??= recordIndex(2)) : undefined;
113
+ const packed = indexes && key ? packInto(key, PACKED) : undefined;
114
+ const index = packed ? ownerOf(indexes, packed) : -1;
115
+ const held = key && !counts ? own.keyed.get(key) : undefined;
135
116
  if (held) {
136
117
  if (keep)
137
118
  held.text = [held.text, text].filter(Boolean).join("\n");
@@ -142,6 +123,11 @@ function record(value, at, { state, seen, keep, add }) {
142
123
  held.usage = usage;
143
124
  return;
144
125
  }
126
+ if (index !== -1) {
127
+ if (usage)
128
+ setUsage(counts, index, usage);
129
+ return;
130
+ }
145
131
  // A request an earlier file of the import holds: counted there, never again; its calls' results are copies too. A
146
132
  // store imported before requests were keyed by message and request id holds the bare message id ("l:", serve's).
147
133
  if (key && (seen.has(`m:${key}`) || (str(message.id) && seen.has(`l:${message.id}`)))) {
@@ -151,14 +137,22 @@ function record(value, at, { state, seen, keep, add }) {
151
137
  }
152
138
  if (!key && !text && calls.length === 0)
153
139
  return;
154
- const effort = effortOf(value.perTurnEffort) ?? effortOf(value.effort);
155
- // Without turns, no answer text: the request is kept for its counts, in the result and the cursor alike.
156
- const turn = { role: "assistant", text: keep ? text : "", calls, at, ...(key ? { key } : {}), ...(session.model ? { model: session.model } : {}), ...(effort ? { effort } : {}), ...(usage ? { usage } : {}), ...(failed ? { error: text || "API error" } : {}), ...mark };
157
- if (key) {
158
- own.keyed.set(key, turn);
159
- seen.add(`m:${key}`);
140
+ if (counts) {
141
+ const added = addRequest(counts, session.model, usage, at);
142
+ // ownerOf found none above, and nothing has been added to this file's index since.
143
+ if (packed)
144
+ addNew(indexes, packed, added);
145
+ }
146
+ else {
147
+ const effort = effortOf(value.perTurnEffort) ?? effortOf(value.effort);
148
+ // Without turns, no answer text: the request is kept for its counts, in the result and the cursor alike.
149
+ const turn = { role: "assistant", text: keep ? text : "", calls, at, ...(key ? { key } : {}), ...(session.model ? { model: session.model } : {}), ...(effort ? { effort } : {}), ...(usage ? { usage } : {}), ...(failed ? { error: keep ? text || "API error" : FAILED } : {}), ...mark };
150
+ if (key)
151
+ own.keyed.set(key, turn);
152
+ add(turn);
160
153
  }
161
- add(turn);
154
+ if (key)
155
+ seen.add(`m:${key}`);
162
156
  return;
163
157
  }
164
158
  if (typeof message.content === "string") {
@@ -236,6 +230,25 @@ export async function childFiles(home, env) {
236
230
  }
237
231
  /** Claude Code's own directory: CLAUDE_CONFIG_DIR where the environment names it, else ~/.claude. */
238
232
  const configDir = (home, env) => env?.CLAUDE_CONFIG_DIR || join(home, ".claude");
233
+ /** The counts normalizeUsage takes from Anthropic's usage (usage.ts). */
234
+ const USAGE = { input_tokens: true, output_tokens: true, cache_read_input_tokens: true, cache_creation_input_tokens: true, cache_creation: { ephemeral_1h_input_tokens: true } };
235
+ /** What record reads of a line in a totals read (LineSource.fields). A tool_result's content is left out: it is only the
236
+ * result's turn's text, which a totals read keeps none of; so are a tool_use's input (its call's arguments, kept only
237
+ * with turns), a thinking block's text, and every field record reads only with turns (titles, efforts). */
238
+ const FIELDS = {
239
+ cwd: true,
240
+ isSidechain: true,
241
+ type: true,
242
+ uuid: true,
243
+ totalCostUSD: true,
244
+ hasUnknownModelCost: true,
245
+ isMeta: true,
246
+ isCompactSummary: true,
247
+ isVisibleInTranscriptOnly: true,
248
+ isApiErrorMessage: true,
249
+ requestId: true,
250
+ message: { model: true, id: true, usage: USAGE, content: [{ type: true, text: true, id: true, name: true, tool_use_id: true, is_error: true }] },
251
+ };
239
252
  const spec = {
240
253
  source: "claude-code",
241
254
  name: "Claude Code",
@@ -258,6 +271,7 @@ const spec = {
258
271
  start: () => ({ keyed: new Map(), copied: new Set(), titles: { summaries: [], uuids: new Set() } }),
259
272
  record,
260
273
  totals: true,
274
+ fields: FIELDS,
261
275
  finish(out, turns, { state: { own } }) {
262
276
  const { titles } = own;
263
277
  const sourceTitle = titles.custom ?? titles.ai ?? titles.summaries.findLast(({ leaf }) => leaf === undefined || titles.uuids.has(leaf))?.title;
@@ -270,3 +284,5 @@ const spec = {
270
284
  forget: (own) => ({ ...own, titles: { summaries: [], uuids: new Set() } }),
271
285
  };
272
286
  export const claudeCode = lineReader(spec);
287
+ /** The source as lineReader takes it (a test reads with it, its fields or not). */
288
+ export const claudeCodeLines = spec;
@@ -1 +1,60 @@
1
- export declare const codex: import("../reader.js").Reader<"codex", import("../lines.js").FileCursor>;
1
+ import { type FileCursor, type LineSource } from "../lines.js";
2
+ import { type Reader } from "../reader.js";
3
+ import type { Turn } from "../types.js";
4
+ /** Whether the file has user_message events, and its user messages, each with its place among the turns, held to the
5
+ * file's end. `openAt`: the request the model's output items join, until a result of one of its calls (`openCalls`);
6
+ * `unpaidAt`, the latest request whose usage has not come (written after its calls' results at times), each by its index
7
+ * in the read's answers, or a totals read's counts; `records`, whether the file has token_usage_records, with the
8
+ * responses they counted, and `total`, the last token_count's total; `items`, whether each completed item failed, and
9
+ * `results`, each tool result by call id, with whether its output said; `provider`, the model provider its session_meta
10
+ * names; `forkedFrom`, the threads its session_meta says it forked or was spawned from; `replay`, the parent's history
11
+ * a fork copied in (forkReplay), null when there is none. */
12
+ type Own = {
13
+ forkedFrom?: Set<string>;
14
+ replay?: Replay | null;
15
+ looked?: Looked;
16
+ events: boolean;
17
+ held: {
18
+ index: number;
19
+ turn: Turn;
20
+ }[];
21
+ openAt?: number;
22
+ unpaidAt?: number;
23
+ openCalls?: string[];
24
+ records: boolean;
25
+ responses: Set<string>;
26
+ total?: string;
27
+ items: Map<string, boolean>;
28
+ results: Map<string, {
29
+ turn: Extract<Turn, {
30
+ role: "toolResult";
31
+ }>;
32
+ stated: boolean;
33
+ }>;
34
+ provider?: string;
35
+ };
36
+ /** A fork's copy (forkReplay): the usages to skip, by ordinal, the last of them, the usages read so far, and the total
37
+ * the last skipped one left; `open`, whether the file ended before its copy's end could be known, so a read of more of it
38
+ * finds the copy again (codex.read). */
39
+ type Replay = {
40
+ skip: [number, number][];
41
+ until: number;
42
+ seen: number;
43
+ total?: string;
44
+ open: boolean;
45
+ };
46
+ /** The parent a fork's read looked for, and the rollout it read for it: null when there was none to read. */
47
+ type Looked = {
48
+ parent: string;
49
+ path: string | null;
50
+ };
51
+ /** Codex's reader over `source` (spec, or a test's variant of it): a read from a rollout's start first finds the parent's
52
+ * history a fork copied in (forkReplay); a read going on from a cursor has it in the cursor's state. Whether a read goes
53
+ * on is decided before reading, so each read reads the file once: a rollout the line reader would start over (rewritten
54
+ * in place), and a fork whose copy was still open where the last read ended and that has grown since, are read from
55
+ * their start with the copy found again. */
56
+ export declare function codexReader(source: LineSource<"codex", Own>): Reader<"codex", FileCursor>;
57
+ export declare const codex: Reader<"codex", FileCursor>;
58
+ /** The source as lineReader takes it (a test reads with it, its fields or not). */
59
+ export declare const codexLines: LineSource<"codex", Own>;
60
+ export {};