@clinebot/core 0.0.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 (200) hide show
  1. package/README.md +88 -0
  2. package/dist/account/cline-account-service.d.ts +34 -0
  3. package/dist/account/index.d.ts +3 -0
  4. package/dist/account/rpc.d.ts +38 -0
  5. package/dist/account/types.d.ts +74 -0
  6. package/dist/agents/agent-config-loader.d.ts +18 -0
  7. package/dist/agents/agent-config-parser.d.ts +25 -0
  8. package/dist/agents/hooks-config-loader.d.ts +23 -0
  9. package/dist/agents/index.d.ts +11 -0
  10. package/dist/agents/plugin-config-loader.d.ts +22 -0
  11. package/dist/agents/plugin-loader.d.ts +9 -0
  12. package/dist/agents/plugin-sandbox.d.ts +12 -0
  13. package/dist/agents/unified-config-file-watcher.d.ts +77 -0
  14. package/dist/agents/user-instruction-config-loader.d.ts +63 -0
  15. package/dist/auth/client.d.ts +11 -0
  16. package/dist/auth/cline.d.ts +41 -0
  17. package/dist/auth/codex.d.ts +39 -0
  18. package/dist/auth/oca.d.ts +22 -0
  19. package/dist/auth/server.d.ts +22 -0
  20. package/dist/auth/types.d.ts +72 -0
  21. package/dist/auth/utils.d.ts +32 -0
  22. package/dist/chat/chat-schema.d.ts +145 -0
  23. package/dist/default-tools/constants.d.ts +23 -0
  24. package/dist/default-tools/definitions.d.ts +96 -0
  25. package/dist/default-tools/executors/apply-patch-parser.d.ts +68 -0
  26. package/dist/default-tools/executors/apply-patch.d.ts +26 -0
  27. package/dist/default-tools/executors/bash.d.ts +49 -0
  28. package/dist/default-tools/executors/editor.d.ts +31 -0
  29. package/dist/default-tools/executors/file-read.d.ts +40 -0
  30. package/dist/default-tools/executors/index.d.ts +44 -0
  31. package/dist/default-tools/executors/search.d.ts +50 -0
  32. package/dist/default-tools/executors/web-fetch.d.ts +58 -0
  33. package/dist/default-tools/index.d.ts +57 -0
  34. package/dist/default-tools/presets.d.ts +124 -0
  35. package/dist/default-tools/schemas.d.ts +121 -0
  36. package/dist/default-tools/types.d.ts +237 -0
  37. package/dist/index.d.ts +23 -0
  38. package/dist/index.js +220 -0
  39. package/dist/input/file-indexer.d.ts +5 -0
  40. package/dist/input/index.d.ts +4 -0
  41. package/dist/input/mention-enricher.d.ts +12 -0
  42. package/dist/mcp/config-loader.d.ts +15 -0
  43. package/dist/mcp/index.d.ts +4 -0
  44. package/dist/mcp/manager.d.ts +24 -0
  45. package/dist/mcp/types.d.ts +66 -0
  46. package/dist/runtime/hook-file-hooks.d.ts +18 -0
  47. package/dist/runtime/rules.d.ts +5 -0
  48. package/dist/runtime/runtime-builder.d.ts +5 -0
  49. package/dist/runtime/sandbox/subprocess-sandbox.d.ts +19 -0
  50. package/dist/runtime/session-runtime.d.ts +36 -0
  51. package/dist/runtime/tool-approval.d.ts +9 -0
  52. package/dist/runtime/workflows.d.ts +13 -0
  53. package/dist/server/index.d.ts +47 -0
  54. package/dist/server/index.js +641 -0
  55. package/dist/session/default-session-manager.d.ts +77 -0
  56. package/dist/session/rpc-session-service.d.ts +12 -0
  57. package/dist/session/runtime-oauth-token-manager.d.ts +28 -0
  58. package/dist/session/session-artifacts.d.ts +19 -0
  59. package/dist/session/session-graph.d.ts +15 -0
  60. package/dist/session/session-host.d.ts +21 -0
  61. package/dist/session/session-manager.d.ts +50 -0
  62. package/dist/session/session-manifest.d.ts +30 -0
  63. package/dist/session/session-service.d.ts +113 -0
  64. package/dist/session/sqlite-rpc-session-backend.d.ts +30 -0
  65. package/dist/session/unified-session-persistence-service.d.ts +93 -0
  66. package/dist/session/workspace-manager.d.ts +28 -0
  67. package/dist/session/workspace-manifest.d.ts +25 -0
  68. package/dist/storage/provider-settings-legacy-migration.d.ts +13 -0
  69. package/dist/storage/provider-settings-manager.d.ts +20 -0
  70. package/dist/storage/sqlite-session-store.d.ts +29 -0
  71. package/dist/storage/sqlite-team-store.d.ts +31 -0
  72. package/dist/storage/team-store.d.ts +2 -0
  73. package/dist/team/index.d.ts +1 -0
  74. package/dist/team/projections.d.ts +8 -0
  75. package/dist/types/common.d.ts +10 -0
  76. package/dist/types/config.d.ts +37 -0
  77. package/dist/types/events.d.ts +54 -0
  78. package/dist/types/provider-settings.d.ts +20 -0
  79. package/dist/types/sessions.d.ts +9 -0
  80. package/dist/types/storage.d.ts +37 -0
  81. package/dist/types/workspace.d.ts +7 -0
  82. package/dist/types.d.ts +26 -0
  83. package/package.json +63 -0
  84. package/src/account/cline-account-service.test.ts +101 -0
  85. package/src/account/cline-account-service.ts +267 -0
  86. package/src/account/index.ts +20 -0
  87. package/src/account/rpc.test.ts +62 -0
  88. package/src/account/rpc.ts +172 -0
  89. package/src/account/types.ts +80 -0
  90. package/src/agents/agent-config-loader.test.ts +234 -0
  91. package/src/agents/agent-config-loader.ts +107 -0
  92. package/src/agents/agent-config-parser.ts +191 -0
  93. package/src/agents/hooks-config-loader.ts +97 -0
  94. package/src/agents/index.ts +84 -0
  95. package/src/agents/plugin-config-loader.test.ts +91 -0
  96. package/src/agents/plugin-config-loader.ts +160 -0
  97. package/src/agents/plugin-loader.test.ts +102 -0
  98. package/src/agents/plugin-loader.ts +105 -0
  99. package/src/agents/plugin-sandbox.test.ts +120 -0
  100. package/src/agents/plugin-sandbox.ts +471 -0
  101. package/src/agents/unified-config-file-watcher.test.ts +196 -0
  102. package/src/agents/unified-config-file-watcher.ts +483 -0
  103. package/src/agents/user-instruction-config-loader.test.ts +158 -0
  104. package/src/agents/user-instruction-config-loader.ts +438 -0
  105. package/src/auth/client.test.ts +40 -0
  106. package/src/auth/client.ts +25 -0
  107. package/src/auth/cline.test.ts +130 -0
  108. package/src/auth/cline.ts +414 -0
  109. package/src/auth/codex.test.ts +170 -0
  110. package/src/auth/codex.ts +466 -0
  111. package/src/auth/oca.test.ts +215 -0
  112. package/src/auth/oca.ts +546 -0
  113. package/src/auth/server.ts +216 -0
  114. package/src/auth/types.ts +78 -0
  115. package/src/auth/utils.test.ts +128 -0
  116. package/src/auth/utils.ts +247 -0
  117. package/src/chat/chat-schema.ts +82 -0
  118. package/src/default-tools/constants.ts +35 -0
  119. package/src/default-tools/definitions.test.ts +233 -0
  120. package/src/default-tools/definitions.ts +632 -0
  121. package/src/default-tools/executors/apply-patch-parser.ts +520 -0
  122. package/src/default-tools/executors/apply-patch.ts +359 -0
  123. package/src/default-tools/executors/bash.ts +205 -0
  124. package/src/default-tools/executors/editor.ts +231 -0
  125. package/src/default-tools/executors/file-read.test.ts +25 -0
  126. package/src/default-tools/executors/file-read.ts +94 -0
  127. package/src/default-tools/executors/index.ts +75 -0
  128. package/src/default-tools/executors/search.ts +278 -0
  129. package/src/default-tools/executors/web-fetch.ts +259 -0
  130. package/src/default-tools/index.ts +161 -0
  131. package/src/default-tools/presets.test.ts +63 -0
  132. package/src/default-tools/presets.ts +168 -0
  133. package/src/default-tools/schemas.ts +228 -0
  134. package/src/default-tools/types.ts +324 -0
  135. package/src/index.ts +119 -0
  136. package/src/input/file-indexer.d.ts +11 -0
  137. package/src/input/file-indexer.test.ts +87 -0
  138. package/src/input/file-indexer.ts +280 -0
  139. package/src/input/index.ts +7 -0
  140. package/src/input/mention-enricher.test.ts +82 -0
  141. package/src/input/mention-enricher.ts +119 -0
  142. package/src/mcp/config-loader.test.ts +238 -0
  143. package/src/mcp/config-loader.ts +219 -0
  144. package/src/mcp/index.ts +26 -0
  145. package/src/mcp/manager.test.ts +106 -0
  146. package/src/mcp/manager.ts +262 -0
  147. package/src/mcp/types.ts +88 -0
  148. package/src/runtime/hook-file-hooks.test.ts +106 -0
  149. package/src/runtime/hook-file-hooks.ts +736 -0
  150. package/src/runtime/index.ts +27 -0
  151. package/src/runtime/rules.ts +34 -0
  152. package/src/runtime/runtime-builder.team-persistence.test.ts +203 -0
  153. package/src/runtime/runtime-builder.test.ts +215 -0
  154. package/src/runtime/runtime-builder.ts +515 -0
  155. package/src/runtime/runtime-parity.test.ts +132 -0
  156. package/src/runtime/sandbox/subprocess-sandbox.ts +207 -0
  157. package/src/runtime/session-runtime.ts +44 -0
  158. package/src/runtime/tool-approval.ts +104 -0
  159. package/src/runtime/workflows.test.ts +119 -0
  160. package/src/runtime/workflows.ts +54 -0
  161. package/src/server/index.ts +282 -0
  162. package/src/session/default-session-manager.e2e.test.ts +354 -0
  163. package/src/session/default-session-manager.test.ts +816 -0
  164. package/src/session/default-session-manager.ts +1286 -0
  165. package/src/session/index.ts +37 -0
  166. package/src/session/rpc-session-service.ts +189 -0
  167. package/src/session/runtime-oauth-token-manager.test.ts +137 -0
  168. package/src/session/runtime-oauth-token-manager.ts +265 -0
  169. package/src/session/session-artifacts.ts +106 -0
  170. package/src/session/session-graph.ts +90 -0
  171. package/src/session/session-host.ts +190 -0
  172. package/src/session/session-manager.ts +56 -0
  173. package/src/session/session-manifest.ts +29 -0
  174. package/src/session/session-service.team-persistence.test.ts +48 -0
  175. package/src/session/session-service.ts +610 -0
  176. package/src/session/sqlite-rpc-session-backend.ts +303 -0
  177. package/src/session/unified-session-persistence-service.ts +781 -0
  178. package/src/session/workspace-manager.ts +98 -0
  179. package/src/session/workspace-manifest.ts +100 -0
  180. package/src/storage/artifact-store.ts +1 -0
  181. package/src/storage/index.ts +11 -0
  182. package/src/storage/provider-settings-legacy-migration.test.ts +175 -0
  183. package/src/storage/provider-settings-legacy-migration.ts +637 -0
  184. package/src/storage/provider-settings-manager.test.ts +111 -0
  185. package/src/storage/provider-settings-manager.ts +129 -0
  186. package/src/storage/session-store.ts +1 -0
  187. package/src/storage/sqlite-session-store.ts +270 -0
  188. package/src/storage/sqlite-team-store.ts +443 -0
  189. package/src/storage/team-store.ts +5 -0
  190. package/src/team/index.ts +4 -0
  191. package/src/team/projections.ts +285 -0
  192. package/src/types/common.ts +14 -0
  193. package/src/types/config.ts +64 -0
  194. package/src/types/events.ts +46 -0
  195. package/src/types/index.ts +24 -0
  196. package/src/types/provider-settings.ts +43 -0
  197. package/src/types/sessions.ts +16 -0
  198. package/src/types/storage.ts +64 -0
  199. package/src/types/workspace.ts +7 -0
  200. package/src/types.ts +127 -0
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Editor Executor
3
+ *
4
+ * Built-in implementation for filesystem editing operations.
5
+ */
6
+
7
+ import * as fs from "node:fs/promises";
8
+ import * as path from "node:path";
9
+ import type { ToolContext } from "@clinebot/agents";
10
+ import type { EditFileInput } from "../schemas.js";
11
+ import type { EditorExecutor } from "../types.js";
12
+
13
+ /**
14
+ * Options for the editor executor
15
+ */
16
+ export interface EditorExecutorOptions {
17
+ /**
18
+ * File encoding used for read/write operations
19
+ * @default "utf-8"
20
+ */
21
+ encoding?: BufferEncoding;
22
+
23
+ /**
24
+ * Restrict relative-path file operations to paths inside cwd.
25
+ * Absolute paths are always accepted as-is.
26
+ * @default true
27
+ */
28
+ restrictToCwd?: boolean;
29
+
30
+ /**
31
+ * Maximum number of diff lines in str_replace output
32
+ * @default 200
33
+ */
34
+ maxDiffLines?: number;
35
+ }
36
+
37
+ function resolveFilePath(
38
+ cwd: string,
39
+ inputPath: string,
40
+ restrictToCwd: boolean,
41
+ ): string {
42
+ const isAbsoluteInput = path.isAbsolute(inputPath);
43
+ const resolved = isAbsoluteInput
44
+ ? path.normalize(inputPath)
45
+ : path.resolve(cwd, inputPath);
46
+ if (!restrictToCwd) {
47
+ return resolved;
48
+ }
49
+
50
+ // Absolute paths are accepted directly; cwd restriction applies to relative inputs.
51
+ if (isAbsoluteInput) {
52
+ return resolved;
53
+ }
54
+
55
+ const rel = path.relative(cwd, resolved);
56
+ if (rel.startsWith("..") || path.isAbsolute(rel)) {
57
+ throw new Error(`Path must stay within cwd: ${inputPath}`);
58
+ }
59
+ return resolved;
60
+ }
61
+
62
+ function countOccurrences(content: string, needle: string): number {
63
+ if (needle.length === 0) return 0;
64
+ return content.split(needle).length - 1;
65
+ }
66
+
67
+ function createLineDiff(
68
+ oldContent: string,
69
+ newContent: string,
70
+ maxLines: number,
71
+ ): string {
72
+ const oldLines = oldContent.split("\n");
73
+ const newLines = newContent.split("\n");
74
+ const max = Math.max(oldLines.length, newLines.length);
75
+ const out: string[] = ["```diff"];
76
+ let emitted = 0;
77
+
78
+ for (let i = 0; i < max; i++) {
79
+ if (emitted >= maxLines) {
80
+ out.push("... diff truncated ...");
81
+ break;
82
+ }
83
+
84
+ const oldLine = oldLines[i];
85
+ const newLine = newLines[i];
86
+
87
+ if (oldLine === newLine) {
88
+ continue;
89
+ }
90
+
91
+ const lineNo = i + 1;
92
+ if (oldLine !== undefined) {
93
+ out.push(`-${lineNo}: ${oldLine}`);
94
+ emitted++;
95
+ }
96
+ if (newLine !== undefined && emitted < maxLines) {
97
+ out.push(`+${lineNo}: ${newLine}`);
98
+ emitted++;
99
+ }
100
+ }
101
+
102
+ out.push("```");
103
+ return out.join("\n");
104
+ }
105
+
106
+ async function createFile(
107
+ filePath: string,
108
+ fileText: string,
109
+ encoding: BufferEncoding,
110
+ ): Promise<string> {
111
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
112
+ await fs.writeFile(filePath, fileText, { encoding });
113
+ return `File created successfully at: ${filePath}`;
114
+ }
115
+
116
+ async function replaceInFile(
117
+ filePath: string,
118
+ oldStr: string,
119
+ newStr: string | undefined,
120
+ encoding: BufferEncoding,
121
+ maxDiffLines: number,
122
+ ): Promise<string> {
123
+ const content = await fs.readFile(filePath, encoding);
124
+ const occurrences = countOccurrences(content, oldStr);
125
+
126
+ if (occurrences === 0) {
127
+ throw new Error(`No replacement performed: text not found in ${filePath}.`);
128
+ }
129
+
130
+ if (occurrences > 1) {
131
+ throw new Error(
132
+ `No replacement performed: multiple occurrences of text found in ${filePath}.`,
133
+ );
134
+ }
135
+
136
+ const updated = content.replace(oldStr, newStr ?? "");
137
+ await fs.writeFile(filePath, updated, { encoding });
138
+
139
+ const diff = createLineDiff(content, updated, maxDiffLines);
140
+ return `Edited ${filePath}\n${diff}`;
141
+ }
142
+
143
+ async function insertInFile(
144
+ filePath: string,
145
+ insertLine: number,
146
+ newStr: string,
147
+ encoding: BufferEncoding,
148
+ ): Promise<string> {
149
+ const content = await fs.readFile(filePath, encoding);
150
+ const lines = content.split("\n");
151
+
152
+ if (insertLine < 0 || insertLine > lines.length) {
153
+ throw new Error(
154
+ `Invalid line number: ${insertLine}. Valid range: 0-${lines.length}`,
155
+ );
156
+ }
157
+
158
+ lines.splice(insertLine, 0, ...newStr.split("\n"));
159
+ await fs.writeFile(filePath, lines.join("\n"), { encoding });
160
+
161
+ return `Inserted content at line ${insertLine} in ${filePath}.`;
162
+ }
163
+
164
+ /**
165
+ * Create an editor executor using Node.js fs module
166
+ */
167
+ export function createEditorExecutor(
168
+ options: EditorExecutorOptions = {},
169
+ ): EditorExecutor {
170
+ const {
171
+ encoding = "utf-8",
172
+ restrictToCwd = true,
173
+ maxDiffLines = 200,
174
+ } = options;
175
+
176
+ return async (
177
+ input: EditFileInput,
178
+ cwd: string,
179
+ _context: ToolContext,
180
+ ): Promise<string> => {
181
+ const filePath = resolveFilePath(cwd, input.path, restrictToCwd);
182
+
183
+ switch (input.command) {
184
+ case "create":
185
+ if (input.file_text === undefined) {
186
+ throw new Error(
187
+ "Parameter `file_text` is required for command: create",
188
+ );
189
+ }
190
+ return createFile(filePath, input.file_text, encoding);
191
+
192
+ case "str_replace":
193
+ if (input.old_str === undefined) {
194
+ throw new Error(
195
+ "Parameter `old_str` is required for command: str_replace",
196
+ );
197
+ }
198
+ return replaceInFile(
199
+ filePath,
200
+ input.old_str,
201
+ input.new_str,
202
+ encoding,
203
+ maxDiffLines,
204
+ );
205
+
206
+ case "insert":
207
+ if (input.insert_line === undefined) {
208
+ throw new Error(
209
+ "Parameter `insert_line` is required for insert command.",
210
+ );
211
+ }
212
+ if (input.new_str === undefined) {
213
+ throw new Error(
214
+ "Parameter `new_str` is required for insert command.",
215
+ );
216
+ }
217
+ return insertInFile(
218
+ filePath,
219
+ input.insert_line,
220
+ input.new_str,
221
+ encoding,
222
+ );
223
+
224
+ default:
225
+ throw new Error(
226
+ `Unrecognized command ${(input as { command: string }).command}. ` +
227
+ "Allowed commands are: create, str_replace, insert",
228
+ );
229
+ }
230
+ };
231
+ }
@@ -0,0 +1,25 @@
1
+ import * as fs from "node:fs/promises";
2
+ import * as os from "node:os";
3
+ import * as path from "node:path";
4
+ import { describe, expect, it } from "vitest";
5
+ import { createFileReadExecutor } from "./file-read.js";
6
+
7
+ describe("createFileReadExecutor", () => {
8
+ it("reads a file from an absolute path", async () => {
9
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), "agents-file-read-"));
10
+ const filePath = path.join(dir, "example.txt");
11
+ await fs.writeFile(filePath, "hello absolute path", "utf-8");
12
+
13
+ try {
14
+ const readFile = createFileReadExecutor();
15
+ const result = await readFile(filePath, {
16
+ agentId: "agent-1",
17
+ conversationId: "conv-1",
18
+ iteration: 1,
19
+ });
20
+ expect(result).toBe("hello absolute path");
21
+ } finally {
22
+ await fs.rm(dir, { recursive: true, force: true });
23
+ }
24
+ });
25
+ });
@@ -0,0 +1,94 @@
1
+ /**
2
+ * File Read Executor
3
+ *
4
+ * Built-in implementation for reading files using Node.js fs module.
5
+ */
6
+
7
+ import * as fs from "node:fs/promises";
8
+ import * as path from "node:path";
9
+ import type { ToolContext } from "@clinebot/agents";
10
+ import type { FileReadExecutor } from "../types.js";
11
+
12
+ /**
13
+ * Options for the file read executor
14
+ */
15
+ export interface FileReadExecutorOptions {
16
+ /**
17
+ * Maximum file size to read in bytes
18
+ * @default 10_000_000 (10MB)
19
+ */
20
+ maxFileSizeBytes?: number;
21
+
22
+ /**
23
+ * File encoding
24
+ * @default "utf-8"
25
+ */
26
+ encoding?: BufferEncoding;
27
+
28
+ /**
29
+ * Whether to include line numbers in output
30
+ * @default false
31
+ */
32
+ includeLineNumbers?: boolean;
33
+ }
34
+
35
+ /**
36
+ * Create a file read executor using Node.js fs module
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * const readFile = createFileReadExecutor({
41
+ * maxFileSizeBytes: 5_000_000, // 5MB limit
42
+ * includeLineNumbers: true,
43
+ * })
44
+ *
45
+ * const content = await readFile("/path/to/file.ts", context)
46
+ * ```
47
+ */
48
+ export function createFileReadExecutor(
49
+ options: FileReadExecutorOptions = {},
50
+ ): FileReadExecutor {
51
+ const {
52
+ maxFileSizeBytes = 10_000_000,
53
+ encoding = "utf-8",
54
+ includeLineNumbers = false,
55
+ } = options;
56
+
57
+ return async (filePath: string, _context: ToolContext): Promise<string> => {
58
+ const resolvedPath = path.isAbsolute(filePath)
59
+ ? path.normalize(filePath)
60
+ : path.resolve(process.cwd(), filePath);
61
+
62
+ // Check if file exists
63
+ const stat = await fs.stat(resolvedPath);
64
+
65
+ if (!stat.isFile()) {
66
+ throw new Error(`Path is not a file: ${resolvedPath}`);
67
+ }
68
+
69
+ // Check file size
70
+ if (stat.size > maxFileSizeBytes) {
71
+ throw new Error(
72
+ `File too large: ${stat.size} bytes (max: ${maxFileSizeBytes} bytes). ` +
73
+ `Consider reading specific sections or using a different approach.`,
74
+ );
75
+ }
76
+
77
+ // Read file content
78
+ const content = await fs.readFile(resolvedPath, encoding);
79
+
80
+ // Optionally add line numbers
81
+ if (includeLineNumbers) {
82
+ const lines = content.split("\n");
83
+ const maxLineNumWidth = String(lines.length).length;
84
+ return lines
85
+ .map(
86
+ (line, i) =>
87
+ `${String(i + 1).padStart(maxLineNumWidth, " ")} | ${line}`,
88
+ )
89
+ .join("\n");
90
+ }
91
+
92
+ return content;
93
+ };
94
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Built-in Executor Implementations
3
+ *
4
+ * This module provides ready-to-use implementations of the tool executors
5
+ * using Node.js built-in modules. These can be used directly or as references
6
+ * for custom implementations.
7
+ */
8
+
9
+ import type { ToolExecutors } from "../types.js";
10
+ import { createApplyPatchExecutor } from "./apply-patch.js";
11
+ import { createBashExecutor } from "./bash.js";
12
+ import { createEditorExecutor } from "./editor.js";
13
+ import { createFileReadExecutor } from "./file-read.js";
14
+ import { createSearchExecutor } from "./search.js";
15
+ import { createWebFetchExecutor } from "./web-fetch.js";
16
+
17
+ // Re-export individual executors and their options types
18
+ export {
19
+ type ApplyPatchExecutorOptions,
20
+ createApplyPatchExecutor,
21
+ } from "./apply-patch.js";
22
+ export { type BashExecutorOptions, createBashExecutor } from "./bash.js";
23
+ export { createEditorExecutor, type EditorExecutorOptions } from "./editor.js";
24
+ export {
25
+ createFileReadExecutor,
26
+ type FileReadExecutorOptions,
27
+ } from "./file-read.js";
28
+ export { createSearchExecutor, type SearchExecutorOptions } from "./search.js";
29
+ export {
30
+ createWebFetchExecutor,
31
+ type WebFetchExecutorOptions,
32
+ } from "./web-fetch.js";
33
+
34
+ /**
35
+ * Options for creating default executors
36
+ */
37
+ export interface DefaultExecutorsOptions {
38
+ fileRead?: import("./file-read.js").FileReadExecutorOptions;
39
+ search?: import("./search.js").SearchExecutorOptions;
40
+ bash?: import("./bash.js").BashExecutorOptions;
41
+ webFetch?: import("./web-fetch.js").WebFetchExecutorOptions;
42
+ applyPatch?: import("./apply-patch.js").ApplyPatchExecutorOptions;
43
+ editor?: import("./editor.js").EditorExecutorOptions;
44
+ }
45
+
46
+ /**
47
+ * Create all default executors with optional configuration
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * import { createDefaultTools, createDefaultExecutors } from "@clinebot/core/server"
52
+ *
53
+ * const executors = createDefaultExecutors({
54
+ * bash: { timeoutMs: 60000 },
55
+ * search: { maxResults: 50 },
56
+ * })
57
+ *
58
+ * const tools = createDefaultTools({
59
+ * executors,
60
+ * cwd: "/path/to/project",
61
+ * })
62
+ * ```
63
+ */
64
+ export function createDefaultExecutors(
65
+ options: DefaultExecutorsOptions = {},
66
+ ): ToolExecutors {
67
+ return {
68
+ readFile: createFileReadExecutor(options.fileRead),
69
+ search: createSearchExecutor(options.search),
70
+ bash: createBashExecutor(options.bash),
71
+ webFetch: createWebFetchExecutor(options.webFetch),
72
+ applyPatch: createApplyPatchExecutor(options.applyPatch),
73
+ editor: createEditorExecutor(options.editor),
74
+ };
75
+ }
@@ -0,0 +1,278 @@
1
+ /**
2
+ * Search Executor
3
+ *
4
+ * Built-in implementation for searching the codebase using regex.
5
+ */
6
+
7
+ import * as fs from "node:fs/promises";
8
+ import * as path from "node:path";
9
+ import type { ToolContext } from "@clinebot/agents";
10
+ import { getFileIndex } from "../../input";
11
+ import type { SearchExecutor } from "../types.js";
12
+
13
+ /**
14
+ * Options for the search executor
15
+ */
16
+ export interface SearchExecutorOptions {
17
+ /**
18
+ * File extensions to include in search (without dot)
19
+ * @default common code extensions
20
+ */
21
+ includeExtensions?: string[];
22
+
23
+ /**
24
+ * Directories to exclude from search
25
+ * @default ["node_modules", ".git", "dist", "build", ".next", "coverage"]
26
+ */
27
+ excludeDirs?: string[];
28
+
29
+ /**
30
+ * Maximum number of results to return
31
+ * @default 100
32
+ */
33
+ maxResults?: number;
34
+
35
+ /**
36
+ * Number of context lines before and after match
37
+ * @default 2
38
+ */
39
+ contextLines?: number;
40
+
41
+ /**
42
+ * Maximum depth to traverse
43
+ * @default 20
44
+ */
45
+ maxDepth?: number;
46
+ }
47
+
48
+ const DEFAULT_INCLUDE_EXTENSIONS = [
49
+ "ts",
50
+ "tsx",
51
+ "js",
52
+ "jsx",
53
+ "mjs",
54
+ "cjs",
55
+ "json",
56
+ "md",
57
+ "mdx",
58
+ "txt",
59
+ "yaml",
60
+ "yml",
61
+ "toml",
62
+ "py",
63
+ "rb",
64
+ "go",
65
+ "rs",
66
+ "java",
67
+ "kt",
68
+ "swift",
69
+ "c",
70
+ "cpp",
71
+ "h",
72
+ "hpp",
73
+ "css",
74
+ "scss",
75
+ "less",
76
+ "html",
77
+ "vue",
78
+ "svelte",
79
+ "sql",
80
+ "sh",
81
+ "bash",
82
+ "zsh",
83
+ "fish",
84
+ "ps1",
85
+ "env",
86
+ "gitignore",
87
+ "dockerignore",
88
+ "editorconfig",
89
+ ];
90
+
91
+ const DEFAULT_EXCLUDE_DIRS = [
92
+ "node_modules",
93
+ ".git",
94
+ "dist",
95
+ "build",
96
+ ".next",
97
+ "coverage",
98
+ "__pycache__",
99
+ ".venv",
100
+ "venv",
101
+ ".cache",
102
+ ".turbo",
103
+ ".output",
104
+ "out",
105
+ "target",
106
+ "bin",
107
+ "obj",
108
+ ];
109
+
110
+ /**
111
+ * Search result for a single file match
112
+ */
113
+ interface SearchMatch {
114
+ file: string;
115
+ line: number;
116
+ column: number;
117
+ match: string;
118
+ context: string[];
119
+ }
120
+
121
+ function shouldIncludeFile(
122
+ relativePath: string,
123
+ excludeDirs: Set<string>,
124
+ includeExtensions: Set<string>,
125
+ maxDepth: number,
126
+ ): boolean {
127
+ const segments = relativePath.split("/");
128
+ const fileName = segments[segments.length - 1] ?? "";
129
+ const directoryDepth = segments.length - 1;
130
+
131
+ if (directoryDepth > maxDepth) {
132
+ return false;
133
+ }
134
+
135
+ for (let i = 0; i < segments.length - 1; i++) {
136
+ if (excludeDirs.has(segments[i] ?? "")) {
137
+ return false;
138
+ }
139
+ }
140
+
141
+ const ext = path.posix.extname(fileName).slice(1).toLowerCase();
142
+ return includeExtensions.has(ext) || (!ext && !fileName.startsWith("."));
143
+ }
144
+
145
+ /**
146
+ * Create a search executor using regex pattern matching
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * const search = createSearchExecutor({
151
+ * maxResults: 50,
152
+ * contextLines: 3,
153
+ * })
154
+ *
155
+ * const results = await search("function\\s+handleClick", "/path/to/project", context)
156
+ * ```
157
+ */
158
+ export function createSearchExecutor(
159
+ options: SearchExecutorOptions = {},
160
+ ): SearchExecutor {
161
+ const {
162
+ includeExtensions = DEFAULT_INCLUDE_EXTENSIONS,
163
+ excludeDirs = DEFAULT_EXCLUDE_DIRS,
164
+ maxResults = 100,
165
+ contextLines = 2,
166
+ maxDepth = 20,
167
+ } = options;
168
+ const excludeDirsSet = new Set(excludeDirs);
169
+ const includeExtensionsSet = new Set(
170
+ includeExtensions.map((extension) => extension.toLowerCase()),
171
+ );
172
+
173
+ return async (
174
+ query: string,
175
+ cwd: string,
176
+ _context: ToolContext,
177
+ ): Promise<string> => {
178
+ // Compile regex
179
+ let regex: RegExp;
180
+ try {
181
+ regex = new RegExp(query, "gim");
182
+ } catch (error) {
183
+ throw new Error(
184
+ `Invalid regex pattern: ${query}. ${error instanceof Error ? error.message : ""}`,
185
+ );
186
+ }
187
+
188
+ const matches: SearchMatch[] = [];
189
+ let totalFilesSearched = 0;
190
+
191
+ const fileList = await getFileIndex(cwd);
192
+
193
+ // Search files from the fast index.
194
+ for (const relativePath of fileList) {
195
+ if (
196
+ !shouldIncludeFile(
197
+ relativePath,
198
+ excludeDirsSet,
199
+ includeExtensionsSet,
200
+ maxDepth,
201
+ )
202
+ ) {
203
+ continue;
204
+ }
205
+
206
+ if (matches.length >= maxResults) break;
207
+
208
+ totalFilesSearched++;
209
+ const filePath = path.join(cwd, relativePath);
210
+
211
+ try {
212
+ const content = await fs.readFile(filePath, "utf-8");
213
+ const lines = content.split("\n");
214
+
215
+ for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
216
+ const line = lines[lineIdx];
217
+ regex.lastIndex = 0; // Reset regex state
218
+
219
+ let match: RegExpExecArray | null;
220
+ while ((match = regex.exec(line)) !== null) {
221
+ if (matches.length >= maxResults) break;
222
+
223
+ // Get context lines
224
+ const contextStart = Math.max(0, lineIdx - contextLines);
225
+ const contextEnd = Math.min(
226
+ lines.length - 1,
227
+ lineIdx + contextLines,
228
+ );
229
+ const contextLinesArr: string[] = [];
230
+
231
+ for (let i = contextStart; i <= contextEnd; i++) {
232
+ const prefix = i === lineIdx ? ">" : " ";
233
+ contextLinesArr.push(`${prefix} ${i + 1}: ${lines[i]}`);
234
+ }
235
+
236
+ matches.push({
237
+ file: relativePath,
238
+ line: lineIdx + 1,
239
+ column: match.index + 1,
240
+ match: match[0],
241
+ context: contextLinesArr,
242
+ });
243
+
244
+ // Prevent infinite loop on zero-length matches
245
+ if (match.index === regex.lastIndex) {
246
+ regex.lastIndex++;
247
+ }
248
+ }
249
+ }
250
+ } catch {}
251
+ }
252
+
253
+ // Format results
254
+ if (matches.length === 0) {
255
+ return `No results found for pattern: ${query}\nSearched ${totalFilesSearched} files.`;
256
+ }
257
+
258
+ const resultLines: string[] = [
259
+ `Found ${matches.length} result${matches.length === 1 ? "" : "s"} for pattern: ${query}`,
260
+ `Searched ${totalFilesSearched} files.`,
261
+ "",
262
+ ];
263
+
264
+ for (const match of matches) {
265
+ resultLines.push(`${match.file}:${match.line}:${match.column}`);
266
+ resultLines.push(...match.context);
267
+ resultLines.push("");
268
+ }
269
+
270
+ if (matches.length >= maxResults) {
271
+ resultLines.push(
272
+ `(Showing first ${maxResults} results. Refine your search for more specific results.)`,
273
+ );
274
+ }
275
+
276
+ return resultLines.join("\n");
277
+ };
278
+ }