@openweave/weave-graph 0.2.0 → 0.2.1
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/dist/cjs/compression.d.ts +94 -0
- package/dist/cjs/compression.d.ts.map +1 -0
- package/dist/cjs/compression.js +215 -0
- package/dist/cjs/compression.js.map +1 -0
- package/dist/cjs/edge.d.ts +43 -0
- package/dist/cjs/edge.d.ts.map +1 -0
- package/dist/cjs/edge.js +83 -0
- package/dist/cjs/edge.js.map +1 -0
- package/dist/cjs/hebbian-weights.d.ts +100 -0
- package/dist/cjs/hebbian-weights.d.ts.map +1 -0
- package/dist/cjs/hebbian-weights.js +152 -0
- package/dist/cjs/hebbian-weights.js.map +1 -0
- package/dist/cjs/index.d.ts +193 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +417 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/node.d.ts +43 -0
- package/dist/cjs/node.d.ts.map +1 -0
- package/dist/cjs/node.js +83 -0
- package/dist/cjs/node.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/persistence.d.ts +86 -0
- package/dist/cjs/persistence.d.ts.map +1 -0
- package/dist/cjs/persistence.js +215 -0
- package/dist/cjs/persistence.js.map +1 -0
- package/dist/cjs/synaptic-engine.d.ts +126 -0
- package/dist/cjs/synaptic-engine.d.ts.map +1 -0
- package/dist/cjs/synaptic-engine.js +243 -0
- package/dist/cjs/synaptic-engine.js.map +1 -0
- package/dist/cjs/types.d.ts +74 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +30 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/hebbian-weights.d.ts +100 -0
- package/dist/hebbian-weights.d.ts.map +1 -0
- package/dist/hebbian-weights.js +148 -0
- package/dist/hebbian-weights.js.map +1 -0
- package/dist/index.d.ts +36 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +65 -3
- package/dist/index.js.map +1 -1
- package/dist/persistence.d.ts +43 -15
- package/dist/persistence.d.ts.map +1 -1
- package/dist/persistence.js +109 -119
- package/dist/persistence.js.map +1 -1
- package/dist/synaptic-engine.d.ts +126 -0
- package/dist/synaptic-engine.d.ts.map +1 -0
- package/dist/synaptic-engine.js +236 -0
- package/dist/synaptic-engine.js.map +1 -0
- package/package.json +6 -4
package/dist/persistence.d.ts
CHANGED
|
@@ -1,43 +1,65 @@
|
|
|
1
|
+
import { IWeaveProvider } from "@openweave/weave-provider";
|
|
1
2
|
import { ContextGraphManager } from "./index";
|
|
2
3
|
import { GraphSnapshot } from "./types";
|
|
4
|
+
type SerializedSnapshot = Record<string, unknown>;
|
|
3
5
|
/**
|
|
4
6
|
* PersistenceManager
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
+
*
|
|
8
|
+
* Handles saving and loading graph snapshots through a pluggable
|
|
9
|
+
* IWeaveProvider. If no provider is injected, it falls back to the
|
|
10
|
+
* built-in JsonProvider (same behaviour as before, backward-compatible).
|
|
11
|
+
*
|
|
12
|
+
* Key convention: `graph:<chatId>` — namespaced so multiple subsystems can
|
|
13
|
+
* share a single provider without key collisions.
|
|
7
14
|
*/
|
|
8
15
|
export declare class PersistenceManager {
|
|
16
|
+
/** Kept for backward-compat (`getDataDir` / `setDataDir` / constructor). */
|
|
9
17
|
private dataDir;
|
|
10
|
-
|
|
18
|
+
private provider;
|
|
11
19
|
/**
|
|
12
|
-
*
|
|
20
|
+
* @param dataDir Root directory used by the default JsonProvider.
|
|
21
|
+
* Ignored when an explicit `provider` is supplied.
|
|
22
|
+
* @param provider Optional storage provider. Defaults to `JsonProvider(dataDir)`.
|
|
13
23
|
*/
|
|
14
|
-
|
|
24
|
+
constructor(dataDir?: string, provider?: IWeaveProvider<SerializedSnapshot>);
|
|
25
|
+
private graphKey;
|
|
15
26
|
/**
|
|
16
|
-
*
|
|
27
|
+
* Serialize a GraphSnapshot to a plain object (dates → ISO strings).
|
|
28
|
+
*/
|
|
29
|
+
private serialize;
|
|
30
|
+
/**
|
|
31
|
+
* Deserialize a plain object back to a GraphSnapshot (ISO strings → dates).
|
|
32
|
+
*/
|
|
33
|
+
private deserialize;
|
|
34
|
+
/**
|
|
35
|
+
* Ensure the data directory exists.
|
|
36
|
+
* Delegates to the OS mkdir so callers that relied on the old
|
|
37
|
+
* PersistenceManager behaviour continue to work unchanged.
|
|
17
38
|
*/
|
|
18
39
|
ensureDataDir(): Promise<void>;
|
|
19
40
|
/**
|
|
20
|
-
* Save a graph snapshot
|
|
41
|
+
* Save a graph snapshot via the configured provider.
|
|
21
42
|
*/
|
|
22
43
|
saveGraph(snapshot: GraphSnapshot): Promise<void>;
|
|
23
44
|
/**
|
|
24
|
-
* Load a graph snapshot from
|
|
45
|
+
* Load a graph snapshot from the configured provider.
|
|
46
|
+
* Returns `null` if the snapshot does not exist.
|
|
25
47
|
*/
|
|
26
48
|
loadGraph(chatId: string): Promise<GraphSnapshot | null>;
|
|
27
49
|
/**
|
|
28
|
-
* Load or create a graph manager for a chat session
|
|
50
|
+
* Load or create a graph manager for a chat session.
|
|
29
51
|
*/
|
|
30
52
|
loadOrCreateGraph(chatId: string, compressionThreshold?: number): Promise<ContextGraphManager>;
|
|
31
53
|
/**
|
|
32
|
-
* Check
|
|
54
|
+
* Check whether a graph exists in the provider.
|
|
33
55
|
*/
|
|
34
56
|
graphExists(chatId: string): Promise<boolean>;
|
|
35
57
|
/**
|
|
36
|
-
* Delete a graph from
|
|
58
|
+
* Delete a graph from the provider. No-op if it does not exist.
|
|
37
59
|
*/
|
|
38
60
|
deleteGraph(chatId: string): Promise<void>;
|
|
39
61
|
/**
|
|
40
|
-
* List all saved chat sessions
|
|
62
|
+
* List all saved chat sessions by querying the `graph:` namespace.
|
|
41
63
|
*/
|
|
42
64
|
listSessions(): Promise<Array<{
|
|
43
65
|
chatId: string;
|
|
@@ -47,12 +69,18 @@ export declare class PersistenceManager {
|
|
|
47
69
|
edgeCount: number;
|
|
48
70
|
}>>;
|
|
49
71
|
/**
|
|
50
|
-
*
|
|
72
|
+
* Swap in a different provider at runtime (e.g. after migration).
|
|
51
73
|
*/
|
|
52
|
-
|
|
74
|
+
setProvider(provider: IWeaveProvider<SerializedSnapshot>): void;
|
|
75
|
+
/** Returns the active provider (useful for tests / diagnostics). */
|
|
76
|
+
getProvider(): IWeaveProvider<SerializedSnapshot>;
|
|
53
77
|
/**
|
|
54
|
-
*
|
|
78
|
+
* Change the data directory used by the default JsonProvider.
|
|
79
|
+
* Has no effect when an external provider was injected.
|
|
55
80
|
*/
|
|
81
|
+
setDataDir(newDataDir: string): void;
|
|
82
|
+
/** Returns the configured data directory. */
|
|
56
83
|
getDataDir(): string;
|
|
57
84
|
}
|
|
85
|
+
export {};
|
|
58
86
|
//# sourceMappingURL=persistence.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persistence.d.ts","sourceRoot":"","sources":["../src/persistence.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"persistence.d.ts","sourceRoot":"","sources":["../src/persistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAgB,MAAM,2BAA2B,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGxC,KAAK,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAElD;;;;;;;;;GASG;AACH,qBAAa,kBAAkB;IAC7B,4EAA4E;IAC5E,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAqC;IAErD;;;;OAIG;gBAED,OAAO,GAAE,MAAuB,EAChC,QAAQ,CAAC,EAAE,cAAc,CAAC,kBAAkB,CAAC;IAQ/C,OAAO,CAAC,QAAQ;IAIhB;;OAEG;IACH,OAAO,CAAC,SAAS;IA+BjB;;OAEG;IAEH,OAAO,CAAC,WAAW;IA2BnB;;;;OAIG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAKpC;;OAEG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvD;;;OAGG;IACG,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAM9D;;OAEG;IACG,iBAAiB,CACrB,MAAM,EAAE,MAAM,EACd,oBAAoB,CAAC,EAAE,MAAM,GAC5B,OAAO,CAAC,mBAAmB,CAAC;IAM/B;;OAEG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAInD;;OAEG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIhD;;OAEG;IACG,YAAY,IAAI,OAAO,CAC3B,KAAK,CAAC;QACJ,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,EAAE,IAAI,CAAC;QAChB,SAAS,EAAE,IAAI,CAAC;QAChB,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC,CACH;IAmBD;;OAEG;IACH,WAAW,CAAC,QAAQ,EAAE,cAAc,CAAC,kBAAkB,CAAC,GAAG,IAAI;IAI/D,oEAAoE;IACpE,WAAW,IAAI,cAAc,CAAC,kBAAkB,CAAC;IAIjD;;;OAGG;IACH,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAOpC,6CAA6C;IAC7C,UAAU,IAAI,MAAM;CAGrB"}
|
package/dist/persistence.js
CHANGED
|
@@ -1,43 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as path from "path";
|
|
1
|
+
import { JsonProvider } from "@openweave/weave-provider";
|
|
3
2
|
import { ContextGraphManager } from "./index";
|
|
4
3
|
/**
|
|
5
4
|
* PersistenceManager
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
*
|
|
6
|
+
* Handles saving and loading graph snapshots through a pluggable
|
|
7
|
+
* IWeaveProvider. If no provider is injected, it falls back to the
|
|
8
|
+
* built-in JsonProvider (same behaviour as before, backward-compatible).
|
|
9
|
+
*
|
|
10
|
+
* Key convention: `graph:<chatId>` — namespaced so multiple subsystems can
|
|
11
|
+
* share a single provider without key collisions.
|
|
8
12
|
*/
|
|
9
13
|
export class PersistenceManager {
|
|
14
|
+
/** Kept for backward-compat (`getDataDir` / `setDataDir` / constructor). */
|
|
10
15
|
dataDir;
|
|
11
|
-
|
|
12
|
-
this.dataDir = dataDir;
|
|
13
|
-
}
|
|
16
|
+
provider;
|
|
14
17
|
/**
|
|
15
|
-
*
|
|
18
|
+
* @param dataDir Root directory used by the default JsonProvider.
|
|
19
|
+
* Ignored when an explicit `provider` is supplied.
|
|
20
|
+
* @param provider Optional storage provider. Defaults to `JsonProvider(dataDir)`.
|
|
16
21
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
constructor(dataDir = "./weave-data", provider) {
|
|
23
|
+
this.dataDir = dataDir;
|
|
24
|
+
this.provider = provider ?? new JsonProvider(dataDir);
|
|
20
25
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
async ensureDataDir() {
|
|
25
|
-
try {
|
|
26
|
-
await fs.mkdir(this.dataDir, { recursive: true });
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
if (error.code !== "EEXIST") {
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
26
|
+
// ── Key helpers ───────────────────────────────────────────────────────────
|
|
27
|
+
graphKey(chatId) {
|
|
28
|
+
return `graph:${chatId}`;
|
|
33
29
|
}
|
|
34
30
|
/**
|
|
35
|
-
*
|
|
31
|
+
* Serialize a GraphSnapshot to a plain object (dates → ISO strings).
|
|
36
32
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const filePath = this.getFilePath(snapshot.metadata.chatId);
|
|
40
|
-
const serialized = {
|
|
33
|
+
serialize(snapshot) {
|
|
34
|
+
return {
|
|
41
35
|
...snapshot,
|
|
42
36
|
metadata: {
|
|
43
37
|
...snapshot.metadata,
|
|
@@ -61,126 +55,122 @@ export class PersistenceManager {
|
|
|
61
55
|
},
|
|
62
56
|
])),
|
|
63
57
|
};
|
|
64
|
-
await fs.writeFile(filePath, JSON.stringify(serialized, null, 2), "utf-8");
|
|
65
58
|
}
|
|
66
59
|
/**
|
|
67
|
-
*
|
|
60
|
+
* Deserialize a plain object back to a GraphSnapshot (ISO strings → dates).
|
|
61
|
+
*/
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
+
deserialize(parsed) {
|
|
64
|
+
return {
|
|
65
|
+
...parsed,
|
|
66
|
+
metadata: {
|
|
67
|
+
...parsed.metadata,
|
|
68
|
+
createdAt: new Date(parsed.metadata.createdAt),
|
|
69
|
+
updatedAt: new Date(parsed.metadata.updatedAt),
|
|
70
|
+
},
|
|
71
|
+
nodes: Object.fromEntries(
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
+
Object.entries(parsed.nodes).map(([id, node]) => [
|
|
74
|
+
id,
|
|
75
|
+
{ ...node, createdAt: new Date(node.createdAt), updatedAt: new Date(node.updatedAt) },
|
|
76
|
+
])),
|
|
77
|
+
edges: Object.fromEntries(
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
79
|
+
Object.entries(parsed.edges).map(([id, edge]) => [
|
|
80
|
+
id,
|
|
81
|
+
{ ...edge, createdAt: new Date(edge.createdAt), updatedAt: new Date(edge.updatedAt) },
|
|
82
|
+
])),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// ── Public API ────────────────────────────────────────────────────────────
|
|
86
|
+
/**
|
|
87
|
+
* Ensure the data directory exists.
|
|
88
|
+
* Delegates to the OS mkdir so callers that relied on the old
|
|
89
|
+
* PersistenceManager behaviour continue to work unchanged.
|
|
90
|
+
*/
|
|
91
|
+
async ensureDataDir() {
|
|
92
|
+
const { promises: fsp } = await import('fs');
|
|
93
|
+
await fsp.mkdir(this.dataDir, { recursive: true });
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Save a graph snapshot via the configured provider.
|
|
97
|
+
*/
|
|
98
|
+
async saveGraph(snapshot) {
|
|
99
|
+
await this.provider.set(this.graphKey(snapshot.metadata.chatId), this.serialize(snapshot));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Load a graph snapshot from the configured provider.
|
|
103
|
+
* Returns `null` if the snapshot does not exist.
|
|
68
104
|
*/
|
|
69
105
|
async loadGraph(chatId) {
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
// Deserialize dates
|
|
75
|
-
return {
|
|
76
|
-
...parsed,
|
|
77
|
-
metadata: {
|
|
78
|
-
...parsed.metadata,
|
|
79
|
-
createdAt: new Date(parsed.metadata.createdAt),
|
|
80
|
-
updatedAt: new Date(parsed.metadata.updatedAt),
|
|
81
|
-
},
|
|
82
|
-
nodes: Object.fromEntries(Object.entries(parsed.nodes).map(([id, node]) => [
|
|
83
|
-
id,
|
|
84
|
-
{
|
|
85
|
-
...node,
|
|
86
|
-
createdAt: new Date(node.createdAt),
|
|
87
|
-
updatedAt: new Date(node.updatedAt),
|
|
88
|
-
},
|
|
89
|
-
])),
|
|
90
|
-
edges: Object.fromEntries(Object.entries(parsed.edges).map(([id, edge]) => [
|
|
91
|
-
id,
|
|
92
|
-
{
|
|
93
|
-
...edge,
|
|
94
|
-
createdAt: new Date(edge.createdAt),
|
|
95
|
-
updatedAt: new Date(edge.updatedAt),
|
|
96
|
-
},
|
|
97
|
-
])),
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
catch (error) {
|
|
101
|
-
if (error.code === "ENOENT") {
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
throw error;
|
|
105
|
-
}
|
|
106
|
+
const raw = await this.provider.get(this.graphKey(chatId));
|
|
107
|
+
if (raw === null)
|
|
108
|
+
return null;
|
|
109
|
+
return this.deserialize(raw);
|
|
106
110
|
}
|
|
107
111
|
/**
|
|
108
|
-
* Load or create a graph manager for a chat session
|
|
112
|
+
* Load or create a graph manager for a chat session.
|
|
109
113
|
*/
|
|
110
114
|
async loadOrCreateGraph(chatId, compressionThreshold) {
|
|
111
115
|
const snapshot = await this.loadGraph(chatId);
|
|
112
|
-
if (snapshot)
|
|
116
|
+
if (snapshot)
|
|
113
117
|
return ContextGraphManager.fromSnapshot(snapshot);
|
|
114
|
-
}
|
|
115
118
|
return new ContextGraphManager(chatId, compressionThreshold);
|
|
116
119
|
}
|
|
117
120
|
/**
|
|
118
|
-
* Check
|
|
121
|
+
* Check whether a graph exists in the provider.
|
|
119
122
|
*/
|
|
120
123
|
async graphExists(chatId) {
|
|
121
|
-
|
|
122
|
-
try {
|
|
123
|
-
await fs.access(filePath);
|
|
124
|
-
return true;
|
|
125
|
-
}
|
|
126
|
-
catch {
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
124
|
+
return (await this.provider.get(this.graphKey(chatId))) !== null;
|
|
129
125
|
}
|
|
130
126
|
/**
|
|
131
|
-
* Delete a graph from
|
|
127
|
+
* Delete a graph from the provider. No-op if it does not exist.
|
|
132
128
|
*/
|
|
133
129
|
async deleteGraph(chatId) {
|
|
134
|
-
|
|
135
|
-
try {
|
|
136
|
-
await fs.unlink(filePath);
|
|
137
|
-
}
|
|
138
|
-
catch (error) {
|
|
139
|
-
if (error.code !== "ENOENT") {
|
|
140
|
-
throw error;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
130
|
+
await this.provider.delete(this.graphKey(chatId));
|
|
143
131
|
}
|
|
144
132
|
/**
|
|
145
|
-
* List all saved chat sessions
|
|
133
|
+
* List all saved chat sessions by querying the `graph:` namespace.
|
|
146
134
|
*/
|
|
147
135
|
async listSessions() {
|
|
148
|
-
await this.
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
edgeCount: Object.keys(snapshot.edges).length,
|
|
164
|
-
};
|
|
165
|
-
}));
|
|
166
|
-
return sessions.filter((s) => s !== null);
|
|
167
|
-
}
|
|
168
|
-
catch (error) {
|
|
169
|
-
if (error.code === "ENOENT") {
|
|
170
|
-
return [];
|
|
171
|
-
}
|
|
172
|
-
throw error;
|
|
173
|
-
}
|
|
136
|
+
const keys = await this.provider.list("graph:");
|
|
137
|
+
const sessions = await Promise.all(keys.map(async (key) => {
|
|
138
|
+
const chatId = key.replace(/^graph:/, "");
|
|
139
|
+
const snapshot = await this.loadGraph(chatId);
|
|
140
|
+
if (!snapshot)
|
|
141
|
+
return null;
|
|
142
|
+
return {
|
|
143
|
+
chatId,
|
|
144
|
+
createdAt: snapshot.metadata.createdAt,
|
|
145
|
+
updatedAt: snapshot.metadata.updatedAt,
|
|
146
|
+
nodeCount: Object.keys(snapshot.nodes).length,
|
|
147
|
+
edgeCount: Object.keys(snapshot.edges).length,
|
|
148
|
+
};
|
|
149
|
+
}));
|
|
150
|
+
return sessions.filter((s) => s !== null);
|
|
174
151
|
}
|
|
175
152
|
/**
|
|
176
|
-
*
|
|
153
|
+
* Swap in a different provider at runtime (e.g. after migration).
|
|
177
154
|
*/
|
|
178
|
-
|
|
179
|
-
this.
|
|
155
|
+
setProvider(provider) {
|
|
156
|
+
this.provider = provider;
|
|
157
|
+
}
|
|
158
|
+
/** Returns the active provider (useful for tests / diagnostics). */
|
|
159
|
+
getProvider() {
|
|
160
|
+
return this.provider;
|
|
180
161
|
}
|
|
181
162
|
/**
|
|
182
|
-
*
|
|
163
|
+
* Change the data directory used by the default JsonProvider.
|
|
164
|
+
* Has no effect when an external provider was injected.
|
|
183
165
|
*/
|
|
166
|
+
setDataDir(newDataDir) {
|
|
167
|
+
this.dataDir = newDataDir;
|
|
168
|
+
// Re-create the default provider so the new path takes effect
|
|
169
|
+
if (!(this.provider instanceof JsonProvider))
|
|
170
|
+
return;
|
|
171
|
+
this.provider = new JsonProvider(newDataDir);
|
|
172
|
+
}
|
|
173
|
+
/** Returns the configured data directory. */
|
|
184
174
|
getDataDir() {
|
|
185
175
|
return this.dataDir;
|
|
186
176
|
}
|
package/dist/persistence.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persistence.js","sourceRoot":"","sources":["../src/persistence.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"persistence.js","sourceRoot":"","sources":["../src/persistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAM9C;;;;;;;;;GASG;AACH,MAAM,OAAO,kBAAkB;IAC7B,4EAA4E;IACpE,OAAO,CAAS;IAChB,QAAQ,CAAqC;IAErD;;;;OAIG;IACH,YACE,UAAkB,cAAc,EAChC,QAA6C;QAE7C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,IAAI,YAAY,CAAqB,OAAO,CAAC,CAAC;IAC5E,CAAC;IAED,6EAA6E;IAErE,QAAQ,CAAC,MAAc;QAC7B,OAAO,SAAS,MAAM,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACK,SAAS,CAAC,QAAuB;QACvC,OAAO;YACL,GAAG,QAAQ;YACX,QAAQ,EAAE;gBACR,GAAG,QAAQ,CAAC,QAAQ;gBACpB,SAAS,EAAE,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,WAAW,EAAE;gBACpD,SAAS,EAAE,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,WAAW,EAAE;aACrD;YACD,KAAK,EAAE,MAAM,CAAC,W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,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,MAAM;gBAC7C,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,MAAM;aAC9C,CAAC;QACJ,CAAC,CAAC,CACH,CAAC;QACF,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAA8B,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;IACxE,CAAC;IAED;;OAEG;IACH,WAAW,CAAC,QAA4C;QACtD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,oEAAoE;IACpE,WAAW;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;OAGG;IACH,UAAU,CAAC,UAAkB;QAC3B,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC;QAC1B,8DAA8D;QAC9D,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,YAAY,YAAY,CAAC;YAAE,OAAO;QACrD,IAAI,CAAC,QAAQ,GAAG,IAAI,YAAY,CAAqB,UAAU,CAAC,CAAC;IACnE,CAAC;IAED,6CAA6C;IAC7C,UAAU;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Edge, Node } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal duck-typed interface for an embedding service.
|
|
4
|
+
* `EmbeddingService` from `@openweave/weave-embed` satisfies this interface
|
|
5
|
+
* directly — no import required, keeping weave-graph free of extra deps.
|
|
6
|
+
*/
|
|
7
|
+
export interface SynapticEmbeddingService {
|
|
8
|
+
/** Embed a single text string. Returns an object with an `embedding` vector. */
|
|
9
|
+
embed(text: string): Promise<{
|
|
10
|
+
embedding: number[];
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
export interface SynapticOptions {
|
|
14
|
+
/** Minimum similarity required to create a retroactive edge. Default: 0.72 */
|
|
15
|
+
threshold?: number;
|
|
16
|
+
/** Maximum number of retroactive edges created per new node. Default: 20 */
|
|
17
|
+
maxConnections?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Optional embedding service for semantic (cosine) retroactive linking.
|
|
20
|
+
* When provided, `linkRetroactivelyEmbedding()` uses cosine similarity
|
|
21
|
+
* instead of Jaccard keyword overlap — enabling cross-vocabulary matching.
|
|
22
|
+
*/
|
|
23
|
+
embeddingService?: SynapticEmbeddingService;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Minimal structural interface that ContextGraphManager satisfies.
|
|
27
|
+
* SynapticEngine only needs these two methods — no import of the full class.
|
|
28
|
+
*/
|
|
29
|
+
export interface SynapticGraph {
|
|
30
|
+
getAllNodes(): Node[];
|
|
31
|
+
addEdge(edge: Edge): Edge;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Tokenize a text string into a normalised set of meaningful tokens.
|
|
35
|
+
*
|
|
36
|
+
* - Splits camelCase / PascalCase boundaries before lowercasing
|
|
37
|
+
* - Splits on whitespace and common punctuation
|
|
38
|
+
* - Filters tokens shorter than 2 chars
|
|
39
|
+
* - Removes stop-words
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* tokenize("TypeScript generics") → Set { "typescript", "generics" }
|
|
43
|
+
* tokenize("useContextManager") → Set { "context", "manager" }
|
|
44
|
+
*/
|
|
45
|
+
export declare function tokenize(text: string): Set<string>;
|
|
46
|
+
/**
|
|
47
|
+
* Jaccard similarity coefficient between two token sets.
|
|
48
|
+
* Returns 0 when both sets are empty.
|
|
49
|
+
*
|
|
50
|
+
* J(A,B) = |A ∩ B| / |A ∪ B|
|
|
51
|
+
*/
|
|
52
|
+
export declare function jaccardSimilarity(a: Set<string>, b: Set<string>): number;
|
|
53
|
+
/**
|
|
54
|
+
* Cosine similarity between two dense embedding vectors.
|
|
55
|
+
* Returns a value in [−1, 1]. Returns 0 when either vector has zero magnitude.
|
|
56
|
+
*
|
|
57
|
+
* cos(θ) = (A · B) / (|A| × |B|)
|
|
58
|
+
*/
|
|
59
|
+
export declare function cosineSimilarity(a: number[], b: number[]): number;
|
|
60
|
+
/**
|
|
61
|
+
* SynapticEngine — retroactive keyword-based linking between nodes.
|
|
62
|
+
*
|
|
63
|
+
* When a new node enters the graph, `linkRetroactively()` scans **all**
|
|
64
|
+
* existing nodes — regardless of when they were created — and creates
|
|
65
|
+
* RELATES edges wherever the Jaccard similarity between tokenised
|
|
66
|
+
* labels/descriptions meets the configured threshold.
|
|
67
|
+
*
|
|
68
|
+
* This gives WeaveGraph its neuronal behaviour: new concepts automatically
|
|
69
|
+
* form connections with historically relevant knowledge.
|
|
70
|
+
*
|
|
71
|
+
* Produced edges carry `metadata.synapse = true` so callers can distinguish
|
|
72
|
+
* auto-generated synaptic edges from manually created ones.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const engine = new SynapticEngine({ threshold: 0.72, maxConnections: 20 });
|
|
77
|
+
*
|
|
78
|
+
* // Inject into ContextGraphManager
|
|
79
|
+
* graph.setSynapticEngine(engine);
|
|
80
|
+
*
|
|
81
|
+
* // From now on, every addNode() triggers retroactive linking automatically
|
|
82
|
+
* graph.addNode(NodeBuilder.concept("TypeScript generics"));
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare class SynapticEngine {
|
|
86
|
+
private readonly threshold;
|
|
87
|
+
private readonly maxConnections;
|
|
88
|
+
private readonly embeddingService?;
|
|
89
|
+
constructor(options?: SynapticOptions);
|
|
90
|
+
/** Read-only view of the resolved configuration. */
|
|
91
|
+
get config(): Required<Omit<SynapticOptions, "embeddingService">> & {
|
|
92
|
+
hasEmbeddings: boolean;
|
|
93
|
+
};
|
|
94
|
+
/** Whether an embedding service is configured. */
|
|
95
|
+
get hasEmbeddingService(): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Scan all existing nodes in `graph` and create RELATES edges to `newNode`
|
|
98
|
+
* wherever the Jaccard similarity between their tokenised text meets the
|
|
99
|
+
* threshold. At most `maxConnections` edges are created, selecting the
|
|
100
|
+
* highest-similarity candidates first.
|
|
101
|
+
*
|
|
102
|
+
* The new node itself **must already exist** in the graph before calling
|
|
103
|
+
* this method (so that `addEdge` can reference a valid node id).
|
|
104
|
+
*
|
|
105
|
+
* @returns The list of synaptic edges created and added to the graph.
|
|
106
|
+
*/
|
|
107
|
+
linkRetroactively(newNode: Node, graph: SynapticGraph): Edge[];
|
|
108
|
+
/**
|
|
109
|
+
* Embedding-based retroactive linking (async).
|
|
110
|
+
*
|
|
111
|
+
* Requires an `embeddingService` to have been provided at construction time.
|
|
112
|
+
* If none is configured, falls back to keyword-based Jaccard similarity
|
|
113
|
+
* (identical to `linkRetroactively()`).
|
|
114
|
+
*
|
|
115
|
+
* Produced edges carry:
|
|
116
|
+
* - `metadata.synapse: true`
|
|
117
|
+
* - `metadata.similarity: number` — cosine similarity score
|
|
118
|
+
* - `metadata.mode: "embedding"` (or `"keyword"` on fallback)
|
|
119
|
+
*
|
|
120
|
+
* @returns The list of synaptic edges created and added to the graph.
|
|
121
|
+
*/
|
|
122
|
+
linkRetroactivelyEmbedding(newNode: Node, graph: SynapticGraph): Promise<Edge[]>;
|
|
123
|
+
/** Combine label + description into a single text fingerprint for a node. */
|
|
124
|
+
private _nodeText;
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=synaptic-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"synaptic-engine.d.ts","sourceRoot":"","sources":["../src/synaptic-engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAOxC;;;;GAIG;AACH,MAAM,WAAW,wBAAwB;IACvC,gFAAgF;IAChF,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;CACv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