@aprimediet/codewalker 1.1.0 → 1.2.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/src/query.test.ts CHANGED
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
2
  import * as fs from 'node:fs';
3
3
  import * as path from 'node:path';
4
4
  import * as os from 'node:os';
5
- import { openDb, upsertSymbol, setMeta, getMeta, searchSymbols } from './db.ts';
5
+ import { openDb, upsertSymbol, setMeta, getMeta, upsertLibSymbol, searchLibSymbols } from './db.ts';
6
6
  import { runQuery } from './query.ts';
7
7
 
8
8
  describe('query.ts', () => {
@@ -95,4 +95,75 @@ describe('query.ts', () => {
95
95
  // In a non-git dir, staleness should be null
96
96
  expect(result.staleness).toBeNull();
97
97
  });
98
+
99
+ // ── source filter ───────────────────────────────────────────
100
+ it('source="code" returns only code symbols (default)', () => {
101
+ const db = openDb(dbPath);
102
+ upsertSymbol(db, {
103
+ name: 'myFunc', kind: 'function', file_path: 'src/a.ts',
104
+ line_start: 1, line_end: 1, signature: '', doc: '', summary: '', card_path: '',
105
+ });
106
+ upsertLibSymbol(db, {
107
+ lib: 'hono', version: '4.6.3', name: 'honoFunc',
108
+ kind: 'function', signature: '', doc: '', summary: '', card_path: '',
109
+ });
110
+ db.close();
111
+
112
+ const result = runQuery(dbPath, { query: '', source: 'code' });
113
+ expect(result.rows).toHaveLength(1);
114
+ expect(result.rows[0]!.name).toBe('myFunc');
115
+ expect(result.rows[0]!.source).toBeUndefined();
116
+ });
117
+
118
+ it('source="libs" returns only lib symbols', () => {
119
+ const db = openDb(dbPath);
120
+ upsertSymbol(db, {
121
+ name: 'myFunc', kind: 'function', file_path: 'src/a.ts',
122
+ line_start: 1, line_end: 1, signature: '', doc: '', summary: '', card_path: '',
123
+ });
124
+ upsertLibSymbol(db, {
125
+ lib: 'hono', version: '4.6.3', name: 'honoFunc',
126
+ kind: 'function', signature: '', doc: '', summary: '', card_path: '',
127
+ });
128
+ db.close();
129
+
130
+ const result = runQuery(dbPath, { query: '', source: 'libs' });
131
+ expect(result.rows).toHaveLength(1);
132
+ expect(result.rows[0]!.name).toBe('honoFunc');
133
+ expect(result.rows[0]!.source).toBe('lib');
134
+ expect(result.rows[0]!.lib).toBe('hono');
135
+ expect(result.rows[0]!.version).toBe('4.6.3');
136
+ });
137
+
138
+ it('source="all" returns merged code and lib symbols', () => {
139
+ const db = openDb(dbPath);
140
+ upsertSymbol(db, {
141
+ name: 'myFunc', kind: 'function', file_path: 'src/a.ts',
142
+ line_start: 1, line_end: 1, signature: '', doc: '', summary: '', card_path: '',
143
+ });
144
+ upsertLibSymbol(db, {
145
+ lib: 'hono', version: '4.6.3', name: 'honoFunc',
146
+ kind: 'function', signature: '', doc: '', summary: '', card_path: '',
147
+ });
148
+ db.close();
149
+
150
+ const result = runQuery(dbPath, { query: '', source: 'all' });
151
+ expect(result.rows).toHaveLength(2);
152
+ });
153
+
154
+ it('source=all respects limit across merged set', () => {
155
+ const db = openDb(dbPath);
156
+ upsertSymbol(db, {
157
+ name: 'aCode', kind: 'function', file_path: 'src/a.ts',
158
+ line_start: 1, line_end: 1, signature: '', doc: '', summary: '', card_path: '',
159
+ });
160
+ upsertLibSymbol(db, {
161
+ lib: 'pkg', version: '1.0.0', name: 'bLib',
162
+ kind: 'function', signature: '', doc: '', summary: '', card_path: '',
163
+ });
164
+ db.close();
165
+
166
+ const result = runQuery(dbPath, { query: '', source: 'all', limit: 1 });
167
+ expect(result.rows).toHaveLength(1);
168
+ });
98
169
  });
