opencodekit 0.23.0 → 0.23.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/dist/index.js +354 -825
- package/dist/template/.opencode/AGENTS.md +15 -0
- package/dist/template/.opencode/command/init.md +198 -34
- package/dist/template/.opencode/context/fallow.md +137 -0
- package/dist/template/.opencode/dcp-prompts/overrides/compress-range.md +89 -0
- package/dist/template/.opencode/opencode.json +110 -315
- package/dist/template/.opencode/plugin/README.md +10 -0
- package/dist/template/.opencode/plugin/memory/compile.ts +171 -186
- package/dist/template/.opencode/plugin/memory/index-generator.ts +118 -133
- package/dist/template/.opencode/plugin/memory/lint.ts +253 -275
- package/dist/template/.opencode/plugin/memory/tools.ts +224 -268
- package/dist/template/.opencode/plugin/memory/validate.ts +154 -164
- package/dist/template/.opencode/plugin/sdk/copilot/responses/tool/web-search-preview.ts +13 -30
- package/dist/template/.opencode/plugin/sdk/copilot/responses/tool/web-search-shared.ts +25 -0
- package/dist/template/.opencode/plugin/sdk/copilot/responses/tool/web-search.ts +17 -34
- package/dist/template/.opencode/plugin/session-summary.ts +542 -0
- package/dist/template/.opencode/plugin/srcwalk.ts +775 -661
- package/dist/template/.opencode/skill/condition-based-waiting/example.ts +15 -2
- package/dist/template/.opencode/skill/fallow/SKILL.md +409 -0
- package/dist/template/.opencode/skill/fallow/references/cli-reference.md +1905 -0
- package/dist/template/.opencode/skill/fallow/references/gotchas.md +644 -0
- package/dist/template/.opencode/skill/fallow/references/patterns.md +791 -0
- package/package.json +2 -2
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* - srcwalk_impact — Heuristic blast-radius triage
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import {
|
|
19
|
+
import { execFileSync } from "node:child_process";
|
|
20
20
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
21
21
|
import { readdir } from "node:fs/promises";
|
|
22
22
|
import path from "node:path";
|
|
@@ -31,637 +31,745 @@ const MAX_BUFFER = 5 * 1024 * 1024;
|
|
|
31
31
|
// Helpers
|
|
32
32
|
// ---------------------------------------------------------------------------
|
|
33
33
|
|
|
34
|
-
function run(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
34
|
+
function run(
|
|
35
|
+
cmd: string,
|
|
36
|
+
args: string[],
|
|
37
|
+
cwd?: string,
|
|
38
|
+
): { stdout: string; stderr: string; code: number } {
|
|
39
|
+
try {
|
|
40
|
+
const result = execFileSync(cmd, args, {
|
|
41
|
+
encoding: "utf-8",
|
|
42
|
+
timeout: TIMEOUT_MS,
|
|
43
|
+
maxBuffer: MAX_BUFFER,
|
|
44
|
+
cwd: cwd ?? process.cwd(),
|
|
45
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
46
|
+
});
|
|
47
|
+
return { stdout: result.stdout ?? "", stderr: result.stderr ?? "", code: 0 };
|
|
48
|
+
} catch (err: unknown) {
|
|
49
|
+
const e = err as { stdout?: string; stderr?: string; status?: number; message?: string };
|
|
50
|
+
return {
|
|
51
|
+
stdout: e.stdout ?? "",
|
|
52
|
+
stderr: e.stderr ?? "",
|
|
53
|
+
code: e.status ?? 1,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
function hasRg(): boolean {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
try {
|
|
60
|
+
execFileSync(BIN_RG, ["--version"], { encoding: "utf-8", timeout: 1000, stdio: "ignore" });
|
|
61
|
+
return true;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
function plural(n: number, word: string): string {
|
|
64
|
-
|
|
68
|
+
return `${n} ${word}${n !== 1 ? "s" : ""}`;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
// ---------------------------------------------------------------------------
|
|
68
72
|
// Plugin
|
|
69
73
|
// ---------------------------------------------------------------------------
|
|
70
74
|
|
|
71
|
-
interface PluginConfig {
|
|
72
|
-
directory?: string;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
75
|
const srcwalkTools = {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
76
|
+
// -----------------------------------------------------------------------
|
|
77
|
+
// srcwalk_search: Search codebase
|
|
78
|
+
// -----------------------------------------------------------------------
|
|
79
|
+
srcwalk_search: tool({
|
|
80
|
+
description: `Search for symbols, text, or regex patterns in code.\n\nUses ripgrep (rg) when available, falls back to grep.\nSupports symbol search (definitions first), content search, and regex.`,
|
|
81
|
+
args: {
|
|
82
|
+
query: tool.schema.string().describe("Search query or regex pattern"),
|
|
83
|
+
scope: tool.schema.string().optional().describe("Subdirectory scope (default: project root)"),
|
|
84
|
+
kind: tool.schema.string().optional().describe("Search type: text (default), regex, content"),
|
|
85
|
+
context: tool.schema.number().optional().describe("Lines of context before/after each match"),
|
|
86
|
+
limit: tool.schema.number().optional().describe("Max results (default: 30)"),
|
|
87
|
+
},
|
|
88
|
+
execute: async (args, context) => {
|
|
89
|
+
const query = String(args.query ?? "").trim();
|
|
90
|
+
if (!query) return "❌ query is required.";
|
|
91
|
+
const scope = args.scope ? String(args.scope) : context.directory;
|
|
92
|
+
const limit = Math.min(args.limit ?? 30, 100);
|
|
93
|
+
const ctxLines = args.context ?? 0;
|
|
94
|
+
const kind = String(args.kind ?? "text");
|
|
95
|
+
|
|
96
|
+
const searchDir = path.resolve(context.directory, scope);
|
|
97
|
+
|
|
98
|
+
if (hasRg()) {
|
|
99
|
+
const rgArgs = ["--no-heading", "--line-number", "--color", "never"];
|
|
100
|
+
rgArgs.push("--max-count", String(limit * 2));
|
|
101
|
+
if (ctxLines > 0) {
|
|
102
|
+
rgArgs.push("-C", String(ctxLines));
|
|
103
|
+
}
|
|
104
|
+
if (kind === "regex") {
|
|
105
|
+
rgArgs.push("-E", "rust");
|
|
106
|
+
} else {
|
|
107
|
+
rgArgs.push("-F"); // literal
|
|
108
|
+
}
|
|
109
|
+
rgArgs.push(query, searchDir);
|
|
110
|
+
|
|
111
|
+
const result = run(BIN_RG, rgArgs);
|
|
112
|
+
if (result.code !== 0 && result.stderr) {
|
|
113
|
+
// Try plain grep fallback
|
|
114
|
+
} else {
|
|
115
|
+
const lines = result.stdout.split("\n").filter(Boolean).slice(0, limit);
|
|
116
|
+
if (lines.length === 0) return "No matches found.";
|
|
117
|
+
return lines.join("\n");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Fallback to grep
|
|
122
|
+
const grepArgs = ["-rn", "--color=never"];
|
|
123
|
+
if (ctxLines > 0) grepArgs.push("-C", String(ctxLines));
|
|
124
|
+
if (kind === "regex") {
|
|
125
|
+
grepArgs.push("-E");
|
|
126
|
+
} else {
|
|
127
|
+
grepArgs.push("-F");
|
|
128
|
+
}
|
|
129
|
+
grepArgs.push(query, searchDir);
|
|
130
|
+
|
|
131
|
+
const result = run("grep", grepArgs);
|
|
132
|
+
const lines = result.stdout.split("\n").filter(Boolean).slice(0, limit);
|
|
133
|
+
if (lines.length === 0) return "No matches found.";
|
|
134
|
+
return lines.join("\n");
|
|
135
|
+
},
|
|
136
|
+
}),
|
|
137
|
+
|
|
138
|
+
// -----------------------------------------------------------------------
|
|
139
|
+
// srcwalk_read: Read files
|
|
140
|
+
// -----------------------------------------------------------------------
|
|
141
|
+
srcwalk_read: tool({
|
|
142
|
+
description: `Read a file with optional section (line range, symbol, or path:line format).\n\nSmall files return full content; large files support outlining.\nUse path:start-end for range reads (e.g. "src/app.ts:44-89").`,
|
|
143
|
+
args: {
|
|
144
|
+
path: tool.schema
|
|
145
|
+
.string()
|
|
146
|
+
.describe("File path to read (supports path:line or path:start-end)"),
|
|
147
|
+
section: tool.schema
|
|
148
|
+
.string()
|
|
149
|
+
.optional()
|
|
150
|
+
.describe("Line range '44-89' or heading/symbol name"),
|
|
151
|
+
full: tool.schema.boolean().optional().describe("Force full content"),
|
|
152
|
+
},
|
|
153
|
+
execute: async (args, context) => {
|
|
154
|
+
const fileArg = String(args.path);
|
|
155
|
+
const fullFilePath = path.resolve(context.directory, fileArg);
|
|
156
|
+
|
|
157
|
+
// Parse path:line or path:start-end shortcut
|
|
158
|
+
let startLine: number | undefined;
|
|
159
|
+
let endLine: number | undefined;
|
|
160
|
+
|
|
161
|
+
const rangeMatch = fileArg.match(/^(.+?):(\d+)(?:-(\d+))?$/);
|
|
162
|
+
if (rangeMatch) {
|
|
163
|
+
const relPath = rangeMatch[1];
|
|
164
|
+
const resolved = path.resolve(context.directory, relPath);
|
|
165
|
+
startLine = parseInt(rangeMatch[2], 10);
|
|
166
|
+
endLine = rangeMatch[3] ? parseInt(rangeMatch[3], 10) : startLine;
|
|
167
|
+
return readFileRange(resolved, startLine, endLine, relPath);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (!existsSync(fullFilePath)) return `File not found: ${fileArg}`;
|
|
171
|
+
const stats = statSync(fullFilePath);
|
|
172
|
+
|
|
173
|
+
// If section specified, try grep for symbol/heading
|
|
174
|
+
if (args.section) {
|
|
175
|
+
const section = String(args.section);
|
|
176
|
+
// Check if it's a line range
|
|
177
|
+
const lineMatch = section.match(/^(\d+)(?:-(\d+))?$/);
|
|
178
|
+
if (lineMatch) {
|
|
179
|
+
const start = parseInt(lineMatch[1], 10);
|
|
180
|
+
const end = lineMatch[2] ? parseInt(lineMatch[2], 10) : start + 30;
|
|
181
|
+
return readFileRange(fullFilePath, start, end);
|
|
182
|
+
}
|
|
183
|
+
// Symbol/heading: use grep to find location, then read around it
|
|
184
|
+
const grepArgs = [
|
|
185
|
+
"-n",
|
|
186
|
+
"--color=never",
|
|
187
|
+
"-E",
|
|
188
|
+
`^(function |const |let |class |interface |type |export |## |### )?.*${section}`,
|
|
189
|
+
fullFilePath,
|
|
190
|
+
];
|
|
191
|
+
const result = run("grep", grepArgs);
|
|
192
|
+
if (result.stdout) {
|
|
193
|
+
const firstMatch = result.stdout.split("\n")[0];
|
|
194
|
+
const lineNum = parseInt(firstMatch.split(":")[0], 10);
|
|
195
|
+
if (!isNaN(lineNum)) {
|
|
196
|
+
return readFileRange(fullFilePath, Math.max(1, lineNum - 3), lineNum + 40);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return `Section "${section}" not found in ${fileArg}`;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Full content for small files
|
|
203
|
+
if (stats.size < 50 * 1024 || args.full) {
|
|
204
|
+
const content = readFileSync(fullFilePath, "utf-8");
|
|
205
|
+
const lines = content.split("\n");
|
|
206
|
+
if (lines.length > 2000)
|
|
207
|
+
return `[File too large: ${plural(lines.length, "line")}. Showing first 2000 lines]\n\n${lines.slice(0, 2000).join("\n")}`;
|
|
208
|
+
return content;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Large file: outline
|
|
212
|
+
const content = readFileSync(fullFilePath, "utf-8");
|
|
213
|
+
const lines = content.split("\n");
|
|
214
|
+
const headings: string[] = [];
|
|
215
|
+
for (let i = 0; i < Math.min(lines.length, 5000); i++) {
|
|
216
|
+
const l = lines[i].trim();
|
|
217
|
+
if (
|
|
218
|
+
l.match(
|
|
219
|
+
/^(export\s+)?(function|class|interface|type|const|enum|def|struct|impl|pub\s+fn)\s/,
|
|
220
|
+
) ||
|
|
221
|
+
l.match(/^(##|###)\s/) ||
|
|
222
|
+
l.match(/^\w+\s*[:=]/)
|
|
223
|
+
) {
|
|
224
|
+
headings.push(` ${i + 1}: ${l.slice(0, 120)}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const header = `[File: ${fileArg} — ${plural(lines.length, "line")}, ${(stats.size / 1024).toFixed(1)}KB]\n\n`;
|
|
228
|
+
const outline =
|
|
229
|
+
headings.length > 0
|
|
230
|
+
? `Outline (${plural(headings.length, "entry")}):\n${headings.slice(0, 50).join("\n")}\n\nUse path:line or section to read a specific range.`
|
|
231
|
+
: `Use path:line to read a specific range (e.g., ${fileArg}:1-${Math.min(50, lines.length)}).`;
|
|
232
|
+
return header + outline;
|
|
233
|
+
},
|
|
234
|
+
}),
|
|
235
|
+
|
|
236
|
+
// -----------------------------------------------------------------------
|
|
237
|
+
// srcwalk_files: Find files
|
|
238
|
+
// -----------------------------------------------------------------------
|
|
239
|
+
srcwalk_files: tool({
|
|
240
|
+
description: `Find files by glob pattern. Returns matched file paths with size estimates, grouped by directory. Respects .gitignore.`,
|
|
241
|
+
args: {
|
|
242
|
+
pattern: tool.schema.string().describe("Glob pattern (e.g. '*.ts', 'src/**/*.ts')"),
|
|
243
|
+
scope: tool.schema
|
|
244
|
+
.string()
|
|
245
|
+
.optional()
|
|
246
|
+
.describe("Directory to search (default: project root)"),
|
|
247
|
+
},
|
|
248
|
+
execute: async (args, context) => {
|
|
249
|
+
const pattern = String(args.pattern ?? "").trim();
|
|
250
|
+
if (!pattern) return "❌ pattern is required.";
|
|
251
|
+
const scopeDir = path.resolve(context.directory, args.scope ? String(args.scope) : "");
|
|
252
|
+
|
|
253
|
+
// Use find with simple glob pattern
|
|
254
|
+
const findArgs: string[] = [scopeDir, "-type", "f"];
|
|
255
|
+
|
|
256
|
+
// Convert simple glob to -name pattern
|
|
257
|
+
if (pattern.includes("**")) {
|
|
258
|
+
// find handles ** naturally with -path
|
|
259
|
+
findArgs.push("-path", `*/${pattern.replace(/\*\*/g, "*")}`);
|
|
260
|
+
} else if (pattern.includes("*")) {
|
|
261
|
+
findArgs.push("-name", pattern);
|
|
262
|
+
} else if (pattern.includes(".")) {
|
|
263
|
+
findArgs.push("-name", pattern);
|
|
264
|
+
} else {
|
|
265
|
+
findArgs.push("-name", `*${pattern}*`);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Exclude common dirs
|
|
269
|
+
for (const dir of [
|
|
270
|
+
".git",
|
|
271
|
+
"node_modules",
|
|
272
|
+
"dist",
|
|
273
|
+
"build",
|
|
274
|
+
"coverage",
|
|
275
|
+
".next",
|
|
276
|
+
".opencode",
|
|
277
|
+
]) {
|
|
278
|
+
findArgs.push("-not", "-path", `*/${dir}/*`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const result = run("find", findArgs);
|
|
282
|
+
const files = result.stdout.split("\n").filter(Boolean).slice(0, 200);
|
|
283
|
+
|
|
284
|
+
if (files.length === 0) return `No files matching "${pattern}" found.`;
|
|
285
|
+
|
|
286
|
+
// Group by directory
|
|
287
|
+
const groups: Record<string, string[]> = {};
|
|
288
|
+
for (const f of files) {
|
|
289
|
+
const dir = path.dirname(f);
|
|
290
|
+
if (!groups[dir]) groups[dir] = [];
|
|
291
|
+
groups[dir].push(path.basename(f));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const lines: string[] = [`# Files matching "${pattern}" (${plural(files.length, "file")})\n`];
|
|
295
|
+
for (const [dir, files] of Object.entries(groups).sort()) {
|
|
296
|
+
const relDir = path.relative(context.directory, dir) || ".";
|
|
297
|
+
lines.push(`${relDir}/ (${plural(files.length, "file")})`);
|
|
298
|
+
for (const f of files) {
|
|
299
|
+
const fp = path.join(dir, f);
|
|
300
|
+
try {
|
|
301
|
+
const size = statSync(fp).size;
|
|
302
|
+
const tokenEst = Math.ceil(size / 4);
|
|
303
|
+
lines.push(` ${f} (~${tokenEst.toLocaleString()} tokens)`);
|
|
304
|
+
} catch {
|
|
305
|
+
lines.push(` ${f}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
lines.push("");
|
|
309
|
+
}
|
|
310
|
+
return lines.join("\n");
|
|
311
|
+
},
|
|
312
|
+
}),
|
|
313
|
+
|
|
314
|
+
// -----------------------------------------------------------------------
|
|
315
|
+
// srcwalk_deps: Import analysis
|
|
316
|
+
// -----------------------------------------------------------------------
|
|
317
|
+
srcwalk_deps: tool({
|
|
318
|
+
description: `Show what imports a file (dependents) and what a file imports (dependencies).\nBlast-radius check before breaking changes.`,
|
|
319
|
+
args: {
|
|
320
|
+
path: tool.schema.string().describe("File path to analyze"),
|
|
321
|
+
scope: tool.schema.string().optional().describe("Search scope (default: project root)"),
|
|
322
|
+
},
|
|
323
|
+
execute: async (args, context) => {
|
|
324
|
+
const filePath = String(args.path);
|
|
325
|
+
const absPath = path.resolve(context.directory, filePath);
|
|
326
|
+
if (!existsSync(absPath)) return `File not found: ${filePath}`;
|
|
327
|
+
|
|
328
|
+
const scopeDir = path.resolve(context.directory, args.scope ? String(args.scope) : "");
|
|
329
|
+
const fileName = path.basename(filePath, path.extname(filePath));
|
|
330
|
+
|
|
331
|
+
// What this file imports
|
|
332
|
+
const content = readFileSync(absPath, "utf-8");
|
|
333
|
+
const importLines: string[] = [];
|
|
334
|
+
for (const line of content.split("\n")) {
|
|
335
|
+
const m = line.match(
|
|
336
|
+
/(?:import|require)\s+.*?from\s+['"]([^'"]+)['"]|import\s+['"]([^'"]+)['"]/,
|
|
337
|
+
);
|
|
338
|
+
if (m) importLines.push(` ${line.trim()}`);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// What imports this file (grep for the module name)
|
|
342
|
+
const searchName = fileName;
|
|
343
|
+
const grepResult = run("grep", [
|
|
344
|
+
"-rn",
|
|
345
|
+
"--color=never",
|
|
346
|
+
"-E",
|
|
347
|
+
`from ['"](\\./|\\.\\./|.*/)${searchName}['"]|require\\(['"](\\./|\\.\\./|.*/)${searchName}['"]`,
|
|
348
|
+
scopeDir,
|
|
349
|
+
"--include=*.ts",
|
|
350
|
+
"--include=*.tsx",
|
|
351
|
+
"--include=*.js",
|
|
352
|
+
"--include=*.jsx",
|
|
353
|
+
"--include=*.mjs",
|
|
354
|
+
]);
|
|
355
|
+
const importers = grepResult.stdout.split("\n").filter(Boolean).slice(0, 30);
|
|
356
|
+
|
|
357
|
+
const result: string[] = [`## Dependencies for ${filePath}\n`];
|
|
358
|
+
result.push(`### Imports (${plural(importLines.length, "module")})`);
|
|
359
|
+
if (importLines.length === 0) result.push(" (none)");
|
|
360
|
+
else result.push(...importLines);
|
|
361
|
+
|
|
362
|
+
result.push(`\n### Importers (${plural(importers.length, "file")})`);
|
|
363
|
+
if (importers.length === 0) result.push(" (no files import this module)");
|
|
364
|
+
else
|
|
365
|
+
result.push(
|
|
366
|
+
...importers.map(
|
|
367
|
+
(l) => ` ${path.relative(context.directory, l.split(":")[0])}:${l.split(":")[1]}`,
|
|
368
|
+
),
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
return result.join("\n");
|
|
372
|
+
},
|
|
373
|
+
}),
|
|
374
|
+
|
|
375
|
+
// -----------------------------------------------------------------------
|
|
376
|
+
// srcwalk_map: Directory overview
|
|
377
|
+
// -----------------------------------------------------------------------
|
|
378
|
+
srcwalk_map: tool({
|
|
379
|
+
description: `Token-annotated directory skeleton. Shows repo structure with file sizes and token estimates. Good for understanding codebase shape.`,
|
|
380
|
+
args: {
|
|
381
|
+
scope: tool.schema.string().optional().describe("Directory to map (default: project root)"),
|
|
382
|
+
depth: tool.schema.number().optional().describe("Max directory depth (default: 3)"),
|
|
383
|
+
},
|
|
384
|
+
execute: async (args, context) => {
|
|
385
|
+
const scopeDir = path.resolve(context.directory, args.scope ? String(args.scope) : "");
|
|
386
|
+
const maxDepth = args.depth ?? 3;
|
|
387
|
+
|
|
388
|
+
const treeArgs = ["-L", String(maxDepth), "--dirsfirst"];
|
|
389
|
+
// Use tree if available, else ls -R
|
|
390
|
+
const treeResult = run("tree", [
|
|
391
|
+
...treeArgs,
|
|
392
|
+
"-I",
|
|
393
|
+
".git|node_modules|dist|build|coverage|.next",
|
|
394
|
+
scopeDir,
|
|
395
|
+
]);
|
|
396
|
+
if (treeResult.code === 0) {
|
|
397
|
+
return treeResult.stdout.slice(0, 10_000);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Fallback: simple directory listing
|
|
401
|
+
const result: string[] = [
|
|
402
|
+
`## Directory: ${path.relative(context.directory, scopeDir) || "."}\n`,
|
|
403
|
+
];
|
|
404
|
+
await listDirRecursive(scopeDir, "", maxDepth, result);
|
|
405
|
+
return result.join("\n");
|
|
406
|
+
},
|
|
407
|
+
}),
|
|
408
|
+
|
|
409
|
+
// -----------------------------------------------------------------------
|
|
410
|
+
// srcwalk_callers: Reverse call graph
|
|
411
|
+
// -----------------------------------------------------------------------
|
|
412
|
+
srcwalk_callers: tool({
|
|
413
|
+
description: `Reverse call graph — find what calls a function.\nGrep-based: searches for symbol usage across the codebase.\nUse depth for transitive callers (multi-hop).`,
|
|
414
|
+
args: {
|
|
415
|
+
symbol: tool.schema.string().describe("Function/symbol name"),
|
|
416
|
+
scope: tool.schema.string().optional().describe("Search scope"),
|
|
417
|
+
depth: tool.schema.number().optional().describe("BFS hop depth (default: 1, max: 3)"),
|
|
418
|
+
filter: tool.schema.string().optional().describe("Optional filter (e.g. path:api)"),
|
|
419
|
+
},
|
|
420
|
+
execute: async (args, context) => {
|
|
421
|
+
const symbol = String(args.symbol ?? "").trim();
|
|
422
|
+
if (!symbol) return "❌ symbol is required.";
|
|
423
|
+
const scopeDir = path.resolve(context.directory, args.scope ? String(args.scope) : "");
|
|
424
|
+
const depth = Math.min(args.depth ?? 1, 3);
|
|
425
|
+
|
|
426
|
+
// Search for direct calls: symbol( or .symbol or symbol.
|
|
427
|
+
const grepArgs = [
|
|
428
|
+
"-rn",
|
|
429
|
+
"--color=never",
|
|
430
|
+
"-E",
|
|
431
|
+
`[.\\s]${symbol}\\s*\\(|[.]${symbol}\\b|\\b${symbol}\\.`,
|
|
432
|
+
scopeDir,
|
|
433
|
+
"--include=*.ts",
|
|
434
|
+
"--include=*.tsx",
|
|
435
|
+
"--include=*.js",
|
|
436
|
+
"--include=*.jsx",
|
|
437
|
+
];
|
|
438
|
+
|
|
439
|
+
if (args.filter) {
|
|
440
|
+
const filterStr = String(args.filter);
|
|
441
|
+
if (filterStr.startsWith("path:")) {
|
|
442
|
+
grepArgs.push(path.join(scopeDir, filterStr.slice(5)));
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const result = run("grep", grepArgs);
|
|
447
|
+
const lines = result.stdout.split("\n").filter(Boolean).slice(0, 50);
|
|
448
|
+
|
|
449
|
+
if (lines.length === 0) return `No callers found for "${symbol}".`;
|
|
450
|
+
|
|
451
|
+
const output: string[] = [
|
|
452
|
+
`## Callers of \`${symbol}\` (${plural(lines.length, "result")})${depth > 1 ? ` (depth: ${depth})` : ""}\n`,
|
|
453
|
+
];
|
|
454
|
+
for (const line of lines) {
|
|
455
|
+
const parts = line.split(":");
|
|
456
|
+
if (parts.length >= 2) {
|
|
457
|
+
const relPath = path.relative(context.directory, parts[0]);
|
|
458
|
+
output.push(` ${relPath}:${parts[1]}: ${parts.slice(2).join(":").trim().slice(0, 150)}`);
|
|
459
|
+
} else {
|
|
460
|
+
output.push(` ${line.slice(0, 200)}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (depth > 1) {
|
|
465
|
+
output.push(`\n_Note: Multi-hop depth (${depth}) requires re-running on each caller._`);
|
|
466
|
+
}
|
|
467
|
+
return output.join("\n");
|
|
468
|
+
},
|
|
469
|
+
}),
|
|
470
|
+
|
|
471
|
+
// -----------------------------------------------------------------------
|
|
472
|
+
// srcwalk_callees: Forward call graph
|
|
473
|
+
// -----------------------------------------------------------------------
|
|
474
|
+
srcwalk_callees: tool({
|
|
475
|
+
description: `Forward call graph — what does this function call?\nReads the function body and extracts call sites.`,
|
|
476
|
+
args: {
|
|
477
|
+
symbol: tool.schema.string().describe("Function/symbol name"),
|
|
478
|
+
scope: tool.schema.string().optional().describe("Scope directory"),
|
|
479
|
+
detailed: tool.schema
|
|
480
|
+
.boolean()
|
|
481
|
+
.optional()
|
|
482
|
+
.describe("Show ordered call sites with argument slots"),
|
|
483
|
+
},
|
|
484
|
+
execute: async (args, context) => {
|
|
485
|
+
const symbol = String(args.symbol ?? "").trim();
|
|
486
|
+
if (!symbol) return "❌ symbol is required.";
|
|
487
|
+
const scopeDir = path.resolve(context.directory, args.scope ? String(args.scope) : "");
|
|
488
|
+
|
|
489
|
+
// Find the function definition
|
|
490
|
+
const defArgs = [
|
|
491
|
+
"-rn",
|
|
492
|
+
"--color=never",
|
|
493
|
+
"-E",
|
|
494
|
+
`(export\\s+)?(function|const|let|async\\s+function)\\s+${symbol}\\b|${symbol}\\s*[:=]\\s*(async\\s+)?\\(`,
|
|
495
|
+
scopeDir,
|
|
496
|
+
"--include=*.ts",
|
|
497
|
+
"--include=*.tsx",
|
|
498
|
+
"--include=*.js",
|
|
499
|
+
"--include=*.jsx",
|
|
500
|
+
];
|
|
501
|
+
const defResult = run("grep", defArgs);
|
|
502
|
+
const defLines = defResult.stdout.split("\n").filter(Boolean).slice(0, 5);
|
|
503
|
+
|
|
504
|
+
if (defLines.length === 0) {
|
|
505
|
+
return `Definition not found for "${symbol}". Cannot trace callees without finding the function body.`;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const output: string[] = [`## Callees of \`${symbol}\`\n`];
|
|
509
|
+
|
|
510
|
+
// For each definition location, show the function and extract calls
|
|
511
|
+
for (const def of defLines) {
|
|
512
|
+
const parts = def.split(":");
|
|
513
|
+
if (parts.length >= 2) {
|
|
514
|
+
const relPath = path.relative(context.directory, parts[0]);
|
|
515
|
+
const lineNum = parseInt(parts[1], 10);
|
|
516
|
+
output.push(`**Definition:** ${relPath}:${lineNum}`);
|
|
517
|
+
|
|
518
|
+
// Read function body to extract calls
|
|
519
|
+
const filePath = path.resolve(context.directory, parts[0]);
|
|
520
|
+
if (existsSync(filePath)) {
|
|
521
|
+
const fileLines = readFileSync(filePath, "utf-8").split("\n");
|
|
522
|
+
let braceCount = 0;
|
|
523
|
+
let inFunc = false;
|
|
524
|
+
const calls: string[] = [];
|
|
525
|
+
|
|
526
|
+
for (let i = lineNum - 1; i < Math.min(lineNum + 80, fileLines.length); i++) {
|
|
527
|
+
const line = fileLines[i];
|
|
528
|
+
if (!inFunc) {
|
|
529
|
+
if (line.includes("{")) {
|
|
530
|
+
inFunc = true;
|
|
531
|
+
braceCount = (line.match(/{/g) || []).length;
|
|
532
|
+
braceCount -= (line.match(/}/g) || []).length;
|
|
533
|
+
}
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
braceCount += (line.match(/{/g) || []).length;
|
|
537
|
+
braceCount -= (line.match(/}/g) || []).length;
|
|
538
|
+
|
|
539
|
+
// Extract function calls
|
|
540
|
+
const callMatch = [...line.matchAll(/(?<![.\w])(\w+)\s*\(/g)];
|
|
541
|
+
for (const m of callMatch) {
|
|
542
|
+
const name = m[1];
|
|
543
|
+
if (
|
|
544
|
+
![
|
|
545
|
+
"if",
|
|
546
|
+
"for",
|
|
547
|
+
"while",
|
|
548
|
+
"switch",
|
|
549
|
+
"catch",
|
|
550
|
+
"typeof",
|
|
551
|
+
"instanceof",
|
|
552
|
+
"return",
|
|
553
|
+
"throw",
|
|
554
|
+
"new",
|
|
555
|
+
"delete",
|
|
556
|
+
"await",
|
|
557
|
+
"yield",
|
|
558
|
+
].includes(name)
|
|
559
|
+
) {
|
|
560
|
+
const argStart = line.indexOf("(", m.index!);
|
|
561
|
+
const argEnd = line.indexOf(")", argStart);
|
|
562
|
+
const args_s =
|
|
563
|
+
argEnd > argStart
|
|
564
|
+
? line
|
|
565
|
+
.slice(argStart + 1, argEnd)
|
|
566
|
+
.trim()
|
|
567
|
+
.slice(0, 60)
|
|
568
|
+
: "";
|
|
569
|
+
calls.push(` ${args.detailed ? `${name}(${args_s})` : `${name}()`}`);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (braceCount <= 0) break;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (calls.length > 0) {
|
|
577
|
+
output.push(...calls);
|
|
578
|
+
} else {
|
|
579
|
+
output.push(" (no internal calls found)");
|
|
580
|
+
}
|
|
581
|
+
output.push("");
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return output.join("\n");
|
|
587
|
+
},
|
|
588
|
+
}),
|
|
589
|
+
|
|
590
|
+
// -----------------------------------------------------------------------
|
|
591
|
+
// srcwalk_flow: Compact orientation
|
|
592
|
+
// -----------------------------------------------------------------------
|
|
593
|
+
srcwalk_flow: tool({
|
|
594
|
+
description: `Compact function orientation — ordered callees + direct callers.\nQuick understanding of a function's role in the call graph.`,
|
|
595
|
+
args: {
|
|
596
|
+
symbol: tool.schema.string().describe("Symbol name to analyze"),
|
|
597
|
+
scope: tool.schema.string().optional().describe("Search scope"),
|
|
598
|
+
},
|
|
599
|
+
execute: async (args, context) => {
|
|
600
|
+
const symbol = String(args.symbol ?? "").trim();
|
|
601
|
+
if (!symbol) return "❌ symbol is required.";
|
|
602
|
+
const scopeDir = path.resolve(context.directory, args.scope ? String(args.scope) : "");
|
|
603
|
+
|
|
604
|
+
// Get callers
|
|
605
|
+
const callersResult = run("grep", [
|
|
606
|
+
"-rn",
|
|
607
|
+
"--color=never",
|
|
608
|
+
"-E",
|
|
609
|
+
`[.\\s]${symbol}\\s*\\(|[.]${symbol}\\b`,
|
|
610
|
+
scopeDir,
|
|
611
|
+
"--include=*.ts",
|
|
612
|
+
"--include=*.tsx",
|
|
613
|
+
"--include=*.js",
|
|
614
|
+
"--include=*.jsx",
|
|
615
|
+
]);
|
|
616
|
+
const callers = callersResult.stdout.split("\n").filter(Boolean).slice(0, 15);
|
|
617
|
+
|
|
618
|
+
// Get callees by reading function body
|
|
619
|
+
const defResult = run("grep", [
|
|
620
|
+
"-rn",
|
|
621
|
+
"--color=never",
|
|
622
|
+
"-E",
|
|
623
|
+
`(function|const|let)\\s+${symbol}\\b|${symbol}\\s*[:=]\\s*(async\\s+)?\\(`,
|
|
624
|
+
scopeDir,
|
|
625
|
+
"--include=*.ts",
|
|
626
|
+
"--include=*.tsx",
|
|
627
|
+
"--include=*.js",
|
|
628
|
+
"--include=*.jsx",
|
|
629
|
+
]);
|
|
630
|
+
const defLine = defResult.stdout.split("\n").filter(Boolean)[0];
|
|
631
|
+
let callees: string[] = [];
|
|
632
|
+
|
|
633
|
+
if (defLine) {
|
|
634
|
+
const parts = defLine.split(":");
|
|
635
|
+
if (parts.length >= 2) {
|
|
636
|
+
const fp = path.resolve(context.directory, parts[0]);
|
|
637
|
+
const ln = parseInt(parts[1], 10);
|
|
638
|
+
if (existsSync(fp)) {
|
|
639
|
+
const fileLines = readFileSync(fp, "utf-8").split("\n");
|
|
640
|
+
let bc = 0,
|
|
641
|
+
inF = false;
|
|
642
|
+
for (let i = ln - 1; i < Math.min(ln + 60, fileLines.length); i++) {
|
|
643
|
+
const l = fileLines[i];
|
|
644
|
+
if (!inF) {
|
|
645
|
+
if (l.includes("{")) {
|
|
646
|
+
inF = true;
|
|
647
|
+
bc = (l.match(/{/g) || []).length - (l.match(/}/g) || []).length;
|
|
648
|
+
}
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
bc += (l.match(/{/g) || []).length - (l.match(/}/g) || []).length;
|
|
652
|
+
for (const m of l.matchAll(/(\w+)\s*\(/g)) {
|
|
653
|
+
if (
|
|
654
|
+
![
|
|
655
|
+
"if",
|
|
656
|
+
"for",
|
|
657
|
+
"while",
|
|
658
|
+
"switch",
|
|
659
|
+
"catch",
|
|
660
|
+
"typeof",
|
|
661
|
+
"instanceof",
|
|
662
|
+
"return",
|
|
663
|
+
"throw",
|
|
664
|
+
"new",
|
|
665
|
+
"delete",
|
|
666
|
+
"await",
|
|
667
|
+
"yield",
|
|
668
|
+
].includes(m[1])
|
|
669
|
+
)
|
|
670
|
+
callees.push(m[1]);
|
|
671
|
+
}
|
|
672
|
+
if (bc <= 0) break;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const output: string[] = [`## Flow: \`${symbol}\``];
|
|
679
|
+
output.push(`\n**Callers (${plural(callers.length, "file")}):`);
|
|
680
|
+
if (callers.length === 0) output.push(" (none)");
|
|
681
|
+
else
|
|
682
|
+
output.push(
|
|
683
|
+
...callers
|
|
684
|
+
.slice(0, 10)
|
|
685
|
+
.map(
|
|
686
|
+
(l) => ` ${path.relative(context.directory, l.split(":")[0])}:${l.split(":")[1]}`,
|
|
687
|
+
),
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
output.push(`\n**Callees (${plural(callees.length, "call")}):`);
|
|
691
|
+
if (callees.length === 0) output.push(" (none)");
|
|
692
|
+
else output.push(...[...new Set(callees)].slice(0, 20).map((c) => ` ${c}()`));
|
|
693
|
+
return output.join("\n");
|
|
694
|
+
},
|
|
695
|
+
}),
|
|
696
|
+
|
|
697
|
+
// -----------------------------------------------------------------------
|
|
698
|
+
// srcwalk_impact: Heuristic blast-radius
|
|
699
|
+
// -----------------------------------------------------------------------
|
|
700
|
+
srcwalk_impact: tool({
|
|
701
|
+
description: `Heuristic blast-radius triage — broad 'what might be affected?' starting point.\nName-matched, not proof. Use as starting point before verifying with srcwalk_callers or exact reads.`,
|
|
702
|
+
args: {
|
|
703
|
+
symbol: tool.schema.string().describe("Symbol name to triage"),
|
|
704
|
+
scope: tool.schema.string().optional().describe("Search scope"),
|
|
705
|
+
},
|
|
706
|
+
execute: async (args, context) => {
|
|
707
|
+
const symbol = String(args.symbol ?? "").trim();
|
|
708
|
+
if (!symbol) return "❌ symbol is required.";
|
|
709
|
+
const scopeDir = path.resolve(context.directory, args.scope ? String(args.scope) : "");
|
|
710
|
+
|
|
711
|
+
// Count usages per directory
|
|
712
|
+
const grepResult = run("grep", [
|
|
713
|
+
"-rn",
|
|
714
|
+
"--color=never",
|
|
715
|
+
"-E",
|
|
716
|
+
`\\b${symbol}\\b`,
|
|
717
|
+
scopeDir,
|
|
718
|
+
"--include=*.ts",
|
|
719
|
+
"--include=*.tsx",
|
|
720
|
+
"--include=*.js",
|
|
721
|
+
"--include=*.jsx",
|
|
722
|
+
]);
|
|
723
|
+
const lines = grepResult.stdout.split("\n").filter(Boolean);
|
|
724
|
+
|
|
725
|
+
// Group by file
|
|
726
|
+
const fileCounts: Record<string, number> = {};
|
|
727
|
+
for (const line of lines) {
|
|
728
|
+
const filePath = line.split(":")[0];
|
|
729
|
+
fileCounts[filePath] = (fileCounts[filePath] || 0) + 1;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// Group by directory
|
|
733
|
+
const dirCounts: Record<string, { files: number; total: number }> = {};
|
|
734
|
+
for (const [filePath, count] of Object.entries(fileCounts)) {
|
|
735
|
+
const dir = path.dirname(filePath);
|
|
736
|
+
if (!dirCounts[dir]) dirCounts[dir] = { files: 0, total: 0 };
|
|
737
|
+
dirCounts[dir].files++;
|
|
738
|
+
dirCounts[dir].total += count;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
const totalOccurrences = lines.length;
|
|
742
|
+
const totalFiles = Object.keys(fileCounts).length;
|
|
743
|
+
|
|
744
|
+
const output: string[] = [
|
|
745
|
+
`## Impact: \`${symbol}\``,
|
|
746
|
+
`Total: ${plural(totalOccurrences, "occurrence")} across ${plural(totalFiles, "file")}\n`,
|
|
747
|
+
`### By directory`,
|
|
748
|
+
];
|
|
749
|
+
|
|
750
|
+
const sortedDirs = Object.entries(dirCounts).sort((a, b) => b[1].total - a[1].total);
|
|
751
|
+
for (const [dir, info] of sortedDirs.slice(0, 15)) {
|
|
752
|
+
const relDir = path.relative(context.directory, dir) || ".";
|
|
753
|
+
output.push(
|
|
754
|
+
` ${relDir}/ — ${plural(info.total, "occurrence")} in ${plural(info.files, "file")}`,
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const topFiles = Object.entries(fileCounts)
|
|
759
|
+
.sort((a, b) => b[1] - a[1])
|
|
760
|
+
.slice(0, 20);
|
|
761
|
+
output.push(`\n### Top files`);
|
|
762
|
+
for (const [filePath, count] of topFiles) {
|
|
763
|
+
const relPath = path.relative(context.directory, filePath);
|
|
764
|
+
output.push(` ${relPath} (${plural(count, "occurrence")})`);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
output.push(
|
|
768
|
+
`\n_Heuristic: name-matched, not proof. Follow up with srcwalk_callers for exact call sites._`,
|
|
769
|
+
);
|
|
770
|
+
return output.join("\n");
|
|
771
|
+
},
|
|
772
|
+
}),
|
|
665
773
|
};
|
|
666
774
|
|
|
667
775
|
// ---------------------------------------------------------------------------
|
|
@@ -669,53 +777,59 @@ const srcwalkTools = {
|
|
|
669
777
|
// ---------------------------------------------------------------------------
|
|
670
778
|
|
|
671
779
|
function readFileRange(filePath: string, start: number, end: number, displayPath?: string): string {
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
780
|
+
try {
|
|
781
|
+
const content = readFileSync(filePath, "utf-8");
|
|
782
|
+
const lines = content.split("\n");
|
|
783
|
+
const from = Math.max(0, start - 1);
|
|
784
|
+
const to = Math.min(lines.length, end);
|
|
785
|
+
const result: string[] = [`File: ${displayPath ?? filePath} (lines ${start}-${end}):\n`];
|
|
786
|
+
for (let i = from; i < to; i++) {
|
|
787
|
+
result.push(`${i + 1}: ${lines[i]}`);
|
|
788
|
+
}
|
|
789
|
+
return result.join("\n");
|
|
790
|
+
} catch (err) {
|
|
791
|
+
return `Error reading file: ${err instanceof Error ? err.message : String(err)}`;
|
|
792
|
+
}
|
|
685
793
|
}
|
|
686
794
|
|
|
687
|
-
async function listDirRecursive(
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
795
|
+
async function listDirRecursive(
|
|
796
|
+
dir: string,
|
|
797
|
+
prefix: string,
|
|
798
|
+
maxDepth: number,
|
|
799
|
+
output: string[],
|
|
800
|
+
): Promise<void> {
|
|
801
|
+
if (maxDepth <= 0) return;
|
|
802
|
+
try {
|
|
803
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
804
|
+
const skipDirs = new Set([".git", "node_modules", "dist", "build", "coverage", ".next"]);
|
|
805
|
+
const dirs = entries
|
|
806
|
+
.filter((e) => e.isDirectory() && !skipDirs.has(e.name))
|
|
807
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
808
|
+
const files = entries.filter((e) => e.isFile()).sort((a, b) => a.name.localeCompare(b.name));
|
|
809
|
+
|
|
810
|
+
for (const d of dirs) {
|
|
811
|
+
output.push(`${prefix}${d.name}/`);
|
|
812
|
+
await listDirRecursive(path.join(dir, d.name), prefix + " ", maxDepth - 1, output);
|
|
813
|
+
}
|
|
814
|
+
for (const f of files) {
|
|
815
|
+
const fp = path.join(dir, f.name);
|
|
816
|
+
try {
|
|
817
|
+
const size = statSync(fp).size;
|
|
818
|
+
const tokenEst = Math.ceil(size / 4);
|
|
819
|
+
output.push(`${prefix}${f.name} (~${tokenEst.toLocaleString()} tokens)`);
|
|
820
|
+
} catch {
|
|
821
|
+
output.push(`${prefix}${f.name}`);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
} catch {
|
|
825
|
+
// Permission denied, skip
|
|
826
|
+
}
|
|
713
827
|
}
|
|
714
828
|
|
|
715
829
|
export const SrcwalkPlugin: Plugin = async () => {
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
830
|
+
return {
|
|
831
|
+
tool: srcwalkTools,
|
|
832
|
+
};
|
|
719
833
|
};
|
|
720
834
|
|
|
721
835
|
export default SrcwalkPlugin;
|