@akon-labs/nexus-cli 0.1.10 → 0.1.11
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/cli/require-login.d.ts.map +1 -1
- package/dist/cli/require-login.js +2 -2
- package/dist/cli/require-login.js.map +1 -1
- package/dist/cli.js +2492 -1088
- package/dist/core/agent-session.d.ts +3 -1
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +4 -0
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +6 -0
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +7 -1
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +14 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +18 -0
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/graph-rail-store.d.ts +10 -0
- package/dist/core/graph-rail-store.d.ts.map +1 -1
- package/dist/core/graph-rail-store.js +9 -0
- package/dist/core/graph-rail-store.js.map +1 -1
- package/dist/core/keybindings.d.ts +2 -1
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +4 -1
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +1 -1
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +1 -1
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/extensions/gitnexus/client.d.ts.map +1 -1
- package/dist/extensions/gitnexus/client.js +45 -14
- package/dist/extensions/gitnexus/client.js.map +1 -1
- package/dist/extensions/gitnexus/config.d.ts +15 -1
- package/dist/extensions/gitnexus/config.d.ts.map +1 -1
- package/dist/extensions/gitnexus/config.js +86 -2
- package/dist/extensions/gitnexus/config.js.map +1 -1
- package/dist/extensions/gitnexus/hub-api.d.ts +56 -0
- package/dist/extensions/gitnexus/hub-api.d.ts.map +1 -0
- package/dist/extensions/gitnexus/hub-api.js +105 -0
- package/dist/extensions/gitnexus/hub-api.js.map +1 -0
- package/dist/extensions/gitnexus/index-repo.d.ts +50 -0
- package/dist/extensions/gitnexus/index-repo.d.ts.map +1 -0
- package/dist/extensions/gitnexus/index-repo.js +162 -0
- package/dist/extensions/gitnexus/index-repo.js.map +1 -0
- package/dist/extensions/gitnexus/index.d.ts.map +1 -1
- package/dist/extensions/gitnexus/index.js +137 -17
- package/dist/extensions/gitnexus/index.js.map +1 -1
- package/dist/extensions/gitnexus/observatory.d.ts.map +1 -1
- package/dist/extensions/gitnexus/observatory.js +2 -2
- package/dist/extensions/gitnexus/observatory.js.map +1 -1
- package/dist/extensions/gitnexus/resolve.d.ts +9 -0
- package/dist/extensions/gitnexus/resolve.d.ts.map +1 -1
- package/dist/extensions/gitnexus/resolve.js +52 -2
- package/dist/extensions/gitnexus/resolve.js.map +1 -1
- package/dist/extensions/index.d.ts.map +1 -1
- package/dist/extensions/index.js +3 -0
- package/dist/extensions/index.js.map +1 -1
- package/dist/extensions/mcp/commands.d.ts +22 -2
- package/dist/extensions/mcp/commands.d.ts.map +1 -1
- package/dist/extensions/mcp/commands.js +201 -9
- package/dist/extensions/mcp/commands.js.map +1 -1
- package/dist/extensions/mcp/config.d.ts +2 -0
- package/dist/extensions/mcp/config.d.ts.map +1 -1
- package/dist/extensions/mcp/config.js +6 -1
- package/dist/extensions/mcp/config.js.map +1 -1
- package/dist/extensions/mcp/index.d.ts.map +1 -1
- package/dist/extensions/mcp/index.js +54 -5
- package/dist/extensions/mcp/index.js.map +1 -1
- package/dist/extensions/mcp/mcp-setup-panel.d.ts +63 -2
- package/dist/extensions/mcp/mcp-setup-panel.d.ts.map +1 -1
- package/dist/extensions/mcp/mcp-setup-panel.js +563 -77
- package/dist/extensions/mcp/mcp-setup-panel.js.map +1 -1
- package/dist/extensions/subagent/index.d.ts +2 -1
- package/dist/extensions/subagent/index.d.ts.map +1 -1
- package/dist/extensions/subagent/index.js +19 -6
- package/dist/extensions/subagent/index.js.map +1 -1
- package/dist/extensions/subagent-render.d.ts +3 -0
- package/dist/extensions/subagent-render.d.ts.map +1 -0
- package/dist/extensions/subagent-render.js +39 -0
- package/dist/extensions/subagent-render.js.map +1 -0
- package/dist/index.js +2524 -1120
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +2 -0
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/command-palette.d.ts +2 -1
- package/dist/modes/interactive/components/command-palette.d.ts.map +1 -1
- package/dist/modes/interactive/components/command-palette.js +11 -4
- package/dist/modes/interactive/components/command-palette.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +9 -3
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/graph-rail.d.ts.map +1 -1
- package/dist/modes/interactive/components/graph-rail.js +18 -3
- package/dist/modes/interactive/components/graph-rail.js.map +1 -1
- package/dist/modes/interactive/components/keybinding-hints.d.ts +1 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -1
- package/dist/modes/interactive/components/keybinding-hints.js +7 -1
- package/dist/modes/interactive/components/keybinding-hints.js.map +1 -1
- package/dist/modes/interactive/components/landing-panel.d.ts.map +1 -1
- package/dist/modes/interactive/components/landing-panel.js +15 -2
- package/dist/modes/interactive/components/landing-panel.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts +3 -2
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +12 -2
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +3 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +42 -8
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/rpc-entry.js +2492 -1088
- package/dist/utils/hidden-spawn.d.ts +29 -0
- package/dist/utils/hidden-spawn.d.ts.map +1 -0
- package/dist/utils/hidden-spawn.js +101 -0
- package/dist/utils/hidden-spawn.js.map +1 -0
- package/dist/vendor/ai.js +233 -233
- package/dist/vendor/tui.js +1 -1
- package/native/win32/build.mjs +95 -0
- package/native/win32/prebuilds/win32-x64/hidden-spawn.exe +0 -0
- package/native/win32/src/hidden-spawn.c +157 -0
- package/npm-shrinkwrap.json +2 -37
- package/package.json +7 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grep.js","sourceRoot":"","sources":["../../../src/core/tools/grep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AAEjF,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EACN,iBAAiB,EACjB,UAAU,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AC,UAAU,CAAC,CAAC,CAAC;YAClF,OAAO,IAAI,CAAC;QAAA,CACZ;KACD,CAAC;AAAA,CACF;AAED,MAAM,UAAU,cAAc,CAAC,GAAW,EAAE,OAAyB,EAAgC;IACpG,OAAO,kBAAkB,CAAC,wBAAwB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AAAA,CAClE","sourcesContent":["import { readFile as fsReadFile, stat as fsStat } from \"node:fs/promises\";\nimport { createInterface } from \"node:readline\";\nimport type { AgentTool } from \"@earendil-works/pi-agent-core\";\nimport { Text } from \"@earendil-works/pi-tui\";\nimport { spawn } from \"child_process\";\nimport path from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport type { Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport { ensureTool } from \"../../utils/tools-manager.ts\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { getTextOutput, invalidArgText, shortenPath, str } from \"./render-utils.ts\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.ts\";\nimport {\n\tDEFAULT_MAX_BYTES,\n\tformatSize,\n\tGREP_MAX_LINE_LENGTH,\n\ttype TruncationResult,\n\ttruncateHead,\n\ttruncateLine,\n} from \"./truncate.ts\";\n\nconst grepSchema = Type.Object({\n\tpattern: Type.String({ description: \"Search pattern (regex or literal string)\" }),\n\tpath: Type.Optional(Type.String({ description: \"Directory or file to search (default: current directory)\" })),\n\tglob: Type.Optional(Type.String({ description: \"Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'\" })),\n\tignoreCase: Type.Optional(Type.Boolean({ description: \"Case-insensitive search (default: false)\" })),\n\tliteral: Type.Optional(\n\t\tType.Boolean({ description: \"Treat pattern as literal string instead of regex (default: false)\" }),\n\t),\n\tcontext: Type.Optional(\n\t\tType.Number({ description: \"Number of lines to show before and after each match (default: 0)\" }),\n\t),\n\tlimit: Type.Optional(Type.Number({ description: \"Maximum number of matches to return (default: 100)\" })),\n});\n\nexport type GrepToolInput = Static<typeof grepSchema>;\nconst DEFAULT_LIMIT = 100;\n\nexport interface GrepToolDetails {\n\ttruncation?: TruncationResult;\n\tmatchLimitReached?: number;\n\tlinesTruncated?: boolean;\n}\n\n/**\n * Pluggable operations for the grep tool.\n * Override these to delegate search to remote systems (for example SSH).\n */\nexport interface GrepOperations {\n\t/** Check if path is a directory. Throws if path does not exist. */\n\tisDirectory: (absolutePath: string) => Promise<boolean> | boolean;\n\t/** Read file contents for context lines */\n\treadFile: (absolutePath: string) => Promise<string> | string;\n}\n\nconst defaultGrepOperations: GrepOperations = {\n\tisDirectory: async (p) => (await fsStat(p)).isDirectory(),\n\treadFile: (p) => fsReadFile(p, \"utf-8\"),\n};\n\nexport interface GrepToolOptions {\n\t/** Custom operations for grep. Default: local filesystem plus ripgrep */\n\toperations?: GrepOperations;\n}\n\nfunction formatGrepCall(\n\targs: { pattern: string; path?: string; glob?: string; limit?: number } | undefined,\n\ttheme: Theme,\n): string {\n\tconst pattern = str(args?.pattern);\n\tconst rawPath = str(args?.path);\n\tconst path = rawPath !== null ? shortenPath(rawPath || \".\") : null;\n\tconst glob = str(args?.glob);\n\tconst limit = args?.limit;\n\tconst invalidArg = invalidArgText(theme);\n\tlet text =\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"grep\")) +\n\t\t\" \" +\n\t\t(pattern === null ? invalidArg : theme.fg(\"accent\", `/${pattern || \"\"}/`)) +\n\t\ttheme.fg(\"toolOutput\", ` in ${path === null ? invalidArg : path}`);\n\tif (glob) text += theme.fg(\"toolOutput\", ` (${glob})`);\n\tif (limit !== undefined) text += theme.fg(\"toolOutput\", ` limit ${limit}`);\n\treturn text;\n}\n\nfunction formatGrepResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: GrepToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: Theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 15;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"to expand\")}${theme.fg(\"muted\", \")\")}`;\n\t\t}\n\t}\n\n\tconst matchLimit = result.details?.matchLimitReached;\n\tconst truncation = result.details?.truncation;\n\tconst linesTruncated = result.details?.linesTruncated;\n\tif (matchLimit || truncation?.truncated || linesTruncated) {\n\t\tconst warnings: string[] = [];\n\t\tif (matchLimit) warnings.push(`${matchLimit} matches limit`);\n\t\tif (truncation?.truncated) warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);\n\t\tif (linesTruncated) warnings.push(\"some lines truncated\");\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${warnings.join(\", \")}]`)}`;\n\t}\n\treturn text;\n}\n\nexport function createGrepToolDefinition(\n\tcwd: string,\n\toptions?: GrepToolOptions,\n): ToolDefinition<typeof grepSchema, GrepToolDetails | undefined> {\n\tconst customOps = options?.operations;\n\treturn {\n\t\tname: \"grep\",\n\t\tlabel: \"grep\",\n\t\tdescription: `Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Long lines are truncated to ${GREP_MAX_LINE_LENGTH} chars.`,\n\t\tpromptSnippet: \"Search file contents for patterns (respects .gitignore)\",\n\t\tparameters: grepSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{\n\t\t\t\tpattern,\n\t\t\t\tpath: searchDir,\n\t\t\t\tglob,\n\t\t\t\tignoreCase,\n\t\t\t\tliteral,\n\t\t\t\tcontext,\n\t\t\t\tlimit,\n\t\t\t}: {\n\t\t\t\tpattern: string;\n\t\t\t\tpath?: string;\n\t\t\t\tglob?: string;\n\t\t\t\tignoreCase?: boolean;\n\t\t\t\tliteral?: boolean;\n\t\t\t\tcontext?: number;\n\t\t\t\tlimit?: number;\n\t\t\t},\n\t\t\tsignal?: AbortSignal,\n\t\t\t_onUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\treject(new Error(\"Operation aborted\"));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlet settled = false;\n\t\t\t\tconst settle = (fn: () => void) => {\n\t\t\t\t\tif (!settled) {\n\t\t\t\t\t\tsettled = true;\n\t\t\t\t\t\tfn();\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\t(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst rgPath = await ensureTool(\"rg\", true);\n\t\t\t\t\t\tif (!rgPath) {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(\"ripgrep (rg) is not available and could not be downloaded\")));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst searchPath = resolveToCwd(searchDir || \".\", cwd);\n\t\t\t\t\t\tconst ops = customOps ?? defaultGrepOperations;\n\t\t\t\t\t\tlet isDirectory: boolean;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tisDirectory = await ops.isDirectory(searchPath);\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Path not found: ${searchPath}`)));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst contextValue = context && context > 0 ? context : 0;\n\t\t\t\t\t\tconst effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);\n\t\t\t\t\t\tconst formatPath = (filePath: string): string => {\n\t\t\t\t\t\t\tif (isDirectory) {\n\t\t\t\t\t\t\t\tconst relative = path.relative(searchPath, filePath);\n\t\t\t\t\t\t\t\tif (relative && !relative.startsWith(\"..\")) {\n\t\t\t\t\t\t\t\t\treturn relative.replace(/\\\\/g, \"/\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn path.basename(filePath);\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tconst fileCache = new Map<string, string[]>();\n\t\t\t\t\t\tconst getFileLines = async (filePath: string): Promise<string[]> => {\n\t\t\t\t\t\t\tlet lines = fileCache.get(filePath);\n\t\t\t\t\t\t\tif (!lines) {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tconst content = await ops.readFile(filePath);\n\t\t\t\t\t\t\t\t\tlines = content.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\").split(\"\\n\");\n\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\tlines = [];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tfileCache.set(filePath, lines);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn lines;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tconst args: string[] = [\"--json\", \"--line-number\", \"--color=never\", \"--hidden\"];\n\t\t\t\t\t\tif (ignoreCase) args.push(\"--ignore-case\");\n\t\t\t\t\t\tif (literal) args.push(\"--fixed-strings\");\n\t\t\t\t\t\tif (glob) args.push(\"--glob\", glob);\n\t\t\t\t\t\targs.push(\"--\", pattern, searchPath);\n\n\t\t\t\t\t\tconst child = spawn(rgPath, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\t\t\t\t\tconst rl = createInterface({ input: child.stdout });\n\t\t\t\t\t\tlet stderr = \"\";\n\t\t\t\t\t\tlet matchCount = 0;\n\t\t\t\t\t\tlet matchLimitReached = false;\n\t\t\t\t\t\tlet linesTruncated = false;\n\t\t\t\t\t\tlet aborted = false;\n\t\t\t\t\t\tlet killedDueToLimit = false;\n\t\t\t\t\t\tconst outputLines: string[] = [];\n\n\t\t\t\t\t\tconst cleanup = () => {\n\t\t\t\t\t\t\trl.close();\n\t\t\t\t\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst stopChild = (dueToLimit = false) => {\n\t\t\t\t\t\t\tif (!child.killed) {\n\t\t\t\t\t\t\t\tkilledDueToLimit = dueToLimit;\n\t\t\t\t\t\t\t\tchild.kill();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst onAbort = () => {\n\t\t\t\t\t\t\taborted = true;\n\t\t\t\t\t\t\tstopChild();\n\t\t\t\t\t\t};\n\t\t\t\t\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t\t\tchild.stderr?.on(\"data\", (chunk) => {\n\t\t\t\t\t\t\tstderr += chunk.toString();\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst formatBlock = async (filePath: string, lineNumber: number): Promise<string[]> => {\n\t\t\t\t\t\t\tconst relativePath = formatPath(filePath);\n\t\t\t\t\t\t\tconst lines = await getFileLines(filePath);\n\t\t\t\t\t\t\tif (!lines.length) return [`${relativePath}:${lineNumber}: (unable to read file)`];\n\t\t\t\t\t\t\tconst block: string[] = [];\n\t\t\t\t\t\t\tconst start = contextValue > 0 ? Math.max(1, lineNumber - contextValue) : lineNumber;\n\t\t\t\t\t\t\tconst end = contextValue > 0 ? Math.min(lines.length, lineNumber + contextValue) : lineNumber;\n\t\t\t\t\t\t\tfor (let current = start; current <= end; current++) {\n\t\t\t\t\t\t\t\tconst lineText = lines[current - 1] ?? \"\";\n\t\t\t\t\t\t\t\tconst sanitized = lineText.replace(/\\r/g, \"\");\n\t\t\t\t\t\t\t\tconst isMatchLine = current === lineNumber;\n\t\t\t\t\t\t\t\t// Truncate long lines so grep output stays compact.\n\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\tif (isMatchLine) block.push(`${relativePath}:${current}: ${truncatedText}`);\n\t\t\t\t\t\t\t\telse block.push(`${relativePath}-${current}- ${truncatedText}`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn block;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\t// Collect matches during streaming, then format them after rg exits.\n\t\t\t\t\t\tconst matches: Array<{ filePath: string; lineNumber: number; lineText?: string }> = [];\n\t\t\t\t\t\trl.on(\"line\", (line) => {\n\t\t\t\t\t\t\tif (!line.trim() || matchCount >= effectiveLimit) return;\n\t\t\t\t\t\t\tlet event: any;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tevent = JSON.parse(line);\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (event.type === \"match\") {\n\t\t\t\t\t\t\t\tmatchCount++;\n\t\t\t\t\t\t\t\tconst filePath = event.data?.path?.text;\n\t\t\t\t\t\t\t\tconst lineNumber = event.data?.line_number;\n\t\t\t\t\t\t\t\tconst lineText = event.data?.lines?.text;\n\t\t\t\t\t\t\t\tif (filePath && typeof lineNumber === \"number\")\n\t\t\t\t\t\t\t\t\tmatches.push({ filePath, lineNumber, lineText });\n\t\t\t\t\t\t\t\tif (matchCount >= effectiveLimit) {\n\t\t\t\t\t\t\t\t\tmatchLimitReached = true;\n\t\t\t\t\t\t\t\t\tstopChild(true);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tchild.on(\"error\", (error) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Failed to run ripgrep: ${error.message}`)));\n\t\t\t\t\t\t});\n\t\t\t\t\t\tchild.on(\"close\", async (code) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tif (aborted) {\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(\"Operation aborted\")));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!killedDueToLimit && code !== 0 && code !== 1) {\n\t\t\t\t\t\t\t\tconst errorMsg = stderr.trim() || `ripgrep exited with code ${code}`;\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(errorMsg)));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (matchCount === 0) {\n\t\t\t\t\t\t\t\tsettle(() =>\n\t\t\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: \"No matches found\" }], details: undefined }),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Format matches after streaming finishes so custom readFile() backends can be async.\n\t\t\t\t\t\t\tfor (const match of matches) {\n\t\t\t\t\t\t\t\tif (contextValue === 0 && match.lineText !== undefined) {\n\t\t\t\t\t\t\t\t\tconst relativePath = formatPath(match.filePath);\n\t\t\t\t\t\t\t\t\tconst sanitized = match.lineText\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r\\n/g, \"\\n\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r/g, \"\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\n$/, \"\");\n\t\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\t\toutputLines.push(`${relativePath}:${match.lineNumber}: ${truncatedText}`);\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tconst block = await formatBlock(match.filePath, match.lineNumber);\n\t\t\t\t\t\t\t\t\toutputLines.push(...block);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst rawOutput = outputLines.join(\"\\n\");\n\t\t\t\t\t\t\t// Apply byte truncation. There is no line limit here because the match limit already capped rows.\n\t\t\t\t\t\t\tconst truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });\n\t\t\t\t\t\t\tlet output = truncation.content;\n\t\t\t\t\t\t\tconst details: GrepToolDetails = {};\n\t\t\t\t\t\t\t// Build actionable notices for truncation and match limits.\n\t\t\t\t\t\t\tconst notices: string[] = [];\n\t\t\t\t\t\t\tif (matchLimitReached) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.matchLimitReached = effectiveLimit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t\tnotices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);\n\t\t\t\t\t\t\t\tdetails.truncation = truncation;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (linesTruncated) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.linesTruncated = true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (notices.length > 0) output += `\\n\\n[${notices.join(\". \")}]`;\n\t\t\t\t\t\t\tsettle(() =>\n\t\t\t\t\t\t\t\tresolve({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: output }],\n\t\t\t\t\t\t\t\t\tdetails: Object.keys(details).length > 0 ? details : undefined,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tsettle(() => reject(err as Error));\n\t\t\t\t\t}\n\t\t\t\t})();\n\t\t\t});\n\t\t},\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepCall(args, theme));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepResult(result as any, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createGrepTool(cwd: string, options?: GrepToolOptions): AgentTool<typeof grepSchema> {\n\treturn wrapToolDefinition(createGrepToolDefinition(cwd, options));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"grep.js","sourceRoot":"","sources":["../../../src/core/tools/grep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AAEjF,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EACN,iBA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Aa,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;YAClF,OAAO,IAAI,CAAC;QAAA,CACZ;KACD,CAAC;AAAA,CACF;AAED,MAAM,UAAU,cAAc,CAAC,GAAW,EAAE,OAAyB,EAAgC;IACpG,OAAO,kBAAkB,CAAC,wBAAwB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AAAA,CAClE","sourcesContent":["import { readFile as fsReadFile, stat as fsStat } from \"node:fs/promises\";\nimport { createInterface } from \"node:readline\";\nimport type { AgentTool } from \"@earendil-works/pi-agent-core\";\nimport { Text } from \"@earendil-works/pi-tui\";\nimport { spawn } from \"child_process\";\nimport path from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport type { Theme } from \"../../modes/interactive/theme/theme.ts\";\nimport { ensureTool } from \"../../utils/tools-manager.ts\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { getTextOutput, invalidArgText, shortenPath, str } from \"./render-utils.ts\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.ts\";\nimport {\n\tDEFAULT_MAX_BYTES,\n\tformatSize,\n\tGREP_MAX_LINE_LENGTH,\n\ttype TruncationResult,\n\ttruncateHead,\n\ttruncateLine,\n} from \"./truncate.ts\";\n\nconst grepSchema = Type.Object({\n\tpattern: Type.String({ description: \"Search pattern (regex or literal string)\" }),\n\tpath: Type.Optional(Type.String({ description: \"Directory or file to search (default: current directory)\" })),\n\tglob: Type.Optional(Type.String({ description: \"Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'\" })),\n\tignoreCase: Type.Optional(Type.Boolean({ description: \"Case-insensitive search (default: false)\" })),\n\tliteral: Type.Optional(\n\t\tType.Boolean({ description: \"Treat pattern as literal string instead of regex (default: false)\" }),\n\t),\n\tcontext: Type.Optional(\n\t\tType.Number({ description: \"Number of lines to show before and after each match (default: 0)\" }),\n\t),\n\tlimit: Type.Optional(Type.Number({ description: \"Maximum number of matches to return (default: 100)\" })),\n});\n\nexport type GrepToolInput = Static<typeof grepSchema>;\nconst DEFAULT_LIMIT = 100;\n\nexport interface GrepToolDetails {\n\ttruncation?: TruncationResult;\n\tmatchLimitReached?: number;\n\tlinesTruncated?: boolean;\n}\n\n/**\n * Pluggable operations for the grep tool.\n * Override these to delegate search to remote systems (for example SSH).\n */\nexport interface GrepOperations {\n\t/** Check if path is a directory. Throws if path does not exist. */\n\tisDirectory: (absolutePath: string) => Promise<boolean> | boolean;\n\t/** Read file contents for context lines */\n\treadFile: (absolutePath: string) => Promise<string> | string;\n}\n\nconst defaultGrepOperations: GrepOperations = {\n\tisDirectory: async (p) => (await fsStat(p)).isDirectory(),\n\treadFile: (p) => fsReadFile(p, \"utf-8\"),\n};\n\nexport interface GrepToolOptions {\n\t/** Custom operations for grep. Default: local filesystem plus ripgrep */\n\toperations?: GrepOperations;\n}\n\nfunction formatGrepCall(\n\targs: { pattern: string; path?: string; glob?: string; limit?: number } | undefined,\n\ttheme: Theme,\n): string {\n\tconst pattern = str(args?.pattern);\n\tconst rawPath = str(args?.path);\n\tconst path = rawPath !== null ? shortenPath(rawPath || \".\") : null;\n\tconst glob = str(args?.glob);\n\tconst limit = args?.limit;\n\tconst invalidArg = invalidArgText(theme);\n\tlet text =\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"grep\")) +\n\t\t\" \" +\n\t\t(pattern === null ? invalidArg : theme.fg(\"accent\", `/${pattern || \"\"}/`)) +\n\t\ttheme.fg(\"toolOutput\", ` in ${path === null ? invalidArg : path}`);\n\tif (glob) text += theme.fg(\"toolOutput\", ` (${glob})`);\n\tif (limit !== undefined) text += theme.fg(\"toolOutput\", ` limit ${limit}`);\n\treturn text;\n}\n\nfunction formatGrepResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: GrepToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: Theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 15;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"to expand\")}${theme.fg(\"muted\", \")\")}`;\n\t\t}\n\t}\n\n\tconst matchLimit = result.details?.matchLimitReached;\n\tconst truncation = result.details?.truncation;\n\tconst linesTruncated = result.details?.linesTruncated;\n\tif (matchLimit || truncation?.truncated || linesTruncated) {\n\t\tconst warnings: string[] = [];\n\t\tif (matchLimit) warnings.push(`${matchLimit} matches limit`);\n\t\tif (truncation?.truncated) warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);\n\t\tif (linesTruncated) warnings.push(\"some lines truncated\");\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${warnings.join(\", \")}]`)}`;\n\t}\n\treturn text;\n}\n\nexport function createGrepToolDefinition(\n\tcwd: string,\n\toptions?: GrepToolOptions,\n): ToolDefinition<typeof grepSchema, GrepToolDetails | undefined> {\n\tconst customOps = options?.operations;\n\treturn {\n\t\tname: \"grep\",\n\t\tlabel: \"grep\",\n\t\tdescription: `Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Long lines are truncated to ${GREP_MAX_LINE_LENGTH} chars.`,\n\t\tpromptSnippet: \"Search file contents for patterns (respects .gitignore)\",\n\t\tparameters: grepSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{\n\t\t\t\tpattern,\n\t\t\t\tpath: searchDir,\n\t\t\t\tglob,\n\t\t\t\tignoreCase,\n\t\t\t\tliteral,\n\t\t\t\tcontext,\n\t\t\t\tlimit,\n\t\t\t}: {\n\t\t\t\tpattern: string;\n\t\t\t\tpath?: string;\n\t\t\t\tglob?: string;\n\t\t\t\tignoreCase?: boolean;\n\t\t\t\tliteral?: boolean;\n\t\t\t\tcontext?: number;\n\t\t\t\tlimit?: number;\n\t\t\t},\n\t\t\tsignal?: AbortSignal,\n\t\t\t_onUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\treject(new Error(\"Operation aborted\"));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlet settled = false;\n\t\t\t\tconst settle = (fn: () => void) => {\n\t\t\t\t\tif (!settled) {\n\t\t\t\t\t\tsettled = true;\n\t\t\t\t\t\tfn();\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\t(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst rgPath = await ensureTool(\"rg\", true);\n\t\t\t\t\t\tif (!rgPath) {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(\"ripgrep (rg) is not available and could not be downloaded\")));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst searchPath = resolveToCwd(searchDir || \".\", cwd);\n\t\t\t\t\t\tconst ops = customOps ?? defaultGrepOperations;\n\t\t\t\t\t\tlet isDirectory: boolean;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tisDirectory = await ops.isDirectory(searchPath);\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Path not found: ${searchPath}`)));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst contextValue = context && context > 0 ? context : 0;\n\t\t\t\t\t\tconst effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);\n\t\t\t\t\t\tconst formatPath = (filePath: string): string => {\n\t\t\t\t\t\t\tif (isDirectory) {\n\t\t\t\t\t\t\t\tconst relative = path.relative(searchPath, filePath);\n\t\t\t\t\t\t\t\tif (relative && !relative.startsWith(\"..\")) {\n\t\t\t\t\t\t\t\t\treturn relative.replace(/\\\\/g, \"/\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn path.basename(filePath);\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tconst fileCache = new Map<string, string[]>();\n\t\t\t\t\t\tconst getFileLines = async (filePath: string): Promise<string[]> => {\n\t\t\t\t\t\t\tlet lines = fileCache.get(filePath);\n\t\t\t\t\t\t\tif (!lines) {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tconst content = await ops.readFile(filePath);\n\t\t\t\t\t\t\t\t\tlines = content.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\").split(\"\\n\");\n\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\tlines = [];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tfileCache.set(filePath, lines);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn lines;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tconst args: string[] = [\"--json\", \"--line-number\", \"--color=never\", \"--hidden\"];\n\t\t\t\t\t\tif (ignoreCase) args.push(\"--ignore-case\");\n\t\t\t\t\t\tif (literal) args.push(\"--fixed-strings\");\n\t\t\t\t\t\tif (glob) args.push(\"--glob\", glob);\n\t\t\t\t\t\targs.push(\"--\", pattern, searchPath);\n\n\t\t\t\t\t\tconst child = spawn(rgPath, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"], windowsHide: true });\n\t\t\t\t\t\tconst rl = createInterface({ input: child.stdout });\n\t\t\t\t\t\tlet stderr = \"\";\n\t\t\t\t\t\tlet matchCount = 0;\n\t\t\t\t\t\tlet matchLimitReached = false;\n\t\t\t\t\t\tlet linesTruncated = false;\n\t\t\t\t\t\tlet aborted = false;\n\t\t\t\t\t\tlet killedDueToLimit = false;\n\t\t\t\t\t\tconst outputLines: string[] = [];\n\n\t\t\t\t\t\tconst cleanup = () => {\n\t\t\t\t\t\t\trl.close();\n\t\t\t\t\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst stopChild = (dueToLimit = false) => {\n\t\t\t\t\t\t\tif (!child.killed) {\n\t\t\t\t\t\t\t\tkilledDueToLimit = dueToLimit;\n\t\t\t\t\t\t\t\tchild.kill();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst onAbort = () => {\n\t\t\t\t\t\t\taborted = true;\n\t\t\t\t\t\t\tstopChild();\n\t\t\t\t\t\t};\n\t\t\t\t\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t\t\tchild.stderr?.on(\"data\", (chunk) => {\n\t\t\t\t\t\t\tstderr += chunk.toString();\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst formatBlock = async (filePath: string, lineNumber: number): Promise<string[]> => {\n\t\t\t\t\t\t\tconst relativePath = formatPath(filePath);\n\t\t\t\t\t\t\tconst lines = await getFileLines(filePath);\n\t\t\t\t\t\t\tif (!lines.length) return [`${relativePath}:${lineNumber}: (unable to read file)`];\n\t\t\t\t\t\t\tconst block: string[] = [];\n\t\t\t\t\t\t\tconst start = contextValue > 0 ? Math.max(1, lineNumber - contextValue) : lineNumber;\n\t\t\t\t\t\t\tconst end = contextValue > 0 ? Math.min(lines.length, lineNumber + contextValue) : lineNumber;\n\t\t\t\t\t\t\tfor (let current = start; current <= end; current++) {\n\t\t\t\t\t\t\t\tconst lineText = lines[current - 1] ?? \"\";\n\t\t\t\t\t\t\t\tconst sanitized = lineText.replace(/\\r/g, \"\");\n\t\t\t\t\t\t\t\tconst isMatchLine = current === lineNumber;\n\t\t\t\t\t\t\t\t// Truncate long lines so grep output stays compact.\n\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\tif (isMatchLine) block.push(`${relativePath}:${current}: ${truncatedText}`);\n\t\t\t\t\t\t\t\telse block.push(`${relativePath}-${current}- ${truncatedText}`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn block;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\t// Collect matches during streaming, then format them after rg exits.\n\t\t\t\t\t\tconst matches: Array<{ filePath: string; lineNumber: number; lineText?: string }> = [];\n\t\t\t\t\t\trl.on(\"line\", (line) => {\n\t\t\t\t\t\t\tif (!line.trim() || matchCount >= effectiveLimit) return;\n\t\t\t\t\t\t\tlet event: any;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tevent = JSON.parse(line);\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (event.type === \"match\") {\n\t\t\t\t\t\t\t\tmatchCount++;\n\t\t\t\t\t\t\t\tconst filePath = event.data?.path?.text;\n\t\t\t\t\t\t\t\tconst lineNumber = event.data?.line_number;\n\t\t\t\t\t\t\t\tconst lineText = event.data?.lines?.text;\n\t\t\t\t\t\t\t\tif (filePath && typeof lineNumber === \"number\")\n\t\t\t\t\t\t\t\t\tmatches.push({ filePath, lineNumber, lineText });\n\t\t\t\t\t\t\t\tif (matchCount >= effectiveLimit) {\n\t\t\t\t\t\t\t\t\tmatchLimitReached = true;\n\t\t\t\t\t\t\t\t\tstopChild(true);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tchild.on(\"error\", (error) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Failed to run ripgrep: ${error.message}`)));\n\t\t\t\t\t\t});\n\t\t\t\t\t\tchild.on(\"close\", async (code) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tif (aborted) {\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(\"Operation aborted\")));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!killedDueToLimit && code !== 0 && code !== 1) {\n\t\t\t\t\t\t\t\tconst errorMsg = stderr.trim() || `ripgrep exited with code ${code}`;\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(errorMsg)));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (matchCount === 0) {\n\t\t\t\t\t\t\t\tsettle(() =>\n\t\t\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: \"No matches found\" }], details: undefined }),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Format matches after streaming finishes so custom readFile() backends can be async.\n\t\t\t\t\t\t\tfor (const match of matches) {\n\t\t\t\t\t\t\t\tif (contextValue === 0 && match.lineText !== undefined) {\n\t\t\t\t\t\t\t\t\tconst relativePath = formatPath(match.filePath);\n\t\t\t\t\t\t\t\t\tconst sanitized = match.lineText\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r\\n/g, \"\\n\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r/g, \"\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\n$/, \"\");\n\t\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\t\toutputLines.push(`${relativePath}:${match.lineNumber}: ${truncatedText}`);\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tconst block = await formatBlock(match.filePath, match.lineNumber);\n\t\t\t\t\t\t\t\t\toutputLines.push(...block);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst rawOutput = outputLines.join(\"\\n\");\n\t\t\t\t\t\t\t// Apply byte truncation. There is no line limit here because the match limit already capped rows.\n\t\t\t\t\t\t\tconst truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });\n\t\t\t\t\t\t\tlet output = truncation.content;\n\t\t\t\t\t\t\tconst details: GrepToolDetails = {};\n\t\t\t\t\t\t\t// Build actionable notices for truncation and match limits.\n\t\t\t\t\t\t\tconst notices: string[] = [];\n\t\t\t\t\t\t\tif (matchLimitReached) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.matchLimitReached = effectiveLimit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t\tnotices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);\n\t\t\t\t\t\t\t\tdetails.truncation = truncation;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (linesTruncated) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.linesTruncated = true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (notices.length > 0) output += `\\n\\n[${notices.join(\". \")}]`;\n\t\t\t\t\t\t\tsettle(() =>\n\t\t\t\t\t\t\t\tresolve({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: output }],\n\t\t\t\t\t\t\t\t\tdetails: Object.keys(details).length > 0 ? details : undefined,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tsettle(() => reject(err as Error));\n\t\t\t\t\t}\n\t\t\t\t})();\n\t\t\t});\n\t\t},\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepCall(args, theme));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepResult(result as any, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createGrepTool(cwd: string, options?: GrepToolOptions): AgentTool<typeof grepSchema> {\n\treturn wrapToolDefinition(createGrepToolDefinition(cwd, options));\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/extensions/gitnexus/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAmBlD,MAAM,WAAW,aAAa;IAC7B,OAAO,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACjD,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,UAAU,GAAG,MAAM,CAAC;IAExD,YAAY,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,UAAU,GAAG,MAAM,EAI1E;CACD;AAKD,qBAAa,cAAc;IAC1B,OAAO,CAAC,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EAEjC;IAED,gFAAgF;IAC1E,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,CAAC,CAIxG;IAED,gEAAgE;IAChE,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,CAUzC;IAED,OAAO,CAAC,iBAAiB;YASX,UAAU;YAaV,MAAM;YAIN,OAAO;YAUP,IAAI;CA0FlB","sourcesContent":["/**\n * Minimal Streamable-HTTP MCP client for the GitNexus hub — and only the hub.\n *\n * This is intentionally NOT a general MCP client: it implements exactly the\n * subset nexus-cli needs against a single known server — initialize,\n * notifications/initialized, tools/call — over the Streamable HTTP transport\n * (JSON-RPC over POST; responses arrive as application/json or as a\n * text/event-stream carrying JSON-RPC messages).\n */\n\nimport { appendFileSync } from \"node:fs\";\nimport type { GitNexusConfig } from \"./config.ts\";\n\n/** File-based trace (NEXUS_TRACE=1): TUI captures stderr, so console won't do. */\nfunction clientTrace(message: string): void {\n\tif (!process.env.NEXUS_TRACE) return;\n\ttry {\n\t\tappendFileSync(\"/tmp/nexus-observatory-trace.log\", `${new Date().toISOString()} [client] ${message}\\n`);\n\t} catch {\n\t\t// best-effort\n\t}\n}\n\ninterface JsonRpcResponse {\n\tjsonrpc: \"2.0\";\n\tid?: number | string | null;\n\tresult?: unknown;\n\terror?: { code: number; message: string; data?: unknown };\n}\n\nexport interface McpToolResult {\n\tcontent?: Array<{ type: string; text?: string }>;\n\tisError?: boolean;\n\tstructuredContent?: unknown;\n}\n\nexport class GitNexusClientError extends Error {\n\treadonly kind: \"auth\" | \"network\" | \"protocol\" | \"tool\";\n\n\tconstructor(message: string, kind: \"auth\" | \"network\" | \"protocol\" | \"tool\") {\n\t\tsuper(message);\n\t\tthis.name = \"GitNexusClientError\";\n\t\tthis.kind = kind;\n\t}\n}\n\nconst PROTOCOL_VERSION = \"2025-06-18\";\nconst REQUEST_TIMEOUT_MS = 60_000;\n\nexport class GitNexusClient {\n\tprivate sessionId: string | undefined;\n\tprivate nextId = 1;\n\tprivate initializing: Promise<void> | undefined;\n\tprivate readonly config: GitNexusConfig;\n\n\tconstructor(config: GitNexusConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/** Call a hub tool by name. Initializes the MCP session lazily on first use. */\n\tasync callTool(name: string, args: Record<string, unknown>, signal?: AbortSignal): Promise<McpToolResult> {\n\t\tawait this.ensureInitialized(signal);\n\t\tconst result = await this.request(\"tools/call\", { name, arguments: args }, signal);\n\t\treturn (result ?? {}) as McpToolResult;\n\t}\n\n\t/** Extract the primary text payload from an MCP tool result. */\n\tstatic text(result: McpToolResult): string {\n\t\tconst parts = result.content ?? [];\n\t\tconst text = parts\n\t\t\t.filter((p) => p.type === \"text\" && typeof p.text === \"string\")\n\t\t\t.map((p) => p.text)\n\t\t\t.join(\"\\n\");\n\t\tif (result.isError) {\n\t\t\tthrow new GitNexusClientError(text || \"hub tool returned an error\", \"tool\");\n\t\t}\n\t\treturn text;\n\t}\n\n\tprivate ensureInitialized(signal?: AbortSignal): Promise<void> {\n\t\tthis.initializing ??= this.initialize(signal).catch((error) => {\n\t\t\tthis.initializing = undefined;\n\t\t\tthis.sessionId = undefined;\n\t\t\tthrow error;\n\t\t});\n\t\treturn this.initializing;\n\t}\n\n\tprivate async initialize(signal?: AbortSignal): Promise<void> {\n\t\tawait this.request(\n\t\t\t\"initialize\",\n\t\t\t{\n\t\t\t\tprotocolVersion: PROTOCOL_VERSION,\n\t\t\t\tcapabilities: {},\n\t\t\t\tclientInfo: { name: \"nexus-cli\", version: \"0.1.0\" },\n\t\t\t},\n\t\t\tsignal,\n\t\t);\n\t\tawait this.notify(\"notifications/initialized\", signal);\n\t}\n\n\tprivate async notify(method: string, signal?: AbortSignal): Promise<void> {\n\t\tawait this.post({ jsonrpc: \"2.0\", method }, signal);\n\t}\n\n\tprivate async request(method: string, params: unknown, signal?: AbortSignal): Promise<unknown> {\n\t\tconst id = this.nextId++;\n\t\tconst response = await this.post({ jsonrpc: \"2.0\", id, method, params }, signal);\n\t\tif (!response) throw new GitNexusClientError(`empty response for ${method}`, \"protocol\");\n\t\tif (response.error) {\n\t\t\tthrow new GitNexusClientError(`hub ${method} failed: ${response.error.message}`, \"protocol\");\n\t\t}\n\t\treturn response.result;\n\t}\n\n\tprivate async post(body: unknown, signal?: AbortSignal): Promise<JsonRpcResponse | undefined> {\n\t\tsignal?.throwIfAborted();\n\t\tconst controller = new AbortController();\n\t\tconst timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);\n\t\tconst onOuterAbort = () => controller.abort(signal?.reason);\n\t\tconst throwIfTimedOut = () => {\n\t\t\tif (controller.signal.aborted) {\n\t\t\t\tthrow new GitNexusClientError(\"cannot reach GitNexus hub: request timed out\", \"network\");\n\t\t\t}\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onOuterAbort, { once: true });\n\t\ttry {\n\t\t\tconst method = (body as { method?: string }).method ?? \"?\";\n\t\t\tclientTrace(`${method}: fetch dispatch`);\n\t\t\tlet response: Response;\n\t\t\ttry {\n\t\t\t\tresponse = await fetch(this.config.hubUrl, {\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t\t\taccept: \"application/json, text/event-stream\",\n\t\t\t\t\t\tauthorization: `Bearer ${this.config.token}`,\n\t\t\t\t\t\t...(this.config.deviceFingerprint ? { \"x-device-fingerprint\": this.config.deviceFingerprint } : {}),\n\t\t\t\t\t\t...(this.config.deviceName ? { \"x-device-name\": this.config.deviceName } : {}),\n\t\t\t\t\t\t...(this.sessionId ? { \"mcp-session-id\": this.sessionId } : {}),\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\t\tthrow new GitNexusClientError(`cannot reach GitNexus hub: ${reason}`, \"network\");\n\t\t\t}\n\t\t\tsignal?.throwIfAborted();\n\t\t\tthrowIfTimedOut();\n\n\t\t\tclientTrace(`${method}: status ${response.status} content-type ${response.headers.get(\"content-type\")}`);\n\t\t\tconst newSessionId = response.headers.get(\"mcp-session-id\");\n\n\t\t\tif (response.status === 401 || response.status === 403) {\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tthrow new GitNexusClientError(\"GitNexus hub rejected the token (401/403). Run /login.\", \"auth\");\n\t\t\t}\n\t\t\tif (response.status === 202) {\n\t\t\t\t// Drain the body — an unconsumed response pins its socket, and under a\n\t\t\t\t// connection-capped dispatcher that deadlocks every later hub call.\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tif (newSessionId) this.sessionId = newSessionId;\n\t\t\t\treturn undefined; // accepted notification\n\t\t\t}\n\t\t\tif (!response.ok) {\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tthrow new GitNexusClientError(`GitNexus hub HTTP ${response.status}`, \"protocol\");\n\t\t\t}\n\n\t\t\tconst contentType = response.headers.get(\"content-type\") ?? \"\";\n\t\t\tlet text: string;\n\t\t\ttry {\n\t\t\t\ttext = await response.text();\n\t\t\t} catch (error) {\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\t\tthrow new GitNexusClientError(`cannot reach GitNexus hub: ${reason}`, \"network\");\n\t\t\t}\n\t\t\tsignal?.throwIfAborted();\n\t\t\tthrowIfTimedOut();\n\t\t\tlet parsed: JsonRpcResponse | undefined;\n\t\t\tif (contentType.includes(\"text/event-stream\")) {\n\t\t\t\tclientTrace(`${method}: sse body ${text.length} bytes`);\n\t\t\t\tparsed = parseSseForResponse(text);\n\t\t\t} else {\n\t\t\t\tclientTrace(`${method}: json body ${text.length} bytes`);\n\t\t\t\tparsed = text ? (JSON.parse(text) as JsonRpcResponse) : undefined;\n\t\t\t}\n\t\t\tif (newSessionId && parsed) this.sessionId = newSessionId;\n\t\t\treturn parsed;\n\t\t} finally {\n\t\t\tclearTimeout(timeout);\n\t\t\tsignal?.removeEventListener(\"abort\", onOuterAbort);\n\t\t}\n\t}\n}\n\n/** Pull the (single) JSON-RPC response message out of an SSE body. */\nfunction parseSseForResponse(sse: string): JsonRpcResponse | undefined {\n\tlet last: JsonRpcResponse | undefined;\n\tfor (const rawEvent of sse.split(\"\\n\\n\")) {\n\t\tconst data = rawEvent\n\t\t\t.split(\"\\n\")\n\t\t\t.filter((line) => line.startsWith(\"data:\"))\n\t\t\t.map((line) => line.slice(5).trim())\n\t\t\t.join(\"\\n\");\n\t\tif (!data) continue;\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(data) as JsonRpcResponse;\n\t\t\t// Responses carry an id; server-initiated notifications do not.\n\t\t\tif (parsed.id !== undefined && parsed.id !== null) last = parsed;\n\t\t} catch {\n\t\t\t// Ignore non-JSON keepalives.\n\t\t}\n\t}\n\treturn last;\n}\n"]}
|
|
@@ -58,6 +58,7 @@ export class GitNexusClient {
|
|
|
58
58
|
ensureInitialized(signal) {
|
|
59
59
|
this.initializing ??= this.initialize(signal).catch((error) => {
|
|
60
60
|
this.initializing = undefined;
|
|
61
|
+
this.sessionId = undefined;
|
|
61
62
|
throw error;
|
|
62
63
|
});
|
|
63
64
|
return this.initializing;
|
|
@@ -68,10 +69,10 @@ export class GitNexusClient {
|
|
|
68
69
|
capabilities: {},
|
|
69
70
|
clientInfo: { name: "nexus-cli", version: "0.1.0" },
|
|
70
71
|
}, signal);
|
|
71
|
-
await this.notify("notifications/initialized");
|
|
72
|
+
await this.notify("notifications/initialized", signal);
|
|
72
73
|
}
|
|
73
|
-
async notify(method) {
|
|
74
|
-
await this.post({ jsonrpc: "2.0", method },
|
|
74
|
+
async notify(method, signal) {
|
|
75
|
+
await this.post({ jsonrpc: "2.0", method }, signal);
|
|
75
76
|
}
|
|
76
77
|
async request(method, params, signal) {
|
|
77
78
|
const id = this.nextId++;
|
|
@@ -84,9 +85,15 @@ export class GitNexusClient {
|
|
|
84
85
|
return response.result;
|
|
85
86
|
}
|
|
86
87
|
async post(body, signal) {
|
|
88
|
+
signal?.throwIfAborted();
|
|
87
89
|
const controller = new AbortController();
|
|
88
90
|
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
|
|
89
|
-
const onOuterAbort = () => controller.abort();
|
|
91
|
+
const onOuterAbort = () => controller.abort(signal?.reason);
|
|
92
|
+
const throwIfTimedOut = () => {
|
|
93
|
+
if (controller.signal.aborted) {
|
|
94
|
+
throw new GitNexusClientError("cannot reach GitNexus hub: request timed out", "network");
|
|
95
|
+
}
|
|
96
|
+
};
|
|
90
97
|
signal?.addEventListener("abort", onOuterAbort, { once: true });
|
|
91
98
|
try {
|
|
92
99
|
const method = body.method ?? "?";
|
|
@@ -108,38 +115,62 @@ export class GitNexusClient {
|
|
|
108
115
|
});
|
|
109
116
|
}
|
|
110
117
|
catch (error) {
|
|
118
|
+
signal?.throwIfAborted();
|
|
119
|
+
throwIfTimedOut();
|
|
111
120
|
const reason = error instanceof Error ? error.message : String(error);
|
|
112
121
|
throw new GitNexusClientError(`cannot reach GitNexus hub: ${reason}`, "network");
|
|
113
122
|
}
|
|
123
|
+
signal?.throwIfAborted();
|
|
124
|
+
throwIfTimedOut();
|
|
114
125
|
clientTrace(`${method}: status ${response.status} content-type ${response.headers.get("content-type")}`);
|
|
115
126
|
const newSessionId = response.headers.get("mcp-session-id");
|
|
116
|
-
if (newSessionId)
|
|
117
|
-
this.sessionId = newSessionId;
|
|
118
127
|
if (response.status === 401 || response.status === 403) {
|
|
119
128
|
await response.body?.cancel().catch(() => { });
|
|
129
|
+
signal?.throwIfAborted();
|
|
130
|
+
throwIfTimedOut();
|
|
120
131
|
throw new GitNexusClientError("GitNexus hub rejected the token (401/403). Run /login.", "auth");
|
|
121
132
|
}
|
|
122
133
|
if (response.status === 202) {
|
|
123
134
|
// Drain the body — an unconsumed response pins its socket, and under a
|
|
124
135
|
// connection-capped dispatcher that deadlocks every later hub call.
|
|
125
136
|
await response.body?.cancel().catch(() => { });
|
|
137
|
+
signal?.throwIfAborted();
|
|
138
|
+
throwIfTimedOut();
|
|
139
|
+
if (newSessionId)
|
|
140
|
+
this.sessionId = newSessionId;
|
|
126
141
|
return undefined; // accepted notification
|
|
127
142
|
}
|
|
128
143
|
if (!response.ok) {
|
|
129
144
|
await response.body?.cancel().catch(() => { });
|
|
145
|
+
signal?.throwIfAborted();
|
|
146
|
+
throwIfTimedOut();
|
|
130
147
|
throw new GitNexusClientError(`GitNexus hub HTTP ${response.status}`, "protocol");
|
|
131
148
|
}
|
|
132
149
|
const contentType = response.headers.get("content-type") ?? "";
|
|
150
|
+
let text;
|
|
151
|
+
try {
|
|
152
|
+
text = await response.text();
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
signal?.throwIfAborted();
|
|
156
|
+
throwIfTimedOut();
|
|
157
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
158
|
+
throw new GitNexusClientError(`cannot reach GitNexus hub: ${reason}`, "network");
|
|
159
|
+
}
|
|
160
|
+
signal?.throwIfAborted();
|
|
161
|
+
throwIfTimedOut();
|
|
162
|
+
let parsed;
|
|
133
163
|
if (contentType.includes("text/event-stream")) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return parseSseForResponse(sse);
|
|
164
|
+
clientTrace(`${method}: sse body ${text.length} bytes`);
|
|
165
|
+
parsed = parseSseForResponse(text);
|
|
137
166
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
167
|
+
else {
|
|
168
|
+
clientTrace(`${method}: json body ${text.length} bytes`);
|
|
169
|
+
parsed = text ? JSON.parse(text) : undefined;
|
|
170
|
+
}
|
|
171
|
+
if (newSessionId && parsed)
|
|
172
|
+
this.sessionId = newSessionId;
|
|
173
|
+
return parsed;
|
|
143
174
|
}
|
|
144
175
|
finally {
|
|
145
176
|
clearTimeout(timeout);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/extensions/gitnexus/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAGzC,kFAAkF;AAClF,SAAS,WAAW,CAAC,OAAe,EAAQ;IAC3C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW;QAAE,OAAO;IACrC,IAAI,CAAC;QACJ,cAAc,CAAC,kCAAkC,EAAE,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,aAAa,OAAO,IAAI,CAAC,CAAC;IACzG,CAAC;IAAC,MAAM,CAAC;QACR,cAAc;IACf,CAAC;AAAA,CACD;AAeD,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACpC,IAAI,CAA2C;IAExD,YAAY,OAAe,EAAE,IAA8C,EAAE;QAC5E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAAA,CACjB;CACD;AAED,MAAM,gBAAgB,GAAG,YAAY,CAAC;AACtC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,MAAM,OAAO,cAAc;IAClB,SAAS,CAAqB;IAC9B,MAAM,GAAG,CAAC,CAAC;IACX,YAAY,CAA4B;IAC/B,MAAM,CAAiB;IAExC,YAAY,MAAsB,EAAE;QACnC,IAAI,CAA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b,IAAI,CAAC,IAAI;YAAE,SAAS;QACpB,IAAI,CAAC;YACJ,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAoB,CAAC;YACnD,gEAAgE;YAChE,IAAI,MAAM,CAAC,EAAE,KAAK,SAAS,IAAI,MAAM,CAAC,EAAE,KAAK,IAAI;gBAAE,IAAI,GAAG,MAAM,CAAC;QAClE,CAAC;QAAC,MAAM,CAAC;YACR,8BAA8B;QAC/B,CAAC;IACF,CAAC;IACD,OAAO,IAAI,CAAC;AAAA,CACZ","sourcesContent":["/**\n * Minimal Streamable-HTTP MCP client for the GitNexus hub — and only the hub.\n *\n * This is intentionally NOT a general MCP client: it implements exactly the\n * subset nexus-cli needs against a single known server — initialize,\n * notifications/initialized, tools/call — over the Streamable HTTP transport\n * (JSON-RPC over POST; responses arrive as application/json or as a\n * text/event-stream carrying JSON-RPC messages).\n */\n\nimport { appendFileSync } from \"node:fs\";\nimport type { GitNexusConfig } from \"./config.ts\";\n\n/** File-based trace (NEXUS_TRACE=1): TUI captures stderr, so console won't do. */\nfunction clientTrace(message: string): void {\n\tif (!process.env.NEXUS_TRACE) return;\n\ttry {\n\t\tappendFileSync(\"/tmp/nexus-observatory-trace.log\", `${new Date().toISOString()} [client] ${message}\\n`);\n\t} catch {\n\t\t// best-effort\n\t}\n}\n\ninterface JsonRpcResponse {\n\tjsonrpc: \"2.0\";\n\tid?: number | string | null;\n\tresult?: unknown;\n\terror?: { code: number; message: string; data?: unknown };\n}\n\nexport interface McpToolResult {\n\tcontent?: Array<{ type: string; text?: string }>;\n\tisError?: boolean;\n\tstructuredContent?: unknown;\n}\n\nexport class GitNexusClientError extends Error {\n\treadonly kind: \"auth\" | \"network\" | \"protocol\" | \"tool\";\n\n\tconstructor(message: string, kind: \"auth\" | \"network\" | \"protocol\" | \"tool\") {\n\t\tsuper(message);\n\t\tthis.name = \"GitNexusClientError\";\n\t\tthis.kind = kind;\n\t}\n}\n\nconst PROTOCOL_VERSION = \"2025-06-18\";\nconst REQUEST_TIMEOUT_MS = 60_000;\n\nexport class GitNexusClient {\n\tprivate sessionId: string | undefined;\n\tprivate nextId = 1;\n\tprivate initializing: Promise<void> | undefined;\n\tprivate readonly config: GitNexusConfig;\n\n\tconstructor(config: GitNexusConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/** Call a hub tool by name. Initializes the MCP session lazily on first use. */\n\tasync callTool(name: string, args: Record<string, unknown>, signal?: AbortSignal): Promise<McpToolResult> {\n\t\tawait this.ensureInitialized(signal);\n\t\tconst result = await this.request(\"tools/call\", { name, arguments: args }, signal);\n\t\treturn (result ?? {}) as McpToolResult;\n\t}\n\n\t/** Extract the primary text payload from an MCP tool result. */\n\tstatic text(result: McpToolResult): string {\n\t\tconst parts = result.content ?? [];\n\t\tconst text = parts\n\t\t\t.filter((p) => p.type === \"text\" && typeof p.text === \"string\")\n\t\t\t.map((p) => p.text)\n\t\t\t.join(\"\\n\");\n\t\tif (result.isError) {\n\t\t\tthrow new GitNexusClientError(text || \"hub tool returned an error\", \"tool\");\n\t\t}\n\t\treturn text;\n\t}\n\n\tprivate ensureInitialized(signal?: AbortSignal): Promise<void> {\n\t\tthis.initializing ??= this.initialize(signal).catch((error) => {\n\t\t\tthis.initializing = undefined;\n\t\t\tthrow error;\n\t\t});\n\t\treturn this.initializing;\n\t}\n\n\tprivate async initialize(signal?: AbortSignal): Promise<void> {\n\t\tawait this.request(\n\t\t\t\"initialize\",\n\t\t\t{\n\t\t\t\tprotocolVersion: PROTOCOL_VERSION,\n\t\t\t\tcapabilities: {},\n\t\t\t\tclientInfo: { name: \"nexus-cli\", version: \"0.1.0\" },\n\t\t\t},\n\t\t\tsignal,\n\t\t);\n\t\tawait this.notify(\"notifications/initialized\");\n\t}\n\n\tprivate async notify(method: string): Promise<void> {\n\t\tawait this.post({ jsonrpc: \"2.0\", method }, undefined);\n\t}\n\n\tprivate async request(method: string, params: unknown, signal?: AbortSignal): Promise<unknown> {\n\t\tconst id = this.nextId++;\n\t\tconst response = await this.post({ jsonrpc: \"2.0\", id, method, params }, signal);\n\t\tif (!response) throw new GitNexusClientError(`empty response for ${method}`, \"protocol\");\n\t\tif (response.error) {\n\t\t\tthrow new GitNexusClientError(`hub ${method} failed: ${response.error.message}`, \"protocol\");\n\t\t}\n\t\treturn response.result;\n\t}\n\n\tprivate async post(body: unknown, signal?: AbortSignal): Promise<JsonRpcResponse | undefined> {\n\t\tconst controller = new AbortController();\n\t\tconst timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);\n\t\tconst onOuterAbort = () => controller.abort();\n\t\tsignal?.addEventListener(\"abort\", onOuterAbort, { once: true });\n\t\ttry {\n\t\t\tconst method = (body as { method?: string }).method ?? \"?\";\n\t\t\tclientTrace(`${method}: fetch dispatch`);\n\t\t\tlet response: Response;\n\t\t\ttry {\n\t\t\t\tresponse = await fetch(this.config.hubUrl, {\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t\t\taccept: \"application/json, text/event-stream\",\n\t\t\t\t\t\tauthorization: `Bearer ${this.config.token}`,\n\t\t\t\t\t\t...(this.config.deviceFingerprint ? { \"x-device-fingerprint\": this.config.deviceFingerprint } : {}),\n\t\t\t\t\t\t...(this.config.deviceName ? { \"x-device-name\": this.config.deviceName } : {}),\n\t\t\t\t\t\t...(this.sessionId ? { \"mcp-session-id\": this.sessionId } : {}),\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\t\tthrow new GitNexusClientError(`cannot reach GitNexus hub: ${reason}`, \"network\");\n\t\t\t}\n\n\t\t\tclientTrace(`${method}: status ${response.status} content-type ${response.headers.get(\"content-type\")}`);\n\t\t\tconst newSessionId = response.headers.get(\"mcp-session-id\");\n\t\t\tif (newSessionId) this.sessionId = newSessionId;\n\n\t\t\tif (response.status === 401 || response.status === 403) {\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tthrow new GitNexusClientError(\"GitNexus hub rejected the token (401/403). Run /login.\", \"auth\");\n\t\t\t}\n\t\t\tif (response.status === 202) {\n\t\t\t\t// Drain the body — an unconsumed response pins its socket, and under a\n\t\t\t\t// connection-capped dispatcher that deadlocks every later hub call.\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\treturn undefined; // accepted notification\n\t\t\t}\n\t\t\tif (!response.ok) {\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tthrow new GitNexusClientError(`GitNexus hub HTTP ${response.status}`, \"protocol\");\n\t\t\t}\n\n\t\t\tconst contentType = response.headers.get(\"content-type\") ?? \"\";\n\t\t\tif (contentType.includes(\"text/event-stream\")) {\n\t\t\t\tconst sse = await response.text();\n\t\t\t\tclientTrace(`${method}: sse body ${sse.length} bytes`);\n\t\t\t\treturn parseSseForResponse(sse);\n\t\t\t}\n\t\t\tconst text = await response.text();\n\t\t\tclientTrace(`${method}: json body ${text.length} bytes`);\n\t\t\tif (!text) return undefined;\n\t\t\treturn JSON.parse(text) as JsonRpcResponse;\n\t\t} finally {\n\t\t\tclearTimeout(timeout);\n\t\t\tsignal?.removeEventListener(\"abort\", onOuterAbort);\n\t\t}\n\t}\n}\n\n/** Pull the (single) JSON-RPC response message out of an SSE body. */\nfunction parseSseForResponse(sse: string): JsonRpcResponse | undefined {\n\tlet last: JsonRpcResponse | undefined;\n\tfor (const rawEvent of sse.split(\"\\n\\n\")) {\n\t\tconst data = rawEvent\n\t\t\t.split(\"\\n\")\n\t\t\t.filter((line) => line.startsWith(\"data:\"))\n\t\t\t.map((line) => line.slice(5).trim())\n\t\t\t.join(\"\\n\");\n\t\tif (!data) continue;\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(data) as JsonRpcResponse;\n\t\t\t// Responses carry an id; server-initiated notifications do not.\n\t\t\tif (parsed.id !== undefined && parsed.id !== null) last = parsed;\n\t\t} catch {\n\t\t\t// Ignore non-JSON keepalives.\n\t\t}\n\t}\n\treturn last;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/extensions/gitnexus/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAGzC,kFAAkF;AAClF,SAAS,WAAW,CAAC,OAAe,EAAQ;IAC3C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW;QAAE,OAAO;IACrC,IAAI,CAAC;QACJ,cAAc,CAAC,kCAAkC,EAAE,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,aAAa,OAAO,IAAI,CAAC,CAAC;IACzG,CAAC;IAAC,MAAM,CAAC;QACR,cAAc;IACf,CAAC;AAAA,CACD;AAeD,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACpC,IAAI,CAA2C;IAExD,YAAY,OAAe,EAAE,IAA8C,EAAE;QAC5E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAAA,CACjB;CACD;AAED,MAAM,gBAAgB,GAAG,YAAY,CAAC;AACtC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAElC,MAAM,OAAO,cAAc;IAClB,SAAS,CAAqB;IAC9B,MAAM,GAAG,CAAC,CAAC;IACX,YAAY,CAA4B;IAC/B,MAAM,CAAiB;IAExC,YAAY,MAAsB,EAAE;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IAAA,CACrB;IAED,gFAAgF;IAChF,KAAK,CAAC,QAAQ,CAAC,IAAY,EAAE,IAA6B,EAAE,MAAoB,EAA0B;QACzG,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;QACrC,MAAM,MAAM,GAAG,MAA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CAAC;IACF,CAAC;IACD,OAAO,IAAI,CAAC;AAAA,CACZ","sourcesContent":["/**\n * Minimal Streamable-HTTP MCP client for the GitNexus hub — and only the hub.\n *\n * This is intentionally NOT a general MCP client: it implements exactly the\n * subset nexus-cli needs against a single known server — initialize,\n * notifications/initialized, tools/call — over the Streamable HTTP transport\n * (JSON-RPC over POST; responses arrive as application/json or as a\n * text/event-stream carrying JSON-RPC messages).\n */\n\nimport { appendFileSync } from \"node:fs\";\nimport type { GitNexusConfig } from \"./config.ts\";\n\n/** File-based trace (NEXUS_TRACE=1): TUI captures stderr, so console won't do. */\nfunction clientTrace(message: string): void {\n\tif (!process.env.NEXUS_TRACE) return;\n\ttry {\n\t\tappendFileSync(\"/tmp/nexus-observatory-trace.log\", `${new Date().toISOString()} [client] ${message}\\n`);\n\t} catch {\n\t\t// best-effort\n\t}\n}\n\ninterface JsonRpcResponse {\n\tjsonrpc: \"2.0\";\n\tid?: number | string | null;\n\tresult?: unknown;\n\terror?: { code: number; message: string; data?: unknown };\n}\n\nexport interface McpToolResult {\n\tcontent?: Array<{ type: string; text?: string }>;\n\tisError?: boolean;\n\tstructuredContent?: unknown;\n}\n\nexport class GitNexusClientError extends Error {\n\treadonly kind: \"auth\" | \"network\" | \"protocol\" | \"tool\";\n\n\tconstructor(message: string, kind: \"auth\" | \"network\" | \"protocol\" | \"tool\") {\n\t\tsuper(message);\n\t\tthis.name = \"GitNexusClientError\";\n\t\tthis.kind = kind;\n\t}\n}\n\nconst PROTOCOL_VERSION = \"2025-06-18\";\nconst REQUEST_TIMEOUT_MS = 60_000;\n\nexport class GitNexusClient {\n\tprivate sessionId: string | undefined;\n\tprivate nextId = 1;\n\tprivate initializing: Promise<void> | undefined;\n\tprivate readonly config: GitNexusConfig;\n\n\tconstructor(config: GitNexusConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/** Call a hub tool by name. Initializes the MCP session lazily on first use. */\n\tasync callTool(name: string, args: Record<string, unknown>, signal?: AbortSignal): Promise<McpToolResult> {\n\t\tawait this.ensureInitialized(signal);\n\t\tconst result = await this.request(\"tools/call\", { name, arguments: args }, signal);\n\t\treturn (result ?? {}) as McpToolResult;\n\t}\n\n\t/** Extract the primary text payload from an MCP tool result. */\n\tstatic text(result: McpToolResult): string {\n\t\tconst parts = result.content ?? [];\n\t\tconst text = parts\n\t\t\t.filter((p) => p.type === \"text\" && typeof p.text === \"string\")\n\t\t\t.map((p) => p.text)\n\t\t\t.join(\"\\n\");\n\t\tif (result.isError) {\n\t\t\tthrow new GitNexusClientError(text || \"hub tool returned an error\", \"tool\");\n\t\t}\n\t\treturn text;\n\t}\n\n\tprivate ensureInitialized(signal?: AbortSignal): Promise<void> {\n\t\tthis.initializing ??= this.initialize(signal).catch((error) => {\n\t\t\tthis.initializing = undefined;\n\t\t\tthis.sessionId = undefined;\n\t\t\tthrow error;\n\t\t});\n\t\treturn this.initializing;\n\t}\n\n\tprivate async initialize(signal?: AbortSignal): Promise<void> {\n\t\tawait this.request(\n\t\t\t\"initialize\",\n\t\t\t{\n\t\t\t\tprotocolVersion: PROTOCOL_VERSION,\n\t\t\t\tcapabilities: {},\n\t\t\t\tclientInfo: { name: \"nexus-cli\", version: \"0.1.0\" },\n\t\t\t},\n\t\t\tsignal,\n\t\t);\n\t\tawait this.notify(\"notifications/initialized\", signal);\n\t}\n\n\tprivate async notify(method: string, signal?: AbortSignal): Promise<void> {\n\t\tawait this.post({ jsonrpc: \"2.0\", method }, signal);\n\t}\n\n\tprivate async request(method: string, params: unknown, signal?: AbortSignal): Promise<unknown> {\n\t\tconst id = this.nextId++;\n\t\tconst response = await this.post({ jsonrpc: \"2.0\", id, method, params }, signal);\n\t\tif (!response) throw new GitNexusClientError(`empty response for ${method}`, \"protocol\");\n\t\tif (response.error) {\n\t\t\tthrow new GitNexusClientError(`hub ${method} failed: ${response.error.message}`, \"protocol\");\n\t\t}\n\t\treturn response.result;\n\t}\n\n\tprivate async post(body: unknown, signal?: AbortSignal): Promise<JsonRpcResponse | undefined> {\n\t\tsignal?.throwIfAborted();\n\t\tconst controller = new AbortController();\n\t\tconst timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);\n\t\tconst onOuterAbort = () => controller.abort(signal?.reason);\n\t\tconst throwIfTimedOut = () => {\n\t\t\tif (controller.signal.aborted) {\n\t\t\t\tthrow new GitNexusClientError(\"cannot reach GitNexus hub: request timed out\", \"network\");\n\t\t\t}\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onOuterAbort, { once: true });\n\t\ttry {\n\t\t\tconst method = (body as { method?: string }).method ?? \"?\";\n\t\t\tclientTrace(`${method}: fetch dispatch`);\n\t\t\tlet response: Response;\n\t\t\ttry {\n\t\t\t\tresponse = await fetch(this.config.hubUrl, {\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t\t\taccept: \"application/json, text/event-stream\",\n\t\t\t\t\t\tauthorization: `Bearer ${this.config.token}`,\n\t\t\t\t\t\t...(this.config.deviceFingerprint ? { \"x-device-fingerprint\": this.config.deviceFingerprint } : {}),\n\t\t\t\t\t\t...(this.config.deviceName ? { \"x-device-name\": this.config.deviceName } : {}),\n\t\t\t\t\t\t...(this.sessionId ? { \"mcp-session-id\": this.sessionId } : {}),\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\t\tthrow new GitNexusClientError(`cannot reach GitNexus hub: ${reason}`, \"network\");\n\t\t\t}\n\t\t\tsignal?.throwIfAborted();\n\t\t\tthrowIfTimedOut();\n\n\t\t\tclientTrace(`${method}: status ${response.status} content-type ${response.headers.get(\"content-type\")}`);\n\t\t\tconst newSessionId = response.headers.get(\"mcp-session-id\");\n\n\t\t\tif (response.status === 401 || response.status === 403) {\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tthrow new GitNexusClientError(\"GitNexus hub rejected the token (401/403). Run /login.\", \"auth\");\n\t\t\t}\n\t\t\tif (response.status === 202) {\n\t\t\t\t// Drain the body — an unconsumed response pins its socket, and under a\n\t\t\t\t// connection-capped dispatcher that deadlocks every later hub call.\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tif (newSessionId) this.sessionId = newSessionId;\n\t\t\t\treturn undefined; // accepted notification\n\t\t\t}\n\t\t\tif (!response.ok) {\n\t\t\t\tawait response.body?.cancel().catch(() => {});\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tthrow new GitNexusClientError(`GitNexus hub HTTP ${response.status}`, \"protocol\");\n\t\t\t}\n\n\t\t\tconst contentType = response.headers.get(\"content-type\") ?? \"\";\n\t\t\tlet text: string;\n\t\t\ttry {\n\t\t\t\ttext = await response.text();\n\t\t\t} catch (error) {\n\t\t\t\tsignal?.throwIfAborted();\n\t\t\t\tthrowIfTimedOut();\n\t\t\t\tconst reason = error instanceof Error ? error.message : String(error);\n\t\t\t\tthrow new GitNexusClientError(`cannot reach GitNexus hub: ${reason}`, \"network\");\n\t\t\t}\n\t\t\tsignal?.throwIfAborted();\n\t\t\tthrowIfTimedOut();\n\t\t\tlet parsed: JsonRpcResponse | undefined;\n\t\t\tif (contentType.includes(\"text/event-stream\")) {\n\t\t\t\tclientTrace(`${method}: sse body ${text.length} bytes`);\n\t\t\t\tparsed = parseSseForResponse(text);\n\t\t\t} else {\n\t\t\t\tclientTrace(`${method}: json body ${text.length} bytes`);\n\t\t\t\tparsed = text ? (JSON.parse(text) as JsonRpcResponse) : undefined;\n\t\t\t}\n\t\t\tif (newSessionId && parsed) this.sessionId = newSessionId;\n\t\t\treturn parsed;\n\t\t} finally {\n\t\t\tclearTimeout(timeout);\n\t\t\tsignal?.removeEventListener(\"abort\", onOuterAbort);\n\t\t}\n\t}\n}\n\n/** Pull the (single) JSON-RPC response message out of an SSE body. */\nfunction parseSseForResponse(sse: string): JsonRpcResponse | undefined {\n\tlet last: JsonRpcResponse | undefined;\n\tfor (const rawEvent of sse.split(\"\\n\\n\")) {\n\t\tconst data = rawEvent\n\t\t\t.split(\"\\n\")\n\t\t\t.filter((line) => line.startsWith(\"data:\"))\n\t\t\t.map((line) => line.slice(5).trim())\n\t\t\t.join(\"\\n\");\n\t\tif (!data) continue;\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(data) as JsonRpcResponse;\n\t\t\t// Responses carry an id; server-initiated notifications do not.\n\t\t\tif (parsed.id !== undefined && parsed.id !== null) last = parsed;\n\t\t} catch {\n\t\t\t// Ignore non-JSON keepalives.\n\t\t}\n\t}\n\treturn last;\n}\n"]}
|
|
@@ -2,8 +2,13 @@
|
|
|
2
2
|
* GitNexus hub connection settings.
|
|
3
3
|
*
|
|
4
4
|
* Resolution order (first hit wins):
|
|
5
|
-
* 1. Environment: GITNEXUS_HUB_URL
|
|
5
|
+
* 1. Environment: GITNEXUS_HUB_URL, then HUB_URL (process env or a `.env` file)
|
|
6
6
|
* 2. Config file: ~/.config/nexus/auth.json ({ hubUrl, token })
|
|
7
|
+
* 3. Production default
|
|
8
|
+
*
|
|
9
|
+
* HUB_URL is the hub origin (`https://host`). The MCP client needs the
|
|
10
|
+
* `.../mcp` endpoint, so a bare origin gets `/mcp` appended. GITNEXUS_HUB_URL
|
|
11
|
+
* may already be that endpoint. Token: GITNEXUS_TOKEN, then the config file.
|
|
7
12
|
*
|
|
8
13
|
* The hub endpoint is the Streamable-HTTP MCP endpoint (".../mcp"). Bearer
|
|
9
14
|
* tokens are hub API tokens; an empty token allowlist on the hub means the
|
|
@@ -17,6 +22,15 @@ export interface GitNexusConfig {
|
|
|
17
22
|
deviceName?: string | undefined;
|
|
18
23
|
}
|
|
19
24
|
export declare const DEFAULT_HUB_URL = "https://gitnexus-enterprise-production.up.railway.app/mcp";
|
|
25
|
+
/** MCP endpoint for a hub origin or an already-qualified `.../mcp` URL. */
|
|
26
|
+
export declare function toMcpHubUrl(value: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Hub MCP endpoint.
|
|
29
|
+
*
|
|
30
|
+
* GITNEXUS_HUB_URL, then HUB_URL from the environment or `.env`, then a saved
|
|
31
|
+
* auth.json value, then the production default.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveHubUrl(savedHubUrl?: string): string;
|
|
20
34
|
/** Resolve hub settings. Returns undefined when no token is configured anywhere. */
|
|
21
35
|
export declare function resolveGitNexusConfig(): GitNexusConfig | undefined;
|
|
22
36
|
/** Persist hub settings from `/login`. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/extensions/gitnexus/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/extensions/gitnexus/config.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,WAAW,cAAc;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,iFAAiF;IACjF,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC;AAED,eAAO,MAAM,eAAe,8DAA8D,CAAC;AAE3F,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIjD;AA0DD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAK1D;AAoBD,oFAAoF;AACpF,wBAAgB,qBAAqB,IAAI,cAAc,GAAG,SAAS,CAQlE;AAED,0CAA0C;AAC1C,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,CAIjE;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAE3C","sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\n/**\n * GitNexus hub connection settings.\n *\n * Resolution order (first hit wins):\n * 1. Environment: GITNEXUS_HUB_URL, then HUB_URL (process env or a `.env` file)\n * 2. Config file: ~/.config/nexus/auth.json ({ hubUrl, token })\n * 3. Production default\n *\n * HUB_URL is the hub origin (`https://host`). The MCP client needs the\n * `.../mcp` endpoint, so a bare origin gets `/mcp` appended. GITNEXUS_HUB_URL\n * may already be that endpoint. Token: GITNEXUS_TOKEN, then the config file.\n *\n * The hub endpoint is the Streamable-HTTP MCP endpoint (\".../mcp\"). Bearer\n * tokens are hub API tokens; an empty token allowlist on the hub means the\n * token can see every repo of its user identity.\n */\n\nexport interface GitNexusConfig {\n\thubUrl: string;\n\ttoken: string;\n\t/** Hub tokens can be device-bound; these headers must then match the binding. */\n\tdeviceFingerprint?: string | undefined;\n\tdeviceName?: string | undefined;\n}\n\nexport const DEFAULT_HUB_URL = \"https://gitnexus-enterprise-production.up.railway.app/mcp\";\n\n/** MCP endpoint for a hub origin or an already-qualified `.../mcp` URL. */\nexport function toMcpHubUrl(value: string): string {\n\tconst trimmed = value.trim().replace(/\\/+$/, \"\");\n\tif (trimmed.endsWith(\"/mcp\")) return trimmed;\n\treturn `${trimmed}/mcp`;\n}\n\nfunction readHubUrlKey(path: string): string | undefined {\n\tlet raw: string;\n\ttry {\n\t\traw = readFileSync(path, \"utf8\");\n\t} catch {\n\t\treturn undefined;\n\t}\n\tfor (const line of raw.split(\"\\n\")) {\n\t\tconst trimmed = line.trim();\n\t\tif (!trimmed || trimmed.startsWith(\"#\")) continue;\n\t\tconst eq = trimmed.indexOf(\"=\");\n\t\tif (eq <= 0) continue;\n\t\tif (trimmed.slice(0, eq).trim() !== \"HUB_URL\") continue;\n\t\tlet value = trimmed.slice(eq + 1).trim();\n\t\tif ((value.startsWith('\"') && value.endsWith('\"')) || (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n\t\t\tvalue = value.slice(1, -1);\n\t\t}\n\t\treturn value || undefined;\n\t}\n\treturn undefined;\n}\n\n/**\n * HUB_URL from a `.env` file next to this checkout.\n *\n * Walks up from the working directory and from this module so `npm start`\n * from the repo root and a launch from a nested package both find\n * `nexus-cli/.env`. Skipped under Vitest so a developer `.env` does not\n * change the default the tests assert.\n */\nfunction readDotEnvHubUrl(): string | undefined {\n\tif (process.env.VITEST) return undefined;\n\tconst starts = [process.cwd()];\n\ttry {\n\t\tstarts.push(dirname(fileURLToPath(import.meta.url)));\n\t} catch {\n\t\t// import.meta.url is always present in the ESM build; ignore if a test bundler drops it.\n\t}\n\tconst seen = new Set<string>();\n\tfor (const start of starts) {\n\t\tlet dir = start;\n\t\tfor (let depth = 0; depth < 8; depth++) {\n\t\t\tconst candidate = join(dir, \".env\");\n\t\t\tif (!seen.has(candidate)) {\n\t\t\t\tseen.add(candidate);\n\t\t\t\tconst value = readHubUrlKey(candidate);\n\t\t\t\tif (value) return value;\n\t\t\t}\n\t\t\tconst parent = dirname(dir);\n\t\t\tif (parent === dir) break;\n\t\t\tdir = parent;\n\t\t}\n\t}\n\treturn undefined;\n}\n\n/**\n * Hub MCP endpoint.\n *\n * GITNEXUS_HUB_URL, then HUB_URL from the environment or `.env`, then a saved\n * auth.json value, then the production default.\n */\nexport function resolveHubUrl(savedHubUrl?: string): string {\n\tconst fromEnv = process.env.GITNEXUS_HUB_URL?.trim() || process.env.HUB_URL?.trim() || readDotEnvHubUrl();\n\tif (fromEnv) return toMcpHubUrl(fromEnv);\n\tif (savedHubUrl) return savedHubUrl;\n\treturn DEFAULT_HUB_URL;\n}\n\nfunction configDir(): string {\n\treturn join(homedir(), \".config\", \"nexus\");\n}\n\nfunction configPath(): string {\n\treturn join(configDir(), \"auth.json\");\n}\n\nfunction readConfigFile(): Partial<GitNexusConfig> {\n\ttry {\n\t\tconst raw = readFileSync(configPath(), \"utf8\");\n\t\tconst parsed = JSON.parse(raw) as Partial<GitNexusConfig>;\n\t\treturn typeof parsed === \"object\" && parsed !== null ? parsed : {};\n\t} catch {\n\t\treturn {};\n\t}\n}\n\n/** Resolve hub settings. Returns undefined when no token is configured anywhere. */\nexport function resolveGitNexusConfig(): GitNexusConfig | undefined {\n\tconst file = readConfigFile();\n\tconst token = process.env.GITNEXUS_TOKEN ?? file.token;\n\tif (!token) return undefined;\n\tconst hubUrl = resolveHubUrl(file.hubUrl);\n\tconst deviceFingerprint = process.env.GITNEXUS_DEVICE_FP ?? file.deviceFingerprint;\n\tconst deviceName = process.env.GITNEXUS_DEVICE_NAME ?? file.deviceName;\n\treturn { hubUrl, token, deviceFingerprint, deviceName };\n}\n\n/** Persist hub settings from `/login`. */\nexport function saveGitNexusConfig(config: GitNexusConfig): string {\n\tif (!existsSync(configDir())) mkdirSync(configDir(), { recursive: true, mode: 0o700 });\n\twriteFileSync(configPath(), `${JSON.stringify(config, null, \"\\t\")}\\n`, { mode: 0o600 });\n\treturn configPath();\n}\n\nexport function gitNexusConfigPath(): string {\n\treturn configPath();\n}\n"]}
|
|
@@ -1,7 +1,91 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
4
5
|
export const DEFAULT_HUB_URL = "https://gitnexus-enterprise-production.up.railway.app/mcp";
|
|
6
|
+
/** MCP endpoint for a hub origin or an already-qualified `.../mcp` URL. */
|
|
7
|
+
export function toMcpHubUrl(value) {
|
|
8
|
+
const trimmed = value.trim().replace(/\/+$/, "");
|
|
9
|
+
if (trimmed.endsWith("/mcp"))
|
|
10
|
+
return trimmed;
|
|
11
|
+
return `${trimmed}/mcp`;
|
|
12
|
+
}
|
|
13
|
+
function readHubUrlKey(path) {
|
|
14
|
+
let raw;
|
|
15
|
+
try {
|
|
16
|
+
raw = readFileSync(path, "utf8");
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
for (const line of raw.split("\n")) {
|
|
22
|
+
const trimmed = line.trim();
|
|
23
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
24
|
+
continue;
|
|
25
|
+
const eq = trimmed.indexOf("=");
|
|
26
|
+
if (eq <= 0)
|
|
27
|
+
continue;
|
|
28
|
+
if (trimmed.slice(0, eq).trim() !== "HUB_URL")
|
|
29
|
+
continue;
|
|
30
|
+
let value = trimmed.slice(eq + 1).trim();
|
|
31
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
32
|
+
value = value.slice(1, -1);
|
|
33
|
+
}
|
|
34
|
+
return value || undefined;
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* HUB_URL from a `.env` file next to this checkout.
|
|
40
|
+
*
|
|
41
|
+
* Walks up from the working directory and from this module so `npm start`
|
|
42
|
+
* from the repo root and a launch from a nested package both find
|
|
43
|
+
* `nexus-cli/.env`. Skipped under Vitest so a developer `.env` does not
|
|
44
|
+
* change the default the tests assert.
|
|
45
|
+
*/
|
|
46
|
+
function readDotEnvHubUrl() {
|
|
47
|
+
if (process.env.VITEST)
|
|
48
|
+
return undefined;
|
|
49
|
+
const starts = [process.cwd()];
|
|
50
|
+
try {
|
|
51
|
+
starts.push(dirname(fileURLToPath(import.meta.url)));
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// import.meta.url is always present in the ESM build; ignore if a test bundler drops it.
|
|
55
|
+
}
|
|
56
|
+
const seen = new Set();
|
|
57
|
+
for (const start of starts) {
|
|
58
|
+
let dir = start;
|
|
59
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
60
|
+
const candidate = join(dir, ".env");
|
|
61
|
+
if (!seen.has(candidate)) {
|
|
62
|
+
seen.add(candidate);
|
|
63
|
+
const value = readHubUrlKey(candidate);
|
|
64
|
+
if (value)
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
const parent = dirname(dir);
|
|
68
|
+
if (parent === dir)
|
|
69
|
+
break;
|
|
70
|
+
dir = parent;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Hub MCP endpoint.
|
|
77
|
+
*
|
|
78
|
+
* GITNEXUS_HUB_URL, then HUB_URL from the environment or `.env`, then a saved
|
|
79
|
+
* auth.json value, then the production default.
|
|
80
|
+
*/
|
|
81
|
+
export function resolveHubUrl(savedHubUrl) {
|
|
82
|
+
const fromEnv = process.env.GITNEXUS_HUB_URL?.trim() || process.env.HUB_URL?.trim() || readDotEnvHubUrl();
|
|
83
|
+
if (fromEnv)
|
|
84
|
+
return toMcpHubUrl(fromEnv);
|
|
85
|
+
if (savedHubUrl)
|
|
86
|
+
return savedHubUrl;
|
|
87
|
+
return DEFAULT_HUB_URL;
|
|
88
|
+
}
|
|
5
89
|
function configDir() {
|
|
6
90
|
return join(homedir(), ".config", "nexus");
|
|
7
91
|
}
|
|
@@ -24,7 +108,7 @@ export function resolveGitNexusConfig() {
|
|
|
24
108
|
const token = process.env.GITNEXUS_TOKEN ?? file.token;
|
|
25
109
|
if (!token)
|
|
26
110
|
return undefined;
|
|
27
|
-
const hubUrl =
|
|
111
|
+
const hubUrl = resolveHubUrl(file.hubUrl);
|
|
28
112
|
const deviceFingerprint = process.env.GITNEXUS_DEVICE_FP ?? file.deviceFingerprint;
|
|
29
113
|
const deviceName = process.env.GITNEXUS_DEVICE_NAME ?? file.deviceName;
|
|
30
114
|
return { hubUrl, token, deviceFingerprint, deviceName };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/extensions/gitnexus/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/extensions/gitnexus/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AA2BzC,MAAM,CAAC,MAAM,eAAe,GAAG,2DAA2D,CAAC;AAE3F,2EAA2E;AAC3E,MAAM,UAAU,WAAW,CAAC,KAAa,EAAU;IAClD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACjD,IAAI,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,OAAO,OAAO,CAAC;IAC7C,OAAO,GAAG,OAAO,MAAM,CAAC;AAAA,CACxB;AAED,SAAS,aAAa,CAAC,IAAY,EAAsB;IACxD,IAAI,GAAW,CAAC;IAChB,IAAI,CAAC;QACJ,GAAG,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAClC,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAA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I,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IACxF,OAAO,UAAU,EAAE,CAAC;AAAA,CACpB;AAED,MAAM,UAAU,kBAAkB,GAAW;IAC5C,OAAO,UAAU,EAAE,CAAC;AAAA,CACpB","sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\n/**\n * GitNexus hub connection settings.\n *\n * Resolution order (first hit wins):\n * 1. Environment: GITNEXUS_HUB_URL, then HUB_URL (process env or a `.env` file)\n * 2. Config file: ~/.config/nexus/auth.json ({ hubUrl, token })\n * 3. Production default\n *\n * HUB_URL is the hub origin (`https://host`). The MCP client needs the\n * `.../mcp` endpoint, so a bare origin gets `/mcp` appended. GITNEXUS_HUB_URL\n * may already be that endpoint. Token: GITNEXUS_TOKEN, then the config file.\n *\n * The hub endpoint is the Streamable-HTTP MCP endpoint (\".../mcp\"). Bearer\n * tokens are hub API tokens; an empty token allowlist on the hub means the\n * token can see every repo of its user identity.\n */\n\nexport interface GitNexusConfig {\n\thubUrl: string;\n\ttoken: string;\n\t/** Hub tokens can be device-bound; these headers must then match the binding. */\n\tdeviceFingerprint?: string | undefined;\n\tdeviceName?: string | undefined;\n}\n\nexport const DEFAULT_HUB_URL = \"https://gitnexus-enterprise-production.up.railway.app/mcp\";\n\n/** MCP endpoint for a hub origin or an already-qualified `.../mcp` URL. */\nexport function toMcpHubUrl(value: string): string {\n\tconst trimmed = value.trim().replace(/\\/+$/, \"\");\n\tif (trimmed.endsWith(\"/mcp\")) return trimmed;\n\treturn `${trimmed}/mcp`;\n}\n\nfunction readHubUrlKey(path: string): string | undefined {\n\tlet raw: string;\n\ttry {\n\t\traw = readFileSync(path, \"utf8\");\n\t} catch {\n\t\treturn undefined;\n\t}\n\tfor (const line of raw.split(\"\\n\")) {\n\t\tconst trimmed = line.trim();\n\t\tif (!trimmed || trimmed.startsWith(\"#\")) continue;\n\t\tconst eq = trimmed.indexOf(\"=\");\n\t\tif (eq <= 0) continue;\n\t\tif (trimmed.slice(0, eq).trim() !== \"HUB_URL\") continue;\n\t\tlet value = trimmed.slice(eq + 1).trim();\n\t\tif ((value.startsWith('\"') && value.endsWith('\"')) || (value.startsWith(\"'\") && value.endsWith(\"'\"))) {\n\t\t\tvalue = value.slice(1, -1);\n\t\t}\n\t\treturn value || undefined;\n\t}\n\treturn undefined;\n}\n\n/**\n * HUB_URL from a `.env` file next to this checkout.\n *\n * Walks up from the working directory and from this module so `npm start`\n * from the repo root and a launch from a nested package both find\n * `nexus-cli/.env`. Skipped under Vitest so a developer `.env` does not\n * change the default the tests assert.\n */\nfunction readDotEnvHubUrl(): string | undefined {\n\tif (process.env.VITEST) return undefined;\n\tconst starts = [process.cwd()];\n\ttry {\n\t\tstarts.push(dirname(fileURLToPath(import.meta.url)));\n\t} catch {\n\t\t// import.meta.url is always present in the ESM build; ignore if a test bundler drops it.\n\t}\n\tconst seen = new Set<string>();\n\tfor (const start of starts) {\n\t\tlet dir = start;\n\t\tfor (let depth = 0; depth < 8; depth++) {\n\t\t\tconst candidate = join(dir, \".env\");\n\t\t\tif (!seen.has(candidate)) {\n\t\t\t\tseen.add(candidate);\n\t\t\t\tconst value = readHubUrlKey(candidate);\n\t\t\t\tif (value) return value;\n\t\t\t}\n\t\t\tconst parent = dirname(dir);\n\t\t\tif (parent === dir) break;\n\t\t\tdir = parent;\n\t\t}\n\t}\n\treturn undefined;\n}\n\n/**\n * Hub MCP endpoint.\n *\n * GITNEXUS_HUB_URL, then HUB_URL from the environment or `.env`, then a saved\n * auth.json value, then the production default.\n */\nexport function resolveHubUrl(savedHubUrl?: string): string {\n\tconst fromEnv = process.env.GITNEXUS_HUB_URL?.trim() || process.env.HUB_URL?.trim() || readDotEnvHubUrl();\n\tif (fromEnv) return toMcpHubUrl(fromEnv);\n\tif (savedHubUrl) return savedHubUrl;\n\treturn DEFAULT_HUB_URL;\n}\n\nfunction configDir(): string {\n\treturn join(homedir(), \".config\", \"nexus\");\n}\n\nfunction configPath(): string {\n\treturn join(configDir(), \"auth.json\");\n}\n\nfunction readConfigFile(): Partial<GitNexusConfig> {\n\ttry {\n\t\tconst raw = readFileSync(configPath(), \"utf8\");\n\t\tconst parsed = JSON.parse(raw) as Partial<GitNexusConfig>;\n\t\treturn typeof parsed === \"object\" && parsed !== null ? parsed : {};\n\t} catch {\n\t\treturn {};\n\t}\n}\n\n/** Resolve hub settings. Returns undefined when no token is configured anywhere. */\nexport function resolveGitNexusConfig(): GitNexusConfig | undefined {\n\tconst file = readConfigFile();\n\tconst token = process.env.GITNEXUS_TOKEN ?? file.token;\n\tif (!token) return undefined;\n\tconst hubUrl = resolveHubUrl(file.hubUrl);\n\tconst deviceFingerprint = process.env.GITNEXUS_DEVICE_FP ?? file.deviceFingerprint;\n\tconst deviceName = process.env.GITNEXUS_DEVICE_NAME ?? file.deviceName;\n\treturn { hubUrl, token, deviceFingerprint, deviceName };\n}\n\n/** Persist hub settings from `/login`. */\nexport function saveGitNexusConfig(config: GitNexusConfig): string {\n\tif (!existsSync(configDir())) mkdirSync(configDir(), { recursive: true, mode: 0o700 });\n\twriteFileSync(configPath(), `${JSON.stringify(config, null, \"\\t\")}\\n`, { mode: 0o600 });\n\treturn configPath();\n}\n\nexport function gitNexusConfigPath(): string {\n\treturn configPath();\n}\n"]}
|