@noldova/terminal 0.0.15 → 0.0.17

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.
Files changed (51) hide show
  1. package/module.json +2 -2
  2. package/package.json +9 -9
  3. package/src/enums/mark-kind.js +1 -0
  4. package/src/enums/shell-family.js +1 -0
  5. package/src/exceptions/command-exited.exception.js +2 -1
  6. package/src/models/hosted-terminal.js +20 -9
  7. package/src/models/one-shot-launch.js +7 -4
  8. package/src/models/right-prompt-span.js +14 -0
  9. package/src/models/terminal-prompt.js +5 -5
  10. package/src/models/typed-line.js +2 -2
  11. package/src/resources.js +263 -13
  12. package/src/services/agent-terminal.js +7 -5
  13. package/src/services/command-describer.js +35 -33
  14. package/src/services/command-runner.js +5 -3
  15. package/src/services/command-tracker.js +17 -2
  16. package/src/services/completion-source.js +62 -30
  17. package/src/services/escape-sequence-tracker.js +1 -1
  18. package/src/services/escape-stripper.js +10 -7
  19. package/src/services/file-system.reader.js +2 -1
  20. package/src/services/fish-shell-adapter.js +90 -0
  21. package/src/services/folder-change-line.js +2 -0
  22. package/src/services/line-process.js +3 -2
  23. package/src/services/log-file.js +4 -3
  24. package/src/services/mac-environment.js +5 -2
  25. package/src/services/mark-scanner.js +5 -5
  26. package/src/services/output-batcher.js +1 -1
  27. package/src/services/output-log.js +2 -1
  28. package/src/services/posix-shell-adapter.js +3 -2
  29. package/src/services/printed-text.js +3 -2
  30. package/src/services/pseudo-terminal.js +1 -1
  31. package/src/services/registry-export.js +5 -5
  32. package/src/services/resize-wakeup.js +1 -1
  33. package/src/services/saved-terminals.js +2 -1
  34. package/src/services/shell-adapters.js +3 -1
  35. package/src/services/shell-command.js +7 -2
  36. package/src/services/shell-integration.js +32 -14
  37. package/src/services/shell-locator.js +27 -13
  38. package/src/services/shell-words.js +5 -5
  39. package/src/services/terminal-history-segment.js +1 -1
  40. package/src/services/terminal-host.js +84 -43
  41. package/src/services/terminal-line-reader.js +9 -3
  42. package/src/services/terminal-mirror.js +12 -11
  43. package/src/services/terminal-session.js +24 -8
  44. package/src/services/wsl-distribution.reader.js +5 -2
  45. package/src/services/wsl-shell-adapter.js +6 -2
  46. package/src/tools/models/output-excerpt.js +2 -1
  47. package/src/tools/models/terminal-output-report.js +4 -2
  48. package/src/tools/models/tool-call-context.js +2 -1
  49. package/src/tools/services/terminal-access-policy.js +4 -2
  50. package/src/tools/services/terminal-text-escaper.js +4 -2
  51. package/src/tools/services/terminal-write-tool.js +5 -3
