claude-db 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +126 -0
- package/README.md +11 -3
- package/dist/capture/flush.js +0 -4
- package/dist/capture/index.d.ts +1 -0
- package/dist/capture/index.js +1 -0
- package/dist/capture/summarize.d.ts +3 -0
- package/dist/capture/summarize.js +32 -0
- package/dist/capture/turn-extractor.js +12 -3
- package/dist/cli/commands/adoption.js +8 -2
- package/dist/cli/commands/doctor.js +39 -0
- package/dist/cli/commands/memory.js +22 -1
- package/dist/cli/commands/setup.js +10 -0
- package/dist/cli/commands/view-page.d.ts +2 -0
- package/dist/cli/commands/view-page.js +151 -0
- package/dist/cli/commands/view.d.ts +23 -0
- package/dist/cli/commands/view.js +99 -0
- package/dist/cli/index.js +4 -0
- package/dist/cli/install.js +9 -3
- package/dist/cli/usage.js +2 -0
- package/dist/config/schema.d.ts +11 -0
- package/dist/config/schema.js +4 -0
- package/dist/graph/languages/basic.d.ts +11 -0
- package/dist/graph/languages/basic.js +156 -0
- package/dist/graph/languages/index.d.ts +1 -0
- package/dist/graph/languages/index.js +7 -4
- package/dist/graph/languages/ruby.d.ts +2 -0
- package/dist/graph/languages/ruby.js +16 -0
- package/dist/graph/languages/rules.d.ts +1 -0
- package/dist/graph/query/suggest.d.ts +0 -1
- package/dist/graph/query/suggest.js +2 -9
- package/dist/graph/scan/extract.d.ts +1 -0
- package/dist/graph/scan/extract.js +43 -2
- package/dist/graph/scan/files.d.ts +1 -1
- package/dist/graph/scan/files.js +3 -2
- package/dist/graph/scan/resolve.js +2 -0
- package/dist/hooks/grep-symbols.d.ts +0 -1
- package/dist/hooks/grep-symbols.js +5 -5
- package/dist/hooks/payload.d.ts +1 -0
- package/dist/hooks/payload.js +5 -0
- package/dist/hooks/relevance.d.ts +4 -0
- package/dist/hooks/relevance.js +10 -0
- package/dist/hooks/session-end.js +15 -3
- package/dist/hooks/session-start.js +9 -2
- package/dist/hooks/user-prompt.js +10 -4
- package/dist/mcp/tools/observations.js +1 -1
- package/dist/store/adapter.d.ts +1 -0
- package/dist/store/mongo/index.d.ts +1 -0
- package/dist/store/mongo/index.js +3 -0
- package/dist/store/mongo/sessions.d.ts +1 -0
- package/dist/store/mongo/sessions.js +4 -0
- package/dist/store/postgres/index.d.ts +1 -0
- package/dist/store/postgres/index.js +3 -0
- package/dist/store/postgres/sessions.d.ts +1 -0
- package/dist/store/postgres/sessions.js +4 -0
- package/dist/store/sqlite/index.d.ts +1 -0
- package/dist/store/sqlite/index.js +3 -0
- package/dist/store/sqlite/sessions.d.ts +1 -0
- package/dist/store/sqlite/sessions.js +6 -0
- package/package.json +5 -3
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createServer } from 'node:http';
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import { writeFileSync } from 'node:fs';
|
|
4
|
+
import { resolve } from 'node:path';
|
|
5
|
+
import { createContext } from '../../context.js';
|
|
6
|
+
import { resolveProject } from '../../util/project.js';
|
|
7
|
+
import { toShortId } from '../../util/shortid.js';
|
|
8
|
+
import { renderPage } from './view-page.js';
|
|
9
|
+
async function collect(ctx, project) {
|
|
10
|
+
const all = (await ctx.store.list({ project, limit: 500 })).sort((a, b) => b.createdAt - a.createdAt);
|
|
11
|
+
const kinds = {};
|
|
12
|
+
for (const obs of all)
|
|
13
|
+
kinds[obs.kind] = (kinds[obs.kind] ?? 0) + 1;
|
|
14
|
+
const rules = all
|
|
15
|
+
.filter((obs) => obs.kind === 'preference')
|
|
16
|
+
.sort((a, b) => {
|
|
17
|
+
const manual = Number(b.sessionId === 'manual') - Number(a.sessionId === 'manual');
|
|
18
|
+
return manual !== 0 ? manual : b.createdAt - a.createdAt;
|
|
19
|
+
})
|
|
20
|
+
.slice(0, 10)
|
|
21
|
+
.map((obs) => ({ id: toShortId(obs.id), when: obs.createdAt, title: obs.title }));
|
|
22
|
+
const sessions = (await ctx.store.recentSessions(project, 8)).map((session) => ({
|
|
23
|
+
when: session.startedAt,
|
|
24
|
+
summary: session.summary ?? '',
|
|
25
|
+
}));
|
|
26
|
+
return {
|
|
27
|
+
project,
|
|
28
|
+
database: ctx.config.database,
|
|
29
|
+
scannedFiles: (await ctx.store.scannedFiles(project)).length,
|
|
30
|
+
kinds,
|
|
31
|
+
rules,
|
|
32
|
+
sessions,
|
|
33
|
+
observations: all.slice(0, 80).map((obs) => ({
|
|
34
|
+
id: toShortId(obs.id),
|
|
35
|
+
kind: obs.kind,
|
|
36
|
+
when: obs.createdAt,
|
|
37
|
+
title: obs.title,
|
|
38
|
+
files: obs.files.length,
|
|
39
|
+
})),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export async function cmdView(args) {
|
|
43
|
+
const project = resolveProject(undefined);
|
|
44
|
+
const ctx = await createContext();
|
|
45
|
+
const exportAt = args.indexOf('--export');
|
|
46
|
+
if (exportAt >= 0) {
|
|
47
|
+
const target = resolve(args[exportAt + 1] ?? 'claude-db-memory.html');
|
|
48
|
+
writeFileSync(target, renderPage(await collect(ctx, project), false));
|
|
49
|
+
await ctx.close();
|
|
50
|
+
console.log(`Wrote snapshot to ${target}`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const server = createServer(async (req, res) => {
|
|
54
|
+
try {
|
|
55
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
56
|
+
if (url.pathname === '/api/data') {
|
|
57
|
+
res.writeHead(200, { 'content-type': 'application/json' });
|
|
58
|
+
res.end(JSON.stringify(await collect(ctx, project)));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (url.pathname === '/api/search') {
|
|
62
|
+
const query = url.searchParams.get('q') ?? '';
|
|
63
|
+
const entries = await ctx.search.search({ text: query, project, limit: 20 });
|
|
64
|
+
res.writeHead(200, { 'content-type': 'application/json' });
|
|
65
|
+
res.end(JSON.stringify(entries.map((entry) => ({
|
|
66
|
+
id: toShortId(entry.id),
|
|
67
|
+
kind: entry.kind,
|
|
68
|
+
when: entry.createdAt,
|
|
69
|
+
title: entry.title,
|
|
70
|
+
}))));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
74
|
+
res.end(renderPage(await collect(ctx, project), true));
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
res.writeHead(500, { 'content-type': 'text/plain' });
|
|
78
|
+
res.end(error instanceof Error ? error.message : String(error));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
server.listen(0, '127.0.0.1', () => {
|
|
82
|
+
const address = server.address();
|
|
83
|
+
const port = typeof address === 'object' && address ? address.port : 0;
|
|
84
|
+
const url = `http://127.0.0.1:${port}`;
|
|
85
|
+
console.log(`Viewing ${project}`);
|
|
86
|
+
console.log(` ${url} (Ctrl+C to stop — nothing keeps running afterwards)`);
|
|
87
|
+
if (process.platform === 'darwin')
|
|
88
|
+
execFile('open', [url], () => { });
|
|
89
|
+
else if (process.platform === 'linux')
|
|
90
|
+
execFile('xdg-open', [url], () => { });
|
|
91
|
+
});
|
|
92
|
+
const stop = async () => {
|
|
93
|
+
server.close();
|
|
94
|
+
await ctx.close();
|
|
95
|
+
process.exit(0);
|
|
96
|
+
};
|
|
97
|
+
process.on('SIGINT', () => void stop());
|
|
98
|
+
process.on('SIGTERM', () => void stop());
|
|
99
|
+
}
|
package/dist/cli/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import { cmdFlush, cmdReset, cmdUpdate } from './commands/maintain.js';
|
|
|
12
12
|
import { cmdScan, cmdUsages } from './commands/graph.js';
|
|
13
13
|
import { cmdSeed } from './commands/seed.js';
|
|
14
14
|
import { cmdAdoption } from './commands/adoption.js';
|
|
15
|
+
import { cmdView } from './commands/view.js';
|
|
15
16
|
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
16
17
|
silenceSqliteWarning();
|
|
17
18
|
const [command, ...args] = process.argv.slice(2);
|
|
@@ -66,6 +67,9 @@ async function run() {
|
|
|
66
67
|
case 'adoption':
|
|
67
68
|
await cmdAdoption();
|
|
68
69
|
break;
|
|
70
|
+
case 'view':
|
|
71
|
+
await cmdView(args);
|
|
72
|
+
break;
|
|
69
73
|
case 'update':
|
|
70
74
|
await cmdUpdate(args);
|
|
71
75
|
break;
|
package/dist/cli/install.js
CHANGED
|
@@ -18,7 +18,7 @@ const HOOKS = [
|
|
|
18
18
|
['SessionStart', 'session-start.js'],
|
|
19
19
|
['UserPromptSubmit', 'user-prompt.js'],
|
|
20
20
|
['SessionEnd', 'session-end.js'],
|
|
21
|
-
['PreToolUse', 'prefer-usages.js', 'Bash|Grep'],
|
|
21
|
+
['PreToolUse', 'prefer-usages.js', 'Bash|Grep', 10],
|
|
22
22
|
];
|
|
23
23
|
export function assertStableLocation(distDir) {
|
|
24
24
|
const ephemeral = ['_npx', '_cacache'];
|
|
@@ -35,7 +35,7 @@ export function install(distDir, scope, project) {
|
|
|
35
35
|
const path = settingsPathFor(scope, project);
|
|
36
36
|
const settings = readJson(path);
|
|
37
37
|
const hooks = (settings['hooks'] ?? {});
|
|
38
|
-
for (const [event, file, matcher] of HOOKS) {
|
|
38
|
+
for (const [event, file, matcher, timeout] of HOOKS) {
|
|
39
39
|
const kept = (hooks[event] ?? [])
|
|
40
40
|
.map((entry) => ({
|
|
41
41
|
...entry,
|
|
@@ -44,7 +44,13 @@ export function install(distDir, scope, project) {
|
|
|
44
44
|
.filter((entry) => entry.hooks.length > 0);
|
|
45
45
|
kept.push({
|
|
46
46
|
...(matcher ? { matcher } : {}),
|
|
47
|
-
hooks: [
|
|
47
|
+
hooks: [
|
|
48
|
+
{
|
|
49
|
+
type: 'command',
|
|
50
|
+
command: hookCommand(distDir, file),
|
|
51
|
+
...(timeout ? { timeout } : {}),
|
|
52
|
+
},
|
|
53
|
+
],
|
|
48
54
|
});
|
|
49
55
|
hooks[event] = kept;
|
|
50
56
|
}
|
package/dist/cli/usage.js
CHANGED
|
@@ -12,10 +12,12 @@ export function usage() {
|
|
|
12
12
|
remember [--kind k] <text> Record something outright, e.g. a house rule
|
|
13
13
|
[--key <name>] ...under a stable name, replacing any earlier one
|
|
14
14
|
forget <id> [id...] Delete specific observations by id
|
|
15
|
+
--session <id> ...or clear one session's summary, so it stops being injected
|
|
15
16
|
projects List every project with memory in this database
|
|
16
17
|
merge [<old-path>] [--yes] Move memory from an old project path onto this one
|
|
17
18
|
stats What this project's memory is made of
|
|
18
19
|
adoption How often sessions grep vs use the memory tools
|
|
20
|
+
view [--export <file>] See this project's memory live in the browser
|
|
19
21
|
flush Re-ingest every transcript for this project
|
|
20
22
|
seed --from-git [--limit n] Fill a cold memory from this repo's history
|
|
21
23
|
scan [--force] Map this repo's symbols and how they connect
|
package/dist/config/schema.d.ts
CHANGED
|
@@ -21,12 +21,23 @@ export declare const ConfigSchema: z.ZodObject<{
|
|
|
21
21
|
tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
22
22
|
exclude: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
23
23
|
maxBodyChars: z.ZodDefault<z.ZodNumber>;
|
|
24
|
+
scripted: z.ZodDefault<z.ZodBoolean>;
|
|
25
|
+
summarize: z.ZodDefault<z.ZodEnum<{
|
|
26
|
+
off: "off";
|
|
27
|
+
on: "on";
|
|
28
|
+
}>>;
|
|
29
|
+
summarizeModel: z.ZodDefault<z.ZodEnum<{
|
|
30
|
+
opus: "opus";
|
|
31
|
+
sonnet: "sonnet";
|
|
32
|
+
haiku: "haiku";
|
|
33
|
+
}>>;
|
|
24
34
|
}, z.core.$strip>>;
|
|
25
35
|
inject: z.ZodPrefault<z.ZodObject<{
|
|
26
36
|
sessions: z.ZodDefault<z.ZodNumber>;
|
|
27
37
|
maxChars: z.ZodDefault<z.ZodNumber>;
|
|
28
38
|
perPrompt: z.ZodDefault<z.ZodBoolean>;
|
|
29
39
|
promptResults: z.ZodDefault<z.ZodNumber>;
|
|
40
|
+
minOverlap: z.ZodDefault<z.ZodNumber>;
|
|
30
41
|
promptMaxChars: z.ZodDefault<z.ZodNumber>;
|
|
31
42
|
expandTop: z.ZodDefault<z.ZodNumber>;
|
|
32
43
|
expandMaxChars: z.ZodDefault<z.ZodNumber>;
|
package/dist/config/schema.js
CHANGED
|
@@ -15,6 +15,9 @@ export const ConfigSchema = z.object({
|
|
|
15
15
|
tools: z.array(z.string()).default(['Edit', 'Write', 'Bash', 'NotebookEdit']),
|
|
16
16
|
exclude: z.array(z.string()).default(['.env', 'secrets', 'node_modules', '.git/']),
|
|
17
17
|
maxBodyChars: z.number().int().positive().default(4000),
|
|
18
|
+
scripted: z.boolean().default(false),
|
|
19
|
+
summarize: z.enum(['off', 'on']).default('off'),
|
|
20
|
+
summarizeModel: z.enum(['opus', 'sonnet', 'haiku']).default('opus'),
|
|
18
21
|
})
|
|
19
22
|
.prefault({}),
|
|
20
23
|
inject: z
|
|
@@ -23,6 +26,7 @@ export const ConfigSchema = z.object({
|
|
|
23
26
|
maxChars: z.number().int().positive().default(6000),
|
|
24
27
|
perPrompt: z.boolean().default(true),
|
|
25
28
|
promptResults: z.number().int().positive().max(10).default(4),
|
|
29
|
+
minOverlap: z.number().int().min(0).default(1),
|
|
26
30
|
promptMaxChars: z.number().int().positive().default(700),
|
|
27
31
|
expandTop: z.number().int().min(0).max(3).default(0),
|
|
28
32
|
expandMaxChars: z.number().int().positive().default(900),
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { LanguageSpec } from './rules.js';
|
|
2
|
+
import type { SymbolKind } from '../../types.js';
|
|
3
|
+
export declare const BASIC_FINGERPRINT: string;
|
|
4
|
+
export declare const BASIC_LANGUAGES: LanguageSpec[];
|
|
5
|
+
export interface BasicDeclaration {
|
|
6
|
+
name: string;
|
|
7
|
+
kind: SymbolKind;
|
|
8
|
+
line: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function declarationsIn(source: string): BasicDeclaration[];
|
|
11
|
+
export declare function callsIn(source: string): BasicDeclaration[];
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
const LABELS = {
|
|
2
|
+
c: ['.c', '.h'],
|
|
3
|
+
cpp: ['.cc', '.cpp', '.cxx', '.hpp', '.hh'],
|
|
4
|
+
csharp: ['.cs'],
|
|
5
|
+
java: ['.java'],
|
|
6
|
+
kotlin: ['.kt', '.kts'],
|
|
7
|
+
swift: ['.swift'],
|
|
8
|
+
'objective-c': ['.m', '.mm'],
|
|
9
|
+
scala: ['.scala'],
|
|
10
|
+
php: ['.php'],
|
|
11
|
+
perl: ['.pl', '.pm'],
|
|
12
|
+
lua: ['.lua'],
|
|
13
|
+
r: ['.r', '.R'],
|
|
14
|
+
julia: ['.jl'],
|
|
15
|
+
dart: ['.dart'],
|
|
16
|
+
elixir: ['.ex', '.exs'],
|
|
17
|
+
erlang: ['.erl'],
|
|
18
|
+
clojure: ['.clj', '.cljs', '.cljc'],
|
|
19
|
+
haskell: ['.hs'],
|
|
20
|
+
ocaml: ['.ml', '.mli'],
|
|
21
|
+
fsharp: ['.fs', '.fsx'],
|
|
22
|
+
groovy: ['.groovy', '.gradle'],
|
|
23
|
+
shell: ['.sh', '.bash', '.zsh'],
|
|
24
|
+
powershell: ['.ps1', '.psm1'],
|
|
25
|
+
sql: ['.sql'],
|
|
26
|
+
zig: ['.zig'],
|
|
27
|
+
nim: ['.nim'],
|
|
28
|
+
crystal: ['.cr'],
|
|
29
|
+
solidity: ['.sol'],
|
|
30
|
+
vala: ['.vala'],
|
|
31
|
+
};
|
|
32
|
+
const KEYWORDS = {
|
|
33
|
+
class: 'class',
|
|
34
|
+
struct: 'class',
|
|
35
|
+
object: 'class',
|
|
36
|
+
record: 'class',
|
|
37
|
+
actor: 'class',
|
|
38
|
+
contract: 'class',
|
|
39
|
+
module: 'class',
|
|
40
|
+
defmodule: 'class',
|
|
41
|
+
namespace: 'class',
|
|
42
|
+
interface: 'interface',
|
|
43
|
+
protocol: 'interface',
|
|
44
|
+
trait: 'interface',
|
|
45
|
+
typeclass: 'interface',
|
|
46
|
+
enum: 'enum',
|
|
47
|
+
type: 'type',
|
|
48
|
+
typedef: 'type',
|
|
49
|
+
data: 'type',
|
|
50
|
+
newtype: 'type',
|
|
51
|
+
def: 'function',
|
|
52
|
+
defp: 'function',
|
|
53
|
+
defn: 'function',
|
|
54
|
+
defun: 'function',
|
|
55
|
+
function: 'function',
|
|
56
|
+
func: 'function',
|
|
57
|
+
fn: 'function',
|
|
58
|
+
sub: 'function',
|
|
59
|
+
proc: 'function',
|
|
60
|
+
method: 'method',
|
|
61
|
+
};
|
|
62
|
+
const DECLARATION = new RegExp(`^[\\t ]*(?:[\\w@\\[\\]]+[\\t ]+){0,3}(${Object.keys(KEYWORDS).join('|')})[\\t ]+([A-Za-z_][\\w]*)`);
|
|
63
|
+
const CALLABLE = /^[\t ]*(?:[A-Za-z_][\w:<>,.\[\]*&\t ]*[\t *&]+)?([A-Za-z_]\w*)[\t ]*\([^;=]*\)[\t ]*(?:const[\t ]*)?\{[\t ]*$/;
|
|
64
|
+
const CALL = /\b([A-Za-z_]\w{2,})[\t ]*\(/g;
|
|
65
|
+
const NOT_A_CALL = new Set([
|
|
66
|
+
'if',
|
|
67
|
+
'for',
|
|
68
|
+
'while',
|
|
69
|
+
'switch',
|
|
70
|
+
'catch',
|
|
71
|
+
'return',
|
|
72
|
+
'with',
|
|
73
|
+
'else',
|
|
74
|
+
'elsif',
|
|
75
|
+
'unless',
|
|
76
|
+
'until',
|
|
77
|
+
'when',
|
|
78
|
+
'case',
|
|
79
|
+
'do',
|
|
80
|
+
'end',
|
|
81
|
+
'and',
|
|
82
|
+
'not',
|
|
83
|
+
'or',
|
|
84
|
+
'sizeof',
|
|
85
|
+
'typeof',
|
|
86
|
+
'defined',
|
|
87
|
+
'print',
|
|
88
|
+
'printf',
|
|
89
|
+
'echo',
|
|
90
|
+
'assert',
|
|
91
|
+
'require',
|
|
92
|
+
'import',
|
|
93
|
+
'include',
|
|
94
|
+
'new',
|
|
95
|
+
'delete',
|
|
96
|
+
'throw',
|
|
97
|
+
'super',
|
|
98
|
+
'self',
|
|
99
|
+
'this',
|
|
100
|
+
...Object.keys(KEYWORDS),
|
|
101
|
+
]);
|
|
102
|
+
export const BASIC_FINGERPRINT = [
|
|
103
|
+
DECLARATION.source,
|
|
104
|
+
CALLABLE.source,
|
|
105
|
+
CALL.source,
|
|
106
|
+
Object.keys(LABELS).join(','),
|
|
107
|
+
].join('|');
|
|
108
|
+
export const BASIC_LANGUAGES = Object.entries(LABELS).map(([label, extensions]) => ({
|
|
109
|
+
id: 'basic',
|
|
110
|
+
label,
|
|
111
|
+
extensions,
|
|
112
|
+
definitions: [],
|
|
113
|
+
references: [],
|
|
114
|
+
basic: true,
|
|
115
|
+
}));
|
|
116
|
+
export function declarationsIn(source) {
|
|
117
|
+
const found = [];
|
|
118
|
+
const lines = source.split('\n');
|
|
119
|
+
for (let index = 0; index < lines.length; index++) {
|
|
120
|
+
const line = lines[index] ?? '';
|
|
121
|
+
if (line.length > 400)
|
|
122
|
+
continue;
|
|
123
|
+
const keyword = DECLARATION.exec(line);
|
|
124
|
+
if (keyword) {
|
|
125
|
+
const kind = KEYWORDS[keyword[1] ?? ''];
|
|
126
|
+
const name = keyword[2];
|
|
127
|
+
if (kind && name && !KEYWORDS[name])
|
|
128
|
+
found.push({ name, kind, line: index + 1 });
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const callable = CALLABLE.exec(line);
|
|
132
|
+
const name = callable?.[1];
|
|
133
|
+
if (name && !NOT_A_CALL.has(name))
|
|
134
|
+
found.push({ name, kind: 'function', line: index + 1 });
|
|
135
|
+
}
|
|
136
|
+
return found;
|
|
137
|
+
}
|
|
138
|
+
const MAX_CALLS = 200;
|
|
139
|
+
export function callsIn(source) {
|
|
140
|
+
const found = [];
|
|
141
|
+
const lines = source.split('\n');
|
|
142
|
+
for (let index = 0; index < lines.length && found.length < MAX_CALLS; index++) {
|
|
143
|
+
const line = lines[index] ?? '';
|
|
144
|
+
if (line.length > 400)
|
|
145
|
+
continue;
|
|
146
|
+
if (DECLARATION.test(line) || CALLABLE.test(line))
|
|
147
|
+
continue;
|
|
148
|
+
for (const match of line.matchAll(CALL)) {
|
|
149
|
+
const name = match[1];
|
|
150
|
+
if (!name || NOT_A_CALL.has(name))
|
|
151
|
+
continue;
|
|
152
|
+
found.push({ name, kind: 'function', line: index + 1 });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return found;
|
|
156
|
+
}
|
|
@@ -2,5 +2,6 @@ import type { LanguageSpec } from './rules.js';
|
|
|
2
2
|
export type { DefinitionRule, LanguageSpec, ReferenceRule } from './rules.js';
|
|
3
3
|
export declare const LANGUAGES: LanguageSpec[];
|
|
4
4
|
export declare const DYNAMIC_LANGUAGES: string[];
|
|
5
|
+
export { BASIC_FINGERPRINT, callsIn, declarationsIn } from './basic.js';
|
|
5
6
|
export declare function languageFor(path: string): LanguageSpec | null;
|
|
6
7
|
export declare function languageNames(): string;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import { BASIC_LANGUAGES } from './basic.js';
|
|
1
2
|
import { javascript, tsx, typescript } from './ecmascript.js';
|
|
2
3
|
import { go } from './go.js';
|
|
3
4
|
import { python } from './python.js';
|
|
5
|
+
import { ruby } from './ruby.js';
|
|
4
6
|
import { rust } from './rust.js';
|
|
5
|
-
export const LANGUAGES = [typescript, tsx, javascript, python, go, rust];
|
|
6
|
-
export const DYNAMIC_LANGUAGES = ['python', 'go', 'rust'];
|
|
7
|
+
export const LANGUAGES = [typescript, tsx, javascript, python, go, rust, ruby];
|
|
8
|
+
export const DYNAMIC_LANGUAGES = ['python', 'go', 'rust', 'ruby'];
|
|
9
|
+
export { BASIC_FINGERPRINT, callsIn, declarationsIn } from './basic.js';
|
|
7
10
|
const BY_EXTENSION = new Map();
|
|
8
|
-
for (const spec of LANGUAGES) {
|
|
11
|
+
for (const spec of [...LANGUAGES, ...BASIC_LANGUAGES]) {
|
|
9
12
|
for (const extension of spec.extensions)
|
|
10
13
|
BY_EXTENSION.set(extension, spec);
|
|
11
14
|
}
|
|
@@ -16,5 +19,5 @@ export function languageFor(path) {
|
|
|
16
19
|
return BY_EXTENSION.get(path.slice(dot).toLowerCase()) ?? null;
|
|
17
20
|
}
|
|
18
21
|
export function languageNames() {
|
|
19
|
-
return LANGUAGES.map((spec) => spec.label).join(', ')
|
|
22
|
+
return `${LANGUAGES.map((spec) => spec.label).join(', ')}, and ${BASIC_LANGUAGES.length} more by pattern`;
|
|
20
23
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const ruby = {
|
|
2
|
+
id: 'ruby',
|
|
3
|
+
label: 'ruby',
|
|
4
|
+
extensions: ['.rb', '.rake'],
|
|
5
|
+
definitions: [
|
|
6
|
+
{ kind: 'method', field: ['name'], symbol: 'method' },
|
|
7
|
+
{ kind: 'singleton_method', field: ['name'], symbol: 'method' },
|
|
8
|
+
{ kind: 'class', field: ['name'], symbol: 'class' },
|
|
9
|
+
{ kind: 'module', field: ['name'], symbol: 'class' },
|
|
10
|
+
],
|
|
11
|
+
references: [
|
|
12
|
+
{ kind: 'call', field: ['method'], relation: 'calls' },
|
|
13
|
+
{ kind: 'call', field: ['receiver'], relation: 'references' },
|
|
14
|
+
{ kind: 'class', field: ['superclass'], relation: 'extends' },
|
|
15
|
+
],
|
|
16
|
+
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { MemoryStore } from '../../store/adapter.js';
|
|
2
|
-
/** Names closest to `symbol`, best first. A miss that names no alternative is where people quit. */
|
|
3
2
|
export declare function nearest(names: Iterable<string>, symbol: string, limit?: number): string[];
|
|
4
3
|
export declare function suggestFor(store: MemoryStore, project: string, symbol: string): Promise<string[]>;
|
|
5
4
|
export declare function formatSuggestions(suggestions: string[]): string;
|
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
const CANDIDATES = 5000;
|
|
2
2
|
function tokens(name) {
|
|
3
|
-
return
|
|
3
|
+
return name
|
|
4
4
|
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
5
5
|
.split(/[^A-Za-z0-9]+/)
|
|
6
6
|
.filter(Boolean)
|
|
7
7
|
.map((part) => part.toLowerCase())
|
|
8
|
-
|
|
9
|
-
.map((part) => (part.length > 3 && part.endsWith('s') ? part.slice(0, -1) : part)));
|
|
8
|
+
.map((part) => (part.length > 3 && part.endsWith('s') ? part.slice(0, -1) : part));
|
|
10
9
|
}
|
|
11
10
|
function score(candidate, query) {
|
|
12
11
|
const left = candidate.toLowerCase();
|
|
13
12
|
const right = query.toLowerCase();
|
|
14
13
|
if (left === right)
|
|
15
14
|
return 100;
|
|
16
|
-
// Ratios are taken against the LONGER name: without that, one-letter locals
|
|
17
|
-
// score as well as the real symbol, because everything contains "e".
|
|
18
15
|
const longest = Math.max(left.length, right.length);
|
|
19
16
|
let prefix = 0;
|
|
20
17
|
while (prefix < left.length && prefix < right.length && left[prefix] === right[prefix]) {
|
|
@@ -29,16 +26,12 @@ function score(candidate, query) {
|
|
|
29
26
|
const byContains = contains ? (Math.min(left.length, right.length) / longest) * 100 : 0;
|
|
30
27
|
return Math.max(byPrefix, byToken, byContains);
|
|
31
28
|
}
|
|
32
|
-
/** Names closest to `symbol`, best first. A miss that names no alternative is where people quit. */
|
|
33
29
|
export function nearest(names, symbol, limit = 5) {
|
|
34
30
|
const scored = new Map();
|
|
35
31
|
for (const name of names) {
|
|
36
|
-
// Two-character locals are never what someone misremembered.
|
|
37
32
|
if (name === symbol || name.length < 3)
|
|
38
33
|
continue;
|
|
39
34
|
const value = score(name, symbol);
|
|
40
|
-
// Below this, matches are incidental ("widget" contains "get") and a wrong
|
|
41
|
-
// suggestion costs more than none: it sends you looking for the wrong thing.
|
|
42
35
|
if (value >= 40)
|
|
43
36
|
scored.set(name, Math.max(scored.get(name) ?? 0, value));
|
|
44
37
|
}
|
|
@@ -1,11 +1,50 @@
|
|
|
1
1
|
import { observationId } from '../../capture/identity.js';
|
|
2
2
|
import { redact } from '../../capture/turn-extractor.js';
|
|
3
|
+
import { callsIn, declarationsIn } from '../languages/index.js';
|
|
3
4
|
import { languageHandle, loadParser } from '../parser.js';
|
|
4
5
|
const CALLABLE = new Set(['function', 'method', 'class']);
|
|
5
|
-
const IDENTIFIER = /^[\w$]
|
|
6
|
+
const IDENTIFIER = /^[\w$]+[?!]?$/;
|
|
6
7
|
export function symbolId(project, file, name, kind) {
|
|
7
8
|
return observationId('graph', 0, `${project}\0${file}\0${name}\0${kind}`);
|
|
8
9
|
}
|
|
10
|
+
function extractByPattern(file, project) {
|
|
11
|
+
const lines = file.source.split('\n');
|
|
12
|
+
const symbols = [];
|
|
13
|
+
const spans = [];
|
|
14
|
+
for (const declaration of declarationsIn(file.source)) {
|
|
15
|
+
const symbol = {
|
|
16
|
+
id: symbolId(project, file.path, declaration.name, declaration.kind),
|
|
17
|
+
project,
|
|
18
|
+
name: declaration.name,
|
|
19
|
+
kind: declaration.kind,
|
|
20
|
+
file: file.path,
|
|
21
|
+
line: declaration.line,
|
|
22
|
+
lang: file.spec.label,
|
|
23
|
+
signature: redact((lines[declaration.line - 1] ?? '').trim()).slice(0, 200),
|
|
24
|
+
};
|
|
25
|
+
symbols.push(symbol);
|
|
26
|
+
spans.push({ start: declaration.line, end: declaration.line, symbol });
|
|
27
|
+
}
|
|
28
|
+
const references = callsIn(file.source).map((call) => ({
|
|
29
|
+
file: file.path,
|
|
30
|
+
name: call.name,
|
|
31
|
+
relation: 'calls',
|
|
32
|
+
line: call.line,
|
|
33
|
+
from: nearestAbove(call.line, spans),
|
|
34
|
+
weak: true,
|
|
35
|
+
}));
|
|
36
|
+
return { symbols, references };
|
|
37
|
+
}
|
|
38
|
+
function nearestAbove(line, spans) {
|
|
39
|
+
let best = null;
|
|
40
|
+
for (const span of spans) {
|
|
41
|
+
if (span.start > line)
|
|
42
|
+
continue;
|
|
43
|
+
if (!best || span.start > best.start)
|
|
44
|
+
best = span;
|
|
45
|
+
}
|
|
46
|
+
return best?.symbol ?? null;
|
|
47
|
+
}
|
|
9
48
|
function resolveField(node, path) {
|
|
10
49
|
let current = node;
|
|
11
50
|
for (const name of path) {
|
|
@@ -16,7 +55,7 @@ function resolveField(node, path) {
|
|
|
16
55
|
return current;
|
|
17
56
|
}
|
|
18
57
|
function unquote(text) {
|
|
19
|
-
return text.replace(/^['"`]|['"`]$/g, '');
|
|
58
|
+
return text.replace(/^['"`]|['"`]$/g, '').replace(/^[<(:\s]+|[)\s]+$/g, '');
|
|
20
59
|
}
|
|
21
60
|
function enclosing(line, spans) {
|
|
22
61
|
let best = null;
|
|
@@ -38,6 +77,8 @@ function enclosing(line, spans) {
|
|
|
38
77
|
return (best ?? fallback)?.symbol ?? null;
|
|
39
78
|
}
|
|
40
79
|
export function extractFile(file, project) {
|
|
80
|
+
if (file.spec.basic)
|
|
81
|
+
return extractByPattern(file, project);
|
|
41
82
|
const parser = loadParser();
|
|
42
83
|
const root = parser.parse(languageHandle(parser, file.spec.id), file.source).root();
|
|
43
84
|
const lines = file.source.split('\n');
|
|
@@ -5,7 +5,7 @@ export interface SourceFile {
|
|
|
5
5
|
source: string;
|
|
6
6
|
hash: string;
|
|
7
7
|
}
|
|
8
|
-
export declare const SCAN_VERSION =
|
|
8
|
+
export declare const SCAN_VERSION = 3;
|
|
9
9
|
export declare function hashOf(bytes: Buffer): string;
|
|
10
10
|
export declare function listFiles(root: string): string[];
|
|
11
11
|
export declare function sourceFiles(root: string): string[];
|
package/dist/graph/scan/files.js
CHANGED
|
@@ -2,12 +2,13 @@ import { execFileSync } from 'node:child_process';
|
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
3
|
import { readFileSync } from 'node:fs';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { LANGUAGES, languageFor } from '../languages/index.js';
|
|
5
|
+
import { BASIC_FINGERPRINT, LANGUAGES, languageFor } from '../languages/index.js';
|
|
6
6
|
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
7
7
|
const MAX_FILE_BYTES = 1024 * 1024;
|
|
8
|
-
export const SCAN_VERSION =
|
|
8
|
+
export const SCAN_VERSION = 3;
|
|
9
9
|
const RULES_FINGERPRINT = createHash('sha256')
|
|
10
10
|
.update(JSON.stringify(LANGUAGES))
|
|
11
|
+
.update(BASIC_FINGERPRINT)
|
|
11
12
|
.digest('hex')
|
|
12
13
|
.slice(0, 12);
|
|
13
14
|
const CACHE_KEY = `v${SCAN_VERSION}:${RULES_FINGERPRINT}\n`;
|
|
@@ -19,6 +19,8 @@ export function resolveEdges(project, symbols, references) {
|
|
|
19
19
|
const candidates = byName.get(reference.name) ?? [];
|
|
20
20
|
const local = candidates.find((candidate) => candidate.file === reference.file);
|
|
21
21
|
const target = local ?? candidates[0];
|
|
22
|
+
if (!target && reference.weak)
|
|
23
|
+
continue;
|
|
22
24
|
let confidence = 'EXTRACTED';
|
|
23
25
|
let score = SAME_FILE;
|
|
24
26
|
if (!local && candidates.length === 1) {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export declare const DECLARED: Set<string>;
|
|
2
2
|
export declare function isSymbol(pattern: string): boolean;
|
|
3
3
|
export declare function isWord(pattern: string): boolean;
|
|
4
|
-
/** Symbols a shell command searches the tree for, which the code graph also answers. */
|
|
5
4
|
export declare function symbolsGreppedIn(command: string): string[];
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
const SYMBOL = /^(?=.{4,})(?:[a-z]+(?:[A-Z][a-zA-Z0-9]*)+|[A-Z][a-z0-9]+(?:[A-Z][a-zA-Z0-9]*)*|[a-z][a-z0-9]*(?:_[a-z0-9]+)+)$/;
|
|
2
2
|
const WORD = /^[a-z][a-z0-9]{3,}$/;
|
|
3
3
|
export const DECLARED = new Set(['function', 'method', 'class', 'interface', 'type', 'enum']);
|
|
4
|
-
const GREP =
|
|
4
|
+
const GREP = /(?:^|[;&|(`]|\$\(|\bdo\s+|\bthen\s+|\belse\s+|\bxargs\s+|\bgit\s+|\bsudo\s+|\btime\s+|-exec\s+)\s*(?:grep|rg)\b((?:\s+(?:-[ABC]\s?\d+|-[tTg]\s\S+|-{1,2}[\w-]+(?:=\S+)?))*)\s+(?:(['"])(.+?)\2|(\S+))([^;&|]*)/gm;
|
|
5
5
|
export function isSymbol(pattern) {
|
|
6
6
|
return SYMBOL.test(pattern);
|
|
7
7
|
}
|
|
8
8
|
export function isWord(pattern) {
|
|
9
9
|
return WORD.test(pattern);
|
|
10
10
|
}
|
|
11
|
-
/** Symbols a shell command searches the tree for, which the code graph also answers. */
|
|
12
11
|
export function symbolsGreppedIn(command) {
|
|
13
12
|
const found = new Set();
|
|
14
13
|
for (const match of command.matchAll(GREP)) {
|
|
@@ -19,11 +18,12 @@ export function symbolsGreppedIn(command) {
|
|
|
19
18
|
continue;
|
|
20
19
|
if (/-\w*v/.test(flags))
|
|
21
20
|
continue;
|
|
22
|
-
const
|
|
21
|
+
const invocation = match[0];
|
|
22
|
+
const piped = /^\s*\|/.test(invocation);
|
|
23
23
|
const searchesTree = /-\w*[rR]\b|--recursive/.test(flags) ||
|
|
24
24
|
/^\s+[^-|;&>][^\s|;&>]*/.test(rest) ||
|
|
25
|
-
/\bgit\s
|
|
26
|
-
(
|
|
25
|
+
/\bgit\s+(?:grep|rg)\b/.test(invocation) ||
|
|
26
|
+
(/(?:^|[\s;&|(])rg\b/.test(invocation) && !piped);
|
|
27
27
|
if (searchesTree)
|
|
28
28
|
found.add(pattern);
|
|
29
29
|
}
|
package/dist/hooks/payload.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface HookPayload {
|
|
|
8
8
|
tool_response?: unknown;
|
|
9
9
|
prompt?: string;
|
|
10
10
|
}
|
|
11
|
+
export declare function capturingDisabled(scripted: boolean): boolean;
|
|
11
12
|
export declare function readPayload(): Promise<HookPayload>;
|
|
12
13
|
export declare function runHook(fn: () => Promise<void>): Promise<void>;
|
|
13
14
|
export declare function emitContext(text: string): void;
|
package/dist/hooks/payload.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
export function capturingDisabled(scripted) {
|
|
2
|
+
if (process.env['CLAUDE_DB_CAPTURE'] === 'off')
|
|
3
|
+
return true;
|
|
4
|
+
return process.env['CLAUDE_CODE_ENTRYPOINT'] === 'sdk-cli' && !scripted;
|
|
5
|
+
}
|
|
1
6
|
export async function readPayload() {
|
|
2
7
|
const chunks = [];
|
|
3
8
|
for await (const chunk of process.stdin)
|