@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,26 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { antigravityServerName } from '../../adapters/src/server-name.js';
|
|
2
|
+
import { hookCommand, isManagedHook } from './platform.js';
|
|
3
|
+
import { readFileSync, existsSync, mkdirSync, writeFileSync, renameSync, rmSync, realpathSync, } from 'node:fs';
|
|
4
|
+
import { join, dirname, resolve } from 'node:path';
|
|
5
|
+
import { execFileSync } from 'node:child_process';
|
|
6
|
+
import { randomUUID } from 'node:crypto';
|
|
7
|
+
import { parse as parseToml } from 'smol-toml';
|
|
7
8
|
export const clients = [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
'codex',
|
|
10
|
+
'claude-code',
|
|
11
|
+
'antigravity',
|
|
12
|
+
'claude-desktop',
|
|
12
13
|
];
|
|
13
|
-
const begin =
|
|
14
|
-
const statePath =
|
|
15
|
-
const read = (path) => existsSync(path) ? readFileSync(path,
|
|
16
|
-
const dump = (v) => JSON.stringify(v, null, 2) +
|
|
17
|
-
const object = (v) => v !== null && typeof v ===
|
|
14
|
+
const begin = '# BEGIN shared-agent-memory', end = '# END shared-agent-memory';
|
|
15
|
+
const statePath = '.memory/npm-connection.json';
|
|
16
|
+
const read = (path) => existsSync(path) ? readFileSync(path, 'utf8') : null;
|
|
17
|
+
const dump = (v) => JSON.stringify(v, null, 2) + '\n';
|
|
18
|
+
const object = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
18
19
|
const equal = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
19
20
|
export function atomicWrite(path, content) {
|
|
20
21
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
21
|
-
const temporary = path +
|
|
22
|
+
const temporary = path + '.' + randomUUID() + '.tmp';
|
|
22
23
|
try {
|
|
23
|
-
writeFileSync(temporary, content, { mode: 0o600, flag:
|
|
24
|
+
writeFileSync(temporary, content, { mode: 0o600, flag: 'wx' });
|
|
24
25
|
renameSync(temporary, path);
|
|
25
26
|
}
|
|
26
27
|
finally {
|
|
@@ -34,7 +35,7 @@ function restore(before, after, current) {
|
|
|
34
35
|
return before;
|
|
35
36
|
if (object(after) &&
|
|
36
37
|
object(current) &&
|
|
37
|
-
typeof after.command ===
|
|
38
|
+
typeof after.command === 'string' &&
|
|
38
39
|
Array.isArray(after.args))
|
|
39
40
|
return current;
|
|
40
41
|
if (object(after) &&
|
|
@@ -76,37 +77,40 @@ export function readConnectionState(workspace) {
|
|
|
76
77
|
return { schema: 1, files: {} };
|
|
77
78
|
const value = JSON.parse(text);
|
|
78
79
|
if (value.schema !== 1 || !object(value.files))
|
|
79
|
-
throw new Error(
|
|
80
|
+
throw new Error('Unsupported connector state');
|
|
80
81
|
for (const [file, snapshot] of Object.entries(value.files)) {
|
|
81
82
|
if (![
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
'.mcp.json',
|
|
84
|
+
'.codex/config.toml',
|
|
85
|
+
'.codex/hooks.json',
|
|
86
|
+
'.agents/hooks.json',
|
|
87
|
+
'.agents/mcp_config.json',
|
|
88
|
+
'.agents/skills/continue-chat/SKILL.md',
|
|
89
|
+
'.agents/skills/knit/SKILL.md',
|
|
90
|
+
'.claude/settings.local.json',
|
|
91
|
+
'.claude/skills/continue-chat/SKILL.md',
|
|
92
|
+
'.claude/skills/knit/SKILL.md',
|
|
93
|
+
'.memory/connectors/claude-desktop.json',
|
|
94
|
+
'.memory/connectors/antigravity.json',
|
|
91
95
|
].includes(file) ||
|
|
92
96
|
!object(snapshot) ||
|
|
93
|
-
typeof snapshot.after !==
|
|
94
|
-
!(snapshot.before === null || typeof snapshot.before ===
|
|
95
|
-
throw new Error(
|
|
97
|
+
typeof snapshot.after !== 'string' ||
|
|
98
|
+
!(snapshot.before === null || typeof snapshot.before === 'string'))
|
|
99
|
+
throw new Error('Invalid connector state');
|
|
96
100
|
}
|
|
97
101
|
return value;
|
|
98
102
|
}
|
|
99
103
|
export function planConnection(opts) {
|
|
100
|
-
const workspace = realpathSync(opts.workspace), app = resolve(opts.appRoot), node = opts.node ?? process.execPath;
|
|
104
|
+
const workspace = realpathSync.native(opts.workspace), app = resolve(opts.appRoot), node = opts.node ?? process.execPath;
|
|
101
105
|
if (!opts.clients.length ||
|
|
102
106
|
opts.clients.some((c) => !clients.includes(c)))
|
|
103
|
-
throw new Error(
|
|
104
|
-
const extension = existsSync(join(app,
|
|
105
|
-
?
|
|
106
|
-
:
|
|
107
|
-
const server = join(app,
|
|
107
|
+
throw new Error('Choose a supported client');
|
|
108
|
+
const extension = existsSync(join(app, 'apps/server/src/main.js'))
|
|
109
|
+
? 'js'
|
|
110
|
+
: 'ts';
|
|
111
|
+
const server = join(app, 'apps/server/src/main.' + extension), runner = join(app, 'packages/adapters/src/cli.' + extension);
|
|
108
112
|
if (!existsSync(server) || !existsSync(runner))
|
|
109
|
-
throw new Error(
|
|
113
|
+
throw new Error('Runtime files missing');
|
|
110
114
|
const state = readConnectionState(workspace), changes = {}, expected = {};
|
|
111
115
|
const get = (path) => {
|
|
112
116
|
const content = read(join(workspace, path));
|
|
@@ -114,33 +118,38 @@ export function planConnection(opts) {
|
|
|
114
118
|
return content;
|
|
115
119
|
};
|
|
116
120
|
const json = (path) => {
|
|
117
|
-
const v = JSON.parse(get(path) ??
|
|
121
|
+
const v = JSON.parse(get(path) ?? '{}');
|
|
118
122
|
if (!object(v))
|
|
119
|
-
throw new Error(path +
|
|
123
|
+
throw new Error(path + ' must be an object');
|
|
120
124
|
return v;
|
|
121
125
|
};
|
|
122
126
|
const env = {
|
|
123
127
|
MEMORY_DB: resolve(opts.database),
|
|
124
128
|
MEMORY_REPO: opts.repoId,
|
|
125
129
|
MEMORY_WORKSPACE: workspace,
|
|
126
|
-
MEMORY_SESSION_SELECTION:
|
|
130
|
+
MEMORY_SESSION_SELECTION: '1',
|
|
127
131
|
...(opts.captureNewSessions
|
|
128
|
-
? { MEMORY_CONVERSATION_MODE:
|
|
132
|
+
? { MEMORY_CONVERSATION_MODE: '1', MEMORY_CAPTURE_NEW_SESSIONS: '1' }
|
|
129
133
|
: {}),
|
|
130
134
|
};
|
|
131
135
|
const handler = (client, action) => ({
|
|
132
|
-
type:
|
|
136
|
+
type: 'command',
|
|
133
137
|
timeout: 10,
|
|
134
138
|
command: hookCommand({
|
|
135
139
|
...env,
|
|
136
|
-
|
|
137
|
-
|
|
140
|
+
...(client === 'antigravity'
|
|
141
|
+
? { MEMORY_MCP_SERVER: antigravityServerName(workspace) }
|
|
142
|
+
: {}),
|
|
143
|
+
MEMORY_QUEUE: join(dirname(opts.database), 'queue', client),
|
|
144
|
+
MEMORY_MANAGED: 'shared-agent-memory',
|
|
138
145
|
}, node, [runner, action, client]),
|
|
139
146
|
});
|
|
140
|
-
const connection = (desktop = false, client =
|
|
147
|
+
const connection = (desktop = false, client = 'claude') => {
|
|
141
148
|
const clientEnv = {
|
|
142
149
|
...env,
|
|
143
|
-
|
|
150
|
+
MEMORY_CLIENT: client,
|
|
151
|
+
...(client === 'antigravity' && !opts.captureNewSessions ? { MEMORY_PICKER_MODE: 'native_question' } : {}),
|
|
152
|
+
...(desktop ? { MEMORY_CHAT_CLIENT: 'claude-desktop' } : {}),
|
|
144
153
|
...(!desktop && opts.captureNewSessions ? { MEMORY_CLIENT: client } : {}),
|
|
145
154
|
};
|
|
146
155
|
if (desktop) {
|
|
@@ -157,13 +166,13 @@ export function planConnection(opts) {
|
|
|
157
166
|
? location.reduce((v, key) => v?.[key], JSON.parse(prior.after))
|
|
158
167
|
: undefined;
|
|
159
168
|
if (!equal(current, previous))
|
|
160
|
-
throw new Error(
|
|
169
|
+
throw new Error('Unmanaged or modified connector conflict in ' + path);
|
|
161
170
|
}
|
|
162
|
-
function mcp(path, desktop = false, client =
|
|
171
|
+
function mcp(path, desktop = false, client = 'claude') {
|
|
163
172
|
const value = json(path);
|
|
164
173
|
value.mcpServers ??= {};
|
|
165
|
-
owned(path, [
|
|
166
|
-
value.mcpServers[
|
|
174
|
+
owned(path, ['mcpServers', 'shared-memory'], value.mcpServers['shared-memory']);
|
|
175
|
+
value.mcpServers['shared-memory'] = connection(desktop, client);
|
|
167
176
|
changes[path] = dump(value);
|
|
168
177
|
}
|
|
169
178
|
function hooks(path, client) {
|
|
@@ -173,19 +182,19 @@ export function planConnection(opts) {
|
|
|
173
182
|
? JSON.parse(state.files[path].after).hooks
|
|
174
183
|
: {};
|
|
175
184
|
for (const [event, action] of [
|
|
176
|
-
[
|
|
177
|
-
[
|
|
185
|
+
['SessionStart', 'recall'],
|
|
186
|
+
['Stop', 'capture'],
|
|
178
187
|
]) {
|
|
179
188
|
const old = prior?.[event] ?? [];
|
|
180
189
|
const groups = value.hooks[event] ?? [];
|
|
181
190
|
if (!Array.isArray(groups))
|
|
182
|
-
throw new Error(
|
|
191
|
+
throw new Error('Invalid hooks in ' + path);
|
|
183
192
|
// Only remove exact previously generated handlers. Edited handlers remain;
|
|
184
193
|
// reject if they mention our runner to avoid duplicate capture.
|
|
185
194
|
const oldOwned = old.filter((g) => g.hooks?.some((h) => isManagedHook(h.command)));
|
|
186
195
|
const kept = groups.filter((g) => !oldOwned.some((p) => equal(p, g)));
|
|
187
196
|
if (kept.some((g) => g.hooks?.some((h) => isManagedHook(h.command))))
|
|
188
|
-
throw new Error(
|
|
197
|
+
throw new Error('Modified hook conflict in ' + path);
|
|
189
198
|
value.hooks[event] = [...kept, { hooks: [handler(client, action)] }];
|
|
190
199
|
}
|
|
191
200
|
changes[path] = dump(value);
|
|
@@ -193,62 +202,69 @@ export function planConnection(opts) {
|
|
|
193
202
|
function skill(path) {
|
|
194
203
|
const current = get(path);
|
|
195
204
|
if (current && current !== state.files[path]?.after)
|
|
196
|
-
throw new Error(
|
|
197
|
-
changes[path] = readFileSync(join(app,
|
|
205
|
+
throw new Error('Unmanaged or modified skill conflict');
|
|
206
|
+
changes[path] = readFileSync(join(app, 'skills', path.includes('continue-chat') ? 'continue-chat' : 'knit', 'SKILL.md'), 'utf8');
|
|
207
|
+
}
|
|
208
|
+
const hasKnit = existsSync(join(app, 'skills/knit/SKILL.md'));
|
|
209
|
+
const hasContinue = existsSync(join(app, 'skills/continue-chat/SKILL.md'));
|
|
210
|
+
const skillNames = [...(hasKnit ? ['knit'] : []), ...(hasContinue ? ['continue-chat'] : [])];
|
|
211
|
+
if (!skillNames.length)
|
|
212
|
+
throw new Error('Target runtime has no continuation skill');
|
|
213
|
+
for (const name of skillNames) {
|
|
214
|
+
if (opts.clients.some(client => client === 'codex' || client === 'antigravity'))
|
|
215
|
+
skill('.agents/skills/' + name + '/SKILL.md');
|
|
216
|
+
if (opts.clients.includes('claude-code'))
|
|
217
|
+
skill('.claude/skills/' + name + '/SKILL.md');
|
|
198
218
|
}
|
|
199
219
|
for (const client of opts.clients) {
|
|
200
|
-
if (client ===
|
|
201
|
-
mcp(
|
|
202
|
-
hooks(
|
|
203
|
-
skill(".claude/skills/continue-chat/SKILL.md");
|
|
220
|
+
if (client === 'claude-code') {
|
|
221
|
+
mcp('.mcp.json');
|
|
222
|
+
hooks('.claude/settings.local.json', 'claude');
|
|
204
223
|
}
|
|
205
|
-
if (client ===
|
|
206
|
-
mcp(
|
|
207
|
-
if (client ===
|
|
208
|
-
mcp(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
value[
|
|
212
|
-
|
|
213
|
-
|
|
224
|
+
if (client === 'claude-desktop')
|
|
225
|
+
mcp('.memory/connectors/claude-desktop.json', true);
|
|
226
|
+
if (client === 'antigravity') {
|
|
227
|
+
mcp('.agents/mcp_config.json', false, 'antigravity');
|
|
228
|
+
mcp('.memory/connectors/antigravity.json', false, 'antigravity');
|
|
229
|
+
const path = '.agents/hooks.json', value = json(path);
|
|
230
|
+
owned(path, ['shared-memory'], value['shared-memory']);
|
|
231
|
+
value['shared-memory'] = {
|
|
232
|
+
SessionStart: [handler('antigravity', 'recall')],
|
|
233
|
+
Stop: [handler('antigravity', 'capture')],
|
|
214
234
|
};
|
|
215
235
|
changes[path] = dump(value);
|
|
216
236
|
}
|
|
217
|
-
if (client ===
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const path = ".codex/config.toml", current = get(path) ?? "";
|
|
237
|
+
if (client === 'codex') {
|
|
238
|
+
hooks('.codex/hooks.json', 'codex');
|
|
239
|
+
const path = '.codex/config.toml', current = get(path) ?? '';
|
|
221
240
|
const parsed = parseToml(current);
|
|
222
241
|
let base = current;
|
|
223
242
|
if (current.includes(begin)) {
|
|
224
243
|
const previous = state.files[path];
|
|
225
244
|
const block = current.slice(current.indexOf(begin), current.indexOf(end) + end.length);
|
|
226
245
|
if (!previous || !previous.after.includes(block))
|
|
227
|
-
throw new Error(
|
|
228
|
-
base = current.replace(block,
|
|
246
|
+
throw new Error('Modified or unmanaged Codex config conflict');
|
|
247
|
+
base = current.replace(block, '');
|
|
229
248
|
}
|
|
230
|
-
else if (parsed.mcp_servers?.[
|
|
231
|
-
throw new Error(
|
|
249
|
+
else if (parsed.mcp_servers?.['shared-memory'])
|
|
250
|
+
throw new Error('Unmanaged Codex server conflict');
|
|
232
251
|
changes[path] =
|
|
233
252
|
base.trimEnd() +
|
|
234
|
-
|
|
253
|
+
'\n\n' +
|
|
235
254
|
begin +
|
|
236
255
|
'\n[mcp_servers.shared-memory]\ndefault_tools_approval_mode = "writes"\ncommand = ' +
|
|
237
256
|
JSON.stringify(node) +
|
|
238
|
-
|
|
257
|
+
'\nargs = ' +
|
|
239
258
|
JSON.stringify([server]) +
|
|
240
|
-
|
|
259
|
+
'\ncwd = ' +
|
|
241
260
|
JSON.stringify(workspace) +
|
|
242
|
-
|
|
243
|
-
Object.entries({
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
})
|
|
247
|
-
.map(([k, v]) => k + " = " + JSON.stringify(v))
|
|
248
|
-
.join(", ") +
|
|
261
|
+
'\nenv = { ' +
|
|
262
|
+
Object.entries({ ...env, MEMORY_CLIENT: 'codex' })
|
|
263
|
+
.map(([k, v]) => k + ' = ' + JSON.stringify(v))
|
|
264
|
+
.join(', ') +
|
|
249
265
|
' }\ntools.choose_conversation.approval_mode = "approve"\n' +
|
|
250
266
|
end +
|
|
251
|
-
|
|
267
|
+
'\n';
|
|
252
268
|
parseToml(changes[path]);
|
|
253
269
|
}
|
|
254
270
|
}
|
|
@@ -267,7 +283,7 @@ export function applyConnectionPlan(plan) {
|
|
|
267
283
|
// Optimistic concurrency prevents applying a preview over later user edits.
|
|
268
284
|
for (const [path, expected] of Object.entries(plan.expected))
|
|
269
285
|
if (read(join(plan.workspace, path)) !== expected)
|
|
270
|
-
throw new Error(
|
|
286
|
+
throw new Error('Configuration changed since preview: ' + path);
|
|
271
287
|
const written = [];
|
|
272
288
|
try {
|
|
273
289
|
for (const [path, content] of Object.entries(plan.changes)) {
|
|
@@ -289,22 +305,22 @@ export function applyConnectionPlan(plan) {
|
|
|
289
305
|
throw error;
|
|
290
306
|
}
|
|
291
307
|
try {
|
|
292
|
-
const git = execFileSync(
|
|
308
|
+
const git = execFileSync('git', ['rev-parse', '--path-format=absolute', '--git-common-dir'], {
|
|
293
309
|
cwd: plan.workspace,
|
|
294
|
-
encoding:
|
|
295
|
-
stdio: [
|
|
310
|
+
encoding: 'utf8',
|
|
311
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
296
312
|
}).trim();
|
|
297
|
-
const path = join(git,
|
|
313
|
+
const path = join(git, 'info/exclude'), current = read(path) ?? '';
|
|
298
314
|
const entries = [
|
|
299
|
-
|
|
315
|
+
'/.memory/',
|
|
300
316
|
...Object.keys(plan.changes)
|
|
301
|
-
.filter((p) => !p.startsWith(
|
|
302
|
-
.map((p) =>
|
|
317
|
+
.filter((p) => !p.startsWith('.memory/'))
|
|
318
|
+
.map((p) => '/' + p),
|
|
303
319
|
];
|
|
304
320
|
atomicWrite(path, current +
|
|
305
|
-
|
|
306
|
-
entries.filter((p) => !current.split(
|
|
307
|
-
|
|
321
|
+
'\n' +
|
|
322
|
+
entries.filter((p) => !current.split('\n').includes(p)).join('\n') +
|
|
323
|
+
'\n');
|
|
308
324
|
}
|
|
309
325
|
catch {
|
|
310
326
|
/* Non-Git projects can use the planner; CLI validates Git separately. */
|
|
@@ -314,17 +330,39 @@ function unapply(path, snapshot, current) {
|
|
|
314
330
|
const { before, after } = snapshot;
|
|
315
331
|
if (current === null || current === after)
|
|
316
332
|
return current === null ? null : before;
|
|
317
|
-
if (path.endsWith(
|
|
333
|
+
if (path.endsWith('.json')) {
|
|
318
334
|
const restored = restore(before === null ? undefined : JSON.parse(before), JSON.parse(after), JSON.parse(current));
|
|
319
335
|
return restored === undefined ? null : dump(restored);
|
|
320
336
|
}
|
|
321
|
-
if (path.endsWith(
|
|
337
|
+
if (path.endsWith('.toml')) {
|
|
322
338
|
const block = after.slice(after.indexOf(begin), after.indexOf(end) + end.length);
|
|
323
339
|
if (current.includes(block))
|
|
324
|
-
return current.replace(block,
|
|
340
|
+
return current.replace(block, '').trim() || before;
|
|
325
341
|
}
|
|
326
342
|
return current;
|
|
327
343
|
}
|
|
344
|
+
// Verification may await while users add unrelated JSON settings. Undo only our
|
|
345
|
+
// generated values rather than dropping those additions or abandoning rollback.
|
|
346
|
+
export function rollbackConnectionPlan(plan) {
|
|
347
|
+
for (const [path, before] of Object.entries(plan.expected).reverse()) {
|
|
348
|
+
if (!(path in plan.changes))
|
|
349
|
+
continue;
|
|
350
|
+
const after = plan.changes[path], target = join(plan.workspace, path), current = read(target);
|
|
351
|
+
let restored;
|
|
352
|
+
if (current === after)
|
|
353
|
+
restored = before;
|
|
354
|
+
else if (after !== null && current !== null)
|
|
355
|
+
restored = unapply(path, { before, after }, current);
|
|
356
|
+
else
|
|
357
|
+
continue; // A new user-created value owns a file we removed.
|
|
358
|
+
if (restored === current)
|
|
359
|
+
continue;
|
|
360
|
+
if (restored === null)
|
|
361
|
+
rmSync(target, { force: true });
|
|
362
|
+
else
|
|
363
|
+
atomicWrite(target, restored);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
328
366
|
export function planUninstall(workspace) {
|
|
329
367
|
const state = readConnectionState(workspace), changes = {}, expected = {};
|
|
330
368
|
for (const [path, snapshot] of Object.entries(state.files)) {
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import { DatabaseSync } from
|
|
2
|
-
import { homedir } from
|
|
3
|
-
import { join, resolve } from
|
|
4
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, mkdtempSync, writeFileSync, } from
|
|
5
|
-
import { execFileSync } from
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { atomicWrite } from
|
|
9
|
-
export const packageName =
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, mkdtempSync, writeFileSync, } from 'node:fs';
|
|
5
|
+
import { execFileSync } from 'node:child_process';
|
|
6
|
+
import { randomUUID } from 'node:crypto';
|
|
7
|
+
import { npmCommand } from './platform.js';
|
|
8
|
+
import { atomicWrite } from './connections.js';
|
|
9
|
+
export const packageName = '@tinchu0/knitt';
|
|
10
10
|
export function defaultHome() {
|
|
11
11
|
if (process.env.SHARED_MEMORY_HOME)
|
|
12
12
|
return resolve(process.env.SHARED_MEMORY_HOME);
|
|
13
|
-
if (process.platform ===
|
|
14
|
-
return join(homedir(),
|
|
15
|
-
if (process.platform ===
|
|
16
|
-
return join(process.env.LOCALAPPDATA ?? join(homedir(),
|
|
17
|
-
return join(process.env.XDG_DATA_HOME ?? join(homedir(),
|
|
13
|
+
if (process.platform === 'darwin')
|
|
14
|
+
return join(homedir(), 'Library/Application Support/SharedAgentMemory');
|
|
15
|
+
if (process.platform === 'win32')
|
|
16
|
+
return join(process.env.LOCALAPPDATA ?? join(homedir(), 'AppData/Local'), 'SharedAgentMemory');
|
|
17
|
+
return join(process.env.XDG_DATA_HOME ?? join(homedir(), '.local/share'), 'shared-agent-memory');
|
|
18
18
|
}
|
|
19
19
|
export function runtimePaths(home) {
|
|
20
20
|
return {
|
|
21
|
-
data: join(home,
|
|
22
|
-
versions: join(home,
|
|
23
|
-
state: join(home,
|
|
21
|
+
data: join(home, 'data'),
|
|
22
|
+
versions: join(home, 'versions'),
|
|
23
|
+
state: join(home, 'installation.json'),
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
export function readInstallation(home) {
|
|
27
27
|
const path = runtimePaths(home).state;
|
|
28
28
|
if (!existsSync(path))
|
|
29
29
|
return null;
|
|
30
|
-
const value = JSON.parse(readFileSync(path,
|
|
30
|
+
const value = JSON.parse(readFileSync(path, 'utf8'));
|
|
31
31
|
if (value.schema !== 1 ||
|
|
32
32
|
!value.current?.appRoot ||
|
|
33
33
|
!Array.isArray(value.projects))
|
|
34
|
-
throw new Error(
|
|
34
|
+
throw new Error('Invalid installation metadata');
|
|
35
35
|
return value;
|
|
36
36
|
}
|
|
37
37
|
export async function withInstallLock(home, work) {
|
|
38
38
|
mkdirSync(home, { recursive: true, mode: 0o700 });
|
|
39
|
-
const lock = join(home,
|
|
39
|
+
const lock = join(home, 'install.lock');
|
|
40
40
|
try {
|
|
41
41
|
mkdirSync(lock, { mode: 0o700 });
|
|
42
42
|
}
|
|
43
43
|
catch {
|
|
44
|
-
throw new Error(
|
|
44
|
+
throw new Error('Another installation is running, or a stale install.lock remains. Check for a running setup process before removing ' +
|
|
45
45
|
lock);
|
|
46
46
|
}
|
|
47
47
|
try {
|
|
48
|
-
writeFileSync(join(lock,
|
|
48
|
+
writeFileSync(join(lock, 'owner.json'), JSON.stringify({ pid: process.pid, started: new Date().toISOString() }), { mode: 0o600 });
|
|
49
49
|
return await work();
|
|
50
50
|
}
|
|
51
51
|
finally {
|
|
@@ -53,78 +53,83 @@ export async function withInstallLock(home, work) {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
export function installRuntime(home, spec) {
|
|
56
|
-
if (Number(process.versions.node.split(
|
|
57
|
-
throw new Error(
|
|
56
|
+
if (Number(process.versions.node.split('.')[0]) < 24)
|
|
57
|
+
throw new Error('Node.js 24 or newer is required');
|
|
58
58
|
const paths = runtimePaths(home);
|
|
59
59
|
mkdirSync(paths.versions, { recursive: true, mode: 0o700 });
|
|
60
60
|
mkdirSync(paths.data, { recursive: true, mode: 0o700 });
|
|
61
|
-
const staging = mkdtempSync(join(paths.versions,
|
|
61
|
+
const staging = mkdtempSync(join(paths.versions, '.staging-'));
|
|
62
62
|
try {
|
|
63
|
-
writeFileSync(join(staging,
|
|
63
|
+
writeFileSync(join(staging, 'package.json'), JSON.stringify({ name: 'shared-memory-runtime', private: true }), { mode: 0o600 });
|
|
64
64
|
// Arguments are passed directly; no shell interpolation or install lifecycle scripts.
|
|
65
65
|
const npm = npmCommand([
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
'install',
|
|
67
|
+
'--prefix',
|
|
68
68
|
staging,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
'--ignore-scripts',
|
|
70
|
+
'--omit=dev',
|
|
71
|
+
'--no-audit',
|
|
72
|
+
'--no-fund',
|
|
73
|
+
'--save-exact',
|
|
74
|
+
'--',
|
|
75
75
|
spec,
|
|
76
76
|
]);
|
|
77
|
-
execFileSync(npm.command, npm.args, {
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
execFileSync(npm.command, npm.args, {
|
|
78
|
+
encoding: 'utf8',
|
|
79
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
80
|
+
timeout: 180_000,
|
|
81
|
+
});
|
|
82
|
+
const root = join(staging, 'node_modules', packageName);
|
|
83
|
+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
|
80
84
|
if (pkg.name !== packageName ||
|
|
81
85
|
!/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(pkg.version))
|
|
82
|
-
throw new Error(
|
|
86
|
+
throw new Error('Unexpected runtime package');
|
|
83
87
|
for (const file of [
|
|
84
|
-
|
|
85
|
-
|
|
88
|
+
'apps/server/src/main.js',
|
|
89
|
+
'packages/installer/src/cli.js',
|
|
86
90
|
])
|
|
87
|
-
if (!existsSync(join(root,
|
|
88
|
-
throw new Error(
|
|
89
|
-
execFileSync(process.execPath, [join(root,
|
|
90
|
-
const destination = join(paths.versions, pkg.version +
|
|
91
|
+
if (!existsSync(join(root, 'dist', file)))
|
|
92
|
+
throw new Error('Incomplete runtime: ' + file);
|
|
93
|
+
execFileSync(process.execPath, [join(root, 'dist/packages/installer/src/cli.js'), '--version'], { encoding: 'utf8', timeout: 10_000 });
|
|
94
|
+
const destination = join(paths.versions, pkg.version + '-' + randomUUID());
|
|
91
95
|
renameSync(staging, destination);
|
|
92
96
|
return {
|
|
93
97
|
version: pkg.version,
|
|
94
98
|
node: process.execPath,
|
|
95
|
-
appRoot: join(destination,
|
|
99
|
+
appRoot: join(destination, 'node_modules', packageName, 'dist'),
|
|
96
100
|
};
|
|
97
101
|
}
|
|
98
102
|
finally {
|
|
99
103
|
rmSync(staging, { recursive: true, force: true });
|
|
100
104
|
}
|
|
101
105
|
}
|
|
102
|
-
export function activateRuntime(home, current, projects) {
|
|
106
|
+
export function activateRuntime(home, current, projects, antigravityConfig) {
|
|
103
107
|
const previous = readInstallation(home);
|
|
104
108
|
atomicWrite(runtimePaths(home).state, JSON.stringify({
|
|
105
109
|
schema: 1,
|
|
106
110
|
current,
|
|
107
111
|
previous: previous?.current,
|
|
108
112
|
projects,
|
|
109
|
-
|
|
113
|
+
antigravityConfig: antigravityConfig ?? previous?.antigravityConfig,
|
|
114
|
+
}, null, 2) + '\n');
|
|
110
115
|
}
|
|
111
116
|
export function assertRuntimeCompatibility(runtime, projects) {
|
|
112
|
-
const marker = join(runtime.appRoot,
|
|
117
|
+
const marker = join(runtime.appRoot, 'memory-schema.json');
|
|
113
118
|
const supported = existsSync(marker)
|
|
114
|
-
? JSON.parse(readFileSync(marker,
|
|
119
|
+
? JSON.parse(readFileSync(marker, 'utf8')).supportedSchema
|
|
115
120
|
: 0;
|
|
116
121
|
if (!Number.isInteger(supported) || supported < 0)
|
|
117
|
-
throw new Error(
|
|
122
|
+
throw new Error('Invalid runtime schema compatibility metadata');
|
|
118
123
|
for (const database of new Set(projects.map((p) => p.database))) {
|
|
119
124
|
if (!existsSync(database))
|
|
120
125
|
continue;
|
|
121
126
|
const db = new DatabaseSync(database, { readOnly: true });
|
|
122
127
|
try {
|
|
123
|
-
const actual = Number(db.prepare(
|
|
128
|
+
const actual = Number(db.prepare('PRAGMA user_version').get().user_version);
|
|
124
129
|
if (actual > supported)
|
|
125
|
-
throw new Error(
|
|
130
|
+
throw new Error('Runtime is incompatible with memory schema ' +
|
|
126
131
|
actual +
|
|
127
|
-
|
|
132
|
+
'. Use a compatible runtime or explicitly restore a consistent backup; no data was changed.');
|
|
128
133
|
}
|
|
129
134
|
finally {
|
|
130
135
|
db.close();
|
|
@@ -30,7 +30,7 @@ try {
|
|
|
30
30
|
if (!values.preview)
|
|
31
31
|
applyConnectionPlan(plan);
|
|
32
32
|
console.log(client === "claude-code"
|
|
33
|
-
? "Start a fresh Claude Code session in this workspace, approve its MCP connection if prompted, and use /
|
|
33
|
+
? "Start a fresh Claude Code session in this workspace, approve its MCP connection if prompted, and use /knit."
|
|
34
34
|
: "Merge the generated .memory/connectors/claude-desktop.json entry into Claude Desktop's local MCP configuration, restart Claude Desktop, then call start_session and choose_conversation.");
|
|
35
35
|
}
|
|
36
36
|
else {
|
|
@@ -6,7 +6,9 @@ description: Open the shared-memory chat picker and load the conversation the us
|
|
|
6
6
|
<!-- shared-agent-memory -->
|
|
7
7
|
# Continue a chat
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
If the startup hook has no sessionToken, follow the installed `knit` skill instead; that mode requires the actual workspace on every picker call and returns context with acceptance. The flow below applies to opt-in conversation mode with a sessionToken.
|
|
10
|
+
|
|
11
|
+
Use the native session ID, repository and matching MCP server from the memory startup hook. In local conversation mode, also pass the sessionToken capability from that hook to every memory tool. Never invent or reuse another session capability. Immediately call shared-memory's choose_conversation with that sessionId. Let the user select a chat in the form. Do not select a chat on their behalf.
|
|
10
12
|
|
|
11
13
|
On cancellation, stop. After acceptance, call get_context with the selected repoId and the same sessionId. Briefly show the selected title, last checkpoint and next step. Opening the picker does not authorize file edits.
|
|
12
14
|
|