@xpert-ai/plugin-sdk 3.37.0 → 3.37.2
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/index.cjs.js +200 -95
- package/index.esm.js +200 -95
- package/package.json +2 -2
- package/src/lib/sandbox/file-edit-command.d.ts +2 -1
- package/src/lib/sandbox/file-edits.d.ts +1 -0
- package/src/lib/sandbox/protocol.d.ts +7 -1
package/index.cjs.js
CHANGED
|
@@ -5224,6 +5224,105 @@ function resolveSandboxBackend(sandbox) {
|
|
|
5224
5224
|
return isSandboxBackend(candidate) ? candidate : null;
|
|
5225
5225
|
}
|
|
5226
5226
|
|
|
5227
|
+
// Embedded in the existing sandbox Node process; no Git, subprocess, or temporary files.
|
|
5228
|
+
const FILE_EDIT_DIFF_SCRIPT = String.raw`
|
|
5229
|
+
function inspectLineDiff(before, after) {
|
|
5230
|
+
if (before === after) return { diff: { removed: 0, added: 0 } };
|
|
5231
|
+
// Include line terminators so a missing final newline is an actual line change.
|
|
5232
|
+
const oldLines = before.match(/[^\n]*\n|[^\n]+$/g) || [];
|
|
5233
|
+
const newLines = after.match(/[^\n]*\n|[^\n]+$/g) || [];
|
|
5234
|
+
let start = 0, oldEnd = oldLines.length, newEnd = newLines.length;
|
|
5235
|
+
while (start < oldEnd && start < newEnd && oldLines[start] === newLines[start]) start++;
|
|
5236
|
+
while (oldEnd > start && newEnd > start && oldLines[oldEnd - 1] === newLines[newEnd - 1]) {
|
|
5237
|
+
oldEnd--;
|
|
5238
|
+
newEnd--;
|
|
5239
|
+
}
|
|
5240
|
+
const removed = oldEnd - start, added = newEnd - start;
|
|
5241
|
+
if (!removed || !added) return { diff: { removed, added } };
|
|
5242
|
+
|
|
5243
|
+
// Myers shortest edit distance needs only a frontier when reporting counts, not a patch.
|
|
5244
|
+
// Bound the optional quadratic search on broad rewrites; never limit the edit itself.
|
|
5245
|
+
const maxDistance = Math.min(removed + added, 2000);
|
|
5246
|
+
const offset = maxDistance + 1;
|
|
5247
|
+
const frontier = new Int32Array(2 * maxDistance + 3);
|
|
5248
|
+
let work = 0;
|
|
5249
|
+
for (let distance = 0; distance <= maxDistance; distance++) {
|
|
5250
|
+
for (let diagonal = -distance; diagonal <= distance; diagonal += 2) {
|
|
5251
|
+
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5252
|
+
const index = offset + diagonal;
|
|
5253
|
+
let x = diagonal === -distance || (diagonal !== distance && frontier[index - 1] < frontier[index + 1])
|
|
5254
|
+
? frontier[index + 1]
|
|
5255
|
+
: frontier[index - 1] + 1;
|
|
5256
|
+
let y = x - diagonal;
|
|
5257
|
+
while (x < removed && y < added && oldLines[start + x] === newLines[start + y]) {
|
|
5258
|
+
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5259
|
+
x++;
|
|
5260
|
+
y++;
|
|
5261
|
+
}
|
|
5262
|
+
if (x >= removed && y >= added) {
|
|
5263
|
+
return { diff: { removed: (distance + removed - added) / 2, added: (distance + added - removed) / 2 } };
|
|
5264
|
+
}
|
|
5265
|
+
frontier[index] = x;
|
|
5266
|
+
}
|
|
5267
|
+
}
|
|
5268
|
+
return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5269
|
+
}
|
|
5270
|
+
`;
|
|
5271
|
+
|
|
5272
|
+
// The same comparison runs for single and grouped edits. Only values encoded as
|
|
5273
|
+
// JSON/base64 enter the shell, never literal paths or replacement strings.
|
|
5274
|
+
function command(filePath, edits, batch) {
|
|
5275
|
+
const encoded = Buffer.from(JSON.stringify({
|
|
5276
|
+
filePath,
|
|
5277
|
+
edits,
|
|
5278
|
+
batch
|
|
5279
|
+
})).toString('base64');
|
|
5280
|
+
const script = `
|
|
5281
|
+
const fs = require('node:fs');
|
|
5282
|
+
const input = JSON.parse(Buffer.from('${encoded}', 'base64').toString('utf8'));
|
|
5283
|
+
const lines = text => text.length ? text.split('\\n').length - Number(text.endsWith('\\n')) : 0;
|
|
5284
|
+
${FILE_EDIT_DIFF_SCRIPT}
|
|
5285
|
+
let source;
|
|
5286
|
+
try { source = fs.readFileSync(input.filePath, 'utf8'); } catch {}
|
|
5287
|
+
let content = source;
|
|
5288
|
+
const steps = [];
|
|
5289
|
+
for (const edit of input.edits) {
|
|
5290
|
+
const metrics = { oldLines: lines(edit.oldString), newLines: lines(edit.newString) };
|
|
5291
|
+
const parts = content === undefined ? [] : content.split(edit.oldString);
|
|
5292
|
+
const count = parts.length - 1;
|
|
5293
|
+
const code = content === undefined ? 3 : count === 0 ? 1 : count > 1 && !edit.replaceAll ? 2 : 0;
|
|
5294
|
+
if (code) {
|
|
5295
|
+
steps.push({ code, metrics: { ...metrics, occurrences: 0, changed: false, diff: { removed: 0, added: 0 } } });
|
|
5296
|
+
break;
|
|
5297
|
+
}
|
|
5298
|
+
const after = parts.join(edit.newString);
|
|
5299
|
+
try { Object.assign(metrics, inspectLineDiff(content, after)); } catch { metrics.diffUnavailable = 'diff_unavailable'; }
|
|
5300
|
+
// A direct SDK edit of empty search text in an empty file historically reports one no-op.
|
|
5301
|
+
const occurrences = input.batch && count < 0 ? 1 : count;
|
|
5302
|
+
steps.push({ code, metrics: { ...metrics, occurrences, changed: content !== after } });
|
|
5303
|
+
content = after;
|
|
5304
|
+
}
|
|
5305
|
+
// A logical mismatch commits its valid prefix. An I/O failure must never emit
|
|
5306
|
+
// a successful receipt (or cause the host to replay a possibly committed edit).
|
|
5307
|
+
if (source !== content) fs.writeFileSync(input.filePath, content, 'utf8');
|
|
5308
|
+
if (input.batch) console.log(JSON.stringify(steps));
|
|
5309
|
+
else { console.log(JSON.stringify(steps[0].metrics)); process.exitCode = steps[0].code; }
|
|
5310
|
+
`;
|
|
5311
|
+
return `node -e '${script.replace(/'/g, "'\\''")}'`;
|
|
5312
|
+
}
|
|
5313
|
+
function buildEditCommand(filePath, oldString, newString, replaceAll) {
|
|
5314
|
+
return command(filePath, [
|
|
5315
|
+
{
|
|
5316
|
+
oldString,
|
|
5317
|
+
newString,
|
|
5318
|
+
replaceAll
|
|
5319
|
+
}
|
|
5320
|
+
], false);
|
|
5321
|
+
}
|
|
5322
|
+
function buildBatchEditCommand(filePath, edits) {
|
|
5323
|
+
return command(filePath, edits, true);
|
|
5324
|
+
}
|
|
5325
|
+
|
|
5227
5326
|
const editMetricsSchema = v3.z.object({
|
|
5228
5327
|
occurrences: v3.z.number().int().nonnegative(),
|
|
5229
5328
|
oldLines: v3.z.number().int().nonnegative(),
|
|
@@ -5283,121 +5382,124 @@ function editCommandResult(filePath, result) {
|
|
|
5283
5382
|
};
|
|
5284
5383
|
}
|
|
5285
5384
|
}
|
|
5286
|
-
|
|
5287
|
-
const
|
|
5288
|
-
if (!
|
|
5385
|
+
function sequenceResult(path, results) {
|
|
5386
|
+
const count = results.length;
|
|
5387
|
+
if (!count) return {
|
|
5289
5388
|
error: 'No edits provided',
|
|
5290
5389
|
progress: {
|
|
5291
5390
|
mode: 'sequential',
|
|
5292
5391
|
appliedEdits: 0
|
|
5293
5392
|
}
|
|
5294
5393
|
};
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
|
|
5298
|
-
|
|
5299
|
-
error: `Multi-edit failed at edit ${results.length}: ${result.error}`,
|
|
5300
|
-
path: filePath,
|
|
5394
|
+
const last = results[count - 1];
|
|
5395
|
+
if (last.error) {
|
|
5396
|
+
return {
|
|
5397
|
+
path,
|
|
5301
5398
|
filesUpdate: null,
|
|
5302
5399
|
results,
|
|
5400
|
+
error: `Multi-edit failed at edit ${count}: ${last.error}`,
|
|
5303
5401
|
progress: {
|
|
5304
5402
|
mode: 'sequential',
|
|
5305
|
-
appliedEdits:
|
|
5306
|
-
failedEdit:
|
|
5403
|
+
appliedEdits: count - 1,
|
|
5404
|
+
failedEdit: count
|
|
5307
5405
|
}
|
|
5308
5406
|
};
|
|
5309
5407
|
}
|
|
5310
5408
|
return {
|
|
5311
|
-
path
|
|
5409
|
+
path,
|
|
5312
5410
|
filesUpdate: null,
|
|
5313
5411
|
results,
|
|
5314
5412
|
progress: {
|
|
5315
5413
|
mode: 'sequential',
|
|
5316
|
-
appliedEdits:
|
|
5414
|
+
appliedEdits: count
|
|
5317
5415
|
}
|
|
5318
5416
|
};
|
|
5319
5417
|
}
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
const
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
|
|
5326
|
-
|
|
5327
|
-
|
|
5328
|
-
|
|
5329
|
-
|
|
5330
|
-
|
|
5331
|
-
|
|
5332
|
-
|
|
5333
|
-
|
|
5334
|
-
|
|
5335
|
-
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
|
|
5339
|
-
|
|
5340
|
-
|
|
5341
|
-
|
|
5342
|
-
|
|
5343
|
-
|
|
5344
|
-
for (let diagonal = -distance; diagonal <= distance; diagonal += 2) {
|
|
5345
|
-
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5346
|
-
const index = offset + diagonal;
|
|
5347
|
-
let x = diagonal === -distance || (diagonal !== distance && frontier[index - 1] < frontier[index + 1])
|
|
5348
|
-
? frontier[index + 1]
|
|
5349
|
-
: frontier[index - 1] + 1;
|
|
5350
|
-
let y = x - diagonal;
|
|
5351
|
-
while (x < removed && y < added && oldLines[start + x] === newLines[start + y]) {
|
|
5352
|
-
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5353
|
-
x++;
|
|
5354
|
-
y++;
|
|
5355
|
-
}
|
|
5356
|
-
if (x >= removed && y >= added) {
|
|
5357
|
-
return { diff: { removed: (distance + removed - added) / 2, added: (distance + added - removed) / 2 } };
|
|
5358
|
-
}
|
|
5359
|
-
frontier[index] = x;
|
|
5418
|
+
/** Stops on failure; never promises rollback or silently reapplies completed edits. */ async function sequentialFileEdits(filePath, edits, apply) {
|
|
5419
|
+
const results = [];
|
|
5420
|
+
for (const edit of edits != null ? edits : []){
|
|
5421
|
+
const result = await apply(edit);
|
|
5422
|
+
results.push(result);
|
|
5423
|
+
if (result.error) break;
|
|
5424
|
+
}
|
|
5425
|
+
return sequenceResult(filePath, results);
|
|
5426
|
+
}
|
|
5427
|
+
const batchReceiptSchema = v3.z.array(v3.z.object({
|
|
5428
|
+
code: v3.z.union([
|
|
5429
|
+
v3.z.literal(0),
|
|
5430
|
+
v3.z.literal(1),
|
|
5431
|
+
v3.z.literal(2),
|
|
5432
|
+
v3.z.literal(3)
|
|
5433
|
+
]),
|
|
5434
|
+
metrics: editMetricsSchema
|
|
5435
|
+
}));
|
|
5436
|
+
function batchSteps(output, count) {
|
|
5437
|
+
let raw;
|
|
5438
|
+
try {
|
|
5439
|
+
raw = JSON.parse(output);
|
|
5440
|
+
} catch (e) {
|
|
5441
|
+
return undefined;
|
|
5360
5442
|
}
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
|
|
5366
|
-
|
|
5367
|
-
|
|
5368
|
-
|
|
5369
|
-
|
|
5370
|
-
|
|
5371
|
-
|
|
5372
|
-
|
|
5373
|
-
|
|
5374
|
-
|
|
5375
|
-
|
|
5376
|
-
|
|
5377
|
-
|
|
5378
|
-
|
|
5379
|
-
|
|
5380
|
-
|
|
5381
|
-
|
|
5382
|
-
|
|
5383
|
-
|
|
5384
|
-
|
|
5385
|
-
|
|
5386
|
-
|
|
5387
|
-
|
|
5388
|
-
|
|
5389
|
-
|
|
5390
|
-
|
|
5391
|
-
|
|
5392
|
-
|
|
5393
|
-
|
|
5394
|
-
|
|
5395
|
-
|
|
5396
|
-
|
|
5397
|
-
}
|
|
5398
|
-
|
|
5399
|
-
|
|
5400
|
-
|
|
5443
|
+
const parsed = batchReceiptSchema.safeParse(raw);
|
|
5444
|
+
if (!parsed.success) return undefined;
|
|
5445
|
+
const steps = parsed.data;
|
|
5446
|
+
const failure = steps.findIndex((step)=>step.code !== 0);
|
|
5447
|
+
if (!steps.length || steps.length > count) return undefined;
|
|
5448
|
+
if (steps.some(({ code, metrics })=>code === 0 ? metrics.occurrences === 0 : metrics.changed || metrics.occurrences !== 0)) return undefined;
|
|
5449
|
+
if (failure < 0 ? steps.length !== count : failure !== steps.length - 1) return undefined;
|
|
5450
|
+
return steps;
|
|
5451
|
+
}
|
|
5452
|
+
/** Keep the encoded shell argument bounded. An oversized edit runs alone. */ function batchEnd(path, edits, start) {
|
|
5453
|
+
let bytes = Buffer.byteLength(JSON.stringify(path));
|
|
5454
|
+
let end = start;
|
|
5455
|
+
while(end < edits.length && end - start < 32){
|
|
5456
|
+
bytes += Buffer.byteLength(JSON.stringify(edits[end])) + 1;
|
|
5457
|
+
if (bytes > 64 * 1024) break;
|
|
5458
|
+
end++;
|
|
5459
|
+
}
|
|
5460
|
+
return Math.max(start + 1, end);
|
|
5461
|
+
}
|
|
5462
|
+
async function groupedFileEdits(path, edits, execute) {
|
|
5463
|
+
const results = [];
|
|
5464
|
+
var _edits_length;
|
|
5465
|
+
while(results.length < ((_edits_length = edits == null ? void 0 : edits.length) != null ? _edits_length : 0)){
|
|
5466
|
+
const start = results.length;
|
|
5467
|
+
const end = batchEnd(path, edits, start);
|
|
5468
|
+
const unconfirmed = (reason)=>({
|
|
5469
|
+
path,
|
|
5470
|
+
filesUpdate: null,
|
|
5471
|
+
results,
|
|
5472
|
+
error: `Sandbox multi-edit: ${reason}. Read the current file before retrying edits ${start + 1}–${end}.`,
|
|
5473
|
+
progress: {
|
|
5474
|
+
mode: 'sequential',
|
|
5475
|
+
appliedEdits: start,
|
|
5476
|
+
unconfirmedEdits: {
|
|
5477
|
+
from: start + 1,
|
|
5478
|
+
to: end
|
|
5479
|
+
}
|
|
5480
|
+
}
|
|
5481
|
+
});
|
|
5482
|
+
const command = buildBatchEditCommand(path, edits.slice(start, end));
|
|
5483
|
+
let response;
|
|
5484
|
+
try {
|
|
5485
|
+
response = await execute(command);
|
|
5486
|
+
} catch (e) {
|
|
5487
|
+
return unconfirmed('execution receipt unavailable');
|
|
5488
|
+
}
|
|
5489
|
+
if (response.timedOut) return unconfirmed('execution timed out');
|
|
5490
|
+
if (response.terminationReason) return unconfirmed(`execution terminated (${response.terminationReason})`);
|
|
5491
|
+
if (response.exitCode !== 0) return unconfirmed('execution did not exit successfully');
|
|
5492
|
+
if (response.truncated) return unconfirmed('truncated execution receipt');
|
|
5493
|
+
const steps = batchSteps(response.output, end - start);
|
|
5494
|
+
if (!steps) return unconfirmed('invalid execution receipt');
|
|
5495
|
+
for (const step of steps){
|
|
5496
|
+
results.push(editCommandResult(path, {
|
|
5497
|
+
output: JSON.stringify(step.metrics),
|
|
5498
|
+
exitCode: step.code}));
|
|
5499
|
+
}
|
|
5500
|
+
if (results[results.length - 1].error) break;
|
|
5501
|
+
}
|
|
5502
|
+
return sequenceResult(path, results);
|
|
5401
5503
|
}
|
|
5402
5504
|
|
|
5403
5505
|
const INDENTATION_SPACES = 2;
|
|
@@ -6226,10 +6328,13 @@ try {
|
|
|
6226
6328
|
return editCommandResult(filePath, result);
|
|
6227
6329
|
}
|
|
6228
6330
|
/** Earlier successful edits remain persisted if a later edit fails. */ multiEdit(filePath, edits) {
|
|
6229
|
-
|
|
6331
|
+
const single = (edit)=>{
|
|
6230
6332
|
var _edit_replaceAll;
|
|
6231
6333
|
return this.edit(filePath, edit.oldString, edit.newString, (_edit_replaceAll = edit.replaceAll) != null ? _edit_replaceAll : false);
|
|
6232
|
-
}
|
|
6334
|
+
};
|
|
6335
|
+
// Backends with their own edit storage/authorization semantics retain that implementation.
|
|
6336
|
+
if (this.edit !== BaseSandbox.prototype.edit) return sequentialFileEdits(filePath, edits, single);
|
|
6337
|
+
return groupedFileEdits(filePath, edits, (command)=>this.executeOperation(command, DEFAULT_SANDBOX_FILE_OPERATION_EXECUTION_OPTIONS));
|
|
6233
6338
|
}
|
|
6234
6339
|
}
|
|
6235
6340
|
|
package/index.esm.js
CHANGED
|
@@ -5206,6 +5206,105 @@ function resolveSandboxBackend(sandbox) {
|
|
|
5206
5206
|
return isSandboxBackend(candidate) ? candidate : null;
|
|
5207
5207
|
}
|
|
5208
5208
|
|
|
5209
|
+
// Embedded in the existing sandbox Node process; no Git, subprocess, or temporary files.
|
|
5210
|
+
const FILE_EDIT_DIFF_SCRIPT = String.raw`
|
|
5211
|
+
function inspectLineDiff(before, after) {
|
|
5212
|
+
if (before === after) return { diff: { removed: 0, added: 0 } };
|
|
5213
|
+
// Include line terminators so a missing final newline is an actual line change.
|
|
5214
|
+
const oldLines = before.match(/[^\n]*\n|[^\n]+$/g) || [];
|
|
5215
|
+
const newLines = after.match(/[^\n]*\n|[^\n]+$/g) || [];
|
|
5216
|
+
let start = 0, oldEnd = oldLines.length, newEnd = newLines.length;
|
|
5217
|
+
while (start < oldEnd && start < newEnd && oldLines[start] === newLines[start]) start++;
|
|
5218
|
+
while (oldEnd > start && newEnd > start && oldLines[oldEnd - 1] === newLines[newEnd - 1]) {
|
|
5219
|
+
oldEnd--;
|
|
5220
|
+
newEnd--;
|
|
5221
|
+
}
|
|
5222
|
+
const removed = oldEnd - start, added = newEnd - start;
|
|
5223
|
+
if (!removed || !added) return { diff: { removed, added } };
|
|
5224
|
+
|
|
5225
|
+
// Myers shortest edit distance needs only a frontier when reporting counts, not a patch.
|
|
5226
|
+
// Bound the optional quadratic search on broad rewrites; never limit the edit itself.
|
|
5227
|
+
const maxDistance = Math.min(removed + added, 2000);
|
|
5228
|
+
const offset = maxDistance + 1;
|
|
5229
|
+
const frontier = new Int32Array(2 * maxDistance + 3);
|
|
5230
|
+
let work = 0;
|
|
5231
|
+
for (let distance = 0; distance <= maxDistance; distance++) {
|
|
5232
|
+
for (let diagonal = -distance; diagonal <= distance; diagonal += 2) {
|
|
5233
|
+
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5234
|
+
const index = offset + diagonal;
|
|
5235
|
+
let x = diagonal === -distance || (diagonal !== distance && frontier[index - 1] < frontier[index + 1])
|
|
5236
|
+
? frontier[index + 1]
|
|
5237
|
+
: frontier[index - 1] + 1;
|
|
5238
|
+
let y = x - diagonal;
|
|
5239
|
+
while (x < removed && y < added && oldLines[start + x] === newLines[start + y]) {
|
|
5240
|
+
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5241
|
+
x++;
|
|
5242
|
+
y++;
|
|
5243
|
+
}
|
|
5244
|
+
if (x >= removed && y >= added) {
|
|
5245
|
+
return { diff: { removed: (distance + removed - added) / 2, added: (distance + added - removed) / 2 } };
|
|
5246
|
+
}
|
|
5247
|
+
frontier[index] = x;
|
|
5248
|
+
}
|
|
5249
|
+
}
|
|
5250
|
+
return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5251
|
+
}
|
|
5252
|
+
`;
|
|
5253
|
+
|
|
5254
|
+
// The same comparison runs for single and grouped edits. Only values encoded as
|
|
5255
|
+
// JSON/base64 enter the shell, never literal paths or replacement strings.
|
|
5256
|
+
function command(filePath, edits, batch) {
|
|
5257
|
+
const encoded = Buffer.from(JSON.stringify({
|
|
5258
|
+
filePath,
|
|
5259
|
+
edits,
|
|
5260
|
+
batch
|
|
5261
|
+
})).toString('base64');
|
|
5262
|
+
const script = `
|
|
5263
|
+
const fs = require('node:fs');
|
|
5264
|
+
const input = JSON.parse(Buffer.from('${encoded}', 'base64').toString('utf8'));
|
|
5265
|
+
const lines = text => text.length ? text.split('\\n').length - Number(text.endsWith('\\n')) : 0;
|
|
5266
|
+
${FILE_EDIT_DIFF_SCRIPT}
|
|
5267
|
+
let source;
|
|
5268
|
+
try { source = fs.readFileSync(input.filePath, 'utf8'); } catch {}
|
|
5269
|
+
let content = source;
|
|
5270
|
+
const steps = [];
|
|
5271
|
+
for (const edit of input.edits) {
|
|
5272
|
+
const metrics = { oldLines: lines(edit.oldString), newLines: lines(edit.newString) };
|
|
5273
|
+
const parts = content === undefined ? [] : content.split(edit.oldString);
|
|
5274
|
+
const count = parts.length - 1;
|
|
5275
|
+
const code = content === undefined ? 3 : count === 0 ? 1 : count > 1 && !edit.replaceAll ? 2 : 0;
|
|
5276
|
+
if (code) {
|
|
5277
|
+
steps.push({ code, metrics: { ...metrics, occurrences: 0, changed: false, diff: { removed: 0, added: 0 } } });
|
|
5278
|
+
break;
|
|
5279
|
+
}
|
|
5280
|
+
const after = parts.join(edit.newString);
|
|
5281
|
+
try { Object.assign(metrics, inspectLineDiff(content, after)); } catch { metrics.diffUnavailable = 'diff_unavailable'; }
|
|
5282
|
+
// A direct SDK edit of empty search text in an empty file historically reports one no-op.
|
|
5283
|
+
const occurrences = input.batch && count < 0 ? 1 : count;
|
|
5284
|
+
steps.push({ code, metrics: { ...metrics, occurrences, changed: content !== after } });
|
|
5285
|
+
content = after;
|
|
5286
|
+
}
|
|
5287
|
+
// A logical mismatch commits its valid prefix. An I/O failure must never emit
|
|
5288
|
+
// a successful receipt (or cause the host to replay a possibly committed edit).
|
|
5289
|
+
if (source !== content) fs.writeFileSync(input.filePath, content, 'utf8');
|
|
5290
|
+
if (input.batch) console.log(JSON.stringify(steps));
|
|
5291
|
+
else { console.log(JSON.stringify(steps[0].metrics)); process.exitCode = steps[0].code; }
|
|
5292
|
+
`;
|
|
5293
|
+
return `node -e '${script.replace(/'/g, "'\\''")}'`;
|
|
5294
|
+
}
|
|
5295
|
+
function buildEditCommand(filePath, oldString, newString, replaceAll) {
|
|
5296
|
+
return command(filePath, [
|
|
5297
|
+
{
|
|
5298
|
+
oldString,
|
|
5299
|
+
newString,
|
|
5300
|
+
replaceAll
|
|
5301
|
+
}
|
|
5302
|
+
], false);
|
|
5303
|
+
}
|
|
5304
|
+
function buildBatchEditCommand(filePath, edits) {
|
|
5305
|
+
return command(filePath, edits, true);
|
|
5306
|
+
}
|
|
5307
|
+
|
|
5209
5308
|
const editMetricsSchema = z.object({
|
|
5210
5309
|
occurrences: z.number().int().nonnegative(),
|
|
5211
5310
|
oldLines: z.number().int().nonnegative(),
|
|
@@ -5265,121 +5364,124 @@ function editCommandResult(filePath, result) {
|
|
|
5265
5364
|
};
|
|
5266
5365
|
}
|
|
5267
5366
|
}
|
|
5268
|
-
|
|
5269
|
-
const
|
|
5270
|
-
if (!
|
|
5367
|
+
function sequenceResult(path, results) {
|
|
5368
|
+
const count = results.length;
|
|
5369
|
+
if (!count) return {
|
|
5271
5370
|
error: 'No edits provided',
|
|
5272
5371
|
progress: {
|
|
5273
5372
|
mode: 'sequential',
|
|
5274
5373
|
appliedEdits: 0
|
|
5275
5374
|
}
|
|
5276
5375
|
};
|
|
5277
|
-
|
|
5278
|
-
|
|
5279
|
-
|
|
5280
|
-
|
|
5281
|
-
error: `Multi-edit failed at edit ${results.length}: ${result.error}`,
|
|
5282
|
-
path: filePath,
|
|
5376
|
+
const last = results[count - 1];
|
|
5377
|
+
if (last.error) {
|
|
5378
|
+
return {
|
|
5379
|
+
path,
|
|
5283
5380
|
filesUpdate: null,
|
|
5284
5381
|
results,
|
|
5382
|
+
error: `Multi-edit failed at edit ${count}: ${last.error}`,
|
|
5285
5383
|
progress: {
|
|
5286
5384
|
mode: 'sequential',
|
|
5287
|
-
appliedEdits:
|
|
5288
|
-
failedEdit:
|
|
5385
|
+
appliedEdits: count - 1,
|
|
5386
|
+
failedEdit: count
|
|
5289
5387
|
}
|
|
5290
5388
|
};
|
|
5291
5389
|
}
|
|
5292
5390
|
return {
|
|
5293
|
-
path
|
|
5391
|
+
path,
|
|
5294
5392
|
filesUpdate: null,
|
|
5295
5393
|
results,
|
|
5296
5394
|
progress: {
|
|
5297
5395
|
mode: 'sequential',
|
|
5298
|
-
appliedEdits:
|
|
5396
|
+
appliedEdits: count
|
|
5299
5397
|
}
|
|
5300
5398
|
};
|
|
5301
5399
|
}
|
|
5302
|
-
|
|
5303
|
-
|
|
5304
|
-
const
|
|
5305
|
-
|
|
5306
|
-
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5314
|
-
|
|
5315
|
-
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
5319
|
-
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
|
|
5326
|
-
for (let diagonal = -distance; diagonal <= distance; diagonal += 2) {
|
|
5327
|
-
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5328
|
-
const index = offset + diagonal;
|
|
5329
|
-
let x = diagonal === -distance || (diagonal !== distance && frontier[index - 1] < frontier[index + 1])
|
|
5330
|
-
? frontier[index + 1]
|
|
5331
|
-
: frontier[index - 1] + 1;
|
|
5332
|
-
let y = x - diagonal;
|
|
5333
|
-
while (x < removed && y < added && oldLines[start + x] === newLines[start + y]) {
|
|
5334
|
-
if (++work > 1000000) return { diffUnavailable: 'comparison_budget_exceeded' };
|
|
5335
|
-
x++;
|
|
5336
|
-
y++;
|
|
5337
|
-
}
|
|
5338
|
-
if (x >= removed && y >= added) {
|
|
5339
|
-
return { diff: { removed: (distance + removed - added) / 2, added: (distance + added - removed) / 2 } };
|
|
5340
|
-
}
|
|
5341
|
-
frontier[index] = x;
|
|
5400
|
+
/** Stops on failure; never promises rollback or silently reapplies completed edits. */ async function sequentialFileEdits(filePath, edits, apply) {
|
|
5401
|
+
const results = [];
|
|
5402
|
+
for (const edit of edits != null ? edits : []){
|
|
5403
|
+
const result = await apply(edit);
|
|
5404
|
+
results.push(result);
|
|
5405
|
+
if (result.error) break;
|
|
5406
|
+
}
|
|
5407
|
+
return sequenceResult(filePath, results);
|
|
5408
|
+
}
|
|
5409
|
+
const batchReceiptSchema = z.array(z.object({
|
|
5410
|
+
code: z.union([
|
|
5411
|
+
z.literal(0),
|
|
5412
|
+
z.literal(1),
|
|
5413
|
+
z.literal(2),
|
|
5414
|
+
z.literal(3)
|
|
5415
|
+
]),
|
|
5416
|
+
metrics: editMetricsSchema
|
|
5417
|
+
}));
|
|
5418
|
+
function batchSteps(output, count) {
|
|
5419
|
+
let raw;
|
|
5420
|
+
try {
|
|
5421
|
+
raw = JSON.parse(output);
|
|
5422
|
+
} catch (e) {
|
|
5423
|
+
return undefined;
|
|
5342
5424
|
}
|
|
5343
|
-
|
|
5344
|
-
|
|
5345
|
-
|
|
5346
|
-
|
|
5347
|
-
|
|
5348
|
-
|
|
5349
|
-
|
|
5350
|
-
|
|
5351
|
-
|
|
5352
|
-
|
|
5353
|
-
|
|
5354
|
-
|
|
5355
|
-
|
|
5356
|
-
|
|
5357
|
-
|
|
5358
|
-
|
|
5359
|
-
|
|
5360
|
-
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
|
|
5366
|
-
|
|
5367
|
-
|
|
5368
|
-
|
|
5369
|
-
|
|
5370
|
-
|
|
5371
|
-
|
|
5372
|
-
|
|
5373
|
-
|
|
5374
|
-
|
|
5375
|
-
|
|
5376
|
-
|
|
5377
|
-
|
|
5378
|
-
|
|
5379
|
-
}
|
|
5380
|
-
|
|
5381
|
-
|
|
5382
|
-
|
|
5425
|
+
const parsed = batchReceiptSchema.safeParse(raw);
|
|
5426
|
+
if (!parsed.success) return undefined;
|
|
5427
|
+
const steps = parsed.data;
|
|
5428
|
+
const failure = steps.findIndex((step)=>step.code !== 0);
|
|
5429
|
+
if (!steps.length || steps.length > count) return undefined;
|
|
5430
|
+
if (steps.some(({ code, metrics })=>code === 0 ? metrics.occurrences === 0 : metrics.changed || metrics.occurrences !== 0)) return undefined;
|
|
5431
|
+
if (failure < 0 ? steps.length !== count : failure !== steps.length - 1) return undefined;
|
|
5432
|
+
return steps;
|
|
5433
|
+
}
|
|
5434
|
+
/** Keep the encoded shell argument bounded. An oversized edit runs alone. */ function batchEnd(path, edits, start) {
|
|
5435
|
+
let bytes = Buffer.byteLength(JSON.stringify(path));
|
|
5436
|
+
let end = start;
|
|
5437
|
+
while(end < edits.length && end - start < 32){
|
|
5438
|
+
bytes += Buffer.byteLength(JSON.stringify(edits[end])) + 1;
|
|
5439
|
+
if (bytes > 64 * 1024) break;
|
|
5440
|
+
end++;
|
|
5441
|
+
}
|
|
5442
|
+
return Math.max(start + 1, end);
|
|
5443
|
+
}
|
|
5444
|
+
async function groupedFileEdits(path, edits, execute) {
|
|
5445
|
+
const results = [];
|
|
5446
|
+
var _edits_length;
|
|
5447
|
+
while(results.length < ((_edits_length = edits == null ? void 0 : edits.length) != null ? _edits_length : 0)){
|
|
5448
|
+
const start = results.length;
|
|
5449
|
+
const end = batchEnd(path, edits, start);
|
|
5450
|
+
const unconfirmed = (reason)=>({
|
|
5451
|
+
path,
|
|
5452
|
+
filesUpdate: null,
|
|
5453
|
+
results,
|
|
5454
|
+
error: `Sandbox multi-edit: ${reason}. Read the current file before retrying edits ${start + 1}–${end}.`,
|
|
5455
|
+
progress: {
|
|
5456
|
+
mode: 'sequential',
|
|
5457
|
+
appliedEdits: start,
|
|
5458
|
+
unconfirmedEdits: {
|
|
5459
|
+
from: start + 1,
|
|
5460
|
+
to: end
|
|
5461
|
+
}
|
|
5462
|
+
}
|
|
5463
|
+
});
|
|
5464
|
+
const command = buildBatchEditCommand(path, edits.slice(start, end));
|
|
5465
|
+
let response;
|
|
5466
|
+
try {
|
|
5467
|
+
response = await execute(command);
|
|
5468
|
+
} catch (e) {
|
|
5469
|
+
return unconfirmed('execution receipt unavailable');
|
|
5470
|
+
}
|
|
5471
|
+
if (response.timedOut) return unconfirmed('execution timed out');
|
|
5472
|
+
if (response.terminationReason) return unconfirmed(`execution terminated (${response.terminationReason})`);
|
|
5473
|
+
if (response.exitCode !== 0) return unconfirmed('execution did not exit successfully');
|
|
5474
|
+
if (response.truncated) return unconfirmed('truncated execution receipt');
|
|
5475
|
+
const steps = batchSteps(response.output, end - start);
|
|
5476
|
+
if (!steps) return unconfirmed('invalid execution receipt');
|
|
5477
|
+
for (const step of steps){
|
|
5478
|
+
results.push(editCommandResult(path, {
|
|
5479
|
+
output: JSON.stringify(step.metrics),
|
|
5480
|
+
exitCode: step.code}));
|
|
5481
|
+
}
|
|
5482
|
+
if (results[results.length - 1].error) break;
|
|
5483
|
+
}
|
|
5484
|
+
return sequenceResult(path, results);
|
|
5383
5485
|
}
|
|
5384
5486
|
|
|
5385
5487
|
const INDENTATION_SPACES = 2;
|
|
@@ -6208,10 +6310,13 @@ try {
|
|
|
6208
6310
|
return editCommandResult(filePath, result);
|
|
6209
6311
|
}
|
|
6210
6312
|
/** Earlier successful edits remain persisted if a later edit fails. */ multiEdit(filePath, edits) {
|
|
6211
|
-
|
|
6313
|
+
const single = (edit)=>{
|
|
6212
6314
|
var _edit_replaceAll;
|
|
6213
6315
|
return this.edit(filePath, edit.oldString, edit.newString, (_edit_replaceAll = edit.replaceAll) != null ? _edit_replaceAll : false);
|
|
6214
|
-
}
|
|
6316
|
+
};
|
|
6317
|
+
// Backends with their own edit storage/authorization semantics retain that implementation.
|
|
6318
|
+
if (this.edit !== BaseSandbox.prototype.edit) return sequentialFileEdits(filePath, edits, single);
|
|
6319
|
+
return groupedFileEdits(filePath, edits, (command)=>this.executeOperation(command, DEFAULT_SANDBOX_FILE_OPERATION_EXECUTION_OPTIONS));
|
|
6215
6320
|
}
|
|
6216
6321
|
}
|
|
6217
6322
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xpert-ai/plugin-sdk",
|
|
3
|
-
"version": "3.37.
|
|
3
|
+
"version": "3.37.2",
|
|
4
4
|
"license": "AGPL-3.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
82
|
"@langchain/openai": "0.6.9",
|
|
83
|
-
"@xpert-ai/contracts": "3.37.
|
|
83
|
+
"@xpert-ai/contracts": "3.37.2",
|
|
84
84
|
"express": "^4.22.1",
|
|
85
85
|
"js-tiktoken": "^1.0.21"
|
|
86
86
|
},
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import type { EditOperation } from './protocol';
|
|
2
2
|
export declare function buildEditCommand(filePath: string, oldString: string, newString: string, replaceAll: boolean): string;
|
|
3
|
+
export declare function buildBatchEditCommand(filePath: string, edits: EditOperation[]): string;
|
|
@@ -2,3 +2,4 @@ import type { EditOperation, EditResult, ExecuteResponse, MaybePromise, MultiEdi
|
|
|
2
2
|
export declare function editCommandResult(filePath: string, result: ExecuteResponse): EditResult;
|
|
3
3
|
/** Stops on failure; never promises rollback or silently reapplies completed edits. */
|
|
4
4
|
export declare function sequentialFileEdits(filePath: string, edits: EditOperation[], apply: (edit: EditOperation) => MaybePromise<EditResult>): Promise<MultiEditResult>;
|
|
5
|
+
export declare function groupedFileEdits(path: string, edits: EditOperation[], execute: (command: string) => MaybePromise<ExecuteResponse>): Promise<MultiEditResult>;
|
|
@@ -151,11 +151,17 @@ export interface MultiEditResult {
|
|
|
151
151
|
filesUpdate?: Record<string, FileData> | null;
|
|
152
152
|
/** Results from each individual edit operation */
|
|
153
153
|
results?: EditResult[];
|
|
154
|
-
/**
|
|
154
|
+
/** Indices are one-based. appliedEdits counts only the confirmed prefix. */
|
|
155
155
|
progress?: {
|
|
156
156
|
mode: 'sequential';
|
|
157
157
|
appliedEdits: number;
|
|
158
|
+
/** An edit was confirmed to have failed (for example, its old text did not match). */
|
|
158
159
|
failedEdit?: number;
|
|
160
|
+
/** Inclusive range of the submitted batch with unknown persistence. Later edits were not sent. */
|
|
161
|
+
unconfirmedEdits?: {
|
|
162
|
+
from: number;
|
|
163
|
+
to: number;
|
|
164
|
+
};
|
|
159
165
|
};
|
|
160
166
|
/** Metadata for the multi-edit operation, attached to the ToolMessage */
|
|
161
167
|
metadata?: Record<string, unknown>;
|