@tinchu0/knitt 0.1.0-beta.3 → 0.1.0-beta.5
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 +6 -0
- package/dist/apps/server/src/http-gateway.js +1 -1
- package/dist/apps/server/src/main.js +6 -2
- package/dist/apps/server/src/server.js +190 -89
- package/dist/packages/adapters/src/cli.js +71 -71
- package/dist/packages/adapters/src/discovery.js +1 -1
- package/dist/packages/adapters/src/handoff.js +13 -8
- package/dist/packages/adapters/src/histories.js +222 -0
- package/dist/packages/adapters/src/history-time.js +10 -0
- package/dist/packages/adapters/src/hooks.js +61 -55
- package/dist/packages/adapters/src/picker.js +2 -1
- package/dist/packages/adapters/src/question-ui.js +37 -0
- package/dist/packages/adapters/src/repository.js +11 -0
- package/dist/packages/adapters/src/scope.js +9 -3
- package/dist/packages/adapters/src/server-name.js +6 -0
- package/dist/packages/adapters/src/transcript.js +2 -0
- package/dist/packages/installer/src/antigravity.js +90 -0
- package/dist/packages/installer/src/cli.js +156 -109
- package/dist/packages/installer/src/connections.js +141 -103
- package/dist/packages/installer/src/runtime.js +58 -53
- package/dist/scripts/connector-setup.js +1 -1
- package/dist/skills/continue-chat/SKILL.md +3 -1
- package/dist/skills/knit/SKILL.md +19 -0
- package/docs/clients/connector-setup.md +2 -2
- package/docs/clients/npm-installation.md +26 -19
- package/docs/clients/support-matrix.md +49 -0
- package/docs/usage.md +169 -0
- package/package.json +5 -3
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
import { recordCaptureStatus, readCaptureStatuses, pendingForSession, } from
|
|
2
|
-
import { execFileSync } from
|
|
3
|
-
import { mkdirSync } from
|
|
4
|
-
import { dirname, resolve } from
|
|
5
|
-
import { capture, flush, recallInstruction, selectionInstruction, } from
|
|
6
|
-
import { resolveScope, resolveConnection } from
|
|
7
|
-
import { captureTranscript } from
|
|
8
|
-
import { MemoryStore } from
|
|
1
|
+
import { recordCaptureStatus, readCaptureStatuses, pendingForSession, } from './status.js';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { mkdirSync } from 'node:fs';
|
|
4
|
+
import { dirname, resolve } from 'node:path';
|
|
5
|
+
import { capture, flush, recallInstruction, selectionInstruction, } from './hooks.js';
|
|
6
|
+
import { resolveScope, resolveConnection } from './scope.js';
|
|
7
|
+
import { captureTranscript } from './transcript.js';
|
|
8
|
+
import { MemoryStore } from '../../memory/src/store.js';
|
|
9
9
|
const [action, client] = process.argv.slice(2);
|
|
10
10
|
let captureSession;
|
|
11
11
|
let capturedAt = null;
|
|
12
|
-
let captureState =
|
|
12
|
+
let captureState = 'unobserved';
|
|
13
13
|
let selectedSessionToken;
|
|
14
|
-
const statusDirectory = process.env.MEMORY_QUEUE ?? resolve(
|
|
14
|
+
const statusDirectory = process.env.MEMORY_QUEUE ?? resolve('.memory/queue');
|
|
15
15
|
try {
|
|
16
|
-
if (![
|
|
17
|
-
throw new Error(
|
|
16
|
+
if (!['claude', 'codex', 'antigravity'].includes(client ?? ''))
|
|
17
|
+
throw new Error('Choose claude, codex or antigravity');
|
|
18
18
|
let payload = {};
|
|
19
|
-
if (action ===
|
|
19
|
+
if (action === 'capture' || action === 'recall') {
|
|
20
20
|
const chunks = [];
|
|
21
21
|
let bytes = 0;
|
|
22
22
|
for await (const chunk of process.stdin) {
|
|
23
23
|
bytes += chunk.length;
|
|
24
24
|
if (bytes > 1024 * 1024)
|
|
25
|
-
throw new Error(
|
|
25
|
+
throw new Error('Hook input exceeds 1 MiB');
|
|
26
26
|
chunks.push(chunk);
|
|
27
27
|
}
|
|
28
|
-
const text = Buffer.concat(chunks).toString(
|
|
28
|
+
const text = Buffer.concat(chunks).toString('utf8').trim();
|
|
29
29
|
if (text) {
|
|
30
30
|
const parsed = JSON.parse(text);
|
|
31
|
-
if (!parsed || typeof parsed !==
|
|
32
|
-
throw new Error(
|
|
31
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
32
|
+
throw new Error('Hook input must be an object');
|
|
33
33
|
payload = parsed;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
let scope;
|
|
37
37
|
let selectedSessionId;
|
|
38
|
-
const conversationMode = process.env.MEMORY_CONVERSATION_MODE ===
|
|
39
|
-
if (conversationMode && action !==
|
|
38
|
+
const conversationMode = process.env.MEMORY_CONVERSATION_MODE === '1';
|
|
39
|
+
if (conversationMode && action !== 'flush') {
|
|
40
40
|
const { repoId, workspace } = resolveConnection(payload);
|
|
41
41
|
const nativeSessionId = payload.session_id ?? payload.conversationId;
|
|
42
|
-
if (typeof nativeSessionId !==
|
|
43
|
-
throw new Error(
|
|
42
|
+
if (typeof nativeSessionId !== 'string' || !nativeSessionId)
|
|
43
|
+
throw new Error('A native session ID is required');
|
|
44
44
|
selectedSessionId = nativeSessionId;
|
|
45
45
|
captureSession = { repoId, client, nativeSessionId };
|
|
46
|
-
const path = process.env.MEMORY_DB ?? resolve(
|
|
46
|
+
const path = process.env.MEMORY_DB ?? resolve('.memory/events.sqlite');
|
|
47
47
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
48
48
|
const store = new MemoryStore(path, { conversationMode: true });
|
|
49
49
|
try {
|
|
50
|
-
if (process.env.MEMORY_CAPTURE_NEW_SESSIONS !==
|
|
50
|
+
if (process.env.MEMORY_CAPTURE_NEW_SESSIONS !== '1' &&
|
|
51
51
|
!store.sessionSelection(repoId, nativeSessionId)) {
|
|
52
|
-
console.log(action ===
|
|
52
|
+
console.log(action === 'recall'
|
|
53
53
|
? JSON.stringify(selectionInstruction(client, repoId, nativeSessionId, workspace))
|
|
54
|
-
:
|
|
54
|
+
: '{}');
|
|
55
55
|
process.exit(0);
|
|
56
56
|
}
|
|
57
57
|
const git = (args) => {
|
|
58
58
|
try {
|
|
59
|
-
return (execFileSync(
|
|
59
|
+
return (execFileSync('git', args, {
|
|
60
60
|
cwd: workspace,
|
|
61
|
-
encoding:
|
|
62
|
-
stdio: [
|
|
61
|
+
encoding: 'utf8',
|
|
62
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
63
63
|
}).trim() || undefined);
|
|
64
64
|
}
|
|
65
65
|
catch {
|
|
66
66
|
return undefined;
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
|
-
const branch = git([
|
|
69
|
+
const branch = git(['symbolic-ref', '--short', 'HEAD']), commit = git(['rev-parse', 'HEAD']);
|
|
70
70
|
const conversation = store.ensureSession({ repoId, client, nativeSessionId }, { workspace, branch, commit });
|
|
71
|
-
if (action ===
|
|
72
|
-
process.env.MEMORY_LAUNCH_FEATURE?.startsWith(
|
|
71
|
+
if (action === 'recall' &&
|
|
72
|
+
process.env.MEMORY_LAUNCH_FEATURE?.startsWith('conversation:'))
|
|
73
73
|
store.linkSource({ repoId, client, nativeSessionId }, process.env.MEMORY_LAUNCH_FEATURE);
|
|
74
|
-
if (action ===
|
|
74
|
+
if (action === 'recall') {
|
|
75
75
|
selectedSessionToken = store.issueSessionToken(captureSession);
|
|
76
76
|
if (!readCaptureStatuses(statusDirectory).some((s) => s.session.repoId === repoId &&
|
|
77
77
|
s.session.client === client &&
|
|
@@ -82,7 +82,7 @@ try {
|
|
|
82
82
|
lastDeliveryAt: null,
|
|
83
83
|
pending: pendingForSession(statusDirectory, captureSession),
|
|
84
84
|
lastErrorCode: null,
|
|
85
|
-
captureState:
|
|
85
|
+
captureState: 'unobserved',
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
88
|
scope = {
|
|
@@ -96,21 +96,21 @@ try {
|
|
|
96
96
|
store.close();
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
else if (process.env.MEMORY_SESSION_SELECTION ===
|
|
100
|
-
action !==
|
|
99
|
+
else if (process.env.MEMORY_SESSION_SELECTION === '1' &&
|
|
100
|
+
action !== 'flush') {
|
|
101
101
|
const { repoId, workspace } = resolveConnection(payload);
|
|
102
102
|
const sessionId = payload.session_id ?? payload.conversationId;
|
|
103
|
-
if (typeof sessionId !==
|
|
104
|
-
throw new Error(
|
|
103
|
+
if (typeof sessionId !== 'string' || !sessionId)
|
|
104
|
+
throw new Error('A native session ID is required for chat selection');
|
|
105
105
|
selectedSessionId = sessionId;
|
|
106
|
-
const path = process.env.MEMORY_DB ?? resolve(
|
|
106
|
+
const path = process.env.MEMORY_DB ?? resolve('.memory/events.sqlite');
|
|
107
107
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
108
108
|
const store = new MemoryStore(path);
|
|
109
109
|
let selection;
|
|
110
110
|
try {
|
|
111
111
|
selection = store.sessionSelection(repoId, sessionId);
|
|
112
112
|
if (!selection && process.env.MEMORY_LAUNCH_FEATURE) {
|
|
113
|
-
store.selectSession(repoId, sessionId, process.env.MEMORY_LAUNCH_FEATURE, process.env.MEMORY_LAUNCH_TITLE ??
|
|
113
|
+
store.selectSession(repoId, sessionId, process.env.MEMORY_LAUNCH_FEATURE, process.env.MEMORY_LAUNCH_TITLE ?? 'Selected chat');
|
|
114
114
|
selection = store.sessionSelection(repoId, sessionId);
|
|
115
115
|
}
|
|
116
116
|
}
|
|
@@ -118,11 +118,11 @@ try {
|
|
|
118
118
|
store.close();
|
|
119
119
|
}
|
|
120
120
|
if (!selection) {
|
|
121
|
-
if (action ===
|
|
122
|
-
console.log(JSON.stringify(selectionInstruction(client, repoId, sessionId, workspace)));
|
|
121
|
+
if (action === 'recall')
|
|
122
|
+
console.log(JSON.stringify(selectionInstruction(client, repoId, sessionId, workspace, process.env.MEMORY_MCP_SERVER)));
|
|
123
123
|
else {
|
|
124
|
-
console.error(
|
|
125
|
-
console.log(
|
|
124
|
+
console.error('Shared memory: no chat selected for this session; capture skipped');
|
|
125
|
+
console.log('{}');
|
|
126
126
|
}
|
|
127
127
|
process.exit(0);
|
|
128
128
|
}
|
|
@@ -130,19 +130,19 @@ try {
|
|
|
130
130
|
}
|
|
131
131
|
else
|
|
132
132
|
scope = resolveScope(payload);
|
|
133
|
-
if (action ===
|
|
134
|
-
console.log(JSON.stringify(recallInstruction(client, scope, process.env.MEMORY_WORKSPACE, selectedSessionId, selectedSessionToken)));
|
|
133
|
+
if (action === 'recall') {
|
|
134
|
+
console.log(JSON.stringify(recallInstruction(client, scope, process.env.MEMORY_WORKSPACE, selectedSessionId, selectedSessionToken, process.env.MEMORY_MCP_SERVER)));
|
|
135
135
|
}
|
|
136
|
-
else if (action ===
|
|
136
|
+
else if (action === 'capture' || action === 'flush') {
|
|
137
137
|
const directory = statusDirectory;
|
|
138
|
-
if (action ===
|
|
138
|
+
if (action === 'capture') {
|
|
139
139
|
const transcriptPath = payload.transcript_path ?? payload.transcriptPath;
|
|
140
|
-
if (typeof transcriptPath ===
|
|
140
|
+
if (typeof transcriptPath === 'string' && transcriptPath) {
|
|
141
141
|
const report = captureTranscript({
|
|
142
142
|
directory,
|
|
143
143
|
client: client,
|
|
144
144
|
scope,
|
|
145
|
-
sessionId: String(payload.session_id ?? payload.conversationId ??
|
|
145
|
+
sessionId: String(payload.session_id ?? payload.conversationId ?? ''),
|
|
146
146
|
path: transcriptPath,
|
|
147
147
|
...(conversationMode ? { session: captureSession } : {}),
|
|
148
148
|
});
|
|
@@ -151,17 +151,17 @@ try {
|
|
|
151
151
|
capturedAt = new Date().toISOString();
|
|
152
152
|
captureState =
|
|
153
153
|
report.remainingBytes > 0
|
|
154
|
-
?
|
|
154
|
+
? 'partial'
|
|
155
155
|
: report.events > 0
|
|
156
|
-
?
|
|
156
|
+
? 'captured'
|
|
157
157
|
: report.records > 0
|
|
158
|
-
?
|
|
159
|
-
:
|
|
158
|
+
? 'no_supported_text'
|
|
159
|
+
: 'no_new_text';
|
|
160
160
|
}
|
|
161
161
|
else {
|
|
162
162
|
capture(directory, client, scope, payload, conversationMode ? captureSession : undefined);
|
|
163
163
|
capturedAt = new Date().toISOString();
|
|
164
|
-
captureState =
|
|
164
|
+
captureState = 'captured';
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
const touched = new Map();
|
|
@@ -174,13 +174,13 @@ try {
|
|
|
174
174
|
if (queuedSession) {
|
|
175
175
|
if (event.repoId !== queuedSession.repoId ||
|
|
176
176
|
event.sessionId !== queuedSession.nativeSessionId ||
|
|
177
|
-
!event.source?.startsWith(queuedSession.client +
|
|
178
|
-
throw new Error(
|
|
177
|
+
!event.source?.startsWith(queuedSession.client + ':'))
|
|
178
|
+
throw new Error('Queued evidence identity mismatch');
|
|
179
179
|
const id = JSON.stringify(queuedSession);
|
|
180
180
|
if (!touched.has(id))
|
|
181
181
|
touched.set(id, { session: queuedSession, delivered: false });
|
|
182
182
|
}
|
|
183
|
-
const path = process.env.MEMORY_DB ?? resolve(
|
|
183
|
+
const path = process.env.MEMORY_DB ?? resolve('.memory/events.sqlite');
|
|
184
184
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
185
185
|
const store = new MemoryStore(path, {
|
|
186
186
|
conversationMode: !!queuedSession,
|
|
@@ -198,7 +198,7 @@ try {
|
|
|
198
198
|
touched.get(JSON.stringify(queuedSession)).delivered = true;
|
|
199
199
|
});
|
|
200
200
|
for (const { session, delivered } of touched.values()) {
|
|
201
|
-
const ownCapture = action ===
|
|
201
|
+
const ownCapture = action === 'capture' &&
|
|
202
202
|
captureSession &&
|
|
203
203
|
JSON.stringify(session) === JSON.stringify(captureSession);
|
|
204
204
|
const previous = readCaptureStatuses(directory).find((s) => JSON.stringify(s.session) === JSON.stringify(session));
|
|
@@ -208,30 +208,30 @@ try {
|
|
|
208
208
|
lastCaptureAt: ownCapture ? capturedAt : null,
|
|
209
209
|
lastDeliveryAt: delivered ? new Date().toISOString() : null,
|
|
210
210
|
pending,
|
|
211
|
-
lastErrorCode: pending ?
|
|
211
|
+
lastErrorCode: pending ? 'DELIVERY_PENDING' : null,
|
|
212
212
|
lastAttemptAt: ownCapture ? new Date().toISOString() : null,
|
|
213
213
|
captureState: ownCapture
|
|
214
214
|
? captureState
|
|
215
|
-
: (previous?.captureState ??
|
|
215
|
+
: (previous?.captureState ?? 'unobserved'),
|
|
216
216
|
});
|
|
217
217
|
}
|
|
218
218
|
console.error(JSON.stringify(report));
|
|
219
|
-
console.log(
|
|
219
|
+
console.log('{}');
|
|
220
220
|
}
|
|
221
221
|
else
|
|
222
|
-
throw new Error(
|
|
222
|
+
throw new Error('Choose capture, recall or flush');
|
|
223
223
|
}
|
|
224
224
|
catch (error) {
|
|
225
|
-
if (action ===
|
|
225
|
+
if (action === 'capture' && captureSession) {
|
|
226
226
|
try {
|
|
227
227
|
recordCaptureStatus(statusDirectory, {
|
|
228
228
|
session: captureSession,
|
|
229
229
|
lastCaptureAt: capturedAt,
|
|
230
230
|
lastDeliveryAt: null,
|
|
231
231
|
pending: pendingForSession(statusDirectory, captureSession),
|
|
232
|
-
lastErrorCode:
|
|
232
|
+
lastErrorCode: 'CAPTURE_FAILED',
|
|
233
233
|
lastAttemptAt: new Date().toISOString(),
|
|
234
|
-
captureState:
|
|
234
|
+
captureState: 'error',
|
|
235
235
|
});
|
|
236
236
|
}
|
|
237
237
|
catch {
|
|
@@ -239,21 +239,21 @@ catch (error) {
|
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
241
|
// Hook failure must not prevent the user's coding turn.
|
|
242
|
-
console.error(
|
|
242
|
+
console.error('Shared memory: ' +
|
|
243
243
|
(error instanceof Error ? error.message : String(error)));
|
|
244
|
-
if (action ===
|
|
245
|
-
const message =
|
|
244
|
+
if (action === 'recall' && process.env.MEMORY_WORKSPACE) {
|
|
245
|
+
const message = 'Memory handoff blocked. ' +
|
|
246
246
|
(error instanceof Error ? error.message : String(error));
|
|
247
247
|
console.log(JSON.stringify({
|
|
248
248
|
continue: false,
|
|
249
249
|
systemMessage: message,
|
|
250
250
|
hookSpecificOutput: {
|
|
251
|
-
hookEventName:
|
|
251
|
+
hookEventName: 'SessionStart',
|
|
252
252
|
additionalContext: message +
|
|
253
|
-
|
|
253
|
+
' Do not continue imported work or edit another project from this chat.',
|
|
254
254
|
},
|
|
255
255
|
}));
|
|
256
256
|
}
|
|
257
257
|
else
|
|
258
|
-
console.log(
|
|
258
|
+
console.log('{}');
|
|
259
259
|
}
|
|
@@ -90,7 +90,7 @@ export function importDiscovered(store, repoId, source, directory) {
|
|
|
90
90
|
const queue = join(directory, createHash("sha256").update(source.id).digest("hex"));
|
|
91
91
|
if (source.client === "antigravity") {
|
|
92
92
|
const report = importConversation({
|
|
93
|
-
conversation: { ...source, id: source.sourceId },
|
|
93
|
+
conversation: { ...source, id: source.sourceId, client: "antigravity" },
|
|
94
94
|
repoId,
|
|
95
95
|
directory: queue,
|
|
96
96
|
store,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { canonicalWorkspace } from "./repository.js";
|
|
2
|
+
import { historyTimestamp } from "./history-time.js";
|
|
1
3
|
import { execFileSync } from "node:child_process";
|
|
2
4
|
import { existsSync, realpathSync } from "node:fs";
|
|
3
5
|
import { resolve, join, relative } from "node:path";
|
|
@@ -48,12 +50,12 @@ export function findConversations(roots, workspace) {
|
|
|
48
50
|
if (!/^[A-Za-z0-9_-]{1,128}$/.test(id))
|
|
49
51
|
continue;
|
|
50
52
|
for (const directory of workspaces(String(row.workspace_uris))) {
|
|
51
|
-
if (workspace &&
|
|
53
|
+
if (workspace && canonicalWorkspace(directory) !== canonicalWorkspace(workspace) && (!expectedRepository || commonDirectory(directory) !== expectedRepository))
|
|
52
54
|
continue;
|
|
53
55
|
const conversation = { id, title: String(row.title), workspace: directory, root, modified: String(row.last_modified_time) };
|
|
54
56
|
const key = id + ":" + directory;
|
|
55
57
|
const previous = found.get(key);
|
|
56
|
-
if (!previous || conversation.modified > previous.modified)
|
|
58
|
+
if (!previous || historyTimestamp(conversation.modified) > historyTimestamp(previous.modified))
|
|
57
59
|
found.set(key, conversation);
|
|
58
60
|
}
|
|
59
61
|
}
|
|
@@ -62,7 +64,7 @@ export function findConversations(roots, workspace) {
|
|
|
62
64
|
db.close();
|
|
63
65
|
}
|
|
64
66
|
}
|
|
65
|
-
return [...found.values()].sort((a, b) => b.modified
|
|
67
|
+
return [...found.values()].sort((a, b) => historyTimestamp(b.modified) - historyTimestamp(a.modified));
|
|
66
68
|
}
|
|
67
69
|
export function selectConversation(conversations, query) {
|
|
68
70
|
const matches = conversations.filter(c => c.id === query || c.title.toLowerCase() === query.toLowerCase());
|
|
@@ -74,17 +76,18 @@ export function selectConversation(conversations, query) {
|
|
|
74
76
|
}
|
|
75
77
|
export function importConversation(options) {
|
|
76
78
|
const { conversation, repoId, directory, store } = options;
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
79
|
+
const client = conversation.client ?? "antigravity";
|
|
80
|
+
const featureId = client + ":" + conversation.id;
|
|
81
|
+
const scope = { repoId, featureId, ...(conversation.branch ? { branch: conversation.branch } : {}) };
|
|
82
|
+
const transcript = conversation.transcript ?? join(conversation.root, "brain", conversation.id, ".system_generated/logs/transcript.jsonl");
|
|
80
83
|
let source = "transcript";
|
|
81
84
|
let captured = 0;
|
|
82
85
|
if (existsSync(transcript)) {
|
|
83
|
-
if (relative(realpathSync(conversation.root), realpathSync(transcript)).startsWith(".."))
|
|
86
|
+
if (relative(realpathSync.native(conversation.root), realpathSync.native(transcript)).startsWith(".."))
|
|
84
87
|
throw new Error("Transcript escapes the source data directory");
|
|
85
88
|
let previousOffset = -1;
|
|
86
89
|
for (;;) {
|
|
87
|
-
const report = captureTranscript({ directory, client
|
|
90
|
+
const report = captureTranscript({ directory, client, scope, sessionId: conversation.id, path: transcript });
|
|
88
91
|
captured += report.events;
|
|
89
92
|
if (report.remainingBytes === 0 || report.offset === previousOffset)
|
|
90
93
|
break;
|
|
@@ -92,6 +95,8 @@ export function importConversation(options) {
|
|
|
92
95
|
}
|
|
93
96
|
}
|
|
94
97
|
else {
|
|
98
|
+
if (client !== "antigravity")
|
|
99
|
+
throw new Error("Selected conversation transcript is unavailable. Refresh the history list.");
|
|
95
100
|
source = "conversation_index";
|
|
96
101
|
const index = new DatabaseSync(join(conversation.root, "conversation_summaries.db"), { readOnly: true });
|
|
97
102
|
const queue = openQueue(directory);
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { canonicalWorkspace } from './repository.js';
|
|
2
|
+
import { historyTimestamp as timestamp } from './history-time.js';
|
|
3
|
+
import { closeSync, existsSync, openSync, readSync, readdirSync, realpathSync, statSync } from 'node:fs';
|
|
4
|
+
import { isAbsolute, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { homedir } from 'node:os';
|
|
6
|
+
import { execFileSync } from 'node:child_process';
|
|
7
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
8
|
+
import { findConversations } from './handoff.js';
|
|
9
|
+
export function defaultHistoryRoots(env = process.env) {
|
|
10
|
+
if (env.MEMORY_HISTORY_ROOTS) {
|
|
11
|
+
const parsed = JSON.parse(env.MEMORY_HISTORY_ROOTS);
|
|
12
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
13
|
+
throw new Error('MEMORY_HISTORY_ROOTS must be an object');
|
|
14
|
+
const roots = parsed;
|
|
15
|
+
for (const client of ['antigravity', 'codex', 'claude'])
|
|
16
|
+
if (!Array.isArray(roots[client]) || !roots[client].every(v => typeof v === 'string'))
|
|
17
|
+
throw new Error('History roots must specify arrays for antigravity, codex and claude');
|
|
18
|
+
return roots;
|
|
19
|
+
}
|
|
20
|
+
// Preserve explicit source-root overrides for isolated/legacy installations.
|
|
21
|
+
if (env.MEMORY_SOURCE_ROOTS) {
|
|
22
|
+
const roots = JSON.parse(env.MEMORY_SOURCE_ROOTS);
|
|
23
|
+
if (!Array.isArray(roots) || !roots.every(v => typeof v === 'string'))
|
|
24
|
+
throw new Error('MEMORY_SOURCE_ROOTS must be a string array');
|
|
25
|
+
return { antigravity: roots, codex: [], claude: [] };
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
antigravity: ['antigravity', 'antigravity-cli', 'antigravity-ide'].map(name => join(homedir(), '.gemini', name)),
|
|
29
|
+
codex: [env.CODEX_HOME ?? join(homedir(), '.codex')],
|
|
30
|
+
claude: [env.CLAUDE_CONFIG_DIR ?? join(homedir(), '.claude')],
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const idValid = (id) => typeof id === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(id);
|
|
34
|
+
const object = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
|
|
35
|
+
function inside(root, path) {
|
|
36
|
+
if (!existsSync(path))
|
|
37
|
+
return false;
|
|
38
|
+
const delta = relative(realpathSync.native(root), realpathSync.native(path));
|
|
39
|
+
return delta !== '..' && !delta.startsWith('..' + (process.platform === 'win32' ? '\\' : '/')) && !isAbsolute(delta);
|
|
40
|
+
}
|
|
41
|
+
function files(directory, recursive = false) {
|
|
42
|
+
if (!existsSync(directory))
|
|
43
|
+
return [];
|
|
44
|
+
const found = [];
|
|
45
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
46
|
+
const path = join(directory, entry.name);
|
|
47
|
+
if (entry.isFile() && entry.name.endsWith('.jsonl'))
|
|
48
|
+
found.push(path);
|
|
49
|
+
else if (recursive && entry.isDirectory())
|
|
50
|
+
found.push(...files(path, true));
|
|
51
|
+
}
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
54
|
+
// Discovery reads metadata from bounded head/tail windows, never whole API blobs.
|
|
55
|
+
// Importing the selected chat later reads the complete supported transcript text.
|
|
56
|
+
function metadata(path) {
|
|
57
|
+
const fd = openSync(path, 'r');
|
|
58
|
+
try {
|
|
59
|
+
const size = statSync(path).size, window = 256 * 1024;
|
|
60
|
+
const records = [];
|
|
61
|
+
const starts = size <= 2 * window ? [0] : [0, size - window];
|
|
62
|
+
for (const start of starts) {
|
|
63
|
+
const buffer = Buffer.alloc(Math.min(size - start, starts.length === 1 ? 2 * window : window));
|
|
64
|
+
const count = readSync(fd, buffer, 0, buffer.length, start);
|
|
65
|
+
let text = buffer.subarray(0, count).toString('utf8');
|
|
66
|
+
if (start > 0)
|
|
67
|
+
text = text.slice(text.indexOf('\n') + 1);
|
|
68
|
+
if (start + count < size)
|
|
69
|
+
text = text.slice(0, text.lastIndexOf('\n') + 1);
|
|
70
|
+
for (const line of text.split('\n')) {
|
|
71
|
+
try {
|
|
72
|
+
const row = JSON.parse(line);
|
|
73
|
+
if (object(row))
|
|
74
|
+
records.push(row);
|
|
75
|
+
}
|
|
76
|
+
catch { /* Incomplete/unsupported metadata is not conversation evidence. */ }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return records;
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
closeSync(fd);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function mainSession(source) {
|
|
86
|
+
if (object(source))
|
|
87
|
+
return !('subagent' in source);
|
|
88
|
+
if (typeof source !== 'string')
|
|
89
|
+
return true;
|
|
90
|
+
try {
|
|
91
|
+
const parsed = JSON.parse(source);
|
|
92
|
+
return !object(parsed) || !('subagent' in parsed);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return !source.toLowerCase().includes('subagent');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function discoverCodex(root) {
|
|
99
|
+
if (!existsSync(root))
|
|
100
|
+
return [];
|
|
101
|
+
const found = [];
|
|
102
|
+
const databases = readdirSync(root).filter(name => /^state_\d+\.sqlite$/.test(name)).sort((a, b) => Number(b.match(/\d+/)[0]) - Number(a.match(/\d+/)[0]));
|
|
103
|
+
for (const database of databases) {
|
|
104
|
+
let db;
|
|
105
|
+
const indexed = [];
|
|
106
|
+
try {
|
|
107
|
+
db = new DatabaseSync(join(root, database), { readOnly: true });
|
|
108
|
+
db.exec('PRAGMA busy_timeout=1000');
|
|
109
|
+
const columns = new Set(db.prepare('PRAGMA table_info(threads)').all().map(row => String(row.name)));
|
|
110
|
+
if (!['id', 'cwd', 'rollout_path'].every(c => columns.has(c)))
|
|
111
|
+
throw new Error('Unsupported Codex history index schema');
|
|
112
|
+
const selected = ['id', 'cwd', 'rollout_path', 'title', 'name', 'first_user_message', 'updated_at_ms', 'updated_at', 'source', 'thread_source', 'git_branch'].filter(c => columns.has(c));
|
|
113
|
+
for (const row of db.prepare('SELECT ' + selected.join(',') + ' FROM threads').all()) {
|
|
114
|
+
if (!idValid(row.id) || typeof row.cwd !== 'string' || typeof row.rollout_path !== 'string' || !mainSession(row.source))
|
|
115
|
+
continue;
|
|
116
|
+
if (typeof row.thread_source === 'string' && ['guardian_review', 'subagent'].some(v => String(row.thread_source).includes(v)))
|
|
117
|
+
continue;
|
|
118
|
+
const transcript = isAbsolute(row.rollout_path) ? row.rollout_path : resolve(root, row.rollout_path);
|
|
119
|
+
if (!inside(root, transcript))
|
|
120
|
+
continue;
|
|
121
|
+
const updated = timestamp(row.updated_at_ms ?? row.updated_at) || statSync(transcript).mtimeMs;
|
|
122
|
+
indexed.push({ client: 'codex', id: row.id, title: String(row.title || row.name || row.first_user_message || 'Untitled Codex chat').slice(0, 256), workspace: row.cwd, root, transcript,
|
|
123
|
+
modified: new Date(updated).toISOString(), ...(typeof row.git_branch === 'string' && row.git_branch ? { branch: row.git_branch } : {}) });
|
|
124
|
+
}
|
|
125
|
+
if (indexed.length)
|
|
126
|
+
return indexed;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
// A native upgrade may leave a newer database incomplete or locked. Try
|
|
130
|
+
// another supported index before discovering sessions directly.
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
db?.close();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const titles = new Map();
|
|
137
|
+
const index = join(root, 'session_index.jsonl');
|
|
138
|
+
if (existsSync(index))
|
|
139
|
+
for (const row of metadata(index))
|
|
140
|
+
if (idValid(row.id) && typeof row.thread_name === 'string')
|
|
141
|
+
titles.set(row.id, row.thread_name);
|
|
142
|
+
for (const transcript of [...files(join(root, 'sessions'), true), ...files(join(root, 'archived_sessions'), true)]) {
|
|
143
|
+
if (!inside(root, transcript))
|
|
144
|
+
continue;
|
|
145
|
+
const rows = metadata(transcript);
|
|
146
|
+
const session = rows.find(row => row.type === 'session_meta');
|
|
147
|
+
const meta = session?.payload;
|
|
148
|
+
if (!object(meta) || !idValid(meta.id ?? meta.session_id) || typeof meta.cwd !== 'string' || !mainSession(meta.source))
|
|
149
|
+
continue;
|
|
150
|
+
const id = String(meta.id ?? meta.session_id);
|
|
151
|
+
const times = rows.map(row => timestamp(row.timestamp)).filter(Boolean);
|
|
152
|
+
const git = object(meta.git) ? meta.git : {};
|
|
153
|
+
found.push({ client: 'codex', id, title: titles.get(id) ?? 'Untitled Codex chat', workspace: meta.cwd, root, transcript,
|
|
154
|
+
modified: new Date(times.length ? Math.max(...times) : statSync(transcript).mtimeMs).toISOString(), ...(typeof git.branch === 'string' ? { branch: git.branch } : {}) });
|
|
155
|
+
}
|
|
156
|
+
return found;
|
|
157
|
+
}
|
|
158
|
+
function discoverClaude(root) {
|
|
159
|
+
const projects = join(root, 'projects');
|
|
160
|
+
if (!existsSync(projects))
|
|
161
|
+
return [];
|
|
162
|
+
const found = [];
|
|
163
|
+
for (const project of readdirSync(projects, { withFileTypes: true })) {
|
|
164
|
+
if (!project.isDirectory())
|
|
165
|
+
continue;
|
|
166
|
+
for (const transcript of files(join(projects, project.name))) {
|
|
167
|
+
if (!inside(root, transcript))
|
|
168
|
+
continue;
|
|
169
|
+
const rows = metadata(transcript);
|
|
170
|
+
const messages = rows.filter(row => ['user', 'assistant'].includes(String(row.type)) && row.isSidechain !== true);
|
|
171
|
+
if (!messages.length)
|
|
172
|
+
continue;
|
|
173
|
+
const latest = [...messages].reverse().find(row => typeof row.cwd === 'string' && idValid(row.sessionId));
|
|
174
|
+
if (!latest || typeof latest.cwd !== 'string' || !idValid(latest.sessionId))
|
|
175
|
+
continue;
|
|
176
|
+
const titleRow = [...rows].reverse().find(row => (row.type === 'ai-title' && typeof row.aiTitle === 'string') || (row.type === 'custom-title' && typeof row.customTitle === 'string') || (row.type === 'summary' && typeof row.summary === 'string'));
|
|
177
|
+
const message = object(messages[0].message) ? messages[0].message : {};
|
|
178
|
+
const firstText = typeof message.content === 'string' ? message.content : '';
|
|
179
|
+
const times = messages.map(row => timestamp(row.timestamp)).filter(Boolean);
|
|
180
|
+
found.push({ client: 'claude', id: latest.sessionId, title: String(titleRow?.customTitle ?? titleRow?.aiTitle ?? titleRow?.summary ?? (firstText.slice(0, 120) || 'Untitled Claude chat')),
|
|
181
|
+
workspace: latest.cwd, root, transcript, modified: new Date(times.length ? Math.max(...times) : statSync(transcript).mtimeMs).toISOString(),
|
|
182
|
+
...(typeof latest.gitBranch === 'string' && latest.gitBranch ? { branch: latest.gitBranch } : {}) });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return found;
|
|
186
|
+
}
|
|
187
|
+
export function discoverHistories(options) {
|
|
188
|
+
const repositories = new Map();
|
|
189
|
+
const common = (directory) => {
|
|
190
|
+
if (!repositories.has(directory)) {
|
|
191
|
+
try {
|
|
192
|
+
repositories.set(directory, execFileSync('git', ['rev-parse', '--path-format=absolute', '--git-common-dir'], { cwd: directory, encoding: 'utf8', timeout: 1000, stdio: ['ignore', 'pipe', 'ignore'] }).trim());
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
repositories.set(directory, null);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return repositories.get(directory);
|
|
199
|
+
};
|
|
200
|
+
const expected = options.workspace ? common(options.workspace) : null;
|
|
201
|
+
const matches = (directory) => !options.workspace || canonicalWorkspace(directory) === canonicalWorkspace(options.workspace) || (!!expected && common(directory) === expected);
|
|
202
|
+
const found = new Map();
|
|
203
|
+
const add = (conversations) => {
|
|
204
|
+
for (const conversation of conversations) {
|
|
205
|
+
if (conversation.client === options.destination || !matches(conversation.workspace))
|
|
206
|
+
continue;
|
|
207
|
+
const key = conversation.client + ':' + conversation.id;
|
|
208
|
+
const previous = found.get(key);
|
|
209
|
+
if (!previous || timestamp(conversation.modified) > timestamp(previous.modified))
|
|
210
|
+
found.set(key, conversation);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
if (options.destination !== 'antigravity')
|
|
214
|
+
add(findConversations(options.roots.antigravity, options.workspace).map(c => ({ ...c, client: 'antigravity' })));
|
|
215
|
+
if (options.destination !== 'codex')
|
|
216
|
+
for (const root of options.roots.codex)
|
|
217
|
+
add(discoverCodex(root));
|
|
218
|
+
if (options.destination !== 'claude')
|
|
219
|
+
for (const root of options.roots.claude)
|
|
220
|
+
add(discoverClaude(root));
|
|
221
|
+
return [...found.values()].sort((a, b) => timestamp(b.modified) - timestamp(a.modified) || String(a.client).localeCompare(String(b.client)) || a.id.localeCompare(b.id));
|
|
222
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function historyTimestamp(value) {
|
|
2
|
+
if (typeof value === 'number' || (typeof value === 'string' && /^\d+(\.\d+)?$/.test(value))) {
|
|
3
|
+
const number = Number(value);
|
|
4
|
+
return number < 1e12 ? number * 1000 : number;
|
|
5
|
+
}
|
|
6
|
+
if (typeof value !== 'string')
|
|
7
|
+
return 0;
|
|
8
|
+
const time = Date.parse(/^\d{4}-\d\d-\d\d \d\d:/.test(value) && !/[Z+-]\d*:?\d*$/.test(value.slice(10)) ? value.replace(' ', 'T') + 'Z' : value);
|
|
9
|
+
return Number.isFinite(time) ? time : 0;
|
|
10
|
+
}
|