@workser/engine 0.1.0 → 0.1.3
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/src/place/e2b.d.ts
CHANGED
|
@@ -39,6 +39,15 @@ export interface E2BSandboxLike {
|
|
|
39
39
|
* a Place — the "loop in our service" mode. The engine runs in our worker; the
|
|
40
40
|
* files and commands are on the machine; the model key never goes there.
|
|
41
41
|
*/
|
|
42
|
+
/**
|
|
43
|
+
* NODE ON PATH, WHERE IT CAME FROM NVM. A project computer's image installs
|
|
44
|
+
* Node with nvm for `user`, which only an interactive shell loads: a job's
|
|
45
|
+
* `npm test`, its checks and the agent's own `node` saw none. Loaded only
|
|
46
|
+
* when `node` is missing, so a machine that has it (the workbench template, a
|
|
47
|
+
* rebuilt image) pays nothing. On its own line, so the command is unchanged.
|
|
48
|
+
*/
|
|
49
|
+
export declare const NODE_ON_PATH = "command -v node >/dev/null 2>&1 || { export NVM_DIR=\"${NVM_DIR:-$HOME/.nvm}\"; [ -s \"$NVM_DIR/nvm.sh\" ] && . \"$NVM_DIR/nvm.sh\" >/dev/null 2>&1; }";
|
|
50
|
+
export declare function withNodeOnPath(command: string): string;
|
|
42
51
|
export declare class E2BPlace implements Place {
|
|
43
52
|
private readonly sandbox;
|
|
44
53
|
readonly root: string;
|
package/dist/src/place/e2b.js
CHANGED
|
@@ -4,6 +4,17 @@ import { parseGitStatus } from './local.js';
|
|
|
4
4
|
* a Place — the "loop in our service" mode. The engine runs in our worker; the
|
|
5
5
|
* files and commands are on the machine; the model key never goes there.
|
|
6
6
|
*/
|
|
7
|
+
/**
|
|
8
|
+
* NODE ON PATH, WHERE IT CAME FROM NVM. A project computer's image installs
|
|
9
|
+
* Node with nvm for `user`, which only an interactive shell loads: a job's
|
|
10
|
+
* `npm test`, its checks and the agent's own `node` saw none. Loaded only
|
|
11
|
+
* when `node` is missing, so a machine that has it (the workbench template, a
|
|
12
|
+
* rebuilt image) pays nothing. On its own line, so the command is unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export const NODE_ON_PATH = 'command -v node >/dev/null 2>&1 || { export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" >/dev/null 2>&1; }';
|
|
15
|
+
export function withNodeOnPath(command) {
|
|
16
|
+
return `${NODE_ON_PATH}\n${command}`;
|
|
17
|
+
}
|
|
7
18
|
export class E2BPlace {
|
|
8
19
|
sandbox;
|
|
9
20
|
root;
|
|
@@ -28,7 +39,7 @@ export class E2BPlace {
|
|
|
28
39
|
async exec(command, options = {}) {
|
|
29
40
|
const started = Date.now();
|
|
30
41
|
try {
|
|
31
|
-
const result = await this.sandbox.commands.run(command, {
|
|
42
|
+
const result = await this.sandbox.commands.run(withNodeOnPath(command), {
|
|
32
43
|
cwd: options.cwd ? this.inside(options.cwd) : this.root,
|
|
33
44
|
...(options.env ? { envs: options.env } : {}),
|
|
34
45
|
...(options.timeoutMs ? { timeoutMs: options.timeoutMs } : {}),
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type Replacer = (content: string, find: string) => Generator<string, void, unknown>;
|
|
2
|
+
export declare const SimpleReplacer: Replacer;
|
|
3
|
+
export declare const LineTrimmedReplacer: Replacer;
|
|
4
|
+
export declare const BlockAnchorReplacer: Replacer;
|
|
5
|
+
export declare const WhitespaceNormalizedReplacer: Replacer;
|
|
6
|
+
export declare const IndentationFlexibleReplacer: Replacer;
|
|
7
|
+
export declare const EscapeNormalizedReplacer: Replacer;
|
|
8
|
+
export declare const MultiOccurrenceReplacer: Replacer;
|
|
9
|
+
export declare const TrimmedBoundaryReplacer: Replacer;
|
|
10
|
+
export declare const ContextAwareReplacer: Replacer;
|
|
11
|
+
export declare function trimDiff(diff: string): string;
|
|
12
|
+
export declare function replace(content: string, oldString: string, newString: string, replaceAll?: boolean): string;
|
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
// VENDORED VERBATIM from OpenCode v1.18.23 packages/opencode/src/tool/edit.ts (MIT, see ./LICENSE).
|
|
2
|
+
// Lines 22-33 (line-ending helpers) and 217-737 (replacers + replace + trimDiff). Do not edit: update by re-vendoring.
|
|
3
|
+
function normalizeLineEndings(text) {
|
|
4
|
+
return text.replaceAll("\r\n", "\n");
|
|
5
|
+
}
|
|
6
|
+
function detectLineEnding(text) {
|
|
7
|
+
return text.includes("\r\n") ? "\r\n" : "\n";
|
|
8
|
+
}
|
|
9
|
+
function convertToLineEnding(text, ending) {
|
|
10
|
+
if (ending === "\n")
|
|
11
|
+
return text;
|
|
12
|
+
return text.replaceAll("\n", "\r\n");
|
|
13
|
+
}
|
|
14
|
+
// Similarity thresholds for block anchor fallback matching
|
|
15
|
+
const SINGLE_CANDIDATE_SIMILARITY_THRESHOLD = 0.65;
|
|
16
|
+
const MULTIPLE_CANDIDATES_SIMILARITY_THRESHOLD = 0.65;
|
|
17
|
+
/**
|
|
18
|
+
* Levenshtein distance algorithm implementation
|
|
19
|
+
*/
|
|
20
|
+
function levenshtein(a, b) {
|
|
21
|
+
// Handle empty strings
|
|
22
|
+
if (a === "" || b === "") {
|
|
23
|
+
return Math.max(a.length, b.length);
|
|
24
|
+
}
|
|
25
|
+
const matrix = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
26
|
+
for (let i = 1; i <= a.length; i++) {
|
|
27
|
+
for (let j = 1; j <= b.length; j++) {
|
|
28
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
29
|
+
matrix[i][j] = Math.min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j - 1] + cost);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return matrix[a.length][b.length];
|
|
33
|
+
}
|
|
34
|
+
export const SimpleReplacer = function* (_content, find) {
|
|
35
|
+
yield find;
|
|
36
|
+
};
|
|
37
|
+
export const LineTrimmedReplacer = function* (content, find) {
|
|
38
|
+
const originalLines = content.split("\n");
|
|
39
|
+
const searchLines = find.split("\n");
|
|
40
|
+
if (searchLines[searchLines.length - 1] === "") {
|
|
41
|
+
searchLines.pop();
|
|
42
|
+
}
|
|
43
|
+
for (let i = 0; i <= originalLines.length - searchLines.length; i++) {
|
|
44
|
+
let matches = true;
|
|
45
|
+
for (let j = 0; j < searchLines.length; j++) {
|
|
46
|
+
const originalTrimmed = originalLines[i + j].trim();
|
|
47
|
+
const searchTrimmed = searchLines[j].trim();
|
|
48
|
+
if (originalTrimmed !== searchTrimmed) {
|
|
49
|
+
matches = false;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (matches) {
|
|
54
|
+
let matchStartIndex = 0;
|
|
55
|
+
for (let k = 0; k < i; k++) {
|
|
56
|
+
matchStartIndex += originalLines[k].length + 1;
|
|
57
|
+
}
|
|
58
|
+
let matchEndIndex = matchStartIndex;
|
|
59
|
+
for (let k = 0; k < searchLines.length; k++) {
|
|
60
|
+
matchEndIndex += originalLines[i + k].length;
|
|
61
|
+
if (k < searchLines.length - 1) {
|
|
62
|
+
matchEndIndex += 1; // Add newline character except for the last line
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
yield content.substring(matchStartIndex, matchEndIndex);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
export const BlockAnchorReplacer = function* (content, find) {
|
|
70
|
+
const originalLines = content.split("\n");
|
|
71
|
+
const searchLines = find.split("\n");
|
|
72
|
+
if (searchLines.length < 3) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (searchLines[searchLines.length - 1] === "") {
|
|
76
|
+
searchLines.pop();
|
|
77
|
+
}
|
|
78
|
+
const firstLineSearch = searchLines[0].trim();
|
|
79
|
+
const lastLineSearch = searchLines[searchLines.length - 1].trim();
|
|
80
|
+
const searchBlockSize = searchLines.length;
|
|
81
|
+
const maxLineDelta = Math.max(1, Math.floor(searchBlockSize * 0.25));
|
|
82
|
+
// Collect all candidate positions where both anchors match
|
|
83
|
+
const candidates = [];
|
|
84
|
+
for (let i = 0; i < originalLines.length; i++) {
|
|
85
|
+
if (originalLines[i].trim() !== firstLineSearch) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
// Look for the matching last line after this first line
|
|
89
|
+
for (let j = i + 2; j < originalLines.length; j++) {
|
|
90
|
+
if (originalLines[j].trim() === lastLineSearch) {
|
|
91
|
+
const actualBlockSize = j - i + 1;
|
|
92
|
+
if (Math.abs(actualBlockSize - searchBlockSize) <= maxLineDelta) {
|
|
93
|
+
candidates.push({ startLine: i, endLine: j });
|
|
94
|
+
}
|
|
95
|
+
break; // Only match the first occurrence of the last line
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// Return immediately if no candidates
|
|
100
|
+
if (candidates.length === 0) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
// Handle single candidate scenario (using relaxed threshold)
|
|
104
|
+
if (candidates.length === 1) {
|
|
105
|
+
const { startLine, endLine } = candidates[0];
|
|
106
|
+
const actualBlockSize = endLine - startLine + 1;
|
|
107
|
+
let similarity = 0;
|
|
108
|
+
const linesToCheck = Math.min(searchBlockSize - 2, actualBlockSize - 2); // Middle lines only
|
|
109
|
+
if (linesToCheck > 0) {
|
|
110
|
+
for (let j = 1; j < searchBlockSize - 1 && j < actualBlockSize - 1; j++) {
|
|
111
|
+
const originalLine = originalLines[startLine + j].trim();
|
|
112
|
+
const searchLine = searchLines[j].trim();
|
|
113
|
+
const maxLen = Math.max(originalLine.length, searchLine.length);
|
|
114
|
+
if (maxLen === 0) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const distance = levenshtein(originalLine, searchLine);
|
|
118
|
+
similarity += (1 - distance / maxLen) / linesToCheck;
|
|
119
|
+
// Exit early when threshold is reached
|
|
120
|
+
if (similarity >= SINGLE_CANDIDATE_SIMILARITY_THRESHOLD) {
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
// No middle lines to compare, just accept based on anchors
|
|
127
|
+
similarity = 1.0;
|
|
128
|
+
}
|
|
129
|
+
if (similarity >= SINGLE_CANDIDATE_SIMILARITY_THRESHOLD) {
|
|
130
|
+
let matchStartIndex = 0;
|
|
131
|
+
for (let k = 0; k < startLine; k++) {
|
|
132
|
+
matchStartIndex += originalLines[k].length + 1;
|
|
133
|
+
}
|
|
134
|
+
let matchEndIndex = matchStartIndex;
|
|
135
|
+
for (let k = startLine; k <= endLine; k++) {
|
|
136
|
+
matchEndIndex += originalLines[k].length;
|
|
137
|
+
if (k < endLine) {
|
|
138
|
+
matchEndIndex += 1; // Add newline character except for the last line
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
yield content.substring(matchStartIndex, matchEndIndex);
|
|
142
|
+
}
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// Calculate similarity for multiple candidates
|
|
146
|
+
let bestMatch = null;
|
|
147
|
+
let maxSimilarity = -1;
|
|
148
|
+
for (const candidate of candidates) {
|
|
149
|
+
const { startLine, endLine } = candidate;
|
|
150
|
+
const actualBlockSize = endLine - startLine + 1;
|
|
151
|
+
let similarity = 0;
|
|
152
|
+
const linesToCheck = Math.min(searchBlockSize - 2, actualBlockSize - 2); // Middle lines only
|
|
153
|
+
if (linesToCheck > 0) {
|
|
154
|
+
for (let j = 1; j < searchBlockSize - 1 && j < actualBlockSize - 1; j++) {
|
|
155
|
+
const originalLine = originalLines[startLine + j].trim();
|
|
156
|
+
const searchLine = searchLines[j].trim();
|
|
157
|
+
const maxLen = Math.max(originalLine.length, searchLine.length);
|
|
158
|
+
if (maxLen === 0) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const distance = levenshtein(originalLine, searchLine);
|
|
162
|
+
similarity += 1 - distance / maxLen;
|
|
163
|
+
}
|
|
164
|
+
similarity /= linesToCheck; // Average similarity
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
// No middle lines to compare, just accept based on anchors
|
|
168
|
+
similarity = 1.0;
|
|
169
|
+
}
|
|
170
|
+
if (similarity > maxSimilarity) {
|
|
171
|
+
maxSimilarity = similarity;
|
|
172
|
+
bestMatch = candidate;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Threshold judgment
|
|
176
|
+
if (maxSimilarity >= MULTIPLE_CANDIDATES_SIMILARITY_THRESHOLD && bestMatch) {
|
|
177
|
+
const { startLine, endLine } = bestMatch;
|
|
178
|
+
let matchStartIndex = 0;
|
|
179
|
+
for (let k = 0; k < startLine; k++) {
|
|
180
|
+
matchStartIndex += originalLines[k].length + 1;
|
|
181
|
+
}
|
|
182
|
+
let matchEndIndex = matchStartIndex;
|
|
183
|
+
for (let k = startLine; k <= endLine; k++) {
|
|
184
|
+
matchEndIndex += originalLines[k].length;
|
|
185
|
+
if (k < endLine) {
|
|
186
|
+
matchEndIndex += 1;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
yield content.substring(matchStartIndex, matchEndIndex);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
export const WhitespaceNormalizedReplacer = function* (content, find) {
|
|
193
|
+
const normalizeWhitespace = (text) => text.replace(/\s+/g, " ").trim();
|
|
194
|
+
const normalizedFind = normalizeWhitespace(find);
|
|
195
|
+
// Handle single line matches
|
|
196
|
+
const lines = content.split("\n");
|
|
197
|
+
for (let i = 0; i < lines.length; i++) {
|
|
198
|
+
const line = lines[i];
|
|
199
|
+
if (normalizeWhitespace(line) === normalizedFind) {
|
|
200
|
+
yield line;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
// Only check for substring matches if the full line doesn't match
|
|
204
|
+
const normalizedLine = normalizeWhitespace(line);
|
|
205
|
+
if (normalizedLine.includes(normalizedFind)) {
|
|
206
|
+
// Find the actual substring in the original line that matches
|
|
207
|
+
const words = find.trim().split(/\s+/);
|
|
208
|
+
if (words.length > 0) {
|
|
209
|
+
const pattern = words.map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("\\s+");
|
|
210
|
+
try {
|
|
211
|
+
const regex = new RegExp(pattern);
|
|
212
|
+
const match = line.match(regex);
|
|
213
|
+
if (match) {
|
|
214
|
+
yield match[0];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// Invalid regex pattern, skip
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// Handle multi-line matches
|
|
225
|
+
const findLines = find.split("\n");
|
|
226
|
+
if (findLines.length > 1) {
|
|
227
|
+
for (let i = 0; i <= lines.length - findLines.length; i++) {
|
|
228
|
+
const block = lines.slice(i, i + findLines.length);
|
|
229
|
+
if (normalizeWhitespace(block.join("\n")) === normalizedFind) {
|
|
230
|
+
yield block.join("\n");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
export const IndentationFlexibleReplacer = function* (content, find) {
|
|
236
|
+
const removeIndentation = (text) => {
|
|
237
|
+
const lines = text.split("\n");
|
|
238
|
+
const nonEmptyLines = lines.filter((line) => line.trim().length > 0);
|
|
239
|
+
if (nonEmptyLines.length === 0)
|
|
240
|
+
return text;
|
|
241
|
+
const minIndent = Math.min(...nonEmptyLines.map((line) => {
|
|
242
|
+
const match = line.match(/^(\s*)/);
|
|
243
|
+
return match ? match[1].length : 0;
|
|
244
|
+
}));
|
|
245
|
+
return lines.map((line) => (line.trim().length === 0 ? line : line.slice(minIndent))).join("\n");
|
|
246
|
+
};
|
|
247
|
+
const normalizedFind = removeIndentation(find);
|
|
248
|
+
const contentLines = content.split("\n");
|
|
249
|
+
const findLines = find.split("\n");
|
|
250
|
+
for (let i = 0; i <= contentLines.length - findLines.length; i++) {
|
|
251
|
+
const block = contentLines.slice(i, i + findLines.length).join("\n");
|
|
252
|
+
if (removeIndentation(block) === normalizedFind) {
|
|
253
|
+
yield block;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
export const EscapeNormalizedReplacer = function* (content, find) {
|
|
258
|
+
const unescapeString = (str) => {
|
|
259
|
+
return str.replace(/\\(n|t|r|'|"|`|\\|\n|\$)/g, (match, capturedChar) => {
|
|
260
|
+
switch (capturedChar) {
|
|
261
|
+
case "n":
|
|
262
|
+
return "\n";
|
|
263
|
+
case "t":
|
|
264
|
+
return "\t";
|
|
265
|
+
case "r":
|
|
266
|
+
return "\r";
|
|
267
|
+
case "'":
|
|
268
|
+
return "'";
|
|
269
|
+
case '"':
|
|
270
|
+
return '"';
|
|
271
|
+
case "`":
|
|
272
|
+
return "`";
|
|
273
|
+
case "\\":
|
|
274
|
+
return "\\";
|
|
275
|
+
case "\n":
|
|
276
|
+
return "\n";
|
|
277
|
+
case "$":
|
|
278
|
+
return "$";
|
|
279
|
+
default:
|
|
280
|
+
return match;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
const unescapedFind = unescapeString(find);
|
|
285
|
+
// Try direct match with unescaped find string
|
|
286
|
+
if (content.includes(unescapedFind)) {
|
|
287
|
+
yield unescapedFind;
|
|
288
|
+
}
|
|
289
|
+
// Also try finding escaped versions in content that match unescaped find
|
|
290
|
+
const lines = content.split("\n");
|
|
291
|
+
const findLines = unescapedFind.split("\n");
|
|
292
|
+
for (let i = 0; i <= lines.length - findLines.length; i++) {
|
|
293
|
+
const block = lines.slice(i, i + findLines.length).join("\n");
|
|
294
|
+
const unescapedBlock = unescapeString(block);
|
|
295
|
+
if (unescapedBlock === unescapedFind) {
|
|
296
|
+
yield block;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
export const MultiOccurrenceReplacer = function* (content, find) {
|
|
301
|
+
// This replacer yields all exact matches, allowing the replace function
|
|
302
|
+
// to handle multiple occurrences based on replaceAll parameter
|
|
303
|
+
let startIndex = 0;
|
|
304
|
+
while (true) {
|
|
305
|
+
const index = content.indexOf(find, startIndex);
|
|
306
|
+
if (index === -1)
|
|
307
|
+
break;
|
|
308
|
+
yield find;
|
|
309
|
+
startIndex = index + find.length;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
export const TrimmedBoundaryReplacer = function* (content, find) {
|
|
313
|
+
const trimmedFind = find.trim();
|
|
314
|
+
if (trimmedFind === find) {
|
|
315
|
+
// Already trimmed, no point in trying
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
// Try to find the trimmed version
|
|
319
|
+
if (content.includes(trimmedFind)) {
|
|
320
|
+
yield trimmedFind;
|
|
321
|
+
}
|
|
322
|
+
// Also try finding blocks where trimmed content matches
|
|
323
|
+
const lines = content.split("\n");
|
|
324
|
+
const findLines = find.split("\n");
|
|
325
|
+
for (let i = 0; i <= lines.length - findLines.length; i++) {
|
|
326
|
+
const block = lines.slice(i, i + findLines.length).join("\n");
|
|
327
|
+
if (block.trim() === trimmedFind) {
|
|
328
|
+
yield block;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
export const ContextAwareReplacer = function* (content, find) {
|
|
333
|
+
const findLines = find.split("\n");
|
|
334
|
+
if (findLines.length < 3) {
|
|
335
|
+
// Need at least 3 lines to have meaningful context
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
// Remove trailing empty line if present
|
|
339
|
+
if (findLines[findLines.length - 1] === "") {
|
|
340
|
+
findLines.pop();
|
|
341
|
+
}
|
|
342
|
+
const contentLines = content.split("\n");
|
|
343
|
+
// Extract first and last lines as context anchors
|
|
344
|
+
const firstLine = findLines[0].trim();
|
|
345
|
+
const lastLine = findLines[findLines.length - 1].trim();
|
|
346
|
+
// Find blocks that start and end with the context anchors
|
|
347
|
+
for (let i = 0; i < contentLines.length; i++) {
|
|
348
|
+
if (contentLines[i].trim() !== firstLine)
|
|
349
|
+
continue;
|
|
350
|
+
// Look for the matching last line
|
|
351
|
+
for (let j = i + 2; j < contentLines.length; j++) {
|
|
352
|
+
if (contentLines[j].trim() === lastLine) {
|
|
353
|
+
// Found a potential context block
|
|
354
|
+
const blockLines = contentLines.slice(i, j + 1);
|
|
355
|
+
const block = blockLines.join("\n");
|
|
356
|
+
// Check if the middle content has reasonable similarity
|
|
357
|
+
// (simple heuristic: at least 50% of non-empty lines should match when trimmed)
|
|
358
|
+
if (blockLines.length === findLines.length) {
|
|
359
|
+
let matchingLines = 0;
|
|
360
|
+
let totalNonEmptyLines = 0;
|
|
361
|
+
for (let k = 1; k < blockLines.length - 1; k++) {
|
|
362
|
+
const blockLine = blockLines[k].trim();
|
|
363
|
+
const findLine = findLines[k].trim();
|
|
364
|
+
if (blockLine.length > 0 || findLine.length > 0) {
|
|
365
|
+
totalNonEmptyLines++;
|
|
366
|
+
if (blockLine === findLine) {
|
|
367
|
+
matchingLines++;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (totalNonEmptyLines === 0 || matchingLines / totalNonEmptyLines >= 0.5) {
|
|
372
|
+
yield block;
|
|
373
|
+
break; // Only match the first occurrence
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
export function trimDiff(diff) {
|
|
382
|
+
const lines = diff.split("\n");
|
|
383
|
+
const contentLines = lines.filter((line) => (line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) &&
|
|
384
|
+
!line.startsWith("---") &&
|
|
385
|
+
!line.startsWith("+++"));
|
|
386
|
+
if (contentLines.length === 0)
|
|
387
|
+
return diff;
|
|
388
|
+
let min = Infinity;
|
|
389
|
+
for (const line of contentLines) {
|
|
390
|
+
const content = line.slice(1);
|
|
391
|
+
if (content.trim().length > 0) {
|
|
392
|
+
const match = content.match(/^(\s*)/);
|
|
393
|
+
if (match)
|
|
394
|
+
min = Math.min(min, match[1].length);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (min === Infinity || min === 0)
|
|
398
|
+
return diff;
|
|
399
|
+
const trimmedLines = lines.map((line) => {
|
|
400
|
+
if ((line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) &&
|
|
401
|
+
!line.startsWith("---") &&
|
|
402
|
+
!line.startsWith("+++")) {
|
|
403
|
+
const prefix = line[0];
|
|
404
|
+
const content = line.slice(1);
|
|
405
|
+
return prefix + content.slice(min);
|
|
406
|
+
}
|
|
407
|
+
return line;
|
|
408
|
+
});
|
|
409
|
+
return trimmedLines.join("\n");
|
|
410
|
+
}
|
|
411
|
+
export function replace(content, oldString, newString, replaceAll = false) {
|
|
412
|
+
if (oldString === newString) {
|
|
413
|
+
throw new Error("No changes to apply: oldString and newString are identical.");
|
|
414
|
+
}
|
|
415
|
+
if (oldString === "") {
|
|
416
|
+
throw new Error("oldString cannot be empty when editing an existing file. Provide the exact text to replace, or use write for an intentional full-file replacement.");
|
|
417
|
+
}
|
|
418
|
+
let notFound = true;
|
|
419
|
+
for (const replacer of [
|
|
420
|
+
SimpleReplacer,
|
|
421
|
+
LineTrimmedReplacer,
|
|
422
|
+
BlockAnchorReplacer,
|
|
423
|
+
WhitespaceNormalizedReplacer,
|
|
424
|
+
IndentationFlexibleReplacer,
|
|
425
|
+
EscapeNormalizedReplacer,
|
|
426
|
+
TrimmedBoundaryReplacer,
|
|
427
|
+
ContextAwareReplacer,
|
|
428
|
+
MultiOccurrenceReplacer,
|
|
429
|
+
]) {
|
|
430
|
+
for (const search of replacer(content, oldString)) {
|
|
431
|
+
const index = content.indexOf(search);
|
|
432
|
+
if (index === -1)
|
|
433
|
+
continue;
|
|
434
|
+
notFound = false;
|
|
435
|
+
if (isDisproportionateMatch(search, oldString)) {
|
|
436
|
+
throw new Error("Refusing replacement because the matched span is much larger than oldString. Re-read the file and provide the full exact oldString for the intended replacement.");
|
|
437
|
+
}
|
|
438
|
+
if (replaceAll) {
|
|
439
|
+
return content.replaceAll(search, newString);
|
|
440
|
+
}
|
|
441
|
+
const lastIndex = content.lastIndexOf(search);
|
|
442
|
+
if (index !== lastIndex)
|
|
443
|
+
continue;
|
|
444
|
+
return content.substring(0, index) + newString + content.substring(index + search.length);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
if (notFound) {
|
|
448
|
+
throw new Error("Could not find oldString in the file. It must match exactly, including whitespace, indentation, and line endings.");
|
|
449
|
+
}
|
|
450
|
+
throw new Error("Found multiple matches for oldString. Provide more surrounding context to make the match unique.");
|
|
451
|
+
}
|
|
452
|
+
function isDisproportionateMatch(search, oldString) {
|
|
453
|
+
const oldLines = oldString.split("\n").length;
|
|
454
|
+
const searchLines = search.split("\n").length;
|
|
455
|
+
if (searchLines >= Math.max(oldLines + 3, oldLines * 2))
|
|
456
|
+
return true;
|
|
457
|
+
if (oldLines === 1)
|
|
458
|
+
return false;
|
|
459
|
+
return search.trim().length > Math.max(oldString.trim().length + 500, oldString.trim().length * 4);
|
|
460
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workser/engine",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Workser's coding engine for the 'loop in our service' mode: OpenCode's agent contract (its prompts, tool schemas and edit semantics, vendored from v1.18.23) running on Node, with every file and shell action going through a Place \u2014 so the machine can be a remote sandbox and no model key is ever on it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"types": "./dist/src/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/src",
|
|
11
|
+
"dist/vendor",
|
|
11
12
|
"vendor",
|
|
12
13
|
"NOTICE",
|
|
13
14
|
"README.md"
|
|
@@ -19,10 +20,11 @@
|
|
|
19
20
|
"build": "tsc -p tsconfig.json",
|
|
20
21
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
21
22
|
"test": "rm -rf dist && tsc -p tsconfig.json && node --test dist/test/*.test.js",
|
|
22
|
-
"prepublishOnly": "rm -rf dist && NODE_OPTIONS=--max-old-space-size=3072 tsc -p tsconfig.json"
|
|
23
|
+
"prepublishOnly": "rm -rf dist && NODE_OPTIONS=--max-old-space-size=3072 tsc -p tsconfig.json && node scripts/check-pack.mjs",
|
|
24
|
+
"check:pack": "node scripts/check-pack.mjs"
|
|
23
25
|
},
|
|
24
26
|
"dependencies": {
|
|
25
|
-
"@workser/runtime": "^0.1.
|
|
27
|
+
"@workser/runtime": "^0.1.2"
|
|
26
28
|
},
|
|
27
29
|
"peerDependencies": {
|
|
28
30
|
"e2b": ">=1"
|