@letta-ai/letta-code 0.14.4 → 0.14.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/letta.js +3700 -2531
- package/package.json +1 -1
- package/skills/defragmenting-memory/SKILL.md +4 -5
- package/skills/finding-agents/SKILL.md +12 -12
- package/skills/messaging-agents/SKILL.md +19 -19
- package/skills/migrating-memory/SKILL.md +47 -81
- package/skills/searching-messages/SKILL.md +14 -16
- package/skills/syncing-memory-filesystem/SKILL.md +27 -46
- package/skills/finding-agents/scripts/find-agents.ts +0 -177
- package/skills/messaging-agents/scripts/continue-conversation.ts +0 -226
- package/skills/messaging-agents/scripts/start-conversation.ts +0 -229
- package/skills/migrating-memory/scripts/attach-block.ts +0 -230
- package/skills/migrating-memory/scripts/copy-block.ts +0 -261
- package/skills/migrating-memory/scripts/get-agent-blocks.ts +0 -103
- package/skills/searching-messages/scripts/get-messages.ts +0 -230
- package/skills/searching-messages/scripts/search-messages.ts +0 -200
- package/skills/syncing-memory-filesystem/scripts/lib/frontmatter.ts +0 -47
- package/skills/syncing-memory-filesystem/scripts/memfs-diff.ts +0 -430
- package/skills/syncing-memory-filesystem/scripts/memfs-resolve.ts +0 -506
- package/skills/syncing-memory-filesystem/scripts/memfs-status.ts +0 -391
|
@@ -1,430 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env npx tsx
|
|
2
|
-
/**
|
|
3
|
-
* Memory Filesystem Diff
|
|
4
|
-
*
|
|
5
|
-
* Shows the full content of conflicting blocks and files.
|
|
6
|
-
* Writes a formatted markdown diff to a file for review.
|
|
7
|
-
* Analogous to `git diff`.
|
|
8
|
-
*
|
|
9
|
-
* Usage:
|
|
10
|
-
* npx tsx memfs-diff.ts <agent-id>
|
|
11
|
-
*
|
|
12
|
-
* Output: Path to the diff file (or "No conflicts" message)
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
16
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
17
|
-
import { readdir, readFile } from "node:fs/promises";
|
|
18
|
-
import { createRequire } from "node:module";
|
|
19
|
-
import { homedir } from "node:os";
|
|
20
|
-
import { join, normalize, relative } from "node:path";
|
|
21
|
-
import { hashFileBody, READ_ONLY_LABELS } from "./lib/frontmatter";
|
|
22
|
-
|
|
23
|
-
const require = createRequire(import.meta.url);
|
|
24
|
-
const Letta = require("@letta-ai/letta-client")
|
|
25
|
-
.default as typeof import("@letta-ai/letta-client").default;
|
|
26
|
-
|
|
27
|
-
function getApiKey(): string {
|
|
28
|
-
if (process.env.LETTA_API_KEY) {
|
|
29
|
-
return process.env.LETTA_API_KEY;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const settingsPath = join(homedir(), ".letta", "settings.json");
|
|
33
|
-
try {
|
|
34
|
-
const settings = JSON.parse(readFileSync(settingsPath, "utf-8"));
|
|
35
|
-
if (settings.env?.LETTA_API_KEY) {
|
|
36
|
-
return settings.env.LETTA_API_KEY;
|
|
37
|
-
}
|
|
38
|
-
} catch {
|
|
39
|
-
// Settings file doesn't exist or is invalid
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
throw new Error(
|
|
43
|
-
"No LETTA_API_KEY found. Set the env var or run the Letta CLI to authenticate.",
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const MEMORY_FS_STATE_FILE = ".sync-state.json";
|
|
48
|
-
|
|
49
|
-
// Unified sync state format (matches main memoryFilesystem.ts)
|
|
50
|
-
type SyncState = {
|
|
51
|
-
blockHashes: Record<string, string>;
|
|
52
|
-
fileHashes: Record<string, string>;
|
|
53
|
-
blockIds: Record<string, string>;
|
|
54
|
-
lastSync: string | null;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
function hashContent(content: string): string {
|
|
58
|
-
return createHash("sha256").update(content).digest("hex");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// parseFrontmatter/hashFileBody provided by shared helper
|
|
62
|
-
|
|
63
|
-
function getMemoryRoot(agentId: string): string {
|
|
64
|
-
return join(homedir(), ".letta", "agents", agentId, "memory");
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function loadSyncState(agentId: string): SyncState {
|
|
68
|
-
const statePath = join(getMemoryRoot(agentId), MEMORY_FS_STATE_FILE);
|
|
69
|
-
if (!existsSync(statePath)) {
|
|
70
|
-
return {
|
|
71
|
-
blockHashes: {},
|
|
72
|
-
fileHashes: {},
|
|
73
|
-
blockIds: {},
|
|
74
|
-
lastSync: null,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const raw = readFileSync(statePath, "utf-8");
|
|
80
|
-
const parsed = JSON.parse(raw);
|
|
81
|
-
return {
|
|
82
|
-
blockHashes: parsed.blockHashes || {},
|
|
83
|
-
fileHashes: parsed.fileHashes || {},
|
|
84
|
-
blockIds: parsed.blockIds || {},
|
|
85
|
-
lastSync: parsed.lastSync || null,
|
|
86
|
-
};
|
|
87
|
-
} catch {
|
|
88
|
-
return {
|
|
89
|
-
blockHashes: {},
|
|
90
|
-
fileHashes: {},
|
|
91
|
-
blockIds: {},
|
|
92
|
-
lastSync: null,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async function scanMdFiles(
|
|
98
|
-
dir: string,
|
|
99
|
-
baseDir = dir,
|
|
100
|
-
excludeDirs: string[] = [],
|
|
101
|
-
): Promise<string[]> {
|
|
102
|
-
if (!existsSync(dir)) return [];
|
|
103
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
104
|
-
const results: string[] = [];
|
|
105
|
-
for (const entry of entries) {
|
|
106
|
-
const fullPath = join(dir, entry.name);
|
|
107
|
-
if (entry.isDirectory()) {
|
|
108
|
-
if (excludeDirs.includes(entry.name)) continue;
|
|
109
|
-
results.push(...(await scanMdFiles(fullPath, baseDir, excludeDirs)));
|
|
110
|
-
} else if (entry.isFile() && entry.name.endsWith(".md")) {
|
|
111
|
-
results.push(relative(baseDir, fullPath));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return results;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function labelFromPath(relativePath: string): string {
|
|
118
|
-
return relativePath.replace(/\\/g, "/").replace(/\.md$/, "");
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async function readMemoryFiles(
|
|
122
|
-
dir: string,
|
|
123
|
-
excludeDirs: string[] = [],
|
|
124
|
-
): Promise<Map<string, { content: string }>> {
|
|
125
|
-
const files = await scanMdFiles(dir, dir, excludeDirs);
|
|
126
|
-
const entries = new Map<string, { content: string }>();
|
|
127
|
-
for (const rel of files) {
|
|
128
|
-
const label = labelFromPath(rel);
|
|
129
|
-
const content = await readFile(join(dir, rel), "utf-8");
|
|
130
|
-
entries.set(label, { content });
|
|
131
|
-
}
|
|
132
|
-
return entries;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Only memory_filesystem is managed by memfs itself
|
|
136
|
-
const MEMFS_MANAGED_LABELS = new Set(["memory_filesystem"]);
|
|
137
|
-
|
|
138
|
-
interface Conflict {
|
|
139
|
-
label: string;
|
|
140
|
-
fileContent: string;
|
|
141
|
-
blockContent: string;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
interface MetadataChange {
|
|
145
|
-
label: string;
|
|
146
|
-
fileContent: string;
|
|
147
|
-
blockContent: string;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Get the overflow directory following the same pattern as tool output overflow.
|
|
152
|
-
* Pattern: ~/.letta/projects/<project-path>/agent-tools/
|
|
153
|
-
*/
|
|
154
|
-
function getOverflowDirectory(): string {
|
|
155
|
-
const cwd = process.cwd();
|
|
156
|
-
const normalizedPath = normalize(cwd);
|
|
157
|
-
const sanitizedPath = normalizedPath
|
|
158
|
-
.replace(/^[/\\]/, "")
|
|
159
|
-
.replace(/[/\\:]/g, "_")
|
|
160
|
-
.replace(/\s+/g, "_");
|
|
161
|
-
|
|
162
|
-
return join(homedir(), ".letta", "projects", sanitizedPath, "agent-tools");
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
async function findConflicts(agentId: string): Promise<{
|
|
166
|
-
conflicts: Conflict[];
|
|
167
|
-
metadataOnly: MetadataChange[];
|
|
168
|
-
}> {
|
|
169
|
-
const baseUrl = process.env.LETTA_BASE_URL || "https://api.letta.com";
|
|
170
|
-
const client = new Letta({ apiKey: getApiKey(), baseUrl });
|
|
171
|
-
|
|
172
|
-
const root = getMemoryRoot(agentId);
|
|
173
|
-
const systemDir = join(root, "system");
|
|
174
|
-
const detachedDir = root;
|
|
175
|
-
|
|
176
|
-
for (const dir of [root, systemDir]) {
|
|
177
|
-
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Read files from both locations
|
|
181
|
-
const systemFiles = await readMemoryFiles(systemDir);
|
|
182
|
-
const detachedFiles = await readMemoryFiles(detachedDir, ["system", "user"]);
|
|
183
|
-
|
|
184
|
-
// Fetch attached blocks
|
|
185
|
-
const blocksResponse = await client.agents.blocks.list(agentId, {
|
|
186
|
-
limit: 1000,
|
|
187
|
-
});
|
|
188
|
-
const attachedBlocks = Array.isArray(blocksResponse)
|
|
189
|
-
? blocksResponse
|
|
190
|
-
: ((blocksResponse as { items?: unknown[] }).items as Array<{
|
|
191
|
-
id?: string;
|
|
192
|
-
label?: string;
|
|
193
|
-
value?: string;
|
|
194
|
-
read_only?: boolean;
|
|
195
|
-
}>) || [];
|
|
196
|
-
|
|
197
|
-
const systemBlockMap = new Map<
|
|
198
|
-
string,
|
|
199
|
-
{ value: string; id: string; read_only?: boolean }
|
|
200
|
-
>();
|
|
201
|
-
for (const block of attachedBlocks) {
|
|
202
|
-
if (block.label && block.id) {
|
|
203
|
-
systemBlockMap.set(block.label, {
|
|
204
|
-
value: block.value || "",
|
|
205
|
-
id: block.id,
|
|
206
|
-
read_only: block.read_only,
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// Fetch detached blocks via owner tag
|
|
212
|
-
const ownedBlocksResponse = await client.blocks.list({
|
|
213
|
-
tags: [`owner:${agentId}`],
|
|
214
|
-
limit: 1000,
|
|
215
|
-
});
|
|
216
|
-
const ownedBlocks = Array.isArray(ownedBlocksResponse)
|
|
217
|
-
? ownedBlocksResponse
|
|
218
|
-
: ((ownedBlocksResponse as { items?: unknown[] }).items as Array<{
|
|
219
|
-
id?: string;
|
|
220
|
-
label?: string;
|
|
221
|
-
value?: string;
|
|
222
|
-
read_only?: boolean;
|
|
223
|
-
}>) || [];
|
|
224
|
-
|
|
225
|
-
const attachedIds = new Set(attachedBlocks.map((b) => b.id));
|
|
226
|
-
const detachedBlockMap = new Map<
|
|
227
|
-
string,
|
|
228
|
-
{ value: string; id: string; read_only?: boolean }
|
|
229
|
-
>();
|
|
230
|
-
for (const block of ownedBlocks) {
|
|
231
|
-
if (block.label && block.id && !attachedIds.has(block.id)) {
|
|
232
|
-
if (!systemBlockMap.has(block.label)) {
|
|
233
|
-
detachedBlockMap.set(block.label, {
|
|
234
|
-
value: block.value || "",
|
|
235
|
-
id: block.id,
|
|
236
|
-
read_only: block.read_only,
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const lastState = loadSyncState(agentId);
|
|
243
|
-
const conflicts: Conflict[] = [];
|
|
244
|
-
const metadataOnly: MetadataChange[] = [];
|
|
245
|
-
|
|
246
|
-
// Collect all labels
|
|
247
|
-
const allLabels = new Set<string>([
|
|
248
|
-
...systemFiles.keys(),
|
|
249
|
-
...detachedFiles.keys(),
|
|
250
|
-
...systemBlockMap.keys(),
|
|
251
|
-
...detachedBlockMap.keys(),
|
|
252
|
-
...Object.keys(lastState.blockHashes),
|
|
253
|
-
...Object.keys(lastState.fileHashes),
|
|
254
|
-
]);
|
|
255
|
-
|
|
256
|
-
for (const label of [...allLabels].sort()) {
|
|
257
|
-
if (MEMFS_MANAGED_LABELS.has(label)) continue;
|
|
258
|
-
|
|
259
|
-
const systemFile = systemFiles.get(label);
|
|
260
|
-
const detachedFile = detachedFiles.get(label);
|
|
261
|
-
const attachedBlock = systemBlockMap.get(label);
|
|
262
|
-
const detachedBlock = detachedBlockMap.get(label);
|
|
263
|
-
|
|
264
|
-
const fileEntry = systemFile || detachedFile;
|
|
265
|
-
const blockEntry = attachedBlock || detachedBlock;
|
|
266
|
-
|
|
267
|
-
if (!fileEntry || !blockEntry) continue;
|
|
268
|
-
|
|
269
|
-
// read_only blocks are API-authoritative; no conflicts possible
|
|
270
|
-
const effectiveReadOnly =
|
|
271
|
-
!!blockEntry.read_only || READ_ONLY_LABELS.has(label);
|
|
272
|
-
if (effectiveReadOnly) continue;
|
|
273
|
-
|
|
274
|
-
// Full file hash for "file changed" check
|
|
275
|
-
const fileHash = hashContent(fileEntry.content);
|
|
276
|
-
// Body hash for "content matches" check
|
|
277
|
-
const fileBodyHash = hashFileBody(fileEntry.content);
|
|
278
|
-
const blockHash = hashContent(blockEntry.value);
|
|
279
|
-
|
|
280
|
-
const lastFileHash = lastState.fileHashes[label] ?? null;
|
|
281
|
-
const lastBlockHash = lastState.blockHashes[label] ?? null;
|
|
282
|
-
const fileChanged = fileHash !== lastFileHash;
|
|
283
|
-
const blockChanged = blockHash !== lastBlockHash;
|
|
284
|
-
|
|
285
|
-
// Content matches - check for frontmatter-only changes
|
|
286
|
-
if (fileBodyHash === blockHash) {
|
|
287
|
-
if (fileChanged) {
|
|
288
|
-
metadataOnly.push({
|
|
289
|
-
label,
|
|
290
|
-
fileContent: fileEntry.content,
|
|
291
|
-
blockContent: blockEntry.value,
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
continue;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// Conflict only if both changed
|
|
298
|
-
if (fileChanged && blockChanged) {
|
|
299
|
-
conflicts.push({
|
|
300
|
-
label,
|
|
301
|
-
fileContent: fileEntry.content,
|
|
302
|
-
blockContent: blockEntry.value,
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return { conflicts, metadataOnly };
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
function formatDiffFile(
|
|
311
|
-
conflicts: Conflict[],
|
|
312
|
-
metadataOnly: MetadataChange[],
|
|
313
|
-
agentId: string,
|
|
314
|
-
): string {
|
|
315
|
-
const lines: string[] = [
|
|
316
|
-
`# Memory Filesystem Diff`,
|
|
317
|
-
``,
|
|
318
|
-
`Agent: ${agentId}`,
|
|
319
|
-
`Generated: ${new Date().toISOString()}`,
|
|
320
|
-
`Conflicts: ${conflicts.length}`,
|
|
321
|
-
`Metadata-only changes: ${metadataOnly.length}`,
|
|
322
|
-
``,
|
|
323
|
-
`---`,
|
|
324
|
-
``,
|
|
325
|
-
];
|
|
326
|
-
|
|
327
|
-
for (const conflict of conflicts) {
|
|
328
|
-
lines.push(`## Conflict: ${conflict.label}`);
|
|
329
|
-
lines.push(``);
|
|
330
|
-
lines.push(`### File Version`);
|
|
331
|
-
lines.push(`\`\`\``);
|
|
332
|
-
lines.push(conflict.fileContent);
|
|
333
|
-
lines.push(`\`\`\``);
|
|
334
|
-
lines.push(``);
|
|
335
|
-
lines.push(`### Block Version`);
|
|
336
|
-
lines.push(`\`\`\``);
|
|
337
|
-
lines.push(conflict.blockContent);
|
|
338
|
-
lines.push(`\`\`\``);
|
|
339
|
-
lines.push(``);
|
|
340
|
-
lines.push(`---`);
|
|
341
|
-
lines.push(``);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
if (metadataOnly.length > 0) {
|
|
345
|
-
lines.push(`## Metadata-only Changes`);
|
|
346
|
-
lines.push(``);
|
|
347
|
-
lines.push(
|
|
348
|
-
`Frontmatter changed while body content stayed the same (file wins).`,
|
|
349
|
-
);
|
|
350
|
-
lines.push(``);
|
|
351
|
-
|
|
352
|
-
for (const change of metadataOnly) {
|
|
353
|
-
lines.push(`### ${change.label}`);
|
|
354
|
-
lines.push(``);
|
|
355
|
-
lines.push(`#### File Version (with frontmatter)`);
|
|
356
|
-
lines.push(`\`\`\``);
|
|
357
|
-
lines.push(change.fileContent);
|
|
358
|
-
lines.push(`\`\`\``);
|
|
359
|
-
lines.push(``);
|
|
360
|
-
lines.push(`#### Block Version (body only)`);
|
|
361
|
-
lines.push(`\`\`\``);
|
|
362
|
-
lines.push(change.blockContent);
|
|
363
|
-
lines.push(`\`\`\``);
|
|
364
|
-
lines.push(``);
|
|
365
|
-
lines.push(`---`);
|
|
366
|
-
lines.push(``);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
return lines.join("\n");
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// CLI Entry Point
|
|
374
|
-
const isMainModule = import.meta.url === `file://${process.argv[1]}`;
|
|
375
|
-
if (isMainModule) {
|
|
376
|
-
const args = process.argv.slice(2);
|
|
377
|
-
|
|
378
|
-
if (args.length === 0 || args[0] === "--help" || args[0] === "-h") {
|
|
379
|
-
console.log(`
|
|
380
|
-
Usage: npx tsx memfs-diff.ts <agent-id>
|
|
381
|
-
|
|
382
|
-
Shows the full content of conflicting memory blocks and files.
|
|
383
|
-
Writes a formatted diff to a file for review.
|
|
384
|
-
Analogous to 'git diff'.
|
|
385
|
-
|
|
386
|
-
Arguments:
|
|
387
|
-
agent-id Agent ID to check (can use $LETTA_AGENT_ID)
|
|
388
|
-
|
|
389
|
-
Output: Path to the diff file, or a message if no conflicts exist.
|
|
390
|
-
`);
|
|
391
|
-
process.exit(0);
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
const agentId = args[0];
|
|
395
|
-
if (!agentId) {
|
|
396
|
-
console.error("Error: agent-id is required");
|
|
397
|
-
process.exit(1);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
findConflicts(agentId)
|
|
401
|
-
.then(({ conflicts, metadataOnly }) => {
|
|
402
|
-
if (conflicts.length === 0 && metadataOnly.length === 0) {
|
|
403
|
-
console.log("No conflicts found. Memory filesystem is clean.");
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
const diffContent = formatDiffFile(conflicts, metadataOnly, agentId);
|
|
408
|
-
|
|
409
|
-
// Write to overflow directory (same pattern as tool output overflow)
|
|
410
|
-
const overflowDir = getOverflowDirectory();
|
|
411
|
-
if (!existsSync(overflowDir)) {
|
|
412
|
-
mkdirSync(overflowDir, { recursive: true });
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
const filename = `memfs-diff-${randomUUID()}.md`;
|
|
416
|
-
const diffPath = join(overflowDir, filename);
|
|
417
|
-
writeFileSync(diffPath, diffContent, "utf-8");
|
|
418
|
-
|
|
419
|
-
console.log(
|
|
420
|
-
`Diff (${conflicts.length} conflict${conflicts.length === 1 ? "" : "s"}, ${metadataOnly.length} metadata-only change${metadataOnly.length === 1 ? "" : "s"}) written to: ${diffPath}`,
|
|
421
|
-
);
|
|
422
|
-
})
|
|
423
|
-
.catch((error) => {
|
|
424
|
-
console.error(
|
|
425
|
-
"Error generating memFS diff:",
|
|
426
|
-
error instanceof Error ? error.message : String(error),
|
|
427
|
-
);
|
|
428
|
-
process.exit(1);
|
|
429
|
-
});
|
|
430
|
-
}
|