@jungjaehoon/mama-core 1.6.0 → 1.8.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.
Files changed (53) hide show
  1. package/db/migrations/037-create-context-packets.sql +32 -0
  2. package/db/migrations/038-create-vnext-operator-contracts.sql +57 -0
  3. package/db/migrations/039-add-connector-event-operator-ingest-seq.sql +96 -0
  4. package/db/migrations/040-create-operator-memory-commit-intents.sql +19 -0
  5. package/db/migrations/041-enforce-operator-memory-commit-claim-invariant.sql +74 -0
  6. package/db/migrations/042-embedding-prefix-scheme.sql +46 -0
  7. package/dist/cases/wiki-page-index.js +6 -2
  8. package/dist/context-compile/boundary-defaults.d.ts +12 -0
  9. package/dist/context-compile/boundary-defaults.js +105 -0
  10. package/dist/context-compile/compiler-policy.d.ts +39 -0
  11. package/dist/context-compile/compiler-policy.js +147 -0
  12. package/dist/context-compile/compiler.d.ts +18 -0
  13. package/dist/context-compile/compiler.js +401 -0
  14. package/dist/context-compile/index.d.ts +9 -0
  15. package/dist/context-compile/index.js +25 -0
  16. package/dist/context-compile/packet-store.d.ts +9 -0
  17. package/dist/context-compile/packet-store.js +255 -0
  18. package/dist/context-compile/ref.d.ts +7 -0
  19. package/dist/context-compile/ref.js +102 -0
  20. package/dist/context-compile/source-readers.d.ts +69 -0
  21. package/dist/context-compile/source-readers.js +707 -0
  22. package/dist/context-compile/types.d.ts +138 -0
  23. package/dist/context-compile/types.js +11 -0
  24. package/dist/context-compile/visibility.d.ts +21 -0
  25. package/dist/context-compile/visibility.js +200 -0
  26. package/dist/db-adapter/node-sqlite-adapter.d.ts +17 -1
  27. package/dist/db-adapter/node-sqlite-adapter.js +399 -6
  28. package/dist/db-manager.d.ts +9 -10
  29. package/dist/db-manager.js +59 -4
  30. package/dist/edges/ref-validation.js +55 -16
  31. package/dist/edges/types.d.ts +2 -0
  32. package/dist/embedding-client.d.ts +3 -1
  33. package/dist/embedding-client.js +3 -2
  34. package/dist/embedding-server/index.d.ts +2 -0
  35. package/dist/embedding-server/index.js +10 -2
  36. package/dist/embeddings.d.ts +8 -3
  37. package/dist/embeddings.js +48 -15
  38. package/dist/entities/store.d.ts +4 -1
  39. package/dist/entities/store.js +206 -78
  40. package/dist/entities/types.d.ts +5 -1
  41. package/dist/entities/types.js +1 -1
  42. package/dist/index.d.ts +4 -2
  43. package/dist/index.js +8 -4
  44. package/dist/mama-api.js +1 -1
  45. package/dist/memory/api.d.ts +6 -1
  46. package/dist/memory/api.js +223 -29
  47. package/dist/memory/provenance.d.ts +1 -0
  48. package/dist/memory/provenance.js +2 -0
  49. package/dist/memory/types.d.ts +1 -0
  50. package/dist/memory-inject.js +1 -1
  51. package/dist/provenance/source-ref.d.ts +29 -0
  52. package/dist/provenance/source-ref.js +204 -0
  53. package/package.json +4 -2
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EntityMergeError = void 0;
4
+ exports.clearEntityTableColumnCache = clearEntityTableColumnCache;
4
5
  exports.parseObservationRow = parseObservationRow;
5
6
  exports.createEntityNode = createEntityNode;
6
7
  exports.getEntityNode = getEntityNode;
@@ -25,6 +26,13 @@ class EntityMergeError extends Error {
25
26
  }
26
27
  exports.EntityMergeError = EntityMergeError;
27
28
  const MAX_MERGE_CHAIN_DEPTH = 8;
29
+ const OBSERVATION_CONTEXT_KEY_COLUMNS = [
30
+ 'workspace_context_key',
31
+ 'channel_context_key',
32
+ 'thread_context_key',
33
+ 'actor_context_key',
34
+ ];
35
+ const tableColumnCache = new WeakMap();
28
36
  function now() {
29
37
  return Date.now();
30
38
  }
@@ -32,6 +40,58 @@ function normalizeSourceLocator(value) {
32
40
  // Write empty-string as the null sentinel so SQLite UNIQUE can collapse null-like locators; parseObservationRow maps it back to null on read.
33
41
  return value ?? '';
34
42
  }
