indusagi-coding-agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/CHANGELOG.md +2249 -0
  2. package/README.md +546 -0
  3. package/dist/cli/args.js +282 -0
  4. package/dist/cli/config-selector.js +30 -0
  5. package/dist/cli/file-processor.js +78 -0
  6. package/dist/cli/list-models.js +91 -0
  7. package/dist/cli/session-picker.js +31 -0
  8. package/dist/cli.js +10 -0
  9. package/dist/config.js +158 -0
  10. package/dist/core/agent-session.js +2097 -0
  11. package/dist/core/auth-storage.js +278 -0
  12. package/dist/core/bash-executor.js +211 -0
  13. package/dist/core/compaction/branch-summarization.js +241 -0
  14. package/dist/core/compaction/compaction.js +606 -0
  15. package/dist/core/compaction/index.js +6 -0
  16. package/dist/core/compaction/utils.js +137 -0
  17. package/dist/core/diagnostics.js +1 -0
  18. package/dist/core/event-bus.js +24 -0
  19. package/dist/core/exec.js +70 -0
  20. package/dist/core/export-html/ansi-to-html.js +248 -0
  21. package/dist/core/export-html/index.js +221 -0
  22. package/dist/core/export-html/template.css +905 -0
  23. package/dist/core/export-html/template.html +54 -0
  24. package/dist/core/export-html/template.js +1549 -0
  25. package/dist/core/export-html/tool-renderer.js +56 -0
  26. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  27. package/dist/core/export-html/vendor/marked.min.js +6 -0
  28. package/dist/core/extensions/index.js +8 -0
  29. package/dist/core/extensions/loader.js +395 -0
  30. package/dist/core/extensions/runner.js +499 -0
  31. package/dist/core/extensions/types.js +31 -0
  32. package/dist/core/extensions/wrapper.js +101 -0
  33. package/dist/core/footer-data-provider.js +133 -0
  34. package/dist/core/index.js +8 -0
  35. package/dist/core/keybindings.js +140 -0
  36. package/dist/core/messages.js +122 -0
  37. package/dist/core/model-registry.js +454 -0
  38. package/dist/core/model-resolver.js +309 -0
  39. package/dist/core/package-manager.js +1142 -0
  40. package/dist/core/prompt-templates.js +250 -0
  41. package/dist/core/resource-loader.js +569 -0
  42. package/dist/core/sdk.js +225 -0
  43. package/dist/core/session-manager.js +1078 -0
  44. package/dist/core/settings-manager.js +430 -0
  45. package/dist/core/skills.js +339 -0
  46. package/dist/core/system-prompt.js +136 -0
  47. package/dist/core/timings.js +24 -0
  48. package/dist/core/tools/bash.js +226 -0
  49. package/dist/core/tools/edit-diff.js +242 -0
  50. package/dist/core/tools/edit.js +145 -0
  51. package/dist/core/tools/find.js +205 -0
  52. package/dist/core/tools/grep.js +238 -0
  53. package/dist/core/tools/index.js +60 -0
  54. package/dist/core/tools/ls.js +117 -0
  55. package/dist/core/tools/path-utils.js +52 -0
  56. package/dist/core/tools/read.js +165 -0
  57. package/dist/core/tools/truncate.js +204 -0
  58. package/dist/core/tools/write.js +77 -0
  59. package/dist/index.js +41 -0
  60. package/dist/main.js +565 -0
  61. package/dist/migrations.js +260 -0
  62. package/dist/modes/index.js +7 -0
  63. package/dist/modes/interactive/components/armin.js +328 -0
  64. package/dist/modes/interactive/components/assistant-message.js +86 -0
  65. package/dist/modes/interactive/components/bash-execution.js +155 -0
  66. package/dist/modes/interactive/components/bordered-loader.js +47 -0
  67. package/dist/modes/interactive/components/branch-summary-message.js +41 -0
  68. package/dist/modes/interactive/components/compaction-summary-message.js +42 -0
  69. package/dist/modes/interactive/components/config-selector.js +458 -0
  70. package/dist/modes/interactive/components/countdown-timer.js +27 -0
  71. package/dist/modes/interactive/components/custom-editor.js +61 -0
  72. package/dist/modes/interactive/components/custom-message.js +80 -0
  73. package/dist/modes/interactive/components/diff.js +132 -0
  74. package/dist/modes/interactive/components/dynamic-border.js +19 -0
  75. package/dist/modes/interactive/components/extension-editor.js +96 -0
  76. package/dist/modes/interactive/components/extension-input.js +54 -0
  77. package/dist/modes/interactive/components/extension-selector.js +70 -0
  78. package/dist/modes/interactive/components/footer.js +213 -0
  79. package/dist/modes/interactive/components/index.js +31 -0
  80. package/dist/modes/interactive/components/keybinding-hints.js +60 -0
  81. package/dist/modes/interactive/components/login-dialog.js +138 -0
  82. package/dist/modes/interactive/components/model-selector.js +253 -0
  83. package/dist/modes/interactive/components/oauth-selector.js +91 -0
  84. package/dist/modes/interactive/components/scoped-models-selector.js +262 -0
  85. package/dist/modes/interactive/components/session-selector-search.js +145 -0
  86. package/dist/modes/interactive/components/session-selector.js +698 -0
  87. package/dist/modes/interactive/components/settings-selector.js +250 -0
  88. package/dist/modes/interactive/components/show-images-selector.js +33 -0
  89. package/dist/modes/interactive/components/skill-invocation-message.js +44 -0
  90. package/dist/modes/interactive/components/theme-selector.js +43 -0
  91. package/dist/modes/interactive/components/thinking-selector.js +45 -0
  92. package/dist/modes/interactive/components/tool-execution.js +608 -0
  93. package/dist/modes/interactive/components/tree-selector.js +892 -0
  94. package/dist/modes/interactive/components/user-message-selector.js +109 -0
  95. package/dist/modes/interactive/components/user-message.js +15 -0
  96. package/dist/modes/interactive/components/visual-truncate.js +32 -0
  97. package/dist/modes/interactive/interactive-mode.js +3576 -0
  98. package/dist/modes/interactive/theme/dark.json +85 -0
  99. package/dist/modes/interactive/theme/light.json +84 -0
  100. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  101. package/dist/modes/interactive/theme/theme.js +938 -0
  102. package/dist/modes/print-mode.js +96 -0
  103. package/dist/modes/rpc/rpc-client.js +390 -0
  104. package/dist/modes/rpc/rpc-mode.js +448 -0
  105. package/dist/modes/rpc/rpc-types.js +7 -0
  106. package/dist/utils/changelog.js +86 -0
  107. package/dist/utils/clipboard-image.js +116 -0
  108. package/dist/utils/clipboard.js +58 -0
  109. package/dist/utils/frontmatter.js +25 -0
  110. package/dist/utils/git.js +5 -0
  111. package/dist/utils/image-convert.js +34 -0
  112. package/dist/utils/image-resize.js +180 -0
  113. package/dist/utils/mime.js +25 -0
  114. package/dist/utils/photon.js +120 -0
  115. package/dist/utils/shell.js +164 -0
  116. package/dist/utils/sleep.js +16 -0
  117. package/dist/utils/tools-manager.js +186 -0
  118. package/docs/compaction.md +390 -0
  119. package/docs/custom-provider.md +538 -0
  120. package/docs/development.md +69 -0
  121. package/docs/extensions.md +1733 -0
  122. package/docs/images/doom-extension.png +0 -0
  123. package/docs/images/interactive-mode.png +0 -0
  124. package/docs/images/tree-view.png +0 -0
  125. package/docs/json.md +79 -0
  126. package/docs/keybindings.md +162 -0
  127. package/docs/models.md +193 -0
  128. package/docs/packages.md +163 -0
  129. package/docs/prompt-templates.md +67 -0
  130. package/docs/providers.md +147 -0
  131. package/docs/rpc.md +1048 -0
  132. package/docs/sdk.md +957 -0
  133. package/docs/session.md +412 -0
  134. package/docs/settings.md +216 -0
  135. package/docs/shell-aliases.md +13 -0
  136. package/docs/skills.md +226 -0
  137. package/docs/terminal-setup.md +65 -0
  138. package/docs/themes.md +295 -0
  139. package/docs/tree.md +219 -0
  140. package/docs/tui.md +887 -0
  141. package/docs/windows.md +17 -0
  142. package/examples/README.md +25 -0
  143. package/examples/extensions/README.md +192 -0
  144. package/examples/extensions/antigravity-image-gen.ts +414 -0
  145. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  146. package/examples/extensions/bookmark.ts +50 -0
  147. package/examples/extensions/claude-rules.ts +86 -0
  148. package/examples/extensions/confirm-destructive.ts +59 -0
  149. package/examples/extensions/custom-compaction.ts +115 -0
  150. package/examples/extensions/custom-footer.ts +65 -0
  151. package/examples/extensions/custom-header.ts +73 -0
  152. package/examples/extensions/custom-provider-anthropic/index.ts +605 -0
  153. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  154. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  155. package/examples/extensions/custom-provider-gitlab-duo/index.ts +350 -0
  156. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  157. package/examples/extensions/custom-provider-gitlab-duo/test.ts +83 -0
  158. package/examples/extensions/dirty-repo-guard.ts +56 -0
  159. package/examples/extensions/doom-overlay/README.md +46 -0
  160. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  161. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  162. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  163. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  164. package/examples/extensions/doom-overlay/doom-component.ts +133 -0
  165. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  166. package/examples/extensions/doom-overlay/doom-keys.ts +105 -0
  167. package/examples/extensions/doom-overlay/index.ts +74 -0
  168. package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
  169. package/examples/extensions/event-bus.ts +43 -0
  170. package/examples/extensions/file-trigger.ts +41 -0
  171. package/examples/extensions/git-checkpoint.ts +53 -0
  172. package/examples/extensions/handoff.ts +151 -0
  173. package/examples/extensions/hello.ts +25 -0
  174. package/examples/extensions/inline-bash.ts +94 -0
  175. package/examples/extensions/input-transform.ts +43 -0
  176. package/examples/extensions/interactive-shell.ts +196 -0
  177. package/examples/extensions/mac-system-theme.ts +47 -0
  178. package/examples/extensions/message-renderer.ts +60 -0
  179. package/examples/extensions/modal-editor.ts +86 -0
  180. package/examples/extensions/model-status.ts +31 -0
  181. package/examples/extensions/notify.ts +25 -0
  182. package/examples/extensions/overlay-qa-tests.ts +882 -0
  183. package/examples/extensions/overlay-test.ts +151 -0
  184. package/examples/extensions/permission-gate.ts +34 -0
  185. package/examples/extensions/pirate.ts +47 -0
  186. package/examples/extensions/plan-mode/README.md +65 -0
  187. package/examples/extensions/plan-mode/index.ts +341 -0
  188. package/examples/extensions/plan-mode/utils.ts +168 -0
  189. package/examples/extensions/preset.ts +399 -0
  190. package/examples/extensions/protected-paths.ts +30 -0
  191. package/examples/extensions/qna.ts +120 -0
  192. package/examples/extensions/question.ts +265 -0
  193. package/examples/extensions/questionnaire.ts +428 -0
  194. package/examples/extensions/rainbow-editor.ts +88 -0
  195. package/examples/extensions/sandbox/index.ts +318 -0
  196. package/examples/extensions/sandbox/package-lock.json +92 -0
  197. package/examples/extensions/sandbox/package.json +19 -0
  198. package/examples/extensions/send-user-message.ts +97 -0
  199. package/examples/extensions/session-name.ts +27 -0
  200. package/examples/extensions/shutdown-command.ts +63 -0
  201. package/examples/extensions/snake.ts +344 -0
  202. package/examples/extensions/space-invaders.ts +561 -0
  203. package/examples/extensions/ssh.ts +220 -0
  204. package/examples/extensions/status-line.ts +40 -0
  205. package/examples/extensions/subagent/README.md +172 -0
  206. package/examples/extensions/subagent/agents/planner.md +37 -0
  207. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  208. package/examples/extensions/subagent/agents/scout.md +50 -0
  209. package/examples/extensions/subagent/agents/worker.md +24 -0
  210. package/examples/extensions/subagent/agents.ts +127 -0
  211. package/examples/extensions/subagent/index.ts +964 -0
  212. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  213. package/examples/extensions/subagent/prompts/implement.md +10 -0
  214. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  215. package/examples/extensions/summarize.ts +196 -0
  216. package/examples/extensions/timed-confirm.ts +70 -0
  217. package/examples/extensions/todo.ts +300 -0
  218. package/examples/extensions/tool-override.ts +144 -0
  219. package/examples/extensions/tools.ts +147 -0
  220. package/examples/extensions/trigger-compact.ts +40 -0
  221. package/examples/extensions/truncated-tool.ts +193 -0
  222. package/examples/extensions/widget-placement.ts +17 -0
  223. package/examples/extensions/with-deps/index.ts +36 -0
  224. package/examples/extensions/with-deps/package-lock.json +31 -0
  225. package/examples/extensions/with-deps/package.json +22 -0
  226. package/examples/sdk/01-minimal.ts +22 -0
  227. package/examples/sdk/02-custom-model.ts +50 -0
  228. package/examples/sdk/03-custom-prompt.ts +55 -0
  229. package/examples/sdk/04-skills.ts +46 -0
  230. package/examples/sdk/05-tools.ts +56 -0
  231. package/examples/sdk/06-extensions.ts +88 -0
  232. package/examples/sdk/07-context-files.ts +40 -0
  233. package/examples/sdk/08-prompt-templates.ts +47 -0
  234. package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
  235. package/examples/sdk/10-settings.ts +38 -0
  236. package/examples/sdk/11-sessions.ts +48 -0
  237. package/examples/sdk/12-full-control.ts +82 -0
  238. package/examples/sdk/13-codex-oauth.ts +37 -0
  239. package/examples/sdk/README.md +144 -0
  240. package/package.json +85 -0
