@hasna/terminal 1.3.9 → 1.5.0

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/ai.js CHANGED
@@ -177,12 +177,32 @@ RULES:
177
177
  - NEVER install packages (npx, npm install, pip install, brew install). This is a READ-ONLY terminal.
178
178
  - NEVER modify source code (sed -i, codemod, awk with redirect). Only observe, never change.
179
179
  - Search src/ directory, NOT dist/ or node_modules/ for code queries.
180
- - For compound questions ("how many X and are they Y"), prefer ONE command that captures all info. Do NOT chain with &&.
181
180
  - Use exact file paths from the project context below. Do NOT guess paths.
182
181
  - For "what would break if I deleted X": use grep -rn "from.*X\\|import.*X\\|require.*X" src/ to find all importers.
183
182
  - For "find where X is defined": use grep -rn "export.*function X\\|export.*class X\\|export.*const X" src/
184
183
  - For "show me the code of function X": use grep -A 20 "function X" src/ to show the function body.
185
184
  - For conceptual questions about what code does: use cat on the relevant file, the AI summary will explain it.
185
+
186
+ COMPOUND QUESTIONS: For questions asking multiple things, prefer ONE command that captures all info. Extract multiple answers from a single output.
187
+ - "how many tests and do they pass" → bun test (extract count AND pass/fail from output)
188
+ - "what files changed and how many lines" → git log --stat -3 (shows files AND line counts)
189
+ - "what version of node and bun" → node -v && bun -v (only use && for trivial non-failing commands)
190
+ NEVER split into separate test runs or expensive commands chained with &&.
191
+
192
+ BLOCKED ALTERNATIVES: If your preferred command would require installing packages (npx, npm install), ALWAYS try a READ-ONLY alternative:
193
+ - Code quality analysis → grep -rn "TODO\\|FIXME\\|HACK\\|XXX" src/
194
+ - Linting → check if "lint" or "typecheck" exists in package.json scripts, run that
195
+ - Security scan → grep -rn "eval\\|exec\\|spawn\\|password\\|secret" src/
196
+ - Dependency audit → cat package.json | grep -A 50 dependencies
197
+ - Test coverage → bun test --coverage (or npm run test:coverage if available)
198
+ NEVER give up. Always try a grep/find/cat read-only alternative.
199
+
200
+ SEMANTIC MAPPING: When the user references a concept, search the file tree for RELATED terms:
201
+ - Look at directory names: src/agent/ likely contains "agentic" code
202
+ - Look at file names: lazy-executor.ts likely handles "lazy mode"
203
+ - When uncertain: grep -rn "keyword" src/ --include="*.ts" -l (list matching files)
204
+
205
+ ACTION vs CONCEPTUAL: If the prompt starts with "run", "execute", "check", "test", "build", "show output of" — ALWAYS generate an executable command. NEVER read README for action requests. Only read docs for "explain why", "what does X mean", "how was X designed".
186
206
  cwd: ${process.cwd()}
187
207
  shell: zsh / macOS${projectContext}${restrictionBlock}${contextBlock}`;
188
208
  }
package/dist/cli.js CHANGED
@@ -445,12 +445,27 @@ else if (args.length > 0) {
445
445
  console.error(`blocked: ${blocked}`);
446
446
  process.exit(1);
447
447
  }
448
- // Safety: warn about irreversible commands (kill, push, rm, etc.)
448
+ // Safety: when command is irreversible, try a safer read-only alternative
449
449
  if (isIrreversible(command)) {
450
- console.error(`⚠ IRREVERSIBLE: $ ${command}`);
451
- console.error(` This command may kill processes, push code, or delete data.`);
452
- console.error(` Run directly in your shell if you're sure: ${command}`);
453
- process.exit(1);
450
+ // Try to generate a safe alternative via AI
451
+ try {
452
+ const safeCommand = await translateToCommand(`${prompt} (IMPORTANT: use ONLY read-only commands like grep, find, cat, wc, ls. Do NOT use npx, install, kill, push, sed, or any modifying command.)`, perms, []);
453
+ if (!isIrreversible(safeCommand) && !checkPermissions(safeCommand, perms)) {
454
+ console.error(`$ ${safeCommand} (safe alternative)`);
455
+ command = safeCommand;
456
+ // Continue to execution below
457
+ }
458
+ else {
459
+ console.error(`⚠ BLOCKED: $ ${command}`);
460
+ console.error(` Run directly in your shell if you're sure.`);
461
+ process.exit(1);
462
+ }
463
+ }
464
+ catch {
465
+ console.error(`⚠ BLOCKED: $ ${command}`);
466
+ console.error(` Run directly in your shell if you're sure.`);
467
+ process.exit(1);
468
+ }
454
469
  }