@@ -99,8 +99,10 @@ export class TerminalHost {
99
99
  await this.restartAsync(TerminalRestartParams.fromJson(payload), client);
100
100
  return null;
101
101
  case TerminalNames.sessionsMethod:
102
- return new TerminalSessions(this.terminals.filter(t => t.isReachedBy(client.connection, client.isApp))
103
- .map(t => t.session.toSummary(t.folder, !Object.isNull(t.ownerSessionId), t.isPanelTerminal, t.owner, t.opensTab, t.name, t.shell.name))).toJson();
102
+ return new TerminalSessions(this.terminals
103
+ .filter(t => t.isReachedBy(client.connection, client.isApp))
104
+ .map(t => t.session.toSummary(t.folder, !Object.isNull(t.ownerSessionId), t.isPanelTerminal, t.owner, t.opensTab, t.name, t.shell.name)))
105
+ .toJson();
104
106
  case TerminalNames.attachMethod:
105
107
  return (await this.attachAsync(TerminalPaneReference.fromJson(payload), client)).toJson();
106
108
  case TerminalNames.inputMethod:
@@ -132,7 +134,8 @@ export class TerminalHost {
132
134
  await this.closeOwnerAsync(TerminalOwnerReference.fromJson(payload).owner, client);
133
135
  return null;
134
136
  case TerminalNames.launchMethod:
135
- return (await this.trackLaunchAsync(this.trackAsync(this.launchAsync(TerminalLaunchParams.fromJson(payload), client), new TerminalOpening(null, client.connection)), client)).toJson();
137
+ return (await this.trackLaunchAsync(this.trackAsync(this.launchAsync(TerminalLaunchParams.fromJson(payload), client), new TerminalOpening(null, client.connection)), client))
138
+ .toJson();
136
139
  case TerminalNames.runMethod: {
137
140
  const params = TerminalRunParams.fromJson(payload);
138
141
  return (await this.runAfterClosesAsync(params, client)).toJson();
@@ -209,8 +212,8 @@ export class TerminalHost {
209
212
  for (const [agentSessionId, bound] of this.agentSessionOwners)
210
213
  if (bound === owner)
211
214
  this.agentSessionOwners.delete(agentSessionId);
212
- const isOwned = (terminal) => terminal.owner === owner
213
- && (Object.isNull(client) || terminal.isReachedBy(client.connection, client.isApp));
215
+ const isOwned = (terminal) => terminal.owner === owner &&
216
+ (Object.isNull(client) || terminal.isReachedBy(client.connection, client.isApp));
214
217
  const closed = Promise.withResolvers();
215
218
  this.closings.set(isOwned, closed);
216
219
  try {
@@ -352,7 +355,8 @@ export class TerminalHost {
352
355
  return launching;
353
356
  }
354
357
  async endConnectionAsync(connection, wasApp) {
355
- const isEnding = (terminal) => Object.isNull(terminal.connection) ? wasApp && !Object.isNull(terminal.owner)
358
+ const isEnding = (terminal) => Object.isNull(terminal.connection)
359
+ ? wasApp && !Object.isNull(terminal.owner)
356
360
  : terminal.connection === connection;
357
361
  const closed = Promise.withResolvers();
358
362
  this.closings.set(isEnding, closed);
@@ -420,16 +424,21 @@ export class TerminalHost {
420
424
  terminal.keepsAlive = params.keepsAlive;
421
425
  }
422
426
  if (!Object.isNull(params.command))
423
- void this.typeCommandAsync(terminal, params.command).then(t => {
427
+ void this.typeCommandAsync(terminal, params.command)
428
+ .then(t => {
424
429
  terminal.launchCommandOffset = terminal.session.text.endOffset;
425
- if (t !== TerminalRunOutcome.Typed || !terminal.session.commands.hasMarks || Object.isNull(terminal.session.commands.promptEndOffset) || terminal.keyCount !== 0)
430
+ if (t !== TerminalRunOutcome.Typed ||
431
+ !terminal.session.commands.hasMarks ||
432
+ Object.isNull(terminal.session.commands.promptEndOffset) ||
433
+ terminal.keyCount !== 0)
426
434
  this.endLaunchCommand(terminal, null);
427
- }).catch((error) => this.log.write(Resources.formatLaunchCommandFailed(terminal.session.sessionId, FailureText.read(error))));
435
+ })
436
+ .catch((error) => this.log.write(Resources.formatLaunchCommandFailed(terminal.session.sessionId, FailureText.read(error))));
428
437
  return new TerminalLaunched(terminal.session.sessionId, null, terminal.shell.kind, terminal.session.hasIntegration);
429
438
  }
430
439
  async typeCommandAsync(terminal, command, isPaste = false, waitsForFit = false, readRefusal = () => null, folderKeys = null) {
431
440
  const session = terminal.session;
432
- const typed = isPaste ? command.replace(Resources.leadingBlankLines, "") : command;
441
+ const typed = isPaste ? command.replace(Resources.leadingBlankLines, String.empty) : command;
433
442
  const pending = new AbortController();
434
443
  terminal.pendingCommand = pending;
435
444
  const signal = pending.signal;
@@ -456,16 +465,21 @@ export class TerminalHost {
456
465
  await session.changes.waitUntilAsync(() => session.hasExited || isReady(), Resources.shellStartMilliseconds, signal);
457
466
  const isEnded = () => signal.aborted || session.hasExited || this.sessions.get(session.sessionId) !== terminal || this.isClosing(terminal);
458
467
  const isBracketed = isPaste && !isEnded() && await session.isBracketedPasteAsync();
459
- const outcome = isEnded() ? TerminalRunOutcome.Ended
460
- : isPaste && ((waitsForFit ? terminal.keyCount !== keyCount : terminal.inputCount !== inputCount) || !Object.isNull(session.commands.promptEndOffset) && session.readPromptLine() !== "") ? TerminalRunOutcome.Input
461
- : isPaste && !isBracketed && typed.includes(Resources.tabKey) ? TerminalRunOutcome.Tab
468
+ const outcome = isEnded()
469
+ ? TerminalRunOutcome.Ended
470
+ : isPaste &&
471
+ ((waitsForFit ? terminal.keyCount !== keyCount : terminal.inputCount !== inputCount) ||
472
+ !Object.isNull(session.commands.promptEndOffset) && session.readPromptLine() !== String.empty)
473
+ ? TerminalRunOutcome.Input
474
+ : isPaste && !isBracketed && typed.includes(Resources.tabKey)
475
+ ? TerminalRunOutcome.Tab
462
476
  : readRefusal() ?? TerminalRunOutcome.Typed;
463
477
  if (outcome === TerminalRunOutcome.Typed && !Object.isNull(folderKeys))
464
478
  session.sendFolder(folderKeys, command);
465
479
  else if (outcome === TerminalRunOutcome.Typed) {
466
480
  const text = isPaste ? TerminalHost.toPaste(typed, isBracketed) : `${command}${Resources.enterKey}`;
467
481
  terminal.typedEnds = isBracketed || !isPaste ? 1 : text.split(Resources.enterKey).length - 1;
468
- terminal.paste = isBracketed ? typed.replace(Resources.whitespace, "") : "";
482
+ terminal.paste = isBracketed ? typed.replace(Resources.whitespace, String.empty) : String.empty;
469
483
  if (isPaste)
470
484
  terminal.recordRunInput();
471
485
  session.write(text);
@@ -481,7 +495,7 @@ export class TerminalHost {
481
495
  this.audience.publish(this.events.launchCommandEnded, terminal, new TerminalCommandEnded(terminal.session.sessionId, exitCode).toJson());
482
496
  }
483
497
  static findEchoEnd(output, paste) {
484
- const start = output.replace(Resources.whitespace, "").indexOf(paste);
498
+ const start = output.replace(Resources.whitespace, String.empty).indexOf(paste);
485
499
  if (start < 0)
486
500
  return null;
487
501
  let index = 0;
@@ -491,7 +505,7 @@ export class TerminalHost {
491
505
  return index;
492
506
  }
493
507
  static toPaste(command, isBracketed) {
494
- const text = command.replace(Resources.trailingLineBreaks, "").replace(Resources.pasteLineBreaks, Resources.enterKey);
508
+ const text = command.replace(Resources.trailingLineBreaks, String.empty).replace(Resources.pasteLineBreaks, Resources.enterKey);
495
509
  return isBracketed ? `${Resources.pasteStart}${text}${Resources.pasteEnd}${Resources.enterKey}` : `${text}${Resources.enterKey}`;
496
510
  }
497
511
  async runAfterClosesAsync(params, client) {
@@ -515,14 +529,17 @@ export class TerminalHost {
515
529
  this.ensureRunning();
516
530
  if (!Object.isNull(params.sessionId)) {
517
531
  const target = this.findRunTarget(params.sessionId, params, client);
518
- return Object.isNull(target) || this.isClosing(target) ? new TerminalRunResult(params.sessionId, TerminalRunOutcome.Ended)
532
+ return Object.isNull(target) || this.isClosing(target)
533
+ ? new TerminalRunResult(params.sessionId, TerminalRunOutcome.Ended)
519
534
  : this.runIntoAsync(params, target, client);
520
535
  }
521
536
  const place = new TerminalOpening(params.owner, client.connection);
522
537
  if (this.isClosing(place))
523
538
  return this.endForClose(params, client);
524
- const isOwned = (t) => t.owner === params.owner && t.isReachedBy(client.connection, client.isApp)
525
- && (params.shellKinds.length === 0 || params.shellKinds.includes(t.shell.kind)) && !t.isRunStarting;
539
+ const isOwned = (t) => t.owner === params.owner &&
540
+ t.isReachedBy(client.connection, client.isApp) &&
541
+ (params.shellKinds.length === 0 || params.shellKinds.includes(t.shell.kind)) &&
542
+ !t.isRunStarting;
526
543
  const wanted = Object.isNull(params.folder) ? null : await this.toRunFolderKeyAsync(params.folder);
527
544
  let inFolder = await this.findInFolderAsync(wanted, isOwned);
528
545
  const newest = this.terminals.findLast(t => isOwned(t) && inFolder?.has(t) !== false && (t.isFree || t.isDrawingPrompt));
@@ -533,7 +550,8 @@ export class TerminalHost {
533
550
  let terminal = this.terminals.findLast(t => isOwned(t) && inFolder?.has(t) !== false && t.isFree);
534
551
  const isNew = Object.isUndefined(terminal);
535
552
  if (Object.isUndefined(terminal)) {
536
- const shell = params.shellKinds.length === 0 ? await this.findFallbackShellAsync(params.shellId)
553
+ const shell = params.shellKinds.length === 0
554
+ ? await this.findFallbackShellAsync(params.shellId)
537
555
  : await this.findShellOfKindsAsync(params.shellKinds, params.shellId);
538
556
  if (Object.isUndefined(shell) && params.shellKinds.length > 0)
539
557
  return new TerminalRunResult(null, TerminalRunOutcome.NoShell);
@@ -564,8 +582,13 @@ export class TerminalHost {
564
582
  }
565
583
  findRunTarget(sessionId, params, client) {
566
584
  const terminal = this.sessions.get(sessionId);
567
- return Object.isUndefined(terminal) || !terminal.isReachedBy(client.connection, client.isApp) || !Object.isNull(terminal.ownerSessionId)
568
- || terminal.owner !== params.owner || params.shellKinds.length > 0 && !params.shellKinds.includes(terminal.shell.kind) ? null : terminal;
585
+ return Object.isUndefined(terminal) ||
586
+ !terminal.isReachedBy(client.connection, client.isApp) ||
587
+ !Object.isNull(terminal.ownerSessionId) ||
588
+ terminal.owner !== params.owner ||
589
+ params.shellKinds.length > 0 && !params.shellKinds.includes(terminal.shell.kind)
590
+ ? null
591
+ : terminal;
569
592
  }
570
593
  async runIntoAsync(params, terminal, client) {
571
594
  const session = terminal.session;
@@ -573,8 +596,10 @@ export class TerminalHost {
573
596
  return new TerminalRunResult(session.sessionId, TerminalRunOutcome.Busy);
574
597
  terminal.isRunStarting = true;
575
598
  try {
576
- const isBusy = () => !terminal.isFree && !session.hasExited && this.sessions.get(session.sessionId) === terminal
577
- && !this.isClosing(terminal);
599
+ const isBusy = () => !terminal.isFree &&
600
+ !session.hasExited &&
601
+ this.sessions.get(session.sessionId) === terminal &&
602
+ !this.isClosing(terminal);
578
603
  if (isBusy() && terminal.isDrawingPrompt) {
579
604
  await this.waitForPromptAsync(terminal);
580
605
  if (this.sessions.get(session.sessionId) !== terminal)
@@ -649,7 +674,7 @@ export class TerminalHost {
649
674
  run.isAfterEndMark = isEndMark;
650
675
  run.exitCode = exitCode;
651
676
  run.offset = text.endOffset;
652
- run.paste = "";
677
+ run.paste = String.empty;
653
678
  run.remainingEnds--;
654
679
  if (run.remainingEnds === 0)
655
680
  this.endRun(terminal, TerminalRunEndedBy.Command, exitCode);
@@ -670,8 +695,13 @@ export class TerminalHost {
670
695
  const runIds = Object.isUndefined(terminal) ? [] : [...terminal.runs.keys()];
671
696
  const index = runIds.indexOf(params.runId);
672
697
  const run = terminal?.runs.get(params.runId);
673
- if (Object.isUndefined(terminal) || Object.isUndefined(run) || !client.isApp || !terminal.isReachedBy(client.connection, true) || !Object.isNull(terminal.ownerSessionId)
674
- || terminal.session.hasExited || terminal.isRestarting)
698
+ if (Object.isUndefined(terminal) ||
699
+ Object.isUndefined(run) ||
700
+ !client.isApp ||
701
+ !terminal.isReachedBy(client.connection, true) ||
702
+ !Object.isNull(terminal.ownerSessionId) ||
703
+ terminal.session.hasExited ||
704
+ terminal.isRestarting)
675
705
  return null;
676
706
  const nextId = runIds[index + 1];
677
707
  const next = Object.isUndefined(nextId) ? undefined : terminal.runs.get(nextId);
@@ -742,8 +772,10 @@ export class TerminalHost {
742
772
  for (const terminal of saved) {
743
773
  if (this.sessions.has(terminal.sessionId))
744
774
  continue;
745
- if (terminal.isPanelTerminal && (terminal !== panel || this.panelOpenings.has(client.connection)
746
- || this.terminals.some(t => t.isPanelTerminal && t.connection === client.connection)))
775
+ if (terminal.isPanelTerminal &&
776
+ (terminal !== panel ||
777
+ this.panelOpenings.has(client.connection) ||
778
+ this.terminals.some(t => t.isPanelTerminal && t.connection === client.connection)))
747
779
  this.saved.forget(terminal.sessionId);
748
780
  else
749
781
  restoring.push(terminal);
@@ -777,7 +809,7 @@ export class TerminalHost {
777
809
  : hasFolder ? Resources.formatRestoredShellNote(saved.shellId) : Resources.formatRestoredShellAndFolderNote(saved.shellId, saved.folder);
778
810
  const terminal = await this.openTerminalAsync(hasShell ? saved.shellId : fallback, hasFolder ? saved.folder : this.homeFolder, params.size, null, params.storedLimit, note, saved.isPanelTerminal, saved.owner, client, saved.sessionId, null, false, saved.opensTab, saved);
779
811
  const name = saved.name;
780
- terminal.name = !Object.isNull(name) && name.trim() !== "" && name.length <= ProtocolResources.maximumTitleLength ? name : null;
812
+ terminal.name = !Object.isNull(name) && name.trim() !== String.empty && name.length <= ProtocolResources.maximumTitleLength ? name : null;
781
813
  if (!hasShell)
782
814
  this.log.write(Resources.formatRestoredShellMissing(saved.sessionId, saved.shellId));
783
815
  if (!hasFolder)
@@ -852,7 +884,7 @@ export class TerminalHost {
852
884
  }
853
885
  }
854
886
  toFolderKey(folder) {
855
- const trimmed = folder.replace(Resources.trailingFolderSeparators, "");
887
+ const trimmed = folder.replace(Resources.trailingFolderSeparators, String.empty);
856
888
  return Object.isNull(this.windowsPty) ? trimmed : trimmed.replaceAll(Resources.posixRoot, Resources.windowsSeparator).toLowerCase();
857
889
  }
858
890
  async closeAsync(sessionId, client) {
@@ -868,19 +900,22 @@ export class TerminalHost {
868
900
  async completeAsync(params, client) {
869
901
  const terminal = this.findHosted(params.sessionId, client);
870
902
  const keys = params.asksShell && this.completions.followsCommand(terminal.shell, params.line, params.cursor)
871
- ? this.completions.adapterOf(terminal.shell)?.optionsKeys ?? undefined : undefined;
903
+ ? this.completions.adapterOf(terminal.shell)?.optionsKeys ?? undefined
904
+ : undefined;
872
905
  const [completion, reply] = await Promise.all([
873
906
  this.completions.completeAsync(params.sessionId, terminal.shell, params.line, params.cursor, terminal.session.cwd ?? terminal.folder, this.readCompletionEnvironment(terminal), terminal.session.shellNames, terminal.session.shellBuiltins),
874
907
  Object.isUndefined(keys) ? Promise.resolve(ShellOptionsReply.none) : terminal.session.askOptionsAsync(keys)
875
908
  ]);
876
909
  if (reply.isTimedOut)
877
910
  return new TerminalCompletion(params.cursor, [], true);
878
- return Object.isNull(reply.options) ? completion
911
+ return Object.isNull(reply.options)
912
+ ? completion
879
913
  : this.completions.addOptions(completion, terminal.shell, params.line, params.cursor, reply.options);
880
914
  }
881
915
  async findCommandsAsync(params, client) {
882
916
  const terminal = this.findHosted(params.sessionId, client);
883
- const shellNames = terminal.session.isShellPathReset || terminal.session.isShellModulePathReset ? null
917
+ const shellNames = terminal.session.isShellPathReset || terminal.session.isShellModulePathReset
918
+ ? null
884
919
  : terminal.session.shellNames;
885
920
  const environment = this.readCompletionEnvironment(terminal);
886
921
  return this.completions.adapterOf(terminal.shell)?.looksUpMissing === true
@@ -981,7 +1016,8 @@ export class TerminalHost {
981
1016
  const terminal = this.findHosted(reference.sessionId, client);
982
1017
  terminal.paneClients.set(reference.paneId, client);
983
1018
  const attachment = await terminal.session.attachAsync(reference.paneId, Object.isNull(terminal.ownerSessionId) && this.isSuggesting()
984
- ? this.commandHistory.list(terminal.shell.id) : []);
1019
+ ? this.commandHistory.list(terminal.shell.id)
1020
+ : []);
985
1021
  if (!terminal.paneClients.has(reference.paneId))
986
1022
  terminal.session.detach(reference.paneId);
987
1023
  return attachment;
@@ -1000,11 +1036,11 @@ export class TerminalHost {
1000
1036
  async writeAsync(params, client) {
1001
1037
  const terminal = this.findHosted(params.sessionId, client);
1002
1038
  const session = terminal.session;
1003
- const text = params.text.replace(Resources.pasteMarkers, "").replace(Resources.pasteLineBreaks, Resources.enterKey);
1039
+ const text = params.text.replace(Resources.pasteMarkers, String.empty).replace(Resources.pasteLineBreaks, Resources.enterKey);
1004
1040
  const isBracketed = text.length > 0 && !session.hasExited && await session.isBracketedPasteAsync();
1005
1041
  if (session.hasExited || this.sessions.get(params.sessionId) !== terminal)
1006
1042
  throw new SessionNotFoundException(params.sessionId);
1007
- const written = `${isBracketed ? `${Resources.pasteStart}${text}${Resources.pasteEnd}` : text}${params.isEnter ? Resources.enterKey : ""}`;
1043
+ const written = `${isBracketed ? `${Resources.pasteStart}${text}${Resources.pasteEnd}` : text}${params.isEnter ? Resources.enterKey : String.empty}`;
1008
1044
  if (written.length === 0)
1009
1045
  return;
1010
1046
  terminal.recordInput(false, written, true);
@@ -1033,12 +1069,16 @@ export class TerminalHost {
1033
1069
  throw new SessionNotFoundException(params.sessionId);
1034
1070
  const typedAt = session.text.endOffset;
1035
1071
  const prompts = session.prompts;
1036
- const isMoved = () => Object.isNull(folderKeys) ? (session.commands.promptEndOffset ?? typedAt) > typedAt
1072
+ const isMoved = () => Object.isNull(folderKeys)
1073
+ ? (session.commands.promptEndOffset ?? typedAt) > typedAt
1037
1074
  : session.prompts > prompts && !Object.isNull(session.commands.promptEndOffset);
1038
1075
  await session.changes.waitUntilAsync(() => session.hasExited || this.sessions.get(params.sessionId) !== terminal || isMoved(), Resources.folderChangeWaitMilliseconds, new AbortController().signal);
1039
1076
  if (session.hasExited || this.sessions.get(params.sessionId) !== terminal)
1040
1077
  throw new SessionNotFoundException(params.sessionId);
1041
- const isChanged = Object.isNull(folderKeys) ? session.commands.lastCommand?.exitCode === 0 : session.cwd !== before || session.cwd === params.folder;
1078
+ const isChanged = Object.isNull(folderKeys)
1079
+ ? session.commands.lastCommand?.exitCode === 0
1080
+ : session.cwd !== before ||
1081
+ session.cwd === params.folder;
1042
1082
  return new TerminalFolderChanged(isMoved() && isChanged ? session.cwd : null, true);
1043
1083
  }
1044
1084
  static readMoveRefusal(terminal) {
@@ -1047,7 +1087,7 @@ export class TerminalHost {
1047
1087
  return TerminalRunOutcome.NoShell;
1048
1088
  if (Object.isNull(commands.promptEndOffset))
1049
1089
  return TerminalRunOutcome.Busy;
1050
- return terminal.hasUnreadInput || terminal.session.readPromptLine() !== "" ? TerminalRunOutcome.Input : null;
1090
+ return terminal.hasUnreadInput || terminal.session.readPromptLine() !== String.empty ? TerminalRunOutcome.Input : null;
1051
1091
  }
1052
1092
  resize(params, client) {
1053
1093
  const terminal = this.findHosted(params.sessionId, client);
@@ -1139,9 +1179,10 @@ export class TerminalHost {
1139
1179
  }
1140
1180
  async startShellAsync(shell, folder, size, note, variables, deferredSessionId = null) {
1141
1181
  const environment = this.withVariables(await this.readEnvironmentAsync(), variables);
1142
- const printedNote = Object.isNull(note) || Object.isNull(this.windowsPty) ? null
1143
- : note.split(Resources.lineBreaks).map(t => TerminalSession.toNote(t)).join("");
1144
- const launch = await this.integration.prepareAsync(shell, randomUUID().replaceAll(Resources.terminalHostUuidSeparator, ""), environment, printedNote);
1182
+ const printedNote = Object.isNull(note) || Object.isNull(this.windowsPty)
1183
+ ? null
1184
+ : note.split(Resources.lineBreaks).map(t => TerminalSession.toNote(t)).join(String.empty);
1185
+ const launch = await this.integration.prepareAsync(shell, randomUUID().replaceAll(Resources.terminalHostUuidSeparator, String.empty), environment, printedNote);
1145
1186
  const inheritsCursor = !Object.isNull(note) && !launch.printsNote;
1146
1187
  let process;
1147
1188
  let deferred = null;
@@ -25,7 +25,7 @@ export class TerminalLineReader {
25
25
  }
26
26
  readRuns(line, end) {
27
27
  const runs = [];
28
- let text = "";
28
+ let text = String.empty;
29
29
  let length = 0;
30
30
  let foreground = ProtocolResources.defaultColor;
31
31
  let background = ProtocolResources.defaultColor;
@@ -64,8 +64,14 @@ export class TerminalLineReader {
64
64
  return end;
65
65
  }
66
66
  isBlank(cell) {
67
- return !Object.isUndefined(cell) && ((cell.getCode() === 0 && cell.isAttributeDefault()) || (cell.getChars().trim().length === 0 && cell.isBgDefault() &&
68
- !cell.isInverse() && !cell.isUnderline() && !cell.isStrikethrough() && !cell.isOverline()));
67
+ return !Object.isUndefined(cell) &&
68
+ ((cell.getCode() === 0 && cell.isAttributeDefault()) ||
69
+ (cell.getChars().trim().length === 0 &&
70
+ cell.isBgDefault() &&
71
+ !cell.isInverse() &&
72
+ !cell.isUnderline() &&
73
+ !cell.isStrikethrough() &&
74
+ !cell.isOverline()));
69
75
  }
70
76
  static foregroundOf(cell) {
71
77
  if (cell.isFgDefault())
@@ -24,7 +24,7 @@ export class TerminalMirror {
24
24
  anchors = new Map();
25
25
  writtenOffset = 0;
26
26
  removedLines = 0;
27
- currentTitle = "";
27
+ currentTitle = String.empty;
28
28
  oldest = null;
29
29
  oldestMarker = null;
30
30
  inputStart = undefined;
@@ -56,15 +56,15 @@ export class TerminalMirror {
56
56
  this.terminal.write(data, () => this.listener.onParsed(offset));
57
57
  }
58
58
  resize(size) {
59
- this.terminal.write("", () => this.resizeNow(size));
59
+ this.terminal.write(String.empty, () => this.resizeNow(size));
60
60
  }
61
61
  snapshotAsync() {
62
62
  const offset = this.writtenOffset;
63
63
  const pending = this.tracker.pending;
64
- return new Promise(resolve => this.terminal.write("", () => resolve(new MirrorSnapshot(this.serializer.serialize({ scrollback: Resources.scrollback }), pending, offset, this.readInputStart()))));
64
+ return new Promise(resolve => this.terminal.write(String.empty, () => resolve(new MirrorSnapshot(this.serializer.serialize({ scrollback: Resources.scrollback }), pending, offset, this.readInputStart()))));
65
65
  }
66
66
  startInput() {
67
- this.terminal.write("", () => {
67
+ this.terminal.write(String.empty, () => {
68
68
  this.inputStart?.dispose();
69
69
  const buffer = this.terminal.buffer.active;
70
70
  this.inputStart = buffer.type === Resources.normalBufferType ? this.terminal.registerMarker(0) : undefined;
@@ -73,7 +73,7 @@ export class TerminalMirror {
73
73
  });
74
74
  }
75
75
  markRightPrompt() {
76
- this.terminal.write("", () => {
76
+ this.terminal.write(String.empty, () => {
77
77
  const start = this.inputStart;
78
78
  const buffer = this.terminal.buffer.active;
79
79
  if (!Object.isUndefined(start) && !start.isDisposed && buffer.baseY + buffer.cursorY === start.line)
@@ -81,7 +81,7 @@ export class TerminalMirror {
81
81
  });
82
82
  }
83
83
  endInput() {
84
- return new Promise(resolve => this.terminal.write("", () => {
84
+ return new Promise(resolve => this.terminal.write(String.empty, () => {
85
85
  const line = this.isDisposed ? null : this.readInput();
86
86
  this.inputStart?.dispose();
87
87
  this.inputStart = undefined;
@@ -89,14 +89,15 @@ export class TerminalMirror {
89
89
  }));
90
90
  }
91
91
  isBracketedPasteAsync() {
92
- return new Promise(resolve => this.terminal.write("", () => resolve(this.terminal.modes.bracketedPasteMode)));
92
+ return new Promise(resolve => this.terminal.write(String.empty, () => resolve(this.terminal.modes.bracketedPasteMode)));
93
93
  }
94
94
  anchorAsync(at) {
95
- return new Promise(resolve => this.terminal.write("", () => {
95
+ return new Promise(resolve => this.terminal.write(String.empty, () => {
96
96
  const buffer = this.terminal.buffer.normal;
97
97
  const cursor = buffer.baseY + buffer.cursorY;
98
98
  const index = Object.isNull(at) ? cursor : at.index;
99
- const marker = Object.isNull(index) || this.terminal.buffer.active.type !== Resources.normalBufferType ? undefined
99
+ const marker = Object.isNull(index) || this.terminal.buffer.active.type !== Resources.normalBufferType
100
+ ? undefined
100
101
  : this.terminal.registerMarker(index - cursor);
101
102
  const line = new MirrorLine(at?.number ?? this.removedLines + cursor, marker);
102
103
  if (!Object.isUndefined(marker)) {
@@ -107,7 +108,7 @@ export class TerminalMirror {
107
108
  }));
108
109
  }
109
110
  readLinesAsync(start, end, maximum) {
110
- return new Promise(resolve => this.terminal.write("", () => resolve(this.readLines(start.number, end?.number ?? null, maximum))));
111
+ return new Promise(resolve => this.terminal.write(String.empty, () => resolve(this.readLines(start.number, end?.number ?? null, maximum))));
111
112
  }
112
113
  dispose() {
113
114
  this.isDisposed = true;
@@ -119,7 +120,7 @@ export class TerminalMirror {
119
120
  if (Object.isUndefined(start) || start.isDisposed)
120
121
  return null;
121
122
  const buffer = this.terminal.buffer.normal;
122
- let text = "";
123
+ let text = String.empty;
123
124
  let line = buffer.getLine(start.line);
124
125
  for (let index = start.line; !Object.isUndefined(line); index++) {
125
126
  const next = buffer.getLine(index + 1);
@@ -147,7 +147,7 @@ export class TerminalSession {
147
147
  if (isKey)
148
148
  this.options.cancel();
149
149
  this.beforeInput();
150
- for (let lines = data.replace(Resources.bracketedPastes, "").split(Resources.lineBreaks).length - 1; lines > 0; lines--) {
150
+ for (let lines = data.replace(Resources.bracketedPastes, String.empty).split(Resources.lineBreaks).length - 1; lines > 0; lines--) {
151
151
  this.commands.enterLine();
152
152
  this.enteredAt = Date.now();
153
153
  }
@@ -173,10 +173,14 @@ export class TerminalSession {
173
173
  return true;
174
174
  }
175
175
  async askOptionsAsync(keys) {
176
- if (!this.hasIntegration || !this.commands.hasOptionsBinding || !this.commands.isWaitingAtPrompt || !Object.isNull(this.commands.runningCommand)
177
- || !Object.isNull(this.exit))
176
+ if (!this.hasIntegration ||
177
+ !this.commands.hasOptionsBinding ||
178
+ !this.commands.isWaitingAtPrompt ||
179
+ !Object.isNull(this.commands.runningCommand) ||
180
+ !Object.isNull(this.exit))
178
181
  return ShellOptionsReply.none;
179
- const reply = this.options.askAsync(this.hasAnsweredOptions ? Resources.optionsMilliseconds
182
+ const reply = this.options.askAsync(this.hasAnsweredOptions
183
+ ? Resources.optionsMilliseconds
180
184
  : Resources.firstOptionsMilliseconds);
181
185
  this.beforeInput();
182
186
  this.terminal.write(keys);
@@ -192,7 +196,14 @@ export class TerminalSession {
192
196
  const promptEnd = this.commands.promptEndOffset;
193
197
  if (Object.isNull(promptEnd) || !Object.isNull(this.commands.runningCommand))
194
198
  return null;
195
- return this.text.readSince(promptEnd).text.trim();
199
+ let line = String.empty;
200
+ let from = promptEnd;
201
+ for (const span of this.commands.rightPrompts)
202
+ if (span.start >= promptEnd && !Object.isNull(span.end)) {
203
+ line += this.text.readRange(from, span.start);
204
+ from = span.end;
205
+ }
206
+ return (line + this.text.readRange(from, this.text.endOffset)).trim();
196
207
  }
197
208
  resize(size) {
198
209
  if (size.equals(this.size))
@@ -374,7 +385,8 @@ export class TerminalSession {
374
385
  this.listener.onCwdChanged(new TerminalCwd(this.sessionId, cwd));
375
386
  }
376
387
  settleTypedLines(mark) {
377
- const command = mark.kind === MarkKind.OutputStart ? this.commands.runningCommand
388
+ const command = mark.kind === MarkKind.OutputStart
389
+ ? this.commands.runningCommand
378
390
  : mark.kind === MarkKind.CommandEnd ? this.commands.lastCommand : null;
379
391
  for (const line of this.typedLines) {
380
392
  if (!Object.isNull(command))
@@ -429,10 +441,13 @@ export class TerminalSession {
429
441
  this.wakeup?.stop();
430
442
  }
431
443
  endInput(startsCommand, command) {
432
- void this.mirror.endInput().then(t => {
444
+ void this.mirror
445
+ .endInput()
446
+ .then(t => {
433
447
  if (startsCommand && !Object.isNull(t))
434
448
  this.listener.onCommandStarted(new TerminalCommandStarted(this.sessionId, TerminalSession.toCommand(command) ?? t));
435
- }).catch((error) => this.listener.onFailed(this.sessionId, FailureText.read(error)));
449
+ })
450
+ .catch((error) => this.listener.onFailed(this.sessionId, FailureText.read(error)));
436
451
  }
437
452
  readInputEdge(mark) {
438
453
  return this.marksInput ? TerminalSession.toInputEdge(mark.kind) : null;
@@ -455,6 +470,7 @@ export class TerminalSession {
455
470
  return TerminalInputEdge.Continuation;
456
471
  case MarkKind.Property:
457
472
  case MarkKind.Options:
473
+ case MarkKind.RightPromptEnd:
458
474
  return null;
459
475
  default:
460
476
  return TerminalInputEdge.End;
@@ -27,7 +27,7 @@ export class WslDistributionReader {
27
27
  throw new WslDistributionsUnreadableException(error);
28
28
  throw error;
29
29
  }
30
- return WslDistributionReader.parse(output.replace(Resources.wslDistributionReaderByteOrderMark, ""));
30
+ return WslDistributionReader.parse(output.replace(Resources.wslDistributionReaderByteOrderMark, String.empty));
31
31
  }
32
32
  static parse(output) {
33
33
  const lines = output.split(Resources.wslDistributionReaderLineBreak).filter(t => t.length > 0);
@@ -37,7 +37,10 @@ export class WslDistributionReader {
37
37
  const others = [];
38
38
  for (const line of lines) {
39
39
  const [name, flag, ...rest] = line.split(Resources.wslDistributionReaderFieldSeparator);
40
- if (Object.isUndefined(name) || name.length === 0 || !Resources.base64Pattern.test(name) || rest.length > 0 ||
40
+ if (Object.isUndefined(name) ||
41
+ name.length === 0 ||
42
+ !Resources.base64Pattern.test(name) ||
43
+ rest.length > 0 ||
41
44
  (flag !== Resources.wslDefaultFlag && flag !== Resources.wslOtherFlag))
42
45
  throw new WslDistributionsUnreadableException();
43
46
  (flag === Resources.wslDefaultFlag ? defaults : others).push(Buffer.from(name, Resources.base64Encoding).toString(Resources.utf8Encoding));
@@ -47,10 +47,14 @@ export class WslShellAdapter {
47
47
  return value.includes(Resources.windowsSeparator) ? null : `${this.share}${value.replaceAll(Resources.posixSeparator, Resources.windowsSeparator)}`;
48
48
  }
49
49
  readSearchFolders(value) {
50
- return value.split(path.posix.delimiter).filter(t => path.posix.isAbsolute(t) && !t.includes(Resources.windowsSeparator)).map(t => `${this.share}${t.replaceAll(Resources.posixSeparator, Resources.windowsSeparator)}`);
50
+ return value
51
+ .split(path.posix.delimiter)
52
+ .filter(t => path.posix.isAbsolute(t) && !t.includes(Resources.windowsSeparator))
53
+ .map(t => `${this.share}${t.replaceAll(Resources.posixSeparator, Resources.windowsSeparator)}`);
51
54
  }
52
55
  toProgramCall(file, args) {
53
- return new ProgramCall(this.launcher, [Resources.wslDistributionArgument, this.distribution, Resources.wslExecArgument, ...[file, ...args].map(t => this.toShellPath(t))]);
56
+ return new ProgramCall(this.launcher, [Resources.wslDistributionArgument, this.distribution, Resources.wslExecArgument, ...[file, ...args]
57
+ .map(t => this.toShellPath(t))]);
54
58
  }
55
59
  toShellPath(value) {
56
60
  if (!value.toLowerCase().startsWith(`${this.share.toLowerCase()}${Resources.windowsSeparator}`))
@@ -5,6 +5,7 @@
5
5
  * This source code is licensed under the license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ import "@noldova/context.core";
8
9
  import { ArgumentValidator, Resources as ProtocolResources } from "@noldova/terminal.protocol";
9
10
  import { Resources } from "../../resources.js";
10
11
  export class OutputExcerpt {
@@ -13,7 +14,7 @@ export class OutputExcerpt {
13
14
  constructor(output, maximumCharacters, trimmedCharacters = 0) {
14
15
  ArgumentValidator.requireIntegerInRange(Resources.outputExcerptMaximumCharactersField, maximumCharacters, 1, Resources.maximumToolOutputCharacters);
15
16
  ArgumentValidator.requireIntegerInRange(Resources.outputExcerptTrimmedCharactersField, trimmedCharacters, 0, ProtocolResources.maximumOffset);
16
- const trimmed = trimmedCharacters === 0 ? "" : Resources.formatOmittedCharacters(trimmedCharacters);
17
+ const trimmed = trimmedCharacters === 0 ? String.empty : Resources.formatOmittedCharacters(trimmedCharacters);
17
18
  if (output.length <= maximumCharacters) {
18
19
  this.text = trimmed + output;
19
20
  this.omittedCharacters = trimmedCharacters;
@@ -15,7 +15,7 @@ export class TerminalOutputReport {
15
15
  type: Resources.jsonObjectType,
16
16
  additionalProperties: false,
17
17
  required: [
18
- Resources.terminalIdField, Resources.titleField, Resources.nameField, Resources.shellNameField, Resources.terminalOutputReportMethodField, Resources.terminalOutputReportStatusField,
18
+ Resources.terminalIdField, Resources.titleField, Resources.nameField, Resources.shellNameField, Resources.shellField, Resources.terminalOutputReportMethodField, Resources.terminalOutputReportStatusField,
19
19
  Resources.terminalOutputReportExitCodeField, Resources.cwdField, Resources.terminalOutputReportOutputField,
20
20
  Resources.terminalOutputReportOmittedCharactersField, Resources.terminalOutputReportLogFileField, Resources.terminalOutputReportIsAwaitingSecretField
21
21
  ],
@@ -24,6 +24,7 @@ export class TerminalOutputReport {
24
24
  [Resources.titleField]: { type: Resources.jsonStringType },
25
25
  [Resources.nameField]: { type: [Resources.jsonStringType, Resources.jsonNullType], description: Resources.nameFieldDescription },
26
26
  [Resources.shellNameField]: { type: Resources.jsonStringType, description: Resources.shellNameFieldDescription },
27
+ [Resources.shellField]: { type: Resources.jsonStringType, description: Resources.shellIdFieldDescription },
27
28
  [Resources.terminalOutputReportMethodField]: { type: [Resources.jsonStringType, Resources.jsonNullType], enum: [...Object.values(RunMethod), null], description: Resources.methodFieldDescription },
28
29
  [Resources.terminalOutputReportStatusField]: { type: [Resources.jsonStringType, Resources.jsonNullType], enum: [...Object.values(CommandStatus), null], description: Resources.statusFieldDescription },
29
30
  [Resources.terminalOutputReportExitCodeField]: { type: [Resources.jsonIntegerType, Resources.jsonNullType] },
@@ -44,7 +45,7 @@ export class TerminalOutputReport {
44
45
  this.excerpt = excerpt;
45
46
  this.cwd = cwd;
46
47
  }
47
- toResult(note = "") {
48
+ toResult(note = String.empty) {
48
49
  const status = Object.isNull(this.command) ? null : this.command.status;
49
50
  const exitCode = Object.isNull(this.command) ? null : this.command.exitCode;
50
51
  const isAwaitingSecret = this.access.terminal.isAwaitingSecret;
@@ -53,6 +54,7 @@ export class TerminalOutputReport {
53
54
  [Resources.titleField]: this.access.terminal.title,
54
55
  [Resources.nameField]: this.access.terminal.name,
55
56
  [Resources.shellNameField]: this.access.terminal.shellName,
57
+ [Resources.shellField]: this.access.terminal.shellId,
56
58
  [Resources.terminalOutputReportMethodField]: Object.isNull(this.command) ? null : this.command.method,
57
59
  [Resources.terminalOutputReportStatusField]: status,
58
60
  [Resources.terminalOutputReportExitCodeField]: exitCode,
@@ -57,6 +57,7 @@ export class ToolCallContext {
57
57
  }
58
58
  isInsideWorkingFolder(folder) {
59
59
  const path = relative(this.workingFolder, folder);
60
- return path === "" || (path !== Resources.toolCallContextParentFolder && !path.startsWith(`${Resources.toolCallContextParentFolder}${sep}`) && !isAbsolute(path));
60
+ return path === String.empty ||
61
+ (path !== Resources.toolCallContextParentFolder && !path.startsWith(`${Resources.toolCallContextParentFolder}${sep}`) && !isAbsolute(path));
61
62
  }
62
63
  }
@@ -17,7 +17,8 @@ export class TerminalAccessPolicy {
17
17
  }
18
18
  resolve(terminalId, context) {
19
19
  const terminal = this.terminals.find(terminalId);
20
- const access = Object.isUndefined(terminal) ? null
20
+ const access = Object.isUndefined(terminal)
21
+ ? null
21
22
  : TerminalAccessPolicy.accessTo(terminal, context, this.ownerOf(context));
22
23
  if (Object.isNull(access))
23
24
  throw ToolFailures.create(ToolFailureCode.Refused, Resources.notFoundCode, Resources.formatTerminalNotFound(terminalId));
@@ -38,7 +39,8 @@ export class TerminalAccessPolicy {
38
39
  }
39
40
  requireStill(access, context) {
40
41
  const terminal = this.terminals.find(access.terminal.id);
41
- const now = Object.isUndefined(terminal) ? null
42
+ const now = Object.isUndefined(terminal)
43
+ ? null
42
44
  : TerminalAccessPolicy.accessTo(terminal, context, this.ownerOf(context));
43
45
  if (Object.isNull(now) || now.isShared !== access.isShared)
44
46
  throw ToolFailures.create(ToolFailureCode.Stale, Resources.staleCode, Resources.formatTerminalStale(access.terminal.id));