@tangle-network/harness-sessions 1.0.6 → 1.0.8
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/dist/builder.d.ts +0 -13
- package/dist/builder.js +0 -17
- package/dist/capture.d.ts +0 -8
- package/dist/capture.js +3 -14
- package/dist/catalog.d.ts +16 -18
- package/dist/catalog.js +6 -20
- package/dist/cli.js +0 -1
- package/dist/formats/claude-code.d.ts +0 -1
- package/dist/formats/claude-code.js +2 -25
- package/dist/formats/claude-records.d.ts +0 -8
- package/dist/formats/claude-records.js +0 -6
- package/dist/formats/codex.js +2 -45
- package/dist/formats/factory.js +2 -19
- package/dist/formats/fold.d.ts +6 -2
- package/dist/formats/fold.js +16 -0
- package/dist/formats/grok.js +29 -30
- package/dist/formats/head.d.ts +0 -1
- package/dist/formats/head.js +0 -4
- package/dist/formats/hermes.d.ts +3 -0
- package/dist/formats/hermes.js +89 -0
- package/dist/formats/kimi.d.ts +0 -1
- package/dist/formats/kimi.js +2 -22
- package/dist/formats/nanoclaw.js +17 -27
- package/dist/formats/openclaw.d.ts +3 -0
- package/dist/formats/openclaw.js +98 -0
- package/dist/formats/opencode.d.ts +0 -1
- package/dist/formats/opencode.js +12 -31
- package/dist/formats/pi.d.ts +1 -1
- package/dist/formats/pi.js +5 -25
- package/dist/formats/prime.d.ts +2 -0
- package/dist/formats/prime.js +30 -0
- package/dist/formats/sandbox-events.d.ts +12 -0
- package/dist/formats/sandbox-events.js +69 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -0
- package/dist/input.d.ts +0 -6
- package/dist/input.js +0 -2
- package/dist/projections/chat.d.ts +0 -4
- package/dist/projections/chat.js +0 -1
- package/dist/projections/otlp.d.ts +0 -2
- package/dist/projections/otlp.js +0 -1
- package/dist/projections/turns.d.ts +0 -14
- package/dist/projections/turns.js +0 -3
- package/dist/registry.d.ts +0 -5
- package/dist/registry.js +4 -6
- package/dist/schema.d.ts +0 -39
- package/dist/source.d.ts +0 -11
- package/dist/source.js +0 -19
- package/dist/stores.d.ts +0 -4
- package/dist/stores.js +0 -5
- package/package.json +1 -1
package/dist/formats/codex.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/** Codex rollout jsonl. turn_context is the configured model. total_token_usage is the session total. */
|
|
3
2
|
import { basename } from 'node:path';
|
|
4
3
|
import { SessionBuilder, argumentsOf, count, sessionError, str, textOf, usageOrNull, } from '../builder.js';
|
|
5
4
|
import { fileRecords, isMissing } from '../source.js';
|
|
6
|
-
import { runFold } from './fold.js';
|
|
5
|
+
import { foldedReader, runFold } from './fold.js';
|
|
7
6
|
import { CODEX_HOME_RULE, EVIDENCE, LOCATE_READS, STORES, codexHomes, mapLimit, storeGlob, walkFiles, withinPath } from '../stores.js';
|
|
8
7
|
import { headRecord } from './head.js';
|
|
9
8
|
import { stat } from 'node:fs/promises';
|
|
@@ -12,7 +11,6 @@ const FORMAT = 'codex.rollout-jsonl';
|
|
|
12
11
|
const STORE = STORES[FORMAT];
|
|
13
12
|
const HARNESS = 'codex';
|
|
14
13
|
const ID_IN_NAME = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/u;
|
|
15
|
-
/** Codex counts cached tokens inside `input_tokens`; the normalized input excludes them. */
|
|
16
14
|
function usageOf(value) {
|
|
17
15
|
if (!isRecord(value))
|
|
18
16
|
return null;
|
|
@@ -39,7 +37,6 @@ function addTotals(a, b) {
|
|
|
39
37
|
const sum = (x, y) => (x === null && y === null ? null : (x ?? 0) + (y ?? 0));
|
|
40
38
|
return { input: sum(a.input, b.input), output: sum(a.output, b.output), reasoning: sum(a.reasoning, b.reasoning), cacheRead: sum(a.cacheRead, b.cacheRead), cacheWrite: sum(a.cacheWrite, b.cacheWrite) };
|
|
41
39
|
}
|
|
42
|
-
// Text the harness puts in a user-role message: environment context, instructions, shell output.
|
|
43
40
|
const INJECTED_USER_TEXT = /^\s*(?:<[a-z_]+(?:\s[^>]*)?>|# AGENTS\.md instructions)/u;
|
|
44
41
|
function contentParts(content) {
|
|
45
42
|
const parts = [];
|
|
@@ -57,16 +54,12 @@ function contentParts(content) {
|
|
|
57
54
|
}
|
|
58
55
|
return parts;
|
|
59
56
|
}
|
|
60
|
-
// `exec_command` and `write_stdin` answer with a text header (`Chunk ID`, `Wall time`, then
|
|
61
|
-
// `Process exited with code N` once the process is done) before `Output:`; a still-running process
|
|
62
|
-
// says `Process running with session ID` instead and has no status yet.
|
|
63
57
|
const PROCESS_EXIT = /^Process exited with code (-?\d+)\s*$/mu;
|
|
64
58
|
function processFailed(text) {
|
|
65
59
|
const header = text.split(/^Output:/mu, 1)[0] ?? '';
|
|
66
60
|
const match = PROCESS_EXIT.exec(header);
|
|
67
61
|
return match !== null && Number(match[1]) !== 0;
|
|
68
62
|
}
|
|
69
|
-
/** A nonzero numeric `exit_code` anywhere in a parsed JSON value (code-mode tool results nest it). */
|
|
70
63
|
function jsonExitFailed(value, depth = 0) {
|
|
71
64
|
if (depth > 4)
|
|
72
65
|
return false;
|
|
@@ -78,10 +71,6 @@ function jsonExitFailed(value, depth = 0) {
|
|
|
78
71
|
return true;
|
|
79
72
|
return Object.values(value).some((v) => jsonExitFailed(v, depth + 1));
|
|
80
73
|
}
|
|
81
|
-
/**
|
|
82
|
-
* Code mode runs JavaScript in an `exec` call; its `tools.exec_command(...)` results are printed
|
|
83
|
-
* as JSON items carrying `exit_code`. The first item is `Script failed` when the script threw.
|
|
84
|
-
*/
|
|
85
74
|
function codeModeFailed(output) {
|
|
86
75
|
if (!Array.isArray(output))
|
|
87
76
|
return false;
|
|
@@ -126,9 +115,6 @@ export function createFold(ref, mode, stats) {
|
|
|
126
115
|
let aborted = null;
|
|
127
116
|
let sawMeta = false;
|
|
128
117
|
let compacted = 0;
|
|
129
|
-
// A forked rollout replays the parent's history after the parent's own session_meta; that
|
|
130
|
-
// history is copied context, not this session's turns. It ends at this session's first turn,
|
|
131
|
-
// whose (time-ordered, UUIDv7) turn id sorts at or after the session id.
|
|
132
118
|
let forked = false;
|
|
133
119
|
let inherited = false;
|
|
134
120
|
let inheritedRecords = 0;
|
|
@@ -200,7 +186,6 @@ export function createFold(ref, mode, stats) {
|
|
|
200
186
|
case 'response_item': {
|
|
201
187
|
const kind = str(payload.type) ?? '';
|
|
202
188
|
if (kind === 'agent_message') {
|
|
203
|
-
// A message from another agent of the same run (a task or a reply); input to this thread.
|
|
204
189
|
open = null;
|
|
205
190
|
builder.message({ id: str(payload.id) ?? `${builder.nativeSessionId}:${stats.line}`, role: 'user', actor: 'subagent-spawn', at, modelCallId: null, parts: contentParts(payload.content) });
|
|
206
191
|
}
|
|
@@ -235,7 +220,6 @@ export function createFold(ref, mode, stats) {
|
|
|
235
220
|
assistantMessage(at, call, [{ type: 'tool-call', toolCallId: callId }]);
|
|
236
221
|
tools.set(callId, { name, input: typeof raw === 'string' ? raw : '' });
|
|
237
222
|
builder.toolCall({ id: callId, name, ...argumentsOf(raw), at, messageId: openMessage });
|
|
238
|
-
// A web search is answered inside the response; there is no output item.
|
|
239
223
|
if (kind === 'web_search_call')
|
|
240
224
|
builder.toolResult(callId, { output: payload.action ?? null, text: null, isError: payload.status === 'failed', at, messageId: openMessage });
|
|
241
225
|
}
|
|
@@ -251,9 +235,6 @@ export function createFold(ref, mode, stats) {
|
|
|
251
235
|
case 'event_msg': {
|
|
252
236
|
const kind = str(payload.type);
|
|
253
237
|
if (kind === 'item_completed') {
|
|
254
|
-
// Items of this thread repeat its response items. Items of another thread are a child
|
|
255
|
-
// thread's activity (a review or a sub-agent) logged in this rollout; that thread is its
|
|
256
|
-
// own session.
|
|
257
238
|
const thread = str(payload.thread_id);
|
|
258
239
|
if (thread && thread !== builder.nativeSessionId && !foreignThreads.has(thread)) {
|
|
259
240
|
foreignThreads.add(thread);
|
|
@@ -265,13 +246,10 @@ export function createFold(ref, mode, stats) {
|
|
|
265
246
|
if (info === null)
|
|
266
247
|
break;
|
|
267
248
|
const totalSignature = signature(info.total_token_usage);
|
|
268
|
-
// Codex repeats a token_count without a new response (rate-limit refreshes); a
|
|
269
|
-
// response is counted once, when the cumulative counter moves.
|
|
270
249
|
if (totalSignature !== '' && totalSignature === lastTotalSignature)
|
|
271
250
|
break;
|
|
272
251
|
if (isRecord(info.total_token_usage)) {
|
|
273
252
|
const raw = info.total_token_usage;
|
|
274
|
-
// A resumed process restarts the counter; bank what the previous process reported.
|
|
275
253
|
if (lastTotalRaw && count(raw.total_tokens) !== null && count(lastTotalRaw.total_tokens) !== null && raw.total_tokens < lastTotalRaw.total_tokens) {
|
|
276
254
|
committedTotal = addTotals(committedTotal, lastTotal);
|
|
277
255
|
}
|
|
@@ -293,7 +271,6 @@ export function createFold(ref, mode, stats) {
|
|
|
293
271
|
open = null;
|
|
294
272
|
}
|
|
295
273
|
else if (kind === 'stream_error') {
|
|
296
|
-
// A dropped stream that Codex retries. It is a model request that returned no answer.
|
|
297
274
|
calls += 1;
|
|
298
275
|
builder.modelCall({ id: `${builder.nativeSessionId}:response:${calls}`, at, provider, servedModel: model, requestedModel: model, usage: null, costUsd: null, stopReason: null, error: sessionError('stream_error', str(payload.message) ?? 'stream error', at, httpStatusOf(payload)) });
|
|
299
276
|
}
|
|
@@ -308,8 +285,6 @@ export function createFold(ref, mode, stats) {
|
|
|
308
285
|
else if (kind === 'task_complete') {
|
|
309
286
|
aborted = null;
|
|
310
287
|
open = null;
|
|
311
|
-
// Codex 0.145 records a turn that ended on a failed request only here: the request that
|
|
312
|
-
// failed returned no answer, so it is a model call with that error and no served model.
|
|
313
288
|
if (isRecord(payload.error)) {
|
|
314
289
|
const failure = payload.error;
|
|
315
290
|
calls += 1;
|
|
@@ -357,7 +332,6 @@ function httpStatusOf(payload) {
|
|
|
357
332
|
}
|
|
358
333
|
const ROLLOUT = storeGlob(CODEX_HOME_RULE.rollout);
|
|
359
334
|
const UUID_V7 = /^([0-9a-f]{8})-([0-9a-f]{4})-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
|
|
360
|
-
/** Every rollout file, live and archived, in every Codex home under `home`. */
|
|
361
335
|
async function rollouts(home) {
|
|
362
336
|
const files = [];
|
|
363
337
|
for (const codexHome of await codexHomes(home)) {
|
|
@@ -370,12 +344,10 @@ async function rollouts(home) {
|
|
|
370
344
|
}
|
|
371
345
|
return files;
|
|
372
346
|
}
|
|
373
|
-
/** A UUIDv7's creation time: a session's rollout is written at or after its thread id is made. */
|
|
374
347
|
function uuidV7Ms(id) {
|
|
375
348
|
const match = UUID_V7.exec(id);
|
|
376
349
|
return match ? Number.parseInt(`${match[1]}${match[2]}`, 16) : 0;
|
|
377
350
|
}
|
|
378
|
-
/** A ref for each of `files` that passes the filters, from its `session_meta`, with the file's bytes. */
|
|
379
351
|
async function refsOf(files, home, opts) {
|
|
380
352
|
const found = await mapLimit(files, LOCATE_READS, async (path) => {
|
|
381
353
|
let info;
|
|
@@ -413,7 +385,6 @@ async function refsOf(files, home, opts) {
|
|
|
413
385
|
});
|
|
414
386
|
return found.filter((entry) => entry !== null);
|
|
415
387
|
}
|
|
416
|
-
/** Sessions in every Codex home under `home`. A lookup by id takes the rollout named for it; when no name carries the id, it reads the `session_meta` of each rollout written since the id was made. A session copied into several homes (`acct move`) is one ref, its largest copy, which holds every record. */
|
|
417
388
|
async function locate(home, opts) {
|
|
418
389
|
const files = await rollouts(home);
|
|
419
390
|
const id = opts.nativeSessionId;
|
|
@@ -436,21 +407,7 @@ async function locate(home, opts) {
|
|
|
436
407
|
}
|
|
437
408
|
return [...largest.values()].map((c) => c.ref).sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
438
409
|
}
|
|
439
|
-
export const codexReader = {
|
|
440
|
-
harness: HARNESS,
|
|
441
|
-
aliases: ['codex-acp', 'codex-cli'],
|
|
442
|
-
formats: [FORMAT],
|
|
443
|
-
stores: [STORE],
|
|
444
|
-
globs: { session: STORE.files[0] },
|
|
445
|
-
evidence: EVIDENCE[FORMAT],
|
|
446
|
-
locate: (home, opts = {}) => locate(home, opts),
|
|
447
|
-
async read(ref, opts = {}) {
|
|
448
|
-
return (await fold(ref, 'full', opts)).build();
|
|
449
|
-
},
|
|
450
|
-
async summarize(ref, opts = {}) {
|
|
451
|
-
return (await fold(ref, 'summary', opts)).summary();
|
|
452
|
-
},
|
|
453
|
-
};
|
|
410
|
+
export const codexReader = foldedReader({ harness: HARNESS, aliases: ['codex-acp', 'codex-cli'], format: FORMAT }, locate, fold);
|
|
454
411
|
export function codexRefForFile(path) {
|
|
455
412
|
return {
|
|
456
413
|
harness: HARNESS,
|
package/dist/formats/factory.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/** Factory Droid sessions: jsonl transcript plus a sibling settings file (model and token totals). */
|
|
3
2
|
import { readFile } from 'node:fs/promises';
|
|
4
3
|
import { basename, join } from 'node:path';
|
|
5
4
|
import { SessionBuilder, count, str, textOf, usageOrNull } from '../builder.js';
|
|
6
5
|
import { fileRecords, isMissing } from '../source.js';
|
|
7
6
|
import { EVIDENCE, STORES, mtimeMs, storeGlob, walkFiles, withinPath } from '../stores.js';
|
|
8
|
-
import { runFold } from './fold.js';
|
|
7
|
+
import { foldedReader, runFold } from './fold.js';
|
|
9
8
|
import { headRecord } from './head.js';
|
|
10
9
|
const FORMAT = 'factory.session-jsonl';
|
|
11
10
|
const STORE = STORES[FORMAT];
|
|
@@ -78,7 +77,6 @@ export function createFold(ref, mode, stats, settings = { model: null, usage: nu
|
|
|
78
77
|
};
|
|
79
78
|
const finish = () => {
|
|
80
79
|
builder.source(stats);
|
|
81
|
-
// Factory records only the session's token totals, in its settings file.
|
|
82
80
|
if (settings.usage !== null)
|
|
83
81
|
builder.setSessionUsage(settings.usage);
|
|
84
82
|
builder.inferEnding();
|
|
@@ -125,7 +123,6 @@ async function locate(home, opts) {
|
|
|
125
123
|
const filtered = [];
|
|
126
124
|
for (const ref of refs) {
|
|
127
125
|
if (opts.cwd) {
|
|
128
|
-
// The directory name encodes the cwd with `/` as `-`; the session_start line holds it exactly.
|
|
129
126
|
const head = await headRecord(ref.path, (r) => r.type === 'session_start', 1);
|
|
130
127
|
const cwd = head ? str(head.cwd) : null;
|
|
131
128
|
ref.cwd = cwd;
|
|
@@ -136,21 +133,7 @@ async function locate(home, opts) {
|
|
|
136
133
|
}
|
|
137
134
|
return filtered.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
138
135
|
}
|
|
139
|
-
export const factoryReader = {
|
|
140
|
-
harness: HARNESS,
|
|
141
|
-
aliases: ['factory', 'droid'],
|
|
142
|
-
formats: [FORMAT],
|
|
143
|
-
stores: [STORE],
|
|
144
|
-
globs: { session: STORE.files[0] },
|
|
145
|
-
evidence: EVIDENCE[FORMAT],
|
|
146
|
-
locate: (home, opts = {}) => locate(home, opts),
|
|
147
|
-
async read(ref, opts = {}) {
|
|
148
|
-
return (await fold(ref, 'full', opts)).build();
|
|
149
|
-
},
|
|
150
|
-
async summarize(ref, opts = {}) {
|
|
151
|
-
return (await fold(ref, 'summary', opts)).summary();
|
|
152
|
-
},
|
|
153
|
-
};
|
|
136
|
+
export const factoryReader = foldedReader({ harness: HARNESS, aliases: ['factory', 'droid'], format: FORMAT }, locate, fold);
|
|
154
137
|
export function factoryRefForFile(path) {
|
|
155
138
|
return { harness: HARNESS, format: FORMAT, nativeSessionId: basename(path, '.jsonl'), path, files: [path], home: null, cwd: null, mtimeMs: 0, parentNativeSessionId: null };
|
|
156
139
|
}
|
package/dist/formats/fold.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import type { SessionBuilder } from '../builder.js';
|
|
1
|
+
import type { BuildMode, SessionBuilder } from '../builder.js';
|
|
2
|
+
import type { HarnessSessionReader, LocateOptions, ReadOptions, SessionFormatId, SessionRef } from '../schema.js';
|
|
2
3
|
import type { RecordSource } from '../source.js';
|
|
3
|
-
/** One format's fold, driven by whoever has the records: `observe` takes each record in order and `finish` closes the session. Reading a file drives it asynchronously; text or parsed records in memory drive it synchronously. Either way it is the same fold. */
|
|
4
4
|
export interface SessionFold {
|
|
5
5
|
builder: SessionBuilder;
|
|
6
6
|
observe(record: Record<string, unknown>): void;
|
|
7
7
|
finish(): SessionBuilder;
|
|
8
8
|
}
|
|
9
9
|
export declare function runFold(fold: SessionFold, source: RecordSource): Promise<SessionBuilder>;
|
|
10
|
+
/** A reader: what differs between harnesses is how a session is found and folded. */
|
|
11
|
+
export declare function foldedReader(reader: Pick<HarnessSessionReader, 'harness' | 'aliases'> & Partial<Pick<HarnessSessionReader, 'stores' | 'globs'>> & {
|
|
12
|
+
format: SessionFormatId;
|
|
13
|
+
}, locate: (home: string, opts: LocateOptions) => Promise<SessionRef[]>, fold: (ref: SessionRef, mode: BuildMode, opts: ReadOptions) => Promise<SessionBuilder>): HarnessSessionReader;
|
package/dist/formats/fold.js
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
|
+
import { EVIDENCE, STORES } from '../stores.js';
|
|
1
2
|
export async function runFold(fold, source) {
|
|
2
3
|
for await (const record of source.records)
|
|
3
4
|
fold.observe(record);
|
|
4
5
|
return fold.finish();
|
|
5
6
|
}
|
|
7
|
+
/** A reader: what differs between harnesses is how a session is found and folded. */
|
|
8
|
+
export function foldedReader(reader, locate, fold) {
|
|
9
|
+
const store = STORES[reader.format];
|
|
10
|
+
return {
|
|
11
|
+
harness: reader.harness,
|
|
12
|
+
aliases: reader.aliases,
|
|
13
|
+
formats: [reader.format],
|
|
14
|
+
stores: reader.stores ?? [store],
|
|
15
|
+
globs: reader.globs ?? { session: store.files[0] },
|
|
16
|
+
evidence: EVIDENCE[reader.format],
|
|
17
|
+
locate: (home, opts = {}) => locate(home, opts),
|
|
18
|
+
read: async (ref, opts = {}) => (await fold(ref, 'full', opts)).build(),
|
|
19
|
+
summarize: async (ref, opts = {}) => (await fold(ref, 'summary', opts)).summary(),
|
|
20
|
+
};
|
|
21
|
+
}
|
package/dist/formats/grok.js
CHANGED
|
@@ -4,7 +4,7 @@ import { basename, dirname, join } from 'node:path';
|
|
|
4
4
|
import { SessionBuilder, count, str, textOf, usageOrNull } from '../builder.js';
|
|
5
5
|
import { fileRecords, isMissing } from '../source.js';
|
|
6
6
|
import { EVIDENCE, STORES, mtimeMs, storeGlob, walkFiles } from '../stores.js';
|
|
7
|
-
import { runFold } from './fold.js';
|
|
7
|
+
import { foldedReader, runFold } from './fold.js';
|
|
8
8
|
const FORMAT = 'grok.acp-session-dir';
|
|
9
9
|
const STORE = STORES[FORMAT];
|
|
10
10
|
const HARNESS = 'grok';
|
|
@@ -93,39 +93,38 @@ function createFold(ref, mode, stats, model, usage) {
|
|
|
93
93
|
};
|
|
94
94
|
return { builder, observe, finish: () => { builder.source(stats); builder.inferEnding(); return builder; } };
|
|
95
95
|
}
|
|
96
|
+
async function markedJson(ref, name, mark) {
|
|
97
|
+
for (const file of [join(dirname(ref.path), name), ...ref.files]) {
|
|
98
|
+
if (file === ref.path)
|
|
99
|
+
continue;
|
|
100
|
+
const parsed = await readJson(file);
|
|
101
|
+
if (isRecord(parsed) && mark in parsed)
|
|
102
|
+
return parsed;
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
96
106
|
async function fold(ref, mode, options) {
|
|
97
107
|
const records = fileRecords(ref.path, { strict: options.corruption === 'strict', signal: options.signal });
|
|
98
|
-
const
|
|
99
|
-
const summary = await readJson(join(dir, 'summary.json'));
|
|
108
|
+
const summary = await markedJson(ref, 'summary.json', 'current_model_id');
|
|
100
109
|
const model = isRecord(summary) ? str(summary.current_model_id) : null;
|
|
101
|
-
return runFold(createFold(ref, mode, records.stats, model, usageOf(await
|
|
110
|
+
return runFold(createFold(ref, mode, records.stats, model, usageOf(await markedJson(ref, 'signals.json', 'primaryModelId'))), records);
|
|
102
111
|
}
|
|
103
|
-
export const grokReader = {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const id = basename(dirname(rel));
|
|
120
|
-
if ((opts.sinceMs && mtime < opts.sinceMs) || (opts.nativeSessionId && id !== opts.nativeSessionId))
|
|
121
|
-
continue;
|
|
122
|
-
refs.push({ harness: HARNESS, format: FORMAT, nativeSessionId: id, path, files: [path], home, cwd: null, mtimeMs: mtime, parentNativeSessionId: null });
|
|
123
|
-
}
|
|
124
|
-
return refs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
125
|
-
},
|
|
126
|
-
async read(ref, opts = {}) { return (await fold(ref, 'full', opts)).build(); },
|
|
127
|
-
async summarize(ref, opts = {}) { return (await fold(ref, 'summary', opts)).summary(); },
|
|
128
|
-
};
|
|
112
|
+
export const grokReader = foldedReader({ harness: HARNESS, aliases: ['grok-build'], format: FORMAT }, async (home, opts) => {
|
|
113
|
+
const root = join(home, STORE.root);
|
|
114
|
+
const pattern = storeGlob(STORE.files[0], opts.nativeSessionId);
|
|
115
|
+
const refs = [];
|
|
116
|
+
for (const rel of await walkFiles(root)) {
|
|
117
|
+
if (!pattern.test(rel))
|
|
118
|
+
continue;
|
|
119
|
+
const path = join(root, rel);
|
|
120
|
+
const mtime = await mtimeMs(path);
|
|
121
|
+
const id = basename(dirname(rel));
|
|
122
|
+
if ((opts.sinceMs && mtime < opts.sinceMs) || (opts.nativeSessionId && id !== opts.nativeSessionId))
|
|
123
|
+
continue;
|
|
124
|
+
refs.push({ harness: HARNESS, format: FORMAT, nativeSessionId: id, path, files: [path], home, cwd: null, mtimeMs: mtime, parentNativeSessionId: null });
|
|
125
|
+
}
|
|
126
|
+
return refs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
127
|
+
}, fold);
|
|
129
128
|
export function grokRefForFile(path) {
|
|
130
129
|
return { harness: HARNESS, format: FORMAT, nativeSessionId: basename(dirname(path)), path, files: [path], home: null, cwd: null, mtimeMs: 0, parentNativeSessionId: null };
|
|
131
130
|
}
|
package/dist/formats/head.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
/** The first record among a session file's first `limit` records that satisfies `match`. Locate reads one for every session in a store, so it reads the file a small step at a time and stops at the answer: never the rest of the file, and no digest of it. Lines parse as the session reader parses them: a blank, malformed or non-object line is skipped, and a last line with no newline counts when it is a whole record. */
|
|
2
1
|
export declare function headRecord(path: string, match: (record: Record<string, unknown>) => boolean, limit?: number): Promise<Record<string, unknown> | null>;
|
package/dist/formats/head.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { open } from 'node:fs/promises';
|
|
2
2
|
import { StringDecoder } from 'node:string_decoder';
|
|
3
3
|
import { isMissing } from '../source.js';
|
|
4
|
-
/** Bytes a head read takes at a time. A session's identifying record is almost always in the first. */
|
|
5
4
|
const HEAD_CHUNK = 64 * 1024;
|
|
6
|
-
/** The first record among a session file's first `limit` records that satisfies `match`. Locate reads one for every session in a store, so it reads the file a small step at a time and stops at the answer: never the rest of the file, and no digest of it. Lines parse as the session reader parses them: a blank, malformed or non-object line is skipped, and a last line with no newline counts when it is a whole record. */
|
|
7
5
|
export async function headRecord(path, match, limit = 64) {
|
|
8
6
|
let handle;
|
|
9
7
|
try {
|
|
@@ -15,7 +13,6 @@ export async function headRecord(path, match, limit = 64) {
|
|
|
15
13
|
throw error;
|
|
16
14
|
}
|
|
17
15
|
let seen = 0;
|
|
18
|
-
// undefined: read on; null: `limit` records without a match; a record: the match.
|
|
19
16
|
const take = (line) => {
|
|
20
17
|
const text = line.replace(/\r$/u, '');
|
|
21
18
|
if (text.trim().length === 0)
|
|
@@ -37,7 +34,6 @@ export async function headRecord(path, match, limit = 64) {
|
|
|
37
34
|
try {
|
|
38
35
|
const decoder = new StringDecoder('utf8');
|
|
39
36
|
const buffer = Buffer.allocUnsafe(HEAD_CHUNK);
|
|
40
|
-
// The line being read, in pieces, so a record longer than a step is joined once.
|
|
41
37
|
let pending = [];
|
|
42
38
|
let position = 0;
|
|
43
39
|
for (;;) {
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { foldedReader } from './fold.js';
|
|
2
|
+
import { basename, join } from 'node:path';
|
|
3
|
+
import { SessionBuilder, count, str, usageOrNull } from '../builder.js';
|
|
4
|
+
import { withSqlite } from '../source.js';
|
|
5
|
+
import { EVIDENCE, STORES, mtimeMs, walkFiles } from '../stores.js';
|
|
6
|
+
const FORMAT = 'hermes.state-sqlite';
|
|
7
|
+
const STORE = STORES[FORMAT];
|
|
8
|
+
const HARNESS = 'hermes';
|
|
9
|
+
function callsOf(raw) {
|
|
10
|
+
let value = raw;
|
|
11
|
+
if (typeof raw === 'string') {
|
|
12
|
+
try {
|
|
13
|
+
value = JSON.parse(raw);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (!Array.isArray(value))
|
|
20
|
+
return [];
|
|
21
|
+
return value.flatMap((call) => {
|
|
22
|
+
if (!call || typeof call !== 'object')
|
|
23
|
+
return [];
|
|
24
|
+
const row = call;
|
|
25
|
+
const fn = row.function;
|
|
26
|
+
const id = str(row.id);
|
|
27
|
+
const name = str(row.name) ?? str(fn?.name);
|
|
28
|
+
if (!id || !name)
|
|
29
|
+
return [];
|
|
30
|
+
return [{ id, name, input: row.arguments ?? fn?.arguments ?? null }];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function readDb(db, sessionId, builder) {
|
|
34
|
+
const session = db.prepare('SELECT model, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, started_at FROM sessions WHERE id = ?').get(sessionId);
|
|
35
|
+
const at = session ? builder.seen(session.started_at ? session.started_at * 1000 : null) : null;
|
|
36
|
+
const usage = session ? usageOrNull({ input: count(session.input_tokens), output: count(session.output_tokens), reasoning: null, cacheRead: count(session.cache_read_tokens), cacheWrite: count(session.cache_write_tokens) }) : null;
|
|
37
|
+
const model = session ? str(session.model) : null;
|
|
38
|
+
const rows = db.prepare('SELECT id, role, content, tool_call_id, tool_calls, tool_name, timestamp FROM messages WHERE session_id = ? ORDER BY id').all(sessionId);
|
|
39
|
+
let calls = 0;
|
|
40
|
+
for (const row of rows) {
|
|
41
|
+
const when = builder.seen(row.timestamp ? row.timestamp * 1000 : null);
|
|
42
|
+
const id = String(row.id);
|
|
43
|
+
if (row.role === 'user' && row.content)
|
|
44
|
+
builder.message({ id, role: 'user', actor: 'human', at: when, modelCallId: null, parts: [{ type: 'text', text: row.content }] });
|
|
45
|
+
if (row.role === 'assistant') {
|
|
46
|
+
const modelCallId = `${sessionId}:${calls}`;
|
|
47
|
+
builder.modelCall({ id: modelCallId, at: when ?? at, provider: null, servedModel: model, requestedModel: model, usage: calls === 0 ? usage : null, costUsd: null, stopReason: null, error: null });
|
|
48
|
+
calls += 1;
|
|
49
|
+
for (const call of callsOf(row.tool_calls))
|
|
50
|
+
builder.toolCall({ id: call.id, name: call.name, input: call.input, inputText: null, at: when, messageId: id });
|
|
51
|
+
if (row.content)
|
|
52
|
+
builder.message({ id, role: 'assistant', actor: 'agent', at: when, modelCallId, parts: [{ type: 'text', text: row.content }] });
|
|
53
|
+
}
|
|
54
|
+
if (row.role === 'tool' && row.tool_call_id)
|
|
55
|
+
builder.toolResult(row.tool_call_id, { output: row.content, text: row.content, isError: false, at: when, messageId: id });
|
|
56
|
+
}
|
|
57
|
+
return usage;
|
|
58
|
+
}
|
|
59
|
+
async function fold(ref, mode) {
|
|
60
|
+
const builder = new SessionBuilder(HARNESS, FORMAT, EVIDENCE[FORMAT].servedModel, mode, ref.nativeSessionId, ref.parentNativeSessionId);
|
|
61
|
+
await withSqlite(ref.path, (db) => readDb(db, ref.nativeSessionId, builder));
|
|
62
|
+
builder.inferEnding();
|
|
63
|
+
return builder;
|
|
64
|
+
}
|
|
65
|
+
export async function hermesSessionsInStore(path) {
|
|
66
|
+
const refs = [];
|
|
67
|
+
await withSqlite(path, (db) => {
|
|
68
|
+
for (const row of db.prepare('SELECT id FROM sessions').all()) {
|
|
69
|
+
refs.push({ harness: HARNESS, format: FORMAT, nativeSessionId: row.id, path, files: [path], home: null, cwd: null, mtimeMs: 0, parentNativeSessionId: null });
|
|
70
|
+
}
|
|
71
|
+
}).catch(() => undefined);
|
|
72
|
+
return refs;
|
|
73
|
+
}
|
|
74
|
+
export const hermesReader = foldedReader({ harness: HARNESS, aliases: [], format: FORMAT }, async (home, opts) => {
|
|
75
|
+
const root = join(home, STORE.root);
|
|
76
|
+
const refs = [];
|
|
77
|
+
for (const rel of await walkFiles(root)) {
|
|
78
|
+
if (basename(rel) !== 'state.db')
|
|
79
|
+
continue;
|
|
80
|
+
const path = join(root, rel);
|
|
81
|
+
const mtime = await mtimeMs(path);
|
|
82
|
+
for (const ref of await hermesSessionsInStore(path)) {
|
|
83
|
+
if (opts.nativeSessionId && ref.nativeSessionId !== opts.nativeSessionId)
|
|
84
|
+
continue;
|
|
85
|
+
refs.push({ ...ref, home, mtimeMs: mtime });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return refs;
|
|
89
|
+
}, fold);
|
package/dist/formats/kimi.d.ts
CHANGED
|
@@ -5,5 +5,4 @@ import { type SessionFold } from './fold.js';
|
|
|
5
5
|
export declare function createFold(ref: SessionRef, mode: BuildMode, stats: SourceStats): SessionFold;
|
|
6
6
|
export declare function fold(ref: SessionRef, mode: BuildMode, options: ReadOptions, source?: RecordSource): Promise<SessionBuilder>;
|
|
7
7
|
export declare const kimiReader: HarnessSessionReader;
|
|
8
|
-
/** A session read from its `wire.jsonl` outside a HOME layout. */
|
|
9
8
|
export declare function kimiRefForFile(path: string): SessionRef;
|
package/dist/formats/kimi.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { isRecord } from '@tangle-network/agent-core';
|
|
2
|
-
/** Kimi wire.jsonl: TurnBegin, StepBegin, ToolCall, StatusUpdate, ToolResult, StepRetry. Legacy wire has no model. */
|
|
3
2
|
import { basename, dirname, join } from 'node:path';
|
|
4
3
|
import { SessionBuilder, count, sessionError, str, textOf, usageOrNull } from '../builder.js';
|
|
5
4
|
import { fileRecords } from '../source.js';
|
|
6
5
|
import { KIMI_STORES } from '../catalog.js';
|
|
7
6
|
import { EVIDENCE, mtimeMs, storeGlob, walkFiles, withinPath } from '../stores.js';
|
|
8
|
-
import { runFold } from './fold.js';
|
|
7
|
+
import { foldedReader, runFold } from './fold.js';
|
|
9
8
|
const FORMAT = 'kimi.session-dir';
|
|
10
9
|
const HARNESS = 'kimi-code';
|
|
11
|
-
/** The event stream this reader folds; the store also holds context.jsonl and state.json. */
|
|
12
10
|
const WIRE = '*/{id}/wire.jsonl';
|
|
13
11
|
const CHILD_WIRE = '*/*/subagents/{id}/wire.jsonl';
|
|
14
|
-
/** Current main agents belong to session_<uuid>; legacy child wires retain their own agent IDs. */
|
|
15
12
|
function sessionIdentity(path) {
|
|
16
13
|
const directory = dirname(path);
|
|
17
14
|
const container = dirname(directory);
|
|
@@ -59,7 +56,6 @@ export function createFold(ref, mode, stats) {
|
|
|
59
56
|
const observe = (record) => {
|
|
60
57
|
const message = isRecord(record.message) ? record.message : {};
|
|
61
58
|
const outer = str(record.type);
|
|
62
|
-
// Both protocols have metadata; only flat payload events use namespaced discriminators.
|
|
63
59
|
flat ||= outer?.includes('.') === true;
|
|
64
60
|
const payload = outer === 'context.append_loop_event' && isRecord(record.event) ? record.event : outer ? record : isRecord(message.payload) ? message.payload : {};
|
|
65
61
|
const type = outer === 'context.append_loop_event' ? str(payload.type) : outer ?? str(message.type);
|
|
@@ -150,7 +146,6 @@ export function createFold(ref, mode, stats) {
|
|
|
150
146
|
break;
|
|
151
147
|
}
|
|
152
148
|
case 'StepRetry': {
|
|
153
|
-
// A failed attempt Kimi retried: a model call that returned no answer.
|
|
154
149
|
builder.modelCall({
|
|
155
150
|
id: `${builder.nativeSessionId}:turn:${turn}:retry:${builder.modelCalls.length + 1}`, at, provider: null, servedModel: null, requestedModel: null, usage: null, costUsd: null, stopReason: null,
|
|
156
151
|
error: sessionError(str(payload.error_type) ?? 'retry', `${str(payload.error_type) ?? 'error'}${payload.status_code ? ` (status ${String(payload.status_code)})` : ''}`, at, count(payload.status_code)),
|
|
@@ -239,22 +234,7 @@ async function locate(home, opts) {
|
|
|
239
234
|
}
|
|
240
235
|
return refs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
241
236
|
}
|
|
242
|
-
export const kimiReader = {
|
|
243
|
-
harness: HARNESS,
|
|
244
|
-
aliases: ['kimi', 'kimi-cli'],
|
|
245
|
-
formats: [FORMAT],
|
|
246
|
-
stores: KIMI_STORES,
|
|
247
|
-
globs: { session: WIRE, children: CHILD_WIRE },
|
|
248
|
-
evidence: EVIDENCE[FORMAT],
|
|
249
|
-
locate: (home, opts = {}) => locate(home, opts),
|
|
250
|
-
async read(ref, opts = {}) {
|
|
251
|
-
return (await fold(ref, 'full', opts)).build();
|
|
252
|
-
},
|
|
253
|
-
async summarize(ref, opts = {}) {
|
|
254
|
-
return (await fold(ref, 'summary', opts)).summary();
|
|
255
|
-
},
|
|
256
|
-
};
|
|
257
|
-
/** A session read from its `wire.jsonl` outside a HOME layout. */
|
|
237
|
+
export const kimiReader = foldedReader({ harness: HARNESS, aliases: ['kimi', 'kimi-cli'], format: FORMAT, globs: { session: WIRE, children: CHILD_WIRE }, stores: KIMI_STORES }, locate, fold);
|
|
258
238
|
export function kimiRefForFile(path) {
|
|
259
239
|
const { id, parent } = sessionIdentity(path);
|
|
260
240
|
return { harness: HARNESS, format: FORMAT, nativeSessionId: id, path, files: [path], home: null, cwd: null, mtimeMs: 0, parentNativeSessionId: parent };
|
package/dist/formats/nanoclaw.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { foldedReader } from './fold.js';
|
|
1
2
|
import { basename, dirname, join } from 'node:path';
|
|
2
3
|
import { SessionBuilder } from '../builder.js';
|
|
3
4
|
import { withSqlite } from '../source.js';
|
|
@@ -12,7 +13,6 @@ function textOf(content) {
|
|
|
12
13
|
return value.text;
|
|
13
14
|
}
|
|
14
15
|
catch {
|
|
15
|
-
// A non-JSON body is already the message text.
|
|
16
16
|
}
|
|
17
17
|
return content;
|
|
18
18
|
}
|
|
@@ -35,32 +35,22 @@ async function fold(ref, mode) {
|
|
|
35
35
|
builder.inferEnding();
|
|
36
36
|
return builder;
|
|
37
37
|
}
|
|
38
|
-
export const nanoclawReader = {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const mtime = await mtimeMs(path);
|
|
55
|
-
if ((opts.sinceMs && mtime < opts.sinceMs) || (opts.nativeSessionId && id !== opts.nativeSessionId))
|
|
56
|
-
continue;
|
|
57
|
-
refs.push({ harness: HARNESS, format: FORMAT, nativeSessionId: id, path, files: [path, join(dirname(path), 'inbound.db')], home, cwd: null, mtimeMs: mtime, parentNativeSessionId: null });
|
|
58
|
-
}
|
|
59
|
-
return refs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
60
|
-
},
|
|
61
|
-
async read(ref, _opts = {}) { return (await fold(ref, 'full')).build(); },
|
|
62
|
-
async summarize(ref, _opts = {}) { return (await fold(ref, 'summary')).summary(); },
|
|
63
|
-
};
|
|
38
|
+
export const nanoclawReader = foldedReader({ harness: HARNESS, aliases: [], format: FORMAT }, async (home, opts) => {
|
|
39
|
+
const root = join(home, STORE.root);
|
|
40
|
+
const pattern = storeGlob(STORE.files[0], opts.nativeSessionId);
|
|
41
|
+
const refs = [];
|
|
42
|
+
for (const rel of await walkFiles(root)) {
|
|
43
|
+
if (!pattern.test(rel))
|
|
44
|
+
continue;
|
|
45
|
+
const path = join(root, rel);
|
|
46
|
+
const id = basename(dirname(rel));
|
|
47
|
+
const mtime = await mtimeMs(path);
|
|
48
|
+
if ((opts.sinceMs && mtime < opts.sinceMs) || (opts.nativeSessionId && id !== opts.nativeSessionId))
|
|
49
|
+
continue;
|
|
50
|
+
refs.push({ harness: HARNESS, format: FORMAT, nativeSessionId: id, path, files: [path, join(dirname(path), 'inbound.db')], home, cwd: null, mtimeMs: mtime, parentNativeSessionId: null });
|
|
51
|
+
}
|
|
52
|
+
return refs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
53
|
+
}, fold);
|
|
64
54
|
export function nanoclawRefForFile(path) {
|
|
65
55
|
return { harness: HARNESS, format: FORMAT, nativeSessionId: basename(dirname(path)), path, files: [path], home: null, cwd: null, mtimeMs: 0, parentNativeSessionId: null };
|
|
66
56
|
}
|