claude-db 0.12.0 → 0.12.2
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 +18 -0
- package/dist/capture/active.d.ts +12 -0
- package/dist/capture/active.js +74 -0
- package/dist/capture/flush.d.ts +1 -1
- package/dist/capture/flush.js +12 -2
- package/dist/capture/git.d.ts +1 -0
- package/dist/capture/git.js +3 -2
- package/dist/capture/transcript.d.ts +1 -0
- package/dist/capture/transcript.js +3 -2
- package/dist/capture/turn-extractor.d.ts +3 -0
- package/dist/capture/turn-extractor.js +12 -2
- package/dist/catchup/gather.d.ts +9 -4
- package/dist/catchup/gather.js +22 -15
- package/dist/catchup/render.js +17 -8
- package/dist/cli/commands/catchup.js +2 -1
- package/dist/cli/commands/pick.js +4 -2
- package/dist/cli/install.js +1 -0
- package/dist/facts/handoff.d.ts +2 -0
- package/dist/facts/handoff.js +7 -0
- package/dist/facts/last-chat.d.ts +13 -0
- package/dist/facts/last-chat.js +45 -0
- package/dist/facts/start.d.ts +2 -1
- package/dist/facts/start.js +44 -5
- package/dist/hooks/detached.d.ts +1 -0
- package/dist/hooks/detached.js +1 -0
- package/dist/hooks/payload.d.ts +5 -0
- package/dist/hooks/relevance.js +1 -5
- package/dist/hooks/session-end.js +6 -0
- package/dist/hooks/session-start.js +9 -6
- package/dist/hooks/turn-end.d.ts +2 -0
- package/dist/hooks/turn-end.js +26 -0
- package/dist/hooks/turn-job.d.ts +10 -0
- package/dist/hooks/turn-job.js +30 -0
- package/dist/hooks/turn-save.d.ts +2 -0
- package/dist/hooks/turn-save.js +25 -0
- package/dist/hooks/user-prompt.js +2 -0
- package/dist/pick/pending.js +7 -37
- package/dist/pick/run.d.ts +6 -0
- package/dist/pick/run.js +8 -1
- package/dist/store/mongo/filters.d.ts +1 -0
- package/dist/store/mongo/filters.js +9 -3
- package/dist/store/mongo/observations.js +2 -3
- package/dist/store/postgres/filters.d.ts +1 -0
- package/dist/store/postgres/filters.js +9 -4
- package/dist/store/postgres/observations.js +4 -1
- package/dist/store/sqlite/filters.d.ts +1 -0
- package/dist/store/sqlite/filters.js +9 -4
- package/dist/store/sqlite/observations.js +4 -1
- package/dist/store/sqlite/search.js +4 -5
- package/dist/types.d.ts +1 -0
- package/dist/util/claude-cli.d.ts +1 -0
- package/dist/util/claude-cli.js +15 -3
- package/dist/util/day.d.ts +1 -0
- package/dist/util/day.js +4 -0
- package/dist/util/json-file.d.ts +4 -0
- package/dist/util/json-file.js +37 -0
- package/docs/how-it-works.md +6 -1
- package/package.json +6 -6
- package/skills/catchup/SKILL.md +6 -2
package/dist/hooks/relevance.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { redact } from '../capture/redact.js';
|
|
2
|
+
import { askedIn } from '../capture/turn-extractor.js';
|
|
2
3
|
import { meaningfulTokens } from '../search/stopwords.js';
|
|
3
4
|
import { formatDay } from '../util/day.js';
|
|
4
5
|
import { toShortId } from '../util/shortid.js';
|
|
@@ -46,11 +47,6 @@ export function renderPromptContext(entries, maxChars, expanded = [], expandMaxC
|
|
|
46
47
|
return null;
|
|
47
48
|
return ['<memory>', ...lines, '</memory>'].join('\n');
|
|
48
49
|
}
|
|
49
|
-
const ASKED_CHARS = 120;
|
|
50
|
-
function askedIn(body) {
|
|
51
|
-
const asked = /^Asked: (.*)$/m.exec(body)?.[1]?.replace(/\s+/g, ' ').trim() ?? '';
|
|
52
|
-
return asked.length > ASKED_CHARS ? `${asked.slice(0, ASKED_CHARS)}…` : asked;
|
|
53
|
-
}
|
|
54
50
|
export function renderPicked(picks, maxChars, shown = [], now = Date.now()) {
|
|
55
51
|
const lines = [];
|
|
56
52
|
let budget = maxChars;
|
|
@@ -11,6 +11,8 @@ import { resolveProject } from '../util/project.js';
|
|
|
11
11
|
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
12
12
|
import { distillInBackground } from './background.js';
|
|
13
13
|
import { clearPick, sweepPicks } from '../pick/pending.js';
|
|
14
|
+
import { clearActive, sweepActive } from '../capture/active.js';
|
|
15
|
+
import { dropTurnSave, sweepTurnSaves } from './turn-job.js';
|
|
14
16
|
silenceSqliteWarning();
|
|
15
17
|
await runHook(async () => {
|
|
16
18
|
if (capturingDisabled(loadConfig().capture.scripted))
|
|
@@ -42,6 +44,10 @@ await runHook(async () => {
|
|
|
42
44
|
clearCursor(sessionId);
|
|
43
45
|
clearPick(sessionId);
|
|
44
46
|
sweepPicks();
|
|
47
|
+
dropTurnSave(sessionId);
|
|
48
|
+
sweepTurnSaves();
|
|
49
|
+
clearActive(sessionId);
|
|
50
|
+
sweepActive();
|
|
45
51
|
if (ctx.config.distill.enabled)
|
|
46
52
|
distillInBackground(project, sessionId);
|
|
47
53
|
if (ctx.config.updates !== 'off' && isDue(readState())) {
|
|
@@ -5,7 +5,7 @@ import { dirname, join, resolve } from 'node:path';
|
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { createContext } from '../context.js';
|
|
7
7
|
import { distillNotice } from '../facts/notice.js';
|
|
8
|
-
import { startFacts } from '../facts/start.js';
|
|
8
|
+
import { recentWorkBlock, startFacts } from '../facts/start.js';
|
|
9
9
|
import { startBackgroundWork } from './background.js';
|
|
10
10
|
import { refreshGraphInBackground } from './graph-refresh.js';
|
|
11
11
|
import { recoverAfterCompact } from './compact.js';
|
|
@@ -61,12 +61,15 @@ await runHook(async () => {
|
|
|
61
61
|
? await recoverAfterCompact(ctx, project, payload.session_id, payload.transcript_path)
|
|
62
62
|
: null;
|
|
63
63
|
const restored = earlier?.ids ?? new Set();
|
|
64
|
-
const facts = await startFacts(ctx, project, restored);
|
|
64
|
+
const facts = await startFacts(ctx, project, restored, payload.session_id);
|
|
65
|
+
const recent = facts ? null : await recentWorkBlock(ctx, project, payload.session_id, restored);
|
|
65
66
|
const legacy = facts ? null : await legacyBlock(ctx, project, restored);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
const shown = [...(facts?.ids ?? []), ...(recent?.ids ?? [])];
|
|
68
|
+
if (shown.length > 0 && payload.session_id)
|
|
69
|
+
markShown(payload.session_id, shown);
|
|
70
|
+
const fallback = legacy?.hasMemory || !recent ? legacy?.text : undefined;
|
|
71
|
+
const parts = [earlier?.block, facts?.block ?? fallback, recent?.block].filter((part) => Boolean(part));
|
|
72
|
+
if ((facts || recent || legacy?.hasMemory) && mcpRegistered(project)) {
|
|
70
73
|
parts.push("Search this project's full history with the memory MCP tools before " +
|
|
71
74
|
'asking the user to re-explain prior decisions.');
|
|
72
75
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { activeToken } from '../capture/active.js';
|
|
3
|
+
import { loadConfig } from '../config/index.js';
|
|
4
|
+
import { resolveProject } from '../util/project.js';
|
|
5
|
+
import { TURN_WORKER, runDetached } from './detached.js';
|
|
6
|
+
import { capturingDisabled, readPayload, runHook } from './payload.js';
|
|
7
|
+
import { queueTurnSave } from './turn-job.js';
|
|
8
|
+
await runHook(async () => {
|
|
9
|
+
if (capturingDisabled(loadConfig().capture.scripted))
|
|
10
|
+
return;
|
|
11
|
+
const payload = await readPayload();
|
|
12
|
+
const sessionId = payload.session_id;
|
|
13
|
+
if (!sessionId)
|
|
14
|
+
return;
|
|
15
|
+
const project = resolveProject(payload.cwd);
|
|
16
|
+
const tasks = Array.isArray(payload.background_tasks) ? payload.background_tasks : [];
|
|
17
|
+
const crons = Array.isArray(payload.session_crons) ? payload.session_crons : [];
|
|
18
|
+
const stillWorking = crons.length > 0 || tasks.some((task) => task?.status === 'running');
|
|
19
|
+
queueTurnSave(sessionId, {
|
|
20
|
+
project,
|
|
21
|
+
...(payload.transcript_path ? { transcriptPath: payload.transcript_path } : {}),
|
|
22
|
+
finalReply: payload.last_assistant_message ?? '',
|
|
23
|
+
activeToken: stillWorking ? null : activeToken(sessionId),
|
|
24
|
+
});
|
|
25
|
+
runDetached(TURN_WORKER, [sessionId], project, () => { });
|
|
26
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface TurnJob {
|
|
2
|
+
project: string;
|
|
3
|
+
transcriptPath?: string;
|
|
4
|
+
finalReply: string;
|
|
5
|
+
activeToken: string | null;
|
|
6
|
+
}
|
|
7
|
+
export declare function queueTurnSave(sessionId: string, job: TurnJob): void;
|
|
8
|
+
export declare function takeTurnSave(sessionId: string): TurnJob | null;
|
|
9
|
+
export declare function dropTurnSave(sessionId: string): void;
|
|
10
|
+
export declare function sweepTurnSaves(now?: number): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { rmSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { CONFIG_DIR } from '../config/dir.js';
|
|
4
|
+
import { readJson, safeName, sweepOlderThan, writeJsonAtomic } from '../util/json-file.js';
|
|
5
|
+
const KEPT_FOR_MS = 24 * 60 * 60 * 1000;
|
|
6
|
+
function folder() {
|
|
7
|
+
return join(CONFIG_DIR, 'turns');
|
|
8
|
+
}
|
|
9
|
+
function fileFor(sessionId) {
|
|
10
|
+
return join(folder(), `${safeName(sessionId)}.json`);
|
|
11
|
+
}
|
|
12
|
+
export function queueTurnSave(sessionId, job) {
|
|
13
|
+
writeJsonAtomic(fileFor(sessionId), job);
|
|
14
|
+
}
|
|
15
|
+
export function takeTurnSave(sessionId) {
|
|
16
|
+
const file = fileFor(sessionId);
|
|
17
|
+
const job = readJson(file);
|
|
18
|
+
rmSync(file, { force: true });
|
|
19
|
+
const valid = typeof job?.project === 'string' &&
|
|
20
|
+
typeof job.finalReply === 'string' &&
|
|
21
|
+
(job.transcriptPath === undefined || typeof job.transcriptPath === 'string') &&
|
|
22
|
+
(job.activeToken === null || typeof job.activeToken === 'string');
|
|
23
|
+
return valid ? job : null;
|
|
24
|
+
}
|
|
25
|
+
export function dropTurnSave(sessionId) {
|
|
26
|
+
rmSync(fileFor(sessionId), { force: true });
|
|
27
|
+
}
|
|
28
|
+
export function sweepTurnSaves(now = Date.now()) {
|
|
29
|
+
sweepOlderThan(folder(), KEPT_FOR_MS, now);
|
|
30
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { clearActive } from '../capture/active.js';
|
|
3
|
+
import { flushSession } from '../capture/index.js';
|
|
4
|
+
import { createContext } from '../context.js';
|
|
5
|
+
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
6
|
+
import { runHook } from './payload.js';
|
|
7
|
+
import { takeTurnSave } from './turn-job.js';
|
|
8
|
+
silenceSqliteWarning();
|
|
9
|
+
await runHook(async () => {
|
|
10
|
+
const [sessionId] = process.argv.slice(2);
|
|
11
|
+
if (!sessionId)
|
|
12
|
+
return;
|
|
13
|
+
const job = takeTurnSave(sessionId);
|
|
14
|
+
if (!job)
|
|
15
|
+
return;
|
|
16
|
+
const ctx = await createContext();
|
|
17
|
+
try {
|
|
18
|
+
await flushSession(ctx, sessionId, job.project, job.transcriptPath, false, job.finalReply);
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
await ctx.close();
|
|
22
|
+
}
|
|
23
|
+
if (job.activeToken)
|
|
24
|
+
clearActive(sessionId, job.activeToken);
|
|
25
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { recordActive } from '../capture/active.js';
|
|
2
3
|
import { flushSession, redact } from '../capture/index.js';
|
|
3
4
|
import { createContext } from '../context.js';
|
|
4
5
|
import { capturingDisabled, emitContext, readPayload, runHook } from './payload.js';
|
|
@@ -22,6 +23,7 @@ await runHook(async () => {
|
|
|
22
23
|
const ctx = await createContext();
|
|
23
24
|
try {
|
|
24
25
|
await ctx.store.upsertSession({ id: sessionId, project, startedAt: Date.now() });
|
|
26
|
+
recordActive(sessionId, project, payload.prompt ?? '');
|
|
25
27
|
const { lastReply } = await flushSession(ctx, sessionId, project, payload.transcript_path);
|
|
26
28
|
const candidates = await promptCandidates(ctx, {
|
|
27
29
|
prompt: payload.prompt ?? '',
|
package/dist/pick/pending.js
CHANGED
|
@@ -1,32 +1,16 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import {
|
|
2
|
+
import { renameSync, rmSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { CONFIG_DIR } from '../config/dir.js';
|
|
5
|
+
import { readJson, safeName, sweepOlderThan, writeJsonAtomic } from '../util/json-file.js';
|
|
5
6
|
const STALE_MS = 10 * 60 * 1000;
|
|
6
7
|
const ORPHAN_MS = 24 * 60 * 60 * 1000;
|
|
7
8
|
const TOKEN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
8
9
|
function folder() {
|
|
9
10
|
return join(CONFIG_DIR, 'pick', 'pending');
|
|
10
11
|
}
|
|
11
|
-
function safe(sessionId) {
|
|
12
|
-
return sessionId.replace(/[^\w-]/g, '_');
|
|
13
|
-
}
|
|
14
12
|
function path(sessionId, suffix) {
|
|
15
|
-
return join(folder(), `${
|
|
16
|
-
}
|
|
17
|
-
function readJson(file) {
|
|
18
|
-
try {
|
|
19
|
-
return JSON.parse(readFileSync(file, 'utf8'));
|
|
20
|
-
}
|
|
21
|
-
catch {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function writeAtomic(file, value) {
|
|
26
|
-
mkdirSync(folder(), { recursive: true });
|
|
27
|
-
const temp = `${file}.${randomUUID()}.tmp`;
|
|
28
|
-
writeFileSync(temp, JSON.stringify(value), 'utf8');
|
|
29
|
-
renameSync(temp, file);
|
|
13
|
+
return join(folder(), `${safeName(sessionId)}${suffix}`);
|
|
30
14
|
}
|
|
31
15
|
function isToken(value) {
|
|
32
16
|
return typeof value === 'string' && TOKEN.test(value);
|
|
@@ -40,27 +24,13 @@ export function clearPick(sessionId) {
|
|
|
40
24
|
rmSync(path(sessionId, '.json'), { force: true });
|
|
41
25
|
}
|
|
42
26
|
export function sweepPicks(now = Date.now()) {
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
names = readdirSync(folder());
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
for (const name of names) {
|
|
51
|
-
const file = join(folder(), name);
|
|
52
|
-
try {
|
|
53
|
-
if (now - statSync(file).mtimeMs > ORPHAN_MS)
|
|
54
|
-
rmSync(file, { force: true });
|
|
55
|
-
}
|
|
56
|
-
catch { }
|
|
57
|
-
}
|
|
27
|
+
sweepOlderThan(folder(), ORPHAN_MS, now);
|
|
58
28
|
}
|
|
59
29
|
export function startPick(sessionId, job, now = Date.now()) {
|
|
60
30
|
clearPick(sessionId);
|
|
61
31
|
const token = randomUUID();
|
|
62
|
-
|
|
63
|
-
|
|
32
|
+
writeJsonAtomic(path(sessionId, `.${token}.job.json`), job);
|
|
33
|
+
writeJsonAtomic(path(sessionId, '.json'), { token, startedAt: now });
|
|
64
34
|
return token;
|
|
65
35
|
}
|
|
66
36
|
export function takeJob(sessionId, token) {
|
|
@@ -85,7 +55,7 @@ function current(sessionId, now) {
|
|
|
85
55
|
export function finishPick(sessionId, token, ready, now = Date.now()) {
|
|
86
56
|
if (current(sessionId, now)?.token !== token)
|
|
87
57
|
return false;
|
|
88
|
-
|
|
58
|
+
writeJsonAtomic(path(sessionId, `.${token}.ready.json`), ready);
|
|
89
59
|
return true;
|
|
90
60
|
}
|
|
91
61
|
export function takeReady(sessionId, now = Date.now()) {
|
package/dist/pick/run.d.ts
CHANGED
|
@@ -21,5 +21,11 @@ export interface PickRequest {
|
|
|
21
21
|
export declare function takePick(config: Config, now?: number): boolean;
|
|
22
22
|
export declare function recordPickFailure(reason: string, now?: number): void;
|
|
23
23
|
export declare function recordPickSuccess(now?: number): void;
|
|
24
|
+
interface PickModel {
|
|
25
|
+
model: string;
|
|
26
|
+
fallback?: string;
|
|
27
|
+
}
|
|
28
|
+
export declare function pickModel(config: Config): PickModel;
|
|
24
29
|
export declare function headlessPicker(config: Config): PickRunner;
|
|
25
30
|
export declare function pickMemories(ctx: RecallContext, request: PickRequest, run: PickRunner): Promise<PickOutcome>;
|
|
31
|
+
export {};
|
package/dist/pick/run.js
CHANGED
|
@@ -5,6 +5,7 @@ import { recordFailure, recordSuccess, takeBudget } from '../util/daily-budget.j
|
|
|
5
5
|
import { PICK_SYSTEM_PROMPT, buildPickPrompt, parsePicks } from './prompt.js';
|
|
6
6
|
export const PICK_BUDGET = 'pick';
|
|
7
7
|
const TIMEOUT_MS = 30_000;
|
|
8
|
+
const LATEST_HAIKU = 'claude-haiku-5-5';
|
|
8
9
|
export function takePick(config, now = Date.now()) {
|
|
9
10
|
return config.pick.enabled && takeBudget(PICK_BUDGET, config.pick.dailyLimit, 1, now);
|
|
10
11
|
}
|
|
@@ -14,10 +15,16 @@ export function recordPickFailure(reason, now = Date.now()) {
|
|
|
14
15
|
export function recordPickSuccess(now = Date.now()) {
|
|
15
16
|
recordSuccess(PICK_BUDGET, now);
|
|
16
17
|
}
|
|
18
|
+
export function pickModel(config) {
|
|
19
|
+
const { model } = config.pick;
|
|
20
|
+
return model === 'haiku' ? { model: LATEST_HAIKU, fallback: model } : { model };
|
|
21
|
+
}
|
|
17
22
|
export function headlessPicker(config) {
|
|
18
|
-
|
|
23
|
+
const { model, fallback } = pickModel(config);
|
|
24
|
+
return (prompt) => runHeadlessResult(prompt, model, TIMEOUT_MS, {
|
|
19
25
|
flags: [['--system-prompt', PICK_SYSTEM_PROMPT], ['--tools', ''], ['--strict-mcp-config']],
|
|
20
26
|
env: { MAX_THINKING_TOKENS: '0' },
|
|
27
|
+
...(fallback ? { fallbackModel: fallback } : {}),
|
|
21
28
|
});
|
|
22
29
|
}
|
|
23
30
|
export async function pickMemories(ctx, request, run) {
|
|
@@ -5,4 +5,5 @@ export interface VectorCache {
|
|
|
5
5
|
atlasVectorIndex: boolean | null;
|
|
6
6
|
}
|
|
7
7
|
export declare function scopeFilter(query: SearchQuery): Doc;
|
|
8
|
+
export declare function sessionMatch(only: string | undefined, exclude: string[] | undefined): Doc;
|
|
8
9
|
export declare function visibleFilter(query: SearchQuery): Doc;
|
|
@@ -23,11 +23,17 @@ export function scopeFilter(query) {
|
|
|
23
23
|
}
|
|
24
24
|
return filter;
|
|
25
25
|
}
|
|
26
|
+
export function sessionMatch(only, exclude) {
|
|
27
|
+
const match = {};
|
|
28
|
+
if (only)
|
|
29
|
+
match['$eq'] = only;
|
|
30
|
+
if (exclude && exclude.length > 0)
|
|
31
|
+
match['$nin'] = exclude;
|
|
32
|
+
return Object.keys(match).length === 0 ? {} : { sessionId: match };
|
|
33
|
+
}
|
|
26
34
|
export function visibleFilter(query) {
|
|
27
35
|
return {
|
|
28
36
|
status: { $ne: 'replaced' },
|
|
29
|
-
...(
|
|
30
|
-
? { sessionId: { $nin: query.excludeSessions } }
|
|
31
|
-
: {}),
|
|
37
|
+
...sessionMatch(undefined, query.excludeSessions),
|
|
32
38
|
};
|
|
33
39
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isWholeScope } from '../adapter.js';
|
|
2
2
|
import { toDoc, toObservation } from './docs.js';
|
|
3
|
-
import { projectMatch } from './filters.js';
|
|
3
|
+
import { projectMatch, sessionMatch } from './filters.js';
|
|
4
4
|
import { escapeRegex } from './helpers.js';
|
|
5
5
|
import { partitionIds } from '../../util/shortid.js';
|
|
6
6
|
import { noProjects } from '../project-scope.js';
|
|
@@ -65,8 +65,7 @@ export async function list(observations, sessions, symbols, edges, scanned, filt
|
|
|
65
65
|
return [];
|
|
66
66
|
const query = {};
|
|
67
67
|
Object.assign(query, projectMatch(filter.project));
|
|
68
|
-
|
|
69
|
-
query['sessionId'] = filter.sessionId;
|
|
68
|
+
Object.assign(query, sessionMatch(filter.sessionId, filter.excludeSessions));
|
|
70
69
|
if (filter.kind)
|
|
71
70
|
query['kind'] = filter.kind;
|
|
72
71
|
if (filter.status)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { ProjectFilter, SearchQuery } from '../../types.js';
|
|
2
2
|
export declare function projectClause(filter: ProjectFilter | undefined, values: unknown[], column?: string): string | null;
|
|
3
|
+
export declare function excludeSessionsClause(sessions: string[] | undefined, values: unknown[]): string | null;
|
|
3
4
|
export declare function appendScope(query: SearchQuery, conditions: string[], values: unknown[]): void;
|
|
@@ -6,6 +6,12 @@ export function projectClause(filter, values, column = 'project') {
|
|
|
6
6
|
values.push(projects);
|
|
7
7
|
return `${column} = ANY($${values.length}::text[])`;
|
|
8
8
|
}
|
|
9
|
+
export function excludeSessionsClause(sessions, values) {
|
|
10
|
+
if (!sessions || sessions.length === 0)
|
|
11
|
+
return null;
|
|
12
|
+
values.push(sessions);
|
|
13
|
+
return `session_id <> ALL($${values.length}::text[])`;
|
|
14
|
+
}
|
|
9
15
|
export function appendScope(query, conditions, values) {
|
|
10
16
|
conditions.push("status <> 'replaced'");
|
|
11
17
|
const project = projectClause(query.project, values);
|
|
@@ -27,8 +33,7 @@ export function appendScope(query, conditions, values) {
|
|
|
27
33
|
values.push(query.until);
|
|
28
34
|
conditions.push(`created_at <= $${values.length}`);
|
|
29
35
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
conditions.push(
|
|
33
|
-
}
|
|
36
|
+
const excluded = excludeSessionsClause(query.excludeSessions, values);
|
|
37
|
+
if (excluded)
|
|
38
|
+
conditions.push(excluded);
|
|
34
39
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { foreignNames, isWholeScope } from '../adapter.js';
|
|
2
2
|
import { partitionIds } from '../../util/shortid.js';
|
|
3
|
-
import { projectClause } from './filters.js';
|
|
3
|
+
import { excludeSessionsClause, projectClause } from './filters.js';
|
|
4
4
|
import { toIndexEntry, toObservation } from './rows.js';
|
|
5
5
|
import { noProjects } from '../project-scope.js';
|
|
6
6
|
export async function getObservations(pool, ids) {
|
|
@@ -88,6 +88,9 @@ export async function list(pool, filter) {
|
|
|
88
88
|
values.push(filter.sessionId);
|
|
89
89
|
conditions.push(`session_id = $${values.length}`);
|
|
90
90
|
}
|
|
91
|
+
const excluded = excludeSessionsClause(filter.excludeSessions, values);
|
|
92
|
+
if (excluded)
|
|
93
|
+
conditions.push(excluded);
|
|
91
94
|
if (filter.kind) {
|
|
92
95
|
values.push(filter.kind);
|
|
93
96
|
conditions.push(`kind = $${values.length}`);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ProjectFilter, RemoveFilter, SearchQuery } from '../../types.js';
|
|
2
2
|
export declare function projectClause(column: string, filter: ProjectFilter | undefined, params: unknown[]): string | null;
|
|
3
|
+
export declare function excludeSessionsClause(column: string, sessions: string[] | undefined, params: unknown[]): string | null;
|
|
3
4
|
export declare function removeWhere(filter: RemoveFilter): {
|
|
4
5
|
where: string;
|
|
5
6
|
params: unknown[];
|
|
@@ -12,6 +12,12 @@ export function projectClause(column, filter, params) {
|
|
|
12
12
|
? `${column} = ?`
|
|
13
13
|
: `${column} IN (${projects.map(() => '?').join(',')})`;
|
|
14
14
|
}
|
|
15
|
+
export function excludeSessionsClause(column, sessions, params) {
|
|
16
|
+
if (!sessions || sessions.length === 0)
|
|
17
|
+
return null;
|
|
18
|
+
params.push(...sessions);
|
|
19
|
+
return `${column} NOT IN (${sessions.map(() => '?').join(',')})`;
|
|
20
|
+
}
|
|
15
21
|
export function removeWhere(filter) {
|
|
16
22
|
const conditions = [];
|
|
17
23
|
const params = [];
|
|
@@ -66,10 +72,9 @@ export function appendScope(query, conditions, params, prefix) {
|
|
|
66
72
|
conditions.push(`${prefix}created_at <= ?`);
|
|
67
73
|
params.push(query.until);
|
|
68
74
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
75
|
+
const excluded = excludeSessionsClause(`${prefix}session_id`, query.excludeSessions, params);
|
|
76
|
+
if (excluded)
|
|
77
|
+
conditions.push(excluded);
|
|
73
78
|
}
|
|
74
79
|
function scopeExpression(filter) {
|
|
75
80
|
const tokens = projectsOf(filter).map(scopeToken);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { foreignNames, isWholeScope } from '../adapter.js';
|
|
2
2
|
import { toObservation } from './rows.js';
|
|
3
|
-
import { projectClause, removeWhere } from './filters.js';
|
|
3
|
+
import { excludeSessionsClause, projectClause, removeWhere } from './filters.js';
|
|
4
4
|
import { partitionIds } from '../../util/shortid.js';
|
|
5
5
|
import { packVector } from '../../util/vector.js';
|
|
6
6
|
import { scopeToken } from '../../util/scope.js';
|
|
@@ -88,6 +88,9 @@ export async function list(db, filter) {
|
|
|
88
88
|
conditions.push('session_id = ?');
|
|
89
89
|
params.push(filter.sessionId);
|
|
90
90
|
}
|
|
91
|
+
const excluded = excludeSessionsClause('session_id', filter.excludeSessions, params);
|
|
92
|
+
if (excluded)
|
|
93
|
+
conditions.push(excluded);
|
|
91
94
|
if (filter.kind) {
|
|
92
95
|
conditions.push('kind = ?');
|
|
93
96
|
params.push(filter.kind);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toBuffer, toIndexEntry } from './rows.js';
|
|
2
|
-
import { TAG_PREDICATE, appendScope, toMatchExpression } from './filters.js';
|
|
2
|
+
import { TAG_PREDICATE, appendScope, excludeSessionsClause, toMatchExpression } from './filters.js';
|
|
3
3
|
import { cosine, unpackVector } from '../../util/vector.js';
|
|
4
4
|
import { noProjects } from '../project-scope.js';
|
|
5
5
|
export async function searchKeyword(db, query) {
|
|
@@ -26,10 +26,9 @@ export async function searchKeyword(db, query) {
|
|
|
26
26
|
conditions.push('o.created_at <= ?');
|
|
27
27
|
params.push(query.until);
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
29
|
+
const excluded = excludeSessionsClause('o.session_id', query.excludeSessions, params);
|
|
30
|
+
if (excluded)
|
|
31
|
+
conditions.push(excluded);
|
|
33
32
|
params.push(query.limit);
|
|
34
33
|
const rows = db
|
|
35
34
|
.prepare(`SELECT o.id, o.kind, o.title, o.project, o.created_at,
|
package/dist/types.d.ts
CHANGED
package/dist/util/claude-cli.js
CHANGED
|
@@ -7,12 +7,19 @@ const OPTIONAL_FLAGS = [
|
|
|
7
7
|
['--no-session-persistence'],
|
|
8
8
|
];
|
|
9
9
|
const UNKNOWN_OPTION = /unknown option '(--[\w-]+)'/i;
|
|
10
|
+
const MODEL_UNAVAILABLE = /issue with the selected model/i;
|
|
10
11
|
export async function runHeadlessResult(prompt, model, timeoutMs, options = {}) {
|
|
11
12
|
let optional = [...OPTIONAL_FLAGS, ...(options.flags ?? [])];
|
|
13
|
+
let current = model;
|
|
12
14
|
for (;;) {
|
|
13
|
-
const result = await call(['-p', prompt, '--model',
|
|
15
|
+
const result = await call(['-p', prompt, '--model', current, ...optional.flat()], timeoutMs, options.env ?? {});
|
|
14
16
|
if (result.ok)
|
|
15
17
|
return { ok: true, stdout: result.stdout };
|
|
18
|
+
const { fallbackModel } = options;
|
|
19
|
+
if (fallbackModel && current !== fallbackModel && MODEL_UNAVAILABLE.test(result.stdout)) {
|
|
20
|
+
current = fallbackModel;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
16
23
|
const rejected = UNKNOWN_OPTION.exec(result.stderr)?.[1];
|
|
17
24
|
const remaining = optional.filter((flags) => flags[0] !== rejected);
|
|
18
25
|
if (remaining.length === optional.length)
|
|
@@ -69,12 +76,17 @@ function call(args, timeoutMs, env) {
|
|
|
69
76
|
return;
|
|
70
77
|
}
|
|
71
78
|
const text = String(stderr);
|
|
72
|
-
done({
|
|
79
|
+
done({
|
|
80
|
+
ok: false,
|
|
81
|
+
stdout: String(stdout),
|
|
82
|
+
stderr: text,
|
|
83
|
+
reason: describeFailure(error, text, timeoutMs),
|
|
84
|
+
});
|
|
73
85
|
});
|
|
74
86
|
child.stdin?.end();
|
|
75
87
|
}
|
|
76
88
|
catch (error) {
|
|
77
|
-
done({ ok: false, stderr: '', reason: describeFailure(error, '', timeoutMs) });
|
|
89
|
+
done({ ok: false, stdout: '', stderr: '', reason: describeFailure(error, '', timeoutMs) });
|
|
78
90
|
}
|
|
79
91
|
});
|
|
80
92
|
}
|
package/dist/util/day.d.ts
CHANGED
package/dist/util/day.js
CHANGED
|
@@ -4,6 +4,10 @@ const OTHER_YEAR = new Intl.DateTimeFormat('en-US', {
|
|
|
4
4
|
day: 'numeric',
|
|
5
5
|
year: 'numeric',
|
|
6
6
|
});
|
|
7
|
+
const CLOCK = new Intl.DateTimeFormat('en-GB', { hour: '2-digit', minute: '2-digit' });
|
|
8
|
+
export function formatDayTime(at, now = Date.now()) {
|
|
9
|
+
return `${formatDay(at, now)}, ${CLOCK.format(new Date(at))}`;
|
|
10
|
+
}
|
|
7
11
|
export function formatDay(at, now = Date.now()) {
|
|
8
12
|
const date = new Date(at);
|
|
9
13
|
const format = date.getFullYear() === new Date(now).getFullYear() ? THIS_YEAR : OTHER_YEAR;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function safeName(id: string): string;
|
|
2
|
+
export declare function readJson<T>(file: string): T | null;
|
|
3
|
+
export declare function writeJsonAtomic(file: string, value: unknown): void;
|
|
4
|
+
export declare function sweepOlderThan(folder: string, maxAgeMs: number, now?: number): void;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
export function safeName(id) {
|
|
5
|
+
return id.replace(/[^\w-]/g, '_');
|
|
6
|
+
}
|
|
7
|
+
export function readJson(file) {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function writeJsonAtomic(file, value) {
|
|
16
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
17
|
+
const temp = `${file}.${randomUUID()}.tmp`;
|
|
18
|
+
writeFileSync(temp, JSON.stringify(value), 'utf8');
|
|
19
|
+
renameSync(temp, file);
|
|
20
|
+
}
|
|
21
|
+
export function sweepOlderThan(folder, maxAgeMs, now = Date.now()) {
|
|
22
|
+
let names;
|
|
23
|
+
try {
|
|
24
|
+
names = readdirSync(folder);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
for (const name of names) {
|
|
30
|
+
const file = join(folder, name);
|
|
31
|
+
try {
|
|
32
|
+
if (now - statSync(file).mtimeMs > maxAgeMs)
|
|
33
|
+
rmSync(file, { force: true });
|
|
34
|
+
}
|
|
35
|
+
catch { }
|
|
36
|
+
}
|
|
37
|
+
}
|
package/docs/how-it-works.md
CHANGED
|
@@ -25,6 +25,10 @@ you send a prompt
|
|
|
25
25
|
Both steps are hooks, so they always run. Nothing depends on Claude deciding to
|
|
26
26
|
look something up.
|
|
27
27
|
|
|
28
|
+
A turn is also saved as soon as Claude finishes the reply, in the background, so
|
|
29
|
+
another chat sees it without waiting for your next prompt. The save at the next
|
|
30
|
+
prompt stays as the backup, for a reply you interrupted.
|
|
31
|
+
|
|
28
32
|
Install also writes a short standing instruction into `CLAUDE.local.md` (or
|
|
29
33
|
`~/.claude/CLAUDE.md` for a machine-wide install), telling Claude to search
|
|
30
34
|
memory before asking you to re-explain something. Hook output is context the
|
|
@@ -36,7 +40,8 @@ for. `claude-db uninstall` takes the block back out.
|
|
|
36
40
|
file or ran a real command. Questions, `grep`, and "ok" are skipped. A busy day
|
|
37
41
|
produces 10 to 20 rows, not hundreds.
|
|
38
42
|
|
|
39
|
-
**What gets injected.**
|
|
43
|
+
**What gets injected.** At startup: facts, the newest 5 requests from other
|
|
44
|
+
chats, any request another chat has not finished, and the handoff note. Above a prompt, at
|
|
40
45
|
most two memories from earlier chats, picked by Claude Haiku:
|
|
41
46
|
|
|
42
47
|
1. Search finds the ten closest memories from other chats. If none shares at
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "claude-db",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.2",
|
|
4
4
|
"description": "Free, open-source memory for Claude Code. Remembers why decisions were made and maps your code. Your own database (SQLite, Postgres or MongoDB), no cloud, no subscription.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -130,10 +130,10 @@
|
|
|
130
130
|
"access": "public"
|
|
131
131
|
},
|
|
132
132
|
"optionalDependencies": {
|
|
133
|
-
"claude-db-grammars-linux-x64": "0.12.
|
|
134
|
-
"claude-db-grammars-linux-arm64": "0.12.
|
|
135
|
-
"claude-db-grammars-darwin-x64": "0.12.
|
|
136
|
-
"claude-db-grammars-darwin-arm64": "0.12.
|
|
137
|
-
"claude-db-grammars-win32-x64": "0.12.
|
|
133
|
+
"claude-db-grammars-linux-x64": "0.12.2",
|
|
134
|
+
"claude-db-grammars-linux-arm64": "0.12.2",
|
|
135
|
+
"claude-db-grammars-darwin-x64": "0.12.2",
|
|
136
|
+
"claude-db-grammars-darwin-arm64": "0.12.2",
|
|
137
|
+
"claude-db-grammars-win32-x64": "0.12.2"
|
|
138
138
|
}
|
|
139
139
|
}
|