shellway 0.4.0 → 0.4.1
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/package.json +1 -1
- package/shellway.js +20 -4
package/package.json
CHANGED
package/shellway.js
CHANGED
|
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
7
7
|
import { execFileSync } from 'node:child_process';
|
|
8
8
|
import { readFile, writeFile, appendFile, readdir, stat, lstat, mkdir, rename, cp, rm } from 'node:fs/promises';
|
|
9
9
|
|
|
10
|
+
const AGENT_VERSION = '0.4.1'; // keep equal to agent/package.json
|
|
10
11
|
if (Number(process.versions.node.split('.')[0]) < 22) {
|
|
11
12
|
console.error(`Shellway needs Node.js 22 or newer (you have ${process.versions.node}). Get it at https://nodejs.org`);
|
|
12
13
|
process.exit(1);
|
|
@@ -238,11 +239,26 @@ const handlers = {
|
|
|
238
239
|
if (st.size > MAX_EDIT_BYTES) throw new Error(`File is too large to edit (${st.size} bytes, limit ${MAX_EDIT_BYTES})`);
|
|
239
240
|
const buf = await readFile(p);
|
|
240
241
|
if (buf.subarray(0, 8000).includes(0)) throw new Error('This looks like a binary file; edit_block only edits text files');
|
|
241
|
-
const
|
|
242
|
+
const text = buf.toString('utf8');
|
|
243
|
+
let oldS = old_string, newS = new_string;
|
|
244
|
+
let parts = text.split(oldS);
|
|
245
|
+
// Windows files often use CRLF line endings while the AI sends plain LF: retry with the file's own line endings.
|
|
246
|
+
if (parts.length === 1 && text.includes('\r\n') && /\n/.test(oldS)) {
|
|
247
|
+
const crlf = (x) => x.replace(/\r?\n/g, '\r\n');
|
|
248
|
+
const retry = text.split(crlf(oldS));
|
|
249
|
+
if (retry.length > 1) { parts = retry; oldS = crlf(oldS); newS = crlf(newS); }
|
|
250
|
+
}
|
|
242
251
|
const found = parts.length - 1;
|
|
243
|
-
if (found === 0)
|
|
252
|
+
if (found === 0) {
|
|
253
|
+
const first = old_string.split(/\r?\n/).map((l) => l.trim()).find(Boolean);
|
|
254
|
+
const lines = text.split(/\r?\n/);
|
|
255
|
+
const at = first ? lines.findIndex((l) => l.includes(first)) : -1;
|
|
256
|
+
throw new Error(`old_string was not found in the file. Nothing changed. ${at >= 0
|
|
257
|
+
? `Its first line does appear at line ${at + 1}, so the difference is further down: check spaces, tabs and blank lines. Read that part of the file again and copy the text exactly.`
|
|
258
|
+
: 'No line contains the start of it. The file may have changed: read it again and copy the text exactly.'}`);
|
|
259
|
+
}
|
|
244
260
|
if (found !== exp) throw new Error(`old_string appears ${found} time(s) but expected_replacements is ${exp}. Nothing changed. Add more surrounding text to make it unique, or set expected_replacements to ${found}.`);
|
|
245
|
-
await writeFile(p, parts.join(
|
|
261
|
+
await writeFile(p, parts.join(newS));
|
|
246
262
|
return `Replaced ${found} occurrence${found === 1 ? '' : 's'} in ${p}`;
|
|
247
263
|
},
|
|
248
264
|
async search_files({ path, pattern, mode, max_results, ignore_case }) {
|
|
@@ -471,7 +487,7 @@ function start() {
|
|
|
471
487
|
let delay = 1000;
|
|
472
488
|
const connect = async () => {
|
|
473
489
|
if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
|
|
474
|
-
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}
|
|
490
|
+
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
|
|
475
491
|
ws.onopen = () => { delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
|
|
476
492
|
ws.onmessage = async (ev) => {
|
|
477
493
|
let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
|