pi-gitnexus-fork 0.7.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/.github/workflows/ci.yml +20 -0
- package/.gitnexusignore +11 -0
- package/.sg-rules/async-function-must-await-or-return.yml +55 -0
- package/.sg-rules/catch-must-log-error.yml +78 -0
- package/.sg-rules/class-must-implement-or-extend.yml +61 -0
- package/.sg-rules/class-property-must-be-readonly.yml +61 -0
- package/.sg-rules/error-must-extend-base.yml +56 -0
- package/.sg-rules/generic-must-be-constrained.yml +60 -0
- package/.sg-rules/import-reexport-risk.yml +9 -0
- package/.sg-rules/missing-session-id-in-api.yml +16 -0
- package/.sg-rules/no-any-in-generic-args.yml +57 -0
- package/.sg-rules/no-await-in-promise-all.yml +28 -0
- package/.sg-rules/no-barrel-export.yml +17 -0
- package/.sg-rules/no-bq-write-in-module.yml +65 -0
- package/.sg-rules/no-console-except-error.yml +27 -0
- package/.sg-rules/no-console-in-server.yml +42 -0
- package/.sg-rules/no-empty-catch.yml +20 -0
- package/.sg-rules/no-empty-function.yml +24 -0
- package/.sg-rules/no-eval.yml +28 -0
- package/.sg-rules/no-explicit-any.yml +34 -0
- package/.sg-rules/no-hardcoded-placeholder-string.yml +23 -0
- package/.sg-rules/no-hardcoded-secrets.yml +32 -0
- package/.sg-rules/no-innerHTML.yml +22 -0
- package/.sg-rules/no-json-parse-without-trycatch.yml +33 -0
- package/.sg-rules/no-magic-numbers.yml +25 -0
- package/.sg-rules/no-nested-ternary.yml +21 -0
- package/.sg-rules/no-non-null-assertion.yml +25 -0
- package/.sg-rules/no-stub-implementation.yml +44 -0
- package/.sg-rules/no-throw-literal.yml +50 -0
- package/.sg-rules/no-todo-comment.yml +24 -0
- package/.sg-rules/no-ts-ignore-comment.yml +48 -0
- package/.sg-rules/no-type-assertion-in-jsx.yml +23 -0
- package/.sg-rules/no-unguarded-trim.yml +24 -0
- package/.sg-rules/no-unknown-without-narrowing.yml +76 -0
- package/.sg-rules/no-unsafe-bracket-access.yml +58 -0
- package/.sg-rules/no-unsafe-type-assertion.yml +45 -0
- package/.sg-rules/switch-must-be-exhaustive.yml +62 -0
- package/.sg-rules/zod-async-refine-without-abort.yml +62 -0
- package/.sg-rules/zod-enum-unsafe-access.yml +59 -0
- package/.sg-rules/zod-nested-object-deep-path.yml +70 -0
- package/.sg-rules/zod-optional-without-default-in-route.yml +50 -0
- package/.sg-rules/zod-parse-not-safe.yml +42 -0
- package/.sg-rules/zod-preprocess-without-fallback.yml +58 -0
- package/.sg-rules/zod-refine-no-return-undefined.yml +54 -0
- package/.sg-rules/zod-transform-without-output-type.yml +52 -0
- package/.sg-sha +1 -0
- package/.sgignore +4 -0
- package/AGENTS.md +1 -0
- package/CHANGELOG.md +99 -0
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/biome.json +25 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +890 -0
- package/coverage/coverage-final.json +12 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/augment-remote.ts.html +274 -0
- package/coverage/src/gitnexus.ts.html +1363 -0
- package/coverage/src/index.html +236 -0
- package/coverage/src/index.ts.html +1561 -0
- package/coverage/src/mcp-client-factory.ts.html +367 -0
- package/coverage/src/mcp-client-stdio.ts.html +736 -0
- package/coverage/src/mcp-client.ts.html +568 -0
- package/coverage/src/remote-mcp-client.ts.html +709 -0
- package/coverage/src/repo-resolver.ts.html +526 -0
- package/coverage/src/tools.ts.html +970 -0
- package/coverage/src/ui/index.html +131 -0
- package/coverage/src/ui/main-menu.ts.html +502 -0
- package/coverage/src/ui/settings-menu.ts.html +460 -0
- package/dist/augment-remote.d.ts +11 -0
- package/dist/augment-remote.js +55 -0
- package/dist/gitnexus.d.ts +103 -0
- package/dist/gitnexus.js +410 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +479 -0
- package/dist/mcp-client-factory.d.ts +19 -0
- package/dist/mcp-client-factory.js +78 -0
- package/dist/mcp-client-stdio.d.ts +35 -0
- package/dist/mcp-client-stdio.js +186 -0
- package/dist/mcp-client.d.ts +45 -0
- package/dist/mcp-client.js +145 -0
- package/dist/remote-mcp-client.d.ts +43 -0
- package/dist/remote-mcp-client.js +181 -0
- package/dist/repo-resolver.d.ts +47 -0
- package/dist/repo-resolver.js +123 -0
- package/dist/tools.d.ts +6 -0
- package/dist/tools.js +230 -0
- package/dist/ui/main-menu.d.ts +33 -0
- package/dist/ui/main-menu.js +102 -0
- package/dist/ui/settings-menu.d.ts +16 -0
- package/dist/ui/settings-menu.js +95 -0
- package/docs/design/remote-mcp-backend.md +153 -0
- package/media/screenshot.png +0 -0
- package/package.json +61 -0
- package/sgconfig.yml +4 -0
- package/skills/gitnexus-debugging/SKILL.md +84 -0
- package/skills/gitnexus-exploring/SKILL.md +73 -0
- package/skills/gitnexus-impact-analysis/SKILL.md +93 -0
- package/skills/gitnexus-pr-review/SKILL.md +109 -0
- package/skills/gitnexus-refactoring/SKILL.md +85 -0
- package/src/augment-remote.ts +63 -0
- package/src/gitnexus.ts +426 -0
- package/src/index.ts +492 -0
- package/src/mcp-client-factory.ts +94 -0
- package/src/mcp-client-stdio.ts +217 -0
- package/src/mcp-client.ts +208 -0
- package/src/remote-mcp-client.ts +250 -0
- package/src/repo-resolver.ts +147 -0
- package/src/tools.ts +295 -0
- package/src/ui/main-menu.ts +139 -0
- package/src/ui/settings-menu.ts +125 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { McpClient } from './mcp-client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Patterns already augmented this session (with non-empty results).
|
|
5
|
+
* Keys are lowercased for case-insensitive dedup.
|
|
6
|
+
* Uses microsecond-precision timestamps for test isolation.
|
|
7
|
+
*/
|
|
8
|
+
const augmentedCache = new Map<string, { result: string; ts: number }>();
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Patterns that returned empty results on first attempt.
|
|
12
|
+
*/
|
|
13
|
+
const emptyCache = new Set<string>();
|
|
14
|
+
|
|
15
|
+
/** Reset caches. Call on session start. */
|
|
16
|
+
export function clearAugmentCache(): void {
|
|
17
|
+
augmentedCache.clear();
|
|
18
|
+
emptyCache.clear();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Run augment via remote MCP query tool.
|
|
23
|
+
*
|
|
24
|
+
* @param pattern The search pattern to look up
|
|
25
|
+
* @param cwd The host working directory (passed to callTool)
|
|
26
|
+
* @param serverRepo The server-side repo path
|
|
27
|
+
* @returns The augment result text, or empty string on error/empty
|
|
28
|
+
*/
|
|
29
|
+
export async function augmentRemote(pattern: string, cwd: string, serverRepo: string): Promise<string> {
|
|
30
|
+
const key = pattern.toLowerCase();
|
|
31
|
+
const now = performance.now();
|
|
32
|
+
|
|
33
|
+
// Check augmented cache — return cached result if recent
|
|
34
|
+
const cached = augmentedCache.get(key);
|
|
35
|
+
if (cached && (now - cached.ts) < 0.5) {
|
|
36
|
+
return cached.result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Check empty cache — skip early
|
|
40
|
+
if (emptyCache.has(key)) {
|
|
41
|
+
return '';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
// Dynamic import to avoid vi.mock hoisting issues with mcp-client-factory
|
|
46
|
+
const { McpClientFactory } = await import('./mcp-client-factory');
|
|
47
|
+
const config = McpClientFactory.loadConfig();
|
|
48
|
+
const client = await McpClientFactory.createClient(config);
|
|
49
|
+
|
|
50
|
+
const result = await client.callTool('query', { query: pattern, repo: serverRepo }, cwd);
|
|
51
|
+
|
|
52
|
+
if (result) {
|
|
53
|
+
augmentedCache.set(key, { result, ts: performance.now() });
|
|
54
|
+
return result;
|
|
55
|
+
} else {
|
|
56
|
+
emptyCache.add(key);
|
|
57
|
+
return '';
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
emptyCache.add(key);
|
|
61
|
+
return '';
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/gitnexus.ts
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
import spawn from 'cross-spawn';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
3
|
+
import { homedir } from 'os';
|
|
4
|
+
import { basename, extname, join, posix, relative, resolve, sep } from 'path';
|
|
5
|
+
|
|
6
|
+
/** Max output chars returned to the LLM. Prevents context flooding. JS strings are UTF-16 chars, not bytes. */
|
|
7
|
+
export const MAX_OUTPUT_CHARS = 8 * 1024;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Environment passed to all child processes.
|
|
11
|
+
* On session_start, the agent's PATH is merged with the login shell's PATH
|
|
12
|
+
* (via resolveShellPath) so that nvm/fnm/volta paths are picked up while
|
|
13
|
+
* preserving any directories the agent already had (e.g. ~/.local/share/nvm/…).
|
|
14
|
+
*/
|
|
15
|
+
export let spawnEnv: NodeJS.ProcessEnv = process.env;
|
|
16
|
+
export function updateSpawnEnv(env: NodeJS.ProcessEnv): void { spawnEnv = env; }
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolved command prefix for invoking gitnexus.
|
|
20
|
+
* Defaults to ['gitnexus']; session_start may override it from the flag or saved config.
|
|
21
|
+
*/
|
|
22
|
+
export let gitnexusCmd: string[] = ['gitnexus'];
|
|
23
|
+
export function setGitnexusCmd(cmd: string[]): void { gitnexusCmd = cmd; }
|
|
24
|
+
|
|
25
|
+
const CONFIG_PATH = join(homedir(), '.pi', 'pi-gitnexus.json');
|
|
26
|
+
|
|
27
|
+
/** Supported MCP transport modes. */
|
|
28
|
+
export type McpMode = 'auto' | 'local' | 'remote';
|
|
29
|
+
|
|
30
|
+
/** Persisted configuration for pi-gitnexus. Loaded from ~/.pi/pi-gitnexus.json. */
|
|
31
|
+
export interface GitNexusConfig {
|
|
32
|
+
/** Transport mode: 'auto' probes local binary first, 'local' forces stdio, 'remote' forces HTTP. */
|
|
33
|
+
mode?: McpMode;
|
|
34
|
+
/** Remote MCP server URL (StreamableHTTP). Only used when mode is 'remote' or 'auto' fallback. */
|
|
35
|
+
serverUrl?: string;
|
|
36
|
+
/** Local gitnexus command override, e.g. 'npx gitnexus@latest'. */
|
|
37
|
+
cmd?: string;
|
|
38
|
+
/** Whether the tool_result hook auto-appends graph context. Default: true. */
|
|
39
|
+
autoAugment?: boolean;
|
|
40
|
+
/** Augment subprocess timeout in seconds. Default: 8. */
|
|
41
|
+
augmentTimeout?: number;
|
|
42
|
+
/** Maximum number of augment calls per tool_result event. Default: 3. */
|
|
43
|
+
maxAugmentsPerResult?: number;
|
|
44
|
+
/** Maximum secondary file patterns extracted from grep output. Default: 2. */
|
|
45
|
+
maxSecondaryPatterns?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Validate and normalize a McpMode value. Returns 'auto' for invalid values. */
|
|
49
|
+
export function validateMcpMode(mode: unknown): McpMode {
|
|
50
|
+
if (mode === 'local' || mode === 'remote' || mode === 'auto') return mode;
|
|
51
|
+
return 'auto';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Default remote MCP server URL. */
|
|
55
|
+
export const DEFAULT_SERVER_URL = 'http://100.114.135.99:4747/api/mcp';
|
|
56
|
+
|
|
57
|
+
export function loadSavedConfig(): GitNexusConfig {
|
|
58
|
+
try {
|
|
59
|
+
const raw = JSON.parse(readFileSync(CONFIG_PATH, 'utf8')) as Record<string, unknown>;
|
|
60
|
+
// Environment variables take highest precedence over file config
|
|
61
|
+
const envMode = process.env.GITNEXUS_MODE ? validateMcpMode(process.env.GITNEXUS_MODE) : undefined;
|
|
62
|
+
const envUrl = process.env.GITNEXUS_SERVER_URL || undefined;
|
|
63
|
+
return {
|
|
64
|
+
mode: envMode ?? validateMcpMode(raw.mode),
|
|
65
|
+
serverUrl: envUrl ?? (typeof raw.serverUrl === 'string' && raw.serverUrl.trim() ? raw.serverUrl.trim() : undefined),
|
|
66
|
+
cmd: typeof raw.cmd === 'string' ? raw.cmd : undefined,
|
|
67
|
+
autoAugment: typeof raw.autoAugment === 'boolean' ? raw.autoAugment : undefined,
|
|
68
|
+
augmentTimeout: typeof raw.augmentTimeout === 'number' ? raw.augmentTimeout : undefined,
|
|
69
|
+
maxAugmentsPerResult: typeof raw.maxAugmentsPerResult === 'number' ? raw.maxAugmentsPerResult : undefined,
|
|
70
|
+
maxSecondaryPatterns: typeof raw.maxSecondaryPatterns === 'number' ? raw.maxSecondaryPatterns : undefined,
|
|
71
|
+
};
|
|
72
|
+
} catch {
|
|
73
|
+
return {};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function saveConfig(config: GitNexusConfig): void {
|
|
78
|
+
try {
|
|
79
|
+
mkdirSync(join(homedir(), '.pi'), { recursive: true });
|
|
80
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + '\n');
|
|
81
|
+
} catch { /* ignore write errors */ }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function resolveGitNexusCmd(flag: string | undefined, saved: string | undefined): string[] {
|
|
85
|
+
const cmd = flag?.trim() || saved?.trim() || 'gitnexus';
|
|
86
|
+
return cmd.split(/\s+/);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function normalizePathArg(path: string): string {
|
|
90
|
+
return path.startsWith('@') ? path.slice(1) : path;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function expandUserPath(path: string): string {
|
|
94
|
+
return path === '~' || path.startsWith('~/')
|
|
95
|
+
? join(homedir(), path.slice(2))
|
|
96
|
+
: path;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Default augment subprocess timeout in ms. Overridden by config.augmentTimeout. */
|
|
100
|
+
const DEFAULT_AUGMENT_TIMEOUT = 8_000;
|
|
101
|
+
|
|
102
|
+
/** Current augment timeout in ms. Updated by setAugmentTimeout(). */
|
|
103
|
+
let augmentTimeout = DEFAULT_AUGMENT_TIMEOUT;
|
|
104
|
+
|
|
105
|
+
export function setAugmentTimeout(seconds: number): void {
|
|
106
|
+
augmentTimeout = seconds * 1000;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
/** Per-cwd cache: resolved repo root with .gitnexus, or null if none found. */
|
|
111
|
+
const indexRootCache = new Map<string, string | null>();
|
|
112
|
+
|
|
113
|
+
/** Walk up ancestors looking for a .gitnexus/ directory. Result is cached per cwd. */
|
|
114
|
+
export function findGitNexusRoot(cwd: string): string | null {
|
|
115
|
+
if (indexRootCache.has(cwd)) return indexRootCache.get(cwd)!;
|
|
116
|
+
let dir = cwd;
|
|
117
|
+
while (true) {
|
|
118
|
+
if (existsSync(resolve(dir, '.gitnexus'))) {
|
|
119
|
+
indexRootCache.set(cwd, dir);
|
|
120
|
+
return dir;
|
|
121
|
+
}
|
|
122
|
+
const parent = resolve(dir, '..');
|
|
123
|
+
if (parent === dir) break;
|
|
124
|
+
dir = parent;
|
|
125
|
+
}
|
|
126
|
+
indexRootCache.set(cwd, null);
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function findGitNexusIndex(cwd: string): boolean {
|
|
131
|
+
return findGitNexusRoot(cwd) != null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Clear the index cache. Call on session_start when cwd may have changed. */
|
|
135
|
+
export function clearIndexCache(): void {
|
|
136
|
+
indexRootCache.clear();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** File extensions worth augmenting when the agent reads a file. */
|
|
140
|
+
const CODE_EXTENSIONS = new Set([
|
|
141
|
+
'.sol', '.ts', '.tsx', '.js', '.jsx', '.py', '.go', '.rs', '.java',
|
|
142
|
+
'.kt', '.swift', '.c', '.cpp', '.h', '.hpp', '.cs', '.rb', '.php',
|
|
143
|
+
'.vy', '.fe', '.huff', '.md', '.mdx',
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Extract the longest identifier-like literal from a regex pattern.
|
|
148
|
+
* Splits on metacharacters, returns the longest segment that looks like
|
|
149
|
+
* a code identifier (>= 3 chars, starts with letter/underscore).
|
|
150
|
+
*/
|
|
151
|
+
export function extractLiteralFromRegex(raw: string): string | null {
|
|
152
|
+
const segments = raw.split(/[\\^$.*+?()[\]{}|]+/);
|
|
153
|
+
let best: string | null = null;
|
|
154
|
+
for (const seg of segments) {
|
|
155
|
+
const clean = seg.replace(/['"]/g, '');
|
|
156
|
+
if (clean.length >= 3 && /^[a-zA-Z_]\w*$/.test(clean)) {
|
|
157
|
+
if (!best || clean.length > best.length) best = clean;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return best;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Simple shell-aware tokenizer for bash commands.
|
|
165
|
+
* Respects single/double quotes. Inserts a '|' boundary token at
|
|
166
|
+
* pipe, &&, ||, and ; boundaries so extractPattern can reset state.
|
|
167
|
+
*/
|
|
168
|
+
function tokenizeBashCmd(cmd: string): string[] {
|
|
169
|
+
const tokens: string[] = [];
|
|
170
|
+
let current = '';
|
|
171
|
+
let inSingle = false;
|
|
172
|
+
let inDouble = false;
|
|
173
|
+
|
|
174
|
+
const flush = () => { if (current) { tokens.push(current); current = ''; } };
|
|
175
|
+
|
|
176
|
+
for (let i = 0; i < cmd.length; i++) {
|
|
177
|
+
const ch = cmd[i];
|
|
178
|
+
|
|
179
|
+
if (inSingle) {
|
|
180
|
+
if (ch === "'") { inSingle = false; } else { current += ch; }
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (inDouble) {
|
|
184
|
+
if (ch === '"') { inDouble = false; } else { current += ch; }
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Command boundaries: |, &&, ||, ;
|
|
189
|
+
if (ch === '|' || ch === ';') {
|
|
190
|
+
flush();
|
|
191
|
+
tokens.push('|'); // boundary marker
|
|
192
|
+
if (ch === '|' && cmd[i + 1] === '|') i++; // skip ||
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (ch === '&' && cmd[i + 1] === '&') {
|
|
196
|
+
flush();
|
|
197
|
+
tokens.push('|'); // boundary marker
|
|
198
|
+
i++; // skip second &
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (ch === "'") { inSingle = true; continue; }
|
|
203
|
+
if (ch === '"') { inDouble = true; continue; }
|
|
204
|
+
|
|
205
|
+
if (/\s/.test(ch)) {
|
|
206
|
+
flush();
|
|
207
|
+
} else {
|
|
208
|
+
current += ch;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
flush();
|
|
212
|
+
return tokens;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Extract the primary search pattern from a tool's input object.
|
|
217
|
+
*
|
|
218
|
+
* grep → input.pattern
|
|
219
|
+
* find → basename of the glob pattern (e.g. "**\/foo.ts" → "foo")
|
|
220
|
+
* bash → grep/rg pattern, find -name value, or cat/head/tail filename
|
|
221
|
+
* read → basename of the file path (code files only)
|
|
222
|
+
*
|
|
223
|
+
* Returns null if pattern is missing or shorter than 3 chars.
|
|
224
|
+
*/
|
|
225
|
+
export function extractPattern(toolName: string, input: Record<string, unknown>): string | null {
|
|
226
|
+
let pattern: string | null = null;
|
|
227
|
+
|
|
228
|
+
if (toolName === 'grep') {
|
|
229
|
+
const raw = typeof input.pattern === 'string' ? input.pattern : null;
|
|
230
|
+
pattern = raw ? extractLiteralFromRegex(raw) : null;
|
|
231
|
+
} else if (toolName === 'find') {
|
|
232
|
+
// pi's find tool field name is unconfirmed — try common variants
|
|
233
|
+
const raw =
|
|
234
|
+
typeof input.pattern === 'string' ? input.pattern :
|
|
235
|
+
typeof input.glob === 'string' ? input.glob :
|
|
236
|
+
typeof input.path === 'string' ? input.path :
|
|
237
|
+
null;
|
|
238
|
+
if (raw) {
|
|
239
|
+
const seg = basename(raw).replace(/\.\w+$/, '').replace(/[*?[\]{}]/g, '');
|
|
240
|
+
pattern = seg || null;
|
|
241
|
+
}
|
|
242
|
+
} else if (toolName === 'bash') {
|
|
243
|
+
const cmd = typeof input.command === 'string' ? input.command : '';
|
|
244
|
+
const tokens = tokenizeBashCmd(cmd);
|
|
245
|
+
let foundCmd = false;
|
|
246
|
+
let foundFileCmd = false;
|
|
247
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
248
|
+
const tok = tokens[i];
|
|
249
|
+
|
|
250
|
+
// Reset state at command boundaries (pipe, &&, ||, ;)
|
|
251
|
+
if (tok === '|') { foundCmd = false; foundFileCmd = false; continue; }
|
|
252
|
+
|
|
253
|
+
// grep/rg: first non-flag arg after the command is the search pattern
|
|
254
|
+
if (tok === 'grep' || tok === 'rg') { foundCmd = true; foundFileCmd = false; continue; }
|
|
255
|
+
if (foundCmd) {
|
|
256
|
+
if (tok.startsWith('-')) continue;
|
|
257
|
+
pattern = extractLiteralFromRegex(tok);
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// cat / head / tail / less / wc: next non-flag arg is a file path → use basename
|
|
262
|
+
if (tok === 'cat' || tok === 'head' || tok === 'tail' || tok === 'less' || tok === 'wc') {
|
|
263
|
+
foundFileCmd = true; foundCmd = false; continue;
|
|
264
|
+
}
|
|
265
|
+
if (foundFileCmd) {
|
|
266
|
+
if (tok.startsWith('-')) continue;
|
|
267
|
+
const ext = extname(tok);
|
|
268
|
+
if (CODE_EXTENSIONS.has(ext)) {
|
|
269
|
+
pattern = basename(tok).replace(/\.\w+$/, '');
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
// Non-code file — reset and keep scanning for grep/rg in later segments.
|
|
273
|
+
foundFileCmd = false;
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// find -name / -iname: strip glob chars and extension from value
|
|
278
|
+
if (tok === 'find') { foundCmd = false; foundFileCmd = false; continue; }
|
|
279
|
+
if ((tok === '-name' || tok === '-iname') && tokens[i + 1]) {
|
|
280
|
+
const seg = basename(tokens[i + 1]).replace(/\.\w+$/, '').replace(/[*?[\]{}]/g, '');
|
|
281
|
+
if (seg.length >= 3) { pattern = seg; }
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
} else if (toolName === 'read') {
|
|
286
|
+
const raw = typeof input.path === 'string' ? input.path : null;
|
|
287
|
+
if (raw && CODE_EXTENSIONS.has(extname(raw))) {
|
|
288
|
+
pattern = basename(raw).replace(/\.\w+$/, '');
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (!pattern || pattern.length < 3) return null;
|
|
293
|
+
return pattern;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Extract { path, pattern } pairs from a read_many tool input.
|
|
298
|
+
* read_many input is { files: Array<{ path: string, ... }> }.
|
|
299
|
+
* Falls back to scanning content for @path lines if input lacks a files array.
|
|
300
|
+
* Returns code files only, deduplicated by basename pattern.
|
|
301
|
+
*/
|
|
302
|
+
export function extractFilesFromReadMany(
|
|
303
|
+
input: Record<string, unknown>,
|
|
304
|
+
content: { type: string; text?: string }[],
|
|
305
|
+
): Array<{ path: string; pattern: string }> {
|
|
306
|
+
const seen = new Set<string>();
|
|
307
|
+
const results: Array<{ path: string; pattern: string }> = [];
|
|
308
|
+
|
|
309
|
+
const add = (filePath: string) => {
|
|
310
|
+
const ext = extname(filePath);
|
|
311
|
+
if (!CODE_EXTENSIONS.has(ext)) return;
|
|
312
|
+
const pattern = basename(filePath).replace(/\.\w+$/, '');
|
|
313
|
+
if (pattern.length < 3 || seen.has(pattern)) return;
|
|
314
|
+
seen.add(pattern);
|
|
315
|
+
results.push({ path: filePath, pattern });
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// Primary: extract from structured input
|
|
319
|
+
const files = Array.isArray(input.files) ? input.files : [];
|
|
320
|
+
for (const f of files) {
|
|
321
|
+
if (typeof f === 'object' && f !== null && typeof (f as Record<string, unknown>).path === 'string') {
|
|
322
|
+
add((f as Record<string, unknown>).path as string);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Fallback: parse @path lines from content (if input was empty/unknown)
|
|
327
|
+
if (results.length === 0) {
|
|
328
|
+
const text = content.map(c => c.text ?? '').join('\n');
|
|
329
|
+
for (const line of text.split('\n')) {
|
|
330
|
+
const m = line.match(/^@(.+)$/);
|
|
331
|
+
if (m) add(m[1].trim());
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return results;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Extract up to `limit` unique file basenames (without extension) from
|
|
340
|
+
* grep-style output lines of the form "path/to/file.ext:lineno:content".
|
|
341
|
+
* Used to augment secondary context from search results.
|
|
342
|
+
*/
|
|
343
|
+
export function extractFilePatternsFromContent(
|
|
344
|
+
content: { type: string; text?: string }[],
|
|
345
|
+
limit = 2,
|
|
346
|
+
): string[] {
|
|
347
|
+
const text = content.map(c => c.text ?? '').join('\n');
|
|
348
|
+
const seen = new Set<string>();
|
|
349
|
+
const results: string[] = [];
|
|
350
|
+
for (const line of text.split('\n')) {
|
|
351
|
+
// Match "some/path/File.ext:digits:" at the start of a line
|
|
352
|
+
const m = line.match(/^([^\n:]+\.\w+):\d+:/);
|
|
353
|
+
if (!m) continue;
|
|
354
|
+
const base = basename(m[1]).replace(/\.\w+$/, '');
|
|
355
|
+
if (base.length >= 3 && !seen.has(base)) {
|
|
356
|
+
seen.add(base);
|
|
357
|
+
results.push(base);
|
|
358
|
+
}
|
|
359
|
+
if (results.length >= limit) break;
|
|
360
|
+
}
|
|
361
|
+
return results;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Validate that a file path stays within cwd (path traversal guard).
|
|
366
|
+
* Returns the resolved absolute path, or null if it escapes cwd.
|
|
367
|
+
*/
|
|
368
|
+
export function safeResolvePath(file: string, cwd: string): string | null {
|
|
369
|
+
const resolved = resolve(cwd, file);
|
|
370
|
+
return resolved.startsWith(cwd + sep) || resolved === cwd ? resolved : null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export function toRepoRelativePath(file: string, repoRoot: string): string | null {
|
|
374
|
+
const resolved = safeResolvePath(file, repoRoot);
|
|
375
|
+
if (!resolved) return null;
|
|
376
|
+
return relative(repoRoot, resolved) || '.';
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function validateRepoRelativePath(file: string): string | null {
|
|
380
|
+
const normalized = posix.normalize(file.replaceAll('\\', '/'));
|
|
381
|
+
if (normalized === '.' || normalized === '') return null;
|
|
382
|
+
if (normalized.startsWith('../') || normalized === '..' || normalized.startsWith('/')) return null;
|
|
383
|
+
return normalized;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Spawn `gitnexus augment <pattern>` and return its output.
|
|
388
|
+
* gitnexus augment writes results to stderr (not stdout).
|
|
389
|
+
* Used by the tool_result hook — not by registered tools (those use mcp-client).
|
|
390
|
+
* Returns output trimmed and truncated to MAX_OUTPUT_CHARS, or "" on any error.
|
|
391
|
+
*/
|
|
392
|
+
export async function runAugment(pattern: string, cwd: string): Promise<string> {
|
|
393
|
+
return new Promise((resolve_) => {
|
|
394
|
+
// gitnexus augment writes results to stderr (not stdout)
|
|
395
|
+
let output = '';
|
|
396
|
+
let done = false;
|
|
397
|
+
|
|
398
|
+
const [bin, ...baseArgs] = gitnexusCmd;
|
|
399
|
+
const proc = spawn(bin, [...baseArgs, 'augment', pattern], {
|
|
400
|
+
cwd,
|
|
401
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
402
|
+
env: spawnEnv,
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const timer = setTimeout(() => {
|
|
406
|
+
if (!done) {
|
|
407
|
+
done = true;
|
|
408
|
+
proc.kill('SIGTERM');
|
|
409
|
+
resolve_('');
|
|
410
|
+
}
|
|
411
|
+
}, augmentTimeout);
|
|
412
|
+
|
|
413
|
+
proc.stderr!.on('data', (chunk: { toString(): string }) => { output += chunk.toString(); });
|
|
414
|
+
|
|
415
|
+
proc.on('close', (code: number | null) => {
|
|
416
|
+
if (done) return;
|
|
417
|
+
done = true;
|
|
418
|
+
clearTimeout(timer);
|
|
419
|
+
resolve_(code === 0 ? output.trim().slice(0, MAX_OUTPUT_CHARS) : '');
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
proc.on('error', () => {
|
|
423
|
+
if (!done) { done = true; clearTimeout(timer); resolve_(''); }
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
}
|