@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.
- package/README.md +88 -0
- package/dist/account/cline-account-service.d.ts +34 -0
- package/dist/account/index.d.ts +3 -0
- package/dist/account/rpc.d.ts +38 -0
- package/dist/account/types.d.ts +74 -0
- package/dist/agents/agent-config-loader.d.ts +18 -0
- package/dist/agents/agent-config-parser.d.ts +25 -0
- package/dist/agents/hooks-config-loader.d.ts +23 -0
- package/dist/agents/index.d.ts +11 -0
- package/dist/agents/plugin-config-loader.d.ts +22 -0
- package/dist/agents/plugin-loader.d.ts +9 -0
- package/dist/agents/plugin-sandbox.d.ts +12 -0
- package/dist/agents/unified-config-file-watcher.d.ts +77 -0
- package/dist/agents/user-instruction-config-loader.d.ts +63 -0
- package/dist/auth/client.d.ts +11 -0
- package/dist/auth/cline.d.ts +41 -0
- package/dist/auth/codex.d.ts +39 -0
- package/dist/auth/oca.d.ts +22 -0
- package/dist/auth/server.d.ts +22 -0
- package/dist/auth/types.d.ts +72 -0
- package/dist/auth/utils.d.ts +32 -0
- package/dist/chat/chat-schema.d.ts +145 -0
- package/dist/default-tools/constants.d.ts +23 -0
- package/dist/default-tools/definitions.d.ts +96 -0
- package/dist/default-tools/executors/apply-patch-parser.d.ts +68 -0
- package/dist/default-tools/executors/apply-patch.d.ts +26 -0
- package/dist/default-tools/executors/bash.d.ts +49 -0
- package/dist/default-tools/executors/editor.d.ts +31 -0
- package/dist/default-tools/executors/file-read.d.ts +40 -0
- package/dist/default-tools/executors/index.d.ts +44 -0
- package/dist/default-tools/executors/search.d.ts +50 -0
- package/dist/default-tools/executors/web-fetch.d.ts +58 -0
- package/dist/default-tools/index.d.ts +57 -0
- package/dist/default-tools/presets.d.ts +124 -0
- package/dist/default-tools/schemas.d.ts +121 -0
- package/dist/default-tools/types.d.ts +237 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +220 -0
- package/dist/input/file-indexer.d.ts +5 -0
- package/dist/input/index.d.ts +4 -0
- package/dist/input/mention-enricher.d.ts +12 -0
- package/dist/mcp/config-loader.d.ts +15 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/manager.d.ts +24 -0
- package/dist/mcp/types.d.ts +66 -0
- package/dist/runtime/hook-file-hooks.d.ts +18 -0
- package/dist/runtime/rules.d.ts +5 -0
- package/dist/runtime/runtime-builder.d.ts +5 -0
- package/dist/runtime/sandbox/subprocess-sandbox.d.ts +19 -0
- package/dist/runtime/session-runtime.d.ts +36 -0
- package/dist/runtime/tool-approval.d.ts +9 -0
- package/dist/runtime/workflows.d.ts +13 -0
- package/dist/server/index.d.ts +47 -0
- package/dist/server/index.js +641 -0
- package/dist/session/default-session-manager.d.ts +77 -0
- package/dist/session/rpc-session-service.d.ts +12 -0
- package/dist/session/runtime-oauth-token-manager.d.ts +28 -0
- package/dist/session/session-artifacts.d.ts +19 -0
- package/dist/session/session-graph.d.ts +15 -0
- package/dist/session/session-host.d.ts +21 -0
- package/dist/session/session-manager.d.ts +50 -0
- package/dist/session/session-manifest.d.ts +30 -0
- package/dist/session/session-service.d.ts +113 -0
- package/dist/session/sqlite-rpc-session-backend.d.ts +30 -0
- package/dist/session/unified-session-persistence-service.d.ts +93 -0
- package/dist/session/workspace-manager.d.ts +28 -0
- package/dist/session/workspace-manifest.d.ts +25 -0
- package/dist/storage/provider-settings-legacy-migration.d.ts +13 -0
- package/dist/storage/provider-settings-manager.d.ts +20 -0
- package/dist/storage/sqlite-session-store.d.ts +29 -0
- package/dist/storage/sqlite-team-store.d.ts +31 -0
- package/dist/storage/team-store.d.ts +2 -0
- package/dist/team/index.d.ts +1 -0
- package/dist/team/projections.d.ts +8 -0
- package/dist/types/common.d.ts +10 -0
- package/dist/types/config.d.ts +37 -0
- package/dist/types/events.d.ts +54 -0
- package/dist/types/provider-settings.d.ts +20 -0
- package/dist/types/sessions.d.ts +9 -0
- package/dist/types/storage.d.ts +37 -0
- package/dist/types/workspace.d.ts +7 -0
- package/dist/types.d.ts +26 -0
- package/package.json +63 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +267 -0
- package/src/account/index.ts +20 -0
- package/src/account/rpc.test.ts +62 -0
- package/src/account/rpc.ts +172 -0
- package/src/account/types.ts +80 -0
- package/src/agents/agent-config-loader.test.ts +234 -0
- package/src/agents/agent-config-loader.ts +107 -0
- package/src/agents/agent-config-parser.ts +191 -0
- package/src/agents/hooks-config-loader.ts +97 -0
- package/src/agents/index.ts +84 -0
- package/src/agents/plugin-config-loader.test.ts +91 -0
- package/src/agents/plugin-config-loader.ts +160 -0
- package/src/agents/plugin-loader.test.ts +102 -0
- package/src/agents/plugin-loader.ts +105 -0
- package/src/agents/plugin-sandbox.test.ts +120 -0
- package/src/agents/plugin-sandbox.ts +471 -0
- package/src/agents/unified-config-file-watcher.test.ts +196 -0
- package/src/agents/unified-config-file-watcher.ts +483 -0
- package/src/agents/user-instruction-config-loader.test.ts +158 -0
- package/src/agents/user-instruction-config-loader.ts +438 -0
- package/src/auth/client.test.ts +40 -0
- package/src/auth/client.ts +25 -0
- package/src/auth/cline.test.ts +130 -0
- package/src/auth/cline.ts +414 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +466 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +546 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +78 -0
- package/src/auth/utils.test.ts +128 -0
- package/src/auth/utils.ts +247 -0
- package/src/chat/chat-schema.ts +82 -0
- package/src/default-tools/constants.ts +35 -0
- package/src/default-tools/definitions.test.ts +233 -0
- package/src/default-tools/definitions.ts +632 -0
- package/src/default-tools/executors/apply-patch-parser.ts +520 -0
- package/src/default-tools/executors/apply-patch.ts +359 -0
- package/src/default-tools/executors/bash.ts +205 -0
- package/src/default-tools/executors/editor.ts +231 -0
- package/src/default-tools/executors/file-read.test.ts +25 -0
- package/src/default-tools/executors/file-read.ts +94 -0
- package/src/default-tools/executors/index.ts +75 -0
- package/src/default-tools/executors/search.ts +278 -0
- package/src/default-tools/executors/web-fetch.ts +259 -0
- package/src/default-tools/index.ts +161 -0
- package/src/default-tools/presets.test.ts +63 -0
- package/src/default-tools/presets.ts +168 -0
- package/src/default-tools/schemas.ts +228 -0
- package/src/default-tools/types.ts +324 -0
- package/src/index.ts +119 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +87 -0
- package/src/input/file-indexer.ts +280 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +82 -0
- package/src/input/mention-enricher.ts +119 -0
- package/src/mcp/config-loader.test.ts +238 -0
- package/src/mcp/config-loader.ts +219 -0
- package/src/mcp/index.ts +26 -0
- package/src/mcp/manager.test.ts +106 -0
- package/src/mcp/manager.ts +262 -0
- package/src/mcp/types.ts +88 -0
- package/src/runtime/hook-file-hooks.test.ts +106 -0
- package/src/runtime/hook-file-hooks.ts +736 -0
- package/src/runtime/index.ts +27 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +203 -0
- package/src/runtime/runtime-builder.test.ts +215 -0
- package/src/runtime/runtime-builder.ts +515 -0
- package/src/runtime/runtime-parity.test.ts +132 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +207 -0
- package/src/runtime/session-runtime.ts +44 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +54 -0
- package/src/server/index.ts +282 -0
- package/src/session/default-session-manager.e2e.test.ts +354 -0
- package/src/session/default-session-manager.test.ts +816 -0
- package/src/session/default-session-manager.ts +1286 -0
- package/src/session/index.ts +37 -0
- package/src/session/rpc-session-service.ts +189 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +265 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-graph.ts +90 -0
- package/src/session/session-host.ts +190 -0
- package/src/session/session-manager.ts +56 -0
- package/src/session/session-manifest.ts +29 -0
- package/src/session/session-service.team-persistence.test.ts +48 -0
- package/src/session/session-service.ts +610 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.ts +781 -0
- package/src/session/workspace-manager.ts +98 -0
- package/src/session/workspace-manifest.ts +100 -0
- package/src/storage/artifact-store.ts +1 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +175 -0
- package/src/storage/provider-settings-legacy-migration.ts +637 -0
- package/src/storage/provider-settings-manager.test.ts +111 -0
- package/src/storage/provider-settings-manager.ts +129 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +270 -0
- package/src/storage/sqlite-team-store.ts +443 -0
- package/src/storage/team-store.ts +5 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/types/common.ts +14 -0
- package/src/types/config.ts +64 -0
- package/src/types/events.ts +46 -0
- package/src/types/index.ts +24 -0
- package/src/types/provider-settings.ts +43 -0
- package/src/types/sessions.ts +16 -0
- package/src/types/storage.ts +64 -0
- package/src/types/workspace.ts +7 -0
- package/src/types.ts +127 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Apply Patch Executor
|
|
3
|
+
*
|
|
4
|
+
* Built-in implementation for the legacy apply_patch format.
|
|
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 { ApplyPatchInput } from "../schemas.js";
|
|
11
|
+
import type { ApplyPatchExecutor } from "../types.js";
|
|
12
|
+
import {
|
|
13
|
+
BASH_WRAPPERS,
|
|
14
|
+
DiffError,
|
|
15
|
+
PATCH_MARKERS,
|
|
16
|
+
PatchActionType,
|
|
17
|
+
type PatchChunk,
|
|
18
|
+
PatchParser,
|
|
19
|
+
} from "./apply-patch-parser.js";
|
|
20
|
+
|
|
21
|
+
interface FileChange {
|
|
22
|
+
type: PatchActionType;
|
|
23
|
+
oldContent?: string;
|
|
24
|
+
newContent?: string;
|
|
25
|
+
movePath?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Options for the apply_patch executor
|
|
30
|
+
*/
|
|
31
|
+
export interface ApplyPatchExecutorOptions {
|
|
32
|
+
/**
|
|
33
|
+
* File encoding used for read/write operations
|
|
34
|
+
* @default "utf-8"
|
|
35
|
+
*/
|
|
36
|
+
encoding?: BufferEncoding;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Restrict relative-path file operations to paths inside cwd.
|
|
40
|
+
* Absolute paths are always accepted as-is.
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
restrictToCwd?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveFilePath(
|
|
47
|
+
cwd: string,
|
|
48
|
+
inputPath: string,
|
|
49
|
+
restrictToCwd: boolean,
|
|
50
|
+
): string {
|
|
51
|
+
const isAbsoluteInput = path.isAbsolute(inputPath);
|
|
52
|
+
const resolved = isAbsoluteInput
|
|
53
|
+
? path.normalize(inputPath)
|
|
54
|
+
: path.resolve(cwd, inputPath);
|
|
55
|
+
if (!restrictToCwd || isAbsoluteInput) {
|
|
56
|
+
return resolved;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const rel = path.relative(cwd, resolved);
|
|
60
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
61
|
+
throw new DiffError(`Path must stay within cwd: ${inputPath}`);
|
|
62
|
+
}
|
|
63
|
+
return resolved;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function stripBashWrapper(lines: string[]): string[] {
|
|
67
|
+
const result: string[] = [];
|
|
68
|
+
let insidePatch = false;
|
|
69
|
+
let foundBegin = false;
|
|
70
|
+
let foundContent = false;
|
|
71
|
+
|
|
72
|
+
for (let i = 0; i < lines.length; i++) {
|
|
73
|
+
const line = lines[i];
|
|
74
|
+
if (
|
|
75
|
+
!insidePatch &&
|
|
76
|
+
BASH_WRAPPERS.some((wrapper) => line.startsWith(wrapper))
|
|
77
|
+
) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (line.startsWith(PATCH_MARKERS.BEGIN)) {
|
|
82
|
+
insidePatch = true;
|
|
83
|
+
foundBegin = true;
|
|
84
|
+
result.push(line);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (line === PATCH_MARKERS.END) {
|
|
89
|
+
insidePatch = false;
|
|
90
|
+
result.push(line);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const isPatchContent =
|
|
95
|
+
line.startsWith(PATCH_MARKERS.ADD) ||
|
|
96
|
+
line.startsWith(PATCH_MARKERS.UPDATE) ||
|
|
97
|
+
line.startsWith(PATCH_MARKERS.DELETE) ||
|
|
98
|
+
line.startsWith(PATCH_MARKERS.MOVE) ||
|
|
99
|
+
line.startsWith(PATCH_MARKERS.SECTION) ||
|
|
100
|
+
line.startsWith("+") ||
|
|
101
|
+
line.startsWith("-") ||
|
|
102
|
+
line.startsWith(" ") ||
|
|
103
|
+
line === "***";
|
|
104
|
+
|
|
105
|
+
if (isPatchContent && i !== lines.length - 1) {
|
|
106
|
+
foundContent = true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (
|
|
110
|
+
insidePatch ||
|
|
111
|
+
(!foundBegin && isPatchContent) ||
|
|
112
|
+
(line === "" && foundContent)
|
|
113
|
+
) {
|
|
114
|
+
result.push(line);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
while (result.length > 0 && result[result.length - 1] === "") {
|
|
119
|
+
result.pop();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return !foundBegin && !foundContent ? lines : result;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function preprocessLines(input: string): string[] {
|
|
126
|
+
let lines = input.split("\n").map((line) => line.replace(/\r$/, ""));
|
|
127
|
+
lines = stripBashWrapper(lines);
|
|
128
|
+
|
|
129
|
+
const hasBegin = lines.length > 0 && lines[0].startsWith(PATCH_MARKERS.BEGIN);
|
|
130
|
+
const hasEnd =
|
|
131
|
+
lines.length > 0 && lines[lines.length - 1] === PATCH_MARKERS.END;
|
|
132
|
+
if (!hasBegin && !hasEnd) {
|
|
133
|
+
return [PATCH_MARKERS.BEGIN, ...lines, PATCH_MARKERS.END];
|
|
134
|
+
}
|
|
135
|
+
if (hasBegin && hasEnd) {
|
|
136
|
+
return lines;
|
|
137
|
+
}
|
|
138
|
+
throw new DiffError(
|
|
139
|
+
"Invalid patch text - incomplete sentinels. Try breaking it into smaller patches.",
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function extractFilesForOperations(
|
|
144
|
+
text: string,
|
|
145
|
+
markers: readonly string[],
|
|
146
|
+
): string[] {
|
|
147
|
+
const lines = stripBashWrapper(text.split("\n"));
|
|
148
|
+
const files: string[] = [];
|
|
149
|
+
|
|
150
|
+
for (const line of lines) {
|
|
151
|
+
for (const marker of markers) {
|
|
152
|
+
if (!line.startsWith(marker)) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const file = line.substring(marker.length).trim();
|
|
156
|
+
if (!text.trim().endsWith(file)) {
|
|
157
|
+
files.push(file);
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return files;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function applyChunks(
|
|
167
|
+
content: string,
|
|
168
|
+
chunks: PatchChunk[],
|
|
169
|
+
filePath: string,
|
|
170
|
+
): string {
|
|
171
|
+
if (chunks.length === 0) {
|
|
172
|
+
return content;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const lines = content.split("\n");
|
|
176
|
+
const result: string[] = [];
|
|
177
|
+
let currentIndex = 0;
|
|
178
|
+
|
|
179
|
+
for (const chunk of chunks) {
|
|
180
|
+
if (chunk.origIndex > lines.length) {
|
|
181
|
+
throw new DiffError(
|
|
182
|
+
`${filePath}: chunk.origIndex ${chunk.origIndex} > lines.length ${lines.length}`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (currentIndex > chunk.origIndex) {
|
|
186
|
+
throw new DiffError(
|
|
187
|
+
`${filePath}: currentIndex ${currentIndex} > chunk.origIndex ${chunk.origIndex}`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
result.push(...lines.slice(currentIndex, chunk.origIndex));
|
|
191
|
+
result.push(...chunk.insLines);
|
|
192
|
+
currentIndex = chunk.origIndex + chunk.delLines.length;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
result.push(...lines.slice(currentIndex));
|
|
196
|
+
return result.join("\n");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function loadFiles(
|
|
200
|
+
rawInput: string,
|
|
201
|
+
cwd: string,
|
|
202
|
+
encoding: BufferEncoding,
|
|
203
|
+
restrictToCwd: boolean,
|
|
204
|
+
): Promise<Record<string, string>> {
|
|
205
|
+
const filesToLoad = extractFilesForOperations(rawInput, [
|
|
206
|
+
PATCH_MARKERS.UPDATE,
|
|
207
|
+
PATCH_MARKERS.DELETE,
|
|
208
|
+
]);
|
|
209
|
+
const files: Record<string, string> = {};
|
|
210
|
+
|
|
211
|
+
for (const filePath of filesToLoad) {
|
|
212
|
+
const absolutePath = resolveFilePath(cwd, filePath, restrictToCwd);
|
|
213
|
+
let fileContent: string;
|
|
214
|
+
try {
|
|
215
|
+
fileContent = await fs.readFile(absolutePath, encoding);
|
|
216
|
+
} catch {
|
|
217
|
+
throw new DiffError(`File not found: ${filePath}`);
|
|
218
|
+
}
|
|
219
|
+
files[filePath] = fileContent.replace(/\r\n/g, "\n");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return files;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function patchToChanges(
|
|
226
|
+
patch: ReturnType<PatchParser["parse"]>["patch"],
|
|
227
|
+
originalFiles: Record<string, string>,
|
|
228
|
+
): Record<string, FileChange> {
|
|
229
|
+
const changes: Record<string, FileChange> = {};
|
|
230
|
+
|
|
231
|
+
for (const [filePath, action] of Object.entries(patch.actions)) {
|
|
232
|
+
switch (action.type) {
|
|
233
|
+
case PatchActionType.DELETE:
|
|
234
|
+
changes[filePath] = {
|
|
235
|
+
type: PatchActionType.DELETE,
|
|
236
|
+
oldContent: originalFiles[filePath],
|
|
237
|
+
};
|
|
238
|
+
break;
|
|
239
|
+
case PatchActionType.ADD:
|
|
240
|
+
if (action.newFile === undefined) {
|
|
241
|
+
throw new DiffError("ADD action without file content");
|
|
242
|
+
}
|
|
243
|
+
changes[filePath] = {
|
|
244
|
+
type: PatchActionType.ADD,
|
|
245
|
+
newContent: action.newFile,
|
|
246
|
+
};
|
|
247
|
+
break;
|
|
248
|
+
case PatchActionType.UPDATE:
|
|
249
|
+
changes[filePath] = {
|
|
250
|
+
type: PatchActionType.UPDATE,
|
|
251
|
+
oldContent: originalFiles[filePath],
|
|
252
|
+
newContent: applyChunks(
|
|
253
|
+
originalFiles[filePath] ?? "",
|
|
254
|
+
action.chunks,
|
|
255
|
+
filePath,
|
|
256
|
+
),
|
|
257
|
+
movePath: action.movePath,
|
|
258
|
+
};
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return changes;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function applyChanges(
|
|
267
|
+
changes: Record<string, FileChange>,
|
|
268
|
+
cwd: string,
|
|
269
|
+
encoding: BufferEncoding,
|
|
270
|
+
restrictToCwd: boolean,
|
|
271
|
+
): Promise<string[]> {
|
|
272
|
+
const touched: string[] = [];
|
|
273
|
+
|
|
274
|
+
for (const [filePath, change] of Object.entries(changes)) {
|
|
275
|
+
const sourceAbsPath = resolveFilePath(cwd, filePath, restrictToCwd);
|
|
276
|
+
switch (change.type) {
|
|
277
|
+
case PatchActionType.DELETE:
|
|
278
|
+
await fs.rm(sourceAbsPath, { force: true });
|
|
279
|
+
touched.push(`${filePath}: [deleted]`);
|
|
280
|
+
break;
|
|
281
|
+
case PatchActionType.ADD:
|
|
282
|
+
if (change.newContent === undefined) {
|
|
283
|
+
throw new DiffError(`Cannot create ${filePath} with no content`);
|
|
284
|
+
}
|
|
285
|
+
await fs.mkdir(path.dirname(sourceAbsPath), { recursive: true });
|
|
286
|
+
await fs.writeFile(sourceAbsPath, change.newContent, { encoding });
|
|
287
|
+
touched.push(filePath);
|
|
288
|
+
break;
|
|
289
|
+
case PatchActionType.UPDATE: {
|
|
290
|
+
if (change.newContent === undefined) {
|
|
291
|
+
throw new DiffError(
|
|
292
|
+
`UPDATE change for ${filePath} has no new content`,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (change.movePath) {
|
|
297
|
+
const moveAbsPath = resolveFilePath(
|
|
298
|
+
cwd,
|
|
299
|
+
change.movePath,
|
|
300
|
+
restrictToCwd,
|
|
301
|
+
);
|
|
302
|
+
await fs.mkdir(path.dirname(moveAbsPath), { recursive: true });
|
|
303
|
+
await fs.writeFile(moveAbsPath, change.newContent, { encoding });
|
|
304
|
+
await fs.rm(sourceAbsPath, { force: true });
|
|
305
|
+
touched.push(`${filePath} -> ${change.movePath}`);
|
|
306
|
+
} else {
|
|
307
|
+
await fs.writeFile(sourceAbsPath, change.newContent, { encoding });
|
|
308
|
+
touched.push(filePath);
|
|
309
|
+
}
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return touched;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Create an apply_patch executor using Node.js fs module.
|
|
320
|
+
*/
|
|
321
|
+
export function createApplyPatchExecutor(
|
|
322
|
+
options: ApplyPatchExecutorOptions = {},
|
|
323
|
+
): ApplyPatchExecutor {
|
|
324
|
+
const { encoding = "utf-8", restrictToCwd = true } = options;
|
|
325
|
+
|
|
326
|
+
return async (
|
|
327
|
+
input: ApplyPatchInput,
|
|
328
|
+
cwd: string,
|
|
329
|
+
_context: ToolContext,
|
|
330
|
+
): Promise<string> => {
|
|
331
|
+
const lines = preprocessLines(input.input);
|
|
332
|
+
const currentFiles = await loadFiles(
|
|
333
|
+
input.input,
|
|
334
|
+
cwd,
|
|
335
|
+
encoding,
|
|
336
|
+
restrictToCwd,
|
|
337
|
+
);
|
|
338
|
+
const parser = new PatchParser(lines, currentFiles);
|
|
339
|
+
const { patch, fuzz } = parser.parse();
|
|
340
|
+
const changes = patchToChanges(patch, currentFiles);
|
|
341
|
+
const touched = await applyChanges(changes, cwd, encoding, restrictToCwd);
|
|
342
|
+
|
|
343
|
+
const responseLines = [
|
|
344
|
+
"Successfully applied patch to the following files:",
|
|
345
|
+
];
|
|
346
|
+
for (const file of touched) {
|
|
347
|
+
responseLines.push(file);
|
|
348
|
+
}
|
|
349
|
+
if (fuzz > 0) {
|
|
350
|
+
responseLines.push(`Note: Patch applied with fuzz factor ${fuzz}`);
|
|
351
|
+
}
|
|
352
|
+
if (patch.warnings && patch.warnings.length > 0) {
|
|
353
|
+
for (const warning of patch.warnings) {
|
|
354
|
+
responseLines.push(`Warning (${warning.path}): ${warning.message}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return responseLines.join("\n");
|
|
358
|
+
};
|
|
359
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bash Executor
|
|
3
|
+
*
|
|
4
|
+
* Built-in implementation for running shell commands using Node.js spawn.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import type { ToolContext } from "@clinebot/agents";
|
|
9
|
+
import type { BashExecutor } from "../types.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options for the bash executor
|
|
13
|
+
*/
|
|
14
|
+
export interface BashExecutorOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Shell to use for execution
|
|
17
|
+
* @default "/bin/bash" on Unix, "cmd.exe" on Windows
|
|
18
|
+
*/
|
|
19
|
+
shell?: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Timeout for command execution in milliseconds
|
|
23
|
+
* @default 30000 (30 seconds)
|
|
24
|
+
*/
|
|
25
|
+
timeoutMs?: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Maximum output size in bytes
|
|
29
|
+
* @default 1_000_000 (1MB)
|
|
30
|
+
*/
|
|
31
|
+
maxOutputBytes?: number;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Environment variables to add/override
|
|
35
|
+
*/
|
|
36
|
+
env?: Record<string, string>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether to combine stdout and stderr
|
|
40
|
+
* @default true
|
|
41
|
+
*/
|
|
42
|
+
combineOutput?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Create a bash executor using Node.js spawn
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const bash = createBashExecutor({
|
|
51
|
+
* timeoutMs: 60000, // 1 minute timeout
|
|
52
|
+
* shell: "/bin/zsh",
|
|
53
|
+
* })
|
|
54
|
+
*
|
|
55
|
+
* const output = await bash("ls -la", "/path/to/project", context)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function createBashExecutor(
|
|
59
|
+
options: BashExecutorOptions = {},
|
|
60
|
+
): BashExecutor {
|
|
61
|
+
const {
|
|
62
|
+
shell = process.platform === "win32" ? "cmd.exe" : "/bin/bash",
|
|
63
|
+
timeoutMs = 30000,
|
|
64
|
+
maxOutputBytes = 1_000_000,
|
|
65
|
+
env = {},
|
|
66
|
+
combineOutput = true,
|
|
67
|
+
} = options;
|
|
68
|
+
|
|
69
|
+
return async (
|
|
70
|
+
command: string,
|
|
71
|
+
cwd: string,
|
|
72
|
+
context: ToolContext,
|
|
73
|
+
): Promise<string> => {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const shellArgs =
|
|
76
|
+
process.platform === "win32" ? ["/c", command] : ["-c", command];
|
|
77
|
+
const isWindows = process.platform === "win32";
|
|
78
|
+
|
|
79
|
+
const child = spawn(shell, shellArgs, {
|
|
80
|
+
cwd,
|
|
81
|
+
env: { ...process.env, ...env },
|
|
82
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
83
|
+
// On Unix, place command in its own process group so abort can kill descendants too.
|
|
84
|
+
detached: !isWindows,
|
|
85
|
+
});
|
|
86
|
+
const childPid = child.pid;
|
|
87
|
+
|
|
88
|
+
let stdout = "";
|
|
89
|
+
let stderr = "";
|
|
90
|
+
let outputSize = 0;
|
|
91
|
+
let killed = false;
|
|
92
|
+
let settled = false;
|
|
93
|
+
|
|
94
|
+
const finalizeReject = (error: Error) => {
|
|
95
|
+
if (settled) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
settled = true;
|
|
99
|
+
reject(error);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const finalizeResolve = (output: string) => {
|
|
103
|
+
if (settled) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
settled = true;
|
|
107
|
+
resolve(output);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const killProcessTree = () => {
|
|
111
|
+
if (!childPid) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (isWindows) {
|
|
115
|
+
const killer = spawn(
|
|
116
|
+
"taskkill",
|
|
117
|
+
["/pid", String(childPid), "/T", "/F"],
|
|
118
|
+
{
|
|
119
|
+
stdio: "ignore",
|
|
120
|
+
windowsHide: true,
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
killer.unref();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
process.kill(-childPid, "SIGKILL");
|
|
128
|
+
} catch {
|
|
129
|
+
child.kill("SIGKILL");
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Handle timeout
|
|
134
|
+
const timeout = setTimeout(() => {
|
|
135
|
+
killed = true;
|
|
136
|
+
killProcessTree();
|
|
137
|
+
finalizeReject(new Error(`Command timed out after ${timeoutMs}ms`));
|
|
138
|
+
}, timeoutMs);
|
|
139
|
+
|
|
140
|
+
// Handle abort signal
|
|
141
|
+
const abortHandler = () => {
|
|
142
|
+
killed = true;
|
|
143
|
+
killProcessTree();
|
|
144
|
+
finalizeReject(new Error("Command was aborted"));
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
if (context.abortSignal) {
|
|
148
|
+
context.abortSignal.addEventListener("abort", abortHandler);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Collect stdout
|
|
152
|
+
child.stdout?.on("data", (data: Buffer) => {
|
|
153
|
+
outputSize += data.length;
|
|
154
|
+
if (outputSize <= maxOutputBytes) {
|
|
155
|
+
stdout += data.toString();
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Collect stderr
|
|
160
|
+
child.stderr?.on("data", (data: Buffer) => {
|
|
161
|
+
outputSize += data.length;
|
|
162
|
+
if (outputSize <= maxOutputBytes) {
|
|
163
|
+
stderr += data.toString();
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Handle completion
|
|
168
|
+
child.on("close", (code) => {
|
|
169
|
+
clearTimeout(timeout);
|
|
170
|
+
if (context.abortSignal) {
|
|
171
|
+
context.abortSignal.removeEventListener("abort", abortHandler);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (killed) return;
|
|
175
|
+
|
|
176
|
+
// Truncation warning
|
|
177
|
+
let output = combineOutput
|
|
178
|
+
? stdout + (stderr ? `\n[stderr]\n${stderr}` : "")
|
|
179
|
+
: stdout;
|
|
180
|
+
|
|
181
|
+
if (outputSize > maxOutputBytes) {
|
|
182
|
+
output += `\n\n[Output truncated: ${outputSize} bytes total, showing first ${maxOutputBytes} bytes]`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (code !== 0) {
|
|
186
|
+
const errorMsg = stderr || `Command exited with code ${code}`;
|
|
187
|
+
finalizeReject(new Error(errorMsg));
|
|
188
|
+
} else {
|
|
189
|
+
finalizeResolve(output);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Handle spawn errors
|
|
194
|
+
child.on("error", (error) => {
|
|
195
|
+
clearTimeout(timeout);
|
|
196
|
+
if (context.abortSignal) {
|
|
197
|
+
context.abortSignal.removeEventListener("abort", abortHandler);
|
|
198
|
+
}
|
|
199
|
+
finalizeReject(
|
|
200
|
+
new Error(`Failed to execute command: ${error.message}`),
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
}
|