43
+ function normalizeContextKey(value) {
44
+ return value ?? '';
45
+ }
46
+ function sourceLocatorForInput(input) {
47
+ return normalizeSourceLocator(input.source_locator);
48
+ }
49
+ function channelContextKey(input) {
50
+ if (input.channel_context_key !== undefined) {
51
+ return normalizeContextKey(input.channel_context_key);
52
+ }
53
+ if (input.scope_kind !== 'channel' || !input.scope_id) {
54
+ return '';
55
+ }
56
+ return input.scope_id.startsWith(`${input.source_connector}:`)
57
+ ? input.scope_id
58
+ : `${input.source_connector}:${input.scope_id}`;
59
+ }
60
+ function actorContextKey(input) {
61
+ if (input.actor_context_key !== undefined) {
62
+ return normalizeContextKey(input.actor_context_key);
63
+ }
64
+ if (input.observation_type === 'author') {
65
+ return input.surface_form;
66
+ }
67
+ return input.related_surface_forms.find((value) => value.trim().length > 0) ?? '';
68
+ }
69
+ function observationContextKeys(input) {
70
+ return {
71
+ workspace_context_key: normalizeContextKey(input.workspace_context_key ?? input.source_connector),
72
+ channel_context_key: channelContextKey(input),
73
+ thread_context_key: normalizeContextKey(input.thread_context_key),
74
+ actor_context_key: actorContextKey(input),
75
+ };
76
+ }
77
+ function tableColumnNames(adapter, tableName) {
78
+ let adapterCache = tableColumnCache.get(adapter);
79
+ if (!adapterCache) {
80
+ adapterCache = new Map();
81
+ tableColumnCache.set(adapter, adapterCache);
82
+ }
83
+ const cached = adapterCache.get(tableName);
84
+ if (cached) {
85
+ return cached;
86
+ }
87
+ const rows = adapter.prepare(`PRAGMA table_info(${tableName})`).all();
88
+ const columns = new Set(rows.map((row) => row.name));
89
+ adapterCache.set(tableName, columns);
90
+ return columns;
91
+ }
92
+ function clearEntityTableColumnCache(adapter) {
93
+ tableColumnCache.delete(adapter);
94
+ }
35
95
  function requireStringField(row, field) {
36
96
  if (typeof row[field] !== 'string' || row[field].length === 0) {
37
97
  throw new Error(`Invalid entity observation row: ${field} must be a non-empty string`);
@@ -134,6 +194,10 @@ function parseObservationRow(row) {
134
194
  source_connector: requireStringField(row, 'source_connector'),
135
195
  source_locator: sourceLocator && sourceLocator.length > 0 ? sourceLocator : null,
136
196
  source_raw_record_id: requireStringField(row, 'source_raw_record_id'),
197
+ workspace_context_key: optionalStringFieldIfPresent(row, 'workspace_context_key'),
198
+ channel_context_key: optionalStringFieldIfPresent(row, 'channel_context_key'),
199
+ thread_context_key: optionalStringFieldIfPresent(row, 'thread_context_key'),
200
+ actor_context_key: optionalStringFieldIfPresent(row, 'actor_context_key'),
137
201
  created_at: createdAt,
138
202
  };
139
203
  }
@@ -226,44 +290,152 @@ function listEntityAliases(entityId, adapter = (0, db_manager_js_1.getAdapter)()
226
290
  created_at: Number(row.created_at),
227
291
  }));
228
292
  }
