@mnemo-ai/types 3.6.0-alpha.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/dist/index.cjs ADDED
@@ -0,0 +1,342 @@
1
+ 'use strict';
2
+
3
+ var zod = require('zod');
4
+
5
+ /* @mnemo-ai/types — see https://github.com/lucasmailland/mnemosyne */
6
+
7
+ // src/branded.ts
8
+ var asWorkspaceId = (s) => s;
9
+ var asFactId = (s) => s;
10
+ var asDecisionId = (s) => s;
11
+ var asEpisodeId = (s) => s;
12
+ var asEntityId = (s) => s;
13
+ var asStrategyId = (s) => s;
14
+ var asWorkflowId = (s) => s;
15
+ var asSkillId = (s) => s;
16
+ var asTaskId = (s) => s;
17
+ var asEventId = (s) => s;
18
+ var asReasoningId = (s) => s;
19
+ var asSessionId = (s) => s;
20
+ var asRelationId = (s) => s;
21
+ var WORKSPACE_UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
22
+ function isWorkspaceId(s) {
23
+ return WORKSPACE_UUID_RE.test(s);
24
+ }
25
+
26
+ // src/audit.ts
27
+ function resolveAuditActorType(entry) {
28
+ if (entry.actorType) return entry.actorType;
29
+ return entry.actorId ? "user" : "system";
30
+ }
31
+
32
+ // src/ports.ts
33
+ var RECOVERY_POINTS = [
34
+ "started",
35
+ "session_locked",
36
+ "llm_called",
37
+ "facts_persisted",
38
+ "completed"
39
+ ];
40
+
41
+ // src/security.ts
42
+ var SENSITIVITY_ORDER = {
43
+ public: 0,
44
+ // visible to anyone with workspace read access
45
+ internal: 1,
46
+ // workspace-internal, not for external sharing
47
+ confidential: 2,
48
+ // restricted distribution within workspace
49
+ restricted: 3
50
+ // highest tier: need-to-know only
51
+ };
52
+ function sensitivityAtLeast(a, b) {
53
+ return SENSITIVITY_ORDER[a] >= SENSITIVITY_ORDER[b];
54
+ }
55
+ var ALL_SCOPES = [
56
+ "memory:read",
57
+ "memory:write",
58
+ "memory:delete",
59
+ "memory:admin",
60
+ "memory:hard_delete",
61
+ "memory:session",
62
+ "memory:task",
63
+ "memory:event",
64
+ "memory:self_edit",
65
+ "memory:procedural",
66
+ "memory:reasoning",
67
+ "memory:export",
68
+ "memory:governance",
69
+ "memory:subscribe",
70
+ "session:read",
71
+ "session:write",
72
+ "session:manage",
73
+ "entity:read",
74
+ "entity:write",
75
+ "entity:admin",
76
+ "federation:read",
77
+ "federation:write",
78
+ "federation:manage",
79
+ "memory:federation",
80
+ "admin:keys",
81
+ "admin:workspace",
82
+ "admin:billing",
83
+ "admin:tenant"
84
+ ];
85
+ var SPEC_SCOPES = [
86
+ "memory:read",
87
+ "memory:write",
88
+ "memory:delete",
89
+ "memory:hard_delete",
90
+ "memory:session",
91
+ "memory:task",
92
+ "memory:event",
93
+ "memory:self_edit",
94
+ "memory:procedural",
95
+ "memory:reasoning",
96
+ "memory:export",
97
+ "memory:admin",
98
+ "memory:federation",
99
+ "memory:governance",
100
+ "memory:subscribe"
101
+ ];
102
+ var FactKindClause = zod.z.object({
103
+ op: zod.z.literal("fact_kind"),
104
+ kind: zod.z.string().min(1).max(64),
105
+ bind: zod.z.string().regex(/^\$[a-z][a-z0-9_]*$/i).optional()
106
+ }).strict();
107
+ var TopicKeyInClause = zod.z.object({
108
+ op: zod.z.literal("topic_key_in"),
109
+ topicKeys: zod.z.array(zod.z.string().min(1).max(200)).min(1).max(32),
110
+ bind: zod.z.string().regex(/^\$[a-z][a-z0-9_]*$/i).optional()
111
+ }).strict();
112
+ var TextContainsClause = zod.z.object({
113
+ op: zod.z.literal("text_contains"),
114
+ needle: zod.z.string().min(1).max(500),
115
+ bind: zod.z.string().regex(/^\$[a-z][a-z0-9_]*$/i).optional()
116
+ }).strict();
117
+ var RELATION_VERBS = [
118
+ "related",
119
+ "compatible",
120
+ "scoped",
121
+ "conflicts_with",
122
+ "supersedes",
123
+ "not_conflict",
124
+ "derived_from",
125
+ "part_of",
126
+ "member_of"
127
+ ];
128
+ var BIND_RE = /^\$[a-z][a-z0-9_]*$/i;
129
+ var RelationClause = zod.z.object({
130
+ op: zod.z.literal("relation"),
131
+ from: zod.z.string().regex(BIND_RE),
132
+ to: zod.z.string().regex(BIND_RE),
133
+ verb: zod.z.enum(RELATION_VERBS)
134
+ }).strict();
135
+ var NumberOrBind = zod.z.union([zod.z.number(), zod.z.string().regex(BIND_RE)]);
136
+ var FactValueClause = zod.z.object({
137
+ op: zod.z.literal("fact_value"),
138
+ subject: zod.z.string().regex(BIND_RE),
139
+ predicate: zod.z.string().min(1).max(128),
140
+ valueType: zod.z.enum(["duration", "number"]),
141
+ compare: zod.z.object({
142
+ lt: NumberOrBind.optional(),
143
+ gt: NumberOrBind.optional(),
144
+ lte: NumberOrBind.optional(),
145
+ gte: NumberOrBind.optional()
146
+ }).strict().partial().optional(),
147
+ bind: zod.z.string().regex(BIND_RE).optional()
148
+ }).strict();
149
+ var ConditionClause = zod.z.discriminatedUnion("op", [
150
+ FactKindClause,
151
+ TopicKeyInClause,
152
+ TextContainsClause,
153
+ RelationClause,
154
+ FactValueClause
155
+ ]);
156
+ var ConditionDslSchema = zod.z.object({
157
+ all: zod.z.array(ConditionClause).min(1).max(8)
158
+ }).strict();
159
+ var DeriveFactAction = zod.z.object({
160
+ op: zod.z.literal("derive_fact"),
161
+ statementTemplate: zod.z.string().min(1).max(2e3),
162
+ // DSL kind: NOT a valid MnemoFact.kind — stored in metadata.inference.derivedKind.
163
+ // Engine writes kind='other' to mnemo_fact (valid kind) and attribution='inferred'.
164
+ kind: zod.z.string().min(1).max(64).default("inferred"),
165
+ topicKey: zod.z.string().max(200).optional(),
166
+ baseConfidence: zod.z.number().min(0).max(1).default(0.7),
167
+ sensitivity: zod.z.enum(["public", "internal", "confidential", "restricted"]).default("internal"),
168
+ type: zod.z.literal("fact").default("fact")
169
+ }).strict();
170
+ var DeriveWarningAction = zod.z.object({
171
+ op: zod.z.literal("derive_warning"),
172
+ statementTemplate: zod.z.string().min(1).max(2e3),
173
+ kind: zod.z.string().min(1).max(64).default("inferred"),
174
+ topicKey: zod.z.string().max(200).optional(),
175
+ baseConfidence: zod.z.number().min(0).max(1).default(0.7),
176
+ sensitivity: zod.z.enum(["public", "internal", "confidential", "restricted"]).default("internal"),
177
+ type: zod.z.literal("warning").default("warning")
178
+ }).strict();
179
+ var ActionDslSchema = zod.z.discriminatedUnion("op", [DeriveFactAction, DeriveWarningAction]);
180
+ function parseConditionDsl(raw) {
181
+ return ConditionDslSchema.parse(raw);
182
+ }
183
+ function parseActionDsl(raw) {
184
+ return ActionDslSchema.parse(raw);
185
+ }
186
+
187
+ // src/observability.ts
188
+ var MNEMO_ERROR_CODES = {
189
+ // Storage errors
190
+ STORAGE_NOT_FOUND: "mnemo.storage.not_found",
191
+ STORAGE_CONFLICT: "mnemo.storage.conflict",
192
+ STORAGE_CONSTRAINT: "mnemo.storage.constraint_violation",
193
+ STORAGE_TIMEOUT: "mnemo.storage.timeout",
194
+ // Auth errors
195
+ AUTH_MISSING_KEY: "mnemo.auth.missing_key",
196
+ AUTH_EXPIRED_KEY: "mnemo.auth.expired_key",
197
+ AUTH_INSUFFICIENT_SCOPE: "mnemo.auth.insufficient_scope",
198
+ AUTH_WORKSPACE_MISMATCH: "mnemo.auth.workspace_mismatch",
199
+ // Recall errors
200
+ RECALL_BUDGET_EXCEEDED: "mnemo.recall.budget_exceeded",
201
+ RECALL_VECTOR_MISSING: "mnemo.recall.vector_missing",
202
+ RECALL_UPSTREAM_TIMEOUT: "mnemo.recall.upstream_timeout",
203
+ // Composition errors
204
+ COMPOSE_DSL_INVALID: "mnemo.compose.dsl_invalid",
205
+ COMPOSE_MAX_FIRINGS: "mnemo.compose.max_firings_reached",
206
+ COMPOSE_MATERIALISE_FAIL: "mnemo.compose.materialise_failed",
207
+ // Self-edit errors
208
+ SELF_EDIT_BUDGET: "mnemo.self_edit.budget_exceeded",
209
+ SELF_EDIT_COOLDOWN: "mnemo.self_edit.cooldown_active",
210
+ SELF_EDIT_SCOPE: "mnemo.self_edit.scope_denied",
211
+ SELF_EDIT_ABUSE_FLAG: "mnemo.self_edit.abuse_flagged",
212
+ SELF_EDIT_WORTH_FROZEN: "mnemo.self_edit.worth_frozen",
213
+ // Federation errors
214
+ FED_SIGNATURE_INVALID: "mnemo.federation.signature_invalid",
215
+ FED_VERSION_SKEW: "mnemo.federation.version_skew",
216
+ FED_QUORUM_UNREACHABLE: "mnemo.federation.quorum_unreachable",
217
+ // Procedural errors
218
+ PROC_BUDGET_BELOW_GATE: "mnemo.procedural.budget_below_gate",
219
+ PROC_NO_TRAJECTORIES: "mnemo.procedural.no_trajectories",
220
+ // General
221
+ RATE_LIMIT: "mnemo.rate_limit",
222
+ VALIDATION: "mnemo.validation",
223
+ INTERNAL: "mnemo.internal"
224
+ };
225
+ var MNEMO_SPAN_NAMES = {
226
+ recall: "mnemo.recall",
227
+ remember: "mnemo.remember",
228
+ /** Spec alias of `remember` (spec calls this span "mnemo.write"). */
229
+ write: "mnemo.remember",
230
+ sessionCollapse: "mnemo.session.collapse",
231
+ reasoningResolve: "mnemo.reasoning.resolve",
232
+ federationSync: "mnemo.federation.sync",
233
+ /** Auth middleware span — wraps the bearer-token resolution path. */
234
+ auth: "mnemo.auth",
235
+ /** Embedding span — wraps embedSingle / embed batch calls. */
236
+ embed: "mnemo.embed",
237
+ /** Re-ranking span — wraps RecMem rerank on the cold path. */
238
+ rerank: "mnemo.rerank",
239
+ /** Composition span — wraps composeInferences on the cold path. */
240
+ composition: "mnemo.composition",
241
+ /** Render span — wraps renderFactsCompact at the call site. */
242
+ render: "mnemo.render",
243
+ plugin: (name) => `mnemo.plugin.${name}`
244
+ };
245
+
246
+ // src/delta-text.ts
247
+ var DELTA_MAGIC_V1 = "MNEMO_DELTA_V1";
248
+ var FULL_MAGIC_V1 = "MNEMO_FULL_V1";
249
+ function toLines(s) {
250
+ return s.split("\n");
251
+ }
252
+ function encodeDelta(parent, child) {
253
+ const p = toLines(parent);
254
+ const c = toLines(child);
255
+ let prefix = 0;
256
+ const maxPrefix = Math.min(p.length, c.length);
257
+ while (prefix < maxPrefix && p[prefix] === c[prefix]) prefix++;
258
+ let suffix = 0;
259
+ const maxSuffix = Math.min(p.length, c.length) - prefix;
260
+ while (suffix < maxSuffix && p[p.length - 1 - suffix] === c[c.length - 1 - suffix]) {
261
+ suffix++;
262
+ }
263
+ const middle = c.slice(prefix, c.length - suffix);
264
+ const header = `${DELTA_MAGIC_V1} ${prefix} ${suffix}`;
265
+ const residual = [header, ...middle].join("\n");
266
+ const full = `${FULL_MAGIC_V1}
267
+ ${child}`;
268
+ return residual.length < full.length ? residual : full;
269
+ }
270
+ function applyDelta(parent, delta) {
271
+ if (delta.startsWith(`${FULL_MAGIC_V1}
272
+ `)) {
273
+ return delta.slice(FULL_MAGIC_V1.length + 1);
274
+ }
275
+ if (delta === FULL_MAGIC_V1) {
276
+ return "";
277
+ }
278
+ if (!delta.startsWith(`${DELTA_MAGIC_V1} `)) {
279
+ return delta;
280
+ }
281
+ const newlineIdx = delta.indexOf("\n");
282
+ const headerLine = newlineIdx === -1 ? delta : delta.slice(0, newlineIdx);
283
+ const body = newlineIdx === -1 ? "" : delta.slice(newlineIdx + 1);
284
+ const parts = headerLine.split(" ");
285
+ const prefix = Number(parts[1]);
286
+ const suffix = Number(parts[2]);
287
+ if (!Number.isInteger(prefix) || !Number.isInteger(suffix)) {
288
+ throw new Error(`applyDelta: malformed ${DELTA_MAGIC_V1} header "${headerLine}"`);
289
+ }
290
+ const p = toLines(parent);
291
+ const middle = newlineIdx === -1 ? [] : toLines(body);
292
+ const head = p.slice(0, prefix);
293
+ const tail = suffix === 0 ? [] : p.slice(p.length - suffix);
294
+ return [...head, ...middle, ...tail].join("\n");
295
+ }
296
+
297
+ // src/embedding.ts
298
+ function generateSpaceId(space) {
299
+ const parts = [
300
+ space.provider,
301
+ space.model,
302
+ space.modelRevision ?? "v1",
303
+ String(space.dimensions),
304
+ space.distanceMetric ?? "cosine",
305
+ space.dtype ?? "float32"
306
+ ];
307
+ return parts.join(":").toLowerCase();
308
+ }
309
+
310
+ exports.ALL_SCOPES = ALL_SCOPES;
311
+ exports.ActionDslSchema = ActionDslSchema;
312
+ exports.ConditionDslSchema = ConditionDslSchema;
313
+ exports.DELTA_MAGIC_V1 = DELTA_MAGIC_V1;
314
+ exports.FULL_MAGIC_V1 = FULL_MAGIC_V1;
315
+ exports.MNEMO_ERROR_CODES = MNEMO_ERROR_CODES;
316
+ exports.MNEMO_SPAN_NAMES = MNEMO_SPAN_NAMES;
317
+ exports.RECOVERY_POINTS = RECOVERY_POINTS;
318
+ exports.SENSITIVITY_ORDER = SENSITIVITY_ORDER;
319
+ exports.SPEC_SCOPES = SPEC_SCOPES;
320
+ exports.applyDelta = applyDelta;
321
+ exports.asDecisionId = asDecisionId;
322
+ exports.asEntityId = asEntityId;
323
+ exports.asEpisodeId = asEpisodeId;
324
+ exports.asEventId = asEventId;
325
+ exports.asFactId = asFactId;
326
+ exports.asReasoningId = asReasoningId;
327
+ exports.asRelationId = asRelationId;
328
+ exports.asSessionId = asSessionId;
329
+ exports.asSkillId = asSkillId;
330
+ exports.asStrategyId = asStrategyId;
331
+ exports.asTaskId = asTaskId;
332
+ exports.asWorkflowId = asWorkflowId;
333
+ exports.asWorkspaceId = asWorkspaceId;
334
+ exports.encodeDelta = encodeDelta;
335
+ exports.generateSpaceId = generateSpaceId;
336
+ exports.isWorkspaceId = isWorkspaceId;
337
+ exports.parseActionDsl = parseActionDsl;
338
+ exports.parseConditionDsl = parseConditionDsl;
339
+ exports.resolveAuditActorType = resolveAuditActorType;
340
+ exports.sensitivityAtLeast = sensitivityAtLeast;
341
+ //# sourceMappingURL=index.cjs.map
342
+ //# sourceMappingURL=index.cjs.map