@@ -0,0 +1,205 @@
1
+ import { Type } from "@sinclair/typebox";
2
+ import { spawnSync } from "child_process";
3
+ import { existsSync } from "fs";
4
+ import { globSync } from "glob";
5
+ import path from "path";
6
+ import { ensureTool } from "../../utils/tools-manager.js";
7
+ import { resolveToCwd } from "./path-utils.js";
8
+ import { DEFAULT_MAX_BYTES, formatSize, truncateHead } from "./truncate.js";
9
+ const findSchema = Type.Object({
10
+ pattern: Type.String({
11
+ description: "Glob pattern to match files, e.g. '*.ts', '**/*.json', or 'src/**/*.spec.ts'",
12
+ }),
13
+ path: Type.Optional(Type.String({ description: "Directory to search in (default: current directory)" })),
14
+ limit: Type.Optional(Type.Number({ description: "Maximum number of results (default: 1000)" })),
15
+ });
16
+ const DEFAULT_LIMIT = 1000;
17
+ const defaultFindOperations = {
18
+ exists: existsSync,
19
+ glob: (_pattern, _searchCwd, _options) => {
20
+ // This is a placeholder - actual fd execution happens in execute
21
+ return [];
22
+ },
23
+ };
24
+ export function createFindTool(cwd, options) {
25
+ const customOps = options?.operations;
26
+ return {
27
+ name: "find",
28
+ label: "find",
29
+ description: `Search for files by glob pattern. Returns matching file paths relative to the search directory. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} results or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first).`,
30
+ parameters: findSchema,
31
+ execute: async (_toolCallId, { pattern, path: searchDir, limit }, signal) => {
32
+ return new Promise((resolve, reject) => {
33
+ if (signal?.aborted) {
34
+ reject(new Error("Operation aborted"));
35
+ return;
36
+ }
37
+ const onAbort = () => reject(new Error("Operation aborted"));
38
+ signal?.addEventListener("abort", onAbort, { once: true });
39
+ (async () => {
40
+ try {
41
+ const searchPath = resolveToCwd(searchDir || ".", cwd);
42
+ const effectiveLimit = limit ?? DEFAULT_LIMIT;
43
+ const ops = customOps ?? defaultFindOperations;
44
+ // If custom operations provided with glob, use that
45
+ if (customOps?.glob) {
46
+ if (!(await ops.exists(searchPath))) {
47
+ reject(new Error(`Path not found: ${searchPath}`));
48
+ return;
49
+ }
50
+ const results = await ops.glob(pattern, searchPath, {
51
+ ignore: ["**/node_modules/**", "**/.git/**"],
52
+ limit: effectiveLimit,
53
+ });
54
+ signal?.removeEventListener("abort", onAbort);
55
+ if (results.length === 0) {
56
+ resolve({
57
+ content: [{ type: "text", text: "No files found matching pattern" }],
58
+ details: undefined,
59
+ });
60
+ return;
61
+ }
62
+ // Relativize paths
63
+ const relativized = results.map((p) => {
64
+ if (p.startsWith(searchPath)) {
65
+ return p.slice(searchPath.length + 1);
66
+ }
67
+ return path.relative(searchPath, p);
68
+ });
69
+ const resultLimitReached = relativized.length >= effectiveLimit;
70
+ const rawOutput = relativized.join("\n");
71
+ const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
72
+ let resultOutput = truncation.content;
73
+ const details = {};
74
+ const notices = [];
75
+ if (resultLimitReached) {
76
+ notices.push(`${effectiveLimit} results limit reached`);
77
+ details.resultLimitReached = effectiveLimit;
78
+ }
79
+ if (truncation.truncated) {
80
+ notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
81
+ details.truncation = truncation;
82
+ }
83
+ if (notices.length > 0) {
84
+ resultOutput += `\n\n[${notices.join(". ")}]`;
85
+ }
86
+ resolve({
87
+ content: [{ type: "text", text: resultOutput }],
88
+ details: Object.keys(details).length > 0 ? details : undefined,
89
+ });
90
+ return;
91
+ }
92
+ // Default: use fd
93
+ const fdPath = await ensureTool("fd", true);
94
+ if (!fdPath) {
95
+ reject(new Error("fd is not available and could not be downloaded"));
96
+ return;
97
+ }
98
+ // Build fd arguments
99
+ const args = [
100
+ "--glob",
101
+ "--color=never",
102
+ "--hidden",
103
+ "--max-results",
104
+ String(effectiveLimit),
105
+ ];
106
+ // Include .gitignore files
107
+ const gitignoreFiles = new Set();
108
+ const rootGitignore = path.join(searchPath, ".gitignore");
109
+ if (existsSync(rootGitignore)) {
110
+ gitignoreFiles.add(rootGitignore);
111
+ }
112
+ try {
113
+ const nestedGitignores = globSync("**/.gitignore", {
114
+ cwd: searchPath,
115
+ dot: true,
116
+ absolute: true,
117
+ ignore: ["**/node_modules/**", "**/.git/**"],
118
+ });
119
+ for (const file of nestedGitignores) {
120
+ gitignoreFiles.add(file);
121
+ }
122
+ }
123
+ catch {
124
+ // Ignore glob errors
125
+ }
126
+ for (const gitignorePath of gitignoreFiles) {
127
+ args.push("--ignore-file", gitignorePath);
128
+ }
129
+ args.push(pattern, searchPath);
130
+ const result = spawnSync(fdPath, args, {
131
+ encoding: "utf-8",
132
+ maxBuffer: 10 * 1024 * 1024,
133
+ });
134
+ signal?.removeEventListener("abort", onAbort);
135
+ if (result.error) {
136
+ reject(new Error(`Failed to run fd: ${result.error.message}`));
137
+ return;
138
+ }
139
+ const output = result.stdout?.trim() || "";
140
+ if (result.status !== 0) {
141
+ const errorMsg = result.stderr?.trim() || `fd exited with code ${result.status}`;
142
+ if (!output) {
143
+ reject(new Error(errorMsg));
144
+ return;
145
+ }
146
+ }
147
+ if (!output) {
148
+ resolve({
149
+ content: [{ type: "text", text: "No files found matching pattern" }],
150
+ details: undefined,
151
+ });
152
+ return;
153
+ }
154
+ const lines = output.split("\n");
155
+ const relativized = [];
156
+ for (const rawLine of lines) {
157
+ const line = rawLine.replace(/\r$/, "").trim();
158
+ if (!line)
159
+ continue;
160
+ const hadTrailingSlash = line.endsWith("/") || line.endsWith("\\");
161
+ let relativePath = line;
162
+ if (line.startsWith(searchPath)) {
163
+ relativePath = line.slice(searchPath.length + 1);
164
+ }
165
+ else {
166
+ relativePath = path.relative(searchPath, line);
167
+ }
168
+ if (hadTrailingSlash && !relativePath.endsWith("/")) {
169
+ relativePath += "/";
170
+ }
171
+ relativized.push(relativePath);
172
+ }
173
+ const resultLimitReached = relativized.length >= effectiveLimit;
174
+ const rawOutput = relativized.join("\n");
175
+ const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
176
+ let resultOutput = truncation.content;
177
+ const details = {};
178
+ const notices = [];
179
+ if (resultLimitReached) {
180
+ notices.push(`${effectiveLimit} results limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`);
181
+ details.resultLimitReached = effectiveLimit;
182
+ }
183
+ if (truncation.truncated) {
184
+ notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
185
+ details.truncation = truncation;
186
+ }
187
+ if (notices.length > 0) {
188
+ resultOutput += `\n\n[${notices.join(". ")}]`;
189
+ }
190
+ resolve({
191
+ content: [{ type: "text", text: resultOutput }],
192
+ details: Object.keys(details).length > 0 ? details : undefined,
193
+ });
194
+ }
195
+ catch (e) {
196
+ signal?.removeEventListener("abort", onAbort);
197
+ reject(e);
198
+ }
199
+ })();
200
+ });
201
+ },
202
+ };
203
+ }
204
+ /** Default find tool using process.cwd() - for backwards compatibility */
205
+ export const findTool = createFindTool(process.cwd());
@@ -0,0 +1,238 @@
1
+ import { createInterface } from "node:readline";
2
+ import { Type } from "@sinclair/typebox";
3
+ import { spawn } from "child_process";
4
+ import { readFileSync, statSync } from "fs";
5
+ import path from "path";
6
+ import { ensureTool } from "../../utils/tools-manager.js";
7
+ import { resolveToCwd } from "./path-utils.js";
8
+ import { DEFAULT_MAX_BYTES, formatSize, GREP_MAX_LINE_LENGTH, truncateHead, truncateLine, } from "./truncate.js";
9
+ const grepSchema = Type.Object({
10
+ pattern: Type.String({ description: "Search pattern (regex or literal string)" }),
11
+ path: Type.Optional(Type.String({ description: "Directory or file to search (default: current directory)" })),
12
+ glob: Type.Optional(Type.String({ description: "Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'" })),
13
+ ignoreCase: Type.Optional(Type.Boolean({ description: "Case-insensitive search (default: false)" })),
14
+ literal: Type.Optional(Type.Boolean({ description: "Treat pattern as literal string instead of regex (default: false)" })),
15
+ context: Type.Optional(Type.Number({ description: "Number of lines to show before and after each match (default: 0)" })),
16
+ limit: Type.Optional(Type.Number({ description: "Maximum number of matches to return (default: 100)" })),
17
+ });
18
+ const DEFAULT_LIMIT = 100;
19
+ const defaultGrepOperations = {
20
+ isDirectory: (p) => statSync(p).isDirectory(),
21
+ readFile: (p) => readFileSync(p, "utf-8"),
22
+ };
23
+ export function createGrepTool(cwd, options) {
24
+ const customOps = options?.operations;
25
+ return {
26
+ name: "grep",
27
+ label: "grep",
28
+ description: `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.`,
29
+ parameters: grepSchema,
30
+ execute: async (_toolCallId, { pattern, path: searchDir, glob, ignoreCase, literal, context, limit, }, signal) => {
31
+ return new Promise((resolve, reject) => {
32
+ if (signal?.aborted) {
33
+ reject(new Error("Operation aborted"));
34
+ return;
35
+ }
36
+ let settled = false;
37
+ const settle = (fn) => {
38
+ if (!settled) {
39
+ settled = true;
40
+ fn();
41
+ }
42
+ };
43
+ (async () => {
44
+ try {
45
+ const rgPath = await ensureTool("rg", true);
46
+ if (!rgPath) {
47
+ settle(() => reject(new Error("ripgrep (rg) is not available and could not be downloaded")));
48
+ return;
49
+ }
50
+ const searchPath = resolveToCwd(searchDir || ".", cwd);
51
+ const ops = customOps ?? defaultGrepOperations;
52
+ let isDirectory;
53
+ try {
54
+ isDirectory = await ops.isDirectory(searchPath);
55
+ }
56
+ catch (_err) {
57
+ settle(() => reject(new Error(`Path not found: ${searchPath}`)));
58
+ return;
59
+ }
60
+ const contextValue = context && context > 0 ? context : 0;
61
+ const effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);
62
+ const formatPath = (filePath) => {
63
+ if (isDirectory) {
64
+ const relative = path.relative(searchPath, filePath);
65
+ if (relative && !relative.startsWith("..")) {
66
+ return relative.replace(/\\/g, "/");
67
+ }
68
+ }
69
+ return path.basename(filePath);
70
+ };
71
+ const fileCache = new Map();
72
+ const getFileLines = async (filePath) => {
73
+ let lines = fileCache.get(filePath);
74
+ if (!lines) {
75
+ try {
76
+ const content = await ops.readFile(filePath);
77
+ lines = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
78
+ }
79
+ catch {
80
+ lines = [];
81
+ }
82
+ fileCache.set(filePath, lines);
83
+ }
84
+ return lines;
85
+ };
86
+ const args = ["--json", "--line-number", "--color=never", "--hidden"];
87
+ if (ignoreCase) {
88
+ args.push("--ignore-case");
89
+ }
90
+ if (literal) {
91
+ args.push("--fixed-strings");
92
+ }
93
+ if (glob) {
94
+ args.push("--glob", glob);
95
+ }
96
+ args.push(pattern, searchPath);
97
+ const child = spawn(rgPath, args, { stdio: ["ignore", "pipe", "pipe"] });
98
+ const rl = createInterface({ input: child.stdout });
99
+ let stderr = "";
100
+ let matchCount = 0;
101
+ let matchLimitReached = false;
102
+ let linesTruncated = false;
103
+ let aborted = false;
104
+ let killedDueToLimit = false;
105
+ const outputLines = [];
106
+ const cleanup = () => {
107
+ rl.close();
108
+ signal?.removeEventListener("abort", onAbort);
109
+ };
110
+ const stopChild = (dueToLimit = false) => {
111
+ if (!child.killed) {
112
+ killedDueToLimit = dueToLimit;
113
+ child.kill();
114
+ }
115
+ };
116
+ const onAbort = () => {
117
+ aborted = true;
118
+ stopChild();
119
+ };
120
+ signal?.addEventListener("abort", onAbort, { once: true });
121
+ child.stderr?.on("data", (chunk) => {
122
+ stderr += chunk.toString();
123
+ });
124
+ const formatBlock = async (filePath, lineNumber) => {
125
+ const relativePath = formatPath(filePath);
126
+ const lines = await getFileLines(filePath);
127
+ if (!lines.length) {
128
+ return [`${relativePath}:${lineNumber}: (unable to read file)`];
129
+ }
130
+ const block = [];
131
+ const start = contextValue > 0 ? Math.max(1, lineNumber - contextValue) : lineNumber;
132
+ const end = contextValue > 0 ? Math.min(lines.length, lineNumber + contextValue) : lineNumber;
133
+ for (let current = start; current <= end; current++) {
134
+ const lineText = lines[current - 1] ?? "";
135
+ const sanitized = lineText.replace(/\r/g, "");
136
+ const isMatchLine = current === lineNumber;
137
+ // Truncate long lines
138
+ const { text: truncatedText, wasTruncated } = truncateLine(sanitized);
139
+ if (wasTruncated) {
140
+ linesTruncated = true;
141
+ }
142
+ if (isMatchLine) {
143
+ block.push(`${relativePath}:${current}: ${truncatedText}`);
144
+ }
145
+ else {
146
+ block.push(`${relativePath}-${current}- ${truncatedText}`);
147
+ }
148
+ }
149
+ return block;
150
+ };
151
+ // Collect matches during streaming, format after
152
+ const matches = [];
153
+ rl.on("line", (line) => {
154
+ if (!line.trim() || matchCount >= effectiveLimit) {
155
+ return;
156
+ }
157
+ let event;
158
+ try {
159
+ event = JSON.parse(line);
160
+ }
161
+ catch {
162
+ return;
163
+ }
164
+ if (event.type === "match") {
165
+ matchCount++;
166
+ const filePath = event.data?.path?.text;
167
+ const lineNumber = event.data?.line_number;
168
+ if (filePath && typeof lineNumber === "number") {
169
+ matches.push({ filePath, lineNumber });
170
+ }
171
+ if (matchCount >= effectiveLimit) {
172
+ matchLimitReached = true;
173
+ stopChild(true);
174
+ }
175
+ }
176
+ });
177
+ child.on("error", (error) => {
178
+ cleanup();
179
+ settle(() => reject(new Error(`Failed to run ripgrep: ${error.message}`)));
180
+ });
181
+ child.on("close", async (code) => {
182
+ cleanup();
183
+ if (aborted) {
184
+ settle(() => reject(new Error("Operation aborted")));
185
+ return;
186
+ }
187
+ if (!killedDueToLimit && code !== 0 && code !== 1) {
188
+ const errorMsg = stderr.trim() || `ripgrep exited with code ${code}`;
189
+ settle(() => reject(new Error(errorMsg)));
190
+ return;
191
+ }
192
+ if (matchCount === 0) {
193
+ settle(() => resolve({ content: [{ type: "text", text: "No matches found" }], details: undefined }));
194
+ return;
195
+ }
196
+ // Format matches (async to support remote file reading)
197
+ for (const match of matches) {
198
+ const block = await formatBlock(match.filePath, match.lineNumber);
199
+ outputLines.push(...block);
200
+ }
201
+ // Apply byte truncation (no line limit since we already have match limit)
202
+ const rawOutput = outputLines.join("\n");
203
+ const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
204
+ let output = truncation.content;
205
+ const details = {};
206
+ // Build notices
207
+ const notices = [];
208
+ if (matchLimitReached) {
209
+ notices.push(`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`);
210
+ details.matchLimitReached = effectiveLimit;
211
+ }
212
+ if (truncation.truncated) {
213
+ notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
214
+ details.truncation = truncation;
215
+ }
216
+ if (linesTruncated) {
217
+ notices.push(`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`);
218
+ details.linesTruncated = true;
219
+ }
220
+ if (notices.length > 0) {
221
+ output += `\n\n[${notices.join(". ")}]`;
222
+ }
223
+ settle(() => resolve({
224
+ content: [{ type: "text", text: output }],
225
+ details: Object.keys(details).length > 0 ? details : undefined,
226
+ }));
227
+ });
228
+ }
229
+ catch (err) {
230
+ settle(() => reject(err));
231
+ }
232
+ })();
233
+ });
234
+ },
235
+ };
236
+ }
237
+ /** Default grep tool using process.cwd() - for backwards compatibility */
238
+ export const grepTool = createGrepTool(process.cwd());
@@ -0,0 +1,60 @@
1
+ export { bashTool, createBashTool, } from "./bash.js";
2
+ export { createEditTool, editTool } from "./edit.js";
3
+ export { createFindTool, findTool } from "./find.js";
4
+ export { createGrepTool, grepTool } from "./grep.js";
5
+ export { createLsTool, lsTool } from "./ls.js";
6
+ export { createReadTool, readTool, } from "./read.js";
7
+ export { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, truncateHead, truncateLine, truncateTail, } from "./truncate.js";
8
+ export { createWriteTool, writeTool } from "./write.js";
9
+ import { bashTool, createBashTool } from "./bash.js";
10
+ import { createEditTool, editTool } from "./edit.js";
11
+ import { createFindTool, findTool } from "./find.js";
12
+ import { createGrepTool, grepTool } from "./grep.js";
13
+ import { createLsTool, lsTool } from "./ls.js";
14
+ import { createReadTool, readTool } from "./read.js";
15
+ import { createWriteTool, writeTool } from "./write.js";
16
+ // Default tools for full access mode (using process.cwd())
17
+ export const codingTools = [readTool, bashTool, editTool, writeTool];
18
+ // Read-only tools for exploration without modification (using process.cwd())
19
+ export const readOnlyTools = [readTool, grepTool, findTool, lsTool];
20
+ // All available tools (using process.cwd())
21
+ export const allTools = {
22
+ read: readTool,
23
+ bash: bashTool,
24
+ edit: editTool,
25
+ write: writeTool,
26
+ grep: grepTool,
27
+ find: findTool,
28
+ ls: lsTool,
29
+ };
30
+ /**
31
+ * Create coding tools configured for a specific working directory.
32
+ */
33
+ export function createCodingTools(cwd, options) {
34
+ return [
35
+ createReadTool(cwd, options?.read),
36
+ createBashTool(cwd, options?.bash),
37
+ createEditTool(cwd),
38
+ createWriteTool(cwd),
39
+ ];
40
+ }
41
+ /**
42
+ * Create read-only tools configured for a specific working directory.
43
+ */
44
+ export function createReadOnlyTools(cwd, options) {
45
+ return [createReadTool(cwd, options?.read), createGrepTool(cwd), createFindTool(cwd), createLsTool(cwd)];
46
+ }
47
+ /**
48
+ * Create all tools configured for a specific working directory.
49
+ */
50
+ export function createAllTools(cwd, options) {
51
+ return {
52
+ read: createReadTool(cwd, options?.read),
53
+ bash: createBashTool(cwd, options?.bash),
54
+ edit: createEditTool(cwd),
55
+ write: createWriteTool(cwd),
56
+ grep: createGrepTool(cwd),
57
+ find: createFindTool(cwd),
58
+ ls: createLsTool(cwd),
59
+ };
60
+ }
@@ -0,0 +1,117 @@
1
+ import { Type } from "@sinclair/typebox";
2
+ import { existsSync, readdirSync, statSync } from "fs";
3
+ import nodePath from "path";
4
+ import { resolveToCwd } from "./path-utils.js";
5
+ import { DEFAULT_MAX_BYTES, formatSize, truncateHead } from "./truncate.js";
6
+ const lsSchema = Type.Object({
7
+ path: Type.Optional(Type.String({ description: "Directory to list (default: current directory)" })),
8
+ limit: Type.Optional(Type.Number({ description: "Maximum number of entries to return (default: 500)" })),
9
+ });
10
+ const DEFAULT_LIMIT = 500;
11
+ const defaultLsOperations = {
12
+ exists: existsSync,
13
+ stat: statSync,
14
+ readdir: readdirSync,
15
+ };
16
+ export function createLsTool(cwd, options) {
17
+ const ops = options?.operations ?? defaultLsOperations;
18
+ return {
19
+ name: "ls",
20
+ label: "ls",
21
+ description: `List directory contents. Returns entries sorted alphabetically, with '/' suffix for directories. Includes dotfiles. Output is truncated to ${DEFAULT_LIMIT} entries or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first).`,
22
+ parameters: lsSchema,
23
+ execute: async (_toolCallId, { path, limit }, signal) => {
24
+ return new Promise((resolve, reject) => {
25
+ if (signal?.aborted) {
26
+ reject(new Error("Operation aborted"));
27
+ return;
28
+ }
29
+ const onAbort = () => reject(new Error("Operation aborted"));
30
+ signal?.addEventListener("abort", onAbort, { once: true });
31
+ (async () => {
32
+ try {
33
+ const dirPath = resolveToCwd(path || ".", cwd);
34
+ const effectiveLimit = limit ?? DEFAULT_LIMIT;
35
+ // Check if path exists
36
+ if (!(await ops.exists(dirPath))) {
37
+ reject(new Error(`Path not found: ${dirPath}`));
38
+ return;
39
+ }
40
+ // Check if path is a directory
41
+ const stat = await ops.stat(dirPath);
42
+ if (!stat.isDirectory()) {
43
+ reject(new Error(`Not a directory: ${dirPath}`));
44
+ return;
45
+ }
46
+ // Read directory entries
47
+ let entries;
48
+ try {
49
+ entries = await ops.readdir(dirPath);
50
+ }
51
+ catch (e) {
52
+ reject(new Error(`Cannot read directory: ${e.message}`));
53
+ return;
54
+ }
55
+ // Sort alphabetically (case-insensitive)
56
+ entries.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
57
+ // Format entries with directory indicators
58
+ const results = [];
59
+ let entryLimitReached = false;
60
+ for (const entry of entries) {
61
+ if (results.length >= effectiveLimit) {
62
+ entryLimitReached = true;
63
+ break;
64
+ }
65
+ const fullPath = nodePath.join(dirPath, entry);
66
+ let suffix = "";
67
+ try {
68
+ const entryStat = await ops.stat(fullPath);
69
+ if (entryStat.isDirectory()) {
70
+ suffix = "/";
71
+ }
72
+ }
73
+ catch {
74
+ // Skip entries we can't stat
75
+ continue;
76
+ }
77
+ results.push(entry + suffix);
78
+ }
79
+ signal?.removeEventListener("abort", onAbort);
80
+ if (results.length === 0) {
81
+ resolve({ content: [{ type: "text", text: "(empty directory)" }], details: undefined });
82
+ return;
83
+ }
84
+ // Apply byte truncation (no line limit since we already have entry limit)
85
+ const rawOutput = results.join("\n");
86
+ const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
87
+ let output = truncation.content;
88
+ const details = {};
89
+ // Build notices
90
+ const notices = [];
91
+ if (entryLimitReached) {
92
+ notices.push(`${effectiveLimit} entries limit reached. Use limit=${effectiveLimit * 2} for more`);
93
+ details.entryLimitReached = effectiveLimit;
94
+ }
95
+ if (truncation.truncated) {
96
+ notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
97
+ details.truncation = truncation;
98
+ }
99
+ if (notices.length > 0) {
100
+ output += `\n\n[${notices.join(". ")}]`;
101
+ }
102
+ resolve({
103
+ content: [{ type: "text", text: output }],
104
+ details: Object.keys(details).length > 0 ? details : undefined,
105
+ });
106
+ }
107
+ catch (e) {
108
+ signal?.removeEventListener("abort", onAbort);
109
+ reject(e);
110
+ }
111
+ })();
112
+ });
113
+ },
114
+ };
115
+ }
116
+ /** Default ls tool using process.cwd() - for backwards compatibility */
117
+ export const lsTool = createLsTool(process.cwd());
@@ -0,0 +1,52 @@
1
+ import { accessSync, constants } from "node:fs";
2
+ import * as os from "node:os";
3
+ import { isAbsolute, resolve as resolvePath } from "node:path";
4
+ const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
5
+ const NARROW_NO_BREAK_SPACE = "\u202F";
6
+ function normalizeUnicodeSpaces(str) {
7
+ return str.replace(UNICODE_SPACES, " ");
8
+ }
9
+ function tryMacOSScreenshotPath(filePath) {
10
+ return filePath.replace(/ (AM|PM)\./g, `${NARROW_NO_BREAK_SPACE}$1.`);
11
+ }
12
+ function fileExists(filePath) {
13
+ try {
14
+ accessSync(filePath, constants.F_OK);
15
+ return true;
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ }
21
+ export function expandPath(filePath) {
22
+ const normalized = normalizeUnicodeSpaces(filePath);
23
+ if (normalized === "~") {
24
+ return os.homedir();
25
+ }
26
+ if (normalized.startsWith("~/")) {
27
+ return os.homedir() + normalized.slice(1);
28
+ }
29
+ return normalized;
30
+ }
31
+ /**
32
+ * Resolve a path relative to the given cwd.
33
+ * Handles ~ expansion and absolute paths.
34
+ */
35
+ export function resolveToCwd(filePath, cwd) {
36
+ const expanded = expandPath(filePath);
37
+ if (isAbsolute(expanded)) {
38
+ return expanded;
39
+ }
40
+ return resolvePath(cwd, expanded);
41
+ }
42
+ export function resolveReadPath(filePath, cwd) {
43
+ const resolved = resolveToCwd(filePath, cwd);
44
+ if (fileExists(resolved)) {
45
+ return resolved;
46
+ }
47
+ const macOSVariant = tryMacOSScreenshotPath(resolved);
48
+ if (macOSVariant !== resolved && fileExists(macOSVariant)) {
49
+ return macOSVariant;
50
+ }
51
+ return resolved;
52
+ }