229
- async function upsertEntityObservation(input) {
230
- await (0, db_manager_js_1.initDB)();
231
- const adapter = (0, db_manager_js_1.getAdapter)();
232
- const createdAt = now();
233
- const normalizedSourceLocator = normalizeSourceLocator(input.source_locator);
293
+ function observationContextColumns(columns) {
294
+ return OBSERVATION_CONTEXT_KEY_COLUMNS.filter((column) => columns.has(column));
295
+ }
296
+ function observationInsertValues(input, normalizedSourceLocator, contextColumns, contextKeys, createdAt) {
297
+ const columns = [
298
+ 'id',
299
+ 'observation_type',
300
+ 'entity_kind_hint',
301
+ 'surface_form',
302
+ 'normalized_form',
303
+ 'lang',
304
+ 'script',
305
+ 'context_summary',
306
+ 'related_surface_forms',
307
+ 'timestamp_observed',
308
+ 'scope_kind',
309
+ 'scope_id',
310
+ 'extractor_version',
311
+ 'embedding_model_version',
312
+ 'source_connector',
313
+ 'source_locator',
314
+ 'source_raw_record_id',
315
+ ...contextColumns,
316
+ 'created_at',
317
+ ];
318
+ const values = [
319
+ input.id,
320
+ input.observation_type,
321
+ input.entity_kind_hint,
322
+ input.surface_form,
323
+ input.normalized_form,
324
+ input.lang,
325
+ input.script,
326
+ input.context_summary,
327
+ JSON.stringify(input.related_surface_forms),
328
+ input.timestamp_observed,
329
+ input.scope_kind,
330
+ input.scope_id,
331
+ input.extractor_version,
332
+ input.embedding_model_version,
333
+ input.source_connector,
334
+ normalizedSourceLocator,
335
+ input.source_raw_record_id,
336
+ ...contextColumns.map((column) => contextKeys[column]),
337
+ createdAt,
338
+ ];
339
+ return { columns, values };
340
+ }
341
+ function findExistingObservation(adapter, input, normalizedSourceLocator, contextColumns, contextKeys) {
342
+ const conditions = [
343
+ 'source_connector = ?',
344
+ 'source_locator = ?',
345
+ 'source_raw_record_id = ?',
346
+ 'observation_type = ?',
347
+ ...contextColumns.map((column) => `COALESCE(${column}, '') = ?`),
348
+ ];
349
+ const params = [
350
+ input.source_connector,
351
+ normalizedSourceLocator,
352
+ input.source_raw_record_id,
353
+ input.observation_type,
354
+ ...contextColumns.map((column) => contextKeys[column]),
355
+ ];
356
+ return adapter
357
+ .prepare(`
358
+ SELECT id FROM entity_observations
359
+ WHERE ${conditions.join(' AND ')}
360
+ ORDER BY created_at ASC, id ASC
361
+ LIMIT 1
362
+ `)
363
+ .get(...params);
364
+ }
365
+ function insertObservation(adapter, input, normalizedSourceLocator, contextColumns, contextKeys, createdAt) {
366
+ const insert = observationInsertValues(input, normalizedSourceLocator, contextColumns, contextKeys, createdAt);
367
+ const placeholders = insert.columns.map(() => '?').join(', ');
234
368
  adapter
235
369
  .prepare(`
236
- INSERT INTO entity_observations (
237
- id, observation_type, entity_kind_hint, surface_form, normalized_form, lang, script,
238
- context_summary, related_surface_forms, timestamp_observed, scope_kind, scope_id,
239
- extractor_version, embedding_model_version, source_connector, source_locator,
240
- source_raw_record_id, created_at
241
- )
242
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
243
- ON CONFLICT(source_connector, source_locator, source_raw_record_id, observation_type)
244
- DO UPDATE SET
245
- entity_kind_hint = excluded.entity_kind_hint,
246
- surface_form = excluded.surface_form,
247
- normalized_form = excluded.normalized_form,
248
- lang = excluded.lang,
249
- script = excluded.script,
250
- context_summary = excluded.context_summary,
251
- related_surface_forms = excluded.related_surface_forms,
252
- timestamp_observed = excluded.timestamp_observed,
253
- scope_kind = excluded.scope_kind,
254
- scope_id = excluded.scope_id,
255
- extractor_version = excluded.extractor_version,
256
- embedding_model_version = excluded.embedding_model_version,
257
- source_locator = excluded.source_locator,
258
- created_at = entity_observations.created_at
370
+ INSERT INTO entity_observations (${insert.columns.join(', ')})
371
+ VALUES (${placeholders})
259
372
  `)
260
- .run(input.id, input.observation_type, input.entity_kind_hint, input.surface_form, input.normalized_form, input.lang, input.script, input.context_summary, JSON.stringify(input.related_surface_forms), input.timestamp_observed, input.scope_kind, input.scope_id, input.extractor_version, input.embedding_model_version, input.source_connector, normalizedSourceLocator, input.source_raw_record_id, createdAt);
261
- const saved = adapter
373
+ .run(...insert.values);
374
+ }
375
+ function updateObservation(adapter, id, input, normalizedSourceLocator, contextColumns, contextKeys) {
376
+ const assignments = [
377
+ 'entity_kind_hint = ?',
378
+ 'surface_form = ?',
379
+ 'normalized_form = ?',
380
+ 'lang = ?',
381
+ 'script = ?',
382
+ 'context_summary = ?',
383
+ 'related_surface_forms = ?',
384
+ 'timestamp_observed = ?',
385
+ 'scope_kind = ?',
386
+ 'scope_id = ?',
387
+ 'extractor_version = ?',
388
+ 'embedding_model_version = ?',
389
+ 'source_locator = ?',
390
+ ...contextColumns.map((column) => `${column} = ?`),
391
+ ];
392
+ const values = [
393
+ input.entity_kind_hint,
394
+ input.surface_form,
395
+ input.normalized_form,
396
+ input.lang,
397
+ input.script,
398
+ input.context_summary,
399
+ JSON.stringify(input.related_surface_forms),
400
+ input.timestamp_observed,
401
+ input.scope_kind,
402
+ input.scope_id,
403
+ input.extractor_version,
404
+ input.embedding_model_version,
405
+ normalizedSourceLocator,
406
+ ...contextColumns.map((column) => contextKeys[column]),
407
+ id,
408
+ ];
409
+ adapter
262
410
  .prepare(`
263
- SELECT * FROM entity_observations
264
- WHERE source_connector = ? AND source_locator = ? AND source_raw_record_id = ? AND observation_type = ?
411
+ UPDATE entity_observations
412
+ SET ${assignments.join(', ')}
413
+ WHERE id = ?
265
414
  `)
266
- .get(input.source_connector, normalizedSourceLocator, input.source_raw_record_id, input.observation_type);
415
+ .run(...values);
416
+ }
417
+ function saveObservation(adapter, input) {
418
+ const columns = tableColumnNames(adapter, 'entity_observations');
419
+ const contextColumns = observationContextColumns(columns);
420
+ const contextKeys = observationContextKeys(input);
421
+ const normalizedSourceLocator = sourceLocatorForInput(input);
422
+ const existing = findExistingObservation(adapter, input, normalizedSourceLocator, contextColumns, contextKeys);
423
+ if (existing) {
424
+ const id = requireStringField(existing, 'id');
425
+ updateObservation(adapter, id, input, normalizedSourceLocator, contextColumns, contextKeys);
426
+ return { id, created: false };
427
+ }
428
+ insertObservation(adapter, input, normalizedSourceLocator, contextColumns, contextKeys, now());
429
+ return { id: input.id, created: true };
430
+ }
431
+ async function upsertEntityObservation(input) {
432
+ await (0, db_manager_js_1.initDB)();
433
+ const adapter = (0, db_manager_js_1.getAdapter)();
434
+ const result = saveObservation(adapter, input);
435
+ const saved = adapter.prepare('SELECT * FROM entity_observations WHERE id = ?').get(result.id);
436
+ if (!saved) {
437
+ throw new Error(`Failed to load saved entity observation ${result.id}`);
438
+ }
267
439
  return parseObservationRow(saved);
268
440
  }
269
441
  async function upsertEntityObservations(inputs) {
@@ -271,51 +443,7 @@ async function upsertEntityObservations(inputs) {
271
443
  const adapter = (0, db_manager_js_1.getAdapter)();
272
444
  const observations = [];
273
445
  const upsertOne = (input) => {
274
- const normalizedSourceLocator = normalizeSourceLocator(input.source_locator);
275
- const existing = adapter
276
- .prepare(`
277
- SELECT id FROM entity_observations
278
- WHERE source_connector = ? AND source_locator = ? AND source_raw_record_id = ? AND observation_type = ?
279
- `)
280
- .get(input.source_connector, normalizedSourceLocator, input.source_raw_record_id, input.observation_type);
281
- const createdAt = now();
282
- adapter
283
- .prepare(`
284
- INSERT INTO entity_observations (
285
- id, observation_type, entity_kind_hint, surface_form, normalized_form, lang, script,
286
- context_summary, related_surface_forms, timestamp_observed, scope_kind, scope_id,
287
- extractor_version, embedding_model_version, source_connector, source_locator,
288
- source_raw_record_id, created_at
289
- )
290
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
291
- ON CONFLICT(source_connector, source_locator, source_raw_record_id, observation_type)
292
- DO UPDATE SET
293
- entity_kind_hint = excluded.entity_kind_hint,
294
- surface_form = excluded.surface_form,
295
- normalized_form = excluded.normalized_form,
296
- lang = excluded.lang,
297
- script = excluded.script,
298
- context_summary = excluded.context_summary,
299
- related_surface_forms = excluded.related_surface_forms,
300
- timestamp_observed = excluded.timestamp_observed,
301
- scope_kind = excluded.scope_kind,
302
- scope_id = excluded.scope_id,
303
- extractor_version = excluded.extractor_version,
304
- embedding_model_version = excluded.embedding_model_version,
305
- source_locator = excluded.source_locator,
306
- created_at = entity_observations.created_at
307
- `)
308
- .run(input.id, input.observation_type, input.entity_kind_hint, input.surface_form, input.normalized_form, input.lang, input.script, input.context_summary, JSON.stringify(input.related_surface_forms), input.timestamp_observed, input.scope_kind, input.scope_id, input.extractor_version, input.embedding_model_version, input.source_connector, normalizedSourceLocator, input.source_raw_record_id, createdAt);
309
- const saved = adapter
310
- .prepare(`
311
- SELECT * FROM entity_observations
312
- WHERE source_connector = ? AND source_locator = ? AND source_raw_record_id = ? AND observation_type = ?
313
- `)
314
- .get(input.source_connector, normalizedSourceLocator, input.source_raw_record_id, input.observation_type);
315
- observations.push({
316
- id: requireStringField(saved, 'id'),
317
- created: !existing,
318
- });
446
+ observations.push(saveObservation(adapter, input));
319
447
  };
320
448
  if ('transaction' in adapter && typeof adapter.transaction === 'function') {
321
449
  const txResult = adapter.transaction(() => {
@@ -12,7 +12,7 @@ export declare const ENTITY_NODE_STATUSES: readonly ["active", "merged", "archiv
12
12
  export type EntityNodeStatus = (typeof ENTITY_NODE_STATUSES)[number];
13
13
  export declare const ENTITY_ALIAS_STATUSES: readonly ["active", "suppressed"];
14
14
  export type EntityAliasStatus = (typeof ENTITY_ALIAS_STATUSES)[number];
15
- export declare const ENTITY_OBSERVATION_TYPES: readonly ["generic", "author", "channel"];
15
+ export declare const ENTITY_OBSERVATION_TYPES: readonly ["generic", "author", "channel", "thread"];
16
16
  export type EntityObservationType = (typeof ENTITY_OBSERVATION_TYPES)[number];
17
17
  export declare const ENTITY_CANDIDATE_KINDS: readonly ["alias_to_entity", "entity_to_entity", "cluster"];
18
18
  export type EntityCandidateKind = (typeof ENTITY_CANDIDATE_KINDS)[number];
@@ -71,6 +71,10 @@ export interface EntityObservation {
71
71
  source_connector: string;
72
72
  source_locator: string | null;
73
73
  source_raw_record_id: string;
74
+ workspace_context_key?: string | null;
75
+ channel_context_key?: string | null;
76
+ thread_context_key?: string | null;
77
+ actor_context_key?: string | null;
74
78
  created_at: number;
75
79
  }
76
80
  export interface EntityResolutionCandidate {
@@ -14,7 +14,7 @@ exports.ENTITY_MERGE_ACTION_TYPES = ['merge', 'reject', 'defer', 'split'];
14
14
  exports.ENTITY_SCOPE_KINDS = ['project', 'channel', 'user', 'global'];
15
15
  exports.ENTITY_NODE_STATUSES = ['active', 'merged', 'archived'];
16
16
  exports.ENTITY_ALIAS_STATUSES = ['active', 'suppressed'];
17
- exports.ENTITY_OBSERVATION_TYPES = ['generic', 'author', 'channel'];
17
+ exports.ENTITY_OBSERVATION_TYPES = ['generic', 'author', 'channel', 'thread'];
18
18
  exports.ENTITY_CANDIDATE_KINDS = ['alias_to_entity', 'entity_to_entity', 'cluster'];
19
19
  exports.ENTITY_ACTOR_TYPES = ['system', 'user', 'agent'];
20
20
  exports.ENTITY_INGEST_RUN_STATUSES = ['running', 'complete', 'failed'];
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  * @module mama-core
8
8
  * @version 1.0.0
9
9
  */
10
- export { generateEmbedding, generateEnhancedEmbedding, generateBatchEmbeddings, cosineSimilarity, embeddingCache, EMBEDDING_DIM, MODEL_NAME, } from './embeddings.js';
10
+ export { generateEmbedding, generateEnhancedEmbedding, generateBatchEmbeddings, cosineSimilarity, embeddingCache, EMBEDDING_DIM, MODEL_NAME, EMBEDDING_PREFIX_SCHEME, type EmbeddingRole, } from './embeddings.js';
11
11
  export { EmbeddingCache } from './embedding-cache.js';
12
12
  export { getEmbeddingFromServer, getServerStatus, isServerRunning, getServerPort, DEFAULT_PORT, HOST, TIMEOUT_MS, type ServerStatus, } from './embedding-client.js';
13
13
  export { initDB, getDB, getAdapter, closeDB, insertEmbedding, vectorSearch, queryVectorSearch, insertDecisionWithEmbedding, queryDecisionGraph, querySemanticEdges, updateDecisionOutcome, getPreparedStmt, getDbPath, fts5Search, reindexEmbeddings, type DatabaseAdapter as DBManagerAdapter, type PreparedStatement, type DecisionRecord, type OutcomeData, type VectorSearchParams, type SemanticEdges, type SemanticEdgeItem, type DecisionInput, } from './db-manager.js';
@@ -16,7 +16,7 @@ export { getSessionDecisions, incrementUsageSuccess, incrementUsageFailure, getD
16
16
  import mama from './mama-api.js';
17
17
  export { mama };
18
18
  export { MEMORY_SCOPE_KINDS, MEMORY_KINDS, MEMORY_STATUSES, MEMORY_EDGE_TYPES, createEmptyRecallBundle, type MemoryScopeKind, type MemoryKind, type MemoryStatus, type MemoryEdgeType, type MemoryScopeRef, type MemorySourceRef, type MemoryRecord, type MemoryWriteProvenance, type MemoryProvenanceRecord, type PublicSaveMemoryInput, type PublicIngestMemoryInput, type PublicIngestConversationInput, type MemorySearchResultHit, type MemoryEdge, type ProfileSnapshot, type RecallBundle, type ConversationMessage, type IngestConversationInput, type ExtractedMemoryUnit, type IngestConversationResult, } from './memory/types.js';
19
- export { saveMemory, saveMemoryWithTrustedProvenance, recallMemory, buildProfile, ingestMemory, ingestWithTrustedProvenance, evolveMemory, buildMemoryBootstrap, createAuditAck, recordMemoryAudit, ingestConversation, ingestConversationWithTrustedProvenance, setExtractionFn, upsertChannelSummary, getChannelSummary, } from './memory/api.js';
19
+ export { saveMemory, saveMemoryWithTrustedProvenance, promoteMemoryStatus, recallMemory, buildProfile, ingestMemory, ingestWithTrustedProvenance, evolveMemory, buildMemoryBootstrap, createAuditAck, recordMemoryAudit, ingestConversation, ingestConversationWithTrustedProvenance, setExtractionFn, upsertChannelSummary, getChannelSummary, } from './memory/api.js';
20
20
  export { buildExtractionPrompt, parseExtractionResponse } from './memory/extraction-prompt.js';
21
21
  export { appendMemoryEvent, insertMemoryEventInTransaction, listMemoryEventsForMemory, listRecentMemoryEvents, } from './memory/event-store.js';
22
22
  export { createTrustedProvenanceCapability, assertTrustedProvenanceCapability, type TrustedProvenanceCapability, type TrustedMemoryWriteOptions, } from './memory/provenance.js';
@@ -29,6 +29,8 @@ export { appendToolTrace, listToolTracesForRun } from './model-runs/tool-trace-s
29
29
  export { TWIN_EDGE_SOURCES, TWIN_EDGE_TYPES, TWIN_REF_KINDS, type InsertTwinEdgeInput, type ListVisibleTwinEdgesOptions, type TwinEdgeRecord, type TwinEdgeSource, type TwinEdgeType, type TwinRef, type TwinRefKind, type TwinScopeRef, } from './edges/types.js';
30
30
  export { getTwinEdge, insertTwinEdge, listTwinEdgesForRefs, mapTwinEdgeRow, } from './edges/store.js';
31
31
  export { assertTwinRefsVisibleToScopes, listVisibleTwinEdgesForRefs, } from './edges/ref-validation.js';
32
+ export * from './context-compile/index.js';
33
+ export * from './provenance/source-ref.js';
32
34
  export * from './agent-situation/index.js';
33
35
  export * from './agent-graph/index.js';
34
36
  export * from './entities/types.js';
package/dist/index.js CHANGED
@@ -26,10 +26,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.saveMemoryWithTrustedProvenance = exports.saveMemory = exports.createEmptyRecallBundle = exports.MEMORY_EDGE_TYPES = exports.MEMORY_STATUSES = exports.MEMORY_KINDS = exports.MEMORY_SCOPE_KINDS = exports.mama = exports.DEFAULT_DB_PATH = exports.LEGACY_DB_PATH = exports.DB_DIR = exports.DB_PATH = exports.traverseDecisionChain = exports.getDecisionById = exports.incrementUsageFailure = exports.incrementUsageSuccess = exports.getSessionDecisions = exports.SQLiteAdapter = exports.DatabaseAdapter = exports.createAdapter = exports.reindexEmbeddings = exports.fts5Search = exports.getDbPath = exports.getPreparedStmt = exports.updateDecisionOutcome = exports.querySemanticEdges = exports.queryDecisionGraph = exports.insertDecisionWithEmbedding = exports.queryVectorSearch = exports.vectorSearch = exports.insertEmbedding = exports.closeDB = exports.getAdapter = exports.getDB = exports.initDB = exports.TIMEOUT_MS = exports.HOST = exports.DEFAULT_PORT = exports.getServerPort = exports.isServerRunning = exports.getServerStatus = exports.getEmbeddingFromServer = exports.EmbeddingCache = exports.MODEL_NAME = exports.EMBEDDING_DIM = exports.embeddingCache = exports.cosineSimilarity = exports.generateBatchEmbeddings = exports.generateEnhancedEmbedding = exports.generateEmbedding = void 0;
30
- exports.canonicalizeJSON = exports.listVisibleTwinEdgesForRefs = exports.assertTwinRefsVisibleToScopes = exports.mapTwinEdgeRow = exports.listTwinEdgesForRefs = exports.insertTwinEdge = exports.getTwinEdge = exports.TWIN_REF_KINDS = exports.TWIN_EDGE_TYPES = exports.TWIN_EDGE_SOURCES = exports.listToolTracesForRun = exports.appendToolTrace = exports.getModelRunInAdapter = exports.getModelRun = exports.failModelRunInAdapter = exports.failModelRun = exports.commitModelRunInAdapter = exports.commitModelRun = exports.beginModelRunInAdapter = exports.beginModelRun = exports.MODEL_RUN_STATUSES = exports.listMemoryProvenanceAudit = exports.getMemoryProvenanceAudit = exports.backfillConnectorEventScopeMetadata = exports.backfillLegacyMemoryProvenance = exports.listMemoriesByModelRunId = exports.listMemoriesByGatewayCallId = exports.listMemoriesByEnvelopeHash = exports.getMemoryProvenance = exports.assertTrustedProvenanceCapability = exports.createTrustedProvenanceCapability = exports.listRecentMemoryEvents = exports.listMemoryEventsForMemory = exports.insertMemoryEventInTransaction = exports.appendMemoryEvent = exports.parseExtractionResponse = exports.buildExtractionPrompt = exports.getChannelSummary = exports.upsertChannelSummary = exports.setExtractionFn = exports.ingestConversationWithTrustedProvenance = exports.ingestConversation = exports.recordMemoryAudit = exports.createAuditAck = exports.buildMemoryBootstrap = exports.evolveMemory = exports.ingestWithTrustedProvenance = exports.ingestMemory = exports.buildProfile = exports.recallMemory = void 0;
31
- exports.DatabaseError = exports.ValidationError = exports.NotFoundError = exports.MAMAError = exports.formatTimeAgo = exports.DebugLogger = exports.error = exports.warn = exports.info = exports.debug = exports.logSearching = exports.logLoading = exports.logInfo = exports.logError = exports.logFailed = exports.logComplete = exports.logProgress = exports.getTierBanner = exports.getTierDescription = exports.checkDatabase = exports.checkEmbeddings = exports.checkSQLite = exports.checkNodeVersion = exports.validateTier = exports.VALID_EDGE_TYPES = exports.updateConfidence = exports.getSupersededChainDepth = exports.createEdgesFromReasoning = exports.parseReasoningForRelationships = exports.detectRefinement = exports.calculateCombinedConfidence = exports.markSuperseded = exports.createSupersedesEdge = exports.createEdge = exports.getPreviousDecision = exports.generateDecisionId = exports.learnDecision = exports.testRelevanceScoring = exports.formatTopNContext = exports.selectTopDecisions = exports.calculateRelevance = exports.DEFAULT_CONFIG = exports.getConfigPath = exports.updateConfig = exports.getCacheDir = exports.getEmbeddingDim = exports.getModelName = exports.loadConfig = exports.CanonicalizeError = exports.targetRefHash = void 0;
32
- exports.notifyInsight = exports.FALLBACK_MODEL = exports.DEFAULT_MODEL = exports.listModels = exports.isAvailable = exports.analyzeQueryIntent = exports.analyzeDecision = exports.generate = exports.extractTopicKeywords = exports.analyzeIntent = exports.injectDecisionContext = exports.RECENT_WINDOW_MS = exports.PARTIAL_INDICATORS = exports.SUCCESS_INDICATORS = exports.FAILURE_INDICATORS = exports.onUserPromptSubmit = exports.markOutcome = exports.getEvidenceImpact = exports.calculateDurationDays = exports.getRecentDecision = exports.extractFailureReason = exports.matchesPartialIndicators = exports.matchesSuccessIndicators = exports.matchesFailureIndicators = exports.analyzeOutcome = exports.extractCodeExample = exports.extractQuickAnswer = exports.estimateTokens = exports.ensureTokenBudget = exports.formatTrustContext = exports.formatInstantAnswer = exports.formatTeaser = exports.formatList = exports.formatRecall = exports.formatLegacyContext = exports.formatContext = exports.isMAMAError = exports.wrapError = exports.ErrorCodes = exports.TimeoutError = exports.RateLimitError = exports.LinkError = exports.ConfigurationError = exports.EmbeddingError = void 0;
29
+ exports.saveMemory = exports.createEmptyRecallBundle = exports.MEMORY_EDGE_TYPES = exports.MEMORY_STATUSES = exports.MEMORY_KINDS = exports.MEMORY_SCOPE_KINDS = exports.mama = exports.DEFAULT_DB_PATH = exports.LEGACY_DB_PATH = exports.DB_DIR = exports.DB_PATH = exports.traverseDecisionChain = exports.getDecisionById = exports.incrementUsageFailure = exports.incrementUsageSuccess = exports.getSessionDecisions = exports.SQLiteAdapter = exports.DatabaseAdapter = exports.createAdapter = exports.reindexEmbeddings = exports.fts5Search = exports.getDbPath = exports.getPreparedStmt = exports.updateDecisionOutcome = exports.querySemanticEdges = exports.queryDecisionGraph = exports.insertDecisionWithEmbedding = exports.queryVectorSearch = exports.vectorSearch = exports.insertEmbedding = exports.closeDB = exports.getAdapter = exports.getDB = exports.initDB = exports.TIMEOUT_MS = exports.HOST = exports.DEFAULT_PORT = exports.getServerPort = exports.isServerRunning = exports.getServerStatus = exports.getEmbeddingFromServer = exports.EmbeddingCache = exports.EMBEDDING_PREFIX_SCHEME = exports.MODEL_NAME = exports.EMBEDDING_DIM = exports.embeddingCache = exports.cosineSimilarity = exports.generateBatchEmbeddings = exports.generateEnhancedEmbedding = exports.generateEmbedding = void 0;
30
+ exports.assertTwinRefsVisibleToScopes = exports.mapTwinEdgeRow = exports.listTwinEdgesForRefs = exports.insertTwinEdge = exports.getTwinEdge = exports.TWIN_REF_KINDS = exports.TWIN_EDGE_TYPES = exports.TWIN_EDGE_SOURCES = exports.listToolTracesForRun = exports.appendToolTrace = exports.getModelRunInAdapter = exports.getModelRun = exports.failModelRunInAdapter = exports.failModelRun = exports.commitModelRunInAdapter = exports.commitModelRun = exports.beginModelRunInAdapter = exports.beginModelRun = exports.MODEL_RUN_STATUSES = exports.listMemoryProvenanceAudit = exports.getMemoryProvenanceAudit = exports.backfillConnectorEventScopeMetadata = exports.backfillLegacyMemoryProvenance = exports.listMemoriesByModelRunId = exports.listMemoriesByGatewayCallId = exports.listMemoriesByEnvelopeHash = exports.getMemoryProvenance = exports.assertTrustedProvenanceCapability = exports.createTrustedProvenanceCapability = exports.listRecentMemoryEvents = exports.listMemoryEventsForMemory = exports.insertMemoryEventInTransaction = exports.appendMemoryEvent = exports.parseExtractionResponse = exports.buildExtractionPrompt = exports.getChannelSummary = exports.upsertChannelSummary = exports.setExtractionFn = exports.ingestConversationWithTrustedProvenance = exports.ingestConversation = exports.recordMemoryAudit = exports.createAuditAck = exports.buildMemoryBootstrap = exports.evolveMemory = exports.ingestWithTrustedProvenance = exports.ingestMemory = exports.buildProfile = exports.recallMemory = exports.promoteMemoryStatus = exports.saveMemoryWithTrustedProvenance = void 0;
31
+ exports.NotFoundError = exports.MAMAError = exports.formatTimeAgo = exports.DebugLogger = exports.error = exports.warn = exports.info = exports.debug = exports.logSearching = exports.logLoading = exports.logInfo = exports.logError = exports.logFailed = exports.logComplete = exports.logProgress = exports.getTierBanner = exports.getTierDescription = exports.checkDatabase = exports.checkEmbeddings = exports.checkSQLite = exports.checkNodeVersion = exports.validateTier = exports.VALID_EDGE_TYPES = exports.updateConfidence = exports.getSupersededChainDepth = exports.createEdgesFromReasoning = exports.parseReasoningForRelationships = exports.detectRefinement = exports.calculateCombinedConfidence = exports.markSuperseded = exports.createSupersedesEdge = exports.createEdge = exports.getPreviousDecision = exports.generateDecisionId = exports.learnDecision = exports.testRelevanceScoring = exports.formatTopNContext = exports.selectTopDecisions = exports.calculateRelevance = exports.DEFAULT_CONFIG = exports.getConfigPath = exports.updateConfig = exports.getCacheDir = exports.getEmbeddingDim = exports.getModelName = exports.loadConfig = exports.CanonicalizeError = exports.targetRefHash = exports.canonicalizeJSON = exports.listVisibleTwinEdgesForRefs = void 0;
32
+ exports.notifyInsight = exports.FALLBACK_MODEL = exports.DEFAULT_MODEL = exports.listModels = exports.isAvailable = exports.analyzeQueryIntent = exports.analyzeDecision = exports.generate = exports.extractTopicKeywords = exports.analyzeIntent = exports.injectDecisionContext = exports.RECENT_WINDOW_MS = exports.PARTIAL_INDICATORS = exports.SUCCESS_INDICATORS = exports.FAILURE_INDICATORS = exports.onUserPromptSubmit = exports.markOutcome = exports.getEvidenceImpact = exports.calculateDurationDays = exports.getRecentDecision = exports.extractFailureReason = exports.matchesPartialIndicators = exports.matchesSuccessIndicators = exports.matchesFailureIndicators = exports.analyzeOutcome = exports.extractCodeExample = exports.extractQuickAnswer = exports.estimateTokens = exports.ensureTokenBudget = exports.formatTrustContext = exports.formatInstantAnswer = exports.formatTeaser = exports.formatList = exports.formatRecall = exports.formatLegacyContext = exports.formatContext = exports.isMAMAError = exports.wrapError = exports.ErrorCodes = exports.TimeoutError = exports.RateLimitError = exports.LinkError = exports.ConfigurationError = exports.EmbeddingError = exports.DatabaseError = exports.ValidationError = void 0;
33
33
  var embeddings_js_1 = require("./embeddings.js");
34
34
  Object.defineProperty(exports, "generateEmbedding", { enumerable: true, get: function () { return embeddings_js_1.generateEmbedding; } });
35
35
  Object.defineProperty(exports, "generateEnhancedEmbedding", { enumerable: true, get: function () { return embeddings_js_1.generateEnhancedEmbedding; } });
@@ -38,6 +38,7 @@ Object.defineProperty(exports, "cosineSimilarity", { enumerable: true, get: func
38
38
  Object.defineProperty(exports, "embeddingCache", { enumerable: true, get: function () { return embeddings_js_1.embeddingCache; } });
39
39
  Object.defineProperty(exports, "EMBEDDING_DIM", { enumerable: true, get: function () { return embeddings_js_1.EMBEDDING_DIM; } });
40
40
  Object.defineProperty(exports, "MODEL_NAME", { enumerable: true, get: function () { return embeddings_js_1.MODEL_NAME; } });
41
+ Object.defineProperty(exports, "EMBEDDING_PREFIX_SCHEME", { enumerable: true, get: function () { return embeddings_js_1.EMBEDDING_PREFIX_SCHEME; } });
41
42
  var embedding_cache_js_1 = require("./embedding-cache.js");
42
43
  Object.defineProperty(exports, "EmbeddingCache", { enumerable: true, get: function () { return embedding_cache_js_1.EmbeddingCache; } });
43
44
  var embedding_client_js_1 = require("./embedding-client.js");
@@ -89,6 +90,7 @@ Object.defineProperty(exports, "createEmptyRecallBundle", { enumerable: true, ge
89
90
  var api_js_1 = require("./memory/api.js");
90
91
  Object.defineProperty(exports, "saveMemory", { enumerable: true, get: function () { return api_js_1.saveMemory; } });
91
92
  Object.defineProperty(exports, "saveMemoryWithTrustedProvenance", { enumerable: true, get: function () { return api_js_1.saveMemoryWithTrustedProvenance; } });
93
+ Object.defineProperty(exports, "promoteMemoryStatus", { enumerable: true, get: function () { return api_js_1.promoteMemoryStatus; } });
92
94
  Object.defineProperty(exports, "recallMemory", { enumerable: true, get: function () { return api_js_1.recallMemory; } });
93
95
  Object.defineProperty(exports, "buildProfile", { enumerable: true, get: function () { return api_js_1.buildProfile; } });
94
96
  Object.defineProperty(exports, "ingestMemory", { enumerable: true, get: function () { return api_js_1.ingestMemory; } });
@@ -150,6 +152,8 @@ Object.defineProperty(exports, "mapTwinEdgeRow", { enumerable: true, get: functi
150
152
  var ref_validation_js_1 = require("./edges/ref-validation.js");
151
153
  Object.defineProperty(exports, "assertTwinRefsVisibleToScopes", { enumerable: true, get: function () { return ref_validation_js_1.assertTwinRefsVisibleToScopes; } });
152
154
  Object.defineProperty(exports, "listVisibleTwinEdgesForRefs", { enumerable: true, get: function () { return ref_validation_js_1.listVisibleTwinEdgesForRefs; } });
155
+ __exportStar(require("./context-compile/index.js"), exports);
156
+ __exportStar(require("./provenance/source-ref.js"), exports);
153
157
  __exportStar(require("./agent-situation/index.js"), exports);
154
158
  __exportStar(require("./agent-graph/index.js"), exports);
155
159
  __exportStar(require("./entities/types.js"), exports);
package/dist/mama-api.js CHANGED
@@ -1118,7 +1118,7 @@ async function suggest(userQuestion, options = {}) {
1118
1118
  throw new Error('Vector search not available');
1119
1119
  }
1120
1120
  // Generate query embedding
1121
- const queryEmbedding = await (0, embeddings_js_1.generateEmbedding)(userQuestion);
1121
+ const queryEmbedding = await (0, embeddings_js_1.generateEmbedding)(userQuestion, 'query');
1122
1122
  // Adaptive threshold (shorter queries need higher confidence)
1123
1123
  const wordCount = userQuestion.split(/\s+/).length;
1124
1124
  const adaptiveThreshold = threshold !== undefined ? threshold : wordCount < 3 ? 0.7 : 0.6;
@@ -2,7 +2,7 @@ import { type WikiPageIndexRecord } from '../cases/wiki-page-index.js';
2
2
  import { resolveMemoryEvolution } from './evolution-engine.js';
3
3
  import { getChannelSummary, upsertChannelSummary } from './channel-summary-store.js';
4
4
  import { type SearchHitDiagnostics } from '../search/search-quality.js';
5
- import type { MemoryAgentBootstrap, MemoryAuditAck, MemoryEdge, MemoryRecord, MemoryScopeRef, ProfileSnapshot, PublicIngestMemoryInput, PublicSaveMemoryInput, RecallBundle, IngestConversationInput, ExtractedMemoryUnit, IngestConversationResult, RecallMemoryOptions } from './types.js';
5
+ import type { MemoryAgentBootstrap, MemoryAuditAck, MemoryEdge, MemoryRecord, MemoryScopeRef, MemoryStatus, ProfileSnapshot, PublicIngestMemoryInput, PublicSaveMemoryInput, RecallBundle, IngestConversationInput, ExtractedMemoryUnit, IngestConversationResult, RecallMemoryOptions } from './types.js';
6
6
  import { type TrustedMemoryWriteOptions } from './provenance.js';
7
7
  type SaveMemoryInput = PublicSaveMemoryInput;
8
8
  type IngestMemoryInput = PublicIngestMemoryInput;
@@ -27,6 +27,11 @@ type SaveMemoryResult = {
27
27
  };
28
28
  export declare function saveMemory(input: SaveMemoryInput): Promise<SaveMemoryResult>;
29
29
  export declare function saveMemoryWithTrustedProvenance(input: SaveMemoryInput, options: TrustedMemoryWriteOptions): Promise<SaveMemoryResult>;
30
+ export declare function promoteMemoryStatus(input: {
31
+ memoryId: string;
32
+ status: MemoryStatus;
33
+ nowMs?: number;
34
+ }): Promise<void>;
30
35
  export declare function buildProfile(scopes: MemoryScopeRef[]): Promise<ProfileSnapshot>;
31
36
  export declare function recallMemory(query: string, options?: RecallMemoryOptions): Promise<RecallBundle>;
32
37
  export declare function ingestMemory(input: IngestMemoryInput): Promise<{