@clinebot/core 0.0.22 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ClineCore.d.ts +110 -0
- package/dist/ClineCore.d.ts.map +1 -0
- package/dist/account/cline-account-service.d.ts +2 -1
- package/dist/account/cline-account-service.d.ts.map +1 -1
- package/dist/account/index.d.ts +1 -1
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/rpc.d.ts +3 -1
- package/dist/account/rpc.d.ts.map +1 -1
- package/dist/account/types.d.ts +3 -0
- package/dist/account/types.d.ts.map +1 -1
- package/dist/agents/plugin-loader.d.ts.map +1 -1
- package/dist/agents/plugin-sandbox-bootstrap.js +17 -17
- package/dist/auth/client.d.ts +1 -1
- package/dist/auth/client.d.ts.map +1 -1
- package/dist/auth/cline.d.ts +1 -1
- package/dist/auth/cline.d.ts.map +1 -1
- package/dist/auth/codex.d.ts +1 -1
- package/dist/auth/codex.d.ts.map +1 -1
- package/dist/auth/oca.d.ts +1 -1
- package/dist/auth/oca.d.ts.map +1 -1
- package/dist/auth/utils.d.ts +2 -2
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/index.d.ts +50 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +949 -0
- package/dist/providers/local-provider-service.d.ts +4 -4
- package/dist/providers/local-provider-service.d.ts.map +1 -1
- package/dist/runtime/runtime-builder.d.ts +1 -0
- package/dist/runtime/runtime-builder.d.ts.map +1 -1
- package/dist/runtime/session-runtime.d.ts +2 -1
- package/dist/runtime/session-runtime.d.ts.map +1 -1
- package/dist/runtime/team-runtime-registry.d.ts +13 -0
- package/dist/runtime/team-runtime-registry.d.ts.map +1 -0
- package/dist/session/default-session-manager.d.ts +2 -2
- package/dist/session/default-session-manager.d.ts.map +1 -1
- package/dist/session/rpc-runtime-ensure.d.ts +53 -0
- package/dist/session/rpc-runtime-ensure.d.ts.map +1 -0
- package/dist/session/session-config-builder.d.ts +2 -3
- package/dist/session/session-config-builder.d.ts.map +1 -1
- package/dist/session/session-host.d.ts +8 -18
- package/dist/session/session-host.d.ts.map +1 -1
- package/dist/session/session-manager.d.ts +1 -1
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manifest.d.ts +1 -2
- package/dist/session/session-manifest.d.ts.map +1 -1
- package/dist/session/unified-session-persistence-service.d.ts +2 -2
- package/dist/session/unified-session-persistence-service.d.ts.map +1 -1
- package/dist/session/utils/helpers.d.ts +1 -1
- package/dist/session/utils/helpers.d.ts.map +1 -1
- package/dist/session/utils/types.d.ts +1 -1
- package/dist/session/utils/types.d.ts.map +1 -1
- package/dist/storage/provider-settings-legacy-migration.d.ts.map +1 -1
- package/dist/telemetry/OpenTelemetryProvider.d.ts.map +1 -1
- package/dist/telemetry/distinct-id.d.ts +2 -0
- package/dist/telemetry/distinct-id.d.ts.map +1 -0
- package/dist/telemetry/{opentelemetry.d.ts → index.d.ts} +1 -1
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +28 -0
- package/dist/tools/constants.d.ts +1 -1
- package/dist/tools/constants.d.ts.map +1 -1
- package/dist/tools/definitions.d.ts +3 -3
- package/dist/tools/definitions.d.ts.map +1 -1
- package/dist/tools/executors/apply-patch.d.ts +1 -1
- package/dist/tools/executors/apply-patch.d.ts.map +1 -1
- package/dist/tools/executors/bash.d.ts +1 -1
- package/dist/tools/executors/bash.d.ts.map +1 -1
- package/dist/tools/executors/editor.d.ts +1 -1
- package/dist/tools/executors/editor.d.ts.map +1 -1
- package/dist/tools/executors/file-read.d.ts +1 -1
- package/dist/tools/executors/file-read.d.ts.map +1 -1
- package/dist/tools/executors/index.d.ts +14 -14
- package/dist/tools/executors/index.d.ts.map +1 -1
- package/dist/tools/executors/search.d.ts +1 -1
- package/dist/tools/executors/search.d.ts.map +1 -1
- package/dist/tools/executors/web-fetch.d.ts +1 -1
- package/dist/tools/executors/web-fetch.d.ts.map +1 -1
- package/dist/tools/helpers.d.ts +1 -1
- package/dist/tools/helpers.d.ts.map +1 -1
- package/dist/tools/index.d.ts +10 -10
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/model-tool-routing.d.ts +1 -1
- package/dist/tools/model-tool-routing.d.ts.map +1 -1
- package/dist/tools/presets.d.ts +1 -1
- package/dist/tools/presets.d.ts.map +1 -1
- package/dist/types/common.d.ts +17 -8
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/config.d.ts +4 -3
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/provider-settings.d.ts +1 -1
- package/dist/types/provider-settings.d.ts.map +1 -1
- package/dist/types.d.ts +5 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/package.json +44 -38
- package/src/ClineCore.ts +137 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +300 -0
- package/src/account/featurebase-token.test.ts +175 -0
- package/src/account/index.ts +23 -0
- package/src/account/rpc.test.ts +63 -0
- package/src/account/rpc.ts +185 -0
- package/src/account/types.ts +102 -0
- package/src/agents/agent-config-loader.test.ts +236 -0
- package/src/agents/agent-config-loader.ts +108 -0
- package/src/agents/agent-config-parser.ts +198 -0
- package/src/agents/hooks-config-loader.test.ts +20 -0
- package/src/agents/hooks-config-loader.ts +118 -0
- package/src/agents/index.ts +85 -0
- package/src/agents/plugin-config-loader.test.ts +140 -0
- package/src/agents/plugin-config-loader.ts +97 -0
- package/src/agents/plugin-loader.test.ts +210 -0
- package/src/agents/plugin-loader.ts +175 -0
- package/src/agents/plugin-sandbox-bootstrap.ts +448 -0
- package/src/agents/plugin-sandbox.test.ts +296 -0
- package/src/agents/plugin-sandbox.ts +341 -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 +420 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +491 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +573 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +81 -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/index.ts +479 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +127 -0
- package/src/input/file-indexer.ts +327 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +85 -0
- package/src/input/mention-enricher.ts +122 -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/providers/local-provider-registry.ts +232 -0
- package/src/providers/local-provider-service.test.ts +783 -0
- package/src/providers/local-provider-service.ts +471 -0
- package/src/runtime/commands.test.ts +98 -0
- package/src/runtime/commands.ts +83 -0
- package/src/runtime/hook-file-hooks.test.ts +237 -0
- package/src/runtime/hook-file-hooks.ts +859 -0
- package/src/runtime/index.ts +37 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +245 -0
- package/src/runtime/runtime-builder.test.ts +371 -0
- package/src/runtime/runtime-builder.ts +631 -0
- package/src/runtime/runtime-parity.test.ts +143 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +231 -0
- package/src/runtime/session-runtime.ts +49 -0
- package/src/runtime/skills.ts +44 -0
- package/src/runtime/team-runtime-registry.ts +46 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +45 -0
- package/src/session/default-session-manager.e2e.test.ts +384 -0
- package/src/session/default-session-manager.test.ts +1931 -0
- package/src/session/default-session-manager.ts +1422 -0
- package/src/session/file-session-service.ts +280 -0
- package/src/session/index.ts +45 -0
- package/src/session/rpc-runtime-ensure.ts +521 -0
- package/src/session/rpc-session-service.ts +107 -0
- package/src/session/rpc-spawn-lease.test.ts +49 -0
- package/src/session/rpc-spawn-lease.ts +122 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +272 -0
- package/src/session/session-agent-events.ts +248 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-config-builder.ts +113 -0
- package/src/session/session-graph.ts +92 -0
- package/src/session/session-host.test.ts +89 -0
- package/src/session/session-host.ts +205 -0
- package/src/session/session-manager.ts +69 -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 +673 -0
- package/src/session/session-team-coordination.ts +229 -0
- package/src/session/session-telemetry.ts +100 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.test.ts +85 -0
- package/src/session/unified-session-persistence-service.ts +994 -0
- package/src/session/utils/helpers.ts +139 -0
- package/src/session/utils/types.ts +57 -0
- package/src/session/utils/usage.ts +32 -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/file-team-store.ts +257 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +424 -0
- package/src/storage/provider-settings-legacy-migration.ts +826 -0
- package/src/storage/provider-settings-manager.test.ts +191 -0
- package/src/storage/provider-settings-manager.ts +152 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +275 -0
- package/src/storage/sqlite-team-store.ts +454 -0
- package/src/storage/team-store.ts +40 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/telemetry/ITelemetryAdapter.ts +94 -0
- package/src/telemetry/LoggerTelemetryAdapter.test.ts +42 -0
- package/src/telemetry/LoggerTelemetryAdapter.ts +114 -0
- package/src/telemetry/OpenTelemetryAdapter.test.ts +157 -0
- package/src/telemetry/OpenTelemetryAdapter.ts +348 -0
- package/src/telemetry/OpenTelemetryProvider.test.ts +113 -0
- package/src/telemetry/OpenTelemetryProvider.ts +325 -0
- package/src/telemetry/TelemetryService.test.ts +134 -0
- package/src/telemetry/TelemetryService.ts +141 -0
- package/src/telemetry/core-events.ts +400 -0
- package/src/telemetry/distinct-id.test.ts +57 -0
- package/src/telemetry/distinct-id.ts +58 -0
- package/src/telemetry/index.ts +20 -0
- package/src/tools/constants.ts +35 -0
- package/src/tools/definitions.test.ts +704 -0
- package/src/tools/definitions.ts +709 -0
- package/src/tools/executors/apply-patch-parser.ts +520 -0
- package/src/tools/executors/apply-patch.ts +359 -0
- package/src/tools/executors/bash.test.ts +87 -0
- package/src/tools/executors/bash.ts +207 -0
- package/src/tools/executors/editor.test.ts +35 -0
- package/src/tools/executors/editor.ts +219 -0
- package/src/tools/executors/file-read.test.ts +49 -0
- package/src/tools/executors/file-read.ts +110 -0
- package/src/tools/executors/index.ts +87 -0
- package/src/tools/executors/search.ts +278 -0
- package/src/tools/executors/web-fetch.ts +259 -0
- package/src/tools/helpers.ts +130 -0
- package/src/tools/index.ts +169 -0
- package/src/tools/model-tool-routing.test.ts +86 -0
- package/src/tools/model-tool-routing.ts +132 -0
- package/src/tools/presets.test.ts +62 -0
- package/src/tools/presets.ts +168 -0
- package/src/tools/schemas.ts +327 -0
- package/src/tools/types.ts +329 -0
- package/src/types/common.ts +26 -0
- package/src/types/config.ts +86 -0
- package/src/types/events.ts +74 -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 +132 -0
- package/src/version.ts +3 -0
- package/dist/index.node.d.ts +0 -47
- package/dist/index.node.d.ts.map +0 -1
- package/dist/index.node.js +0 -948
- package/dist/telemetry/opentelemetry.d.ts.map +0 -1
- package/dist/telemetry/opentelemetry.js +0 -27
|
@@ -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";
|
|
11
|
+
import type { ApplyPatchExecutor } from "../types";
|
|
12
|
+
import {
|
|
13
|
+
BASH_WRAPPERS,
|
|
14
|
+
DiffError,
|
|
15
|
+
PATCH_MARKERS,
|
|
16
|
+
PatchActionType,
|
|
17
|
+
type PatchChunk,
|
|
18
|
+
PatchParser,
|
|
19
|
+
} from "./apply-patch-parser";
|
|
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,87 @@
|
|
|
1
|
+
import type { ToolContext } from "@clinebot/shared";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { createBashExecutor } from "./bash";
|
|
4
|
+
|
|
5
|
+
const ctx: ToolContext = {
|
|
6
|
+
agentId: "agent-1",
|
|
7
|
+
conversationId: "conv-1",
|
|
8
|
+
iteration: 1,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
describe("createBashExecutor", () => {
|
|
12
|
+
it("runs a simple command and returns stdout", async () => {
|
|
13
|
+
const bash = createBashExecutor();
|
|
14
|
+
const output = await bash("echo hello", process.cwd(), ctx);
|
|
15
|
+
expect(output.trim()).toBe("hello");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("rejects on non-zero exit code", async () => {
|
|
19
|
+
const bash = createBashExecutor();
|
|
20
|
+
await expect(bash("exit 1", process.cwd(), ctx)).rejects.toThrow();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("includes stderr in combined output on success", async () => {
|
|
24
|
+
const bash = createBashExecutor({ combineOutput: true });
|
|
25
|
+
const cmd = `${process.execPath} -e "process.stdout.write('ok'); process.stderr.write('warn')"`;
|
|
26
|
+
const output = await bash(cmd, process.cwd(), ctx);
|
|
27
|
+
expect(output).toContain("ok");
|
|
28
|
+
expect(output).toContain("[stderr]");
|
|
29
|
+
expect(output).toContain("warn");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("excludes stderr when combineOutput is false", async () => {
|
|
33
|
+
const bash = createBashExecutor({ combineOutput: false });
|
|
34
|
+
const cmd = `${process.execPath} -e "process.stdout.write('ok'); process.stderr.write('warn')"`;
|
|
35
|
+
const output = await bash(cmd, process.cwd(), ctx);
|
|
36
|
+
expect(output.trim()).toBe("ok");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("rejects on timeout", async () => {
|
|
40
|
+
const bash = createBashExecutor({ timeoutMs: 50 });
|
|
41
|
+
await expect(bash("sleep 10", process.cwd(), ctx)).rejects.toThrow(
|
|
42
|
+
"timed out",
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("truncates output exceeding maxOutputBytes", async () => {
|
|
47
|
+
const bash = createBashExecutor({ maxOutputBytes: 10 });
|
|
48
|
+
const output = await bash(
|
|
49
|
+
`${process.execPath} -e "process.stdout.write('a'.repeat(100))"`,
|
|
50
|
+
process.cwd(),
|
|
51
|
+
ctx,
|
|
52
|
+
);
|
|
53
|
+
expect(output).toContain("[Output truncated:");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("rejects when abort signal fires", async () => {
|
|
57
|
+
const ac = new AbortController();
|
|
58
|
+
const abortCtx: ToolContext = { ...ctx, abortSignal: ac.signal };
|
|
59
|
+
const bash = createBashExecutor();
|
|
60
|
+
|
|
61
|
+
setTimeout(() => ac.abort(), 50);
|
|
62
|
+
await expect(bash("sleep 10", process.cwd(), abortCtx)).rejects.toThrow(
|
|
63
|
+
"aborted",
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe.runIf(process.platform === "win32")("createWindowsExecutor", () => {
|
|
69
|
+
it("runs structured commands without shell parsing", async () => {
|
|
70
|
+
const executor = createBashExecutor();
|
|
71
|
+
const output = await executor(
|
|
72
|
+
{
|
|
73
|
+
command: process.execPath,
|
|
74
|
+
args: ["-e", "process.stdout.write(process.argv[1])", "argv-ok"],
|
|
75
|
+
},
|
|
76
|
+
process.cwd(),
|
|
77
|
+
ctx,
|
|
78
|
+
);
|
|
79
|
+
expect(output).toBe("argv-ok");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("runs string commands through the shell", async () => {
|
|
83
|
+
const executor = createBashExecutor();
|
|
84
|
+
const output = await executor("echo shell-ok", process.cwd(), ctx);
|
|
85
|
+
expect(output.trim()).toBe("shell-ok");
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,207 @@
|
|
|
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 {
|
|
9
|
+
getDefaultShell,
|
|
10
|
+
getShellArgs,
|
|
11
|
+
type ToolContext,
|
|
12
|
+
} from "@clinebot/shared";
|
|
13
|
+
import type { BashExecutor } from "../types";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for the bash executor
|
|
17
|
+
*/
|
|
18
|
+
export interface BashExecutorOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Shell to use for execution
|
|
21
|
+
* @default "/bin/bash" on Unix, "powershell" on Windows
|
|
22
|
+
*/
|
|
23
|
+
shell?: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Timeout for command execution in milliseconds
|
|
27
|
+
* @default 30000 (30 seconds)
|
|
28
|
+
*/
|
|
29
|
+
timeoutMs?: number;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Maximum output size in bytes
|
|
33
|
+
* @default 1_000_000 (1MB)
|
|
34
|
+
*/
|
|
35
|
+
maxOutputBytes?: number;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Environment variables to add/override
|
|
39
|
+
*/
|
|
40
|
+
env?: Record<string, string>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Whether to combine stdout and stderr
|
|
44
|
+
* @default true
|
|
45
|
+
*/
|
|
46
|
+
combineOutput?: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface SpawnConfig {
|
|
50
|
+
executable: string;
|
|
51
|
+
args: string[];
|
|
52
|
+
cwd: string;
|
|
53
|
+
env: Record<string, string>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function spawnAndCollect(
|
|
57
|
+
config: SpawnConfig,
|
|
58
|
+
context: ToolContext,
|
|
59
|
+
timeoutMs: number,
|
|
60
|
+
maxOutputBytes: number,
|
|
61
|
+
combineOutput: boolean,
|
|
62
|
+
): Promise<string> {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const isWindows = process.platform === "win32";
|
|
65
|
+
|
|
66
|
+
const child = spawn(config.executable, config.args, {
|
|
67
|
+
cwd: config.cwd,
|
|
68
|
+
env: { ...process.env, ...config.env },
|
|
69
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
70
|
+
detached: !isWindows,
|
|
71
|
+
});
|
|
72
|
+
const childPid = child.pid;
|
|
73
|
+
|
|
74
|
+
let stdout = "";
|
|
75
|
+
let stderr = "";
|
|
76
|
+
let outputSize = 0;
|
|
77
|
+
let killed = false;
|
|
78
|
+
let settled = false;
|
|
79
|
+
|
|
80
|
+
const settle = (fn: () => void) => {
|
|
81
|
+
if (settled) return;
|
|
82
|
+
settled = true;
|
|
83
|
+
fn();
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const killProcessTree = () => {
|
|
87
|
+
if (!childPid) return;
|
|
88
|
+
if (isWindows) {
|
|
89
|
+
const killer = spawn(
|
|
90
|
+
"taskkill",
|
|
91
|
+
["/pid", String(childPid), "/T", "/F"],
|
|
92
|
+
{ stdio: "ignore", windowsHide: true },
|
|
93
|
+
);
|
|
94
|
+
killer.unref();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
process.kill(-childPid, "SIGKILL");
|
|
99
|
+
} catch {
|
|
100
|
+
child.kill("SIGKILL");
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const killAndReject = (error: Error) => {
|
|
105
|
+
killed = true;
|
|
106
|
+
killProcessTree();
|
|
107
|
+
settle(() => reject(error));
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const timeout = setTimeout(
|
|
111
|
+
() => killAndReject(new Error(`Command timed out after ${timeoutMs}ms`)),
|
|
112
|
+
timeoutMs,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const abortHandler = () => killAndReject(new Error("Command was aborted"));
|
|
116
|
+
|
|
117
|
+
if (context.abortSignal) {
|
|
118
|
+
context.abortSignal.addEventListener("abort", abortHandler);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const cleanup = () => {
|
|
122
|
+
clearTimeout(timeout);
|
|
123
|
+
context.abortSignal?.removeEventListener("abort", abortHandler);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
child.stdout?.on("data", (data: Buffer) => {
|
|
127
|
+
outputSize += data.length;
|
|
128
|
+
if (outputSize <= maxOutputBytes) stdout += data.toString();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
child.stderr?.on("data", (data: Buffer) => {
|
|
132
|
+
outputSize += data.length;
|
|
133
|
+
if (outputSize <= maxOutputBytes) stderr += data.toString();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
child.on("close", (code) => {
|
|
137
|
+
cleanup();
|
|
138
|
+
if (killed) return;
|
|
139
|
+
|
|
140
|
+
let output = combineOutput
|
|
141
|
+
? stdout + (stderr ? `\n[stderr]\n${stderr}` : "")
|
|
142
|
+
: stdout;
|
|
143
|
+
|
|
144
|
+
if (outputSize > maxOutputBytes) {
|
|
145
|
+
output += `\n\n[Output truncated: ${outputSize} bytes total, showing first ${maxOutputBytes} bytes]`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (code !== 0) {
|
|
149
|
+
settle(() =>
|
|
150
|
+
reject(new Error(stderr || `Command exited with code ${code}`)),
|
|
151
|
+
);
|
|
152
|
+
} else {
|
|
153
|
+
settle(() => resolve(output));
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
child.on("error", (error) => {
|
|
158
|
+
cleanup();
|
|
159
|
+
settle(() =>
|
|
160
|
+
reject(new Error(`Failed to execute command: ${error.message}`)),
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Create a bash executor using Node.js spawn
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const bash = createBashExecutor({
|
|
172
|
+
* timeoutMs: 60000, // 1 minute timeout
|
|
173
|
+
* shell: "/bin/zsh",
|
|
174
|
+
* })
|
|
175
|
+
*
|
|
176
|
+
* const output = await bash("ls -la", "/path/to/project", context)
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export function createBashExecutor(
|
|
180
|
+
options: BashExecutorOptions = {},
|
|
181
|
+
): BashExecutor {
|
|
182
|
+
const {
|
|
183
|
+
shell = getDefaultShell(process.platform),
|
|
184
|
+
timeoutMs = 30000,
|
|
185
|
+
maxOutputBytes = 1_000_000,
|
|
186
|
+
env = {},
|
|
187
|
+
combineOutput = true,
|
|
188
|
+
} = options;
|
|
189
|
+
|
|
190
|
+
return (command, cwd, context) => {
|
|
191
|
+
const isStructured = typeof command !== "string";
|
|
192
|
+
return spawnAndCollect(
|
|
193
|
+
{
|
|
194
|
+
executable: isStructured ? command.command : shell,
|
|
195
|
+
args: isStructured
|
|
196
|
+
? (command.args ?? [])
|
|
197
|
+
: getShellArgs(shell, command),
|
|
198
|
+
cwd,
|
|
199
|
+
env,
|
|
200
|
+
},
|
|
201
|
+
context,
|
|
202
|
+
timeoutMs,
|
|
203
|
+
maxOutputBytes,
|
|
204
|
+
combineOutput,
|
|
205
|
+
);
|
|
206
|
+
};
|
|
207
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { createEditorExecutor } from "./editor";
|
|
6
|
+
|
|
7
|
+
describe("createEditorExecutor", () => {
|
|
8
|
+
it("creates a missing file when edit is used", async () => {
|
|
9
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "agents-editor-"));
|
|
10
|
+
const filePath = path.join(dir, "example.txt");
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const editor = createEditorExecutor();
|
|
14
|
+
const result = await editor(
|
|
15
|
+
{
|
|
16
|
+
path: filePath,
|
|
17
|
+
new_text: "created with edit",
|
|
18
|
+
},
|
|
19
|
+
dir,
|
|
20
|
+
{
|
|
21
|
+
agentId: "agent-1",
|
|
22
|
+
conversationId: "conv-1",
|
|
23
|
+
iteration: 1,
|
|
24
|
+
},
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
expect(result).toBe(`File created successfully at: ${filePath}`);
|
|
28
|
+
await expect(fs.readFile(filePath, "utf-8")).resolves.toBe(
|
|
29
|
+
"created with edit",
|
|
30
|
+
);
|
|
31
|
+
} finally {
|
|
32
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|