@hncode/hncode 0.8.0 → 0.8.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hncode/hncode",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "hncode — A cyan-blue TUI AI coding agent with zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/clipboard.js CHANGED
@@ -59,6 +59,51 @@ const ONESHOT_MS = 20000;
59
59
 
60
60
  function win() { return process.platform === 'win32'; }
61
61
 
62
+ /**
63
+ * The clipboard programs for a POSIX box, in preference order, chosen by what is actually installed.
64
+ *
65
+ * The old code hardcoded `xclip` for everything that was not macOS, which fails on two common machines: a Wayland
66
+ * session, where `wl-copy` is the native tool and `xclip` often is not installed at all; and any box without X11
67
+ * tooling, where the command was not found and the throw surfaced as "clipboard unavailable" with no hint about the
68
+ * fix.
69
+ *
70
+ * macOS is not consulted here: `pbcopy`/`pbpaste` ship with the system, so naming them is enough.
71
+ */
72
+ function posixClipboard() {
73
+ if (process.platform === 'darwin') {
74
+ return { copy: ['pbcopy', []], paste: ['pbpaste', []] };
75
+ }
76
+ const has = (name) => {
77
+ try { return cp.execFileSync('which', [name], { stdio: ['ignore', 'pipe', 'ignore'] }).length > 0; }
78
+ catch { return false; }
79
+ };
80
+ // Wayland first when the session says so: on a Wayland desktop `xclip` may be present but talking to XWayland, where
81
+ // the selection does not reach native windows.
82
+ const wayland = !!process.env.WAYLAND_DISPLAY;
83
+ const order = wayland
84
+ ? ['wl-copy', 'xclip', 'xsel']
85
+ : ['xclip', 'xsel', 'wl-copy'];
86
+ for (const name of order) {
87
+ if (!has(name)) continue;
88
+ if (name === 'wl-copy') return { copy: ['wl-copy', []], paste: ['wl-paste', ['--no-newline']] };
89
+ if (name === 'xclip') return { copy: ['xclip', ['-selection', 'clipboard']], paste: ['xclip', ['-selection', 'clipboard', '-o']] };
90
+ return { copy: ['xsel', ['--clipboard', '--input']], paste: ['xsel', ['--clipboard', '--output']] };
91
+ }
92
+ return null;
93
+ }
94
+
95
+ /** Run a clipboard program, returning true on success. Never throws. */
96
+ function runClipboard(spec, input) {
97
+ if (!spec) return false;
98
+ try {
99
+ const r = cp.spawnSync(spec[0], spec[1], {
100
+ input: input === undefined ? undefined : Buffer.from(input, 'utf8'),
101
+ timeout: 5000,
102
+ });
103
+ return !!r && r.status === 0;
104
+ } catch { return false; }
105
+ }
106
+
62
107
  /**
63
108
  * Put text on the clipboard.
64
109
  *
@@ -108,9 +153,10 @@ export function copyText(text) {
108
153
  if (r && r.status === 0) return true;
109
154
  return setClipboardText(s);
110
155
  }
111
- const cmd = process.platform === 'darwin' ? 'pbcopy' : 'xclip -selection clipboard';
112
- cp.execSync(cmd, { input: Buffer.from(s, 'utf8'), timeout: 5000 });
113
- return true;
156
+ // POSIX: whichever of wl-copy / xclip / xsel is actually installed.
157
+ const clip = posixClipboard();
158
+ if (!clip) return false;
159
+ return runClipboard(clip.copy, s);
114
160
  }
115
161
 
116
162
  /** `Set-Clipboard` via PowerShell. False when no PowerShell can be run. */
@@ -318,8 +364,11 @@ function readOnce() {
318
364
  const s = String(b64).replace(/[^A-Za-z0-9+/=]/g, '');
319
365
  return Buffer.from(s, 'base64').toString('utf8');
320
366
  }
321
- const cmd = process.platform === 'darwin' ? 'pbpaste' : 'xclip -selection clipboard -o';
322
- return cp.execSync(cmd, { encoding: 'utf8', timeout: 5000 });
367
+ const clip = posixClipboard();
368
+ if (!clip) throw new Error('no clipboard tool (tried wl-paste, xclip and xsel)');
369
+ const r = cp.spawnSync(clip.paste[0], clip.paste[1], { encoding: 'utf8', timeout: 5000 });
370
+ if (!r || r.status !== 0) throw new Error(`${clip.paste[0]} failed`);
371
+ return String(r.stdout || '');
323
372
  }
324
373
 
325
374
  // Async read: uses the warm helper when available. Returns { text, via } so the
package/src/tools/bash.js CHANGED
@@ -7,7 +7,7 @@ import fs from 'node:fs';
7
7
  import cp from 'node:child_process';
