@facthouse/mcp 0.26.0 → 0.27.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 (66) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +611 -611
  3. package/dist/cli/inspect-html.js +460 -460
  4. package/dist/cli/inspect-intelligence.js +70 -70
  5. package/dist/cli/inspect-payload.js +39 -39
  6. package/dist/cli/inspect-theme.js +18 -18
  7. package/dist/cli/spend-dashboard.js +4 -4
  8. package/dist/cli/stats-html.js +674 -674
  9. package/dist/cli/web.js +89 -89
  10. package/dist/db/consolidation-lock.js +2 -2
  11. package/dist/db/consolidations.js +15 -15
  12. package/dist/db/embeddings.js +10 -10
  13. package/dist/db/entities.js +26 -26
  14. package/dist/db/extract-watermarks.js +79 -79
  15. package/dist/db/facts.js +39 -39
  16. package/dist/db/inferences.js +8 -8
  17. package/dist/db/intelligence-runs.js +5 -5
  18. package/dist/db/postgres-schema.js +323 -323
  19. package/dist/db/prune.js +17 -17
  20. package/dist/db/schema.js +530 -530
  21. package/dist/db/session-facts.js +26 -26
  22. package/dist/db/sessions.js +10 -10
  23. package/dist/db/sources.js +1 -1
  24. package/dist/db/stats.js +10 -10
  25. package/dist/db/watermarks.js +7 -7
  26. package/dist/intelligence/consolidate.js +35 -35
  27. package/dist/intelligence/extract-prompt.js +26 -26
  28. package/dist/intelligence/token-budget.d.ts +2 -0
  29. package/dist/intelligence/token-budget.d.ts.map +1 -1
  30. package/dist/intelligence/token-budget.js +1 -1
  31. package/dist/intelligence/token-budget.js.map +1 -1
  32. package/dist/search/episodes.js +14 -14
  33. package/dist/search/key-facts.js +3 -3
  34. package/dist/tools/fact-manager.js +3 -3
  35. package/dist/tools/read-tools.js +4 -4
  36. package/package.json +62 -62
  37. package/dist/cli/log-event.d.ts +0 -55
  38. package/dist/cli/log-event.d.ts.map +0 -1
  39. package/dist/cli/log-event.js +0 -149
  40. package/dist/cli/log-event.js.map +0 -1
  41. package/dist/db/scheduler-signal.d.ts +0 -20
  42. package/dist/db/scheduler-signal.d.ts.map +0 -1
  43. package/dist/db/scheduler-signal.js +0 -32
  44. package/dist/db/scheduler-signal.js.map +0 -1
  45. package/dist/schemas/starter-vocabulary.d.ts +0 -34
  46. package/dist/schemas/starter-vocabulary.d.ts.map +0 -1
  47. package/dist/schemas/starter-vocabulary.js +0 -91
  48. package/dist/schemas/starter-vocabulary.js.map +0 -1
  49. package/dist/search/profile.d.ts +0 -31
  50. package/dist/search/profile.d.ts.map +0 -1
  51. package/dist/search/profile.js +0 -43
  52. package/dist/search/profile.js.map +0 -1
  53. package/dist/sources/jsonl-ingest.d.ts +0 -58
  54. package/dist/sources/jsonl-ingest.d.ts.map +0 -1
  55. package/dist/sources/jsonl-ingest.js +0 -195
  56. package/dist/sources/jsonl-ingest.js.map +0 -1
  57. package/dist/sources/pull.d.ts +0 -85
  58. package/dist/sources/pull.d.ts.map +0 -1
  59. package/dist/sources/pull.js +0 -145
  60. package/dist/sources/pull.js.map +0 -1
  61. package/dist/types.d.ts +0 -115
  62. package/dist/types.d.ts.map +0 -1
  63. package/dist/types.js +0 -6
  64. package/dist/types.js.map +0 -1
  65. package/dist/warnings.d.ts.map +0 -1
  66. package/dist/warnings.js.map +0 -1
