claude-db 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -0
- package/dist/capture/cursors.d.ts +1 -3
- package/dist/capture/cursors.js +16 -35
- package/dist/capture/flush.d.ts +1 -3
- package/dist/capture/flush.js +34 -34
- package/dist/capture/progress.js +4 -2
- package/dist/capture/transcript.d.ts +1 -1
- package/dist/capture/transcript.js +6 -6
- package/dist/catchup/gather.js +1 -1
- package/dist/cli/commands/catchup.js +2 -1
- package/dist/cli/commands/graph.js +9 -10
- package/dist/cli/commands/insight.js +10 -1
- package/dist/cli/commands/memory.js +2 -1
- package/dist/cli/commands/setup.js +1 -2
- package/dist/cli/commands/sync.js +4 -1
- package/dist/cli/install.js +1 -2
- package/dist/cli/instructions.d.ts +1 -3
- package/dist/cli/instructions.js +5 -40
- package/dist/cli/refresh.js +2 -6
- package/dist/config/load.js +2 -1
- package/dist/config/schema.d.ts +0 -2
- package/dist/config/schema.js +0 -2
- package/dist/context.js +16 -7
- package/dist/daemon/client.d.ts +5 -3
- package/dist/daemon/client.js +43 -25
- package/dist/daemon/paths.d.ts +3 -1
- package/dist/daemon/pid.d.ts +3 -0
- package/dist/daemon/pid.js +20 -0
- package/dist/daemon/server.js +9 -7
- package/dist/embed/local.js +4 -1
- package/dist/facts/claude-memory.js +2 -2
- package/dist/facts/last-chat.d.ts +1 -1
- package/dist/facts/last-chat.js +5 -4
- package/dist/facts/model.d.ts +1 -1
- package/dist/facts/model.js +20 -16
- package/dist/facts/start-render.d.ts +13 -0
- package/dist/facts/start-render.js +43 -0
- package/dist/facts/start.d.ts +3 -6
- package/dist/facts/start.js +32 -46
- package/dist/graph/grammars.js +2 -1
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/graph/query/index.d.ts +1 -0
- package/dist/graph/query/index.js +1 -0
- package/dist/graph/query/stale.js +19 -2
- package/dist/graph/query/usages.d.ts +12 -0
- package/dist/graph/query/usages.js +37 -0
- package/dist/graph/scan/cache.js +2 -1
- package/dist/graph/scan/files.js +2 -2
- package/dist/hooks/compact.d.ts +1 -0
- package/dist/hooks/compact.js +3 -2
- package/dist/hooks/detached.d.ts +0 -1
- package/dist/hooks/detached.js +2 -6
- package/dist/hooks/grep-symbols.d.ts +3 -1
- package/dist/hooks/grep-symbols.js +41 -2
- package/dist/hooks/handlers/prefer-usages.js +15 -10
- package/dist/hooks/handlers/session-end.js +4 -6
- package/dist/hooks/handlers/session-start.js +19 -40
- package/dist/hooks/handlers/turn-end.js +1 -1
- package/dist/hooks/handlers/user-prompt.js +5 -5
- package/dist/hooks/local.js +0 -1
- package/dist/hooks/payload.js +6 -5
- package/dist/hooks/pick-deliver.js +2 -1
- package/dist/hooks/prompt-recall.d.ts +4 -2
- package/dist/hooks/prompt-recall.js +10 -10
- package/dist/hooks/route.js +8 -3
- package/dist/hooks/run.d.ts +1 -2
- package/dist/mcp/tools/ids.d.ts +3 -0
- package/dist/mcp/tools/ids.js +6 -0
- package/dist/mcp/tools/memory.js +2 -1
- package/dist/mcp/tools/observations.js +3 -2
- package/dist/mcp/tools/search.js +4 -9
- package/dist/mcp/tools/usages.js +18 -44
- package/dist/pick/worker.js +1 -2
- package/dist/search/service.d.ts +5 -0
- package/dist/search/service.js +11 -5
- package/dist/store/postgres/filters.d.ts +1 -0
- package/dist/store/postgres/filters.js +3 -0
- package/dist/store/postgres/graph.js +3 -2
- package/dist/store/postgres/index.js +3 -1
- package/dist/store/postgres/meta.d.ts +1 -0
- package/dist/store/postgres/meta.js +4 -0
- package/dist/store/postgres/observations.js +3 -3
- package/dist/store/project-resolver.d.ts +1 -0
- package/dist/store/project-resolver.js +1 -1
- package/dist/store/scoped-store.js +3 -1
- package/dist/store/sqlite/filters.d.ts +1 -0
- package/dist/store/sqlite/filters.js +6 -2
- package/dist/store/sqlite/graph.js +3 -2
- package/dist/store/sqlite/observations.js +6 -7
- package/dist/util/claude-binary.js +5 -4
- package/dist/util/claude-cli.js +2 -1
- package/dist/util/claude-dir.d.ts +2 -0
- package/dist/util/claude-dir.js +8 -0
- package/dist/util/env.d.ts +2 -0
- package/dist/util/env.js +6 -0
- package/dist/util/shortid.d.ts +3 -0
- package/dist/util/shortid.js +11 -0
- package/docs/how-it-works.md +5 -10
- package/docs/setup-guide.md +1 -1
- package/package.json +7 -6
- package/dist/capture/flush-lock.d.ts +0 -11
- package/dist/capture/flush-lock.js +0 -108
- package/dist/hooks/start-legacy.d.ts +0 -6
- package/dist/hooks/start-legacy.js +0 -47
package/dist/config/schema.js
CHANGED
|
@@ -26,7 +26,6 @@ export const ConfigSchema = z.object({
|
|
|
26
26
|
.prefault({}),
|
|
27
27
|
capture: z
|
|
28
28
|
.object({
|
|
29
|
-
tools: z.array(z.string()).default(['Edit', 'Write', 'Bash', 'NotebookEdit']),
|
|
30
29
|
exclude: z.array(z.string()).default(['.env', 'secrets', 'node_modules', '.git/']),
|
|
31
30
|
maxBodyChars: z.number().int().positive().default(4000),
|
|
32
31
|
scripted: z.boolean().default(false),
|
|
@@ -51,7 +50,6 @@ export const ConfigSchema = z.object({
|
|
|
51
50
|
.prefault({}),
|
|
52
51
|
inject: z
|
|
53
52
|
.object({
|
|
54
|
-
sessions: z.number().int().nonnegative().default(5),
|
|
55
53
|
maxChars: z.number().int().positive().default(6000),
|
|
56
54
|
perPrompt: z.boolean().default(true),
|
|
57
55
|
promptResults: z.number().int().positive().max(10).default(2),
|
package/dist/context.js
CHANGED
|
@@ -4,6 +4,7 @@ import { SearchService } from './search/index.js';
|
|
|
4
4
|
import { createStore } from './store/index.js';
|
|
5
5
|
import { createScopeResolver } from './store/project-resolver.js';
|
|
6
6
|
import { withProjectScope } from './store/scoped-store.js';
|
|
7
|
+
const RETRY_LOAD_MS = 60_000;
|
|
7
8
|
export async function createContext(overrides) {
|
|
8
9
|
const config = { ...loadConfig(), ...overrides };
|
|
9
10
|
const raw = await createStore(config.database);
|
|
@@ -17,16 +18,24 @@ export function embedderWithDeadline(load, timeoutMs, now = Date.now) {
|
|
|
17
18
|
let loading = null;
|
|
18
19
|
let ready = null;
|
|
19
20
|
let deadline = 0;
|
|
21
|
+
let failedAt = null;
|
|
22
|
+
const startLoading = () => {
|
|
23
|
+
deadline = now() + timeoutMs;
|
|
24
|
+
failedAt = null;
|
|
25
|
+
return load().then((embedder) => {
|
|
26
|
+
ready = embedder;
|
|
27
|
+
return embedder;
|
|
28
|
+
}, () => {
|
|
29
|
+
failedAt = now();
|
|
30
|
+
return new NoopEmbedder();
|
|
31
|
+
});
|
|
32
|
+
};
|
|
20
33
|
return () => {
|
|
21
34
|
if (ready)
|
|
22
35
|
return Promise.resolve(ready);
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
void loading.then((embedder) => {
|
|
27
|
-
ready = embedder;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
36
|
+
if (failedAt !== null && now() - failedAt >= RETRY_LOAD_MS)
|
|
37
|
+
loading = null;
|
|
38
|
+
loading ??= startLoading();
|
|
30
39
|
if (timeoutMs <= 0)
|
|
31
40
|
return loading;
|
|
32
41
|
return withTimeout(loading, deadline - now());
|
package/dist/daemon/client.d.ts
CHANGED
|
@@ -2,11 +2,13 @@ import type { DaemonReply, DaemonRequest } from './paths.js';
|
|
|
2
2
|
export declare const STARTING_PREFIX = "daemon.starting.";
|
|
3
3
|
export type Asked = DaemonReply | {
|
|
4
4
|
kind: 'unreachable';
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'silent';
|
|
5
7
|
} | {
|
|
6
8
|
kind: 'timeout';
|
|
7
9
|
};
|
|
8
10
|
export declare function daemonWanted(env?: NodeJS.ProcessEnv): boolean;
|
|
9
|
-
export declare function
|
|
10
|
-
export declare function
|
|
11
|
-
export declare function askDaemon(request: DaemonRequest, timeoutMs: number): Promise<Asked>;
|
|
11
|
+
export declare function envForDaemon(env?: NodeJS.ProcessEnv): Record<string, string>;
|
|
12
|
+
export declare function askDaemon(request: DaemonRequest, replyMs: number): Promise<Asked>;
|
|
12
13
|
export declare function startDaemon(now?: number): boolean;
|
|
14
|
+
export declare function replaceDaemon(asked: number | null): void;
|
package/dist/daemon/client.js
CHANGED
|
@@ -4,12 +4,14 @@ import { homedir } from 'node:os';
|
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { CONFIG_DIR } from '../config/dir.js';
|
|
6
6
|
import { runDetached } from '../hooks/detached.js';
|
|
7
|
+
import { hookEnv } from '../util/env.js';
|
|
8
|
+
import { clearDaemonPid, readDaemonPid } from './pid.js';
|
|
7
9
|
import { DAEMON_SCRIPT, socketPath } from './paths.js';
|
|
8
|
-
const FORWARDED = ['CLAUDE_DB_CAPTURE', 'CLAUDE_CODE_ENTRYPOINT', 'CLAUDE_DB_USAGES_HOOK'];
|
|
9
10
|
const OWN_SETUP = ['CLAUDE_DB_URL', 'CLAUDE_DB_GRAPH_CACHE'];
|
|
10
11
|
const STARTING_FOR_MS = 15_000;
|
|
12
|
+
const ACK_MS = 2_000;
|
|
11
13
|
export const STARTING_PREFIX = 'daemon.starting.';
|
|
12
|
-
export function daemonWanted(env =
|
|
14
|
+
export function daemonWanted(env = hookEnv()) {
|
|
13
15
|
if (OWN_SETUP.some((name) => env[name]))
|
|
14
16
|
return false;
|
|
15
17
|
const forced = env['CLAUDE_DB_DAEMON'];
|
|
@@ -25,53 +27,54 @@ export function daemonWanted(env = process.env) {
|
|
|
25
27
|
return false;
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
|
-
export function
|
|
30
|
+
export function envForDaemon(env = hookEnv()) {
|
|
29
31
|
const picked = {};
|
|
30
|
-
for (const name of
|
|
31
|
-
const value = env[name];
|
|
32
|
+
for (const [name, value] of Object.entries(env)) {
|
|
32
33
|
if (value !== undefined)
|
|
33
34
|
picked[name] = value;
|
|
34
35
|
}
|
|
35
36
|
return picked;
|
|
36
37
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
function parseMessage(line) {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(line);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
42
44
|
}
|
|
43
|
-
return picked;
|
|
44
45
|
}
|
|
45
|
-
export function askDaemon(request,
|
|
46
|
+
export function askDaemon(request, replyMs) {
|
|
46
47
|
return new Promise((settle) => {
|
|
47
48
|
let sent = false;
|
|
48
|
-
let
|
|
49
|
+
let buffer = '';
|
|
49
50
|
const socket = createConnection(socketPath());
|
|
50
51
|
const finish = (asked) => {
|
|
51
52
|
clearTimeout(timer);
|
|
52
53
|
socket.destroy();
|
|
53
54
|
settle(asked);
|
|
54
55
|
};
|
|
55
|
-
const
|
|
56
|
+
const failed = () => (sent ? { kind: 'timeout' } : { kind: 'unreachable' });
|
|
57
|
+
let timer = setTimeout(() => finish({ kind: 'silent' }), ACK_MS);
|
|
56
58
|
socket.setEncoding('utf8');
|
|
57
59
|
socket.on('connect', () => {
|
|
58
60
|
sent = true;
|
|
59
61
|
socket.write(`${JSON.stringify(request)}\n`);
|
|
60
62
|
});
|
|
61
63
|
socket.on('data', (chunk) => {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
buffer += chunk;
|
|
65
|
+
for (let end = buffer.indexOf('\n'); end >= 0; end = buffer.indexOf('\n')) {
|
|
66
|
+
const message = parseMessage(buffer.slice(0, end));
|
|
67
|
+
buffer = buffer.slice(end + 1);
|
|
68
|
+
if (message?.kind !== 'ack') {
|
|
69
|
+
finish(message ?? { kind: 'timeout' });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
timer = setTimeout(() => finish({ kind: 'timeout' }), replyMs);
|
|
71
74
|
}
|
|
72
75
|
});
|
|
73
|
-
socket.on('error', () => finish(
|
|
74
|
-
socket.on('close', () => finish(
|
|
76
|
+
socket.on('error', () => finish(failed()));
|
|
77
|
+
socket.on('close', () => finish(failed()));
|
|
75
78
|
});
|
|
76
79
|
}
|
|
77
80
|
export function startDaemon(now = Date.now()) {
|
|
@@ -86,3 +89,18 @@ export function startDaemon(now = Date.now()) {
|
|
|
86
89
|
runDetached(DAEMON_SCRIPT, [], homedir(), () => rmSync(marker, { force: true }));
|
|
87
90
|
return true;
|
|
88
91
|
}
|
|
92
|
+
export function replaceDaemon(asked) {
|
|
93
|
+
if (asked !== null) {
|
|
94
|
+
try {
|
|
95
|
+
process.kill(asked, 'SIGKILL');
|
|
96
|
+
}
|
|
97
|
+
catch { }
|
|
98
|
+
}
|
|
99
|
+
if (readDaemonPid() === asked) {
|
|
100
|
+
if (asked !== null)
|
|
101
|
+
clearDaemonPid(asked);
|
|
102
|
+
if (process.platform !== 'win32')
|
|
103
|
+
rmSync(socketPath(), { force: true });
|
|
104
|
+
}
|
|
105
|
+
startDaemon();
|
|
106
|
+
}
|
package/dist/daemon/paths.d.ts
CHANGED
|
@@ -6,7 +6,6 @@ export interface DaemonRequest {
|
|
|
6
6
|
hook: string;
|
|
7
7
|
raw: string;
|
|
8
8
|
env: Record<string, string>;
|
|
9
|
-
workerEnv?: Record<string, string>;
|
|
10
9
|
}
|
|
11
10
|
export type DaemonReply = {
|
|
12
11
|
kind: 'done';
|
|
@@ -14,3 +13,6 @@ export type DaemonReply = {
|
|
|
14
13
|
} | {
|
|
15
14
|
kind: 'stale';
|
|
16
15
|
};
|
|
16
|
+
export type DaemonMessage = DaemonReply | {
|
|
17
|
+
kind: 'ack';
|
|
18
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { CONFIG_DIR } from '../config/dir.js';
|
|
4
|
+
const pidPath = () => join(CONFIG_DIR, 'daemon.pid');
|
|
5
|
+
export function readDaemonPid() {
|
|
6
|
+
try {
|
|
7
|
+
const pid = Number(readFileSync(pidPath(), 'utf8'));
|
|
8
|
+
return Number.isInteger(pid) && pid > 1 ? pid : null;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function writeDaemonPid() {
|
|
15
|
+
writeFileSync(pidPath(), String(process.pid));
|
|
16
|
+
}
|
|
17
|
+
export function clearDaemonPid(pid) {
|
|
18
|
+
if (readDaemonPid() === pid)
|
|
19
|
+
rmSync(pidPath(), { force: true });
|
|
20
|
+
}
|
package/dist/daemon/server.js
CHANGED
|
@@ -6,15 +6,17 @@ import { CONFIG_DIR, CONFIG_PATH, loadConfig } from '../config/index.js';
|
|
|
6
6
|
import { createContext } from '../context.js';
|
|
7
7
|
import { parsePayload } from '../hooks/payload.js';
|
|
8
8
|
import { isHookName, loadHandler } from '../hooks/run.js';
|
|
9
|
-
import {
|
|
9
|
+
import { withHookEnv } from '../util/env.js';
|
|
10
10
|
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
11
11
|
import { buildId, socketPath } from './paths.js';
|
|
12
|
+
import { clearDaemonPid, writeDaemonPid } from './pid.js';
|
|
12
13
|
import { STARTING_PREFIX } from './client.js';
|
|
13
14
|
silenceSqliteWarning();
|
|
14
15
|
const BUILD = buildId();
|
|
15
16
|
const SOCKET = socketPath();
|
|
16
17
|
const MAX_REQUEST_BYTES = 8 * 1024 * 1024;
|
|
17
18
|
const RETIRE_AFTER_MS = 30_000;
|
|
19
|
+
const ACK_LINE = `${JSON.stringify({ kind: 'ack' })}\n`;
|
|
18
20
|
let current = null;
|
|
19
21
|
let running = 0;
|
|
20
22
|
const pending = new Set();
|
|
@@ -92,14 +94,13 @@ async function answer(request) {
|
|
|
92
94
|
const out = [];
|
|
93
95
|
const run = {
|
|
94
96
|
payload: parsePayload(request.raw),
|
|
95
|
-
env: request.env,
|
|
96
97
|
write: (text) => out.push(text),
|
|
97
98
|
withContext: async (work) => work(await context()),
|
|
98
99
|
inDaemon: track,
|
|
99
100
|
};
|
|
100
101
|
try {
|
|
101
102
|
const handle = await loadHandler(request.hook);
|
|
102
|
-
await
|
|
103
|
+
await withHookEnv(request.env, () => handle(run));
|
|
103
104
|
}
|
|
104
105
|
catch (error) {
|
|
105
106
|
log(`${request.hook} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -119,14 +120,12 @@ function parseRequest(line) {
|
|
|
119
120
|
}
|
|
120
121
|
if (typeof parsed !== 'object' || parsed === null)
|
|
121
122
|
return null;
|
|
122
|
-
const { build, hook, raw, env
|
|
123
|
+
const { build, hook, raw, env } = parsed;
|
|
123
124
|
if (typeof build !== 'string' || typeof hook !== 'string' || typeof raw !== 'string')
|
|
124
125
|
return null;
|
|
125
126
|
if (!isStringMap(env))
|
|
126
127
|
return null;
|
|
127
|
-
|
|
128
|
-
return null;
|
|
129
|
-
return { build, hook, raw, env, ...(workerEnv ? { workerEnv } : {}) };
|
|
128
|
+
return { build, hook, raw, env };
|
|
130
129
|
}
|
|
131
130
|
function serve(socket) {
|
|
132
131
|
let buffer = '';
|
|
@@ -152,6 +151,7 @@ function serve(socket) {
|
|
|
152
151
|
socket.destroy();
|
|
153
152
|
return;
|
|
154
153
|
}
|
|
154
|
+
socket.write(ACK_LINE);
|
|
155
155
|
void answer(request)
|
|
156
156
|
.then((reply) => socket.end(`${JSON.stringify(reply)}\n`))
|
|
157
157
|
.catch((error) => {
|
|
@@ -172,6 +172,7 @@ async function shutdown(reason) {
|
|
|
172
172
|
clearTimeout(idleTimer);
|
|
173
173
|
server?.close();
|
|
174
174
|
await Promise.allSettled([...pending]);
|
|
175
|
+
clearDaemonPid(process.pid);
|
|
175
176
|
if (current)
|
|
176
177
|
await current.ctx.then((ctx) => ctx.close()).catch(() => undefined);
|
|
177
178
|
process.exit(0);
|
|
@@ -212,6 +213,7 @@ async function start() {
|
|
|
212
213
|
server.listen(SOCKET, () => {
|
|
213
214
|
if (process.platform !== 'win32')
|
|
214
215
|
chmodSync(SOCKET, 0o600);
|
|
216
|
+
writeDaemonPid();
|
|
215
217
|
clearStartMarkers();
|
|
216
218
|
log(`listening on ${SOCKET}`);
|
|
217
219
|
touch();
|
package/dist/embed/local.js
CHANGED
|
@@ -24,7 +24,10 @@ export class LocalEmbedder {
|
|
|
24
24
|
}
|
|
25
25
|
transformers.env.allowLocalModels = false;
|
|
26
26
|
return (await transformers.pipeline('feature-extraction', this.id));
|
|
27
|
-
})()
|
|
27
|
+
})().catch((error) => {
|
|
28
|
+
this.pipelinePromise = null;
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
28
31
|
return this.pipelinePromise;
|
|
29
32
|
}
|
|
30
33
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
-
import { homedir } from 'node:os';
|
|
3
2
|
import { join } from 'node:path';
|
|
4
3
|
import { embedObservations } from '../capture/flush.js';
|
|
4
|
+
import { claudeProjectsDir } from '../util/claude-dir.js';
|
|
5
5
|
import { CLAUDE_MEMORY_TAG, FACT_SESSION, factToObservation, machineTag, originTag, youScope, } from './model.js';
|
|
6
6
|
const MAX_TITLE = 200;
|
|
7
7
|
export function claudeMemoryDir(project) {
|
|
8
|
-
return join(
|
|
8
|
+
return join(claudeProjectsDir(), project.replace(/[^a-zA-Z0-9]/g, '-'), 'memory');
|
|
9
9
|
}
|
|
10
10
|
export function parseMemoryFile(text, fallbackTime) {
|
|
11
11
|
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/.exec(text);
|
|
@@ -3,7 +3,7 @@ import type { Observation } from '../types.js';
|
|
|
3
3
|
export interface RecentChat {
|
|
4
4
|
sessionId: string;
|
|
5
5
|
at: number;
|
|
6
|
-
summary: string
|
|
6
|
+
summary: string[];
|
|
7
7
|
turns: Observation[];
|
|
8
8
|
}
|
|
9
9
|
export declare function recentChats(ctx: RecallContext, project: string, current?: string): Promise<RecentChat[]>;
|
package/dist/facts/last-chat.js
CHANGED
|
@@ -29,11 +29,12 @@ function toChat(sessionId, newestFirst, session) {
|
|
|
29
29
|
}
|
|
30
30
|
function chatSummary(session, at, turns) {
|
|
31
31
|
if (!session?.summary || session.endedAt === undefined || session.endedAt < at)
|
|
32
|
-
return
|
|
32
|
+
return [];
|
|
33
33
|
const shown = new Set(turns.map((turn) => turn.title));
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
return session.summary
|
|
35
|
+
.split(SUMMARY_PARTS)
|
|
36
|
+
.filter((part) => !shown.has(part))
|
|
37
|
+
.flatMap((part) => toSnippet(part, SUMMARY_CHARS) ?? []);
|
|
37
38
|
}
|
|
38
39
|
export function latestRequestAt(chats, unfinished) {
|
|
39
40
|
const times = [...chats.map((chat) => chat.at), ...unfinished.map((entry) => entry.askedAt)];
|
package/dist/facts/model.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export interface Fact {
|
|
|
17
17
|
source: string;
|
|
18
18
|
tags?: string[];
|
|
19
19
|
}
|
|
20
|
-
export declare function youScope(project?: string): string;
|
|
20
|
+
export declare function youScope(project?: string, now?: () => number): string;
|
|
21
21
|
export declare function machineTag(): string;
|
|
22
22
|
export declare function originTag(project: string): string;
|
|
23
23
|
export declare function scopeProject(scope: FactScope, project: string): string;
|
package/dist/facts/model.js
CHANGED
|
@@ -4,6 +4,7 @@ import { homedir, hostname } from 'node:os';
|
|
|
4
4
|
import { isAbsolute } from 'node:path';
|
|
5
5
|
import { currentAuthor, observationId } from '../capture/identity.js';
|
|
6
6
|
import { redact } from '../capture/redact.js';
|
|
7
|
+
import { SCOPE_TTL_MS } from '../store/project-resolver.js';
|
|
7
8
|
import { scopeToken } from '../util/scope.js';
|
|
8
9
|
export const FACT_SESSION = 'facts';
|
|
9
10
|
export const FACT_TAG = 'fact';
|
|
@@ -30,22 +31,25 @@ function folderOf(project) {
|
|
|
30
31
|
return project;
|
|
31
32
|
return homedir();
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}).trim();
|
|
44
|
-
}
|
|
45
|
-
catch { }
|
|
46
|
-
scope = `@you:${email || currentAuthor() || 'unknown'}`;
|
|
47
|
-
you.set(folder, scope);
|
|
34
|
+
function gitEmail(folder) {
|
|
35
|
+
try {
|
|
36
|
+
return execFileSync('git', ['config', '--get', 'user.email'], {
|
|
37
|
+
cwd: folder,
|
|
38
|
+
encoding: 'utf8',
|
|
39
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
40
|
+
}).trim();
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return '';
|
|
48
44
|
}
|
|
45
|
+
}
|
|
46
|
+
export function youScope(project, now = Date.now) {
|
|
47
|
+
const folder = folderOf(project);
|
|
48
|
+
const cached = you.get(folder);
|
|
49
|
+
if (cached && now() - cached.at < SCOPE_TTL_MS)
|
|
50
|
+
return cached.scope;
|
|
51
|
+
const scope = `@you:${gitEmail(folder) || currentAuthor() || 'unknown'}`;
|
|
52
|
+
you.set(folder, { at: now(), scope });
|
|
49
53
|
return scope;
|
|
50
54
|
}
|
|
51
55
|
export function machineTag() {
|
|
@@ -82,7 +86,7 @@ export function factLabel(obs) {
|
|
|
82
86
|
const type = factType(obs);
|
|
83
87
|
if (type)
|
|
84
88
|
return LABELS[type];
|
|
85
|
-
return obs.sessionId === 'manual' ? LABELS.rule : null;
|
|
89
|
+
return obs.sessionId === 'manual' || obs.kind === 'preference' ? LABELS.rule : null;
|
|
86
90
|
}
|
|
87
91
|
export function factToObservation(fact, project) {
|
|
88
92
|
const owner = scopeProject(fact.scope, project);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface StartFacts {
|
|
2
|
+
block: string;
|
|
3
|
+
ids: Set<string>;
|
|
4
|
+
}
|
|
5
|
+
export interface Entry {
|
|
6
|
+
texts: string[];
|
|
7
|
+
id: string | null;
|
|
8
|
+
line: (texts: string[]) => string;
|
|
9
|
+
}
|
|
10
|
+
export type Section = [string, Entry[]];
|
|
11
|
+
export declare function entry(line: string, id: string | null, text: string): Entry;
|
|
12
|
+
export declare function summaryEntry(parts: string[]): Entry;
|
|
13
|
+
export declare function render(sections: Section[], maxChars: number, earlier?: ReadonlySet<string>): StartFacts | null;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const SUMMARY_JOIN = '; ';
|
|
2
|
+
export function entry(line, id, text) {
|
|
3
|
+
return { texts: [text], id, line: () => line };
|
|
4
|
+
}
|
|
5
|
+
export function summaryEntry(parts) {
|
|
6
|
+
return { texts: parts, id: null, line: (kept) => `- Summary: ${kept.join(SUMMARY_JOIN)}` };
|
|
7
|
+
}
|
|
8
|
+
const textKey = (text) => text.toLowerCase().replace(/\s+/g, ' ').trim();
|
|
9
|
+
function unseen(texts, seen) {
|
|
10
|
+
const keys = texts.map(textKey);
|
|
11
|
+
return texts.filter((_, index) => {
|
|
12
|
+
const key = keys[index] ?? '';
|
|
13
|
+
return !seen.has(key) && keys.indexOf(key) === index;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export function render(sections, maxChars, earlier = new Set()) {
|
|
17
|
+
const seen = new Set([...earlier].map(textKey));
|
|
18
|
+
const ids = new Set();
|
|
19
|
+
const blocks = [];
|
|
20
|
+
let budget = maxChars;
|
|
21
|
+
for (const [heading, entries] of sections) {
|
|
22
|
+
const lines = [];
|
|
23
|
+
for (const item of entries) {
|
|
24
|
+
const kept = unseen(item.texts, seen);
|
|
25
|
+
if (kept.length > 0) {
|
|
26
|
+
const line = item.line(kept);
|
|
27
|
+
if (line.length > budget)
|
|
28
|
+
break;
|
|
29
|
+
budget -= line.length + 1;
|
|
30
|
+
for (const text of kept)
|
|
31
|
+
seen.add(textKey(text));
|
|
32
|
+
lines.push(line);
|
|
33
|
+
}
|
|
34
|
+
if (item.id)
|
|
35
|
+
ids.add(item.id);
|
|
36
|
+
}
|
|
37
|
+
if (lines.length > 0)
|
|
38
|
+
blocks.push([heading, ...lines].join('\n'));
|
|
39
|
+
}
|
|
40
|
+
if (blocks.length === 0)
|
|
41
|
+
return null;
|
|
42
|
+
return { block: ['<memory>', blocks.join('\n\n'), '</memory>'].join('\n'), ids };
|
|
43
|
+
}
|
package/dist/facts/start.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import type { RecallContext } from '../context.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string>, current?: string): Promise<StartFacts | null>;
|
|
7
|
-
export declare function recentWorkBlock(ctx: RecallContext, project: string, current?: string, hidden?: ReadonlySet<string>): Promise<StartFacts | null>;
|
|
2
|
+
import type { StartFacts } from './start-render.js';
|
|
3
|
+
export type { StartFacts } from './start-render.js';
|
|
4
|
+
export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string>, current?: string, earlier?: ReadonlySet<string>): Promise<StartFacts | null>;
|
package/dist/facts/start.js
CHANGED
|
@@ -4,14 +4,16 @@ import { activeRequests } from '../capture/active.js';
|
|
|
4
4
|
import { handoffBodyLines, handoffHeading, newestHandoff, passedNote } from './handoff.js';
|
|
5
5
|
import { latestRequestAt, recentChats, turnLine } from './last-chat.js';
|
|
6
6
|
import { MANUAL_SESSION, factLine, seenByClaude } from './render.js';
|
|
7
|
+
import { entry, render, summaryEntry } from './start-render.js';
|
|
7
8
|
import { formatDay, formatDayTime } from '../util/day.js';
|
|
8
9
|
import { toShortId } from '../util/shortid.js';
|
|
9
10
|
const ABOUT_YOU = 6;
|
|
10
11
|
const THIS_PROJECT = 10;
|
|
11
12
|
const TODOS = 4;
|
|
12
13
|
const UNCOMMITTED = 3;
|
|
14
|
+
const CHAT_RULES = 8;
|
|
13
15
|
const ORDER = ['rule', 'decision', 'deadend', 'fact'];
|
|
14
|
-
export async function startFacts(ctx, project, hidden = new Set(), current) {
|
|
16
|
+
export async function startFacts(ctx, project, hidden = new Set(), current, earlier = new Set()) {
|
|
15
17
|
const visible = (obs) => obs.status !== 'replaced' && !hidden.has(obs.id) && !seenByClaude(obs, project);
|
|
16
18
|
const list = async (owner, sessionId, limit) => (await ctx.store.list({ project: owner, sessionId, newest: true, limit, bodies: false })).filter(visible);
|
|
17
19
|
const you = onePerKey(await list(youScope(project), FACT_SESSION, 50));
|
|
@@ -19,21 +21,21 @@ export async function startFacts(ctx, project, hidden = new Set(), current) {
|
|
|
19
21
|
const handoff = await newestHandoff(ctx, project);
|
|
20
22
|
const shownHandoff = handoff && visible(handoff) ? handoff : null;
|
|
21
23
|
const manual = (await list(project, MANUAL_SESSION, 20)).filter((obs) => obs.id !== handoff?.id);
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
+
const chatRules = await chatRulesFor(ctx, project, [...facts, ...manual], visible);
|
|
24
25
|
const rank = (obs) => {
|
|
25
26
|
const index = ORDER.indexOf(factType(obs) ?? 'rule');
|
|
26
27
|
return index === -1 ? ORDER.length : index;
|
|
27
28
|
};
|
|
28
|
-
const known = [...manual, ...facts.filter((obs) => factType(obs) !== 'todo')].sort((a, b) => rank(a) - rank(b) || b.createdAt - a.createdAt);
|
|
29
|
+
const known = [...manual, ...chatRules, ...facts.filter((obs) => factType(obs) !== 'todo')].sort((a, b) => rank(a) - rank(b) || b.createdAt - a.createdAt);
|
|
29
30
|
const todos = facts.filter((obs) => factType(obs) === 'todo').slice(0, TODOS);
|
|
30
31
|
const uncommitted = (await openWork(ctx.store, project))
|
|
31
32
|
.filter((obs) => !hidden.has(obs.id))
|
|
32
33
|
.slice(0, UNCOMMITTED);
|
|
33
34
|
const recent = await recentWork(ctx, project, current);
|
|
35
|
+
const factEntry = (obs) => entry(factLine(obs), obs.id, obs.title);
|
|
34
36
|
const sections = [
|
|
35
|
-
['About you:', you.slice(0, ABOUT_YOU).map(
|
|
36
|
-
['This project:', known.slice(0, THIS_PROJECT).map(
|
|
37
|
+
['About you:', you.slice(0, ABOUT_YOU).map(factEntry)],
|
|
38
|
+
['This project:', known.slice(0, THIS_PROJECT).map(factEntry)],
|
|
37
39
|
...recentSections(recent, hidden),
|
|
38
40
|
[
|
|
39
41
|
shownHandoff
|
|
@@ -44,12 +46,25 @@ export async function startFacts(ctx, project, hidden = new Set(), current) {
|
|
|
44
46
|
[
|
|
45
47
|
'Where you stopped:',
|
|
46
48
|
[
|
|
47
|
-
...todos.map(
|
|
48
|
-
...uncommitted.map((obs) =>
|
|
49
|
+
...todos.map(factEntry),
|
|
50
|
+
...uncommitted.map((obs) => entry(`- Not committed: ${obs.title}`, obs.id, obs.title)),
|
|
49
51
|
],
|
|
50
52
|
],
|
|
51
53
|
];
|
|
52
|
-
return render(sections, ctx.config.inject.maxChars);
|
|
54
|
+
return render(sections, ctx.config.inject.maxChars, earlier);
|
|
55
|
+
}
|
|
56
|
+
async function chatRulesFor(ctx, project, rulesAndFacts, visible) {
|
|
57
|
+
if (rulesAndFacts.some((obs) => obs.kind === 'preference'))
|
|
58
|
+
return [];
|
|
59
|
+
const rows = await ctx.store.list({
|
|
60
|
+
project,
|
|
61
|
+
kind: 'preference',
|
|
62
|
+
excludeSessions: [FACT_SESSION, MANUAL_SESSION],
|
|
63
|
+
newest: true,
|
|
64
|
+
limit: CHAT_RULES,
|
|
65
|
+
bodies: false,
|
|
66
|
+
});
|
|
67
|
+
return rows.filter(visible);
|
|
53
68
|
}
|
|
54
69
|
async function recentWork(ctx, project, current) {
|
|
55
70
|
const unfinished = activeRequests(await ctx.store.projectScope(project), current);
|
|
@@ -62,50 +77,21 @@ function recentSections(recent, hidden) {
|
|
|
62
77
|
...recent.chats.map((chat, index) => chatSection(chat, index, hidden)),
|
|
63
78
|
];
|
|
64
79
|
}
|
|
65
|
-
|
|
66
|
-
const sections = recentSections(await recentWork(ctx, project, current), hidden);
|
|
67
|
-
return sections.some(([, entries]) => entries.length > 0)
|
|
68
|
-
? render(sections, ctx.config.inject.maxChars)
|
|
69
|
-
: null;
|
|
70
|
-
}
|
|
71
|
-
function render(sections, maxChars) {
|
|
72
|
-
const lines = ['<memory>'];
|
|
73
|
-
const ids = new Set();
|
|
74
|
-
let budget = maxChars;
|
|
75
|
-
for (const [heading, entries] of sections) {
|
|
76
|
-
if (entries.length === 0)
|
|
77
|
-
continue;
|
|
78
|
-
if (lines.length > 1)
|
|
79
|
-
lines.push('');
|
|
80
|
-
lines.push(heading);
|
|
81
|
-
for (const [line, id] of entries) {
|
|
82
|
-
if (line.length > budget)
|
|
83
|
-
break;
|
|
84
|
-
budget -= line.length + 1;
|
|
85
|
-
lines.push(line);
|
|
86
|
-
if (id)
|
|
87
|
-
ids.add(id);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
lines.push('</memory>');
|
|
91
|
-
return { block: lines.join('\n'), ids };
|
|
92
|
-
}
|
|
93
|
-
function unfinishedSection(entry) {
|
|
80
|
+
function unfinishedSection(item) {
|
|
94
81
|
return [
|
|
95
|
-
`Not finished yet in another chat (${formatDayTime(
|
|
96
|
-
[
|
|
82
|
+
`Not finished yet in another chat (${formatDayTime(item.askedAt)}):`,
|
|
83
|
+
[entry(`- asked "${item.request}"`, null, item.request)],
|
|
97
84
|
];
|
|
98
85
|
}
|
|
99
86
|
function chatSection(chat, index, hidden) {
|
|
100
87
|
const heading = `${index === 0 ? 'Last chat' : 'Chat before'} (${formatDay(chat.at)}):`;
|
|
101
88
|
const turns = chat.turns
|
|
102
89
|
.filter((turn) => !hidden.has(turn.id))
|
|
103
|
-
.map((turn) =>
|
|
104
|
-
return [heading, chat.summary ? [
|
|
90
|
+
.map((turn) => entry(`- ${turnLine(turn)} (${toShortId(turn.id)})`, turn.id, turn.title));
|
|
91
|
+
return [heading, chat.summary.length > 0 ? [summaryEntry(chat.summary), ...turns] : turns];
|
|
105
92
|
}
|
|
106
93
|
function handoffEntries(handoff) {
|
|
107
|
-
return handoffBodyLines(handoff).map((line, index) =>
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
]);
|
|
94
|
+
return handoffBodyLines(handoff).map((line, index) => index === 0
|
|
95
|
+
? entry(`${line} (${toShortId(handoff.id)})`, handoff.id, line)
|
|
96
|
+
: entry(line, null, line));
|
|
111
97
|
}
|
package/dist/graph/grammars.js
CHANGED
|
@@ -2,11 +2,12 @@ import { statSync } from 'node:fs';
|
|
|
2
2
|
import { createRequire } from 'node:module';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { CONFIG_DIR } from '../config/dir.js';
|
|
5
|
+
import { hookEnv } from '../util/env.js';
|
|
5
6
|
export const GRAMMARS = ['go', 'java', 'kotlin', 'python', 'ruby', 'rust'];
|
|
6
7
|
export const PLATFORM = `${process.platform}-${process.arch}`;
|
|
7
8
|
export const PLATFORM_PACKAGE = `claude-db-grammars-${PLATFORM}`;
|
|
8
9
|
const loaded = new Map();
|
|
9
|
-
export const grammarHome = () =>
|
|
10
|
+
export const grammarHome = () => hookEnv()['CLAUDE_DB_GRAMMARS'] ?? join(CONFIG_DIR, 'grammars');
|
|
10
11
|
const places = () => [
|
|
11
12
|
createRequire(import.meta.url),
|
|
12
13
|
createRequire(join(grammarHome(), 'package.json')),
|
package/dist/graph/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { LANGUAGES, languageFor, languageNames } from './languages/index.js';
|
|
2
2
|
export { SCAN_VERSION, currentHashes, hashOf, saveScan, scanRepository } from './scan/index.js';
|
|
3
|
-
export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, } from './query/index.js';
|
|
3
|
+
export { NO_GRAPH, NO_GRAPH_FOR_PATH, addText, answerQuery, formatGraph, formatText, hasGraph, nearest, queryGraph, refreshGraph, suggestFor, usagesAnswer, } from './query/index.js';
|