@context-vault/core 2.17.1 → 3.0.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 (101) hide show
  1. package/dist/capture.d.ts +21 -0
  2. package/dist/capture.d.ts.map +1 -0
  3. package/dist/capture.js +269 -0
  4. package/dist/capture.js.map +1 -0
  5. package/dist/categories.d.ts +6 -0
  6. package/dist/categories.d.ts.map +1 -0
  7. package/dist/categories.js +50 -0
  8. package/dist/categories.js.map +1 -0
  9. package/dist/config.d.ts +4 -0
  10. package/dist/config.d.ts.map +1 -0
  11. package/dist/config.js +190 -0
  12. package/dist/config.js.map +1 -0
  13. package/dist/constants.d.ts +33 -0
  14. package/dist/constants.d.ts.map +1 -0
  15. package/dist/constants.js +23 -0
  16. package/dist/constants.js.map +1 -0
  17. package/dist/db.d.ts +13 -0
  18. package/dist/db.d.ts.map +1 -0
  19. package/dist/db.js +191 -0
  20. package/dist/db.js.map +1 -0
  21. package/dist/embed.d.ts +5 -0
  22. package/dist/embed.d.ts.map +1 -0
  23. package/dist/embed.js +78 -0
  24. package/dist/embed.js.map +1 -0
  25. package/dist/files.d.ts +13 -0
  26. package/dist/files.d.ts.map +1 -0
  27. package/dist/files.js +66 -0
  28. package/dist/files.js.map +1 -0
  29. package/dist/formatters.d.ts +8 -0
  30. package/dist/formatters.d.ts.map +1 -0
  31. package/dist/formatters.js +18 -0
  32. package/dist/formatters.js.map +1 -0
  33. package/dist/frontmatter.d.ts +12 -0
  34. package/dist/frontmatter.d.ts.map +1 -0
  35. package/dist/frontmatter.js +101 -0
  36. package/dist/frontmatter.js.map +1 -0
  37. package/dist/index.d.ts +10 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +297 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/ingest-url.d.ts +20 -0
  42. package/dist/ingest-url.d.ts.map +1 -0
  43. package/dist/ingest-url.js +113 -0
  44. package/dist/ingest-url.js.map +1 -0
  45. package/dist/main.d.ts +14 -0
  46. package/dist/main.d.ts.map +1 -0
  47. package/dist/main.js +25 -0
  48. package/dist/main.js.map +1 -0
  49. package/dist/search.d.ts +18 -0
  50. package/dist/search.d.ts.map +1 -0
  51. package/dist/search.js +238 -0
  52. package/dist/search.js.map +1 -0
  53. package/dist/types.d.ts +176 -0
  54. package/dist/types.d.ts.map +1 -0
  55. package/dist/types.js +2 -0
  56. package/dist/types.js.map +1 -0
  57. package/package.json +66 -16
  58. package/src/capture.ts +308 -0
  59. package/src/categories.ts +54 -0
  60. package/src/{core/config.js → config.ts} +34 -33
  61. package/src/{constants.js → constants.ts} +6 -3
  62. package/src/db.ts +229 -0
  63. package/src/{index/embed.js → embed.ts} +10 -35
  64. package/src/{core/files.js → files.ts} +15 -20
  65. package/src/{capture/formatters.js → formatters.ts} +13 -11
  66. package/src/{core/frontmatter.js → frontmatter.ts} +26 -33
  67. package/src/index.ts +351 -0
  68. package/src/ingest-url.ts +99 -0
  69. package/src/main.ts +111 -0
  70. package/src/{retrieve/index.js → search.ts} +62 -150
  71. package/src/types.ts +166 -0
  72. package/src/capture/file-ops.js +0 -99
  73. package/src/capture/import-pipeline.js +0 -46
  74. package/src/capture/importers.js +0 -387
  75. package/src/capture/index.js +0 -250
  76. package/src/capture/ingest-url.js +0 -252
  77. package/src/consolidation/index.js +0 -112
  78. package/src/core/categories.js +0 -73
  79. package/src/core/error-log.js +0 -54
  80. package/src/core/linking.js +0 -161
  81. package/src/core/migrate-dirs.js +0 -196
  82. package/src/core/status.js +0 -350
  83. package/src/core/telemetry.js +0 -90
  84. package/src/core/temporal.js +0 -146
  85. package/src/index/db.js +0 -586
  86. package/src/index/index.js +0 -583
  87. package/src/index.js +0 -71
  88. package/src/server/helpers.js +0 -44
  89. package/src/server/tools/clear-context.js +0 -47
  90. package/src/server/tools/context-status.js +0 -182
  91. package/src/server/tools/create-snapshot.js +0 -200
  92. package/src/server/tools/delete-context.js +0 -60
  93. package/src/server/tools/get-context.js +0 -765
  94. package/src/server/tools/ingest-project.js +0 -244
  95. package/src/server/tools/ingest-url.js +0 -88
  96. package/src/server/tools/list-buckets.js +0 -116
  97. package/src/server/tools/list-context.js +0 -163
  98. package/src/server/tools/save-context.js +0 -632
  99. package/src/server/tools/session-start.js +0 -285
  100. package/src/server/tools.js +0 -172
  101. package/src/sync/sync.js +0 -235