package/dist/db/facts.js CHANGED
@@ -65,13 +65,13 @@ export async function insertFact(db, fact) {
65
65
  const sourceQuality = fact.source_quality ?? "heuristic";
66
66
  const speakerRole = fact.speaker_role ?? null;
67
67
  const speaker = speakerNameOf(fact.speaker);
68
- const result = await db.prepare(`INSERT INTO facts
69
- (id, content, domain, subdomain, confidence, importance,
70
- source_type, source_tool, source_id, status, superseded_by,
71
- is_latest, created_at, valid_from, valid_until,
72
- system_retired_at, session_id, capture_context, access_count, source_quality,
73
- speaker_role, speaker)
74
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', NULL,
68
+ const result = await db.prepare(`INSERT INTO facts
69
+ (id, content, domain, subdomain, confidence, importance,
70
+ source_type, source_tool, source_id, status, superseded_by,
71
+ is_latest, created_at, valid_from, valid_until,
72
+ system_retired_at, session_id, capture_context, access_count, source_quality,
73
+ speaker_role, speaker)
74
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', NULL,
75
75
  1, ?, ?, NULL, NULL, ?, ?, 0, ?, ?, ?)`).run(id, fact.content, fact.domain, subdomain, confidence, importance, fact.source_type, sourceTool, sourceId, now, validFrom, sessionId, captureContext, sourceQuality, speakerRole, speaker);
76
76
  if (result.changes === 0) {
77
77
  throw new Error(`Failed to insert fact '${id}'`);
@@ -126,8 +126,8 @@ export async function getFactsByIds(db, ids, opts) {
126
126
  const placeholders = ids.map(() => "?").join(",");
127
127
  const currency = currencyClause("", opts?.asOfSystemTime);
128
128
  const rows = (await db
129
- .prepare(`SELECT * FROM facts
130
- WHERE id IN (${placeholders})
129
+ .prepare(`SELECT * FROM facts
130
+ WHERE id IN (${placeholders})
131
131
  AND ${currency.sql}`)
132
132
  .all(...ids, ...currency.params));
133
133
  return rows.map(mapFact);
@@ -135,7 +135,7 @@ export async function getFactsByIds(db, ids, opts) {
135
135
  /** Get all currently-true facts for a domain — or those believed at T. */
136
136
  export async function getFactsByDomain(db, domain, subdomain, opts) {
137
137
  const currency = currencyClause("", opts?.asOfSystemTime);
138
- let sql = `SELECT * FROM facts
138
+ let sql = `SELECT * FROM facts
139
139
  WHERE domain = ? AND ${currency.sql}`;
140
140
  const params = [domain, ...currency.params];
141
141
  if (subdomain !== undefined) {
@@ -165,11 +165,11 @@ export async function getFactsByEntity(db, entityId, opts) {
165
165
  //
166
166
  // MAX() because an entity can be linked to one fact more than once, with
167
167
  // different relationships; the fact is about it if any link says so.
168
- `SELECT f.*, MAX(fe.relationship = ?) AS is_subject
169
- FROM facts f
170
- JOIN fact_entities fe ON f.id = fe.fact_id
171
- WHERE fe.entity_id = ? AND ${currency.sql}
172
- GROUP BY f.id
168
+ `SELECT f.*, MAX(fe.relationship = ?) AS is_subject
169
+ FROM facts f
170
+ JOIN fact_entities fe ON f.id = fe.fact_id
171
+ WHERE fe.entity_id = ? AND ${currency.sql}
172
+ GROUP BY f.id
173
173
  ORDER BY is_subject DESC, f.importance DESC, f.created_at DESC`)
174
174
  .all(SUBJECT_OF, entityId, ...currency.params));
