@tinchu0/knitt 0.1.0-beta.5 → 0.1.0-beta.6
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 +25 -27
- package/dist/apps/server/src/cloud-picker.js +245 -0
- package/dist/apps/server/src/onboarding.js +266 -0
- package/dist/apps/server/src/plugin-bootstrap.js +82 -0
- package/dist/apps/server/src/plugin-hook.js +51 -0
- package/dist/apps/server/src/plugin-main.js +10 -0
- package/dist/apps/server/src/server.js +126 -258
- package/dist/packages/adapters/src/cli.js +59 -190
- package/dist/packages/adapters/src/command.js +18 -149
- package/dist/packages/adapters/src/handoff.js +2 -3
- package/dist/packages/adapters/src/histories.js +2 -3
- package/dist/packages/adapters/src/hooks.js +43 -122
- package/dist/packages/adapters/src/launch.js +3 -10
- package/dist/packages/adapters/src/question-ui.js +4 -4
- package/dist/packages/adapters/src/repository.js +5 -32
- package/dist/packages/adapters/src/scope.js +1 -1
- package/dist/packages/adapters/src/server-name.js +1 -2
- package/dist/packages/adapters/src/transcript.js +15 -70
- package/dist/packages/cloud/src/client.js +40 -0
- package/dist/packages/cloud/src/local-sync.js +300 -0
- package/dist/packages/installer/src/antigravity.js +3 -3
- package/dist/packages/installer/src/cli.js +79 -194
- package/dist/packages/installer/src/cloud-cli.js +199 -0
- package/dist/packages/installer/src/connections.js +43 -170
- package/dist/packages/installer/src/runtime.js +10 -72
- package/dist/packages/memory/src/store.js +58 -200
- package/dist/skills/knit/SKILL.md +20 -4
- package/docs/clients/npm-installation.md +17 -13
- package/docs/usage.md +19 -10
- package/package.json +9 -7
- package/dist/apps/server/src/conversation-server.js +0 -203
- package/dist/memory-schema.json +0 -1
- package/dist/packages/adapters/src/discovery.js +0 -128
- package/dist/packages/adapters/src/providers/jsonl.js +0 -105
- package/dist/packages/adapters/src/status.js +0 -81
- package/dist/packages/contracts/src/conversations.js +0 -30
- package/dist/packages/memory/src/checkpoints.js +0 -68
- package/dist/packages/memory/src/context.js +0 -91
- package/dist/packages/memory/src/conversations.js +0 -148
- package/dist/packages/memory/src/migrations.js +0 -55
- package/dist/skills/continue-chat/SKILL.md +0 -15
- package/docs/clients/local-agent-memory.md +0 -50
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { chmodSync, mkdirSync, realpathSync } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
import { credentials, api, CloudError } from "./client.js";
|
|
6
|
+
import { discoverHistories, } from "../../adapters/src/histories.js";
|
|
7
|
+
import { importConversation } from "../../adapters/src/handoff.js";
|
|
8
|
+
import { MemoryStore } from "../../memory/src/store.js";
|
|
9
|
+
export const digest = (value) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
10
|
+
export function canonicalWorkspace(workspace) {
|
|
11
|
+
return realpathSync(resolve(workspace));
|
|
12
|
+
}
|
|
13
|
+
export function syncDatabase(home) {
|
|
14
|
+
const directory = join(home, "cloud");
|
|
15
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
16
|
+
const path = join(directory, "sync.sqlite");
|
|
17
|
+
const db = new DatabaseSync(path);
|
|
18
|
+
chmodSync(path, 0o600);
|
|
19
|
+
db.exec(`PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL;
|
|
20
|
+
CREATE TABLE IF NOT EXISTS settings(key TEXT PRIMARY KEY,value TEXT NOT NULL);
|
|
21
|
+
CREATE TABLE IF NOT EXISTS bindings(key TEXT PRIMARY KEY,origin TEXT NOT NULL,account TEXT NOT NULL,workspace TEXT NOT NULL,project TEXT NOT NULL,enabled INTEGER NOT NULL,roots TEXT NOT NULL);
|
|
22
|
+
CREATE TABLE IF NOT EXISTS uploads(id TEXT PRIMARY KEY,binding TEXT NOT NULL,payload TEXT NOT NULL,delivered INTEGER NOT NULL DEFAULT 0);
|
|
23
|
+
CREATE TABLE IF NOT EXISTS sync_errors(binding TEXT PRIMARY KEY,message TEXT NOT NULL,at TEXT NOT NULL);`);
|
|
24
|
+
db.prepare("INSERT OR IGNORE INTO settings VALUES(?,?)").run("installationId", randomUUID());
|
|
25
|
+
return db;
|
|
26
|
+
}
|
|
27
|
+
export function bindingKey(device, workspace) {
|
|
28
|
+
return digest([
|
|
29
|
+
device.origin,
|
|
30
|
+
device.account.id,
|
|
31
|
+
device.device.id,
|
|
32
|
+
canonicalWorkspace(workspace),
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
export function syncStatus(home, workspace) {
|
|
36
|
+
const device = credentials(home), db = syncDatabase(home);
|
|
37
|
+
try {
|
|
38
|
+
const binding = device
|
|
39
|
+
? db
|
|
40
|
+
.prepare("SELECT project,enabled,roots FROM bindings WHERE key=?")
|
|
41
|
+
.get(bindingKey(device, workspace))
|
|
42
|
+
: undefined;
|
|
43
|
+
const key = device ? bindingKey(device, workspace) : "";
|
|
44
|
+
const pending = db
|
|
45
|
+
.prepare("SELECT COUNT(*) AS count FROM uploads WHERE binding=? AND delivered=0")
|
|
46
|
+
.get(key);
|
|
47
|
+
const error = db
|
|
48
|
+
.prepare("SELECT message,at FROM sync_errors WHERE binding=?")
|
|
49
|
+
.get(key);
|
|
50
|
+
return {
|
|
51
|
+
connected: !!device,
|
|
52
|
+
account: device?.account,
|
|
53
|
+
device: device?.device,
|
|
54
|
+
workspace: canonicalWorkspace(workspace),
|
|
55
|
+
binding: binding
|
|
56
|
+
? {
|
|
57
|
+
projectId: String(binding.project),
|
|
58
|
+
enabled: !!binding.enabled,
|
|
59
|
+
historyRoots: JSON.parse(String(binding.roots)),
|
|
60
|
+
}
|
|
61
|
+
: null,
|
|
62
|
+
pending: Number(pending?.count ?? 0),
|
|
63
|
+
error: error ?? null,
|
|
64
|
+
lastSuccessfulSync: db
|
|
65
|
+
.prepare("SELECT value FROM settings WHERE key=?")
|
|
66
|
+
.get("lastSync:" + key)?.value ?? null,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
db.close();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export function bindWorkspace(home, device, workspace, projectId, roots) {
|
|
74
|
+
const db = syncDatabase(home);
|
|
75
|
+
try {
|
|
76
|
+
const key = bindingKey(device, workspace);
|
|
77
|
+
const previous = db
|
|
78
|
+
.prepare("SELECT project, roots FROM bindings WHERE key=?")
|
|
79
|
+
.get(key);
|
|
80
|
+
if (previous &&
|
|
81
|
+
(previous.project !== projectId ||
|
|
82
|
+
previous.roots !== JSON.stringify(roots))) {
|
|
83
|
+
db.prepare("DELETE FROM uploads WHERE binding=?").run(key);
|
|
84
|
+
}
|
|
85
|
+
db.prepare("INSERT INTO bindings VALUES(?,?,?,?,?,?,?) ON CONFLICT(key) DO UPDATE SET project=excluded.project,enabled=1,roots=excluded.roots").run(bindingKey(device, workspace), device.origin, device.account.id, canonicalWorkspace(workspace), projectId, 1, JSON.stringify(roots));
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
db.close();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export async function controlSync(home, workspace, action) {
|
|
92
|
+
const device = credentials(home);
|
|
93
|
+
if (!device)
|
|
94
|
+
throw new Error("Connect an account first.");
|
|
95
|
+
const db = syncDatabase(home);
|
|
96
|
+
try {
|
|
97
|
+
const key = bindingKey(device, workspace), binding = db.prepare("SELECT * FROM bindings WHERE key=?").get(key);
|
|
98
|
+
if (!binding)
|
|
99
|
+
throw new Error("This workspace has no approved sync mapping.");
|
|
100
|
+
// Disable locally first, including when the server is offline.
|
|
101
|
+
if (action !== "resume")
|
|
102
|
+
db.prepare("UPDATE bindings SET enabled=0 WHERE key=?").run(key);
|
|
103
|
+
await api(device, `/api/private/projects/${encodeURIComponent(String(binding.project))}/control`, { action });
|
|
104
|
+
if (action === "resume")
|
|
105
|
+
db.prepare("UPDATE bindings SET enabled=1 WHERE key=?").run(key);
|
|
106
|
+
if (action === "disconnect") {
|
|
107
|
+
db.prepare("DELETE FROM uploads WHERE binding=?").run(key);
|
|
108
|
+
db.prepare("DELETE FROM bindings WHERE key=?").run(key);
|
|
109
|
+
}
|
|
110
|
+
return { action, enabled: action === "resume" };
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
db.close();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const running = new Map();
|
|
117
|
+
export function syncWorkspace(home, env, workspace) {
|
|
118
|
+
const key = resolve(home) + ":" + canonicalWorkspace(workspace);
|
|
119
|
+
const active = running.get(key);
|
|
120
|
+
if (active)
|
|
121
|
+
return active;
|
|
122
|
+
const promise = performSync(home, env, workspace).finally(() => running.delete(key));
|
|
123
|
+
running.set(key, promise);
|
|
124
|
+
return promise;
|
|
125
|
+
}
|
|
126
|
+
async function performSync(home, _env, workspace) {
|
|
127
|
+
const device = credentials(home);
|
|
128
|
+
if (!device)
|
|
129
|
+
return { skipped: "disconnected" };
|
|
130
|
+
const db = syncDatabase(home), key = bindingKey(device, workspace);
|
|
131
|
+
try {
|
|
132
|
+
const binding = db
|
|
133
|
+
.prepare("SELECT * FROM bindings WHERE key=? AND enabled=1")
|
|
134
|
+
.get(key);
|
|
135
|
+
if (!binding)
|
|
136
|
+
return { skipped: "consent_required_or_paused" };
|
|
137
|
+
await api(device, `/api/private/projects/${encodeURIComponent(String(binding.project))}/status`, {
|
|
138
|
+
status: "syncing",
|
|
139
|
+
pendingCount: Number(db
|
|
140
|
+
.prepare("SELECT COUNT(*) AS count FROM uploads WHERE binding=? AND delivered=0")
|
|
141
|
+
.get(key).count),
|
|
142
|
+
}).catch(() => { });
|
|
143
|
+
const installationId = String(db
|
|
144
|
+
.prepare("SELECT value FROM settings WHERE key=?")
|
|
145
|
+
.get("installationId").value);
|
|
146
|
+
const roots = JSON.parse(String(binding.roots));
|
|
147
|
+
const conversations = discoverHistories({
|
|
148
|
+
roots,
|
|
149
|
+
workspace: canonicalWorkspace(workspace),
|
|
150
|
+
});
|
|
151
|
+
let imported = 0;
|
|
152
|
+
let importErrors = 0;
|
|
153
|
+
for (const conversation of conversations) {
|
|
154
|
+
try {
|
|
155
|
+
const root = realpathSync(conversation.root), agent = conversation.client ?? "antigravity";
|
|
156
|
+
const id = digest([binding.project, agent, conversation.id]);
|
|
157
|
+
const directory = join(home, "cloud", "imports", id);
|
|
158
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
159
|
+
const store = new MemoryStore(join(directory, "events.sqlite"));
|
|
160
|
+
const events = [];
|
|
161
|
+
try {
|
|
162
|
+
const report = importConversation({
|
|
163
|
+
conversation: { ...conversation, branch: undefined },
|
|
164
|
+
repoId: id,
|
|
165
|
+
directory: join(directory, "queue"),
|
|
166
|
+
store,
|
|
167
|
+
});
|
|
168
|
+
let before;
|
|
169
|
+
const ordered = [];
|
|
170
|
+
do {
|
|
171
|
+
const page = store.search({
|
|
172
|
+
repoId: id,
|
|
173
|
+
featureId: report.featureId,
|
|
174
|
+
before,
|
|
175
|
+
limit: 20,
|
|
176
|
+
});
|
|
177
|
+
for (const event of page.matches)
|
|
178
|
+
ordered.push({
|
|
179
|
+
id: event.eventId,
|
|
180
|
+
text: store.evidence(id, event.eventId).content,
|
|
181
|
+
});
|
|
182
|
+
before = page.nextCursor ?? undefined;
|
|
183
|
+
} while (before);
|
|
184
|
+
let role = "assistant";
|
|
185
|
+
ordered.reverse().forEach((event, index) => {
|
|
186
|
+
if (event.text.startsWith("user: "))
|
|
187
|
+
role = "user";
|
|
188
|
+
else if (event.text.startsWith("assistant: ") ||
|
|
189
|
+
event.text.startsWith("model: "))
|
|
190
|
+
role = "assistant";
|
|
191
|
+
events.push({ ...event, sequence: index + 1, role });
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
store.close();
|
|
196
|
+
}
|
|
197
|
+
for (let offset = 0; offset < events.length; offset += 40) {
|
|
198
|
+
const chunk = events.slice(offset, offset + 40), batchId = digest([key, id, chunk]);
|
|
199
|
+
const payload = {
|
|
200
|
+
projectId: String(binding.project),
|
|
201
|
+
batchId,
|
|
202
|
+
conversations: [
|
|
203
|
+
{
|
|
204
|
+
id,
|
|
205
|
+
origin: agent,
|
|
206
|
+
sessionId: conversation.id,
|
|
207
|
+
title: conversation.title,
|
|
208
|
+
provenance: {
|
|
209
|
+
installationId,
|
|
210
|
+
...(conversation.branch
|
|
211
|
+
? { branch: conversation.branch }
|
|
212
|
+
: {}),
|
|
213
|
+
},
|
|
214
|
+
events: chunk,
|
|
215
|
+
},
|
|
216
|
+
],
|
|
217
|
+
};
|
|
218
|
+
db.prepare("INSERT OR IGNORE INTO uploads(id,binding,payload) VALUES(?,?,?)").run(batchId, key, JSON.stringify(payload));
|
|
219
|
+
}
|
|
220
|
+
imported++;
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
importErrors++;
|
|
224
|
+
db.prepare("INSERT OR REPLACE INTO sync_errors VALUES(?,?,?)").run(key, String(error), new Date().toISOString());
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
let delivered = 0;
|
|
228
|
+
// Each persisted batch is replayed exactly after network failures or restart.
|
|
229
|
+
for (const row of db
|
|
230
|
+
.prepare("SELECT id,payload FROM uploads WHERE binding=? AND delivered=0 ORDER BY rowid")
|
|
231
|
+
.all(key)) {
|
|
232
|
+
const current = credentials(home);
|
|
233
|
+
if (!current ||
|
|
234
|
+
current.account.id !== device.account.id ||
|
|
235
|
+
current.device.id !== device.device.id ||
|
|
236
|
+
current.origin !== device.origin)
|
|
237
|
+
break;
|
|
238
|
+
if (!db
|
|
239
|
+
.prepare("SELECT 1 FROM bindings WHERE key=? AND enabled=1 AND project=? AND roots=?")
|
|
240
|
+
.get(key, binding.project, binding.roots))
|
|
241
|
+
break;
|
|
242
|
+
await api(current, "/api/private/sync", JSON.parse(String(row.payload)));
|
|
243
|
+
db.prepare("UPDATE uploads SET delivered=1 WHERE id=?").run(row.id);
|
|
244
|
+
delivered++;
|
|
245
|
+
}
|
|
246
|
+
const pending = Number(db
|
|
247
|
+
.prepare("SELECT COUNT(*) AS count FROM uploads WHERE binding=? AND delivered=0")
|
|
248
|
+
.get(key).count);
|
|
249
|
+
if (!importErrors && pending === 0 && conversations.length > 0) {
|
|
250
|
+
db.prepare("DELETE FROM sync_errors WHERE binding=?").run(key);
|
|
251
|
+
db.prepare("INSERT OR REPLACE INTO settings VALUES(?,?)").run("lastSync:" + key, new Date().toISOString());
|
|
252
|
+
}
|
|
253
|
+
await api(device, `/api/private/projects/${encodeURIComponent(String(binding.project))}/status`, {
|
|
254
|
+
status: importErrors || pending
|
|
255
|
+
? "offline"
|
|
256
|
+
: conversations.length
|
|
257
|
+
? "up_to_date"
|
|
258
|
+
: "waiting",
|
|
259
|
+
pendingCount: Number(db
|
|
260
|
+
.prepare("SELECT COUNT(*) AS count FROM uploads WHERE binding=? AND delivered=0")
|
|
261
|
+
.get(key).count),
|
|
262
|
+
}).catch(() => { });
|
|
263
|
+
return {
|
|
264
|
+
imported,
|
|
265
|
+
delivered,
|
|
266
|
+
importErrors,
|
|
267
|
+
pending: Number(db
|
|
268
|
+
.prepare("SELECT COUNT(*) AS count FROM uploads WHERE binding=? AND delivered=0")
|
|
269
|
+
.get(key).count),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
db.prepare("INSERT OR REPLACE INTO sync_errors VALUES(?,?,?)").run(key, String(error), new Date().toISOString());
|
|
274
|
+
const binding = db
|
|
275
|
+
.prepare("SELECT project FROM bindings WHERE key=?")
|
|
276
|
+
.get(key);
|
|
277
|
+
if (binding)
|
|
278
|
+
await api(device, `/api/private/projects/${encodeURIComponent(String(binding.project))}/status`, {
|
|
279
|
+
status: error instanceof CloudError && [401, 403].includes(error.status)
|
|
280
|
+
? "reconnect"
|
|
281
|
+
: "offline",
|
|
282
|
+
pendingCount: Number(db
|
|
283
|
+
.prepare("SELECT COUNT(*) AS count FROM uploads WHERE binding=? AND delivered=0")
|
|
284
|
+
.get(key).count),
|
|
285
|
+
}).catch(() => { });
|
|
286
|
+
throw error;
|
|
287
|
+
}
|
|
288
|
+
finally {
|
|
289
|
+
db.close();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
export function startAutosync(home, env, workspace) {
|
|
293
|
+
const run = () => {
|
|
294
|
+
void syncWorkspace(home, env, workspace).catch(() => { });
|
|
295
|
+
};
|
|
296
|
+
run();
|
|
297
|
+
const timer = setInterval(run, 30_000);
|
|
298
|
+
timer.unref();
|
|
299
|
+
return () => clearInterval(timer);
|
|
300
|
+
}
|
|
@@ -18,8 +18,8 @@ function entries(opts) {
|
|
|
18
18
|
const name = antigravityServerName(p.workspace);
|
|
19
19
|
return [name, { command: opts.node ?? process.execPath, args: [join(opts.appRoot, 'apps/server/src/main.' + extension)], env: {
|
|
20
20
|
MEMORY_CLIENT: 'antigravity', MEMORY_DB: resolve(p.database), MEMORY_REPO: p.repoId, MEMORY_WORKSPACE: p.workspace,
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
SHARED_MEMORY_HOME: resolve(opts.installationHome), KNITT_HOME: resolve(opts.installationHome),
|
|
22
|
+
MEMORY_SESSION_SELECTION: '1', MEMORY_MCP_SERVER: name, MEMORY_PICKER_MODE: 'native_question',
|
|
23
23
|
} }];
|
|
24
24
|
}));
|
|
25
25
|
}
|
|
@@ -75,7 +75,7 @@ export async function verifyAntigravity(opts) {
|
|
|
75
75
|
throw new Error('Missing or modified server registration');
|
|
76
76
|
await client.connect(new StdioClientTransport({ ...entry, env: { ...process.env, ...entry.env }, stderr: 'pipe' }), { timeout: 10_000 });
|
|
77
77
|
const tools = (await client.listTools(undefined, { timeout: 10_000 })).tools;
|
|
78
|
-
for (const tool of ['choose_conversation', 'get_context', 'get_evidence', 'search_memory',
|
|
78
|
+
for (const tool of ['choose_conversation', 'get_context', 'get_evidence', 'search_memory', 'save_memory']) {
|
|
79
79
|
if (!tools.some(t => t.name === tool))
|
|
80
80
|
throw new Error('Missing tool ' + tool);
|
|
81
81
|
}
|