@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,219 @@
|
|
|
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";
|
|
11
|
+
import type { EditorExecutor } from "../types";
|
|
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 fileExists(filePath: string): Promise<boolean> {
|
|
117
|
+
try {
|
|
118
|
+
await fs.access(filePath);
|
|
119
|
+
return true;
|
|
120
|
+
} catch {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function replaceInFile(
|
|
126
|
+
filePath: string,
|
|
127
|
+
oldStr: string,
|
|
128
|
+
newStr: string | null | undefined,
|
|
129
|
+
encoding: BufferEncoding,
|
|
130
|
+
maxDiffLines: number,
|
|
131
|
+
): Promise<string> {
|
|
132
|
+
const content = await fs.readFile(filePath, encoding);
|
|
133
|
+
const occurrences = countOccurrences(content, oldStr);
|
|
134
|
+
|
|
135
|
+
if (occurrences === 0) {
|
|
136
|
+
throw new Error(`No replacement performed: text not found in ${filePath}.`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (occurrences > 1) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`No replacement performed: multiple occurrences of text found in ${filePath}.`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const updated = content.replace(oldStr, newStr ?? "");
|
|
146
|
+
await fs.writeFile(filePath, updated, { encoding });
|
|
147
|
+
|
|
148
|
+
const diff = createLineDiff(content, updated, maxDiffLines);
|
|
149
|
+
return `Edited ${filePath}\n${diff}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function insertInFile(
|
|
153
|
+
filePath: string,
|
|
154
|
+
insertLineOneBased: number,
|
|
155
|
+
newStr: string,
|
|
156
|
+
encoding: BufferEncoding,
|
|
157
|
+
): Promise<string> {
|
|
158
|
+
const content = await fs.readFile(filePath, encoding);
|
|
159
|
+
const lines = content.split("\n");
|
|
160
|
+
const insertLine = insertLineOneBased - 1; // Convert to zero-based index
|
|
161
|
+
|
|
162
|
+
if (insertLine < 0 || insertLine > lines.length) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`Invalid line number: ${insertLineOneBased}. Valid range: 1-${lines.length}`,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
lines.splice(insertLine, 0, ...newStr.split("\n"));
|
|
169
|
+
await fs.writeFile(filePath, lines.join("\n"), { encoding });
|
|
170
|
+
|
|
171
|
+
return `Inserted content at line ${insertLineOneBased} in ${filePath}.`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Create an editor executor using Node.js fs module
|
|
176
|
+
*/
|
|
177
|
+
export function createEditorExecutor(
|
|
178
|
+
options: EditorExecutorOptions = {},
|
|
179
|
+
): EditorExecutor {
|
|
180
|
+
const {
|
|
181
|
+
encoding = "utf-8",
|
|
182
|
+
restrictToCwd = true,
|
|
183
|
+
maxDiffLines = 200,
|
|
184
|
+
} = options;
|
|
185
|
+
|
|
186
|
+
return async (
|
|
187
|
+
input: EditFileInput,
|
|
188
|
+
cwd: string,
|
|
189
|
+
_context: ToolContext,
|
|
190
|
+
): Promise<string> => {
|
|
191
|
+
const filePath = resolveFilePath(cwd, input.path, restrictToCwd);
|
|
192
|
+
|
|
193
|
+
if (input.insert_line != null) {
|
|
194
|
+
return insertInFile(
|
|
195
|
+
filePath,
|
|
196
|
+
input.insert_line, // One-based index
|
|
197
|
+
input.new_text,
|
|
198
|
+
encoding,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!(await fileExists(filePath))) {
|
|
203
|
+
return createFile(filePath, input.new_text, encoding);
|
|
204
|
+
}
|
|
205
|
+
if (input.old_text == null) {
|
|
206
|
+
throw new Error(
|
|
207
|
+
"Parameter `old_text` is required when editing an existing file without `insert_line`",
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return replaceInFile(
|
|
212
|
+
filePath,
|
|
213
|
+
input.old_text,
|
|
214
|
+
input.new_text,
|
|
215
|
+
encoding,
|
|
216
|
+
maxDiffLines,
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
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";
|
|
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(
|
|
16
|
+
{ path: filePath },
|
|
17
|
+
{
|
|
18
|
+
agentId: "agent-1",
|
|
19
|
+
conversationId: "conv-1",
|
|
20
|
+
iteration: 1,
|
|
21
|
+
},
|
|
22
|
+
);
|
|
23
|
+
expect(result).toBe("1 | hello absolute path");
|
|
24
|
+
} finally {
|
|
25
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("returns only the requested inclusive line range", async () => {
|
|
30
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "agents-file-read-"));
|
|
31
|
+
const filePath = path.join(dir, "example.txt");
|
|
32
|
+
await fs.writeFile(filePath, "alpha\nbeta\ngamma\ndelta", "utf-8");
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const readFile = createFileReadExecutor();
|
|
36
|
+
const result = await readFile(
|
|
37
|
+
{ path: filePath, start_line: 2, end_line: 3 },
|
|
38
|
+
{
|
|
39
|
+
agentId: "agent-1",
|
|
40
|
+
conversationId: "conv-1",
|
|
41
|
+
iteration: 1,
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
expect(result).toBe("2 | beta\n3 | gamma");
|
|
45
|
+
} finally {
|
|
46
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
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 { ReadFileRequest } from "../schemas";
|
|
11
|
+
import type { FileReadExecutor } from "../types";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Options for the file read executor
|
|
15
|
+
*/
|
|
16
|
+
export interface FileReadExecutorOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Maximum file size to read in bytes
|
|
19
|
+
* @default 10_000_000 (10MB)
|
|
20
|
+
*/
|
|
21
|
+
maxFileSizeBytes?: number;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* File encoding
|
|
25
|
+
* @default "utf-8"
|
|
26
|
+
*/
|
|
27
|
+
encoding?: BufferEncoding;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether to include line numbers in output
|
|
31
|
+
* @default false
|
|
32
|
+
*/
|
|
33
|
+
includeLineNumbers?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const DEFAULT_FILE_READ_OPTIONS: Required<FileReadExecutorOptions> = {
|
|
37
|
+
maxFileSizeBytes: 10_000_000, // 10MB default limit
|
|
38
|
+
encoding: "utf-8", // Default to UTF-8 encoding
|
|
39
|
+
includeLineNumbers: true, // Include line numbers by default
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create a file read executor using Node.js fs module
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const readFile = createFileReadExecutor({
|
|
48
|
+
* maxFileSizeBytes: 5_000_000, // 5MB limit
|
|
49
|
+
* includeLineNumbers: true,
|
|
50
|
+
* })
|
|
51
|
+
*
|
|
52
|
+
* const content = await readFile({ path: "/path/to/file.ts" }, context)
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export function createFileReadExecutor(
|
|
56
|
+
options: FileReadExecutorOptions = {},
|
|
57
|
+
): FileReadExecutor {
|
|
58
|
+
const { maxFileSizeBytes, encoding, includeLineNumbers } = {
|
|
59
|
+
...DEFAULT_FILE_READ_OPTIONS,
|
|
60
|
+
...options,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return async (
|
|
64
|
+
request: ReadFileRequest,
|
|
65
|
+
_context: ToolContext,
|
|
66
|
+
): Promise<string> => {
|
|
67
|
+
const { path: filePath, start_line, end_line } = request;
|
|
68
|
+
const resolvedPath = path.isAbsolute(filePath)
|
|
69
|
+
? path.normalize(filePath)
|
|
70
|
+
: path.resolve(process.cwd(), filePath);
|
|
71
|
+
|
|
72
|
+
// Check if file exists
|
|
73
|
+
const stat = await fs.stat(resolvedPath);
|
|
74
|
+
|
|
75
|
+
if (!stat.isFile()) {
|
|
76
|
+
throw new Error(`Path is not a file: ${resolvedPath}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Check file size
|
|
80
|
+
if (stat.size > maxFileSizeBytes) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`File too large: ${stat.size} bytes (max: ${maxFileSizeBytes} bytes). ` +
|
|
83
|
+
`Consider reading specific sections or using a different approach.`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Read file content
|
|
88
|
+
const content = await fs.readFile(resolvedPath, encoding);
|
|
89
|
+
const allLines = content.split("\n");
|
|
90
|
+
const rangeStart = Math.max((start_line ?? 1) - 1, 0);
|
|
91
|
+
const rangeEndExclusive = Math.min(
|
|
92
|
+
end_line ?? allLines.length,
|
|
93
|
+
allLines.length,
|
|
94
|
+
);
|
|
95
|
+
const lines = allLines.slice(rangeStart, rangeEndExclusive);
|
|
96
|
+
|
|
97
|
+
// Optionally add line numbers - one-based indexing for better readability
|
|
98
|
+
if (includeLineNumbers) {
|
|
99
|
+
const maxLineNumWidth = String(allLines.length).length;
|
|
100
|
+
return lines
|
|
101
|
+
.map(
|
|
102
|
+
(line, i) =>
|
|
103
|
+
`${String(rangeStart + i + 1).padStart(maxLineNumWidth, " ")} | ${line}`,
|
|
104
|
+
)
|
|
105
|
+
.join("\n");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return lines.join("\n");
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
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";
|
|
10
|
+
import {
|
|
11
|
+
type ApplyPatchExecutorOptions,
|
|
12
|
+
createApplyPatchExecutor,
|
|
13
|
+
} from "./apply-patch";
|
|
14
|
+
import { type BashExecutorOptions, createBashExecutor } from "./bash";
|
|
15
|
+
import { createEditorExecutor, type EditorExecutorOptions } from "./editor";
|
|
16
|
+
import {
|
|
17
|
+
createFileReadExecutor,
|
|
18
|
+
type FileReadExecutorOptions,
|
|
19
|
+
} from "./file-read";
|
|
20
|
+
import { createSearchExecutor, type SearchExecutorOptions } from "./search";
|
|
21
|
+
import {
|
|
22
|
+
createWebFetchExecutor,
|
|
23
|
+
type WebFetchExecutorOptions,
|
|
24
|
+
} from "./web-fetch";
|
|
25
|
+
|
|
26
|
+
// Re-export individual executors and their options types
|
|
27
|
+
export {
|
|
28
|
+
type ApplyPatchExecutorOptions,
|
|
29
|
+
createApplyPatchExecutor,
|
|
30
|
+
} from "./apply-patch";
|
|
31
|
+
export {
|
|
32
|
+
type BashExecutorOptions,
|
|
33
|
+
createBashExecutor,
|
|
34
|
+
} from "./bash";
|
|
35
|
+
export { createEditorExecutor, type EditorExecutorOptions } from "./editor";
|
|
36
|
+
export {
|
|
37
|
+
createFileReadExecutor,
|
|
38
|
+
type FileReadExecutorOptions,
|
|
39
|
+
} from "./file-read";
|
|
40
|
+
export { createSearchExecutor, type SearchExecutorOptions } from "./search";
|
|
41
|
+
export {
|
|
42
|
+
createWebFetchExecutor,
|
|
43
|
+
type WebFetchExecutorOptions,
|
|
44
|
+
} from "./web-fetch";
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Options for creating default executors
|
|
48
|
+
*/
|
|
49
|
+
export interface DefaultExecutorsOptions {
|
|
50
|
+
fileRead?: FileReadExecutorOptions;
|
|
51
|
+
search?: SearchExecutorOptions;
|
|
52
|
+
bash?: BashExecutorOptions;
|
|
53
|
+
webFetch?: WebFetchExecutorOptions;
|
|
54
|
+
applyPatch?: ApplyPatchExecutorOptions;
|
|
55
|
+
editor?: EditorExecutorOptions;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create all default executors with optional configuration
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* import { createDefaultTools, createDefaultExecutors } from "@clinebot/core"
|
|
64
|
+
*
|
|
65
|
+
* const executors = createDefaultExecutors({
|
|
66
|
+
* bash: { timeoutMs: 60000 },
|
|
67
|
+
* search: { maxResults: 50 },
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* const tools = createDefaultTools({
|
|
71
|
+
* executors,
|
|
72
|
+
* cwd: "/path/to/project",
|
|
73
|
+
* })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function createDefaultExecutors(
|
|
77
|
+
options: DefaultExecutorsOptions = {},
|
|
78
|
+
): ToolExecutors {
|
|
79
|
+
return {
|
|
80
|
+
readFile: createFileReadExecutor(options.fileRead),
|
|
81
|
+
search: createSearchExecutor(options.search),
|
|
82
|
+
bash: createBashExecutor(options.bash),
|
|
83
|
+
webFetch: createWebFetchExecutor(options.webFetch),
|
|
84
|
+
applyPatch: createApplyPatchExecutor(options.applyPatch),
|
|
85
|
+
editor: createEditorExecutor(options.editor),
|
|
86
|
+
};
|
|
87
|
+
}
|