455
470
  // Show what we're running
456
471
  console.error(`$ ${command}`);
@@ -514,8 +529,20 @@ else if (args.length > 0) {
514
529
  console.log(`No results found for: ${prompt}`);
515
530
  process.exit(0);
516
531
  }
532
+ // Combine stdout+stderr and try AI answer framing (for audit/lint/test commands)
517
533
  const combined = errStderr && errStdout.includes(errStderr.trim()) ? errStdout : errStdout + errStderr;
518
- console.log(stripNoise(stripAnsi(combined)).cleaned);
534
+ const errorClean = stripNoise(stripAnsi(combined)).cleaned;
535
+ if (errorClean.length > 20) {
536
+ try {
537
+ const processed = await processOutput(actualCmd, errorClean, prompt);
538
+ if (processed.aiProcessed) {
539
+ console.log(processed.summary);
540
+ process.exit(e.status ?? 1);
541
+ }
542
+ }
543
+ catch { }
544
+ }
545
+ console.log(errorClean);
519
546
  process.exit(e.status ?? 1);
520
547
  }
521
548
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/terminal",
3
- "version": "1.3.9",
3
+ "version": "1.5.0",
4
4
  "description": "Smart terminal wrapper for AI agents and humans — structured output, token compression, MCP server, natural language",
5
5
  "type": "module",
