@cydm/magic-shell-agent-node 0.1.7 → 0.1.8
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/adapters/pty-adapter.js +65 -5
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawn } from "node-pty";
|
|
2
|
-
import {
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { delimiter, dirname, extname, join } from "path";
|
|
3
4
|
import { fileURLToPath } from "url";
|
|
4
5
|
const __filename = fileURLToPath(import.meta.url);
|
|
5
6
|
const __dirname = dirname(__filename);
|
|
@@ -18,13 +19,18 @@ export class PtyAdapter {
|
|
|
18
19
|
const agentId = `pty-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
19
20
|
const spawnCwd = config.cwd || process.cwd();
|
|
20
21
|
const spawnEnv = { ...process.env, ...config.env };
|
|
21
|
-
|
|
22
|
+
const spawnPlan = process.platform === "win32"
|
|
23
|
+
? buildWindowsSpawnPlan(config.command, config.args || [], spawnEnv)
|
|
24
|
+
: { command: config.command, args: config.args || [] };
|
|
25
|
+
console.log(`[PtyAdapter] Starting ${agentId}: command=${JSON.stringify(spawnPlan.command)} args=${JSON.stringify(spawnPlan.args)} cwd=${JSON.stringify(spawnCwd)} platform=${process.platform}`);
|
|
22
26
|
if (process.platform === "win32" && !isAbsoluteOrQualifiedCommand(config.command)) {
|
|
23
|
-
|
|
27
|
+
const pathValue = spawnEnv.Path || spawnEnv.PATH;
|
|
28
|
+
console.log(`[PtyAdapter] Windows bare command probe for ${agentId}: ${JSON.stringify(findWindowsCommandCandidates(config.command, pathValue))}`);
|
|
29
|
+
console.log(`[PtyAdapter] Windows spawn plan for ${agentId}: command=${JSON.stringify(spawnPlan.command)} args=${JSON.stringify(spawnPlan.args)}`);
|
|
24
30
|
}
|
|
25
31
|
let pty;
|
|
26
32
|
try {
|
|
27
|
-
pty = spawn(
|
|
33
|
+
pty = spawn(spawnPlan.command, spawnPlan.args, {
|
|
28
34
|
name: "xterm-256color",
|
|
29
35
|
cols: 80,
|
|
30
36
|
rows: 24,
|
|
@@ -116,7 +122,7 @@ function isAbsoluteOrQualifiedCommand(command) {
|
|
|
116
122
|
function findWindowsCommandCandidates(command, pathValue) {
|
|
117
123
|
if (!pathValue)
|
|
118
124
|
return [];
|
|
119
|
-
const pathext = (process.env.PATHEXT || ".COM;.EXE;.BAT;.CMD;.PS1")
|
|
125
|
+
const pathext = ((process.env.PATHEXT || ".COM;.EXE;.BAT;.CMD;.PS1"))
|
|
120
126
|
.split(";")
|
|
121
127
|
.map((value) => value.trim())
|
|
122
128
|
.filter(Boolean);
|
|
@@ -128,3 +134,57 @@ function findWindowsCommandCandidates(command, pathValue) {
|
|
|
128
134
|
}
|
|
129
135
|
return results.slice(0, 20);
|
|
130
136
|
}
|
|
137
|
+
function buildWindowsSpawnPlan(command, args, env) {
|
|
138
|
+
if (isAbsoluteOrQualifiedCommand(command)) {
|
|
139
|
+
return { command, args };
|
|
140
|
+
}
|
|
141
|
+
const resolved = resolveWindowsBareCommand(command, env);
|
|
142
|
+
if (resolved && isDirectlySpawnableWindowsBinary(resolved)) {
|
|
143
|
+
return { command: resolved, args };
|
|
144
|
+
}
|
|
145
|
+
const shellCommand = resolved || command;
|
|
146
|
+
return {
|
|
147
|
+
command: env.ComSpec || process.env.ComSpec || "cmd.exe",
|
|
148
|
+
args: ["/d", "/s", "/c", quoteWindowsCommand(shellCommand, args)],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function resolveWindowsBareCommand(command, env) {
|
|
152
|
+
const pathValue = env.Path || env.PATH;
|
|
153
|
+
if (!pathValue)
|
|
154
|
+
return null;
|
|
155
|
+
try {
|
|
156
|
+
const result = spawnSync("where", [command], {
|
|
157
|
+
env,
|
|
158
|
+
encoding: "utf8",
|
|
159
|
+
windowsHide: true,
|
|
160
|
+
});
|
|
161
|
+
if (result.status === 0) {
|
|
162
|
+
const first = String(result.stdout || "")
|
|
163
|
+
.split(/\r?\n/)
|
|
164
|
+
.map((line) => line.trim())
|
|
165
|
+
.find(Boolean);
|
|
166
|
+
if (first)
|
|
167
|
+
return first;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
// Fall through to manual candidates.
|
|
172
|
+
}
|
|
173
|
+
const candidate = findWindowsCommandCandidates(command, pathValue).find(Boolean);
|
|
174
|
+
return candidate || null;
|
|
175
|
+
}
|
|
176
|
+
function isDirectlySpawnableWindowsBinary(command) {
|
|
177
|
+
const ext = extname(command).toLowerCase();
|
|
178
|
+
return ext === ".exe" || ext === ".com";
|
|
179
|
+
}
|
|
180
|
+
function quoteWindowsCommand(command, args) {
|
|
181
|
+
return [command, ...args].map(quoteWindowsArg).join(" ");
|
|
182
|
+
}
|
|
183
|
+
function quoteWindowsArg(value) {
|
|
184
|
+
if (value.length === 0)
|
|
185
|
+
return '""';
|
|
186
|
+
if (!/[\s"]/u.test(value))
|
|
187
|
+
return value;
|
|
188
|
+
const escaped = value.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/g, "$1$1");
|
|
189
|
+
return `"${escaped}"`;
|
|
190
|
+
}
|