package/src/query.ts CHANGED
@@ -3,13 +3,15 @@
3
3
  */
4
4
 
5
5
  import type { QueryResult, QueryResultRow, StalenessInfo } from "./types.ts";
6
- import { openDb, searchSymbols, getMeta } from "./db.ts";
6
+ import { openDb, searchSymbols, searchLibSymbols, getMeta } from "./db.ts";
7
7
  import { getHeadSha, changedFilesSince } from "./git.ts";
8
8
 
9
9
  export interface QueryParams {
10
10
  query: string;
11
11
  kind?: string;
12
12
  limit?: number;
13
+ /** Source scope: "code" (default, only code symbols), "libs" (only lib symbols), or "all" (both). */
14
+ source?: "code" | "libs" | "all";
13
15
  }
14
16
 
15
17
  /**
@@ -27,12 +29,26 @@ export function runQuery(
27
29
  const db = openDb(dbPath);
28
30
 
29
31
  try {
30
- const rows = searchSymbols(
31
- db,
32
- params.query,
33
- params.kind,
34
- params.limit ?? 10,
35
- );
32
+ const source = params.source ?? "code";
33
+ const limit = params.limit ?? 10;
34
+
35
+ let rows: QueryResultRow[];
36
+
37
+ if (source === "libs") {
38
+ rows = searchLibSymbols(db, params.query, params.kind, limit) as unknown as QueryResultRow[];
39
+ } else if (source === "all") {
40
+ // Run both code and lib searches, merge, sort by score, apply limit
41
+ const codeRows = searchSymbols(db, params.query, params.kind, limit);
42
+ const libRows = searchLibSymbols(db, params.query, params.kind, limit) as unknown as QueryResultRow[];
43
+
44
+ // Merge and sort by score ascending (lower bm25 = better match)
45
+ const merged: QueryResultRow[] = [...codeRows, ...libRows];
46
+ merged.sort((a, b) => a.score - b.score);
47
+ rows = merged.slice(0, limit);
48
+ } else {
49
+ // "code" — default, existing behavior
50
+ rows = searchSymbols(db, params.query, params.kind, limit);
51
+ }
36
52
 
37
53
  const staleness = detectStaleness(db, repoDir);
38
54
 
package/src/types.ts CHANGED
@@ -4,6 +4,18 @@
4
4
  * These types are shared across all layers (extraction, cards, DB, query).
5
5
  */
6
6
 
7
+ /** Library symbol kind — extends SymbolKind with reexport + namespace. */
8
+ export type LibSymbolKind =
9
+ | "function"
10
+ | "const"
11
+ | "class"
12
+ | "interface"
13
+ | "type"
14
+ | "enum"
15
+ | "namespace"
16
+ | "reexport"
17
+ | "module";
18
+
7
19
  /** The kind of a code symbol. */
8
20
  export type SymbolKind =
9
21
  | "function"
@@ -17,6 +29,18 @@ export type SymbolKind =
17
29
  | "namespace"
18
30
  | "module";
19
31
 
32
+ /** A single symbol extracted from a library's .d.ts file. */
33
+ export interface LibSymbol {
34
+ lib: string;
35
+ version: string;
36
+ name: string;
37
+ kind: LibSymbolKind;
38
+ signature: string;
39
+ doc: string;
40
+ summary: string;
41
+ card_path: string;
42
+ }
43
+
20
44
  /** A single symbol extracted from source code. */
21
45
  export interface Symbol {
22
46
  name: string;
@@ -51,6 +75,10 @@ export interface QueryResultRow {
51
75
  summary: string;
52
76
  score: number;
53
77
  id: number;
78
+ /** Origin fields — code rows omit these; lib rows set them. */
79
+ source?: "code" | "lib";
80
+ lib?: string;
81
+ version?: string;
54
82
  }
55
83
 
56
84
  /** The full result of a query, including staleness info. */