6
6
  "bin": {
package/src/ai.ts CHANGED
@@ -212,12 +212,32 @@ RULES:
212
212
  - NEVER install packages (npx, npm install, pip install, brew install). This is a READ-ONLY terminal.
213
213
  - NEVER modify source code (sed -i, codemod, awk with redirect). Only observe, never change.
214
214
  - Search src/ directory, NOT dist/ or node_modules/ for code queries.
215
- - For compound questions ("how many X and are they Y"), prefer ONE command that captures all info. Do NOT chain with &&.
216
215
  - Use exact file paths from the project context below. Do NOT guess paths.
217
216
  - For "what would break if I deleted X": use grep -rn "from.*X\\|import.*X\\|require.*X" src/ to find all importers.
218
217
  - For "find where X is defined": use grep -rn "export.*function X\\|export.*class X\\|export.*const X" src/
219
218
  - For "show me the code of function X": use grep -A 20 "function X" src/ to show the function body.
220
219
  - For conceptual questions about what code does: use cat on the relevant file, the AI summary will explain it.
220
+
221
+ COMPOUND QUESTIONS: For questions asking multiple things, prefer ONE command that captures all info. Extract multiple answers from a single output.
222
+ - "how many tests and do they pass" → bun test (extract count AND pass/fail from output)
223
+ - "what files changed and how many lines" → git log --stat -3 (shows files AND line counts)
224
+ - "what version of node and bun" → node -v && bun -v (only use && for trivial non-failing commands)
225
+ NEVER split into separate test runs or expensive commands chained with &&.
226
+
227
+ BLOCKED ALTERNATIVES: If your preferred command would require installing packages (npx, npm install), ALWAYS try a READ-ONLY alternative:
228
+ - Code quality analysis → grep -rn "TODO\\|FIXME\\|HACK\\|XXX" src/
229
+ - Linting → check if "lint" or "typecheck" exists in package.json scripts, run that
230
+ - Security scan → grep -rn "eval\\|exec\\|spawn\\|password\\|secret" src/
231
+ - Dependency audit → cat package.json | grep -A 50 dependencies
232
+ - Test coverage → bun test --coverage (or npm run test:coverage if available)
233
+ NEVER give up. Always try a grep/find/cat read-only alternative.
234
+
235
+ SEMANTIC MAPPING: When the user references a concept, search the file tree for RELATED terms:
236
+ - Look at directory names: src/agent/ likely contains "agentic" code
237
+ - Look at file names: lazy-executor.ts likely handles "lazy mode"
238
+ - When uncertain: grep -rn "keyword" src/ --include="*.ts" -l (list matching files)
239
+
240
+ ACTION vs CONCEPTUAL: If the prompt starts with "run", "execute", "check", "test", "build", "show output of" — ALWAYS generate an executable command. NEVER read README for action requests. Only read docs for "explain why", "what does X mean", "how was X designed".
221
241
  cwd: ${process.cwd()}
222
242
  shell: zsh / macOS${projectContext}${restrictionBlock}${contextBlock}`;
223
243
  }
package/src/cli.tsx CHANGED
@@ -424,12 +424,28 @@ else if (args.length > 0) {
424
424
  const blocked = checkPermissions(command, perms);
425
425
  if (blocked) { console.error(`blocked: ${blocked}`); process.exit(1); }
426
426
 
427
- // Safety: warn about irreversible commands (kill, push, rm, etc.)
427
+ // Safety: when command is irreversible, try a safer read-only alternative
428
428
  if (isIrreversible(command)) {
429
- console.error(`⚠ IRREVERSIBLE: $ ${command}`);
430
- console.error(` This command may kill processes, push code, or delete data.`);
431
- console.error(` Run directly in your shell if you're sure: ${command}`);
432
- process.exit(1);
429
+ // Try to generate a safe alternative via AI
430
+ try {
431
+ const safeCommand = await translateToCommand(
432
+ `${prompt} (IMPORTANT: use ONLY read-only commands like grep, find, cat, wc, ls. Do NOT use npx, install, kill, push, sed, or any modifying command.)`,
433
+ perms, []
434
+ );
435
+ if (!isIrreversible(safeCommand) && !checkPermissions(safeCommand, perms)) {
436
+ console.error(`$ ${safeCommand} (safe alternative)`);
437
+ command = safeCommand;
438
+ // Continue to execution below
439
+ } else {
440
+ console.error(`⚠ BLOCKED: $ ${command}`);
441
+ console.error(` Run directly in your shell if you're sure.`);
442
+ process.exit(1);
443
+ }
444
+ } catch {
445
+ console.error(`⚠ BLOCKED: $ ${command}`);
446
+ console.error(` Run directly in your shell if you're sure.`);
447
+ process.exit(1);
448
+ }
433
449
  }
434
450
 
435
451
  // Show what we're running
@@ -493,8 +509,19 @@ else if (args.length > 0) {
493
509
  console.log(`No results found for: ${prompt}`);
494
510
  process.exit(0);
495
511
  }
512
+ // Combine stdout+stderr and try AI answer framing (for audit/lint/test commands)
496
513
  const combined = errStderr && errStdout.includes(errStderr.trim()) ? errStdout : errStdout + errStderr;
497
- console.log(stripNoise(stripAnsi(combined)).cleaned);
514
+ const errorClean = stripNoise(stripAnsi(combined)).cleaned;
515
+ if (errorClean.length > 20) {
516
+ try {
517
+ const processed = await processOutput(actualCmd, errorClean, prompt);
518
+ if (processed.aiProcessed) {
519
+ console.log(processed.summary);
520
+ process.exit(e.status ?? 1);
521
+ }
522
+ } catch {}
523
+ }
524
+ console.log(errorClean);
498
525
  process.exit(e.status ?? 1);
499
526
  }
500
527
  }