@tangle-network/harness-sessions 1.0.18 → 1.0.19
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/README.md +2 -0
- package/dist/formats/opencode.d.ts +7 -1
- package/dist/formats/opencode.js +61 -15
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -80,6 +80,8 @@ const { sessions, missing } = await listCapture(captureDir)
|
|
|
80
80
|
|
|
81
81
|
The SDK and `harness-sessions read <file>` select OpenCode, Hermes and OpenClaw by database schema. NanoClaw uses the inbound and outbound databases in its session directory. A session reference lists its database files and any existing SQLite sidecars. A foreign schema or an absent session ID returns an empty list. Pass `harness` to resolve a database with more than one native schema.
|
|
82
82
|
|
|
83
|
+
For a bounded JSON payload from `opencode export`, use `readOpenCodeSessionExport(text, { nativeSessionId, label })`. It verifies session, message and part ownership and records the exported bytes' digest. Its model authority is OpenCode's turn context. The capture owner enumerates and supplies each session in the native family.
|
|
84
|
+
|
|
83
85
|
Projections trim; the reader never does:
|
|
84
86
|
|
|
85
87
|
- `toChatMessages(session)`: agent-eval's chat-with-tools messages.
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
import type { HarnessSessionReader, LocateOptions, SessionRef } from '../schema.js';
|
|
1
|
+
import type { HarnessSession, HarnessSessionReader, LocateOptions, SessionRef } from '../schema.js';
|
|
2
|
+
/** Read one bounded JSON session produced by `opencode export`. The capture
|
|
3
|
+
* owner supplies its expected native identity and enumerates the whole family. */
|
|
4
|
+
export declare function readOpenCodeSessionExport(text: string, options: {
|
|
5
|
+
nativeSessionId: string;
|
|
6
|
+
label?: string;
|
|
7
|
+
}): HarnessSession;
|
|
2
8
|
export declare function opencodeSessionsInStore(path: string, home: string | null, opts?: LocateOptions): Promise<SessionRef[]>;
|
|
3
9
|
export declare const opencodeReader: HarnessSessionReader;
|
package/dist/formats/opencode.js
CHANGED
|
@@ -31,6 +31,8 @@ function errorOf(value, at) {
|
|
|
31
31
|
return sessionError(name, str(data.message) ?? str(value.message) ?? name, at, count(data.statusCode));
|
|
32
32
|
}
|
|
33
33
|
function parse(json) {
|
|
34
|
+
if (isRecord(json))
|
|
35
|
+
return json;
|
|
34
36
|
if (typeof json !== 'string')
|
|
35
37
|
return null;
|
|
36
38
|
try {
|
|
@@ -41,24 +43,19 @@ function parse(json) {
|
|
|
41
43
|
return null;
|
|
42
44
|
}
|
|
43
45
|
}
|
|
44
|
-
function
|
|
45
|
-
const session = db.prepare('SELECT id, parent_id, directory, time_created, time_updated FROM session WHERE id = ?').get(ref.nativeSessionId);
|
|
46
|
-
if (!session) {
|
|
47
|
-
builder.gap(`session ${ref.nativeSessionId} is not in the store`);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
46
|
+
function foldRows(label, session, messages, partsOf, childIds, builder, source) {
|
|
50
47
|
builder.cwd = session.directory;
|
|
51
48
|
builder.parentNativeSessionId = session.parent_id ?? null;
|
|
52
49
|
builder.seen(session.time_created);
|
|
53
50
|
const digest = createHash('sha256').update(JSON.stringify(session));
|
|
54
51
|
let bytes = 0;
|
|
55
52
|
const note = (row) => {
|
|
53
|
+
if (source)
|
|
54
|
+
return;
|
|
56
55
|
const data = String(row.data);
|
|
57
56
|
digest.update(`\n${row.id}\t${row.time_created}\t`).update(data);
|
|
58
57
|
bytes += Buffer.byteLength(data);
|
|
59
58
|
};
|
|
60
|
-
const messages = db.prepare('SELECT id, time_created, data FROM message WHERE session_id = ? ORDER BY time_created, id').all(ref.nativeSessionId);
|
|
61
|
-
const partsOf = db.prepare('SELECT id, time_created, data FROM part WHERE message_id = ? ORDER BY id');
|
|
62
59
|
let unparsed = 0;
|
|
63
60
|
let aborted = null;
|
|
64
61
|
for (const row of messages) {
|
|
@@ -72,7 +69,7 @@ function foldSession(db, ref, mode, builder) {
|
|
|
72
69
|
const at = builder.seen(time.created ?? row.time_created);
|
|
73
70
|
builder.seen(time.completed);
|
|
74
71
|
const parts = [];
|
|
75
|
-
for (const partRow of partsOf
|
|
72
|
+
for (const partRow of partsOf(row.id)) {
|
|
76
73
|
note(partRow);
|
|
77
74
|
const part = parse(partRow.data);
|
|
78
75
|
if (part === null)
|
|
@@ -180,21 +177,70 @@ function foldSession(db, ref, mode, builder) {
|
|
|
180
177
|
builder.modelCalls[builder.modelCalls.length - 1].error = messageError;
|
|
181
178
|
aborted = messageError?.kind === 'MessageAbortedError' ? { at: messageError.at, error: messageError } : null;
|
|
182
179
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
builder.child(
|
|
180
|
+
for (const id of childIds) {
|
|
181
|
+
if (!source)
|
|
182
|
+
digest.update(`\n${id}`);
|
|
183
|
+
builder.child(id, null);
|
|
187
184
|
}
|
|
188
|
-
builder.source({ path:
|
|
185
|
+
builder.source({ path: label, ...(source ?? { sha256: `sha256:${digest.digest('hex')}`, bytes }), unparsed });
|
|
189
186
|
const last = builder.modelCalls[builder.modelCalls.length - 1];
|
|
190
187
|
if (aborted && last?.error?.kind === 'MessageAbortedError')
|
|
191
188
|
last.error = null;
|
|
192
189
|
builder.inferEnding(aborted ?? undefined);
|
|
193
190
|
}
|
|
191
|
+
function foldSession(db, ref, builder) {
|
|
192
|
+
const session = db.prepare('SELECT id, parent_id, directory, time_created, time_updated FROM session WHERE id = ?').get(ref.nativeSessionId);
|
|
193
|
+
if (!session) {
|
|
194
|
+
builder.gap(`session ${ref.nativeSessionId} is not in the store`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const messages = db.prepare('SELECT id, time_created, data FROM message WHERE session_id = ? ORDER BY time_created, id').all(ref.nativeSessionId);
|
|
198
|
+
const parts = db.prepare('SELECT id, time_created, data FROM part WHERE message_id = ? ORDER BY id');
|
|
199
|
+
const children = db.prepare('SELECT id FROM session WHERE parent_id = ? ORDER BY time_created, id').all(ref.nativeSessionId);
|
|
200
|
+
foldRows(`${ref.path}#session=${ref.nativeSessionId}`, session, messages, (id) => parts.iterate(id), children.map(({ id }) => id), builder);
|
|
201
|
+
}
|
|
202
|
+
/** Read one bounded JSON session produced by `opencode export`. The capture
|
|
203
|
+
* owner supplies its expected native identity and enumerates the whole family. */
|
|
204
|
+
export function readOpenCodeSessionExport(text, options) {
|
|
205
|
+
const value = parse(text);
|
|
206
|
+
const info = value?.info;
|
|
207
|
+
if (!isRecord(info) || !Array.isArray(value?.messages) || !isRecord(info.time))
|
|
208
|
+
throw new Error('Invalid OpenCode session export');
|
|
209
|
+
const { nativeSessionId, label = `opencode:export:${nativeSessionId}` } = options;
|
|
210
|
+
const { id, directory, parentID, time } = info;
|
|
211
|
+
const created = count(time.created), updated = count(time.updated);
|
|
212
|
+
if (!nativeSessionId || id !== nativeSessionId || typeof directory !== 'string' || created === null || updated === null ||
|
|
213
|
+
(parentID !== undefined && parentID !== null && typeof parentID !== 'string'))
|
|
214
|
+
throw new Error('OpenCode export does not match the declared native session');
|
|
215
|
+
const messages = [];
|
|
216
|
+
const parts = new Map();
|
|
217
|
+
const partIds = new Set();
|
|
218
|
+
for (const message of value.messages) {
|
|
219
|
+
if (!isRecord(message) || !isRecord(message.info) || !Array.isArray(message.parts))
|
|
220
|
+
throw new Error('Invalid OpenCode exported message');
|
|
221
|
+
const data = message.info;
|
|
222
|
+
const messageId = str(data.id);
|
|
223
|
+
const at = isRecord(data.time) ? count(data.time.created) : null;
|
|
224
|
+
if (!messageId || parts.has(messageId) || data.sessionID !== id || at === null || (data.role !== 'assistant' && data.role !== 'user'))
|
|
225
|
+
throw new Error('OpenCode export has an unbound or duplicate message');
|
|
226
|
+
messages.push({ id: messageId, time_created: at, data });
|
|
227
|
+
parts.set(messageId, message.parts.map((part) => {
|
|
228
|
+
if (!isRecord(part) || typeof part.id !== 'string' || !part.id || partIds.has(part.id) ||
|
|
229
|
+
part.sessionID !== id || part.messageID !== messageId || typeof part.type !== 'string')
|
|
230
|
+
throw new Error('OpenCode export has an unbound or duplicate message part');
|
|
231
|
+
partIds.add(part.id);
|
|
232
|
+
return { id: part.id, time_created: null, data: part };
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
const parent = parentID ?? null;
|
|
236
|
+
const builder = new SessionBuilder(HARNESS, FORMAT, EVIDENCE[FORMAT].servedModel, 'full', id, parent);
|
|
237
|
+
foldRows(label, { id, parent_id: parent, directory, time_created: created, time_updated: updated }, messages, (message) => parts.get(message), [], builder, { sha256: `sha256:${createHash('sha256').update(text).digest('hex')}`, bytes: Buffer.byteLength(text) });
|
|
238
|
+
return builder.build();
|
|
239
|
+
}
|
|
194
240
|
async function fold(ref, mode, options) {
|
|
195
241
|
options.signal?.throwIfAborted();
|
|
196
242
|
const builder = new SessionBuilder(HARNESS, FORMAT, EVIDENCE[FORMAT].servedModel, mode, ref.nativeSessionId, ref.parentNativeSessionId);
|
|
197
|
-
await withSqlite(ref.path, (db) => foldSession(db, ref,
|
|
243
|
+
await withSqlite(ref.path, (db) => foldSession(db, ref, builder));
|
|
198
244
|
if (ref.truncated)
|
|
199
245
|
builder.markTruncated(`${ref.path}: the store copy was cut short`);
|
|
200
246
|
return builder;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { isNativeStoreDatabase, nativeStoreSessions } from './native-store.js';
|
|
|
7
7
|
export { claudeCodeReader, claudeCodeRefForFile } from './formats/claude-code.js';
|
|
8
8
|
export { claudeRecordIsMachine, claudeTypedPromptText, type ClaudeRecordFacts } from './formats/claude-records.js';
|
|
9
9
|
export { codexReader, codexRefForFile } from './formats/codex.js';
|
|
10
|
-
export { opencodeReader, opencodeSessionsInStore } from './formats/opencode.js';
|
|
10
|
+
export { opencodeReader, opencodeSessionsInStore, readOpenCodeSessionExport } from './formats/opencode.js';
|
|
11
11
|
export { piReader, piRefForFile } from './formats/pi.js';
|
|
12
12
|
export { kimiReader, kimiRefForFile } from './formats/kimi.js';
|
|
13
13
|
export { factoryReader, factoryRefForFile } from './formats/factory.js';
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ export { isNativeStoreDatabase, nativeStoreSessions } from './native-store.js';
|
|
|
6
6
|
export { claudeCodeReader, claudeCodeRefForFile } from './formats/claude-code.js';
|
|
7
7
|
export { claudeRecordIsMachine, claudeTypedPromptText } from './formats/claude-records.js';
|
|
8
8
|
export { codexReader, codexRefForFile } from './formats/codex.js';
|
|
9
|
-
export { opencodeReader, opencodeSessionsInStore } from './formats/opencode.js';
|
|
9
|
+
export { opencodeReader, opencodeSessionsInStore, readOpenCodeSessionExport } from './formats/opencode.js';
|
|
10
10
|
export { piReader, piRefForFile } from './formats/pi.js';
|
|
11
11
|
export { kimiReader, kimiRefForFile } from './formats/kimi.js';
|
|
12
12
|
export { factoryReader, factoryRefForFile } from './formats/factory.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tangle-network/harness-sessions",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"description": "One reader for every coding-agent harness's native session (Claude Code, Codex, OpenCode, Pi): store layouts, a normalized session with messages, tool calls, served models, usage and ending errors, and projections to chat messages, OTLP spans and turns",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|