8
8
  import { resolvePath, truncateBuf } from './utils.js';
9
9
  import { createTask, appendTaskOutput, settleTask } from '../agent-task.js';
10
- import { shellSpawnArgs } from './shell.js';
10
+ import { shellSpawnArgs, getShell } from './shell.js';
11
11
 
12
12
  // kimi-code's timeout policy (agent-core-v2/agent/tools/os/bash/bash.ts).
13
13
  export const DEFAULT_TIMEOUT_S = 60;
@@ -127,7 +127,13 @@ function killTree(child, opts = {}) {
127
127
 
128
128
  export const spec = {
129
129
  name: 'Bash',
130
- description: 'Run a shell command via pwsh. Returns combined stdout+stderr. timeout default 60s (max 300s); run_in_background detaches and returns a task id.',
130
+ // The shell is named at runtime, not baked in: the model is told which syntax it is writing against, and on a POSIX
131
+ // box "via pwsh" was both wrong and an invitation to emit PowerShell.
132
+ get description() {
133
+ const shell = getShell();
134
+ const which = shell ? (shell.edition === 'posix' ? 'the POSIX shell' : shell.name) : 'the system shell';
135
+ return `Run a shell command via ${which}. Returns combined stdout+stderr. timeout default 60s (max 300s); run_in_background detaches and returns a task id.`;
136
+ },
131
137
  parameters: {
132
138
  type: 'object',
133
139
  properties: {
@@ -69,6 +69,25 @@ function whichSync(name) {
69
69
  * cmd's, not PowerShell's.
70
70
  */
71
71
  export function detectShell() {
72
+ // POSIX FIRST, AND ON ITS OWN PATH.
73
+ //
74
+ // The PowerShell probe used to run on EVERY platform, so on Linux or macOS it looked for `pwsh`, then
75
+ // `powershell`, then `cmd.exe`, found none of them, and reported "install PowerShell 7+" — advice that is wrong on
76
+ // a machine whose shell has been there all along. A Unix box is never looking for PowerShell and a Windows box is
77
+ // never looking for bash, so the two branches do not overlap at all.
78
+ if (process.platform !== 'win32') {
79
+ // `$SHELL` is the user's own choice and is what a terminal would use. `/bin/sh` is the floor: it is on every
80
+ // POSIX system, so the tool survives a stripped PATH the same way `cmd.exe` does on Windows.
81
+ const fromEnv = process.env.SHELL ? whichSync(process.env.SHELL) : null;
82
+ if (fromEnv) return { path: fromEnv, name: fromEnv.split('/').pop(), edition: 'posix' };
83
+ for (const name of ['bash', 'zsh', 'sh']) {
84
+ const path = whichSync(name);
85
+ if (path) return { path, name, edition: 'posix' };
86
+ }
87
+ const sh = fs.existsSync('/bin/sh') ? '/bin/sh' : null;
88
+ return sh ? { path: sh, name: 'sh', edition: 'posix' } : null;
89
+ }
90
+
72
91
  for (const name of SHELL_CANDIDATES) {
73
92
  const path = whichSync(name);
74
93
  if (path) {
@@ -128,8 +147,9 @@ export function shellSpawnArgs(command, opts = {}) {
128
147
  const shell = getShell();
129
148
  if (!shell) {
130
149
  throw new Error(
131
- 'No shell found (tried pwsh, powershell, and cmd.exe). '
132
- + 'Install PowerShell 7+ or add it to PATH, then retry.',
150
+ process.platform === 'win32'
151
+ ? 'No shell found (tried pwsh, powershell, and cmd.exe). Install PowerShell 7+ or add it to PATH, then retry.'
152
+ : 'No shell found (tried $SHELL, bash, zsh and sh). Install a POSIX shell, then retry.',
133
153
  );
134
154
  }
135
155
  if (shell.edition === 'cmd') {
@@ -138,6 +158,12 @@ export function shellSpawnArgs(command, opts = {}) {
138
158
  // quoting and we do not wrap it ourselves.
139
159
  return { bin: shell.path, args: ['/d', '/s', '/c', command], shell };
140
160
  }
161
+ if (shell.edition === 'posix') {
162
+ // `-c` takes the command as one string, which is what every POSIX shell expects. There is no `-NoProfile`
163
+ // equivalent and none is wanted: a user's rc file is part of their environment, and the Windows flags exist to
164
+ // suppress a PowerShell banner that has no counterpart here.
165
+ return { bin: shell.path, args: ['-c', command], shell };
166
+ }
141
167
  const args = ['-NoProfile'];
142
168
  if (!opts.interactive) args.push('-NonInteractive');
143
169
  args.push('-Command', command);