175
175
  return rows.map((row) => ({
@@ -189,9 +189,9 @@ export async function getFactsAsOfSystemTime(db, at, limit = 100) {
189
189
  const instant = parseSystemTime(at);
190
190
  const currency = currencyClause("", instant);
191
191
  const rows = (await db
192
- .prepare(`SELECT * FROM facts
193
- WHERE ${currency.sql}
194
- ORDER BY created_at DESC
192
+ .prepare(`SELECT * FROM facts
193
+ WHERE ${currency.sql}
194
+ ORDER BY created_at DESC
195
195
  LIMIT ?`)
196
196
  .all(...currency.params, limit));
197
197
  return rows.map(mapFact);
@@ -222,23 +222,23 @@ export async function supersedeFact(db, oldId, newFact, opts) {
222
222
  : now;
223
223
  const result = await withTransaction(db, async () => {
224
224
  const updated = retireSystemTime
225
- ? await db.prepare(`UPDATE facts
226
- SET status = 'superseded', superseded_by = ?, is_latest = 0,
227
- valid_until = ?, system_retired_at = ?
225
+ ? await db.prepare(`UPDATE facts
226
+ SET status = 'superseded', superseded_by = ?, is_latest = 0,
227
+ valid_until = ?, system_retired_at = ?
228
228
  WHERE id = ?`).run(newId, now, now, oldId)
229
- : await db.prepare(`UPDATE facts
230
- SET status = 'superseded', superseded_by = ?, is_latest = 0, valid_until = ?
229
+ : await db.prepare(`UPDATE facts
230
+ SET status = 'superseded', superseded_by = ?, is_latest = 0, valid_until = ?
231
231
  WHERE id = ?`).run(newId, now, oldId);
232
232
  if (updated.changes === 0) {
233
233
  throw new Error(`Cannot supersede fact '${oldId}': not found`);
234
234
  }
235
- await db.prepare(`INSERT INTO facts
236
- (id, content, domain, subdomain, confidence, importance,
237
- source_type, source_tool, source_id, status, superseded_by,
238
- is_latest, created_at, valid_from, valid_until,
239
- system_retired_at, session_id, capture_context, access_count, source_quality,
240
- speaker_role, speaker)
241
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', NULL,
235
+ await db.prepare(`INSERT INTO facts
236
+ (id, content, domain, subdomain, confidence, importance,
237
+ source_type, source_tool, source_id, status, superseded_by,
238
+ is_latest, created_at, valid_from, valid_until,
239
+ system_retired_at, session_id, capture_context, access_count, source_quality,
240
+ speaker_role, speaker)
241
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', NULL,
242
242
  1, ?, ?, NULL, NULL, ?, ?, 0, ?, ?, ?)`).run(newId, newFact.content, newFact.domain, subdomain, confidence, importance, newFact.source_type, sourceTool, sourceId, now, validFrom, sessionId, captureContext, sourceQuality, speakerRole, speaker);
243
243
  return {
244
244
  id: newId,
@@ -295,10 +295,10 @@ export async function keywordSearch(db, query, limit, opts) {
295
295
  if (!text)
296
296
  return [];
297
297
  const rows = (await db
298
- .prepare(`SELECT f.*, ts_rank(f.content_tsv, q) AS rank
299
- FROM facts f, plainto_tsquery('simple', ?) q
300
- WHERE f.content_tsv @@ q AND ${currency.sql}
301
- ORDER BY rank DESC
298
+ .prepare(`SELECT f.*, ts_rank(f.content_tsv, q) AS rank
299
+ FROM facts f, plainto_tsquery('simple', ?) q
300
+ WHERE f.content_tsv @@ q AND ${currency.sql}
301
+ ORDER BY rank DESC
302
302
  LIMIT ?`)
303
303
  .all(text, ...currency.params, effectiveLimit));
304
304
  return rows.map((row) => {
@@ -307,11 +307,11 @@ export async function keywordSearch(db, query, limit, opts) {
307
307
  });
308
308
  }
309
309
  const rows = (await db
310
- .prepare(`SELECT f.*, fts.rank
311
- FROM facts_fts fts
312
- JOIN facts f ON f.rowid = fts.rowid
313
- WHERE facts_fts MATCH ? AND ${currency.sql}
314
- ORDER BY fts.rank
310
+ .prepare(`SELECT f.*, fts.rank
311
+ FROM facts_fts fts
312
+ JOIN facts f ON f.rowid = fts.rowid
313
+ WHERE facts_fts MATCH ? AND ${currency.sql}
314
+ ORDER BY fts.rank
315
315
  LIMIT ?`)
316
316
  .all(query, ...currency.params, effectiveLimit));
317
317
  return rows.map((row) => {
@@ -15,7 +15,7 @@ function uniqueIds(ids) {
15
15
  }
16
16
  async function loadEvidence(db, inferenceId) {
17
17
  const rows = (await db
18
- .prepare(`SELECT fact_id FROM inference_evidence
18
+ .prepare(`SELECT fact_id FROM inference_evidence
19
19
  WHERE inference_id = ? ORDER BY fact_id`)
20
20
  .all(inferenceId));
21
21
  return rows.map((r) => r.fact_id);
@@ -89,7 +89,7 @@ export async function insertInference(db, input) {
89
89
  const now = new Date().toISOString();
90
90
  return withTransaction(db, async () => {
91
91
  await db
92
- .prepare(`INSERT INTO inferences (id, hypothesis, status, reason, fact_id, metadata, created_at, validated_at)
92
+ .prepare(`INSERT INTO inferences (id, hypothesis, status, reason, fact_id, metadata, created_at, validated_at)
93
93
  VALUES (?, ?, 'pending', NULL, NULL, ?, ?, NULL)`)
94
94
  .run(id, hypothesis, metadata, now);
95
95
  const link = db.prepare(`INSERT INTO inference_evidence (inference_id, fact_id) VALUES (?, ?)`);
@@ -110,7 +110,7 @@ export async function insertInference(db, input) {
110
110
  }
111
111
  export async function getInference(db, id) {
112
112
  const row = (await db
113
- .prepare(`SELECT id, hypothesis, status, reason, fact_id, metadata, created_at, validated_at
113
+ .prepare(`SELECT id, hypothesis, status, reason, fact_id, metadata, created_at, validated_at
114
114
  FROM inferences WHERE id = ?`)
115
115
  .get(id));
116
116
  if (!row)
@@ -119,7 +119,7 @@ export async function getInference(db, id) {
119
119
  }
120
120
  export async function listInferences(db, status = "pending") {
121
121
  const rows = (await db
122
- .prepare(`SELECT id, hypothesis, status, reason, fact_id, metadata, created_at, validated_at
122
+ .prepare(`SELECT id, hypothesis, status, reason, fact_id, metadata, created_at, validated_at
123
123
  FROM inferences WHERE status = ? ORDER BY created_at ASC`)
124
124
  .all(status));
125
125
  const out = [];
@@ -149,8 +149,8 @@ export async function validateInference(db, input) {
149
149
  const reason = input.reason?.trim() || null;
150
150
  if (!input.confirmed) {
151
151
  const result = await db
152
- .prepare(`UPDATE inferences
153
- SET status = 'rejected', reason = ?, validated_at = ?
152
+ .prepare(`UPDATE inferences
153
+ SET status = 'rejected', reason = ?, validated_at = ?
154
154
  WHERE id = ? AND status = 'pending'`)
155
155
  .run(reason, now, existing.id);
156
156
  if (result.changes === 0) {
@@ -187,8 +187,8 @@ export async function validateInference(db, input) {
187
187
  valid_from: null,
188
188
  });
189
189
  const result = await db
190
- .prepare(`UPDATE inferences
191
- SET status = 'confirmed', reason = ?, fact_id = ?, validated_at = ?
190
+ .prepare(`UPDATE inferences
191
+ SET status = 'confirmed', reason = ?, fact_id = ?, validated_at = ?
192
192
  WHERE id = ? AND status = 'pending'`)
193
193
  .run(reason, fact.id, now, existing.id);
194
194
  if (result.changes === 0) {
@@ -15,8 +15,8 @@ function asIso(value) {
15
15
  export async function insertIntelligenceRun(db, opts) {
16
16
  const id = randomUUID();
17
17
  await db
18
- .prepare(`INSERT INTO intelligence_runs
19
- (id, kind, consolidation_id, created_at, usage, trigger, source_tool, project)
18
+ .prepare(`INSERT INTO intelligence_runs
19
+ (id, kind, consolidation_id, created_at, usage, trigger, source_tool, project)
20
20
  VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
21
21
  .run(id, opts.kind, opts.consolidationId ?? null, opts.createdAt ?? new Date().toISOString(), JSON.stringify(opts.usage), opts.trigger ?? null, opts.sourceTool ?? null, opts.project ?? null);
22
22
  return id;
@@ -44,15 +44,15 @@ function mapRunRows(rows) {
44
44
  }
45
45
  export async function listIntelligenceRuns(db) {
46
46
  const rows = (await db
47
- .prepare(`SELECT id, kind, consolidation_id, created_at, usage, trigger, source_tool, project
47
+ .prepare(`SELECT id, kind, consolidation_id, created_at, usage, trigger, source_tool, project
48
48
  FROM intelligence_runs`)
49
49
  .all());
50
50
  return mapRunRows(rows);
51
51
  }
52
52
  export async function listIntelligenceRunsSince(db, sinceIso) {
53
53
  const rows = (await db
54
- .prepare(`SELECT id, kind, consolidation_id, created_at, usage, trigger, source_tool, project
55
- FROM intelligence_runs
54
+ .prepare(`SELECT id, kind, consolidation_id, created_at, usage, trigger, source_tool, project
55
+ FROM intelligence_runs
56
56
  WHERE created_at >= ?`)
57
57
  .all(sinceIso));
58
58
  return mapRunRows(rows);