memhouse 0.18.12 → 0.20.1
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/AGENTS.md +22 -151
- package/CHANGELOG.md +263 -29
- package/LICENSE +202 -22
- package/LICENSES/agentlytics-MIT.txt +21 -0
- package/LICENSES/memhouse-MIT-until-0.18.11.txt +22 -0
- package/LICENSES/models.dev-MIT.txt +22 -0
- package/NOTICE +28 -0
- package/README.md +42 -20
- package/SECURITY.md +21 -8
- package/bin/memhouse.js +2223 -311
- package/docs/README.md +11 -9
- package/docs/guides/admin-password.md +37 -10
- package/docs/guides/configuration.md +13 -6
- package/docs/guides/daemons.md +64 -3
- package/docs/guides/import-claude-ai.md +204 -0
- package/docs/guides/install.md +11 -6
- package/docs/guides/invite.md +21 -7
- package/docs/guides/operate.md +114 -7
- package/docs/guides/search-and-resume.md +72 -3
- package/docs/guides/share.md +91 -52
- package/docs/guides/storage.md +44 -4
- package/docs/guides/troubleshooting.md +121 -14
- package/docs/guides/uninstall.md +4 -3
- package/docs/guides/update.md +16 -0
- package/docs/guides/upgrade-to-0.20.md +188 -0
- package/docs/releases/README.md +2 -1
- package/docs/releases/v0.18.10.md +5 -5
- package/docs/releases/v0.18.11.md +3 -3
- package/docs/releases/v0.20.0.md +627 -0
- package/docs/releases/v0.20.1.md +114 -0
- package/docs/tutorials/01-first-house.md +10 -7
- package/docs/tutorials/02-join-a-house.md +24 -15
- package/docs/tutorials/03-share-your-memory.md +40 -31
- package/editors/antigravity.js +1 -80
- package/editors/base.js +463 -9
- package/editors/bridge.js +119 -0
- package/editors/claude.js +273 -105
- package/editors/codex.js +278 -52
- package/editors/copilot.js +1 -70
- package/editors/cursor.js +1 -75
- package/editors/derive-fingerprints.json +18 -0
- package/editors/harness-paths.js +69 -0
- package/editors/index.js +1 -20
- package/editors/normalise/agy.js +910 -0
- package/editors/normalise/copilot-cli.js +321 -0
- package/editors/normalise/cursor-agent.js +852 -0
- package/editors/normalise/cursor.js +474 -0
- package/editors/normalise/devin.js +575 -0
- package/editors/normalise/gemini-cli.js +602 -0
- package/editors/normalise/goose.js +673 -0
- package/editors/normalise/opencode.js +633 -0
- package/editors/normalise/vscode.js +549 -0
- package/editors/normalise/zed.js +473 -0
- package/editors/opencode-v4.js +119 -0
- package/editors/scope.js +14 -3
- package/editors/sessions-log.js +349 -0
- package/editors/sqlite.js +28 -1
- package/editors/vscode.js +1 -72
- package/editors/windsurf.js +1 -143
- package/examples/01-local-one-machine/README.md +24 -19
- package/examples/01-local-one-machine/run.sh +1 -1
- package/examples/02-existing-clickhouse/README.md +30 -22
- package/examples/02-existing-clickhouse/run.sh +5 -4
- package/examples/03-team-house/README.md +44 -37
- package/examples/03-team-house/run.sh +35 -18
- package/examples/04-self-hosted-team/README.md +29 -14
- package/examples/04-self-hosted-team/compose.yml +5 -2
- package/examples/04-self-hosted-team/smoke.sh +26 -12
- package/examples/README.md +8 -4
- package/examples/fixtures/README.md +4 -1
- package/examples/fixtures/make-sessions.js +8 -5
- package/memhouse/admin-secret.js +89 -24
- package/memhouse/argv-secrets.js +64 -0
- package/memhouse/channel.js +49 -9
- package/memhouse/codex-plugin.js +194 -0
- package/memhouse/delivery/AGENT-INSTALL-PROMPT.md +2 -1
- package/memhouse/delivery/AGENT-INSTALL.md +22 -11
- package/memhouse/delivery/INVITATION.md.tpl +44 -45
- package/memhouse/delivery/PROMPT.md +53 -5
- package/memhouse/delivery/UPGRADE.md.tpl +1 -1
- package/memhouse/delivery/codex/AGENTS-BLOCK.md +11 -0
- package/memhouse/delivery/codex/SKILL.md +50 -0
- package/memhouse/delivery/kernel-install.md +17 -9
- package/memhouse/delivery/plugin/.claude-plugin/plugin.json +2 -2
- package/memhouse/delivery/plugin/reference/HOUSE.md +168 -41
- package/memhouse/delivery/plugin/skills/access/SKILL.md +13 -5
- package/memhouse/delivery/plugin/skills/admin/SKILL.md +14 -6
- package/memhouse/delivery/plugin/skills/house/SKILL.md +25 -8
- package/memhouse/delivery/plugin/skills/recall/SKILL.md +66 -8
- package/memhouse/delivery/plugin/skills/sql/SKILL.md +66 -7
- package/memhouse/deploy.js +1 -1
- package/memhouse/flags.js +35 -18
- package/memhouse/help.js +37 -0
- package/memhouse/host.js +82 -2
- package/memhouse/house/HOUSE.md +2 -2
- package/memhouse/house/convert-profile.js +638 -0
- package/memhouse/house/convert-v3.js +742 -0
- package/memhouse/house/doctor-v3.js +324 -0
- package/memhouse/house/host-alias.js +85 -0
- package/memhouse/house/house.js +3 -3
- package/memhouse/house/migrate.js +2 -2
- package/memhouse/house/profile-dedupe.js +183 -0
- package/memhouse/house/profile-edges.js +554 -0
- package/memhouse/house/profile-house.js +430 -0
- package/memhouse/house/profile-rollup.js +190 -0
- package/memhouse/house/q-cli.js +863 -0
- package/memhouse/house/queries-v3.js +1013 -0
- package/memhouse/house/reconcile-v3.js +192 -0
- package/memhouse/house/records-to-profile.js +174 -0
- package/memhouse/house/records-to-rows.js +169 -0
- package/memhouse/house/relocate.js +2 -1
- package/memhouse/house/schema-choice.js +121 -0
- package/memhouse/house/schema-profile.js +290 -0
- package/memhouse/house/schema-profile.sql.tpl +398 -0
- package/memhouse/house/schema-v3.js +267 -0
- package/memhouse/house/schema-v3.sql.tpl +213 -0
- package/memhouse/import/claude-ai.js +420 -0
- package/memhouse/import/import.js +372 -0
- package/memhouse/import/pyjson.js +206 -0
- package/memhouse/import/records.js +64 -0
- package/memhouse/import/writer-profile.js +192 -0
- package/memhouse/import/writer-v3.js +240 -0
- package/memhouse/import/zip.js +112 -0
- package/memhouse/invitation.js +102 -0
- package/memhouse/invitefile.js +5 -2
- package/memhouse/plugin-copies.js +176 -0
- package/memhouse/provision.js +7 -2
- package/memhouse/release-hint.js +24 -0
- package/memhouse/resume.js +4 -4
- package/memhouse/server/api-v3.js +632 -0
- package/memhouse/server/dashboard-auth.js +181 -0
- package/memhouse/server/dashboard-port.js +91 -0
- package/memhouse/server/queries.js +30 -0
- package/memhouse/server/server.js +118 -20
- package/memhouse/service.js +1 -1
- package/memhouse/share.js +687 -1
- package/memhouse/shipper/adapter-capture.js +756 -0
- package/memhouse/shipper/capture.js +2571 -0
- package/memhouse/shipper/dedupe-profile.js +237 -0
- package/memhouse/shipper/dedupe-v3.js +1027 -0
- package/memhouse/shipper/forks.js +233 -0
- package/memhouse/shipper/log-adapter.js +219 -0
- package/memhouse/shipper/maintenance.js +49 -0
- package/memhouse/shipper/normalise-run.js +148 -0
- package/memhouse/shipper/passlog.js +68 -5
- package/memhouse/shipper/profile-capture.js +903 -0
- package/memhouse/shipper/ship.js +91 -8
- package/memhouse/shipper/snapshots.js +193 -0
- package/memhouse/shipper/source-roots.js +111 -0
- package/memhouse/version-notice.js +66 -0
- package/package.json +14 -7
- package/public/THIRD_PARTY_NOTICES.txt +2 -2
- package/public/assets/index-CcNnhfM3.js +78 -0
- package/public/index.html +1 -1
- package/ui/package-lock.json +56 -37
- package/ui/package.json +1 -0
- package/ui/src/App.jsx +29 -14
- package/ui/src/components/ChatSidebar.jsx +69 -12
- package/ui/src/lib/api.js +76 -0
- package/ui/src/lib/dashboard-token.js +41 -0
- package/ui/src/main.jsx +22 -5
- package/ui/src/pages/Dashboard.jsx +22 -36
- package/ui/third-party-notices.js +119 -0
- package/ui/vite.config.js +5 -1
- package/docs/releases/v0.18.12.md +0 -59
- package/memhouse/COMPETITION.md +0 -221
- package/memhouse/DESIGN.md +0 -222
- package/memhouse/RESUME-AND-UPDATE.md +0 -182
- package/public/assets/index-CnWzrjF3.js +0 -78
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// v4 wave 2: Gemini CLI → normalised session log records (docs/design/sessions-log.md, plus the
|
|
3
|
+
// wave-1 format additions: state, hidden, source, children, lifecycle).
|
|
4
|
+
//
|
|
5
|
+
// Store: <GEMINI_CLI_HOME|~>/.gemini/ (GEMINI_CLI_HOME relocates the whole store).
|
|
6
|
+
// - tmp/<slug>/chats/session-<YYYY-MM-DDTHH-MM>-[<i>-]<sid8>.jsonl: one session per file, an
|
|
7
|
+
// append-only, event-sourced log. Line 1 is the header {sessionId, projectHash, startTime,
|
|
8
|
+
// lastUpdated, kind: main | subagent}. Then plain message records {id, timestamp, type, content, …},
|
|
9
|
+
// plus `$set` (shallow merge of session fields), `$patch` {updates by id, removeIds, orderIds} and
|
|
10
|
+
// `$rewindTo` (a tail rollback). Pre-0.6x `session-*.json` files are one snapshot object; the CLI
|
|
11
|
+
// migrates one to `<name>.jsonl` (same sessionId) when it resumes it.
|
|
12
|
+
// - tmp/<slug>/chats/<main session id, sanitised>/<agentId>.jsonl: a sub-agent's own session (kind
|
|
13
|
+
// subagent; source-only). Every sub-agent of a tree sits under the MAIN session's directory.
|
|
14
|
+
// - Not sessions, never opened: tmp/<slug>/checkpoint-<tag>.json (a /chat save snapshot; carries
|
|
15
|
+
// `authType`), checkpoints/, logs.json, tool-outputs/, history/, *.unreadable-<ts> backups.
|
|
16
|
+
// - Read by list() besides the chat files' prefixes: projects.json and tmp/<slug>/.project_root (the
|
|
17
|
+
// cwd fallback, carried in the entry as a plain string).
|
|
18
|
+
// Spec: docs/design/adapter-specs/gemini-cli.md (repository; Gemini CLI 0.65.0), the
|
|
19
|
+
// sessions-model v0 annex row and conformance cases, and the 0.65.0 bundle's chatRecordingService
|
|
20
|
+
// (createJsonlRecordAccumulator, rewindTo, updateMessagesFromHistory, recordCompletedToolCalls).
|
|
21
|
+
//
|
|
22
|
+
// Rules:
|
|
23
|
+
// - session key: the header's sessionId (uuid; reuse impossible, incarnation ''). A slug directory
|
|
24
|
+
// can be reused by another workspace, so nothing keys on the slug or the file name;
|
|
25
|
+
// - fold, as the CLI's own reader does: a record id written again replaces the record (last wins) and
|
|
26
|
+
// keeps its first position; a removed id written again goes to the end; orderIds moves the listed
|
|
27
|
+
// messages to the end, in that order;
|
|
28
|
+
// - `$rewindTo X` and `$patch.removeIds` are append-only retractions (sessions-model §2.6 (a)): the
|
|
29
|
+
// messages stay in the log with state 'retracted', at their place, and a lifecycle record follows
|
|
30
|
+
// at the op's place. `$rewindTo X` retracts X itself and everything after it (the CLI: "all messages
|
|
31
|
+
// from (and including) the specified ID onwards are removed"); an X the fold does not hold retracts
|
|
32
|
+
// every message. Its lifecycle record is {event: 'rewind', message_ids: the retracted ids (X first),
|
|
33
|
+
// through: the last message kept}. A `$patch` gives one {event: 'edit'} record listing what it
|
|
34
|
+
// retracted and the messages whose content its `updates` revised (a no-op revision, such as a
|
|
35
|
+
// string content re-sent as [{text}], is not an edit). Only content that leaves the store vanishes;
|
|
36
|
+
// a `$set.messages` snapshot (legacy) is the one fold op that drops messages outright;
|
|
37
|
+
// - `$set` merges session fields: `summary` is the title; `lastUpdated` and `memoryScratchpad` are
|
|
38
|
+
// volatile and change no record;
|
|
39
|
+
// - message id = record id. The injected `<session_context>` user message has the fixed id
|
|
40
|
+
// d04923d38bb0f6017037e74183378ef4 in every session: source 'injected', hidden. So is a
|
|
41
|
+
// `<hook_context>` message;
|
|
42
|
+
// - roles: user → user; gemini → assistant; info and warning → system notice (`native_type` keeps
|
|
43
|
+
// the record type); error → system, status 'error', an error part; any other type → system notice
|
|
44
|
+
// (never dropped);
|
|
45
|
+
// - a gemini record carries its tool calls (toolCalls[] with result[], status success | error |
|
|
46
|
+
// cancelled, `agentId` for a sub-agent call). The CLI also echoes each result as a user record of
|
|
47
|
+
// functionResponse parts: one tool part (part_id = call id), and the echo is not a message. An
|
|
48
|
+
// echo whose call no gemini record holds becomes a role 'tool' message;
|
|
49
|
+
// - parts of a gemini record: thoughts[] → reasoning (`**subject** description`, as the CLI renders
|
|
50
|
+
// it), content → text ('0' for a string, else the content index), then the tool calls. After a
|
|
51
|
+
// `$patch` the content can be an API parts array with `thought` and `functionCall` parts: those are
|
|
52
|
+
// used instead, as the CLI does;
|
|
53
|
+
// - usage per gemini record: tokens {input, output, cached → cache_read, thoughts → reasoning};
|
|
54
|
+
// `tool` → usage.tool (not read yet), `total` has no slot. `input_includes_cache` true,
|
|
55
|
+
// `output_includes_reasoning` false (sessions-model §2.4: Gemini's usageMetadata counts
|
|
56
|
+
// cachedContentTokenCount inside promptTokenCount and thoughtsTokenCount apart from
|
|
57
|
+
// candidatesTokenCount). A stored zero is null (the CLI writes `?? 0`);
|
|
58
|
+
// - display: a user record's `displayContent` (the CLI stores it only when it differs from what the
|
|
59
|
+
// model saw) becomes `display_text` on its first text part; `text` stays what the model saw;
|
|
60
|
+
// - model: the record `model`; there is no effort field;
|
|
61
|
+
// - held back: a partial last line; a trailing gemini record with no text and no tool calls yet (the
|
|
62
|
+
// CLI writes the reply at the end of the stream and adds its calls when they complete), and one
|
|
63
|
+
// whose tool call has no result yet. Once anything follows, it is written ('interrupted' if a
|
|
64
|
+
// call never finished);
|
|
65
|
+
// - links, explicit only: a sub-agent file gives {kind: 'subagent', origin: <its directory>, at: ''}
|
|
66
|
+
// (child side); a parent's tool call `agentId` gives `children` (origin side). No fork link
|
|
67
|
+
// exists (/chat save + resume copies into a new id, with no pointer);
|
|
68
|
+
// - redaction: no secret path is declared for chat records. The one contract path, checkpoint
|
|
69
|
+
// `authType`, sits in files this module never opens.
|
|
70
|
+
|
|
71
|
+
const fs = require('fs');
|
|
72
|
+
const path = require('path');
|
|
73
|
+
const crypto = require('crypto');
|
|
74
|
+
const { dataFolder, defaultFolder, resolveNative } = require('../harness-paths');
|
|
75
|
+
const { placeChildren } = require('../sessions-log');
|
|
76
|
+
|
|
77
|
+
const HARNESS = 'gemini-cli';
|
|
78
|
+
// Bumped when the records change for an unchanged store, so the runner normalises every session again
|
|
79
|
+
// (normalise-run.js). 2: the usage flags.
|
|
80
|
+
const VERSION = 2;
|
|
81
|
+
const SESSION_CONTEXT_ID = 'd04923d38bb0f6017037e74183378ef4';
|
|
82
|
+
// sessions-model §4: the declared secret paths, by file. Checkpoints are never read here.
|
|
83
|
+
const SECRET_PATHS = { 'checkpoint-<tag>.json': [['authType']] };
|
|
84
|
+
const DONE = new Set(['success', 'error', 'cancelled']);
|
|
85
|
+
const INJECTED = /^\s*<(session_context|hook_context)>/;
|
|
86
|
+
|
|
87
|
+
/** The default store root: harness-paths' folder for gemini-cli ($GEMINI_CLI_HOME/.gemini, else ~/.gemini). */
|
|
88
|
+
function storeRootDefault() {
|
|
89
|
+
return defaultFolder(HARNESS);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const readdir = (d) => { try { return fs.readdirSync(d, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)); } catch { return []; } };
|
|
93
|
+
const readText = (f) => { try { return fs.readFileSync(f, 'utf-8'); } catch { return null; } };
|
|
94
|
+
const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
95
|
+
const str = (v) => (typeof v === 'string' ? v : '');
|
|
96
|
+
const ms = (iso) => { const t = Date.parse(iso); return Number.isFinite(t) ? t : null; };
|
|
97
|
+
const num = (v) => { const x = Number(v); return Number.isFinite(x) && x > 0 ? Math.trunc(x) : null; };
|
|
98
|
+
const sha256 = (buf) => crypto.createHash('sha256').update(buf).digest('hex');
|
|
99
|
+
|
|
100
|
+
/** A header line: the session's metadata, not a message or an op. */
|
|
101
|
+
function isHeader(r) {
|
|
102
|
+
return isObj(r) && typeof r.sessionId === 'string' && typeof r.id !== 'string' && !('$set' in r) && !('$patch' in r) && !('$rewindTo' in r);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// list() reads only a chat file's prefix: 4 KB, grown (for a .jsonl) only until the first newline.
|
|
106
|
+
const HEAD_CHUNK = 4096;
|
|
107
|
+
const HEAD_MAX = 1 << 20; // a first line longer than this is no header
|
|
108
|
+
const unquote = (s) => { try { return JSON.parse(`"${s}"`); } catch { return ''; } };
|
|
109
|
+
const WS = new Set([' ', '\t', '\n', '\r']);
|
|
110
|
+
|
|
111
|
+
/** The end of the JSON string that opens at `i` (the index of its closing quote), or -1 if it is cut. */
|
|
112
|
+
function stringEnd(text, i) {
|
|
113
|
+
for (let j = i + 1; j < text.length; j++) {
|
|
114
|
+
if (text[j] === '\\') j++;
|
|
115
|
+
else if (text[j] === '"') return j;
|
|
116
|
+
}
|
|
117
|
+
return -1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The string values of `keys` at the TOP level of the JSON object a prefix opens, by a scan that
|
|
122
|
+
* tracks strings and depth: a key inside nested content, or inside a string, never matches. Works on
|
|
123
|
+
* a cut prefix (what lies past the cut is simply not seen); the text is never parsed whole.
|
|
124
|
+
*/
|
|
125
|
+
function topLevelStrings(text, keys) {
|
|
126
|
+
const want = new Set(keys);
|
|
127
|
+
const out = {};
|
|
128
|
+
let depth = 0;
|
|
129
|
+
let expectKey = false;
|
|
130
|
+
for (let i = 0; i < text.length; i++) {
|
|
131
|
+
const c = text[i];
|
|
132
|
+
if (c === '"') {
|
|
133
|
+
const end = stringEnd(text, i);
|
|
134
|
+
if (end < 0) break;
|
|
135
|
+
const raw = text.slice(i + 1, end);
|
|
136
|
+
i = end;
|
|
137
|
+
if (depth !== 1 || !expectKey) continue;
|
|
138
|
+
expectKey = false;
|
|
139
|
+
let k = end + 1;
|
|
140
|
+
while (k < text.length && WS.has(text[k])) k++;
|
|
141
|
+
if (text[k] !== ':') continue;
|
|
142
|
+
k++;
|
|
143
|
+
while (k < text.length && WS.has(text[k])) k++;
|
|
144
|
+
const key = unquote(raw);
|
|
145
|
+
if (!want.has(key) || key in out || text[k] !== '"') { i = k - 1; continue; }
|
|
146
|
+
const vend = stringEnd(text, k);
|
|
147
|
+
if (vend < 0) break;
|
|
148
|
+
out[key] = unquote(text.slice(k + 1, vend));
|
|
149
|
+
i = vend;
|
|
150
|
+
if (Object.keys(out).length === want.size) break;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (c === '{' || c === '[') { depth++; if (depth === 1) { if (c !== '{') break; expectKey = true; } }
|
|
154
|
+
else if (c === '}' || c === ']') { depth--; if (depth <= 0) break; }
|
|
155
|
+
else if (c === ',' && depth === 1) expectKey = true;
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A file's first bytes: HEAD_CHUNK, or (untilNewline) its first line, null while it has none. */
|
|
161
|
+
function readPrefix(file, untilNewline) {
|
|
162
|
+
let fd;
|
|
163
|
+
try { fd = fs.openSync(file, 'r'); } catch { return null; }
|
|
164
|
+
try {
|
|
165
|
+
let buf = Buffer.alloc(0);
|
|
166
|
+
let size = HEAD_CHUNK;
|
|
167
|
+
for (;;) {
|
|
168
|
+
const chunk = Buffer.alloc(size);
|
|
169
|
+
const n = fs.readSync(fd, chunk, 0, size, buf.length);
|
|
170
|
+
const from = buf.length;
|
|
171
|
+
buf = Buffer.concat([buf, chunk.subarray(0, n)]);
|
|
172
|
+
if (!untilNewline) return buf;
|
|
173
|
+
const nl = buf.indexOf(10, from);
|
|
174
|
+
if (nl >= 0) return buf.subarray(0, nl);
|
|
175
|
+
if (n < size || buf.length >= HEAD_MAX) return null; // no complete first line (still being written), or no header
|
|
176
|
+
size = Math.min(size * 2, HEAD_MAX - buf.length);
|
|
177
|
+
}
|
|
178
|
+
} catch { return null; } finally { try { fs.closeSync(fd); } catch { /* closed */ } }
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* A chat file's sessionId and kind, read at the top level of its prefix; the file is never parsed
|
|
183
|
+
* whole. A .jsonl's header is its first line. A legacy .json is one object whose top-level sessionId
|
|
184
|
+
* comes before its `messages` (the CLI writes it first), so it lies inside the 4 KB prefix.
|
|
185
|
+
*/
|
|
186
|
+
function readHeader(file, format) {
|
|
187
|
+
const buf = readPrefix(file, format !== 'json');
|
|
188
|
+
if (!buf) return null;
|
|
189
|
+
const f = topLevelStrings(buf.toString('utf-8'), ['sessionId', 'kind']);
|
|
190
|
+
if (!f.sessionId) return null;
|
|
191
|
+
return { sessionId: f.sessionId, kind: f.kind || '' };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Sessions in one store (a `.gemini` folder): every chat file under tmp/<slug>/chats/, sub-agent
|
|
196
|
+
* files included, keyed by the header's sessionId. Where two files carry one id, a .jsonl beats a
|
|
197
|
+
* .json (the CLI's migration copy), then the larger file wins.
|
|
198
|
+
*
|
|
199
|
+
* An entry names one absolute file path, its own chat file, and holds no shared file (the capture
|
|
200
|
+
* runner stats every absolute file path in an entry). The cwd fallback is resolved here into the plain
|
|
201
|
+
* `cwd` field: the slug's .project_root, else the projects.json entry that maps to the slug. A
|
|
202
|
+
* session's own records name its cwd first (normalise), because a slug can be reused.
|
|
203
|
+
*/
|
|
204
|
+
function list(storeRoot) {
|
|
205
|
+
const root = path.resolve(storeRoot == null ? defaultFolder(HARNESS) : storeRoot);
|
|
206
|
+
if (!fs.existsSync(root)) return [];
|
|
207
|
+
// The default store's harness_path is harness-paths' data folder (GEMINI_CLI_HOME included), so the
|
|
208
|
+
// bridge→normaliser switch dedupes; an explicit root is its own resolved path.
|
|
209
|
+
const harnessPath = storeRoot == null ? dataFolder(HARNESS) : resolveNative(root);
|
|
210
|
+
let bySlug = null;
|
|
211
|
+
const projectOf = (slug) => {
|
|
212
|
+
if (!bySlug) {
|
|
213
|
+
bySlug = new Map();
|
|
214
|
+
try {
|
|
215
|
+
const pj = JSON.parse(fs.readFileSync(path.join(root, 'projects.json'), 'utf-8'));
|
|
216
|
+
for (const [dir, sl] of Object.entries((pj && pj.projects) || {}).sort()) if (typeof sl === 'string' && !bySlug.has(sl)) bySlug.set(sl, dir);
|
|
217
|
+
} catch { /* no index */ }
|
|
218
|
+
}
|
|
219
|
+
return bySlug.get(slug) || '';
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const found = new Map();
|
|
223
|
+
const consider = (file, format, slug, cwd, parent) => {
|
|
224
|
+
const head = readHeader(file, format);
|
|
225
|
+
if (!head || !head.sessionId) return;
|
|
226
|
+
let size = 0;
|
|
227
|
+
try { size = fs.statSync(file).size; } catch { return; }
|
|
228
|
+
const e = { harness: HARNESS, harness_path: harnessPath, session_id: head.sessionId, file, format, kind: head.kind, parent, slug, cwd, size };
|
|
229
|
+
const prev = found.get(e.session_id);
|
|
230
|
+
const better = !prev || (e.format === 'jsonl' && prev.format !== 'jsonl')
|
|
231
|
+
|| (e.format === prev.format && (e.size > prev.size || (e.size === prev.size && e.file < prev.file)));
|
|
232
|
+
if (better) found.set(e.session_id, e);
|
|
233
|
+
};
|
|
234
|
+
for (const s of readdir(path.join(root, 'tmp'))) {
|
|
235
|
+
if (!s.isDirectory()) continue;
|
|
236
|
+
const slugDir = path.join(root, 'tmp', s.name);
|
|
237
|
+
const chats = path.join(slugDir, 'chats');
|
|
238
|
+
const names = readdir(chats);
|
|
239
|
+
if (!names.length) continue;
|
|
240
|
+
const cwd = (readText(path.join(slugDir, '.project_root')) || '').trim() || projectOf(s.name);
|
|
241
|
+
for (const f of names) {
|
|
242
|
+
const full = path.join(chats, f.name);
|
|
243
|
+
const m = /^session-.+\.(jsonl|json)$/.exec(f.name);
|
|
244
|
+
if (m && f.isFile()) { consider(full, m[1], s.name, cwd, ''); continue; }
|
|
245
|
+
if (!f.isDirectory()) continue;
|
|
246
|
+
// A sub-agent directory, named for the main session's (sanitised) id.
|
|
247
|
+
for (const c of readdir(full)) if (c.isFile() && /\.jsonl$/.test(c.name)) consider(path.join(full, c.name), 'jsonl', s.name, cwd, f.name);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return [...found.values()].map(({ size, ...e }) => e);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Complete lines only (a partial last line is held back); malformed lines are skipped. */
|
|
254
|
+
function parseLines(text) {
|
|
255
|
+
const end = text.lastIndexOf('\n');
|
|
256
|
+
if (end < 0) return [];
|
|
257
|
+
const out = [];
|
|
258
|
+
for (const line of text.slice(0, end).split('\n')) {
|
|
259
|
+
if (!line.trim()) continue;
|
|
260
|
+
try { out.push(JSON.parse(line)); } catch { /* malformed: skipped, the CLI skips it too */ }
|
|
261
|
+
}
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** A PartListUnion as an array of part objects. */
|
|
266
|
+
function partArray(c) {
|
|
267
|
+
if (c == null) return [];
|
|
268
|
+
if (typeof c === 'string') return [{ text: c }];
|
|
269
|
+
if (Array.isArray(c)) return c.map((p) => (typeof p === 'string' ? { text: p } : p)).filter((p) => p && typeof p === 'object');
|
|
270
|
+
return isObj(c) ? [c] : [];
|
|
271
|
+
}
|
|
272
|
+
const textOf = (c) => partArray(c).map((p) => (p.thought ? '' : str(p.text))).join('');
|
|
273
|
+
|
|
274
|
+
/** What a revision changes: the content and each tool call's result. */
|
|
275
|
+
const contentKey = (r) => JSON.stringify([partArray(r.content), Array.isArray(r.toolCalls) ? r.toolCalls.map((t) => (t && t.result) ?? null) : null]);
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Fold the records, keeping a timeline beside the state: every message slot in the order it entered
|
|
279
|
+
* (retracted ones stay), and an op slot where a `$rewindTo` or `$patch` retracted or revised.
|
|
280
|
+
*/
|
|
281
|
+
function fold(records) {
|
|
282
|
+
let header = null;
|
|
283
|
+
const meta = {};
|
|
284
|
+
let title = '';
|
|
285
|
+
let dirs = null;
|
|
286
|
+
let context = null; // the first <session_context> text, as first written
|
|
287
|
+
const slots = [];
|
|
288
|
+
const cur = new Map(); // id → slot, in the CLI's fold order
|
|
289
|
+
const put = (r) => {
|
|
290
|
+
const id = r.id;
|
|
291
|
+
if (id === SESSION_CONTEXT_ID && context === null) context = textOf(r.content);
|
|
292
|
+
const s = cur.get(id);
|
|
293
|
+
if (s) { s.rec = r; return; }
|
|
294
|
+
const slot = { msg: true, id, rec: r, state: 'current' };
|
|
295
|
+
slots.push(slot);
|
|
296
|
+
cur.set(id, slot);
|
|
297
|
+
};
|
|
298
|
+
const retract = (slot) => { slot.state = 'retracted'; cur.delete(slot.id); };
|
|
299
|
+
for (const r of records) {
|
|
300
|
+
if (!isObj(r)) continue;
|
|
301
|
+
if (typeof r.$rewindTo === 'string') {
|
|
302
|
+
const ids = [...cur.keys()];
|
|
303
|
+
const i = ids.indexOf(r.$rewindTo);
|
|
304
|
+
const cut = i < 0 ? ids : ids.slice(i);
|
|
305
|
+
const before = i < 0 ? [] : ids.slice(0, i);
|
|
306
|
+
for (const id of cut) retract(cur.get(id));
|
|
307
|
+
if (cut.length) slots.push({ op: 'rewind', ids: cut, before });
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (isObj(r.$patch)) {
|
|
311
|
+
const p = r.$patch;
|
|
312
|
+
const revised = [];
|
|
313
|
+
const removed = [];
|
|
314
|
+
const ups = [...(typeof p.id === 'string' ? [p] : []), ...(Array.isArray(p.updates) ? p.updates : [])];
|
|
315
|
+
for (const u of ups) {
|
|
316
|
+
const s = isObj(u) && typeof u.id === 'string' ? cur.get(u.id) : null;
|
|
317
|
+
if (!s) continue;
|
|
318
|
+
const was = contentKey(s.rec);
|
|
319
|
+
const rec = JSON.parse(JSON.stringify(s.rec));
|
|
320
|
+
if ('content' in u && u.content !== undefined) rec.content = u.content;
|
|
321
|
+
if (Array.isArray(u.toolCalls) && rec.type === 'gemini' && Array.isArray(rec.toolCalls)) {
|
|
322
|
+
for (const tp of u.toolCalls) {
|
|
323
|
+
const tc = isObj(tp) ? rec.toolCalls.find((t) => t && t.id === tp.id) : null;
|
|
324
|
+
if (tc && 'result' in tp) tc.result = tp.result;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
s.rec = rec;
|
|
328
|
+
if (contentKey(rec) !== was && !revised.includes(u.id)) revised.push(u.id);
|
|
329
|
+
}
|
|
330
|
+
for (const id of Array.isArray(p.removeIds) ? p.removeIds : []) {
|
|
331
|
+
const s = typeof id === 'string' ? cur.get(id) : null;
|
|
332
|
+
if (s) { retract(s); removed.push(id); }
|
|
333
|
+
}
|
|
334
|
+
if (Array.isArray(p.orderIds)) {
|
|
335
|
+
const moved = p.orderIds.filter((id, k) => typeof id === 'string' && cur.has(id) && p.orderIds.indexOf(id) === k).map((id) => cur.get(id));
|
|
336
|
+
if (moved.length) {
|
|
337
|
+
const set = new Set(moved);
|
|
338
|
+
const keep = slots.filter((s) => !set.has(s));
|
|
339
|
+
slots.length = 0;
|
|
340
|
+
slots.push(...keep, ...moved);
|
|
341
|
+
for (const s of moved) { cur.delete(s.id); cur.set(s.id, s); }
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (removed.length || revised.length) slots.push({ op: 'edit', ids: [...removed, ...revised] });
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (typeof r.id === 'string') { put(r); continue; }
|
|
348
|
+
if (isObj(r.$set)) {
|
|
349
|
+
const s = r.$set;
|
|
350
|
+
if (Array.isArray(s.messages)) {
|
|
351
|
+
// A legacy snapshot: what it leaves out is gone from the conversation, not retracted.
|
|
352
|
+
const keep = new Set(s.messages.filter((m) => isObj(m) && typeof m.id === 'string').map((m) => m.id));
|
|
353
|
+
for (const [id, slot] of [...cur]) if (!keep.has(id)) { cur.delete(id); slots.splice(slots.indexOf(slot), 1); }
|
|
354
|
+
for (const m of s.messages) if (isObj(m) && typeof m.id === 'string') put(m);
|
|
355
|
+
}
|
|
356
|
+
if (typeof s.summary === 'string') title = s.summary;
|
|
357
|
+
if (dirs === null && Array.isArray(s.directories)) dirs = s.directories;
|
|
358
|
+
Object.assign(meta, s);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (isHeader(r)) {
|
|
362
|
+
if (!header) header = r;
|
|
363
|
+
if (typeof r.summary === 'string' && r.summary) title = r.summary;
|
|
364
|
+
if (dirs === null && Array.isArray(r.directories)) dirs = r.directories;
|
|
365
|
+
for (const m of Array.isArray(r.messages) ? r.messages : []) if (isObj(m) && typeof m.id === 'string') put(m);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return { header, meta, title, dirs, context, slots, current: cur };
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** The first workspace directory the injected session context names. */
|
|
372
|
+
function contextCwd(text) {
|
|
373
|
+
const m = /\*\*Workspace Directories:\*\*\s*\n\s*-\s+([^\n]+)/.exec(String(text || ''));
|
|
374
|
+
return m ? m[1].trim() : '';
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** A tool result's text, as the model saw it. */
|
|
378
|
+
function resultText(result) {
|
|
379
|
+
const out = [];
|
|
380
|
+
for (const p of partArray(result)) {
|
|
381
|
+
if (isObj(p.functionResponse)) {
|
|
382
|
+
const r = p.functionResponse.response;
|
|
383
|
+
if (r == null) continue;
|
|
384
|
+
if (typeof r === 'string') out.push(r);
|
|
385
|
+
else if (typeof r.output === 'string') out.push(r.output);
|
|
386
|
+
else if (r.error != null) out.push(typeof r.error === 'string' ? r.error : JSON.stringify(r.error));
|
|
387
|
+
else out.push(JSON.stringify(r));
|
|
388
|
+
} else if (typeof p.text === 'string') out.push(p.text);
|
|
389
|
+
}
|
|
390
|
+
return out.join('\n');
|
|
391
|
+
}
|
|
392
|
+
const resultIsError = (result) => partArray(result).some((p) => isObj(p.functionResponse) && isObj(p.functionResponse.response) && p.functionResponse.response.error != null);
|
|
393
|
+
|
|
394
|
+
function filePart(p, partId) {
|
|
395
|
+
if (isObj(p.inlineData)) {
|
|
396
|
+
let bytes = null;
|
|
397
|
+
try { bytes = typeof p.inlineData.data === 'string' ? Buffer.from(p.inlineData.data, 'base64') : null; } catch { bytes = null; }
|
|
398
|
+
return { part_id: partId, kind: 'file', media_type: str(p.inlineData.mimeType), att_sha: bytes ? sha256(bytes) : null, att_len: bytes ? bytes.length : null, ref: '' };
|
|
399
|
+
}
|
|
400
|
+
if (isObj(p.fileData)) return { part_id: partId, kind: 'file', media_type: str(p.fileData.mimeType), att_sha: null, att_len: null, ref: str(p.fileData.fileUri) };
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** One tool part: the call, and its result where the record (or its echo) has one. */
|
|
405
|
+
function toolPart(id, name, args, tc, echo, ts) {
|
|
406
|
+
const has = tc && tc.result != null && partArray(tc.result).length > 0;
|
|
407
|
+
const result = has ? tc.result : (echo ? [echo] : null);
|
|
408
|
+
const st = tc ? str(tc.status) : '';
|
|
409
|
+
const finished = DONE.has(st) || result != null;
|
|
410
|
+
const isError = st === 'error' || (result != null && resultIsError(result));
|
|
411
|
+
return {
|
|
412
|
+
part: {
|
|
413
|
+
part_id: id, kind: 'tool', call_id: id, tool: name, args: isObj(args) ? args : (args == null ? {} : { value: args }),
|
|
414
|
+
output: result != null ? resultText(result) : null, is_error: isError,
|
|
415
|
+
status: st === 'cancelled' ? 'cancelled' : isError ? 'error' : finished ? 'completed' : 'interrupted',
|
|
416
|
+
...(tc && ms(tc.timestamp) ? { ts: ms(tc.timestamp) } : (ts ? { ts } : {})),
|
|
417
|
+
},
|
|
418
|
+
finished,
|
|
419
|
+
agent: tc && typeof tc.agentId === 'string' && tc.agentId ? tc.agentId : '',
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** A gemini record's parts, and whether a tool call is still running. */
|
|
424
|
+
function geminiParts(r, echoes) {
|
|
425
|
+
const parts = [];
|
|
426
|
+
let pending = false;
|
|
427
|
+
const agents = [];
|
|
428
|
+
const ts = ms(r.timestamp);
|
|
429
|
+
const content = partArray(r.content);
|
|
430
|
+
const calls = Array.isArray(r.toolCalls) ? r.toolCalls.filter(isObj) : [];
|
|
431
|
+
const byId = new Map(calls.filter((t) => typeof t.id === 'string').map((t) => [t.id, t]));
|
|
432
|
+
const done = new Set();
|
|
433
|
+
const addTool = (id, name, args, tc) => {
|
|
434
|
+
const t = toolPart(id, name, args, tc, echoes.get(id), ts);
|
|
435
|
+
parts.push(t.part);
|
|
436
|
+
if (!t.finished) pending = true;
|
|
437
|
+
if (t.agent) agents.push(t.agent);
|
|
438
|
+
done.add(id);
|
|
439
|
+
};
|
|
440
|
+
// The CLI's own rule (convertSessionToClientHistory): content holding thought or functionCall parts
|
|
441
|
+
// (the API history a $patch wrote back) is the whole reply; otherwise thoughts, text, then calls.
|
|
442
|
+
if (!content.some((p) => p.thought || p.functionCall)) {
|
|
443
|
+
(Array.isArray(r.thoughts) ? r.thoughts : []).forEach((t, i) => {
|
|
444
|
+
if (!isObj(t)) return;
|
|
445
|
+
const text = str(t.subject) ? `**${t.subject}** ${str(t.description)}` : str(t.description);
|
|
446
|
+
if (text) parts.push({ part_id: `th:${i}`, kind: 'reasoning', text, ...(ms(t.timestamp) ? { ts: ms(t.timestamp) } : {}) });
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
content.forEach((p, j) => {
|
|
450
|
+
if (isObj(p.functionCall)) {
|
|
451
|
+
const fc = p.functionCall;
|
|
452
|
+
const id = typeof fc.id === 'string' && fc.id ? fc.id : `@${r.id}:${j}`;
|
|
453
|
+
if (!done.has(id)) addTool(id, str(fc.name) || str(byId.get(id) && byId.get(id).name), fc.args, byId.get(id));
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (isObj(p.functionResponse)) return;
|
|
457
|
+
const f = filePart(p, String(j));
|
|
458
|
+
if (f) { parts.push(f); return; }
|
|
459
|
+
if (typeof p.text === 'string') {
|
|
460
|
+
if (p.text) parts.push({ part_id: String(j), kind: p.thought ? 'reasoning' : 'text', text: p.text });
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
parts.push({ part_id: String(j), kind: 'notice', text: JSON.stringify(p) });
|
|
464
|
+
});
|
|
465
|
+
calls.forEach((tc, k) => {
|
|
466
|
+
const id = typeof tc.id === 'string' && tc.id ? tc.id : `@${r.id}:${content.length + k}`;
|
|
467
|
+
if (!done.has(id)) addTool(id, str(tc.name), tc.args, tc);
|
|
468
|
+
});
|
|
469
|
+
return { parts, pending, agents };
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Normalise one session. `session` is a list() entry, or `{ session_id, records: [...] }` /
|
|
474
|
+
* `{ session_id, text }` already loaded (tests), with optional `cwd` and `parent` (the sub-agent
|
|
475
|
+
* directory). Returns the log records: the session record first, then messages and lifecycle records
|
|
476
|
+
* in native order; null when the file holds no header. Deterministic: the same files give the same
|
|
477
|
+
* records, byte for byte.
|
|
478
|
+
*/
|
|
479
|
+
function normalise(session) {
|
|
480
|
+
let records;
|
|
481
|
+
if (Array.isArray(session.records)) records = JSON.parse(JSON.stringify(session.records));
|
|
482
|
+
else {
|
|
483
|
+
const text = typeof session.text === 'string' ? session.text : readText(session.file);
|
|
484
|
+
if (text == null) return null;
|
|
485
|
+
const json = session.format === 'json' || (!session.format && session.file && /\.json$/.test(session.file));
|
|
486
|
+
if (json) { try { records = [JSON.parse(text)]; } catch { return null; } } else records = parseLines(text);
|
|
487
|
+
}
|
|
488
|
+
const f = fold(records);
|
|
489
|
+
if (!f.header) return null;
|
|
490
|
+
|
|
491
|
+
// Calls the gemini records hold (a functionResponse for one of them is an echo), and each gemini
|
|
492
|
+
// record's echoed responses: the user records after it, up to the next gemini record.
|
|
493
|
+
const callIds = new Set();
|
|
494
|
+
const echoes = new Map(); // gemini slot → Map(call id → functionResponse part)
|
|
495
|
+
let last = null;
|
|
496
|
+
for (const s of f.slots) {
|
|
497
|
+
if (!s.msg) continue;
|
|
498
|
+
const r = s.rec;
|
|
499
|
+
if (r.type === 'gemini') {
|
|
500
|
+
last = new Map();
|
|
501
|
+
echoes.set(s, last);
|
|
502
|
+
for (const tc of Array.isArray(r.toolCalls) ? r.toolCalls : []) if (isObj(tc) && typeof tc.id === 'string') callIds.add(tc.id);
|
|
503
|
+
for (const p of partArray(r.content)) if (isObj(p.functionCall) && typeof p.functionCall.id === 'string') callIds.add(p.functionCall.id);
|
|
504
|
+
} else if (r.type === 'user' && last) {
|
|
505
|
+
for (const p of partArray(r.content)) if (isObj(p.functionResponse) && typeof p.functionResponse.id === 'string' && !last.has(p.functionResponse.id)) last.set(p.functionResponse.id, p);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// Only the last slot of an id is written, so ids stay unique (a retracted copy never shadows the id
|
|
510
|
+
// written again later).
|
|
511
|
+
const lastSlot = new Map();
|
|
512
|
+
for (const s of f.slots) if (s.msg) lastSlot.set(s.id, s);
|
|
513
|
+
const live = f.slots.filter((s) => !s.op && lastSlot.get(s.id) === s);
|
|
514
|
+
const liveCur = live.filter((s) => s.state === 'current');
|
|
515
|
+
const tail = new Map(liveCur.map((s, i) => [s, i === liveCur.length - 1]));
|
|
516
|
+
|
|
517
|
+
const built = new Map();
|
|
518
|
+
const children = [];
|
|
519
|
+
for (const s of live) {
|
|
520
|
+
const r = s.rec;
|
|
521
|
+
const ts = ms(r.timestamp);
|
|
522
|
+
const base = { k: 'message', message_id: s.id, ts, state: s.state };
|
|
523
|
+
if (r.type === 'gemini') {
|
|
524
|
+
const { parts, pending, agents } = geminiParts(r, echoes.get(s) || new Map());
|
|
525
|
+
for (const a of agents) if (!children.some((c) => c.session_id === a)) children.push({ kind: 'subagent', session_id: a, evidence: 'explicit', after: s.id });
|
|
526
|
+
const waiting = !textOf(r.content).trim() && !parts.some((p) => p.kind === 'tool');
|
|
527
|
+
if (s.state === 'current' && tail.get(s) && (pending || waiting)) continue; // its calls have not completed: held back
|
|
528
|
+
const t = isObj(r.tokens) ? r.tokens : null;
|
|
529
|
+
const usage = t ? { input_includes_cache: true, output_includes_reasoning: false, input: num(t.input), output: num(t.output), cache_read: num(t.cached), cache_write: null, reasoning: num(t.thoughts), cost: null, credits: null, tool: num(t.tool) } : null;
|
|
530
|
+
built.set(s, { ...base, role: 'assistant', status: pending ? 'interrupted' : 'complete', model: str(r.model), effort: '', usage: usage && Object.values(usage).some((v) => typeof v === 'number') ? usage : null, parts });
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
if (r.type === 'user') {
|
|
534
|
+
const parts = [];
|
|
535
|
+
const content = partArray(r.content);
|
|
536
|
+
content.forEach((p, j) => {
|
|
537
|
+
if (isObj(p.functionResponse)) {
|
|
538
|
+
const fr = p.functionResponse;
|
|
539
|
+
if (typeof fr.id === 'string' && callIds.has(fr.id)) return; // the echo of a call a gemini record holds
|
|
540
|
+
const id = typeof fr.id === 'string' && fr.id ? fr.id : `@${s.id}:${j}`;
|
|
541
|
+
parts.push({ part_id: id, kind: 'tool', call_id: id, tool: str(fr.name), args: {}, output: resultText([p]), is_error: resultIsError([p]), status: 'completed' });
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
const fp = filePart(p, String(j));
|
|
545
|
+
if (fp) { parts.push(fp); return; }
|
|
546
|
+
if (typeof p.text === 'string') { if (p.text) parts.push({ part_id: String(j), kind: 'text', text: p.text }); return; }
|
|
547
|
+
parts.push({ part_id: String(j), kind: 'notice', text: JSON.stringify(p) });
|
|
548
|
+
});
|
|
549
|
+
if (r.displayContent !== undefined) {
|
|
550
|
+
const shown = textOf(r.displayContent);
|
|
551
|
+
const first = parts.find((p) => p.kind === 'text');
|
|
552
|
+
if (first && shown !== textOf(r.content)) first.display_text = shown;
|
|
553
|
+
}
|
|
554
|
+
if (!parts.length) continue; // an echo, or nothing at all
|
|
555
|
+
const injected = s.id === SESSION_CONTEXT_ID || INJECTED.test(textOf(r.content));
|
|
556
|
+
const role = parts.every((p) => p.kind === 'tool') ? 'tool' : 'user';
|
|
557
|
+
built.set(s, { ...base, role, status: 'complete', ...(injected ? { source: 'injected', hidden: true } : {}), parts });
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
const text = typeof r.content === 'string' ? r.content : (textOf(r.content) || JSON.stringify(r.content ?? null));
|
|
561
|
+
if (r.type === 'error') built.set(s, { ...base, role: 'system', status: 'error', parts: [{ part_id: '0', kind: 'error', text }] });
|
|
562
|
+
else built.set(s, { ...base, role: 'system', status: 'complete', parts: [{ part_id: '0', kind: 'notice', text, native_type: str(r.type) }] });
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Native order: message slots as they entered, each op where it was written.
|
|
566
|
+
const written = new Set([...built.values()].map((m) => m.message_id));
|
|
567
|
+
const out = [];
|
|
568
|
+
let order = 0;
|
|
569
|
+
for (const s of f.slots) {
|
|
570
|
+
if (s.op) {
|
|
571
|
+
const ids = s.ids.filter((id) => written.has(id));
|
|
572
|
+
if (!ids.length) continue;
|
|
573
|
+
if (s.op === 'rewind') {
|
|
574
|
+
const kept = s.before.filter((id) => written.has(id));
|
|
575
|
+
out.push({ k: 'lifecycle', event: 'rewind', at: null, message_ids: ids, through: kept.length ? kept[kept.length - 1] : null });
|
|
576
|
+
} else out.push({ k: 'lifecycle', event: 'edit', at: null, message_ids: ids });
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
const m = built.get(s);
|
|
580
|
+
if (!m) continue;
|
|
581
|
+
const { k, message_id, role, ts, state, ...rest } = m;
|
|
582
|
+
out.push({ k, message_id, role, ts, order: order++, state, ...rest });
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const head = f.header;
|
|
586
|
+
const parent = str(session.parent);
|
|
587
|
+
const head0 = {
|
|
588
|
+
k: 'session',
|
|
589
|
+
title: f.title,
|
|
590
|
+
cwd: contextCwd(f.context) || (Array.isArray(f.dirs) && str(f.dirs[0])) || str(session.cwd),
|
|
591
|
+
created: ms(head.startTime),
|
|
592
|
+
updated: null, archived: null,
|
|
593
|
+
version: '',
|
|
594
|
+
incarnation: '',
|
|
595
|
+
kind: str(head.kind),
|
|
596
|
+
links: parent && parent !== head.sessionId ? [{ kind: 'subagent', origin: parent, evidence: 'explicit', at: '' }] : [],
|
|
597
|
+
...(children.length ? { children } : {}),
|
|
598
|
+
};
|
|
599
|
+
return placeChildren([head0, ...out]);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
module.exports = { HARNESS, VERSION, SESSION_CONTEXT_ID, SECRET_PATHS, list, normalise, parseLines, fold, storeRootDefault };
|