@@ -1,47 +0,0 @@
1
- import { z } from "zod";
2
- import { ok } from "../helpers.js";
3
-
4
- export const name = "clear_context";
5
-
6
- export const description =
7
- "Reset active in-memory session context without deleting vault entries. Call this when switching projects or topics mid-session. With `scope`, all subsequent get_context calls should filter to that tag/project. Vault data is never modified.";
8
-
9
- export const inputSchema = {
10
- scope: z
11
- .string()
12
- .optional()
13
- .describe(
14
- "Optional tag or project name to focus on going forward. When provided, treat subsequent get_context calls as if filtered to this tag.",
15
- ),
16
- };
17
-
18
- /**
19
- * @param {object} args
20
- * @param {import('../types.js').BaseCtx & Partial<import('../types.js').HostedCtxExtensions>} _ctx
21
- */
22
- export function handler({ scope } = {}) {
23
- const lines = [
24
- "## Context Reset",
25
- "",
26
- "Active session context has been cleared. All previous context from this session should be disregarded.",
27
- "",
28
- "Vault entries are unchanged — no data was deleted.",
29
- ];
30
-
31
- if (scope?.trim()) {
32
- const trimmed = scope.trim();
33
- lines.push(
34
- "",
35
- `### Active Scope: \`${trimmed}\``,
36
- "",
37
- `Going forward, treat \`get_context\` calls as scoped to the tag or project **"${trimmed}"** unless the user explicitly requests a different scope or passes their own tag filters.`,
38
- );
39
- } else {
40
- lines.push(
41
- "",
42
- "No scope set. Use `get_context` normally — all vault entries are accessible.",
43
- );
44
- }
45
-
46
- return ok(lines.join("\n"));
47
- }
@@ -1,182 +0,0 @@
1
- import { gatherVaultStatus, computeGrowthWarnings } from "../../core/status.js";
2
- import { errorLogPath, errorLogCount } from "../../core/error-log.js";
3
- import { ok } from "../helpers.js";
4
-
5
- function relativeTime(ts) {
6
- const secs = Math.floor((Date.now() - ts) / 1000);
7
- if (secs < 60) return `${secs}s ago`;
8
- const mins = Math.floor(secs / 60);
9
- if (mins < 60) return `${mins} minute${mins === 1 ? "" : "s"} ago`;
10
- const hrs = Math.floor(mins / 60);
11
- return `${hrs} hour${hrs === 1 ? "" : "s"} ago`;
12
- }
13
-
14
- export const name = "context_status";
15
-
16
- export const description =
17
- "Show vault health: resolved config, file counts per kind, database size, and any issues. Use to verify setup or troubleshoot. Call this when a user asks about their vault or to debug search issues.";
18
-
19
- export const inputSchema = {};
20
-
21
- /**
22
- * @param {object} _args
23
- * @param {import('../types.js').BaseCtx & Partial<import('../types.js').HostedCtxExtensions>} ctx
24
- */
25
- export function handler(_args, ctx) {
26
- const { config } = ctx;
27
- const userId = ctx.userId !== undefined ? ctx.userId : undefined;
28
-
29
- const status = gatherVaultStatus(ctx, { userId });
30
-
31
- const hasIssues = status.stalePaths || status.embeddingStatus?.missing > 0;
32
- const healthIcon = hasIssues ? "⚠" : "✓";
33
-
34
- const lines = [
35
- `## ${healthIcon} Vault Status (connected)`,
36
- ``,
37
- `Vault: ${config.vaultDir} (${config.vaultDirExists ? status.fileCount + " files" : "missing"})`,
38
- `Database: ${config.dbPath} (${status.dbSize})`,
39
- `Dev dir: ${config.devDir}`,
40
- `Data dir: ${config.dataDir}`,
41
- `Config: ${config.configPath}`,
42
- `Resolved via: ${status.resolvedFrom}`,
43
- `Schema: v9 (updated_at, superseded_by)`,
44
- ];
45
-
46
- if (status.embeddingStatus) {
47
- const { indexed, total, missing } = status.embeddingStatus;
48
- const pct = total > 0 ? Math.round((indexed / total) * 100) : 100;
49
- lines.push(`Embeddings: ${indexed}/${total} (${pct}%)`);
50
- }
51
- if (status.embedModelAvailable === false) {
52
- lines.push(
53
- `Embed model: unavailable (semantic search disabled, FTS still works)`,
54
- );
55
- } else if (status.embedModelAvailable === true) {
56
- lines.push(`Embed model: loaded`);
57
- }
58
- lines.push(`Decay: ${config.eventDecayDays} days (event recency window)`);
59
- if (status.expiredCount > 0) {
60
- lines.push(
61
- `Expired: ${status.expiredCount} entries pending prune (run \`context-vault prune\` to remove now)`,
62
- );
63
- }
64
-
65
- lines.push(``, `### Indexed`);
66
-
67
- if (status.kindCounts.length) {
68
- for (const { kind, c } of status.kindCounts) lines.push(`- ${c} ${kind}s`);
69
- } else {
70
- lines.push(`- (empty)`);
71
- }
72
-
73
- if (status.categoryCounts.length) {
74
- lines.push(``);
75
- lines.push(`### Categories`);
76
- for (const { category, c } of status.categoryCounts)
77
- lines.push(`- ${category}: ${c}`);
78
- }
79
-
80
- if (status.subdirs.length) {
81
- lines.push(``);
82
- lines.push(`### Disk Directories`);
83
- for (const { name, count } of status.subdirs)
84
- lines.push(`- ${name}/: ${count} files`);
85
- }
86
-
87
- if (status.stalePaths) {
88
- lines.push(``);
89
- lines.push(`### ⚠ Stale Paths`);
90
- lines.push(
91
- `DB contains ${status.staleCount} paths not matching current vault dir.`,
92
- );
93
- lines.push(`Auto-reindex will fix this on next search or save.`);
94
- }
95
-
96
- if (status.staleKnowledge?.length > 0) {
97
- lines.push(``);
98
- lines.push(`### ⚠ Potentially Stale Knowledge`);
99
- lines.push(
100
- `Not updated within kind staleness window (pattern: 180d, decision: 365d, reference: 90d):`,
101
- );
102
- for (const entry of status.staleKnowledge) {
103
- const lastUpdated = entry.last_updated
104
- ? entry.last_updated.split("T")[0]
105
- : "unknown";
106
- lines.push(
107
- `- "${entry.title}" (${entry.kind}) — last updated ${lastUpdated}`,
108
- );
109
- }
110
- lines.push(
111
- `Use save_context to refresh or add expires_at to retire stale entries.`,
112
- );
113
- }
114
-
115
- // Error log
116
- const logPath = errorLogPath(config.dataDir);
117
- const logCount = errorLogCount(config.dataDir);
118
- if (logCount > 0) {
119
- lines.push(``, `### Startup Error Log`);
120
- lines.push(`- Path: ${logPath}`);
121
- lines.push(`- Entries: ${logCount} (share this file for support)`);
122
- }
123
-
124
- // Health: session-level tool call stats
125
- const ts = ctx.toolStats;
126
- if (ts) {
127
- lines.push(``, `### Health`);
128
- lines.push(`- Tool calls (session): ${ts.ok} ok, ${ts.errors} errors`);
129
- if (ts.lastError) {
130
- const { tool, code, timestamp } = ts.lastError;
131
- lines.push(
132
- `- Last error: ${tool ?? "unknown"} — ${code} (${relativeTime(timestamp)})`,
133
- );
134
- }
135
- if (status.autoCapturedFeedbackCount > 0) {
136
- lines.push(
137
- `- Auto-captured feedback entries: ${status.autoCapturedFeedbackCount} (run get_context with kind:feedback tags:auto-captured)`,
138
- );
139
- }
140
- }
141
-
142
- // Growth warnings
143
- const growth = computeGrowthWarnings(status, config.thresholds);
144
- if (growth.hasWarnings) {
145
- lines.push("", "### ⚠ Vault Growth Warning");
146
- for (const w of growth.warnings) {
147
- lines.push(` ${w.message}`);
148
- }
149
- if (growth.kindBreakdown.length) {
150
- lines.push("");
151
- lines.push(" Breakdown by kind:");
152
- for (const { kind, count, pct } of growth.kindBreakdown) {
153
- lines.push(` ${kind}: ${count.toLocaleString()} (${pct}%)`);
154
- }
155
- }
156
- if (growth.actions.length) {
157
- lines.push("", "Suggested growth actions:");
158
- for (const a of growth.actions) {
159
- lines.push(` • ${a}`);
160
- }
161
- }
162
- }
163
-
164
- // Suggested actions
165
- const actions = [];
166
- if (status.stalePaths)
167
- actions.push("- Run `context-vault reindex` to fix stale paths");
168
- if (status.embeddingStatus?.missing > 0)
169
- actions.push(
170
- "- Run `context-vault reindex` to generate missing embeddings",
171
- );
172
- if (!config.vaultDirExists)
173
- actions.push("- Run `context-vault setup` to create the vault directory");
174
- if (status.kindCounts.length === 0 && config.vaultDirExists)
175
- actions.push("- Use `save_context` to add your first entry");
176
-
177
- if (actions.length) {
178
- lines.push("", "### Suggested Actions", ...actions);
179
- }
180
-
181
- return ok(lines.join("\n"));
182
- }
@@ -1,200 +0,0 @@
1
- import { z } from "zod";
2
- import { hybridSearch } from "../../retrieve/index.js";
3
- import { captureAndIndex } from "../../capture/index.js";
4
- import { normalizeKind } from "../../core/files.js";
5
- import { ok, err, ensureVaultExists } from "../helpers.js";
6
-
7
- const NOISE_KINDS = new Set(["prompt-history", "task-notification"]);
8
- const MAX_ENTRIES_FOR_GATHER = 40;
9
- const MAX_BODY_PER_ENTRY = 600;
10
-
11
- export const name = "create_snapshot";
12
-
13
- export const description =
14
- "Pull all relevant vault entries matching a topic, deduplicate, and save them as a structured context brief (kind: 'brief'). Entries are formatted as markdown — no external API or LLM call required. The calling agent can synthesize the gathered content directly. Retrieve with: get_context(kind: 'brief', identity_key: '<key>').";
15
-
16
- export const inputSchema = {
17
- topic: z.string().describe("The topic or project name to snapshot"),
18
- tags: z
19
- .array(z.string())
20
- .optional()
21
- .describe("Optional tag filters — entries must match at least one"),
22
- buckets: z
23
- .array(z.string())
24
- .optional()
25
- .describe(
26
- "Filter by project-scoped buckets. Each name expands to a 'bucket:<name>' tag. Composes with 'tags' via OR (entries matching any tag or any bucket are included).",
27
- ),
28
- kinds: z
29
- .array(z.string())
30
- .optional()
31
- .describe("Optional kind filters to restrict which entry types are pulled"),
32
- identity_key: z
33
- .string()
34
- .optional()
35
- .describe(
36
- "Deterministic key for the saved brief (defaults to slugified topic). Use the same key to overwrite a previous snapshot.",
37
- ),
38
- };
39
-
40
- function formatGatheredEntries(topic, entries) {
41
- const header = [
42
- `# ${topic} — Context Brief`,
43
- "",
44
- `*Gathered from ${entries.length} vault ${entries.length === 1 ? "entry" : "entries"}. Synthesize the content below to extract key decisions, patterns, and constraints.*`,
45
- "",
46
- "---",
47
- "",
48
- ].join("\n");
49
-
50
- const body = entries
51
- .map((e, i) => {
52
- const tags = e.tags ? JSON.parse(e.tags) : [];
53
- const tagStr = tags.length ? tags.join(", ") : "none";
54
- const updated = e.updated_at || e.created_at || "unknown";
55
- const bodyText = e.body
56
- ? e.body.slice(0, MAX_BODY_PER_ENTRY) +
57
- (e.body.length > MAX_BODY_PER_ENTRY ? "…" : "")
58
- : "(no body)";
59
- const title = e.title || `Entry ${i + 1}`;
60
- return [
61
- `## ${i + 1}. [${e.kind}] ${title}`,
62
- "",
63
- `**Tags:** ${tagStr}`,
64
- `**Updated:** ${updated}`,
65
- `**ID:** \`${e.id}\``,
66
- "",
67
- bodyText,
68
- "",
69
- "---",
70
- "",
71
- ].join("\n");
72
- })
73
- .join("");
74
-
75
- return header + body;
76
- }
77
-
78
- function slugifyTopic(topic) {
79
- return topic
80
- .toLowerCase()
81
- .replace(/[^a-z0-9]+/g, "-")
82
- .replace(/^-+|-+$/g, "")
83
- .slice(0, 120);
84
- }
85
-
86
- export async function handler(
87
- { topic, tags, buckets, kinds, identity_key },
88
- ctx,
89
- { ensureIndexed },
90
- ) {
91
- const { config } = ctx;
92
- const userId = ctx.userId !== undefined ? ctx.userId : undefined;
93
-
94
- const vaultErr = ensureVaultExists(config);
95
- if (vaultErr) return vaultErr;
96
-
97
- if (!topic?.trim()) {
98
- return err("Required: topic (non-empty string)", "INVALID_INPUT");
99
- }
100
-
101
- await ensureIndexed();
102
-
103
- const normalizedKinds = kinds?.map(normalizeKind) ?? [];
104
- const bucketTags = buckets?.length ? buckets.map((b) => `bucket:${b}`) : [];
105
- const effectiveTags = [...(tags ?? []), ...bucketTags];
106
-
107
- let candidates = [];
108
-
109
- if (normalizedKinds.length > 0) {
110
- for (const kindFilter of normalizedKinds) {
111
- const rows = await hybridSearch(ctx, topic, {
112
- kindFilter,
113
- limit: Math.ceil(MAX_ENTRIES_FOR_GATHER / normalizedKinds.length),
114
- userIdFilter: userId,
115
- includeSuperseeded: false,
116
- });
117
- candidates.push(...rows);
118
- }
119
- const seen = new Set();
120
- candidates = candidates.filter((r) => {
121
- if (seen.has(r.id)) return false;
122
- seen.add(r.id);
123
- return true;
124
- });
125
- } else {
126
- candidates = await hybridSearch(ctx, topic, {
127
- limit: MAX_ENTRIES_FOR_GATHER,
128
- userIdFilter: userId,
129
- includeSuperseeded: false,
130
- });
131
- }
132
-
133
- if (effectiveTags.length) {
134
- candidates = candidates.filter((r) => {
135
- const entryTags = r.tags ? JSON.parse(r.tags) : [];
136
- return effectiveTags.some((t) => entryTags.includes(t));
137
- });
138
- }
139
-
140
- const noiseIds = candidates
141
- .filter((r) => NOISE_KINDS.has(r.kind))
142
- .map((r) => r.id);
143
-
144
- const gatherEntries = candidates.filter((r) => !NOISE_KINDS.has(r.kind));
145
-
146
- if (gatherEntries.length === 0) {
147
- return err(
148
- `No entries found for topic "${topic}". Try a broader topic or different tags.`,
149
- "NO_ENTRIES",
150
- );
151
- }
152
-
153
- const briefBody = formatGatheredEntries(topic, gatherEntries);
154
-
155
- const effectiveIdentityKey =
156
- identity_key ?? `snapshot-${slugifyTopic(topic)}`;
157
-
158
- const briefTags = [
159
- "snapshot",
160
- ...(tags ?? []),
161
- ...(normalizedKinds.length > 0 ? [] : []),
162
- ];
163
-
164
- const supersedes = noiseIds.length > 0 ? noiseIds : undefined;
165
-
166
- const entry = await captureAndIndex(ctx, {
167
- kind: "brief",
168
- title: `${topic} — Context Brief`,
169
- body: briefBody,
170
- tags: briefTags,
171
- source: "create_snapshot",
172
- identity_key: effectiveIdentityKey,
173
- supersedes,
174
- userId,
175
- meta: {
176
- topic,
177
- entry_count: gatherEntries.length,
178
- noise_superseded: noiseIds.length,
179
- synthesized_from: gatherEntries.map((e) => e.id),
180
- },
181
- });
182
-
183
- const parts = [
184
- `✓ Snapshot created → id: ${entry.id}`,
185
- ` title: ${entry.title}`,
186
- ` identity_key: ${effectiveIdentityKey}`,
187
- ` synthesized from: ${gatherEntries.length} entries`,
188
- noiseIds.length > 0
189
- ? ` noise superseded: ${noiseIds.length} entries`
190
- : null,
191
- "",
192
- "_Retrieve with: get_context(kind: 'brief', identity_key: '" +
193
- effectiveIdentityKey +
194
- "')_",
195
- ]
196
- .filter((l) => l !== null)
197
- .join("\n");
198
-
199
- return ok(parts);
200
- }
@@ -1,60 +0,0 @@
1
- import { z } from "zod";
2
- import { unlinkSync } from "node:fs";
3
- import { ok, err } from "../helpers.js";
4
-
5
- export const name = "delete_context";
6
-
7
- export const description =
8
- "Delete an entry from your vault by its ULID id. Removes the file from disk and cleans up the search index.";
9
-
10
- export const inputSchema = {
11
- id: z.string().describe("The entry ULID to delete"),
12
- };
13
-
14
- /**
15
- * @param {object} args
16
- * @param {import('../types.js').BaseCtx & Partial<import('../types.js').HostedCtxExtensions>} ctx
17
- * @param {import('../types.js').ToolShared} shared
18
- */
19
- export async function handler({ id }, ctx, { ensureIndexed }) {
20
- const userId = ctx.userId !== undefined ? ctx.userId : undefined;
21
-
22
- if (!id?.trim())
23
- return err("Required: id (non-empty string)", "INVALID_INPUT");
24
- await ensureIndexed();
25
-
26
- const entry = ctx.stmts.getEntryById.get(id);
27
- if (!entry) return err(`Entry not found: ${id}`, "NOT_FOUND");
28
-
29
- // Ownership check: don't leak existence across users
30
- if (userId !== undefined && entry.user_id !== userId) {
31
- return err(`Entry not found: ${id}`, "NOT_FOUND");
32
- }
33
-
34
- // Delete file from disk first (source of truth)
35
- let fileWarning = null;
36
- if (entry.file_path) {
37
- try {
38
- unlinkSync(entry.file_path);
39
- } catch (e) {
40
- // ENOENT = already gone — not an error worth surfacing
41
- if (e.code !== "ENOENT") {
42
- fileWarning = `file could not be removed from disk (${e.code}): ${entry.file_path}`;
43
- }
44
- }
45
- }
46
-
47
- // Delete vector embedding
48
- const rowidResult = ctx.stmts.getRowid.get(id);
49
- if (rowidResult?.rowid) {
50
- try {
51
- ctx.deleteVec(Number(rowidResult.rowid));
52
- } catch {}
53
- }
54
-
55
- // Delete DB row (FTS trigger handles FTS cleanup)
56
- ctx.stmts.deleteEntry.run(id);
57
-
58
- const msg = `Deleted ${entry.kind}: ${entry.title || "(untitled)"} [${id}]`;
59
- return ok(fileWarning ? `${msg}\nWarning: ${fileWarning}` : msg);
60
- }