@contextableai/openclaw-memory-graphiti 0.1.2 → 0.2.0
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 +54 -0
- package/authorization.ts +27 -38
- package/bin/graphiti-mem.ts +140 -0
- package/cli.ts +522 -0
- package/docker/docker-compose.yml +2 -2
- package/graphiti.ts +75 -6
- package/index.ts +154 -337
- package/package.json +11 -6
- package/search.ts +17 -5
- package/spicedb.ts +190 -9
package/cli.ts
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared CLI command registration for graphiti-mem.
|
|
3
|
+
*
|
|
4
|
+
* Used by both the OpenClaw plugin (index.ts) and the standalone CLI (bin/graphiti-mem.ts).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Command } from "commander";
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
10
|
+
import { join, dirname, basename, resolve } from "node:path";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
import type { GraphitiClient } from "./graphiti.js";
|
|
15
|
+
import type { SpiceDbClient, RelationshipTuple } from "./spicedb.js";
|
|
16
|
+
import type { GraphitiMemoryConfig } from "./config.js";
|
|
17
|
+
import {
|
|
18
|
+
lookupAuthorizedGroups,
|
|
19
|
+
ensureGroupMembership,
|
|
20
|
+
type Subject,
|
|
21
|
+
} from "./authorization.js";
|
|
22
|
+
import { searchAuthorizedMemories } from "./search.js";
|
|
23
|
+
|
|
24
|
+
// ============================================================================
|
|
25
|
+
// Session helpers (duplicated from index.ts to avoid circular imports)
|
|
26
|
+
// ============================================================================
|
|
27
|
+
|
|
28
|
+
function sessionGroupId(sessionId: string): string {
|
|
29
|
+
return `session-${sessionId}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ============================================================================
|
|
33
|
+
// CLI Context
|
|
34
|
+
// ============================================================================
|
|
35
|
+
|
|
36
|
+
export type CliContext = {
|
|
37
|
+
graphiti: GraphitiClient;
|
|
38
|
+
spicedb: SpiceDbClient;
|
|
39
|
+
cfg: GraphitiMemoryConfig;
|
|
40
|
+
currentSubject: Subject;
|
|
41
|
+
getLastWriteToken: () => string | undefined;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// ============================================================================
|
|
45
|
+
// Command Registration
|
|
46
|
+
// ============================================================================
|
|
47
|
+
|
|
48
|
+
export function registerCommands(cmd: Command, ctx: CliContext): void {
|
|
49
|
+
const { graphiti, spicedb, cfg, currentSubject, getLastWriteToken } = ctx;
|
|
50
|
+
|
|
51
|
+
cmd
|
|
52
|
+
.command("search")
|
|
53
|
+
.description("Search memories with authorization")
|
|
54
|
+
.argument("<query>", "Search query")
|
|
55
|
+
.option("--limit <n>", "Max results", "10")
|
|
56
|
+
.option("--scope <scope>", "Memory scope: session, long-term, all", "all")
|
|
57
|
+
.action(async (query: string, opts: { limit: string; scope: string }) => {
|
|
58
|
+
const authorizedGroups = await lookupAuthorizedGroups(spicedb, currentSubject, getLastWriteToken());
|
|
59
|
+
if (authorizedGroups.length === 0) {
|
|
60
|
+
console.log("No accessible memory groups.");
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
console.log(`Searching ${authorizedGroups.length} authorized groups...`);
|
|
65
|
+
const results = await searchAuthorizedMemories(graphiti, {
|
|
66
|
+
query,
|
|
67
|
+
groupIds: authorizedGroups,
|
|
68
|
+
limit: parseInt(opts.limit),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (results.length === 0) {
|
|
72
|
+
console.log("No results found.");
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
console.log(JSON.stringify(results, null, 2));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
cmd
|
|
80
|
+
.command("episodes")
|
|
81
|
+
.description("List recent episodes")
|
|
82
|
+
.option("--last <n>", "Number of episodes", "10")
|
|
83
|
+
.option("--group <id>", "Group ID", cfg.graphiti.defaultGroupId)
|
|
84
|
+
.action(async (opts: { last: string; group: string }) => {
|
|
85
|
+
const episodes = await graphiti.getEpisodes(opts.group, parseInt(opts.last));
|
|
86
|
+
console.log(JSON.stringify(episodes, null, 2));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
cmd
|
|
90
|
+
.command("status")
|
|
91
|
+
.description("Check SpiceDB + Graphiti health")
|
|
92
|
+
.action(async () => {
|
|
93
|
+
const graphitiOk = await graphiti.healthCheck();
|
|
94
|
+
let spicedbOk = false;
|
|
95
|
+
try {
|
|
96
|
+
await spicedb.readSchema();
|
|
97
|
+
spicedbOk = true;
|
|
98
|
+
} catch {
|
|
99
|
+
// unreachable
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
console.log(`Graphiti MCP: ${graphitiOk ? "OK" : "UNREACHABLE"} (${cfg.graphiti.endpoint})`);
|
|
103
|
+
console.log(`SpiceDB: ${spicedbOk ? "OK" : "UNREACHABLE"} (${cfg.spicedb.endpoint})`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
cmd
|
|
107
|
+
.command("schema-write")
|
|
108
|
+
.description("Write/update SpiceDB authorization schema")
|
|
109
|
+
.action(async () => {
|
|
110
|
+
const schemaPath = join(dirname(fileURLToPath(import.meta.url)), "schema.zed");
|
|
111
|
+
const schema = readFileSync(schemaPath, "utf-8");
|
|
112
|
+
await spicedb.writeSchema(schema);
|
|
113
|
+
console.log("SpiceDB schema written successfully.");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
cmd
|
|
117
|
+
.command("groups")
|
|
118
|
+
.description("List authorized groups for current subject")
|
|
119
|
+
.action(async () => {
|
|
120
|
+
const groups = await lookupAuthorizedGroups(spicedb, currentSubject, getLastWriteToken());
|
|
121
|
+
if (groups.length === 0) {
|
|
122
|
+
console.log("No authorized groups.");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
console.log(`Authorized groups for ${currentSubject.type}:${currentSubject.id}:`);
|
|
126
|
+
for (const g of groups) {
|
|
127
|
+
console.log(` - ${g}`);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
cmd
|
|
132
|
+
.command("add-member")
|
|
133
|
+
.description("Add a subject to a group")
|
|
134
|
+
.argument("<group-id>", "Group ID")
|
|
135
|
+
.argument("<subject-id>", "Subject ID")
|
|
136
|
+
.option("--type <type>", "Subject type (agent|person)", "person")
|
|
137
|
+
.action(async (groupId: string, subjectId: string, opts: { type: string }) => {
|
|
138
|
+
const subjectType = opts.type === "agent" ? "agent" : "person";
|
|
139
|
+
await ensureGroupMembership(spicedb, groupId, {
|
|
140
|
+
type: subjectType as "agent" | "person",
|
|
141
|
+
id: subjectId,
|
|
142
|
+
});
|
|
143
|
+
console.log(`Added ${subjectType}:${subjectId} to group:${groupId}`);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
cmd
|
|
147
|
+
.command("cleanup")
|
|
148
|
+
.description("Find and optionally delete orphaned Graphiti episodes (no SpiceDB relationships)")
|
|
149
|
+
.option("--group <id>", "Group ID to check", cfg.graphiti.defaultGroupId)
|
|
150
|
+
.option("--last <n>", "Number of recent episodes to check", "100")
|
|
151
|
+
.option("--delete", "Delete orphaned episodes", false)
|
|
152
|
+
.option("--dry-run", "Preview what would be cleaned up", false)
|
|
153
|
+
.action(async (opts: { group: string; last: string; delete: boolean; dryRun: boolean }) => {
|
|
154
|
+
// 1. Fetch episodes from Graphiti for this group
|
|
155
|
+
const episodes = await graphiti.getEpisodes(opts.group, parseInt(opts.last));
|
|
156
|
+
|
|
157
|
+
if (episodes.length === 0) {
|
|
158
|
+
console.log(`No episodes found in group "${opts.group}".`);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 2. Cross-reference with SpiceDB: find all memory_fragment source_group
|
|
163
|
+
// relationships pointing to this group
|
|
164
|
+
const relationships = await spicedb.readRelationships({
|
|
165
|
+
resourceType: "memory_fragment",
|
|
166
|
+
relation: "source_group",
|
|
167
|
+
subjectType: "group",
|
|
168
|
+
subjectId: opts.group,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const authorizedUuids = new Set(relationships.map((r) => r.resourceId));
|
|
172
|
+
|
|
173
|
+
// 3. Identify orphans — episodes without a source_group relationship
|
|
174
|
+
const orphans = episodes.filter((ep) => !authorizedUuids.has(ep.uuid));
|
|
175
|
+
|
|
176
|
+
if (orphans.length === 0) {
|
|
177
|
+
console.log(
|
|
178
|
+
`Checked ${episodes.length} episodes in group "${opts.group}". No orphans found.`,
|
|
179
|
+
);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
console.log(`Found ${orphans.length} orphaned episodes:`);
|
|
184
|
+
for (const ep of orphans) {
|
|
185
|
+
console.log(` ${ep.uuid} (created ${ep.created_at}, no SpiceDB relationships)`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 4. Delete if requested (and not dry-run)
|
|
189
|
+
if (opts.delete && !opts.dryRun) {
|
|
190
|
+
let deleted = 0;
|
|
191
|
+
for (const ep of orphans) {
|
|
192
|
+
try {
|
|
193
|
+
await graphiti.deleteEpisode(ep.uuid);
|
|
194
|
+
deleted++;
|
|
195
|
+
} catch (err) {
|
|
196
|
+
console.error(
|
|
197
|
+
` Failed to delete ${ep.uuid}: ${err instanceof Error ? err.message : String(err)}`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
console.log(`Deleted ${deleted} orphaned episodes.`);
|
|
202
|
+
} else {
|
|
203
|
+
console.log(`\nRun with --delete to remove these episodes.`);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
cmd
|
|
208
|
+
.command("fact")
|
|
209
|
+
.description("Get a specific fact (entity edge) by UUID")
|
|
210
|
+
.argument("<uuid>", "Fact UUID")
|
|
211
|
+
.action(async (uuid: string) => {
|
|
212
|
+
try {
|
|
213
|
+
const fact = await graphiti.getEntityEdge(uuid);
|
|
214
|
+
console.log(JSON.stringify(fact, null, 2));
|
|
215
|
+
} catch (err) {
|
|
216
|
+
console.error(`Failed to get fact ${uuid}: ${err instanceof Error ? err.message : String(err)}`);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
cmd
|
|
221
|
+
.command("clear-graph")
|
|
222
|
+
.description("Clear graph data for specified groups (destructive!)")
|
|
223
|
+
.option("--group <id...>", "Group ID(s) to clear")
|
|
224
|
+
.option("--confirm", "Required safety flag to confirm the operation", false)
|
|
225
|
+
.action(async (opts: { group?: string[]; confirm: boolean }) => {
|
|
226
|
+
if (!opts.confirm) {
|
|
227
|
+
console.log("This is a destructive operation. Pass --confirm to proceed.");
|
|
228
|
+
if (opts.group && opts.group.length > 0) {
|
|
229
|
+
console.log(`Would clear graph data for groups: ${opts.group.join(", ")}`);
|
|
230
|
+
} else {
|
|
231
|
+
console.log("Would clear the default group's graph data.");
|
|
232
|
+
}
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
await graphiti.clearGraph(opts.group);
|
|
238
|
+
if (opts.group && opts.group.length > 0) {
|
|
239
|
+
console.log(`Graph data cleared for groups: ${opts.group.join(", ")}`);
|
|
240
|
+
} else {
|
|
241
|
+
console.log("Graph data cleared for default group.");
|
|
242
|
+
}
|
|
243
|
+
} catch (err) {
|
|
244
|
+
console.error(`Failed to clear graph: ${err instanceof Error ? err.message : String(err)}`);
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
cmd
|
|
249
|
+
.command("import")
|
|
250
|
+
.description("Import workspace markdown files (and optionally session transcripts) into Graphiti")
|
|
251
|
+
.option("--workspace <path>", "Workspace directory", join(homedir(), ".openclaw", "workspace"))
|
|
252
|
+
.option("--include-sessions", "Also import session JSONL transcripts", false)
|
|
253
|
+
.option("--sessions-only", "Only import session transcripts (skip workspace files)", false)
|
|
254
|
+
.option("--session-dir <path>", "Session transcripts directory", join(homedir(), ".openclaw", "agents", "main", "sessions"))
|
|
255
|
+
.option("--group <id>", "Target group for workspace files", cfg.graphiti.defaultGroupId)
|
|
256
|
+
.option("--dry-run", "List files without importing", false)
|
|
257
|
+
.action(async (opts: {
|
|
258
|
+
workspace: string;
|
|
259
|
+
includeSessions: boolean;
|
|
260
|
+
sessionsOnly: boolean;
|
|
261
|
+
sessionDir: string;
|
|
262
|
+
group: string;
|
|
263
|
+
dryRun: boolean;
|
|
264
|
+
}) => {
|
|
265
|
+
const workspacePath = resolve(opts.workspace);
|
|
266
|
+
const targetGroup = opts.group;
|
|
267
|
+
const importSessions = opts.includeSessions || opts.sessionsOnly;
|
|
268
|
+
const importWorkspace = !opts.sessionsOnly;
|
|
269
|
+
|
|
270
|
+
// Discover workspace markdown files
|
|
271
|
+
let mdFiles: string[] = [];
|
|
272
|
+
try {
|
|
273
|
+
const entries = await readdir(workspacePath);
|
|
274
|
+
mdFiles = entries.filter((f) => f.endsWith(".md")).sort();
|
|
275
|
+
} catch {
|
|
276
|
+
console.error(`Cannot read workspace directory: ${workspacePath}`);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Also check for memory/ subdirectory
|
|
281
|
+
try {
|
|
282
|
+
const memDir = join(workspacePath, "memory");
|
|
283
|
+
const memEntries = await readdir(memDir);
|
|
284
|
+
for (const f of memEntries) {
|
|
285
|
+
if (f.endsWith(".md")) {
|
|
286
|
+
mdFiles.push(join("memory", f));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
} catch {
|
|
290
|
+
// No memory/ subdirectory — that's fine
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (mdFiles.length === 0) {
|
|
294
|
+
console.log("No markdown files found in workspace.");
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
console.log(`Found ${mdFiles.length} workspace file(s) in ${workspacePath}:`);
|
|
299
|
+
for (const f of mdFiles) {
|
|
300
|
+
const filePath = join(workspacePath, f);
|
|
301
|
+
const info = await stat(filePath);
|
|
302
|
+
console.log(` ${f} (${info.size} bytes)`);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (opts.dryRun) {
|
|
306
|
+
console.log("\n[dry-run] No files imported.");
|
|
307
|
+
if (importSessions) {
|
|
308
|
+
const sessionPath = resolve(opts.sessionDir);
|
|
309
|
+
try {
|
|
310
|
+
const sessions = (await readdir(sessionPath)).filter((f) => f.endsWith(".jsonl"));
|
|
311
|
+
console.log(`\nFound ${sessions.length} session transcript(s) in ${sessionPath}:`);
|
|
312
|
+
for (const f of sessions) {
|
|
313
|
+
const info = await stat(join(sessionPath, f));
|
|
314
|
+
console.log(` ${f} (${info.size} bytes)`);
|
|
315
|
+
}
|
|
316
|
+
} catch {
|
|
317
|
+
console.log(`\nCannot read session directory: ${sessionPath}`);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Two-phase import:
|
|
324
|
+
// Phase 1 — Graphiti ingestion: addEpisode for each file, collect results
|
|
325
|
+
// Phase 2 — SpiceDB bulk: single bulkImportRelationships call for all tuples
|
|
326
|
+
// This is more efficient than interleaving and leaves SpiceDB in a clean
|
|
327
|
+
// state if Graphiti ingestion fails partway (orphaned episodes are invisible
|
|
328
|
+
// without authorization).
|
|
329
|
+
|
|
330
|
+
// Collect resolvedUuid promises during Phase 1 so we can await
|
|
331
|
+
// real server-side UUIDs before writing SpiceDB relationships.
|
|
332
|
+
const pendingResolutions: { resolvedUuid: Promise<string>; groupId: string; name: string }[] = [];
|
|
333
|
+
const membershipGroups = new Set<string>();
|
|
334
|
+
|
|
335
|
+
// Ensure agent is a member of the target workspace group
|
|
336
|
+
if (importWorkspace) {
|
|
337
|
+
cmdbershipGroups.add(targetGroup);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Phase 1a: Import workspace files to Graphiti
|
|
341
|
+
if (importWorkspace) {
|
|
342
|
+
console.log(`\nPhase 1: Importing workspace files to Graphiti (group: ${targetGroup})...`);
|
|
343
|
+
let imported = 0;
|
|
344
|
+
for (const f of mdFiles) {
|
|
345
|
+
const filePath = join(workspacePath, f);
|
|
346
|
+
const content = await readFile(filePath, "utf-8");
|
|
347
|
+
if (!content.trim()) {
|
|
348
|
+
console.log(` Skipping ${f} (empty)`);
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
try {
|
|
352
|
+
const result = await graphiti.addEpisode({
|
|
353
|
+
name: f,
|
|
354
|
+
episode_body: content,
|
|
355
|
+
source_description: `Imported from OpenClaw workspace: ${f}`,
|
|
356
|
+
group_id: targetGroup,
|
|
357
|
+
source: "text",
|
|
358
|
+
});
|
|
359
|
+
pendingResolutions.push({
|
|
360
|
+
resolvedUuid: result.resolvedUuid,
|
|
361
|
+
groupId: targetGroup,
|
|
362
|
+
name: f,
|
|
363
|
+
});
|
|
364
|
+
console.log(` Queued ${f} (${content.length} bytes) — resolving UUID in background`);
|
|
365
|
+
imported++;
|
|
366
|
+
} catch (err) {
|
|
367
|
+
console.error(` Failed to import ${f}: ${err instanceof Error ? err.message : String(err)}`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
console.log(`Workspace: ${imported}/${mdFiles.length} files ingested.`);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Phase 1b: Import session transcripts to Graphiti
|
|
374
|
+
if (importSessions) {
|
|
375
|
+
const sessionPath = resolve(opts.sessionDir);
|
|
376
|
+
let jsonlFiles: string[] = [];
|
|
377
|
+
try {
|
|
378
|
+
jsonlFiles = (await readdir(sessionPath)).filter((f) => f.endsWith(".jsonl")).sort();
|
|
379
|
+
} catch {
|
|
380
|
+
console.error(`\nCannot read session directory: ${sessionPath}`);
|
|
381
|
+
// Continue to Phase 2 with whatever tuples we have
|
|
382
|
+
jsonlFiles = [];
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (jsonlFiles.length === 0) {
|
|
386
|
+
console.log("\nNo session transcripts found.");
|
|
387
|
+
} else {
|
|
388
|
+
console.log(`\nPhase 1: Importing ${jsonlFiles.length} session transcript(s) to Graphiti...`);
|
|
389
|
+
let sessionsImported = 0;
|
|
390
|
+
for (const f of jsonlFiles) {
|
|
391
|
+
const sessionId = basename(f, ".jsonl");
|
|
392
|
+
const sessionGroup = sessionGroupId(sessionId);
|
|
393
|
+
const filePath = join(sessionPath, f);
|
|
394
|
+
const raw = await readFile(filePath, "utf-8");
|
|
395
|
+
const lines = raw.split("\n").filter(Boolean);
|
|
396
|
+
|
|
397
|
+
// Extract user/assistant message text from JSONL
|
|
398
|
+
const conversationLines: string[] = [];
|
|
399
|
+
for (const line of lines) {
|
|
400
|
+
try {
|
|
401
|
+
const entry = JSON.parse(line) as Record<string, unknown>;
|
|
402
|
+
// OpenClaw JSONL format: {"type":"message","message":{"role":"user","content":[...]}}
|
|
403
|
+
const msg = (entry.type === "message" && entry.message && typeof entry.message === "object")
|
|
404
|
+
? entry.message as Record<string, unknown>
|
|
405
|
+
: entry;
|
|
406
|
+
const role = msg.role as string | undefined;
|
|
407
|
+
if (role !== "user" && role !== "assistant") continue;
|
|
408
|
+
const content = msg.content;
|
|
409
|
+
let text = "";
|
|
410
|
+
if (typeof content === "string") {
|
|
411
|
+
text = content;
|
|
412
|
+
} else if (Array.isArray(content)) {
|
|
413
|
+
text = content
|
|
414
|
+
.filter((b: unknown) =>
|
|
415
|
+
typeof b === "object" && b !== null &&
|
|
416
|
+
(b as Record<string, unknown>).type === "text" &&
|
|
417
|
+
typeof (b as Record<string, unknown>).text === "string",
|
|
418
|
+
)
|
|
419
|
+
.map((b: unknown) => (b as Record<string, unknown>).text as string)
|
|
420
|
+
.join("\n");
|
|
421
|
+
}
|
|
422
|
+
if (text && text.length >= 5 && !text.includes("<relevant-memories>") && !text.includes("<memory-tools>")) {
|
|
423
|
+
const roleLabel = role === "user" ? "User" : "Assistant";
|
|
424
|
+
conversationLines.push(`${roleLabel}: ${text}`);
|
|
425
|
+
}
|
|
426
|
+
} catch {
|
|
427
|
+
// Skip malformed JSONL lines
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (conversationLines.length === 0) {
|
|
432
|
+
console.log(` Skipping ${f} (no user/assistant messages)`);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
try {
|
|
437
|
+
const episodeBody = conversationLines.join("\n");
|
|
438
|
+
const result = await graphiti.addEpisode({
|
|
439
|
+
name: `session_${sessionId}`,
|
|
440
|
+
episode_body: episodeBody,
|
|
441
|
+
source_description: `Imported session transcript: ${sessionId}`,
|
|
442
|
+
group_id: sessionGroup,
|
|
443
|
+
source: "message",
|
|
444
|
+
});
|
|
445
|
+
cmdbershipGroups.add(sessionGroup);
|
|
446
|
+
pendingResolutions.push({
|
|
447
|
+
resolvedUuid: result.resolvedUuid,
|
|
448
|
+
groupId: sessionGroup,
|
|
449
|
+
name: f,
|
|
450
|
+
});
|
|
451
|
+
console.log(` Queued ${f} (${conversationLines.length} messages) — resolving UUID in background [group: ${sessionGroup}]`);
|
|
452
|
+
sessionsImported++;
|
|
453
|
+
} catch (err) {
|
|
454
|
+
console.error(` Failed to import ${f}: ${err instanceof Error ? err.message : String(err)}`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
console.log(`Sessions: ${sessionsImported}/${jsonlFiles.length} transcripts ingested.`);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// Phase 1.5: Await real server-side UUIDs from Graphiti.
|
|
462
|
+
// The background polls started during Phase 1 run concurrently,
|
|
463
|
+
// so the total wait is max(processing time) not sum.
|
|
464
|
+
const pendingTuples: RelationshipTuple[] = [];
|
|
465
|
+
if (pendingResolutions.length > 0) {
|
|
466
|
+
console.log(`\nResolving ${pendingResolutions.length} episode UUIDs (waiting for Graphiti processing)...`);
|
|
467
|
+
const results = await Promise.allSettled(
|
|
468
|
+
pendingResolutions.map((p) => p.resolvedUuid),
|
|
469
|
+
);
|
|
470
|
+
for (let i = 0; i < results.length; i++) {
|
|
471
|
+
const resolution = results[i];
|
|
472
|
+
if (resolution.status === "fulfilled") {
|
|
473
|
+
const realUuid = resolution.value;
|
|
474
|
+
pendingTuples.push(
|
|
475
|
+
{
|
|
476
|
+
resourceType: "memory_fragment",
|
|
477
|
+
resourceId: realUuid,
|
|
478
|
+
relation: "source_group",
|
|
479
|
+
subjectType: "group",
|
|
480
|
+
subjectId: pendingResolutions[i].groupId,
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
resourceType: "memory_fragment",
|
|
484
|
+
resourceId: realUuid,
|
|
485
|
+
relation: "shared_by",
|
|
486
|
+
subjectType: currentSubject.type,
|
|
487
|
+
subjectId: currentSubject.id,
|
|
488
|
+
},
|
|
489
|
+
);
|
|
490
|
+
console.log(` ${pendingResolutions[i].name} → ${realUuid}`);
|
|
491
|
+
} else {
|
|
492
|
+
console.warn(` Warning: could not resolve UUID for ${pendingResolutions[i].name} — SpiceDB linkage skipped`);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Phase 2: Bulk write all SpiceDB relationships
|
|
498
|
+
if (pendingTuples.length > 0 || membershipGroups.size > 0) {
|
|
499
|
+
// Add group membership tuples for session groups
|
|
500
|
+
for (const groupId of membershipGroups) {
|
|
501
|
+
pendingTuples.push({
|
|
502
|
+
resourceType: "group",
|
|
503
|
+
resourceId: groupId,
|
|
504
|
+
relation: "member",
|
|
505
|
+
subjectType: currentSubject.type,
|
|
506
|
+
subjectId: currentSubject.id,
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
console.log(`\nPhase 2: Writing ${pendingTuples.length} SpiceDB relationships...`);
|
|
511
|
+
try {
|
|
512
|
+
const count = await spicedb.bulkImportRelationships(pendingTuples);
|
|
513
|
+
console.log(`SpiceDB: ${count} relationships written.`);
|
|
514
|
+
} catch (err) {
|
|
515
|
+
console.error(`SpiceDB bulk import failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
516
|
+
console.error("Graphiti episodes were ingested but lack authorization. Re-run import or add relationships manually.");
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
console.log("\nImport complete.")
|
|
521
|
+
});
|
|
522
|
+
}
|
|
@@ -75,12 +75,12 @@ services:
|
|
|
75
75
|
# Graphiti MCP Server — knowledge graph API over HTTP
|
|
76
76
|
# --------------------------------------------------------------------------
|
|
77
77
|
graphiti-mcp:
|
|
78
|
-
image:
|
|
78
|
+
image: zepai/knowledge-graph-mcp:latest
|
|
79
79
|
ports:
|
|
80
80
|
- "${GRAPHITI_PORT:-8000}:8000" # MCP HTTP endpoint
|
|
81
81
|
environment:
|
|
82
82
|
- OPENAI_API_KEY=${OPENAI_API_KEY}
|
|
83
|
-
- FALKORDB_URI=redis://
|
|
83
|
+
- FALKORDB_URI=redis://host.docker.internal:${FALKORDB_PORT:-6379}
|
|
84
84
|
depends_on:
|
|
85
85
|
falkordb:
|
|
86
86
|
condition: service_healthy
|