@ai2070/memex 0.9.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.
Files changed (46) hide show
  1. package/.github/workflows/ci.yml +31 -0
  2. package/.github/workflows/release.yml +35 -0
  3. package/API.md +1078 -0
  4. package/LICENSE +190 -0
  5. package/README.md +574 -0
  6. package/package.json +30 -0
  7. package/src/bulk.ts +128 -0
  8. package/src/envelope.ts +52 -0
  9. package/src/errors.ts +27 -0
  10. package/src/graph.ts +15 -0
  11. package/src/helpers.ts +51 -0
  12. package/src/index.ts +142 -0
  13. package/src/integrity.ts +378 -0
  14. package/src/intent.ts +311 -0
  15. package/src/query.ts +357 -0
  16. package/src/reducer.ts +177 -0
  17. package/src/replay.ts +32 -0
  18. package/src/retrieval.ts +306 -0
  19. package/src/serialization.ts +34 -0
  20. package/src/stats.ts +62 -0
  21. package/src/task.ts +373 -0
  22. package/src/transplant.ts +488 -0
  23. package/src/types.ts +248 -0
  24. package/tests/bugfix-and-coverage.test.ts +958 -0
  25. package/tests/bugfix-holes.test.ts +856 -0
  26. package/tests/bulk.test.ts +256 -0
  27. package/tests/edge-cases-v2.test.ts +355 -0
  28. package/tests/edge-cases.test.ts +661 -0
  29. package/tests/envelope.test.ts +92 -0
  30. package/tests/graph.test.ts +41 -0
  31. package/tests/helpers.test.ts +120 -0
  32. package/tests/integrity.test.ts +371 -0
  33. package/tests/intent.test.ts +276 -0
  34. package/tests/query-advanced.test.ts +252 -0
  35. package/tests/query.test.ts +623 -0
  36. package/tests/reducer.test.ts +342 -0
  37. package/tests/replay.test.ts +145 -0
  38. package/tests/retrieval.test.ts +691 -0
  39. package/tests/serialization.test.ts +118 -0
  40. package/tests/setup.test.ts +7 -0
  41. package/tests/stats.test.ts +163 -0
  42. package/tests/task.test.ts +322 -0
  43. package/tests/transplant.test.ts +385 -0
  44. package/tests/types.test.ts +231 -0
  45. package/tsconfig.json +18 -0
  46. package/vitest.config.ts +7 -0
