context-vault 3.1.6 → 3.1.8
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/bin/cli.js +1369 -1774
- package/dist/archive.d.ts +23 -0
- package/dist/archive.d.ts.map +1 -0
- package/dist/archive.js +197 -0
- package/dist/archive.js.map +1 -0
- package/dist/consolidation.d.ts +14 -0
- package/dist/consolidation.d.ts.map +1 -0
- package/dist/consolidation.js +59 -0
- package/dist/consolidation.js.map +1 -0
- package/dist/error-log.d.ts +4 -0
- package/dist/error-log.d.ts.map +1 -0
- package/dist/error-log.js +33 -0
- package/dist/error-log.js.map +1 -0
- package/dist/helpers.d.ts +10 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +42 -0
- package/dist/helpers.js.map +1 -0
- package/dist/linking.d.ts +13 -0
- package/dist/linking.d.ts.map +1 -0
- package/dist/linking.js +86 -0
- package/dist/linking.js.map +1 -0
- package/dist/migrate-dirs.d.ts +16 -0
- package/dist/migrate-dirs.d.ts.map +1 -0
- package/dist/migrate-dirs.js +127 -0
- package/dist/migrate-dirs.js.map +1 -0
- package/dist/register-tools.d.ts +3 -0
- package/dist/register-tools.d.ts.map +1 -0
- package/dist/register-tools.js +161 -0
- package/dist/register-tools.js.map +1 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +241 -0
- package/dist/server.js.map +1 -0
- package/dist/status.d.ts +18 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +265 -0
- package/dist/status.js.map +1 -0
- package/dist/telemetry.d.ts +6 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +74 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/temporal.d.ts +9 -0
- package/dist/temporal.d.ts.map +1 -0
- package/dist/temporal.js +76 -0
- package/dist/temporal.js.map +1 -0
- package/dist/tools/clear-context.d.ts +11 -0
- package/dist/tools/clear-context.d.ts.map +1 -0
- package/dist/tools/clear-context.js +28 -0
- package/dist/tools/clear-context.js.map +1 -0
- package/dist/tools/context-status.d.ts +6 -0
- package/dist/tools/context-status.d.ts.map +1 -0
- package/dist/tools/context-status.js +160 -0
- package/dist/tools/context-status.js.map +1 -0
- package/dist/tools/create-snapshot.d.ts +13 -0
- package/dist/tools/create-snapshot.d.ts.map +1 -0
- package/dist/tools/create-snapshot.js +161 -0
- package/dist/tools/create-snapshot.js.map +1 -0
- package/dist/tools/delete-context.d.ts +9 -0
- package/dist/tools/delete-context.d.ts.map +1 -0
- package/dist/tools/delete-context.js +45 -0
- package/dist/tools/delete-context.js.map +1 -0
- package/dist/tools/get-context.d.ts +85 -0
- package/dist/tools/get-context.d.ts.map +1 -0
- package/dist/tools/get-context.js +576 -0
- package/dist/tools/get-context.js.map +1 -0
- package/dist/tools/ingest-project.d.ts +11 -0
- package/dist/tools/ingest-project.d.ts.map +1 -0
- package/dist/tools/ingest-project.js +226 -0
- package/dist/tools/ingest-project.js.map +1 -0
- package/dist/tools/ingest-url.d.ts +11 -0
- package/dist/tools/ingest-url.d.ts.map +1 -0
- package/dist/tools/ingest-url.js +62 -0
- package/dist/tools/ingest-url.js.map +1 -0
- package/dist/tools/list-buckets.d.ts +9 -0
- package/dist/tools/list-buckets.d.ts.map +1 -0
- package/dist/tools/list-buckets.js +76 -0
- package/dist/tools/list-buckets.js.map +1 -0
- package/dist/tools/list-context.d.ts +19 -0
- package/dist/tools/list-context.d.ts.map +1 -0
- package/dist/tools/list-context.js +110 -0
- package/dist/tools/list-context.js.map +1 -0
- package/dist/tools/save-context.d.ts +36 -0
- package/dist/tools/save-context.d.ts.map +1 -0
- package/dist/tools/save-context.js +458 -0
- package/dist/tools/save-context.js.map +1 -0
- package/dist/tools/session-start.d.ts +11 -0
- package/dist/tools/session-start.d.ts.map +1 -0
- package/dist/tools/session-start.js +224 -0
- package/dist/tools/session-start.js.map +1 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/node_modules/@context-vault/core/dist/capture.d.ts +1 -1
- package/node_modules/@context-vault/core/dist/capture.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/capture.js +34 -47
- package/node_modules/@context-vault/core/dist/capture.js.map +1 -1
- package/node_modules/@context-vault/core/dist/categories.js +30 -30
- package/node_modules/@context-vault/core/dist/config.d.ts +1 -1
- package/node_modules/@context-vault/core/dist/config.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/config.js +37 -43
- package/node_modules/@context-vault/core/dist/config.js.map +1 -1
- package/node_modules/@context-vault/core/dist/constants.d.ts +1 -1
- package/node_modules/@context-vault/core/dist/constants.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/constants.js +4 -4
- package/node_modules/@context-vault/core/dist/constants.js.map +1 -1
- package/node_modules/@context-vault/core/dist/db.d.ts +2 -2
- package/node_modules/@context-vault/core/dist/db.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/db.js +21 -20
- package/node_modules/@context-vault/core/dist/db.js.map +1 -1
- package/node_modules/@context-vault/core/dist/embed.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/embed.js +11 -11
- package/node_modules/@context-vault/core/dist/embed.js.map +1 -1
- package/node_modules/@context-vault/core/dist/files.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/files.js +12 -13
- package/node_modules/@context-vault/core/dist/files.js.map +1 -1
- package/node_modules/@context-vault/core/dist/formatters.js +5 -5
- package/node_modules/@context-vault/core/dist/frontmatter.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/frontmatter.js +23 -23
- package/node_modules/@context-vault/core/dist/frontmatter.js.map +1 -1
- package/node_modules/@context-vault/core/dist/index.d.ts +1 -1
- package/node_modules/@context-vault/core/dist/index.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/index.js +58 -46
- package/node_modules/@context-vault/core/dist/index.js.map +1 -1
- package/node_modules/@context-vault/core/dist/ingest-url.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/ingest-url.js +30 -33
- package/node_modules/@context-vault/core/dist/ingest-url.js.map +1 -1
- package/node_modules/@context-vault/core/dist/main.d.ts +13 -13
- package/node_modules/@context-vault/core/dist/main.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/main.js +12 -12
- package/node_modules/@context-vault/core/dist/main.js.map +1 -1
- package/node_modules/@context-vault/core/dist/search.d.ts +1 -1
- package/node_modules/@context-vault/core/dist/search.d.ts.map +1 -1
- package/node_modules/@context-vault/core/dist/search.js +20 -22
- package/node_modules/@context-vault/core/dist/search.js.map +1 -1
- package/node_modules/@context-vault/core/dist/types.d.ts +1 -1
- package/node_modules/@context-vault/core/package.json +1 -1
- package/node_modules/@context-vault/core/src/capture.ts +44 -81
- package/node_modules/@context-vault/core/src/categories.ts +30 -30
- package/node_modules/@context-vault/core/src/config.ts +45 -60
- package/node_modules/@context-vault/core/src/constants.ts +8 -10
- package/node_modules/@context-vault/core/src/db.ts +37 -56
- package/node_modules/@context-vault/core/src/embed.ts +15 -26
- package/node_modules/@context-vault/core/src/files.ts +13 -16
- package/node_modules/@context-vault/core/src/formatters.ts +5 -5
- package/node_modules/@context-vault/core/src/frontmatter.ts +26 -30
- package/node_modules/@context-vault/core/src/index.ts +94 -100
- package/node_modules/@context-vault/core/src/ingest-url.ts +56 -93
- package/node_modules/@context-vault/core/src/main.ts +13 -18
- package/node_modules/@context-vault/core/src/search.ts +34 -56
- package/node_modules/@context-vault/core/src/types.ts +1 -1
- package/package.json +10 -4
- package/scripts/postinstall.js +18 -25
- package/scripts/prepack.js +13 -19
- package/src/archive.ts +244 -0
- package/src/consolidation.ts +78 -0
- package/src/{error-log.js → error-log.ts} +10 -10
- package/src/helpers.ts +61 -0
- package/src/{linking.js → linking.ts} +22 -20
- package/src/migrate-dirs.ts +152 -0
- package/src/register-tools.ts +183 -0
- package/src/{server.js → server.ts} +89 -109
- package/src/{status.js → status.ts} +94 -108
- package/src/telemetry.ts +80 -0
- package/src/{temporal.js → temporal.ts} +29 -33
- package/src/tools/clear-context.ts +41 -0
- package/src/tools/{context-status.js → context-status.ts} +43 -66
- package/src/tools/{create-snapshot.js → create-snapshot.ts} +54 -65
- package/src/tools/delete-context.ts +53 -0
- package/src/tools/{get-context.js → get-context.ts} +142 -205
- package/src/tools/ingest-project.ts +260 -0
- package/src/tools/ingest-url.ts +74 -0
- package/src/tools/{list-buckets.js → list-buckets.ts} +27 -37
- package/src/tools/{list-context.js → list-context.ts} +46 -71
- package/src/tools/{save-context.js → save-context.ts} +148 -204
- package/src/tools/{session-start.js → session-start.ts} +72 -79
- package/src/types.ts +29 -0
- package/src/helpers.js +0 -57
- package/src/register-tools.js +0 -175
- package/src/telemetry.js +0 -80
- package/src/tools/clear-context.js +0 -47
- package/src/tools/delete-context.js +0 -54
- package/src/tools/ingest-project.js +0 -272
- package/src/tools/ingest-url.js +0 -87
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
readdirSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
renameSync,
|
|
6
|
+
copyFileSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
statSync,
|
|
9
|
+
} from 'node:fs';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
|
|
12
|
+
export const PLURAL_TO_SINGULAR: Record<string, string> = {
|
|
13
|
+
insights: 'insight',
|
|
14
|
+
decisions: 'decision',
|
|
15
|
+
patterns: 'pattern',
|
|
16
|
+
statuses: 'status',
|
|
17
|
+
analyses: 'analysis',
|
|
18
|
+
contacts: 'contact',
|
|
19
|
+
projects: 'project',
|
|
20
|
+
tools: 'tool',
|
|
21
|
+
sources: 'source',
|
|
22
|
+
conversations: 'conversation',
|
|
23
|
+
messages: 'message',
|
|
24
|
+
sessions: 'session',
|
|
25
|
+
logs: 'log',
|
|
26
|
+
feedbacks: 'feedback',
|
|
27
|
+
notes: 'note',
|
|
28
|
+
prompts: 'prompt',
|
|
29
|
+
documents: 'document',
|
|
30
|
+
references: 'reference',
|
|
31
|
+
tasks: 'task',
|
|
32
|
+
buckets: 'bucket',
|
|
33
|
+
architectures: 'architecture',
|
|
34
|
+
briefs: 'brief',
|
|
35
|
+
companies: 'company',
|
|
36
|
+
discoveries: 'discovery',
|
|
37
|
+
events: 'event',
|
|
38
|
+
ideas: 'idea',
|
|
39
|
+
issues: 'issue',
|
|
40
|
+
agents: 'agent',
|
|
41
|
+
'session-summaries': 'session-summary',
|
|
42
|
+
'session-reviews': 'session-review',
|
|
43
|
+
'user-prompts': 'user-prompt',
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const CATEGORY_DIRS = ['knowledge', 'entities', 'events'];
|
|
47
|
+
|
|
48
|
+
function countMdFiles(dir: string): number {
|
|
49
|
+
let count = 0;
|
|
50
|
+
try {
|
|
51
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
52
|
+
if (entry.isDirectory()) {
|
|
53
|
+
count += countMdFiles(join(dir, entry.name));
|
|
54
|
+
} else if (entry.isFile() && entry.name.endsWith('.md')) {
|
|
55
|
+
count++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} catch {}
|
|
59
|
+
return count;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface MigrationOp {
|
|
63
|
+
action: 'rename' | 'merge';
|
|
64
|
+
pluralDir: string;
|
|
65
|
+
singularDir: string;
|
|
66
|
+
pluralName: string;
|
|
67
|
+
singularName: string;
|
|
68
|
+
fileCount: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function planMigration(vaultDir: string): MigrationOp[] {
|
|
72
|
+
const ops = [];
|
|
73
|
+
|
|
74
|
+
for (const catName of CATEGORY_DIRS) {
|
|
75
|
+
const catDir = join(vaultDir, catName);
|
|
76
|
+
if (!existsSync(catDir) || !statSync(catDir).isDirectory()) continue;
|
|
77
|
+
|
|
78
|
+
let entries;
|
|
79
|
+
try {
|
|
80
|
+
entries = readdirSync(catDir, { withFileTypes: true });
|
|
81
|
+
} catch {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
if (!entry.isDirectory()) continue;
|
|
87
|
+
const dirName = entry.name;
|
|
88
|
+
const singular = PLURAL_TO_SINGULAR[dirName];
|
|
89
|
+
if (!singular) continue;
|
|
90
|
+
|
|
91
|
+
const pluralDir = join(catDir, dirName);
|
|
92
|
+
const singularDir = join(catDir, singular);
|
|
93
|
+
if (pluralDir === singularDir) continue;
|
|
94
|
+
|
|
95
|
+
const fileCount = countMdFiles(pluralDir);
|
|
96
|
+
const singularExists = existsSync(singularDir);
|
|
97
|
+
|
|
98
|
+
ops.push({
|
|
99
|
+
action: (singularExists ? 'merge' : 'rename') as 'rename' | 'merge',
|
|
100
|
+
pluralDir,
|
|
101
|
+
singularDir,
|
|
102
|
+
pluralName: dirName,
|
|
103
|
+
singularName: singular,
|
|
104
|
+
fileCount,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return ops;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function mergeDir(src: string, dst: string): void {
|
|
113
|
+
mkdirSync(dst, { recursive: true });
|
|
114
|
+
for (const entry of readdirSync(src, { withFileTypes: true })) {
|
|
115
|
+
const srcPath = join(src, entry.name);
|
|
116
|
+
const dstPath = join(dst, entry.name);
|
|
117
|
+
if (entry.isDirectory()) {
|
|
118
|
+
mergeDir(srcPath, dstPath);
|
|
119
|
+
} else if (entry.isFile()) {
|
|
120
|
+
if (!existsSync(dstPath)) {
|
|
121
|
+
copyFileSync(srcPath, dstPath);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function executeMigration(ops: MigrationOp[]): {
|
|
128
|
+
renamed: number;
|
|
129
|
+
merged: number;
|
|
130
|
+
errors: string[];
|
|
131
|
+
} {
|
|
132
|
+
let renamed = 0;
|
|
133
|
+
let merged = 0;
|
|
134
|
+
const errors: string[] = [];
|
|
135
|
+
|
|
136
|
+
for (const op of ops) {
|
|
137
|
+
try {
|
|
138
|
+
if (op.action === 'rename') {
|
|
139
|
+
renameSync(op.pluralDir, op.singularDir);
|
|
140
|
+
renamed++;
|
|
141
|
+
} else {
|
|
142
|
+
mergeDir(op.pluralDir, op.singularDir);
|
|
143
|
+
rmSync(op.pluralDir, { recursive: true, force: true });
|
|
144
|
+
merged++;
|
|
145
|
+
}
|
|
146
|
+
} catch (e) {
|
|
147
|
+
errors.push(`${op.pluralName} → ${op.singularName}: ${(e as Error).message}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return { renamed, merged, errors };
|
|
152
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { reindex } from '@context-vault/core/index';
|
|
2
|
+
import { captureAndIndex } from '@context-vault/core/capture';
|
|
3
|
+
import { err } from './helpers.js';
|
|
4
|
+
import { sendTelemetryEvent } from './telemetry.js';
|
|
5
|
+
import type { LocalCtx, SharedCtx, ToolResult } from './types.js';
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
import { join, dirname } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
|
|
10
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
const pkg = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'), 'utf-8'));
|
|
12
|
+
|
|
13
|
+
import * as getContext from './tools/get-context.js';
|
|
14
|
+
import * as saveContext from './tools/save-context.js';
|
|
15
|
+
import * as listContext from './tools/list-context.js';
|
|
16
|
+
import * as deleteContext from './tools/delete-context.js';
|
|
17
|
+
import * as ingestUrl from './tools/ingest-url.js';
|
|
18
|
+
import * as contextStatus from './tools/context-status.js';
|
|
19
|
+
import * as clearContext from './tools/clear-context.js';
|
|
20
|
+
import * as createSnapshot from './tools/create-snapshot.js';
|
|
21
|
+
import * as sessionStart from './tools/session-start.js';
|
|
22
|
+
import * as listBuckets from './tools/list-buckets.js';
|
|
23
|
+
import * as ingestProject from './tools/ingest-project.js';
|
|
24
|
+
|
|
25
|
+
const toolModules = [
|
|
26
|
+
getContext,
|
|
27
|
+
saveContext,
|
|
28
|
+
listContext,
|
|
29
|
+
deleteContext,
|
|
30
|
+
ingestUrl,
|
|
31
|
+
ingestProject,
|
|
32
|
+
contextStatus,
|
|
33
|
+
clearContext,
|
|
34
|
+
createSnapshot,
|
|
35
|
+
sessionStart,
|
|
36
|
+
listBuckets,
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const TOOL_TIMEOUT_MS = 120_000;
|
|
40
|
+
|
|
41
|
+
export function registerTools(server: any, ctx: LocalCtx): void {
|
|
42
|
+
function tracked(
|
|
43
|
+
handler: (...args: any[]) => Promise<ToolResult>,
|
|
44
|
+
toolName: string
|
|
45
|
+
): (...args: any[]) => Promise<ToolResult> {
|
|
46
|
+
return async (...args: any[]): Promise<ToolResult> => {
|
|
47
|
+
if (ctx.activeOps) ctx.activeOps.count++;
|
|
48
|
+
let timer;
|
|
49
|
+
let handlerPromise;
|
|
50
|
+
try {
|
|
51
|
+
handlerPromise = Promise.resolve(handler(...args));
|
|
52
|
+
const result = await Promise.race([
|
|
53
|
+
handlerPromise,
|
|
54
|
+
new Promise((_, reject) => {
|
|
55
|
+
timer = setTimeout(() => reject(new Error('TOOL_TIMEOUT')), TOOL_TIMEOUT_MS);
|
|
56
|
+
}),
|
|
57
|
+
]);
|
|
58
|
+
if (ctx.toolStats) ctx.toolStats.ok++;
|
|
59
|
+
return result as ToolResult;
|
|
60
|
+
} catch (rawErr) {
|
|
61
|
+
const e = rawErr as Error;
|
|
62
|
+
if (e.message === 'TOOL_TIMEOUT') {
|
|
63
|
+
handlerPromise?.catch(() => {});
|
|
64
|
+
if (ctx.toolStats) {
|
|
65
|
+
ctx.toolStats.errors++;
|
|
66
|
+
ctx.toolStats.lastError = {
|
|
67
|
+
tool: toolName,
|
|
68
|
+
code: 'TIMEOUT',
|
|
69
|
+
timestamp: Date.now(),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
sendTelemetryEvent(ctx.config, {
|
|
73
|
+
event: 'tool_error',
|
|
74
|
+
code: 'TIMEOUT',
|
|
75
|
+
tool: toolName,
|
|
76
|
+
cv_version: pkg.version,
|
|
77
|
+
});
|
|
78
|
+
return err(
|
|
79
|
+
'Tool timed out after 120s. Try a simpler query or run `context-vault reindex` first.',
|
|
80
|
+
'TIMEOUT'
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (ctx.toolStats) {
|
|
84
|
+
ctx.toolStats.errors++;
|
|
85
|
+
ctx.toolStats.lastError = {
|
|
86
|
+
tool: toolName,
|
|
87
|
+
code: 'UNKNOWN',
|
|
88
|
+
timestamp: Date.now(),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
sendTelemetryEvent(ctx.config, {
|
|
92
|
+
event: 'tool_error',
|
|
93
|
+
code: 'UNKNOWN',
|
|
94
|
+
tool: toolName,
|
|
95
|
+
cv_version: pkg.version,
|
|
96
|
+
});
|
|
97
|
+
try {
|
|
98
|
+
await captureAndIndex(ctx, {
|
|
99
|
+
kind: 'feedback',
|
|
100
|
+
title: `Unhandled error in ${toolName ?? 'tool'} call`,
|
|
101
|
+
body: `${e.message}\n\n${(e as any).stack ?? ''}`,
|
|
102
|
+
tags: ['bug', 'auto-captured'],
|
|
103
|
+
source: 'auto-capture',
|
|
104
|
+
meta: {
|
|
105
|
+
tool: toolName,
|
|
106
|
+
error_type: (e as any).constructor?.name,
|
|
107
|
+
cv_version: pkg.version,
|
|
108
|
+
auto: true,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
} catch {}
|
|
112
|
+
return err(e.message, 'INTERNAL_ERROR');
|
|
113
|
+
} finally {
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
if (ctx.activeOps) ctx.activeOps.count--;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let reindexDone = false;
|
|
121
|
+
let reindexPromise: Promise<void> | null = null;
|
|
122
|
+
let reindexAttempts = 0;
|
|
123
|
+
let reindexFailed = false;
|
|
124
|
+
const MAX_REINDEX_ATTEMPTS = 2;
|
|
125
|
+
|
|
126
|
+
async function ensureIndexed({ blocking = true }: { blocking?: boolean } = {}): Promise<void> {
|
|
127
|
+
if (reindexDone) return;
|
|
128
|
+
if (reindexPromise) {
|
|
129
|
+
if (blocking) return reindexPromise;
|
|
130
|
+
return; // non-blocking: just ensure it's started
|
|
131
|
+
}
|
|
132
|
+
const promise = reindex(ctx, { fullSync: true })
|
|
133
|
+
.then((stats) => {
|
|
134
|
+
reindexDone = true;
|
|
135
|
+
const total = stats.added + stats.updated + stats.removed;
|
|
136
|
+
if (total > 0) {
|
|
137
|
+
console.error(
|
|
138
|
+
`[context-vault] Auto-reindex: +${stats.added} ~${stats.updated} -${stats.removed} (${stats.unchanged} unchanged)`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
.catch((e: Error) => {
|
|
143
|
+
reindexAttempts++;
|
|
144
|
+
console.error(
|
|
145
|
+
`[context-vault] Auto-reindex failed (attempt ${reindexAttempts}/${MAX_REINDEX_ATTEMPTS}): ${e.message}`
|
|
146
|
+
);
|
|
147
|
+
if (reindexAttempts >= MAX_REINDEX_ATTEMPTS) {
|
|
148
|
+
console.error(
|
|
149
|
+
`[context-vault] Giving up on auto-reindex. Run \`context-vault reindex\` manually to diagnose.`
|
|
150
|
+
);
|
|
151
|
+
reindexDone = true;
|
|
152
|
+
reindexFailed = true;
|
|
153
|
+
} else {
|
|
154
|
+
reindexPromise = null;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
reindexPromise = promise;
|
|
158
|
+
if (blocking) return reindexPromise;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const shared = {
|
|
162
|
+
ensureIndexed,
|
|
163
|
+
get reindexFailed() {
|
|
164
|
+
return reindexFailed;
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
for (const mod of toolModules) {
|
|
169
|
+
server.tool(
|
|
170
|
+
mod.name,
|
|
171
|
+
mod.description,
|
|
172
|
+
mod.inputSchema,
|
|
173
|
+
tracked(
|
|
174
|
+
((args: Record<string, unknown>) => mod.handler(args, ctx, shared)) as (
|
|
175
|
+
...args: any[]
|
|
176
|
+
) => Promise<ToolResult>,
|
|
177
|
+
mod.name
|
|
178
|
+
)
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
ensureIndexed().catch(() => {});
|
|
183
|
+
}
|