package/src/types.ts ADDED
@@ -0,0 +1,248 @@
1
+ // ---------------------------------------------------------------------------
2
+ // Memory Item Kind — what the item *is*
3
+ // ---------------------------------------------------------------------------
4
+
5
+ export type KnownMemoryKind =
6
+ | "observation"
7
+ | "assertion"
8
+ | "assumption"
9
+ | "hypothesis"
10
+ | "derivation"
11
+ | "simulation"
12
+ | "policy"
13
+ | "trait";
14
+
15
+ export type MemoryKind = KnownMemoryKind | (string & {});
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Source Kind — how the item *got here*
19
+ // ---------------------------------------------------------------------------
20
+
21
+ export type KnownSourceKind =
22
+ | "user_explicit"
23
+ | "observed"
24
+ | "derived_deterministic"
25
+ | "agent_inferred"
26
+ | "simulated"
27
+ | "imported";
28
+
29
+ export type SourceKind = KnownSourceKind | (string & {});
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // MemoryItem (the core node type)
33
+ // ---------------------------------------------------------------------------
34
+
35
+ export interface MemoryItem {
36
+ id: string;
37
+ scope: string;
38
+ kind: MemoryKind;
39
+ content: Record<string, unknown>;
40
+
41
+ author: string;
42
+ source_kind: SourceKind;
43
+ parents?: string[]; // item ids this was derived/inferred from
44
+
45
+ authority: number; // 0..1 -- how much should the system trust this?
46
+ conviction?: number; // 0..1 -- how sure was the author?
47
+ importance?: number; // 0..1 -- how much attention does this need right now? (salience)
48
+
49
+ meta?: {
50
+ agent_id?: string;
51
+ session_id?: string;
52
+ [key: string]: unknown;
53
+ };
54
+ }
55
+
56
+ // ---------------------------------------------------------------------------
57
+ // Edge
58
+ // ---------------------------------------------------------------------------
59
+
60
+ export type KnownEdgeKind =
61
+ | "DERIVED_FROM"
62
+ | "CONTRADICTS"
63
+ | "SUPPORTS"
64
+ | "ABOUT"
65
+ | "SUPERSEDES"
66
+ | "ALIAS";
67
+
68
+ export type EdgeKind = KnownEdgeKind | (string & {});
69
+
70
+ export interface Edge {
71
+ edge_id: string;
72
+ from: string;
73
+ to: string;
74
+ kind: EdgeKind;
75
+
76
+ weight?: number;
77
+
78
+ author: string;
79
+ source_kind: SourceKind;
80
+ authority: number;
81
+ active: boolean;
82
+
83
+ meta?: Record<string, unknown>;
84
+ }
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Event Envelope
88
+ // ---------------------------------------------------------------------------
89
+
90
+ export type KnownNamespace =
91
+ | "memory"
92
+ | "task"
93
+ | "agent"
94
+ | "tool"
95
+ | "net"
96
+ | "app"
97
+ | "chat"
98
+ | "system"
99
+ | "debug";
100
+
101
+ export type Namespace = KnownNamespace | (string & {});
102
+
103
+ export interface EventEnvelope<T = unknown> {
104
+ id: string;
105
+ namespace: Namespace;
106
+ type: string;
107
+ ts: string;
108
+ trace_id?: string;
109
+ payload: T;
110
+ }
111
+
112
+ // ---------------------------------------------------------------------------
113
+ // Graph State
114
+ // ---------------------------------------------------------------------------
115
+
116
+ export interface GraphState {
117
+ items: Map<string, MemoryItem>;
118
+ edges: Map<string, Edge>;
119
+ }
120
+
121
+ // ---------------------------------------------------------------------------
122
+ // Memory Commands (into MemEX)
123
+ // ---------------------------------------------------------------------------
124
+
125
+ export type MemoryCommand =
126
+ | { type: "memory.create"; item: MemoryItem }
127
+ | {
128
+ type: "memory.update";
129
+ item_id: string;
130
+ partial: Partial<MemoryItem>;
131
+ author: string;
132
+ reason?: string;
133
+ basis?: Record<string, unknown>;
134
+ }
135
+ | { type: "memory.retract"; item_id: string; author: string; reason?: string }
136
+ | { type: "edge.create"; edge: Edge }
137
+ | {
138
+ type: "edge.update";
139
+ edge_id: string;
140
+ partial: Partial<Edge>;
141
+ author: string;
142
+ reason?: string;
143
+ }
144
+ | { type: "edge.retract"; edge_id: string; author: string; reason?: string };
145
+
146
+ // ---------------------------------------------------------------------------
147
+ // Memory Lifecycle Events (out of applyCommand)
148
+ // ---------------------------------------------------------------------------
149
+
150
+ export type LifecycleEventType =
151
+ | "memory.created"
152
+ | "memory.updated"
153
+ | "memory.retracted"
154
+ | "edge.created"
155
+ | "edge.updated"
156
+ | "edge.retracted";
157
+
158
+ export interface MemoryLifecycleEvent {
159
+ namespace: "memory";
160
+ type: LifecycleEventType;
161
+ item?: MemoryItem;
162
+ edge?: Edge;
163
+ cause_type?: string;
164
+ }
165
+
166
+ // ---------------------------------------------------------------------------
167
+ // Filters
168
+ // ---------------------------------------------------------------------------
169
+
170
+ export interface MemoryFilter {
171
+ ids?: string[]; // match any of these item ids
172
+ scope?: string; // exact match
173
+ scope_prefix?: string; // starts with, e.g. "project:"
174
+ author?: string;
175
+ kind?: MemoryKind;
176
+ source_kind?: SourceKind;
177
+
178
+ range?: {
179
+ authority?: { min?: number; max?: number };
180
+ conviction?: { min?: number; max?: number };
181
+ importance?: { min?: number; max?: number };
182
+ };
183
+
184
+ has_parent?: string; // sugar for parents.includes
185
+ is_root?: boolean; // sugar for parents.count.max = 0
186
+ parents?: {
187
+ includes?: string;
188
+ includes_any?: string[];
189
+ includes_all?: string[];
190
+ count?: { min?: number; max?: number };
191
+ };
192
+
193
+ decay?: {
194
+ config: DecayConfig;
195
+ min: number; // 0..1 — minimum decay multiplier to keep
196
+ };
197
+ created?: {
198
+ before?: number; // unix ms
199
+ after?: number; // unix ms
200
+ };
201
+ not?: MemoryFilter;
202
+ meta?: Record<string, unknown>; // dot-path exact match
203
+ meta_has?: string[]; // dot-paths that must exist
204
+ or?: MemoryFilter[];
205
+ }
206
+
207
+ export type SortField = "authority" | "conviction" | "importance" | "recency";
208
+
209
+ export interface SortOption {
210
+ field: SortField;
211
+ order: "asc" | "desc";
212
+ }
213
+
214
+ export interface QueryOptions {
215
+ sort?: SortOption | SortOption[]; // single or multi-sort (first = primary)
216
+ limit?: number;
217
+ offset?: number;
218
+ }
219
+
220
+ export type DecayInterval = "hour" | "day" | "week";
221
+ export type DecayType = "exponential" | "linear" | "step";
222
+
223
+ export interface DecayConfig {
224
+ rate: number; // 0..1 — how much to decay per interval
225
+ interval: DecayInterval;
226
+ type: DecayType;
227
+ }
228
+
229
+ export interface ScoreWeights {
230
+ authority?: number;
231
+ conviction?: number;
232
+ importance?: number;
233
+ decay?: DecayConfig;
234
+ }
235
+
236
+ export interface ScoredItem {
237
+ item: MemoryItem;
238
+ score: number;
239
+ contradicted_by?: MemoryItem[]; // present when contradictions are surfaced
240
+ }
241
+
242
+ export interface EdgeFilter {
243
+ from?: string;
244
+ to?: string;
245
+ kind?: EdgeKind;
246
+ min_weight?: number;
247
+ active_only?: boolean;
248
+ }